af4ab31833131338d901f71fa71c9fe035302d30
[platform/upstream/iotivity.git] / resource / csdk / ocsocket / include / ocsocket.h
1 //******************************************************************
2 ///
3 // Copyright 2014 Intel Mobile Communications GmbH 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 typedef enum
91 {
92     OC_SOCKET_NOOPTION = 0,
93     OC_SOCKET_REUSEADDR
94 } OC_SOCKET_OPTION;
95
96 //-- OCInitUDP -----------------------------------------------------------
97 /** @ingroup ocsocket
98  *
99  * This method is used to create a new platform specific UDP socket and binds
100  * it to the address provided.
101  *
102  * @param[in] ipAddr
103  *              device address with which the new socket will be bind.
104  * @param[out] sockfd
105  *              reference to the new socket.
106  * @param[in] sockoption
107  *              specifies which socket option to be used.
108  *
109  * @retval 0 for Success, otherwise some error value
110  */
111 //------------------------------------------------------------------------
112 int32_t OCInitUDP(OCDevAddr* ipAddr, int32_t* sockfd, OC_SOCKET_OPTION sockoption);
113
114
115 //-- OCSendTo -------------------------------------------------------------
116 /** @ingroup ocsocket
117  *
118  * This method is used to transmit a UDP datagram to another endpoint.
119  *
120  * @param[in] sockfd
121  *              socket to be used for sending the datagram.
122  * @param[in] buf
123  *              datagram buffer.
124  * @param[in] bufLen
125  *              length of above buffer.
126  * @param[in] flags
127  *              flags to be used for sending datagram.
128  * @param[in] addr
129  *              endpoint to which datagram needs to be send.
130  *
131  * @retval On Success, it returns the number of bytes send, otherwise
132  *          some negative value.
133  */
134 //------------------------------------------------------------------------
135 int32_t OCSendTo(int32_t sockfd, const uint8_t* buf, uint32_t bufLen, uint32_t flags,
136             OCDevAddr * addr);
137
138
139 //-- OCRecvFrom ------------------------------------------------------------
140 /** @ingroup ocsocket
141  *
142  * This method is used to retrieve a UDP datagram from the socket.
143  *
144  * @param[in] sockfd
145  *              socket to be used for retrieving the datagram.
146  * @param[in] buf
147  *              datagram buffer.
148  * @param[in] bufLen
149  *              length of above buffer.
150  * @param[in] flags
151  *              flags to be used for receiving datagram.
152  * @param[out] addr
153  *              endpoint from which datagram has been received.
154  *
155  * @retval On Success, it returns the number of bytes read from the socket,
156  *          otherwise some negative value.
157  */
158 //------------------------------------------------------------------------
159 int32_t OCRecvFrom(int32_t sockfd, uint8_t* buf, uint32_t bufLen, uint32_t flags,
160             OCDevAddr * addr);
161
162 //-- OCClose ---------------------------------------------------------------
163 /** @ingroup ocsocket
164  *
165  * This method is used to close the platform specific socket and release any
166  * system resources associated with it.
167  *
168  * @param[in] sockfd
169  *              socket to be closed.
170  *
171  * @retval 0 for Success, otherwise some error value
172  */
173 //------------------------------------------------------------------------
174 int32_t OCClose(int32_t sockfd);
175
176
177 //Utility methods
178 //-- OCBuildIPv4Address -------------------------------------------------
179 /** @ingroup ocsocket
180  *
181  * This method is used to create the IPv4 dev_addr structure.
182  *
183  * @param[in]  a first byte of IPv4 address.
184  * @param[in]  b second byte of IPv4 address.
185  * @param[in]  c third byte of IPv4 address.
186  * @param[in]  d fourth byte of IPv4 address.
187  * @param[in]  port port number.
188  * @param[out] ipAddr
189  *              dev_addr to be filled with above data.
190  *
191  * @retval 0 for Success, otherwise some error value
192  */
193 //------------------------------------------------------------------------
194 int32_t OCBuildIPv4Address(uint8_t a, uint8_t b, uint8_t c, uint8_t d,
195             uint16_t port, OCDevAddr *ipAddr);
196
197
198 //-- OCDevAddrToString ----------------------------------------------------
199 /** @ingroup ocsocket
200  *
201  * This method is used to convert the OCDevAddr to string format
202  *
203  * @param[in]   addr
204  *               OCDevAddr address.
205  * @param[out]  stringAddress the target string where the address
206  *               is to be stored. Memory for this parameter is
207  *               allocated by the caller.
208  *
209  * Note: The length of stringAddress may not exceed DEV_ADDR_SIZE_MAX
210  *
211  * @retval 0 for Success, otherwise some error value
212  */
213 //------------------------------------------------------------------------
214 int32_t OCDevAddrToString(OCDevAddr *addr, char *stringAddress);
215
216
217 //-- OCDevAddrToIPv4Addr -------------------------------------------------
218 /** @ingroup ocsocket
219  *
220  * This method is used to retrieved the IPv4 address from OCDev address
221  * data structure.
222  *
223  * @param[in]  ipAddr
224  *              OCDevAddr address.
225  * @param[out]  a first byte of IPv4 address.
226  * @param[out]  b second byte of IPv4 address.
227  * @param[out]  c third byte of IPv4 address.
228  * @param[out]  d fourth byte of IPv4 address.
229  *
230  * @retval 0 for Success, otherwise some error value
231  */
232 //------------------------------------------------------------------------
233 int32_t OCDevAddrToIPv4Addr(OCDevAddr *ipAddr, uint8_t *a, uint8_t *b,
234             uint8_t *c, uint8_t *d );
235
236
237 //-- OCDevAddrToPort -------------------------------------------------
238 /** @ingroup ocsocket
239  *
240  * This method is used to retrieve the port number from OCDev address
241  * data structure.
242  *
243  * @param[in]  ipAddr
244  *              OCDevAddr address.
245  * @param[out] port
246  *              port number
247  *
248  * @retval 0 for Success, otherwise some error value
249  */
250 //------------------------------------------------------------------------
251 int32_t OCDevAddrToPort(OCDevAddr *ipAddr, uint16_t *port);
252
253
254 //-- OCGetSocketInfo -----------------------------------------------------
255 /** @ingroup ocsocket
256  *
257  * This method is used to retrieve the port number to which the @p sockfd
258  * is bound.
259  *
260  * @param[in]  sockfd
261  *              socket whose port needs to be retrieved
262  * @param[out] port
263  *              port number
264  *
265  * @retval 0 for Success, otherwise some error value
266  */
267 //------------------------------------------------------------------------
268 int32_t OCGetSocketInfo(int32_t sockfd, uint16_t *port);
269
270 #ifdef __cplusplus
271 }
272 #endif // __cplusplus
273
274 #endif //_OCSOCKET_H