mqtt specific components for the impact mbed endpoint library

Dependents:   mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_mqtt_endpoint_nxp

Revision:
42:297585f8e7bd
Parent:
32:9a024a6af2fb
Child:
43:14ccc830d6a6
--- a/MQTTTransport.cpp	Tue Jul 01 04:19:24 2014 +0000
+++ b/MQTTTransport.cpp	Tue Jul 01 17:12:25 2014 +0000
@@ -17,7 +17,11 @@
  */
  
  #include "mbed.h"
+ 
+ #ifdef NETWORK_MUTEX
  #include "rtos.h"
+ extern Mutex *network_mutex;
+ #endif
  
  #include "MQTTTransport.h"
  
@@ -26,9 +30,6 @@
  
  // EmulatedResourceFactory support
  #include "EmulatedResourceFactory.h"
- 
- // Network mutex
- extern Mutex *network_mutex;
   
  // our transmitt instance 
  MQTTTransport *_mqtt_instance =  NULL;
@@ -279,9 +280,13 @@
          memset(message,0,MAX_MQTT_MESSAGE_LENGTH+1);
          char *str_success = IOC_RESPONSE_OK; if (!success) str_success = IOC_RESPONSE_FAILED;
          sprintf(message,IOC_RESPONSE_TEMPLATE,IOC_RESPONSE_VERB,endpoint_name,resource_name,value,str_success);
+ #ifdef NETWORK_MUTEX
          if (network_mutex != NULL) network_mutex->lock();
+ #endif
          bool sent = this->m_mqtt->publish(this->getTopic(),message,strlen(message));
+ #ifdef NETWORK_MUTEX
          if (network_mutex != NULL) network_mutex->unlock();
+ #endif
          if (sent) {
              this->logger()->log("Result sent successfully");
              this->logger()->blinkTransportTxLED();
@@ -364,9 +369,13 @@
      
      // send the message over the ping/pong topic
      //this->logger()->log("Sending PING: counter=%d",this->m_ping_counter);
+ #ifdef NETWORK_MUTEX
      if (network_mutex != NULL) network_mutex->lock();
+ #endif
      sent = this->m_mqtt->publish(MQTT_PING_SEND_TOPIC,message,strlen(message));
+ #ifdef NETWORK_MUTEX
      if (network_mutex != NULL) network_mutex->unlock();
+ #endif
      if (sent) {
          // send succeeded
          //this->logger()->log("PING %d sent successfully",this->m_ping_counter);
@@ -399,7 +408,6 @@
  // connect up MQTT
  bool MQTTTransport::connect() {
      memset(_mqtt_id,0,(MQTT_ENDPOINT_IDLEN+1));
-     //if (network_mutex != NULL) network_mutex->lock();
      if (this->m_connected == false) {
          this->logger()->log("MQTT Init: %s:%d...",MQTT_HOSTNAME,MQTT_HOSTPORT);
          this->m_mqtt = &_mqtt;
@@ -440,7 +448,6 @@
      else {
          this->logger()->log("MQTT already connected (OK)");
      }
-     //if (network_mutex != NULL) network_mutex->unlock();
      return this->m_connected;
  }
  
@@ -448,9 +455,13 @@
  bool MQTTTransport::disconnect() {
      if (this->m_mqtt != NULL) {
          this->logger()->log("MQTT Unsubscribing from: %s...",this->getTopic());
+ #ifdef NETWORK_MUTEX
          if (network_mutex != NULL) network_mutex->lock();
+ #endif
          this->m_mqtt->unsubscribe(this->getTopic());
+ #ifdef NETWORK_MUTEX
          if (network_mutex != NULL) network_mutex->unlock();
+ #endif
          this->logger()->log("MQTT Disconnecting...");
          this->m_mqtt->disconnect();
      }
@@ -465,9 +476,13 @@
  void MQTTTransport::checkAndProcess() {
      // process any MQTT messages
      if (this->m_mqtt != NULL && this->m_connected == true) {
+ #ifdef NETWORK_MUTEX
          if (network_mutex != NULL) network_mutex->lock();
+ #endif
          bool connected = this->m_mqtt->loop();
+ #ifdef NETWORK_MUTEX
          if (network_mutex != NULL) network_mutex->unlock();
+ #endif
          if (connected) {
             this->logger()->blinkTransportRxLED();
          }