Osamu Koizumi
/
Pelion-workshop-Grove-sensors
Pelion workshop with Grove sensors.
main.cpp@0:e13a8a944e25, 2018-02-13 (annotated)
- Committer:
- MarceloSalazar
- Date:
- Tue Feb 13 10:07:23 2018 +0000
- Revision:
- 0:e13a8a944e25
- Child:
- 1:1ccf36276cd3
First version
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 | 0:e13a8a944e25 | 19 | #include "simple-mbed-cloud-client.h" |
MarceloSalazar | 0:e13a8a944e25 | 20 | #include "mbed.h" |
MarceloSalazar | 0:e13a8a944e25 | 21 | |
MarceloSalazar | 0:e13a8a944e25 | 22 | // Pointers to the resources that will be created in main_application(). |
MarceloSalazar | 0:e13a8a944e25 | 23 | static M2MResource* button_res; |
MarceloSalazar | 0:e13a8a944e25 | 24 | static M2MResource* pattern_res; |
MarceloSalazar | 0:e13a8a944e25 | 25 | static M2MResource* blink_res; |
MarceloSalazar | 0:e13a8a944e25 | 26 | |
MarceloSalazar | 0:e13a8a944e25 | 27 | // Pointer to mbedClient, used for calling close function. |
MarceloSalazar | 0:e13a8a944e25 | 28 | static SimpleMbedCloudClient *client; |
MarceloSalazar | 0:e13a8a944e25 | 29 | |
MarceloSalazar | 0:e13a8a944e25 | 30 | void pattern_updated(const char *) |
MarceloSalazar | 0:e13a8a944e25 | 31 | { |
MarceloSalazar | 0:e13a8a944e25 | 32 | printf("PUT received, new value: %s\n", pattern_res->get_value_string().c_str()); |
MarceloSalazar | 0:e13a8a944e25 | 33 | } |
MarceloSalazar | 0:e13a8a944e25 | 34 | |
MarceloSalazar | 0:e13a8a944e25 | 35 | void blink_callback(void *) { |
MarceloSalazar | 0:e13a8a944e25 | 36 | String pattern_string = pattern_res->get_value_string(); |
MarceloSalazar | 0:e13a8a944e25 | 37 | const char *pattern = pattern_string.c_str(); |
MarceloSalazar | 0:e13a8a944e25 | 38 | printf("LED pattern = %s\n", pattern); |
MarceloSalazar | 0:e13a8a944e25 | 39 | // The pattern is something like 500:200:500, so parse that. |
MarceloSalazar | 0:e13a8a944e25 | 40 | // LED blinking is done while parsing. |
MarceloSalazar | 0:e13a8a944e25 | 41 | toggle_led(); |
MarceloSalazar | 0:e13a8a944e25 | 42 | while (*pattern != '\0') { |
MarceloSalazar | 0:e13a8a944e25 | 43 | // Wait for requested time. |
MarceloSalazar | 0:e13a8a944e25 | 44 | do_wait(atoi(pattern)); |
MarceloSalazar | 0:e13a8a944e25 | 45 | toggle_led(); |
MarceloSalazar | 0:e13a8a944e25 | 46 | // Search for next value. |
MarceloSalazar | 0:e13a8a944e25 | 47 | pattern = strchr(pattern, ':'); |
MarceloSalazar | 0:e13a8a944e25 | 48 | if(!pattern) { |
MarceloSalazar | 0:e13a8a944e25 | 49 | break; // while |
MarceloSalazar | 0:e13a8a944e25 | 50 | } |
MarceloSalazar | 0:e13a8a944e25 | 51 | pattern++; |
MarceloSalazar | 0:e13a8a944e25 | 52 | } |
MarceloSalazar | 0:e13a8a944e25 | 53 | led_off(); |
MarceloSalazar | 0:e13a8a944e25 | 54 | } |
MarceloSalazar | 0:e13a8a944e25 | 55 | |
MarceloSalazar | 0:e13a8a944e25 | 56 | void button_notification_status_callback(const M2MBase& object, const NoticationDeliveryStatus status) |
MarceloSalazar | 0:e13a8a944e25 | 57 | { |
MarceloSalazar | 0:e13a8a944e25 | 58 | switch(status) { |
MarceloSalazar | 0:e13a8a944e25 | 59 | case NOTIFICATION_STATUS_BUILD_ERROR: |
MarceloSalazar | 0:e13a8a944e25 | 60 | printf("Notification callback: (%s) error when building CoAP message\n", object.uri_path()); |
MarceloSalazar | 0:e13a8a944e25 | 61 | break; |
MarceloSalazar | 0:e13a8a944e25 | 62 | case NOTIFICATION_STATUS_RESEND_QUEUE_FULL: |
MarceloSalazar | 0:e13a8a944e25 | 63 | printf("Notification callback: (%s) CoAP resend queue full\n", object.uri_path()); |
MarceloSalazar | 0:e13a8a944e25 | 64 | break; |
MarceloSalazar | 0:e13a8a944e25 | 65 | case NOTIFICATION_STATUS_SENT: |
MarceloSalazar | 0:e13a8a944e25 | 66 | printf("Notification callback: (%s) Notification sent to server\n", object.uri_path()); |
MarceloSalazar | 0:e13a8a944e25 | 67 | break; |
MarceloSalazar | 0:e13a8a944e25 | 68 | case NOTIFICATION_STATUS_DELIVERED: |
MarceloSalazar | 0:e13a8a944e25 | 69 | printf("Notification callback: (%s) Notification delivered\n", object.uri_path()); |
MarceloSalazar | 0:e13a8a944e25 | 70 | break; |
MarceloSalazar | 0:e13a8a944e25 | 71 | case NOTIFICATION_STATUS_SEND_FAILED: |
MarceloSalazar | 0:e13a8a944e25 | 72 | printf("Notification callback: (%s) Notification sending failed\n", object.uri_path()); |
MarceloSalazar | 0:e13a8a944e25 | 73 | break; |
MarceloSalazar | 0:e13a8a944e25 | 74 | case NOTIFICATION_STATUS_SUBSCRIBED: |
MarceloSalazar | 0:e13a8a944e25 | 75 | printf("Notification callback: (%s) subscribed\n", object.uri_path()); |
MarceloSalazar | 0:e13a8a944e25 | 76 | break; |
MarceloSalazar | 0:e13a8a944e25 | 77 | case NOTIFICATION_STATUS_UNSUBSCRIBED: |
MarceloSalazar | 0:e13a8a944e25 | 78 | printf("Notification callback: (%s) subscription removed\n", object.uri_path()); |
MarceloSalazar | 0:e13a8a944e25 | 79 | break; |
MarceloSalazar | 0:e13a8a944e25 | 80 | default: |
MarceloSalazar | 0:e13a8a944e25 | 81 | break; |
MarceloSalazar | 0:e13a8a944e25 | 82 | } |
MarceloSalazar | 0:e13a8a944e25 | 83 | } |
MarceloSalazar | 0:e13a8a944e25 | 84 | |
MarceloSalazar | 0:e13a8a944e25 | 85 | // This function is called when a POST request is received for resource 5000/0/1. |
MarceloSalazar | 0:e13a8a944e25 | 86 | void unregister(void *) |
MarceloSalazar | 0:e13a8a944e25 | 87 | { |
MarceloSalazar | 0:e13a8a944e25 | 88 | printf("Unregister resource executed\n"); |
MarceloSalazar | 0:e13a8a944e25 | 89 | client->close(); |
MarceloSalazar | 0:e13a8a944e25 | 90 | } |
MarceloSalazar | 0:e13a8a944e25 | 91 | |
MarceloSalazar | 0:e13a8a944e25 | 92 | // This function is called when a POST request is received for resource 5000/0/2. |
MarceloSalazar | 0:e13a8a944e25 | 93 | void factory_reset(void *) |
MarceloSalazar | 0:e13a8a944e25 | 94 | { |
MarceloSalazar | 0:e13a8a944e25 | 95 | printf("Factory reset resource executed\n"); |
MarceloSalazar | 0:e13a8a944e25 | 96 | client->close(); |
MarceloSalazar | 0:e13a8a944e25 | 97 | kcm_status_e kcm_status = kcm_factory_reset(); |
MarceloSalazar | 0:e13a8a944e25 | 98 | if (kcm_status != KCM_STATUS_SUCCESS) { |
MarceloSalazar | 0:e13a8a944e25 | 99 | printf("Failed to do factory reset - %d\n", kcm_status); |
MarceloSalazar | 0:e13a8a944e25 | 100 | } else { |
MarceloSalazar | 0:e13a8a944e25 | 101 | printf("Factory reset completed. Now restart the device\n"); |
MarceloSalazar | 0:e13a8a944e25 | 102 | } |
MarceloSalazar | 0:e13a8a944e25 | 103 | } |
MarceloSalazar | 0:e13a8a944e25 | 104 | |
MarceloSalazar | 0:e13a8a944e25 | 105 | int main(void) |
MarceloSalazar | 0:e13a8a944e25 | 106 | { |
MarceloSalazar | 0:e13a8a944e25 | 107 | // IOTMORF-1712: DAPLINK starts the previous application during flashing a new binary |
MarceloSalazar | 0:e13a8a944e25 | 108 | // This is workaround to prevent possible deletion of credentials or storage corruption |
MarceloSalazar | 0:e13a8a944e25 | 109 | // while replacing the application binary. |
MarceloSalazar | 0:e13a8a944e25 | 110 | wait(2); |
MarceloSalazar | 0:e13a8a944e25 | 111 | |
MarceloSalazar | 0:e13a8a944e25 | 112 | // SimpleClient is used for registering and unregistering resources to a server. |
MarceloSalazar | 0:e13a8a944e25 | 113 | SimpleMbedCloudClient mbedClient; |
MarceloSalazar | 0:e13a8a944e25 | 114 | |
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 | 0:e13a8a944e25 | 118 | printf("Client initialized\r\n"); |
MarceloSalazar | 0:e13a8a944e25 | 119 | #ifdef MBED_HEAP_STATS_ENABLED |
MarceloSalazar | 0:e13a8a944e25 | 120 | heap_stats(); |
MarceloSalazar | 0:e13a8a944e25 | 121 | #endif |
MarceloSalazar | 0:e13a8a944e25 | 122 | |
MarceloSalazar | 0:e13a8a944e25 | 123 | // Create resource for button count. Path of this resource will be: 3200/0/5501. |
MarceloSalazar | 0:e13a8a944e25 | 124 | button_res = mbedClient.add_cloud_resource(3200, 0, 5501, "button_resource", M2MResourceInstance::INTEGER, |
MarceloSalazar | 0:e13a8a944e25 | 125 | M2MBase::GET_ALLOWED, 0, true, NULL, (void*)button_notification_status_callback); |
MarceloSalazar | 0:e13a8a944e25 | 126 | |
MarceloSalazar | 0:e13a8a944e25 | 127 | // Create resource for led blinking pattern. Path of this resource will be: 3201/0/5853. |
MarceloSalazar | 0:e13a8a944e25 | 128 | pattern_res = mbedClient.add_cloud_resource(3201, 0, 5853, "pattern_resource", M2MResourceInstance::STRING, |
MarceloSalazar | 0:e13a8a944e25 | 129 | M2MBase::GET_PUT_ALLOWED, "500:500:500:500", false, (void*)pattern_updated, NULL); |
MarceloSalazar | 0:e13a8a944e25 | 130 | |
MarceloSalazar | 0:e13a8a944e25 | 131 | // Create resource for starting the led blinking. Path of this resource will be: 3201/0/5850. |
MarceloSalazar | 0:e13a8a944e25 | 132 | blink_res = mbedClient.add_cloud_resource(3201, 0, 5850, "blink_resource", M2MResourceInstance::STRING, |
MarceloSalazar | 0:e13a8a944e25 | 133 | M2MBase::POST_ALLOWED, "", false, (void*)blink_callback, NULL); |
MarceloSalazar | 0:e13a8a944e25 | 134 | |
MarceloSalazar | 0:e13a8a944e25 | 135 | // Create resource for unregistering the device. Path of this resource will be: 5000/0/1. |
MarceloSalazar | 0:e13a8a944e25 | 136 | mbedClient.add_cloud_resource(5000, 0, 1, "unregister", M2MResourceInstance::STRING, |
MarceloSalazar | 0:e13a8a944e25 | 137 | M2MBase::POST_ALLOWED, NULL, false, (void*)unregister, NULL); |
MarceloSalazar | 0:e13a8a944e25 | 138 | |
MarceloSalazar | 0:e13a8a944e25 | 139 | // Create resource for running factory reset for the device. Path of this resource will be: 5000/0/2. |
MarceloSalazar | 0:e13a8a944e25 | 140 | mbedClient.add_cloud_resource(5000, 0, 2, "factory_reset", M2MResourceInstance::STRING, |
MarceloSalazar | 0:e13a8a944e25 | 141 | M2MBase::POST_ALLOWED, NULL, false, (void*)factory_reset, NULL); |
MarceloSalazar | 0:e13a8a944e25 | 142 | |
MarceloSalazar | 0:e13a8a944e25 | 143 | mbedClient.register_and_connect(); |
MarceloSalazar | 0:e13a8a944e25 | 144 | |
MarceloSalazar | 0:e13a8a944e25 | 145 | // Check if client is registering or registered, if true sleep and repeat. |
MarceloSalazar | 0:e13a8a944e25 | 146 | while (mbedClient.is_register_called()) { |
MarceloSalazar | 0:e13a8a944e25 | 147 | static int button_count = 0; |
MarceloSalazar | 0:e13a8a944e25 | 148 | do_wait(100); |
MarceloSalazar | 0:e13a8a944e25 | 149 | if (button_clicked()) { |
MarceloSalazar | 0:e13a8a944e25 | 150 | button_res->set_value(++button_count); |
MarceloSalazar | 0:e13a8a944e25 | 151 | } |
MarceloSalazar | 0:e13a8a944e25 | 152 | } |
MarceloSalazar | 0:e13a8a944e25 | 153 | |
MarceloSalazar | 0:e13a8a944e25 | 154 | // Client unregistered, exit program. |
MarceloSalazar | 0:e13a8a944e25 | 155 | return 0; |
MarceloSalazar | 0:e13a8a944e25 | 156 | } |