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