1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
22 * @file cawifiinterface.h
23 * @brief This file provides APIs wifi client/server/network monitor modules
26 #ifndef _CA_WIFI_INTERFACE_
27 #define _CA_WIFI_INTERFACE_
32 #include "uthreadpool.h"
40 * @enum CAAdapterServerType_t
41 * @brief Enum for defining different server types.
45 CA_UNICAST_SERVER = 0,
47 CA_SECURED_UNICAST_SERVER
48 } CAAdapterServerType_t;
51 * @fn CAWiFiPacketReceivedCallback
52 * @brief Callback to be notified on receival of any data from remote OIC devices.
54 * @param[in] ipAddress IP address of remote OIC device.
55 * @param[in] port Port number on which data is received.
56 * @param[in] data Data received from remote OIC device.
57 * @param[in] dataLength Length of data in bytes.
59 * @pre Callback must be registered using CAWiFiSetPacketReceiveCallback()
61 typedef void (*CAWiFiPacketReceivedCallback)(const char *ipAddress, const uint32_t port,
62 const void *data, const uint32_t dataLength);
65 * @fn CAWiFiExceptionCallback
66 * @brief Callback to be notified when exception occures on multicast/unicast server.
68 * @param[in] type Type of server either #CA_UNICAST_SERVER or $CA_MULTICAST_SERVER
70 * @pre Callback must be registered using CAWiFiSetExceptionCallback()
72 typedef void (*CAWiFiExceptionCallback)(CAAdapterServerType_t type);
75 * @fn CAWiFiInitializeServer
76 * @brief API to initialize Wifi server
78 * @param[in] threadPool Thread pool for managing Unicast/Multicast server threads.
80 * @return #CA_STATUS_OK on success otherwise proper error code.
81 * @retval #CA_STATUS_OK Successful
82 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
83 * @retval #CA_STATUS_FAILED Initialization failed
85 CAResult_t CAWiFiInitializeServer(const u_thread_pool_t threadPool);
89 * @fn CAWiFiInitializeServer
90 * @brief API to initialize Wifi server
92 * @return #CA_STATUS_OK on success otherwise proper error code.
93 * @retval #CA_STATUS_OK Successful
94 * @retval #CA_STATUS_FAILED Initialization failed
96 CAResult_t CAWiFiInitializeServer(void);
100 * @fn CAWiFiTerminateServer
101 * @brief API to terminate Wifi server
103 void CAWiFiTerminateServer(void);
106 * @fn CAWiFiStartMulticastServer
107 * @brief API to start multicast server for specified multicast address and port
109 * @param[in] localAddress Local adapter address to which server to be binded.
110 * @param[in] multicastAddress Multicast group address.
111 * @param[in] multicastPort Port number on which server to be running.
112 * @param[out] serverFD Multicast server socket FD.
114 * @return #CA_STATUS_OK on success otherwise proper error code.
115 * @retval #CA_STATUS_OK Successful
116 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
117 * @retval #CA_SERVER_STARTED_ALREADY Multicast server is already started and running.
118 * @retval #CA_STATUS_FAILED Operation failed
120 CAResult_t CAWiFiStartMulticastServer(const char *localAddress, const char *multicastAddress,
121 const int16_t multicastPort, int32_t *serverFD);
124 * @fn CAWiFiStartUnicastServer
125 * @brief API to start unicast server for specified local address and port
127 * @param[in] localAddress Local adapter address to which server to be binded.
128 * @param[in][out] port Port number on which server to be running.
129 * Port number on which server actually started will be returned.
130 * @param[in] forceStart Indicate whether to start server forcesfully on specified port or not.
131 * @param[out] serverFD Unicast server socket FD.
133 * @return #CA_STATUS_OK on success otherwise proper error code.
134 * @retval #CA_STATUS_OK Successful
135 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
136 * @retval #CA_SERVER_STARTED_ALREADY Unicast server is already started and running.
137 * @retval #CA_STATUS_FAILED Operation failed
139 CAResult_t CAWiFiStartUnicastServer(const char *localAddress, int16_t *port,
140 const bool forceStart, int32_t *serverFD);
143 * @fn CAWiFiStopMulticastServer
144 * @brief API to stop multicast server.
146 * @return #CA_STATUS_OK on success otherwise proper error code.
147 * @retval #CA_STATUS_OK Successful
148 * @retval #CA_STATUS_FAILED Operation failed
150 CAResult_t CAWiFiStopMulticastServer(void);
153 * @fn CAWiFiStopUnicastServer
154 * @brief API to stop unicast server.
156 * @return #CA_STATUS_OK on success otherwise proper error code.
157 * @retval #CA_STATUS_OK Successful
158 * @retval #CA_STATUS_FAILED Operation failed
160 CAResult_t CAWiFiStopUnicastServer();
163 * @fn CAWiFiGetUnicastServerInfo
164 * @brief API to get running unicast server information.
165 * @remarks @ipAddress must be freed using free().
167 * @param[in] ipAddress IP address on which server is binded and running.
168 * @param[out] port Port number on which server is running
169 * @param[out] serverFD Server socket fd.
171 * @return #CA_STATUS_OK on success otherwise proper error code.
172 * @retval #CA_STATUS_OK Successful
173 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
174 * @retval #CA_STATUS_FAILED Operation failed
176 CAResult_t CAWiFiGetUnicastServerInfo(char **ipAddress, int16_t *port, int32_t *serverFD);
179 * @fn CAWiFiSetPacketReceiveCallback
180 * @brief API to set callback for receiving data packets from peer devices.
182 * @param[in] callback Callback to be notified on receival of unicast/multicast data packets.
184 * @return #CA_STATUS_OK on success otherwise proper error code.
185 * @retval #CA_STATUS_OK Successful
186 * @retval #CA_STATUS_FAILED Operation failed
188 void CAWiFiSetPacketReceiveCallback(CAWiFiPacketReceivedCallback callback);
193 * @brief API to pull data
195 void CAWiFiPullData();
199 * @fn CAWiFiSetExceptionCallback
200 * @brief API to set callback for receiving exception notifications.
202 * @param[in] callback Callback to be notified on occurance of exception running servers.
204 * @return #CA_STATUS_OK on success otherwise proper error code.
205 * @retval #CA_STATUS_OK Successful
206 * @retval #CA_STATUS_FAILED Operation failed
208 void CAWiFiSetExceptionCallback(CAWiFiExceptionCallback callback);
211 * @fn CAWiFiSetUnicastSocket
212 * @brief API to set socket description for sending unicast UDP data
214 * @param[in] socketFD Socket descriptor used for sending UDP data.
217 void CAWiFiSetUnicastSocket(const int32_t socketFD);
220 * @fn CAWiFiSendUnicastData
221 * @brief API to send unicast UDP data
223 * @param[in] remoteAddress IP address to which data needs to be send.
224 * @param[in] port Port to which data needs to be send.
225 * @param[in] data Data to be send.
226 * @param[in] dataLength Length of data in bytes
227 * @param[in] isMulticast whether data needs to be sent to multicast ip
228 * @param[out] sentLength Number of bytes actually sent
230 * @return #CA_STATUS_OK on success otherwise proper error code.
231 * @retval #CA_STATUS_OK Successful
232 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
233 * @retval #CA_STATUS_FAILED Operation failed
235 uint32_t CAWiFiSendData(const char *remoteAddress, const uint32_t port,
236 const void *data, const uint32_t dataLength, bool isMulticast);
239 * @fn CAWiFiConnectionStateChangeCallback
240 * @brief Callback to be notified when wifi adapter connection state changes.
242 * @param[in] ipAddress IP address of remote OIC device.
243 * @param[in] status Connection status either #CA_INTERFACE_UP or #CA_INTERFACE_DOWN.
245 * @pre Callback must be registered using CAWiFiSetConnectionStateChangeCallback()
247 typedef void (*CAWiFiConnectionStateChangeCallback)(const char *ipAddress,
248 const CANetworkStatus_t status);
251 * @fn CAWiFiInitializeNetworkMonitor
252 * @brief API to initialize Wifi network monitor
254 * @param[in] threadPool Thread pool for managing network monitor thread.
256 * @return #CA_STATUS_OK on success otherwise proper error code.
257 * @retval #CA_STATUS_OK Successful
258 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
259 * @retval #CA_STATUS_FAILED Initialization failed
261 CAResult_t CAWiFiInitializeNetworkMonitor(const u_thread_pool_t threadPool);
265 * @fn CAWiFiInitializeServer
266 * @brief API to initialize Wifi server
268 * @return #CA_STATUS_OK on success otherwise proper error code.
269 * @retval #CA_STATUS_OK Successful
270 * @retval #CA_STATUS_FAILED Initialization failed
272 CAResult_t CAWiFiInitializeNetworkMonitor(void);
276 * @fn CAWiFiTerminateNetworkMonitor
277 * @brief API to terminate Wifi network monitor
279 void CAWiFiTerminateNetworkMonitor(void);
282 * @fn CAWiFiStartNetworkMonitor
283 * @brief API to start network monitoring process.
285 * @return #CA_STATUS_OK on success otherwise proper error code.
286 * @retval #CA_STATUS_OK Successful
287 * @retval #CA_STATUS_FAILED Operation failed
289 CAResult_t CAWiFiStartNetworkMonitor(void);
292 * @fn CAWiFiStopNetworkMonitor
293 * @brief API to stop network monitoring process.
295 * @return #CA_STATUS_OK on success otherwise proper error code.
296 * @retval #CA_STATUS_OK Successful
297 * @retval #CA_STATUS_FAILED Operation failed
299 CAResult_t CAWiFiStopNetworkMonitor(void);
302 * @fn CAWiFiGetInterfaceInfo
303 * @brief API to get local adapter network information.
304 * @remarks @interfaceName and @ipAddress must be freed using free().
306 * @param[out] interfaceName Local adapter interface name
307 * @param[out] ipAddress IP address
309 * @return #CA_STATUS_OK on success otherwise proper error code.
310 * @retval #CA_STATUS_OK Successful
311 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
312 * @retval #CA_STATUS_FAILED Operation failed
314 CAResult_t CAWiFiGetInterfaceInfo(char **interfaceName, char **ipAddress);
317 * @fn CAWiFiIsConnected
318 * @brief API to get wifi adapter connection state.
320 * @return true if wifi adapter is connected, otherwise false
322 bool CAWiFiIsConnected(void);
325 * @fn CAWiFiSetConnectionStateChangeCallback
326 * @brief API to set callback for receiving local wifi adapter connection status.
328 * @param[in] callback Callback to be notified when local wifi adapter connection state changes.
331 void CAWiFiSetConnectionStateChangeCallback(CAWiFiConnectionStateChangeCallback callback);
337 #endif //_CA_WIFI_INTERFACE_