FRDM K64F Metronome

Committer:
ram54288
Date:
Sun May 14 18:35:07 2017 +0000
Revision:
0:a2cb7295a1f7
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ram54288 0:a2cb7295a1f7 1 /*
ram54288 0:a2cb7295a1f7 2 * Copyright (c) 2015 ARM Limited. All rights reserved.
ram54288 0:a2cb7295a1f7 3 * SPDX-License-Identifier: Apache-2.0
ram54288 0:a2cb7295a1f7 4 * Licensed under the Apache License, Version 2.0 (the License); you may
ram54288 0:a2cb7295a1f7 5 * not use this file except in compliance with the License.
ram54288 0:a2cb7295a1f7 6 * You may obtain a copy of the License at
ram54288 0:a2cb7295a1f7 7 *
ram54288 0:a2cb7295a1f7 8 * http://www.apache.org/licenses/LICENSE-2.0
ram54288 0:a2cb7295a1f7 9 *
ram54288 0:a2cb7295a1f7 10 * Unless required by applicable law or agreed to in writing, software
ram54288 0:a2cb7295a1f7 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
ram54288 0:a2cb7295a1f7 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ram54288 0:a2cb7295a1f7 13 * See the License for the specific language governing permissions and
ram54288 0:a2cb7295a1f7 14 * limitations under the License.
ram54288 0:a2cb7295a1f7 15 */
ram54288 0:a2cb7295a1f7 16 #include <unistd.h>
ram54288 0:a2cb7295a1f7 17 #include "mbed-client/m2minterfacefactory.h"
ram54288 0:a2cb7295a1f7 18 #include "mbed-client/m2minterface.h"
ram54288 0:a2cb7295a1f7 19 #include "mbed-client/m2mdevice.h"
ram54288 0:a2cb7295a1f7 20 #include "mbed-client/m2minterfaceobserver.h"
ram54288 0:a2cb7295a1f7 21 #include "mbed-client/m2mobjectinstance.h"
ram54288 0:a2cb7295a1f7 22 #include "mbed-client/m2mresource.h"
ram54288 0:a2cb7295a1f7 23 #include "mbed-client/m2mfirmware.h"
ram54288 0:a2cb7295a1f7 24
ram54288 0:a2cb7295a1f7 25 class M2MLWClient: public M2MInterfaceObserver {
ram54288 0:a2cb7295a1f7 26 public:
ram54288 0:a2cb7295a1f7 27 M2MLWClient();
ram54288 0:a2cb7295a1f7 28
ram54288 0:a2cb7295a1f7 29 ~M2MLWClient();
ram54288 0:a2cb7295a1f7 30
ram54288 0:a2cb7295a1f7 31 bool create_interface(int32_t param_count,
ram54288 0:a2cb7295a1f7 32 const char *endpoint,
ram54288 0:a2cb7295a1f7 33 const char *resource_type,
ram54288 0:a2cb7295a1f7 34 const int32_t lifetime,
ram54288 0:a2cb7295a1f7 35 const uint16_t listen_port,
ram54288 0:a2cb7295a1f7 36 const char *domain,
ram54288 0:a2cb7295a1f7 37 const uint8_t binding_mode,
ram54288 0:a2cb7295a1f7 38 const uint8_t network_interface);
ram54288 0:a2cb7295a1f7 39
ram54288 0:a2cb7295a1f7 40 bool create_bootstrap_object(const char *coap_bootstrap_address);
ram54288 0:a2cb7295a1f7 41
ram54288 0:a2cb7295a1f7 42 bool create_register_object(const char *coap_register_address, bool useSecureConn);
ram54288 0:a2cb7295a1f7 43
ram54288 0:a2cb7295a1f7 44 bool test_bootstrap();
ram54288 0:a2cb7295a1f7 45
ram54288 0:a2cb7295a1f7 46 bool create_device_object();
ram54288 0:a2cb7295a1f7 47
ram54288 0:a2cb7295a1f7 48 bool create_device_object(M2MDevice::DeviceResource resource,
ram54288 0:a2cb7295a1f7 49 const char *value);
ram54288 0:a2cb7295a1f7 50
ram54288 0:a2cb7295a1f7 51 bool create_device_object(M2MDevice::DeviceResource resource,
ram54288 0:a2cb7295a1f7 52 int64_t value,
ram54288 0:a2cb7295a1f7 53 uint16_t instance_id);
ram54288 0:a2cb7295a1f7 54
ram54288 0:a2cb7295a1f7 55 bool create_device_object(M2MDevice::DeviceResource resource,
ram54288 0:a2cb7295a1f7 56 int64_t value);
ram54288 0:a2cb7295a1f7 57
ram54288 0:a2cb7295a1f7 58 bool create_firmware_object();
ram54288 0:a2cb7295a1f7 59
ram54288 0:a2cb7295a1f7 60 bool create_firmware_object(M2MFirmware::FirmwareResource resource,
ram54288 0:a2cb7295a1f7 61 const char *value);
ram54288 0:a2cb7295a1f7 62
ram54288 0:a2cb7295a1f7 63 bool create_firmware_object(M2MFirmware::FirmwareResource resource,
ram54288 0:a2cb7295a1f7 64 int64_t value);
ram54288 0:a2cb7295a1f7 65
ram54288 0:a2cb7295a1f7 66 bool create_firmware_object(M2MFirmware::FirmwareResource resource,
ram54288 0:a2cb7295a1f7 67 const uint8_t *value,
ram54288 0:a2cb7295a1f7 68 const uint32_t length);
ram54288 0:a2cb7295a1f7 69
ram54288 0:a2cb7295a1f7 70 void firmware_resource_int(int resource);
ram54288 0:a2cb7295a1f7 71 void firmware_resource_string(int resource);
ram54288 0:a2cb7295a1f7 72
ram54288 0:a2cb7295a1f7 73 void firmware_resource_buffer();
ram54288 0:a2cb7295a1f7 74
ram54288 0:a2cb7295a1f7 75 bool create_object(const char *name,
ram54288 0:a2cb7295a1f7 76 bool new_instance,
ram54288 0:a2cb7295a1f7 77 uint8_t object_operation,
ram54288 0:a2cb7295a1f7 78 uint8_t object_instance_operation,
ram54288 0:a2cb7295a1f7 79 uint16_t object_instance_id,
ram54288 0:a2cb7295a1f7 80 bool object_observable,
ram54288 0:a2cb7295a1f7 81 bool object_instance_observable);
ram54288 0:a2cb7295a1f7 82
ram54288 0:a2cb7295a1f7 83 bool create_static_resource_string(const char *name,
ram54288 0:a2cb7295a1f7 84 const char *value,
ram54288 0:a2cb7295a1f7 85 bool multiple_instance,
ram54288 0:a2cb7295a1f7 86 uint16_t object_instance = 0);
ram54288 0:a2cb7295a1f7 87
ram54288 0:a2cb7295a1f7 88 bool create_static_resource_int(const char *name,
ram54288 0:a2cb7295a1f7 89 int64_t value,
ram54288 0:a2cb7295a1f7 90 bool multiple_instance,
ram54288 0:a2cb7295a1f7 91 uint16_t object_instance = 0);
ram54288 0:a2cb7295a1f7 92
ram54288 0:a2cb7295a1f7 93 bool create_dynamic_resource_int(const char *name,
ram54288 0:a2cb7295a1f7 94 bool observable,
ram54288 0:a2cb7295a1f7 95 bool multiple_instance,
ram54288 0:a2cb7295a1f7 96 uint16_t object_instance = 0,
ram54288 0:a2cb7295a1f7 97 uint8_t resource_operation = 1);
ram54288 0:a2cb7295a1f7 98 bool create_dynamic_resource_string(const char *name,
ram54288 0:a2cb7295a1f7 99 bool observable,
ram54288 0:a2cb7295a1f7 100 bool multiple_instance,
ram54288 0:a2cb7295a1f7 101 uint16_t object_instance = 0,
ram54288 0:a2cb7295a1f7 102 uint8_t resource_operation = 1);
ram54288 0:a2cb7295a1f7 103
ram54288 0:a2cb7295a1f7 104 bool set_resource_value(const char *name,
ram54288 0:a2cb7295a1f7 105 int32_t value,
ram54288 0:a2cb7295a1f7 106 uint16_t object_instance = 0);
ram54288 0:a2cb7295a1f7 107
ram54288 0:a2cb7295a1f7 108 bool set_resource_value(const char *name,
ram54288 0:a2cb7295a1f7 109 const char *value,
ram54288 0:a2cb7295a1f7 110 uint16_t object_instance = 0);
ram54288 0:a2cb7295a1f7 111
ram54288 0:a2cb7295a1f7 112 bool create_static_resource_instance_string(const char *name,
ram54288 0:a2cb7295a1f7 113 const char *value,
ram54288 0:a2cb7295a1f7 114 bool multiple_instance,
ram54288 0:a2cb7295a1f7 115 uint16_t object_instance = 0,
ram54288 0:a2cb7295a1f7 116 uint16_t resource_instance = 0);
ram54288 0:a2cb7295a1f7 117
ram54288 0:a2cb7295a1f7 118 bool create_static_resource_instance_int(const char *name,
ram54288 0:a2cb7295a1f7 119 int32_t value,
ram54288 0:a2cb7295a1f7 120 bool multiple_instance,
ram54288 0:a2cb7295a1f7 121 uint16_t object_instance = 0,
ram54288 0:a2cb7295a1f7 122 uint16_t resource_instance = 0);
ram54288 0:a2cb7295a1f7 123
ram54288 0:a2cb7295a1f7 124 bool create_dynamic_resource_instance_int(const char *name,
ram54288 0:a2cb7295a1f7 125 bool observable,
ram54288 0:a2cb7295a1f7 126 bool multiple_instance,
ram54288 0:a2cb7295a1f7 127 uint16_t object_instance = 0,
ram54288 0:a2cb7295a1f7 128 uint16_t resource_instance = 0,
ram54288 0:a2cb7295a1f7 129 uint8_t resource_instance_operation = 1);
ram54288 0:a2cb7295a1f7 130 bool create_dynamic_resource_instance_string(const char *name,
ram54288 0:a2cb7295a1f7 131 bool observable,
ram54288 0:a2cb7295a1f7 132 bool multiple_instance,
ram54288 0:a2cb7295a1f7 133 uint16_t object_instance = 0,
ram54288 0:a2cb7295a1f7 134 uint16_t resource_instance = 0,
ram54288 0:a2cb7295a1f7 135 uint8_t resource_instance_operation = 1);
ram54288 0:a2cb7295a1f7 136
ram54288 0:a2cb7295a1f7 137 bool set_resource_instance_value(const char *name,
ram54288 0:a2cb7295a1f7 138 int32_t value,
ram54288 0:a2cb7295a1f7 139 uint16_t object_instance = 0,
ram54288 0:a2cb7295a1f7 140 uint16_t resource_instance = 0);
ram54288 0:a2cb7295a1f7 141
ram54288 0:a2cb7295a1f7 142 bool set_resource_instance_value(const char *name,
ram54288 0:a2cb7295a1f7 143 const char *value,
ram54288 0:a2cb7295a1f7 144 uint16_t object_instance = 0,
ram54288 0:a2cb7295a1f7 145 uint16_t resource_instance = 0);
ram54288 0:a2cb7295a1f7 146
ram54288 0:a2cb7295a1f7 147 bool create_object_instance();
ram54288 0:a2cb7295a1f7 148
ram54288 0:a2cb7295a1f7 149 bool test_register();
ram54288 0:a2cb7295a1f7 150
ram54288 0:a2cb7295a1f7 151 bool test_update_register(const uint32_t lifetime);
ram54288 0:a2cb7295a1f7 152
ram54288 0:a2cb7295a1f7 153 bool test_unregister();
ram54288 0:a2cb7295a1f7 154
ram54288 0:a2cb7295a1f7 155 void bootstrap_done(M2MSecurity *server_object);
ram54288 0:a2cb7295a1f7 156
ram54288 0:a2cb7295a1f7 157 void object_registered(M2MSecurity *security_object, const M2MServer &server_object);
ram54288 0:a2cb7295a1f7 158
ram54288 0:a2cb7295a1f7 159 void object_unregistered(M2MSecurity *server_object);
ram54288 0:a2cb7295a1f7 160
ram54288 0:a2cb7295a1f7 161 void registration_updated(M2MSecurity *security_object, const M2MServer &server_object);
ram54288 0:a2cb7295a1f7 162
ram54288 0:a2cb7295a1f7 163 void error(M2MInterface::Error error);
ram54288 0:a2cb7295a1f7 164
ram54288 0:a2cb7295a1f7 165 void value_updated(M2MBase *base, M2MBase::BaseType type);
ram54288 0:a2cb7295a1f7 166
ram54288 0:a2cb7295a1f7 167 void set_fw_execute_function();
ram54288 0:a2cb7295a1f7 168
ram54288 0:a2cb7295a1f7 169 private:
ram54288 0:a2cb7295a1f7 170
ram54288 0:a2cb7295a1f7 171 M2MBase::Operation int_to_operation(uint8_t operation);
ram54288 0:a2cb7295a1f7 172 void fw_execute_function(void *argument);
ram54288 0:a2cb7295a1f7 173
ram54288 0:a2cb7295a1f7 174
ram54288 0:a2cb7295a1f7 175 private:
ram54288 0:a2cb7295a1f7 176
ram54288 0:a2cb7295a1f7 177 M2MInterface *_interface;
ram54288 0:a2cb7295a1f7 178 M2MSecurity *_security;
ram54288 0:a2cb7295a1f7 179 M2MSecurity *_register_security;
ram54288 0:a2cb7295a1f7 180 M2MDevice *_device;
ram54288 0:a2cb7295a1f7 181 M2MFirmware *_firmware;
ram54288 0:a2cb7295a1f7 182 M2MObject *_object;
ram54288 0:a2cb7295a1f7 183 bool _bootstrapped;
ram54288 0:a2cb7295a1f7 184 bool _error;
ram54288 0:a2cb7295a1f7 185 bool _registered;
ram54288 0:a2cb7295a1f7 186 bool _unregistered;
ram54288 0:a2cb7295a1f7 187 bool _registration_updated;
ram54288 0:a2cb7295a1f7 188 };
ram54288 0:a2cb7295a1f7 189
ram54288 0:a2cb7295a1f7 190
ram54288 0:a2cb7295a1f7 191 const unsigned char psk[] = {0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x31,0x32,0x33,0x34,0x35,0x36};
ram54288 0:a2cb7295a1f7 192 const size_t psk_len = sizeof( psk );
ram54288 0:a2cb7295a1f7 193 const unsigned char psk_identity[] = {0x0F,0x0F};
ram54288 0:a2cb7295a1f7 194 const size_t psk_identity_len = sizeof( psk_identity );
ram54288 0:a2cb7295a1f7 195
ram54288 0:a2cb7295a1f7 196 const uint8_t server_cert[] = "-----BEGIN CERTIFICATE-----\r\n"
ram54288 0:a2cb7295a1f7 197 "MIIBmDCCAT6gAwIBAgIEVUCA0jAKBggqhkjOPQQDAjBLMQswCQYDVQQGEwJGSTEN\r\n"
ram54288 0:a2cb7295a1f7 198 "MAsGA1UEBwwET3VsdTEMMAoGA1UECgwDQVJNMQwwCgYDVQQLDANJb1QxETAPBgNV\r\n"
ram54288 0:a2cb7295a1f7 199 "BAMMCEFSTSBtYmVkMB4XDTE1MDQyOTA2NTc0OFoXDTE4MDQyOTA2NTc0OFowSzEL\r\n"
ram54288 0:a2cb7295a1f7 200 "MAkGA1UEBhMCRkkxDTALBgNVBAcMBE91bHUxDDAKBgNVBAoMA0FSTTEMMAoGA1UE\r\n"
ram54288 0:a2cb7295a1f7 201 "CwwDSW9UMREwDwYDVQQDDAhBUk0gbWJlZDBZMBMGByqGSM49AgEGCCqGSM49AwEH\r\n"
ram54288 0:a2cb7295a1f7 202 "A0IABLuAyLSk0mA3awgFR5mw2RHth47tRUO44q/RdzFZnLsAsd18Esxd5LCpcT9w\r\n"
ram54288 0:a2cb7295a1f7 203 "0tvNfBv4xJxGw0wcYrPDDb8/rjujEDAOMAwGA1UdEwQFMAMBAf8wCgYIKoZIzj0E\r\n"
ram54288 0:a2cb7295a1f7 204 "AwIDSAAwRQIhAPAonEAkwixlJiyYRQQWpXtkMZax+VlEiS201BG0PpAzAiBh2RsD\r\n"
ram54288 0:a2cb7295a1f7 205 "NxLKWwf4O7D6JasGBYf9+ZLwl0iaRjTjytO+Kw==\r\n"
ram54288 0:a2cb7295a1f7 206 "-----END CERTIFICATE-----\r\n";
ram54288 0:a2cb7295a1f7 207
ram54288 0:a2cb7295a1f7 208 const uint8_t cert[] = "-----BEGIN CERTIFICATE-----\r\n"
ram54288 0:a2cb7295a1f7 209 "MIICTDCCAbWgAwIBAgIJAKI3S+LGklSGMA0GCSqGSIb3DQEBCwUAMD8xCzAJBgNV\r\n"
ram54288 0:a2cb7295a1f7 210 "BAYTAkZJMRMwEQYDVQQIDApTb21lLVN0YXRlMQ0wCwYDVQQHDARPdWx1MQwwCgYD\r\n"
ram54288 0:a2cb7295a1f7 211 "VQQKDANBUk0wHhcNMTUwNjExMTI0NTU1WhcNMTYwNjEwMTI0NTU1WjA/MQswCQYD\r\n"
ram54288 0:a2cb7295a1f7 212 "VQQGEwJGSTETMBEGA1UECAwKU29tZS1TdGF0ZTENMAsGA1UEBwwET3VsdTEMMAoG\r\n"
ram54288 0:a2cb7295a1f7 213 "A1UECgwDQVJNMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDBt8ag12ILm2pn\r\n"
ram54288 0:a2cb7295a1f7 214 "RHUc2yMBXpdzspDwVV3VDaNTRXypVUOq/nxJc6zr8yG/Pvga2/XVijWQDLABHVhV\r\n"
ram54288 0:a2cb7295a1f7 215 "PoeulzXVSEHRR2bR2lhqulLzdUzPYJ+yJd4+082akoxdzoom8ms2LFlgiXO7lyBg\r\n"
ram54288 0:a2cb7295a1f7 216 "1t74xjEQapCkr7Tdl2pD9OTUrcfoEwIDAQABo1AwTjAdBgNVHQ4EFgQUHK0O4iK7\r\n"
ram54288 0:a2cb7295a1f7 217 "Rv8AsxAqRDlY3TSHWtUwHwYDVR0jBBgwFoAUHK0O4iK7Rv8AsxAqRDlY3TSHWtUw\r\n"
ram54288 0:a2cb7295a1f7 218 "DAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQsFAAOBgQAPVLihYN65DEeYdf7gDAdP\r\n"
ram54288 0:a2cb7295a1f7 219 "MNH5RxsQlHdktQkYWcUewe3hvOf8yZUBbmO5OeEQmy5Ca2H1QhUuw+kWHo1l/K9g\r\n"
ram54288 0:a2cb7295a1f7 220 "DpR1xoDACeycBd/6tAHfaFzTEEDC9Ug839EAHDrbI4ihrPEbP6hyRIaUIM4IojXf\r\n"
ram54288 0:a2cb7295a1f7 221 "zuqwOcobS3idv8a9Npuohw==\r\n"
ram54288 0:a2cb7295a1f7 222 "-----END CERTIFICATE-----\r\n";
ram54288 0:a2cb7295a1f7 223
ram54288 0:a2cb7295a1f7 224 const uint8_t key[] = "-----BEGIN PRIVATE KEY-----\r\n"
ram54288 0:a2cb7295a1f7 225 "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMG3xqDXYgubamdE\r\n"
ram54288 0:a2cb7295a1f7 226 "dRzbIwFel3OykPBVXdUNo1NFfKlVQ6r+fElzrOvzIb8++Brb9dWKNZAMsAEdWFU+\r\n"
ram54288 0:a2cb7295a1f7 227 "h66XNdVIQdFHZtHaWGq6UvN1TM9gn7Il3j7TzZqSjF3OiibyazYsWWCJc7uXIGDW\r\n"
ram54288 0:a2cb7295a1f7 228 "3vjGMRBqkKSvtN2XakP05NStx+gTAgMBAAECgYEAlRimNxsu6jIXJh4paSk8ewxb\r\n"
ram54288 0:a2cb7295a1f7 229 "0v+n0GJT0uwpPakotofaihxOepYejIfX/IMxoDKVtmImsZUiTelRGGMFORpGomS7\r\n"
ram54288 0:a2cb7295a1f7 230 "3Pb9awfn1yWiYja9MF7+pgFtXj6LxcB5rcxrP8s9rCs+kEq8yl/khGXmlwlpacUm\r\n"
ram54288 0:a2cb7295a1f7 231 "Pdg1rEHriykk/5dlyKkCQQD9zpgQKN87bQNOVSgFSNsrwCjEN2MvVUC6ycqVYqQd\r\n"
ram54288 0:a2cb7295a1f7 232 "oQZrO2lJuf57Jn7kRKJgfqqImwLYaohn5A9gKiqszo+fAkEAw2REr8L9aLoRPe5Q\r\n"
ram54288 0:a2cb7295a1f7 233 "4+lYCmNmxJjBbJoRviSlxx+p5dxK+8H4J+bnFUWzORFJP0+Bjhii2UHGBoMjuP2s\r\n"
ram54288 0:a2cb7295a1f7 234 "e8xDDQJADm0IrZp1Hb6TeodSkAJVGsaASq7PP2h8FmHT1uvVYi293Khy0pL0yPEm\r\n"
ram54288 0:a2cb7295a1f7 235 "MzlHdW6dqMgOwFhFyonm6dNbu5i+BwJBALSPKfIKDMcEev5yBgo3Dga7iLJQPx4u\r\n"
ram54288 0:a2cb7295a1f7 236 "2gqOLyvtlME3a4tu2fV1qUG9ITuxP7DjteMr3QFLiMVn1lETCpt0TNkCQGIvyjdV\r\n"
ram54288 0:a2cb7295a1f7 237 "RzYnzLDMulXCsuRBjs0C75KoZrcPPQ416SZFtYASsOl3P0Ih5jgt/1aPmth3Yson\r\n"
ram54288 0:a2cb7295a1f7 238 "GdnBOWyKzadLmz8=\r\n"
ram54288 0:a2cb7295a1f7 239 "-----END PRIVATE KEY-----\r\n";
ram54288 0:a2cb7295a1f7 240
ram54288 0:a2cb7295a1f7 241 const size_t server_cert_len = sizeof( server_cert );
ram54288 0:a2cb7295a1f7 242 const size_t cert_len = sizeof( cert );
ram54288 0:a2cb7295a1f7 243 const size_t key_len = sizeof( key );
ram54288 0:a2cb7295a1f7 244