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 caipinterface.h
23 * @brief This file provides APIs IP client/server/network monitor modules
26 #ifndef CA_IP_INTERFACE_H_
27 #define CA_IP_INTERFACE_H_
32 #include "cathreadpool.h"
33 #include "uarraylist.h"
41 * @enum CAAdapterServerType_t
42 * @brief Enum for defining different server types.
46 CA_UNICAST_SERVER = 0, /**< Unicast Server */
47 CA_MULTICAST_SERVER, /**< Multicast Server */
48 CA_SECURED_UNICAST_SERVER /**< Secured Unicast Server */
49 } CAAdapterServerType_t;
52 * @brief Callback to be notified on reception of any data from remote OIC devices.
54 * @param endpoint [IN] network endpoint description
55 * @param data [IN] Data received from remote OIC device.
56 * @param dataLength [IN] Length of data in bytes.
59 * @pre Callback must be registered using CAIPSetPacketReceiveCallback()
61 typedef void (*CAIPPacketReceivedCallback)(const CAEndpoint_t *endpoint,
66 * @brief Callback to notify error in the IP adapter
68 * @param endpoint [IN] [IN] network endpoint description
69 * @param data [IN] Data sent/received
70 * @param dataLength [IN] Length of data in bytes.
71 * @param result [IN] result of request from R.I
73 * @pre Callback must be registered using CAIPSetPacketReceiveCallback()
75 typedef void (*CAIPErrorHandleCallback)(const CAEndpoint_t *endpoint, const void *data,
76 uint32_t dataLength, CAResult_t result);
79 * @brief Callback to be notified when exception occures on multicast/unicast server.
80 * @param type [IN] Type of server(#CAAdapterServerType_t)
82 * @pre Callback must be registered using CAIPSetExceptionCallback()
84 typedef void (*CAIPExceptionCallback)(CAAdapterServerType_t type);
87 * @brief Initialize IP server
89 * @param threadPool [IN] Thread pool for managing Unicast/Multicast server threads.
91 * @return #CA_STATUS_OK or Appropriate error code
92 * @retval #CA_STATUS_OK Successful
93 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
94 * @retval #CA_STATUS_FAILED Initialization failed
96 CAResult_t CAIPInitializeServer(const ca_thread_pool_t threadPool);
99 * @brief Terminate IP server
102 void CAIPTerminateServer();
105 * @brief Start multicast server for specified multicast address and port
107 * @param localAddress [IN] Local adapter address to which server to be binded.
108 * @param multicastAddress [IN] Multicast group address.
109 * @param multicastPort [IN,OUT] Port number on which server will be running. If binding
110 * the port failed, server starts in the next available port.
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 Multicast server is already started and running.
116 * @retval #CA_STATUS_FAILED Operation failed
118 CAResult_t CAIPStartMulticastServer(const char *localAddress, const char *multicastAddress,
119 uint16_t multicastPort);
122 * @brief Start unicast server for specified local address and port
124 * @param localAddress [IN] Local adapter address to which server to be binded.
125 * @param port [IN,OUT] Port number on which server will be running. If binding
126 * the port failed, server starts in the next available port.
127 * @param secured [IN] True if the secure server to be started, otherwise false.
129 * @return #CA_STATUS_OK or Appropriate error code
130 * @retval #CA_STATUS_OK Successful
131 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
132 * @retval #CA_SERVER_STARTED_ALREADY Unicast server is already started and running.
133 * @retval #CA_STATUS_FAILED Operation failed
135 CAResult_t CAIPStartUnicastServer(const char *localAddress, uint16_t *port, bool secured);
138 * @brief Stop servers that are running in particular interface address.
140 * @param interfaceAddress [IN] interface address in which servers are running.
142 * @return #CA_STATUS_OK or Appropriate error code
143 * @retval #CA_STATUS_OK Successful
144 * @retval #CA_STATUS_FAILED Operation failed
146 CAResult_t CAIPStopServer(const char *interfaceAddress);
149 * @brief Used to stop all unicast and multicast servers.
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 CAIPStopAllServers();
158 * @brief Used to get the socket fd based on index value of server info list.
160 * @param index [IN] Index where we need socket fd value.
161 * @param isSecured [IN] For secured unicast server or normal server.
163 * @return positive value on success and -1 on error.
165 int CAGetSocketFdFromUnicastIPServerbyIndex(int16_t index, bool isSecured);
168 * @brief Used to get the number of unicast server currently running.
170 * @param isSecured [IN] To identify whether its secured unicast server or normal server.
172 * @return positive value on success and -1 on error.
174 int16_t CAGetNumberOfUnicastIPServers(bool isSecured);
177 * @brief Used to get the stored socket fd for corresponding ipAddress.
179 * @param ipAddress [IN] IpAddress of server.
180 * @param isSecured [IN] Used to check the server is secured or not.
181 * @param isMulticast [IN] To identify whether its for multicast or unicast.
183 * @return socket fd on success and -1 on error.
185 int CAGetSocketFdFromUnicastIPServer(const char *ipAddress, bool isSecured, bool isMulticast);
188 * @brief Used to get the port number to the corresponding ip for giving interface info.
190 * @param ipAddress [IN] IpAddress of server.
191 * @param isSecured [IN] Used to check the server is secured or not.
193 * @return port number on success and -1 on error.
195 uint16_t CAGetServerPortNum(const char *ipAddress, bool isSecured);
198 * @brief Used to get the port number for corresponding ipAddress.
200 * @param serverInfoList [OUT] ServerInfoList holds unicast and multicast server informations.
202 * @return #CA_STATUS_OK or Appropriate error code
203 * @retval #CA_STATUS_OK Successful
204 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
205 * @retval #CA_STATUS_FAILED Initialization failed
207 CAResult_t CAGetIPServerInfoList(u_arraylist_t **serverInfoList);
210 * @brief Set this callback for receiving data packets from peer devices.
212 * @param callback [IN] Callback to be notified on reception of unicast/multicast data packets.
216 void CAIPSetPacketReceiveCallback(CAIPPacketReceivedCallback callback);
219 * @brief Set this callback for receiving exception notifications.
221 * @param callback [IN] Callback to be notified on occurance of exception on running servers.
225 void CAIPSetExceptionCallback(CAIPExceptionCallback callback);
228 * @brief Set socket description for sending unicast UDP data. Once the Unicast server is started,
229 * the same socket descriptor is used for sending the Unicast UDP data.
231 * @param socketFD [IN] Socket descriptor used for sending UDP data.
234 void CAIPSetUnicastSocket(int socketFD);
237 * @brief Set the port number for sending unicast UDP data
238 * @param port [IN] Port number used for sending UDP data.
241 void CAIPSetUnicastPort(uint16_t port);
244 * @brief API to send unicast UDP data
246 * @param endpoint [IN] complete network address to send to
247 * @param data [IN] Data to be send.
248 * @param dataLength [IN] Length of data in bytes
249 * @param isMulticast [IN] Whether data needs to be sent to multicast ip
251 * @return The number of bytes sent on the network. Returns 0 on error.
252 * @remarks isSecure will be ignored when isMulticast is true.
254 uint32_t CAIPSendData(const CAEndpoint_t *endpoint,
260 * @brief Callback to be notified when IP adapter connection state changes.
262 * @param ipAddress [IN] IP address of remote OIC device.
263 * @param status [IN] Connection status either #CA_INTERFACE_UP or #CA_INTERFACE_DOWN.
265 * @pre Callback must be registered using CAIPSetConnectionStateChangeCallback()
267 typedef void (*CAIPConnectionStateChangeCallback)(const char *ipAddress,
268 CANetworkStatus_t status);
271 * @brief Initialize IP network monitor
273 * @param threadPool [IN] Thread pool for managing network monitor thread.
275 * @return #CA_STATUS_OK or Appropriate error code
276 * @retval #CA_STATUS_OK Successful
277 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
278 * @retval #CA_STATUS_FAILED Initialization failed
280 CAResult_t CAIPInitializeNetworkMonitor(const ca_thread_pool_t threadPool);
283 * @brief Terminate IP network monitor by removing interface list.
286 void CAIPTerminateNetworkMonitor();
289 * @brief Start network monitoring process. It will start the monitor thread.
291 * @return #CA_STATUS_OK or Appropriate error code
292 * @retval #CA_STATUS_OK Successful
293 * @retval #CA_STATUS_FAILED Operation failed
295 CAResult_t CAIPStartNetworkMonitor();
298 * @brief Stop network monitoring process. It will stop the monitor thread.
300 * @return #CA_STATUS_OK or Appropriate error code
301 * @retval #CA_STATUS_OK Successful
302 * @retval #CA_STATUS_FAILED Operation failed
304 CAResult_t CAIPStopNetworkMonitor();
307 * @brief Pull the Received Data
313 * @brief Get local adapter network information.
315 * @param netInterfaceList [OUT] network interface information list
317 * @return #CA_STATUS_OK or Appropriate error code
318 * @retval #CA_STATUS_OK Successful
319 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
320 * @retval #CA_STATUS_FAILED Operation failed
321 * @remarks interfaceName and ipAddress must be freed using free().
323 CAResult_t CAIPGetInterfaceInfo(u_arraylist_t **netInterfaceList);
326 * @brief Get local adapter network subnet mask.
328 * @param ipAddress [IN] IpAddress which is used for getting subnet mask.
329 * @param subnetMask [OUT] Local adapter interface subnet mask
331 * @return #CA_STATUS_OK or Appropriate error code
332 * @retval #CA_STATUS_OK Successful
333 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
334 * @retval #CA_STATUS_FAILED Operation failed
335 * @remarks subnetMask must be freed using free().
337 CAResult_t CAIPGetInterfaceSubnetMask(const char *ipAddress, char **subnetMask);
340 * @brief Get IP adapter connection state.
342 * @return True if IP adapter is connected, otherwise false
344 bool CAIPIsConnected();
347 * @brief Set callback for receiving local IP adapter connection status.
349 * @param callback [IN] Callback to be notified when IP adapter connection state changes.
352 void CAIPSetConnectionStateChangeCallback(CAIPConnectionStateChangeCallback callback);
355 * @brief Set callback for error handling
357 * @param ipErrorCallback [IN] callback to notify error to the ipadapter
360 void CAIPSetErrorHandleCallback(CAIPErrorHandleCallback ipErrorCallback);
366 #endif /* CA_IP_INTERFACE_H_ */