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