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