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