mbed Connector Interface simplification API on top of mbed-client
Fork of mbedConnectorInterfaceV3 by
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!
source/ThreadedResourceObserver.cpp@13:9edad7677211, 2016-06-08 (annotated)
- Committer:
- ansond
- Date:
- Wed Jun 08 22:32:08 2016 +0000
- Revision:
- 13:9edad7677211
- Parent:
- 0:1f1f55e73248
- Child:
- 16:dffa38c3340f
updated to latest revision with new DM functions
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:1f1f55e73248 | 1 | /** |
ansond | 0:1f1f55e73248 | 2 | * @file ThreadedResourceObserver.cpp |
ansond | 0:1f1f55e73248 | 3 | * @brief mbed CoAP DynamicResource Thread-based observer (implementation) |
ansond | 0:1f1f55e73248 | 4 | * @author Doug Anson/Chris Paola |
ansond | 0:1f1f55e73248 | 5 | * @version 1.0 |
ansond | 0:1f1f55e73248 | 6 | * @see |
ansond | 0:1f1f55e73248 | 7 | * |
ansond | 0:1f1f55e73248 | 8 | * Copyright (c) 2014 |
ansond | 0:1f1f55e73248 | 9 | * |
ansond | 0:1f1f55e73248 | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ansond | 0:1f1f55e73248 | 11 | * you may not use this file except in compliance with the License. |
ansond | 0:1f1f55e73248 | 12 | * You may obtain a copy of the License at |
ansond | 0:1f1f55e73248 | 13 | * |
ansond | 0:1f1f55e73248 | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
ansond | 0:1f1f55e73248 | 15 | * |
ansond | 0:1f1f55e73248 | 16 | * Unless required by applicable law or agreed to in writing, software |
ansond | 0:1f1f55e73248 | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
ansond | 0:1f1f55e73248 | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ansond | 0:1f1f55e73248 | 19 | * See the License for the specific language governing permissions and |
ansond | 0:1f1f55e73248 | 20 | * limitations under the License. |
ansond | 0:1f1f55e73248 | 21 | */ |
ansond | 0:1f1f55e73248 | 22 | |
ansond | 0:1f1f55e73248 | 23 | #include "ThreadedResourceObserver.h" |
ansond | 0:1f1f55e73248 | 24 | |
ansond | 0:1f1f55e73248 | 25 | #ifdef CONNECTOR_USING_THREADS |
ansond | 0:1f1f55e73248 | 26 | // DEBUG |
ansond | 0:1f1f55e73248 | 27 | //DigitalOut __threaded_led(LED2); |
ansond | 0:1f1f55e73248 | 28 | #endif |
ansond | 0:1f1f55e73248 | 29 | |
ansond | 0:1f1f55e73248 | 30 | // constructor |
ansond | 0:1f1f55e73248 | 31 | ThreadedResourceObserver::ThreadedResourceObserver(DynamicResource *resource,int sleep_time) : |
ansond | 0:1f1f55e73248 | 32 | ResourceObserver(resource,sleep_time) |
ansond | 0:1f1f55e73248 | 33 | #ifdef CONNECTOR_USING_THREADS |
ansond | 0:1f1f55e73248 | 34 | ,m_observation_thread(&ThreadedResourceObserver::_observation_notifier,this) |
ansond | 0:1f1f55e73248 | 35 | #endif |
ansond | 0:1f1f55e73248 | 36 | { |
ansond | 0:1f1f55e73248 | 37 | this->setObserving(false); |
ansond | 0:1f1f55e73248 | 38 | // DEBUG |
ansond | 0:1f1f55e73248 | 39 | std::printf("ThreadedResourceObserver being used for %s (sleep_time=%d)\r\n",resource->getFullName().c_str(),sleep_time); |
ansond | 0:1f1f55e73248 | 40 | } |
ansond | 0:1f1f55e73248 | 41 | |
ansond | 0:1f1f55e73248 | 42 | // destructor |
ansond | 0:1f1f55e73248 | 43 | ThreadedResourceObserver::~ThreadedResourceObserver() { |
ansond | 0:1f1f55e73248 | 44 | this->stopObservation(); |
ansond | 0:1f1f55e73248 | 45 | #ifdef CONNECTOR_USING_THREADS |
ansond | 0:1f1f55e73248 | 46 | this->m_observation_thread.terminate(); |
ansond | 0:1f1f55e73248 | 47 | #endif |
ansond | 0:1f1f55e73248 | 48 | } |
ansond | 0:1f1f55e73248 | 49 | |
ansond | 0:1f1f55e73248 | 50 | // notifier |
ansond | 0:1f1f55e73248 | 51 | void ThreadedResourceObserver::_observation_notifier(void const *instance) { |
ansond | 0:1f1f55e73248 | 52 | #ifdef CONNECTOR_USING_THREADS |
ansond | 0:1f1f55e73248 | 53 | ThreadedResourceObserver *me = (ThreadedResourceObserver *)instance; |
ansond | 0:1f1f55e73248 | 54 | while(true) { |
ansond | 0:1f1f55e73248 | 55 | Thread::wait(me->getSleepTime()); |
ansond | 13:9edad7677211 | 56 | if (me->isObserving() == true && me->getResource() != NULL && me->getResource()->isConnected() == true) { |
ansond | 0:1f1f55e73248 | 57 | // |
ansond | 0:1f1f55e73248 | 58 | // implementsObservation(): This switch denotes whether this threaded resource generates its own observations or relies on the heartbeat (time-based) mechanism. |
ansond | 0:1f1f55e73248 | 59 | // -- if resource itself doesn't implement it, then we can call notify() with the get() on each heartbeat of the threaded resource (see note below) |
ansond | 0:1f1f55e73248 | 60 | // -- otherwise, we let the resource itself call observe() as part of its observation implementation |
ansond | 0:1f1f55e73248 | 61 | // |
ansond | 0:1f1f55e73248 | 62 | // 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 | 0:1f1f55e73248 | 63 | // as there will be no observations for this resource regardless of this switch value. |
ansond | 0:1f1f55e73248 | 64 | // |
ansond | 0:1f1f55e73248 | 65 | bool do_notify = !(me->getResource()->implementsObservation()); |
ansond | 0:1f1f55e73248 | 66 | me->getResource()->observe(); |
ansond | 0:1f1f55e73248 | 67 | //__threaded_led = !__threaded_led; |
ansond | 0:1f1f55e73248 | 68 | } |
ansond | 0:1f1f55e73248 | 69 | } |
ansond | 0:1f1f55e73248 | 70 | #endif |
ansond | 0:1f1f55e73248 | 71 | } |
ansond | 0:1f1f55e73248 | 72 | |
ansond | 0:1f1f55e73248 | 73 | // begin observing... |
ansond | 0:1f1f55e73248 | 74 | void ThreadedResourceObserver::beginObservation() { |
ansond | 0:1f1f55e73248 | 75 | this->setObserving(true); |
ansond | 0:1f1f55e73248 | 76 | } |
ansond | 0:1f1f55e73248 | 77 | |
ansond | 0:1f1f55e73248 | 78 | // stop observing... |
ansond | 0:1f1f55e73248 | 79 | void ThreadedResourceObserver::stopObservation() { |
ansond | 0:1f1f55e73248 | 80 | this->setObserving(false); |
ansond | 0:1f1f55e73248 | 81 | } |