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_
35 #ifdef HAVE_SYS_SOCKET_H
36 #include <sys/socket.h>
39 #if defined(HAVE_WINSOCK2_H) && defined(HAVE_WS2TCPIP_H)
43 #ifdef HAVE_SYS_SOCKET_H
44 #include <sys/socket.h>
50 #include "uarraylist.h"
51 #include "cacommonutil.h"
59 * Length of network interface name.
61 #define CA_INTERFACE_NAME_SIZE 16
64 * Macro to allocate memory for ipv4 address in the form of uint8_t.
66 #define IPV4_ADDR_ONE_OCTECT_LEN 4
70 * Network Interface Information. Only needed for Arduino.
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. **/
81 * unicast and multicast server information.
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. **/
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.
102 CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
103 size_t ipAddrLen, uint16_t *port);
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.
112 bool CAAdapterIsSameSubnet(const char *ipAddress1, const char *ipAddress2,
113 const char *netMask);
115 * Used to check the multicast server is running or not.
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.
122 * @return true or false.
124 bool CAIsMulticastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
125 const char *multicastAddress, uint16_t port);
128 * Used to check the unicast server is running or not.
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.
134 * @return true or false.
136 bool CAIsUnicastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
140 * Used to get the port number based on given information.
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.
146 * @return positive value on success and 0 on error.
148 uint16_t CAGetServerPort(const u_arraylist_t *serverInfoList, const char *ipAddress,
152 * Used to get the socket fd for given server information.
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
158 * @return positive value on success and -1 on error.
160 int CAGetSocketFdForUnicastServer(const u_arraylist_t *serverInfoList,
161 bool isMulticast, const CAEndpoint_t *endpoint);
164 * Used to add the server information into serverinfo list.
166 * @param[in/out] serverInfoList server information list.
167 * @param[in] info server informations like ip, port.
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.
174 CAResult_t CAAddServerInfo(u_arraylist_t *serverInfoList, CAServerInfo_t *info);
177 * Used to remove the server information based on socket fd from server info list.
179 * @param[in/out] serverInfoList server information list.
180 * @param[in] sockFd Socket descriptor.
182 void CARemoveServerInfo(u_arraylist_t *serverInfoList, int sockFd);
185 * Used to clear the memory of network interface list.
186 * Memory pointed by infoList will become invalid after this function call.
188 * @param[in] infoList Network interface list.
190 void CAClearNetInterfaceInfoList(u_arraylist_t *infoList);
193 * Used to clear the memory of server info list.
194 * Memory pointed by serverInfoList will become invalid after this function call.
196 * @param[in] infoList Server information list.
198 void CAClearServerInfoList(u_arraylist_t *serverInfoList);
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.
208 void CAConvertAddrToName(const struct sockaddr_storage *sockAddr, socklen_t sockAddrLen,
209 char *host, uint16_t *port);
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.
217 void CAConvertNameToAddr(const char *host, uint16_t port, struct sockaddr_storage *sockaddr);
218 #endif /* WITH_ARDUINO */
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.
227 void CANativeJNISetContext(JNIEnv *env, jobject context);
231 * This must be called by the Android API before CA Initialization.
232 * @param[in] jvm jvm object.
234 void CANativeJNISetJavaVM(JavaVM *jvm);
238 * Called by adapters to get Application context.
239 * @return context object.
241 jobject CANativeJNIGetContext();
245 * Called from adapters to get JavaVM object.
246 * @return JVM object.
248 JavaVM *CANativeJNIGetJavaVM();
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.
256 void CANativeSetActivity(JNIEnv *env, jobject activity);
260 * Called from adapters to get Activity.
261 * @return Activity object.
263 jobject *CANativeGetActivity();
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.
273 jmethodID CAGetJNIMethodID(JNIEnv *env, const char* className,
274 const char* methodName,
275 const char* methodFormat);
278 * To Delete other Global References
279 * Called during CATerminate to remove global references
281 void CADeleteGlobalReferences();
288 #endif /* CA_ADAPTER_UTILS_H_ */