replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / connectivity / inc / caipinterface.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 caipinterface.h
23  * This file provides APIs IP client/server/network monitor modules.
24  */
25
26 #ifndef CA_IP_INTERFACE_H_
27 #define CA_IP_INTERFACE_H_
28
29 #include <stdbool.h>
30
31 #include "cacommon.h"
32 #include "cathreadpool.h"
33 #include "uarraylist.h"
34
35 #ifdef __cplusplus
36 extern "C"
37 {
38 #endif
39
40 /**
41  * Enum for defining different server types.
42  */
43 typedef enum
44 {
45     CA_UNICAST_SERVER = 0,      /**< Unicast Server */
46     CA_MULTICAST_SERVER,        /**< Multicast Server */
47     CA_SECURED_UNICAST_SERVER   /**< Secured Unicast Server */
48 } CAAdapterServerType_t;
49
50 /**
51  * Callback to be notified on reception of any data from remote OIC devices.
52  *
53  * @param[in]  sep         network endpoint description.
54  * @param[in]  data          Data received from remote OIC device.
55  * @param[in]  dataLength    Length of data in bytes.
56  * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
57  * @pre  Callback must be registered using CAIPSetPacketReceiveCallback().
58  */
59 typedef CAResult_t (*CAIPPacketReceivedCallback)(const CASecureEndpoint_t *sep,
60                                                  const void *data,
61                                                  uint32_t dataLength);
62
63 /**
64   * Callback to notify error in the IP adapter.
65   *
66   * @param[in]  endpoint       network endpoint description.
67   * @param[in]  data          Data sent/received.
68   * @param[in]  dataLength    Length of data in bytes.
69   * @param[in]  result        result of request from R.I.
70   * @pre  Callback must be registered using CAIPSetPacketReceiveCallback().
71  */
72 typedef void (*CAIPErrorHandleCallback)(const CAEndpoint_t *endpoint, const void *data,
73                                         uint32_t dataLength, CAResult_t result);
74
75 /**
76  * Start IP server.
77  *
78  * @param   threadPool   Thread pool for managing Unicast/Multicast server threads.
79  * @return ::CA_STATUS_OK or Appropriate error code.
80  * @retval ::CA_STATUS_OK  Successful.
81  * @retval ::CA_STATUS_INVALID_PARAM Invalid input data.
82  * @retval ::CA_STATUS_FAILED Initialization failed.
83  */
84 #ifdef SINGLE_THREAD
85 CAResult_t CAIPStartServer();
86 #else
87 CAResult_t CAIPStartServer(const ca_thread_pool_t threadPool);
88 #endif
89
90 /**
91  * Close IP socket.
92  */
93 void CADeInitializeIPGlobals();
94
95 /**
96  * Stop IP server.
97  */
98 void CAIPStopServer();
99
100 /**
101  * Starts receiving the multicast traffic.
102  *
103  * This will be used in case sleepy device wants to start back receiving the multicast
104  * traffic.
105  */
106 CAResult_t CAIPStartListenServer();
107
108 /**
109  * Stops the multicast traffic.
110  *
111  * This is to be used by the sleeping device to stop receiving multicast traffic.
112  * Once this is set no multicast traffic will be received. Device can still receive
113  * the unicast traffic.
114  */
115 CAResult_t CAIPStopListenServer();
116
117 /**
118  * Set this callback for receiving data packets from peer devices.
119  *
120  * @param[in]  callback    Callback to be notified on reception of unicast/multicast data packets.
121  */
122 void CAIPSetPacketReceiveCallback(CAIPPacketReceivedCallback callback);
123
124 /**
125  * Set socket description for sending unicast UDP data.
126  * Once the Unicast server is started,
127  * the same socket descriptor is used for sending the Unicast UDP data.
128  *
129  * @param[in]  socketFD   Socket descriptor used for sending UDP data.
130  */
131 void CAIPSetUnicastSocket(int socketFD);
132
133 /**
134  * Set the port number for sending unicast UDP data.
135  * @param[in]  port  Port number used for sending UDP data.
136  */
137 void CAIPSetUnicastPort(uint16_t port);
138
139 /**
140  * API to send unicast UDP data.
141  *
142  * @param[in]  endpoint          complete network address to send to.
143  * @param[in]  data              Data to be send.
144  * @param[in]  dataLength        Length of data in bytes.
145  * @param[in]  isMulticast       Whether data needs to be sent to multicast ip.
146  */
147 void CAIPSendData(CAEndpoint_t *endpoint,
148                   const void *data,
149                   uint32_t dataLength,
150                   bool isMulticast);
151
152 /**
153  * Get IP adapter connection state.
154  *
155  * @return  True if IP adapter is connected, otherwise false.
156  */
157 bool CAIPIsConnected();
158
159 /**
160  * Pull the Received Data.
161  */
162 void CAIPPullData();
163
164 #define CA_COAP        5683
165 #define CA_SECURE_COAP 5684
166
167 /**
168  * Let the network monitor update the polling interval.
169  * @param   [in] current polling interval
170  *
171  * @return  desired polling interval
172  */
173 int CAGetPollingInterval(int interval);
174
175 /**
176  * Set callback for error handling.
177  *
178  * @param[in]  errorHandleCallback  callback to notify error to the ipadapter.
179  */
180 void CAIPSetErrorHandler(CAIPErrorHandleCallback errorHandleCallback);
181
182 /**
183  * Set udp multicast time to live value to control the scope of the multicasts.
184  * @param[in]  ttl         To be set to any value from 0 to 255.
185  *
186  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
187  */
188 CAResult_t CAIPSetMulticastTTL(size_t ttl);
189
190 /**
191  * Get udp multicast time to live value.
192  * @param[out]  ttl         TTL pointer to get the stored multicast time to live.
193  *
194  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
195  */
196 CAResult_t CAIPGetMulticastTTL(size_t *ttl);
197
198 /**
199  * Create multicast socket to receive multicast message from remote device.
200  */
201 void CreateMulticastSocket();
202
203 /**
204  * Close multicast socket to stop receiving multicast message.
205  */
206 void CloseMulticastSocket();
207 #ifdef __cplusplus
208 }
209 #endif
210
211 #endif /* CA_IP_INTERFACE_H_ */