5822383019f3dbf6f51ac8e42c9e6075de91581d
[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
34 #ifdef __cplusplus
35 extern "C"
36 {
37 #endif
38
39 /**
40  * @def VERIFY_NON_NULL
41  * @brief Macro to verify the validity of input argument
42  */
43 #define VERIFY_NON_NULL(arg, log_tag, log_message) \
44     if (NULL == arg ){ \
45         OIC_LOG_V(ERROR, log_tag, "Invalid input:%s", log_message); \
46         return CA_STATUS_INVALID_PARAM; \
47     } \
48
49 /**
50  * @def VERIFY_NON_NULL_RET
51  * @brief Macro to verify the validity of input argument
52  */
53 #define VERIFY_NON_NULL_RET(arg, log_tag, log_message,ret) \
54     if (NULL == arg ){ \
55         OIC_LOG_V(ERROR, log_tag, "Invalid input:%s", log_message); \
56         return ret; \
57     } \
58
59 /**
60  * @def VERIFY_NON_NULL_VOID
61  * @brief Macro to verify the validity of input argument
62  */
63 #define VERIFY_NON_NULL_VOID(arg, log_tag, log_message) \
64     if (NULL == arg ){ \
65         OIC_LOG_V(ERROR, log_tag, "Invalid input:%s", log_message); \
66         return; \
67     } \
68
69 /**
70  * @def IPV4_ADDR_ONE_OCTECT_LEN
71  * @brief Macro to allocate memory for ipv4 address in the form of uint8_t.
72  */
73 #define IPV4_ADDR_ONE_OCTECT_LEN 4
74
75 /**
76  * @brief To log the PDU data
77  */
78 void CALogPDUData(coap_pdu_t *pdu);
79
80 /**
81  * @fn CAAdapterCreateLocalEndpoint
82  * @brief Create CALocalConnectivity_t instance.
83  */
84 CALocalConnectivity_t *CAAdapterCreateLocalEndpoint(CAConnectivityType_t type,
85         const char *address);
86
87 /**
88  * @fn CAAdapterCopyLocalEndpoint
89  * @brief Create CALocalConnectivity_t duplicate instance.
90  */
91 CALocalConnectivity_t *CAAdapterCopyLocalEndpoint(const CALocalConnectivity_t *connectivity);
92
93 /**
94  * @fn CAAdapterFreeLocalEndpoint
95  * @brief Deallocate CALocalConnectivity_t instance.
96  */
97 void CAAdapterFreeLocalEndpoint(CALocalConnectivity_t *localEndPoint);
98
99 /**
100  * @fn CAAdapterCreateRemoteEndpoint
101  * @brief Allocate CARemoteEndpoint_t instance.
102  */
103 CARemoteEndpoint_t *CAAdapterCreateRemoteEndpoint(CAConnectivityType_t type,
104         const char *address, const char *resourceUri);
105
106 /**
107  * @fn CAAdapterCopyRemoteEndpoint
108  * @brief Create CARemoteEndpoint_t duplicate instance.
109  */
110 CARemoteEndpoint_t *CAAdapterCopyRemoteEndpoint(
111     const CARemoteEndpoint_t *remoteEndpoint);
112
113 /**
114  * @fn CAAdapterFreeRemoteEndpoint
115  * @brief Deallocate CARemoteEndpoint_t instance.
116  */
117 void CAAdapterFreeRemoteEndpoint(CARemoteEndpoint_t *remoteEndPoint);
118
119 /**
120  * @fn CAParseIPv4AddressInternal
121  * @brief   To parse the IP address and port from "ipaddress:port"
122  * @param   ipAddrStr   [IN]   IP address to be parsed
123  * @param   ipAddr      [OUT]  Parsed IP address
124  * @param   ipAddr      [IN]   Buffer length for parsed IP address
125  * @param   port        [OUT]  Parsed Port number
126  * @return  #CA_STATUS_OK or Appropriate error code
127  */
128 CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
129                                       size_t ipAddrLen, uint16_t *port);
130
131 /**
132  * @fn CAAdapterIsSameSubnet
133  * @brief Check if two ip address belong to same subnet
134  */
135 bool CAAdapterIsSameSubnet(const char *ipAddress1, const char *ipAddress2,
136                            const char *netMask);
137 #ifdef __cplusplus
138 } /* extern "C" */
139 #endif
140 #endif  // _CA_ADAPTER_UTILS_H_
141