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"
50 * Macro to verify the validity of input argument.
52 #define VERIFY_NON_NULL_RET(arg, log_tag, log_message,ret) \
54 OIC_LOG_V(ERROR, log_tag, "Invalid input:%s", log_message); \
59 * Macro to verify the validity of input argument.
61 #define VERIFY_NON_NULL(arg, log_tag, log_message) \
62 VERIFY_NON_NULL_RET((arg), (log_tag), (log_message), CA_STATUS_INVALID_PARAM)
65 * Macro to verify the validity of input argument.
67 #define VERIFY_NON_NULL_VOID(arg, log_tag, log_message) \
69 OIC_LOG_V(ERROR, log_tag, "Invalid input:%s", log_message); \
74 * Length of network interface name.
76 #define CA_INTERFACE_NAME_SIZE 16
79 * Macro to allocate memory for ipv4 address in the form of uint8_t.
81 #define IPV4_ADDR_ONE_OCTECT_LEN 4
85 * Network Interface Information. Only needed for Arduino.
89 char ipAddress[CA_IPADDR_SIZE]; /**< Address of the interface. **/
90 char subnetMask[CA_IPADDR_SIZE]; /**< Maintains interface subnetmask. **/
91 char interfaceName[CA_INTERFACE_NAME_SIZE]; /**< Interface name. **/
96 * unicast and multicast server information.
100 int socketFd; /**< Socket descriptor. **/
101 CAEndpoint_t endpoint; /**< endpoint description. **/
102 bool isServerStarted; /**< Indicates server started. **/
103 bool isMulticastServer; /**< Indicates multicast server. **/
104 char ifAddr[CA_IPADDR_SIZE]; /**< Address of the multicast interface. **/
105 char interfaceName[CA_INTERFACE_NAME_SIZE]; /**< Interface Name. **/
106 char subNetMask[CA_IPADDR_SIZE]; /**< Subnet Mask. **/
110 * To parse the IP address and port from "ipaddress:port".
111 * @param[in] ipAddrStr IP address to be parsed.
112 * @param[out] ipAddr Parsed IP address.
113 * @param[in] ipAddr Buffer length for parsed IP address.
114 * @param[out] port Parsed Port number.
115 * @return ::CA_STATUS_OK or Appropriate error code.
117 CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
118 size_t ipAddrLen, uint16_t *port);
121 * Check if two ip address belong to same subnet.
122 * @param[in] ipAddress1 IP address to be checked.
123 * @param[in] ipAddress2 IP address to be checked.
124 * @param[in] netMask Subnet mask.
125 * @return true if same subnet and false if not same subnet.
127 bool CAAdapterIsSameSubnet(const char *ipAddress1, const char *ipAddress2,
128 const char *netMask);
130 * Used to check the multicast server is running or not.
132 * @param[in] serverInfoList Server information list.
133 * @param[in] ipAddress Interface address of the server.
134 * @param[in] multicastAddress Multicast address of the server.
135 * @param[in] port Port number of the server.
137 * @return true or false.
139 bool CAIsMulticastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
140 const char *multicastAddress, uint16_t port);
143 * Used to check the unicast server is running or not.
145 * @param[in] serverInfoList Server information list.
146 * @param[in] ipAddress Ip address of the server.
147 * @param[in] port Port number of the server.
149 * @return true or false.
151 bool CAIsUnicastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
155 * Used to get the port number based on given information.
157 * @param[in] serverInfoList Server information list.
158 * @param[in] ipAddress Ip address of the server.
159 * @param[in] isSecured specifies whether to get secured or normal unicast server port.
161 * @return positive value on success and 0 on error.
163 uint16_t CAGetServerPort(const u_arraylist_t *serverInfoList, const char *ipAddress,
167 * Used to get the socket fd for given server information.
169 * @param[in] serverInfoList Server information list.
170 * @param[in] isMulticast To check whether it is multicast server or not.
171 * @param[in] endpoint network address
173 * @return positive value on success and -1 on error.
175 int CAGetSocketFdForUnicastServer(const u_arraylist_t *serverInfoList,
176 bool isMulticast, const CAEndpoint_t *endpoint);
179 * Used to add the server information into serverinfo list.
181 * @param[in/out] serverInfoList server information list.
182 * @param[in] info server informations like ip, port.
184 * @return ::CA_STATUS_OK or Appropriate error code.
185 * @retval ::CA_STATUS_OK Successful.
186 * @retval ::CA_STATUS_INVALID_PARAM Invalid input data.
187 * @retval ::CA_STATUS_FAILED Initialization failed.
189 CAResult_t CAAddServerInfo(u_arraylist_t *serverInfoList, CAServerInfo_t *info);
192 * Used to remove the server information based on socket fd from server info list.
194 * @param[in/out] serverInfoList server information list.
195 * @param[in] sockFd Socket descriptor.
197 void CARemoveServerInfo(u_arraylist_t *serverInfoList, int sockFd);
200 * Used to clear the memory of network interface list.
201 * Memory pointed by infoList will become invalid after this function call.
203 * @param[in] infoList Network interface list.
205 void CAClearNetInterfaceInfoList(u_arraylist_t *infoList);
208 * Used to clear the memory of server info list.
209 * Memory pointed by serverInfoList will become invalid after this function call.
211 * @param[in] infoList Server information list.
213 void CAClearServerInfoList(u_arraylist_t *serverInfoList);
217 * Convert address from binary to string.
218 * @param[in] sockAddr IP address info.
219 * @param[in] sockAddrLen size of sockAddr.
220 * @param[out] host address string (must be CA_IPADDR_SIZE).
221 * @param[out] port host order port number.
223 void CAConvertAddrToName(const struct sockaddr_storage *sockAddr, socklen_t sockAddrLen,
224 char *host, uint16_t *port);
227 * Convert address from string to binary.
228 * @param[in] host address string.
229 * @param[in] port host order port number.
230 * @param[out] ipaddr IP address info.
232 void CAConvertNameToAddr(const char *host, uint16_t port, struct sockaddr_storage *sockaddr);
233 #endif /* WITH_ARDUINO */
237 * To set context of JNI Application.
238 * This must be called by the Android API before CA Initialization.
239 * @param[in] env JNI interface pointer.
240 * @param[in] context context object.
242 void CANativeJNISetContext(JNIEnv *env, jobject context);
246 * This must be called by the Android API before CA Initialization.
247 * @param[in] jvm jvm object.
249 void CANativeJNISetJavaVM(JavaVM *jvm);
253 * Called by adapters to get Application context.
254 * @return context object.
256 jobject CANativeJNIGetContext();
260 * Called from adapters to get JavaVM object.
261 * @return JVM object.
263 JavaVM *CANativeJNIGetJavaVM();
269 #endif /* CA_ADAPTER_UTILS_H_ */