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 for WIFI adapter - client, server, network monitor modules
26 #ifndef _CA_WIFI_INTERFACE_H_
27 #define _CA_WIFI_INTERFACE_H_
32 #include "uthreadpool.h"
40 * @enum CAAdapterServerType_t
41 * @brief Enum for defining different server types.
45 CA_UNICAST_SERVER = 0, /**< Unicast Server */
46 CA_MULTICAST_SERVER, /**< Multicast Server */
47 CA_SECURED_UNICAST_SERVER /**< Secured Unicast Server */
48 } CAAdapterServerType_t;
51 * @brief Callback to be notified on reception of any data from remote OIC devices.
52 * @param ipAddress [IN] IP address of remote OIC device.
53 * @param port [IN] Port number on which data is received.
54 * @param data [IN] Data received from remote OIC device.
55 * @param dataLength [IN] Length of data in bytes.
56 * @param isSecure [IN] Indicates the data is secure or not.
58 * @pre Callback must be registered using CAWiFiSetPacketReceiveCallback()
60 typedef void (*CAWiFiPacketReceivedCallback)(const char *ipAddress, const uint32_t port,
61 const void *data, const uint32_t dataLength, const CABool_t isSecure);
64 * @brief Callback to be notified when exception occures on multicast/unicast server.
65 * @param type [IN] Type of server(#CAAdapterServerType_t)
67 * @pre Callback must be registered using CAWiFiSetExceptionCallback()
69 typedef void (*CAWiFiExceptionCallback)(CAAdapterServerType_t type);
72 * @brief Initialize WIFI server
73 * @param threadPool [IN] Thread pool for managing Unicast/Multicast server threads.
74 * @return #CA_STATUS_OK or Appropriate error code
75 * @retval #CA_STATUS_OK Successful
76 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
77 * @retval #CA_STATUS_FAILED Initialization failed
79 CAResult_t CAWiFiInitializeServer(const u_thread_pool_t threadPool);
83 * @brief Terminate WIFI server
86 void CAWiFiTerminateServer(void);
89 * @brief Start multicast server for specified multicast address and port
91 * @param localAddress [IN] Local adapter address to which server to be binded.
92 * @param multicastAddress [IN] Multicast group address.
93 * @param multicastPort [IN,OUT] Port number on which server will be running. If binding
94 the port failed, server starts in the next available port.
95 * @param serverFD [OUT] Multicast server socket FD.
97 * @return #CA_STATUS_OK or Appropriate error code
98 * @retval #CA_STATUS_OK Successful
99 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
100 * @retval #CA_SERVER_STARTED_ALREADY Multicast server is already started and running.
101 * @retval #CA_STATUS_FAILED Operation failed
103 CAResult_t CAWiFiStartMulticastServer(const char *localAddress, const char *multicastAddress,
104 const int16_t multicastPort, int32_t *serverFD);
107 * @brief Start unicast server for specified local address and port
109 * @param localAddress [IN] Local adapter address to which server to be binded.
110 * @param port [IN,OUT] Port number on which server will be running. If binding
111 the port failed, server starts in the next available port.
112 * @param forceStart [IN] Indicate whether to start server forcesfully on specified port
114 * @param isSecured [IN] True if the secure server to be started, otherwise false.
115 * @param serverFD [OUT] Unicast server socket FD.
117 * @return #CA_STATUS_OK or Appropriate error code
118 * @retval #CA_STATUS_OK Successful
119 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
120 * @retval #CA_SERVER_STARTED_ALREADY Unicast server is already started and running.
121 * @retval #CA_STATUS_FAILED Operation failed
123 CAResult_t CAWiFiStartUnicastServer(const char *localAddress, int16_t *port,
124 const bool forceStart, const CABool_t isSecured,
128 * @brief Stop multicast server.
130 * @return #CA_STATUS_OK or Appropriate error code
131 * @retval #CA_STATUS_OK Successful
132 * @retval #CA_STATUS_FAILED Operation failed
134 CAResult_t CAWiFiStopMulticastServer(void);
137 * @brief Stop unicast server.
139 * @return #CA_STATUS_OK or Appropriate error code
140 * @retval #CA_STATUS_OK Successful
141 * @retval #CA_STATUS_FAILED Operation failed
143 CAResult_t CAWiFiStopUnicastServer();
147 * @brief Stop secured unicast server.
149 * @return #CA_STATUS_OK or Appropriate error code
150 * @retval #CA_STATUS_OK Successful
151 * @retval #CA_STATUS_FAILED Operation failed
153 CAResult_t CAWiFiStopSecureUnicastServer();
157 * @brief Get the Unicast Server Information if it is started
159 * @param isSecured [IN] true if the secure server information needed, otherwise false.
160 * @param ipAddress [OUT] IP address on which server is binded and running.
161 * @param port [OUT]Port number on which server is running
162 * @param serverFD [OUT]Server socket fd.
164 * @return #CA_STATUS_OK or Appropriate error code
165 * @retval #CA_STATUS_OK Successful
166 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
167 * @retval #CA_STATUS_FAILED Operation failed
168 * @remarks ipAddress must be freed using free().
170 CAResult_t CAWiFiGetUnicastServerInfo(const CABool_t isSecured, char **ipAddress, int16_t *port,
174 * @brief Set this callback for receiving data packets from peer devices.
176 * @param callback [IN] Callback to be notified on reception of unicast/multicast data packets.
180 void CAWiFiSetPacketReceiveCallback(CAWiFiPacketReceivedCallback callback);
183 * @brief Set this callback for receiving exception notifications.
185 * @param callback [IN] Callback to be notified on occurance of exception on running servers.
189 void CAWiFiSetExceptionCallback(CAWiFiExceptionCallback callback);
192 * @brief Set socket description for sending unicast UDP data. Once the Unicast server is started,
193 * the same socket descriptor is used for sending the Unicast UDP data.
195 * @param socketFD [IN] Socket descriptor used for sending UDP data.
198 void CAWiFiSetUnicastSocket(const int32_t socketFD);
202 * @brief Set socket description for sending secured (encrypted) unicast UDP data
204 * @param socketFD [IN] Socket descriptor used for sending secured (encrypted) UDP data.
207 void CAWiFiSetSecureUnicastSocket(const int32_t socketFD);
211 * @brief API to send unicast UDP data
213 * @param remoteAddress [IN] IP address to which data needs to be sent.
214 * @param port [IN] Port to which data needs to be send.
215 * @param data [IN] Data to be send.
216 * @param dataLength [IN] Length of data in bytes
217 * @param isMulticast [IN] Whether data needs to be sent to multicast ip
218 * @param isSecured [IN] Whether data to be sent on secured channel.
220 * @return The number of bytes sent on the network. Returns 0 on error.
221 * @remarks isSecure will be ignored when isMulticast is true.
223 uint32_t CAWiFiSendData(const char *remoteAddress, const uint32_t port,
224 const void *data, const uint32_t dataLength,
225 CABool_t isMulticast, CABool_t isSecured);
228 * @brief Callback to be notified when wifi adapter connection state changes.
230 * @param ipAddress [IN] IP address of remote OIC device.
231 * @param status [IN] Connection status either #CA_INTERFACE_UP or #CA_INTERFACE_DOWN.
233 * @pre Callback must be registered using CAWiFiSetConnectionStateChangeCallback()
235 typedef void (*CAWiFiConnectionStateChangeCallback)(const char *ipAddress,
236 const CANetworkStatus_t status);
239 * @brief Initialize Wifi network monitor
241 * @param threadPool [IN] Thread pool for managing network monitor thread.
243 * @return #CA_STATUS_OK or Appropriate error code
244 * @retval #CA_STATUS_OK Successful
245 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
246 * @retval #CA_STATUS_FAILED Initialization failed
248 CAResult_t CAWiFiInitializeNetworkMonitor(const u_thread_pool_t threadPool);
251 * @brief Terminate WIFI network monitor
254 void CAWiFiTerminateNetworkMonitor(void);
257 * @brief Start network monitoring process.
259 * @return #CA_STATUS_OK or Appropriate error code
260 * @retval #CA_STATUS_OK Successful
261 * @retval #CA_STATUS_FAILED Operation failed
263 CAResult_t CAWiFiStartNetworkMonitor(void);
266 * @brief Stop network monitoring process.
268 * @return #CA_STATUS_OK or Appropriate error code
269 * @retval #CA_STATUS_OK Successful
270 * @retval #CA_STATUS_FAILED Operation failed
272 CAResult_t CAWiFiStopNetworkMonitor(void);
275 * @brief Get local adapter network information.
277 * @param interfaceName [OUT] Local adapter interface name
278 * @param ipAddress [OUT] IP address
280 * @return #CA_STATUS_OK or Appropriate error code
281 * @retval #CA_STATUS_OK Successful
282 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
283 * @retval #CA_STATUS_FAILED Operation failed
284 * @remarks interfaceName and ipAddress must be freed using free().
286 CAResult_t CAWiFiGetInterfaceInfo(char **interfaceName, char **ipAddress);
289 * @brief Get local adapter network subnet mask.
291 * @param subnetMask [OUT] Local adapter interface subnet mask
293 * @return #CA_STATUS_OK or Appropriate error code
294 * @retval #CA_STATUS_OK Successful
295 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
296 * @retval #CA_STATUS_FAILED Operation failed
297 * @remarks subnetMask must be freed using free().
299 CAResult_t CAWiFiGetInterfaceSubnetMask(char **subnetMask);
302 * @brief Get WIFI adapter connection state.
304 * @return True if WIFI adapter is connected, otherwise false
306 bool CAWiFiIsConnected(void);
309 * @brief Set callback for receiving local wifi adapter connection status.
311 * @param callback [IN] Callback to be notified when local WIFI adapter connection state changes.
314 void CAWiFiSetConnectionStateChangeCallback(CAWiFiConnectionStateChangeCallback callback);
320 #endif //_CA_WIFI_INTERFACE_H_