A metronome using the FRDM K64F board

Committer:
ram54288
Date:
Sun May 14 18:40:18 2017 +0000
Revision:
0:a7a43371b306
Initial commit

Who changed what in which revision?

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