mbed Connector Endpoint interface. This interface permits a mbed endpoint to easily setup MDS resources and emit those resources to an MDS server.

Dependents:   IoT_LED_demo ServoTest uWater_Project hackathon ... more

Committer:
ansond
Date:
Sun Sep 06 03:16:02 2015 +0000
Revision:
61:143beb6d8800
Parent:
60:167e8f021e30
fixes to observation configuration/toggle switch issues.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 30:113c2a1d8db2 1 /**
ansond 30:113c2a1d8db2 2 * @file ThreadedResourceObserver.cpp
ansond 30:113c2a1d8db2 3 * @brief mbed CoAP DynamicResource Thread-based observer (implementation)
ansond 30:113c2a1d8db2 4 * @author Doug Anson/Chris Paola
ansond 30:113c2a1d8db2 5 * @version 1.0
ansond 30:113c2a1d8db2 6 * @see
ansond 30:113c2a1d8db2 7 *
ansond 30:113c2a1d8db2 8 * Copyright (c) 2014
ansond 30:113c2a1d8db2 9 *
ansond 30:113c2a1d8db2 10 * Licensed under the Apache License, Version 2.0 (the "License");
ansond 30:113c2a1d8db2 11 * you may not use this file except in compliance with the License.
ansond 30:113c2a1d8db2 12 * You may obtain a copy of the License at
ansond 30:113c2a1d8db2 13 *
ansond 30:113c2a1d8db2 14 * http://www.apache.org/licenses/LICENSE-2.0
ansond 30:113c2a1d8db2 15 *
ansond 30:113c2a1d8db2 16 * Unless required by applicable law or agreed to in writing, software
ansond 30:113c2a1d8db2 17 * distributed under the License is distributed on an "AS IS" BASIS,
ansond 30:113c2a1d8db2 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ansond 30:113c2a1d8db2 19 * See the License for the specific language governing permissions and
ansond 30:113c2a1d8db2 20 * limitations under the License.
ansond 30:113c2a1d8db2 21 */
ansond 30:113c2a1d8db2 22
ansond 30:113c2a1d8db2 23 #include "ThreadedResourceObserver.h"
ansond 30:113c2a1d8db2 24
ansond 43:769d491a48c1 25 #ifdef CONNECTOR_USING_THREADS
ansond 43:769d491a48c1 26 // DEBUG
ansond 43:769d491a48c1 27 //DigitalOut __threaded_led(LED2);
ansond 43:769d491a48c1 28 #endif
ansond 43:769d491a48c1 29
ansond 30:113c2a1d8db2 30 // constructor
ansond 31:bacc63106754 31 ThreadedResourceObserver::ThreadedResourceObserver(DynamicResource *resource,int sleep_time) :
ansond 31:bacc63106754 32 ResourceObserver(resource,sleep_time)
ansond 31:bacc63106754 33 #ifdef CONNECTOR_USING_THREADS
ansond 31:bacc63106754 34 ,m_observation_thread(&ThreadedResourceObserver::_observation_notifier,this)
ansond 31:bacc63106754 35 #endif
ansond 31:bacc63106754 36 {
ansond 43:769d491a48c1 37 this->setObserving(false);
ansond 43:769d491a48c1 38 // DEBUG
ansond 43:769d491a48c1 39 std::printf("ThreadedResourceObserver being used for %s (sleep_time=%d)\r\n",resource->getName().c_str(),sleep_time);
ansond 30:113c2a1d8db2 40 }
ansond 30:113c2a1d8db2 41
ansond 30:113c2a1d8db2 42 // destructor
ansond 30:113c2a1d8db2 43 ThreadedResourceObserver::~ThreadedResourceObserver() {
ansond 31:bacc63106754 44 this->stopObservation();
ansond 31:bacc63106754 45 #ifdef CONNECTOR_USING_THREADS
ansond 30:113c2a1d8db2 46 this->m_observation_thread.terminate();
ansond 31:bacc63106754 47 #endif
ansond 30:113c2a1d8db2 48 }
ansond 30:113c2a1d8db2 49
ansond 30:113c2a1d8db2 50 // notifier
ansond 31:bacc63106754 51 void ThreadedResourceObserver::_observation_notifier(void const *instance) {
ansond 31:bacc63106754 52 #ifdef CONNECTOR_USING_THREADS
ansond 31:bacc63106754 53 ThreadedResourceObserver *me = (ThreadedResourceObserver *)instance;
ansond 30:113c2a1d8db2 54 while(true) {
ansond 31:bacc63106754 55 Thread::wait(me->getSleepTime());
ansond 36:1c6c45584c13 56 if (me->isObserving() == true && me->getResource() != NULL && nsdl_endpoint_is_registered() == true) {
ansond 61:143beb6d8800 57 //
ansond 61:143beb6d8800 58 // implementsObservation(): This switch denotes whether this threaded resource generates its own observations or relies on the heartbeat (time-based) mechanism.
ansond 61:143beb6d8800 59 // -- if resource itself doesn't implement it, then we can call notify() with the get() on each heartbeat of the threaded resource (see note below)
ansond 61:143beb6d8800 60 // -- otherwise, we let the resource itself call observe() as part of its observation implementation
ansond 61:143beb6d8800 61 //
ansond 61:143beb6d8800 62 // note: this is independent of whether the resource, when declared, is defined as an observable resource or not. If not, then notify() wont be called
ansond 61:143beb6d8800 63 // as there will be no observations for this resource regardless of this switch value.
ansond 61:143beb6d8800 64 //
ansond 61:143beb6d8800 65 bool do_notify = !(me->getResource()->implementsObservation());
ansond 61:143beb6d8800 66 me->getResource()->observe(do_notify);
ansond 43:769d491a48c1 67 //__threaded_led = !__threaded_led;
ansond 30:113c2a1d8db2 68 }
ansond 30:113c2a1d8db2 69 }
ansond 31:bacc63106754 70 #endif
ansond 31:bacc63106754 71 }
ansond 31:bacc63106754 72
ansond 31:bacc63106754 73 // begin observing...
ansond 31:bacc63106754 74 void ThreadedResourceObserver::beginObservation() {
ansond 31:bacc63106754 75 this->setObserving(true);
ansond 30:113c2a1d8db2 76 }
ansond 30:113c2a1d8db2 77
ansond 31:bacc63106754 78 // stop observing...
ansond 31:bacc63106754 79 void ThreadedResourceObserver::stopObservation() {
ansond 31:bacc63106754 80 this->setObserving(false);
ansond 30:113c2a1d8db2 81 }