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_
38 #include "uarraylist.h"
46 * @def VERIFY_NON_NULL
47 * @brief Macro to verify the validity of input argument
49 #define VERIFY_NON_NULL(arg, log_tag, log_message) \
51 OIC_LOG_V(ERROR, log_tag, "Invalid input:%s", log_message); \
52 return CA_STATUS_INVALID_PARAM; \
56 * @def VERIFY_NON_NULL_RET
57 * @brief Macro to verify the validity of input argument
59 #define VERIFY_NON_NULL_RET(arg, log_tag, log_message,ret) \
61 OIC_LOG_V(ERROR, log_tag, "Invalid input:%s", log_message); \
66 * @def VERIFY_NON_NULL_VOID
67 * @brief Macro to verify the validity of input argument
69 #define VERIFY_NON_NULL_VOID(arg, log_tag, log_message) \
71 OIC_LOG_V(ERROR, log_tag, "Invalid input:%s", log_message); \
76 * @brief Length of network interface name.
78 #define CA_INTERFACE_NAME_SIZE 16
81 * @def IPV4_ADDR_ONE_OCTECT_LEN
82 * @brief Macro to allocate memory for ipv4 address in the form of uint8_t.
84 #define IPV4_ADDR_ONE_OCTECT_LEN 4
87 * @brief Network Interface Information.
91 char ipAddress[CA_IPADDR_SIZE]; /**< Address of the interface **/
92 char subnetMask[CA_IPADDR_SIZE]; /**< Maintains interface subnetmask **/
93 char interfaceName[CA_INTERFACE_NAME_SIZE]; /**< Interface name**/
97 * @brief unicast and multicast server information.
101 int socketFd; /**< Socket decriptor **/
102 char ipAddress[CA_IPADDR_SIZE]; /**< Address of the ip **/
103 uint16_t port; /**< Server port number **/
104 bool isSecured; /**< Indicates secured server **/
105 bool isServerStarted; /**< Indicates server started **/
106 bool isMulticastServer; /**< Indicates multicast server **/
107 char ifAddr[CA_IPADDR_SIZE]; /**< Address of the multicast interface **/
108 char interfaceName[CA_INTERFACE_NAME_SIZE]; /**< Interface Name **/
109 char subNetMask[CA_IPADDR_SIZE]; /**< Subnet Mask **/
113 * @brief To log the PDU data
115 void CALogPDUData(coap_pdu_t *pdu);
118 * @fn CAAdapterCreateLocalEndpoint
119 * @brief Create CALocalConnectivity_t instance.
121 CALocalConnectivity_t *CAAdapterCreateLocalEndpoint(CATransportType_t type, const char *address);
124 * @fn CAAdapterCopyLocalEndpoint
125 * @brief Create CALocalConnectivity_t duplicate instance.
127 CALocalConnectivity_t *CAAdapterCopyLocalEndpoint(const CALocalConnectivity_t *connectivity);
130 * @fn CAAdapterFreeLocalEndpoint
131 * @brief Deallocate CALocalConnectivity_t instance.
133 void CAAdapterFreeLocalEndpoint(CALocalConnectivity_t *localEndPoint);
136 * @fn CAAdapterCreateRemoteEndpoint
137 * @brief Allocate CARemoteEndpoint_t instance.
139 CARemoteEndpoint_t *CAAdapterCreateRemoteEndpoint(CATransportType_t type, const char *address,
140 const char *resourceUri);
143 * @fn CAAdapterCopyRemoteEndpoint
144 * @brief Create CARemoteEndpoint_t duplicate instance.
146 CARemoteEndpoint_t *CAAdapterCopyRemoteEndpoint(
147 const CARemoteEndpoint_t *remoteEndpoint);
150 * @fn CAAdapterFreeRemoteEndpoint
151 * @brief Deallocate CARemoteEndpoint_t instance.
153 void CAAdapterFreeRemoteEndpoint(CARemoteEndpoint_t *remoteEndPoint);
156 * @fn CAParseIPv4AddressInternal
157 * @brief To parse the IP address and port from "ipaddress:port"
158 * @param ipAddrStr [IN] IP address to be parsed
159 * @param ipAddr [OUT] Parsed IP address
160 * @param ipAddr [IN] Buffer length for parsed IP address
161 * @param port [OUT] Parsed Port number
162 * @return #CA_STATUS_OK or Appropriate error code
164 CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
165 size_t ipAddrLen, uint16_t *port);
168 * @fn CAAdapterIsSameSubnet
169 * @brief Check if two ip address belong to same subnet.
170 * @param ipAddress1 [IN] IP address to be checked
171 * @param ipAddress2 [IN] IP address to be checked
172 * @param netMask [IN] Subnet mask
173 * @return true if same subnet and false if not same subnet
175 bool CAAdapterIsSameSubnet(const char *ipAddress1, const char *ipAddress2,
176 const char *netMask);
178 * @brief Used to check the multicast server is running or not.
180 * @param serverInfoList [IN] Server information list.
181 * @param ipAddress [IN] Interface address of the server.
182 * @param multicastAddress [IN] Multicast address of the server.
183 * @param port [IN] Port number of the server.
185 * @return true or false.
187 bool CAIsMulticastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
188 const char *multicastAddress, uint16_t port);
191 * @brief Used to check the unicast server is running or not.
193 * @param serverInfoList [IN] Server information list.
194 * @param ipAddress [IN] Ip address of the server.
195 * @param port [IN] Port number of the server.
197 * @return true or false.
199 bool CAIsUnicastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
203 * @brief Used to get the port number based on given information.
205 * @param serverInfoList [IN] Server information list.
206 * @param ipAddress [IN] Ip address of the server.
207 * @param isSecured [IN] specifies whether to get secured or normal unicast server port.
209 * @return positive value on success and 0 on error.
211 uint16_t CAGetServerPort(const u_arraylist_t *serverInfoList, const char *ipAddress,
215 * @brief Used to get the socket fd for given server information.
217 * @param serverInfoList [IN] Server information list.
218 * @param ipAddress [IN] Ip address of the server.
219 * @param isSecured [IN] To check whether it is secured server or not.
220 * @param isMulticast [IN] To check whether it is multicast server or not.
221 * @param type [IN] CA_IPV4, CA_IPV6 etc.
223 * @return positive value on success and -1 on error.
225 int CAGetSocketFdForUnicastServer(const u_arraylist_t *serverInfoList, const char *ipAddress,
226 bool isSecured, bool isMulticast, CATransportType_t type);
229 * @brief Used to add the server information into serverinfo list
231 * @param serverInfoList [INOUT] server information list.
232 * @param info [IN] server informations like ip, port.
234 * @return #CA_STATUS_OK or Appropriate error code
235 * @retval #CA_STATUS_OK Successful
236 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
237 * @retval #CA_STATUS_FAILED Initialization failed
239 CAResult_t CAAddServerInfo(u_arraylist_t *serverInfoList, CAServerInfo_t *info);
242 * @brief Used to remove the server information based on socket fd from server info list.
244 * @param serverInfoList [INOUT] server information list.
245 * @param sockFd [IN] Socket descriptor.
249 void CARemoveServerInfo(u_arraylist_t *serverInfoList, int sockFd);
252 * @brief Used to clear the memory of network inteface list
253 * Memory pointed by infoList will become invalid after this function call.
255 * @param infoList [IN] Network interface list.
259 void CAClearNetInterfaceInfoList(u_arraylist_t *infoList);
262 * @brief Used to clear the memory of server info list.
263 * Memory pointed by serverInfoList will become invalid after this function call.
265 * @param infoList [IN] Server information list.
269 void CAClearServerInfoList(u_arraylist_t *serverInfoList);
273 * @fn CANativeJNISetContext
274 * @brief To set context of JNI Application
275 * This must be called by the Android API before CA Initialization
276 * @param env [IN] JNI interface pointer
277 * @param context [IN] context object
280 void CANativeJNISetContext(JNIEnv *env, jobject context);
283 * @fn CANativeJNISetJavaVM
284 * @brief To set jvm object
285 * This must be called by the Android API before CA Initialization
286 * @param jvm [IN] jvm object
289 void CANativeJNISetJavaVM(JavaVM *jvm);
292 * @fn CANativeJNISetContext
293 * @brief To get context
294 * Called by adapters to get Application context
295 * @return context object
297 jobject CANativeJNIGetContext();
300 * @fn CANativeJNIGetJavaVM
301 * @brief To get JVM object
302 * Called from adapters to get JavaVM object
305 JavaVM *CANativeJNIGetJavaVM();
311 #endif /* CA_ADAPTER_UTILS_H_ */