FRDM K64F Metronome

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

Who changed what in which revision?

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