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