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 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 CAIPSetExceptionCallback()
71 typedef void (*CAIPExceptionCallback)(CAAdapterServerType_t type);
74 * @brief Initialize IP server
76 * @param threadPool [IN] Thread pool for managing Unicast/Multicast server threads.
78 * @return #CA_STATUS_OK or Appropriate error code
79 * @retval #CA_STATUS_OK Successful
80 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
81 * @retval #CA_STATUS_FAILED Initialization failed
83 CAResult_t CAIPInitializeServer(const ca_thread_pool_t threadPool);
86 * @brief Terminate IP server
89 void CAIPTerminateServer();
92 * @brief Start multicast server for specified multicast address and port
94 * @param localAddress [IN] Local adapter address to which server to be binded.
95 * @param multicastAddress [IN] Multicast group address.
96 * @param multicastPort [IN,OUT] Port number on which server will be running. If binding
97 * the port failed, server starts in the next available port.
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 CAIPStartMulticastServer(const char *localAddress, const char *multicastAddress,
106 uint16_t multicastPort);
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 secured [IN] True if the secure server to be started, otherwise false.
116 * @return #CA_STATUS_OK or Appropriate error code
117 * @retval #CA_STATUS_OK Successful
118 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
119 * @retval #CA_SERVER_STARTED_ALREADY Unicast server is already started and running.
120 * @retval #CA_STATUS_FAILED Operation failed
122 CAResult_t CAIPStartUnicastServer(const char *localAddress, uint16_t *port, bool secured);
125 * @brief Stop servers that are running in particular interface address.
127 * @param interfaceAddress [IN] interface address in which servers are running.
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 CAIPStopServer(const char *interfaceAddress);
136 * @brief Used to stop all unicast and multicast servers.
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 CAIPStopAllServers();
145 * @brief Used to get the socket fd based on index value of server info list.
147 * @param index [IN] Index where we need socket fd value.
148 * @param isSecured [IN] For secured unicast server or normal server.
150 * @return positive value on success and -1 on error.
152 int CAGetSocketFdFromUnicastIPServerbyIndex(int16_t index, bool isSecured);
155 * @brief Used to get the number of unicast server currently running.
157 * @param isSecured [IN] To identify whether its secured unicast server or normal server.
159 * @return positive value on success and -1 on error.
161 int16_t CAGetNumberOfUnicastIPServers(bool isSecured);
164 * @brief Used to get the stored socket fd for corresponding ipAddress.
166 * @param ipAddress [IN] IpAddress of server.
167 * @param isSecured [IN] Used to check the server is secured or not.
168 * @param isMulticast [IN] To identify whether its for multicast or unicast.
170 * @return socket fd on success and -1 on error.
172 int CAGetSocketFdFromUnicastIPServer(const char *ipAddress, bool isSecured, bool isMulticast);
175 * @brief Used to get the port number to the corresponding ip for giving interface info.
177 * @param ipAddress [IN] IpAddress of server.
178 * @param isSecured [IN] Used to check the server is secured or not.
180 * @return port number on success and -1 on error.
182 uint16_t CAGetServerPortNum(const char *ipAddress, bool isSecured);
185 * @brief Used to get the port number for corresponding ipAddress.
187 * @param serverInfoList [OUT] ServerInfoList holds unicast and multicast server informations.
189 * @return #CA_STATUS_OK or Appropriate error code
190 * @retval #CA_STATUS_OK Successful
191 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
192 * @retval #CA_STATUS_FAILED Initialization failed
194 CAResult_t CAGetIPServerInfoList(u_arraylist_t **serverInfoList);
197 * @brief Set this callback for receiving data packets from peer devices.
199 * @param callback [IN] Callback to be notified on reception of unicast/multicast data packets.
203 void CAIPSetPacketReceiveCallback(CAIPPacketReceivedCallback callback);
206 * @brief Set this callback for receiving exception notifications.
208 * @param callback [IN] Callback to be notified on occurance of exception on running servers.
212 void CAIPSetExceptionCallback(CAIPExceptionCallback callback);
215 * @brief API to send unicast UDP data
217 * @param endpoint [IN] complete network address to send to
218 * @param data [IN] Data to be send.
219 * @param dataLength [IN] Length of data in bytes
220 * @param isMulticast [IN] Whether data needs to be sent to multicast ip
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 CAIPSendData(const CAEndpoint_t *endpoint,
231 * @brief Callback to be notified when IP adapter connection state changes.
233 * @param ipAddress [IN] IP address of remote OIC device.
234 * @param status [IN] Connection status either #CA_INTERFACE_UP or #CA_INTERFACE_DOWN.
236 * @pre Callback must be registered using CAIPSetConnectionStateChangeCallback()
238 typedef void (*CAIPConnectionStateChangeCallback)(const char *ipAddress,
239 CANetworkStatus_t status);
242 * @brief Initialize IP network monitor
244 * @param threadPool [IN] Thread pool for managing network monitor thread.
246 * @return #CA_STATUS_OK or Appropriate error code
247 * @retval #CA_STATUS_OK Successful
248 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
249 * @retval #CA_STATUS_FAILED Initialization failed
251 CAResult_t CAIPInitializeNetworkMonitor(const ca_thread_pool_t threadPool);
254 * @brief Terminate IP network monitor by removing interface list.
257 void CAIPTerminateNetworkMonitor();
260 * @brief Start network monitoring process. It will start the monitor thread.
262 * @return #CA_STATUS_OK or Appropriate error code
263 * @retval #CA_STATUS_OK Successful
264 * @retval #CA_STATUS_FAILED Operation failed
266 CAResult_t CAIPStartNetworkMonitor();
269 * @brief Stop network monitoring process. It will stop the monitor thread.
271 * @return #CA_STATUS_OK or Appropriate error code
272 * @retval #CA_STATUS_OK Successful
273 * @retval #CA_STATUS_FAILED Operation failed
275 CAResult_t CAIPStopNetworkMonitor();
278 * @brief Get local adapter network information.
280 * @param netInterfaceList [OUT] network interface information list
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 CAIPGetInterfaceInfo(u_arraylist_t **netInterfaceList);
291 * @brief Get local adapter network subnet mask.
293 * @param ipAddress [IN] IpAddress which is used for getting subnet mask.
294 * @param subnetMask [OUT] Local adapter interface subnet mask
296 * @return #CA_STATUS_OK or Appropriate error code
297 * @retval #CA_STATUS_OK Successful
298 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
299 * @retval #CA_STATUS_FAILED Operation failed
300 * @remarks subnetMask must be freed using free().
302 CAResult_t CAIPGetInterfaceSubnetMask(const char *ipAddress, char **subnetMask);
305 * @brief Get IP adapter connection state.
307 * @return True if IP adapter is connected, otherwise false
309 bool CAIPIsConnected();
312 * @brief Set callback for receiving local IP adapter connection status.
314 * @param callback [IN] Callback to be notified when IP adapter connection state changes.
317 void CAIPSetConnectionStateChangeCallback(CAIPConnectionStateChangeCallback callback);
323 #endif /* CA_IP_INTERFACE_H_ */