5cf683ae1b29860127d62dd71dd4a9fd46af2115
[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  * @pre  Callback must be registered using CAIPSetPacketReceiveCallback().
57  */
58 typedef void (*CAIPPacketReceivedCallback)(const CASecureEndpoint_t *sep,
59                                            const void *data,
60                                            uint32_t dataLength);
61
62 /**
63   * Callback to notify error in the IP adapter.
64   *
65   * @param[in]  endpoint       network endpoint description.
66   * @param[in]  data          Data sent/received.
67   * @param[in]  dataLength    Length of data in bytes.
68   * @param[in]  result        result of request from R.I.
69   * @pre  Callback must be registered using CAIPSetPacketReceiveCallback().
70  */
71 typedef void (*CAIPErrorHandleCallback)(const CAEndpoint_t *endpoint, const void *data,
72                                         uint32_t dataLength, CAResult_t result);
73
74 /**
75  * Start IP server.
76  *
77  * @param   threadPool   Thread pool for managing Unicast/Multicast server threads.
78  * @return ::CA_STATUS_OK or Appropriate error code.
79  * @retval ::CA_STATUS_OK  Successful.
80  * @retval ::CA_STATUS_INVALID_PARAM Invalid input data.
81  * @retval ::CA_STATUS_FAILED Initialization failed.
82  */
83 #ifdef SINGLE_THREAD
84 CAResult_t CAIPStartServer();
85 #else
86 CAResult_t CAIPStartServer(const ca_thread_pool_t threadPool);
87 #endif
88
89 /**
90  * Close IP socket.
91  */
92 void CADeInitializeIPGlobals();
93
94 /**
95  * Stop IP server.
96  */
97 void CAIPStopServer();
98
99 /**
100  * Starts receiving the multicast traffic.
101  *
102  * This will be used in case sleepy device wants to start back receiving the multicast
103  * traffic.
104  */
105 CAResult_t CAIPStartListenServer();
106
107 /**
108  * Stops the multicast traffic.
109  *
110  * This is to be used by the sleeping device to stop receiving multicast traffic.
111  * Once this is set no multicast traffic will be received. Device can still receive
112  * the unicast traffic.
113  */
114 CAResult_t CAIPStopListenServer();
115
116 /**
117  * Set this callback for receiving data packets from peer devices.
118  *
119  * @param[in]  callback    Callback to be notified on reception of unicast/multicast data packets.
120  */
121 void CAIPSetPacketReceiveCallback(CAIPPacketReceivedCallback callback);
122
123 /**
124  * Set socket description for sending unicast UDP data.
125  * Once the Unicast server is started,
126  * the same socket descriptor is used for sending the Unicast UDP data.
127  *
128  * @param[in]  socketFD   Socket descriptor used for sending UDP data.
129  */
130 void CAIPSetUnicastSocket(int socketFD);
131
132 /**
133  * Set the port number for sending unicast UDP data.
134  * @param[in]  port  Port number used for sending UDP data.
135  */
136 void CAIPSetUnicastPort(uint16_t port);
137
138 /**
139  * API to send unicast UDP data.
140  *
141  * @param[in]  endpoint          complete network address to send to.
142  * @param[in]  data              Data to be send.
143  * @param[in]  dataLength        Length of data in bytes.
144  * @param[in]  isMulticast       Whether data needs to be sent to multicast ip.
145  */
146 void CAIPSendData(CAEndpoint_t *endpoint,
147                   const void *data,
148                   uint32_t dataLength,
149                   bool isMulticast);
150
151 /**
152  * Get IP adapter connection state.
153  *
154  * @return  True if IP adapter is connected, otherwise false.
155  */
156 bool CAIPIsConnected();
157
158 /**
159  * Pull the Received Data.
160  */
161 void CAIPPullData();
162
163 #define CA_COAP        5683
164 #define CA_SECURE_COAP 5684
165
166 /**
167  * Let the network monitor update the polling interval.
168  * @param   [in] current polling interval
169  *
170  * @return  desired polling interval
171  */
172 int CAGetPollingInterval(int interval);
173
174 /**
175  * Tell the IP server an interface has been added.
176  */
177 void CAWakeUpForChange();
178
179 /**
180  * Set callback for error handling.
181  *
182  * @param[in]  errorHandleCallback  callback to notify error to the ipadapter.
183  */
184 void CAIPSetErrorHandler(CAIPErrorHandleCallback errorHandleCallback);
185
186 #ifdef __cplusplus
187 }
188 #endif
189
190 #endif /* CA_IP_INTERFACE_H_ */