mqtt specific components for the impact mbed endpoint library

Dependents:   mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_mqtt_endpoint_nxp

Files at this revision

API Documentation at this revision

Comitter:
ansond
Date:
Fri Mar 28 16:24:22 2014 +0000
Parent:
6:2db2c7e75ad9
Child:
8:17c29d3b967b
Commit message:
refactor personality

Changed in this revision

IOCEndpoint.cpp Show annotated file Show diff for this revision Revisions of this file
IOCEndpoint.h Show annotated file Show diff for this revision Revisions of this file
MBEDEndpoint.cpp Show annotated file Show diff for this revision Revisions of this file
MBEDEndpoint.h Show annotated file Show diff for this revision Revisions of this file
MQTTDefinitions.h Show annotated file Show diff for this revision Revisions of this file
MQTTTransport.cpp Show annotated file Show diff for this revision Revisions of this file
MQTTTransport.h Show annotated file Show diff for this revision Revisions of this file
--- a/IOCEndpoint.cpp	Thu Mar 27 18:14:05 2014 +0000
+++ b/IOCEndpoint.cpp	Fri Mar 28 16:24:22 2014 +0000
@@ -21,7 +21,7 @@
  
  // MBED instance support
  #include "MBEDEndpoint.h"
- 
+  
  // default constructor
  IOCEndpoint::IOCEndpoint(ErrorHandler *error_handler,void *endpoint) {
      this->m_error_handler = error_handler;
@@ -33,7 +33,7 @@
  }
  
  // build out the IOC Payload
- char *IOCEndpoint::buildPayload(char *data,int data_length,Light *light) {
+ char *IOCEndpoint::buildLightPayload(char *data,int data_length,Light *light) {
      char tmp[TEMP_BUFFER_LEN+1];
      
      // MBED Endpoint
@@ -69,8 +69,8 @@
             
                  // Handle /dev/addldata 
                  char *dev_addldata = endpoint->getMap()->endpointNameToIOCName("/dev/addldata");
-                 if (dev_addldata != NULL && strcmp(name,dev_addldata) == 0 && light != NULL && light->getIOCID() > 0) {
-                     char buf[IOC_IOC_ID_LEN+1]; memset(buf,0,IOC_IOC_ID_LEN+1); sprintf(buf,"%d",light->getIOCID());
+                 if (dev_addldata != NULL && strcmp(name,dev_addldata) == 0 && light != NULL && light->getExternalID() > 0) {
+                     char buf[IOC_IOC_ID_LEN+1]; memset(buf,0,IOC_IOC_ID_LEN+1); sprintf(buf,"%d",light->getExternalID());
                      sprintf(tmp,"\"%s\":\"id:%s\",",name,buf);
                      strcat(data,tmp);
                  }
@@ -101,8 +101,8 @@
      return data;
  }
   
- // save the IOC ID for a Light node
- void IOCEndpoint::saveLightID(Light *light,char *json) {
+ // save the IOC ID for our Personality
+ void IOCEndpoint::saveExternalID(Personality *instance,char *json) {
      if (json != NULL) {          
          //this->logger()->log("RESULT: %s",json);
          
@@ -126,7 +126,7 @@
                  sscanf(str_ioc_id,"%d",&ioc_id);
                           
                  // save the IOC ID
-                 if (ioc_id > 0) light->setIOCID(ioc_id);
+                 if (ioc_id > 0) instance->setExternalID(ioc_id);
              }
              else {
                  // cannot find the ID tag in the result JSON
--- a/IOCEndpoint.h	Thu Mar 27 18:14:05 2014 +0000
+++ b/IOCEndpoint.h	Fri Mar 28 16:24:22 2014 +0000
@@ -22,7 +22,7 @@
 // Error handler support
 #include "ErrorHandler.h"
 
-// Light personality support
+// Personality support - Lights
 #include "Light.h"
 
 class IOCEndpoint {
@@ -34,8 +34,9 @@
         IOCEndpoint(ErrorHandler *error_handler,void *endpoint);
        ~IOCEndpoint();
        
-       char *buildPayload(char *data,int data_length,Light *light);
-       void saveLightID(Light *light,char *data);
+       char *buildLightPayload(char *data,int data_length,Light *light);
+       
+       void saveExternalID(Personality *instance,char *data);
        
     private:
         ErrorHandler *logger(); 
--- a/MBEDEndpoint.cpp	Thu Mar 27 18:14:05 2014 +0000
+++ b/MBEDEndpoint.cpp	Fri Mar 28 16:24:22 2014 +0000
@@ -63,7 +63,7 @@
      if (success) this->logger()->turnLEDYellow();
      if (success)this->m_map = new MBEDToIOCResourceMap(error_handler); 
      if (success) success = this->initializeTransports();
-     if (success) success = this->initializeEndpointPersonality();   
+     if (success) success = this->initializePersonalities();   
      if (success) this->logger()->turnLEDOrange();
      this->logger()->lcdStatusOnly(true);
      if (!success) closedown(2);
@@ -73,7 +73,7 @@
  MBEDEndpoint::~MBEDEndpoint() {
      bool success = true;
      if (success) this->logger()->turnLEDYellow();
-     if (success) success = this->closeEndpointPersonality();
+     if (success) success = this->closePersonalities();
      if (success) success = this->closeTransports();
      if (success) success = this->closeEthernet();
      if (success) this->logger()->turnLEDBlue();
@@ -104,10 +104,10 @@
      memset(this->m_lcd_status,0,TEMP_BUFFER_LEN+1);
      
      // look at Light#0 to determine the IOC linkage ID...
-     char *ioc = this->m_lights[0]->getResourceFactory()->getResourceValue(IOC_LINKAGE_RESOURCE);
+     char *ioc = this->m_personalities[0]->getResourceFactory()->getResourceValue(EXTERNAL_LINKAGE_RESOURCE);
      
      // color our LED depending on whether we have IOC linkage or not...
-     if (ioc == NULL || strcmp(ioc,IOC_LINKAGE_UNSET) == 0) this->logger()->turnLEDOrange();
+     if (ioc == NULL || strcmp(ioc,EXTERNAL_LINKAGE_UNSET) == 0) this->logger()->turnLEDOrange();
      else this->logger()->turnLEDGreen();
            
      sprintf(this->m_lcd_status,"Node: %s\nGW IP: %s\nIOC Link: %s",this->getEndpointName(),this->getGWAddress(),ioc);
@@ -115,7 +115,7 @@
  }
  
  // initialize our personality
- bool MBEDEndpoint::initializeEndpointPersonality() {
+ bool MBEDEndpoint::initializePersonalities() {
  #ifdef LIGHT_PERSONALITY
     return this->initializeLights();
  #else
@@ -125,23 +125,23 @@
  
  // initialize the Lights
  bool MBEDEndpoint::initializeLights() {
-     int index = this->preferences()->getIntPreference("endpoint_id",LIGHT_NAME_INDEX); 
+     int index = this->preferences()->getIntPreference("endpoint_id",PERSONALITY_NAME_INDEX); 
      this->logger()->log("Initializing Lights...");
-     for(int i=0;i<NUM_LIGHTS;++i) {
-         this->m_lights[i] = new MBEDLight(this->logger(),this->m_transports,i+index,this);
-         this->m_lights[i]->setDimmerAction(new EmulatedLightDimmerAction(this->logger(),this->m_lights[i]));
-         this->m_lights[i]->setSwitchAction(new EmulatedLightSwitchAction(this->logger(),this->m_lights[i]));
+     for(int i=0;i<NUM_PERSONALITY_INSTANCES;++i) {
+         this->m_personalities[i] = new MBEDLight(this->logger(),this->m_transports,i+index,this);
+         this->m_personalities[i]->setDimmerAction(new EmulatedLightDimmerAction(this->logger(),this->m_personalities[i]));
+         this->m_personalities[i]->setSwitchAction(new EmulatedLightSwitchAction(this->logger(),this->m_personalities[i]));
      }
      return true;
  }
  
- // does the input name match any of our light resources?
- int MBEDEndpoint::indexOfLight(char *name) {
+ // does the input name match any of our personality instances?
+ int MBEDEndpoint::indexOfPersonality(char *name) {
      bool found = false;
      int index = -1;
      
-     for(int i=0;i<NUM_LIGHTS && !found;++i) {
-         if (strcmp(this->m_lights[i]->getName(),name) == 0) {
+     for(int i=0;i<NUM_PERSONALITY_INSTANCES && !found;++i) {
+         if (strcmp(this->m_personalities[i]->getName(),name) == 0) {
              found = true;
              index = i;
          }
@@ -152,7 +152,7 @@
  
  // get a specific resources
  ResourceFactory *MBEDEndpoint::getResources(int index) {
-     if (index >= 0 && index < NUM_LIGHTS) return this->m_lights[index]->resources();
+     if (index >= 0 && index < NUM_PERSONALITY_INSTANCES) return this->m_personalities[index]->getResourceFactory();
      return NULL;
  }
  
@@ -170,9 +170,9 @@
  
  // Initialize the Endpoint Name - will be the first Light resource name (and there must be one...)
  void MBEDEndpoint::initEndpointName() {
-     this->m_instance_id = this->preferences()->getIntPreference("endpoint_id",LIGHT_NAME_INDEX);
-     memset(this->m_endpoint_name,0,LIGHT_NAME_LEN+1);
-     sprintf(this->m_endpoint_name,LIGHT_NAME,this->m_instance_id);
+     this->m_instance_id = this->preferences()->getIntPreference("endpoint_id",PERSONALITY_NAME_INDEX);
+     memset(this->m_endpoint_name,0,PERSONALITY_NAME_LEN+1);
+     sprintf(this->m_endpoint_name,PERSONALITY_NAME,this->m_instance_id);
  }
  
  // get our endpoint name
@@ -242,16 +242,16 @@
      return success;
  }
  
- // load up all endpoints into the IOC
- bool MBEDEndpoint::loadEndpoints() {
+ // load up all personality instances into the IOC
+ bool MBEDEndpoint::loadPersonalities() {
      bool success = true;
-     this->logger()->log("Loading All Endpoints to IOC...");
-     for(int i=0;i<NUM_LIGHTS && success;++i) success = this->loadEndpoint(this->m_lights[i]);
+     this->logger()->log("Loading All Personalities into the IOC...");
+     for(int i=0;i<NUM_PERSONALITY_INSTANCES && success;++i) success = this->loadPersonality(this->m_personalities[i]);
      return success;
  }
  
- // load up our endpoint to the IOC
- bool MBEDEndpoint::loadEndpoint(Light *light) {      
+ // load up our personality to the IOC
+ bool MBEDEndpoint::loadPersonality(Personality *instance) {      
      bool success = false;
      char result[IOC_RESULT_LEN+1];
      char payload[IOC_PAYLOAD_LEN+1];
@@ -261,15 +261,18 @@
      memset(payload,0,IOC_PAYLOAD_LEN+1);
      
      // DEBUG
-     this->logger()->log("Building Payload for Light: %s...",light->getName());
+     this->logger()->log("Building Payload for Personality(%s): %s...",instance->getType(),instance->getName());
           
-     // build the payload
-     char *data = this->iocEndpoint()->buildPayload(payload,IOC_PAYLOAD_LEN,light);
+     // build the personality payload
+      char *data = NULL;
+#ifdef LIGHT_PERSONALITY
+     data = this->iocEndpoint()->buildLightPayload(payload,IOC_PAYLOAD_LEN,(Light *)instance);
+#endif
       
      // issue the request
      if (data != NULL && strlen(data) > 0) {
         // DEBUG
-        this->logger()->log("Sending Light: %s to the IOC...",light->getName());
+        this->logger()->log("Sending Personality(%s): %s to the IOC...",instance->getType(),instance->getName());
      
         // load
         success = this->m_transports[LOAD_TRANSPORT]->loadEndpoint((char *)payload,strlen(payload),(char *)result,IOC_RESULT_LEN);
@@ -278,37 +281,37 @@
      // DEBUG
      //if (success) this->logger()->log("Saving IOC ID for Light: %s...",light->getName());
      
-     // update the IOC ID if found
-     if (success) this->iocEndpoint()->saveLightID(light,result);
+     // update the External ID if found
+     if (success) this->iocEndpoint()->saveExternalID(instance,result);
      
      // DEBUG
-     if (success) this->logger()->log("Light: %s IOC ID=%d sent successfully",light->getName(),light->getIOCID());
+     if (success) this->logger()->log("Personality(%s): %s IOC ID=%d sent successfully",instance->getType(),instance->getName(),instance->getExternalID());
      
      // DEBUG
      if (!success) {
-         if (light != NULL) this->logger()->log("Light: %s send FAILED",light->getName());
-         else this->logger()->log("Light: send FAILED (NULL LIGHT)");
+         if (instance != NULL) this->logger()->log("Personality(%s): %s send FAILED",instance->getType(),instance->getName());
+         else this->logger()->log("Personality send FAILED: NULL Personality instance");
      }
      
      // return our status
      return success;
  }
  
- // update all endpoints to the IOC
- bool MBEDEndpoint::updateEndpoints() {
+ // update all personality instances into the IOC
+ bool MBEDEndpoint::updatePersonalities() {
      bool success = true;
-     for(int i=0;i<NUM_LIGHTS && success;++i) success = this->updateEndpoints(i);
+     for(int i=0;i<NUM_PERSONALITY_INSTANCES && success;++i) success = this->updatePersonality(i);
      return success;
  }
  
- // update all endpoints to the IOC
- bool MBEDEndpoint::updateEndpoints(int index) {
-     if (index >= 0 && index < NUM_LIGHTS) return this->updateEndpoint(this->m_lights[index]);
+ // update all personality instances to the IOC
+ bool MBEDEndpoint::updatePersonality(int index) {
+     if (index >= 0 && index < NUM_PERSONALITY_INSTANCES) return this->updatePersonality(this->m_personalities[index]);
      return false; 
  }
  
- // update our endpoint with the IOC
- bool MBEDEndpoint::updateEndpoint(Light *light) { 
+ // update our ith personality instance with the IOC
+ bool MBEDEndpoint::updatePersonality(Personality *instance) { 
      bool success = false;
      char result[IOC_RESULT_LEN+1];
      char payload[IOC_PAYLOAD_LEN+1];
@@ -318,40 +321,35 @@
      memset(payload,0,IOC_PAYLOAD_LEN+1);
           
      // build the payload
-     char *data = this->iocEndpoint()->buildPayload(payload,IOC_PAYLOAD_LEN,light);
-     
+     char *data = NULL;
+
+#ifdef LIGHT_PERSONALITY     
+     data = this->iocEndpoint()->buildLightPayload(payload,IOC_PAYLOAD_LEN,(Light *)instance);
+#endif
+
      // issue the request  
      if (data != NULL && strlen(data) > 0) {
         // DEBUG
-        this->logger()->log("Updating Light: %s at the IOC...",light->getName()); 
+        this->logger()->log("Updating Personality(%s): %s at the IOC...",instance->getType(),instance->getName()); 
         
         // update
-        success = this->m_transports[LOAD_TRANSPORT]->updateEndpoint(light->getIOCID(),(char *)payload,strlen(payload),(char *)result,IOC_RESULT_LEN);
+        success = this->m_transports[LOAD_TRANSPORT]->updateEndpoint(instance->getExternalID(),(char *)payload,strlen(payload),(char *)result,IOC_RESULT_LEN);
      }
      
      // DEBUG
-     if (success) this->logger()->log("Update of Endpoint to IOC successful");
-     else this->logger()->log("Update of Endpoint to IOC FAILED");
+     if (success) this->logger()->log("Update of Personality instance to IOC successful");
+     else this->logger()->log("Update of Personality instance to IOC FAILED");
           
      // return our status
      return success; 
  } 
  
- // close down our endpoint personality
- bool MBEDEndpoint::closeEndpointPersonality() {
- #ifdef LIGHT_PERSONALITY
-    return this->closeLights();
- #else
-    return NULL;
- #endif
- }
- 
- // close down the Lights
- bool MBEDEndpoint::closeLights() {
+ // close down our personalities
+ bool MBEDEndpoint::closePersonalities() {
     bool success = true;
-    this->logger()->log("Closing down Lights...");
-    for(int i=0;i<NUM_LIGHTS;++i)
-        if (this->m_lights[i] != NULL) delete this->m_lights[i];
+    this->logger()->log("Closing down Personalities...");
+    for(int i=0;i<NUM_PERSONALITY_INSTANCES;++i)
+        if (this->m_personalities[i] != NULL) delete this->m_personalities[i];
     return success;
  }
  
--- a/MBEDEndpoint.h	Thu Mar 27 18:14:05 2014 +0000
+++ b/MBEDEndpoint.h	Fri Mar 28 16:24:22 2014 +0000
@@ -46,17 +46,17 @@
   
 class MBEDEndpoint {
     private:
-        EthernetInterface    *m_ethernet;                              // ethernet interface
-        ErrorHandler         *m_error_handler;                         // our error handler
-        IOCEndpoint          *m_ioc_endpoint;                          // IOC integration
-        Transport            *m_transports[NUM_TRANSPORTS];            // our transport
-        Light                *m_lights[NUM_LIGHTS];                    // our lights (at least 1)
-        char                  m_endpoint_name[LIGHT_NAME_LEN+1];       // our endpoint name (light[0])
-        MBEDToIOCResourceMap *m_map;                                   // IOC <--> MBED resource mapping
-        Preferences          *m_preferences;                           // preference support
-        char                  m_lcd_status[TEMP_BUFFER_LEN+1];         // LCD status buffer
-        char                  m_gw_address[PREFERENCE_VALUE_LEN+1];    // GW address buffer
-        int                   m_instance_id;                           // instance ID for this endpoint
+        EthernetInterface    *m_ethernet;                                  // ethernet interface
+        ErrorHandler         *m_error_handler;                             // our error handler
+        IOCEndpoint          *m_ioc_endpoint;                              // IOC integration
+        Transport            *m_transports[NUM_TRANSPORTS];                // our transport
+        Light                *m_personalities[NUM_PERSONALITY_INSTANCES];  // our personality instances (at least 1)
+        char                  m_endpoint_name[PERSONALITY_NAME_LEN+1];     // our endpoint name (personality[0])
+        MBEDToIOCResourceMap *m_map;                                       // IOC <--> MBED resource mapping
+        Preferences          *m_preferences;                               // preference support
+        char                  m_lcd_status[TEMP_BUFFER_LEN+1];             // LCD status buffer
+        char                  m_gw_address[PREFERENCE_VALUE_LEN+1];        // GW address buffer
+        int                   m_instance_id;                               // instance ID for this endpoint
          
     public:
         MBEDEndpoint(ErrorHandler *error_handler,EthernetInterface *ethernet);
@@ -64,12 +64,12 @@
         ResourceFactory *initResourceFactory();
         void run();
         
-        int indexOfLight(char *name);
+        int indexOfPersonality(char *name);
         ResourceFactory *getResources(int index);
         
-        bool loadEndpoints();
-        bool updateEndpoints();
-        bool updateEndpoints(int index);
+        bool loadPersonalities();
+        bool updatePersonalities();
+        bool updatePersonality(int index);
         
         char *getEndpointName();
         char *getGWAddress();
@@ -84,20 +84,20 @@
     protected:
     
     private:
-        bool initializeEndpointPersonality();
-        bool closeEndpointPersonality();
+        bool initializePersonalities();
+        bool closePersonalities();
         
         ResourceFactory *initLightResourceFactory();
+        bool initializeLights();
+        
         void initPreferences();
         void initGWAddress();
         void initEndpointName();
-        bool initializeLights();
         bool initializeTransport(int index,char *key,Transport *transport);
         bool initializeTransports();
         bool initializeEthernet(EthernetInterface *ethernet);
-        bool loadEndpoint(Light *light);
-        bool updateEndpoint(Light *light);
-        bool closeLights();
+        bool loadPersonality(Personality *instance);
+        bool updatePersonality(Personality *instance);
         bool closeTransport(int index,char *key);
         bool closeTransports();
         bool closeEthernet(); 
--- a/MQTTDefinitions.h	Thu Mar 27 18:14:05 2014 +0000
+++ b/MQTTDefinitions.h	Fri Mar 28 16:24:22 2014 +0000
@@ -33,9 +33,7 @@
 #define PREFERENCES_FILE            "/local/mqtt.cfg"               // preferences file for endpoint  
 
 // Light Configuration
-#define LIGHT_NAME                  "light-mqtt-%d"                 // name of each light in this endpoint
-#define LIGHT_BLINK_WAIT_MS         1000                            // time between blinks (ms)
-#define LIGHT_NAME_INDEX            1                               // start index for the ID of the light name
+#define PERSONALITY_NAME            "light-mqtt-%d"                 // name of each personality (i.e. light) in this endpoint
 
 // Keyword Definitions for MQTT packets
 #define IOC_REQUEST_LOAD_ALL_VERB   "load"
--- a/MQTTTransport.cpp	Thu Mar 27 18:14:05 2014 +0000
+++ b/MQTTTransport.cpp	Fri Mar 28 16:24:22 2014 +0000
@@ -92,7 +92,7 @@
  // pull the endpoint name from the MQTT topic
  char *MQTTTransport::getEndpointNameFromTopic(char *topic) {
      if (topic != NULL) {
-         memset(this->m_endpoint_name,0,LIGHT_NAME_LEN+1);
+         memset(this->m_endpoint_name,0,PERSONALITY_NAME_LEN+1);
          char trash[MQTT_IOC_TOPIC_LEN+1];
          char ep[MQTT_IOC_TOPIC_LEN+1];
          memset(trash,0,MQTT_IOC_TOPIC_LEN+1);
@@ -168,30 +168,30 @@
              if (message_verb != NULL && strcmp(message_verb,IOC_REQUEST_LOAD_ALL_VERB) == 0) {     // load
                  if (message_value != NULL && strcmp(message_value,IOC_ENDPOINT_ALL_VERB) == 0) {   // all
                     // load up our endpoints
-                    endpoint->loadEndpoints();
-                    endpoint->updateEndpoints();
+                    endpoint->loadPersonalities();
+                    endpoint->updatePersonalities();
                  }
                  else if (message_value != NULL && strcmp(message_value,this->m_endpoint_name) == 0) {
                     // load up our endpoints (us only)
-                    endpoint->loadEndpoints();
-                    endpoint->updateEndpoints();
+                    endpoint->loadPersonalities();
+                    endpoint->updatePersonalities();
                  }
              }
              
              else if (message_verb != NULL && strcmp(message_verb,IOC_REQUEST_UPDATE_ALL_VERB) == 0) {  // update
                  if (message_value != NULL && strcmp(message_value,IOC_ENDPOINT_ALL_VERB) == 0) {       // all
                      // update our endpoints
-                     endpoint->updateEndpoints();
+                     endpoint->updatePersonalities();
                  }
                  else {
                      // update just our endpoint
                      int index = -1;
                      if (message_name != NULL) {
-                         index = endpoint->indexOfLight((char *)message_name);
+                         index = endpoint->indexOfPersonality((char *)message_name);
                          if (index >= 0) {
                              if (message_verb != NULL && strcmp(message_verb,IOC_REQUEST_UPDATE_ALL_VERB) == 0) {
                                 // update our endpoint
-                                endpoint->updateEndpoints(index);
+                                endpoint->updatePersonality(index);
                              }
                          }
                      }
@@ -203,7 +203,7 @@
          if (message_type != NULL && strcmp(message_type,IOC_CHANGE_VERB) == 0) {
               if (message_name != NULL) {
                  // destined for our lights?
-                 int index = endpoint->indexOfLight((char *)message_name);
+                 int index = endpoint->indexOfPersonality((char *)message_name);
                  if (index >= 0) {
                      if (message_verb != NULL) {
                          // map the parameter to one of ours
@@ -226,7 +226,7 @@
          if (message_type != NULL && strcmp(message_type,IOC_REQUEST_VALUE_VERB) == 0) {
              if (message_name != NULL) {
                  // destined for our lights?
-                 int index = endpoint->indexOfLight((char *)message_name);
+                 int index = endpoint->indexOfPersonality((char *)message_name);
                  if (index >= 0) {
                      if (message_verb != NULL) {
                          // map the parameter to one of ours
--- a/MQTTTransport.h	Thu Mar 27 18:14:05 2014 +0000
+++ b/MQTTTransport.h	Fri Mar 28 16:24:22 2014 +0000
@@ -33,7 +33,7 @@
         PubSubClient         *m_mqtt;
         MBEDToIOCResourceMap *m_map;
         char                  m_topic[MQTT_IOC_TOPIC_LEN+1];
-        char                  m_endpoint_name[LIGHT_NAME_LEN+1];
+        char                  m_endpoint_name[PERSONALITY_NAME_LEN+1];
         int                   m_ping_counter;
         int                   m_ping_countdown;