Integrated WIFI/ETHERNET adapters to single IPAdapter.
[platform/upstream/iotivity.git] / resource / csdk / connectivity / inc / caadapterutils.h
1 /* ****************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 /**
22  * @file
23  *
24  * This file contains common utility function for CA transport adaptors.
25  */
26
27 #ifndef CA_ADAPTER_UTILS_H_
28 #define CA_ADAPTER_UTILS_H_
29
30 #include "cacommon.h"
31 #include "logger.h"
32 #include "pdu.h"
33 #include "uarraylist.h"
34
35 #ifdef __cplusplus
36 extern "C"
37 {
38 #endif
39
40 /**
41  * @def VERIFY_NON_NULL
42  * @brief Macro to verify the validity of input argument
43  */
44 #define VERIFY_NON_NULL(arg, log_tag, log_message) \
45     if (NULL == arg ){ \
46         OIC_LOG_V(ERROR, log_tag, "Invalid input:%s", log_message); \
47         return CA_STATUS_INVALID_PARAM; \
48     } \
49
50 /**
51  * @def VERIFY_NON_NULL_RET
52  * @brief Macro to verify the validity of input argument
53  */
54 #define VERIFY_NON_NULL_RET(arg, log_tag, log_message,ret) \
55     if (NULL == arg ){ \
56         OIC_LOG_V(ERROR, log_tag, "Invalid input:%s", log_message); \
57         return ret; \
58     } \
59
60 /**
61  * @def VERIFY_NON_NULL_VOID
62  * @brief Macro to verify the validity of input argument
63  */
64 #define VERIFY_NON_NULL_VOID(arg, log_tag, log_message) \
65     if (NULL == arg ){ \
66         OIC_LOG_V(ERROR, log_tag, "Invalid input:%s", log_message); \
67         return; \
68     } \
69
70 /**
71  * @brief Length of network interface name.
72  */
73 #define CA_INTERFACE_NAME_SIZE 16
74
75 /**
76  * @def IPV4_ADDR_ONE_OCTECT_LEN
77  * @brief Macro to allocate memory for ipv4 address in the form of uint8_t.
78  */
79 #define IPV4_ADDR_ONE_OCTECT_LEN 4
80
81 /**
82  * @brief Network Interface Information.
83  */
84 typedef struct
85 {
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**/
89 } CANetInfo_t;
90
91 /**
92  * @brief unicast and multicast server information.
93  */
94 typedef struct
95 {
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 **/
105 } CAServerInfo_t;
106
107 /**
108  * @brief To log the PDU data
109  */
110 void CALogPDUData(coap_pdu_t *pdu);
111
112 /**
113  * @fn CAAdapterCreateLocalEndpoint
114  * @brief Create CALocalConnectivity_t instance.
115  */
116 CALocalConnectivity_t *CAAdapterCreateLocalEndpoint(CATransportType_t type, const char *address);
117
118 /**
119  * @fn CAAdapterCopyLocalEndpoint
120  * @brief Create CALocalConnectivity_t duplicate instance.
121  */
122 CALocalConnectivity_t *CAAdapterCopyLocalEndpoint(const CALocalConnectivity_t *connectivity);
123
124 /**
125  * @fn CAAdapterFreeLocalEndpoint
126  * @brief Deallocate CALocalConnectivity_t instance.
127  */
128 void CAAdapterFreeLocalEndpoint(CALocalConnectivity_t *localEndPoint);
129
130 /**
131  * @fn CAAdapterCreateRemoteEndpoint
132  * @brief Allocate CARemoteEndpoint_t instance.
133  */
134 CARemoteEndpoint_t *CAAdapterCreateRemoteEndpoint(CATransportType_t type, const char *address,
135                                                   const char *resourceUri);
136
137 /**
138  * @fn CAAdapterCopyRemoteEndpoint
139  * @brief Create CARemoteEndpoint_t duplicate instance.
140  */
141 CARemoteEndpoint_t *CAAdapterCopyRemoteEndpoint(
142     const CARemoteEndpoint_t *remoteEndpoint);
143
144 /**
145  * @fn CAAdapterFreeRemoteEndpoint
146  * @brief Deallocate CARemoteEndpoint_t instance.
147  */
148 void CAAdapterFreeRemoteEndpoint(CARemoteEndpoint_t *remoteEndPoint);
149
150 /**
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
158  */
159 CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
160                                       size_t ipAddrLen, uint16_t *port);
161
162 /**
163  * @fn CAAdapterIsSameSubnet
164  * @brief Check if two ip address belong to same subnet
165  */
166 bool CAAdapterIsSameSubnet(const char *ipAddress1, const char *ipAddress2,
167                            const char *netMask);
168 /**
169  * @brief  Used to check the multicast server is running or not.
170  *
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.
175  *
176  * @return  true or false.
177  */
178 bool CAIsMulticastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
179                                 const char *multicastAddress, uint16_t port);
180
181 /**
182  * @brief  Used to check the unicast server is running or not.
183  *
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.
187  *
188  * @return  true or false.
189  */
190 bool CAIsUnicastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
191                               uint16_t port);
192
193 /**
194  * @brief  Used to get the port number based on given information.
195  *
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.
199  *
200  * @return  positive value on success and 0 on error.
201  */
202 uint16_t CAGetServerPort(const u_arraylist_t *serverInfoList, const char *ipAddress,
203                          bool isSecured);
204
205 /**
206  * @brief  Used to get the socket fd for given server information.
207  *
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.
213
214  * @return  positive value on success and -1 on error.
215  */
216 int CAGetSocketFdForUnicastServer(const u_arraylist_t *serverInfoList, const char *ipAddress,
217                                   bool isSecured, bool isMulticast, CATransportType_t type);
218
219 /**
220  * @brief  Used to add the server information into serverinfo list
221  *
222  * @param   serverInfoList     [INOUT] server information list.
223  * @param   info               [IN] server informations like ip, port.
224  *
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
229  */
230 CAResult_t CAAddServerInfo(u_arraylist_t *serverInfoList, CAServerInfo_t *info);
231
232 /**
233  * @brief  Used to remove the server information based on socket fd from server info list.
234  *
235  * @param   serverInfoList  [INOUT] server information list.
236  * @param   sockFd          [IN] Socket descriptor.
237  *
238  * @return  None
239  */
240 void CARemoveServerInfo(u_arraylist_t *serverInfoList, int sockFd);
241
242 /**
243  * @brief  Used to clear the memory of network inteface list
244  *         Memory pointed by infoList will become invalid after this function call.
245  *
246  * @param   infoList  [IN] Network interface list.
247  *
248  * @return  None
249  */
250 void CAClearNetInterfaceInfoList(u_arraylist_t *infoList);
251
252 /**
253  * @brief  Used to clear the memory of server info list.
254  *         Memory pointed by serverInfoList will become invalid after this function call.
255  *
256  * @param   infoList  [IN] Server information list.
257  *
258  * @return  None
259  */
260 void CAClearServerInfoList(u_arraylist_t *serverInfoList);
261
262 #ifdef __cplusplus
263 } /* extern "C" */
264 #endif
265 #endif  // CA_ADAPTER_UTILS_H_
266