Add authorization check and messages

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample-MACfix-DebugFix by James Sutton

Files at this revision

API Documentation at this revision

Comitter:
icraggs
Date:
Mon Aug 03 12:41:51 2015 +0000
Parent:
15:09458079f4bb
Commit message:
Add authorization check and diagnostic messages

Changed in this revision

MQTT.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/MQTT.lib	Mon Jul 27 09:03:30 2015 +0000
+++ b/MQTT.lib	Mon Aug 03 12:41:51 2015 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/teams/mqtt/code/MQTT/#c299463ae853
+http://mbed.org/teams/mqtt/code/MQTT/#37f007d2a8ae
--- a/main.cpp	Mon Jul 27 09:03:30 2015 +0000
+++ b/main.cpp	Mon Aug 03 12:41:51 2015 +0000
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2014 IBM Corp.
+ * Copyright (c) 2014, 2015 IBM Corp.
  *
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
@@ -15,6 +15,8 @@
  *    Ian Craggs - refactoring to remove STL and other changes
  *    Sam Grove  - added check for Ethernet cable.
  *    Chris Styles - Added additional menu screen for software revision
+ *    James Sutton - Mac fix and extra debug
+ *    Ian Craggs - add not authorized messages
  *
  * To do :
  *    Add magnetometer sensor output to IoT data stream
@@ -30,7 +32,7 @@
 #include "rtos.h"
 
 // Update this to the next number *before* a commit
-#define __APP_SW_REVISION__ "11"
+#define __APP_SW_REVISION__ "12"
 
 // Configuration values needed to connect to IBM IoT Cloud
 #define ORG "quickstart"             // For a registered connection, replace with your org
@@ -67,6 +69,7 @@
 bool netConnected = false;
 bool netConnecting = false;
 bool ethernetInitialising = true;
+int connack_rc = 0; // MQTT connack return code
 int retryAttempt = 0;
 int menuItem = 0;
 
@@ -125,7 +128,7 @@
 void flashing_yellow(void const *args)
 {
     bool on = false;
-    while (!connected)    // flashing yellow only while connecting 
+    while (!connected && connack_rc != MQTT_NOT_AUTHORIZED && connack_rc != MQTT_BAD_USERNAME_OR_PASSWORD)    // flashing yellow only while connecting 
     {
         on = !on; 
         if (on)
@@ -152,8 +155,6 @@
 }
 
 
-
-
 void printMenu(int menuItem) 
 {
     lcd.cls();
@@ -178,10 +179,29 @@
         case 3:
             lcd.printf("MQTT Status:");
             lcd.locate(0,16);
-            if(mqttConnecting){
+            if (mqttConnecting)
                 lcd.printf("Connecting... %d/5", retryAttempt);
-            } else {
-                lcd.printf(connected ? "Connected" : "Disconnected");
+            else
+            {
+                if (connected)
+                    lcd.printf("Connected");
+                else
+                {
+                    switch (connack_rc)
+                    {
+                        case MQTT_CLIENTID_REJECTED:
+                            lcd.printf("Clientid rejected");
+                            break;
+                        case MQTT_BAD_USERNAME_OR_PASSWORD:
+                            lcd.printf("Invalid username or password");
+                            break;
+                        case MQTT_NOT_AUTHORIZED:
+                            lcd.printf("Not authorized");
+                            break;
+                        default:
+                            lcd.printf("Disconnected");
+                    }
+                }
             }
             break;
         case 4:
@@ -192,11 +212,10 @@
         case 5:
             lcd.printf("Socket State:");
             lcd.locate(0,16);
-            if(netConnecting){
+            if (netConnecting)
                 lcd.printf("Connecting... %d/5", retryAttempt);
-            } else {
+            else
                 lcd.printf(netConnected ? "Connected" : "Disconnected");
-            }
             break;
         case 6:
             lcd.printf("IP Address:");
@@ -206,17 +225,17 @@
         case 7:
             lcd.printf("Gateway:");
             lcd.locate(0,16);
-            lcd.printf("%s",gateway_addr);
+            lcd.printf("%s", gateway_addr);
             break;
         case 8:
             lcd.printf("App version:");
             lcd.locate(0,16);
-            lcd.printf("%s",__APP_SW_REVISION__);
+            lcd.printf("%s", __APP_SW_REVISION__);
             break;
         case 9:
             lcd.printf("Current Timeout:");
             lcd.locate(0,16);
-            lcd.printf("%d ms",connectTimeout);
+            lcd.printf("%d ms", connectTimeout);
             break;
     }
 }
@@ -250,15 +269,13 @@
 void menu_loop(void const *args)
 {
     int count = 0;
-    while(true){
+    while(true)
+    {
         setMenu();
-        if(++count % 10 == 0)
-        {
+        if (++count % 10 == 0)
             printMenu(menuItem);
-        }
         Thread::wait(100);
     }
-    
 }
 
 
@@ -289,7 +306,7 @@
     char clientId[strlen(org) + strlen(type) + strlen(id) + 5];
     sprintf(clientId, "d:%s:%s:%s", org, type, id);
     
-    /* Network debug statements */
+    // Network debug statements 
     LOG("=====================================\n");
     LOG("Connecting Ethernet.\n");
     LOG("IP ADDRESS: %s\n", eth.getIPAddress());
@@ -302,13 +319,13 @@
     
     netConnecting = true;
     int rc = ipstack->connect(hostname, IBM_IOT_PORT, connectTimeout);
-    if (rc != 0){
+    if (rc != 0)
+    {
         WARN("IP Stack returned: %d\n", rc);    
         return rc;
     }
     netConnected = true;
     netConnecting = false;
-    
 
     // MQTT Connect
     mqttConnecting = true;
@@ -329,8 +346,11 @@
         displayMessage("Connected");
         wait(1);
         displayMessage("Scroll with joystick");
-
     }
+    else
+        WARN("MQTT connect returned %d\n", rc);
+    if (rc >= 0)
+        connack_rc = rc;
     mqttConnecting = false;
     return rc;
 }
@@ -345,17 +365,20 @@
 
 void attemptConnect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack)
 {
-    
     connected = false;
-    
+   
     // make sure a cable is connected before starting to connect
-    while (!linkStatus()) {
+    while (!linkStatus()) 
+    {
         wait(1.0f);
         WARN("Ethernet link not present. Check cable connection\n");
     }
         
-    while (connect(client, ipstack) != 0) 
+    while (connect(client, ipstack) != MQTT_CONNECTION_ACCEPTED) 
     {    
+        if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD)
+            return; // don't reattempt to connect if credentials are wrong
+            
         Thread red_thread(flashing_red);
 
         int timeout = getConnTimeout(++retryAttempt);
@@ -393,7 +416,6 @@
 }
 
 
-
 char* getMac(EthernetInterface& eth, char* buf, int buflen)    // Obtain MAC address
 {   
     strncpy(buf, eth.getMACAddress(), buflen);
@@ -405,7 +427,6 @@
 }
 
 
-
 void messageArrived(MQTT::MessageData& md)
 {
     MQTT::Message &message = md.message;
@@ -456,14 +477,20 @@
     MQTTEthernet ipstack;
     ethernetInitialising = false;
     MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack);
-    LOG("Ethernet Initialised.\n");
-    
+    LOG("Ethernet Initialised.\n"); 
     
     if (quickstartMode)
+        getMac(ipstack.getEth(), id, sizeof(id));
+        
+    attemptConnect(&client, &ipstack);
+    
+    if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD)    
     {
-        getMac(ipstack.getEth(), id, sizeof(id));
+        red();
+        while (true)
+            wait(1.0); // Permanent failures - don't retry
     }
-    attemptConnect(&client, &ipstack);
+    
     if (!quickstartMode) 
     {
         int rc = 0;