# An Efficient Interface for the Integration of IoT Devices with Smart Grids
## 4 Architecture
In Algorithm 1, the tableID parameter, which is as an identifier in OSGP, takes the protocol header identifier from CoAP. The command that will be executed is defined by code and remains unchanged. The mapping still checks against PED because if the OSGP packet implements this attribute, it is mapped to a CoAP parameter. Algorithm 2 differs from the first algorithm because it writes in the full table, while Algorithm 1 use an offset (line 4) to select a region of the table. Algorithms 3 and 4 perform partial and full reads from a table, respectively. The mappings of fields are similar to the ones performed in the first two algorithms. Figure 5: Mapping a response packet from OSGP to CoAP. Figure 6: Example of a COIIoT-based system. ```
Input : CoAP PUT request packet Output : OSGP partial write request APDU
1 ifCoAP packet \(\leq\)OSGP APDU write requestthen
2 APDU.command \(\leftarrow\) write_partial;
3 APDU.tableID \(\leftarrow\) packet.id;
4 APDU.offset \(\leftarrow\) packet.payload_offset;
5 APDU.count_bytes \(\leftarrow\) packet.payload_size-3;
6 APDU.pedding_event \(\leftarrow\) NULL;
7 ifPED andpacket.options \(=\)Size1 then
8 APDU.pedding_event \(\leftarrow\) packet.options;
9 foreachpayload_data \(i\in\) packetdo
10 APDU.payload \(\leftarrow\) packet.payload;
11
12 returnOSGP partial write request APDU
```  
**Algorithm 1**PUT_to_Partial_Write_request  
```
Input : CoAP POST request packet Output : OSGP full write request APDU
1 ifCoAP packet \(\leq\)OSGP APDU write requestthen
2 APDU.command \(\leftarrow\) write_full;
3 APDU.tableID \(\leftarrow\) packet.id;
4 APDU.count_bytes \(\leftarrow\) packet.payload_length;
5 APDU.pedding_event \(\leftarrow\) NULL;
6 ifPED andpacket.options \(=\)Size1 then
7 APDU.pedding_event \(\leftarrow\) packet.options;
8
9 foreachpayload data \(i\in\) packetdo
10 APDU.payload \(\leftarrow\) packet.payload;
11
12 returnOSGP full write request APDU
```  
**Algorithm 2**POST_to_Full_Write_request  
```
Input : CoAP POST request packet Output : OSGP full write request APDU
13 ifCoAP packet \(\leq\)OSGP APDU write requestthen
14 APDU.command \(\leftarrow\) write_full;
15 APDU.tableID \(\leftarrow\) packet.id;
16 APDU.count_bytes \(\leftarrow\) packet.payload_length;
17 APDU.pedding_event \(\leftarrow\) NULL;
18
19 ifPED andpacket.options \(=\)Size1 then
20 APDU.pedding_event \(\leftarrow\) packet.options;
21
22
23 foreachpayload data \(i\in\) packetdo
24 APDU.payload \(\leftarrow\) packet.payload;
25
26 returnOSGP full write request APDU
```  
**Algorithm 3**GET_to_Partial_Read_request  
```
Input : CoAP GET request packet Output : OSGP partial read request APDU
17 ifCoAP packet payload length \(\leq\)OSGP APDU read requestthen
28 APDU.command \(\leftarrow\) partial_table_read;
30 APDU.tableID \(\leftarrow\) packet.id;
31 APDU.count_bytes \(\leftarrow\) packet.payload_length;
32 APDU.offset \(\leftarrow\) packet.payload.data;
33
34 returnOSGP partial read request APDU
```  
**Algorithm 4**GET_to_Partial_Read_request  
```
Input : CoAP GET request packet Output : OSGP partial read request APDU
18 APDU.command \(\leftarrow\) partial_table_read;
19 APDU.tableID \(\leftarrow\) packet.id;
20 APDU.count_bytes \(\leftarrow\) packet.payload_length;
21 APDU.offset \(\leftarrow\) packet.payload.data;
22
24


```
Input : CoAP GET request packet Output : OSGP full read request APDU
1 APDU.tableID \(\leftarrow\) packet.id;
2 APDU.command \(\leftarrow\) full_table_read;
3 return OSGP full read request APDU
```
**Algorithm 4**GET_to Full_Read_request

Algorithms A1-A4 (shown in Appendix A) describe the methods developed to map requests from the OSGP domain to the CoAP domain, while Algorithms A5-A8 (also shown in Appendix A) describe the methods designed to map the responses to the requests received by each domain. It is worth noting that the same method is used to map the responses for partial and full write requests, as well as for partial and full read requests.