yh Tang
/
NuMaker-mbed-AWS-IoT-example
NuMaker connection with AWS IoT thru MQTT/HTTPS
main.cpp@12:97762a6f55e9, 2018-10-12 (annotated)
- Committer:
- ccli8
- Date:
- Fri Oct 12 15:30:28 2018 +0800
- Revision:
- 12:97762a6f55e9
- Parent:
- 8:d5ea623ffc1f
- Child:
- 17:6f0ff065cd76
Update to mbed-os 5.10.1
1. Remove easy-connect, replaced by NetworkInterface::get_default_instance since mbed-o 5.10
2. Modify related files with removal of easy-connect
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ccli8 |
1:5ffad9f24d63 | 1 | /* This example demonstrates connection with AWS IoT through MQTT/HTTPS protocol. |
ccli8 |
1:5ffad9f24d63 | 2 | * |
ccli8 |
1:5ffad9f24d63 | 3 | * AWS IoT: Thing Shadow MQTT Topics |
ccli8 |
1:5ffad9f24d63 | 4 | * http://docs.aws.amazon.com/iot/latest/developerguide/thing-shadow-mqtt.html |
ccli8 |
1:5ffad9f24d63 | 5 | * |
ccli8 |
1:5ffad9f24d63 | 6 | * AWS IoT: Publish to a topic through HTTPS/POST method: |
ccli8 |
1:5ffad9f24d63 | 7 | * http://docs.aws.amazon.com/iot/latest/developerguide/protocols.html |
ccli8 |
1:5ffad9f24d63 | 8 | * |
ccli8 |
1:5ffad9f24d63 | 9 | * AWS IoT: Thing Shadow RESTful API: |
ccli8 |
1:5ffad9f24d63 | 10 | * http://docs.aws.amazon.com/iot/latest/developerguide/thing-shadow-rest-api.html |
ccli8 |
1:5ffad9f24d63 | 11 | */ |
ccli8 |
1:5ffad9f24d63 | 12 | |
ccli8 |
1:5ffad9f24d63 | 13 | #define AWS_IOT_MQTT_TEST 1 |
ccli8 |
1:5ffad9f24d63 | 14 | #define AWS_IOT_HTTPS_TEST 0 |
ccli8 |
1:5ffad9f24d63 | 15 | |
ccli8 |
1:5ffad9f24d63 | 16 | #include "mbed.h" |
ccli8 |
1:5ffad9f24d63 | 17 | |
ccli8 |
1:5ffad9f24d63 | 18 | /* TLSSocket = Mbed TLS over TCPSocket */ |
ccli8 |
1:5ffad9f24d63 | 19 | #include "TLSSocket.h" |
ccli8 |
1:5ffad9f24d63 | 20 | |
ccli8 |
1:5ffad9f24d63 | 21 | /* Measure memory footprint */ |
ccli8 |
1:5ffad9f24d63 | 22 | #include "mbed_stats.h" |
ccli8 |
1:5ffad9f24d63 | 23 | |
ccli8 |
1:5ffad9f24d63 | 24 | #if AWS_IOT_MQTT_TEST |
ccli8 |
1:5ffad9f24d63 | 25 | /* MQTT-specific header files */ |
ccli8 |
1:5ffad9f24d63 | 26 | #include "MQTTmbed.h" |
ccli8 |
1:5ffad9f24d63 | 27 | #include "MQTTClient.h" |
ccli8 |
1:5ffad9f24d63 | 28 | #endif // End of AWS_IOT_MQTT_TEST |
ccli8 |
1:5ffad9f24d63 | 29 | |
ccli8 |
1:5ffad9f24d63 | 30 | |
ccli8 |
1:5ffad9f24d63 | 31 | namespace { |
ccli8 |
1:5ffad9f24d63 | 32 | |
ccli8 |
1:5ffad9f24d63 | 33 | /* List of trusted root CA certificates |
ccli8 |
1:5ffad9f24d63 | 34 | * currently only GlobalSign, the CA for os.mbed.com |
ccli8 |
1:5ffad9f24d63 | 35 | * |
ccli8 |
1:5ffad9f24d63 | 36 | * To add more than one root, just concatenate them. |
ccli8 |
1:5ffad9f24d63 | 37 | */ |
ccli8 |
1:5ffad9f24d63 | 38 | const char SSL_CA_CERT_PEM[] = "-----BEGIN CERTIFICATE-----\n" |
ccli8 |
1:5ffad9f24d63 | 39 | "MIIE0zCCA7ugAwIBAgIQGNrRniZ96LtKIVjNzGs7SjANBgkqhkiG9w0BAQUFADCB\n" |
ccli8 |
1:5ffad9f24d63 | 40 | "yjELMAkGA1UEBhMCVVMxFzAVBgNVBAoTDlZlcmlTaWduLCBJbmMuMR8wHQYDVQQL\n" |
ccli8 |
1:5ffad9f24d63 | 41 | "ExZWZXJpU2lnbiBUcnVzdCBOZXR3b3JrMTowOAYDVQQLEzEoYykgMjAwNiBWZXJp\n" |
ccli8 |
1:5ffad9f24d63 | 42 | "U2lnbiwgSW5jLiAtIEZvciBhdXRob3JpemVkIHVzZSBvbmx5MUUwQwYDVQQDEzxW\n" |
ccli8 |
1:5ffad9f24d63 | 43 | "ZXJpU2lnbiBDbGFzcyAzIFB1YmxpYyBQcmltYXJ5IENlcnRpZmljYXRpb24gQXV0\n" |
ccli8 |
1:5ffad9f24d63 | 44 | "aG9yaXR5IC0gRzUwHhcNMDYxMTA4MDAwMDAwWhcNMzYwNzE2MjM1OTU5WjCByjEL\n" |
ccli8 |
1:5ffad9f24d63 | 45 | "MAkGA1UEBhMCVVMxFzAVBgNVBAoTDlZlcmlTaWduLCBJbmMuMR8wHQYDVQQLExZW\n" |
ccli8 |
1:5ffad9f24d63 | 46 | "ZXJpU2lnbiBUcnVzdCBOZXR3b3JrMTowOAYDVQQLEzEoYykgMjAwNiBWZXJpU2ln\n" |
ccli8 |
1:5ffad9f24d63 | 47 | "biwgSW5jLiAtIEZvciBhdXRob3JpemVkIHVzZSBvbmx5MUUwQwYDVQQDEzxWZXJp\n" |
ccli8 |
1:5ffad9f24d63 | 48 | "U2lnbiBDbGFzcyAzIFB1YmxpYyBQcmltYXJ5IENlcnRpZmljYXRpb24gQXV0aG9y\n" |
ccli8 |
1:5ffad9f24d63 | 49 | "aXR5IC0gRzUwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCvJAgIKXo1\n" |
ccli8 |
1:5ffad9f24d63 | 50 | "nmAMqudLO07cfLw8RRy7K+D+KQL5VwijZIUVJ/XxrcgxiV0i6CqqpkKzj/i5Vbex\n" |
ccli8 |
1:5ffad9f24d63 | 51 | "t0uz/o9+B1fs70PbZmIVYc9gDaTY3vjgw2IIPVQT60nKWVSFJuUrjxuf6/WhkcIz\n" |
ccli8 |
1:5ffad9f24d63 | 52 | "SdhDY2pSS9KP6HBRTdGJaXvHcPaz3BJ023tdS1bTlr8Vd6Gw9KIl8q8ckmcY5fQG\n" |
ccli8 |
1:5ffad9f24d63 | 53 | "BO+QueQA5N06tRn/Arr0PO7gi+s3i+z016zy9vA9r911kTMZHRxAy3QkGSGT2RT+\n" |
ccli8 |
1:5ffad9f24d63 | 54 | "rCpSx4/VBEnkjWNHiDxpg8v+R70rfk/Fla4OndTRQ8Bnc+MUCH7lP59zuDMKz10/\n" |
ccli8 |
1:5ffad9f24d63 | 55 | "NIeWiu5T6CUVAgMBAAGjgbIwga8wDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8E\n" |
ccli8 |
1:5ffad9f24d63 | 56 | "BAMCAQYwbQYIKwYBBQUHAQwEYTBfoV2gWzBZMFcwVRYJaW1hZ2UvZ2lmMCEwHzAH\n" |
ccli8 |
1:5ffad9f24d63 | 57 | "BgUrDgMCGgQUj+XTGoasjY5rw8+AatRIGCx7GS4wJRYjaHR0cDovL2xvZ28udmVy\n" |
ccli8 |
1:5ffad9f24d63 | 58 | "aXNpZ24uY29tL3ZzbG9nby5naWYwHQYDVR0OBBYEFH/TZafC3ey78DAJ80M5+gKv\n" |
ccli8 |
1:5ffad9f24d63 | 59 | "MzEzMA0GCSqGSIb3DQEBBQUAA4IBAQCTJEowX2LP2BqYLz3q3JktvXf2pXkiOOzE\n" |
ccli8 |
1:5ffad9f24d63 | 60 | "p6B4Eq1iDkVwZMXnl2YtmAl+X6/WzChl8gGqCBpH3vn5fJJaCGkgDdk+bW48DW7Y\n" |
ccli8 |
1:5ffad9f24d63 | 61 | "5gaRQBi5+MHt39tBquCWIMnNZBU4gcmU7qKEKQsTb47bDN0lAtukixlE0kF6BWlK\n" |
ccli8 |
1:5ffad9f24d63 | 62 | "WE9gyn6CagsCqiUXObXbf+eEZSqVir2G3l6BFoMtEMze/aiCKm0oHw0LxOXnGiYZ\n" |
ccli8 |
1:5ffad9f24d63 | 63 | "4fQRbxC1lfznQgUy286dUV4otp6F01vvpX1FQHKOtw5rDgb7MzVIcbidJ4vEZV8N\n" |
ccli8 |
1:5ffad9f24d63 | 64 | "hnacRHr2lVz2XTIIM6RUthg/aFzyQkqFOFSDX9HoLPKsEdao7WNq\n" |
ccli8 |
1:5ffad9f24d63 | 65 | "-----END CERTIFICATE-----\n"; |
ccli8 |
1:5ffad9f24d63 | 66 | |
ccli8 |
1:5ffad9f24d63 | 67 | /* User certificate which has been activated and attached with specific thing and policy */ |
ccli8 |
1:5ffad9f24d63 | 68 | const char SSL_USER_CERT_PEM[] = "-----BEGIN CERTIFICATE-----\n" |
ccli8 |
8:d5ea623ffc1f | 69 | "MIIDWTCCAkGgAwIBAgIUAzDIpEQWV/yKVo8suGhvjmFY0n4wDQYJKoZIhvcNAQEL\n" |
ccli8 |
8:d5ea623ffc1f | 70 | "BQAwTTFLMEkGA1UECwxCQW1hem9uIFdlYiBTZXJ2aWNlcyBPPUFtYXpvbi5jb20g\n" |
ccli8 |
8:d5ea623ffc1f | 71 | "SW5jLiBMPVNlYXR0bGUgU1Q9V2FzaGluZ3RvbiBDPVVTMB4XDTE4MDQxNzA5NDMx\n" |
ccli8 |
8:d5ea623ffc1f | 72 | "M1oXDTQ5MTIzMTIzNTk1OVowHjEcMBoGA1UEAwwTQVdTIElvVCBDZXJ0aWZpY2F0\n" |
ccli8 |
8:d5ea623ffc1f | 73 | "ZTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKsLlECiw4ud5laejJmL\n" |
ccli8 |
8:d5ea623ffc1f | 74 | "bBhafKLdCRx6tkcjBYyEUoAC3Qs2ogqGngQgjU4QJoWpEBO/U1M+e1QtlZ2o/CiL\n" |
ccli8 |
8:d5ea623ffc1f | 75 | "MViHA3rYvP86N/TH8pFA3aPKaeEp+WIt5v4OXdfPkVNKTotiRuRCpzRzrY4xKp11\n" |
ccli8 |
8:d5ea623ffc1f | 76 | "ouKkVKf3FcNuKIMt/uEhje90KofBbFHQY3HFYe19qIg1m/IBV+npmNlAKElGNSB7\n" |
ccli8 |
8:d5ea623ffc1f | 77 | "xHHLzzUuue38s+ceJyzsWuPjFiVYoeyPHF8gDVWf28XJ4KUFs80Deycqe9efroud\n" |
ccli8 |
8:d5ea623ffc1f | 78 | "cQY/6aLDWDJXHvhenwoAIbHqUsYRoWoanrg5Cq3id5+pzVkadNV3+x9bGwROhpbQ\n" |
ccli8 |
8:d5ea623ffc1f | 79 | "M9ECAwEAAaNgMF4wHwYDVR0jBBgwFoAUVUXg3+Dd1qSnAT9LN413zSdNoE0wHQYD\n" |
ccli8 |
8:d5ea623ffc1f | 80 | "VR0OBBYEFIx86SOxw5k/50GtyRjUwlj+9d1gMAwGA1UdEwEB/wQCMAAwDgYDVR0P\n" |
ccli8 |
8:d5ea623ffc1f | 81 | "AQH/BAQDAgeAMA0GCSqGSIb3DQEBCwUAA4IBAQCI1fqqjvLAFzL2E1nvWMrkaWN2\n" |
ccli8 |
8:d5ea623ffc1f | 82 | "EQK44uOcw53ZzgNNH7fJ85BW8T2l1yZx/Blgs10pEp7vmccnRoR7nYbUGO8++9nG\n" |
ccli8 |
8:d5ea623ffc1f | 83 | "S7bfZhiaE2syJqqvLwPGdqR6fvDdfEpmhgJ1CqeMCqun9XZvUTsgBn7Sqqz7P99h\n" |
ccli8 |
8:d5ea623ffc1f | 84 | "gGmDRKS/CtsPai0Df0ZPNuV/YuUkpHKJSDm+ZTnzevMS3KXkG1cc/sIuc4IwF+aj\n" |
ccli8 |
8:d5ea623ffc1f | 85 | "nbyzdC2fN0r+34srQ8/9aXezOTQ0NBWtoJCCkD+LL6PYJJkAgLA2jcbcbuRJUQ7n\n" |
ccli8 |
8:d5ea623ffc1f | 86 | "Zsp25kKX40fuyIcgPRsd/7sao3zTVYxwKy8r6/mbgrPiMeHvJZ8y3nwUpsPO\n" |
ccli8 |
1:5ffad9f24d63 | 87 | "-----END CERTIFICATE-----\n"; |
ccli8 |
1:5ffad9f24d63 | 88 | |
ccli8 |
1:5ffad9f24d63 | 89 | /* User private key paired with above */ |
ccli8 |
1:5ffad9f24d63 | 90 | const char SSL_USER_PRIV_KEY_PEM[] = "-----BEGIN RSA PRIVATE KEY-----\n" |
ccli8 |
8:d5ea623ffc1f | 91 | "MIIEowIBAAKCAQEAqwuUQKLDi53mVp6MmYtsGFp8ot0JHHq2RyMFjIRSgALdCzai\n" |
ccli8 |
8:d5ea623ffc1f | 92 | "CoaeBCCNThAmhakQE79TUz57VC2Vnaj8KIsxWIcDeti8/zo39MfykUDdo8pp4Sn5\n" |
ccli8 |
8:d5ea623ffc1f | 93 | "Yi3m/g5d18+RU0pOi2JG5EKnNHOtjjEqnXWi4qRUp/cVw24ogy3+4SGN73Qqh8Fs\n" |
ccli8 |
8:d5ea623ffc1f | 94 | "UdBjccVh7X2oiDWb8gFX6emY2UAoSUY1IHvEccvPNS657fyz5x4nLOxa4+MWJVih\n" |
ccli8 |
8:d5ea623ffc1f | 95 | "7I8cXyANVZ/bxcngpQWzzQN7Jyp715+ui51xBj/posNYMlce+F6fCgAhsepSxhGh\n" |
ccli8 |
8:d5ea623ffc1f | 96 | "ahqeuDkKreJ3n6nNWRp01Xf7H1sbBE6GltAz0QIDAQABAoIBAAzl7KILJA/NMmdp\n" |
ccli8 |
8:d5ea623ffc1f | 97 | "wVR6zQXxHODzJhK9ti0bGPoFqGr6zExiLEn66MOK6NzwHteJbirvDIuEdKxeW5/t\n" |
ccli8 |
8:d5ea623ffc1f | 98 | "9EXiaTAxzjNfULE2ZK3Svhnx+ES3qNBP5/xdVcPmtXDmuCC9w7qDCLGBzTYJWxcT\n" |
ccli8 |
8:d5ea623ffc1f | 99 | "4hDJpCTPG4sm+L8p+Wga+dNkQl3CFyHHINDZ0pKcP0kDDt6inKfiU7uU4lFYbCZy\n" |
ccli8 |
8:d5ea623ffc1f | 100 | "PceUgIOTQiNVoPQYtkHgZAtmD9rcwdq2/0GZEbzTkZuSE9S8+WlGxJP5xMGzeVsv\n" |
ccli8 |
8:d5ea623ffc1f | 101 | "zZ/scx0LM7fz5Zq0lsvAwSB1mcs04DaaNpU7Z0tXDIS249RTqdtpPkJzmevpAGhF\n" |
ccli8 |
8:d5ea623ffc1f | 102 | "VNe30/kCgYEA4rflfqyw/YHWKRxCGJRO+q0gPvlBIes30noz5Hxl0knb/J5Ng4Nx\n" |
ccli8 |
8:d5ea623ffc1f | 103 | "xMaIMZgCbwHbw5i01JOPvVKICROKb8wkli4Y2eVzxMPKk2CSpji16RQZ4eOl3YXL\n" |
ccli8 |
8:d5ea623ffc1f | 104 | "1Vnn07Ei+GpsGgDNF0HWf/Ur7es/KdAPCWbKJyoSR90+WN29gP2+Zp8CgYEAwSLv\n" |
ccli8 |
8:d5ea623ffc1f | 105 | "Kt/vdd6XKnR9xR3IajsW/X2GR/x/m2JffJPOP6VpDTKAbv86hVHDV0oBEDMDc7qy\n" |
ccli8 |
8:d5ea623ffc1f | 106 | "023ognyFCPb9Gzol2lq8egjMsisA2bgoB9HqldrSYlaZ0wPe0QJBf1gZ29jPyVJ0\n" |
ccli8 |
8:d5ea623ffc1f | 107 | "ciaBbNbSRhwTrwet7Bae9EbpJsyvBxVh00v0f48CgYEAvKQKviXudmCL01UB4fW0\n" |
ccli8 |
8:d5ea623ffc1f | 108 | "6XsXs44tlY1juyuW9exTxG9ULZOCJ4U9Kl+OfsVecQL42ny7KY1GMl7zdanerDsN\n" |
ccli8 |
8:d5ea623ffc1f | 109 | "zi+42cTDWNsYORxHqSrSoYbqKjwCjJmBCppt/IQM9umF3PUBsPJFCd7zmFj/C0lk\n" |
ccli8 |
8:d5ea623ffc1f | 110 | "2Yu/dGrbHxSFheeqgCOhQz0CgYBfZxdHUYji64o2cYay+QxH1Vp86yWKp6KNKeHL\n" |
ccli8 |
8:d5ea623ffc1f | 111 | "EuP9soKa/0hMDA1nT8UzeB3gV6Kr5xxwrkj9M+8vR3otmeKa4tlZWsFqfS2VXo9/\n" |
ccli8 |
8:d5ea623ffc1f | 112 | "lWTQk1/7LZYckzvceMXL1sQnQgkaBH366SRjlBYYhcP/YMa76Uypk+GVxePrltdU\n" |
ccli8 |
8:d5ea623ffc1f | 113 | "3Z8v5wKBgEXL38yc9LqTIWe1U40ZZKvp2A8c86jtstorEEFqXharE8kxcEpL8ZLL\n" |
ccli8 |
8:d5ea623ffc1f | 114 | "wjgPKdfNMIuApHSrhG7a7gU1rgJyDy1sOIwSvgTYrWfITPTVu5owvSZEblx4KYOm\n" |
ccli8 |
8:d5ea623ffc1f | 115 | "g8hke3Oego4v9cwctkQss3/HZ6rs3PR942oAetuxLy3KPF83IeFm\n" |
ccli8 |
1:5ffad9f24d63 | 116 | "-----END RSA PRIVATE KEY-----\n"; |
ccli8 |
1:5ffad9f24d63 | 117 | |
ccli8 |
1:5ffad9f24d63 | 118 | #if AWS_IOT_MQTT_TEST |
ccli8 |
1:5ffad9f24d63 | 119 | |
ccli8 |
8:d5ea623ffc1f | 120 | #define AWS_IOT_MQTT_SERVER_NAME "a1fljoeglhtf61.iot.us-east-2.amazonaws.com" |
ccli8 |
1:5ffad9f24d63 | 121 | #define AWS_IOT_MQTT_SERVER_PORT 8883 |
ccli8 |
1:5ffad9f24d63 | 122 | |
ccli8 |
1:5ffad9f24d63 | 123 | #define AWS_IOT_MQTT_THINGNAME "Nuvoton-Mbed-D001" |
ccli8 |
1:5ffad9f24d63 | 124 | #define AWS_IOT_MQTT_CLIENTNAME "Nuvoton Client" |
ccli8 |
1:5ffad9f24d63 | 125 | |
ccli8 |
1:5ffad9f24d63 | 126 | /* User self-test topic */ |
ccli8 |
1:5ffad9f24d63 | 127 | const char USER_MQTT_TOPIC[] = "Nuvoton/Mbed/D001"; |
ccli8 |
1:5ffad9f24d63 | 128 | const char *USER_MQTT_TOPIC_FILTERS[] = { |
ccli8 |
1:5ffad9f24d63 | 129 | "Nuvoton/Mbed/+" |
ccli8 |
1:5ffad9f24d63 | 130 | }; |
ccli8 |
1:5ffad9f24d63 | 131 | const char USER_MQTT_TOPIC_PUBLISH_MESSAGE[] = "{ \"message\": \"Hello from Nuvoton Mbed device\" }"; |
ccli8 |
1:5ffad9f24d63 | 132 | |
ccli8 |
1:5ffad9f24d63 | 133 | /* Update thing shadow */ |
ccli8 |
1:5ffad9f24d63 | 134 | const char UPDATETHINGSHADOW_MQTT_TOPIC[] = "$aws/things/" AWS_IOT_MQTT_THINGNAME "/shadow/update"; |
ccli8 |
1:5ffad9f24d63 | 135 | const char *UPDATETHINGSHADOW_MQTT_TOPIC_FILTERS[] = { |
ccli8 |
1:5ffad9f24d63 | 136 | "$aws/things/" AWS_IOT_MQTT_THINGNAME "/shadow/update/accepted", |
ccli8 |
1:5ffad9f24d63 | 137 | "$aws/things/" AWS_IOT_MQTT_THINGNAME "/shadow/update/rejected" |
ccli8 |
1:5ffad9f24d63 | 138 | }; |
ccli8 |
1:5ffad9f24d63 | 139 | const char UPDATETHINGSHADOW_MQTT_TOPIC_PUBLISH_MESSAGE[] = "{ \"state\": { \"reported\": { \"attribute1\": 3, \"attribute2\": \"1\" } } }"; |
ccli8 |
1:5ffad9f24d63 | 140 | |
ccli8 |
1:5ffad9f24d63 | 141 | /* Get thing shadow */ |
ccli8 |
1:5ffad9f24d63 | 142 | const char GETTHINGSHADOW_MQTT_TOPIC[] = "$aws/things/" AWS_IOT_MQTT_THINGNAME "/shadow/get"; |
ccli8 |
1:5ffad9f24d63 | 143 | const char *GETTHINGSHADOW_MQTT_TOPIC_FILTERS[] = { |
ccli8 |
1:5ffad9f24d63 | 144 | "$aws/things/" AWS_IOT_MQTT_THINGNAME "/shadow/get/accepted", |
ccli8 |
1:5ffad9f24d63 | 145 | "$aws/things/" AWS_IOT_MQTT_THINGNAME "/shadow/get/rejected" |
ccli8 |
1:5ffad9f24d63 | 146 | }; |
ccli8 |
1:5ffad9f24d63 | 147 | const char GETTHINGSHADOW_MQTT_TOPIC_PUBLISH_MESSAGE[] = ""; |
ccli8 |
1:5ffad9f24d63 | 148 | |
ccli8 |
1:5ffad9f24d63 | 149 | /* Delete thing shadow */ |
ccli8 |
1:5ffad9f24d63 | 150 | const char DELETETHINGSHADOW_MQTT_TOPIC[] = "$aws/things/" AWS_IOT_MQTT_THINGNAME "/shadow/delete"; |
ccli8 |
1:5ffad9f24d63 | 151 | const char *DELETETHINGSHADOW_MQTT_TOPIC_FILTERS[] = { |
ccli8 |
1:5ffad9f24d63 | 152 | "$aws/things/" AWS_IOT_MQTT_THINGNAME "/shadow/delete/accepted", |
ccli8 |
1:5ffad9f24d63 | 153 | "$aws/things/" AWS_IOT_MQTT_THINGNAME "/shadow/delete/rejected" |
ccli8 |
1:5ffad9f24d63 | 154 | }; |
ccli8 |
1:5ffad9f24d63 | 155 | const char DELETETHINGSHADOW_MQTT_TOPIC_PUBLISH_MESSAGE[] = ""; |
ccli8 |
1:5ffad9f24d63 | 156 | |
ccli8 |
1:5ffad9f24d63 | 157 | /* MQTT user buffer size */ |
ccli8 |
1:5ffad9f24d63 | 158 | const int MQTT_USER_BUFFER_SIZE = 600; |
ccli8 |
1:5ffad9f24d63 | 159 | |
ccli8 |
1:5ffad9f24d63 | 160 | /* Configure MAX_MQTT_PACKET_SIZE to meet your application. |
ccli8 |
1:5ffad9f24d63 | 161 | * We may meet unknown MQTT error with MAX_MQTT_PACKET_SIZE too small, but |
ccli8 |
1:5ffad9f24d63 | 162 | * MQTT lib doesn't tell enough error message. Try to enlarge it. */ |
ccli8 |
1:5ffad9f24d63 | 163 | const int MAX_MQTT_PACKET_SIZE = 1000; |
ccli8 |
1:5ffad9f24d63 | 164 | |
ccli8 |
1:5ffad9f24d63 | 165 | #endif // End of AWS_IOT_MQTT_TEST |
ccli8 |
1:5ffad9f24d63 | 166 | |
ccli8 |
1:5ffad9f24d63 | 167 | #if AWS_IOT_HTTPS_TEST |
ccli8 |
1:5ffad9f24d63 | 168 | |
ccli8 |
8:d5ea623ffc1f | 169 | #define AWS_IOT_HTTPS_SERVER_NAME "a1fljoeglhtf61.iot.us-east-2.amazonaws.com" |
ccli8 |
1:5ffad9f24d63 | 170 | #define AWS_IOT_HTTPS_SERVER_PORT 8443 |
ccli8 |
1:5ffad9f24d63 | 171 | |
ccli8 |
1:5ffad9f24d63 | 172 | #define AWS_IOT_HTTPS_THINGNAME "Nuvoton-Mbed-D001" |
ccli8 |
1:5ffad9f24d63 | 173 | |
ccli8 |
1:5ffad9f24d63 | 174 | /* Publish to user topic through HTTPS/POST |
ccli8 |
1:5ffad9f24d63 | 175 | * HTTP POST https://"endpoint"/topics/"yourTopicHierarchy" */ |
ccli8 |
1:5ffad9f24d63 | 176 | const char USER_TOPIC_HTTPS_PATH[] = "/topics/Nuvoton/Mbed/D001?qos=1"; |
ccli8 |
1:5ffad9f24d63 | 177 | const char USER_TOPIC_HTTPS_REQUEST_METHOD[] = "POST"; |
ccli8 |
1:5ffad9f24d63 | 178 | const char USER_TOPIC_HTTPS_REQUEST_MESSAGE_BODY[] = "{ \"message\": \"Hello from Nuvoton Mbed device\" }"; |
ccli8 |
1:5ffad9f24d63 | 179 | |
ccli8 |
1:5ffad9f24d63 | 180 | /* Update thing shadow by publishing to UpdateThingShadow topic through HTTPS/POST |
ccli8 |
1:5ffad9f24d63 | 181 | * HTTP POST https://"endpoint"/topics/$aws/things/"thingName"/shadow/update */ |
ccli8 |
1:5ffad9f24d63 | 182 | const char UPDATETHINGSHADOW_TOPIC_HTTPS_PATH[] = "/topics/$aws/things/" AWS_IOT_HTTPS_THINGNAME "/shadow/update?qos=1"; |
ccli8 |
1:5ffad9f24d63 | 183 | const char UPDATETHINGSHADOW_TOPIC_HTTPS_REQUEST_METHOD[] = "POST"; |
ccli8 |
1:5ffad9f24d63 | 184 | const char UPDATETHINGSHADOW_TOPIC_HTTPS_REQUEST_MESSAGE_BODY[] = "{ \"state\": { \"reported\": { \"attribute1\": 3, \"attribute2\": \"1\" } } }"; |
ccli8 |
1:5ffad9f24d63 | 185 | |
ccli8 |
1:5ffad9f24d63 | 186 | /* Get thing shadow by publishing to GetThingShadow topic through HTTPS/POST |
ccli8 |
1:5ffad9f24d63 | 187 | * HTTP POST https://"endpoint"/topics/$aws/things/"thingName"/shadow/get */ |
ccli8 |
1:5ffad9f24d63 | 188 | const char GETTHINGSHADOW_TOPIC_HTTPS_PATH[] = "/topics/$aws/things/" AWS_IOT_HTTPS_THINGNAME "/shadow/get?qos=1"; |
ccli8 |
1:5ffad9f24d63 | 189 | const char GETTHINGSHADOW_TOPIC_HTTPS_REQUEST_METHOD[] = "POST"; |
ccli8 |
1:5ffad9f24d63 | 190 | const char GETTHINGSHADOW_TOPIC_HTTPS_REQUEST_MESSAGE_BODY[] = ""; |
ccli8 |
1:5ffad9f24d63 | 191 | |
ccli8 |
1:5ffad9f24d63 | 192 | /* Delete thing shadow by publishing to DeleteThingShadow topic through HTTPS/POST |
ccli8 |
1:5ffad9f24d63 | 193 | * HTTP POST https://"endpoint"/topics/$aws/things/"thingName"/shadow/delete */ |
ccli8 |
1:5ffad9f24d63 | 194 | const char DELETETHINGSHADOW_TOPIC_HTTPS_PATH[] = "/topics/$aws/things/" AWS_IOT_HTTPS_THINGNAME "/shadow/delete?qos=1"; |
ccli8 |
1:5ffad9f24d63 | 195 | const char DELETETHINGSHADOW_TOPIC_HTTPS_REQUEST_METHOD[] = "POST"; |
ccli8 |
1:5ffad9f24d63 | 196 | const char DELETETHINGSHADOW_TOPIC_HTTPS_REQUEST_MESSAGE_BODY[] = ""; |
ccli8 |
1:5ffad9f24d63 | 197 | |
ccli8 |
1:5ffad9f24d63 | 198 | /* Update thing shadow RESTfully through HTTPS/POST |
ccli8 |
1:5ffad9f24d63 | 199 | * HTTP POST https://endpoint/things/thingName/shadow */ |
ccli8 |
1:5ffad9f24d63 | 200 | const char UPDATETHINGSHADOW_THING_HTTPS_PATH[] = "/things/" AWS_IOT_HTTPS_THINGNAME "/shadow"; |
ccli8 |
1:5ffad9f24d63 | 201 | const char UPDATETHINGSHADOW_THING_HTTPS_REQUEST_METHOD[] = "POST"; |
ccli8 |
1:5ffad9f24d63 | 202 | const char UPDATETHINGSHADOW_THING_HTTPS_REQUEST_MESSAGE_BODY[] = "{ \"state\": { \"desired\": { \"attribute1\": 1, \"attribute2\": \"2\" }, \"reported\": { \"attribute1\": 2, \"attribute2\": \"1\" } } }"; |
ccli8 |
1:5ffad9f24d63 | 203 | |
ccli8 |
1:5ffad9f24d63 | 204 | /* Get thing shadow RESTfully through HTTPS/GET |
ccli8 |
1:5ffad9f24d63 | 205 | * HTTP GET https://"endpoint"/things/"thingName"/shadow */ |
ccli8 |
1:5ffad9f24d63 | 206 | const char GETTHINGSHADOW_THING_HTTPS_PATH[] = "/things/" AWS_IOT_HTTPS_THINGNAME "/shadow"; |
ccli8 |
1:5ffad9f24d63 | 207 | const char GETTHINGSHADOW_THING_HTTPS_REQUEST_METHOD[] = "GET"; |
ccli8 |
1:5ffad9f24d63 | 208 | const char GETTHINGSHADOW_THING_HTTPS_REQUEST_MESSAGE_BODY[] = ""; |
ccli8 |
1:5ffad9f24d63 | 209 | |
ccli8 |
1:5ffad9f24d63 | 210 | /* Delete thing shadow RESTfully through HTTPS/DELETE |
ccli8 |
1:5ffad9f24d63 | 211 | * HTTP DELETE https://endpoint/things/thingName/shadow */ |
ccli8 |
1:5ffad9f24d63 | 212 | const char DELETETHINGSHADOW_THING_HTTPS_PATH[] = "/things/" AWS_IOT_HTTPS_THINGNAME "/shadow"; |
ccli8 |
1:5ffad9f24d63 | 213 | const char DELETETHINGSHADOW_THING_HTTPS_REQUEST_METHOD[] = "DELETE"; |
ccli8 |
1:5ffad9f24d63 | 214 | const char DELETETHINGSHADOW_THING_HTTPS_REQUEST_MESSAGE_BODY[] = ""; |
ccli8 |
1:5ffad9f24d63 | 215 | |
ccli8 |
1:5ffad9f24d63 | 216 | /* HTTPS user buffer size */ |
ccli8 |
1:5ffad9f24d63 | 217 | const int HTTPS_USER_BUFFER_SIZE = 600; |
ccli8 |
1:5ffad9f24d63 | 218 | |
ccli8 |
1:5ffad9f24d63 | 219 | const char *HTTPS_OK_STR = "200 OK"; |
ccli8 |
1:5ffad9f24d63 | 220 | |
ccli8 |
1:5ffad9f24d63 | 221 | #endif // End of AWS_IOT_HTTPS_TEST |
ccli8 |
1:5ffad9f24d63 | 222 | |
ccli8 |
1:5ffad9f24d63 | 223 | } |
ccli8 |
1:5ffad9f24d63 | 224 | |
ccli8 |
1:5ffad9f24d63 | 225 | #if AWS_IOT_MQTT_TEST |
ccli8 |
1:5ffad9f24d63 | 226 | |
ccli8 |
1:5ffad9f24d63 | 227 | /** |
ccli8 |
1:5ffad9f24d63 | 228 | * /brief AWS_IoT_MQTT_Test implements the logic with AWS IoT User/Thing Shadow topics through MQTT. |
ccli8 |
1:5ffad9f24d63 | 229 | */ |
ccli8 |
1:5ffad9f24d63 | 230 | class AWS_IoT_MQTT_Test { |
ccli8 |
1:5ffad9f24d63 | 231 | |
ccli8 |
1:5ffad9f24d63 | 232 | public: |
ccli8 |
1:5ffad9f24d63 | 233 | /** |
ccli8 |
1:5ffad9f24d63 | 234 | * @brief AWS_IoT_MQTT_Test Constructor |
ccli8 |
1:5ffad9f24d63 | 235 | * |
ccli8 |
1:5ffad9f24d63 | 236 | * @param[in] domain Domain name of the MQTT server |
ccli8 |
1:5ffad9f24d63 | 237 | * @param[in] port Port number of the MQTT server |
ccli8 |
1:5ffad9f24d63 | 238 | * @param[in] net_iface Network interface |
ccli8 |
1:5ffad9f24d63 | 239 | */ |
ccli8 |
1:5ffad9f24d63 | 240 | AWS_IoT_MQTT_Test(const char * domain, const uint16_t port, NetworkInterface *net_iface) : |
ccli8 |
1:5ffad9f24d63 | 241 | _domain(domain), _port(port) { |
ccli8 |
1:5ffad9f24d63 | 242 | _tlssocket = new TLSSocket(net_iface, SSL_CA_CERT_PEM, SSL_USER_CERT_PEM, SSL_USER_PRIV_KEY_PEM); |
ccli8 |
1:5ffad9f24d63 | 243 | /* Blocking mode */ |
ccli8 |
1:5ffad9f24d63 | 244 | _tlssocket->set_blocking(true); |
ccli8 |
1:5ffad9f24d63 | 245 | /* Print Mbed TLS handshake log */ |
ccli8 |
1:5ffad9f24d63 | 246 | _tlssocket->set_debug(true); |
ccli8 |
1:5ffad9f24d63 | 247 | |
ccli8 |
1:5ffad9f24d63 | 248 | _mqtt_client = new MQTT::Client<TLSSocket, Countdown, MAX_MQTT_PACKET_SIZE>(*_tlssocket); |
ccli8 |
1:5ffad9f24d63 | 249 | } |
ccli8 |
1:5ffad9f24d63 | 250 | |
ccli8 |
1:5ffad9f24d63 | 251 | /** |
ccli8 |
1:5ffad9f24d63 | 252 | * @brief AWS_IoT_MQTT_Test Destructor |
ccli8 |
1:5ffad9f24d63 | 253 | */ |
ccli8 |
1:5ffad9f24d63 | 254 | ~AWS_IoT_MQTT_Test() { |
ccli8 |
1:5ffad9f24d63 | 255 | delete _mqtt_client; |
ccli8 |
1:5ffad9f24d63 | 256 | _mqtt_client = NULL; |
ccli8 |
1:5ffad9f24d63 | 257 | |
ccli8 |
1:5ffad9f24d63 | 258 | _tlssocket->close(); |
ccli8 |
1:5ffad9f24d63 | 259 | delete _tlssocket; |
ccli8 |
1:5ffad9f24d63 | 260 | _tlssocket = NULL; |
ccli8 |
1:5ffad9f24d63 | 261 | } |
ccli8 |
1:5ffad9f24d63 | 262 | /** |
ccli8 |
1:5ffad9f24d63 | 263 | * @brief Start AWS IoT test through MQTT |
ccli8 |
1:5ffad9f24d63 | 264 | */ |
ccli8 |
1:5ffad9f24d63 | 265 | void start_test() { |
ccli8 |
1:5ffad9f24d63 | 266 | |
ccli8 |
1:5ffad9f24d63 | 267 | int tls_rc; |
ccli8 |
1:5ffad9f24d63 | 268 | int mqtt_rc; |
ccli8 |
1:5ffad9f24d63 | 269 | |
ccli8 |
1:5ffad9f24d63 | 270 | do { |
ccli8 |
1:5ffad9f24d63 | 271 | /* Connect to the server */ |
ccli8 |
1:5ffad9f24d63 | 272 | /* Initialize TLS-related stuff */ |
ccli8 |
1:5ffad9f24d63 | 273 | printf("Connecting with %s:%d\n", _domain, _port); |
ccli8 |
1:5ffad9f24d63 | 274 | tls_rc = _tlssocket->connect(_domain, _port); |
ccli8 |
1:5ffad9f24d63 | 275 | if (tls_rc != NSAPI_ERROR_OK) { |
ccli8 |
3:4c8cccd32e34 | 276 | printf("Connects with %s:%d failed: %d\n", _domain, _port, tls_rc); |
ccli8 |
1:5ffad9f24d63 | 277 | break; |
ccli8 |
1:5ffad9f24d63 | 278 | } |
ccli8 |
1:5ffad9f24d63 | 279 | printf("Connects with %s:%d OK\n", _domain, _port); |
ccli8 |
1:5ffad9f24d63 | 280 | |
ccli8 |
1:5ffad9f24d63 | 281 | /* See the link below for AWS IoT support for MQTT: |
ccli8 |
1:5ffad9f24d63 | 282 | * http://docs.aws.amazon.com/iot/latest/developerguide/protocols.html */ |
ccli8 |
1:5ffad9f24d63 | 283 | |
ccli8 |
1:5ffad9f24d63 | 284 | /* MQTT connect */ |
ccli8 |
1:5ffad9f24d63 | 285 | /* The message broker does not support persistent sessions (connections made with |
ccli8 |
1:5ffad9f24d63 | 286 | * the cleanSession flag set to false. */ |
ccli8 |
1:5ffad9f24d63 | 287 | MQTTPacket_connectData conn_data = MQTTPacket_connectData_initializer; |
ccli8 |
1:5ffad9f24d63 | 288 | /* AWS IoT message broker implementation is based on MQTT version 3.1.1 |
ccli8 |
1:5ffad9f24d63 | 289 | * 3 = 3.1 |
ccli8 |
1:5ffad9f24d63 | 290 | * 4 = 3.1.1 */ |
ccli8 |
1:5ffad9f24d63 | 291 | conn_data.MQTTVersion = 4; |
ccli8 |
1:5ffad9f24d63 | 292 | /* Version number of this structure. Must be 0 */ |
ccli8 |
1:5ffad9f24d63 | 293 | conn_data.struct_version = 0; |
ccli8 |
1:5ffad9f24d63 | 294 | /* The message broker uses the client ID to identify each client. The client ID is passed |
ccli8 |
1:5ffad9f24d63 | 295 | * in from the client to the message broker as part of the MQTT payload. Two clients with |
ccli8 |
1:5ffad9f24d63 | 296 | * the same client ID are not allowed to be connected concurrently to the message broker. |
ccli8 |
1:5ffad9f24d63 | 297 | * When a client connects to the message broker using a client ID that another client is using, |
ccli8 |
1:5ffad9f24d63 | 298 | * a CONNACK message will be sent to both clients and the currently connected client will be |
ccli8 |
1:5ffad9f24d63 | 299 | * disconnected. */ |
ccli8 |
1:5ffad9f24d63 | 300 | conn_data.clientID.cstring = AWS_IOT_MQTT_CLIENTNAME; |
ccli8 |
1:5ffad9f24d63 | 301 | /* The message broker does not support persistent sessions (connections made with |
ccli8 |
1:5ffad9f24d63 | 302 | * the cleanSession flag set to false. The AWS IoT message broker assumes all sessions |
ccli8 |
1:5ffad9f24d63 | 303 | * are clean sessions and messages are not stored across sessions. If an MQTT client |
ccli8 |
1:5ffad9f24d63 | 304 | * attempts to connect to the AWS IoT message broker with the cleanSession set to false, |
ccli8 |
1:5ffad9f24d63 | 305 | * the client will be disconnected. */ |
ccli8 |
1:5ffad9f24d63 | 306 | conn_data.cleansession = 1; |
ccli8 |
1:5ffad9f24d63 | 307 | //conn_data.username.cstring = "USERNAME"; |
ccli8 |
1:5ffad9f24d63 | 308 | //conn_data.password.cstring = "PASSWORD"; |
ccli8 |
1:5ffad9f24d63 | 309 | |
ccli8 |
1:5ffad9f24d63 | 310 | MQTT::connackData connack_data; |
ccli8 |
1:5ffad9f24d63 | 311 | |
ccli8 |
1:5ffad9f24d63 | 312 | /* _tlssocket must connect to the network endpoint before calling this. */ |
ccli8 |
1:5ffad9f24d63 | 313 | printf("MQTT connecting"); |
ccli8 |
1:5ffad9f24d63 | 314 | if ((mqtt_rc = _mqtt_client->connect(conn_data, connack_data)) != 0) { |
ccli8 |
1:5ffad9f24d63 | 315 | printf("\rMQTT connects failed: %d\n", mqtt_rc); |
ccli8 |
1:5ffad9f24d63 | 316 | break; |
ccli8 |
1:5ffad9f24d63 | 317 | } |
ccli8 |
1:5ffad9f24d63 | 318 | printf("\rMQTT connects OK\n\n"); |
ccli8 |
1:5ffad9f24d63 | 319 | |
ccli8 |
1:5ffad9f24d63 | 320 | /* Subscribe/publish user topic */ |
ccli8 |
1:5ffad9f24d63 | 321 | printf("Subscribing/publishing user topic\n"); |
ccli8 |
1:5ffad9f24d63 | 322 | if (! sub_pub_topic(USER_MQTT_TOPIC, USER_MQTT_TOPIC_FILTERS, sizeof (USER_MQTT_TOPIC_FILTERS) / sizeof (USER_MQTT_TOPIC_FILTERS[0]), USER_MQTT_TOPIC_PUBLISH_MESSAGE)) { |
ccli8 |
1:5ffad9f24d63 | 323 | break; |
ccli8 |
1:5ffad9f24d63 | 324 | } |
ccli8 |
1:5ffad9f24d63 | 325 | printf("Subscribes/publishes user topic OK\n\n"); |
ccli8 |
1:5ffad9f24d63 | 326 | |
ccli8 |
1:5ffad9f24d63 | 327 | /* Subscribe/publish UpdateThingShadow topic */ |
ccli8 |
1:5ffad9f24d63 | 328 | printf("Subscribing/publishing UpdateThingShadow topic\n"); |
ccli8 |
1:5ffad9f24d63 | 329 | if (! sub_pub_topic(UPDATETHINGSHADOW_MQTT_TOPIC, UPDATETHINGSHADOW_MQTT_TOPIC_FILTERS, sizeof (UPDATETHINGSHADOW_MQTT_TOPIC_FILTERS) / sizeof (UPDATETHINGSHADOW_MQTT_TOPIC_FILTERS[0]), UPDATETHINGSHADOW_MQTT_TOPIC_PUBLISH_MESSAGE)) { |
ccli8 |
1:5ffad9f24d63 | 330 | break; |
ccli8 |
1:5ffad9f24d63 | 331 | } |
ccli8 |
1:5ffad9f24d63 | 332 | printf("Subscribes/publishes UpdateThingShadow topic OK\n\n"); |
ccli8 |
1:5ffad9f24d63 | 333 | |
ccli8 |
1:5ffad9f24d63 | 334 | /* Subscribe/publish GetThingShadow topic */ |
ccli8 |
1:5ffad9f24d63 | 335 | printf("Subscribing/publishing GetThingShadow topic\n"); |
ccli8 |
1:5ffad9f24d63 | 336 | if (! sub_pub_topic(GETTHINGSHADOW_MQTT_TOPIC, GETTHINGSHADOW_MQTT_TOPIC_FILTERS, sizeof (GETTHINGSHADOW_MQTT_TOPIC_FILTERS) / sizeof (GETTHINGSHADOW_MQTT_TOPIC_FILTERS[0]), GETTHINGSHADOW_MQTT_TOPIC_PUBLISH_MESSAGE)) { |
ccli8 |
1:5ffad9f24d63 | 337 | break; |
ccli8 |
1:5ffad9f24d63 | 338 | } |
ccli8 |
1:5ffad9f24d63 | 339 | printf("Subscribes/publishes GetThingShadow topic OK\n\n"); |
ccli8 |
1:5ffad9f24d63 | 340 | |
ccli8 |
1:5ffad9f24d63 | 341 | /* Subscribe/publish DeleteThingShadow topic */ |
ccli8 |
1:5ffad9f24d63 | 342 | printf("Subscribing/publishing DeleteThingShadow topic\n"); |
ccli8 |
1:5ffad9f24d63 | 343 | if (! sub_pub_topic(DELETETHINGSHADOW_MQTT_TOPIC, DELETETHINGSHADOW_MQTT_TOPIC_FILTERS, sizeof (DELETETHINGSHADOW_MQTT_TOPIC_FILTERS) / sizeof (DELETETHINGSHADOW_MQTT_TOPIC_FILTERS[0]), DELETETHINGSHADOW_MQTT_TOPIC_PUBLISH_MESSAGE)) { |
ccli8 |
1:5ffad9f24d63 | 344 | break; |
ccli8 |
1:5ffad9f24d63 | 345 | } |
ccli8 |
1:5ffad9f24d63 | 346 | printf("Subscribes/publishes DeleteThingShadow topic OK\n\n"); |
ccli8 |
1:5ffad9f24d63 | 347 | |
ccli8 |
1:5ffad9f24d63 | 348 | } while (0); |
ccli8 |
1:5ffad9f24d63 | 349 | |
ccli8 |
1:5ffad9f24d63 | 350 | printf("MQTT disconnecting"); |
ccli8 |
1:5ffad9f24d63 | 351 | if ((mqtt_rc = _mqtt_client->disconnect()) != 0) { |
ccli8 |
1:5ffad9f24d63 | 352 | printf("\rMQTT disconnects failed %d\n", mqtt_rc); |
ccli8 |
1:5ffad9f24d63 | 353 | } |
ccli8 |
1:5ffad9f24d63 | 354 | printf("\rMQTT disconnects OK\n"); |
ccli8 |
1:5ffad9f24d63 | 355 | |
ccli8 |
1:5ffad9f24d63 | 356 | _tlssocket->close(); |
ccli8 |
1:5ffad9f24d63 | 357 | } |
ccli8 |
1:5ffad9f24d63 | 358 | |
ccli8 |
1:5ffad9f24d63 | 359 | |
ccli8 |
1:5ffad9f24d63 | 360 | protected: |
ccli8 |
1:5ffad9f24d63 | 361 | |
ccli8 |
1:5ffad9f24d63 | 362 | /** |
ccli8 |
1:5ffad9f24d63 | 363 | * @brief Subscribe/publish specific topic |
ccli8 |
1:5ffad9f24d63 | 364 | */ |
ccli8 |
1:5ffad9f24d63 | 365 | bool sub_pub_topic(const char *topic, const char **topic_filters, size_t topic_filters_size, const char *publish_message_body) { |
ccli8 |
1:5ffad9f24d63 | 366 | |
ccli8 |
1:5ffad9f24d63 | 367 | bool ret = false; |
ccli8 |
1:5ffad9f24d63 | 368 | int mqtt_rc; |
ccli8 |
1:5ffad9f24d63 | 369 | |
ccli8 |
1:5ffad9f24d63 | 370 | do { |
ccli8 |
1:5ffad9f24d63 | 371 | const char **topic_filter; |
ccli8 |
1:5ffad9f24d63 | 372 | const char **topic_filter_end = topic_filters + topic_filters_size; |
ccli8 |
1:5ffad9f24d63 | 373 | |
ccli8 |
1:5ffad9f24d63 | 374 | for (topic_filter = topic_filters; topic_filter != topic_filter_end; topic_filter ++) { |
ccli8 |
1:5ffad9f24d63 | 375 | /* AWS IoT does not support publishing and subscribing with QoS 2. |
ccli8 |
1:5ffad9f24d63 | 376 | * The AWS IoT message broker does not send a PUBACK or SUBACK when QoS 2 is requested. */ |
ccli8 |
1:5ffad9f24d63 | 377 | printf("MQTT subscribing to %s", *topic_filter); |
ccli8 |
1:5ffad9f24d63 | 378 | if ((mqtt_rc = _mqtt_client->subscribe(*topic_filter, MQTT::QOS1, message_arrived)) != 0) { |
ccli8 |
1:5ffad9f24d63 | 379 | printf("\rMQTT subscribes to %s failed: %d\n", *topic_filter, mqtt_rc); |
ccli8 |
1:5ffad9f24d63 | 380 | continue; |
ccli8 |
1:5ffad9f24d63 | 381 | } |
ccli8 |
1:5ffad9f24d63 | 382 | printf("\rMQTT subscribes to %s OK\n", *topic_filter); |
ccli8 |
1:5ffad9f24d63 | 383 | } |
ccli8 |
1:5ffad9f24d63 | 384 | |
ccli8 |
1:5ffad9f24d63 | 385 | MQTT::Message message; |
ccli8 |
1:5ffad9f24d63 | 386 | |
ccli8 |
1:5ffad9f24d63 | 387 | int _bpos; |
ccli8 |
1:5ffad9f24d63 | 388 | |
ccli8 |
1:5ffad9f24d63 | 389 | _bpos = snprintf(_buffer, sizeof (_buffer) - 1, publish_message_body); |
ccli8 |
1:5ffad9f24d63 | 390 | if (_bpos < 0 || ((size_t) _bpos) > (sizeof (_buffer) - 1)) { |
ccli8 |
1:5ffad9f24d63 | 391 | printf("snprintf failed: %d\n", _bpos); |
ccli8 |
1:5ffad9f24d63 | 392 | break; |
ccli8 |
1:5ffad9f24d63 | 393 | } |
ccli8 |
1:5ffad9f24d63 | 394 | _buffer[_bpos] = 0; |
ccli8 |
1:5ffad9f24d63 | 395 | /* AWS IoT does not support publishing and subscribing with QoS 2. |
ccli8 |
1:5ffad9f24d63 | 396 | * The AWS IoT message broker does not send a PUBACK or SUBACK when QoS 2 is requested. */ |
ccli8 |
1:5ffad9f24d63 | 397 | message.qos = MQTT::QOS1; |
ccli8 |
1:5ffad9f24d63 | 398 | message.retained = false; |
ccli8 |
1:5ffad9f24d63 | 399 | message.dup = false; |
ccli8 |
1:5ffad9f24d63 | 400 | message.payload = _buffer; |
ccli8 |
1:5ffad9f24d63 | 401 | message.payloadlen = strlen(_buffer); |
ccli8 |
1:5ffad9f24d63 | 402 | /* Print publish message */ |
ccli8 |
1:5ffad9f24d63 | 403 | printf("Message to publish:\n"); |
ccli8 |
1:5ffad9f24d63 | 404 | printf("%s\n", _buffer); |
ccli8 |
1:5ffad9f24d63 | 405 | printf("MQTT publishing message to %s", topic); |
ccli8 |
1:5ffad9f24d63 | 406 | if ((mqtt_rc = _mqtt_client->publish(topic, message)) != 0) { |
ccli8 |
1:5ffad9f24d63 | 407 | printf("\rMQTT publishes message to %s failed: %d\n", topic, mqtt_rc); |
ccli8 |
1:5ffad9f24d63 | 408 | break; |
ccli8 |
1:5ffad9f24d63 | 409 | } |
ccli8 |
1:5ffad9f24d63 | 410 | printf("\rMQTT publishes message to %s OK\n", topic); |
ccli8 |
1:5ffad9f24d63 | 411 | |
ccli8 |
1:5ffad9f24d63 | 412 | /* Receive message with subscribed topic */ |
ccli8 |
1:5ffad9f24d63 | 413 | while (! _message_arrive_count) { |
ccli8 |
1:5ffad9f24d63 | 414 | _mqtt_client->yield(100); |
ccli8 |
1:5ffad9f24d63 | 415 | } |
ccli8 |
1:5ffad9f24d63 | 416 | clear_message_arrive_count(); |
ccli8 |
1:5ffad9f24d63 | 417 | printf("\n"); |
ccli8 |
1:5ffad9f24d63 | 418 | |
ccli8 |
1:5ffad9f24d63 | 419 | /* Unsubscribe |
ccli8 |
1:5ffad9f24d63 | 420 | * We meet second unsubscribe failed. This is caused by MQTT lib bug. */ |
ccli8 |
1:5ffad9f24d63 | 421 | for (topic_filter = topic_filters; topic_filter != topic_filter_end; topic_filter ++) { |
ccli8 |
1:5ffad9f24d63 | 422 | printf("MQTT unsubscribing from %s", *topic_filter); |
ccli8 |
1:5ffad9f24d63 | 423 | if ((mqtt_rc = _mqtt_client->unsubscribe(*topic_filter)) != 0) { |
ccli8 |
1:5ffad9f24d63 | 424 | printf("\rMQTT unsubscribes from %s failed: %d\n", *topic_filter, mqtt_rc); |
ccli8 |
1:5ffad9f24d63 | 425 | continue; |
ccli8 |
1:5ffad9f24d63 | 426 | } |
ccli8 |
1:5ffad9f24d63 | 427 | printf("\rMQTT unsubscribes from %s OK\n", *topic_filter); |
ccli8 |
1:5ffad9f24d63 | 428 | } |
ccli8 |
1:5ffad9f24d63 | 429 | |
ccli8 |
1:5ffad9f24d63 | 430 | ret = true; |
ccli8 |
1:5ffad9f24d63 | 431 | |
ccli8 |
1:5ffad9f24d63 | 432 | } while (0); |
ccli8 |
1:5ffad9f24d63 | 433 | |
ccli8 |
1:5ffad9f24d63 | 434 | return ret; |
ccli8 |
1:5ffad9f24d63 | 435 | } |
ccli8 |
1:5ffad9f24d63 | 436 | |
ccli8 |
1:5ffad9f24d63 | 437 | protected: |
ccli8 |
1:5ffad9f24d63 | 438 | TLSSocket * _tlssocket; |
ccli8 |
1:5ffad9f24d63 | 439 | MQTT::Client<TLSSocket, Countdown, MAX_MQTT_PACKET_SIZE> * _mqtt_client; |
ccli8 |
1:5ffad9f24d63 | 440 | |
ccli8 |
1:5ffad9f24d63 | 441 | const char *_domain; /**< Domain name of the MQTT server */ |
ccli8 |
1:5ffad9f24d63 | 442 | const uint16_t _port; /**< Port number of the MQTT server */ |
ccli8 |
1:5ffad9f24d63 | 443 | char _buffer[MQTT_USER_BUFFER_SIZE]; /**< User buffer */ |
ccli8 |
1:5ffad9f24d63 | 444 | |
ccli8 |
1:5ffad9f24d63 | 445 | private: |
ccli8 |
1:5ffad9f24d63 | 446 | static volatile uint16_t _message_arrive_count; |
ccli8 |
1:5ffad9f24d63 | 447 | |
ccli8 |
1:5ffad9f24d63 | 448 | static void message_arrived(MQTT::MessageData& md) { |
ccli8 |
1:5ffad9f24d63 | 449 | MQTT::Message &message = md.message; |
ccli8 |
1:5ffad9f24d63 | 450 | printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); |
ccli8 |
1:5ffad9f24d63 | 451 | printf("Payload:\n"); |
ccli8 |
1:5ffad9f24d63 | 452 | printf("%.*s\n", message.payloadlen, (char*)message.payload); |
ccli8 |
1:5ffad9f24d63 | 453 | ++ _message_arrive_count; |
ccli8 |
1:5ffad9f24d63 | 454 | } |
ccli8 |
1:5ffad9f24d63 | 455 | |
ccli8 |
1:5ffad9f24d63 | 456 | static void clear_message_arrive_count() { |
ccli8 |
1:5ffad9f24d63 | 457 | _message_arrive_count = 0; |
ccli8 |
1:5ffad9f24d63 | 458 | } |
ccli8 |
1:5ffad9f24d63 | 459 | }; |
ccli8 |
1:5ffad9f24d63 | 460 | |
ccli8 |
1:5ffad9f24d63 | 461 | volatile uint16_t AWS_IoT_MQTT_Test::_message_arrive_count = 0; |
ccli8 |
1:5ffad9f24d63 | 462 | |
ccli8 |
1:5ffad9f24d63 | 463 | #endif // End of AWS_IOT_MQTT_TEST |
ccli8 |
1:5ffad9f24d63 | 464 | |
ccli8 |
1:5ffad9f24d63 | 465 | |
ccli8 |
1:5ffad9f24d63 | 466 | #if AWS_IOT_HTTPS_TEST |
ccli8 |
1:5ffad9f24d63 | 467 | |
ccli8 |
1:5ffad9f24d63 | 468 | /** |
ccli8 |
1:5ffad9f24d63 | 469 | * /brief AWS_IoT_HTTPS_Test implements the logic with AWS IoT User/Thing Shadow topics (publish-only) |
ccli8 |
1:5ffad9f24d63 | 470 | * and Thing Shadow RESTful API through HTTPS. |
ccli8 |
1:5ffad9f24d63 | 471 | */ |
ccli8 |
1:5ffad9f24d63 | 472 | class AWS_IoT_HTTPS_Test { |
ccli8 |
1:5ffad9f24d63 | 473 | |
ccli8 |
1:5ffad9f24d63 | 474 | public: |
ccli8 |
1:5ffad9f24d63 | 475 | /** |
ccli8 |
1:5ffad9f24d63 | 476 | * @brief AWS_IoT_HTTPS_Test Constructor |
ccli8 |
1:5ffad9f24d63 | 477 | * |
ccli8 |
1:5ffad9f24d63 | 478 | * @param[in] domain Domain name of the HTTPS server |
ccli8 |
1:5ffad9f24d63 | 479 | * @param[in] port Port number of the HTTPS server |
ccli8 |
1:5ffad9f24d63 | 480 | * @param[in] net_iface Network interface |
ccli8 |
1:5ffad9f24d63 | 481 | */ |
ccli8 |
1:5ffad9f24d63 | 482 | AWS_IoT_HTTPS_Test(const char * domain, const uint16_t port, NetworkInterface *net_iface) : |
ccli8 |
1:5ffad9f24d63 | 483 | _domain(domain), _port(port) { |
ccli8 |
1:5ffad9f24d63 | 484 | |
ccli8 |
1:5ffad9f24d63 | 485 | _tlssocket = new TLSSocket(net_iface, SSL_CA_CERT_PEM, SSL_USER_CERT_PEM, SSL_USER_PRIV_KEY_PEM); |
ccli8 |
1:5ffad9f24d63 | 486 | /* Non-blocking mode */ |
ccli8 |
1:5ffad9f24d63 | 487 | _tlssocket->set_blocking(false); |
ccli8 |
1:5ffad9f24d63 | 488 | /* Print Mbed TLS handshake log */ |
ccli8 |
1:5ffad9f24d63 | 489 | _tlssocket->set_debug(true); |
ccli8 |
1:5ffad9f24d63 | 490 | } |
ccli8 |
1:5ffad9f24d63 | 491 | /** |
ccli8 |
1:5ffad9f24d63 | 492 | * @brief AWS_IoT_HTTPS_Test Destructor |
ccli8 |
1:5ffad9f24d63 | 493 | */ |
ccli8 |
1:5ffad9f24d63 | 494 | ~AWS_IoT_HTTPS_Test() { |
ccli8 |
1:5ffad9f24d63 | 495 | _tlssocket->close(); |
ccli8 |
1:5ffad9f24d63 | 496 | delete _tlssocket; |
ccli8 |
1:5ffad9f24d63 | 497 | _tlssocket = NULL; |
ccli8 |
1:5ffad9f24d63 | 498 | } |
ccli8 |
1:5ffad9f24d63 | 499 | /** |
ccli8 |
1:5ffad9f24d63 | 500 | * @brief Start AWS IoT test through HTTPS |
ccli8 |
1:5ffad9f24d63 | 501 | * |
ccli8 |
1:5ffad9f24d63 | 502 | * @param[in] path The path of the file to fetch from the HTTPS server |
ccli8 |
1:5ffad9f24d63 | 503 | */ |
ccli8 |
1:5ffad9f24d63 | 504 | void start_test() { |
ccli8 |
1:5ffad9f24d63 | 505 | |
ccli8 |
1:5ffad9f24d63 | 506 | int tls_rc; |
ccli8 |
1:5ffad9f24d63 | 507 | |
ccli8 |
1:5ffad9f24d63 | 508 | do { |
ccli8 |
1:5ffad9f24d63 | 509 | /* Connect to the server */ |
ccli8 |
1:5ffad9f24d63 | 510 | /* Initialize TLS-related stuff */ |
ccli8 |
1:5ffad9f24d63 | 511 | printf("Connecting with %s:%d\n", _domain, _port); |
ccli8 |
1:5ffad9f24d63 | 512 | tls_rc = _tlssocket->connect(_domain, _port); |
ccli8 |
1:5ffad9f24d63 | 513 | if (tls_rc != NSAPI_ERROR_OK) { |
ccli8 |
3:4c8cccd32e34 | 514 | printf("Connects with %s:%d failed: %d\n", _domain, _port, tls_rc); |
ccli8 |
1:5ffad9f24d63 | 515 | break; |
ccli8 |
1:5ffad9f24d63 | 516 | } |
ccli8 |
1:5ffad9f24d63 | 517 | printf("Connects with %s:%d OK\n\n", _domain, _port); |
ccli8 |
1:5ffad9f24d63 | 518 | |
ccli8 |
1:5ffad9f24d63 | 519 | /* Publish to user topic through HTTPS/POST */ |
ccli8 |
1:5ffad9f24d63 | 520 | printf("Publishing to user topic through HTTPS/POST\n"); |
ccli8 |
1:5ffad9f24d63 | 521 | if (! run_req_resp(USER_TOPIC_HTTPS_PATH, USER_TOPIC_HTTPS_REQUEST_METHOD, USER_TOPIC_HTTPS_REQUEST_MESSAGE_BODY)) { |
ccli8 |
1:5ffad9f24d63 | 522 | break; |
ccli8 |
1:5ffad9f24d63 | 523 | } |
ccli8 |
1:5ffad9f24d63 | 524 | printf("Publishes to user topic through HTTPS/POST OK\n\n"); |
ccli8 |
1:5ffad9f24d63 | 525 | |
ccli8 |
1:5ffad9f24d63 | 526 | /* Update thing shadow by publishing to UpdateThingShadow topic through HTTPS/POST */ |
ccli8 |
1:5ffad9f24d63 | 527 | printf("Updating thing shadow by publishing to Update Thing Shadow topic through HTTPS/POST\n"); |
ccli8 |
1:5ffad9f24d63 | 528 | if (! run_req_resp(UPDATETHINGSHADOW_TOPIC_HTTPS_PATH, UPDATETHINGSHADOW_TOPIC_HTTPS_REQUEST_METHOD, UPDATETHINGSHADOW_TOPIC_HTTPS_REQUEST_MESSAGE_BODY)) { |
ccli8 |
1:5ffad9f24d63 | 529 | break; |
ccli8 |
1:5ffad9f24d63 | 530 | } |
ccli8 |
1:5ffad9f24d63 | 531 | printf("Update thing shadow by publishing to Update Thing Shadow topic through HTTPS/POST OK\n\n"); |
ccli8 |
1:5ffad9f24d63 | 532 | |
ccli8 |
1:5ffad9f24d63 | 533 | /* Get thing shadow by publishing to GetThingShadow topic through HTTPS/POST */ |
ccli8 |
1:5ffad9f24d63 | 534 | printf("Getting thing shadow by publishing to GetThingShadow topic through HTTPS/POST\n"); |
ccli8 |
1:5ffad9f24d63 | 535 | if (! run_req_resp(GETTHINGSHADOW_TOPIC_HTTPS_PATH, GETTHINGSHADOW_TOPIC_HTTPS_REQUEST_METHOD, GETTHINGSHADOW_TOPIC_HTTPS_REQUEST_MESSAGE_BODY)) { |
ccli8 |
1:5ffad9f24d63 | 536 | break; |
ccli8 |
1:5ffad9f24d63 | 537 | } |
ccli8 |
1:5ffad9f24d63 | 538 | printf("Get thing shadow by publishing to GetThingShadow topic through HTTPS/POST OK\n\n"); |
ccli8 |
1:5ffad9f24d63 | 539 | |
ccli8 |
1:5ffad9f24d63 | 540 | /* Delete thing shadow by publishing to DeleteThingShadow topic through HTTPS/POST */ |
ccli8 |
1:5ffad9f24d63 | 541 | printf("Deleting thing shadow by publishing to DeleteThingShadow topic through HTTPS/POST\n"); |
ccli8 |
1:5ffad9f24d63 | 542 | if (! run_req_resp(DELETETHINGSHADOW_TOPIC_HTTPS_PATH, DELETETHINGSHADOW_TOPIC_HTTPS_REQUEST_METHOD, DELETETHINGSHADOW_TOPIC_HTTPS_REQUEST_MESSAGE_BODY)) { |
ccli8 |
1:5ffad9f24d63 | 543 | break; |
ccli8 |
1:5ffad9f24d63 | 544 | } |
ccli8 |
1:5ffad9f24d63 | 545 | printf("Delete thing shadow by publishing to DeleteThingShadow topic through HTTPS/POST OK\n\n"); |
ccli8 |
1:5ffad9f24d63 | 546 | |
ccli8 |
1:5ffad9f24d63 | 547 | /* Update thing shadow RESTfully through HTTPS/POST */ |
ccli8 |
1:5ffad9f24d63 | 548 | printf("Updating thing shadow RESTfully through HTTPS/POST\n"); |
ccli8 |
1:5ffad9f24d63 | 549 | if (! run_req_resp(UPDATETHINGSHADOW_THING_HTTPS_PATH, UPDATETHINGSHADOW_THING_HTTPS_REQUEST_METHOD, UPDATETHINGSHADOW_THING_HTTPS_REQUEST_MESSAGE_BODY)) { |
ccli8 |
1:5ffad9f24d63 | 550 | break; |
ccli8 |
1:5ffad9f24d63 | 551 | } |
ccli8 |
1:5ffad9f24d63 | 552 | printf("Update thing shadow RESTfully through HTTPS/POST OK\n\n"); |
ccli8 |
1:5ffad9f24d63 | 553 | |
ccli8 |
1:5ffad9f24d63 | 554 | /* Get thing shadow RESTfully through HTTPS/GET */ |
ccli8 |
1:5ffad9f24d63 | 555 | printf("Getting thing shadow RESTfully through HTTPS/GET\n"); |
ccli8 |
1:5ffad9f24d63 | 556 | if (! run_req_resp(GETTHINGSHADOW_THING_HTTPS_PATH, GETTHINGSHADOW_THING_HTTPS_REQUEST_METHOD, GETTHINGSHADOW_THING_HTTPS_REQUEST_MESSAGE_BODY)) { |
ccli8 |
1:5ffad9f24d63 | 557 | break; |
ccli8 |
1:5ffad9f24d63 | 558 | } |
ccli8 |
1:5ffad9f24d63 | 559 | printf("Get thing shadow RESTfully through HTTPS/GET OK\n\n"); |
ccli8 |
1:5ffad9f24d63 | 560 | |
ccli8 |
1:5ffad9f24d63 | 561 | /* Delete thing shadow RESTfully through HTTPS/DELETE */ |
ccli8 |
1:5ffad9f24d63 | 562 | printf("Deleting thing shadow RESTfully through HTTPS/DELETE\n"); |
ccli8 |
1:5ffad9f24d63 | 563 | if (! run_req_resp(DELETETHINGSHADOW_THING_HTTPS_PATH, DELETETHINGSHADOW_THING_HTTPS_REQUEST_METHOD, DELETETHINGSHADOW_THING_HTTPS_REQUEST_MESSAGE_BODY)) { |
ccli8 |
1:5ffad9f24d63 | 564 | break; |
ccli8 |
1:5ffad9f24d63 | 565 | } |
ccli8 |
1:5ffad9f24d63 | 566 | printf("Delete thing shadow RESTfully through HTTPS/DELETE OK\n\n"); |
ccli8 |
1:5ffad9f24d63 | 567 | |
ccli8 |
1:5ffad9f24d63 | 568 | } while (0); |
ccli8 |
1:5ffad9f24d63 | 569 | |
ccli8 |
1:5ffad9f24d63 | 570 | /* Close socket */ |
ccli8 |
1:5ffad9f24d63 | 571 | _tlssocket->close(); |
ccli8 |
1:5ffad9f24d63 | 572 | } |
ccli8 |
1:5ffad9f24d63 | 573 | |
ccli8 |
1:5ffad9f24d63 | 574 | protected: |
ccli8 |
1:5ffad9f24d63 | 575 | |
ccli8 |
1:5ffad9f24d63 | 576 | /** |
ccli8 |
1:5ffad9f24d63 | 577 | * @brief Run request/response through HTTPS |
ccli8 |
1:5ffad9f24d63 | 578 | */ |
ccli8 |
1:5ffad9f24d63 | 579 | bool run_req_resp(const char *https_path, const char *https_request_method, const char *https_request_message_body) { |
ccli8 |
1:5ffad9f24d63 | 580 | |
ccli8 |
1:5ffad9f24d63 | 581 | bool ret = false; |
ccli8 |
1:5ffad9f24d63 | 582 | |
ccli8 |
1:5ffad9f24d63 | 583 | do { |
ccli8 |
1:5ffad9f24d63 | 584 | int tls_rc; |
ccli8 |
1:5ffad9f24d63 | 585 | bool _got200 = false; |
ccli8 |
1:5ffad9f24d63 | 586 | |
ccli8 |
1:5ffad9f24d63 | 587 | int _bpos; |
ccli8 |
1:5ffad9f24d63 | 588 | |
ccli8 |
1:5ffad9f24d63 | 589 | /* Fill the request buffer */ |
ccli8 |
1:5ffad9f24d63 | 590 | _bpos = snprintf(_buffer, sizeof(_buffer) - 1, |
ccli8 |
1:5ffad9f24d63 | 591 | "%s %s HTTP/1.1\r\n" "Host: %s\r\n" "Content-Length: %d\r\n" "\r\n" "%s", |
ccli8 |
1:5ffad9f24d63 | 592 | https_request_method, https_path, AWS_IOT_HTTPS_SERVER_NAME, strlen(https_request_message_body), https_request_message_body); |
ccli8 |
1:5ffad9f24d63 | 593 | if (_bpos < 0 || ((size_t) _bpos) > (sizeof (_buffer) - 1)) { |
ccli8 |
1:5ffad9f24d63 | 594 | printf("snprintf failed: %d\n", _bpos); |
ccli8 |
1:5ffad9f24d63 | 595 | break; |
ccli8 |
1:5ffad9f24d63 | 596 | } |
ccli8 |
1:5ffad9f24d63 | 597 | _buffer[_bpos] = 0; |
ccli8 |
1:5ffad9f24d63 | 598 | /* Print request message */ |
ccli8 |
1:5ffad9f24d63 | 599 | printf("HTTPS: Request message:\n"); |
ccli8 |
1:5ffad9f24d63 | 600 | printf("%s\n", _buffer); |
ccli8 |
1:5ffad9f24d63 | 601 | |
ccli8 |
1:5ffad9f24d63 | 602 | int offset = 0; |
ccli8 |
1:5ffad9f24d63 | 603 | do { |
ccli8 |
1:5ffad9f24d63 | 604 | tls_rc = _tlssocket->send((const unsigned char *) _buffer + offset, _bpos - offset); |
ccli8 |
1:5ffad9f24d63 | 605 | if (tls_rc > 0) { |
ccli8 |
1:5ffad9f24d63 | 606 | offset += tls_rc; |
ccli8 |
1:5ffad9f24d63 | 607 | } |
ccli8 |
1:5ffad9f24d63 | 608 | } while (offset < _bpos && |
ccli8 |
1:5ffad9f24d63 | 609 | (tls_rc > 0 || tls_rc == MBEDTLS_ERR_SSL_WANT_READ || tls_rc == MBEDTLS_ERR_SSL_WANT_WRITE)); |
ccli8 |
1:5ffad9f24d63 | 610 | if (tls_rc < 0) { |
ccli8 |
1:5ffad9f24d63 | 611 | print_mbedtls_error("_tlssocket->send", tls_rc); |
ccli8 |
1:5ffad9f24d63 | 612 | break; |
ccli8 |
1:5ffad9f24d63 | 613 | } |
ccli8 |
1:5ffad9f24d63 | 614 | |
ccli8 |
1:5ffad9f24d63 | 615 | /* Read data out of the socket */ |
ccli8 |
1:5ffad9f24d63 | 616 | offset = 0; |
ccli8 |
1:5ffad9f24d63 | 617 | size_t content_length = 0; |
ccli8 |
1:5ffad9f24d63 | 618 | size_t offset_end = 0; |
ccli8 |
1:5ffad9f24d63 | 619 | char *line_beg = _buffer; |
ccli8 |
1:5ffad9f24d63 | 620 | char *line_end = NULL; |
ccli8 |
1:5ffad9f24d63 | 621 | do { |
ccli8 |
1:5ffad9f24d63 | 622 | tls_rc = _tlssocket->recv((unsigned char *) _buffer + offset, sizeof(_buffer) - offset - 1); |
ccli8 |
1:5ffad9f24d63 | 623 | if (tls_rc > 0) { |
ccli8 |
1:5ffad9f24d63 | 624 | offset += tls_rc; |
ccli8 |
1:5ffad9f24d63 | 625 | } |
ccli8 |
1:5ffad9f24d63 | 626 | |
ccli8 |
1:5ffad9f24d63 | 627 | /* Make it null-terminated */ |
ccli8 |
1:5ffad9f24d63 | 628 | _buffer[offset] = 0; |
ccli8 |
1:5ffad9f24d63 | 629 | |
ccli8 |
1:5ffad9f24d63 | 630 | /* Scan response message |
ccli8 |
1:5ffad9f24d63 | 631 | * |
ccli8 |
1:5ffad9f24d63 | 632 | * 1. A status line which includes the status code and reason message (e.g., HTTP/1.1 200 OK) |
ccli8 |
1:5ffad9f24d63 | 633 | * 2. Response header fields (e.g., Content-Type: text/html) |
ccli8 |
1:5ffad9f24d63 | 634 | * 3. An empty line (\r\n) |
ccli8 |
1:5ffad9f24d63 | 635 | * 4. An optional message body |
ccli8 |
1:5ffad9f24d63 | 636 | */ |
ccli8 |
1:5ffad9f24d63 | 637 | if (! offset_end) { |
ccli8 |
1:5ffad9f24d63 | 638 | line_end = strstr(line_beg, "\r\n"); |
ccli8 |
1:5ffad9f24d63 | 639 | if (line_end) { |
ccli8 |
1:5ffad9f24d63 | 640 | /* Scan status line */ |
ccli8 |
1:5ffad9f24d63 | 641 | if (! _got200) { |
ccli8 |
1:5ffad9f24d63 | 642 | _got200 = strstr(line_beg, HTTPS_OK_STR) != NULL; |
ccli8 |
1:5ffad9f24d63 | 643 | } |
ccli8 |
1:5ffad9f24d63 | 644 | |
ccli8 |
1:5ffad9f24d63 | 645 | /* Scan response header fields for Content-Length |
ccli8 |
1:5ffad9f24d63 | 646 | * |
ccli8 |
1:5ffad9f24d63 | 647 | * NOTE: Assume chunked transfer (Transfer-Encoding: chunked) is not used |
ccli8 |
1:5ffad9f24d63 | 648 | * NOTE: Assume response field name are in lower case |
ccli8 |
1:5ffad9f24d63 | 649 | */ |
ccli8 |
1:5ffad9f24d63 | 650 | if (content_length == 0) { |
ccli8 |
1:5ffad9f24d63 | 651 | sscanf(line_beg, "content-length:%d", &content_length); |
ccli8 |
1:5ffad9f24d63 | 652 | } |
ccli8 |
1:5ffad9f24d63 | 653 | |
ccli8 |
1:5ffad9f24d63 | 654 | /* An empty line indicates end of response header fields */ |
ccli8 |
1:5ffad9f24d63 | 655 | if (line_beg == line_end) { |
ccli8 |
1:5ffad9f24d63 | 656 | offset_end = line_end - _buffer + 2 + content_length; |
ccli8 |
1:5ffad9f24d63 | 657 | } |
ccli8 |
1:5ffad9f24d63 | 658 | |
ccli8 |
1:5ffad9f24d63 | 659 | /* Go to next line */ |
ccli8 |
1:5ffad9f24d63 | 660 | line_beg = line_end + 2; |
ccli8 |
1:5ffad9f24d63 | 661 | line_end = NULL; |
ccli8 |
1:5ffad9f24d63 | 662 | } |
ccli8 |
1:5ffad9f24d63 | 663 | } |
ccli8 |
1:5ffad9f24d63 | 664 | } while ((offset_end == 0 || offset < offset_end) && |
ccli8 |
1:5ffad9f24d63 | 665 | (tls_rc > 0 || tls_rc == MBEDTLS_ERR_SSL_WANT_READ || tls_rc == MBEDTLS_ERR_SSL_WANT_WRITE)); |
ccli8 |
1:5ffad9f24d63 | 666 | if (tls_rc < 0 && |
ccli8 |
1:5ffad9f24d63 | 667 | tls_rc != MBEDTLS_ERR_SSL_WANT_READ && |
ccli8 |
1:5ffad9f24d63 | 668 | tls_rc != MBEDTLS_ERR_SSL_WANT_WRITE) { |
ccli8 |
1:5ffad9f24d63 | 669 | print_mbedtls_error("_tlssocket->read", tls_rc); |
ccli8 |
1:5ffad9f24d63 | 670 | break; |
ccli8 |
1:5ffad9f24d63 | 671 | } |
ccli8 |
1:5ffad9f24d63 | 672 | _bpos = offset; |
ccli8 |
1:5ffad9f24d63 | 673 | |
ccli8 |
1:5ffad9f24d63 | 674 | _buffer[_bpos] = 0; |
ccli8 |
1:5ffad9f24d63 | 675 | |
ccli8 |
1:5ffad9f24d63 | 676 | /* Print status messages */ |
ccli8 |
1:5ffad9f24d63 | 677 | printf("HTTPS: Received %d chars from server\n", _bpos); |
ccli8 |
1:5ffad9f24d63 | 678 | printf("HTTPS: Received 200 OK status ... %s\n", _got200 ? "[OK]" : "[FAIL]"); |
ccli8 |
1:5ffad9f24d63 | 679 | printf("HTTPS: Received message:\n"); |
ccli8 |
1:5ffad9f24d63 | 680 | printf("%s\n", _buffer); |
ccli8 |
1:5ffad9f24d63 | 681 | |
ccli8 |
1:5ffad9f24d63 | 682 | ret = true; |
ccli8 |
1:5ffad9f24d63 | 683 | |
ccli8 |
1:5ffad9f24d63 | 684 | } while (0); |
ccli8 |
1:5ffad9f24d63 | 685 | |
ccli8 |
1:5ffad9f24d63 | 686 | return ret; |
ccli8 |
1:5ffad9f24d63 | 687 | } |
ccli8 |
1:5ffad9f24d63 | 688 | |
ccli8 |
1:5ffad9f24d63 | 689 | protected: |
ccli8 |
1:5ffad9f24d63 | 690 | TLSSocket * _tlssocket; |
ccli8 |
1:5ffad9f24d63 | 691 | |
ccli8 |
1:5ffad9f24d63 | 692 | const char *_domain; /**< Domain name of the HTTPS server */ |
ccli8 |
1:5ffad9f24d63 | 693 | const uint16_t _port; /**< Port number of the HTTPS server */ |
ccli8 |
1:5ffad9f24d63 | 694 | char _buffer[HTTPS_USER_BUFFER_SIZE]; /**< User buffer */ |
ccli8 |
1:5ffad9f24d63 | 695 | }; |
ccli8 |
1:5ffad9f24d63 | 696 | |
ccli8 |
1:5ffad9f24d63 | 697 | #endif // End of AWS_IOT_HTTPS_TEST |
ccli8 |
1:5ffad9f24d63 | 698 | |
ccli8 |
1:5ffad9f24d63 | 699 | int main() { |
ccli8 |
1:5ffad9f24d63 | 700 | |
ccli8 |
1:5ffad9f24d63 | 701 | /* The default 9600 bps is too slow to print full TLS debug info and could |
ccli8 |
1:5ffad9f24d63 | 702 | * cause the other party to time out. */ |
ccli8 |
1:5ffad9f24d63 | 703 | |
ccli8 |
1:5ffad9f24d63 | 704 | printf("\nStarting AWS IoT test\n"); |
ccli8 |
1:5ffad9f24d63 | 705 | |
ccli8 |
1:5ffad9f24d63 | 706 | #if defined(MBED_MAJOR_VERSION) |
ccli8 |
1:5ffad9f24d63 | 707 | printf("Using Mbed OS %d.%d.%d\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); |
ccli8 |
1:5ffad9f24d63 | 708 | #else |
ccli8 |
1:5ffad9f24d63 | 709 | printf("Using Mbed OS from master.\n"); |
ccli8 |
1:5ffad9f24d63 | 710 | #endif |
ccli8 |
1:5ffad9f24d63 | 711 | |
ccli8 |
12:97762a6f55e9 | 712 | NetworkInterface *net = NetworkInterface::get_default_instance(); |
ccli8 |
12:97762a6f55e9 | 713 | if (NULL == net) { |
ccli8 |
1:5ffad9f24d63 | 714 | printf("Connecting to the network failed. See serial output.\n"); |
ccli8 |
1:5ffad9f24d63 | 715 | return 1; |
ccli8 |
1:5ffad9f24d63 | 716 | } |
ccli8 |
12:97762a6f55e9 | 717 | nsapi_error_t status = net->connect(); |
ccli8 |
12:97762a6f55e9 | 718 | if (status != NSAPI_ERROR_OK) { |
ccli8 |
12:97762a6f55e9 | 719 | printf("Connecting to the network failed %d!\n", status); |
ccli8 |
12:97762a6f55e9 | 720 | return -1; |
ccli8 |
12:97762a6f55e9 | 721 | } |
ccli8 |
12:97762a6f55e9 | 722 | printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address()); |
ccli8 |
1:5ffad9f24d63 | 723 | |
ccli8 |
1:5ffad9f24d63 | 724 | #if AWS_IOT_MQTT_TEST |
ccli8 |
12:97762a6f55e9 | 725 | AWS_IoT_MQTT_Test *mqtt_test = new AWS_IoT_MQTT_Test(AWS_IOT_MQTT_SERVER_NAME, AWS_IOT_MQTT_SERVER_PORT, net); |
ccli8 |
1:5ffad9f24d63 | 726 | mqtt_test->start_test(); |
ccli8 |
1:5ffad9f24d63 | 727 | delete mqtt_test; |
ccli8 |
1:5ffad9f24d63 | 728 | #endif // End of AWS_IOT_MQTT_TEST |
ccli8 |
1:5ffad9f24d63 | 729 | |
ccli8 |
1:5ffad9f24d63 | 730 | #if AWS_IOT_HTTPS_TEST |
ccli8 |
12:97762a6f55e9 | 731 | AWS_IoT_HTTPS_Test *https_test = new AWS_IoT_HTTPS_Test(AWS_IOT_HTTPS_SERVER_NAME, AWS_IOT_HTTPS_SERVER_PORT, net); |
ccli8 |
1:5ffad9f24d63 | 732 | https_test->start_test(); |
ccli8 |
1:5ffad9f24d63 | 733 | delete https_test; |
ccli8 |
1:5ffad9f24d63 | 734 | #endif // End of AWS_IOT_HTTPS_TEST |
ccli8 |
1:5ffad9f24d63 | 735 | |
ccli8 |
1:5ffad9f24d63 | 736 | /* Heap usage */ |
ccli8 |
1:5ffad9f24d63 | 737 | mbed_stats_heap_t heap_stats; |
ccli8 |
1:5ffad9f24d63 | 738 | mbed_stats_heap_get(&heap_stats); |
ccli8 |
1:5ffad9f24d63 | 739 | printf("\nCurrent heap size: %lu\n", heap_stats.current_size); |
ccli8 |
3:4c8cccd32e34 | 740 | printf("Max heap size: %lu\n\n", heap_stats.max_size); |
ccli8 |
1:5ffad9f24d63 | 741 | } |