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