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 * @def VERIFY_NON_NULL
51 * @brief Macro to verify the validity of input argument
53 #define VERIFY_NON_NULL(arg, log_tag, log_message) \
55 OIC_LOG_V(ERROR, log_tag, "Invalid input:%s", log_message); \
56 return CA_STATUS_INVALID_PARAM; \
60 * @def VERIFY_NON_NULL_RET
61 * @brief Macro to verify the validity of input argument
63 #define VERIFY_NON_NULL_RET(arg, log_tag, log_message,ret) \
65 OIC_LOG_V(ERROR, log_tag, "Invalid input:%s", log_message); \
70 * @def VERIFY_NON_NULL_VOID
71 * @brief Macro to verify the validity of input argument
73 #define VERIFY_NON_NULL_VOID(arg, log_tag, log_message) \
75 OIC_LOG_V(ERROR, log_tag, "Invalid input:%s", log_message); \
80 * @brief Length of network interface name.
82 #define CA_INTERFACE_NAME_SIZE 16
85 * @def IPV4_ADDR_ONE_OCTECT_LEN
86 * @brief Macro to allocate memory for ipv4 address in the form of uint8_t.
88 #define IPV4_ADDR_ONE_OCTECT_LEN 4
91 * @brief Network Interface Information.
95 char ipAddress[CA_IPADDR_SIZE]; /**< Address of the interface **/
96 char subnetMask[CA_IPADDR_SIZE]; /**< Maintains interface subnetmask **/
97 char interfaceName[CA_INTERFACE_NAME_SIZE]; /**< Interface name**/
101 * @brief unicast and multicast server information.
105 int socketFd; /**< Socket decriptor **/
106 CAEndpoint_t endpoint; /**< endpoint description **/
107 bool isServerStarted; /**< Indicates server started **/
108 bool isMulticastServer; /**< Indicates multicast server **/
109 char ifAddr[CA_IPADDR_SIZE]; /**< Address of the multicast interface **/
110 char interfaceName[CA_INTERFACE_NAME_SIZE]; /**< Interface Name **/
111 char subNetMask[CA_IPADDR_SIZE]; /**< Subnet Mask **/
115 * @brief To log the PDU data
117 void CALogPDUData(coap_pdu_t *pdu);
120 * @fn CAAdapterCloneEndpoint
121 * @brief Create CAEndpoint_t duplicate instance.
123 CAEndpoint_t *CAAdapterCloneEndpoint(const CAEndpoint_t *endpoint);
126 * @fn CAAdapterFreeEndpoint
127 * @brief Deallocate CAEndpoint_t instance.
129 void CAAdapterFreeEndpoint(CAEndpoint_t *localEndPoint);
132 * @fn CAAdapterCreateEndpoint
133 * @brief Allocate CAEndpoint_t instance.
135 CAEndpoint_t *CAAdapterCreateEndpoint(CATransportFlags_t flags,
136 CATransportAdapter_t adapter, const char *address, uint16_t port);
139 * @fn CAParseIPv4AddressInternal
140 * @brief To parse the IP address and port from "ipaddress:port"
141 * @param ipAddrStr [IN] IP address to be parsed
142 * @param ipAddr [OUT] Parsed IP address
143 * @param ipAddr [IN] Buffer length for parsed IP address
144 * @param port [OUT] Parsed Port number
145 * @return #CA_STATUS_OK or Appropriate error code
147 CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
148 size_t ipAddrLen, uint16_t *port);
151 * @fn CAAdapterIsSameSubnet
152 * @brief Check if two ip address belong to same subnet.
153 * @param ipAddress1 [IN] IP address to be checked
154 * @param ipAddress2 [IN] IP address to be checked
155 * @param netMask [IN] Subnet mask
156 * @return true if same subnet and false if not same subnet
158 bool CAAdapterIsSameSubnet(const char *ipAddress1, const char *ipAddress2,
159 const char *netMask);
161 * @brief Used to check the multicast server is running or not.
163 * @param serverInfoList [IN] Server information list.
164 * @param ipAddress [IN] Interface address of the server.
165 * @param multicastAddress [IN] Multicast address of the server.
166 * @param port [IN] Port number of the server.
168 * @return true or false.
170 bool CAIsMulticastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
171 const char *multicastAddress, uint16_t port);
174 * @brief Used to check the unicast server is running or not.
176 * @param serverInfoList [IN] Server information list.
177 * @param ipAddress [IN] Ip address of the server.
178 * @param port [IN] Port number of the server.
180 * @return true or false.
182 bool CAIsUnicastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
186 * @brief Used to get the port number based on given information.
188 * @param serverInfoList [IN] Server information list.
189 * @param ipAddress [IN] Ip address of the server.
190 * @param isSecured [IN] specifies whether to get secured or normal unicast server port.
192 * @return positive value on success and 0 on error.
194 uint16_t CAGetServerPort(const u_arraylist_t *serverInfoList, const char *ipAddress,
198 * @brief Used to get the socket fd for given server information.
200 * @param serverInfoList [IN] Server information list.
201 * @param isMulticast [IN] To check whether it is multicast server or not.
202 * @param endpoint [IN] network address
204 * @return positive value on success and -1 on error.
206 int CAGetSocketFdForUnicastServer(const u_arraylist_t *serverInfoList,
207 bool isMulticast, const CAEndpoint_t *endpoint);
210 * @brief Used to add the server information into serverinfo list
212 * @param serverInfoList [INOUT] server information list.
213 * @param info [IN] server informations like ip, port.
215 * @return #CA_STATUS_OK or Appropriate error code
216 * @retval #CA_STATUS_OK Successful
217 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
218 * @retval #CA_STATUS_FAILED Initialization failed
220 CAResult_t CAAddServerInfo(u_arraylist_t *serverInfoList, CAServerInfo_t *info);
223 * @brief Used to remove the server information based on socket fd from server info list.
225 * @param serverInfoList [INOUT] server information list.
226 * @param sockFd [IN] Socket descriptor.
230 void CARemoveServerInfo(u_arraylist_t *serverInfoList, int sockFd);
233 * @brief Used to clear the memory of network inteface list
234 * Memory pointed by infoList will become invalid after this function call.
236 * @param infoList [IN] Network interface list.
240 void CAClearNetInterfaceInfoList(u_arraylist_t *infoList);
243 * @brief Used to clear the memory of server info list.
244 * Memory pointed by serverInfoList will become invalid after this function call.
246 * @param infoList [IN] Server information list.
250 void CAClearServerInfoList(u_arraylist_t *serverInfoList);
253 * @brief Convert address from binary to string
254 * @param ipaddr [IN] IP address info
255 * @param host [OUT] address string (must be CA_IPADDR_SIZE)
256 * @param port [OUT] host order port number
258 void CAConvertAddrToName(const struct sockaddr_storage *sockaddr, char *host, uint16_t *port);
261 * @brief Convert address from string to binary
262 * @param host [IN] address string
263 * @param port [IN] host order port number
264 * @param ipaddr [OUT] IP address info
266 void CAConvertNameToAddr(const char *host, uint16_t port, struct sockaddr_storage *sockaddr);
270 * @fn CANativeJNISetContext
271 * @brief To set context of JNI Application
272 * This must be called by the Android API before CA Initialization
273 * @param env [IN] JNI interface pointer
274 * @param context [IN] context object
277 void CANativeJNISetContext(JNIEnv *env, jobject context);
280 * @fn CANativeJNISetJavaVM
281 * @brief To set jvm object
282 * This must be called by the Android API before CA Initialization
283 * @param jvm [IN] jvm object
286 void CANativeJNISetJavaVM(JavaVM *jvm);
289 * @fn CANativeJNISetContext
290 * @brief To get context
291 * Called by adapters to get Application context
292 * @return context object
294 jobject CANativeJNIGetContext();
297 * @fn CANativeJNIGetJavaVM
298 * @brief To get JVM object
299 * Called from adapters to get JavaVM object
302 JavaVM *CANativeJNIGetJavaVM();
308 #endif /* CA_ADAPTER_UTILS_H_ */