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_singlethread.h
23 * @brief This file provides APIs for WIFI adapter - client, server, network monitor modules
26 #ifndef _CA_WIFI_INTERFACE_SINGLETHREAD_H_
27 #define _CA_WIFI_INTERFACE_SINGLETHREAD_H_
39 * @enum CAAdapterServerType_t
40 * @brief Enum for defining different server types.
44 CA_UNICAST_SERVER = 0, /**< Unicast Server */
45 CA_MULTICAST_SERVER, /**< Multicast Server */
46 CA_SECURED_UNICAST_SERVER /**< Secured Unicast Server */
47 } CAAdapterServerType_t;
50 * @brief Callback to be notified on reception of any data from remote OIC devices.
51 * @param ipAddress [IN] IP address of remote OIC device.
52 * @param port [IN] Port number on which data is received.
53 * @param data [IN] Data received from remote OIC device.
54 * @param dataLength [IN] Length of data in bytes.
56 * @pre Callback must be registered using CAWiFiSetPacketReceiveCallback()
58 typedef void (*CAWiFiPacketReceivedCallback)(const char *ipAddress, uint32_t port,
59 const void *data, uint32_t dataLength);
62 * @brief Callback to be notified when exception occures on multicast/unicast server.
63 * @param type [IN] Type of server(#CAAdapterServerType_t)
65 * @pre Callback must be registered using CAWiFiSetExceptionCallback()
67 typedef void (*CAWiFiExceptionCallback)(CAAdapterServerType_t type);
70 * @brief Initialize WIFI server
71 * @return #CA_STATUS_OK or Appropriate error code
72 * @retval #CA_STATUS_OK Successful
73 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
74 * @retval #CA_STATUS_FAILED Initialization failed
76 CAResult_t CAWiFiInitializeServer(void);
79 * @brief Terminate WIFI server
82 void CAWiFiTerminateServer(void);
85 * @brief Start multicast server for specified multicast address and port
87 * @param localAddress [IN] Local adapter address to which server to be binded.
88 * @param multicastAddress [IN] Multicast group address.
89 * @param multicastPort [IN,OUT] Port number on which server will be running. If binding
90 the port failed, server starts in the next available port.
91 * @param serverFD [OUT] Multicast server socket FD.
93 * @return #CA_STATUS_OK or Appropriate error code
94 * @retval #CA_STATUS_OK Successful
95 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
96 * @retval #CA_SERVER_STARTED_ALREADY Multicast server is already started and running.
97 * @retval #CA_STATUS_FAILED Operation failed
99 CAResult_t CAWiFiStartMulticastServer(const char *localAddress, const char *multicastAddress,
100 int16_t multicastPort, int *serverFD);
103 * @brief Start unicast server for specified local address and port
105 * @param localAddress [IN] Local adapter address to which server to be binded.
106 * @param port [IN,OUT] Port number on which server will be running. If binding
107 the port failed, server starts in the next available port.
108 * @param forceStart [IN] Indicate whether to start server forcesfully on specified port
110 * @param serverFD [OUT] Unicast server socket FD.
112 * @return #CA_STATUS_OK or Appropriate error code
113 * @retval #CA_STATUS_OK Successful
114 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
115 * @retval #CA_SERVER_STARTED_ALREADY Unicast server is already started and running.
116 * @retval #CA_STATUS_FAILED Operation failed
118 CAResult_t CAWiFiStartUnicastServer(const char *localAddress, int16_t *port,
119 bool forceStart, int *serverFD);
122 * @brief Stop multicast server.
124 * @return #CA_STATUS_OK or Appropriate error code
125 * @retval #CA_STATUS_OK Successful
126 * @retval #CA_STATUS_FAILED Operation failed
128 CAResult_t CAWiFiStopMulticastServer(void);
131 * @brief Stop unicast server.
133 * @return #CA_STATUS_OK or Appropriate error code
134 * @retval #CA_STATUS_OK Successful
135 * @retval #CA_STATUS_FAILED Operation failed
137 CAResult_t CAWiFiStopUnicastServer();
141 * @brief Stop secured unicast server.
143 * @return #CA_STATUS_OK or Appropriate error code
144 * @retval #CA_STATUS_OK Successful
145 * @retval #CA_STATUS_FAILED Operation failed
147 CAResult_t CAWiFiStopSecureUnicastServer();
151 * @brief Get the Unicast Server Information if it is started
152 * @param ipAddress [OUT] IP address on which server is binded and running.
153 * @param port [OUT]Port number on which server is running
154 * @param serverFD [OUT]Server socket fd.
156 * @return #CA_STATUS_OK or Appropriate error code
157 * @retval #CA_STATUS_OK Successful
158 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
159 * @retval #CA_STATUS_FAILED Operation failed
160 * @remarks ipAddress must be freed using free().
162 CAResult_t CAWiFiGetUnicastServerInfo(char **ipAddress, int16_t *port, int *serverFD);
165 * @brief Set this callback for receiving data packets from peer devices.
166 * @param callback [IN] Callback to be notified on reception of unicast/multicast data packets.
170 void CAWiFiSetPacketReceiveCallback(CAWiFiPacketReceivedCallback callback);
173 * @brief Pull the Received Data
176 void CAWiFiPullData();
179 * @brief Set this callback for receiving exception notifications.
181 * @param callback [IN] Callback to be notified on occurance of exception on running servers.
185 void CAWiFiSetExceptionCallback(CAWiFiExceptionCallback callback);
188 * @brief Set socket description for sending unicast UDP data. Once the Unicast server is started,
189 * the same socket descriptor is used for sending the Unicast UDP data.
191 * @param socketFD [IN] Socket descriptor used for sending UDP data.
194 void CAWiFiSetUnicastSocket(int socketFD);
198 * @brief Set socket description for sending secured (encrypted) unicast UDP data
200 * @param socketFD [IN] Socket descriptor used for sending secured (encrypted) UDP data.
203 void CAWiFiSetSecureUnicastSocket(int socketFD);
207 * @brief API to send unicast UDP data
209 * @param remoteAddress [IN] IP address to which data needs to be sent.
210 * @param port [IN] Port to which data needs to be send.
211 * @param data [IN] Data to be send.
212 * @param dataLength [IN] Length of data in bytes
213 * @param isMulticast [IN] Whether data needs to be sent to multicast ip
215 * @return The number of bytes sent on the network. Returns 0 on error.
217 uint32_t CAWiFiSendData(const char *remoteAddress, uint32_t port,
218 const void *data, uint32_t dataLength, bool isMulticast);
221 * @brief Callback to be notified when wifi adapter connection state changes.
223 * @param ipAddress [IN] IP address of remote OIC device.
224 * @param status [IN] Connection status either #CA_INTERFACE_UP or #CA_INTERFACE_DOWN.
226 * @pre Callback must be registered using CAWiFiSetConnectionStateChangeCallback()
228 typedef void (*CAWiFiConnectionStateChangeCallback)(const char *ipAddress,
229 CANetworkStatus_t status);
232 * @brief Initialize Wifi network monitor
233 * @return #CA_STATUS_OK or Appropriate error code
234 * @retval #CA_STATUS_OK Successful
235 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
236 * @retval #CA_STATUS_FAILED Initialization failed
238 CAResult_t CAWiFiInitializeNetworkMonitor(void);
241 * @brief Terminate WIFI network monitor
244 void CAWiFiTerminateNetworkMonitor(void);
247 * @brief Start network monitoring process.
249 * @return #CA_STATUS_OK or Appropriate error code
250 * @retval #CA_STATUS_OK Successful
251 * @retval #CA_STATUS_FAILED Operation failed
253 CAResult_t CAWiFiStartNetworkMonitor(void);
256 * @brief Stop network monitoring process.
258 * @return #CA_STATUS_OK or Appropriate error code
259 * @retval #CA_STATUS_OK Successful
260 * @retval #CA_STATUS_FAILED Operation failed
262 CAResult_t CAWiFiStopNetworkMonitor(void);
265 * @brief Get local adapter network information.
267 * @param interfaceName [OUT] Local adapter interface name
268 * @param ipAddress [OUT] IP address
270 * @return #CA_STATUS_OK or Appropriate error code
271 * @retval #CA_STATUS_OK Successful
272 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
273 * @retval #CA_STATUS_FAILED Operation failed
274 * @remarks interfaceName and ipAddress must be freed using free().
276 CAResult_t CAWiFiGetInterfaceInfo(char **interfaceName, char **ipAddress);
279 * @brief Get WIFI adapter connection state.
281 * @return True if WIFI adapter is connected, otherwise false
283 bool CAWiFiIsConnected(void);
286 * @brief Set callback for receiving local wifi adapter connection status.
288 * @param callback [IN] Callback to be notified when local WIFI adapter connection state changes.
291 void CAWiFiSetConnectionStateChangeCallback(CAWiFiConnectionStateChangeCallback callback);
297 #endif //_CA_WIFI_INTERFACE_SINGLETHREAD_H_