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:
44:dac601597b0e
fixes to observation configuration/toggle switch issues.

Who changed what in which revision?

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