Doug Anson / mbedConnectorInterface

Dependents:   IoT_LED_demo ServoTest uWater_Project hackathon ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TickerResourceObserver.cpp Source File

TickerResourceObserver.cpp

Go to the documentation of this file.
00001 /**
00002  * @file    TickerResourceObserver.cpp
00003  * @brief   mbed CoAP DynamicResource Ticker-based observer (implementation)
00004  * @author  Doug Anson/Chris Paola
00005  * @version 1.0
00006  * @see
00007  *
00008  * Copyright (c) 2014
00009  *
00010  * Licensed under the Apache License, Version 2.0 (the "License");
00011  * you may not use this file except in compliance with the License.
00012  * You may obtain a copy of the License at
00013  *
00014  *     http://www.apache.org/licenses/LICENSE-2.0
00015  *
00016  * Unless required by applicable law or agreed to in writing, software
00017  * distributed under the License is distributed on an "AS IS" BASIS,
00018  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00019  * See the License for the specific language governing permissions and
00020  * limitations under the License.
00021  */
00022  
00023  #include "TickerResourceObserver.h"
00024  
00025  // DEBUG
00026  //DigitalOut __ticker_led(LED2);
00027  
00028  // determines whether the mDS endpoint has been registered or not...
00029  extern "C" bool nsdl_endpoint_is_registered(void);
00030  
00031  // constructor
00032  TickerResourceObserver::TickerResourceObserver(DynamicResource *resource,int sleep_time) : 
00033                                     ResourceObserver(resource,(int)(sleep_time/1000)) {
00034      this->setObserving(false);
00035      // DEBUG
00036      std::printf("TickerResourceObserver being used for %s (sleep_time=%d)\r\n",resource->getName().c_str(),sleep_time);
00037  }
00038   
00039  // destructor
00040  TickerResourceObserver::~TickerResourceObserver() {
00041      this->stopObservation();
00042  }
00043 
00044  // notifier
00045  void TickerResourceObserver::observationNotifier() {
00046      if (this->isObserving() == true && this->getResource() != NULL && nsdl_endpoint_is_registered() == true) {
00047          //
00048          // implementsObservation(): This switch denotes whether this ticker resource generates its own observations or relies on the ticker (time-based) mechanism.
00049          //   -- 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)
00050          //   -- otherwise, we let the resource itself call observe() as part of its observation implementation
00051          //
00052          // 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 
00053          //       as there will be no observations for this resource regardless of this switch value. 
00054          //
00055          bool do_notify = !(this->getResource()->implementsObservation());
00056          this->getResource()->observe(do_notify);
00057          //__ticker_led = !__ticker_led;
00058      }
00059  }
00060  
00061  // begin observing...
00062  void TickerResourceObserver::beginObservation() {
00063      if (this->isObserving() == false) {
00064         this->m_ticker.attach(this,&TickerResourceObserver::observationNotifier,(float)this->getSleepTime());
00065         this->setObserving(true);
00066      }
00067  }
00068  
00069  // begin observing...
00070  void TickerResourceObserver::stopObservation() {
00071      this->setObserving(false);
00072  }