1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
33 * This module is a part of Open Communication Thin-Block SDK.
37 /**@defgroup socket Socket Interface
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.
45 #define ERR_SUCCESS (0)
46 #define ERR_INVALID_INPUT (-900)
47 #define ERR_UNKNOWN (-901)
48 #define ERR_NOT_IMPLEMENTED (-903)
51 /** This would need to be modified for specific platforms and specific
54 #define DEV_ADDR_SIZE_MAX (16)
57 *IPv4 or IPv6 addresses
69 * Data structure to encapsulate IPv4/IPv6/Contiki/lwIP device addresses
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. */
79 //-- OCInitNetworkStack -----------------------------------------------------------
82 * This method is used to perform any platform specific network
83 * initialization. Optional to implement.
85 * @retval 0 for Success, otherwise some error value
87 //------------------------------------------------------------------------
88 int32_t OCInitNetworkStack();
92 OC_SOCKET_NOOPTION = 0,
96 //-- OCInitUDP -----------------------------------------------------------
99 * This method is used to create a new platform specific UDP socket and binds
100 * it to the address provided.
103 * device address with which the new socket will be bind.
105 * reference to the new socket.
106 * @param[in] sockoption
107 * specifies which socket option to be used.
109 * @retval 0 for Success, otherwise some error value
111 //------------------------------------------------------------------------
112 int32_t OCInitUDP(OCDevAddr* ipAddr, int32_t* sockfd, OC_SOCKET_OPTION sockoption);
115 //-- OCSendTo -------------------------------------------------------------
116 /** @ingroup ocsocket
118 * This method is used to transmit a UDP datagram to another endpoint.
121 * socket to be used for sending the datagram.
125 * length of above buffer.
127 * flags to be used for sending datagram.
129 * endpoint to which datagram needs to be send.
131 * @retval On Success, it returns the number of bytes send, otherwise
132 * some negative value.
134 //------------------------------------------------------------------------
135 int32_t OCSendTo(int32_t sockfd, const uint8_t* buf, uint32_t bufLen, uint32_t flags,
139 //-- OCRecvFrom ------------------------------------------------------------
140 /** @ingroup ocsocket
142 * This method is used to retrieve a UDP datagram from the socket.
145 * socket to be used for retrieving the datagram.
149 * length of above buffer.
151 * flags to be used for receiving datagram.
153 * endpoint from which datagram has been received.
155 * @retval On Success, it returns the number of bytes read from the socket,
156 * otherwise some negative value.
158 //------------------------------------------------------------------------
159 int32_t OCRecvFrom(int32_t sockfd, uint8_t* buf, uint32_t bufLen, uint32_t flags,
162 //-- OCClose ---------------------------------------------------------------
163 /** @ingroup ocsocket
165 * This method is used to close the platform specific socket and release any
166 * system resources associated with it.
169 * socket to be closed.
171 * @retval 0 for Success, otherwise some error value
173 //------------------------------------------------------------------------
174 int32_t OCClose(int32_t sockfd);
178 //-- OCBuildIPv4Address -------------------------------------------------
179 /** @ingroup ocsocket
181 * This method is used to create the IPv4 dev_addr structure.
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.
189 * dev_addr to be filled with above data.
191 * @retval 0 for Success, otherwise some error value
193 //------------------------------------------------------------------------
194 int32_t OCBuildIPv4Address(uint8_t a, uint8_t b, uint8_t c, uint8_t d,
195 uint16_t port, OCDevAddr *ipAddr);
198 //-- OCDevAddrToString ----------------------------------------------------
199 /** @ingroup ocsocket
201 * This method is used to convert the OCDevAddr to string format
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.
209 * Note: The length of stringAddress may not exceed DEV_ADDR_SIZE_MAX
211 * @retval 0 for Success, otherwise some error value
213 //------------------------------------------------------------------------
214 int32_t OCDevAddrToString(OCDevAddr *addr, char *stringAddress);
217 //-- OCDevAddrToIPv4Addr -------------------------------------------------
218 /** @ingroup ocsocket
220 * This method is used to retrieved the IPv4 address from OCDev 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.
230 * @retval 0 for Success, otherwise some error value
232 //------------------------------------------------------------------------
233 int32_t OCDevAddrToIPv4Addr(OCDevAddr *ipAddr, uint8_t *a, uint8_t *b,
234 uint8_t *c, uint8_t *d );
237 //-- OCDevAddrToPort -------------------------------------------------
238 /** @ingroup ocsocket
240 * This method is used to retrieve the port number from OCDev address
248 * @retval 0 for Success, otherwise some error value
250 //------------------------------------------------------------------------
251 int32_t OCDevAddrToPort(OCDevAddr *ipAddr, uint16_t *port);
254 //-- OCGetSocketInfo -----------------------------------------------------
255 /** @ingroup ocsocket
257 * This method is used to retrieve the port number to which the @p sockfd
261 * socket whose port needs to be retrieved
265 * @retval 0 for Success, otherwise some error value
267 //------------------------------------------------------------------------
268 int32_t OCGetSocketInfo(int32_t sockfd, uint16_t *port);
272 #endif // __cplusplus