From: hyuna0213.jo Date: Thu, 15 Dec 2016 07:34:53 +0000 (+0900) Subject: Fixed keepalive to send ping request only when host mode is client X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=092aa11fa418d577bac3509e679f236b2d62aea9;p=contrib%2Fiotivity.git Fixed keepalive to send ping request only when host mode is client ping message should be sent when host mode is client Change-Id: I2ac94e1340c1607969391b86577b9101ddc013b5 Signed-off-by: hyuna0213.jo Reviewed-on: https://gerrit.iotivity.org/gerrit/15675 Tested-by: jenkins-iotivity Reviewed-by: Dan Mihai Reviewed-by: Jaehong Jo Reviewed-by: Ashok Babu Channa --- diff --git a/resource/csdk/connectivity/api/cainterface.h b/resource/csdk/connectivity/api/cainterface.h index 70c15a9..b5ad39c 100644 --- a/resource/csdk/connectivity/api/cainterface.h +++ b/resource/csdk/connectivity/api/cainterface.h @@ -68,8 +68,11 @@ typedef struct /** * Callback function to pass the connection information from CA to RI. * @param[out] object remote device information. + * @param[out] isConnected Whether keepalive message needs to be sent. + * @param[out] isClient Host Mode of Operation. */ -typedef void (*CAKeepAliveConnectionCallback)(const CAEndpoint_t *object, bool isConnected); +typedef void (*CAKeepAliveConnectionCallback)(const CAEndpoint_t *object, bool isConnected, + bool isClient); /** * Register connection status changes callback to process KeepAlive. diff --git a/resource/csdk/connectivity/inc/catcpadapter.h b/resource/csdk/connectivity/inc/catcpadapter.h index cb2a740..4801a03 100644 --- a/resource/csdk/connectivity/inc/catcpadapter.h +++ b/resource/csdk/connectivity/inc/catcpadapter.h @@ -64,10 +64,11 @@ typedef struct unsigned char* data; /**< received data from remote device */ size_t len; /**< received data length */ size_t totalLen; /**< total coap data length required to receive */ - unsigned char tlsdata[18437]; /**< tls data(rfc5246: TLSCiphertext max (2^14+2048+5)) */ - size_t tlsLen; /**< received tls data length */ + unsigned char tlsdata[18437]; /**< tls data(rfc5246: TLSCiphertext max (2^14+2048+5)) */ + size_t tlsLen; /**< received tls data length */ CAProtocol_t protocol; /**< application-level protocol */ CATCPConnectionState_t state; /**< current tcp session state */ + bool isClient; /**< Host Mode of Operation. */ } CATCPSessionInfo_t; /** diff --git a/resource/csdk/connectivity/inc/catcpinterface.h b/resource/csdk/connectivity/inc/catcpinterface.h index dc86e0f..54c8d60 100644 --- a/resource/csdk/connectivity/inc/catcpinterface.h +++ b/resource/csdk/connectivity/inc/catcpinterface.h @@ -67,9 +67,11 @@ typedef void (*CATCPErrorHandleCallback)(const CAEndpoint_t *endpoint, const voi * * @param[in] endpoint network endpoint description. * @param[in] isConnected Whether keepalive message needs to be sent. + * @param[in] isClient Host Mode of Operation. * @see Callback must be registered using CATCPSetKeepAliveCallback(). */ -typedef void (*CATCPConnectionHandleCallback)(const CAEndpoint_t *endpoint, bool isConnected); +typedef void (*CATCPConnectionHandleCallback)(const CAEndpoint_t *endpoint, bool isConnected, + bool isClient); /** * set error callback to notify error in TCP adapter. diff --git a/resource/csdk/connectivity/src/tcp_adapter/catcpadapter.c b/resource/csdk/connectivity/src/tcp_adapter/catcpadapter.c index fda6b66..129b253 100755 --- a/resource/csdk/connectivity/src/tcp_adapter/catcpadapter.c +++ b/resource/csdk/connectivity/src/tcp_adapter/catcpadapter.c @@ -88,17 +88,17 @@ static CAConnectionChangeCallback g_connectionChangeCallback = NULL; */ static CAErrorHandleCallback g_errorCallback = NULL; +/** + * KeepAlive Connected or Disconnected Callback to CA adapter. + */ +static CAKeepAliveConnectionCallback g_connKeepAliveCallback = NULL; + static void CATCPPacketReceivedCB(const CASecureEndpoint_t *sep, const void *data, uint32_t dataLength); static void CATCPErrorHandler(const CAEndpoint_t *endpoint, const void *data, size_t dataLength, CAResult_t result); -/** - * KeepAlive Connected or Disconnected Callback to CA adapter. - */ -static CAKeepAliveConnectionCallback g_connKeepAliveCallback = NULL; - static CAResult_t CATCPInitializeQueueHandles(); static void CATCPDeinitializeQueueHandles(); @@ -242,12 +242,12 @@ static void CATCPErrorHandler(const CAEndpoint_t *endpoint, const void *data, } } -static void CATCPConnectionHandler(const CAEndpoint_t *endpoint, bool isConnected) +static void CATCPConnectionHandler(const CAEndpoint_t *endpoint, bool isConnected, bool isClient) { // Pass the changed connection status to RI Layer for keepalive. if (g_connKeepAliveCallback) { - g_connKeepAliveCallback(endpoint, isConnected); + g_connKeepAliveCallback(endpoint, isConnected, isClient); } // Pass the changed connection status to CAUtil. diff --git a/resource/csdk/connectivity/src/tcp_adapter/catcpserver.c b/resource/csdk/connectivity/src/tcp_adapter/catcpserver.c index 8f31775..c1f330b 100644 --- a/resource/csdk/connectivity/src/tcp_adapter/catcpserver.c +++ b/resource/csdk/connectivity/src/tcp_adapter/catcpserver.c @@ -325,6 +325,7 @@ static void CAAcceptConnection(CATransportFlags_t flag, CASocket_t *sock) svritem->sep.endpoint.flags = flag; svritem->sep.endpoint.adapter = CA_ADAPTER_TCP; svritem->state = CONNECTED; + svritem->isClient = false; CAConvertAddrToName((struct sockaddr_storage *)&clientaddr, clientlen, svritem->sep.endpoint.addr, &svritem->sep.endpoint.port); @@ -345,7 +346,7 @@ static void CAAcceptConnection(CATransportFlags_t flag, CASocket_t *sock) // pass the connection information to CA Common Layer. if (g_connectionCallback) { - g_connectionCallback(&(svritem->sep.endpoint), true); + g_connectionCallback(&(svritem->sep.endpoint), true, svritem->isClient); } } } @@ -1169,6 +1170,7 @@ CASocketFd_t CAConnectTCPSession(const CAEndpoint_t *endpoint) svritem->sep.endpoint.flags = endpoint->flags; svritem->sep.endpoint.ifindex = endpoint->ifindex; svritem->state = CONNECTING; + svritem->isClient = true; // #2. add TCP connection info to list oc_mutex_lock(g_mutexObjectList); @@ -1196,7 +1198,7 @@ CASocketFd_t CAConnectTCPSession(const CAEndpoint_t *endpoint) // #4. pass the connection information to CA Common Layer. if (g_connectionCallback) { - g_connectionCallback(&(svritem->sep.endpoint), true); + g_connectionCallback(&(svritem->sep.endpoint), true, svritem->isClient); } return svritem->fd; @@ -1226,7 +1228,7 @@ CAResult_t CADisconnectTCPSession(size_t index) // pass the connection information to CA Common Layer. if (g_connectionCallback && DISCONNECTED == removedData->state) { - g_connectionCallback(&(removedData->sep.endpoint), false); + g_connectionCallback(&(removedData->sep.endpoint), false, removedData->isClient); } } OICFree(removedData->data); diff --git a/resource/csdk/stack/include/internal/oickeepalive.h b/resource/csdk/stack/include/internal/oickeepalive.h index 38203c6..05556e6 100644 --- a/resource/csdk/stack/include/internal/oickeepalive.h +++ b/resource/csdk/stack/include/internal/oickeepalive.h @@ -83,9 +83,11 @@ OCStackResult HandleKeepAliveRequest(OCServerRequest *request, /** * API to handle the connected device for KeepAlive. - * @return Current Time. + * @param[in] endpoint Remote endpoint information. + * @param[in] isConnected Whether keepalive message needs to be sent. + * @param[in] isClient Host Mode of Operation. */ -void HandleKeepAliveConnCB(const CAEndpoint_t *endpoint, bool isConnected); +void HandleKeepAliveConnCB(const CAEndpoint_t *endpoint, bool isConnected, bool isClient); #ifdef __cplusplus } // extern "C" diff --git a/resource/csdk/stack/src/oickeepalive.c b/resource/csdk/stack/src/oickeepalive.c index caa1125..a179068 100644 --- a/resource/csdk/stack/src/oickeepalive.c +++ b/resource/csdk/stack/src/oickeepalive.c @@ -814,7 +814,7 @@ OCStackResult RemoveKeepAliveEntry(const CAEndpoint_t *endpoint) return OC_STACK_OK; } -void HandleKeepAliveConnCB(const CAEndpoint_t *endpoint, bool isConnected) +void HandleKeepAliveConnCB(const CAEndpoint_t *endpoint, bool isConnected, bool isClient) { VERIFY_NON_NULL_NR(endpoint, FATAL); @@ -822,18 +822,21 @@ void HandleKeepAliveConnCB(const CAEndpoint_t *endpoint, bool isConnected) { OIC_LOG(DEBUG, TAG, "Received the connected device information from CA"); - // Send discover message to find ping resource - OCCallbackData pingData = {.context = NULL, .cb = PingRequestCallback }; - OCDevAddr devAddr = { .adapter = OC_ADAPTER_TCP }; - CopyEndpointToDevAddr(endpoint, &devAddr); - - OCStackResult result = OCDoResource(NULL, OC_REST_DISCOVER, KEEPALIVE_RESOURCE_URI, - &devAddr, NULL, CT_ADAPTER_TCP, OC_HIGH_QOS, - &pingData, NULL, 0); - if (OC_STACK_OK != result) + if (isClient) { - OIC_LOG(ERROR, TAG, "OCDoResource has failed"); - return; + // Send discover message to find ping resource + OCCallbackData pingData = {.context = NULL, .cb = PingRequestCallback }; + OCDevAddr devAddr = { .adapter = OC_ADAPTER_TCP }; + CopyEndpointToDevAddr(endpoint, &devAddr); + + OCStackResult result = OCDoResource(NULL, OC_REST_DISCOVER, KEEPALIVE_RESOURCE_URI, + &devAddr, NULL, CT_ADAPTER_TCP, OC_HIGH_QOS, + &pingData, NULL, 0); + if (OC_STACK_OK != result) + { + OIC_LOG(ERROR, TAG, "OCDoResource has failed"); + return; + } } } else