Add support for Connect and FW Update

Fork of example-Ethernet-mbed-Cloud-connect by ST

Committer:
MarceloSalazar
Date:
Mon Oct 15 01:13:24 2018 +0100
Revision:
14:13ea72e8475c
Parent:
10:8da380655d09
Add support for FW Update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 1 // ----------------------------------------------------------------------------
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 2 // Copyright 2016-2018 ARM Ltd.
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 3 //
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 4 // SPDX-License-Identifier: Apache-2.0
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 5 //
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 6 // Licensed under the Apache License, Version 2.0 (the "License");
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 7 // you may not use this file except in compliance with the License.
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 8 // You may obtain a copy of the License at
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 9 //
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 10 // http://www.apache.org/licenses/LICENSE-2.0
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 11 //
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 12 // Unless required by applicable law or agreed to in writing, software
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 13 // distributed under the License is distributed on an "AS IS" BASIS,
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 15 // See the License for the specific language governing permissions and
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 16 // limitations under the License.
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 17 // ----------------------------------------------------------------------------
MarceloSalazar 14:13ea72e8475c 18 #ifndef MBED_TEST_MODE
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 19
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 20 #include "mbed.h"
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 21 #include "simple-mbed-cloud-client.h"
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 22 #include "FATFileSystem.h"
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 23
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 24 // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads)
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 25 // This is great because things such as network operations are illegal in ISR, so updating a resource in a button's fall() function is not allowed
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 26 EventQueue eventQueue;
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 27
MarceloSalazar 14:13ea72e8475c 28 // Default block device
MarceloSalazar 14:13ea72e8475c 29 BlockDevice* bd = BlockDevice::get_default_instance();
MarceloSalazar 14:13ea72e8475c 30 FATFileSystem fs("sd", bd);
MarceloSalazar 14:13ea72e8475c 31
MarceloSalazar 14:13ea72e8475c 32 // Default network interface object
MarceloSalazar 14:13ea72e8475c 33 NetworkInterface *net;
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 34
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 35 // Declaring pointers for access to Mbed Cloud Client resources outside of main()
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 36 MbedCloudClientResource *button_res;
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 37 MbedCloudClientResource *pattern_res;
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 38
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 39 // This function gets triggered by the timer. It's easy to replace it by an InterruptIn and fall() mode on a real button
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 40 void fake_button_press() {
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 41 int v = button_res->get_value_int() + 1;
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 42
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 43 button_res->set_value(v);
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 44
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 45 printf("Simulated button clicked %d times\n", v);
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 46 }
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 47
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 48 /**
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 49 * PUT handler
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 50 * @param resource The resource that triggered the callback
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 51 * @param newValue Updated value for the resource
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 52 */
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 53 void pattern_updated(MbedCloudClientResource *resource, m2m::String newValue) {
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 54 printf("PUT received, new value: %s\n", newValue.c_str());
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 55 }
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 56
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 57 /**
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 58 * POST handler
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 59 * @param resource The resource that triggered the callback
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 60 * @param buffer If a body was passed to the POST function, this contains the data.
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 61 * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer.
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 62 * @param size Size of the body
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 63 */
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 64 void blink_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) {
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 65 printf("POST received. Going to blink LED pattern: %s\n", pattern_res->get_value().c_str());
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 66
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 67 static DigitalOut augmentedLed(LED1); // LED that is used for blinking the pattern
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 68
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 69 // Parse the pattern string, and toggle the LED in that pattern
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 70 string s = std::string(pattern_res->get_value().c_str());
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 71 size_t i = 0;
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 72 size_t pos = s.find(':');
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 73 while (pos != string::npos) {
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 74 wait_ms(atoi(s.substr(i, pos - i).c_str()));
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 75 augmentedLed = !augmentedLed;
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 76
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 77 i = ++pos;
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 78 pos = s.find(':', pos);
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 79
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 80 if (pos == string::npos) {
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 81 wait_ms(atoi(s.substr(i, s.length()).c_str()));
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 82 augmentedLed = !augmentedLed;
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 83 }
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 84 }
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 85 }
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 86
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 87 /**
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 88 * Notification callback handler
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 89 * @param resource The resource that triggered the callback
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 90 * @param status The delivery status of the notification
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 91 */
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 92 void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) {
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 93 printf("Button notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status);
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 94 }
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 95
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 96 /**
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 97 * Registration callback handler
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 98 * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal)
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 99 */
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 100 void registered(const ConnectorClientEndpointInfo *endpoint) {
MarceloSalazar 14:13ea72e8475c 101 printf("Connected to Pelion Device Management. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str());
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 102 }
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 103
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 104 int main(void) {
MarceloSalazar 14:13ea72e8475c 105 printf("Starting Simple Pelion Device Management Client example\n");
MarceloSalazar 14:13ea72e8475c 106 printf("Connecting to the network...\n");
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 107
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 108 // Connect to the internet (DHCP is expected to be on)
MarceloSalazar 14:13ea72e8475c 109 net = NetworkInterface::get_default_instance();
MarceloSalazar 14:13ea72e8475c 110
MarceloSalazar 14:13ea72e8475c 111 nsapi_error_t status = net->connect();
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 112
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 113 if (status != 0) {
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 114 printf("Connecting to the network failed %d!\n", status);
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 115 return -1;
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 116 }
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 117
MarceloSalazar 14:13ea72e8475c 118 printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address());
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 119
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 120 // SimpleMbedCloudClient handles registering over LwM2M to Mbed Cloud
MarceloSalazar 14:13ea72e8475c 121 SimpleMbedCloudClient client(net, bd, &fs);
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 122 int client_status = client.init();
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 123 if (client_status != 0) {
MarceloSalazar 14:13ea72e8475c 124 printf("Pelion Client initialization failed (%d)\n", client_status);
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 125 return -1;
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 126 }
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 127
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 128 // Creating resources, which can be written or read from the cloud
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 129 button_res = client.create_resource("3200/0/5501", "button_count");
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 130 button_res->set_value(0);
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 131 button_res->methods(M2MMethod::GET);
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 132 button_res->observable(true);
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 133 button_res->attach_notification_callback(button_callback);
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 134
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 135 pattern_res = client.create_resource("3201/0/5853", "blink_pattern");
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 136 pattern_res->set_value("500:500:500:500:500:500:500:500");
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 137 pattern_res->methods(M2MMethod::GET | M2MMethod::PUT);
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 138 pattern_res->attach_put_callback(pattern_updated);
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 139
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 140 MbedCloudClientResource *blink_res = client.create_resource("3201/0/5850", "blink_action");
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 141 blink_res->methods(M2MMethod::POST);
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 142 blink_res->attach_post_callback(blink_callback);
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 143
MarceloSalazar 14:13ea72e8475c 144 printf("Initialized Pelion Client. Registering...\n");
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 145
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 146 // Callback that fires when registering is complete
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 147 client.on_registered(&registered);
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 148
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 149 // Register with Mbed Cloud
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 150 client.register_and_connect();
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 151
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 152 // Placeholder for callback to update local resource when GET comes.
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 153 // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 154 Ticker timer;
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 155 timer.attach(eventQueue.event(&fake_button_press), 5.0);
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 156
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 157 // You can easily run the eventQueue in a separate thread if required
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 158 eventQueue.dispatch_forever();
davwal04@E108444.Emea.Arm.com 6:2d0e0866f2e7 159 }
MarceloSalazar 14:13ea72e8475c 160 #endif