1 /* ****************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
24 * This file contains common utility function for CA transport adaptors.
27 #ifndef CA_ADAPTER_UTILS_H_
28 #define CA_ADAPTER_UTILS_H_
30 #include "iotivity_config.h"
37 #ifdef HAVE_SYS_SOCKET_H
38 #include <sys/socket.h>
41 #if defined(HAVE_WINSOCK2_H) && defined(HAVE_WS2TCPIP_H)
45 #ifdef HAVE_SYS_SOCKET_H
46 #include <sys/socket.h>
52 #include "uarraylist.h"
53 #include "cacommonutil.h"
61 * Length of network interface name.
63 #define CA_INTERFACE_NAME_SIZE 16
66 * Macro to allocate memory for ipv4 address in the form of uint8_t.
68 #define IPV4_ADDR_ONE_OCTECT_LEN 4
72 * Network Interface Information. Only needed for Arduino.
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. **/
83 * unicast and multicast server information.
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. **/
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.
104 CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
105 size_t ipAddrLen, uint16_t *port);
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.
114 bool CAAdapterIsSameSubnet(const char *ipAddress1, const char *ipAddress2,
115 const char *netMask);
117 * Used to check the multicast server is running or not.
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.
124 * @return true or false.
126 bool CAIsMulticastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
127 const char *multicastAddress, uint16_t port);
130 * Used to check the unicast server is running or not.
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.
136 * @return true or false.
138 bool CAIsUnicastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
142 * Used to get the port number based on given information.
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.
148 * @return positive value on success and 0 on error.
150 uint16_t CAGetServerPort(const u_arraylist_t *serverInfoList, const char *ipAddress,
154 * Used to get the socket fd for given server information.
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
160 * @return positive value on success and -1 on error.
162 int CAGetSocketFdForUnicastServer(const u_arraylist_t *serverInfoList,
163 bool isMulticast, const CAEndpoint_t *endpoint);
166 * Used to add the server information into serverinfo list.
168 * @param[in/out] serverInfoList server information list.
169 * @param[in] info server informations like ip, port.
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.
176 CAResult_t CAAddServerInfo(u_arraylist_t *serverInfoList, CAServerInfo_t *info);
179 * Used to remove the server information based on socket fd from server info list.
181 * @param[in/out] serverInfoList server information list.
182 * @param[in] sockFd Socket descriptor.
184 void CARemoveServerInfo(u_arraylist_t *serverInfoList, int sockFd);
187 * Used to clear the memory of network interface list.
188 * Memory pointed by infoList will become invalid after this function call.
190 * @param[in] infoList Network interface list.
192 void CAClearNetInterfaceInfoList(u_arraylist_t *infoList);
195 * Used to clear the memory of server info list.
196 * Memory pointed by serverInfoList will become invalid after this function call.
198 * @param[in] infoList Server information list.
200 void CAClearServerInfoList(u_arraylist_t *serverInfoList);
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.
210 void CAConvertAddrToName(const struct sockaddr_storage *sockAddr, socklen_t sockAddrLen,
211 char *host, uint16_t *port);
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.
219 void CAConvertNameToAddr(const char *host, uint16_t port, struct sockaddr_storage *sockaddr);
220 #endif /* WITH_ARDUINO */
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.
229 void CANativeJNISetContext(JNIEnv *env, jobject context);
233 * This must be called by the Android API before CA Initialization.
234 * @param[in] jvm jvm object.
236 void CANativeJNISetJavaVM(JavaVM *jvm);
240 * Called by adapters to get Application context.
241 * @return context object.
243 jobject CANativeJNIGetContext();
247 * Called from adapters to get JavaVM object.
248 * @return JVM object.
250 JavaVM *CANativeJNIGetJavaVM();
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.
258 void CANativeSetActivity(JNIEnv *env, jobject activity);
262 * Called from adapters to get Activity.
263 * @return Activity object.
265 jobject *CANativeGetActivity();
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.
275 jmethodID CAGetJNIMethodID(JNIEnv *env, const char* className,
276 const char* methodName,
277 const char* methodFormat);
280 * To Delete other Global References
281 * Called during CATerminate to remove global references
283 void CADeleteGlobalReferences();
290 #endif /* CA_ADAPTER_UTILS_H_ */