FRDM K64F Metronome

Committer:
ram54288
Date:
Sun May 14 18:37:05 2017 +0000
Revision:
0:dbad57390bd1
Initial commit

Who changed what in which revision?

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