Porting TBStack Functionality to Arduino. The following changes were made
[platform/upstream/iotivity.git] / csdk / ocsocket / include / ocsocket.h
1 //******************************************************************
2 ///
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #ifndef _OCSOCKET_H
22 #define _OCSOCKET_H
23
24
25 #include <stdint.h>
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 /**@mainpage
32  *
33  * This module is a part of Open Communication Thin-Block SDK.
34  */
35
36
37 /**@defgroup socket Socket Interface
38  *
39  * This Socket interface  needs to be implemented for every platform on
40  * which CCF TB stack is expected to run. If some functionality is not
41  * available on a platform, implement the method by returning error
42  * ERR_NOT_IMPLEMENTED.
43  */
44
45 #define ERR_SUCCESS          (0)
46 #define ERR_INVALID_INPUT    (-900)
47 #define ERR_UNKNOWN          (-901)
48 #define ERR_NOT_IMPLEMENTED  (-903)
49
50
51 /** This would need to be modified for specific platforms and specific
52  *  technologies
53  */
54 #define DEV_ADDR_SIZE_MAX (16)
55
56 /**
57  *IPv4 or IPv6 addresses
58  */
59 #ifndef AF_INET
60 #define AF_INET (2)
61 #endif //AF_INET
62
63 #ifndef AF_INET6
64 #define AF_INET6 (10)
65 #endif //AF_INET6
66
67
68 /**
69  * Data structure to encapsulate IPv4/IPv6/Contiki/lwIP device addresses
70  *
71 */
72 #pragma pack(push, 1)
73 typedef struct OCDevAddr {
74     uint32_t     size;                    /**< length of the address stored in addr field. */
75     uint8_t      addr[DEV_ADDR_SIZE_MAX]; /**< device address. */
76 }OCDevAddr;
77 #pragma pack(pop)
78
79 //-- OCInitNetworkStack -----------------------------------------------------------
80 /** @ingroup ocsocket
81  *
82  * This method is used to perform any platform specific network
83  * initialization. Optional to implement.
84  *
85  * @retval 0 for Success, otherwise some error value
86  */
87 //------------------------------------------------------------------------
88 int32_t OCInitNetworkStack();
89
90
91 //-- OCInitUDP -----------------------------------------------------------
92 /** @ingroup ocsocket
93  *
94  * This method is used to create a new platform specific UDP socket and binds
95  * it to the address provided.
96  *
97  * @param[in] ipAddr
98  *              device address with which the new socket will be bind.
99  * @param[out] sockfd
100  *              reference to the new socket.
101  *
102  * @retval 0 for Success, otherwise some error value
103  */
104 //------------------------------------------------------------------------
105 int32_t OCInitUDP(OCDevAddr* ipAddr, int32_t* sockfd);
106
107
108
109 //-- OCInitUDPMulticast --------------------------------------------------
110 /** @ingroup ocsocket
111  *
112  * This method is used to create a new platform specific UDP socket for
113  * multicast and and binds it to the address provided.
114  *
115  * @param[in] ipmcastaddr
116  *              multicast address to which IGMP membership be added.
117  * @param[out] sockfd
118  *              reference to the new socket.
119  *
120  * @retval 0 for Success, otherwise some error value
121  */
122 //------------------------------------------------------------------------
123 int32_t OCInitUDPMulticast(OCDevAddr* ipmcastaddr, int32_t* sockfd);
124
125
126
127 //-- OCSendTo -------------------------------------------------------------
128 /** @ingroup ocsocket
129  *
130  * This method is used to transmit a UDP datagram to another endpoint.
131  *
132  * @param[in] sockfd
133  *              socket to be used for sending the datagram.
134  * @param[in] buf
135  *              datagram buffer.
136  * @param[in] bufLen
137  *              length of above buffer.
138  * @param[in] flags
139  *              flags to be used for sending datagram.
140  * @param[in] addr
141  *              endpoint to which datagram needs to be send.
142  *
143  * @retval On Success, it returns the number of bytes send, otherwise
144  *          some negative value.
145  */
146 //------------------------------------------------------------------------
147 int32_t OCSendTo(int32_t sockfd, const uint8_t* buf, uint32_t bufLen, uint32_t flags,
148             OCDevAddr * addr);
149
150
151 //-- OCRecvFrom ------------------------------------------------------------
152 /** @ingroup ocsocket
153  *
154  * This method is used to retrieve a UDP datagram from the socket.
155  *
156  * @param[in] sockfd
157  *              socket to be used for retrieving the datagram.
158  * @param[in] buf
159  *              datagram buffer.
160  * @param[in] bufLen
161  *              length of above buffer.
162  * @param[in] flags
163  *              flags to be used for receiving datagram.
164  * @param[out] addr
165  *              endpoint from which datagram has been received.
166  *
167  * @retval On Success, it returns the number of bytes read from the socket,
168  *          otherwise some negative value.
169  */
170 //------------------------------------------------------------------------
171 int32_t OCRecvFrom(int32_t sockfd, uint8_t* buf, uint32_t bufLen, uint32_t flags,
172             OCDevAddr * addr);
173
174 //-- OCClose ---------------------------------------------------------------
175 /** @ingroup ocsocket
176  *
177  * This method is used to close the platform specific socket and release any
178  * system resources associated with it.
179  *
180  * @param[in] sockfd
181  *              socket to be closed.
182  *
183  * @retval 0 for Success, otherwise some error value
184  */
185 //------------------------------------------------------------------------
186 int32_t OCClose(int32_t sockfd);
187
188
189 //Utility methods
190 //-- OCBuildIPv4Address -------------------------------------------------
191 /** @ingroup ocsocket
192  *
193  * This method is used to create the IPv4 dev_addr structure.
194  *
195  * @param[in]  a first byte of IPv4 address.
196  * @param[in]  b second byte of IPv4 address.
197  * @param[in]  c third byte of IPv4 address.
198  * @param[in]  d fourth byte of IPv4 address.
199  * @param[in]  port port number.
200  * @param[out] ipAddr
201  *              dev_addr to be filled with above data.
202  *
203  * @retval 0 for Success, otherwise some error value
204  */
205 //------------------------------------------------------------------------
206 int32_t OCBuildIPv4Address(uint8_t a, uint8_t b, uint8_t c, uint8_t d,
207             uint16_t port, OCDevAddr *ipAddr);
208
209
210 //-- OCGetInterfaceAddress ------------------------------------------------------
211 /** @ingroup ocsocket
212  *
213  * This method is used to retrieved the IPv4/IPv6 address of the local interface.
214  * If no interface name is provided, this API retrieves the IP address of
215  * the default interface.
216  *
217  * @note currently, only IPv4(AF_INET) is supported for addrType argument.
218  *
219  * @param[in]  ifName
220  *              interface whose address needs to be retrieved.
221  * @param[in]  ifNameLen
222  *              length of the interface name
223  * @param[in]  addrType
224  *              IPv4 or IPv6
225  * @param[out] addrv4
226  *              IPv4 address in a.b.c.d format
227  * @param[in]  addrLen
228  *              size of the buffer at addrv4. Should be at least 16 bytes for an
229  *              IPv4 address.
230  *
231  * @retval 0 for Success, otherwise some error value
232  */
233 //-------------------------------------------------------------------------------
234 int32_t OCGetInterfaceAddress(uint8_t* ifName, uint32_t ifNameLen, uint16_t addrType,
235              uint8_t *addrv4,  uint32_t addrLen);
236
237
238 //-- OCDevAddrToIPv4Addr -------------------------------------------------
239 /** @ingroup ocsocket
240  *
241  * This method is used to retrieved the IPv4 address from OCDev address
242  * data structure.
243  *
244  * @param[in]  ipAddr
245  *              OCDevAddr address.
246  * @param[out]  a first byte of IPv4 address.
247  * @param[out]  b second byte of IPv4 address.
248  * @param[out]  c third byte of IPv4 address.
249  * @param[out]  d fourth byte of IPv4 address.
250  *
251  * @retval 0 for Success, otherwise some error value
252  */
253 //------------------------------------------------------------------------
254 int32_t OCDevAddrToIPv4Addr(OCDevAddr *ipAddr, uint8_t *a, uint8_t *b,
255             uint8_t *c, uint8_t *d );
256
257
258 //-- OCDevAddrToPort -------------------------------------------------
259 /** @ingroup ocsocket
260  *
261  * This method is used to retrieve the port number from OCDev address
262  * data structure.
263  *
264  * @param[in]  ipAddr
265  *              OCDevAddr address.
266  * @param[out] port
267  *              port number
268  *
269  * @retval 0 for Success, otherwise some error value
270  */
271 //------------------------------------------------------------------------
272 int32_t OCDevAddrToPort(OCDevAddr *ipAddr, uint16_t *port);
273
274 #ifdef __cplusplus
275 }
276 #endif // __cplusplus
277
278 #endif //_OCSOCKET_H