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 #if !defined(WITH_ARDUINO) && !defined(__msys_nt__)
36 #include <sys/socket.h>
47 #include "uarraylist.h"
48 #include "cacommonutil.h"
56 * Length of network interface name.
58 #define CA_INTERFACE_NAME_SIZE 16
61 * Macro to allocate memory for ipv4 address in the form of uint8_t.
63 #define IPV4_ADDR_ONE_OCTECT_LEN 4
67 * Network Interface Information. Only needed for Arduino.
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. **/
78 * unicast and multicast server information.
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. **/
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.
99 CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
100 size_t ipAddrLen, uint16_t *port);
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.
109 bool CAAdapterIsSameSubnet(const char *ipAddress1, const char *ipAddress2,
110 const char *netMask);
112 * Used to check the multicast server is running or not.
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.
119 * @return true or false.
121 bool CAIsMulticastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
122 const char *multicastAddress, uint16_t port);
125 * Used to check the unicast server is running or not.
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.
131 * @return true or false.
133 bool CAIsUnicastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
137 * Used to get the port number based on given information.
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.
143 * @return positive value on success and 0 on error.
145 uint16_t CAGetServerPort(const u_arraylist_t *serverInfoList, const char *ipAddress,
149 * Used to get the socket fd for given server information.
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
155 * @return positive value on success and -1 on error.
157 int CAGetSocketFdForUnicastServer(const u_arraylist_t *serverInfoList,
158 bool isMulticast, const CAEndpoint_t *endpoint);
161 * Used to add the server information into serverinfo list.
163 * @param[in/out] serverInfoList server information list.
164 * @param[in] info server informations like ip, port.
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.
171 CAResult_t CAAddServerInfo(u_arraylist_t *serverInfoList, CAServerInfo_t *info);
174 * Used to remove the server information based on socket fd from server info list.
176 * @param[in/out] serverInfoList server information list.
177 * @param[in] sockFd Socket descriptor.
179 void CARemoveServerInfo(u_arraylist_t *serverInfoList, int sockFd);
182 * Used to clear the memory of network interface list.
183 * Memory pointed by infoList will become invalid after this function call.
185 * @param[in] infoList Network interface list.
187 void CAClearNetInterfaceInfoList(u_arraylist_t *infoList);
190 * Used to clear the memory of server info list.
191 * Memory pointed by serverInfoList will become invalid after this function call.
193 * @param[in] infoList Server information list.
195 void CAClearServerInfoList(u_arraylist_t *serverInfoList);
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.
205 void CAConvertAddrToName(const struct sockaddr_storage *sockAddr, socklen_t sockAddrLen,
206 char *host, uint16_t *port);
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.
214 void CAConvertNameToAddr(const char *host, uint16_t port, struct sockaddr_storage *sockaddr);
215 #endif /* WITH_ARDUINO */
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.
224 void CANativeJNISetContext(JNIEnv *env, jobject context);
228 * This must be called by the Android API before CA Initialization.
229 * @param[in] jvm jvm object.
231 void CANativeJNISetJavaVM(JavaVM *jvm);
235 * Called by adapters to get Application context.
236 * @return context object.
238 jobject CANativeJNIGetContext();
242 * Called from adapters to get JavaVM object.
243 * @return JVM object.
245 JavaVM *CANativeJNIGetJavaVM();
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.
253 void CANativeSetActivity(JNIEnv *env, jobject activity);
257 * Called from adapters to get Activity.
258 * @return Activity object.
260 jobject *CANativeGetActivity();
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.
270 jmethodID CAGetJNIMethodID(JNIEnv *env, const char* className,
271 const char* methodName,
272 const char* methodFormat);
275 * To Delete other Global References
276 * Called during CATerminate to remove global references
278 void CADeleteGlobalReferences();
285 #endif /* CA_ADAPTER_UTILS_H_ */