Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

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