Taking photo using GR-LYCHEE through Pelion Device Management.
This firmware project works for GR-LYCHEE and GR-PEACH.
Steps to build this firmware with Mbed CLI
Import project
$ mbed import http://os.mbed.com/users/coisme/code/Pelion-GR-LYCHEE-camera-firmware/
Setting
- Ethernet (GR-PEACH only)
- Change the file name
mbed_app.RZ_A1H_Ethernet.json
tombed_app.json
- Change the file name
- Wi-Fi
- Change the Wi-Fi setting in
mbed_app.json
, i.e.nsapi.default-wifi-ssid
andnsapi.default-wifi-password
- Change the Wi-Fi setting in
If you haven't set your Pelion API key, run the following command.
$ mbed config -G CLOUD_SDK_API_KEY <API_KEY>
Setting for Pelion Device Management
$ mbed dm init -d "example.com" --model-name "PELION_DEMO" -q --force
Compile
For GR-LYCHEE:
$ mbed compile -t GCC_ARM -m GR_LYCHEE
For GR-PEACH:
$ mbed compile -t GCC_ARM -m RZ_A1H
Write the created .bin
file to your GR-LYCHEE/PEACH.
That's it! :-)
LED Status Indicator
LEDs next to UB0 show the status of your GR-LYCHEE/PEACH.
LED# | GR-LYCHEE | GR-PEACH | Status | Description |
---|---|---|---|---|
LED1 | green | red | Normal | Turned on after the device is registered to Pelion Device Management successfully. |
LED2 | yellow | green | Error | Turned on when the network initialization failed. Check your Wi-Fi setting. |
LED3 | orange | blue | Error | Turned on when the Pelion Device Management Client initialization failed. Check your SD card. |
LED4 | red | red | Error | Turned on when the network is disconnected. Check your Wi-Fi network status. |
Clear device identity
If you want to clear the device's identity, connect to the device via serial terminal. Then input r
command. The device flushes the identity storage, then reboot.
Known Issues
client_error(6) -> Client in reconnection mode NetworkError
appears when connecting to network, but eventually connection will be established.- Warning message
"MBEDTLS_TEST_NULL_ENTROPY has been enabled. This configuration is not secure and is not suitable for production use"
appears when compiling the project for GR-PEACH. This can be ignored in development stage.
main.cpp@28:7c220e487ae6, 2019-10-17 (annotated)
- Committer:
- Osamu Koizumi
- Date:
- Thu Oct 17 13:31:21 2019 +0900
- Revision:
- 28:7c220e487ae6
- Parent:
- 27:cafd85d3818d
Remove Wi-Fi setting
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MACRUM | 0:6d2053b84a92 | 1 | // ---------------------------------------------------------------------------- |
Osamu Koizumi |
24:c5412f9149fe | 2 | // Copyright 2016-2019 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 |
Osamu Koizumi |
24:c5412f9149fe | 19 | #include "mbed.h" |
Osamu Koizumi |
24:c5412f9149fe | 20 | #include "kv_config.h" |
Osamu Koizumi |
24:c5412f9149fe | 21 | #include "mbed-cloud-client/MbedCloudClient.h" // Required for new MbedCloudClient() |
Osamu Koizumi |
24:c5412f9149fe | 22 | #include "factory_configurator_client.h" // Required for fcc_* functions and FCC_* defines |
Osamu Koizumi |
24:c5412f9149fe | 23 | #include "m2mresource.h" // Required for M2MResource |
MACRUM | 0:6d2053b84a92 | 24 | |
Osamu Koizumi |
24:c5412f9149fe | 25 | #include "mbed-trace/mbed_trace.h" // Required for mbed_trace_* |
Osamu Koizumi |
24:c5412f9149fe | 26 | |
Osamu Koizumi |
24:c5412f9149fe | 27 | // Camera setting |
dkato | 9:eac9e0c9effd | 28 | #include "EasyAttach_CameraAndLCD.h" |
dkato | 9:eac9e0c9effd | 29 | #include "dcache-control.h" |
dkato | 9:eac9e0c9effd | 30 | #include "JPEG_Converter.h" |
dkato | 9:eac9e0c9effd | 31 | /**** User Selection *********/ |
dkato | 9:eac9e0c9effd | 32 | #define VIDEO_PIXEL_HW (320u) /* QVGA */ |
dkato | 9:eac9e0c9effd | 33 | #define VIDEO_PIXEL_VW (240u) /* QVGA */ |
dkato | 9:eac9e0c9effd | 34 | #define JPEG_ENCODE_QUALITY (75) /* JPEG encode quality (min:1, max:75 (Considering the size of JpegBuffer, about 75 is the upper limit.)) */ |
dkato | 9:eac9e0c9effd | 35 | /*****************************/ |
dkato | 9:eac9e0c9effd | 36 | #define DATA_SIZE_PER_PIC (2u) |
dkato | 9:eac9e0c9effd | 37 | #define FRAME_BUFFER_STRIDE (((VIDEO_PIXEL_HW * DATA_SIZE_PER_PIC) + 31u) & ~31u) |
dkato | 9:eac9e0c9effd | 38 | #define FRAME_BUFFER_HEIGHT (VIDEO_PIXEL_VW) |
dkato | 9:eac9e0c9effd | 39 | |
dkato | 9:eac9e0c9effd | 40 | uint8_t user_frame_buffer0[FRAME_BUFFER_STRIDE * FRAME_BUFFER_HEIGHT]__attribute((aligned(32))); |
dkato | 9:eac9e0c9effd | 41 | uint8_t JpegBuffer[1024 * 32]__attribute((aligned(32))); |
dkato | 9:eac9e0c9effd | 42 | DisplayBase Display; |
dkato | 9:eac9e0c9effd | 43 | JPEG_Converter Jcu; |
Osamu Koizumi |
24:c5412f9149fe | 44 | // End camera setting |
Osamu Koizumi |
4:7c58c47eca55 | 45 | |
Osamu Koizumi |
24:c5412f9149fe | 46 | const int LED_ON = MBED_CONF_APP_LED_ON; |
Osamu Koizumi |
24:c5412f9149fe | 47 | const int LED_OFF = (!(MBED_CONF_APP_LED_ON)); |
Osamu Koizumi |
14:0480150782c8 | 48 | |
Osamu Koizumi |
24:c5412f9149fe | 49 | // Pointers to the resources that will be created in main_application(). |
Osamu Koizumi |
24:c5412f9149fe | 50 | static MbedCloudClient *cloud_client; |
Osamu Koizumi |
24:c5412f9149fe | 51 | static bool cloud_client_running = true; |
Osamu Koizumi |
24:c5412f9149fe | 52 | static NetworkInterface *network = NULL; |
MACRUM | 0:6d2053b84a92 | 53 | |
Osamu Koizumi |
24:c5412f9149fe | 54 | // Fake entropy needed for non-TRNG boards. Suitable only for demo devices. |
Osamu Koizumi |
24:c5412f9149fe | 55 | const uint8_t MBED_CLOUD_DEV_ENTROPY[] = { 0xf6, 0xd6, 0xc0, 0x09, 0x9e, 0x6e, 0xf2, 0x37, 0xdc, 0x29, 0x88, 0xf1, 0x57, 0x32, 0x7d, 0xde, 0xac, 0xb3, 0x99, 0x8c, 0xb9, 0x11, 0x35, 0x18, 0xeb, 0x48, 0x29, 0x03, 0x6a, 0x94, 0x6d, 0xe8, 0x40, 0xc0, 0x28, 0xcc, 0xe4, 0x04, 0xc3, 0x1f, 0x4b, 0xc2, 0xe0, 0x68, 0xa0, 0x93, 0xe6, 0x3a }; |
MACRUM | 0:6d2053b84a92 | 56 | |
Osamu Koizumi |
24:c5412f9149fe | 57 | static M2MResource* m2m_get_res; |
Osamu Koizumi |
24:c5412f9149fe | 58 | static M2MResource* m2m_put_res; |
Osamu Koizumi |
24:c5412f9149fe | 59 | static M2MResource* m2m_post_res; |
Osamu Koizumi |
24:c5412f9149fe | 60 | static M2MResource* m2m_deregister_res; |
MACRUM | 0:6d2053b84a92 | 61 | |
Osamu Koizumi |
24:c5412f9149fe | 62 | // Additional resources for camera |
Osamu Koizumi |
24:c5412f9149fe | 63 | static M2MResource* m2m_camera_capture_res; |
Osamu Koizumi |
24:c5412f9149fe | 64 | |
Osamu Koizumi |
24:c5412f9149fe | 65 | // Camera functions |
dkato | 9:eac9e0c9effd | 66 | bool take_photo(uint8_t *photo_data, uint32_t *photo_data_len) { |
dkato | 9:eac9e0c9effd | 67 | JPEG_Converter::bitmap_buff_info_t buff_info; |
dkato | 9:eac9e0c9effd | 68 | JPEG_Converter::encode_options_t encode_opt; |
dkato | 9:eac9e0c9effd | 69 | |
dkato | 9:eac9e0c9effd | 70 | if ((photo_data == NULL) || (photo_data_len == NULL)) { |
dkato | 9:eac9e0c9effd | 71 | return false; |
dkato | 9:eac9e0c9effd | 72 | } |
dkato | 9:eac9e0c9effd | 73 | |
dkato | 9:eac9e0c9effd | 74 | // Jpeg setting |
dkato | 9:eac9e0c9effd | 75 | buff_info.width = VIDEO_PIXEL_HW; |
dkato | 9:eac9e0c9effd | 76 | buff_info.height = VIDEO_PIXEL_VW; |
dkato | 9:eac9e0c9effd | 77 | buff_info.format = JPEG_Converter::WR_RD_YCbCr422; |
dkato | 9:eac9e0c9effd | 78 | buff_info.buffer_address = (void *)user_frame_buffer0; |
dkato | 9:eac9e0c9effd | 79 | encode_opt.encode_buff_size = *photo_data_len; |
dkato | 9:eac9e0c9effd | 80 | encode_opt.input_swapsetting = JPEG_Converter::WR_RD_WRSWA_32_16_8BIT; |
dkato | 9:eac9e0c9effd | 81 | |
dkato | 9:eac9e0c9effd | 82 | dcache_invalid(photo_data, *photo_data_len); |
Osamu Koizumi |
16:e0cf507dcd5d | 83 | JPEG_Converter::jpeg_conv_error_t err = Jcu.encode(&buff_info, photo_data, (size_t *)photo_data_len, &encode_opt); |
Osamu Koizumi |
14:0480150782c8 | 84 | if(err != JPEG_Converter::JPEG_CONV_OK) |
Osamu Koizumi |
14:0480150782c8 | 85 | { |
dkato | 9:eac9e0c9effd | 86 | return false; |
dkato | 9:eac9e0c9effd | 87 | } |
dkato | 9:eac9e0c9effd | 88 | return true; |
dkato | 9:eac9e0c9effd | 89 | } |
dkato | 9:eac9e0c9effd | 90 | |
Osamu Koizumi |
10:29d334f90c99 | 91 | bool take_and_send_photo() { |
Osamu Koizumi |
10:29d334f90c99 | 92 | // Send photo data on button click |
Osamu Koizumi |
10:29d334f90c99 | 93 | uint32_t photo_data_len = sizeof(JpegBuffer); |
Osamu Koizumi |
16:e0cf507dcd5d | 94 | bool result = take_photo(JpegBuffer, &photo_data_len); |
Osamu Koizumi |
16:e0cf507dcd5d | 95 | if (result) { |
Osamu Koizumi |
24:c5412f9149fe | 96 | m2m_camera_capture_res->set_value((const uint8_t *)JpegBuffer, photo_data_len); |
Osamu Koizumi |
10:29d334f90c99 | 97 | } |
Osamu Koizumi |
16:e0cf507dcd5d | 98 | |
Osamu Koizumi |
16:e0cf507dcd5d | 99 | return result; |
Osamu Koizumi |
10:29d334f90c99 | 100 | } |
Osamu Koizumi |
10:29d334f90c99 | 101 | |
Osamu Koizumi |
14:0480150782c8 | 102 | /* |
Osamu Koizumi |
19:bdc69891952a | 103 | * Monitor the network connection. If the network is disconnected, LED4 on the board lights. |
Osamu Koizumi |
14:0480150782c8 | 104 | */ |
Osamu Koizumi |
14:0480150782c8 | 105 | void monitor() { |
Osamu Koizumi |
24:c5412f9149fe | 106 | static DigitalOut led(LED4, LED_OFF); |
Osamu Koizumi |
24:c5412f9149fe | 107 | nsapi_connection_status status = network->get_connection_status(); |
Osamu Koizumi |
14:0480150782c8 | 108 | if(status != NSAPI_STATUS_GLOBAL_UP) { |
Osamu Koizumi |
24:c5412f9149fe | 109 | led = LED_ON; |
Osamu Koizumi |
14:0480150782c8 | 110 | printf("Network is disconnected. Status = %d.\n", status); |
Osamu Koizumi |
14:0480150782c8 | 111 | } else { |
Osamu Koizumi |
24:c5412f9149fe | 112 | led = LED_OFF; |
Osamu Koizumi |
14:0480150782c8 | 113 | } |
Osamu Koizumi |
14:0480150782c8 | 114 | } |
Osamu Koizumi |
14:0480150782c8 | 115 | |
Osamu Koizumi |
24:c5412f9149fe | 116 | |
Osamu Koizumi |
24:c5412f9149fe | 117 | void print_client_ids(void) |
Osamu Koizumi |
4:7c58c47eca55 | 118 | { |
Osamu Koizumi |
24:c5412f9149fe | 119 | printf("Account ID: %s\n", cloud_client->endpoint_info()->account_id.c_str()); |
Osamu Koizumi |
24:c5412f9149fe | 120 | printf("Endpoint name: %s\n", cloud_client->endpoint_info()->internal_endpoint_name.c_str()); |
Osamu Koizumi |
24:c5412f9149fe | 121 | printf("Device Id: %s\n\n", cloud_client->endpoint_info()->endpoint_name.c_str()); |
MACRUM | 0:6d2053b84a92 | 122 | } |
MACRUM | 0:6d2053b84a92 | 123 | |
Osamu Koizumi |
24:c5412f9149fe | 124 | void button_press() { |
Osamu Koizumi |
24:c5412f9149fe | 125 | m2m_get_res->set_value(m2m_get_res->get_value_int() + 1); |
Osamu Koizumi |
24:c5412f9149fe | 126 | printf("User button clicked %d times\n", m2m_get_res->get_value_int()); |
Osamu Koizumi |
24:c5412f9149fe | 127 | |
Osamu Koizumi |
24:c5412f9149fe | 128 | if(!take_and_send_photo()) { |
Osamu Koizumi |
24:c5412f9149fe | 129 | printf("Failed to send photo."); |
Osamu Koizumi |
24:c5412f9149fe | 130 | } |
Osamu Koizumi |
24:c5412f9149fe | 131 | } |
Osamu Koizumi |
24:c5412f9149fe | 132 | |
Osamu Koizumi |
24:c5412f9149fe | 133 | void put_update(const char* /*object_name*/) |
Osamu Koizumi |
24:c5412f9149fe | 134 | { |
Osamu Koizumi |
24:c5412f9149fe | 135 | printf("Camera shutter triggered. %d\n", (int)m2m_put_res->get_value_int()); |
MACRUM | 0:6d2053b84a92 | 136 | |
Osamu Koizumi |
10:29d334f90c99 | 137 | if(!take_and_send_photo()) { |
Osamu Koizumi |
10:29d334f90c99 | 138 | printf("Failed to send photo."); |
dkato | 9:eac9e0c9effd | 139 | } |
MACRUM | 0:6d2053b84a92 | 140 | } |
MACRUM | 0:6d2053b84a92 | 141 | |
Osamu Koizumi |
24:c5412f9149fe | 142 | void execute_post(void* /*arguments*/) |
Osamu Koizumi |
24:c5412f9149fe | 143 | { |
Osamu Koizumi |
24:c5412f9149fe | 144 | printf("POST executed\n"); |
Osamu Koizumi |
24:c5412f9149fe | 145 | } |
Osamu Koizumi |
24:c5412f9149fe | 146 | |
Osamu Koizumi |
24:c5412f9149fe | 147 | void deregister_client(void) |
Osamu Koizumi |
24:c5412f9149fe | 148 | { |
Osamu Koizumi |
24:c5412f9149fe | 149 | printf("Unregistering and disconnecting from the network.\n"); |
Osamu Koizumi |
24:c5412f9149fe | 150 | cloud_client->close(); |
MACRUM | 0:6d2053b84a92 | 151 | } |
MACRUM | 0:6d2053b84a92 | 152 | |
Osamu Koizumi |
24:c5412f9149fe | 153 | void deregister(void* /*arguments*/) |
Osamu Koizumi |
24:c5412f9149fe | 154 | { |
Osamu Koizumi |
24:c5412f9149fe | 155 | printf("POST deregister executed\n"); |
Osamu Koizumi |
24:c5412f9149fe | 156 | m2m_deregister_res->send_delayed_post_response(); |
Osamu Koizumi |
24:c5412f9149fe | 157 | |
Osamu Koizumi |
24:c5412f9149fe | 158 | deregister_client(); |
Osamu Koizumi |
24:c5412f9149fe | 159 | } |
Osamu Koizumi |
24:c5412f9149fe | 160 | |
Osamu Koizumi |
24:c5412f9149fe | 161 | void client_registered(void) |
Osamu Koizumi |
24:c5412f9149fe | 162 | { |
Osamu Koizumi |
24:c5412f9149fe | 163 | printf("Client registered.\n"); |
Osamu Koizumi |
24:c5412f9149fe | 164 | print_client_ids(); |
Osamu Koizumi |
19:bdc69891952a | 165 | // Turn on LED1 when device is registered. |
Osamu Koizumi |
19:bdc69891952a | 166 | DigitalOut led(LED1, LED_ON); |
MACRUM | 0:6d2053b84a92 | 167 | } |
Osamu Koizumi |
24:c5412f9149fe | 168 | |
Osamu Koizumi |
24:c5412f9149fe | 169 | void client_unregistered(void) |
Osamu Koizumi |
24:c5412f9149fe | 170 | { |
Osamu Koizumi |
24:c5412f9149fe | 171 | printf("Client unregistered.\n"); |
Osamu Koizumi |
24:c5412f9149fe | 172 | (void) network->disconnect(); |
Osamu Koizumi |
24:c5412f9149fe | 173 | cloud_client_running = false; |
Osamu Koizumi |
24:c5412f9149fe | 174 | // Turn off LED1 when device is registered. |
Osamu Koizumi |
24:c5412f9149fe | 175 | DigitalOut led(LED1, LED_OFF); |
Osamu Koizumi |
24:c5412f9149fe | 176 | } |
MACRUM | 0:6d2053b84a92 | 177 | |
Osamu Koizumi |
24:c5412f9149fe | 178 | void client_error(int err) |
Osamu Koizumi |
24:c5412f9149fe | 179 | { |
Osamu Koizumi |
24:c5412f9149fe | 180 | printf("client_error(%d) -> %s\n", err, cloud_client->error_description()); |
Osamu Koizumi |
24:c5412f9149fe | 181 | } |
Osamu Koizumi |
24:c5412f9149fe | 182 | |
Osamu Koizumi |
24:c5412f9149fe | 183 | void update_progress(uint32_t progress, uint32_t total) |
Osamu Koizumi |
24:c5412f9149fe | 184 | { |
Osamu Koizumi |
24:c5412f9149fe | 185 | uint8_t percent = (uint8_t)((uint64_t)progress * 100 / total); |
Osamu Koizumi |
24:c5412f9149fe | 186 | printf("Update progress = %" PRIu8 "%%\n", percent); |
Osamu Koizumi |
24:c5412f9149fe | 187 | } |
Osamu Koizumi |
24:c5412f9149fe | 188 | |
Osamu Koizumi |
24:c5412f9149fe | 189 | int main(void) |
Osamu Koizumi |
24:c5412f9149fe | 190 | { |
Osamu Koizumi |
24:c5412f9149fe | 191 | int status; |
MACRUM | 0:6d2053b84a92 | 192 | |
Osamu Koizumi |
24:c5412f9149fe | 193 | status = mbed_trace_init(); |
Osamu Koizumi |
24:c5412f9149fe | 194 | if (status != 0) { |
Osamu Koizumi |
24:c5412f9149fe | 195 | printf("mbed_trace_init() failed with %d\n", status); |
Osamu Koizumi |
24:c5412f9149fe | 196 | return -1; |
Osamu Koizumi |
24:c5412f9149fe | 197 | } |
MACRUM | 0:6d2053b84a92 | 198 | |
Osamu Koizumi |
24:c5412f9149fe | 199 | // Mount default kvstore |
Osamu Koizumi |
24:c5412f9149fe | 200 | printf("Application ready\n"); |
Osamu Koizumi |
24:c5412f9149fe | 201 | status = kv_init_storage_config(); |
Osamu Koizumi |
24:c5412f9149fe | 202 | if (status != MBED_SUCCESS) { |
Osamu Koizumi |
24:c5412f9149fe | 203 | printf("kv_init_storage_config() - failed, status %d\n", status); |
Osamu Koizumi |
24:c5412f9149fe | 204 | return -1; |
Osamu Koizumi |
24:c5412f9149fe | 205 | } |
Osamu Koizumi |
24:c5412f9149fe | 206 | |
Osamu Koizumi |
24:c5412f9149fe | 207 | // Connect with NetworkInterface |
Osamu Koizumi |
24:c5412f9149fe | 208 | printf("Connect to network\n"); |
Osamu Koizumi |
24:c5412f9149fe | 209 | network = NetworkInterface::get_default_instance(); |
Osamu Koizumi |
24:c5412f9149fe | 210 | if (network == NULL) { |
Osamu Koizumi |
24:c5412f9149fe | 211 | printf("Failed to get default NetworkInterface\n"); |
Osamu Koizumi |
24:c5412f9149fe | 212 | return -1; |
Osamu Koizumi |
24:c5412f9149fe | 213 | } |
Osamu Koizumi |
24:c5412f9149fe | 214 | status = network->connect(); |
MACRUM | 0:6d2053b84a92 | 215 | if (status != NSAPI_ERROR_OK) { |
Osamu Koizumi |
24:c5412f9149fe | 216 | printf("NetworkInterface failed to connect with %d\n", status); |
Osamu Koizumi |
19:bdc69891952a | 217 | // Turn on LED2 when the network initialization failed. |
Osamu Koizumi |
19:bdc69891952a | 218 | DigitalOut led(LED2, LED_ON); |
MACRUM | 0:6d2053b84a92 | 219 | return -1; |
MACRUM | 0:6d2053b84a92 | 220 | } |
MACRUM | 0:6d2053b84a92 | 221 | |
Osamu Koizumi |
24:c5412f9149fe | 222 | printf("Network initialized, connected with IP %s\n\n", network->get_ip_address()); |
MACRUM | 0:6d2053b84a92 | 223 | |
Osamu Koizumi |
24:c5412f9149fe | 224 | // Run developer flow |
Osamu Koizumi |
24:c5412f9149fe | 225 | printf("Start developer flow\n"); |
Osamu Koizumi |
24:c5412f9149fe | 226 | status = fcc_init(); |
Osamu Koizumi |
24:c5412f9149fe | 227 | if (status != FCC_STATUS_SUCCESS) { |
Osamu Koizumi |
24:c5412f9149fe | 228 | printf("fcc_init() failed with %d\n", status); |
Osamu Koizumi |
24:c5412f9149fe | 229 | // Turn on LED3 when Pelion Client initialization failed. |
Osamu Koizumi |
24:c5412f9149fe | 230 | DigitalOut led(LED3, LED_ON); |
Osamu Koizumi |
24:c5412f9149fe | 231 | return -1; |
Osamu Koizumi |
24:c5412f9149fe | 232 | } |
Osamu Koizumi |
24:c5412f9149fe | 233 | |
Osamu Koizumi |
24:c5412f9149fe | 234 | // Inject hardcoded entropy for the device. Suitable only for demo devices. |
Osamu Koizumi |
24:c5412f9149fe | 235 | (void) fcc_entropy_set(MBED_CLOUD_DEV_ENTROPY, sizeof(MBED_CLOUD_DEV_ENTROPY)); |
Osamu Koizumi |
24:c5412f9149fe | 236 | status = fcc_developer_flow(); |
Osamu Koizumi |
24:c5412f9149fe | 237 | if (status != FCC_STATUS_SUCCESS && status != FCC_STATUS_KCM_FILE_EXIST_ERROR && status != FCC_STATUS_CA_ERROR) { |
Osamu Koizumi |
24:c5412f9149fe | 238 | printf("fcc_developer_flow() failed with %d\n", status); |
Osamu Koizumi |
19:bdc69891952a | 239 | // Turn on LED3 when Pelion Client initialization failed. |
Osamu Koizumi |
19:bdc69891952a | 240 | DigitalOut led(LED3, LED_ON); |
MACRUM | 0:6d2053b84a92 | 241 | return -1; |
MACRUM | 0:6d2053b84a92 | 242 | } |
MACRUM | 0:6d2053b84a92 | 243 | |
Osamu Koizumi |
24:c5412f9149fe | 244 | printf("Create resources\n"); |
Osamu Koizumi |
24:c5412f9149fe | 245 | M2MObjectList m2m_obj_list; |
Osamu Koizumi |
24:c5412f9149fe | 246 | |
Osamu Koizumi |
24:c5412f9149fe | 247 | // GET resource 3200/0/5501 |
Osamu Koizumi |
24:c5412f9149fe | 248 | m2m_get_res = M2MInterfaceFactory::create_resource(m2m_obj_list, 3200, 0, 5501, M2MResourceInstance::INTEGER, M2MBase::GET_ALLOWED); |
Osamu Koizumi |
24:c5412f9149fe | 249 | if (m2m_get_res->set_value(0) != true) { |
Osamu Koizumi |
24:c5412f9149fe | 250 | printf("m2m_get_res->set_value() failed\n"); |
Osamu Koizumi |
24:c5412f9149fe | 251 | return -1; |
Osamu Koizumi |
24:c5412f9149fe | 252 | } |
Osamu Koizumi |
24:c5412f9149fe | 253 | |
Osamu Koizumi |
24:c5412f9149fe | 254 | // PUT resource 3201/0/5853 (camera trigger resource) |
Osamu Koizumi |
24:c5412f9149fe | 255 | m2m_put_res = M2MInterfaceFactory::create_resource(m2m_obj_list, 3201, 0, 5853, M2MResourceInstance::INTEGER, M2MBase::GET_PUT_ALLOWED); |
Osamu Koizumi |
24:c5412f9149fe | 256 | if (m2m_put_res->set_value(0) != true) { |
Osamu Koizumi |
24:c5412f9149fe | 257 | printf("m2m_led_res->set_value() failed\n"); |
Osamu Koizumi |
24:c5412f9149fe | 258 | return -1; |
Osamu Koizumi |
24:c5412f9149fe | 259 | } |
Osamu Koizumi |
24:c5412f9149fe | 260 | if (m2m_put_res->set_value_updated_function(put_update) != true) { |
Osamu Koizumi |
24:c5412f9149fe | 261 | printf("m2m_put_res->set_value_updated_function() failed\n"); |
Osamu Koizumi |
24:c5412f9149fe | 262 | return -1; |
Osamu Koizumi |
24:c5412f9149fe | 263 | } |
MACRUM | 0:6d2053b84a92 | 264 | |
Osamu Koizumi |
24:c5412f9149fe | 265 | // POST resource 3201/0/5850 |
Osamu Koizumi |
24:c5412f9149fe | 266 | m2m_post_res = M2MInterfaceFactory::create_resource(m2m_obj_list, 3201, 0, 5850, M2MResourceInstance::INTEGER, M2MBase::POST_ALLOWED); |
Osamu Koizumi |
24:c5412f9149fe | 267 | if (m2m_post_res->set_execute_function(execute_post) != true) { |
Osamu Koizumi |
24:c5412f9149fe | 268 | printf("m2m_post_res->set_execute_function() failed\n"); |
Osamu Koizumi |
24:c5412f9149fe | 269 | return -1; |
Osamu Koizumi |
24:c5412f9149fe | 270 | } |
Osamu Koizumi |
24:c5412f9149fe | 271 | |
Osamu Koizumi |
24:c5412f9149fe | 272 | // POST resource 5000/0/1 to trigger deregister. |
Osamu Koizumi |
24:c5412f9149fe | 273 | m2m_deregister_res = M2MInterfaceFactory::create_resource(m2m_obj_list, 5000, 0, 1, M2MResourceInstance::INTEGER, M2MBase::POST_ALLOWED); |
MACRUM | 0:6d2053b84a92 | 274 | |
Osamu Koizumi |
24:c5412f9149fe | 275 | // Use delayed response |
Osamu Koizumi |
24:c5412f9149fe | 276 | m2m_deregister_res->set_delayed_response(true); |
MACRUM | 0:6d2053b84a92 | 277 | |
Osamu Koizumi |
24:c5412f9149fe | 278 | if (m2m_deregister_res->set_execute_function(deregister) != true) { |
Osamu Koizumi |
24:c5412f9149fe | 279 | printf("m2m_post_res->set_execute_function() failed\n"); |
Osamu Koizumi |
24:c5412f9149fe | 280 | return -1; |
Osamu Koizumi |
24:c5412f9149fe | 281 | } |
Osamu Koizumi |
24:c5412f9149fe | 282 | |
Osamu Koizumi |
24:c5412f9149fe | 283 | // Camera capture resource 3200/0/4014 |
Osamu Koizumi |
24:c5412f9149fe | 284 | m2m_camera_capture_res = M2MInterfaceFactory::create_resource(m2m_obj_list, 3200, 0, 4014, M2MResourceInstance::STRING, M2MBase::GET_ALLOWED); |
Osamu Koizumi |
24:c5412f9149fe | 285 | if(m2m_camera_capture_res->set_value(0) != true) { |
Osamu Koizumi |
24:c5412f9149fe | 286 | printf("m2m_camera_capture_res->set_value() failed.\n"); |
Osamu Koizumi |
24:c5412f9149fe | 287 | return -1; |
Osamu Koizumi |
24:c5412f9149fe | 288 | } |
MACRUM | 0:6d2053b84a92 | 289 | |
dkato | 9:eac9e0c9effd | 290 | // Camera start |
dkato | 9:eac9e0c9effd | 291 | EasyAttach_Init(Display); |
dkato | 9:eac9e0c9effd | 292 | Display.Video_Write_Setting( |
dkato | 9:eac9e0c9effd | 293 | DisplayBase::VIDEO_INPUT_CHANNEL_0, |
dkato | 9:eac9e0c9effd | 294 | DisplayBase::COL_SYS_NTSC_358, |
dkato | 9:eac9e0c9effd | 295 | (void *)user_frame_buffer0, |
dkato | 9:eac9e0c9effd | 296 | FRAME_BUFFER_STRIDE, |
dkato | 9:eac9e0c9effd | 297 | DisplayBase::VIDEO_FORMAT_YCBCR422, |
dkato | 9:eac9e0c9effd | 298 | DisplayBase::WR_RD_WRSWA_32_16BIT, |
dkato | 9:eac9e0c9effd | 299 | VIDEO_PIXEL_VW, |
dkato | 9:eac9e0c9effd | 300 | VIDEO_PIXEL_HW |
dkato | 9:eac9e0c9effd | 301 | ); |
dkato | 9:eac9e0c9effd | 302 | EasyAttach_CameraStart(Display, DisplayBase::VIDEO_INPUT_CHANNEL_0); |
dkato | 9:eac9e0c9effd | 303 | |
dkato | 9:eac9e0c9effd | 304 | // Jpeg setting |
dkato | 9:eac9e0c9effd | 305 | Jcu.SetQuality(JPEG_ENCODE_QUALITY); |
dkato | 9:eac9e0c9effd | 306 | |
MACRUM | 0:6d2053b84a92 | 307 | |
Osamu Koizumi |
24:c5412f9149fe | 308 | printf("Register Pelion Device Management Client\n\n"); |
Osamu Koizumi |
24:c5412f9149fe | 309 | cloud_client = new MbedCloudClient(client_registered, client_unregistered, client_error, NULL, update_progress); |
Osamu Koizumi |
24:c5412f9149fe | 310 | cloud_client->add_objects(m2m_obj_list); |
Osamu Koizumi |
24:c5412f9149fe | 311 | cloud_client->setup(network); // cloud_client->setup(NULL); -- https://jira.arm.com/browse/IOTCLT-3114 |
MACRUM | 0:6d2053b84a92 | 312 | |
Osamu Koizumi |
27:cafd85d3818d | 313 | // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads) |
Osamu Koizumi |
27:cafd85d3818d | 314 | // 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 |
Osamu Koizumi |
27:cafd85d3818d | 315 | EventQueue eventQueue; |
Osamu Koizumi |
27:cafd85d3818d | 316 | Thread t; |
Osamu Koizumi |
27:cafd85d3818d | 317 | // Start the event queue |
Osamu Koizumi |
27:cafd85d3818d | 318 | t.start(callback(&eventQueue, &EventQueue::dispatch_forever)); |
Osamu Koizumi |
27:cafd85d3818d | 319 | |
MACRUM | 0:6d2053b84a92 | 320 | // Setup the button |
Osamu Koizumi |
24:c5412f9149fe | 321 | InterruptIn btn(USER_BUTTON0); |
MACRUM | 0:6d2053b84a92 | 322 | btn.mode(PullUp); |
MACRUM | 0:6d2053b84a92 | 323 | |
MACRUM | 0:6d2053b84a92 | 324 | // The button fall handler is placed in the event queue so it will run in |
MACRUM | 0:6d2053b84a92 | 325 | // thread context instead of ISR context, which allows safely updating the cloud resource |
MACRUM | 0:6d2053b84a92 | 326 | btn.fall(eventQueue.event(&button_press)); |
MACRUM | 0:6d2053b84a92 | 327 | |
Osamu Koizumi |
14:0480150782c8 | 328 | // Monitor the network connection periodically. |
Osamu Koizumi |
14:0480150782c8 | 329 | Ticker ticker; |
Osamu Koizumi |
14:0480150782c8 | 330 | ticker.attach(eventQueue.event(&monitor), 5.0); // second param is period in seconds |
Osamu Koizumi |
14:0480150782c8 | 331 | |
Osamu Koizumi |
24:c5412f9149fe | 332 | while(cloud_client_running) { |
Osamu Koizumi |
24:c5412f9149fe | 333 | int in_char = getchar(); |
Osamu Koizumi |
24:c5412f9149fe | 334 | if (in_char == 'i') { |
Osamu Koizumi |
24:c5412f9149fe | 335 | print_client_ids(); // When 'i' is pressed, print endpoint info |
Osamu Koizumi |
24:c5412f9149fe | 336 | continue; |
Osamu Koizumi |
24:c5412f9149fe | 337 | } else if (in_char == 'r') { |
Osamu Koizumi |
24:c5412f9149fe | 338 | (void) fcc_storage_delete(); // When 'r' is pressed, erase storage and reboot the board. |
Osamu Koizumi |
24:c5412f9149fe | 339 | printf("Storage erased, rebooting the device.\n\n"); |
Osamu Koizumi |
24:c5412f9149fe | 340 | wait_us(1000000); |
Osamu Koizumi |
24:c5412f9149fe | 341 | NVIC_SystemReset(); |
Osamu Koizumi |
24:c5412f9149fe | 342 | } else if (in_char > 0 && in_char != 0x03) { // Ctrl+C is 0x03 in Mbed OS and Linux returns negative number |
Osamu Koizumi |
24:c5412f9149fe | 343 | button_press(); // Simulate button press |
Osamu Koizumi |
24:c5412f9149fe | 344 | continue; |
Osamu Koizumi |
24:c5412f9149fe | 345 | } |
Osamu Koizumi |
24:c5412f9149fe | 346 | deregister_client(); |
Osamu Koizumi |
24:c5412f9149fe | 347 | break; |
Osamu Koizumi |
24:c5412f9149fe | 348 | } |
Osamu Koizumi |
24:c5412f9149fe | 349 | return 0; |
MACRUM | 0:6d2053b84a92 | 350 | } |
Osamu Koizumi |
24:c5412f9149fe | 351 | |
Osamu Koizumi |
24:c5412f9149fe | 352 | #endif /* MBED_TEST_MODE */ |