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