# An Efficient Interface for the Integration of IoT Devices with Smart Grids
## 4 Architecture
```
Input : OSGP partial read request APDU Output : CoAP GET request packet
1 packet.version \(\leftarrow\) CoAP Version;
2 packet.request_type \(\leftarrow\) GET;
3 packet.hdr.code \(\leftarrow\) READ;
4 packet.id \(\leftarrow\) APDU.tableID;
5 packet.command \(\leftarrow\) APDU.command;
6 packet.options \(\gets\) 0;
7 packet.payload_length \(\leftarrow\) offset_size;
8 packet.payload.data \(\leftarrow\) APDU.offset;
9 return CoAP GET request packet
```  
**Algorithm A3**Partial_Read_to_GET_request  
Algorithms A5-A8 map the responses to the requests described in the previous algorithms. It is worth noting that a single method is used to map a response to the different variations of requests of the same operation (partial/full or PUT/POST). Therefore, we have defined only four response packet mapping algorithms. Since the response packets to write operations only notify about the success of the operation, their mapping algorithms are very concise. On the other hand, the mapping algorithms for the response to read operations should also include the data read. ```
Input : OSGP write response APDU Output: CoAP PUT/POST response packet
1 ifAPDU\(response=\ OK\)then
2 packet.hdr.code\(\leftarrow\) Valid Code;
3 else
4 packet.hdr.code\(\leftarrow\)Invalid Code;
5
6 returnCoAP PUT/POST response packet
```  
**Algorithm A5**Write_to_PUT_POST_response  
```
Input : OSGP read response APDU Output: CoAP GET response packet
1 ifAPDU\(response=\ OK\)then
2 packet.hdr.code\(\leftarrow\) Valid Code;
3 else
4 packet.hdr.code\(\leftarrow\)Invalid Code;
5
6 ifAPDU\(bytes>MAX\_SIZE\)and\(table\ read\)then
7 returnERROR;
8 else
9 packet.payload.data\(\leftarrow\) APDU.data;
10
11 returnCoAP GET response packet
```  
**Algorithm A6**Read_to_GET_response  
```
Input : CoAP PUT/POST response packet Output: OSGP write response APDU
12 returnOSGP write complete response APDU
13 else
4 returnOSGP write error response APDU
```  
**Algorithm A7**PUT_POST_to_Write_response  
```
Input : CoAP GET response packet Output: OSGP read response APDU
14 ifCoAP\(code=\ write\ complete\)then
2 APDU.count_bytes\(\leftarrow\)packet.payload_length;
3 APDU.payload\(\leftarrow\)packet.payload.data;
4 returnOSGP read complete response APDU
5
6 else
7 returnOSGP read error response APDU
```  
**Algorithm A7**PUT_POST_to_Write_response  
```
Input : CoAP GET response packet Output: OSGP write response APDU
12 returnOSGP write complete response APDU
13 else
24 returnOSGP write complete response APDU
25
36
47
58 end if
6
70
71 end if
72
73 end if
74
75 end if
76
77 end if
78 returnOSGP read error response APDU
```  
**Algorithm A8**GET_to_Read_response  
```
Input : CoAP GET response packet Output: OSGP read response APDU
12 ifCoAP\(code=\ write\ complete\)then
2 APDU.count_bytes\(\leftarrow\)packet.payload_length;
3 APDU.payload\(\leftarrow\)packet.payload.data;
4 returnOSGP read complete response APDU
5
60 else
61 returnOSGP read error response APDU
```  
**Algorithm A9**Get_to_Read_response  
```
Input : CoAP GET response packet Output: OSGP read response APDU
12 ifCoAP\(code=\



## References

* [PERSON] et al. (2016) [PERSON]; [PERSON]; [PERSON]; [PERSON] Middleware for Internet of Things: A Survey. _IEEE Internet Things J._**2016**, \(3\), 70-95. [CrossRef]
* [PERSON] et al. (2017) [PERSON]; [PERSON] [PERSON]; [PERSON] [PERSON]; [PERSON]; [PERSON] [PERSON] A Novel Locality Algorithm and Peer-to-Peer Communication Infrastructure for Optimizing Network Performance in Smart Microgrids. _Energies_**2017**, _10_, 1275. [CrossRef]
* [PERSON] et al. (2018) [PERSON]; [PERSON]; [PERSON]; [PERSON]; [PERSON] Internet of Things Infrastructure for Wireless Power Transfer Systems. _IEEE Access_**2018**, \(6\), 19295-19303. [CrossRef]
* [PERSON] et al. (2019) [PERSON]; [PERSON]; [PERSON] [PERSON]; [PERSON]; [PERSON] An Embedded Internet of Energy Communication Platform for the Future Smart Microgrids Management. _IEEE Internet Things J._**2019**, \(6\), 7241-7252. [CrossRef]
* [PERSON] (2016) [PERSON] [PERSON] A Survey on Smart Metering and Smart Grid Communication. _Renew.