* This file contains common utility function for CA transport adaptors.
*/
-#ifndef _CA_ADAPTER_UTILS_H_
-#define _CA_ADAPTER_UTILS_H_
+#ifndef CA_ADAPTER_UTILS_H_
+#define CA_ADAPTER_UTILS_H_
+
+#include <stdbool.h>
+#ifdef __ANDROID__
+#include <jni.h>
+#endif
+
+#ifndef WITH_ARDUINO
+#include <sys/socket.h>
+#endif
#include "cacommon.h"
#include "logger.h"
#include "pdu.h"
+#include "uarraylist.h"
#ifdef __cplusplus
extern "C"
#endif
/**
- * @def VERIFY_NON_NULL
- * @brief Macro to verify the validity of input argument
+ * Macro to verify the validity of input argument.
*/
-#define VERIFY_NON_NULL(arg, log_tag, log_message) \
- if (NULL == arg ){ \
+#define VERIFY_NON_NULL_RET(arg, log_tag, log_message,ret) \
+ if (NULL == arg) { \
OIC_LOG_V(ERROR, log_tag, "Invalid input:%s", log_message); \
- return CA_STATUS_INVALID_PARAM; \
+ return ret; \
} \
/**
- * @def VERIFY_NON_NULL_RET
- * @brief Macro to verify the validity of input argument
+ * Macro to verify the validity of input argument.
*/
-#define VERIFY_NON_NULL_RET(arg, log_tag, log_message,ret) \
- if (NULL == arg ){ \
- OIC_LOG_V(ERROR, log_tag, "Invalid input:%s", log_message); \
- return ret; \
- } \
+#define VERIFY_NON_NULL(arg, log_tag, log_message) \
+ VERIFY_NON_NULL_RET((arg), (log_tag), (log_message), CA_STATUS_INVALID_PARAM)
/**
- * @def VERIFY_NON_NULL_VOID
- * @brief Macro to verify the validity of input argument
+ * Macro to verify the validity of input argument.
*/
#define VERIFY_NON_NULL_VOID(arg, log_tag, log_message) \
- if (NULL == arg ){ \
+ if (NULL == arg) { \
OIC_LOG_V(ERROR, log_tag, "Invalid input:%s", log_message); \
return; \
} \
/**
- * @def IPV4_ADDR_ONE_OCTECT_LEN
- * @brief Macro to allocate memory for ipv4 address in the form of uint8_t.
+ * Length of network interface name.
+ */
+#define CA_INTERFACE_NAME_SIZE 16
+
+/**
+ * Macro to allocate memory for ipv4 address in the form of uint8_t.
*/
#define IPV4_ADDR_ONE_OCTECT_LEN 4
+#ifdef SINGLE_THREAD
/**
- * @brief To log the PDU data
+ * Network Interface Information. Only needed for Arduino.
*/
-void CALogPDUData(coap_pdu_t *pdu);
+typedef struct
+{
+ char ipAddress[CA_IPADDR_SIZE]; /**< Address of the interface. **/
+ char subnetMask[CA_IPADDR_SIZE]; /**< Maintains interface subnetmask. **/
+ char interfaceName[CA_INTERFACE_NAME_SIZE]; /**< Interface name. **/
+} CANetInfo_t;
+#endif
/**
- * @fn CAAdapterCreateLocalEndpoint
- * @brief Create CALocalConnectivity_t instance.
+ * unicast and multicast server information.
*/
-CALocalConnectivity_t *CAAdapterCreateLocalEndpoint(CAConnectivityType_t type,
- const char *address);
+typedef struct
+{
+ int socketFd; /**< Socket descriptor. **/
+ CAEndpoint_t endpoint; /**< endpoint description. **/
+ bool isServerStarted; /**< Indicates server started. **/
+ bool isMulticastServer; /**< Indicates multicast server. **/
+ char ifAddr[CA_IPADDR_SIZE]; /**< Address of the multicast interface. **/
+ char interfaceName[CA_INTERFACE_NAME_SIZE]; /**< Interface Name. **/
+ char subNetMask[CA_IPADDR_SIZE]; /**< Subnet Mask. **/
+} CAServerInfo_t;
/**
- * @fn CAAdapterCopyLocalEndpoint
- * @brief Create CALocalConnectivity_t duplicate instance.
+ * To parse the IP address and port from "ipaddress:port".
+ * @param[in] ipAddrStr IP address to be parsed.
+ * @param[out] ipAddr Parsed IP address.
+ * @param[in] ipAddr Buffer length for parsed IP address.
+ * @param[out] port Parsed Port number.
+ * @return ::CA_STATUS_OK or Appropriate error code.
*/
-CALocalConnectivity_t *CAAdapterCopyLocalEndpoint(const CALocalConnectivity_t *connectivity);
+CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
+ size_t ipAddrLen, uint16_t *port);
/**
- * @fn CAAdapterFreeLocalEndpoint
- * @brief Deallocate CALocalConnectivity_t instance.
+ * Check if two ip address belong to same subnet.
+ * @param[in] ipAddress1 IP address to be checked.
+ * @param[in] ipAddress2 IP address to be checked.
+ * @param[in] netMask Subnet mask.
+ * @return true if same subnet and false if not same subnet.
+ */
+bool CAAdapterIsSameSubnet(const char *ipAddress1, const char *ipAddress2,
+ const char *netMask);
+/**
+ * Used to check the multicast server is running or not.
+ *
+ * @param[in] serverInfoList Server information list.
+ * @param[in] ipAddress Interface address of the server.
+ * @param[in] multicastAddress Multicast address of the server.
+ * @param[in] port Port number of the server.
+ *
+ * @return true or false.
*/
-void CAAdapterFreeLocalEndpoint(CALocalConnectivity_t *localEndPoint);
+bool CAIsMulticastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
+ const char *multicastAddress, uint16_t port);
/**
- * @fn CAAdapterCreateRemoteEndpoint
- * @brief Allocate CARemoteEndpoint_t instance.
+ * Used to check the unicast server is running or not.
+ *
+ * @param[in] serverInfoList Server information list.
+ * @param[in] ipAddress Ip address of the server.
+ * @param[in] port Port number of the server.
+ *
+ * @return true or false.
*/
-CARemoteEndpoint_t *CAAdapterCreateRemoteEndpoint(CAConnectivityType_t type,
- const char *address, const char *resourceUri);
+bool CAIsUnicastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
+ uint16_t port);
/**
- * @fn CAAdapterCopyRemoteEndpoint
- * @brief Create CARemoteEndpoint_t duplicate instance.
+ * Used to get the port number based on given information.
+ *
+ * @param[in] serverInfoList Server information list.
+ * @param[in] ipAddress Ip address of the server.
+ * @param[in] isSecured specifies whether to get secured or normal unicast server port.
+ *
+ * @return positive value on success and 0 on error.
*/
-CARemoteEndpoint_t *CAAdapterCopyRemoteEndpoint(
- const CARemoteEndpoint_t *remoteEndpoint);
+uint16_t CAGetServerPort(const u_arraylist_t *serverInfoList, const char *ipAddress,
+ bool isSecured);
/**
- * @fn CAAdapterFreeRemoteEndpoint
- * @brief Deallocate CARemoteEndpoint_t instance.
+ * Used to get the socket fd for given server information.
+ *
+ * @param[in] serverInfoList Server information list.
+ * @param[in] isMulticast To check whether it is multicast server or not.
+ * @param[in] endpoint network address
+
+ * @return positive value on success and -1 on error.
*/
-void CAAdapterFreeRemoteEndpoint(CARemoteEndpoint_t *remoteEndPoint);
+int CAGetSocketFdForUnicastServer(const u_arraylist_t *serverInfoList,
+ bool isMulticast, const CAEndpoint_t *endpoint);
/**
- * @fn CAParseIPv4AddressInternal
- * @brief To parse the IP address and port from "ipaddress:port"
- * @param ipAddrStr [IN] IP address to be parsed
- * @param ipAddr [OUT] Parsed IP address
- * @param ipAddr [IN] Buffer length for parsed IP address
- * @param port [OUT] Parsed Port number
- * @return #CA_STATUS_OK or Appropriate error code
+ * Used to add the server information into serverinfo list.
+ *
+ * @param[in/out] serverInfoList server information list.
+ * @param[in] info server informations like ip, port.
+ *
+ * @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 CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
- size_t ipAddrLen, uint16_t *port);
+CAResult_t CAAddServerInfo(u_arraylist_t *serverInfoList, CAServerInfo_t *info);
/**
- * @fn CAAdapterIsSameSubnet
- * @brief Check if two ip address belong to same subnet
+ * Used to remove the server information based on socket fd from server info list.
+ *
+ * @param[in/out] serverInfoList server information list.
+ * @param[in] sockFd Socket descriptor.
*/
-bool CAAdapterIsSameSubnet(const char *ipAddress1, const char *ipAddress2,
- const char *netMask);
+void CARemoveServerInfo(u_arraylist_t *serverInfoList, int sockFd);
+
+/**
+ * Used to clear the memory of network interface list.
+ * Memory pointed by infoList will become invalid after this function call.
+ *
+ * @param[in] infoList Network interface list.
+ */
+void CAClearNetInterfaceInfoList(u_arraylist_t *infoList);
+
+/**
+ * Used to clear the memory of server info list.
+ * Memory pointed by serverInfoList will become invalid after this function call.
+ *
+ * @param[in] infoList Server information list.
+ */
+void CAClearServerInfoList(u_arraylist_t *serverInfoList);
+
+/**
+ * Convert address from binary to string.
+ * @param[in] ipaddr IP address info.
+ * @param[out] host address string (must be CA_IPADDR_SIZE).
+ * @param[out] port host order port number.
+ */
+void CAConvertAddrToName(const struct sockaddr_storage *sockaddr, char *host, uint16_t *port);
+
+/**
+ * Convert address from string to binary.
+ * @param[in] host address string.
+ * @param[in] port host order port number.
+ * @param[out] ipaddr IP address info.
+ */
+void CAConvertNameToAddr(const char *host, uint16_t port, struct sockaddr_storage *sockaddr);
+
+#ifdef __ANDROID__
+/**
+ * To set context of JNI Application.
+ * This must be called by the Android API before CA Initialization.
+ * @param[in] env JNI interface pointer.
+ * @param[in] context context object.
+ */
+void CANativeJNISetContext(JNIEnv *env, jobject context);
+
+/**
+ * To set jvm object.
+ * This must be called by the Android API before CA Initialization.
+ * @param[in] jvm jvm object.
+ */
+void CANativeJNISetJavaVM(JavaVM *jvm);
+
+/**
+ * To get context.
+ * Called by adapters to get Application context.
+ * @return context object.
+ */
+jobject CANativeJNIGetContext();
+
+/**
+ * To get JVM object.
+ * Called from adapters to get JavaVM object.
+ * @return JVM object.
+ */
+JavaVM *CANativeJNIGetJavaVM();
+#endif
+
#ifdef __cplusplus
} /* extern "C" */
#endif
-#endif // _CA_ADAPTER_UTILS_H_
+#endif /* CA_ADAPTER_UTILS_H_ */