Doug Anson / mbedConnectorInterface

Dependents:   IoT_LED_demo ServoTest uWater_Project hackathon ... more

api/TaskletResourceObserver.cpp

Committer:
ansond
Date:
2015-04-08
Revision:
31:bacc63106754
Parent:
30:113c2a1d8db2
Child:
32:bc40b11c3915

File content as of revision 31:bacc63106754:

/**
 * @file    TaskletResourceObserver.cpp
 * @brief   mbed CoAP DynamicResource Tasklet-based observer (implementation)
 * @author  Doug Anson/Chris Paola
 * @version 1.0
 * @see
 *
 * Copyright (c) 2014
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
 #include "TaskletResourceObserver.h"

 void *m_instance = NULL;
 
 // constructor
 TaskletResourceObserver::TaskletResourceObserver(DynamicResource *resource,uint8_t timer_id,int sleep_time) : 
                                    ResourceObserver(resource,sleep_time), 
                                    m_tasklet_id(-1),
                                    m_timer_id(timer_id) {
     this->m_event = NULL; 
 }
  
 // destructor
 TaskletResourceObserver::~TaskletResourceObserver() {
     this->stopObservation();
 }
 
 #ifdef CONNECTOR_USING_TASKLETS      
 // set the current event
 void TaskletResourceObserver::setEvent(arm_event_s *event) {
    this->m_event = event;
 }
 #endif
 
 // notifier
 void TaskletResourceObserver::_observation_notifier(arm_event_s *event) {
 #ifdef CONNECTOR_USING_TASKLETS
     if (event != NULL) {
         TaskletResourceObserver *me = (TaskletResourceObserver *)m_instance;
         if (event->event_id == me->getTimerID()) {
             timer_sys_event_cancel(event->event_id);
             me->setEvent(event);
             std::printf("Calling notify()...\r\n");
             if (me->isObserving() == true && me->getResource() != NULL) {
                 std::printf("Calling observe()...\r\n");
                 me->getResource()->observe();
             }
             me->startTimer(); 
         }
         else {
             std::printf("TaskletResourceObserver::observation_notifier(): ignoring event_id=%d timer_id=%d\r\n",event->event_id,me->getTimerID());
         }
     } 
 #endif
 }
 
 // begin observing...
 void TaskletResourceObserver::beginObservation() {
 #ifdef CONNECTOR_USING_TASKLETS
     // create the tasklet and begin the observationing
     if (this->m_tasklet_id < 0) {
        this->m_tasklet_id = arm_ns_tasklet_create(&TaskletResourceObserver::_observation_notifier);
     }
     
     // back pointer
     if (m_instance == NULL) {
        m_instance = (void *)this;
     }
     
     // start a timer if we have created a tasklet...
     if (this->m_tasklet_id >= 0) {
         // start the timer...
         std::printf("TaskletResourceObserver::beginObservation(): starting tasklet timer timer_id=%d sleep=%d...\r\n",this->m_timer_id,this->getSleepTime());
         this->startTimer();
     }
 #endif
 }
 
 // begin observing...
 void TaskletResourceObserver::stopObservation() {
 #ifdef CONNECTOR_USING_TASKLETS
     if (this->m_event != NULL) {
        timer_sys_event_cancel(this->m_event->event_id);
        this->m_timer_active = false;
     }
 #endif
 }
  
 // reset the timer
 void TaskletResourceObserver::startTimer() {
 #ifdef CONNECTOR_USING_TASKLETS
    if (this->m_timer_active == false) {
        timer_sys_event(this->m_timer_id,(uint32_t)this->getSleepTime()); 
        this->m_timer_active = true;
    }
 #endif
 }
 
 // get the timer ID
 uint8_t TaskletResourceObserver::getTimerID() { 
    return this->m_timer_id; 
 }