123
Dependencies: MQTTSN
Client-AE.cpp@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 | #define MQTTSNCLIENT_QOS2 1 |
m_ahsan | 15:8c473836feba | 2 | |
m_ahsan | 15:8c473836feba | 3 | #include "mbed.h" |
m_ahsan | 15:8c473836feba | 4 | #include "stdio.h" |
m_ahsan | 15:8c473836feba | 5 | #include "MQTT_server_setting.h" |
m_ahsan | 15:8c473836feba | 6 | #include "MQTTSNUDP.h" |
m_ahsan | 15:8c473836feba | 7 | #include "MQTTSNClient.h" |
m_ahsan | 15:8c473836feba | 8 | #include "sensor.h" |
m_ahsan | 15:8c473836feba | 9 | #include "Enumeration.h" |
m_ahsan | 15:8c473836feba | 10 | #include "easy-connect.h" |
m_ahsan | 15:8c473836feba | 11 | #include "MQTTSN_func.h" |
m_ahsan | 15:8c473836feba | 12 | #include "registration.h" |
m_ahsan | 15:8c473836feba | 13 | #include "container.h" |
m_ahsan | 15:8c473836feba | 14 | #include "contentInstance.h" |
m_ahsan | 15:8c473836feba | 15 | #include "Subscription.h" |
m_ahsan | 15:8c473836feba | 16 | |
m_ahsan | 15:8c473836feba | 17 | NetworkInterface* network = NULL; |
m_ahsan | 15:8c473836feba | 18 | |
m_ahsan | 15:8c473836feba | 19 | int main() { |
m_ahsan | 15:8c473836feba | 20 | |
m_ahsan | 15:8c473836feba | 21 | float version = 1.0; |
m_ahsan | 15:8c473836feba | 22 | MQTTSNUDP* ipstack = NULL; |
m_ahsan | 15:8c473836feba | 23 | MQTTSN::Client<MQTTSNUDP, Countdown> *client = NULL; |
m_ahsan | 15:8c473836feba | 24 | |
m_ahsan | 15:8c473836feba | 25 | ///////////////////////////////////////////////////////////////////////////////////////// |
m_ahsan | 15:8c473836feba | 26 | printf("Opening network interface...\r\n"); |
m_ahsan | 15:8c473836feba | 27 | { |
m_ahsan | 15:8c473836feba | 28 | network = easy_connect(true); // If true, prints out connection details. |
m_ahsan | 15:8c473836feba | 29 | if (!network) { |
m_ahsan | 15:8c473836feba | 30 | printf("Unable to open network interface.\r\n"); |
m_ahsan | 15:8c473836feba | 31 | return -1; |
m_ahsan | 15:8c473836feba | 32 | } |
m_ahsan | 15:8c473836feba | 33 | } |
m_ahsan | 15:8c473836feba | 34 | printf("Network interface opened successfully.\r\n"); |
m_ahsan | 15:8c473836feba | 35 | printf("\r\n"); |
m_ahsan | 15:8c473836feba | 36 | |
m_ahsan | 15:8c473836feba | 37 | const char* Local_IP = network->get_ip_address(); |
m_ahsan | 15:8c473836feba | 38 | printf("IP Address of Device is: %s\n", Local_IP); |
m_ahsan | 15:8c473836feba | 39 | |
m_ahsan | 15:8c473836feba | 40 | const char* Local_MAC = network->get_mac_address(); |
m_ahsan | 15:8c473836feba | 41 | printf("MAC Address of Device is: %s\n", Local_MAC); |
m_ahsan | 15:8c473836feba | 42 | ///////////////////////////////////////////////////////////////////////////////////////// |
m_ahsan | 15:8c473836feba | 43 | ipstack = new MQTTSNUDP(network); |
m_ahsan | 15:8c473836feba | 44 | client = new MQTTSN::Client<MQTTSNUDP, Countdown>(*ipstack); |
m_ahsan | 15:8c473836feba | 45 | |
m_ahsan | 15:8c473836feba | 46 | //////////////////////////////////UDP Connect/////////////////////////////////////////// |
m_ahsan | 15:8c473836feba | 47 | printf("Connecting to %s:%d\n", MQTT_SERVER_HOST_NAME, PORT); |
m_ahsan | 15:8c473836feba | 48 | rc = ipstack->connect(MQTT_SERVER_HOST_NAME, PORT); |
m_ahsan | 15:8c473836feba | 49 | if (rc != 0) |
m_ahsan | 15:8c473836feba | 50 | printf("rc from UDP connect is %d\n", rc); |
m_ahsan | 15:8c473836feba | 51 | else |
m_ahsan | 15:8c473836feba | 52 | printf("UDP connected\n"); |
m_ahsan | 15:8c473836feba | 53 | |
m_ahsan | 15:8c473836feba | 54 | //////////////////////////////////MQTT Connect/////////////////////////////////////////// |
m_ahsan | 15:8c473836feba | 55 | attemptConnect(client, ipstack); |
m_ahsan | 15:8c473836feba | 56 | |
m_ahsan | 15:8c473836feba | 57 | //////////////////////////////////MQTT Subscribe////////////////////////////////////////// |
m_ahsan | 15:8c473836feba | 58 | |
m_ahsan | 15:8c473836feba | 59 | //Set Topic to /oneM2M/reg_resp/Sensor01/CSE_01 |
m_ahsan | 15:8c473836feba | 60 | create_Topic("reg_resp", MQTT_CLIENT_ID, "CSE_01"); |
m_ahsan | 15:8c473836feba | 61 | |
m_ahsan | 15:8c473836feba | 62 | MQTTSN_topicid topicid; |
m_ahsan | 15:8c473836feba | 63 | if ((rc = subscribe(client, ipstack, topicid)) != 0) |
m_ahsan | 15:8c473836feba | 64 | printf("rc from MQTT subscribe is %d\n", rc); |
m_ahsan | 15:8c473836feba | 65 | else |
m_ahsan | 15:8c473836feba | 66 | printf("Subscribed to Topic %s\n", MQTT_TOPIC); |
m_ahsan | 15:8c473836feba | 67 | |
m_ahsan | 15:8c473836feba | 68 | //Set Topic to /oneM2M/reg_req/Sensor01/CSE_01 |
m_ahsan | 15:8c473836feba | 69 | create_Topic("reg_req", MQTT_CLIENT_ID, "CSE_01"); |
m_ahsan | 15:8c473836feba | 70 | |
m_ahsan | 15:8c473836feba | 71 | MQTTSN_topicid topicid2; |
m_ahsan | 15:8c473836feba | 72 | if ((rc = subscribe(client, ipstack, topicid2)) != 0) |
m_ahsan | 15:8c473836feba | 73 | printf("rc from MQTT subscribe is %d\n", rc); |
m_ahsan | 15:8c473836feba | 74 | else |
m_ahsan | 15:8c473836feba | 75 | printf("Subscribed to Topic %s\n", MQTT_TOPIC); |
m_ahsan | 15:8c473836feba | 76 | |
m_ahsan | 15:8c473836feba | 77 | //////////////////////////////////Registration/////////////////////////////////////////// |
m_ahsan | 15:8c473836feba | 78 | if(Registration(client, ipstack, topicid2) != 0 ) |
m_ahsan | 15:8c473836feba | 79 | printf("Registration failed with response: %s\n", response.c_str()); |
m_ahsan | 15:8c473836feba | 80 | else printf("Registration Done wth response: %s\n", response.c_str()); |
m_ahsan | 15:8c473836feba | 81 | |
m_ahsan | 15:8c473836feba | 82 | ////////////////////////////////Req-Resp Topics////////////////////////////////////////// |
m_ahsan | 15:8c473836feba | 83 | c_aei = new char [aei.length()+1];; |
m_ahsan | 15:8c473836feba | 84 | printf("\nAEI in main: %s\r\n\n\n", aei.c_str()); |
m_ahsan | 15:8c473836feba | 85 | strcpy(c_aei, aei.c_str()); |
m_ahsan | 15:8c473836feba | 86 | printf("C_AEI: %s\r\n", c_aei); |
m_ahsan | 15:8c473836feba | 87 | |
m_ahsan | 15:8c473836feba | 88 | //Set Topic to /oneM2M/resp/SAE01/CSE_01 |
m_ahsan | 15:8c473836feba | 89 | create_Topic("resp", c_aei, "CSE_01"); |
m_ahsan | 15:8c473836feba | 90 | |
m_ahsan | 15:8c473836feba | 91 | MQTTSN_topicid topicid3; |
m_ahsan | 15:8c473836feba | 92 | if ((rc = subscribe(client, ipstack, topicid3)) != 0) |
m_ahsan | 15:8c473836feba | 93 | printf("rc from MQTT subscribe is %d\n", rc); |
m_ahsan | 15:8c473836feba | 94 | else |
m_ahsan | 15:8c473836feba | 95 | printf("Subscribed to Topic %s\n", MQTT_TOPIC); |
m_ahsan | 15:8c473836feba | 96 | |
m_ahsan | 15:8c473836feba | 97 | //Set Topic to /oneM2M/req/SAE01/CSE_01 |
m_ahsan | 15:8c473836feba | 98 | create_Topic("req", c_aei, "CSE_01"); |
m_ahsan | 15:8c473836feba | 99 | |
m_ahsan | 15:8c473836feba | 100 | MQTTSN_topicid topicid4; |
m_ahsan | 15:8c473836feba | 101 | if ((rc = subscribe(client, ipstack, topicid4)) != 0) |
m_ahsan | 15:8c473836feba | 102 | printf("rc from MQTT subscribe is %d\n", rc); |
m_ahsan | 15:8c473836feba | 103 | else |
m_ahsan | 15:8c473836feba | 104 | printf("Subscribed to Topic %s\n", MQTT_TOPIC); |
m_ahsan | 15:8c473836feba | 105 | wait(3); |
m_ahsan | 15:8c473836feba | 106 | |
m_ahsan | 15:8c473836feba | 107 | ////////////////////////////////// Container //////////////////////////////////////////// |
m_ahsan | 15:8c473836feba | 108 | if(Container(client, ipstack, topicid4) != 0 ) |
m_ahsan | 15:8c473836feba | 109 | printf("Container Creation failed with response: %s\n", response.c_str()); |
m_ahsan | 15:8c473836feba | 110 | else printf("Container Created wth response: %s\n", response.c_str()); |
m_ahsan | 15:8c473836feba | 111 | wait(3); |
m_ahsan | 15:8c473836feba | 112 | ////////////////////////////////// Subscription //////////////////////////////////////////// |
m_ahsan | 15:8c473836feba | 113 | if(Subscription(client, ipstack, topicid4) != 0 ) |
m_ahsan | 15:8c473836feba | 114 | printf("Subscription Creation failed with response: %s\n", response.c_str()); |
m_ahsan | 15:8c473836feba | 115 | else printf("Subscription Created wth response: %s\n", response.c_str()); |
m_ahsan | 15:8c473836feba | 116 | wait(3); |
m_ahsan | 15:8c473836feba | 117 | ////////////////////////////////// ContentInstance ////////////////////////////////////// |
m_ahsan | 15:8c473836feba | 118 | if(contentInstance(client, ipstack, topicid4) != 0 ) |
m_ahsan | 15:8c473836feba | 119 | printf("ContentInstance Creation failed with response: %s\n", response.c_str()); |
m_ahsan | 15:8c473836feba | 120 | else printf("ContentInstance Created wth response: %s\n", response.c_str()); |
m_ahsan | 15:8c473836feba | 121 | |
m_ahsan | 15:8c473836feba | 122 | rc = delete_cnt(client, ipstack, topicid4); |
m_ahsan | 15:8c473836feba | 123 | rc = delete_sub(client, ipstack, topicid4); |
m_ahsan | 15:8c473836feba | 124 | |
m_ahsan | 15:8c473836feba | 125 | |
m_ahsan | 15:8c473836feba | 126 | while(1) |
m_ahsan | 15:8c473836feba | 127 | { |
m_ahsan | 15:8c473836feba | 128 | // Check connection // |
m_ahsan | 15:8c473836feba | 129 | if(!client->isConnected()){ |
m_ahsan | 15:8c473836feba | 130 | break; |
m_ahsan | 15:8c473836feba | 131 | } |
m_ahsan | 15:8c473836feba | 132 | // Received a control message. // |
m_ahsan | 15:8c473836feba | 133 | if(isMessageArrived) { |
m_ahsan | 15:8c473836feba | 134 | isMessageArrived = false; |
m_ahsan | 15:8c473836feba | 135 | // Just print it out here. |
m_ahsan | 15:8c473836feba | 136 | printf("\r\nMessage arrived:\r\n%s\r\n", messageBuffer); |
m_ahsan | 15:8c473836feba | 137 | |
m_ahsan | 15:8c473836feba | 138 | //generate notification response |
m_ahsan | 15:8c473836feba | 139 | if(op == 5) |
m_ahsan | 15:8c473836feba | 140 | { |
m_ahsan | 15:8c473836feba | 141 | Response Resp; |
m_ahsan | 15:8c473836feba | 142 | RES.resourceName = rn; |
m_ahsan | 15:8c473836feba | 143 | Resp.Request_Identifier = rqi; |
m_ahsan | 15:8c473836feba | 144 | Resp.To = From; |
m_ahsan | 15:8c473836feba | 145 | lcl = false; |
m_ahsan | 15:8c473836feba | 146 | Resp.From = CSE_ID; |
m_ahsan | 15:8c473836feba | 147 | buf = Notify_Resp(Resp); |
m_ahsan | 15:8c473836feba | 148 | if ((rc = publish(client, ipstack, topicid3)) != 0) |
m_ahsan | 15:8c473836feba | 149 | printf("rc from MQTT Publish is %d\n", rc); |
m_ahsan | 15:8c473836feba | 150 | else |
m_ahsan | 15:8c473836feba | 151 | printf("Published Buffer: %s to Topic %s\n",buf, MQTT_TOPIC); |
m_ahsan | 15:8c473836feba | 152 | } |
m_ahsan | 15:8c473836feba | 153 | |
m_ahsan | 15:8c473836feba | 154 | } |
m_ahsan | 15:8c473836feba | 155 | client->yield(4000); |
m_ahsan | 15:8c473836feba | 156 | //wait(4); |
m_ahsan | 15:8c473836feba | 157 | } |
m_ahsan | 15:8c473836feba | 158 | |
m_ahsan | 15:8c473836feba | 159 | //if ((rc = client->unsubscribe(topicid2)) != 0) |
m_ahsan | 15:8c473836feba | 160 | // printf("rc from unsubscribe was %d\n", rc); |
m_ahsan | 15:8c473836feba | 161 | |
m_ahsan | 15:8c473836feba | 162 | if ((rc = client->disconnect()) != 0) |
m_ahsan | 15:8c473836feba | 163 | printf("rc from disconnect was %d\n", rc); |
m_ahsan | 15:8c473836feba | 164 | |
m_ahsan | 15:8c473836feba | 165 | ipstack->disconnect(); |
m_ahsan | 15:8c473836feba | 166 | |
m_ahsan | 15:8c473836feba | 167 | delete ipstack; |
m_ahsan | 15:8c473836feba | 168 | delete client; |
m_ahsan | 15:8c473836feba | 169 | |
m_ahsan | 15:8c473836feba | 170 | printf("Version %.2f: finish %d msgs\n", version, arrivedcount); |
m_ahsan | 15:8c473836feba | 171 | printf("Finishing with %d messages received\n", arrivedcount); |
m_ahsan | 15:8c473836feba | 172 | |
m_ahsan | 15:8c473836feba | 173 | return 0; |
m_ahsan | 15:8c473836feba | 174 | |
m_ahsan | 15:8c473836feba | 175 | } |