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 ******************************************************************/
24 * This file provides APIs for WIFI adapter - client, server, network
28 #ifndef _CA_WIFI_INTERFACE_H_
29 #define _CA_WIFI_INTERFACE_H_
34 #include "cathreadpool.h"
42 * @enum CAAdapterServerType_t
43 * @brief Enum for defining different server types.
47 CA_UNICAST_SERVER = 0, /**< Unicast Server */
48 CA_MULTICAST_SERVER, /**< Multicast Server */
49 CA_SECURED_UNICAST_SERVER /**< Secured Unicast Server */
50 } CAAdapterServerType_t;
53 * @brief Callback to be notified on reception of any data from remote OIC devices.
54 * @param ipAddress [IN] IP address of remote OIC device.
55 * @param port [IN] Port number on which data is received.
56 * @param data [IN] Data received from remote OIC device.
57 * @param dataLength [IN] Length of data in bytes.
58 * @param isSecure [IN] Indicates the data is secure or not.
60 * @pre Callback must be registered using CAWiFiSetPacketReceiveCallback()
62 typedef void (*CAWiFiPacketReceivedCallback)(const char *ipAddress, uint16_t port,
63 const void *data, uint32_t dataLength, bool isSecure);
66 * @brief Callback to be notified when exception occures on multicast/unicast server.
67 * @param type [IN] Type of server(#CAAdapterServerType_t)
69 * @pre Callback must be registered using CAWiFiSetExceptionCallback()
71 typedef void (*CAWiFiExceptionCallback)(CAAdapterServerType_t type);
74 * @brief Initialize WIFI server
75 * @param threadPool [IN] Thread pool for managing Unicast/Multicast server threads.
76 * @return #CA_STATUS_OK or Appropriate error code
77 * @retval #CA_STATUS_OK Successful
78 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
79 * @retval #CA_STATUS_FAILED Initialization failed
81 CAResult_t CAWiFiInitializeServer(const ca_thread_pool_t threadPool);
85 * @brief Terminate WIFI server
88 void CAWiFiTerminateServer(void);
91 * @brief Start multicast server for specified multicast address and port
93 * @param localAddress [IN] Local adapter address to which server to be binded.
94 * @param multicastAddress [IN] Multicast group address.
95 * @param multicastPort [IN,OUT] Port number on which server will be running. If binding
96 the port failed, server starts in the next available port.
97 * @param serverFD [OUT] Multicast server socket FD.
99 * @return #CA_STATUS_OK or Appropriate error code
100 * @retval #CA_STATUS_OK Successful
101 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
102 * @retval #CA_SERVER_STARTED_ALREADY Multicast server is already started and running.
103 * @retval #CA_STATUS_FAILED Operation failed
105 CAResult_t CAWiFiStartMulticastServer(const char *localAddress, const char *multicastAddress,
106 uint16_t multicastPort, int *serverFD);
109 * @brief Start unicast server for specified local address and port
111 * @param localAddress [IN] Local adapter address to which server to be binded.
112 * @param port [IN,OUT] Port number on which server will be running. If binding
113 the port failed, server starts in the next available port.
114 * @param forceStart [IN] Indicate whether to start server forcesfully on specified port
116 * @param isSecured [IN] True if the secure server to be started, otherwise false.
117 * @param serverFD [OUT] Unicast server socket FD.
119 * @return #CA_STATUS_OK or Appropriate error code
120 * @retval #CA_STATUS_OK Successful
121 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
122 * @retval #CA_SERVER_STARTED_ALREADY Unicast server is already started and running.
123 * @retval #CA_STATUS_FAILED Operation failed
125 CAResult_t CAWiFiStartUnicastServer(const char *localAddress, uint16_t *port,
126 bool forceStart, bool isSecured,
130 * @brief Stop multicast server.
132 * @return #CA_STATUS_OK or Appropriate error code
133 * @retval #CA_STATUS_OK Successful
134 * @retval #CA_STATUS_FAILED Operation failed
136 CAResult_t CAWiFiStopMulticastServer(void);
139 * @brief Stop unicast server.
141 * @return #CA_STATUS_OK or Appropriate error code
142 * @retval #CA_STATUS_OK Successful
143 * @retval #CA_STATUS_FAILED Operation failed
145 CAResult_t CAWiFiStopUnicastServer();
149 * @brief Stop secured unicast server.
151 * @return #CA_STATUS_OK or Appropriate error code
152 * @retval #CA_STATUS_OK Successful
153 * @retval #CA_STATUS_FAILED Operation failed
155 CAResult_t CAWiFiStopSecureUnicastServer();
159 * @brief Get the Unicast Server Information if it is started
161 * @param isSecured [IN] true if the secure server information needed, otherwise false.
162 * @param ipAddress [OUT] IP address on which server is binded and running.
163 * @param port [OUT]Port number on which server is running
164 * @param serverFD [OUT]Server socket fd.
166 * @return #CA_STATUS_OK or Appropriate error code
167 * @retval #CA_STATUS_OK Successful
168 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
169 * @retval #CA_STATUS_FAILED Operation failed
170 * @remarks ipAddress must be freed using free().
172 CAResult_t CAWiFiGetUnicastServerInfo(bool isSecured, char **ipAddress, uint16_t *port,
176 * @brief Set this callback for receiving data packets from peer devices.
178 * @param callback [IN] Callback to be notified on reception of unicast/multicast data packets.
182 void CAWiFiSetPacketReceiveCallback(CAWiFiPacketReceivedCallback callback);
185 * @brief Set this callback for receiving exception notifications.
187 * @param callback [IN] Callback to be notified on occurance of exception on running servers.
191 void CAWiFiSetExceptionCallback(CAWiFiExceptionCallback callback);
194 * @brief Set socket description for sending unicast UDP data. Once the Unicast server is started,
195 * the same socket descriptor is used for sending the Unicast UDP data.
197 * @param socketFD [IN] Socket descriptor used for sending UDP data.
200 void CAWiFiSetUnicastSocket(int socketFD);
204 * @brief Set socket description for sending secured (encrypted) unicast UDP data
206 * @param socketFD [IN] Socket descriptor used for sending secured (encrypted) UDP data.
209 void CAWiFiSetSecureUnicastSocket(int socketFD);
213 * @brief API to send unicast UDP data
215 * @param remoteAddress [IN] IP address to which data needs to be sent.
216 * @param port [IN] Port to which data needs to be send.
217 * @param data [IN] Data to be send.
218 * @param dataLength [IN] Length of data in bytes
219 * @param isMulticast [IN] Whether data needs to be sent to multicast ip
220 * @param isSecured [IN] Whether data to be sent on secured channel.
222 * @return The number of bytes sent on the network. Returns 0 on error.
223 * @remarks isSecure will be ignored when isMulticast is true.
225 uint32_t CAWiFiSendData(const char *remoteAddress, uint16_t port,
226 const void *data, uint32_t dataLength,
227 bool isMulticast, bool isSecured);
230 * @brief Callback to be notified when wifi adapter connection state changes.
232 * @param ipAddress [IN] IP address of remote OIC device.
233 * @param status [IN] Connection status either #CA_INTERFACE_UP or #CA_INTERFACE_DOWN.
235 * @pre Callback must be registered using CAWiFiSetConnectionStateChangeCallback()
237 typedef void (*CAWiFiConnectionStateChangeCallback)(const char *ipAddress,
238 CANetworkStatus_t status);
241 * @brief Initialize Wifi network monitor
243 * @param threadPool [IN] Thread pool for managing network monitor thread.
245 * @return #CA_STATUS_OK or Appropriate error code
246 * @retval #CA_STATUS_OK Successful
247 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
248 * @retval #CA_STATUS_FAILED Initialization failed
250 CAResult_t CAWiFiInitializeNetworkMonitor(const ca_thread_pool_t threadPool);
253 * @brief Terminate WIFI network monitor
256 void CAWiFiTerminateNetworkMonitor(void);
259 * @brief Start network monitoring process.
261 * @return #CA_STATUS_OK or Appropriate error code
262 * @retval #CA_STATUS_OK Successful
263 * @retval #CA_STATUS_FAILED Operation failed
265 CAResult_t CAWiFiStartNetworkMonitor(void);
268 * @brief Stop network monitoring process.
270 * @return #CA_STATUS_OK or Appropriate error code
271 * @retval #CA_STATUS_OK Successful
272 * @retval #CA_STATUS_FAILED Operation failed
274 CAResult_t CAWiFiStopNetworkMonitor(void);
277 * @brief Get local adapter network information.
279 * @param interfaceName [OUT] Local adapter interface name
280 * @param ipAddress [OUT] IP address
282 * @return #CA_STATUS_OK or Appropriate error code
283 * @retval #CA_STATUS_OK Successful
284 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
285 * @retval #CA_STATUS_FAILED Operation failed
286 * @remarks interfaceName and ipAddress must be freed using free().
288 CAResult_t CAWiFiGetInterfaceInfo(char **interfaceName, char **ipAddress);
291 * @brief Get local adapter network subnet mask.
293 * @param subnetMask [OUT] Local adapter interface subnet mask
295 * @return #CA_STATUS_OK or Appropriate error code
296 * @retval #CA_STATUS_OK Successful
297 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
298 * @retval #CA_STATUS_FAILED Operation failed
299 * @remarks subnetMask must be freed using free().
301 CAResult_t CAWiFiGetInterfaceSubnetMask(char **subnetMask);
304 * @brief Get WIFI adapter connection state.
306 * @return True if WIFI adapter is connected, otherwise false
308 bool CAWiFiIsConnected(void);
311 * @brief Set callback for receiving local wifi adapter connection status.
313 * @param callback [IN] Callback to be notified when local WIFI adapter connection state changes.
316 void CAWiFiSetConnectionStateChangeCallback(CAWiFiConnectionStateChangeCallback callback);
322 #endif //_CA_WIFI_INTERFACE_H_