merge master code to build iotivity
[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  * Callback to be notified when exception occures on multicast/unicast server.
76  * @param  type   Type of server(#CAAdapterServerType_t).
77  * @pre  Callback must be registered using CAIPSetExceptionCallback().
78  */
79 typedef void (*CAIPExceptionCallback)(CAAdapterServerType_t type);
80
81 /**
82  * Start IP server.
83  *
84  * @param   threadPool   Thread pool for managing Unicast/Multicast server threads.
85  * @return ::CA_STATUS_OK or Appropriate error code.
86  * @retval ::CA_STATUS_OK  Successful.
87  * @retval ::CA_STATUS_INVALID_PARAM Invalid input data.
88  * @retval ::CA_STATUS_FAILED Initialization failed.
89  */
90 #ifdef SINGLE_THREAD
91 CAResult_t CAIPStartServer();
92 #else
93 CAResult_t CAIPStartServer(const ca_thread_pool_t threadPool);
94 #endif
95
96 /**
97  * Stop IP server.
98  */
99 void CAIPStopServer();
100
101 /**
102  * Set this callback for receiving data packets from peer devices.
103  *
104  * @param[in]  callback    Callback to be notified on reception of unicast/multicast data packets.
105  */
106 void CAIPSetPacketReceiveCallback(CAIPPacketReceivedCallback callback);
107
108 /**
109  * Set this callback for receiving exception notifications.
110  *
111  * @param[in]  callback  Callback to be notified on exception on running servers.
112  */
113 void CAIPSetExceptionCallback(CAIPExceptionCallback callback);
114
115 /**
116  * Set socket description for sending unicast UDP data.
117  * Once the Unicast server is started,
118  * the same socket descriptor is used for sending the Unicast UDP data.
119  *
120  * @param[in]  socketFD   Socket descriptor used for sending UDP data.
121  */
122 void CAIPSetUnicastSocket(int socketFD);
123
124 /**
125  * Set the port number for sending unicast UDP data.
126  * @param[in]  port  Port number used for sending UDP data.
127  */
128 void CAIPSetUnicastPort(uint16_t port);
129
130 /**
131  * API to send unicast UDP data.
132  *
133  * @param[in]  endpoint          complete network address to send to.
134  * @param[in]  data              Data to be send.
135  * @param[in]  dataLength        Length of data in bytes.
136  * @param[in]  isMulticast       Whether data needs to be sent to multicast ip.
137  */
138 void CAIPSendData(CAEndpoint_t *endpoint,
139                   const void *data,
140                   uint32_t dataLength,
141                   bool isMulticast);
142
143 /**
144  * Get IP adapter connection state.
145  *
146  * @return  True if IP adapter is connected, otherwise false.
147  */
148 bool CAIPIsConnected();
149
150 /**
151  * Pull the Received Data.
152  */
153 void CAIPPullData();
154
155 #define CA_COAP        5683
156 #define CA_SECURE_COAP 5684
157 #define INTERFACE_NAME_MAX 16
158
159 typedef struct
160 {
161     char name[INTERFACE_NAME_MAX];
162     uint32_t index;
163     uint32_t flags;
164     uint16_t family;
165     uint32_t ipv4addr;        /**< used for IPv4 only. */
166 } CAInterface_t;
167
168 /**
169  * Get a list of CAInterface_t items.
170  *
171  * @return  List of CAInterface_t items.
172  */
173 u_arraylist_t *CAIPGetInterfaceInformation(int desiredIndex);
174
175 /**
176  * @brief   Find a new network interface.
177  *
178  * @return  Description of interface (or NULL if no change)
179  */
180 CAInterface_t *CAFindInterfaceChange();
181
182 /**
183  * @brief   Let the network monitor update the polling interval.
184  * @param   [in] current polling interval
185  *
186  * @return  desired polling interval
187  */
188 int CAGetPollingInterval(int interval);
189
190 /**
191  * @brief   Tell the IP server an interface has been added.
192  */
193 void CAWakeUpForChange();
194
195 /**
196  * Start network monitor.
197  *
198  * @return ::CA_STATUS_OK or Appropriate error code.
199  */
200 CAResult_t CAIPStartNetworkMonitor();
201
202 /**
203  * Stops network monitor.
204  *
205  * @return ::CA_STATUS_OK or Appropriate error code.
206  */
207 CAResult_t CAIPStopNetworkMonitor();
208
209 /**
210  * @brief  Set callback for error handling.
211  *
212  * @param[in]  ipErrorCallback  callback to notify error to the ipadapter.
213  */
214 void CAIPSetErrorHandleCallback(CAIPErrorHandleCallback ipErrorCallback);
215
216 #ifdef __cplusplus
217 }
218 #endif
219
220 #endif /* CA_IP_INTERFACE_H_ */