Daiki Kato
/
Pelion-GR-LYCHEE-test
Pelion test
main.cpp@7:826cdcc3c8ec, 2019-02-19 (annotated)
- Committer:
- Osamu Koizumi
- Date:
- Tue Feb 19 11:59:49 2019 +0900
- Revision:
- 7:826cdcc3c8ec
- Parent:
- 4:7c58c47eca55
- Child:
- 8:5f1c0849001c
Set default photo.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MACRUM | 0:6d2053b84a92 | 1 | // ---------------------------------------------------------------------------- |
MACRUM | 0:6d2053b84a92 | 2 | // Copyright 2016-2018 ARM Ltd. |
MACRUM | 0:6d2053b84a92 | 3 | // |
MACRUM | 0:6d2053b84a92 | 4 | // SPDX-License-Identifier: Apache-2.0 |
MACRUM | 0:6d2053b84a92 | 5 | // |
MACRUM | 0:6d2053b84a92 | 6 | // Licensed under the Apache License, Version 2.0 (the "License"); |
MACRUM | 0:6d2053b84a92 | 7 | // you may not use this file except in compliance with the License. |
MACRUM | 0:6d2053b84a92 | 8 | // You may obtain a copy of the License at |
MACRUM | 0:6d2053b84a92 | 9 | // |
MACRUM | 0:6d2053b84a92 | 10 | // http://www.apache.org/licenses/LICENSE-2.0 |
MACRUM | 0:6d2053b84a92 | 11 | // |
MACRUM | 0:6d2053b84a92 | 12 | // Unless required by applicable law or agreed to in writing, software |
MACRUM | 0:6d2053b84a92 | 13 | // distributed under the License is distributed on an "AS IS" BASIS, |
MACRUM | 0:6d2053b84a92 | 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
MACRUM | 0:6d2053b84a92 | 15 | // See the License for the specific language governing permissions and |
MACRUM | 0:6d2053b84a92 | 16 | // limitations under the License. |
MACRUM | 0:6d2053b84a92 | 17 | // ---------------------------------------------------------------------------- |
MACRUM | 0:6d2053b84a92 | 18 | #ifndef MBED_TEST_MODE |
MACRUM | 0:6d2053b84a92 | 19 | |
MACRUM | 0:6d2053b84a92 | 20 | #include "mbed.h" |
MACRUM | 0:6d2053b84a92 | 21 | #include "simple-mbed-cloud-client.h" |
MACRUM | 0:6d2053b84a92 | 22 | #include "FATFileSystem.h" |
MACRUM | 0:6d2053b84a92 | 23 | |
Osamu Koizumi |
4:7c58c47eca55 | 24 | // Include photo and icon jpeg data |
Osamu Koizumi |
4:7c58c47eca55 | 25 | #include "icon.h" |
Osamu Koizumi |
4:7c58c47eca55 | 26 | #include "photo.h" |
Osamu Koizumi |
4:7c58c47eca55 | 27 | |
MACRUM | 0:6d2053b84a92 | 28 | // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads) |
MACRUM | 0:6d2053b84a92 | 29 | // 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 |
MACRUM | 0:6d2053b84a92 | 30 | EventQueue eventQueue; |
MACRUM | 0:6d2053b84a92 | 31 | |
MACRUM | 0:6d2053b84a92 | 32 | // Default block device |
MACRUM | 0:6d2053b84a92 | 33 | BlockDevice *bd = BlockDevice::get_default_instance(); |
MACRUM | 0:6d2053b84a92 | 34 | FATFileSystem fs("fs"); |
MACRUM | 0:6d2053b84a92 | 35 | |
MACRUM | 0:6d2053b84a92 | 36 | // Default network interface object |
MACRUM | 0:6d2053b84a92 | 37 | NetworkInterface *net = NetworkInterface::get_default_instance(); |
MACRUM | 0:6d2053b84a92 | 38 | |
MACRUM | 0:6d2053b84a92 | 39 | InterruptIn btn(USER_BUTTON0); |
MACRUM | 0:6d2053b84a92 | 40 | // Declaring pointers for access to Pelion Device Management Client resources outside of main() |
MACRUM | 0:6d2053b84a92 | 41 | MbedCloudClientResource *button_res; |
MACRUM | 0:6d2053b84a92 | 42 | MbedCloudClientResource *pattern_res; |
Osamu Koizumi |
4:7c58c47eca55 | 43 | MbedCloudClientResource *res_camera_capture; |
MACRUM | 0:6d2053b84a92 | 44 | |
MACRUM | 0:6d2053b84a92 | 45 | void button_press() { |
MACRUM | 0:6d2053b84a92 | 46 | int v = button_res->get_value_int() + 1; |
MACRUM | 0:6d2053b84a92 | 47 | |
MACRUM | 0:6d2053b84a92 | 48 | button_res->set_value(v); |
MACRUM | 0:6d2053b84a92 | 49 | |
Osamu Koizumi |
7:826cdcc3c8ec | 50 | printf("User button clicked %d times\n", v); |
Osamu Koizumi |
7:826cdcc3c8ec | 51 | |
Osamu Koizumi |
7:826cdcc3c8ec | 52 | // Send photo data on button click |
Osamu Koizumi |
4:7c58c47eca55 | 53 | M2MResource* m2m_res = res_camera_capture->get_m2m_resource(); |
Osamu Koizumi |
4:7c58c47eca55 | 54 | if(v % 2 == 0) { |
Osamu Koizumi |
4:7c58c47eca55 | 55 | m2m_res->set_value((const uint8_t *)data_icon, data_icon_len); |
Osamu Koizumi |
4:7c58c47eca55 | 56 | } else { |
Osamu Koizumi |
4:7c58c47eca55 | 57 | m2m_res->set_value((const uint8_t *)data_photo, data_photo_len); |
Osamu Koizumi |
4:7c58c47eca55 | 58 | } |
Osamu Koizumi |
4:7c58c47eca55 | 59 | } |
Osamu Koizumi |
4:7c58c47eca55 | 60 | |
Osamu Koizumi |
4:7c58c47eca55 | 61 | void res_camera_capture_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) |
Osamu Koizumi |
4:7c58c47eca55 | 62 | { |
Osamu Koizumi |
4:7c58c47eca55 | 63 | printf("camera notification, status: %s (%d)\n", |
Osamu Koizumi |
4:7c58c47eca55 | 64 | MbedCloudClientResource::delivery_status_to_string(status), status); |
MACRUM | 0:6d2053b84a92 | 65 | } |
MACRUM | 0:6d2053b84a92 | 66 | |
MACRUM | 0:6d2053b84a92 | 67 | /** |
MACRUM | 0:6d2053b84a92 | 68 | * PUT handler |
MACRUM | 0:6d2053b84a92 | 69 | * @param resource The resource that triggered the callback |
MACRUM | 0:6d2053b84a92 | 70 | * @param newValue Updated value for the resource |
MACRUM | 0:6d2053b84a92 | 71 | */ |
MACRUM | 0:6d2053b84a92 | 72 | void pattern_updated(MbedCloudClientResource *resource, m2m::String newValue) { |
MACRUM | 0:6d2053b84a92 | 73 | printf("PUT received, new value: %s\n", newValue.c_str()); |
MACRUM | 0:6d2053b84a92 | 74 | } |
MACRUM | 0:6d2053b84a92 | 75 | |
MACRUM | 0:6d2053b84a92 | 76 | /** |
MACRUM | 0:6d2053b84a92 | 77 | * POST handler |
MACRUM | 0:6d2053b84a92 | 78 | * @param resource The resource that triggered the callback |
MACRUM | 0:6d2053b84a92 | 79 | * @param buffer If a body was passed to the POST function, this contains the data. |
MACRUM | 0:6d2053b84a92 | 80 | * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer. |
MACRUM | 0:6d2053b84a92 | 81 | * @param size Size of the body |
MACRUM | 0:6d2053b84a92 | 82 | */ |
MACRUM | 0:6d2053b84a92 | 83 | void blink_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) { |
MACRUM | 0:6d2053b84a92 | 84 | printf("POST received. Going to blink LED pattern: %s\n", pattern_res->get_value().c_str()); |
MACRUM | 0:6d2053b84a92 | 85 | |
MACRUM | 0:6d2053b84a92 | 86 | static DigitalOut augmentedLed(LED1); // LED that is used for blinking the pattern |
MACRUM | 0:6d2053b84a92 | 87 | |
MACRUM | 0:6d2053b84a92 | 88 | // Parse the pattern string, and toggle the LED in that pattern |
MACRUM | 0:6d2053b84a92 | 89 | string s = std::string(pattern_res->get_value().c_str()); |
MACRUM | 0:6d2053b84a92 | 90 | size_t i = 0; |
MACRUM | 0:6d2053b84a92 | 91 | size_t pos = s.find(':'); |
MACRUM | 0:6d2053b84a92 | 92 | while (pos != string::npos) { |
MACRUM | 0:6d2053b84a92 | 93 | wait_ms(atoi(s.substr(i, pos - i).c_str())); |
MACRUM | 0:6d2053b84a92 | 94 | augmentedLed = !augmentedLed; |
MACRUM | 0:6d2053b84a92 | 95 | |
MACRUM | 0:6d2053b84a92 | 96 | i = ++pos; |
MACRUM | 0:6d2053b84a92 | 97 | pos = s.find(':', pos); |
MACRUM | 0:6d2053b84a92 | 98 | |
MACRUM | 0:6d2053b84a92 | 99 | if (pos == string::npos) { |
MACRUM | 0:6d2053b84a92 | 100 | wait_ms(atoi(s.substr(i, s.length()).c_str())); |
MACRUM | 0:6d2053b84a92 | 101 | augmentedLed = !augmentedLed; |
MACRUM | 0:6d2053b84a92 | 102 | } |
MACRUM | 0:6d2053b84a92 | 103 | } |
MACRUM | 0:6d2053b84a92 | 104 | } |
MACRUM | 0:6d2053b84a92 | 105 | |
MACRUM | 0:6d2053b84a92 | 106 | /** |
MACRUM | 0:6d2053b84a92 | 107 | * Notification callback handler |
MACRUM | 0:6d2053b84a92 | 108 | * @param resource The resource that triggered the callback |
MACRUM | 0:6d2053b84a92 | 109 | * @param status The delivery status of the notification |
MACRUM | 0:6d2053b84a92 | 110 | */ |
MACRUM | 0:6d2053b84a92 | 111 | void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) { |
MACRUM | 0:6d2053b84a92 | 112 | printf("Button notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status); |
MACRUM | 0:6d2053b84a92 | 113 | } |
MACRUM | 0:6d2053b84a92 | 114 | |
MACRUM | 0:6d2053b84a92 | 115 | /** |
MACRUM | 0:6d2053b84a92 | 116 | * Registration callback handler |
MACRUM | 0:6d2053b84a92 | 117 | * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal) |
MACRUM | 0:6d2053b84a92 | 118 | */ |
MACRUM | 0:6d2053b84a92 | 119 | void registered(const ConnectorClientEndpointInfo *endpoint) { |
MACRUM | 0:6d2053b84a92 | 120 | printf("Connected to Pelion Device Management. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str()); |
Osamu Koizumi |
7:826cdcc3c8ec | 121 | // Once registered, send default photo. |
Osamu Koizumi |
7:826cdcc3c8ec | 122 | M2MResource* m2m_res = res_camera_capture->get_m2m_resource(); |
Osamu Koizumi |
7:826cdcc3c8ec | 123 | m2m_res->set_value((const uint8_t *)data_icon, data_icon_len); |
MACRUM | 0:6d2053b84a92 | 124 | } |
MACRUM | 0:6d2053b84a92 | 125 | |
MACRUM | 0:6d2053b84a92 | 126 | int main(void) { |
MACRUM | 0:6d2053b84a92 | 127 | printf("Starting Simple Pelion Device Management Client example\n"); |
MACRUM | 0:6d2053b84a92 | 128 | printf("Connecting to the network...\n"); |
MACRUM | 0:6d2053b84a92 | 129 | |
MACRUM | 0:6d2053b84a92 | 130 | // Connect to the internet (DHCP is expected to be on) |
MACRUM | 0:6d2053b84a92 | 131 | nsapi_error_t status = net->connect(); |
MACRUM | 0:6d2053b84a92 | 132 | |
MACRUM | 0:6d2053b84a92 | 133 | if (status != NSAPI_ERROR_OK) { |
MACRUM | 0:6d2053b84a92 | 134 | printf("Connecting to the network failed %d!\n", status); |
MACRUM | 0:6d2053b84a92 | 135 | return -1; |
MACRUM | 0:6d2053b84a92 | 136 | } |
MACRUM | 0:6d2053b84a92 | 137 | |
MACRUM | 0:6d2053b84a92 | 138 | printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address()); |
MACRUM | 0:6d2053b84a92 | 139 | |
MACRUM | 0:6d2053b84a92 | 140 | // SimpleMbedCloudClient handles registering over LwM2M to Pelion Device Management |
MACRUM | 0:6d2053b84a92 | 141 | SimpleMbedCloudClient client(net, bd, &fs); |
MACRUM | 0:6d2053b84a92 | 142 | int client_status = client.init(); |
MACRUM | 0:6d2053b84a92 | 143 | if (client_status != 0) { |
MACRUM | 0:6d2053b84a92 | 144 | printf("Pelion Client initialization failed (%d)\n", client_status); |
MACRUM | 0:6d2053b84a92 | 145 | return -1; |
MACRUM | 0:6d2053b84a92 | 146 | } |
MACRUM | 0:6d2053b84a92 | 147 | |
MACRUM | 0:6d2053b84a92 | 148 | // Creating resources, which can be written or read from the cloud |
MACRUM | 0:6d2053b84a92 | 149 | button_res = client.create_resource("3200/0/5501", "button_count"); |
MACRUM | 0:6d2053b84a92 | 150 | button_res->set_value(0); |
MACRUM | 0:6d2053b84a92 | 151 | button_res->methods(M2MMethod::GET); |
MACRUM | 0:6d2053b84a92 | 152 | button_res->observable(true); |
MACRUM | 0:6d2053b84a92 | 153 | button_res->attach_notification_callback(button_callback); |
MACRUM | 0:6d2053b84a92 | 154 | |
MACRUM | 0:6d2053b84a92 | 155 | pattern_res = client.create_resource("3201/0/5853", "blink_pattern"); |
MACRUM | 0:6d2053b84a92 | 156 | pattern_res->set_value("500:500:500:500:500:500:500:500"); |
MACRUM | 0:6d2053b84a92 | 157 | pattern_res->methods(M2MMethod::GET | M2MMethod::PUT); |
MACRUM | 0:6d2053b84a92 | 158 | pattern_res->attach_put_callback(pattern_updated); |
MACRUM | 0:6d2053b84a92 | 159 | |
Osamu Koizumi |
4:7c58c47eca55 | 160 | res_camera_capture = client.create_resource("3200/0/4014", "CameraCapture"); |
Osamu Koizumi |
4:7c58c47eca55 | 161 | res_camera_capture->set_value(0); |
Osamu Koizumi |
4:7c58c47eca55 | 162 | res_camera_capture->methods(M2MMethod::GET); |
Osamu Koizumi |
4:7c58c47eca55 | 163 | res_camera_capture->observable(true); |
Osamu Koizumi |
4:7c58c47eca55 | 164 | res_camera_capture->attach_notification_callback(res_camera_capture_callback); |
Osamu Koizumi |
4:7c58c47eca55 | 165 | |
MACRUM | 0:6d2053b84a92 | 166 | MbedCloudClientResource *blink_res = client.create_resource("3201/0/5850", "blink_action"); |
MACRUM | 0:6d2053b84a92 | 167 | blink_res->methods(M2MMethod::POST); |
MACRUM | 0:6d2053b84a92 | 168 | blink_res->attach_post_callback(blink_callback); |
MACRUM | 0:6d2053b84a92 | 169 | |
MACRUM | 0:6d2053b84a92 | 170 | printf("Initialized Pelion Client. Registering...\n"); |
MACRUM | 0:6d2053b84a92 | 171 | |
MACRUM | 0:6d2053b84a92 | 172 | // Callback that fires when registering is complete |
MACRUM | 0:6d2053b84a92 | 173 | client.on_registered(®istered); |
MACRUM | 0:6d2053b84a92 | 174 | |
MACRUM | 0:6d2053b84a92 | 175 | // Register with Pelion Device Management |
MACRUM | 0:6d2053b84a92 | 176 | client.register_and_connect(); |
MACRUM | 0:6d2053b84a92 | 177 | |
MACRUM | 0:6d2053b84a92 | 178 | // Setup the button |
MACRUM | 0:6d2053b84a92 | 179 | btn.mode(PullUp); |
MACRUM | 0:6d2053b84a92 | 180 | |
MACRUM | 0:6d2053b84a92 | 181 | // The button fall handler is placed in the event queue so it will run in |
MACRUM | 0:6d2053b84a92 | 182 | // thread context instead of ISR context, which allows safely updating the cloud resource |
MACRUM | 0:6d2053b84a92 | 183 | btn.fall(eventQueue.event(&button_press)); |
MACRUM | 0:6d2053b84a92 | 184 | |
MACRUM | 0:6d2053b84a92 | 185 | // You can easily run the eventQueue in a separate thread if required |
MACRUM | 0:6d2053b84a92 | 186 | eventQueue.dispatch_forever(); |
MACRUM | 0:6d2053b84a92 | 187 | } |
MACRUM | 0:6d2053b84a92 | 188 | #endif |