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 caethernetinterface.h
23 * @brief This file provides APIs ethernet client/server/network monitor modules
26 #ifndef _CA_ETHERNET_INTERFACE_H_
27 #define _CA_ETHERNET_INTERFACE_H_
32 #include "uthreadpool.h" /* for thread pool */
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 isSecured [IN] Indicates the data is secure or not.
58 * @pre Callback must be registered using CAEthernetSetPacketReceiveCallback()
60 typedef void (*CAEthernetPacketReceivedCallback)(const char *ipAddress, const uint32_t port,
61 const void *data, const uint32_t dataLength,
62 const CABool_t isSecured);
65 * @brief Callback to be notified when exception occures on multicast/unicast server.
66 * @param type [IN] Type of server(#CAAdapterServerType_t)
68 * @pre Callback must be registered using CAEthernetSetExceptionCallback()
70 typedef void (*CAEthernetExceptionCallback)(CAAdapterServerType_t type);
73 * @brief Initialize Ethernet server
74 * @param threadPool [IN] Thread pool for managing Unicast/Multicast server threads.
75 * @return #CA_STATUS_OK or Appropriate error code
76 * @retval #CA_STATUS_OK Successful
77 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
78 * @retval #CA_STATUS_FAILED Initialization failed
80 CAResult_t CAEthernetInitializeServer(const u_thread_pool_t threadPool);
83 * @brief Terminate Ethernet server
86 void CAEthernetTerminateServer(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 CAEthernetStartMulticastServer(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 secured [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 CAEthernetStartUnicastServer(const char *localAddress, int16_t *port,
124 const bool forceStart, const bool secured, int32_t *serverFD);
127 * @brief Stop multicast server.
129 * @return #CA_STATUS_OK or Appropriate error code
130 * @retval #CA_STATUS_OK Successful
131 * @retval #CA_STATUS_FAILED Operation failed
133 CAResult_t CAEthernetStopMulticastServer(void);
136 * @brief Stop unicast server.
138 * @return #CA_STATUS_OK or Appropriate error code
139 * @retval #CA_STATUS_OK Successful
140 * @retval #CA_STATUS_FAILED Operation failed
142 CAResult_t CAEthernetStopUnicastServer();
145 * @brief Stop secured unicast server.
147 * @return #CA_STATUS_OK or Appropriate error code
148 * @retval #CA_STATUS_OK Successful
149 * @retval #CA_STATUS_FAILED Operation failed
151 CAResult_t CAEthernetStopSecureUnicastServer();
154 * @brief Get the Unicast Server Information if it is started
156 * @param secure [IN] True if the secure server information needed, otherwise false.
157 * @param ipAddress [OUT]IP address on which server is binded and running.
158 * @param port [OUT]Port number on which server is running
159 * @param serverFD [OUT]Server socket fd.
161 * @return #CA_STATUS_OK or Appropriate error code
162 * @retval #CA_STATUS_OK Successful
163 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
164 * @retval #CA_STATUS_FAILED Operation failed
165 * @remarks ipAddress must be freed using free().
167 CAResult_t CAEthernetGetUnicastServerInfo(const bool secure, char **ipAddress, int16_t *port,
171 * @brief Set this callback for receiving data packets from peer devices.
173 * @param callback [IN] Callback to be notified on reception of unicast/multicast data packets.
177 void CAEthernetSetPacketReceiveCallback(CAEthernetPacketReceivedCallback callback);
180 * @brief Set this callback for receiving exception notifications.
182 * @param callback [IN] Callback to be notified on occurance of exception on running servers.
186 void CAEthernetSetExceptionCallback(CAEthernetExceptionCallback callback);
189 * @brief Set socket description for sending unicast UDP data. Once the Unicast server is started,
190 * the same socket descriptor is used for sending the Unicast UDP data.
192 * @param socketFD [IN] Socket descriptor used for sending UDP data.
195 void CAEthernetSetUnicastSocket(const int32_t socketFD);
198 * @brief Set the port number for sending unicast UDP data
199 * @param port [IN] Port number used for sending UDP data.
202 void CAEthernetSetUnicastPort(const int32_t port);
206 * @brief Set socket description for sending secured (encrypted) unicast UDP data
208 * @param socketFD [IN] Socket descriptor used for sending secured (encrypted) UDP data.
211 void CAEthernetSetSecureUnicastSocket(const int32_t socketFD);
215 * @brief API to send unicast UDP data
217 * @param remoteAddress [IN] IP address to which data needs to be sent.
218 * @param port [IN] Port to which data needs to be send.
219 * @param data [IN] Data to be send.
220 * @param dataLength [IN] Length of data in bytes
221 * @param isMulticast [IN] Whether data needs to be sent to multicast ip
222 * @param isSecure [IN] Whether data to be sent on secured channel.
224 * @return The number of bytes sent on the network. Returns 0 on error.
225 * @remarks isSecure will be ignored when isMulticast is true.
227 uint32_t CAEthernetSendData(const char *remoteAddress, const uint32_t port,
228 const void *data, const uint32_t dataLength,
229 CABool_t isMulticast, CABool_t isSecure);
232 * @brief Callback to be notified when Ethernet adapter connection state changes.
234 * @param ipAddress [IN] IP address of remote OIC device.
235 * @param status [IN] Connection status either #CA_INTERFACE_UP or #CA_INTERFACE_DOWN.
237 * @pre Callback must be registered using CAEthernetSetConnectionStateChangeCallback()
239 typedef void (*CAEthernetConnectionStateChangeCallback)(const char *ipAddress,
240 const CANetworkStatus_t status);
243 * @brief Initialize Ethernet network monitor
245 * @param threadPool [IN] Thread pool for managing network monitor thread.
247 * @return #CA_STATUS_OK or Appropriate error code
248 * @retval #CA_STATUS_OK Successful
249 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
250 * @retval #CA_STATUS_FAILED Initialization failed
252 CAResult_t CAEthernetInitializeNetworkMonitor(const u_thread_pool_t threadPool);
255 * @brief Terminate Ethernet network monitor
258 void CAEthernetTerminateNetworkMonitor(void);
261 * @brief Start network monitoring process.
263 * @return #CA_STATUS_OK or Appropriate error code
264 * @retval #CA_STATUS_OK Successful
265 * @retval #CA_STATUS_FAILED Operation failed
267 CAResult_t CAEthernetStartNetworkMonitor(void);
270 * @brief Stop network monitoring process.
272 * @return #CA_STATUS_OK or Appropriate error code
273 * @retval #CA_STATUS_OK Successful
274 * @retval #CA_STATUS_FAILED Operation failed
276 CAResult_t CAEthernetStopNetworkMonitor(void);
279 * @brief Get local adapter network information.
281 * @param interfaceName [OUT] Local adapter interface name
282 * @param ipAddress [OUT] IP address
284 * @return #CA_STATUS_OK or Appropriate error code
285 * @retval #CA_STATUS_OK Successful
286 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
287 * @retval #CA_STATUS_FAILED Operation failed
288 * @remarks interfaceName and ipAddress must be freed using free().
290 CAResult_t CAEthernetGetInterfaceInfo(char **interfaceName, char **ipAddress);
293 * @brief Get local adapter network subnet mask.
295 * @param subnetMask [OUT] Local adapter interface subnet mask
297 * @return #CA_STATUS_OK or Appropriate error code
298 * @retval #CA_STATUS_OK Successful
299 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
300 * @retval #CA_STATUS_FAILED Operation failed
301 * @remarks subnetMask must be freed using free().
303 CAResult_t CAEthernetGetInterfaceSubnetMask(char **subnetMask);
306 * @brief Get Ethernet adapter connection state.
308 * @return True if Ethernet adapter is connected, otherwise false
310 bool CAEthernetIsConnected(void);
313 * @brief Set callback for receiving local ethernet adapter connection status.
315 * @param callback [IN] Callback to be notified when local Ethernet adapter connection state changes.
318 void CAEthernetSetConnectionStateChangeCallback(CAEthernetConnectionStateChangeCallback callback);
324 #endif //_CA_ETHERNET_INTERFACE_H_