The WiFi enabled version of pelion-example-common. Later, this may be merged to pelion-example-common.

Committer:
Andrew Chong
Date:
Fri Dec 14 18:04:23 2018 +0900
Revision:
0:fdc18ffe96a4
Child:
1:e0ba512426a7
The first version to enable Pelion with SDT.

Who changed what in which revision?

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