Prepare example app Connect+FW Update

Fork of example-ODINW2-mbed-Cloud-connect by u-blox

Committer:
MarceloSalazar
Date:
Sun Oct 14 19:41:29 2018 +0100
Revision:
7:970a69dc8610
Parent:
2:8cdc2cc26bfb
Add support for Connect+Update

Who changed what in which revision?

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