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 notify error in the IP adapter
70 * @param ipAddress [IN] IP address of the device.
71 * @param port [IN] Port number
72 * @param data [IN] Data sent/received
73 * @param dataLength [IN] Length of data in bytes.
74 * @param isSecured [IN] flag to indicate if data is secured
75 * @param result [IN] result of request from R.I
77 * @pre Callback must be registered using CAIPSetPacketReceiveCallback()
79 typedef void (*CAIPErrorHandleCallback)(const char *ipAddress, uint16_t port,
80 const void *data, uint32_t dataLength,
81 bool isSecured, CAResult_t result);
84 * @brief Callback to be notified when exception occures on multicast/unicast server.
85 * @param type [IN] Type of server(#CAAdapterServerType_t)
87 * @pre Callback must be registered using CAIPSetExceptionCallback()
89 typedef void (*CAIPExceptionCallback)(CAAdapterServerType_t type);
92 * @brief Initialize IP server
94 * @param threadPool [IN] Thread pool for managing Unicast/Multicast server threads.
96 * @return #CA_STATUS_OK or Appropriate error code
97 * @retval #CA_STATUS_OK Successful
98 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
99 * @retval #CA_STATUS_FAILED Initialization failed
101 CAResult_t CAIPInitializeServer(const ca_thread_pool_t threadPool);
104 * @brief Terminate IP server
107 void CAIPTerminateServer();
110 * @brief Start multicast server for specified multicast address and port
112 * @param localAddress [IN] Local adapter address to which server to be binded.
113 * @param multicastAddress [IN] Multicast group address.
114 * @param multicastPort [IN,OUT] Port number on which server will be running. If binding
115 * the port failed, server starts in the next available port.
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 Multicast server is already started and running.
121 * @retval #CA_STATUS_FAILED Operation failed
123 CAResult_t CAIPStartMulticastServer(const char *localAddress, const char *multicastAddress,
124 uint16_t multicastPort);
127 * @brief Start unicast server for specified local address and port
129 * @param localAddress [IN] Local adapter address to which server to be binded.
130 * @param port [IN,OUT] Port number on which server will be running. If binding
131 * the port failed, server starts in the next available port.
132 * @param forceStart [IN] Indicate whether to start server forcesfully on specified port
134 * @param secured [IN] True if the secure server to be started, otherwise false.
136 * @return #CA_STATUS_OK or Appropriate error code
137 * @retval #CA_STATUS_OK Successful
138 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
139 * @retval #CA_SERVER_STARTED_ALREADY Unicast server is already started and running.
140 * @retval #CA_STATUS_FAILED Operation failed
142 CAResult_t CAIPStartUnicastServer(const char *localAddress, uint16_t *port, bool forceStart,
146 * @brief Stop servers that are running in particular interface address.
148 * @param interfaceAddress [IN] interface address in which servers are running.
150 * @return #CA_STATUS_OK or Appropriate error code
151 * @retval #CA_STATUS_OK Successful
152 * @retval #CA_STATUS_FAILED Operation failed
154 CAResult_t CAIPStopServer(const char *interfaceAddress);
157 * @brief Used to stop all unicast and multicast servers.
159 * @return #CA_STATUS_OK or Appropriate error code
160 * @retval #CA_STATUS_OK Successful
161 * @retval #CA_STATUS_FAILED Operation failed
163 CAResult_t CAIPStopAllServers();
166 * @brief Used to get the socket fd based on index value of server info list.
168 * @param index [IN] Index where we need socket fd value.
169 * @param isSecured [IN] For secured unicast server or normal server.
171 * @return positive value on success and -1 on error.
173 int CAGetSocketFdFromUnicastIPServerbyIndex(int16_t index, bool isSecured);
176 * @brief Used to get the number of unicast server currently running.
178 * @param isSecured [IN] To identify whether its secured unicast server or normal server.
180 * @return positive value on success and -1 on error.
182 int16_t CAGetNumberOfUnicastIPServers(bool isSecured);
185 * @brief Used to get the stored socket fd for corresponding ipAddress.
187 * @param ipAddress [IN] IpAddress of server.
188 * @param isSecured [IN] Used to check the server is secured or not.
189 * @param isMulticast [IN] To identify whether its for multicast or unicast.
191 * @return socket fd on success and -1 on error.
193 int CAGetSocketFdFromUnicastIPServer(const char *ipAddress, bool isSecured, bool isMulticast);
196 * @brief Used to get the port number to the corresponding ip for giving interface info.
198 * @param ipAddress [IN] IpAddress of server.
199 * @param isSecured [IN] Used to check the server is secured or not.
201 * @return port number on success and -1 on error.
203 uint16_t CAGetServerPortNum(const char *ipAddress, bool isSecured);
206 * @brief Used to get the port number for corresponding ipAddress.
208 * @param serverInfoList [OUT] ServerInfoList holds unicast and multicast server informations.
210 * @return #CA_STATUS_OK or Appropriate error code
211 * @retval #CA_STATUS_OK Successful
212 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
213 * @retval #CA_STATUS_FAILED Initialization failed
215 CAResult_t CAGetIPServerInfoList(u_arraylist_t **serverInfoList);
218 * @brief Set this callback for receiving data packets from peer devices.
220 * @param callback [IN] Callback to be notified on reception of unicast/multicast data packets.
224 void CAIPSetPacketReceiveCallback(CAIPPacketReceivedCallback callback);
227 * @brief Set this callback for receiving exception notifications.
229 * @param callback [IN] Callback to be notified on occurance of exception on running servers.
233 void CAIPSetExceptionCallback(CAIPExceptionCallback callback);
236 * @brief API to send unicast UDP data
238 * @param remoteAddress [IN] IP address to which data needs to be sent.
239 * @param port [IN] Port to which data needs to be send.
240 * @param data [IN] Data to be send.
241 * @param dataLength [IN] Length of data in bytes
242 * @param isMulticast [IN] Whether data needs to be sent to multicast ip
243 * @param isSecured [IN] Whether data to be sent on secured channel.
245 * @return The number of bytes sent on the network. Returns 0 on error.
246 * @remarks isSecure will be ignored when isMulticast is true.
248 uint32_t CAIPSendData(const char *remoteAddress, uint16_t port, const void *data,
249 uint32_t dataLength, bool isMulticast, bool isSecure);
252 * @brief Callback to be notified when IP adapter connection state changes.
254 * @param ipAddress [IN] IP address of remote OIC device.
255 * @param status [IN] Connection status either #CA_INTERFACE_UP or #CA_INTERFACE_DOWN.
257 * @pre Callback must be registered using CAIPSetConnectionStateChangeCallback()
259 typedef void (*CAIPConnectionStateChangeCallback)(const char *ipAddress,
260 CANetworkStatus_t status);
263 * @brief Initialize IP network monitor
265 * @param threadPool [IN] Thread pool for managing network monitor thread.
267 * @return #CA_STATUS_OK or Appropriate error code
268 * @retval #CA_STATUS_OK Successful
269 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
270 * @retval #CA_STATUS_FAILED Initialization failed
272 CAResult_t CAIPInitializeNetworkMonitor(const ca_thread_pool_t threadPool);
275 * @brief Terminate IP network monitor by removing interface list.
278 void CAIPTerminateNetworkMonitor();
281 * @brief Start network monitoring process. It will start the monitor thread.
283 * @return #CA_STATUS_OK or Appropriate error code
284 * @retval #CA_STATUS_OK Successful
285 * @retval #CA_STATUS_FAILED Operation failed
287 CAResult_t CAIPStartNetworkMonitor();
290 * @brief Stop network monitoring process. It will stop the monitor thread.
292 * @return #CA_STATUS_OK or Appropriate error code
293 * @retval #CA_STATUS_OK Successful
294 * @retval #CA_STATUS_FAILED Operation failed
296 CAResult_t CAIPStopNetworkMonitor();
299 * @brief Get local adapter network information.
301 * @param netInterfaceList [OUT] network interface information list
303 * @return #CA_STATUS_OK or Appropriate error code
304 * @retval #CA_STATUS_OK Successful
305 * @retval #CA_STATUS_INVALID_PARAM Invalid input data
306 * @retval #CA_STATUS_FAILED Operation failed
307 * @remarks interfaceName and ipAddress must be freed using free().
309 CAResult_t CAIPGetInterfaceInfo(u_arraylist_t **netInterfaceList);
312 * @brief Get local adapter network subnet mask.
314 * @param ipAddress [IN] IpAddress which is used for getting subnet mask.
315 * @param subnetMask [OUT] Local adapter interface subnet mask
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 subnetMask must be freed using free().
323 CAResult_t CAIPGetInterfaceSubnetMask(const char *ipAddress, char **subnetMask);
326 * @brief Get IP adapter connection state.
328 * @return True if IP adapter is connected, otherwise false
330 bool CAIPIsConnected();
333 * @brief Set callback for receiving local IP adapter connection status.
335 * @param callback [IN] Callback to be notified when IP adapter connection state changes.
338 void CAIPSetConnectionStateChangeCallback(CAIPConnectionStateChangeCallback callback);
344 #endif /* CA_IP_INTERFACE_H_ */