mbed Connector Interface simplification API on top of mbed-client

Fork of mbedConnectorInterfaceV3 by Doug Anson

NOTE:

This repo has been replaced with https://github.com/ARMmbed/mbedConnectorInterface. No further updates will occur with this repo. Please use the github repo instead. Thanks!

Committer:
ansond
Date:
Thu Jul 13 19:04:25 2017 +0000
Revision:
123:2167e9286edb
Parent:
122:4072e03884e4
Child:
124:097ec45bd12b
updates for EventQueue usage

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 122:4072e03884e4 1 /**
ansond 122:4072e03884e4 2 * @file EventQueueResourceObserver.cpp
ansond 122:4072e03884e4 3 * @brief mbed CoAP DynamicResource Thread-based observer (implementation)
ansond 122:4072e03884e4 4 * @author Doug Anson/Chris Paola
ansond 122:4072e03884e4 5 * @version 1.0
ansond 122:4072e03884e4 6 * @see
ansond 122:4072e03884e4 7 *
ansond 122:4072e03884e4 8 * Copyright (c) 2017
ansond 122:4072e03884e4 9 *
ansond 122:4072e03884e4 10 * Licensed under the Apache License, Version 2.0 (the "License");
ansond 122:4072e03884e4 11 * you may not use this file except in compliance with the License.
ansond 122:4072e03884e4 12 * You may obtain a copy of the License at
ansond 122:4072e03884e4 13 *
ansond 122:4072e03884e4 14 * http://www.apache.org/licenses/LICENSE-2.0
ansond 122:4072e03884e4 15 *
ansond 122:4072e03884e4 16 * Unless required by applicable law or agreed to in writing, software
ansond 122:4072e03884e4 17 * distributed under the License is distributed on an "AS IS" BASIS,
ansond 122:4072e03884e4 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ansond 122:4072e03884e4 19 * See the License for the specific language governing permissions and
ansond 122:4072e03884e4 20 * limitations under the License.
ansond 122:4072e03884e4 21 */
ansond 122:4072e03884e4 22
ansond 122:4072e03884e4 23 // Class support
ansond 122:4072e03884e4 24 #include "mbed-connector-interface/EventQueueResourceObserver.h"
ansond 122:4072e03884e4 25
ansond 122:4072e03884e4 26 #ifdef CONNECTOR_USING_EVENT_QUEUES
ansond 122:4072e03884e4 27
ansond 122:4072e03884e4 28 // constructor
ansond 122:4072e03884e4 29 EventQueueResourceObserver::EventQueueResourceObserver(DynamicResource *resource,int sleep_time) : ResourceObserver(resource,sleep_time), m_event_queue() {
ansond 122:4072e03884e4 30 // default is not observing...
ansond 122:4072e03884e4 31 this->setObserving(false);
ansond 122:4072e03884e4 32
ansond 122:4072e03884e4 33 // DEBUG
ansond 122:4072e03884e4 34 this->logger()->log("EventQueueResourceObserver being used for %s (sleep_time: %d ms)",resource->getFullName().c_str(),sleep_time);
ansond 122:4072e03884e4 35
ansond 122:4072e03884e4 36 // start the thread by invoking the thread task...
ansond 123:2167e9286edb 37 this->m_id = this->m_event_queue.call_every(sleep_time,callback(this,&EventQueueResourceObserver::observation_task));
ansond 122:4072e03884e4 38 }
ansond 122:4072e03884e4 39
ansond 122:4072e03884e4 40 // destructor
ansond 122:4072e03884e4 41 EventQueueResourceObserver::~EventQueueResourceObserver() {
ansond 122:4072e03884e4 42 this->stopObservation();
ansond 123:2167e9286edb 43 this->m_event_queue.cancel(this->m_id);
ansond 122:4072e03884e4 44 }
ansond 122:4072e03884e4 45
ansond 122:4072e03884e4 46 // observation task method
ansond 122:4072e03884e4 47 void EventQueueResourceObserver::observation_task() {
ansond 122:4072e03884e4 48 if (this->isObserving() == true && this->getResource() != NULL) {
ansond 122:4072e03884e4 49 DynamicResource *res = this->getResource();
ansond 122:4072e03884e4 50 if (res != NULL && res->isRegistered() == true) {
ansond 122:4072e03884e4 51 res->observe();
ansond 122:4072e03884e4 52 }
ansond 122:4072e03884e4 53 }
ansond 122:4072e03884e4 54 }
ansond 122:4072e03884e4 55
ansond 122:4072e03884e4 56 // begin observing...
ansond 122:4072e03884e4 57 void EventQueueResourceObserver::beginObservation() {
ansond 122:4072e03884e4 58 this->setObserving(true);
ansond 122:4072e03884e4 59 }
ansond 122:4072e03884e4 60
ansond 122:4072e03884e4 61 // stop observing...
ansond 122:4072e03884e4 62 void EventQueueResourceObserver::stopObservation() {
ansond 122:4072e03884e4 63 this->setObserving(false);
ansond 122:4072e03884e4 64 }
ansond 122:4072e03884e4 65
ansond 122:4072e03884e4 66 // halt the underlying observer mechanism
ansond 122:4072e03884e4 67 void EventQueueResourceObserver::halt() {
ansond 122:4072e03884e4 68 this->m_event_queue.cancel();
ansond 122:4072e03884e4 69 }
ansond 122:4072e03884e4 70
ansond 122:4072e03884e4 71 #endif // CONNECTOR_USING_EVENT_QUEUES