1 //******************************************************************
3 // Copyright 2014 Intel Corporation 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();
91 //-- OCInitUDP -----------------------------------------------------------
94 * This method is used to create a new platform specific UDP socket and binds
95 * it to the address provided.
98 * device address with which the new socket will be bind.
100 * reference to the new socket.
102 * @retval 0 for Success, otherwise some error value
104 //------------------------------------------------------------------------
105 int32_t OCInitUDP(OCDevAddr* ipAddr, int32_t* sockfd);
109 //-- OCInitUDPMulticast --------------------------------------------------
110 /** @ingroup ocsocket
112 * This method is used to create a new platform specific UDP socket for
113 * multicast and and binds it to the address provided.
115 * @param[in] ipmcastaddr
116 * multicast address to which IGMP membership be added.
118 * reference to the new socket.
120 * @retval 0 for Success, otherwise some error value
122 //------------------------------------------------------------------------
123 int32_t OCInitUDPMulticast(OCDevAddr* ipmcastaddr, int32_t* sockfd);
127 //-- OCSendTo -------------------------------------------------------------
128 /** @ingroup ocsocket
130 * This method is used to transmit a UDP datagram to another endpoint.
133 * socket to be used for sending the datagram.
137 * length of above buffer.
139 * flags to be used for sending datagram.
141 * endpoint to which datagram needs to be send.
143 * @retval On Success, it returns the number of bytes send, otherwise
144 * some negative value.
146 //------------------------------------------------------------------------
147 int32_t OCSendTo(int32_t sockfd, const uint8_t* buf, uint32_t bufLen, uint32_t flags,
151 //-- OCRecvFrom ------------------------------------------------------------
152 /** @ingroup ocsocket
154 * This method is used to retrieve a UDP datagram from the socket.
157 * socket to be used for retrieving the datagram.
161 * length of above buffer.
163 * flags to be used for receiving datagram.
165 * endpoint from which datagram has been received.
167 * @retval On Success, it returns the number of bytes read from the socket,
168 * otherwise some negative value.
170 //------------------------------------------------------------------------
171 int32_t OCRecvFrom(int32_t sockfd, uint8_t* buf, uint32_t bufLen, uint32_t flags,
174 //-- OCClose ---------------------------------------------------------------
175 /** @ingroup ocsocket
177 * This method is used to close the platform specific socket and release any
178 * system resources associated with it.
181 * socket to be closed.
183 * @retval 0 for Success, otherwise some error value
185 //------------------------------------------------------------------------
186 int32_t OCClose(int32_t sockfd);
190 //-- OCBuildIPv4Address -------------------------------------------------
191 /** @ingroup ocsocket
193 * This method is used to create the IPv4 dev_addr structure.
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.
201 * dev_addr to be filled with above data.
203 * @retval 0 for Success, otherwise some error value
205 //------------------------------------------------------------------------
206 int32_t OCBuildIPv4Address(uint8_t a, uint8_t b, uint8_t c, uint8_t d,
207 uint16_t port, OCDevAddr *ipAddr);
210 //-- OCGetInterfaceAddress ------------------------------------------------------
211 /** @ingroup ocsocket
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.
217 * @note currently, only IPv4(AF_INET) is supported for addrType argument.
220 * interface whose address needs to be retrieved.
221 * @param[in] ifNameLen
222 * length of the interface name
223 * @param[in] addrType
226 * IPv4 address in a.b.c.d format
228 * size of the buffer at addrv4. Should be at least 16 bytes for an
231 * @retval 0 for Success, otherwise some error value
233 //-------------------------------------------------------------------------------
234 int32_t OCGetInterfaceAddress(uint8_t* ifName, uint32_t ifNameLen, uint16_t addrType,
235 uint8_t *addrv4, uint32_t addrLen);
238 //-- OCDevAddrToIPv4Addr -------------------------------------------------
239 /** @ingroup ocsocket
241 * This method is used to retrieved the IPv4 address from OCDev 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.
251 * @retval 0 for Success, otherwise some error value
253 //------------------------------------------------------------------------
254 int32_t OCDevAddrToIPv4Addr(OCDevAddr *ipAddr, uint8_t *a, uint8_t *b,
255 uint8_t *c, uint8_t *d );
258 //-- OCDevAddrToPort -------------------------------------------------
259 /** @ingroup ocsocket
261 * This method is used to retrieve the port number from OCDev address
269 * @retval 0 for Success, otherwise some error value
271 //------------------------------------------------------------------------
272 int32_t OCDevAddrToPort(OCDevAddr *ipAddr, uint16_t *port);
276 #endif // __cplusplus