This is an example of BLE GATT Client, which receives broadcast data from BLE_Server_BME280 ( a GATT server) , then transfers values up to mbed Device Connector (cloud).

Please refer details about BLEClient_mbedDevConn below. https://github.com/soramame21/BLEClient_mbedDevConn

The location of required BLE GATT server, BLE_Server_BME280, is at here. https://developer.mbed.org/users/edamame22/code/BLE_Server_BME280/

Committer:
Ren Boting
Date:
Tue Sep 05 11:56:13 2017 +0900
Revision:
2:b894b3508057
Parent:
0:29983394c6b6
Update all libraries and reform main.cpp

Who changed what in which revision?

UserRevisionLine numberNew contents of line
edamame22 0:29983394c6b6 1 /*
edamame22 0:29983394c6b6 2 * Copyright (c) 2015 ARM Limited. All rights reserved.
edamame22 0:29983394c6b6 3 * SPDX-License-Identifier: Apache-2.0
edamame22 0:29983394c6b6 4 * Licensed under the Apache License, Version 2.0 (the License); you may
edamame22 0:29983394c6b6 5 * not use this file except in compliance with the License.
edamame22 0:29983394c6b6 6 * You may obtain a copy of the License at
edamame22 0:29983394c6b6 7 *
edamame22 0:29983394c6b6 8 * http://www.apache.org/licenses/LICENSE-2.0
edamame22 0:29983394c6b6 9 *
edamame22 0:29983394c6b6 10 * Unless required by applicable law or agreed to in writing, software
edamame22 0:29983394c6b6 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
edamame22 0:29983394c6b6 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
edamame22 0:29983394c6b6 13 * See the License for the specific language governing permissions and
edamame22 0:29983394c6b6 14 * limitations under the License.
edamame22 0:29983394c6b6 15 */
edamame22 0:29983394c6b6 16 #include <assert.h>
edamame22 0:29983394c6b6 17 #include <stdlib.h>
edamame22 0:29983394c6b6 18 #include "include/m2minterfaceimpl.h"
edamame22 0:29983394c6b6 19 #include "include/eventdata.h"
edamame22 0:29983394c6b6 20 #include "mbed-client/m2minterfaceobserver.h"
edamame22 0:29983394c6b6 21 #include "mbed-client/m2mconnectionhandler.h"
edamame22 0:29983394c6b6 22 #include "mbed-client/m2mconnectionsecurity.h"
edamame22 0:29983394c6b6 23 #include "include/m2mnsdlinterface.h"
edamame22 0:29983394c6b6 24 #include "include/nsdlaccesshelper.h"
edamame22 0:29983394c6b6 25 #include "mbed-client/m2msecurity.h"
edamame22 0:29983394c6b6 26 #include "mbed-client/m2mconstants.h"
edamame22 0:29983394c6b6 27 #include "mbed-client/m2mtimer.h"
edamame22 0:29983394c6b6 28 #include "mbed-trace/mbed_trace.h"
edamame22 0:29983394c6b6 29
edamame22 0:29983394c6b6 30 #include <stdlib.h>
edamame22 0:29983394c6b6 31
edamame22 0:29983394c6b6 32 #define TRACE_GROUP "mClt"
edamame22 0:29983394c6b6 33
edamame22 0:29983394c6b6 34 #define RESOLVE_SEC_MODE(mode) ((mode == M2MInterface::TCP || mode == M2MInterface::TCP_QUEUE) ? M2MConnectionSecurity::TLS : M2MConnectionSecurity::DTLS)
edamame22 0:29983394c6b6 35
edamame22 0:29983394c6b6 36 M2MInterfaceImpl::M2MInterfaceImpl(M2MInterfaceObserver& observer,
edamame22 0:29983394c6b6 37 const String &ep_name,
edamame22 0:29983394c6b6 38 const String &ep_type,
edamame22 0:29983394c6b6 39 const int32_t l_time,
edamame22 0:29983394c6b6 40 const uint16_t listen_port,
edamame22 0:29983394c6b6 41 const String &dmn,
edamame22 0:29983394c6b6 42 M2MInterface::BindingMode mode,
edamame22 0:29983394c6b6 43 M2MInterface::NetworkStack stack,
edamame22 0:29983394c6b6 44 const String &con_addr)
edamame22 0:29983394c6b6 45 : _event_data(NULL),
edamame22 0:29983394c6b6 46 _bootstrap_timer(NULL),
edamame22 0:29983394c6b6 47 _server_port(0),
edamame22 0:29983394c6b6 48 _listen_port(listen_port),
edamame22 0:29983394c6b6 49 _endpoint_type(ep_type),
edamame22 0:29983394c6b6 50 _domain( dmn),
edamame22 0:29983394c6b6 51 _life_time(l_time),
edamame22 0:29983394c6b6 52 _context_address(con_addr),
edamame22 0:29983394c6b6 53 _register_server(NULL),
edamame22 0:29983394c6b6 54 _queue_sleep_timer(*this),
edamame22 0:29983394c6b6 55 _retry_timer(*this),
edamame22 0:29983394c6b6 56 _callback_handler(NULL),
edamame22 0:29983394c6b6 57 _max_states( STATE_MAX_STATES ),
edamame22 0:29983394c6b6 58 _event_ignored(false),
edamame22 0:29983394c6b6 59 _event_generated(false),
edamame22 0:29983394c6b6 60 _reconnecting(false),
edamame22 0:29983394c6b6 61 _retry_timer_expired(false),
edamame22 0:29983394c6b6 62 _bootstrapped(true), // True as default to get it working with connector only configuration
edamame22 0:29983394c6b6 63 _current_state(0),
edamame22 0:29983394c6b6 64 _retry_count(0),
edamame22 0:29983394c6b6 65 _binding_mode(mode),
edamame22 0:29983394c6b6 66 _observer(observer),
edamame22 0:29983394c6b6 67 _security_connection( new M2MConnectionSecurity( RESOLVE_SEC_MODE(mode) )),
edamame22 0:29983394c6b6 68 _connection_handler(*this, _security_connection, mode, stack),
edamame22 0:29983394c6b6 69 _nsdl_interface(*this),
edamame22 0:29983394c6b6 70 _security(NULL)
edamame22 0:29983394c6b6 71 {
edamame22 0:29983394c6b6 72 tr_debug("M2MInterfaceImpl::M2MInterfaceImpl() -IN");
edamame22 0:29983394c6b6 73 _nsdl_interface.create_endpoint(ep_name,
edamame22 0:29983394c6b6 74 _endpoint_type,
edamame22 0:29983394c6b6 75 _life_time,
edamame22 0:29983394c6b6 76 _domain,
edamame22 0:29983394c6b6 77 (uint8_t)_binding_mode & 0x07, // nsdl binding mode is only 3 least significant bits
edamame22 0:29983394c6b6 78 _context_address);
edamame22 0:29983394c6b6 79
edamame22 0:29983394c6b6 80 //Here we must use TCP still
edamame22 0:29983394c6b6 81 __connection_handler = &_connection_handler;
edamame22 0:29983394c6b6 82 _connection_handler.bind_connection(_listen_port);
edamame22 0:29983394c6b6 83 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
edamame22 0:29983394c6b6 84 _bootstrap_timer = new M2MTimer(*this);
edamame22 0:29983394c6b6 85 #endif
edamame22 0:29983394c6b6 86 tr_debug("M2MInterfaceImpl::M2MInterfaceImpl() -OUT");
edamame22 0:29983394c6b6 87 }
edamame22 0:29983394c6b6 88
edamame22 0:29983394c6b6 89
edamame22 0:29983394c6b6 90 M2MInterfaceImpl::~M2MInterfaceImpl()
edamame22 0:29983394c6b6 91 {
edamame22 0:29983394c6b6 92 tr_debug("M2MInterfaceImpl::~M2MInterfaceImpl() - IN");
edamame22 0:29983394c6b6 93 _connection_handler.stop_listening();
edamame22 0:29983394c6b6 94 delete _bootstrap_timer;
edamame22 0:29983394c6b6 95 _security_connection = NULL;
edamame22 0:29983394c6b6 96 tr_debug("M2MInterfaceImpl::~M2MInterfaceImpl() - OUT");
edamame22 0:29983394c6b6 97 }
edamame22 0:29983394c6b6 98
edamame22 0:29983394c6b6 99 void M2MInterfaceImpl::bootstrap(M2MSecurity *security)
edamame22 0:29983394c6b6 100 {
edamame22 0:29983394c6b6 101 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
edamame22 0:29983394c6b6 102 tr_debug("M2MInterfaceImpl::bootstrap(M2MSecurity *security) - IN");
edamame22 0:29983394c6b6 103 if(!security) {
edamame22 0:29983394c6b6 104 _observer.error(M2MInterface::InvalidParameters);
edamame22 0:29983394c6b6 105 return;
edamame22 0:29983394c6b6 106 }
edamame22 0:29983394c6b6 107 // Transition to a new state based upon
edamame22 0:29983394c6b6 108 // the current state of the state machine
edamame22 0:29983394c6b6 109 M2MSecurityData* data = new M2MSecurityData();
edamame22 0:29983394c6b6 110 data->_object = security;
edamame22 0:29983394c6b6 111 BEGIN_TRANSITION_MAP // - Current State -
edamame22 0:29983394c6b6 112 TRANSITION_MAP_ENTRY (STATE_BOOTSTRAP) // state_idle
edamame22 0:29983394c6b6 113 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap
edamame22 0:29983394c6b6 114 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state__bootstrap_address_resolved
edamame22 0:29983394c6b6 115 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap_resource_created
edamame22 0:29983394c6b6 116 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap_wait
edamame22 0:29983394c6b6 117 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrapped
edamame22 0:29983394c6b6 118 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_register
edamame22 0:29983394c6b6 119 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_register_address_resolved
edamame22 0:29983394c6b6 120 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_registered
edamame22 0:29983394c6b6 121 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_update_registration
edamame22 0:29983394c6b6 122 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_unregister
edamame22 0:29983394c6b6 123 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_unregistered
edamame22 0:29983394c6b6 124 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_sending_coap_data
edamame22 0:29983394c6b6 125 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_sent
edamame22 0:29983394c6b6 126 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_received
edamame22 0:29983394c6b6 127 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_processing_coap_data
edamame22 0:29983394c6b6 128 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_processed
edamame22 0:29983394c6b6 129 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_waiting
edamame22 0:29983394c6b6 130 END_TRANSITION_MAP(data)
edamame22 0:29983394c6b6 131 if(_event_ignored) {
edamame22 0:29983394c6b6 132 _event_ignored = false;
edamame22 0:29983394c6b6 133 _observer.error(M2MInterface::NotAllowed);
edamame22 0:29983394c6b6 134 }
edamame22 0:29983394c6b6 135 tr_debug("M2MInterfaceImpl::bootstrap(M2MSecurity *security) - OUT");
edamame22 0:29983394c6b6 136 #else
edamame22 0:29983394c6b6 137 _observer.error(M2MInterface::NotAllowed);
edamame22 0:29983394c6b6 138 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
edamame22 0:29983394c6b6 139 }
edamame22 0:29983394c6b6 140
edamame22 0:29983394c6b6 141 void M2MInterfaceImpl::cancel_bootstrap()
edamame22 0:29983394c6b6 142 {
edamame22 0:29983394c6b6 143 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
edamame22 0:29983394c6b6 144 //TODO: Do we need this ?
edamame22 0:29983394c6b6 145 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
edamame22 0:29983394c6b6 146 }
edamame22 0:29983394c6b6 147
edamame22 0:29983394c6b6 148 void M2MInterfaceImpl::register_object(M2MSecurity *security, const M2MObjectList &object_list)
edamame22 0:29983394c6b6 149 {
edamame22 0:29983394c6b6 150 tr_debug("M2MInterfaceImpl::register_object - IN");
edamame22 0:29983394c6b6 151 if(!security) {
edamame22 0:29983394c6b6 152 _observer.error(M2MInterface::InvalidParameters);
edamame22 0:29983394c6b6 153 return;
edamame22 0:29983394c6b6 154 }
edamame22 0:29983394c6b6 155 // Transition to a new state based upon
edamame22 0:29983394c6b6 156 // the current state of the state machine
edamame22 0:29983394c6b6 157 //TODO: manage register object in a list.
edamame22 0:29983394c6b6 158 _register_server = security;
edamame22 0:29983394c6b6 159 M2MRegisterData *data = new M2MRegisterData();
edamame22 0:29983394c6b6 160 data->_object = security;
edamame22 0:29983394c6b6 161 data->_object_list = object_list;
edamame22 0:29983394c6b6 162 BEGIN_TRANSITION_MAP // - Current State -
edamame22 0:29983394c6b6 163 TRANSITION_MAP_ENTRY (STATE_REGISTER) // state_idle
edamame22 0:29983394c6b6 164 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap
edamame22 0:29983394c6b6 165 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state__bootstrap_address_resolved
edamame22 0:29983394c6b6 166 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap_resource_created
edamame22 0:29983394c6b6 167 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap_wait
edamame22 0:29983394c6b6 168 TRANSITION_MAP_ENTRY (STATE_REGISTER) // state_bootstrapped
edamame22 0:29983394c6b6 169 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_register
edamame22 0:29983394c6b6 170 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_register_address_resolved
edamame22 0:29983394c6b6 171 TRANSITION_MAP_ENTRY (STATE_REGISTER) // state_registered
edamame22 0:29983394c6b6 172 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_update_registration
edamame22 0:29983394c6b6 173 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_unregister
edamame22 0:29983394c6b6 174 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_unregistered
edamame22 0:29983394c6b6 175 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_sending_coap_data
edamame22 0:29983394c6b6 176 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_sent
edamame22 0:29983394c6b6 177 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_received
edamame22 0:29983394c6b6 178 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_processing_coap_data
edamame22 0:29983394c6b6 179 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_processed
edamame22 0:29983394c6b6 180 TRANSITION_MAP_ENTRY (STATE_REGISTER) // state_waiting
edamame22 0:29983394c6b6 181 END_TRANSITION_MAP(data)
edamame22 0:29983394c6b6 182 if(_event_ignored) {
edamame22 0:29983394c6b6 183 _event_ignored = false;
edamame22 0:29983394c6b6 184 _observer.error(M2MInterface::NotAllowed);
edamame22 0:29983394c6b6 185 }
edamame22 0:29983394c6b6 186 tr_debug("M2MInterfaceImpl::register_object - OUT");
edamame22 0:29983394c6b6 187 }
edamame22 0:29983394c6b6 188
edamame22 0:29983394c6b6 189 void M2MInterfaceImpl::update_registration(M2MSecurity *security_object, const uint32_t lifetime)
edamame22 0:29983394c6b6 190 {
edamame22 0:29983394c6b6 191 tr_debug("M2MInterfaceImpl::update_registration(M2MSecurity *security_object, const uint32_t lifetime)");
edamame22 0:29983394c6b6 192 M2MUpdateRegisterData *data = new M2MUpdateRegisterData();
edamame22 0:29983394c6b6 193 data->_object = security_object;
edamame22 0:29983394c6b6 194 data->_lifetime = lifetime;
edamame22 0:29983394c6b6 195 start_register_update(data);
edamame22 0:29983394c6b6 196 }
edamame22 0:29983394c6b6 197
edamame22 0:29983394c6b6 198 void M2MInterfaceImpl::update_registration(M2MSecurity *security_object,
edamame22 0:29983394c6b6 199 const M2MObjectList &object_list,
edamame22 0:29983394c6b6 200 const uint32_t lifetime)
edamame22 0:29983394c6b6 201 {
edamame22 0:29983394c6b6 202 tr_debug("M2MInterfaceImpl::update_registration(M2MSecurity *security_object, const M2MObjectList &object_list, const uint32_t lifetime)");
edamame22 0:29983394c6b6 203 M2MUpdateRegisterData *data = new M2MUpdateRegisterData();
edamame22 0:29983394c6b6 204 data->_object = security_object;
edamame22 0:29983394c6b6 205 data->_lifetime = lifetime;
edamame22 0:29983394c6b6 206 data->_object_list = object_list;
edamame22 0:29983394c6b6 207 start_register_update(data);
edamame22 0:29983394c6b6 208 }
edamame22 0:29983394c6b6 209
edamame22 0:29983394c6b6 210 void M2MInterfaceImpl::unregister_object(M2MSecurity* /*security*/)
edamame22 0:29983394c6b6 211 {
edamame22 0:29983394c6b6 212 tr_debug("M2MInterfaceImpl::unregister_object - IN");
edamame22 0:29983394c6b6 213 tr_debug("M2MInterfaceImpl::unregister_object - current state %d", _current_state);
edamame22 0:29983394c6b6 214 // Transition to a new state based upon
edamame22 0:29983394c6b6 215 // the current state of the state machine
edamame22 0:29983394c6b6 216 BEGIN_TRANSITION_MAP // - Current State -
edamame22 0:29983394c6b6 217 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_idle
edamame22 0:29983394c6b6 218 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap
edamame22 0:29983394c6b6 219 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state__bootstrap_address_resolved
edamame22 0:29983394c6b6 220 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap_resource_created
edamame22 0:29983394c6b6 221 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap_wait
edamame22 0:29983394c6b6 222 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrapped
edamame22 0:29983394c6b6 223 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_register
edamame22 0:29983394c6b6 224 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_register_address_resolved
edamame22 0:29983394c6b6 225 TRANSITION_MAP_ENTRY (STATE_UNREGISTER) // state_registered
edamame22 0:29983394c6b6 226 TRANSITION_MAP_ENTRY (STATE_UNREGISTER) // state_update_registration
edamame22 0:29983394c6b6 227 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_unregister
edamame22 0:29983394c6b6 228 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_unregistered
edamame22 0:29983394c6b6 229 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_sending_coap_data
edamame22 0:29983394c6b6 230 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_sent
edamame22 0:29983394c6b6 231 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_received
edamame22 0:29983394c6b6 232 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_processing_coap_data
edamame22 0:29983394c6b6 233 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_processed
edamame22 0:29983394c6b6 234 TRANSITION_MAP_ENTRY (STATE_UNREGISTER) // state_waiting
edamame22 0:29983394c6b6 235 END_TRANSITION_MAP(NULL)
edamame22 0:29983394c6b6 236 if(_event_ignored) {
edamame22 0:29983394c6b6 237 _event_ignored = false;
edamame22 0:29983394c6b6 238 _observer.error(M2MInterface::NotAllowed);
edamame22 0:29983394c6b6 239 }
edamame22 0:29983394c6b6 240 tr_debug("M2MInterfaceImpl::unregister_object - OUT");
edamame22 0:29983394c6b6 241 }
edamame22 0:29983394c6b6 242
edamame22 0:29983394c6b6 243 void M2MInterfaceImpl::set_queue_sleep_handler(callback_handler handler)
edamame22 0:29983394c6b6 244 {
edamame22 0:29983394c6b6 245 tr_debug("M2MInterfaceImpl::set_queue_sleep_handler()");
edamame22 0:29983394c6b6 246 _callback_handler = handler;
edamame22 0:29983394c6b6 247 }
edamame22 0:29983394c6b6 248
edamame22 0:29983394c6b6 249 void M2MInterfaceImpl::set_random_number_callback(random_number_cb callback)
edamame22 0:29983394c6b6 250 {
edamame22 0:29983394c6b6 251 if(_security_connection) {
edamame22 0:29983394c6b6 252 _security_connection->set_random_number_callback(callback);
edamame22 0:29983394c6b6 253 }
edamame22 0:29983394c6b6 254 }
edamame22 0:29983394c6b6 255
edamame22 0:29983394c6b6 256 void M2MInterfaceImpl::set_entropy_callback(entropy_cb callback)
edamame22 0:29983394c6b6 257 {
edamame22 0:29983394c6b6 258 if(_security_connection) {
edamame22 0:29983394c6b6 259 _security_connection->set_entropy_callback(callback);
edamame22 0:29983394c6b6 260 }
edamame22 0:29983394c6b6 261 }
edamame22 0:29983394c6b6 262
edamame22 0:29983394c6b6 263 void M2MInterfaceImpl::set_platform_network_handler(void *handler)
edamame22 0:29983394c6b6 264 {
edamame22 0:29983394c6b6 265 tr_debug("M2MInterfaceImpl::set_platform_network_handler()");
edamame22 0:29983394c6b6 266 _connection_handler.set_platform_network_handler(handler);
edamame22 0:29983394c6b6 267 }
edamame22 0:29983394c6b6 268
edamame22 0:29983394c6b6 269 void M2MInterfaceImpl::coap_message_ready(uint8_t *data_ptr,
edamame22 0:29983394c6b6 270 uint16_t data_len,
edamame22 0:29983394c6b6 271 sn_nsdl_addr_s *address_ptr)
edamame22 0:29983394c6b6 272 {
edamame22 0:29983394c6b6 273 tr_debug("M2MInterfaceImpl::coap_message_ready");
edamame22 0:29983394c6b6 274 if (_current_state != STATE_IDLE) {
edamame22 0:29983394c6b6 275 internal_event(STATE_SENDING_COAP_DATA);
edamame22 0:29983394c6b6 276 if(!_connection_handler.send_data(data_ptr,data_len,address_ptr)) {
edamame22 0:29983394c6b6 277 internal_event( STATE_IDLE);
edamame22 0:29983394c6b6 278 tr_error("M2MInterfaceImpl::coap_message_ready() - M2MInterface::NetworkError");
edamame22 0:29983394c6b6 279 if (!_reconnecting) {
edamame22 0:29983394c6b6 280 _observer.error(M2MInterface::NetworkError);
edamame22 0:29983394c6b6 281 }
edamame22 0:29983394c6b6 282 }
edamame22 0:29983394c6b6 283 }
edamame22 0:29983394c6b6 284 }
edamame22 0:29983394c6b6 285
edamame22 0:29983394c6b6 286 void M2MInterfaceImpl::client_registered(M2MServer *server_object)
edamame22 0:29983394c6b6 287 {
edamame22 0:29983394c6b6 288 tr_debug("M2MInterfaceImpl::client_registered");
edamame22 0:29983394c6b6 289 _retry_count = 0;
edamame22 0:29983394c6b6 290 internal_event(STATE_REGISTERED);
edamame22 0:29983394c6b6 291 //Inform client is registered.
edamame22 0:29983394c6b6 292 //TODO: manage register object in a list.
edamame22 0:29983394c6b6 293 _observer.object_registered(_register_server,*server_object);
edamame22 0:29983394c6b6 294 }
edamame22 0:29983394c6b6 295
edamame22 0:29983394c6b6 296 void M2MInterfaceImpl::registration_updated(const M2MServer &server_object)
edamame22 0:29983394c6b6 297 {
edamame22 0:29983394c6b6 298 tr_debug("M2MInterfaceImpl::registration_updated");
edamame22 0:29983394c6b6 299 internal_event(STATE_REGISTERED);
edamame22 0:29983394c6b6 300 _observer.registration_updated(_register_server,server_object);
edamame22 0:29983394c6b6 301 }
edamame22 0:29983394c6b6 302
edamame22 0:29983394c6b6 303 void M2MInterfaceImpl::registration_error(uint8_t error_code, bool retry)
edamame22 0:29983394c6b6 304 {
edamame22 0:29983394c6b6 305 tr_debug("M2MInterfaceImpl::registration_error code [%d]", error_code);
edamame22 0:29983394c6b6 306 // Try to register again
edamame22 0:29983394c6b6 307 if (retry) {
edamame22 0:29983394c6b6 308 socket_error(M2MConnectionHandler::SOCKET_SEND_ERROR);
edamame22 0:29983394c6b6 309 } else {
edamame22 0:29983394c6b6 310 internal_event(STATE_IDLE);
edamame22 0:29983394c6b6 311 _observer.error((M2MInterface::Error)error_code);
edamame22 0:29983394c6b6 312 }
edamame22 0:29983394c6b6 313 }
edamame22 0:29983394c6b6 314
edamame22 0:29983394c6b6 315 void M2MInterfaceImpl::client_unregistered()
edamame22 0:29983394c6b6 316 {
edamame22 0:29983394c6b6 317 tr_debug("M2MInterfaceImpl::client_unregistered()");
edamame22 0:29983394c6b6 318 internal_event(STATE_UNREGISTERED);
edamame22 0:29983394c6b6 319 //TODO: manage register object in a list.
edamame22 0:29983394c6b6 320 _observer.object_unregistered(_register_server);
edamame22 0:29983394c6b6 321 }
edamame22 0:29983394c6b6 322
edamame22 0:29983394c6b6 323 void M2MInterfaceImpl::bootstrap_done(M2MSecurity *security_object)
edamame22 0:29983394c6b6 324 {
edamame22 0:29983394c6b6 325 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
edamame22 0:29983394c6b6 326 tr_debug("M2MInterfaceImpl::bootstrap_done");
edamame22 0:29983394c6b6 327 _retry_count = 0;
edamame22 0:29983394c6b6 328 _reconnecting = false;
edamame22 0:29983394c6b6 329 _bootstrapped = true;
edamame22 0:29983394c6b6 330 _bootstrap_timer->stop_timer();
edamame22 0:29983394c6b6 331 internal_event(STATE_BOOTSTRAPPED);
edamame22 0:29983394c6b6 332 _observer.bootstrap_done(security_object);
edamame22 0:29983394c6b6 333 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
edamame22 0:29983394c6b6 334 }
edamame22 0:29983394c6b6 335
edamame22 0:29983394c6b6 336 void M2MInterfaceImpl::bootstrap_wait(M2MSecurity *security_object)
edamame22 0:29983394c6b6 337 {
edamame22 0:29983394c6b6 338 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
edamame22 0:29983394c6b6 339 tr_debug("M2MInterfaceImpl::bootstrap_wait");
edamame22 0:29983394c6b6 340 _security = security_object;
edamame22 0:29983394c6b6 341 internal_event(STATE_BOOTSTRAP_WAIT);
edamame22 0:29983394c6b6 342 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
edamame22 0:29983394c6b6 343 }
edamame22 0:29983394c6b6 344
edamame22 0:29983394c6b6 345 void M2MInterfaceImpl::bootstrap_error()
edamame22 0:29983394c6b6 346 {
edamame22 0:29983394c6b6 347 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
edamame22 0:29983394c6b6 348 tr_debug("M2MInterfaceImpl::bootstrap_error()");
edamame22 0:29983394c6b6 349 _bootstrapped = false;
edamame22 0:29983394c6b6 350 _bootstrap_timer->stop_timer();
edamame22 0:29983394c6b6 351 internal_event(STATE_IDLE);
edamame22 0:29983394c6b6 352 _observer.error(M2MInterface::BootstrapFailed);
edamame22 0:29983394c6b6 353 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
edamame22 0:29983394c6b6 354 }
edamame22 0:29983394c6b6 355
edamame22 0:29983394c6b6 356 void M2MInterfaceImpl::coap_data_processed()
edamame22 0:29983394c6b6 357 {
edamame22 0:29983394c6b6 358 tr_debug("M2MInterfaceImpl::coap_data_processed()");
edamame22 0:29983394c6b6 359 internal_event(STATE_COAP_DATA_PROCESSED);
edamame22 0:29983394c6b6 360 }
edamame22 0:29983394c6b6 361
edamame22 0:29983394c6b6 362 void M2MInterfaceImpl::value_updated(M2MBase *base)
edamame22 0:29983394c6b6 363 {
edamame22 0:29983394c6b6 364 tr_debug("M2MInterfaceImpl::value_updated");
edamame22 0:29983394c6b6 365 if(base) {
edamame22 0:29983394c6b6 366 M2MBase::BaseType type = base->base_type();
edamame22 0:29983394c6b6 367 _observer.value_updated(base, type);
edamame22 0:29983394c6b6 368 }
edamame22 0:29983394c6b6 369 }
edamame22 0:29983394c6b6 370
edamame22 0:29983394c6b6 371 void M2MInterfaceImpl::data_available(uint8_t* data,
edamame22 0:29983394c6b6 372 uint16_t data_size,
edamame22 0:29983394c6b6 373 const M2MConnectionObserver::SocketAddress &address)
edamame22 0:29983394c6b6 374 {
edamame22 0:29983394c6b6 375 tr_debug("M2MInterfaceImpl::data_available");
edamame22 0:29983394c6b6 376 ReceivedData *event = new ReceivedData();
edamame22 0:29983394c6b6 377 event->_data = data;
edamame22 0:29983394c6b6 378 event->_size = data_size;
edamame22 0:29983394c6b6 379 event->_address = &address;
edamame22 0:29983394c6b6 380 internal_event(STATE_COAP_DATA_RECEIVED, event);
edamame22 0:29983394c6b6 381 }
edamame22 0:29983394c6b6 382
edamame22 0:29983394c6b6 383 void M2MInterfaceImpl::socket_error(uint8_t error_code, bool retry)
edamame22 0:29983394c6b6 384 {
edamame22 0:29983394c6b6 385 tr_debug("M2MInterfaceImpl::socket_error: (%d), retry (%d), reconnecting (%d)", error_code, retry, _reconnecting);
edamame22 0:29983394c6b6 386 #if MBED_CLIENT_RECONNECTION_LOOP < 1
edamame22 0:29983394c6b6 387 if (!_retry_timer_expired && _reconnecting) {
edamame22 0:29983394c6b6 388 tr_debug("M2MInterfaceImpl::socket_error - retry timer running - return");
edamame22 0:29983394c6b6 389 return;
edamame22 0:29983394c6b6 390 }
edamame22 0:29983394c6b6 391 #endif
edamame22 0:29983394c6b6 392
edamame22 0:29983394c6b6 393 M2MInterface::Error error = M2MInterface::ErrorNone;
edamame22 0:29983394c6b6 394 switch (error_code) {
edamame22 0:29983394c6b6 395 case M2MConnectionHandler::SSL_CONNECTION_ERROR:
edamame22 0:29983394c6b6 396 error = M2MInterface::SecureConnectionFailed;
edamame22 0:29983394c6b6 397 break;
edamame22 0:29983394c6b6 398 case M2MConnectionHandler::DNS_RESOLVING_ERROR:
edamame22 0:29983394c6b6 399 error = M2MInterface::DnsResolvingFailed;
edamame22 0:29983394c6b6 400 break;
edamame22 0:29983394c6b6 401 case M2MConnectionHandler::SOCKET_READ_ERROR:
edamame22 0:29983394c6b6 402 error = M2MInterface::NetworkError;
edamame22 0:29983394c6b6 403 break;
edamame22 0:29983394c6b6 404 case M2MConnectionHandler::SOCKET_SEND_ERROR:
edamame22 0:29983394c6b6 405 error = M2MInterface::NetworkError;
edamame22 0:29983394c6b6 406 break;
edamame22 0:29983394c6b6 407 case M2MConnectionHandler::SSL_HANDSHAKE_ERROR:
edamame22 0:29983394c6b6 408 error = M2MInterface::SecureConnectionFailed;
edamame22 0:29983394c6b6 409 break;
edamame22 0:29983394c6b6 410 case M2MConnectionHandler::SOCKET_ABORT:
edamame22 0:29983394c6b6 411 error = M2MInterface::NetworkError;
edamame22 0:29983394c6b6 412 break;
edamame22 0:29983394c6b6 413 default:
edamame22 0:29983394c6b6 414 break;
edamame22 0:29983394c6b6 415 }
edamame22 0:29983394c6b6 416
edamame22 0:29983394c6b6 417 // Try to do reconnecting
edamame22 0:29983394c6b6 418 if (retry) {
edamame22 0:29983394c6b6 419 if (_retry_count < MBED_CLIENT_RECONNECTION_COUNT) {
edamame22 0:29983394c6b6 420 _retry_count++;
edamame22 0:29983394c6b6 421 }
edamame22 0:29983394c6b6 422 #if MBED_CLIENT_RECONNECTION_LOOP > 0
edamame22 0:29983394c6b6 423 else {
edamame22 0:29983394c6b6 424 tr_debug("M2MInterfaceImpl::socket_error - start again");
edamame22 0:29983394c6b6 425 _retry_count = 1;
edamame22 0:29983394c6b6 426 _observer.error(error);
edamame22 0:29983394c6b6 427 }
edamame22 0:29983394c6b6 428 #else
edamame22 0:29983394c6b6 429 else {
edamame22 0:29983394c6b6 430 tr_debug("M2MInterfaceImpl::socket_error - no more retries");
edamame22 0:29983394c6b6 431 _connection_handler.stop_listening();
edamame22 0:29983394c6b6 432 _retry_timer.stop_timer();
edamame22 0:29983394c6b6 433 retry = false;
edamame22 0:29983394c6b6 434 }
edamame22 0:29983394c6b6 435 #endif
edamame22 0:29983394c6b6 436 if (retry) {
edamame22 0:29983394c6b6 437 internal_event(STATE_IDLE);
edamame22 0:29983394c6b6 438 _reconnecting = true;
edamame22 0:29983394c6b6 439 _connection_handler.stop_listening();
edamame22 0:29983394c6b6 440 int retry_time = MBED_CLIENT_RECONNECTION_INTERVAL *
edamame22 0:29983394c6b6 441 MBED_CLIENT_RECONNECTION_COUNT * _retry_count * 1000;
edamame22 0:29983394c6b6 442 _retry_timer_expired = false;
edamame22 0:29983394c6b6 443 _retry_timer.start_timer(retry_time,
edamame22 0:29983394c6b6 444 M2MTimerObserver::RetryTimer);
edamame22 0:29983394c6b6 445 tr_debug("M2MInterfaceImpl::socket_error - reconnecting in %d(s), count %d/%d", retry_time / 1000,
edamame22 0:29983394c6b6 446 _retry_count, MBED_CLIENT_RECONNECTION_COUNT);
edamame22 0:29983394c6b6 447 }
edamame22 0:29983394c6b6 448 }
edamame22 0:29983394c6b6 449 // Inform application
edamame22 0:29983394c6b6 450 if (!retry && M2MInterface::ErrorNone != error) {
edamame22 0:29983394c6b6 451 tr_debug("M2MInterfaceImpl::socket_error - send error to application");
edamame22 0:29983394c6b6 452 _connection_handler.stop_listening();
edamame22 0:29983394c6b6 453 _retry_timer.stop_timer();
edamame22 0:29983394c6b6 454 _retry_count = 0;
edamame22 0:29983394c6b6 455 _reconnecting = false;
edamame22 0:29983394c6b6 456 _observer.error(error);
edamame22 0:29983394c6b6 457 internal_event(STATE_IDLE);
edamame22 0:29983394c6b6 458 }
edamame22 0:29983394c6b6 459 }
edamame22 0:29983394c6b6 460
edamame22 0:29983394c6b6 461 void M2MInterfaceImpl::address_ready(const M2MConnectionObserver::SocketAddress &address,
edamame22 0:29983394c6b6 462 M2MConnectionObserver::ServerType server_type,
edamame22 0:29983394c6b6 463 const uint16_t server_port)
edamame22 0:29983394c6b6 464 {
edamame22 0:29983394c6b6 465 tr_debug("M2MInterfaceImpl::address_ready");
edamame22 0:29983394c6b6 466 ResolvedAddressData *data = new ResolvedAddressData();
edamame22 0:29983394c6b6 467 data->_address = &address;
edamame22 0:29983394c6b6 468 data->_port = server_port;
edamame22 0:29983394c6b6 469 if( M2MConnectionObserver::Bootstrap == server_type) {
edamame22 0:29983394c6b6 470 tr_debug("M2MInterfaceImpl::address_ready() Server Type Bootstrap");
edamame22 0:29983394c6b6 471 internal_event(STATE_BOOTSTRAP_ADDRESS_RESOLVED, data);
edamame22 0:29983394c6b6 472 } else {
edamame22 0:29983394c6b6 473 tr_debug("M2MInterfaceImpl::address_ready() Server Type LWM2M");
edamame22 0:29983394c6b6 474 internal_event(STATE_REGISTER_ADDRESS_RESOLVED, data);
edamame22 0:29983394c6b6 475 }
edamame22 0:29983394c6b6 476 }
edamame22 0:29983394c6b6 477
edamame22 0:29983394c6b6 478 void M2MInterfaceImpl::data_sent()
edamame22 0:29983394c6b6 479 {
edamame22 0:29983394c6b6 480 tr_debug("M2MInterfaceImpl::data_sent()");
edamame22 0:29983394c6b6 481 if(_binding_mode == M2MInterface::UDP_QUEUE ||
edamame22 0:29983394c6b6 482 _binding_mode == M2MInterface::TCP_QUEUE ||
edamame22 0:29983394c6b6 483 _binding_mode == M2MInterface::SMS_QUEUE ||
edamame22 0:29983394c6b6 484 _binding_mode == M2MInterface::UDP_SMS_QUEUE) {
edamame22 0:29983394c6b6 485 if(_callback_handler) {
edamame22 0:29983394c6b6 486 _queue_sleep_timer.stop_timer();
edamame22 0:29983394c6b6 487 _queue_sleep_timer.start_timer(MBED_CLIENT_RECONNECTION_COUNT*MBED_CLIENT_RECONNECTION_INTERVAL*1000,
edamame22 0:29983394c6b6 488 M2MTimerObserver::QueueSleep);
edamame22 0:29983394c6b6 489 }
edamame22 0:29983394c6b6 490 }
edamame22 0:29983394c6b6 491 if (_current_state == STATE_BOOTSTRAP_WAIT) {
edamame22 0:29983394c6b6 492 // For bootstrap we need to call bootstrap_done callback ONLY after we have
edamame22 0:29983394c6b6 493 // sent the last ACK and ended in STATE_BOOTSTRAP_WAIT
edamame22 0:29983394c6b6 494 M2MSecurity *sec = _security;
edamame22 0:29983394c6b6 495 _security = NULL;
edamame22 0:29983394c6b6 496 bootstrap_done(sec);
edamame22 0:29983394c6b6 497 }
edamame22 0:29983394c6b6 498 else {
edamame22 0:29983394c6b6 499 internal_event(STATE_COAP_DATA_SENT);
edamame22 0:29983394c6b6 500 }
edamame22 0:29983394c6b6 501 }
edamame22 0:29983394c6b6 502
edamame22 0:29983394c6b6 503 void M2MInterfaceImpl::timer_expired(M2MTimerObserver::Type type)
edamame22 0:29983394c6b6 504 {
edamame22 0:29983394c6b6 505 tr_debug("M2MInterfaceImpl::timer_expired()");
edamame22 0:29983394c6b6 506 if(M2MTimerObserver::QueueSleep == type) {
edamame22 0:29983394c6b6 507 if(_callback_handler) {
edamame22 0:29983394c6b6 508 _callback_handler();
edamame22 0:29983394c6b6 509 }
edamame22 0:29983394c6b6 510 }
edamame22 0:29983394c6b6 511 else if (M2MTimerObserver::RetryTimer == type) {
edamame22 0:29983394c6b6 512 _retry_timer_expired = true;
edamame22 0:29983394c6b6 513 if (_bootstrapped) {
edamame22 0:29983394c6b6 514 internal_event(STATE_REGISTER);
edamame22 0:29983394c6b6 515 } else {
edamame22 0:29983394c6b6 516 internal_event(STATE_BOOTSTRAP);
edamame22 0:29983394c6b6 517 }
edamame22 0:29983394c6b6 518 }
edamame22 0:29983394c6b6 519 else if (M2MTimerObserver::BootstrapTimer == type) {
edamame22 0:29983394c6b6 520 bootstrap_error();
edamame22 0:29983394c6b6 521 }
edamame22 0:29983394c6b6 522 }
edamame22 0:29983394c6b6 523
edamame22 0:29983394c6b6 524 // state machine sits here.
edamame22 0:29983394c6b6 525 void M2MInterfaceImpl::state_idle(EventData* /*data*/)
edamame22 0:29983394c6b6 526 {
edamame22 0:29983394c6b6 527 tr_debug("M2MInterfaceImpl::state_idle");
edamame22 0:29983394c6b6 528 _nsdl_interface.stop_timers();
edamame22 0:29983394c6b6 529 }
edamame22 0:29983394c6b6 530
edamame22 0:29983394c6b6 531 void M2MInterfaceImpl::state_bootstrap(EventData *data)
edamame22 0:29983394c6b6 532 {
edamame22 0:29983394c6b6 533 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
edamame22 0:29983394c6b6 534 tr_debug("M2MInterfaceImpl::state_bootstrap");
edamame22 0:29983394c6b6 535 // Start with bootstrapping preparation
edamame22 0:29983394c6b6 536 _bootstrapped = false;
edamame22 0:29983394c6b6 537 M2MSecurityData *event = static_cast<M2MSecurityData *> (data);
edamame22 0:29983394c6b6 538 if(!_security) {
edamame22 0:29983394c6b6 539 M2MInterface::Error error = M2MInterface::InvalidParameters;
edamame22 0:29983394c6b6 540 if (event) {
edamame22 0:29983394c6b6 541 _security = event->_object;
edamame22 0:29983394c6b6 542 if(_security) {
edamame22 0:29983394c6b6 543 if(M2MSecurity::Bootstrap == _security->server_type()) {
edamame22 0:29983394c6b6 544 tr_debug("M2MInterfaceImpl::state_bootstrap - server_type : M2MSecurity::Bootstrap");
edamame22 0:29983394c6b6 545 String server_address = _security->resource_value_string(M2MSecurity::M2MServerUri);
edamame22 0:29983394c6b6 546 tr_debug("M2MInterfaceImpl::state_bootstrap - server_address %s", server_address.c_str());
edamame22 0:29983394c6b6 547 _bootstrap_timer->start_timer(MBED_CLIENT_RECONNECTION_COUNT * MBED_CLIENT_RECONNECTION_INTERVAL * 8 * 1000,
edamame22 0:29983394c6b6 548 M2MTimerObserver::BootstrapTimer);
edamame22 0:29983394c6b6 549 String coap;
edamame22 0:29983394c6b6 550 if(server_address.compare(0,sizeof(COAP)-1,COAP) == 0) {
edamame22 0:29983394c6b6 551 coap = COAP;
edamame22 0:29983394c6b6 552 }
edamame22 0:29983394c6b6 553 else if(server_address.compare(0,sizeof(COAPS)-1,COAPS) == 0) {
edamame22 0:29983394c6b6 554 _security->resource_value_int(M2MSecurity::SecurityMode) != M2MSecurity::NoSecurity ? coap = COAPS: coap = "";
edamame22 0:29983394c6b6 555 }
edamame22 0:29983394c6b6 556 if(!coap.empty()) {
edamame22 0:29983394c6b6 557 server_address = server_address.substr(coap.size(),
edamame22 0:29983394c6b6 558 server_address.size()-coap.size());
edamame22 0:29983394c6b6 559
edamame22 0:29983394c6b6 560 process_address(server_address, _server_ip_address, _server_port);
edamame22 0:29983394c6b6 561
edamame22 0:29983394c6b6 562 tr_debug("M2MInterfaceImpl::state_bootstrap - IP address %s , Port %d", _server_ip_address.c_str(), _server_port);
edamame22 0:29983394c6b6 563 // If bind and resolving server address succeed then proceed else
edamame22 0:29983394c6b6 564 // return error to the application and go to Idle state.
edamame22 0:29983394c6b6 565 if(!_server_ip_address.empty()) {
edamame22 0:29983394c6b6 566 error = M2MInterface::ErrorNone;
edamame22 0:29983394c6b6 567 _connection_handler.resolve_server_address(_server_ip_address,
edamame22 0:29983394c6b6 568 _server_port,
edamame22 0:29983394c6b6 569 M2MConnectionObserver::Bootstrap,
edamame22 0:29983394c6b6 570 _security);
edamame22 0:29983394c6b6 571 }
edamame22 0:29983394c6b6 572 }
edamame22 0:29983394c6b6 573 }
edamame22 0:29983394c6b6 574 }
edamame22 0:29983394c6b6 575 }
edamame22 0:29983394c6b6 576 if (error != M2MInterface::ErrorNone) {
edamame22 0:29983394c6b6 577 tr_error("M2MInterfaceImpl::state_bootstrap - set error as M2MInterface::InvalidParameters");
edamame22 0:29983394c6b6 578 internal_event(STATE_IDLE);
edamame22 0:29983394c6b6 579 _observer.error(error);
edamame22 0:29983394c6b6 580 }
edamame22 0:29983394c6b6 581 } else {
edamame22 0:29983394c6b6 582 _listen_port = rand() % 64511 + 1024;
edamame22 0:29983394c6b6 583 _connection_handler.stop_listening();
edamame22 0:29983394c6b6 584 _connection_handler.bind_connection(_listen_port);
edamame22 0:29983394c6b6 585 _connection_handler.resolve_server_address(_server_ip_address,
edamame22 0:29983394c6b6 586 _server_port,
edamame22 0:29983394c6b6 587 M2MConnectionObserver::Bootstrap,
edamame22 0:29983394c6b6 588 _security);
edamame22 0:29983394c6b6 589 }
edamame22 0:29983394c6b6 590 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
edamame22 0:29983394c6b6 591 }
edamame22 0:29983394c6b6 592
edamame22 0:29983394c6b6 593 void M2MInterfaceImpl::state_bootstrap_address_resolved( EventData *data)
edamame22 0:29983394c6b6 594 {
edamame22 0:29983394c6b6 595 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
edamame22 0:29983394c6b6 596 tr_debug("M2MInterfaceImpl::state_bootstrap_address_resolved");
edamame22 0:29983394c6b6 597 if (data) {
edamame22 0:29983394c6b6 598 ResolvedAddressData *event = static_cast<ResolvedAddressData *> (data);
edamame22 0:29983394c6b6 599 sn_nsdl_addr_s address;
edamame22 0:29983394c6b6 600
edamame22 0:29983394c6b6 601 M2MInterface::NetworkStack stack = event->_address->_stack;
edamame22 0:29983394c6b6 602
edamame22 0:29983394c6b6 603 if(M2MInterface::LwIP_IPv4 == stack) {
edamame22 0:29983394c6b6 604 tr_debug("M2MInterfaceImpl::state_bootstrap_address_resolved : IPv4 address");
edamame22 0:29983394c6b6 605 address.type = SN_NSDL_ADDRESS_TYPE_IPV4;
edamame22 0:29983394c6b6 606 } else if((M2MInterface::LwIP_IPv6 == stack) ||
edamame22 0:29983394c6b6 607 (M2MInterface::Nanostack_IPv6 == stack)) {
edamame22 0:29983394c6b6 608 tr_debug("M2MInterfaceImpl::state_bootstrap_address_resolved : IPv6 address");
edamame22 0:29983394c6b6 609 address.type = SN_NSDL_ADDRESS_TYPE_IPV6;
edamame22 0:29983394c6b6 610 }
edamame22 0:29983394c6b6 611 address.port = event->_port;
edamame22 0:29983394c6b6 612 address.addr_ptr = (uint8_t*)event->_address->_address;
edamame22 0:29983394c6b6 613 address.addr_len = event->_address->_length;
edamame22 0:29983394c6b6 614 _connection_handler.start_listening_for_data();
edamame22 0:29983394c6b6 615
edamame22 0:29983394c6b6 616 // Include domain id to be part of endpoint name
edamame22 0:29983394c6b6 617 StringBuffer<M2MBase::MAX_PATH_SIZE> new_ep_name;
edamame22 0:29983394c6b6 618
edamame22 0:29983394c6b6 619 if(!new_ep_name.ensure_space(_nsdl_interface.endpoint_name().size() + 1))
edamame22 0:29983394c6b6 620 {
edamame22 0:29983394c6b6 621 tr_error("MM2MInterfaceImpl::state_bootstrap_address_resolved : name too long");
edamame22 0:29983394c6b6 622 _observer.error(M2MInterface::InvalidParameters);
edamame22 0:29983394c6b6 623 return;
edamame22 0:29983394c6b6 624 }
edamame22 0:29983394c6b6 625
edamame22 0:29983394c6b6 626 new_ep_name.append(_nsdl_interface.endpoint_name().c_str());
edamame22 0:29983394c6b6 627
edamame22 0:29983394c6b6 628 if (!_domain.empty()) {
edamame22 0:29983394c6b6 629
edamame22 0:29983394c6b6 630 if(!new_ep_name.ensure_space(_nsdl_interface.endpoint_name().size() + 1 + _domain.size() + 1))
edamame22 0:29983394c6b6 631 {
edamame22 0:29983394c6b6 632 tr_error("MM2MInterfaceImpl::state_bootstrap_address_resolved : name + domain too long");
edamame22 0:29983394c6b6 633 _observer.error(M2MInterface::InvalidParameters);
edamame22 0:29983394c6b6 634 return;
edamame22 0:29983394c6b6 635 }
edamame22 0:29983394c6b6 636
edamame22 0:29983394c6b6 637 new_ep_name.append('@');
edamame22 0:29983394c6b6 638 new_ep_name.append(_domain.c_str());
edamame22 0:29983394c6b6 639
edamame22 0:29983394c6b6 640 }
edamame22 0:29983394c6b6 641 if(_nsdl_interface.create_bootstrap_resource(&address, new_ep_name.c_str())) {
edamame22 0:29983394c6b6 642 tr_debug("M2MInterfaceImpl::state_bootstrap_address_resolved : create_bootstrap_resource - success");
edamame22 0:29983394c6b6 643 internal_event(STATE_BOOTSTRAP_RESOURCE_CREATED);
edamame22 0:29983394c6b6 644 } else{
edamame22 0:29983394c6b6 645 // If resource creation fails then inform error to application
edamame22 0:29983394c6b6 646 tr_error("M2MInterfaceImpl::state_bootstrap_address_resolved : M2MInterface::InvalidParameters");
edamame22 0:29983394c6b6 647 internal_event(STATE_IDLE);
edamame22 0:29983394c6b6 648 _observer.error(M2MInterface::InvalidParameters);
edamame22 0:29983394c6b6 649 }
edamame22 0:29983394c6b6 650 }
edamame22 0:29983394c6b6 651 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
edamame22 0:29983394c6b6 652 }
edamame22 0:29983394c6b6 653
edamame22 0:29983394c6b6 654 void M2MInterfaceImpl::state_bootstrap_resource_created( EventData */*data*/)
edamame22 0:29983394c6b6 655 {
edamame22 0:29983394c6b6 656 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
edamame22 0:29983394c6b6 657 tr_debug("M2MInterfaceImpl::state_bootstrap_resource_created");
edamame22 0:29983394c6b6 658 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
edamame22 0:29983394c6b6 659 }
edamame22 0:29983394c6b6 660
edamame22 0:29983394c6b6 661 void M2MInterfaceImpl::state_bootstrapped( EventData */*data*/)
edamame22 0:29983394c6b6 662 {
edamame22 0:29983394c6b6 663 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
edamame22 0:29983394c6b6 664 tr_debug("M2MInterfaceImpl::state_bootstrapped");
edamame22 0:29983394c6b6 665 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
edamame22 0:29983394c6b6 666 }
edamame22 0:29983394c6b6 667
edamame22 0:29983394c6b6 668 void M2MInterfaceImpl::state_register(EventData *data)
edamame22 0:29983394c6b6 669 {
edamame22 0:29983394c6b6 670 tr_debug("M2MInterfaceImpl::state_register");
edamame22 0:29983394c6b6 671 M2MRegisterData *event = static_cast<M2MRegisterData *> (data);
edamame22 0:29983394c6b6 672 if (!_security) {
edamame22 0:29983394c6b6 673 M2MInterface::Error error = M2MInterface::InvalidParameters;
edamame22 0:29983394c6b6 674 // Start with registration preparation
edamame22 0:29983394c6b6 675 if(event) {
edamame22 0:29983394c6b6 676 _security = event->_object;
edamame22 0:29983394c6b6 677 if(_security) {
edamame22 0:29983394c6b6 678 if(M2MSecurity::M2MServer == _security->server_type()) {
edamame22 0:29983394c6b6 679 tr_debug("M2MInterfaceImpl::state_register - server_type : M2MSecurity::M2MServer");
edamame22 0:29983394c6b6 680 if(_nsdl_interface.create_nsdl_list_structure(event->_object_list)) {
edamame22 0:29983394c6b6 681 tr_debug("M2MInterfaceImpl::state_register - create_nsdl_list_structure - success");
edamame22 0:29983394c6b6 682 // If the nsdl resource structure is created successfully
edamame22 0:29983394c6b6 683 String server_address = _security->resource_value_string(M2MSecurity::M2MServerUri);
edamame22 0:29983394c6b6 684 tr_debug("M2MInterfaceImpl::state_register - server_address %s", server_address.c_str());
edamame22 0:29983394c6b6 685 String coap;
edamame22 0:29983394c6b6 686 if(server_address.compare(0,sizeof(COAP)-1,COAP) == 0) {
edamame22 0:29983394c6b6 687 coap = COAP;
edamame22 0:29983394c6b6 688 }
edamame22 0:29983394c6b6 689 else if(server_address.compare(0,sizeof(COAPS)-1,COAPS) == 0) {
edamame22 0:29983394c6b6 690 _security->resource_value_int(M2MSecurity::SecurityMode) != M2MSecurity::NoSecurity ? coap = COAPS: coap = "";
edamame22 0:29983394c6b6 691 }
edamame22 0:29983394c6b6 692 if(!coap.empty()) {
edamame22 0:29983394c6b6 693 server_address = server_address.substr(coap.size(),
edamame22 0:29983394c6b6 694 server_address.size() - coap.size());
edamame22 0:29983394c6b6 695 process_address(server_address, _server_ip_address, _server_port);
edamame22 0:29983394c6b6 696
edamame22 0:29983394c6b6 697 tr_debug("M2MInterfaceImpl::state_register - IP address %s , Port %d", _server_ip_address.c_str(), _server_port);
edamame22 0:29983394c6b6 698 if(!_server_ip_address.empty()) {
edamame22 0:29983394c6b6 699 // Connection related errors are coming through callback
edamame22 0:29983394c6b6 700 error = M2MInterface::ErrorNone;
edamame22 0:29983394c6b6 701 _connection_handler.resolve_server_address(_server_ip_address,_server_port,
edamame22 0:29983394c6b6 702 M2MConnectionObserver::LWM2MServer,
edamame22 0:29983394c6b6 703 _security);
edamame22 0:29983394c6b6 704 }
edamame22 0:29983394c6b6 705 }
edamame22 0:29983394c6b6 706 }
edamame22 0:29983394c6b6 707 }
edamame22 0:29983394c6b6 708 }
edamame22 0:29983394c6b6 709 }
edamame22 0:29983394c6b6 710 if (error != M2MInterface::ErrorNone) {
edamame22 0:29983394c6b6 711 tr_error("M2MInterfaceImpl::state_register - set error as M2MInterface::InvalidParameters");
edamame22 0:29983394c6b6 712 internal_event(STATE_IDLE);
edamame22 0:29983394c6b6 713 _observer.error(error);
edamame22 0:29983394c6b6 714 }
edamame22 0:29983394c6b6 715 } else {
edamame22 0:29983394c6b6 716 _listen_port = rand() % 64511 + 1024;
edamame22 0:29983394c6b6 717 _connection_handler.stop_listening();
edamame22 0:29983394c6b6 718 if (event) {
edamame22 0:29983394c6b6 719 _nsdl_interface.create_nsdl_list_structure(event->_object_list);
edamame22 0:29983394c6b6 720 }
edamame22 0:29983394c6b6 721 _connection_handler.bind_connection(_listen_port);
edamame22 0:29983394c6b6 722 _connection_handler.resolve_server_address(_server_ip_address,_server_port,
edamame22 0:29983394c6b6 723 M2MConnectionObserver::LWM2MServer,
edamame22 0:29983394c6b6 724 _security);
edamame22 0:29983394c6b6 725 }
edamame22 0:29983394c6b6 726 }
edamame22 0:29983394c6b6 727
edamame22 0:29983394c6b6 728 void M2MInterfaceImpl::process_address(const String& server_address, String& ip_address, uint16_t& port) {
edamame22 0:29983394c6b6 729
edamame22 0:29983394c6b6 730 int colonFound = server_address.find_last_of(':'); //10
edamame22 0:29983394c6b6 731 if(colonFound != -1) {
edamame22 0:29983394c6b6 732 ip_address = server_address.substr(0,colonFound);
edamame22 0:29983394c6b6 733 port = atoi(server_address.substr(colonFound+1,
edamame22 0:29983394c6b6 734 server_address.size()-ip_address.size()).c_str());
edamame22 0:29983394c6b6 735 colonFound = ip_address.find_last_of(']');
edamame22 0:29983394c6b6 736 if(ip_address.compare(0,1,"[") == 0) {
edamame22 0:29983394c6b6 737 if(colonFound == -1) {
edamame22 0:29983394c6b6 738 ip_address.clear();
edamame22 0:29983394c6b6 739 } else {
edamame22 0:29983394c6b6 740 ip_address = ip_address.substr(1,colonFound-1);
edamame22 0:29983394c6b6 741 }
edamame22 0:29983394c6b6 742 } else if(colonFound != -1) {
edamame22 0:29983394c6b6 743 ip_address.clear();
edamame22 0:29983394c6b6 744 }
edamame22 0:29983394c6b6 745 }
edamame22 0:29983394c6b6 746 }
edamame22 0:29983394c6b6 747
edamame22 0:29983394c6b6 748 void M2MInterfaceImpl::state_register_address_resolved( EventData *data)
edamame22 0:29983394c6b6 749 {
edamame22 0:29983394c6b6 750 tr_debug("M2MInterfaceImpl::state_register_address_resolved");
edamame22 0:29983394c6b6 751 if(data) {
edamame22 0:29983394c6b6 752 ResolvedAddressData *event = static_cast<ResolvedAddressData *> (data);
edamame22 0:29983394c6b6 753
edamame22 0:29983394c6b6 754 sn_nsdl_addr_type_e address_type = SN_NSDL_ADDRESS_TYPE_IPV6;
edamame22 0:29983394c6b6 755
edamame22 0:29983394c6b6 756 M2MInterface::NetworkStack stack = event->_address->_stack;
edamame22 0:29983394c6b6 757
edamame22 0:29983394c6b6 758 if(M2MInterface::LwIP_IPv4 == stack) {
edamame22 0:29983394c6b6 759 tr_debug("M2MInterfaceImpl::state_register_address_resolved : IPv4 address");
edamame22 0:29983394c6b6 760 address_type = SN_NSDL_ADDRESS_TYPE_IPV4;
edamame22 0:29983394c6b6 761 } else if((M2MInterface::LwIP_IPv6 == stack) ||
edamame22 0:29983394c6b6 762 (M2MInterface::Nanostack_IPv6 == stack)) {
edamame22 0:29983394c6b6 763 tr_debug("M2MInterfaceImpl::state_register_address_resolved : IPv6 address");
edamame22 0:29983394c6b6 764 address_type = SN_NSDL_ADDRESS_TYPE_IPV6;
edamame22 0:29983394c6b6 765 }
edamame22 0:29983394c6b6 766 _connection_handler.start_listening_for_data();
edamame22 0:29983394c6b6 767 if(!_nsdl_interface.send_register_message((uint8_t*)event->_address->_address,event->_address->_length,
edamame22 0:29983394c6b6 768 event->_port, address_type)) {
edamame22 0:29983394c6b6 769 // If resource creation fails then inform error to application
edamame22 0:29983394c6b6 770 tr_error("M2MInterfaceImpl::state_register_address_resolved : M2MInterface::InvalidParameters");
edamame22 0:29983394c6b6 771 internal_event(STATE_IDLE);
edamame22 0:29983394c6b6 772 _observer.error(M2MInterface::InvalidParameters);
edamame22 0:29983394c6b6 773 }
edamame22 0:29983394c6b6 774 }
edamame22 0:29983394c6b6 775 }
edamame22 0:29983394c6b6 776
edamame22 0:29983394c6b6 777 void M2MInterfaceImpl::state_registered( EventData */*data*/)
edamame22 0:29983394c6b6 778 {
edamame22 0:29983394c6b6 779 tr_debug("M2MInterfaceImpl::state_registered");
edamame22 0:29983394c6b6 780 _retry_count = 0;
edamame22 0:29983394c6b6 781 _reconnecting = false;
edamame22 0:29983394c6b6 782 }
edamame22 0:29983394c6b6 783
edamame22 0:29983394c6b6 784 void M2MInterfaceImpl::state_update_registration(EventData *data)
edamame22 0:29983394c6b6 785 {
edamame22 0:29983394c6b6 786 tr_debug("M2MInterfaceImpl::state_update_registration");
edamame22 0:29983394c6b6 787 if(data) {
edamame22 0:29983394c6b6 788 M2MUpdateRegisterData *event = static_cast<M2MUpdateRegisterData *> (data);
edamame22 0:29983394c6b6 789 // Create new resources if any
edamame22 0:29983394c6b6 790 if (!event->_object_list.empty()) {
edamame22 0:29983394c6b6 791 _nsdl_interface.create_nsdl_list_structure(event->_object_list);
edamame22 0:29983394c6b6 792 }
edamame22 0:29983394c6b6 793 _nsdl_interface.send_update_registration(event->_lifetime);
edamame22 0:29983394c6b6 794 }
edamame22 0:29983394c6b6 795 }
edamame22 0:29983394c6b6 796
edamame22 0:29983394c6b6 797 void M2MInterfaceImpl::state_unregister( EventData */*data*/)
edamame22 0:29983394c6b6 798 {
edamame22 0:29983394c6b6 799 tr_debug("M2MInterfaceImpl::state_unregister");
edamame22 0:29983394c6b6 800 internal_event(STATE_SENDING_COAP_DATA);
edamame22 0:29983394c6b6 801 if(!_nsdl_interface.send_unregister_message()) {
edamame22 0:29983394c6b6 802 tr_error("M2MInterfaceImpl::state_unregister : M2MInterface::NotRegistered");
edamame22 0:29983394c6b6 803 internal_event(STATE_IDLE);
edamame22 0:29983394c6b6 804 _observer.error(M2MInterface::NotRegistered);
edamame22 0:29983394c6b6 805 }
edamame22 0:29983394c6b6 806 }
edamame22 0:29983394c6b6 807
edamame22 0:29983394c6b6 808 void M2MInterfaceImpl::state_unregistered( EventData */*data*/)
edamame22 0:29983394c6b6 809 {
edamame22 0:29983394c6b6 810 tr_debug("M2MInterfaceImpl::state_unregistered");
edamame22 0:29983394c6b6 811 internal_event(STATE_IDLE);
edamame22 0:29983394c6b6 812 }
edamame22 0:29983394c6b6 813
edamame22 0:29983394c6b6 814 void M2MInterfaceImpl::state_sending_coap_data( EventData */*data*/)
edamame22 0:29983394c6b6 815 {
edamame22 0:29983394c6b6 816 tr_debug("M2MInterfaceImpl::state_sending_coap_data");
edamame22 0:29983394c6b6 817 internal_event(STATE_WAITING);
edamame22 0:29983394c6b6 818 }
edamame22 0:29983394c6b6 819
edamame22 0:29983394c6b6 820 void M2MInterfaceImpl::state_coap_data_sent( EventData */*data*/)
edamame22 0:29983394c6b6 821 {
edamame22 0:29983394c6b6 822 tr_debug("M2MInterfaceImpl::state_coap_data_sent");
edamame22 0:29983394c6b6 823 internal_event(STATE_WAITING);
edamame22 0:29983394c6b6 824 }
edamame22 0:29983394c6b6 825
edamame22 0:29983394c6b6 826 void M2MInterfaceImpl::state_coap_data_received( EventData *data)
edamame22 0:29983394c6b6 827 {
edamame22 0:29983394c6b6 828 tr_debug("M2MInterfaceImpl::state_coap_data_received");
edamame22 0:29983394c6b6 829 if(data) {
edamame22 0:29983394c6b6 830 ReceivedData *event = static_cast<ReceivedData*> (data);
edamame22 0:29983394c6b6 831 sn_nsdl_addr_s address;
edamame22 0:29983394c6b6 832
edamame22 0:29983394c6b6 833 M2MInterface::NetworkStack stack = event->_address->_stack;
edamame22 0:29983394c6b6 834
edamame22 0:29983394c6b6 835 if(M2MInterface::LwIP_IPv4 == stack) {
edamame22 0:29983394c6b6 836 tr_debug("M2MInterfaceImpl::state_coap_data_received : IPv4 address");
edamame22 0:29983394c6b6 837 address.type = SN_NSDL_ADDRESS_TYPE_IPV4;
edamame22 0:29983394c6b6 838 address.addr_len = 4;
edamame22 0:29983394c6b6 839 } else if((M2MInterface::LwIP_IPv6 == stack) ||
edamame22 0:29983394c6b6 840 (M2MInterface::Nanostack_IPv6 == stack)) {
edamame22 0:29983394c6b6 841 tr_debug("M2MInterfaceImpl::state_coap_data_received : IPv6 address");
edamame22 0:29983394c6b6 842 address.type = SN_NSDL_ADDRESS_TYPE_IPV6;
edamame22 0:29983394c6b6 843 address.addr_len = 16;
edamame22 0:29983394c6b6 844 }
edamame22 0:29983394c6b6 845 address.port = event->_address->_port;
edamame22 0:29983394c6b6 846 address.addr_ptr = (uint8_t*)event->_address->_address;
edamame22 0:29983394c6b6 847 address.addr_len = event->_address->_length;
edamame22 0:29983394c6b6 848
edamame22 0:29983394c6b6 849 // Process received data
edamame22 0:29983394c6b6 850 internal_event(STATE_PROCESSING_COAP_DATA);
edamame22 0:29983394c6b6 851 if(!_nsdl_interface.process_received_data(event->_data,
edamame22 0:29983394c6b6 852 event->_size,
edamame22 0:29983394c6b6 853 &address)) {
edamame22 0:29983394c6b6 854 tr_error("M2MInterfaceImpl::state_coap_data_received : M2MInterface::ResponseParseFailed");
edamame22 0:29983394c6b6 855 _observer.error(M2MInterface::ResponseParseFailed);
edamame22 0:29983394c6b6 856 }
edamame22 0:29983394c6b6 857 }
edamame22 0:29983394c6b6 858 }
edamame22 0:29983394c6b6 859
edamame22 0:29983394c6b6 860 void M2MInterfaceImpl::state_processing_coap_data( EventData */*data*/)
edamame22 0:29983394c6b6 861 {
edamame22 0:29983394c6b6 862 tr_debug("M2MInterfaceImpl::state_processing_coap_data");
edamame22 0:29983394c6b6 863 internal_event(STATE_WAITING);
edamame22 0:29983394c6b6 864 }
edamame22 0:29983394c6b6 865
edamame22 0:29983394c6b6 866 void M2MInterfaceImpl::state_coap_data_processed( EventData */*data*/)
edamame22 0:29983394c6b6 867 {
edamame22 0:29983394c6b6 868 tr_debug("M2MInterfaceImpl::state_coap_data_processed");
edamame22 0:29983394c6b6 869 internal_event(STATE_WAITING);
edamame22 0:29983394c6b6 870 }
edamame22 0:29983394c6b6 871
edamame22 0:29983394c6b6 872 void M2MInterfaceImpl::state_waiting( EventData */*data*/)
edamame22 0:29983394c6b6 873 {
edamame22 0:29983394c6b6 874 tr_debug("M2MInterfaceImpl::state_waiting");
edamame22 0:29983394c6b6 875 }
edamame22 0:29983394c6b6 876
edamame22 0:29983394c6b6 877 // generates an external event. called once per external event
edamame22 0:29983394c6b6 878 // to start the state machine executing
edamame22 0:29983394c6b6 879 void M2MInterfaceImpl::external_event(uint8_t new_state,
edamame22 0:29983394c6b6 880 EventData* p_data)
edamame22 0:29983394c6b6 881 {
edamame22 0:29983394c6b6 882 tr_debug("M2MInterfaceImpl::external_event : new state %d", new_state);
edamame22 0:29983394c6b6 883 // if we are supposed to ignore this event
edamame22 0:29983394c6b6 884 if (new_state == EVENT_IGNORED) {
edamame22 0:29983394c6b6 885 tr_debug("M2MInterfaceImpl::external_event : new state is EVENT_IGNORED");
edamame22 0:29983394c6b6 886 // just delete the event data, if any
edamame22 0:29983394c6b6 887 if (p_data) {
edamame22 0:29983394c6b6 888 delete p_data;
edamame22 0:29983394c6b6 889 p_data = NULL;
edamame22 0:29983394c6b6 890 }
edamame22 0:29983394c6b6 891 _event_ignored = true;
edamame22 0:29983394c6b6 892 }
edamame22 0:29983394c6b6 893 else {
edamame22 0:29983394c6b6 894 tr_debug("M2MInterfaceImpl::external_event : handle new state");
edamame22 0:29983394c6b6 895 // generate the event and execute the state engine
edamame22 0:29983394c6b6 896 internal_event(new_state, p_data);
edamame22 0:29983394c6b6 897 }
edamame22 0:29983394c6b6 898 }
edamame22 0:29983394c6b6 899
edamame22 0:29983394c6b6 900 // generates an internal event. called from within a state
edamame22 0:29983394c6b6 901 // function to transition to a new state
edamame22 0:29983394c6b6 902 void M2MInterfaceImpl::internal_event(uint8_t new_state,
edamame22 0:29983394c6b6 903 EventData* p_data)
edamame22 0:29983394c6b6 904 {
edamame22 0:29983394c6b6 905 tr_debug("M2MInterfaceImpl::internal_event : new state %d", new_state);
edamame22 0:29983394c6b6 906 _event_data = p_data;
edamame22 0:29983394c6b6 907 _event_generated = true;
edamame22 0:29983394c6b6 908 _current_state = new_state;
edamame22 0:29983394c6b6 909 state_engine();
edamame22 0:29983394c6b6 910 }
edamame22 0:29983394c6b6 911
edamame22 0:29983394c6b6 912 // the state engine executes the state machine states
edamame22 0:29983394c6b6 913 void M2MInterfaceImpl::state_engine (void)
edamame22 0:29983394c6b6 914 {
edamame22 0:29983394c6b6 915 tr_debug("M2MInterfaceImpl::state_engine");
edamame22 0:29983394c6b6 916 EventData* p_data_temp = NULL;
edamame22 0:29983394c6b6 917
edamame22 0:29983394c6b6 918 // while events are being generated keep executing states
edamame22 0:29983394c6b6 919 while (_event_generated) {
edamame22 0:29983394c6b6 920 p_data_temp = _event_data; // copy of event data pointer
edamame22 0:29983394c6b6 921 _event_data = NULL; // event data used up, reset ptr
edamame22 0:29983394c6b6 922 _event_generated = false; // event used up, reset flag
edamame22 0:29983394c6b6 923
edamame22 0:29983394c6b6 924 assert(_current_state < _max_states);
edamame22 0:29983394c6b6 925
edamame22 0:29983394c6b6 926 state_function( _current_state, p_data_temp );
edamame22 0:29983394c6b6 927
edamame22 0:29983394c6b6 928 // if event data was used, then delete it
edamame22 0:29983394c6b6 929 if (p_data_temp) {
edamame22 0:29983394c6b6 930 delete p_data_temp;
edamame22 0:29983394c6b6 931 p_data_temp = NULL;
edamame22 0:29983394c6b6 932 }
edamame22 0:29983394c6b6 933 }
edamame22 0:29983394c6b6 934 }
edamame22 0:29983394c6b6 935
edamame22 0:29983394c6b6 936 void M2MInterfaceImpl::state_function( uint8_t current_state, EventData* data )
edamame22 0:29983394c6b6 937 {
edamame22 0:29983394c6b6 938 switch( current_state ) {
edamame22 0:29983394c6b6 939 case STATE_IDLE:
edamame22 0:29983394c6b6 940 M2MInterfaceImpl::state_idle(data);
edamame22 0:29983394c6b6 941 break;
edamame22 0:29983394c6b6 942 case STATE_BOOTSTRAP:
edamame22 0:29983394c6b6 943 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
edamame22 0:29983394c6b6 944 M2MInterfaceImpl::state_bootstrap(data);
edamame22 0:29983394c6b6 945 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
edamame22 0:29983394c6b6 946 break;
edamame22 0:29983394c6b6 947 case STATE_BOOTSTRAP_ADDRESS_RESOLVED:
edamame22 0:29983394c6b6 948 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
edamame22 0:29983394c6b6 949 M2MInterfaceImpl::state_bootstrap_address_resolved(data);
edamame22 0:29983394c6b6 950 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
edamame22 0:29983394c6b6 951 break;
edamame22 0:29983394c6b6 952 case STATE_BOOTSTRAP_RESOURCE_CREATED:
edamame22 0:29983394c6b6 953 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
edamame22 0:29983394c6b6 954 M2MInterfaceImpl::state_bootstrap_resource_created(data);
edamame22 0:29983394c6b6 955 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
edamame22 0:29983394c6b6 956 break;
edamame22 0:29983394c6b6 957 case STATE_BOOTSTRAP_WAIT:
edamame22 0:29983394c6b6 958 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
edamame22 0:29983394c6b6 959 // Do nothing, we're just waiting for data_sent callback
edamame22 0:29983394c6b6 960 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
edamame22 0:29983394c6b6 961 break;
edamame22 0:29983394c6b6 962 case STATE_BOOTSTRAPPED:
edamame22 0:29983394c6b6 963 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
edamame22 0:29983394c6b6 964 M2MInterfaceImpl::state_bootstrapped(data);
edamame22 0:29983394c6b6 965 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
edamame22 0:29983394c6b6 966 break;
edamame22 0:29983394c6b6 967 case STATE_REGISTER:
edamame22 0:29983394c6b6 968 M2MInterfaceImpl::state_register(data);
edamame22 0:29983394c6b6 969 break;
edamame22 0:29983394c6b6 970 case STATE_REGISTER_ADDRESS_RESOLVED:
edamame22 0:29983394c6b6 971 M2MInterfaceImpl::state_register_address_resolved(data);
edamame22 0:29983394c6b6 972 break;
edamame22 0:29983394c6b6 973 case STATE_REGISTERED:
edamame22 0:29983394c6b6 974 M2MInterfaceImpl::state_registered(data);
edamame22 0:29983394c6b6 975 break;
edamame22 0:29983394c6b6 976 case STATE_UPDATE_REGISTRATION:
edamame22 0:29983394c6b6 977 M2MInterfaceImpl::state_update_registration(data);
edamame22 0:29983394c6b6 978 break;
edamame22 0:29983394c6b6 979 case STATE_UNREGISTER:
edamame22 0:29983394c6b6 980 M2MInterfaceImpl::state_unregister(data);
edamame22 0:29983394c6b6 981 break;
edamame22 0:29983394c6b6 982 case STATE_UNREGISTERED:
edamame22 0:29983394c6b6 983 M2MInterfaceImpl::state_unregistered(data);
edamame22 0:29983394c6b6 984 break;
edamame22 0:29983394c6b6 985 case STATE_SENDING_COAP_DATA:
edamame22 0:29983394c6b6 986 M2MInterfaceImpl::state_sending_coap_data(data);
edamame22 0:29983394c6b6 987 break;
edamame22 0:29983394c6b6 988 case STATE_COAP_DATA_SENT:
edamame22 0:29983394c6b6 989 M2MInterfaceImpl::state_coap_data_sent(data);
edamame22 0:29983394c6b6 990 break;
edamame22 0:29983394c6b6 991 case STATE_COAP_DATA_RECEIVED:
edamame22 0:29983394c6b6 992 M2MInterfaceImpl::state_coap_data_received(data);
edamame22 0:29983394c6b6 993 break;
edamame22 0:29983394c6b6 994 case STATE_PROCESSING_COAP_DATA:
edamame22 0:29983394c6b6 995 M2MInterfaceImpl::state_processing_coap_data(data);
edamame22 0:29983394c6b6 996 break;
edamame22 0:29983394c6b6 997 case STATE_COAP_DATA_PROCESSED:
edamame22 0:29983394c6b6 998 M2MInterfaceImpl::state_coap_data_processed(data);
edamame22 0:29983394c6b6 999 break;
edamame22 0:29983394c6b6 1000 case STATE_WAITING:
edamame22 0:29983394c6b6 1001 M2MInterfaceImpl::state_waiting(data);
edamame22 0:29983394c6b6 1002 break;
edamame22 0:29983394c6b6 1003 }
edamame22 0:29983394c6b6 1004 }
edamame22 0:29983394c6b6 1005
edamame22 0:29983394c6b6 1006 void M2MInterfaceImpl::start_register_update(M2MUpdateRegisterData *data) {
edamame22 0:29983394c6b6 1007 tr_debug("M2MInterfaceImpl::start_register_update - IN");
edamame22 0:29983394c6b6 1008 if(!data || (data->_lifetime != 0 && (data->_lifetime < MINIMUM_REGISTRATION_TIME))) {
edamame22 0:29983394c6b6 1009 _observer.error(M2MInterface::InvalidParameters);
edamame22 0:29983394c6b6 1010 }
edamame22 0:29983394c6b6 1011 BEGIN_TRANSITION_MAP // - Current State -
edamame22 0:29983394c6b6 1012 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_idle
edamame22 0:29983394c6b6 1013 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap
edamame22 0:29983394c6b6 1014 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state__bootstrap_address_resolved
edamame22 0:29983394c6b6 1015 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap_resource_created
edamame22 0:29983394c6b6 1016 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap_wait
edamame22 0:29983394c6b6 1017 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrapped
edamame22 0:29983394c6b6 1018 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_register
edamame22 0:29983394c6b6 1019 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_register_address_resolved
edamame22 0:29983394c6b6 1020 TRANSITION_MAP_ENTRY (STATE_UPDATE_REGISTRATION) // state_registered
edamame22 0:29983394c6b6 1021 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_update_registration
edamame22 0:29983394c6b6 1022 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_unregister
edamame22 0:29983394c6b6 1023 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_unregistered
edamame22 0:29983394c6b6 1024 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_sending_coap_data
edamame22 0:29983394c6b6 1025 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_sent
edamame22 0:29983394c6b6 1026 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_received
edamame22 0:29983394c6b6 1027 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_processing_coap_data
edamame22 0:29983394c6b6 1028 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_processed
edamame22 0:29983394c6b6 1029 TRANSITION_MAP_ENTRY (STATE_UPDATE_REGISTRATION) // state_waiting
edamame22 0:29983394c6b6 1030 END_TRANSITION_MAP(data)
edamame22 0:29983394c6b6 1031 if(_event_ignored) {
edamame22 0:29983394c6b6 1032 _event_ignored = false;
edamame22 0:29983394c6b6 1033 if (!_reconnecting)
edamame22 0:29983394c6b6 1034 _observer.error(M2MInterface::NotAllowed);
edamame22 0:29983394c6b6 1035 }
edamame22 0:29983394c6b6 1036 }