Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / resource / csdk / connectivity / inc / caipinterface.h
index 092a51e..a7afbd9 100644 (file)
@@ -43,26 +43,37 @@ extern "C"
  */
 typedef enum
 {
-    CA_UNICAST_SERVER = 0,    /**< Unicast Server */
-    CA_MULTICAST_SERVER,      /**< Multicast Server */
-    CA_SECURED_UNICAST_SERVER /**< Secured Unicast Server */
+    CA_UNICAST_SERVER = 0,      /**< Unicast Server */
+    CA_MULTICAST_SERVER,        /**< Multicast Server */
+    CA_SECURED_UNICAST_SERVER   /**< Secured Unicast Server */
 } CAAdapterServerType_t;
 
 /**
  * @brief Callback to be notified on reception of any data from remote OIC devices.
  *
- * @param  ipAddress    [IN] IP address of remote OIC device.
- * @param  port         [IN] Port number on which data is received.
+ * @param  endpoint     [IN] network endpoint description
  * @param  data         [IN] Data received from remote OIC device.
  * @param  dataLength   [IN] Length of data in bytes.
- * @param  isSecured    [IN] Indicates the data is secure or not.
  *
  * @return NONE
  * @pre  Callback must be registered using CAIPSetPacketReceiveCallback()
  */
-typedef void (*CAIPPacketReceivedCallback)(const char *ipAddress, uint16_t port,
-                                           const void *data, uint32_t dataLength,
-                                           bool isSecured);
+typedef void (*CAIPPacketReceivedCallback)(const CAEndpoint_t *endpoint,
+                                           const void *data,
+                                           uint32_t dataLength);
+
+/**
+  * @brief Callback to notify error in the IP adapter
+  *
+  * @param  endpoint     [IN] [IN] network endpoint description
+  * @param  data         [IN] Data sent/received
+  * @param  dataLength   [IN] Length of data in bytes.
+  * @param  result       [IN] result of request from R.I
+  * @return NONE
+  * @pre  Callback must be registered using CAIPSetPacketReceiveCallback()
+ */
+typedef void (*CAIPErrorHandleCallback)(const CAEndpoint_t *endpoint, const void *data,
+                                        uint32_t dataLength, CAResult_t result);
 
 /**
  * @brief  Callback to be notified when exception occures on multicast/unicast server.
@@ -73,7 +84,7 @@ typedef void (*CAIPPacketReceivedCallback)(const char *ipAddress, uint16_t port,
 typedef void (*CAIPExceptionCallback)(CAAdapterServerType_t type);
 
 /**
- * @brief  Initialize IP server
+ * @brief  Start IP server
  *
  * @param   threadPool  [IN] Thread pool for managing Unicast/Multicast server threads.
  *
@@ -82,121 +93,17 @@ typedef void (*CAIPExceptionCallback)(CAAdapterServerType_t type);
  * @retval  #CA_STATUS_INVALID_PARAM Invalid input data
  * @retval  #CA_STATUS_FAILED Initialization failed
  */
-CAResult_t CAIPInitializeServer(const ca_thread_pool_t threadPool);
+#ifdef SINGLE_THREAD
+CAResult_t CAIPStartServer();
+#else
+CAResult_t CAIPStartServer(const ca_thread_pool_t threadPool);
+#endif
 
 /**
- * @brief  Terminate IP server
+ * @brief  Stop IP server
  * @return NONE
  */
-void CAIPTerminateServer();
-
-/**
- * @brief  Start multicast server for specified multicast address and port
- *
- * @param   localAddress        [IN]      Local adapter address to which server to be binded.
- * @param   multicastAddress    [IN]      Multicast group address.
- * @param   multicastPort       [IN,OUT]  Port number on which server will be running. If binding
- *                                        the port failed, server starts in the next available port.
- *
- * @return  #CA_STATUS_OK or Appropriate error code
- * @retval  #CA_STATUS_OK  Successful
- * @retval  #CA_STATUS_INVALID_PARAM Invalid input data
- * @retval  #CA_SERVER_STARTED_ALREADY Multicast server is already started and running.
- * @retval  #CA_STATUS_FAILED Operation failed
- */
-CAResult_t CAIPStartMulticastServer(const char *localAddress, const char *multicastAddress,
-                                    uint16_t multicastPort);
-
-/**
- * @brief  Start unicast server for specified local address and port
- *
- * @param  localAddress [IN]      Local adapter address to which server to be binded.
- * @param  port         [IN,OUT]  Port number on which server will be running. If binding
- *                                the port failed, server starts in the next available port.
- * @param  forceStart   [IN]      Indicate whether to start server forcesfully on specified port
- *                                or not.
- * @param  secured      [IN]      True if the secure server to be started, otherwise false.
- *
- * @return  #CA_STATUS_OK or Appropriate error code
- * @retval  #CA_STATUS_OK  Successful
- * @retval  #CA_STATUS_INVALID_PARAM Invalid input data
- * @retval  #CA_SERVER_STARTED_ALREADY Unicast server is already started and running.
- * @retval  #CA_STATUS_FAILED Operation failed
- */
-CAResult_t CAIPStartUnicastServer(const char *localAddress, uint16_t *port, bool forceStart,
-                                  bool secured);
-
-/**
- * @brief  Stop servers that are running in particular interface address.
- *
- * @param   interfaceAddress  [IN] interface address in which servers are running.
- *
- * @return  #CA_STATUS_OK or Appropriate error code
- * @retval  #CA_STATUS_OK  Successful
- * @retval  #CA_STATUS_FAILED Operation failed
- */
-CAResult_t CAIPStopServer(const char *interfaceAddress);
-
-/**
- * @brief  Used to stop all unicast and multicast servers.
- *
- * @return  #CA_STATUS_OK or Appropriate error code
- * @retval  #CA_STATUS_OK  Successful
- * @retval  #CA_STATUS_FAILED Operation failed
- */
-CAResult_t CAIPStopAllServers();
-
-/**
- * @brief  Used to get the socket fd based on index value of server info list.
- *
- * @param   index      [IN] Index where we need socket fd value.
- * @param   isSecured  [IN] For secured unicast server or normal server.
- *
- * @return  positive value on success and -1 on error.
- */
-int CAGetSocketFdFromUnicastIPServerbyIndex(int16_t index, bool isSecured);
-
-/**
- * @brief  Used to get the number of unicast server currently running.
- *
- * @param   isSecured  [IN] To identify whether its secured unicast server or normal server.
- *
- * @return  positive value on success and -1 on error.
- */
-int16_t CAGetNumberOfUnicastIPServers(bool isSecured);
-
-/**
- * @brief  Used to get the stored socket fd for corresponding ipAddress.
- *
- * @param   ipAddress    [IN] IpAddress of server.
- * @param   isSecured    [IN] Used to check the server is secured or not.
- * @param   isMulticast  [IN] To identify whether its for multicast or unicast.
- *
- * @return  socket fd on success and -1 on error.
- */
-int CAGetSocketFdFromUnicastIPServer(const char *ipAddress, bool isSecured, bool isMulticast);
-
-/**
- * @brief  Used to get the port number to the corresponding ip for giving interface info.
- *
- * @param   ipAddress  [IN] IpAddress of server.
- * @param   isSecured  [IN] Used to check the server is secured or not.
- *
- * @return  port number on success and -1 on error.
- */
-uint16_t CAGetServerPortNum(const char *ipAddress, bool isSecured);
-
-/**
- * @brief  Used to get the port number for corresponding ipAddress.
- *
- * @param   serverInfoList  [OUT] ServerInfoList holds unicast and multicast server informations.
- *
- * @return  #CA_STATUS_OK or Appropriate error code
- * @retval  #CA_STATUS_OK  Successful
- * @retval  #CA_STATUS_INVALID_PARAM Invalid input data
- * @retval  #CA_STATUS_FAILED Initialization failed
- */
-CAResult_t CAGetIPServerInfoList(u_arraylist_t **serverInfoList);
+void CAIPStopServer();
 
 /**
  * @brief  Set this callback for receiving data packets from peer devices.
@@ -217,109 +124,74 @@ void CAIPSetPacketReceiveCallback(CAIPPacketReceivedCallback callback);
 void CAIPSetExceptionCallback(CAIPExceptionCallback callback);
 
 /**
- * @brief  API to send unicast UDP data
+ * @brief  Set socket description for sending unicast UDP data. Once the Unicast server is started,
+ *         the same socket descriptor is used for sending the Unicast UDP data.
  *
- * @param  remoteAddress    [IN] IP address to which data needs to be sent.
- * @param  port             [IN] Port to which data needs to be send.
- * @param  data             [IN] Data to be send.
- * @param  dataLength       [IN] Length of data in bytes
- * @param  isMulticast      [IN] Whether data needs to be sent to multicast ip
- * @param  isSecured        [IN] Whether data to be sent on secured channel.
- *
- * @return  The number of bytes sent on the network. Returns 0 on error.
- * @remarks isSecure will be ignored when isMulticast is true.
- */
-uint32_t CAIPSendData(const char *remoteAddress, uint16_t port, const void *data,
-                      uint32_t dataLength, bool isMulticast, bool isSecure);
-
-/**
- * @brief  Callback to be notified when IP adapter connection state changes.
- *
- * @param  ipAddress    [IN] IP address of remote OIC device.
- * @param  status       [IN] Connection status either #CA_INTERFACE_UP or #CA_INTERFACE_DOWN.
+ * @param  socketFD [IN]  Socket descriptor used for sending UDP data.
  * @return  NONE
- * @pre  Callback must be registered using CAIPSetConnectionStateChangeCallback()
  */
-typedef void (*CAIPConnectionStateChangeCallback)(const char *ipAddress,
-                                                  CANetworkStatus_t status);
+void CAIPSetUnicastSocket(int socketFD);
 
 /**
- * @brief Initialize IP network monitor
- *
- * @param  threadPool [IN] Thread pool for managing network monitor thread.
- *
- * @return  #CA_STATUS_OK or Appropriate error code
- * @retval  #CA_STATUS_OK  Successful
- * @retval  #CA_STATUS_INVALID_PARAM Invalid input data
- * @retval  #CA_STATUS_FAILED Initialization failed
- */
-CAResult_t CAIPInitializeNetworkMonitor(const ca_thread_pool_t threadPool);
-
-/**
- * @brief Terminate IP network monitor by removing interface list.
- * @return  NONE
+ * @brief  Set the port number for sending unicast UDP data
+ * @param  port [IN] Port number used for sending UDP data.
+ * @return NONE
  */
-void CAIPTerminateNetworkMonitor();
+void CAIPSetUnicastPort(uint16_t port);
 
 /**
- * @brief  Start network monitoring process. It will start the monitor thread.
+ * @brief  API to send unicast UDP data
  *
- * @return  #CA_STATUS_OK or Appropriate error code
- * @retval  #CA_STATUS_OK  Successful
- * @retval  #CA_STATUS_FAILED Operation failed
+ * @param  endpoint         [IN] complete network address to send to
+ * @param  data             [IN] Data to be send.
+ * @param  dataLength       [IN] Length of data in bytes
+ * @param  isMulticast      [IN] Whether data needs to be sent to multicast ip
  */
-CAResult_t CAIPStartNetworkMonitor();
+void CAIPSendData(CAEndpoint_t *endpoint,
+                  const void *data,
+                  uint32_t dataLength,
+                  bool isMulticast);
 
 /**
- * @brief  Stop network monitoring process. It will stop the monitor thread.
+ * @brief  Get IP adapter connection state.
  *
- * @return  #CA_STATUS_OK or Appropriate error code
- * @retval  #CA_STATUS_OK  Successful
- * @retval  #CA_STATUS_FAILED Operation failed
+ * @return  True if IP adapter is connected, otherwise false
  */
-CAResult_t CAIPStopNetworkMonitor();
+bool CAIPIsConnected();
 
 /**
- * @brief  Get local adapter network information.
- *
- * @param  netInterfaceList [OUT] network interface information list
- *
- * @return  #CA_STATUS_OK or Appropriate error code
- * @retval  #CA_STATUS_OK  Successful
- * @retval  #CA_STATUS_INVALID_PARAM Invalid input data
- * @retval  #CA_STATUS_FAILED Operation failed
- * @remarks  interfaceName and ipAddress must be freed using free().
+ * @brief  Pull the Received Data
+ * @return NONE
  */
-CAResult_t CAIPGetInterfaceInfo(u_arraylist_t **netInterfaceList);
+void CAIPPullData();
 
-/**
- * @brief  Get local adapter network subnet mask.
- *
- * @param  ipAddress  [IN]  IpAddress which is used for getting subnet mask.
- * @param  subnetMask [OUT] Local adapter interface subnet mask
- *
- * @return  #CA_STATUS_OK or Appropriate error code
- * @retval  #CA_STATUS_OK  Successful
- * @retval  #CA_STATUS_INVALID_PARAM Invalid input data
- * @retval  #CA_STATUS_FAILED Operation failed
- * @remarks subnetMask must be freed using free().
- */
-CAResult_t CAIPGetInterfaceSubnetMask(const char *ipAddress, char **subnetMask);
+#define CA_COAP        5683
+#define CA_SECURE_COAP 5684
+#define INTERFACE_NAME_MAX 16
+
+typedef struct
+{
+    char name[INTERFACE_NAME_MAX];
+    uint32_t index;
+    uint32_t flags;
+    uint16_t family;
+    uint32_t ipv4addr;        // used for IPv4 only
+} CAInterface_t;
 
 /**
- * @brief  Get IP adapter connection state.
+ * @brief  Get a list of CAInterface_t items
  *
- * @return  True if IP adapter is connected, otherwise false
+ * @return  List of CAInterface_t items
  */
- bool CAIPIsConnected();
+u_arraylist_t *CAIPGetInterfaceInformation(int desiredIndex);
 
 /**
- * @brief  Set callback for receiving local IP adapter connection status.
+ * @brief  Set callback for error handling
  *
- * @param  callback [IN] Callback to be notified when IP adapter connection state changes.
+ * @param  ipErrorCallback [IN] callback to notify error to the ipadapter
  * @return NONE
  */
-void CAIPSetConnectionStateChangeCallback(CAIPConnectionStateChangeCallback callback);
+void CAIPSetErrorHandleCallback(CAIPErrorHandleCallback ipErrorCallback);
 
 #ifdef __cplusplus
 }