v1

Dependencies:   MQTTSN mbed-http

Committer:
m_ahsan
Date:
Mon Mar 11 07:51:06 2019 +0000
Revision:
15:557d0008dd2d
123

Who changed what in which revision?

UserRevisionLine numberNew contents of line
m_ahsan 15:557d0008dd2d 1 int connack_rc = 0;
m_ahsan 15:557d0008dd2d 2 int retryAttempt = 0;
m_ahsan 15:557d0008dd2d 3 int connectTimeout = 1000;
m_ahsan 15:557d0008dd2d 4 int rc = 0;
m_ahsan 15:557d0008dd2d 5
m_ahsan 15:557d0008dd2d 6 enum connack_return_codes
m_ahsan 15:557d0008dd2d 7 {
m_ahsan 15:557d0008dd2d 8 MQTTSN_CONNECTION_ACCEPTED = 0,
m_ahsan 15:557d0008dd2d 9 MQTTSN_REJECTED_CONGESTION = 1,
m_ahsan 15:557d0008dd2d 10 MQTTSN_REJECTED_INVALID_TOPIC_ID = 2,
m_ahsan 15:557d0008dd2d 11 MQTTSN_REJECTED_NOT_SUPPORTED= 3,
m_ahsan 15:557d0008dd2d 12 };
m_ahsan 15:557d0008dd2d 13
m_ahsan 15:557d0008dd2d 14 static volatile bool isMessageArrived = false;
m_ahsan 15:557d0008dd2d 15 int arrivedcount = 0;
m_ahsan 15:557d0008dd2d 16
m_ahsan 15:557d0008dd2d 17 int connect(MQTTSN::Client<MQTTSNUDP, Countdown> *client, MQTTSNUDP* ipstack)
m_ahsan 15:557d0008dd2d 18 {
m_ahsan 15:557d0008dd2d 19 int rc;
m_ahsan 15:557d0008dd2d 20 MQTTSNPacket_connectData data = MQTTSNPacket_connectData_initializer;
m_ahsan 15:557d0008dd2d 21 data.clientID.cstring = MQTT_CLIENT_ID;
m_ahsan 15:557d0008dd2d 22 data.duration = 60;
m_ahsan 15:557d0008dd2d 23 if ((rc = client->connect(data)) == 0) {
m_ahsan 15:557d0008dd2d 24 printf ("--->MQTT-SN Connected\n\r");
m_ahsan 15:557d0008dd2d 25 }
m_ahsan 15:557d0008dd2d 26 else {
m_ahsan 15:557d0008dd2d 27 printf("MQTT-SN connect returned %d\n", rc);
m_ahsan 15:557d0008dd2d 28 }
m_ahsan 15:557d0008dd2d 29 if (rc >= 0)
m_ahsan 15:557d0008dd2d 30 connack_rc = rc;
m_ahsan 15:557d0008dd2d 31 return rc;
m_ahsan 15:557d0008dd2d 32 }
m_ahsan 15:557d0008dd2d 33
m_ahsan 15:557d0008dd2d 34 int getConnTimeout(int attemptNumber)
m_ahsan 15:557d0008dd2d 35 { // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute
m_ahsan 15:557d0008dd2d 36 // after 20 attempts, retry every 10 minutes
m_ahsan 15:557d0008dd2d 37 return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600;
m_ahsan 15:557d0008dd2d 38 }
m_ahsan 15:557d0008dd2d 39
m_ahsan 15:557d0008dd2d 40 void attemptConnect(MQTTSN::Client<MQTTSNUDP, Countdown> *client, MQTTSNUDP* ipstack)
m_ahsan 15:557d0008dd2d 41 {
m_ahsan 15:557d0008dd2d 42 while (connect(client, ipstack) != MQTTSN_CONNECTION_ACCEPTED)
m_ahsan 15:557d0008dd2d 43 {
m_ahsan 15:557d0008dd2d 44 int timeout = getConnTimeout(++retryAttempt);
m_ahsan 15:557d0008dd2d 45 printf("Retry attempt number %d waiting %d\n", retryAttempt, timeout);
m_ahsan 15:557d0008dd2d 46
m_ahsan 15:557d0008dd2d 47 // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed
m_ahsan 15:557d0008dd2d 48 // or maybe just add the proper members to do this disconnect and call attemptConnect(...)
m_ahsan 15:557d0008dd2d 49 // this works - reset the system when the retry count gets to a threshold
m_ahsan 15:557d0008dd2d 50 if (retryAttempt == 5)
m_ahsan 15:557d0008dd2d 51 NVIC_SystemReset();
m_ahsan 15:557d0008dd2d 52 else
m_ahsan 15:557d0008dd2d 53 wait(timeout);
m_ahsan 15:557d0008dd2d 54 }
m_ahsan 15:557d0008dd2d 55 }
m_ahsan 15:557d0008dd2d 56
m_ahsan 15:557d0008dd2d 57 void messageArrived(MQTTSN::MessageData& md)
m_ahsan 15:557d0008dd2d 58 {
m_ahsan 15:557d0008dd2d 59 MQTTSN::Message &message = md.message;
m_ahsan 15:557d0008dd2d 60 printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n, Number: %d", message.qos, message.retained, message.dup, message.id, arrivedcount);
m_ahsan 15:557d0008dd2d 61 memcpy(messageBuffer, (char*)message.payload, message.payloadlen);
m_ahsan 15:557d0008dd2d 62 messageBuffer[message.payloadlen] = '\0';
m_ahsan 15:557d0008dd2d 63 printf("Payload %.*s\n", message.payloadlen, (char*)message.payload);
m_ahsan 15:557d0008dd2d 64 printf("Payload length %d\n",message.payloadlen);
m_ahsan 15:557d0008dd2d 65 ++arrivedcount;
m_ahsan 15:557d0008dd2d 66 process_msg();
m_ahsan 15:557d0008dd2d 67 isMessageArrived = true;
m_ahsan 15:557d0008dd2d 68 }
m_ahsan 15:557d0008dd2d 69
m_ahsan 15:557d0008dd2d 70 int subscribe(MQTTSN::Client<MQTTSNUDP, Countdown> *client, MQTTSNUDP* ipstack, MQTTSN_topicid& topicid)
m_ahsan 15:557d0008dd2d 71 {
m_ahsan 15:557d0008dd2d 72 //MQTTSN_topicid topicid;
m_ahsan 15:557d0008dd2d 73 topicid.type = MQTTSN_TOPIC_TYPE_NORMAL;
m_ahsan 15:557d0008dd2d 74 topicid.data.long_.name = MQTT_TOPIC;
m_ahsan 15:557d0008dd2d 75 topicid.data.long_.len = strlen(MQTT_TOPIC);
m_ahsan 15:557d0008dd2d 76 MQTTSN::QoS grantedQoS;
m_ahsan 15:557d0008dd2d 77 return client->subscribe(topicid, MQTTSN::QOS1, grantedQoS, messageArrived);
m_ahsan 15:557d0008dd2d 78 }
m_ahsan 15:557d0008dd2d 79
m_ahsan 15:557d0008dd2d 80 int publish(MQTTSN::Client<MQTTSNUDP, Countdown> *client, MQTTSNUDP* ipstack, MQTTSN_topicid& topicid2)
m_ahsan 15:557d0008dd2d 81 {
m_ahsan 15:557d0008dd2d 82 int rc;
m_ahsan 15:557d0008dd2d 83 if(PUB_REG == false)
m_ahsan 15:557d0008dd2d 84 {
m_ahsan 15:557d0008dd2d 85 if ((rc = subscribe(client, ipstack, topicid2)) != 0)
m_ahsan 15:557d0008dd2d 86 printf("rc from MQTT subscribe is %d\n", rc);
m_ahsan 15:557d0008dd2d 87 else{
m_ahsan 15:557d0008dd2d 88 printf("Subscribed to Topic %s\n", MQTT_TOPIC);
m_ahsan 15:557d0008dd2d 89 PUB_REG = true;
m_ahsan 15:557d0008dd2d 90 }
m_ahsan 15:557d0008dd2d 91 }
m_ahsan 15:557d0008dd2d 92
m_ahsan 15:557d0008dd2d 93 MQTTSN::Message message;
m_ahsan 15:557d0008dd2d 94 message.qos = MQTTSN::QOS1;
m_ahsan 15:557d0008dd2d 95 message.retained = false;
m_ahsan 15:557d0008dd2d 96 message.dup = false;
m_ahsan 15:557d0008dd2d 97 message.payload = (void*)buf;
m_ahsan 15:557d0008dd2d 98 message.payloadlen = strlen(buf)+1;
m_ahsan 15:557d0008dd2d 99 return client->publish(topicid2, message);
m_ahsan 15:557d0008dd2d 100 }