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:
Tue Sep 26 16:01:31 2017 +0000
Revision:
127:b4a661ff6fb9
Parent:
126:f37e34daa100
minor re-ordering of FCC init

Who changed what in which revision?

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