From daf0c65cfe95295ea61fc845f4d1a26b1b71c0ed Mon Sep 17 00:00:00 2001 From: Sashi Penta Date: Thu, 4 Sep 2014 21:36:27 -0700 Subject: [PATCH] OCStartPresence now can be called when the server comes up for the first time, when it comes up from offline, or when it reenters network. Similarly, OCStopPresence can be called when the server is terminating, going offline or when it leaves network. Change-Id: I610979c26c26c1a7c0fcd4fb95294c5dedaeb4e4 --- csdk/stack/src/ocstack.c | 43 +++++++++++++++++++++++++++---------------- examples/presenceclient.cpp | 2 +- examples/presenceserver.cpp | 45 +++++++++++++++++++++++++++++++++++++++++---- include/OCPlatform.h | 24 +++++++++++++++++++----- 4 files changed, 88 insertions(+), 26 deletions(-) diff --git a/csdk/stack/src/ocstack.c b/csdk/stack/src/ocstack.c index fdedd1e..6537ed0 100644 --- a/csdk/stack/src/ocstack.c +++ b/csdk/stack/src/ocstack.c @@ -38,12 +38,20 @@ typedef enum { OC_STACK_UNINITIALIZED = 0, OC_STACK_INITIALIZED } OCStackState; +#ifdef WITH_PRESENCE +typedef enum { + OC_PRESENCE_UNINITIALIZED = 0, OC_PRESENCE_INITIALIZED +} OCPresenceState; +#endif + //----------------------------------------------------------------------------- // Private variables //----------------------------------------------------------------------------- static OCStackState stackState = OC_STACK_UNINITIALIZED; + OCResource *headResource = NULL; #ifdef WITH_PRESENCE +static OCPresenceState presenceState = OC_PRESENCE_UNINITIALIZED; static PresenceResource presenceResource; uint8_t PresenceTimeOutSize = 4; uint32_t PresenceTimeOut[] = {50, 75, 85, 95, 100}; @@ -95,14 +103,8 @@ void HandleStackResponses(OCResponse * response) OC_LOG(INFO, TAG, PCF("Calling into application address space")); result = response->cbNode->callBack(response->cbNode->context, response->cbNode->handle, response->clientResponse); - #ifdef WITH_PRESENCE - if (result == OC_STACK_DELETE_TRANSACTION || - response->clientResponse->result == OC_STACK_COMM_ERROR || - response->clientResponse->result == OC_STACK_PRESENCE_STOPPED) - #else if (result == OC_STACK_DELETE_TRANSACTION || response->clientResponse->result == OC_STACK_COMM_ERROR) - #endif { FindAndDeleteClientCB(response->cbNode); } @@ -551,7 +553,10 @@ OCStackResult OCProcess() { * presence notifications to clients via multicast. Once this API has been called with a success, * clients may query for this server's presence and this server's stack will respond via multicast. * - * @param ttl (Time To Live in seconds) - Used to set the time a server + * Server can call this function when it comes online for the first time, or when it comes back + * online from offline mode, or when it re enters network. + * + * @param ttl - Time To Live in seconds * Note: If ttl is '0', then the default stack value will be used (60 Seconds). * * @return @@ -560,23 +565,26 @@ OCStackResult OCProcess() { */ OCStackResult OCStartPresence(const uint32_t ttl) { - if(((OCResource *)presenceResource.handle)->resourceProperties & OC_ACTIVE) - { - return OC_STACK_ERROR; - } OCChangeResourceProperty( &(((OCResource *)presenceResource.handle)->resourceProperties), OC_ACTIVE, 1); + if(ttl > 0) { presenceResource.presenceTTL = ttl; } - OCDevAddr multiCastAddr = {0}; - OCCoAPToken * token = OCGenerateCoAPToken(); - OCBuildIPv4Address(224, 0, 1, 187, 5683, &multiCastAddr); - //add the presence observer - AddObserver(OC_PRESENCE_URI, NULL, token, &multiCastAddr, + + if(OC_PRESENCE_UNINITIALIZED == presenceState) + { + presenceState = OC_PRESENCE_INITIALIZED; + + OCDevAddr multiCastAddr = {0}; + OCCoAPToken * token = OCGenerateCoAPToken(); + OCBuildIPv4Address(224, 0, 1, 187, 5683, &multiCastAddr); + //add the presence observer + AddObserver(OC_PRESENCE_URI, NULL, token, &multiCastAddr, (OCResource *)presenceResource.handle, OC_NON_CONFIRMABLE); + } return OCNotifyObservers(presenceResource.handle); } @@ -586,6 +594,9 @@ OCStackResult OCStartPresence(const uint32_t ttl) * presence notifications to clients via multicast. Once this API has been called with a success, * this server's stack will not respond to clients querying for this server's presence. * + * Server can call this function when it is terminating, going offline, or when going + * away from network. + * * @return * OC_STACK_OK - No errors; Success * OC_STACK_ERROR - @ref OCStartPresence has never been called or @ref OCStopPresence has diff --git a/examples/presenceclient.cpp b/examples/presenceclient.cpp index 37bff7d..d9a6a04 100644 --- a/examples/presenceclient.cpp +++ b/examples/presenceclient.cpp @@ -43,7 +43,7 @@ void presenceHandler(OCStackResult result, const int& nonce) std::cout << "Nonce# " << nonce << std::endl; break; case OC_STACK_PRESENCE_NO_UPDATE: - std::cout << "No presence updated\n"; + std::cout << "No presence update\n"; break; case OC_STACK_PRESENCE_STOPPED: std::cout << "Presence Stopped\n"; diff --git a/examples/presenceserver.cpp b/examples/presenceserver.cpp index 306b960..1f2bb67 100644 --- a/examples/presenceserver.cpp +++ b/examples/presenceserver.cpp @@ -47,12 +47,15 @@ public: int m_power; std::string m_lightUri; std::string m_lightUri2; + std::string m_lightUri3; OCResourceHandle m_resourceHandle; OCResourceHandle m_resourceHandle2; + OCResourceHandle m_resourceHandle3; public: /// Constructor - LightResource(): m_state(false), m_power(0), m_lightUri("/a/light"), m_lightUri2("/a/light2") {} + LightResource(): m_state(false), m_power(0), m_lightUri("/a/light"), + m_lightUri2("/a/light2"),m_lightUri3("/a/light3") {} /* Note that this does not need to be a member function: for classes you do not have access to, you can accomplish this with a free function: */ @@ -99,6 +102,26 @@ public: } } + void createResource3(OC::OCPlatform& platform) + { + std::string resourceURI = m_lightUri3; // URI of the resource + std::string resourceTypeName = "core.light"; + std::string resourceInterface = DEFAULT_INTERFACE; // resource interface. + + // OCResourceProperty is defined ocstack.h + uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE; + + // This will internally create and register the resource. + OCStackResult result = platform.registerResource( + m_resourceHandle3, resourceURI, resourceTypeName, + resourceInterface, &entityHandler, resourceProperty); + + if (OC_STACK_OK != result) + { + cout << "Resource creation was unsuccessful\n"; + } + } + OCResourceHandle getHandle() { return m_resourceHandle; @@ -315,17 +338,31 @@ int main() { OCPlatform platform(cfg); - // Time to Live is 100 - platform.startPresence(100); + // Time to Live is 30 seconds + platform.startPresence(30); // Invoke createResource function of class light. myLightResource.createResource(platform); - printf("Enter a key to create the second resource\n"); + printf("\nEnter a key to create the second resource\n"); getchar(); myLightResource.createResource2(platform); + printf("\nEnter a key to stop the presence\n"); + getchar(); + platform.stopPresence(); + + printf("\nEnter a key to restart the presence\n"); + getchar(); + + platform.startPresence(30); + + printf("\nEnter a key to create the third resource\n"); + getchar(); + + myLightResource.createResource3(platform); + // Perform app tasks while(true) { diff --git a/include/OCPlatform.h b/include/OCPlatform.h index e39fae0..d227136 100644 --- a/include/OCPlatform.h +++ b/include/OCPlatform.h @@ -232,14 +232,28 @@ namespace OC public: /** - * Start or stop Presence announcements. - * - * @param announceDuration - Duration to keep presence duration active. + * Start Presence announcements. + * + * @param ttl - time to live * @return OCStackResult - Returns OCSTACK_OK if success
* - * These apply only if a server instance is active. + * Server can call this function when it comes online for the + * first time, or when it comes back online from offline mode, + * or when it re enters network. + * + */ + + OCStackResult startPresence(const unsigned int ttl); + + /** + * Stop Presence announcements. + * + * @return OCStackResult - Returns OCSTACK_OK if success
+ * + * Server can call this function when it is terminating, + * going offline, or when going away from network. + * */ - OCStackResult startPresence(const unsigned int announceDurationSeconds); OCStackResult stopPresence(); -- 2.7.4