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_
36 #include <sys/socket.h>
42 #include "uarraylist.h"
43 #include "cacommonutil.h"
51 * Length of network interface name.
53 #define CA_INTERFACE_NAME_SIZE 16
56 * Macro to allocate memory for ipv4 address in the form of uint8_t.
58 #define IPV4_ADDR_ONE_OCTECT_LEN 4
62 * Network Interface Information. Only needed for Arduino.
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. **/
73 * unicast and multicast server information.
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. **/
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.
94 CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
95 size_t ipAddrLen, uint16_t *port);
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.
104 bool CAAdapterIsSameSubnet(const char *ipAddress1, const char *ipAddress2,
105 const char *netMask);
107 * Used to check the multicast server is running or not.
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.
114 * @return true or false.
116 bool CAIsMulticastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
117 const char *multicastAddress, uint16_t port);
120 * Used to check the unicast server is running or not.
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.
126 * @return true or false.
128 bool CAIsUnicastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
132 * Used to get the port number based on given information.
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.
138 * @return positive value on success and 0 on error.
140 uint16_t CAGetServerPort(const u_arraylist_t *serverInfoList, const char *ipAddress,
144 * Used to get the socket fd for given server information.
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
150 * @return positive value on success and -1 on error.
152 int CAGetSocketFdForUnicastServer(const u_arraylist_t *serverInfoList,
153 bool isMulticast, const CAEndpoint_t *endpoint);
156 * Used to add the server information into serverinfo list.
158 * @param[in/out] serverInfoList server information list.
159 * @param[in] info server informations like ip, port.
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.
166 CAResult_t CAAddServerInfo(u_arraylist_t *serverInfoList, CAServerInfo_t *info);
169 * Used to remove the server information based on socket fd from server info list.
171 * @param[in/out] serverInfoList server information list.
172 * @param[in] sockFd Socket descriptor.
174 void CARemoveServerInfo(u_arraylist_t *serverInfoList, int sockFd);
177 * Used to clear the memory of network interface list.
178 * Memory pointed by infoList will become invalid after this function call.
180 * @param[in] infoList Network interface list.
182 void CAClearNetInterfaceInfoList(u_arraylist_t *infoList);
185 * Used to clear the memory of server info list.
186 * Memory pointed by serverInfoList will become invalid after this function call.
188 * @param[in] infoList Server information list.
190 void CAClearServerInfoList(u_arraylist_t *serverInfoList);
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.
200 void CAConvertAddrToName(const struct sockaddr_storage *sockAddr, socklen_t sockAddrLen,
201 char *host, uint16_t *port);
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.
209 void CAConvertNameToAddr(const char *host, uint16_t port, struct sockaddr_storage *sockaddr);
210 #endif /* WITH_ARDUINO */
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.
219 void CANativeJNISetContext(JNIEnv *env, jobject context);
223 * This must be called by the Android API before CA Initialization.
224 * @param[in] jvm jvm object.
226 void CANativeJNISetJavaVM(JavaVM *jvm);
230 * Called by adapters to get Application context.
231 * @return context object.
233 jobject CANativeJNIGetContext();
237 * Called from adapters to get JavaVM object.
238 * @return JVM object.
240 JavaVM *CANativeJNIGetJavaVM();
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.
248 void CANativeSetActivity(JNIEnv *env, jobject activity);
252 * Called from adapters to get Activity.
253 * @return Activity object.
255 jobject *CANativeGetActivity();
258 * To Delete other Global References
259 * Called during CATerminate to remove global references
261 void CADeleteGlobalReferences();
268 #endif /* CA_ADAPTER_UTILS_H_ */