Osamu Koizumi
/
Pelion-workshop-Grove-sensors
Pelion workshop with Grove sensors.
main.cpp@6:254a7e7fbef1, 2018-02-15 (annotated)
- Committer:
- bridadan
- Date:
- Thu Feb 15 10:14:35 2018 -0600
- Revision:
- 6:254a7e7fbef1
- Parent:
- 5:c18fab181ede
- Child:
- 8:ace9e5de6491
Postponing timer until after registration complete
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MarceloSalazar | 0:e13a8a944e25 | 1 | // ---------------------------------------------------------------------------- |
MarceloSalazar | 0:e13a8a944e25 | 2 | // Copyright 2016-2017 ARM Ltd. |
MarceloSalazar | 0:e13a8a944e25 | 3 | // |
MarceloSalazar | 0:e13a8a944e25 | 4 | // SPDX-License-Identifier: Apache-2.0 |
MarceloSalazar | 0:e13a8a944e25 | 5 | // |
MarceloSalazar | 0:e13a8a944e25 | 6 | // Licensed under the Apache License, Version 2.0 (the "License"); |
MarceloSalazar | 0:e13a8a944e25 | 7 | // you may not use this file except in compliance with the License. |
MarceloSalazar | 0:e13a8a944e25 | 8 | // You may obtain a copy of the License at |
MarceloSalazar | 0:e13a8a944e25 | 9 | // |
MarceloSalazar | 0:e13a8a944e25 | 10 | // http://www.apache.org/licenses/LICENSE-2.0 |
MarceloSalazar | 0:e13a8a944e25 | 11 | // |
MarceloSalazar | 0:e13a8a944e25 | 12 | // Unless required by applicable law or agreed to in writing, software |
MarceloSalazar | 0:e13a8a944e25 | 13 | // distributed under the License is distributed on an "AS IS" BASIS, |
MarceloSalazar | 0:e13a8a944e25 | 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
MarceloSalazar | 0:e13a8a944e25 | 15 | // See the License for the specific language governing permissions and |
MarceloSalazar | 0:e13a8a944e25 | 16 | // limitations under the License. |
MarceloSalazar | 0:e13a8a944e25 | 17 | // ---------------------------------------------------------------------------- |
MarceloSalazar | 0:e13a8a944e25 | 18 | |
MarceloSalazar | 1:1ccf36276cd3 | 19 | #include "mbed.h" |
MarceloSalazar | 1:1ccf36276cd3 | 20 | #include "mbed-trace/mbed_trace.h" |
MarceloSalazar | 1:1ccf36276cd3 | 21 | #include "mbed-trace-helper.h" |
MarceloSalazar | 0:e13a8a944e25 | 22 | #include "simple-mbed-cloud-client.h" |
MarceloSalazar | 1:1ccf36276cd3 | 23 | #include "key-config-manager/kcm_status.h" |
MarceloSalazar | 1:1ccf36276cd3 | 24 | #include "key-config-manager/key_config_manager.h" |
MarceloSalazar | 1:1ccf36276cd3 | 25 | #include "SDBlockDevice.h" |
MarceloSalazar | 1:1ccf36276cd3 | 26 | #include "FATFileSystem.h" |
MarceloSalazar | 1:1ccf36276cd3 | 27 | #include "EthernetInterface.h" |
MarceloSalazar | 1:1ccf36276cd3 | 28 | |
MarceloSalazar | 4:a107dae867fb | 29 | // Placeholder to hardware that trigger events (timer, button, etc) |
MarceloSalazar | 4:a107dae867fb | 30 | Ticker timer; |
MarceloSalazar | 0:e13a8a944e25 | 31 | |
bridadan | 5:c18fab181ede | 32 | // Placeholder for storage |
bridadan | 5:c18fab181ede | 33 | SDBlockDevice sd(PTE3, PTE1, PTE2, PTE4); |
bridadan | 5:c18fab181ede | 34 | FATFileSystem fs("sd"); |
bridadan | 5:c18fab181ede | 35 | |
MarceloSalazar | 0:e13a8a944e25 | 36 | // Pointers to the resources that will be created in main_application(). |
MarceloSalazar | 1:1ccf36276cd3 | 37 | static MbedCloudClientResource* pattern_ptr; |
MarceloSalazar | 0:e13a8a944e25 | 38 | |
MarceloSalazar | 0:e13a8a944e25 | 39 | // Pointer to mbedClient, used for calling close function. |
MarceloSalazar | 0:e13a8a944e25 | 40 | static SimpleMbedCloudClient *client; |
MarceloSalazar | 0:e13a8a944e25 | 41 | |
MarceloSalazar | 1:1ccf36276cd3 | 42 | static bool button_pressed = false; |
MarceloSalazar | 1:1ccf36276cd3 | 43 | |
MarceloSalazar | 1:1ccf36276cd3 | 44 | void button_press() { |
MarceloSalazar | 1:1ccf36276cd3 | 45 | button_pressed = true; |
MarceloSalazar | 1:1ccf36276cd3 | 46 | } |
MarceloSalazar | 1:1ccf36276cd3 | 47 | |
MarceloSalazar | 1:1ccf36276cd3 | 48 | void pattern_updated(const char *) { |
bridadan | 5:c18fab181ede | 49 | printf("PUT received, new value: %s\n", pattern_ptr->get_value().c_str()); |
MarceloSalazar | 2:6ed27f413b30 | 50 | // Placeholder for PUT action |
MarceloSalazar | 0:e13a8a944e25 | 51 | } |
MarceloSalazar | 0:e13a8a944e25 | 52 | |
MarceloSalazar | 0:e13a8a944e25 | 53 | void blink_callback(void *) { |
bridadan | 5:c18fab181ede | 54 | String pattern_str = pattern_ptr->get_value(); |
bridadan | 5:c18fab181ede | 55 | const char *pattern = pattern_str.c_str(); |
MarceloSalazar | 2:6ed27f413b30 | 56 | printf("POST received. LED pattern = %s\n", pattern); |
MarceloSalazar | 2:6ed27f413b30 | 57 | // Placeholder for POST action |
MarceloSalazar | 0:e13a8a944e25 | 58 | // The pattern is something like 500:200:500, so parse that. |
MarceloSalazar | 0:e13a8a944e25 | 59 | } |
MarceloSalazar | 0:e13a8a944e25 | 60 | |
MarceloSalazar | 2:6ed27f413b30 | 61 | void button_callback(const M2MBase& object, const NoticationDeliveryStatus status) |
MarceloSalazar | 0:e13a8a944e25 | 62 | { |
MarceloSalazar | 2:6ed27f413b30 | 63 | printf("Button notification. Callback: (%s)\n", object.uri_path()); |
MarceloSalazar | 2:6ed27f413b30 | 64 | // Placeholder for GET |
MarceloSalazar | 0:e13a8a944e25 | 65 | } |
MarceloSalazar | 0:e13a8a944e25 | 66 | |
MarceloSalazar | 0:e13a8a944e25 | 67 | |
MarceloSalazar | 0:e13a8a944e25 | 68 | int main(void) |
MarceloSalazar | 0:e13a8a944e25 | 69 | { |
MarceloSalazar | 2:6ed27f413b30 | 70 | // Requires DAPLink 245+ (https://github.com/ARMmbed/DAPLink/pull/364) |
MarceloSalazar | 2:6ed27f413b30 | 71 | // Older versions: workaround to prevent possible deletion of credentials: |
MarceloSalazar | 0:e13a8a944e25 | 72 | wait(2); |
MarceloSalazar | 0:e13a8a944e25 | 73 | |
MarceloSalazar | 1:1ccf36276cd3 | 74 | // Misc OS setup |
MarceloSalazar | 1:1ccf36276cd3 | 75 | srand(time(NULL)); |
MarceloSalazar | 1:1ccf36276cd3 | 76 | |
MarceloSalazar | 2:6ed27f413b30 | 77 | // Placeholder for network |
MarceloSalazar | 1:1ccf36276cd3 | 78 | EthernetInterface net; |
MarceloSalazar | 2:6ed27f413b30 | 79 | |
MarceloSalazar | 1:1ccf36276cd3 | 80 | printf("Start Simple Mbed Cloud Client\n"); |
MarceloSalazar | 1:1ccf36276cd3 | 81 | |
MarceloSalazar | 1:1ccf36276cd3 | 82 | // Initialize SD card |
MarceloSalazar | 1:1ccf36276cd3 | 83 | int status = sd.init(); |
MarceloSalazar | 1:1ccf36276cd3 | 84 | if (status != BD_ERROR_OK) { |
MarceloSalazar | 1:1ccf36276cd3 | 85 | printf("Failed to init SD card\r\n"); |
MarceloSalazar | 1:1ccf36276cd3 | 86 | return -1; |
MarceloSalazar | 1:1ccf36276cd3 | 87 | } |
MarceloSalazar | 0:e13a8a944e25 | 88 | |
MarceloSalazar | 1:1ccf36276cd3 | 89 | // Mount the file system (reformatting on failure) |
MarceloSalazar | 1:1ccf36276cd3 | 90 | status = fs.mount(&sd); |
MarceloSalazar | 1:1ccf36276cd3 | 91 | if (status) { |
MarceloSalazar | 1:1ccf36276cd3 | 92 | printf("Failed to mount FAT file system, reformatting...\r\n"); |
MarceloSalazar | 1:1ccf36276cd3 | 93 | status = fs.reformat(&sd); |
MarceloSalazar | 1:1ccf36276cd3 | 94 | if (status) { |
MarceloSalazar | 1:1ccf36276cd3 | 95 | printf("Failed to reformat FAT file system\r\n"); |
MarceloSalazar | 1:1ccf36276cd3 | 96 | return -1; |
MarceloSalazar | 1:1ccf36276cd3 | 97 | } else { |
MarceloSalazar | 1:1ccf36276cd3 | 98 | printf("Reformat and mount complete\r\n"); |
MarceloSalazar | 1:1ccf36276cd3 | 99 | } |
MarceloSalazar | 1:1ccf36276cd3 | 100 | } |
MarceloSalazar | 1:1ccf36276cd3 | 101 | |
MarceloSalazar | 1:1ccf36276cd3 | 102 | printf("Connecting to the network using Ethernet...\n"); |
MarceloSalazar | 1:1ccf36276cd3 | 103 | |
MarceloSalazar | 1:1ccf36276cd3 | 104 | status = net.connect(); |
MarceloSalazar | 1:1ccf36276cd3 | 105 | if (status) { |
MarceloSalazar | 1:1ccf36276cd3 | 106 | printf("Connection to Network Failed %d!\n", status); |
MarceloSalazar | 1:1ccf36276cd3 | 107 | return -1; |
MarceloSalazar | 1:1ccf36276cd3 | 108 | } else { |
MarceloSalazar | 1:1ccf36276cd3 | 109 | const char *ip_addr = net.get_ip_address(); |
MarceloSalazar | 1:1ccf36276cd3 | 110 | printf("Connected successfully\n"); |
MarceloSalazar | 1:1ccf36276cd3 | 111 | printf("IP address %s\n", ip_addr); |
MarceloSalazar | 1:1ccf36276cd3 | 112 | } |
MarceloSalazar | 1:1ccf36276cd3 | 113 | |
MarceloSalazar | 1:1ccf36276cd3 | 114 | SimpleMbedCloudClient mbedClient(&net); |
MarceloSalazar | 0:e13a8a944e25 | 115 | // Save pointer to mbedClient so that other functions can access it. |
MarceloSalazar | 0:e13a8a944e25 | 116 | client = &mbedClient; |
MarceloSalazar | 0:e13a8a944e25 | 117 | |
MarceloSalazar | 1:1ccf36276cd3 | 118 | status = mbedClient.init(); |
MarceloSalazar | 1:1ccf36276cd3 | 119 | if (status) { |
MarceloSalazar | 1:1ccf36276cd3 | 120 | return -1; |
MarceloSalazar | 1:1ccf36276cd3 | 121 | } |
MarceloSalazar | 1:1ccf36276cd3 | 122 | |
MarceloSalazar | 0:e13a8a944e25 | 123 | printf("Client initialized\r\n"); |
MarceloSalazar | 0:e13a8a944e25 | 124 | |
MarceloSalazar | 1:1ccf36276cd3 | 125 | // Mbed Cloud Client resource setup |
MarceloSalazar | 1:1ccf36276cd3 | 126 | MbedCloudClientResource *button = mbedClient.create_resource("3200/0/5501", "button_resource"); |
MarceloSalazar | 1:1ccf36276cd3 | 127 | button->set_value("0"); |
MarceloSalazar | 1:1ccf36276cd3 | 128 | button->methods(M2MMethod::GET); |
MarceloSalazar | 1:1ccf36276cd3 | 129 | button->observable(true); |
bridadan | 5:c18fab181ede | 130 | button->attach_notification_callback(button_callback); |
MarceloSalazar | 0:e13a8a944e25 | 131 | |
MarceloSalazar | 1:1ccf36276cd3 | 132 | MbedCloudClientResource *pattern = mbedClient.create_resource("3201/0/5853", "pattern_resource"); |
MarceloSalazar | 1:1ccf36276cd3 | 133 | pattern->set_value("500:500:500:500"); |
MarceloSalazar | 1:1ccf36276cd3 | 134 | pattern->methods(M2MMethod::GET | M2MMethod::PUT); |
bridadan | 5:c18fab181ede | 135 | pattern->attach_put_callback(pattern_updated); |
MarceloSalazar | 1:1ccf36276cd3 | 136 | pattern_ptr = pattern; |
MarceloSalazar | 0:e13a8a944e25 | 137 | |
MarceloSalazar | 1:1ccf36276cd3 | 138 | MbedCloudClientResource *blink = mbedClient.create_resource("3201/0/5850", "blink_resource"); |
MarceloSalazar | 1:1ccf36276cd3 | 139 | blink->methods(M2MMethod::POST); |
bridadan | 5:c18fab181ede | 140 | blink->attach_post_callback(blink_callback); |
MarceloSalazar | 0:e13a8a944e25 | 141 | |
MarceloSalazar | 0:e13a8a944e25 | 142 | mbedClient.register_and_connect(); |
MarceloSalazar | 0:e13a8a944e25 | 143 | |
bridadan | 6:254a7e7fbef1 | 144 | // Wait for client to finish registering |
bridadan | 6:254a7e7fbef1 | 145 | while (!mbedClient.is_client_registered()) { |
bridadan | 6:254a7e7fbef1 | 146 | wait_ms(100); |
bridadan | 6:254a7e7fbef1 | 147 | } |
bridadan | 6:254a7e7fbef1 | 148 | |
bridadan | 6:254a7e7fbef1 | 149 | // Placeholder for callback to update local resource when GET comes. |
bridadan | 6:254a7e7fbef1 | 150 | timer.attach(&button_press, 5.0); |
bridadan | 6:254a7e7fbef1 | 151 | |
MarceloSalazar | 0:e13a8a944e25 | 152 | // Check if client is registering or registered, if true sleep and repeat. |
MarceloSalazar | 0:e13a8a944e25 | 153 | while (mbedClient.is_register_called()) { |
MarceloSalazar | 0:e13a8a944e25 | 154 | static int button_count = 0; |
MarceloSalazar | 1:1ccf36276cd3 | 155 | wait_ms(100); |
MarceloSalazar | 1:1ccf36276cd3 | 156 | |
MarceloSalazar | 1:1ccf36276cd3 | 157 | if (button_pressed) { |
MarceloSalazar | 1:1ccf36276cd3 | 158 | button_pressed = false; |
MarceloSalazar | 4:a107dae867fb | 159 | printf("Simulated button clicked %d times\r\n", ++button_count); |
MarceloSalazar | 1:1ccf36276cd3 | 160 | button->set_value(button_count); |
MarceloSalazar | 0:e13a8a944e25 | 161 | } |
MarceloSalazar | 0:e13a8a944e25 | 162 | } |
MarceloSalazar | 0:e13a8a944e25 | 163 | |
MarceloSalazar | 0:e13a8a944e25 | 164 | // Client unregistered, exit program. |
MarceloSalazar | 0:e13a8a944e25 | 165 | return 0; |
MarceloSalazar | 0:e13a8a944e25 | 166 | } |