a9b3d9eebbe9f6c429d99ae7a0ef3bfbc4194a4c
[platform/upstream/iotivity.git] / resource / csdk / connectivity / inc / caadapterutils.h
1 /* ****************************************************************
2  *
3  * Copyright 2014 Samsung Electronics 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 /**
22  * @file
23  *
24  * This file contains common utility function for CA transport adaptors.
25  */
26
27 #ifndef CA_ADAPTER_UTILS_H_
28 #define CA_ADAPTER_UTILS_H_
29
30 #include <stdbool.h>
31 #ifdef __ANDROID__
32 #include <jni.h>
33 #endif
34
35 #ifndef WITH_ARDUINO
36 #include <sys/socket.h>
37 #endif
38
39 #include "cacommon.h"
40 #include "logger.h"
41 #include "pdu.h"
42 #include "uarraylist.h"
43
44 #ifdef __cplusplus
45 extern "C"
46 {
47 #endif
48
49 /**
50  * @def VERIFY_NON_NULL
51  * @brief Macro to verify the validity of input argument
52  */
53 #define VERIFY_NON_NULL(arg, log_tag, log_message) \
54     if (NULL == arg ){ \
55         OIC_LOG_V(ERROR, log_tag, "Invalid input:%s", log_message); \
56         return CA_STATUS_INVALID_PARAM; \
57     } \
58
59 /**
60  * @def VERIFY_NON_NULL_RET
61  * @brief Macro to verify the validity of input argument
62  */
63 #define VERIFY_NON_NULL_RET(arg, log_tag, log_message,ret) \
64     if (NULL == arg ){ \
65         OIC_LOG_V(ERROR, log_tag, "Invalid input:%s", log_message); \
66         return ret; \
67     } \
68
69 /**
70  * @def VERIFY_NON_NULL_VOID
71  * @brief Macro to verify the validity of input argument
72  */
73 #define VERIFY_NON_NULL_VOID(arg, log_tag, log_message) \
74     if (NULL == arg ){ \
75         OIC_LOG_V(ERROR, log_tag, "Invalid input:%s", log_message); \
76         return; \
77     } \
78
79 /**
80  * @brief Length of network interface name.
81  */
82 #define CA_INTERFACE_NAME_SIZE 16
83
84 /**
85  * @def IPV4_ADDR_ONE_OCTECT_LEN
86  * @brief Macro to allocate memory for ipv4 address in the form of uint8_t.
87  */
88 #define IPV4_ADDR_ONE_OCTECT_LEN 4
89
90 /**
91  * @brief Network Interface Information.
92  */
93 typedef struct
94 {
95     char ipAddress[CA_IPADDR_SIZE];             /**< Address of the interface  **/
96     char subnetMask[CA_IPADDR_SIZE];            /**< Maintains interface subnetmask **/
97     char interfaceName[CA_INTERFACE_NAME_SIZE]; /**< Interface  name**/
98 } CANetInfo_t;
99
100 /**
101  * @brief unicast and multicast server information.
102  */
103 typedef struct
104 {
105     int socketFd;                               /**< Socket decriptor **/
106     CAEndpoint_t endpoint;                      /**< endpoint description **/
107     bool isServerStarted;                       /**< Indicates server started **/
108     bool isMulticastServer;                     /**< Indicates multicast server **/
109     char ifAddr[CA_IPADDR_SIZE];                /**< Address of the multicast interface  **/
110     char interfaceName[CA_INTERFACE_NAME_SIZE]; /**< Interface Name **/
111     char subNetMask[CA_IPADDR_SIZE];            /**< Subnet Mask **/
112 } CAServerInfo_t;
113
114 /**
115  * @brief To log the PDU data
116  */
117 void CALogPDUData(coap_pdu_t *pdu);
118
119 /**
120  * @fn CAAdapterCloneEndpoint
121  * @brief Create CAEndpoint_t duplicate instance.
122  */
123 CAEndpoint_t *CAAdapterCloneEndpoint(const CAEndpoint_t *endpoint);
124
125 /**
126  * @fn CAAdapterFreeEndpoint
127  * @brief Deallocate CAEndpoint_t instance.
128  */
129 void CAAdapterFreeEndpoint(CAEndpoint_t *localEndPoint);
130
131 /**
132  * @fn CAAdapterCreateEndpoint
133  * @brief Allocate CAEndpoint_t instance.
134  */
135 CAEndpoint_t *CAAdapterCreateEndpoint(CATransportFlags_t flags,
136               CATransportAdapter_t adapter, const char *address, uint16_t port);
137
138 /**
139  * @fn CAParseIPv4AddressInternal
140  * @brief   To parse the IP address and port from "ipaddress:port"
141  * @param   ipAddrStr   [IN]   IP address to be parsed
142  * @param   ipAddr      [OUT]  Parsed IP address
143  * @param   ipAddr      [IN]   Buffer length for parsed IP address
144  * @param   port        [OUT]  Parsed Port number
145  * @return  #CA_STATUS_OK or Appropriate error code
146  */
147 CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
148                                       size_t ipAddrLen, uint16_t *port);
149
150 /**
151  * @fn CAAdapterIsSameSubnet
152  * @brief Check if two ip address belong to same subnet.
153  * @param   ipAddress1   [IN]   IP address to be checked
154  * @param   ipAddress2   [IN]   IP address to be checked
155  * @param   netMask      [IN]   Subnet mask
156  * @return  true if same subnet and false if not same subnet
157  */
158 bool CAAdapterIsSameSubnet(const char *ipAddress1, const char *ipAddress2,
159                            const char *netMask);
160 /**
161  * @brief  Used to check the multicast server is running or not.
162  *
163  * @param   serverInfoList    [IN] Server information list.
164  * @param   ipAddress         [IN] Interface address of the server.
165  * @param   multicastAddress  [IN] Multicast address of the server.
166  * @param   port              [IN] Port number of the server.
167  *
168  * @return  true or false.
169  */
170 bool CAIsMulticastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
171                                 const char *multicastAddress, uint16_t port);
172
173 /**
174  * @brief  Used to check the unicast server is running or not.
175  *
176  * @param   serverInfoList  [IN] Server information list.
177  * @param   ipAddress       [IN] Ip address of the server.
178  * @param   port            [IN] Port number of the server.
179  *
180  * @return  true or false.
181  */
182 bool CAIsUnicastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
183                               uint16_t port);
184
185 /**
186  * @brief  Used to get the port number based on given information.
187  *
188  * @param   serverInfoList  [IN] Server information list.
189  * @param   ipAddress       [IN] Ip address of the server.
190  * @param   isSecured       [IN] specifies whether to get secured or normal unicast server port.
191  *
192  * @return  positive value on success and 0 on error.
193  */
194 uint16_t CAGetServerPort(const u_arraylist_t *serverInfoList, const char *ipAddress,
195                          bool isSecured);
196
197 /**
198  * @brief  Used to get the socket fd for given server information.
199  *
200  * @param   serverInfoList  [IN] Server information list.
201  * @param   isMulticast     [IN] To check whether it is multicast server or not.
202  * @param   endpoint        [IN] network address
203
204  * @return  positive value on success and -1 on error.
205  */
206 int CAGetSocketFdForUnicastServer(const u_arraylist_t *serverInfoList,
207                          bool isMulticast, const CAEndpoint_t *endpoint);
208
209 /**
210  * @brief  Used to add the server information into serverinfo list
211  *
212  * @param   serverInfoList     [INOUT] server information list.
213  * @param   info               [IN] server informations like ip, port.
214  *
215  * @return  #CA_STATUS_OK or Appropriate error code
216  * @retval  #CA_STATUS_OK  Successful
217  * @retval  #CA_STATUS_INVALID_PARAM Invalid input data
218  * @retval  #CA_STATUS_FAILED Initialization failed
219  */
220 CAResult_t CAAddServerInfo(u_arraylist_t *serverInfoList, CAServerInfo_t *info);
221
222 /**
223  * @brief  Used to remove the server information based on socket fd from server info list.
224  *
225  * @param   serverInfoList  [INOUT] server information list.
226  * @param   sockFd          [IN] Socket descriptor.
227  *
228  * @return  None
229  */
230 void CARemoveServerInfo(u_arraylist_t *serverInfoList, int sockFd);
231
232 /**
233  * @brief  Used to clear the memory of network inteface list
234  *         Memory pointed by infoList will become invalid after this function call.
235  *
236  * @param   infoList  [IN] Network interface list.
237  *
238  * @return  None
239  */
240 void CAClearNetInterfaceInfoList(u_arraylist_t *infoList);
241
242 /**
243  * @brief  Used to clear the memory of server info list.
244  *         Memory pointed by serverInfoList will become invalid after this function call.
245  *
246  * @param   infoList  [IN] Server information list.
247  *
248  * @return  None
249  */
250 void CAClearServerInfoList(u_arraylist_t *serverInfoList);
251
252 /**
253  * @brief   Convert address from binary to string
254  * @param   ipaddr  [IN] IP address info
255  * @param   host    [OUT] address string (must be CA_IPADDR_SIZE)
256  * @param   port    [OUT] host order port number
257  */
258 void CAConvertAddrToName(const struct sockaddr_storage *sockaddr, char *host, uint16_t *port);
259
260 /**
261  * @brief   Convert address from string to binary
262  * @param   host    [IN] address string
263  * @param   port    [IN] host order port number
264  * @param   ipaddr  [OUT] IP address info
265  */
266 void CAConvertNameToAddr(const char *host, uint16_t port, struct sockaddr_storage *sockaddr);
267
268 #ifdef __ANDROID__
269 /**
270  * @fn CANativeJNISetContext
271  * @brief   To set context of JNI Application
272  *          This must be called by the Android API before CA Initialization
273  * @param   env         [IN] JNI interface pointer
274  * @param   context     [IN] context object
275  * @return  None
276  */
277 void CANativeJNISetContext(JNIEnv *env, jobject context);
278
279 /**
280  * @fn CANativeJNISetJavaVM
281  * @brief   To set jvm object
282  *          This must be called by the Android API before CA Initialization
283  * @param   jvm         [IN] jvm object
284  * @return  None
285  */
286 void CANativeJNISetJavaVM(JavaVM *jvm);
287
288 /**
289  * @fn CANativeJNISetContext
290  * @brief   To get context
291  *          Called by adapters to get Application context
292  * @return  context object
293  */
294 jobject CANativeJNIGetContext();
295
296 /**
297  * @fn CANativeJNIGetJavaVM
298  * @brief   To get JVM object
299  *          Called from adapters to get JavaVM object
300  * @return  JVM object
301  */
302 JavaVM *CANativeJNIGetJavaVM();
303 #endif
304
305 #ifdef __cplusplus
306 } /* extern "C" */
307 #endif
308 #endif  /* CA_ADAPTER_UTILS_H_ */
309