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 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 isSecured [IN] Indicates the data is secure or not.
61 * @pre Callback must be registered using CAIPSetPacketReceiveCallback()
63 typedef void (*CAIPPacketReceivedCallback)(const char *ipAddress, uint16_t port,
64 const void *data, uint32_t dataLength,
68 * @brief Callback to be notified when exception occures on multicast/unicast server.
69 * @param type [IN] Type of server(#CAAdapterServerType_t)
71 * @pre Callback must be registered using CAIPSetExceptionCallback()
73 typedef void (*CAIPExceptionCallback)(CAAdapterServerType_t type);
76 * @brief Initialize IP server
78 * @param threadPool [IN] Thread pool for managing Unicast/Multicast server threads.
80 * @return #CA_STATUS_OK or Appropriate error code
81 * @retval #CA_STATUS_OK Successful
82 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
83 * @retval #CA_STATUS_FAILED Initialization failed
85 CAResult_t CAIPInitializeServer(const ca_thread_pool_t threadPool);
88 * @brief Terminate IP server
91 void CAIPTerminateServer();
94 * @brief Start multicast server for specified multicast address and port
96 * @param localAddress [IN] Local adapter address to which server to be binded.
97 * @param multicastAddress [IN] Multicast group address.
98 * @param multicastPort [IN,OUT] Port number on which server will be running. If binding
99 * the port failed, server starts in the next available port.
101 * @return #CA_STATUS_OK or Appropriate error code
102 * @retval #CA_STATUS_OK Successful
103 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
104 * @retval #CA_SERVER_STARTED_ALREADY Multicast server is already started and running.
105 * @retval #CA_STATUS_FAILED Operation failed
107 CAResult_t CAIPStartMulticastServer(const char *localAddress, const char *multicastAddress,
108 uint16_t multicastPort);
111 * @brief Start unicast server for specified local address and port
113 * @param localAddress [IN] Local adapter address to which server to be binded.
114 * @param port [IN,OUT] Port number on which server will be running. If binding
115 * the port failed, server starts in the next available port.
116 * @param forceStart [IN] Indicate whether to start server forcesfully on specified port
118 * @param secured [IN] True if the secure server to be started, otherwise false.
120 * @return #CA_STATUS_OK or Appropriate error code
121 * @retval #CA_STATUS_OK Successful
122 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
123 * @retval #CA_SERVER_STARTED_ALREADY Unicast server is already started and running.
124 * @retval #CA_STATUS_FAILED Operation failed
126 CAResult_t CAIPStartUnicastServer(const char *localAddress, uint16_t *port, bool forceStart,
130 * @brief Stop servers that are running in particular interface address.
132 * @param interfaceAddress [IN] interface address in which servers are running.
134 * @return #CA_STATUS_OK or Appropriate error code
135 * @retval #CA_STATUS_OK Successful
136 * @retval #CA_STATUS_FAILED Operation failed
138 CAResult_t CAIPStopServer(const char *interfaceAddress);
141 * @brief Used to stop all unicast and multicast servers.
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 CAIPStopAllServers();
150 * @brief Used to get the socket fd based on index value of server info list.
152 * @param index [IN] Index where we need socket fd value.
153 * @param isSecured [IN] For secured unicast server or normal server.
155 * @return positive value on success and -1 on error.
157 int CAGetSocketFdFromUnicastIPServerbyIndex(int16_t index, bool isSecured);
160 * @brief Used to get the number of unicast server currently running.
162 * @param isSecured [IN] To identify whether its secured unicast server or normal server.
164 * @return positive value on success and -1 on error.
166 int16_t CAGetNumberOfUnicastIPServers(bool isSecured);
169 * @brief Used to get the stored socket fd for corresponding ipAddress.
171 * @param ipAddress [IN] IpAddress of server.
172 * @param isSecured [IN] Used to check the server is secured or not.
173 * @param isMulticast [IN] To identify whether its for multicast or unicast.
175 * @return socket fd on success and -1 on error.
177 int CAGetSocketFdFromUnicastIPServer(const char *ipAddress, bool isSecured, bool isMulticast);
180 * @brief Used to get the port number to the corresponding ip for giving interface info.
182 * @param ipAddress [IN] IpAddress of server.
183 * @param isSecured [IN] Used to check the server is secured or not.
185 * @return port number on success and -1 on error.
187 uint16_t CAGetServerPortNum(const char *ipAddress, bool isSecured);
190 * @brief Used to get the port number for corresponding ipAddress.
192 * @param serverInfoList [OUT] ServerInfoList holds unicast and multicast server informations.
194 * @return #CA_STATUS_OK or Appropriate error code
195 * @retval #CA_STATUS_OK Successful
196 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
197 * @retval #CA_STATUS_FAILED Initialization failed
199 CAResult_t CAGetIPServerInfoList(u_arraylist_t **serverInfoList);
202 * @brief Set this callback for receiving data packets from peer devices.
204 * @param callback [IN] Callback to be notified on reception of unicast/multicast data packets.
208 void CAIPSetPacketReceiveCallback(CAIPPacketReceivedCallback callback);
211 * @brief Set this callback for receiving exception notifications.
213 * @param callback [IN] Callback to be notified on occurance of exception on running servers.
217 void CAIPSetExceptionCallback(CAIPExceptionCallback callback);
220 * @brief API to send unicast UDP data
222 * @param remoteAddress [IN] IP address to which data needs to be sent.
223 * @param port [IN] Port to which data needs to be send.
224 * @param data [IN] Data to be send.
225 * @param dataLength [IN] Length of data in bytes
226 * @param isMulticast [IN] Whether data needs to be sent to multicast ip
227 * @param isSecured [IN] Whether data to be sent on secured channel.
229 * @return The number of bytes sent on the network. Returns 0 on error.
230 * @remarks isSecure will be ignored when isMulticast is true.
232 uint32_t CAIPSendData(const char *remoteAddress, uint16_t port, const void *data,
233 uint32_t dataLength, bool isMulticast, bool isSecure);
236 * @brief Callback to be notified when IP adapter connection state changes.
238 * @param ipAddress [IN] IP address of remote OIC device.
239 * @param status [IN] Connection status either #CA_INTERFACE_UP or #CA_INTERFACE_DOWN.
241 * @pre Callback must be registered using CAIPSetConnectionStateChangeCallback()
243 typedef void (*CAIPConnectionStateChangeCallback)(const char *ipAddress,
244 CANetworkStatus_t status);
247 * @brief Initialize IP network monitor
249 * @param threadPool [IN] Thread pool for managing network monitor thread.
251 * @return #CA_STATUS_OK or Appropriate error code
252 * @retval #CA_STATUS_OK Successful
253 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
254 * @retval #CA_STATUS_FAILED Initialization failed
256 CAResult_t CAIPInitializeNetworkMonitor(const ca_thread_pool_t threadPool);
259 * @brief Terminate IP network monitor by removing interface list.
262 void CAIPTerminateNetworkMonitor();
265 * @brief Start network monitoring process. It will start the monitor thread.
267 * @return #CA_STATUS_OK or Appropriate error code
268 * @retval #CA_STATUS_OK Successful
269 * @retval #CA_STATUS_FAILED Operation failed
271 CAResult_t CAIPStartNetworkMonitor();
274 * @brief Stop network monitoring process. It will stop the monitor thread.
276 * @return #CA_STATUS_OK or Appropriate error code
277 * @retval #CA_STATUS_OK Successful
278 * @retval #CA_STATUS_FAILED Operation failed
280 CAResult_t CAIPStopNetworkMonitor();
283 * @brief Get local adapter network information.
285 * @param netInterfaceList [OUT] network interface information list
287 * @return #CA_STATUS_OK or Appropriate error code
288 * @retval #CA_STATUS_OK Successful
289 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
290 * @retval #CA_STATUS_FAILED Operation failed
291 * @remarks interfaceName and ipAddress must be freed using free().
293 CAResult_t CAIPGetInterfaceInfo(u_arraylist_t **netInterfaceList);
296 * @brief Get local adapter network subnet mask.
298 * @param ipAddress [IN] IpAddress which is used for getting subnet mask.
299 * @param subnetMask [OUT] Local adapter interface subnet mask
301 * @return #CA_STATUS_OK or Appropriate error code
302 * @retval #CA_STATUS_OK Successful
303 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
304 * @retval #CA_STATUS_FAILED Operation failed
305 * @remarks subnetMask must be freed using free().
307 CAResult_t CAIPGetInterfaceSubnetMask(const char *ipAddress, char **subnetMask);
310 * @brief Get IP adapter connection state.
312 * @return True if IP adapter is connected, otherwise false
314 bool CAIPIsConnected();
317 * @brief Set callback for receiving local IP adapter connection status.
319 * @param callback [IN] Callback to be notified when IP adapter connection state changes.
322 void CAIPSetConnectionStateChangeCallback(CAIPConnectionStateChangeCallback callback);
328 #endif /* CA_IP_INTERFACE_H_ */