Ram Gandikota
/
metronome
FRDM K64F Metronome
mbed-client/test/helloworld-mbedclient/main.cpp@0:a2cb7295a1f7, 2017-05-14 (annotated)
- Committer:
- ram54288
- Date:
- Sun May 14 18:35:07 2017 +0000
- Revision:
- 0:a2cb7295a1f7
Initial commit
Who changed what in which revision?
User | Revision | Line number | New 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 | |
ram54288 | 0:a2cb7295a1f7 | 17 | #include "mbedclient.h" |
ram54288 | 0:a2cb7295a1f7 | 18 | |
ram54288 | 0:a2cb7295a1f7 | 19 | #ifdef TARGET_LIKE_LINUX |
ram54288 | 0:a2cb7295a1f7 | 20 | #include <unistd.h> |
ram54288 | 0:a2cb7295a1f7 | 21 | #include <stdio.h> |
ram54288 | 0:a2cb7295a1f7 | 22 | #include <stdarg.h> |
ram54288 | 0:a2cb7295a1f7 | 23 | #include <pthread.h> |
ram54288 | 0:a2cb7295a1f7 | 24 | #include <signal.h> /* For SIGIGN and SIGINT */ |
ram54288 | 0:a2cb7295a1f7 | 25 | #include <time.h> |
ram54288 | 0:a2cb7295a1f7 | 26 | #else |
ram54288 | 0:a2cb7295a1f7 | 27 | #include "sockets/UDPSocket.h" |
ram54288 | 0:a2cb7295a1f7 | 28 | #ifdef SIXLOWPAN_INTERFACE |
ram54288 | 0:a2cb7295a1f7 | 29 | #include "atmel-rf-driver/driverRFPhy.h" // rf_device_register |
ram54288 | 0:a2cb7295a1f7 | 30 | #include "mbed-mesh-api/Mesh6LoWPAN_ND.h" |
ram54288 | 0:a2cb7295a1f7 | 31 | #endif |
ram54288 | 0:a2cb7295a1f7 | 32 | #include "EthernetInterface.h" |
ram54288 | 0:a2cb7295a1f7 | 33 | #include "test_env.h" |
ram54288 | 0:a2cb7295a1f7 | 34 | // TODO: Remove when yotta supports init. |
ram54288 | 0:a2cb7295a1f7 | 35 | #include "lwipv4_init.h" |
ram54288 | 0:a2cb7295a1f7 | 36 | using namespace mbed::util; |
ram54288 | 0:a2cb7295a1f7 | 37 | #endif |
ram54288 | 0:a2cb7295a1f7 | 38 | |
ram54288 | 0:a2cb7295a1f7 | 39 | #include "mbed-client/m2minterfacefactory.h" |
ram54288 | 0:a2cb7295a1f7 | 40 | #include "mbed-client/m2mdevice.h" |
ram54288 | 0:a2cb7295a1f7 | 41 | #include "mbed-client/m2minterfaceobserver.h" |
ram54288 | 0:a2cb7295a1f7 | 42 | #include "mbed-client/m2minterface.h" |
ram54288 | 0:a2cb7295a1f7 | 43 | #include "mbed-client/m2mobjectinstance.h" |
ram54288 | 0:a2cb7295a1f7 | 44 | #include "mbed-client/m2mresource.h" |
ram54288 | 0:a2cb7295a1f7 | 45 | #include "mbed-trace/mbed_trace.h" |
ram54288 | 0:a2cb7295a1f7 | 46 | |
ram54288 | 0:a2cb7295a1f7 | 47 | #ifdef TARGET_LIKE_LINUX |
ram54288 | 0:a2cb7295a1f7 | 48 | static void ctrl_c_handle_function(void); |
ram54288 | 0:a2cb7295a1f7 | 49 | typedef void (*signalhandler_t)(int); /* Function pointer type for ctrl-c */ |
ram54288 | 0:a2cb7295a1f7 | 50 | #else |
ram54288 | 0:a2cb7295a1f7 | 51 | #if defined(TARGET_K64F) |
ram54288 | 0:a2cb7295a1f7 | 52 | #define OBS_BUTTON SW2 |
ram54288 | 0:a2cb7295a1f7 | 53 | #define UNREG_BUTTON SW3 |
ram54288 | 0:a2cb7295a1f7 | 54 | #endif |
ram54288 | 0:a2cb7295a1f7 | 55 | #endif |
ram54288 | 0:a2cb7295a1f7 | 56 | |
ram54288 | 0:a2cb7295a1f7 | 57 | bool _have_secure_conn = false; |
ram54288 | 0:a2cb7295a1f7 | 58 | |
ram54288 | 0:a2cb7295a1f7 | 59 | const unsigned char psk[] = {0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x31,0x32,0x33,0x34,0x35,0x36}; |
ram54288 | 0:a2cb7295a1f7 | 60 | const size_t psk_len = sizeof( psk ); |
ram54288 | 0:a2cb7295a1f7 | 61 | const unsigned char psk_identity[] = {0x0F,0x0F}; |
ram54288 | 0:a2cb7295a1f7 | 62 | const size_t psk_identity_len = sizeof( psk_identity ); |
ram54288 | 0:a2cb7295a1f7 | 63 | |
ram54288 | 0:a2cb7295a1f7 | 64 | const uint8_t server_cert[] = "-----BEGIN CERTIFICATE-----\r\n" |
ram54288 | 0:a2cb7295a1f7 | 65 | "MIIBmDCCAT6gAwIBAgIEVUCA0jAKBggqhkjOPQQDAjBLMQswCQYDVQQGEwJGSTEN\r\n" |
ram54288 | 0:a2cb7295a1f7 | 66 | "MAsGA1UEBwwET3VsdTEMMAoGA1UECgwDQVJNMQwwCgYDVQQLDANJb1QxETAPBgNV\r\n" |
ram54288 | 0:a2cb7295a1f7 | 67 | "BAMMCEFSTSBtYmVkMB4XDTE1MDQyOTA2NTc0OFoXDTE4MDQyOTA2NTc0OFowSzEL\r\n" |
ram54288 | 0:a2cb7295a1f7 | 68 | "MAkGA1UEBhMCRkkxDTALBgNVBAcMBE91bHUxDDAKBgNVBAoMA0FSTTEMMAoGA1UE\r\n" |
ram54288 | 0:a2cb7295a1f7 | 69 | "CwwDSW9UMREwDwYDVQQDDAhBUk0gbWJlZDBZMBMGByqGSM49AgEGCCqGSM49AwEH\r\n" |
ram54288 | 0:a2cb7295a1f7 | 70 | "A0IABLuAyLSk0mA3awgFR5mw2RHth47tRUO44q/RdzFZnLsAsd18Esxd5LCpcT9w\r\n" |
ram54288 | 0:a2cb7295a1f7 | 71 | "0tvNfBv4xJxGw0wcYrPDDb8/rjujEDAOMAwGA1UdEwQFMAMBAf8wCgYIKoZIzj0E\r\n" |
ram54288 | 0:a2cb7295a1f7 | 72 | "AwIDSAAwRQIhAPAonEAkwixlJiyYRQQWpXtkMZax+VlEiS201BG0PpAzAiBh2RsD\r\n" |
ram54288 | 0:a2cb7295a1f7 | 73 | "NxLKWwf4O7D6JasGBYf9+ZLwl0iaRjTjytO+Kw==\r\n" |
ram54288 | 0:a2cb7295a1f7 | 74 | "-----END CERTIFICATE-----\r\n"; |
ram54288 | 0:a2cb7295a1f7 | 75 | |
ram54288 | 0:a2cb7295a1f7 | 76 | const uint8_t cert[] = "-----BEGIN CERTIFICATE-----\r\n" |
ram54288 | 0:a2cb7295a1f7 | 77 | "MIICTDCCAbWgAwIBAgIJAKI3S+LGklSGMA0GCSqGSIb3DQEBCwUAMD8xCzAJBgNV\r\n" |
ram54288 | 0:a2cb7295a1f7 | 78 | "BAYTAkZJMRMwEQYDVQQIDApTb21lLVN0YXRlMQ0wCwYDVQQHDARPdWx1MQwwCgYD\r\n" |
ram54288 | 0:a2cb7295a1f7 | 79 | "VQQKDANBUk0wHhcNMTUwNjExMTI0NTU1WhcNMTYwNjEwMTI0NTU1WjA/MQswCQYD\r\n" |
ram54288 | 0:a2cb7295a1f7 | 80 | "VQQGEwJGSTETMBEGA1UECAwKU29tZS1TdGF0ZTENMAsGA1UEBwwET3VsdTEMMAoG\r\n" |
ram54288 | 0:a2cb7295a1f7 | 81 | "A1UECgwDQVJNMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDBt8ag12ILm2pn\r\n" |
ram54288 | 0:a2cb7295a1f7 | 82 | "RHUc2yMBXpdzspDwVV3VDaNTRXypVUOq/nxJc6zr8yG/Pvga2/XVijWQDLABHVhV\r\n" |
ram54288 | 0:a2cb7295a1f7 | 83 | "PoeulzXVSEHRR2bR2lhqulLzdUzPYJ+yJd4+082akoxdzoom8ms2LFlgiXO7lyBg\r\n" |
ram54288 | 0:a2cb7295a1f7 | 84 | "1t74xjEQapCkr7Tdl2pD9OTUrcfoEwIDAQABo1AwTjAdBgNVHQ4EFgQUHK0O4iK7\r\n" |
ram54288 | 0:a2cb7295a1f7 | 85 | "Rv8AsxAqRDlY3TSHWtUwHwYDVR0jBBgwFoAUHK0O4iK7Rv8AsxAqRDlY3TSHWtUw\r\n" |
ram54288 | 0:a2cb7295a1f7 | 86 | "DAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQsFAAOBgQAPVLihYN65DEeYdf7gDAdP\r\n" |
ram54288 | 0:a2cb7295a1f7 | 87 | "MNH5RxsQlHdktQkYWcUewe3hvOf8yZUBbmO5OeEQmy5Ca2H1QhUuw+kWHo1l/K9g\r\n" |
ram54288 | 0:a2cb7295a1f7 | 88 | "DpR1xoDACeycBd/6tAHfaFzTEEDC9Ug839EAHDrbI4ihrPEbP6hyRIaUIM4IojXf\r\n" |
ram54288 | 0:a2cb7295a1f7 | 89 | "zuqwOcobS3idv8a9Npuohw==\r\n" |
ram54288 | 0:a2cb7295a1f7 | 90 | "-----END CERTIFICATE-----\r\n"; |
ram54288 | 0:a2cb7295a1f7 | 91 | |
ram54288 | 0:a2cb7295a1f7 | 92 | const uint8_t key[] = "-----BEGIN PRIVATE KEY-----\r\n" |
ram54288 | 0:a2cb7295a1f7 | 93 | "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMG3xqDXYgubamdE\r\n" |
ram54288 | 0:a2cb7295a1f7 | 94 | "dRzbIwFel3OykPBVXdUNo1NFfKlVQ6r+fElzrOvzIb8++Brb9dWKNZAMsAEdWFU+\r\n" |
ram54288 | 0:a2cb7295a1f7 | 95 | "h66XNdVIQdFHZtHaWGq6UvN1TM9gn7Il3j7TzZqSjF3OiibyazYsWWCJc7uXIGDW\r\n" |
ram54288 | 0:a2cb7295a1f7 | 96 | "3vjGMRBqkKSvtN2XakP05NStx+gTAgMBAAECgYEAlRimNxsu6jIXJh4paSk8ewxb\r\n" |
ram54288 | 0:a2cb7295a1f7 | 97 | "0v+n0GJT0uwpPakotofaihxOepYejIfX/IMxoDKVtmImsZUiTelRGGMFORpGomS7\r\n" |
ram54288 | 0:a2cb7295a1f7 | 98 | "3Pb9awfn1yWiYja9MF7+pgFtXj6LxcB5rcxrP8s9rCs+kEq8yl/khGXmlwlpacUm\r\n" |
ram54288 | 0:a2cb7295a1f7 | 99 | "Pdg1rEHriykk/5dlyKkCQQD9zpgQKN87bQNOVSgFSNsrwCjEN2MvVUC6ycqVYqQd\r\n" |
ram54288 | 0:a2cb7295a1f7 | 100 | "oQZrO2lJuf57Jn7kRKJgfqqImwLYaohn5A9gKiqszo+fAkEAw2REr8L9aLoRPe5Q\r\n" |
ram54288 | 0:a2cb7295a1f7 | 101 | "4+lYCmNmxJjBbJoRviSlxx+p5dxK+8H4J+bnFUWzORFJP0+Bjhii2UHGBoMjuP2s\r\n" |
ram54288 | 0:a2cb7295a1f7 | 102 | "e8xDDQJADm0IrZp1Hb6TeodSkAJVGsaASq7PP2h8FmHT1uvVYi293Khy0pL0yPEm\r\n" |
ram54288 | 0:a2cb7295a1f7 | 103 | "MzlHdW6dqMgOwFhFyonm6dNbu5i+BwJBALSPKfIKDMcEev5yBgo3Dga7iLJQPx4u\r\n" |
ram54288 | 0:a2cb7295a1f7 | 104 | "2gqOLyvtlME3a4tu2fV1qUG9ITuxP7DjteMr3QFLiMVn1lETCpt0TNkCQGIvyjdV\r\n" |
ram54288 | 0:a2cb7295a1f7 | 105 | "RzYnzLDMulXCsuRBjs0C75KoZrcPPQ416SZFtYASsOl3P0Ih5jgt/1aPmth3Yson\r\n" |
ram54288 | 0:a2cb7295a1f7 | 106 | "GdnBOWyKzadLmz8=\r\n" |
ram54288 | 0:a2cb7295a1f7 | 107 | "-----END PRIVATE KEY-----\r\n"; |
ram54288 | 0:a2cb7295a1f7 | 108 | |
ram54288 | 0:a2cb7295a1f7 | 109 | const size_t server_cert_len = sizeof( server_cert ); |
ram54288 | 0:a2cb7295a1f7 | 110 | const size_t cert_len = sizeof( cert ); |
ram54288 | 0:a2cb7295a1f7 | 111 | const size_t key_len = sizeof( key ); |
ram54288 | 0:a2cb7295a1f7 | 112 | |
ram54288 | 0:a2cb7295a1f7 | 113 | #ifdef TARGET_LIKE_LINUX |
ram54288 | 0:a2cb7295a1f7 | 114 | |
ram54288 | 0:a2cb7295a1f7 | 115 | #if defined (BOOTSTRAP_ENABLED) |
ram54288 | 0:a2cb7295a1f7 | 116 | void* wait_for_bootstrap(void* arg) { |
ram54288 | 0:a2cb7295a1f7 | 117 | MbedClient *client; |
ram54288 | 0:a2cb7295a1f7 | 118 | client = (MbedClient*) arg; |
ram54288 | 0:a2cb7295a1f7 | 119 | if(client->bootstrap_successful()) { |
ram54288 | 0:a2cb7295a1f7 | 120 | printf("Registering endpoint\n"); |
ram54288 | 0:a2cb7295a1f7 | 121 | |
ram54288 | 0:a2cb7295a1f7 | 122 | // Create LWM2M device object specifying device resources |
ram54288 | 0:a2cb7295a1f7 | 123 | // as per OMA LWM2M specification. |
ram54288 | 0:a2cb7295a1f7 | 124 | M2MDevice* device_object = client->create_device_object(); |
ram54288 | 0:a2cb7295a1f7 | 125 | |
ram54288 | 0:a2cb7295a1f7 | 126 | M2MObject* object = client->create_generic_object(); |
ram54288 | 0:a2cb7295a1f7 | 127 | |
ram54288 | 0:a2cb7295a1f7 | 128 | // Add all the objects that you would like to register |
ram54288 | 0:a2cb7295a1f7 | 129 | // into the list and pass the list for register API. |
ram54288 | 0:a2cb7295a1f7 | 130 | M2MObjectList object_list; |
ram54288 | 0:a2cb7295a1f7 | 131 | object_list.push_back(device_object); |
ram54288 | 0:a2cb7295a1f7 | 132 | object_list.push_back(object); |
ram54288 | 0:a2cb7295a1f7 | 133 | |
ram54288 | 0:a2cb7295a1f7 | 134 | // Issue register command. |
ram54288 | 0:a2cb7295a1f7 | 135 | client->test_register(object_list); |
ram54288 | 0:a2cb7295a1f7 | 136 | } |
ram54288 | 0:a2cb7295a1f7 | 137 | return NULL; |
ram54288 | 0:a2cb7295a1f7 | 138 | } |
ram54288 | 0:a2cb7295a1f7 | 139 | #endif |
ram54288 | 0:a2cb7295a1f7 | 140 | |
ram54288 | 0:a2cb7295a1f7 | 141 | void* wait_for_unregister(void* arg) { |
ram54288 | 0:a2cb7295a1f7 | 142 | MbedClient *client; |
ram54288 | 0:a2cb7295a1f7 | 143 | client = (MbedClient*) arg; |
ram54288 | 0:a2cb7295a1f7 | 144 | if(client->unregister_successful()) { |
ram54288 | 0:a2cb7295a1f7 | 145 | printf("Unregistered done --> exiting\n"); |
ram54288 | 0:a2cb7295a1f7 | 146 | exit(1); |
ram54288 | 0:a2cb7295a1f7 | 147 | } |
ram54288 | 0:a2cb7295a1f7 | 148 | return NULL; |
ram54288 | 0:a2cb7295a1f7 | 149 | } |
ram54288 | 0:a2cb7295a1f7 | 150 | |
ram54288 | 0:a2cb7295a1f7 | 151 | void* send_observation(void* arg) { |
ram54288 | 0:a2cb7295a1f7 | 152 | MbedClient *client; |
ram54288 | 0:a2cb7295a1f7 | 153 | client = (MbedClient*) arg; |
ram54288 | 0:a2cb7295a1f7 | 154 | static uint8_t counter = 0; |
ram54288 | 0:a2cb7295a1f7 | 155 | while(1) { |
ram54288 | 0:a2cb7295a1f7 | 156 | sleep(1); |
ram54288 | 0:a2cb7295a1f7 | 157 | if(counter >= 10 && |
ram54288 | 0:a2cb7295a1f7 | 158 | client->register_successful()) { |
ram54288 | 0:a2cb7295a1f7 | 159 | printf("Sending observation\n"); |
ram54288 | 0:a2cb7295a1f7 | 160 | client->update_resource(); |
ram54288 | 0:a2cb7295a1f7 | 161 | counter = 0; |
ram54288 | 0:a2cb7295a1f7 | 162 | } |
ram54288 | 0:a2cb7295a1f7 | 163 | else |
ram54288 | 0:a2cb7295a1f7 | 164 | counter++; |
ram54288 | 0:a2cb7295a1f7 | 165 | } |
ram54288 | 0:a2cb7295a1f7 | 166 | return NULL; |
ram54288 | 0:a2cb7295a1f7 | 167 | } |
ram54288 | 0:a2cb7295a1f7 | 168 | |
ram54288 | 0:a2cb7295a1f7 | 169 | static MbedClient *m2mclient = NULL; |
ram54288 | 0:a2cb7295a1f7 | 170 | |
ram54288 | 0:a2cb7295a1f7 | 171 | static void ctrl_c_handle_function(void) |
ram54288 | 0:a2cb7295a1f7 | 172 | { |
ram54288 | 0:a2cb7295a1f7 | 173 | if(m2mclient && m2mclient->register_successful()) { |
ram54288 | 0:a2cb7295a1f7 | 174 | printf("Unregistering endpoint\n"); |
ram54288 | 0:a2cb7295a1f7 | 175 | m2mclient->test_unregister(); |
ram54288 | 0:a2cb7295a1f7 | 176 | } |
ram54288 | 0:a2cb7295a1f7 | 177 | } |
ram54288 | 0:a2cb7295a1f7 | 178 | #else |
ram54288 | 0:a2cb7295a1f7 | 179 | #ifdef SIXLOWPAN_INTERFACE |
ram54288 | 0:a2cb7295a1f7 | 180 | volatile uint8_t mesh_network_state = MESH_DISCONNECTED; |
ram54288 | 0:a2cb7295a1f7 | 181 | |
ram54288 | 0:a2cb7295a1f7 | 182 | void mesh_network_callback(mesh_connection_status_t mesh_state) |
ram54288 | 0:a2cb7295a1f7 | 183 | { |
ram54288 | 0:a2cb7295a1f7 | 184 | tr_info("Network established"); |
ram54288 | 0:a2cb7295a1f7 | 185 | mesh_network_state = mesh_state; |
ram54288 | 0:a2cb7295a1f7 | 186 | } |
ram54288 | 0:a2cb7295a1f7 | 187 | #endif |
ram54288 | 0:a2cb7295a1f7 | 188 | #endif |
ram54288 | 0:a2cb7295a1f7 | 189 | |
ram54288 | 0:a2cb7295a1f7 | 190 | void trace_printer(const char* str) |
ram54288 | 0:a2cb7295a1f7 | 191 | { |
ram54288 | 0:a2cb7295a1f7 | 192 | printf("%s\r\n", str); |
ram54288 | 0:a2cb7295a1f7 | 193 | } |
ram54288 | 0:a2cb7295a1f7 | 194 | |
ram54288 | 0:a2cb7295a1f7 | 195 | #ifdef TARGET_LIKE_MBED |
ram54288 | 0:a2cb7295a1f7 | 196 | void app_start(int /*argc*/, char* /*argv*/[]) { |
ram54288 | 0:a2cb7295a1f7 | 197 | #else |
ram54288 | 0:a2cb7295a1f7 | 198 | int main() { |
ram54288 | 0:a2cb7295a1f7 | 199 | #endif |
ram54288 | 0:a2cb7295a1f7 | 200 | |
ram54288 | 0:a2cb7295a1f7 | 201 | // Instantiate the class which implements |
ram54288 | 0:a2cb7295a1f7 | 202 | // mbed Client API |
ram54288 | 0:a2cb7295a1f7 | 203 | MbedClient mbed_client; |
ram54288 | 0:a2cb7295a1f7 | 204 | |
ram54288 | 0:a2cb7295a1f7 | 205 | #ifdef TARGET_LIKE_LINUX |
ram54288 | 0:a2cb7295a1f7 | 206 | pthread_t unregister_thread; |
ram54288 | 0:a2cb7295a1f7 | 207 | pthread_t observation_thread; |
ram54288 | 0:a2cb7295a1f7 | 208 | |
ram54288 | 0:a2cb7295a1f7 | 209 | m2mclient = &mbed_client; |
ram54288 | 0:a2cb7295a1f7 | 210 | |
ram54288 | 0:a2cb7295a1f7 | 211 | mbed_trace_init(); |
ram54288 | 0:a2cb7295a1f7 | 212 | mbed_trace_print_function_set( trace_printer ); |
ram54288 | 0:a2cb7295a1f7 | 213 | mbed_trace_config_set(TRACE_MODE_COLOR|TRACE_ACTIVE_LEVEL_DEBUG|TRACE_CARRIAGE_RETURN); |
ram54288 | 0:a2cb7295a1f7 | 214 | |
ram54288 | 0:a2cb7295a1f7 | 215 | signal(SIGINT, (signalhandler_t)ctrl_c_handle_function); |
ram54288 | 0:a2cb7295a1f7 | 216 | |
ram54288 | 0:a2cb7295a1f7 | 217 | #else |
ram54288 | 0:a2cb7295a1f7 | 218 | // This sets up the network interface configuration which will be used |
ram54288 | 0:a2cb7295a1f7 | 219 | // by LWM2M Client API to communicate with mbed Device server. |
ram54288 | 0:a2cb7295a1f7 | 220 | |
ram54288 | 0:a2cb7295a1f7 | 221 | #ifdef SIXLOWPAN_INTERFACE |
ram54288 | 0:a2cb7295a1f7 | 222 | |
ram54288 | 0:a2cb7295a1f7 | 223 | Mesh6LoWPAN_ND *mesh_api = Mesh6LoWPAN_ND::getInstance(); |
ram54288 | 0:a2cb7295a1f7 | 224 | int8_t status; |
ram54288 | 0:a2cb7295a1f7 | 225 | |
ram54288 | 0:a2cb7295a1f7 | 226 | status = mesh_api->init(rf_device_register(), mesh_network_callback); |
ram54288 | 0:a2cb7295a1f7 | 227 | if (status != MESH_ERROR_NONE) |
ram54288 | 0:a2cb7295a1f7 | 228 | { |
ram54288 | 0:a2cb7295a1f7 | 229 | tr_error("Mesh network initialization failed %d!", status); |
ram54288 | 0:a2cb7295a1f7 | 230 | return 1; |
ram54288 | 0:a2cb7295a1f7 | 231 | } |
ram54288 | 0:a2cb7295a1f7 | 232 | |
ram54288 | 0:a2cb7295a1f7 | 233 | status = mesh_api->connect(); |
ram54288 | 0:a2cb7295a1f7 | 234 | if (status != MESH_ERROR_NONE) |
ram54288 | 0:a2cb7295a1f7 | 235 | { |
ram54288 | 0:a2cb7295a1f7 | 236 | tr_error("Can't connect to mesh network!"); |
ram54288 | 0:a2cb7295a1f7 | 237 | return 1; |
ram54288 | 0:a2cb7295a1f7 | 238 | } |
ram54288 | 0:a2cb7295a1f7 | 239 | |
ram54288 | 0:a2cb7295a1f7 | 240 | do |
ram54288 | 0:a2cb7295a1f7 | 241 | { |
ram54288 | 0:a2cb7295a1f7 | 242 | mesh_api->processEvent(); |
ram54288 | 0:a2cb7295a1f7 | 243 | } while(mesh_network_state != MESH_CONNECTED); |
ram54288 | 0:a2cb7295a1f7 | 244 | |
ram54288 | 0:a2cb7295a1f7 | 245 | |
ram54288 | 0:a2cb7295a1f7 | 246 | #else |
ram54288 | 0:a2cb7295a1f7 | 247 | EthernetInterface eth; |
ram54288 | 0:a2cb7295a1f7 | 248 | eth.init(); //Use DHCP |
ram54288 | 0:a2cb7295a1f7 | 249 | eth.connect(); |
ram54288 | 0:a2cb7295a1f7 | 250 | |
ram54288 | 0:a2cb7295a1f7 | 251 | lwipv4_socket_init(); |
ram54288 | 0:a2cb7295a1f7 | 252 | #endif |
ram54288 | 0:a2cb7295a1f7 | 253 | |
ram54288 | 0:a2cb7295a1f7 | 254 | // Set up Hardware interrupt button. |
ram54288 | 0:a2cb7295a1f7 | 255 | InterruptIn obs_button(OBS_BUTTON); |
ram54288 | 0:a2cb7295a1f7 | 256 | InterruptIn unreg_button(UNREG_BUTTON); |
ram54288 | 0:a2cb7295a1f7 | 257 | |
ram54288 | 0:a2cb7295a1f7 | 258 | // On press of SW3 button on K64F board, example application |
ram54288 | 0:a2cb7295a1f7 | 259 | // will call unregister API towards mbed Device Server |
ram54288 | 0:a2cb7295a1f7 | 260 | unreg_button.fall(&mbed_client,&MbedClient::test_unregister); |
ram54288 | 0:a2cb7295a1f7 | 261 | |
ram54288 | 0:a2cb7295a1f7 | 262 | // On press of SW2 button on K64F board, example application |
ram54288 | 0:a2cb7295a1f7 | 263 | // will send observation towards mbed Device Server |
ram54288 | 0:a2cb7295a1f7 | 264 | obs_button.fall(&mbed_client,&MbedClient::update_resource); |
ram54288 | 0:a2cb7295a1f7 | 265 | |
ram54288 | 0:a2cb7295a1f7 | 266 | |
ram54288 | 0:a2cb7295a1f7 | 267 | #endif |
ram54288 | 0:a2cb7295a1f7 | 268 | |
ram54288 | 0:a2cb7295a1f7 | 269 | // Create LWM2M Client API interface to manage bootstrap, |
ram54288 | 0:a2cb7295a1f7 | 270 | // register and unregister |
ram54288 | 0:a2cb7295a1f7 | 271 | |
ram54288 | 0:a2cb7295a1f7 | 272 | //These 2 are defining the secure connection usage |
ram54288 | 0:a2cb7295a1f7 | 273 | M2MSecurity::SecurityModeType mode = M2MSecurity::NoSecurity; |
ram54288 | 0:a2cb7295a1f7 | 274 | _have_secure_conn = (mode == M2MSecurity::Certificate || mode == M2MSecurity::Psk); |
ram54288 | 0:a2cb7295a1f7 | 275 | // SecurityNotSet, |
ram54288 | 0:a2cb7295a1f7 | 276 | // Psk = 0, |
ram54288 | 0:a2cb7295a1f7 | 277 | // Certificate = 2, |
ram54288 | 0:a2cb7295a1f7 | 278 | // NoSecurity = 3 |
ram54288 | 0:a2cb7295a1f7 | 279 | |
ram54288 | 0:a2cb7295a1f7 | 280 | mbed_client.create_interface(); |
ram54288 | 0:a2cb7295a1f7 | 281 | |
ram54288 | 0:a2cb7295a1f7 | 282 | #ifdef TARGET_LIKE_LINUX |
ram54288 | 0:a2cb7295a1f7 | 283 | |
ram54288 | 0:a2cb7295a1f7 | 284 | #if defined (BOOTSTRAP_ENABLED) |
ram54288 | 0:a2cb7295a1f7 | 285 | pthread_t bootstrap_thread; |
ram54288 | 0:a2cb7295a1f7 | 286 | // Create LWM2M bootstrap object specifying bootstrap server |
ram54288 | 0:a2cb7295a1f7 | 287 | // information. |
ram54288 | 0:a2cb7295a1f7 | 288 | M2MSecurity* security_object = mbed_client.create_bootstrap_object(_have_secure_conn); |
ram54288 | 0:a2cb7295a1f7 | 289 | // Issue bootstrap command. |
ram54288 | 0:a2cb7295a1f7 | 290 | mbed_client.test_bootstrap(security_object); |
ram54288 | 0:a2cb7295a1f7 | 291 | |
ram54288 | 0:a2cb7295a1f7 | 292 | pthread_create(&bootstrap_thread, NULL, &wait_for_bootstrap, (void*) &mbed_client); |
ram54288 | 0:a2cb7295a1f7 | 293 | pthread_join(bootstrap_thread, NULL); |
ram54288 | 0:a2cb7295a1f7 | 294 | |
ram54288 | 0:a2cb7295a1f7 | 295 | #else |
ram54288 | 0:a2cb7295a1f7 | 296 | M2MSecurity *register_object = mbed_client.create_register_object(_have_secure_conn); |
ram54288 | 0:a2cb7295a1f7 | 297 | |
ram54288 | 0:a2cb7295a1f7 | 298 | if( mode == M2MSecurity::Certificate ){ |
ram54288 | 0:a2cb7295a1f7 | 299 | register_object->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Certificate); |
ram54288 | 0:a2cb7295a1f7 | 300 | register_object->set_resource_value(M2MSecurity::ServerPublicKey,server_cert,server_cert_len); |
ram54288 | 0:a2cb7295a1f7 | 301 | register_object->set_resource_value(M2MSecurity::PublicKey,cert,cert_len); |
ram54288 | 0:a2cb7295a1f7 | 302 | register_object->set_resource_value(M2MSecurity::Secretkey,key,key_len); |
ram54288 | 0:a2cb7295a1f7 | 303 | }else if( mode == M2MSecurity::Psk ){ |
ram54288 | 0:a2cb7295a1f7 | 304 | register_object->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Psk); |
ram54288 | 0:a2cb7295a1f7 | 305 | register_object->set_resource_value(M2MSecurity::ServerPublicKey,psk_identity,psk_identity_len); |
ram54288 | 0:a2cb7295a1f7 | 306 | register_object->set_resource_value(M2MSecurity::PublicKey,psk_identity,psk_identity_len); |
ram54288 | 0:a2cb7295a1f7 | 307 | register_object->set_resource_value(M2MSecurity::Secretkey,psk,psk_len); |
ram54288 | 0:a2cb7295a1f7 | 308 | }else{ |
ram54288 | 0:a2cb7295a1f7 | 309 | register_object->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::NoSecurity); |
ram54288 | 0:a2cb7295a1f7 | 310 | } |
ram54288 | 0:a2cb7295a1f7 | 311 | |
ram54288 | 0:a2cb7295a1f7 | 312 | mbed_client.set_register_object(register_object); |
ram54288 | 0:a2cb7295a1f7 | 313 | M2MDevice* device_object = mbed_client.create_device_object(); |
ram54288 | 0:a2cb7295a1f7 | 314 | |
ram54288 | 0:a2cb7295a1f7 | 315 | M2MObject* object = mbed_client.create_generic_object(); |
ram54288 | 0:a2cb7295a1f7 | 316 | |
ram54288 | 0:a2cb7295a1f7 | 317 | M2MObjectList object_list; |
ram54288 | 0:a2cb7295a1f7 | 318 | object_list.push_back(device_object); |
ram54288 | 0:a2cb7295a1f7 | 319 | object_list.push_back(object); |
ram54288 | 0:a2cb7295a1f7 | 320 | |
ram54288 | 0:a2cb7295a1f7 | 321 | mbed_client.test_register(object_list); |
ram54288 | 0:a2cb7295a1f7 | 322 | #endif |
ram54288 | 0:a2cb7295a1f7 | 323 | |
ram54288 | 0:a2cb7295a1f7 | 324 | pthread_create(&observation_thread, NULL, &send_observation, (void*) &mbed_client); |
ram54288 | 0:a2cb7295a1f7 | 325 | pthread_create(&unregister_thread, NULL, &wait_for_unregister, (void*) &mbed_client); |
ram54288 | 0:a2cb7295a1f7 | 326 | pthread_join(unregister_thread, NULL); |
ram54288 | 0:a2cb7295a1f7 | 327 | |
ram54288 | 0:a2cb7295a1f7 | 328 | #else |
ram54288 | 0:a2cb7295a1f7 | 329 | |
ram54288 | 0:a2cb7295a1f7 | 330 | #if defined (BOOTSTRAP_ENABLED) |
ram54288 | 0:a2cb7295a1f7 | 331 | // Create LWM2M bootstrap object specifying bootstrap server |
ram54288 | 0:a2cb7295a1f7 | 332 | // information. |
ram54288 | 0:a2cb7295a1f7 | 333 | M2MSecurity* security_object = mbed_client.create_bootstrap_object(_have_secure_conn); |
ram54288 | 0:a2cb7295a1f7 | 334 | |
ram54288 | 0:a2cb7295a1f7 | 335 | // Issue bootstrap command. |
ram54288 | 0:a2cb7295a1f7 | 336 | mbed_client.test_bootstrap(security_object); |
ram54288 | 0:a2cb7295a1f7 | 337 | |
ram54288 | 0:a2cb7295a1f7 | 338 | // Wait till the bootstrap callback is called successfully. |
ram54288 | 0:a2cb7295a1f7 | 339 | // Callback comes in bootstrap_done() |
ram54288 | 0:a2cb7295a1f7 | 340 | #ifdef SIXLOWPAN_INTERFACE |
ram54288 | 0:a2cb7295a1f7 | 341 | /* wait network to be established */ |
ram54288 | 0:a2cb7295a1f7 | 342 | do { |
ram54288 | 0:a2cb7295a1f7 | 343 | mesh_interface_run(); |
ram54288 | 0:a2cb7295a1f7 | 344 | } while(!mbed_client.bootstrap_successful()); |
ram54288 | 0:a2cb7295a1f7 | 345 | #else |
ram54288 | 0:a2cb7295a1f7 | 346 | while (!mbed_client.bootstrap_successful()) { __WFI(); } |
ram54288 | 0:a2cb7295a1f7 | 347 | #endif |
ram54288 | 0:a2cb7295a1f7 | 348 | |
ram54288 | 0:a2cb7295a1f7 | 349 | #else |
ram54288 | 0:a2cb7295a1f7 | 350 | |
ram54288 | 0:a2cb7295a1f7 | 351 | M2MSecurity *register_object = mbed_client.create_register_object(_have_secure_conn); |
ram54288 | 0:a2cb7295a1f7 | 352 | |
ram54288 | 0:a2cb7295a1f7 | 353 | if( mode == M2MSecurity::Certificate ){ |
ram54288 | 0:a2cb7295a1f7 | 354 | register_object->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Certificate); |
ram54288 | 0:a2cb7295a1f7 | 355 | register_object->set_resource_value(M2MSecurity::ServerPublicKey,server_cert,server_cert_len); |
ram54288 | 0:a2cb7295a1f7 | 356 | register_object->set_resource_value(M2MSecurity::PublicKey,cert,cert_len); |
ram54288 | 0:a2cb7295a1f7 | 357 | register_object->set_resource_value(M2MSecurity::Secretkey,key,key_len); |
ram54288 | 0:a2cb7295a1f7 | 358 | }else if( mode == M2MSecurity::Psk ){ |
ram54288 | 0:a2cb7295a1f7 | 359 | register_object->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Psk); |
ram54288 | 0:a2cb7295a1f7 | 360 | register_object->set_resource_value(M2MSecurity::ServerPublicKey,psk_identity,psk_identity_len); |
ram54288 | 0:a2cb7295a1f7 | 361 | register_object->set_resource_value(M2MSecurity::PublicKey,psk_identity,psk_identity_len); |
ram54288 | 0:a2cb7295a1f7 | 362 | register_object->set_resource_value(M2MSecurity::Secretkey,psk,psk_len); |
ram54288 | 0:a2cb7295a1f7 | 363 | }else{ |
ram54288 | 0:a2cb7295a1f7 | 364 | register_object->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::NoSecurity); |
ram54288 | 0:a2cb7295a1f7 | 365 | } |
ram54288 | 0:a2cb7295a1f7 | 366 | |
ram54288 | 0:a2cb7295a1f7 | 367 | mbed_client.set_register_object(register_object); |
ram54288 | 0:a2cb7295a1f7 | 368 | |
ram54288 | 0:a2cb7295a1f7 | 369 | #endif |
ram54288 | 0:a2cb7295a1f7 | 370 | |
ram54288 | 0:a2cb7295a1f7 | 371 | // Create LWM2M device object specifying device resources |
ram54288 | 0:a2cb7295a1f7 | 372 | // as per OMA LWM2M specification. |
ram54288 | 0:a2cb7295a1f7 | 373 | M2MDevice* device_object = mbed_client.create_device_object(); |
ram54288 | 0:a2cb7295a1f7 | 374 | |
ram54288 | 0:a2cb7295a1f7 | 375 | M2MObject* object = mbed_client.create_generic_object(); |
ram54288 | 0:a2cb7295a1f7 | 376 | |
ram54288 | 0:a2cb7295a1f7 | 377 | // Add all the objects that you would like to register |
ram54288 | 0:a2cb7295a1f7 | 378 | // into the list and pass the list for register API. |
ram54288 | 0:a2cb7295a1f7 | 379 | M2MObjectList object_list; |
ram54288 | 0:a2cb7295a1f7 | 380 | object_list.push_back(device_object); |
ram54288 | 0:a2cb7295a1f7 | 381 | object_list.push_back(object); |
ram54288 | 0:a2cb7295a1f7 | 382 | |
ram54288 | 0:a2cb7295a1f7 | 383 | // Issue register command. |
ram54288 | 0:a2cb7295a1f7 | 384 | |
ram54288 | 0:a2cb7295a1f7 | 385 | FunctionPointer1<void, M2MObjectList> fp(&mbed_client, &MbedClient::test_register); |
ram54288 | 0:a2cb7295a1f7 | 386 | minar::Scheduler::postCallback(fp.bind(object_list)); |
ram54288 | 0:a2cb7295a1f7 | 387 | |
ram54288 | 0:a2cb7295a1f7 | 388 | minar::Scheduler::start(); |
ram54288 | 0:a2cb7295a1f7 | 389 | |
ram54288 | 0:a2cb7295a1f7 | 390 | // Wait till the register callback is called successfully. |
ram54288 | 0:a2cb7295a1f7 | 391 | // Callback comes in object_registered() |
ram54288 | 0:a2cb7295a1f7 | 392 | #ifdef SIXLOWPAN_INTERFACE |
ram54288 | 0:a2cb7295a1f7 | 393 | /* wait network to be established */ |
ram54288 | 0:a2cb7295a1f7 | 394 | do { |
ram54288 | 0:a2cb7295a1f7 | 395 | mesh_api->processEvent(); |
ram54288 | 0:a2cb7295a1f7 | 396 | } while(!mbed_client.register_successful()); |
ram54288 | 0:a2cb7295a1f7 | 397 | #endif |
ram54288 | 0:a2cb7295a1f7 | 398 | |
ram54288 | 0:a2cb7295a1f7 | 399 | // Wait for the unregister successful callback, |
ram54288 | 0:a2cb7295a1f7 | 400 | // Callback comes in object_unregsitered(), this will be |
ram54288 | 0:a2cb7295a1f7 | 401 | // waiting for user to press SW2 button on K64F board. |
ram54288 | 0:a2cb7295a1f7 | 402 | #ifdef SIXLOWPAN_INTERFACE |
ram54288 | 0:a2cb7295a1f7 | 403 | /* wait network to be established */ |
ram54288 | 0:a2cb7295a1f7 | 404 | do { |
ram54288 | 0:a2cb7295a1f7 | 405 | mesh_api->processEvent(); |
ram54288 | 0:a2cb7295a1f7 | 406 | } while(!mbed_client.unregister_successful()); |
ram54288 | 0:a2cb7295a1f7 | 407 | #endif |
ram54288 | 0:a2cb7295a1f7 | 408 | |
ram54288 | 0:a2cb7295a1f7 | 409 | |
ram54288 | 0:a2cb7295a1f7 | 410 | #if defined(BOOTSTRAP_ENABLED) |
ram54288 | 0:a2cb7295a1f7 | 411 | // This will turn on the LED on the board specifying that |
ram54288 | 0:a2cb7295a1f7 | 412 | // the application has run successfully. |
ram54288 | 0:a2cb7295a1f7 | 413 | notify_completion(mbed_client.unregister_successful() && |
ram54288 | 0:a2cb7295a1f7 | 414 | mbed_client.register_successful() && |
ram54288 | 0:a2cb7295a1f7 | 415 | mbed_client.bootstrap_successful()); |
ram54288 | 0:a2cb7295a1f7 | 416 | |
ram54288 | 0:a2cb7295a1f7 | 417 | // Delete security object created for bootstrapping |
ram54288 | 0:a2cb7295a1f7 | 418 | if(security_object) { |
ram54288 | 0:a2cb7295a1f7 | 419 | delete security_object; |
ram54288 | 0:a2cb7295a1f7 | 420 | } |
ram54288 | 0:a2cb7295a1f7 | 421 | |
ram54288 | 0:a2cb7295a1f7 | 422 | #else |
ram54288 | 0:a2cb7295a1f7 | 423 | |
ram54288 | 0:a2cb7295a1f7 | 424 | // Disconnect the connect and teardown the network interface |
ram54288 | 0:a2cb7295a1f7 | 425 | #ifdef SIXLOWPAN_INTERFACE |
ram54288 | 0:a2cb7295a1f7 | 426 | mesh_api->disconnect(); |
ram54288 | 0:a2cb7295a1f7 | 427 | #else |
ram54288 | 0:a2cb7295a1f7 | 428 | eth.disconnect(); |
ram54288 | 0:a2cb7295a1f7 | 429 | #endif |
ram54288 | 0:a2cb7295a1f7 | 430 | #endif //BOOTSTRAP_ENABLED |
ram54288 | 0:a2cb7295a1f7 | 431 | |
ram54288 | 0:a2cb7295a1f7 | 432 | // Delete device object created for registering device |
ram54288 | 0:a2cb7295a1f7 | 433 | // resources. |
ram54288 | 0:a2cb7295a1f7 | 434 | if(device_object) { |
ram54288 | 0:a2cb7295a1f7 | 435 | M2MDevice::delete_instance(); |
ram54288 | 0:a2cb7295a1f7 | 436 | } |
ram54288 | 0:a2cb7295a1f7 | 437 | if(object) { |
ram54288 | 0:a2cb7295a1f7 | 438 | delete object; |
ram54288 | 0:a2cb7295a1f7 | 439 | } |
ram54288 | 0:a2cb7295a1f7 | 440 | |
ram54288 | 0:a2cb7295a1f7 | 441 | #endif //TARGET_LIKE_LINUX |
ram54288 | 0:a2cb7295a1f7 | 442 | |
ram54288 | 0:a2cb7295a1f7 | 443 | #ifndef TARGET_LIKE_MBED |
ram54288 | 0:a2cb7295a1f7 | 444 | return 0; |
ram54288 | 0:a2cb7295a1f7 | 445 | #endif |
ram54288 | 0:a2cb7295a1f7 | 446 | } |