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 log the PDU data.
112 void CALogPDUData(coap_pdu_t *pdu);
115 * To parse the IP address and port from "ipaddress:port".
116 * @param[in] ipAddrStr IP address to be parsed.
117 * @param[out] ipAddr Parsed IP address.
118 * @param[in] ipAddr Buffer length for parsed IP address.
119 * @param[out] port Parsed Port number.
120 * @return ::CA_STATUS_OK or Appropriate error code.
122 CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
123 size_t ipAddrLen, uint16_t *port);
126 * Check if two ip address belong to same subnet.
127 * @param[in] ipAddress1 IP address to be checked.
128 * @param[in] ipAddress2 IP address to be checked.
129 * @param[in] netMask Subnet mask.
130 * @return true if same subnet and false if not same subnet.
132 bool CAAdapterIsSameSubnet(const char *ipAddress1, const char *ipAddress2,
133 const char *netMask);
135 * Used to check the multicast server is running or not.
137 * @param[in] serverInfoList Server information list.
138 * @param[in] ipAddress Interface address of the server.
139 * @param[in] multicastAddress Multicast address of the server.
140 * @param[in] port Port number of the server.
142 * @return true or false.
144 bool CAIsMulticastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
145 const char *multicastAddress, uint16_t port);
148 * Used to check the unicast server is running or not.
150 * @param[in] serverInfoList Server information list.
151 * @param[in] ipAddress Ip address of the server.
152 * @param[in] port Port number of the server.
154 * @return true or false.
156 bool CAIsUnicastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
160 * Used to get the port number based on given information.
162 * @param[in] serverInfoList Server information list.
163 * @param[in] ipAddress Ip address of the server.
164 * @param[in] isSecured specifies whether to get secured or normal unicast server port.
166 * @return positive value on success and 0 on error.
168 uint16_t CAGetServerPort(const u_arraylist_t *serverInfoList, const char *ipAddress,
172 * Used to get the socket fd for given server information.
174 * @param[in] serverInfoList Server information list.
175 * @param[in] isMulticast To check whether it is multicast server or not.
176 * @param[in] endpoint network address
178 * @return positive value on success and -1 on error.
180 int CAGetSocketFdForUnicastServer(const u_arraylist_t *serverInfoList,
181 bool isMulticast, const CAEndpoint_t *endpoint);
184 * Used to add the server information into serverinfo list.
186 * @param[in/out] serverInfoList server information list.
187 * @param[in] info server informations like ip, port.
189 * @return ::CA_STATUS_OK or Appropriate error code.
190 * @retval ::CA_STATUS_OK Successful.
191 * @retval ::CA_STATUS_INVALID_PARAM Invalid input data.
192 * @retval ::CA_STATUS_FAILED Initialization failed.
194 CAResult_t CAAddServerInfo(u_arraylist_t *serverInfoList, CAServerInfo_t *info);
197 * Used to remove the server information based on socket fd from server info list.
199 * @param[in/out] serverInfoList server information list.
200 * @param[in] sockFd Socket descriptor.
202 void CARemoveServerInfo(u_arraylist_t *serverInfoList, int sockFd);
205 * Used to clear the memory of network interface list.
206 * Memory pointed by infoList will become invalid after this function call.
208 * @param[in] infoList Network interface list.
210 void CAClearNetInterfaceInfoList(u_arraylist_t *infoList);
213 * Used to clear the memory of server info list.
214 * Memory pointed by serverInfoList will become invalid after this function call.
216 * @param[in] infoList Server information list.
218 void CAClearServerInfoList(u_arraylist_t *serverInfoList);
221 * Convert address from binary to string.
222 * @param[in] ipaddr IP address info.
223 * @param[out] host address string (must be CA_IPADDR_SIZE).
224 * @param[out] port host order port number.
226 void CAConvertAddrToName(const struct sockaddr_storage *sockaddr, char *host, uint16_t *port);
229 * Convert address from string to binary.
230 * @param[in] host address string.
231 * @param[in] port host order port number.
232 * @param[out] ipaddr IP address info.
234 void CAConvertNameToAddr(const char *host, uint16_t port, struct sockaddr_storage *sockaddr);
238 * To set context of JNI Application.
239 * This must be called by the Android API before CA Initialization.
240 * @param[in] env JNI interface pointer.
241 * @param[in] context context object.
243 void CANativeJNISetContext(JNIEnv *env, jobject context);
247 * This must be called by the Android API before CA Initialization.
248 * @param[in] jvm jvm object.
250 void CANativeJNISetJavaVM(JavaVM *jvm);
254 * Called by adapters to get Application context.
255 * @return context object.
257 jobject CANativeJNIGetContext();
261 * Called from adapters to get JavaVM object.
262 * @return JVM object.
264 JavaVM *CANativeJNIGetJavaVM();
270 #endif /* CA_ADAPTER_UTILS_H_ */