
Simple Mbed Cloud Client application using features of K64 & K66
Connect to Mbed Cloud!
This example was customized a bit for FRDM-K66 and FRDM-K64F.
It depends on having an SD Card plugged in for storage of credentials. It could be changed later to use a SPI flash or other storage on a shield or wired in.
The app keeps track of how many times switch 2 (SW2) is pressed. The value can be retrieved via a GET request to Mbed Cloud.
Also, it will blink a pattern based on milisecond (ms) timing values that can be sent from Mbed Cloud. The pattern can be sent with a PUT request and the blinking sequence can be triggered by a POST request.
Revision 0:e13a8a944e25, committed 2018-02-13
- Comitter:
- MarceloSalazar
- Date:
- Tue Feb 13 10:07:23 2018 +0000
- Child:
- 1:1ccf36276cd3
- Commit message:
- First version
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.md Tue Feb 13 10:07:23 2018 +0000 @@ -0,0 +1,62 @@ +# Simple Mbed Cloud Client application + +### Overview + +This is a reference application for platform vendors. It demonstrates how to create a simple Mbed Cloud Client application that can connect to Mbed Cloud, register resources and get ready to receive a firmware update. + +It's intenteded to be forked and customized to add platform specific features (sensors, actuators) and configure the connectivity and storage to work **out-of-the-box**. +The Simple Mbed Cloud Client application is intended to work in **developer mode** only. For production considerations, please read the following [document]. + +### Setup process + +This is a summary of the process for developers to get started and get a device connected to Mbed Cloud. + +#### Mbed Online IDE + +- Import application into the Online IDE +- Add API key to stablish connection to Mbed Cloud +- Install developer certificate +- Compile & program + +#### Mbed CLI tools + +- Import application in developer's desktop: + +``` +mbed import https://github.com/ARMmbed/simple-mbed-cloud-client-example +cd simple-mbed-cloud-client-example +``` +- Download developer certificate from Mbed Cloud + +- Compile & program + +``` +mbed compile -t <toolchain> -m <target> -c -f +``` + +### Porting to a new platform + +The hardware requirements for Mbed OS platforms to support Mbed Cloud Client as shown [here]. +This template application works with the [FRDM-K64F](https://os.mbed.com/platforms/FRDM-K64F/) platform by default. + +However, adding a new platform requires the following: + +- Fork the template and create an example application for your platform in https://os.mbed.com +- Modify `mbed_app.json` with corresponding addresses to match your platform memory map. + +``` + "K64F": { + "target.mbed_app_start": "0x00020400", + "update-client.application-details": "0x00020000", + "update-client.bootloader-details": "0x172e4" + } +``` +- [Optional] Change connectivity interface. By default uses Ethernet. See lines [xxx]. +- [Optional] Change the filesystem and/or the block device for storage. By default uses FAT filesystem over SD card. See lines [xxx]. +- Create a [bootloader binary](TBD) according to addresses specified in `mbed_app.json`. The booloader has to use the same storage configuration specified in the application. + +### Known issues + +None. + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/combine.sh Tue Feb 13 10:07:23 2018 +0000 @@ -0,0 +1,1 @@ +python combine_bootloader_with_app.py -b mbed-bootloader-k64f.bin -a BUILD/K64F/GCC_ARM/simple-mbed-cloud-client-example.bin --app-offset 0x20400 --header-offset 0x20000 -o combined.bin
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/combine_bootloader_with_app.py Tue Feb 13 10:07:23 2018 +0000 @@ -0,0 +1,253 @@ +#!/usr/bin/env python + +## ---------------------------------------------------------------------------- +## Copyright 2016-2017 ARM Ltd. +## +## SPDX-License-Identifier: Apache-2.0 +## +## Licensed under the Apache License, Version 2.0 (the "License"); +## you may not use this file except in compliance with the License. +## You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## ---------------------------------------------------------------------------- + +from os import path +import json +import hashlib, zlib, struct +import time +import sys + +''' +define FIRMWARE_HEADER_MAGIC 0x5a51b3d4UL +define FIRMWARE_HEADER_VERSION 2 +define ARM_UC_SHA512_SIZE (512/8) +define ARM_UC_GUID_SIZE (128/8) +typedef struct _arm_uc_internal_header_t +{ + /* Metadata-header specific magic code */ + uint32_t headerMagic; + + /* Revision number for metadata header. */ + uint32_t headerVersion; + + /* Version number accompanying the firmware. Larger numbers imply more + recent and preferred versions. This is used for determining the + selection order when multiple versions are available. For downloaded + firmware the manifest timestamp is used as the firmware version. + */ + uint64_t firmwareVersion; + + /* Total space (in bytes) occupied by the firmware BLOB. */ + uint64_t firmwareSize; + + /* Firmware hash calculated over the firmware size. Should match the hash + generated by standard command line tools, e.g., shasum on Linux/Mac. + */ + uint8_t firmwareHash[ARM_UC_SHA512_SIZE]; + + /* The ID for the update campaign that resulted in the firmware update. + */ + uint8_t campaign[ARM_UC_GUID_SIZE]; + + /* Size of the firmware signature. Must be 0 if no signature is supplied. */ + uint32_t firmwareSignatureSize; + + /* Header 32 bit CRC. Calculated over the entire header, including the CRC + field, but with the CRC set to zero. + */ + uint32_t headerCRC; + + /* Optional firmware signature. Hashing algorithm should be the same as the + one used for the firmware hash. The firmwareSignatureSize must be set. + */ + uint8_t firmwareSignature[0]; +} arm_uc_internal_header_t; +''' + +# define defaults to go into the metadata header +SIZEOF_SHA512 = int(512/8) +SIZEOF_GUID = int(128/8) +FIRMWARE_HEADER_MAGIC = 0x5a51b3d4 +FIRMWARE_HEADER_VERSION = 2 +header_format = ">2I2Q{}s{}s2I".format(SIZEOF_SHA512, SIZEOF_GUID) + +if sys.version_info < (3,): + def b(x): + return bytearray(x) +else: + def b(x): + return x + +def create_header(app_blob, firmwareVersion): + # calculate the hash of the application + firmwareHash = hashlib.sha256(app_blob).digest() + + # calculate the total size which is defined as the application size + metadata header + firmwareSize = len(app_blob) + + # set campaign GUID to 0 + campaign = b'\00' + + # signature not supported, set size to 0 + signatureSize = 0 + + print ('imageSize: {}'.format(firmwareSize)) + print ('imageHash: {}'.format(''.join(['{:0>2x}'.format(c) for c in b(firmwareHash)]))) + print ('imageversion: {}'.format(firmwareVersion)) + + # construct struct for CRC calculation + headerCRC = 0 + FirmwareHeader = struct.pack(header_format, + FIRMWARE_HEADER_MAGIC, + FIRMWARE_HEADER_VERSION, + firmwareVersion, + firmwareSize, + firmwareHash, + campaign, + signatureSize, + headerCRC) + + # calculate checksum over header, including signatureSize but without headerCRC + headerCRC = zlib.crc32(FirmwareHeader[:-4]) & 0xffffffff + + # Pack the data into a binary blob + FirmwareHeader = struct.pack(header_format, + FIRMWARE_HEADER_MAGIC, + FIRMWARE_HEADER_VERSION, + firmwareVersion, + firmwareSize, + firmwareHash, + campaign, + signatureSize, + headerCRC) + + return FirmwareHeader + + +def combine(bootloader_blob, app_blob, app_offset, hdr_offset, output, version): + + # create header to go with the application binary + FirmwareHeader = create_header(app_blob, version) + + # write the bootloader first + offset = 0 + output.write(bootloader_blob) + offset += len(bootloader_blob) + + # write the padding between bootloader and firmware header + output.write(b'\00' * (hdr_offset - offset)) + offset += (hdr_offset - offset) + + # write firmware header + output.write(FirmwareHeader) + offset += len(FirmwareHeader) + + # write padding between header and application + output.write(b'\00' * (app_offset - offset)) + + # write the application + output.write(app_blob) + + +if __name__ == '__main__': + import argparse + + parser = argparse.ArgumentParser( + description='Combine bootloader with application adding metadata header.') + + def offset_arg(s): + if s.startswith('0x'): + return int(s, 16) + else: + return int(s) + + bin_map = { + 'k64f': { + 'mem_start': '0x0' + }, + 'ublox_evk_odin_w2': { + 'mem_start': '0x08000000' + }, + 'nucleo_f429zi': { + 'mem_start': '0x08000000' + } + } + + curdir = path.dirname(path.abspath(__file__)) + + def parse_mbed_app_offset(mcu, key): + mem_start = bin_map[mcu]["mem_start"] + with open(path.join(curdir, "..", "mbed_app.json")) as fd: + mbed_json = json.load(fd) + offset = mbed_json["target_overrides"][mcu.upper()][key] + return offset_arg(offset) - offset_arg(mem_start) + + # specify arguments + parser.add_argument('-m', '--mcu', type=lambda s : s.lower().replace("-","_"), required=False, + help='mcu', choices=bin_map.keys()) + parser.add_argument('-b', '--bootloader', type=argparse.FileType('rb'), required=False, + help='path to the bootloader binary') + parser.add_argument('-a', '--app', type=argparse.FileType('rb'), required=True, + help='path to application binary') + parser.add_argument('-c', '--app-offset', type=offset_arg, required=False, + help='offset of the application') + parser.add_argument('-d', '--header-offset', type=offset_arg, required=False, + help='offset of the firmware metadata header') + parser.add_argument('-o', '--output', type=argparse.FileType('wb'), required=True, + help='output combined file path') + parser.add_argument('-s', '--set-version', type=int, required=False, + help='set version number', default=int(time.time())) + + # workaround for http://bugs.python.org/issue9694 + parser._optionals.title = "arguments" + + # get and validate arguments + args = parser.parse_args() + + if(not (args.mcu or args.bootloader)): + print("Please specify bootloader location or MCU") + exit(-1) + + fname = '' + if args.mcu: + fname = path.join(curdir, 'mbed-bootloader-' + args.mcu.replace('_', '-')+'.bin') + if not path.exists(fname): + print("Specified MCU does not have a binary in this location") + exit(-1) + + # Use specified bootloader or default if none is provided + bootloader = args.bootloader or open(fname, 'rb') + + # read the contents of bootloader and application binaries into buffers + bootloader_blob = bootloader.read() + bootloader.close() + app_blob = args.app.read() + args.app.close() + + # Use specified offsets or default if none are provided + if(not (args.mcu or args.app_offset)): + print("Please specify app offset or MCU") + exit(-1) + app_offset = args.app_offset or parse_mbed_app_offset(args.mcu, "target.mbed_app_start") + + if(not (args.mcu or args.header_offset)): + print("Please specify header offset or MCU") + exit(-1) + header_offset = args.header_offset or parse_mbed_app_offset(args.mcu, "update-client.application-details") + + # combine application and bootloader adding metadata info + combine(bootloader_blob, app_blob, app_offset, header_offset, args.output, args.set_version) + + # close output file + output_fn = path.abspath(args.output.name) + args.output.close() + + # print the output file path + print('Combined binary:' + output_fn)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Feb 13 10:07:23 2018 +0000 @@ -0,0 +1,156 @@ +// ---------------------------------------------------------------------------- +// Copyright 2016-2017 ARM Ltd. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------- + +#include "simple-mbed-cloud-client.h" +#include "mbed.h" + +// Pointers to the resources that will be created in main_application(). +static M2MResource* button_res; +static M2MResource* pattern_res; +static M2MResource* blink_res; + +// Pointer to mbedClient, used for calling close function. +static SimpleMbedCloudClient *client; + +void pattern_updated(const char *) + { + printf("PUT received, new value: %s\n", pattern_res->get_value_string().c_str()); +} + +void blink_callback(void *) { + String pattern_string = pattern_res->get_value_string(); + const char *pattern = pattern_string.c_str(); + printf("LED pattern = %s\n", pattern); + // The pattern is something like 500:200:500, so parse that. + // LED blinking is done while parsing. + toggle_led(); + while (*pattern != '\0') { + // Wait for requested time. + do_wait(atoi(pattern)); + toggle_led(); + // Search for next value. + pattern = strchr(pattern, ':'); + if(!pattern) { + break; // while + } + pattern++; + } + led_off(); +} + +void button_notification_status_callback(const M2MBase& object, const NoticationDeliveryStatus status) +{ + switch(status) { + case NOTIFICATION_STATUS_BUILD_ERROR: + printf("Notification callback: (%s) error when building CoAP message\n", object.uri_path()); + break; + case NOTIFICATION_STATUS_RESEND_QUEUE_FULL: + printf("Notification callback: (%s) CoAP resend queue full\n", object.uri_path()); + break; + case NOTIFICATION_STATUS_SENT: + printf("Notification callback: (%s) Notification sent to server\n", object.uri_path()); + break; + case NOTIFICATION_STATUS_DELIVERED: + printf("Notification callback: (%s) Notification delivered\n", object.uri_path()); + break; + case NOTIFICATION_STATUS_SEND_FAILED: + printf("Notification callback: (%s) Notification sending failed\n", object.uri_path()); + break; + case NOTIFICATION_STATUS_SUBSCRIBED: + printf("Notification callback: (%s) subscribed\n", object.uri_path()); + break; + case NOTIFICATION_STATUS_UNSUBSCRIBED: + printf("Notification callback: (%s) subscription removed\n", object.uri_path()); + break; + default: + break; + } +} + +// This function is called when a POST request is received for resource 5000/0/1. +void unregister(void *) +{ + printf("Unregister resource executed\n"); + client->close(); +} + +// This function is called when a POST request is received for resource 5000/0/2. +void factory_reset(void *) +{ + printf("Factory reset resource executed\n"); + client->close(); + kcm_status_e kcm_status = kcm_factory_reset(); + if (kcm_status != KCM_STATUS_SUCCESS) { + printf("Failed to do factory reset - %d\n", kcm_status); + } else { + printf("Factory reset completed. Now restart the device\n"); + } +} + +int main(void) +{ + // IOTMORF-1712: DAPLINK starts the previous application during flashing a new binary + // This is workaround to prevent possible deletion of credentials or storage corruption + // while replacing the application binary. + wait(2); + + // SimpleClient is used for registering and unregistering resources to a server. + SimpleMbedCloudClient mbedClient; + + // Save pointer to mbedClient so that other functions can access it. + client = &mbedClient; + + printf("Client initialized\r\n"); +#ifdef MBED_HEAP_STATS_ENABLED + heap_stats(); +#endif + + // Create resource for button count. Path of this resource will be: 3200/0/5501. + button_res = mbedClient.add_cloud_resource(3200, 0, 5501, "button_resource", M2MResourceInstance::INTEGER, + M2MBase::GET_ALLOWED, 0, true, NULL, (void*)button_notification_status_callback); + + // Create resource for led blinking pattern. Path of this resource will be: 3201/0/5853. + pattern_res = mbedClient.add_cloud_resource(3201, 0, 5853, "pattern_resource", M2MResourceInstance::STRING, + M2MBase::GET_PUT_ALLOWED, "500:500:500:500", false, (void*)pattern_updated, NULL); + + // Create resource for starting the led blinking. Path of this resource will be: 3201/0/5850. + blink_res = mbedClient.add_cloud_resource(3201, 0, 5850, "blink_resource", M2MResourceInstance::STRING, + M2MBase::POST_ALLOWED, "", false, (void*)blink_callback, NULL); + + // Create resource for unregistering the device. Path of this resource will be: 5000/0/1. + mbedClient.add_cloud_resource(5000, 0, 1, "unregister", M2MResourceInstance::STRING, + M2MBase::POST_ALLOWED, NULL, false, (void*)unregister, NULL); + + // Create resource for running factory reset for the device. Path of this resource will be: 5000/0/2. + mbedClient.add_cloud_resource(5000, 0, 2, "factory_reset", M2MResourceInstance::STRING, + M2MBase::POST_ALLOWED, NULL, false, (void*)factory_reset, NULL); + + mbedClient.register_and_connect(); + + // Check if client is registering or registered, if true sleep and repeat. + while (mbedClient.is_register_called()) { + static int button_count = 0; + do_wait(100); + if (button_clicked()) { + button_res->set_value(++button_count); + } + } + + // Client unregistered, exit program. + return 0; +}
Binary file mbed-bootloader-k64f.bin has changed
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Tue Feb 13 10:07:23 2018 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#caeaa49d68c67ee00275cece10cd88e0ed0f6ed3
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed_app.json Tue Feb 13 10:07:23 2018 +0000 @@ -0,0 +1,36 @@ +{ + "macros": [ + "MBEDTLS_USER_CONFIG_FILE=\"mbedTLSConfig_mbedOS.h\"", + "MBED_CLIENT_USER_CONFIG_FILE=\"mbed_cloud_client_user_config.h\"", + "MBED_CLOUD_CLIENT_USER_CONFIG_FILE=\"mbed_cloud_client_user_config.h\"", + "PAL_DTLS_PEER_MIN_TIMEOUT=5000", + "MBED_CONF_APP_MAIN_STACK_SIZE=4608" + ], + "target_overrides": { + "*": { + "platform.stdio-baud-rate": 115200, + "platform.stdio-convert-newlines": true, + "mbed-client.event-loop-size": 1024, + "mbed-trace.enable": null + }, + "K64F": { + "target.mbed_app_start": "0x00020400", + "update-client.application-details": "0x00020000", + "update-client.bootloader-details": "0x172e4" + } + }, + "config": { + "developer-mode": { + "help": "Enable Developer mode to skip Factory enrollment", + "value": 1 + }, + "button-pinname": { + "help": "PinName for button.", + "value": "BUTTON1" + }, + "led-pinname": { + "help": "PinName for led, which is attached to led blink resource.", + "value": "LED_RED" + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed_cloud_dev_credentials.c Tue Feb 13 10:07:23 2018 +0000 @@ -0,0 +1,277 @@ +/* + * Copyright (c) 2017 ARM Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the License); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef __MBED_CLOUD_DEV_CREDENTIALS_H__ +#define __MBED_CLOUD_DEV_CREDENTIALS_H__ + +#include <inttypes.h> + +const char MBED_CLOUD_DEV_BOOTSTRAP_ENDPOINT_NAME[] = "015d75eb0fe302420a01640503c00000"; +const char MBED_CLOUD_DEV_ACCOUNT_ID[] = "015b5c9279be02420a01041200000000"; +const char MBED_CLOUD_DEV_BOOTSTRAP_SERVER_URI[] = "coaps://coap-systemtest.dev.mbed.com:5684?aid=015b5c9279be02420a01041200000000"; + +const uint8_t MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_CERTIFICATE[] = +{ 0x30, 0x82, 0x02, 0x18, 0x30, 0x82, 0x01, 0xbd, + 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x11, 0x00, + 0xa0, 0x27, 0xa6, 0xfd, 0xac, 0x10, 0x4c, 0xae, + 0x86, 0x93, 0x10, 0xfc, 0xe3, 0xfa, 0xfe, 0xe8, + 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, + 0x3d, 0x04, 0x03, 0x02, 0x30, 0x77, 0x31, 0x0b, + 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, + 0x02, 0x47, 0x42, 0x31, 0x17, 0x30, 0x15, 0x06, + 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0e, 0x43, 0x61, + 0x6d, 0x62, 0x72, 0x69, 0x64, 0x67, 0x65, 0x73, + 0x68, 0x69, 0x72, 0x65, 0x31, 0x12, 0x30, 0x10, + 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x09, 0x43, + 0x61, 0x6d, 0x62, 0x72, 0x69, 0x64, 0x67, 0x65, + 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, + 0x0a, 0x0c, 0x07, 0x41, 0x52, 0x4d, 0x20, 0x4c, + 0x74, 0x64, 0x31, 0x29, 0x30, 0x27, 0x06, 0x03, + 0x55, 0x04, 0x03, 0x0c, 0x20, 0x30, 0x31, 0x35, + 0x64, 0x37, 0x35, 0x65, 0x62, 0x30, 0x66, 0x65, + 0x33, 0x30, 0x32, 0x34, 0x32, 0x30, 0x61, 0x30, + 0x31, 0x36, 0x34, 0x30, 0x35, 0x30, 0x33, 0x63, + 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x1e, 0x17, + 0x0d, 0x31, 0x37, 0x30, 0x37, 0x32, 0x34, 0x31, + 0x38, 0x34, 0x35, 0x32, 0x35, 0x5a, 0x17, 0x0d, + 0x32, 0x37, 0x30, 0x37, 0x32, 0x34, 0x31, 0x38, + 0x34, 0x35, 0x32, 0x35, 0x5a, 0x30, 0x77, 0x31, + 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, + 0x13, 0x02, 0x47, 0x42, 0x31, 0x17, 0x30, 0x15, + 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0e, 0x43, + 0x61, 0x6d, 0x62, 0x72, 0x69, 0x64, 0x67, 0x65, + 0x73, 0x68, 0x69, 0x72, 0x65, 0x31, 0x12, 0x30, + 0x10, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x09, + 0x43, 0x61, 0x6d, 0x62, 0x72, 0x69, 0x64, 0x67, + 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, + 0x04, 0x0a, 0x0c, 0x07, 0x41, 0x52, 0x4d, 0x20, + 0x4c, 0x74, 0x64, 0x31, 0x29, 0x30, 0x27, 0x06, + 0x03, 0x55, 0x04, 0x03, 0x0c, 0x20, 0x30, 0x31, + 0x35, 0x64, 0x37, 0x35, 0x65, 0x62, 0x30, 0x66, + 0x65, 0x33, 0x30, 0x32, 0x34, 0x32, 0x30, 0x61, + 0x30, 0x31, 0x36, 0x34, 0x30, 0x35, 0x30, 0x33, + 0x63, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x59, + 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, + 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, + 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, + 0x04, 0x65, 0x4b, 0x1c, 0x37, 0xd6, 0x47, 0x10, + 0x8d, 0x8a, 0x3b, 0xa9, 0xd9, 0xa8, 0xba, 0x89, + 0xe2, 0x56, 0x2a, 0x0a, 0x07, 0xbb, 0x46, 0xb5, + 0xff, 0x58, 0x91, 0x81, 0xd2, 0x8f, 0xa2, 0xc4, + 0xe4, 0x3f, 0x3b, 0xdd, 0x66, 0x73, 0x79, 0x64, + 0xf5, 0x73, 0x0f, 0x41, 0x47, 0xa9, 0x6e, 0x4f, + 0x75, 0x49, 0xa9, 0x53, 0x6a, 0xeb, 0xa4, 0xd7, + 0x6b, 0x37, 0xd9, 0xec, 0x48, 0xe5, 0xce, 0xab, + 0x3e, 0xa3, 0x2a, 0x30, 0x28, 0x30, 0x12, 0x06, + 0x09, 0x2b, 0x06, 0x01, 0x04, 0x01, 0xa0, 0x20, + 0x81, 0x49, 0x04, 0x05, 0x02, 0x03, 0x40, 0x00, + 0x91, 0x30, 0x12, 0x06, 0x03, 0x55, 0x1d, 0x13, + 0x01, 0x01, 0xff, 0x04, 0x08, 0x30, 0x06, 0x01, + 0x01, 0xff, 0x02, 0x01, 0x00, 0x30, 0x0a, 0x06, + 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, + 0x02, 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, + 0x00, 0xfc, 0x47, 0x35, 0x0e, 0x23, 0xaf, 0x34, + 0x04, 0xc9, 0x1b, 0xed, 0x43, 0x80, 0x81, 0xed, + 0xfb, 0x2f, 0x1d, 0x8b, 0x1b, 0x96, 0xcd, 0xdd, + 0xa7, 0x85, 0xb6, 0x72, 0x19, 0xc7, 0x57, 0xd5, + 0x18, 0x02, 0x21, 0x00, 0x98, 0x31, 0x66, 0x9c, + 0x49, 0x14, 0x22, 0x6c, 0xa0, 0x82, 0x8d, 0x37, + 0x90, 0x1b, 0x9a, 0xd3, 0x5d, 0x65, 0x07, 0xb7, + 0x99, 0x3d, 0xcd, 0x4a, 0x42, 0x4e, 0x20, 0xe3, + 0xdb, 0x3d, 0x43, 0xce }; + +const uint8_t MBED_CLOUD_DEV_BOOTSTRAP_SERVER_ROOT_CA_CERTIFICATE[] = +{ 0x30, 0x82, 0x02, 0x32, 0x30, 0x82, 0x01, 0xd9, + 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x10, 0x45, + 0x5e, 0x28, 0x41, 0x2b, 0xca, 0xf1, 0xb1, 0x4e, + 0xea, 0xad, 0x06, 0x25, 0x6a, 0xd8, 0x4a, 0x30, + 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, + 0x04, 0x03, 0x02, 0x30, 0x71, 0x31, 0x0b, 0x30, + 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, + 0x47, 0x42, 0x31, 0x17, 0x30, 0x15, 0x06, 0x03, + 0x55, 0x04, 0x08, 0x13, 0x0e, 0x43, 0x61, 0x6d, + 0x62, 0x72, 0x69, 0x64, 0x67, 0x65, 0x73, 0x68, + 0x69, 0x72, 0x65, 0x31, 0x12, 0x30, 0x10, 0x06, + 0x03, 0x55, 0x04, 0x07, 0x13, 0x09, 0x43, 0x61, + 0x6d, 0x62, 0x72, 0x69, 0x64, 0x67, 0x65, 0x31, + 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, + 0x13, 0x07, 0x41, 0x52, 0x4d, 0x20, 0x4c, 0x74, + 0x64, 0x31, 0x23, 0x30, 0x21, 0x06, 0x03, 0x55, + 0x04, 0x03, 0x13, 0x1a, 0x41, 0x52, 0x4d, 0x20, + 0x4f, 0x66, 0x66, 0x69, 0x63, 0x69, 0x61, 0x6c, + 0x53, 0x20, 0x42, 0x6f, 0x6f, 0x74, 0x73, 0x74, + 0x72, 0x61, 0x70, 0x20, 0x43, 0x41, 0x30, 0x20, + 0x17, 0x0d, 0x31, 0x37, 0x30, 0x33, 0x32, 0x30, + 0x31, 0x35, 0x31, 0x31, 0x33, 0x33, 0x5a, 0x18, + 0x0f, 0x32, 0x30, 0x35, 0x32, 0x30, 0x33, 0x32, + 0x30, 0x31, 0x35, 0x32, 0x31, 0x33, 0x33, 0x5a, + 0x30, 0x71, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, + 0x55, 0x04, 0x06, 0x13, 0x02, 0x47, 0x42, 0x31, + 0x17, 0x30, 0x15, 0x06, 0x03, 0x55, 0x04, 0x08, + 0x13, 0x0e, 0x43, 0x61, 0x6d, 0x62, 0x72, 0x69, + 0x64, 0x67, 0x65, 0x73, 0x68, 0x69, 0x72, 0x65, + 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, + 0x07, 0x13, 0x09, 0x43, 0x61, 0x6d, 0x62, 0x72, + 0x69, 0x64, 0x67, 0x65, 0x31, 0x10, 0x30, 0x0e, + 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x07, 0x41, + 0x52, 0x4d, 0x20, 0x4c, 0x74, 0x64, 0x31, 0x23, + 0x30, 0x21, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, + 0x1a, 0x41, 0x52, 0x4d, 0x20, 0x4f, 0x66, 0x66, + 0x69, 0x63, 0x69, 0x61, 0x6c, 0x53, 0x20, 0x42, + 0x6f, 0x6f, 0x74, 0x73, 0x74, 0x72, 0x61, 0x70, + 0x20, 0x43, 0x41, 0x30, 0x59, 0x30, 0x13, 0x06, + 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, + 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, + 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0xf7, 0xdc, + 0x05, 0x70, 0x4f, 0x1b, 0x9d, 0xa8, 0x66, 0x52, + 0xf0, 0xb4, 0x99, 0x05, 0xe3, 0x89, 0x73, 0x08, + 0x4e, 0x23, 0x67, 0xdb, 0x6b, 0xac, 0x5a, 0xbe, + 0xab, 0xb0, 0x06, 0x49, 0xff, 0xd6, 0xc5, 0xd0, + 0x82, 0xbd, 0x45, 0xd5, 0x1b, 0xc2, 0x2f, 0x39, + 0x02, 0x3c, 0xf2, 0xa5, 0x42, 0x78, 0xf7, 0x55, + 0x9e, 0x9f, 0xdb, 0x3b, 0x77, 0xba, 0x0e, 0xa1, + 0x9f, 0x93, 0xcc, 0x73, 0x97, 0x99, 0xa3, 0x51, + 0x30, 0x4f, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x1d, + 0x0f, 0x04, 0x04, 0x03, 0x02, 0x01, 0x86, 0x30, + 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, + 0xff, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xff, + 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, + 0x16, 0x04, 0x14, 0xd5, 0x67, 0x40, 0xe7, 0xe2, + 0x8e, 0x96, 0x60, 0xb1, 0xb7, 0xbc, 0x68, 0xe9, + 0x76, 0xc9, 0x0e, 0xa4, 0xe6, 0x90, 0x9a, 0x30, + 0x10, 0x06, 0x09, 0x2b, 0x06, 0x01, 0x04, 0x01, + 0x82, 0x37, 0x15, 0x01, 0x04, 0x03, 0x02, 0x01, + 0x00, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, + 0xce, 0x3d, 0x04, 0x03, 0x02, 0x03, 0x47, 0x00, + 0x30, 0x44, 0x02, 0x20, 0x09, 0x7d, 0xce, 0x2f, + 0x1c, 0x93, 0xf9, 0x1f, 0x5f, 0x0f, 0xf5, 0x02, + 0x76, 0x7e, 0xa2, 0xf0, 0x5b, 0x1f, 0xc9, 0xe4, + 0x04, 0xae, 0x58, 0xf0, 0xd6, 0x3d, 0xea, 0x1a, + 0xf4, 0x81, 0x4d, 0x87, 0x02, 0x20, 0x0c, 0xd4, + 0xbd, 0x67, 0xa4, 0xf4, 0xd6, 0x3d, 0x52, 0xa5, + 0xbe, 0x6d, 0x66, 0x03, 0xc5, 0xb1, 0x29, 0x7e, + 0x9a, 0xb0, 0x19, 0x30, 0x69, 0x9d, 0x7d, 0x72, + 0xb7, 0x88, 0x3c, 0xb9, 0x94, 0x9b }; + +const uint8_t MBED_CLOUD_DEV_LWM2M_SERVER_ROOT_CA_CERTIFICATE[] = +{ 0x30, 0x82, 0x02, 0x1d, 0x30, 0x82, 0x01, 0xc3, + 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x10, 0x48, + 0x74, 0xf6, 0xaf, 0xd3, 0xce, 0x7b, 0xb7, 0x40, + 0xa3, 0x02, 0xc6, 0x6f, 0x4f, 0xa1, 0xed, 0x30, + 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, + 0x04, 0x03, 0x02, 0x30, 0x66, 0x31, 0x0b, 0x30, + 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, + 0x47, 0x42, 0x31, 0x17, 0x30, 0x15, 0x06, 0x03, + 0x55, 0x04, 0x08, 0x13, 0x0e, 0x43, 0x61, 0x6d, + 0x62, 0x72, 0x69, 0x64, 0x67, 0x65, 0x73, 0x68, + 0x69, 0x72, 0x65, 0x31, 0x12, 0x30, 0x10, 0x06, + 0x03, 0x55, 0x04, 0x07, 0x13, 0x09, 0x43, 0x61, + 0x6d, 0x62, 0x72, 0x69, 0x64, 0x67, 0x65, 0x31, + 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, + 0x13, 0x07, 0x41, 0x52, 0x4d, 0x20, 0x4c, 0x74, + 0x64, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, + 0x04, 0x03, 0x13, 0x0f, 0x41, 0x52, 0x4d, 0x20, + 0x4f, 0x53, 0x20, 0x4c, 0x57, 0x4d, 0x32, 0x4d, + 0x20, 0x43, 0x41, 0x30, 0x20, 0x17, 0x0d, 0x31, + 0x37, 0x30, 0x33, 0x32, 0x30, 0x31, 0x35, 0x32, + 0x39, 0x32, 0x32, 0x5a, 0x18, 0x0f, 0x32, 0x30, + 0x35, 0x32, 0x30, 0x33, 0x32, 0x30, 0x31, 0x35, + 0x33, 0x39, 0x32, 0x32, 0x5a, 0x30, 0x66, 0x31, + 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, + 0x13, 0x02, 0x47, 0x42, 0x31, 0x17, 0x30, 0x15, + 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x0e, 0x43, + 0x61, 0x6d, 0x62, 0x72, 0x69, 0x64, 0x67, 0x65, + 0x73, 0x68, 0x69, 0x72, 0x65, 0x31, 0x12, 0x30, + 0x10, 0x06, 0x03, 0x55, 0x04, 0x07, 0x13, 0x09, + 0x43, 0x61, 0x6d, 0x62, 0x72, 0x69, 0x64, 0x67, + 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, + 0x04, 0x0a, 0x13, 0x07, 0x41, 0x52, 0x4d, 0x20, + 0x4c, 0x74, 0x64, 0x31, 0x18, 0x30, 0x16, 0x06, + 0x03, 0x55, 0x04, 0x03, 0x13, 0x0f, 0x41, 0x52, + 0x4d, 0x20, 0x4f, 0x53, 0x20, 0x4c, 0x57, 0x4d, + 0x32, 0x4d, 0x20, 0x43, 0x41, 0x30, 0x59, 0x30, + 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, + 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, + 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, + 0xea, 0xdc, 0xb1, 0xd7, 0xed, 0xbc, 0x98, 0x68, + 0xa3, 0xc6, 0xb3, 0x52, 0xf0, 0x9d, 0xb8, 0xc6, + 0x3f, 0xd5, 0x28, 0x0a, 0xc9, 0xdf, 0xb2, 0xee, + 0xbd, 0x28, 0x83, 0xa4, 0x49, 0x26, 0xb5, 0x9f, + 0xf7, 0x34, 0xdd, 0x2a, 0x44, 0xba, 0x01, 0xec, + 0x1d, 0xdf, 0x83, 0xbb, 0xd6, 0xe4, 0x80, 0x18, + 0x4f, 0x9a, 0x2a, 0x72, 0x37, 0x80, 0x81, 0x40, + 0x91, 0x4b, 0xd6, 0x85, 0x96, 0xee, 0xd4, 0xe7, + 0xa3, 0x51, 0x30, 0x4f, 0x30, 0x0b, 0x06, 0x03, + 0x55, 0x1d, 0x0f, 0x04, 0x04, 0x03, 0x02, 0x01, + 0x86, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x13, + 0x01, 0x01, 0xff, 0x04, 0x05, 0x30, 0x03, 0x01, + 0x01, 0xff, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, + 0x0e, 0x04, 0x16, 0x04, 0x14, 0x38, 0xab, 0x7f, + 0xef, 0x5e, 0xf6, 0x70, 0xe5, 0xab, 0x6d, 0x08, + 0x73, 0xba, 0x48, 0x63, 0x71, 0x59, 0x85, 0x86, + 0x04, 0x30, 0x10, 0x06, 0x09, 0x2b, 0x06, 0x01, + 0x04, 0x01, 0x82, 0x37, 0x15, 0x01, 0x04, 0x03, + 0x02, 0x01, 0x00, 0x30, 0x0a, 0x06, 0x08, 0x2a, + 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x03, + 0x48, 0x00, 0x30, 0x45, 0x02, 0x21, 0x00, 0x83, + 0xb5, 0x3b, 0x4e, 0x00, 0x6b, 0x14, 0x28, 0x08, + 0xc4, 0x9b, 0x9e, 0xb2, 0x1b, 0xbf, 0x69, 0xc8, + 0xc5, 0x63, 0xe1, 0x06, 0xa7, 0x0d, 0xdf, 0x52, + 0xdb, 0xac, 0xb9, 0x73, 0x14, 0x4e, 0x40, 0x02, + 0x20, 0x49, 0xa5, 0x60, 0x11, 0xce, 0x05, 0x6a, + 0x44, 0x97, 0xf1, 0xff, 0x19, 0x04, 0x77, 0x51, + 0x65, 0x04, 0x5b, 0xb1, 0x35, 0xf0, 0xf3, 0xaf, + 0x58, 0xb2, 0x1d, 0xc2, 0x1f, 0x6c, 0x61, 0xb8, + 0x7b }; + +const uint8_t MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_PRIVATE_KEY[] = +{ 0x30, 0x81, 0x93, 0x02, 0x01, 0x00, 0x30, 0x13, + 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, + 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, + 0x03, 0x01, 0x07, 0x04, 0x79, 0x30, 0x77, 0x02, + 0x01, 0x01, 0x04, 0x20, 0xa7, 0x55, 0x4b, 0xf8, + 0x2b, 0xb1, 0x4c, 0xec, 0x06, 0xe0, 0x38, 0xd7, + 0x0d, 0xd8, 0x9c, 0x3b, 0x78, 0x2c, 0xfd, 0x97, + 0xb6, 0xa5, 0x1f, 0xf9, 0xf4, 0xfc, 0x77, 0x12, + 0x0e, 0xdf, 0x5b, 0xfc, 0xa0, 0x0a, 0x06, 0x08, + 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, + 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0x65, 0x4b, + 0x1c, 0x37, 0xd6, 0x47, 0x10, 0x8d, 0x8a, 0x3b, + 0xa9, 0xd9, 0xa8, 0xba, 0x89, 0xe2, 0x56, 0x2a, + 0x0a, 0x07, 0xbb, 0x46, 0xb5, 0xff, 0x58, 0x91, + 0x81, 0xd2, 0x8f, 0xa2, 0xc4, 0xe4, 0x3f, 0x3b, + 0xdd, 0x66, 0x73, 0x79, 0x64, 0xf5, 0x73, 0x0f, + 0x41, 0x47, 0xa9, 0x6e, 0x4f, 0x75, 0x49, 0xa9, + 0x53, 0x6a, 0xeb, 0xa4, 0xd7, 0x6b, 0x37, 0xd9, + 0xec, 0x48, 0xe5, 0xce, 0xab, 0x3e }; + +const char MBED_CLOUD_DEV_MANUFACTURER[] = "dev_manufacturer"; + +const char MBED_CLOUD_DEV_MODEL_NUMBER[] = "dev_model_num"; + +const char MBED_CLOUD_DEV_SERIAL_NUMBER[] = "0"; + +const char MBED_CLOUD_DEV_DEVICE_TYPE[] = "dev_device_type"; + +const char MBED_CLOUD_DEV_HARDWARE_VERSION[] = "dev_hardware_version"; + +const uint32_t MBED_CLOUD_DEV_MEMORY_TOTAL_KB = 0; + +const uint32_t MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_CERTIFICATE_SIZE = sizeof(MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_CERTIFICATE); +const uint32_t MBED_CLOUD_DEV_BOOTSTRAP_SERVER_ROOT_CA_CERTIFICATE_SIZE = sizeof(MBED_CLOUD_DEV_BOOTSTRAP_SERVER_ROOT_CA_CERTIFICATE); +const uint32_t MBED_CLOUD_DEV_LWM2M_SERVER_ROOT_CA_CERTIFICATE_SIZE = sizeof(MBED_CLOUD_DEV_LWM2M_SERVER_ROOT_CA_CERTIFICATE); +const uint32_t MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_PRIVATE_KEY_SIZE = sizeof(MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_PRIVATE_KEY); + +#endif //__MBED_CLOUD_DEV_CREDENTIALS_H__
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sd-driver.lib Tue Feb 13 10:07:23 2018 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/sd-driver/#f4ab55df7768cfcb049b522bebd30218ee729c81
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/setup.cpp Tue Feb 13 10:07:23 2018 +0000 @@ -0,0 +1,352 @@ +// ---------------------------------------------------------------------------- +// Copyright 2016-2017 ARM Ltd. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------- + + +/////////// +// INCLUDES +/////////// + +// Note: this macro is needed on armcc to get the the PRI*32 macros +// from inttypes.h in a C++ code. +#ifndef __STDC_FORMAT_MACROS +#define __STDC_FORMAT_MACROS +#endif + +#include "mbed.h" +#include "setup.h" +#include "memory_tests.h" +#include "EthernetInterface.h" // Networking interface include +#include "simple-mbed-cloud-client.h" +#include "pal.h" +#include "mbed-trace/mbed_trace.h" +#include "mbed-trace-helper.h" +#include "factory_configurator_client.h" + +#include "SDBlockDevice.h" +#include "FATFileSystem.h" + +// Network interface +#include "EthernetInterface.h" +EthernetInterface eth; + +//////////////////////////////////////// +// PLATFORM SPECIFIC DEFINES & FUNCTIONS +//////////////////////////////////////// +#define DEFAULT_FIRMWARE_PATH "/sd/firmware" + +#include "mbed_trace.h" +#define TRACE_GROUP "exam" + + +// Define led on/off +#ifdef TARGET_STM +#define LED_ON (true) +#else // #ifdef TARGET_STM +#define LED_ON (false) +#endif // #ifdef TARGET_STM + +#define LED_OFF (!LED_ON) + +DigitalOut led(MBED_CONF_APP_LED_PINNAME, LED_OFF); +InterruptIn button(MBED_CONF_APP_BUTTON_PINNAME); + +static bool button_pressed = false; +static void button_press(void); + +// Block device and Filesystem +SDBlockDevice sd(PTE3, PTE1, PTE2, PTE4); +FATFileSystem fs("sd", &sd); + + +Thread resource_thread; + +NetworkInterface* network_interface = NULL; + + +void button_press(void) +{ + button_pressed = true; +} + +///////////////////////// +// SETUP.H IMPLEMENTATION +///////////////////////// +int initPlatform() +{ + /* Explicit declaration to catch Block Device initialization errors. */ + int sd_ret = sd.init(); + + if(sd_ret != BD_ERROR_OK) { + tr_error("initPlatform() - sd.init() failed with %d\n", sd_ret); + printf("SD card initialization failed. Verify that SD-card is attached.\n"); + return -1; + } + tr_debug("initPlatform() - BlockDevice init OK.\n"); + + if(MBED_CONF_APP_BUTTON_PINNAME != NC) { + button.fall(&button_press); + } + + return 0; +} + +bool rmFirmwareImages() +{ + palStatus_t status = PAL_SUCCESS; + status = pal_fsRmFiles(DEFAULT_FIRMWARE_PATH); + if(status == PAL_SUCCESS) { + printf("Firmware storage erased.\n"); + } else if (status == PAL_ERR_FS_NO_PATH) { + printf("Firmware path not found/does not exist.\n"); + } else { + printf("Firmware storage erasing failed with %" PRId32, status); + return false; + } + return true; +} + +int reformat_storage() +{ + int reformat_result = -1; + printf("Autoformatting the storage.\n"); + if (1/*sd*/) { + // TODO + // reformat_result = fs.reformat(sd); + if (reformat_result != 0) { + printf("Autoformatting failed with error %d\n", reformat_result); + } + } + return reformat_result; +} + +int run_application(int(*function)(void)) +{ + // application_init() runs the following initializations: + // 1. trace initialization + // 2. platform initialization + // 3. print memory statistics if MBED_HEAP_STATS_ENABLED is defined + // 4. FCC initialization. + if (!application_init()) { + printf("Initialization failed, exiting application!\n"); + return 1; + } + return function(); +} + +// Helper function, could be moved someone sd +void print_MAC(NetworkInterface* network_interface, bool log_messages) { +#if MBED_CONF_APP_NETWORK_INTERFACE != CELLULAR_ONBOARD + const char *mac_addr = network_interface->get_mac_address(); + if (mac_addr == NULL) { + if (log_messages) { + printf("ERROR - No MAC address\n"); + } + return; + } + if (log_messages) { + printf("MAC address %s\n", mac_addr); + } +#endif +} + +bool init_connection() +{ + int connect_success = -1; + bool log_messages = 1; + + srand(time(NULL)); + + printf("Using Ethernet\n"); + + network_interface = ð + connect_success = eth.connect(); + + if(connect_success == 0) { + if (log_messages) { + printf("Connected to Network successfully\n"); + print_MAC(network_interface, log_messages); + } + } else { + if (log_messages) { + print_MAC(network_interface, log_messages); + printf("Connection to Network Failed %d!\n", connect_success); + } + return NULL; + } + const char *ip_addr = network_interface->get_ip_address(); + if (ip_addr == NULL) { + if (log_messages) { + printf("ERROR - No IP address\n"); + } + return NULL; + } + + if (log_messages) { + printf("IP address %s\n", ip_addr); + } + + if(network_interface == NULL) { + return false; + } + return true; +} + +void* get_network_interface() +{ + return network_interface; +} + + +void toggle_led(void) +{ + if (MBED_CONF_APP_LED_PINNAME != NC) { + led = !led; + } + else { + printf("Virtual LED toggled\n"); + } +} + +void led_off(void) +{ + if (MBED_CONF_APP_LED_PINNAME != NC) { + led = LED_OFF; + } + else { + printf("Virtual LED off\n"); + } +} + +uint8_t button_clicked(void) +{ + if (button_pressed) { + button_pressed = false; + return true; + } + return false; +} + +void do_wait(int timeout_ms) +{ + wait_ms(timeout_ms); +} + + +static bool application_init_mbed_trace(void) +{ + // Create mutex for tracing to avoid broken lines in logs + if(!mbed_trace_helper_create_mutex()) { + printf("ERROR - Mutex creation for mbed_trace failed!\n"); + return 1; + } + + // Initialize mbed trace + mbed_trace_init(); + mbed_trace_mutex_wait_function_set(mbed_trace_helper_mutex_wait); + mbed_trace_mutex_release_function_set(mbed_trace_helper_mutex_release); + + return 0; +} + +static void reset_storage(void) +{ + printf("Resets storage to an empty state.\n"); + fcc_status_e delete_status = fcc_storage_delete(); + if (delete_status != FCC_STATUS_SUCCESS) { + printf("Failed to delete storage - %d\n", delete_status); + } +} + +static bool application_init_fcc(void) +{ + fcc_status_e status = fcc_init(); + if(status != FCC_STATUS_SUCCESS) { + printf("fcc_init failed with status %d! - exit\n", status); + return 1; + } + + // This is designed to simplify user-experience by auto-formatting the + // primary storage if no valid certificates exist. + // This should never be used for any kind of production devices. +#ifndef MBED_CONF_APP_MCC_NO_AUTO_FORMAT + status = fcc_verify_device_configured_4mbed_cloud(); + if (status != FCC_STATUS_SUCCESS) { + if (reformat_storage() != 0) { + return 1; + } + reset_storage(); + } +#endif + + // Resets storage to an empty state. + // Use this function when you want to clear storage from all the factory-tool generated data and user data. + // After this operation device must be injected again by using factory tool or developer certificate. +#ifdef RESET_STORAGE + reset_storage(); +#endif + + // Deletes existing firmware images from storage. + // This deletes any existing firmware images during application startup. + // This compilation flag is currently implemented only for mbed OS. +#ifdef RESET_FIRMWARE + bool status_erase = rmFirmwareImages(); + if(status_erase == false) { + return 1; + } +#endif + +#if MBED_CONF_APP_DEVELOPER_MODE == 1 + printf("Start developer flow\n"); + status = fcc_developer_flow(); + if (status == FCC_STATUS_KCM_FILE_EXIST_ERROR) { + printf("Developer credentials already exists\n"); + } else if (status != FCC_STATUS_SUCCESS) { + printf("Failed to load developer credentials - exit\n"); + return 1; + } +#endif + status = fcc_verify_device_configured_4mbed_cloud(); + if (status != FCC_STATUS_SUCCESS) { + printf("Device not configured for mbed Cloud - exit\n"); + return 1; + } + + return 0; +} + +bool application_init(void) +{ + if (application_init_mbed_trace() != 0) { + printf("Failed initializing mbed trace\n" ); + return false; + } + + if(initPlatform() != 0) { + printf("ERROR - initPlatform() failed!\n"); + return false; + } + + printf("Start Simple Mbed Cloud Client\n"); + + if (application_init_fcc() != 0) { + printf("Failed initializing FCC\n" ); + return false; + } + + return true; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/setup.h Tue Feb 13 10:07:23 2018 +0000 @@ -0,0 +1,95 @@ +// ---------------------------------------------------------------------------- +// Copyright 2016-2017 ARM Ltd. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------- + +#ifndef SETUP_H +#define SETUP_H + +#include <stdint.h> + +//FORWARD DECLARATION +class M2MObject; +namespace m2m { + template<class ObjectTemplate> class Vector; +} + +using namespace m2m; + +typedef Vector<M2MObject *> M2MObjectList; + +#ifdef __cplusplus +extern "C" { +#endif + +// Interval to update resource value in ms +#define INCREMENT_INTERVAL 25000 + +typedef void (*main_t)(void); + +// Initialize platform +// This function initializes screen and any other non-network +// related platform specific initializations required. +// +// @returns +// 0 for success, anything else for error +extern int initPlatform(); + +// Initialize network connection +extern bool init_connection(); + +// Returns network interface. +extern void *get_network_interface(); + +// Toggle led (if available) +extern void toggle_led(void); + +// Put led off (if available) +extern void led_off(void); + +// Check if button has been pressed (if available) +extern uint8_t button_clicked(void); + +// Thread for updating resource value +extern void increment_resource_thread(void* client); + +// Print heap allocations +extern void print_heap_stats(); + +// Print m2mobject sizes +extern void print_m2mobject_stats(); + +// Create set of objects to test size +extern void create_m2mobject_test_set(M2MObjectList *object_list); + +// Wait +extern void do_wait(int timeout_ms); + +int run_application(int(*function)(void)); + +extern bool runProgram(main_t mainFunc); + +extern bool application_init(void); + +extern bool rmFirmwareImages(void); + +extern int reformat_storage(void); + +#ifdef __cplusplus +} +#endif + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/simple-mbed-cloud-client.lib Tue Feb 13 10:07:23 2018 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/simple-mbed-cloud-client/#919bc54a33df12591398d4602a583a61d97c53f7