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
92 * @brief Network Interface Information. Only needed for Arduino.
96 char ipAddress[CA_IPADDR_SIZE]; /**< Address of the interface **/
97 char subnetMask[CA_IPADDR_SIZE]; /**< Maintains interface subnetmask **/
98 char interfaceName[CA_INTERFACE_NAME_SIZE]; /**< Interface name**/
103 * @brief unicast and multicast server information.
107 int socketFd; /**< Socket decriptor **/
108 CAEndpoint_t endpoint; /**< endpoint description **/
109 bool isServerStarted; /**< Indicates server started **/
110 bool isMulticastServer; /**< Indicates multicast server **/
111 char ifAddr[CA_IPADDR_SIZE]; /**< Address of the multicast interface **/
112 char interfaceName[CA_INTERFACE_NAME_SIZE]; /**< Interface Name **/
113 char subNetMask[CA_IPADDR_SIZE]; /**< Subnet Mask **/
117 * @brief To log the PDU data
119 void CALogPDUData(coap_pdu_t *pdu);
122 * @fn CAParseIPv4AddressInternal
123 * @brief To parse the IP address and port from "ipaddress:port"
124 * @param ipAddrStr [IN] IP address to be parsed
125 * @param ipAddr [OUT] Parsed IP address
126 * @param ipAddr [IN] Buffer length for parsed IP address
127 * @param port [OUT] Parsed Port number
128 * @return #CA_STATUS_OK or Appropriate error code
130 CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
131 size_t ipAddrLen, uint16_t *port);
134 * @fn CAAdapterIsSameSubnet
135 * @brief Check if two ip address belong to same subnet.
136 * @param ipAddress1 [IN] IP address to be checked
137 * @param ipAddress2 [IN] IP address to be checked
138 * @param netMask [IN] Subnet mask
139 * @return true if same subnet and false if not same subnet
141 bool CAAdapterIsSameSubnet(const char *ipAddress1, const char *ipAddress2,
142 const char *netMask);
144 * @brief Used to check the multicast server is running or not.
146 * @param serverInfoList [IN] Server information list.
147 * @param ipAddress [IN] Interface address of the server.
148 * @param multicastAddress [IN] Multicast address of the server.
149 * @param port [IN] Port number of the server.
151 * @return true or false.
153 bool CAIsMulticastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
154 const char *multicastAddress, uint16_t port);
157 * @brief Used to check the unicast server is running or not.
159 * @param serverInfoList [IN] Server information list.
160 * @param ipAddress [IN] Ip address of the server.
161 * @param port [IN] Port number of the server.
163 * @return true or false.
165 bool CAIsUnicastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
169 * @brief Used to get the port number based on given information.
171 * @param serverInfoList [IN] Server information list.
172 * @param ipAddress [IN] Ip address of the server.
173 * @param isSecured [IN] specifies whether to get secured or normal unicast server port.
175 * @return positive value on success and 0 on error.
177 uint16_t CAGetServerPort(const u_arraylist_t *serverInfoList, const char *ipAddress,
181 * @brief Used to get the socket fd for given server information.
183 * @param serverInfoList [IN] Server information list.
184 * @param isMulticast [IN] To check whether it is multicast server or not.
185 * @param endpoint [IN] network address
187 * @return positive value on success and -1 on error.
189 int CAGetSocketFdForUnicastServer(const u_arraylist_t *serverInfoList,
190 bool isMulticast, const CAEndpoint_t *endpoint);
193 * @brief Used to add the server information into serverinfo list
195 * @param serverInfoList [INOUT] server information list.
196 * @param info [IN] server informations like ip, port.
198 * @return #CA_STATUS_OK or Appropriate error code
199 * @retval #CA_STATUS_OK Successful
200 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
201 * @retval #CA_STATUS_FAILED Initialization failed
203 CAResult_t CAAddServerInfo(u_arraylist_t *serverInfoList, CAServerInfo_t *info);
206 * @brief Used to remove the server information based on socket fd from server info list.
208 * @param serverInfoList [INOUT] server information list.
209 * @param sockFd [IN] Socket descriptor.
213 void CARemoveServerInfo(u_arraylist_t *serverInfoList, int sockFd);
216 * @brief Used to clear the memory of network inteface list
217 * Memory pointed by infoList will become invalid after this function call.
219 * @param infoList [IN] Network interface list.
223 void CAClearNetInterfaceInfoList(u_arraylist_t *infoList);
226 * @brief Used to clear the memory of server info list.
227 * Memory pointed by serverInfoList will become invalid after this function call.
229 * @param infoList [IN] Server information list.
233 void CAClearServerInfoList(u_arraylist_t *serverInfoList);
236 * @brief Convert address from binary to string
237 * @param ipaddr [IN] IP address info
238 * @param host [OUT] address string (must be CA_IPADDR_SIZE)
239 * @param port [OUT] host order port number
241 void CAConvertAddrToName(const struct sockaddr_storage *sockaddr, char *host, uint16_t *port);
244 * @brief Convert address from string to binary
245 * @param host [IN] address string
246 * @param port [IN] host order port number
247 * @param ipaddr [OUT] IP address info
249 void CAConvertNameToAddr(const char *host, uint16_t port, struct sockaddr_storage *sockaddr);
253 * @fn CANativeJNISetContext
254 * @brief To set context of JNI Application
255 * This must be called by the Android API before CA Initialization
256 * @param env [IN] JNI interface pointer
257 * @param context [IN] context object
260 void CANativeJNISetContext(JNIEnv *env, jobject context);
263 * @fn CANativeJNISetJavaVM
264 * @brief To set jvm object
265 * This must be called by the Android API before CA Initialization
266 * @param jvm [IN] jvm object
269 void CANativeJNISetJavaVM(JavaVM *jvm);
272 * @fn CANativeJNISetContext
273 * @brief To get context
274 * Called by adapters to get Application context
275 * @return context object
277 jobject CANativeJNIGetContext();
280 * @fn CANativeJNIGetJavaVM
281 * @brief To get JVM object
282 * Called from adapters to get JavaVM object
285 JavaVM *CANativeJNIGetJavaVM();
291 #endif /* CA_ADAPTER_UTILS_H_ */