Imported Upstream version 1.2.0
[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 "iotivity_config.h"
31
32 #include <stdbool.h>
33 #ifdef __ANDROID__
34 #include <jni.h>
35 #endif
36
37 #ifdef HAVE_SYS_SOCKET_H
38 #include <sys/socket.h>
39 #endif
40
41 #if defined(HAVE_WINSOCK2_H) && defined(HAVE_WS2TCPIP_H)
42 #include <winsock2.h>
43 #include <ws2tcpip.h>
44 #endif
45 #ifdef HAVE_SYS_SOCKET_H
46 #include <sys/socket.h>
47 #endif
48
49 #include "cacommon.h"
50 #include "logger.h"
51 #include <coap/pdu.h>
52 #include "uarraylist.h"
53 #include "cacommonutil.h"
54
55 #ifdef __cplusplus
56 extern "C"
57 {
58 #endif
59
60 /**
61  * Length of network interface name.
62  */
63 #define CA_INTERFACE_NAME_SIZE 16
64
65 /**
66  * Macro to allocate memory for ipv4 address in the form of uint8_t.
67  */
68 #define IPV4_ADDR_ONE_OCTECT_LEN 4
69
70 #ifdef SINGLE_THREAD
71 /**
72  * Network Interface Information. Only needed for Arduino.
73  */
74 typedef struct
75 {
76     char ipAddress[CA_IPADDR_SIZE];             /**< Address of the interface. **/
77     char subnetMask[CA_IPADDR_SIZE];            /**< Maintains interface subnetmask. **/
78     char interfaceName[CA_INTERFACE_NAME_SIZE]; /**< Interface name. **/
79 } CANetInfo_t;
80 #endif
81
82 /**
83  * unicast and multicast server information.
84  */
85 typedef struct
86 {
87     int socketFd;                               /**< Socket descriptor. **/
88     CAEndpoint_t endpoint;                      /**< endpoint description. **/
89     bool isServerStarted;                       /**< Indicates server started. **/
90     bool isMulticastServer;                     /**< Indicates multicast server. **/
91     char ifAddr[CA_IPADDR_SIZE];                /**< Address of the multicast interface. **/
92     char interfaceName[CA_INTERFACE_NAME_SIZE]; /**< Interface Name. **/
93     char subNetMask[CA_IPADDR_SIZE];            /**< Subnet Mask. **/
94 } CAServerInfo_t;
95
96 /**
97  * To parse the IP address and port from "ipaddress:port".
98  * @param[in]   ipAddrStr       IP address to be parsed.
99  * @param[out]  ipAddr          Parsed IP address.
100  * @param[in]   ipAddr          Buffer length for parsed IP address.
101  * @param[out]  port            Parsed Port number.
102  * @return ::CA_STATUS_OK or Appropriate error code.
103  */
104 CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
105                                       size_t ipAddrLen, uint16_t *port);
106
107 /**
108  * Check if two ip address belong to same subnet.
109  * @param[in]   ipAddress1      IP address to be checked.
110  * @param[in]   ipAddress2      IP address to be checked.
111  * @param[in]   netMask         Subnet mask.
112  * @return  true if same subnet and false if not same subnet.
113  */
114 bool CAAdapterIsSameSubnet(const char *ipAddress1, const char *ipAddress2,
115                            const char *netMask);
116 /**
117  * Used to check the multicast server is running or not.
118  *
119  * @param[in]   serverInfoList    Server information list.
120  * @param[in]   ipAddress         Interface address of the server.
121  * @param[in]   multicastAddress  Multicast address of the server.
122  * @param[in]   port              Port number of the server.
123  *
124  * @return  true or false.
125  */
126 bool CAIsMulticastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
127                                 const char *multicastAddress, uint16_t port);
128
129 /**
130  * Used to check the unicast server is running or not.
131  *
132  * @param[in]   serverInfoList   Server information list.
133  * @param[in]   ipAddress        Ip address of the server.
134  * @param[in]   port             Port number of the server.
135  *
136  * @return  true or false.
137  */
138 bool CAIsUnicastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
139                               uint16_t port);
140
141 /**
142  * Used to get the port number based on given information.
143  *
144  * @param[in]   serverInfoList   Server information list.
145  * @param[in]   ipAddress        Ip address of the server.
146  * @param[in]   isSecured        specifies whether to get secured or normal unicast server port.
147  *
148  * @return  positive value on success and 0 on error.
149  */
150 uint16_t CAGetServerPort(const u_arraylist_t *serverInfoList, const char *ipAddress,
151                          bool isSecured);
152
153 /**
154  * Used to get the socket fd for given server information.
155  *
156  * @param[in]   serverInfoList   Server information list.
157  * @param[in]   isMulticast      To check whether it is multicast server or not.
158  * @param[in]   endpoint         network address
159
160  * @return  positive value on success and -1 on error.
161  */
162 int CAGetSocketFdForUnicastServer(const u_arraylist_t *serverInfoList,
163                          bool isMulticast, const CAEndpoint_t *endpoint);
164
165 /**
166  * Used to add the server information into serverinfo list.
167  *
168  * @param[in/out]   serverInfoList    server information list.
169  * @param[in]       info              server informations like ip, port.
170  *
171  * @return ::CA_STATUS_OK or Appropriate error code.
172  * @retval ::CA_STATUS_OK  Successful.
173  * @retval ::CA_STATUS_INVALID_PARAM Invalid input data.
174  * @retval ::CA_STATUS_FAILED Initialization failed.
175  */
176 CAResult_t CAAddServerInfo(u_arraylist_t *serverInfoList, CAServerInfo_t *info);
177
178 /**
179  * Used to remove the server information based on socket fd from server info list.
180  *
181  * @param[in/out]   serverInfoList    server information list.
182  * @param[in]       sockFd            Socket descriptor.
183  */
184 void CARemoveServerInfo(u_arraylist_t *serverInfoList, int sockFd);
185
186 /**
187  * Used to clear the memory of network interface list.
188  * Memory pointed by infoList will become invalid after this function call.
189  *
190  * @param[in]   infoList    Network interface list.
191  */
192 void CAClearNetInterfaceInfoList(u_arraylist_t *infoList);
193
194 /**
195  * Used to clear the memory of server info list.
196  * Memory pointed by serverInfoList will become invalid after this function call.
197  *
198  * @param[in]   infoList    Server information list.
199  */
200 void CAClearServerInfoList(u_arraylist_t *serverInfoList);
201
202 #ifndef WITH_ARDUINO
203 /**
204  * Convert address from binary to string.
205  * @param[in]    sockAddr     IP address info.
206  * @param[in]    sockAddrLen  size of sockAddr.
207  * @param[out]   host         address string (must be CA_IPADDR_SIZE).
208  * @param[out]   port         host order port number.
209  */
210 void CAConvertAddrToName(const struct sockaddr_storage *sockAddr, socklen_t sockAddrLen,
211                          char *host, uint16_t *port);
212
213 /**
214  * Convert address from string to binary.
215  * @param[in]   host      address string.
216  * @param[in]   port      host order port number.
217  * @param[out]  ipaddr    IP address info.
218  */
219 void CAConvertNameToAddr(const char *host, uint16_t port, struct sockaddr_storage *sockaddr);
220 #endif /* WITH_ARDUINO */
221
222 #ifdef __ANDROID__
223 /**
224  * To set context of JNI Application.
225  * This must be called by the Android API before CA Initialization.
226  * @param[in]   env         JNI interface pointer.
227  * @param[in]   context     context object.
228  */
229 void CANativeJNISetContext(JNIEnv *env, jobject context);
230
231 /**
232  * To set jvm object.
233  * This must be called by the Android API before CA Initialization.
234  * @param[in]   jvm         jvm object.
235  */
236 void CANativeJNISetJavaVM(JavaVM *jvm);
237
238 /**
239  * To get context.
240  * Called by adapters to get Application context.
241  * @return  context object.
242  */
243 jobject CANativeJNIGetContext();
244
245 /**
246  * To get JVM object.
247  * Called from adapters to get JavaVM object.
248  * @return  JVM object.
249  */
250 JavaVM *CANativeJNIGetJavaVM();
251
252 /**
253  * To set Activity to JNI.
254  * This must be called by the Android API before CA Initialization.
255  * @param[in]   env         JNI Environment pointer.
256  * @param[in]   activity    Activity object.
257  */
258 void CANativeSetActivity(JNIEnv *env, jobject activity);
259
260 /**
261  * To get Activity.
262  * Called from adapters to get Activity.
263  * @return  Activity object.
264  */
265 jobject *CANativeGetActivity();
266
267 /**
268  * get method ID for method Name and class
269  * @param[in]   env              JNI interface pointer.
270  * @param[in]   className        android class.
271  * @param[in]   methodName       android method name.
272  * @param[in]   methodFormat     method type of methodName.
273  * @return      jmethodID        iD of the method.
274  */
275 jmethodID CAGetJNIMethodID(JNIEnv *env, const char* className,
276                            const char* methodName,
277                            const char* methodFormat);
278
279 /**
280  * To Delete other Global References
281  * Called during CATerminate to remove global references
282  */
283 void CADeleteGlobalReferences();
284
285 #endif
286
287 #ifdef __cplusplus
288 } /* extern "C" */
289 #endif
290 #endif  /* CA_ADAPTER_UTILS_H_ */
291