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_
33 #include "uarraylist.h"
41 * @def VERIFY_NON_NULL
42 * @brief Macro to verify the validity of input argument
44 #define VERIFY_NON_NULL(arg, log_tag, log_message) \
46 OIC_LOG_V(ERROR, log_tag, "Invalid input:%s", log_message); \
47 return CA_STATUS_INVALID_PARAM; \
51 * @def VERIFY_NON_NULL_RET
52 * @brief Macro to verify the validity of input argument
54 #define VERIFY_NON_NULL_RET(arg, log_tag, log_message,ret) \
56 OIC_LOG_V(ERROR, log_tag, "Invalid input:%s", log_message); \
61 * @def VERIFY_NON_NULL_VOID
62 * @brief Macro to verify the validity of input argument
64 #define VERIFY_NON_NULL_VOID(arg, log_tag, log_message) \
66 OIC_LOG_V(ERROR, log_tag, "Invalid input:%s", log_message); \
71 * @brief Length of network interface name.
73 #define CA_INTERFACE_NAME_SIZE 16
76 * @def IPV4_ADDR_ONE_OCTECT_LEN
77 * @brief Macro to allocate memory for ipv4 address in the form of uint8_t.
79 #define IPV4_ADDR_ONE_OCTECT_LEN 4
82 * @brief Network Interface Information.
86 char ipAddress[CA_IPADDR_SIZE]; /**< Address of the interface **/
87 char subnetMask[CA_IPADDR_SIZE]; /**< Maintains interface subnetmask **/
88 char interfaceName[CA_INTERFACE_NAME_SIZE]; /**< Interface name**/
92 * @brief unicast and multicast server information.
96 int socketFd; /**< Socket decriptor **/
97 char ipAddress[CA_IPADDR_SIZE]; /**< Address of the ip **/
98 uint16_t port; /**< Server port number **/
99 bool isSecured; /**< Indicates secured server **/
100 bool isServerStarted; /**< Indicates server started **/
101 bool isMulticastServer; /**< Indicates multicast server **/
102 char ifAddr[CA_IPADDR_SIZE]; /**< Address of the multicast interface **/
103 char interfaceName[CA_INTERFACE_NAME_SIZE]; /**< Interface Name **/
104 char subNetMask[CA_IPADDR_SIZE]; /**< Subnet Mask **/
108 * @brief To log the PDU data
110 void CALogPDUData(coap_pdu_t *pdu);
113 * @fn CAAdapterCreateLocalEndpoint
114 * @brief Create CALocalConnectivity_t instance.
116 CALocalConnectivity_t *CAAdapterCreateLocalEndpoint(CATransportType_t type, const char *address);
119 * @fn CAAdapterCopyLocalEndpoint
120 * @brief Create CALocalConnectivity_t duplicate instance.
122 CALocalConnectivity_t *CAAdapterCopyLocalEndpoint(const CALocalConnectivity_t *connectivity);
125 * @fn CAAdapterFreeLocalEndpoint
126 * @brief Deallocate CALocalConnectivity_t instance.
128 void CAAdapterFreeLocalEndpoint(CALocalConnectivity_t *localEndPoint);
131 * @fn CAAdapterCreateRemoteEndpoint
132 * @brief Allocate CARemoteEndpoint_t instance.
134 CARemoteEndpoint_t *CAAdapterCreateRemoteEndpoint(CATransportType_t type, const char *address,
135 const char *resourceUri);
138 * @fn CAAdapterCopyRemoteEndpoint
139 * @brief Create CARemoteEndpoint_t duplicate instance.
141 CARemoteEndpoint_t *CAAdapterCopyRemoteEndpoint(
142 const CARemoteEndpoint_t *remoteEndpoint);
145 * @fn CAAdapterFreeRemoteEndpoint
146 * @brief Deallocate CARemoteEndpoint_t instance.
148 void CAAdapterFreeRemoteEndpoint(CARemoteEndpoint_t *remoteEndPoint);
151 * @fn CAParseIPv4AddressInternal
152 * @brief To parse the IP address and port from "ipaddress:port"
153 * @param ipAddrStr [IN] IP address to be parsed
154 * @param ipAddr [OUT] Parsed IP address
155 * @param ipAddr [IN] Buffer length for parsed IP address
156 * @param port [OUT] Parsed Port number
157 * @return #CA_STATUS_OK or Appropriate error code
159 CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
160 size_t ipAddrLen, uint16_t *port);
163 * @fn CAAdapterIsSameSubnet
164 * @brief Check if two ip address belong to same subnet
166 bool CAAdapterIsSameSubnet(const char *ipAddress1, const char *ipAddress2,
167 const char *netMask);
169 * @brief Used to check the multicast server is running or not.
171 * @param serverInfoList [IN] Server information list.
172 * @param ipAddress [IN] Interface address of the server.
173 * @param multicastAddress [IN] Multicast address of the server.
174 * @param port [IN] Port number of the server.
176 * @return true or false.
178 bool CAIsMulticastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
179 const char *multicastAddress, uint16_t port);
182 * @brief Used to check the unicast server is running or not.
184 * @param serverInfoList [IN] Server information list.
185 * @param ipAddress [IN] Ip address of the server.
186 * @param port [IN] Port number of the server.
188 * @return true or false.
190 bool CAIsUnicastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
194 * @brief Used to get the port number based on given information.
196 * @param serverInfoList [IN] Server information list.
197 * @param ipAddress [IN] Ip address of the server.
198 * @param isSecured [IN] specifies whether to get secured or normal unicast server port.
200 * @return positive value on success and 0 on error.
202 uint16_t CAGetServerPort(const u_arraylist_t *serverInfoList, const char *ipAddress,
206 * @brief Used to get the socket fd for given server information.
208 * @param serverInfoList [IN] Server information list.
209 * @param ipAddress [IN] Ip address of the server.
210 * @param isSecured [IN] To check whether it is secured server or not.
211 * @param isMulticast [IN] To check whether it is multicast server or not.
212 * @param type [IN] CA_IPV4, CA_IPV6 etc.
214 * @return positive value on success and -1 on error.
216 int CAGetSocketFdForUnicastServer(const u_arraylist_t *serverInfoList, const char *ipAddress,
217 bool isSecured, bool isMulticast, CATransportType_t type);
220 * @brief Used to add the server information into serverinfo list
222 * @param serverInfoList [INOUT] server information list.
223 * @param info [IN] server informations like ip, port.
225 * @return #CA_STATUS_OK or Appropriate error code
226 * @retval #CA_STATUS_OK Successful
227 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
228 * @retval #CA_STATUS_FAILED Initialization failed
230 CAResult_t CAAddServerInfo(u_arraylist_t *serverInfoList, CAServerInfo_t *info);
233 * @brief Used to remove the server information based on socket fd from server info list.
235 * @param serverInfoList [INOUT] server information list.
236 * @param sockFd [IN] Socket descriptor.
240 void CARemoveServerInfo(u_arraylist_t *serverInfoList, int sockFd);
243 * @brief Used to clear the memory of network inteface list
244 * Memory pointed by infoList will become invalid after this function call.
246 * @param infoList [IN] Network interface list.
250 void CAClearNetInterfaceInfoList(u_arraylist_t *infoList);
253 * @brief Used to clear the memory of server info list.
254 * Memory pointed by serverInfoList will become invalid after this function call.
256 * @param infoList [IN] Server information list.
260 void CAClearServerInfoList(u_arraylist_t *serverInfoList);
265 #endif // CA_ADAPTER_UTILS_H_