Prepare example app Connect+FW Update

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

Committer:
davwal04@E108444.Emea.Arm.com
Date:
Thu Jun 21 09:22:54 2018 +0100
Revision:
2:8cdc2cc26bfb
Parent:
0:aef13a373320
Child:
7:970a69dc8610
Updating to SMCC 1.3.3

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 // ----------------------------------------------------------------------------
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 18
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 "SDBlockDevice.h"
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 22 #include "FATFileSystem.h"
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 23 #include "OdinWiFiInterface.h"
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 24
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 25 // 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 26 // 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 27 EventQueue eventQueue;
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 28
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 29 // Storage implementation definition, currently using SDBlockDevice (SPI flash, DataFlash, and internal flash are also available)
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 30 SDBlockDevice sd(D11, D12, D13, D9);
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 31 FATFileSystem fs("sd", &sd);
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 32
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 33 // Declaring pointers for access to Mbed Cloud Client resources outside of main()
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 34 MbedCloudClientResource *button_res;
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 35 MbedCloudClientResource *pattern_res;
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 36
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 37 // 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 38 void fake_button_press() {
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 39 int v = button_res->get_value_int() + 1;
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 40
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 41 button_res->set_value(v);
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 42
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 43 printf("Simulated button clicked %d times\n", v);
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 44 }
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 45
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 46 /**
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 47 * PUT handler
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 48 * @param resource The resource that triggered the callback
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 49 * @param newValue Updated value for the resource
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 50 */
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 51 void pattern_updated(MbedCloudClientResource *resource, m2m::String newValue) {
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 52 printf("PUT received, new value: %s\n", newValue.c_str());
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 53 }
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 54
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 55 /**
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 56 * POST handler
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 57 * @param resource The resource that triggered the callback
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 58 * @param buffer If a body was passed to the POST function, this contains the data.
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 59 * 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 60 * @param size Size of the body
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 61 */
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 62 void blink_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) {
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 63 printf("POST received. Going to blink LED pattern: %s\n", pattern_res->get_value().c_str());
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 64
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 65 static DigitalOut augmentedLed(LED1); // LED that is used for blinking the pattern
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 66
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 67 // Parse the pattern string, and toggle the LED in that pattern
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 68 string s = std::string(pattern_res->get_value().c_str());
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 69 size_t i = 0;
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 70 size_t pos = s.find(':');
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 71 while (pos != string::npos) {
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 72 wait_ms(atoi(s.substr(i, pos - i).c_str()));
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 73 augmentedLed = !augmentedLed;
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 74
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 75 i = ++pos;
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 76 pos = s.find(':', pos);
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 77
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 78 if (pos == string::npos) {
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 79 wait_ms(atoi(s.substr(i, s.length()).c_str()));
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 80 augmentedLed = !augmentedLed;
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 81 }
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 * Notification callback handler
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 87 * @param resource The resource that triggered the callback
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 88 * @param status The delivery status of the notification
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 89 */
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 90 void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) {
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 91 printf("Button notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status);
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 92 }
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 93
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 94 /**
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 95 * Registration callback handler
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 96 * @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 97 */
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 98 void registered(const ConnectorClientEndpointInfo *endpoint) {
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 99 printf("Connected to Mbed Cloud. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str());
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 100 }
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 101
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 102 int main(void) {
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 103 printf("Starting Simple Mbed Cloud Client example\n");
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 104 printf("Connecting to the network using Odin WiFi...\n");
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 105
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 106 // Connect to the internet (DHCP is expected to be on)
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 107 OdinWiFiInterface net;
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 108 nsapi_error_t status = net.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 109
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 110 if (status != 0) {
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 111 printf("Connecting to the network failed %d!\n", status);
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 112 return -1;
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 113 }
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 114
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 115 printf("Connected to the network successfully. IP address: %s\n", net.get_ip_address());
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 116
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 117 // SimpleMbedCloudClient handles registering over LwM2M to Mbed Cloud
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 118 SimpleMbedCloudClient client(&net, &sd, &fs);
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 119 int client_status = client.init();
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 120 if (client_status != 0) {
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 121 printf("Initializing Mbed Cloud Client failed (%d)\n", client_status);
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 122 return -1;
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 123 }
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 124
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 125 // Creating resources, which can be written or read from the cloud
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 126 button_res = client.create_resource("3200/0/5501", "button_count");
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 127 button_res->set_value(0);
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 128 button_res->methods(M2MMethod::GET);
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 129 button_res->observable(true);
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 130 button_res->attach_notification_callback(button_callback);
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 131
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 132 pattern_res = client.create_resource("3201/0/5853", "blink_pattern");
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 133 pattern_res->set_value("500:500:500:500:500:500:500:500");
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 134 pattern_res->methods(M2MMethod::GET | M2MMethod::PUT);
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 135 pattern_res->attach_put_callback(pattern_updated);
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 136
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 137 MbedCloudClientResource *blink_res = client.create_resource("3201/0/5850", "blink_action");
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 138 blink_res->methods(M2MMethod::POST);
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 139 blink_res->attach_post_callback(blink_callback);
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 140
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 141 printf("Initialized Mbed Cloud Client. Registering...\n");
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 142
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 143 // Callback that fires when registering is complete
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 144 client.on_registered(&registered);
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 145
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 146 // Register with Mbed Cloud
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 147 client.register_and_connect();
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 148
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 149 // Placeholder for callback to update local resource when GET comes.
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 150 // 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 151 Ticker timer;
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 152 timer.attach(eventQueue.event(&fake_button_press), 5.0);
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 153
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 154 // You can easily run the eventQueue in a separate thread if required
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 155 eventQueue.dispatch_forever();
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 156 }
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 157
davwal04@E108444.Emea.Arm.com 2:8cdc2cc26bfb 158