Doug Anson / mbedConnectorInterface

Dependents:   IoT_LED_demo ServoTest uWater_Project hackathon ... more

Files at this revision

API Documentation at this revision

Comitter:
ansond
Date:
Sat Apr 11 21:14:28 2015 +0000
Parent:
41:fb12c88260ad
Child:
43:769d491a48c1
Commit message:
internalized resource observers

Changed in this revision

api/Options.h Show annotated file Show diff for this revision Revisions of this file
api/OptionsBuilder.cpp Show annotated file Show diff for this revision Revisions of this file
api/OptionsBuilder.h Show annotated file Show diff for this revision Revisions of this file
api/TaskletResourceObserver.cpp Show diff for this revision Revisions of this file
api/TaskletResourceObserver.h Show diff for this revision Revisions of this file
api/ThreadedResourceObserver.h Show annotated file Show diff for this revision Revisions of this file
api/TickerResourceObserver.h Show annotated file Show diff for this revision Revisions of this file
mbedConnectorInterface.h Show annotated file Show diff for this revision Revisions of this file
--- a/api/Options.h	Sat Apr 11 18:42:51 2015 +0000
+++ b/api/Options.h	Sat Apr 11 21:14:28 2015 +0000
@@ -32,12 +32,23 @@
 // include the mbed connector resource list
 #include "mbedConnectorInterface.h"
 
+// determine if we have seen rtos.h yet or not...
+#ifdef RTOS_H
+    // we will use the ThreadedResourceObserver if needed
+    #define CONNECTOR_USING_THREADS   1
+#endif
+
+// include the resource observer includes here so that they are not required in main.cpp
+#include "ThreadedResourceObserver.h"
+#include "TickerResourceObserver.h"
+
 // Vector support
 #include <vector>
 
 // Resources list
 typedef vector<StaticResource *> StaticResourcesList;
 typedef vector<DynamicResource *> DynamicResourcesList;
+typedef vector<ResourceObserver *> ResourceObserversList;
 
 // WiFi Security types
 typedef enum {
@@ -171,8 +182,9 @@
     uint8_t              m_channel;
     
     // Endpoint Resources
-    StaticResourcesList  m_static_resources;
-    DynamicResourcesList m_dynamic_resources;
+    StaticResourcesList   m_static_resources;
+    DynamicResourcesList  m_dynamic_resources;
+    ResourceObserversList m_resource_observers;
 };
 
 } // namespace Connector
--- a/api/OptionsBuilder.cpp	Sat Apr 11 18:42:51 2015 +0000
+++ b/api/OptionsBuilder.cpp	Sat Apr 11 21:14:28 2015 +0000
@@ -43,6 +43,7 @@
     memset(this->m_mac_address,0,NODE_MAC_ADDRESS_LENGTH);
     this->m_static_resources.clear();
     this->m_dynamic_resources.clear();
+    this->m_resource_observers.clear();
 }
 
 // Copy Constructor
@@ -53,6 +54,7 @@
     this->m_endpoint_type = ob.m_endpoint_type;
     this->m_static_resources = ob.m_static_resources;
     this->m_dynamic_resources = ob.m_dynamic_resources;
+    this->m_resource_observers = ob.m_resource_observers;
 }
 
 // Destructor
@@ -60,6 +62,7 @@
 {
     this->m_static_resources.clear();
     this->m_dynamic_resources.clear();
+    this->m_resource_observers.clear();
 }
 
 // set lifetime
@@ -136,14 +139,27 @@
 // add static resource
 OptionsBuilder &OptionsBuilder::addResource(const StaticResource *resource)
 {
-    this->m_static_resources.push_back((StaticResource *)resource);
+    if (resource != NULL) {
+        this->m_static_resources.push_back((StaticResource *)resource);
+    }
     return *this;
 }
 
 // add dynamic resource
-OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource)
+OptionsBuilder &OptionsBuilder::addResource(const DynamicResource *resource,const int sleep_time,const bool use_observer)
 {
-    this->m_dynamic_resources.push_back((DynamicResource *)resource);
+    if (resource != NULL) {
+        this->m_dynamic_resources.push_back((DynamicResource *)resource);
+        if (((DynamicResource *)resource)->isObservable() == true && use_observer == true) {
+#ifdef CONNECTOR_USING_THREADS
+            ThreadedResourceObserver *observer = new ThreadedResourceObserver((DynamicResource *)resource,(int)sleep_time);
+#else
+            TickerResourceObserver *observer = new TickerResourceObserver((DynamicResource *)resource,(int)sleep_time);
+#endif
+            this->m_resource_observers.push_back(observer);
+            observer->beginObservation();
+        }
+    }
     return *this;
 }
 
@@ -188,4 +204,4 @@
     return (Options *)this;
 }
 
-} // namespace Connector
+} // namespace Connector
\ No newline at end of file
--- a/api/OptionsBuilder.h	Sat Apr 11 18:42:51 2015 +0000
+++ b/api/OptionsBuilder.h	Sat Apr 11 21:14:28 2015 +0000
@@ -125,9 +125,11 @@
     /**
     Add a NSDL endpoint resource (dynamic)
     @param dynamic_resource input the NSDL dynamic resource
+    @param sleep_time input the observation sleep time in milliseconds (for observable resource only)
+    @param use_observer input if true, use an appropriate ResourceObserver to observer. if false, the underlying resource will handle it 
     @return instance to ourself
     */
-    OptionsBuilder &addResource(const DynamicResource *static_resource);
+    OptionsBuilder &addResource(const DynamicResource *dynamic_resource,const int sleep_time = 0,const bool use_observer = true);
 
     /**
     Set the WiFi SSID
--- a/api/TaskletResourceObserver.cpp	Sat Apr 11 18:42:51 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,115 +0,0 @@
-/**
- * @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) {
- #ifdef CONNECTOR_USING_TASKLETS
-     this->m_event = NULL; 
- #endif
- }
-  
- // 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
- 
- #ifdef CONNECTOR_USING_TASKLETS
- // notifier
- void TaskletResourceObserver::_observation_notifier(arm_event_s *event) {
-     if (event != NULL) {
-         TaskletResourceObserver *me = (TaskletResourceObserver *)m_instance;
-         if (event->event_id == me->getTimerID()) {
-             timer_sys_event_cancel(event->event_id);
-             me->setEvent(event);
-             if (me->isObserving() == true && me->getResource() != NULL && nsdl_endpoint_is_registered() == true) {
-                 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; 
- }
--- a/api/TaskletResourceObserver.h	Sat Apr 11 18:42:51 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,106 +0,0 @@
-/**
- * @file    TaskletResourceObserver.h
- * @brief   mbed CoAP DynamicResource Tasklet-based observer (header)
- * @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.
- */
-
-#ifndef __TASKLET_RESOURCE_OBSERVER_H__
-#define __TASKLET_RESOURCE_OBSERVER_H__
-
-// mbed support
-#include "mbed.h"
-
-// mbedConnectorInterface configuration
-#include "mbedConnectorInterface.h"
-
-// Base class support
-#include "ResourceObserver.h"
-
-// Tasklet support under network_stubs.h in mbedEndpointNetwork
-#ifdef CONNECTOR_USING_TASKLETS
-    #include "network_stubs.h"
-#endif
-
-class TaskletResourceObserver : public ResourceObserver {
-    public:
-        /**
-        Default Constructor
-        @param resource input the resource to observe
-        @param timer_id input the id for our timer (can be index value of each resource that is observed...)
-        @param sleep_time input the time for the observation tasklet to sleep (in ms)
-        */
-        TaskletResourceObserver(DynamicResource *resource,uint8_t timer_id, int sleep_time = NSP_RD_UPDATE_PERIOD);
-        
-        /**
-        Copy Constructor
-        */
-        TaskletResourceObserver(const TaskletResourceObserver &observer);
-        
-        /**
-        Destructor
-        */
-        virtual ~TaskletResourceObserver();
-        
-        /**
-        begin the observation
-        */
-        virtual void beginObservation();
-        
-        /**
-        stop the observation
-        */
-        virtual void stopObservation();
-        
-#ifdef CONNECTOR_USING_TASKLETS              
-        /**
-        tasklet invoke function (static)
-        */
-        static void _observation_notifier(arm_event_s *event);
-#endif
-
-
-#ifdef CONNECTOR_USING_TASKLETS  
-        /**
-        set the current event
-        @param event input the current event
-        */
-        void setEvent(arm_event_s *event);
-#endif
-        
-        /**
-        start the tasklet timer
-        */
-        void startTimer();
-        
-        /**
-        get the tasklet timer ID
-        @return timer id
-        */
-        uint8_t getTimerID();
-
-    private:
-        int          m_tasklet_id;
-#ifdef CONNECTOR_USING_TASKLETS
-        arm_event_s *m_event;
-#endif
-        uint8_t      m_timer_id;
-        bool         m_timer_active;
-};
-
-#endif // __TASKLET_RESOURCE_OBSERVER_H__
\ No newline at end of file
--- a/api/ThreadedResourceObserver.h	Sat Apr 11 18:42:51 2015 +0000
+++ b/api/ThreadedResourceObserver.h	Sat Apr 11 21:14:28 2015 +0000
@@ -44,7 +44,7 @@
         @param resource input the resource to observe
         @param sleep_time input the time for the observation thread to sleep (in ms)
         */
-        ThreadedResourceObserver(DynamicResource *resource,int sleep_time = NSP_RD_UPDATE_PERIOD);
+        ThreadedResourceObserver(DynamicResource *resource,int sleep_time = NSP_DEFAULT_OBS_PERIOD);
                 
         /**
         Destructor
--- a/api/TickerResourceObserver.h	Sat Apr 11 18:42:51 2015 +0000
+++ b/api/TickerResourceObserver.h	Sat Apr 11 21:14:28 2015 +0000
@@ -40,7 +40,7 @@
         @param timer_id input the id for our timer (can be index value of each resource that is observed...)
         @param sleep_time input the time for the observation tasklet to sleep (in whole seconds...)
         */
-        TickerResourceObserver(DynamicResource *resource,int sleep_time = NSP_RD_UPDATE_PERIOD);
+        TickerResourceObserver(DynamicResource *resource,int sleep_time = NSP_DEFAULT_OBS_PERIOD);
         
         /**
         Copy Constructor
--- a/mbedConnectorInterface.h	Sat Apr 11 18:42:51 2015 +0000
+++ b/mbedConnectorInterface.h	Sat Apr 11 21:14:28 2015 +0000
@@ -29,11 +29,11 @@
 /************** DEFAULT CONFIGURATION PARAMETERS  ************************/
 
 // NSP Configuration
-#define NSP_LIFE_TIME_LENGTH     24                     
-#define NSP_LIFE_TIME            "120"                                       // seconds
-#define NSP_REREGISTER_TIME      60                                          // 1/2 of NSP_LIFE_TIME as int in seconds...
-#define NSP_COAP_UDP_PORT        5683
-#define NSP_RD_UPDATE_PERIOD     30000                                       // (in ms) - 30 seconds 
+#define NSP_COAP_UDP_PORT        5683                                        // Default CoAP UDP port
+#define NSP_LIFE_TIME_LENGTH     24                                          // length of the lifetime buffer string
+#define NSP_LIFE_TIME            "120"                                       // liftime buffer (representing seconds)
+#define NSP_RD_UPDATE_PERIOD     60000                                       // (in ms) - 60 seconds (1/2 of NSP_LIFE_TIME seconds)
+#define NSP_DEFAULT_OBS_PERIOD   10000                                       // (in ms) - 10 seconds between observations..
 
 // 6LowPAN Configuration
 #define NODE_MAC_ADDRESS_LENGTH  8