Imported Upstream version 0.9.2
[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 #ifdef SINGLE_THREAD
91 /**
92  * @brief Network Interface Information. Only needed for Arduino.
93  */
94 typedef struct
95 {
96     char ipAddress[CA_IPADDR_SIZE];             /**< Address of the interface  **/
97     char subnetMask[CA_IPADDR_SIZE];            /**< Maintains interface subnetmask **/
98     char interfaceName[CA_INTERFACE_NAME_SIZE]; /**< Interface  name**/
99 } CANetInfo_t;
100 #endif
101
102 /**
103  * @brief unicast and multicast server information.
104  */
105 typedef struct
106 {
107     int socketFd;                               /**< Socket decriptor **/
108     CAEndpoint_t endpoint;                      /**< endpoint description **/
109     bool isServerStarted;                       /**< Indicates server started **/
110     bool isMulticastServer;                     /**< Indicates multicast server **/
111     char ifAddr[CA_IPADDR_SIZE];                /**< Address of the multicast interface  **/
112     char interfaceName[CA_INTERFACE_NAME_SIZE]; /**< Interface Name **/
113     char subNetMask[CA_IPADDR_SIZE];            /**< Subnet Mask **/
114 } CAServerInfo_t;
115
116 /**
117  * @brief To log the PDU data
118  */
119 void CALogPDUData(coap_pdu_t *pdu);
120
121 /**
122  * @fn CAParseIPv4AddressInternal
123  * @brief   To parse the IP address and port from "ipaddress:port"
124  * @param   ipAddrStr   [IN]   IP address to be parsed
125  * @param   ipAddr      [OUT]  Parsed IP address
126  * @param   ipAddr      [IN]   Buffer length for parsed IP address
127  * @param   port        [OUT]  Parsed Port number
128  * @return  #CA_STATUS_OK or Appropriate error code
129  */
130 CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
131                                       size_t ipAddrLen, uint16_t *port);
132
133 /**
134  * @fn CAAdapterIsSameSubnet
135  * @brief Check if two ip address belong to same subnet.
136  * @param   ipAddress1   [IN]   IP address to be checked
137  * @param   ipAddress2   [IN]   IP address to be checked
138  * @param   netMask      [IN]   Subnet mask
139  * @return  true if same subnet and false if not same subnet
140  */
141 bool CAAdapterIsSameSubnet(const char *ipAddress1, const char *ipAddress2,
142                            const char *netMask);
143 /**
144  * @brief  Used to check the multicast server is running or not.
145  *
146  * @param   serverInfoList    [IN] Server information list.
147  * @param   ipAddress         [IN] Interface address of the server.
148  * @param   multicastAddress  [IN] Multicast address of the server.
149  * @param   port              [IN] Port number of the server.
150  *
151  * @return  true or false.
152  */
153 bool CAIsMulticastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
154                                 const char *multicastAddress, uint16_t port);
155
156 /**
157  * @brief  Used to check the unicast server is running or not.
158  *
159  * @param   serverInfoList  [IN] Server information list.
160  * @param   ipAddress       [IN] Ip address of the server.
161  * @param   port            [IN] Port number of the server.
162  *
163  * @return  true or false.
164  */
165 bool CAIsUnicastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
166                               uint16_t port);
167
168 /**
169  * @brief  Used to get the port number based on given information.
170  *
171  * @param   serverInfoList  [IN] Server information list.
172  * @param   ipAddress       [IN] Ip address of the server.
173  * @param   isSecured       [IN] specifies whether to get secured or normal unicast server port.
174  *
175  * @return  positive value on success and 0 on error.
176  */
177 uint16_t CAGetServerPort(const u_arraylist_t *serverInfoList, const char *ipAddress,
178                          bool isSecured);
179
180 /**
181  * @brief  Used to get the socket fd for given server information.
182  *
183  * @param   serverInfoList  [IN] Server information list.
184  * @param   isMulticast     [IN] To check whether it is multicast server or not.
185  * @param   endpoint        [IN] network address
186
187  * @return  positive value on success and -1 on error.
188  */
189 int CAGetSocketFdForUnicastServer(const u_arraylist_t *serverInfoList,
190                          bool isMulticast, const CAEndpoint_t *endpoint);
191
192 /**
193  * @brief  Used to add the server information into serverinfo list
194  *
195  * @param   serverInfoList     [INOUT] server information list.
196  * @param   info               [IN] server informations like ip, port.
197  *
198  * @return  #CA_STATUS_OK or Appropriate error code
199  * @retval  #CA_STATUS_OK  Successful
200  * @retval  #CA_STATUS_INVALID_PARAM Invalid input data
201  * @retval  #CA_STATUS_FAILED Initialization failed
202  */
203 CAResult_t CAAddServerInfo(u_arraylist_t *serverInfoList, CAServerInfo_t *info);
204
205 /**
206  * @brief  Used to remove the server information based on socket fd from server info list.
207  *
208  * @param   serverInfoList  [INOUT] server information list.
209  * @param   sockFd          [IN] Socket descriptor.
210  *
211  * @return  None
212  */
213 void CARemoveServerInfo(u_arraylist_t *serverInfoList, int sockFd);
214
215 /**
216  * @brief  Used to clear the memory of network inteface list
217  *         Memory pointed by infoList will become invalid after this function call.
218  *
219  * @param   infoList  [IN] Network interface list.
220  *
221  * @return  None
222  */
223 void CAClearNetInterfaceInfoList(u_arraylist_t *infoList);
224
225 /**
226  * @brief  Used to clear the memory of server info list.
227  *         Memory pointed by serverInfoList will become invalid after this function call.
228  *
229  * @param   infoList  [IN] Server information list.
230  *
231  * @return  None
232  */
233 void CAClearServerInfoList(u_arraylist_t *serverInfoList);
234
235 /**
236  * @brief   Convert address from binary to string
237  * @param   ipaddr  [IN] IP address info
238  * @param   host    [OUT] address string (must be CA_IPADDR_SIZE)
239  * @param   port    [OUT] host order port number
240  */
241 void CAConvertAddrToName(const struct sockaddr_storage *sockaddr, char *host, uint16_t *port);
242
243 /**
244  * @brief   Convert address from string to binary
245  * @param   host    [IN] address string
246  * @param   port    [IN] host order port number
247  * @param   ipaddr  [OUT] IP address info
248  */
249 void CAConvertNameToAddr(const char *host, uint16_t port, struct sockaddr_storage *sockaddr);
250
251 #ifdef __ANDROID__
252 /**
253  * @fn CANativeJNISetContext
254  * @brief   To set context of JNI Application
255  *          This must be called by the Android API before CA Initialization
256  * @param   env         [IN] JNI interface pointer
257  * @param   context     [IN] context object
258  * @return  None
259  */
260 void CANativeJNISetContext(JNIEnv *env, jobject context);
261
262 /**
263  * @fn CANativeJNISetJavaVM
264  * @brief   To set jvm object
265  *          This must be called by the Android API before CA Initialization
266  * @param   jvm         [IN] jvm object
267  * @return  None
268  */
269 void CANativeJNISetJavaVM(JavaVM *jvm);
270
271 /**
272  * @fn CANativeJNISetContext
273  * @brief   To get context
274  *          Called by adapters to get Application context
275  * @return  context object
276  */
277 jobject CANativeJNIGetContext();
278
279 /**
280  * @fn CANativeJNIGetJavaVM
281  * @brief   To get JVM object
282  *          Called from adapters to get JavaVM object
283  * @return  JVM object
284  */
285 JavaVM *CANativeJNIGetJavaVM();
286 #endif
287
288 #ifdef __cplusplus
289 } /* extern "C" */
290 #endif
291 #endif  /* CA_ADAPTER_UTILS_H_ */
292