leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leothedragon 0:8f0bb79ddd48 1 /*
leothedragon 0:8f0bb79ddd48 2 * Copyright (c) 2015 ARM Limited. All rights reserved.
leothedragon 0:8f0bb79ddd48 3 * SPDX-License-Identifier: Apache-2.0
leothedragon 0:8f0bb79ddd48 4 * Licensed under the Apache License, Version 2.0 (the License); you may
leothedragon 0:8f0bb79ddd48 5 * not use this file except in compliance with the License.
leothedragon 0:8f0bb79ddd48 6 * You may obtain a copy of the License at
leothedragon 0:8f0bb79ddd48 7 *
leothedragon 0:8f0bb79ddd48 8 * http://www.apache.org/licenses/LICENSE-2.0
leothedragon 0:8f0bb79ddd48 9 *
leothedragon 0:8f0bb79ddd48 10 * Unless required by applicable law or agreed to in writing, software
leothedragon 0:8f0bb79ddd48 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
leothedragon 0:8f0bb79ddd48 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
leothedragon 0:8f0bb79ddd48 13 * See the License for the specific language governing permissions and
leothedragon 0:8f0bb79ddd48 14 * limitations under the License.
leothedragon 0:8f0bb79ddd48 15 */
leothedragon 0:8f0bb79ddd48 16
leothedragon 0:8f0bb79ddd48 17 // Needed for PRIu64 on FreeRTOS
leothedragon 0:8f0bb79ddd48 18 #include <stdio.h>
leothedragon 0:8f0bb79ddd48 19 // Note: this macro is needed on armcc to get the the PRI*32 macros
leothedragon 0:8f0bb79ddd48 20 // from inttypes.h in a C++ code.
leothedragon 0:8f0bb79ddd48 21 #ifndef __STDC_FORMAT_MACROS
leothedragon 0:8f0bb79ddd48 22 #define __STDC_FORMAT_MACROS
leothedragon 0:8f0bb79ddd48 23 #endif
leothedragon 0:8f0bb79ddd48 24 #include <inttypes.h>
leothedragon 0:8f0bb79ddd48 25
leothedragon 0:8f0bb79ddd48 26 #include <assert.h>
leothedragon 0:8f0bb79ddd48 27 #include <stdlib.h>
leothedragon 0:8f0bb79ddd48 28 #include <stdio.h>
leothedragon 0:8f0bb79ddd48 29 #include <time.h>
leothedragon 0:8f0bb79ddd48 30 #include "include/m2minterfaceimpl.h"
leothedragon 0:8f0bb79ddd48 31 #include "include/eventdata.h"
leothedragon 0:8f0bb79ddd48 32 #include "mbed-client/m2minterfaceobserver.h"
leothedragon 0:8f0bb79ddd48 33 #include "mbed-client/m2mconnectionhandler.h"
leothedragon 0:8f0bb79ddd48 34 #include "mbed-client/m2mconnectionsecurity.h"
leothedragon 0:8f0bb79ddd48 35 #include "include/m2mnsdlinterface.h"
leothedragon 0:8f0bb79ddd48 36 #include "mbed-client/m2msecurity.h"
leothedragon 0:8f0bb79ddd48 37 #include "mbed-client/m2mtimer.h"
leothedragon 0:8f0bb79ddd48 38 #include "mbed-trace/mbed_trace.h"
leothedragon 0:8f0bb79ddd48 39 #include "randLIB.h"
leothedragon 0:8f0bb79ddd48 40
leothedragon 0:8f0bb79ddd48 41 #include <stdlib.h>
leothedragon 0:8f0bb79ddd48 42
leothedragon 0:8f0bb79ddd48 43 #define TRACE_GROUP "mClt"
leothedragon 0:8f0bb79ddd48 44
leothedragon 0:8f0bb79ddd48 45 #define RESOLVE_SEC_MODE(mode) ((mode == M2MInterface::TCP || mode == M2MInterface::TCP_QUEUE) ? M2MConnectionSecurity::TLS : M2MConnectionSecurity::DTLS)
leothedragon 0:8f0bb79ddd48 46
leothedragon 0:8f0bb79ddd48 47 M2MInterfaceImpl::M2MInterfaceImpl(M2MInterfaceObserver& observer,
leothedragon 0:8f0bb79ddd48 48 const String &ep_name,
leothedragon 0:8f0bb79ddd48 49 const String &ep_type,
leothedragon 0:8f0bb79ddd48 50 const int32_t l_time,
leothedragon 0:8f0bb79ddd48 51 const uint16_t listen_port,
leothedragon 0:8f0bb79ddd48 52 const String &dmn,
leothedragon 0:8f0bb79ddd48 53 M2MInterface::BindingMode mode,
leothedragon 0:8f0bb79ddd48 54 M2MInterface::NetworkStack stack,
leothedragon 0:8f0bb79ddd48 55 const String &con_addr)
leothedragon 0:8f0bb79ddd48 56 : _event_data(NULL),
leothedragon 0:8f0bb79ddd48 57 _registration_flow_timer(NULL),
leothedragon 0:8f0bb79ddd48 58 _server_port(0),
leothedragon 0:8f0bb79ddd48 59 _listen_port(listen_port),
leothedragon 0:8f0bb79ddd48 60 _life_time(l_time),
leothedragon 0:8f0bb79ddd48 61 _register_server(NULL),
leothedragon 0:8f0bb79ddd48 62 _queue_sleep_timer(*this),
leothedragon 0:8f0bb79ddd48 63 _retry_timer(*this),
leothedragon 0:8f0bb79ddd48 64 _callback_handler(NULL),
leothedragon 0:8f0bb79ddd48 65 _max_states( STATE_MAX_STATES ),
leothedragon 0:8f0bb79ddd48 66 _event_ignored(false),
leothedragon 0:8f0bb79ddd48 67 _event_generated(false),
leothedragon 0:8f0bb79ddd48 68 _reconnecting(false),
leothedragon 0:8f0bb79ddd48 69 _retry_timer_expired(false),
leothedragon 0:8f0bb79ddd48 70 _bootstrapped(true), // True as default to get it working with connector only configuration
leothedragon 0:8f0bb79ddd48 71 _bootstrap_finished(false),
leothedragon 0:8f0bb79ddd48 72 _queue_mode_timer_ongoing(false),
leothedragon 0:8f0bb79ddd48 73 _current_state(0),
leothedragon 0:8f0bb79ddd48 74 _binding_mode(mode),
leothedragon 0:8f0bb79ddd48 75 _reconnection_state(M2MInterfaceImpl::None),
leothedragon 0:8f0bb79ddd48 76 _observer(observer),
leothedragon 0:8f0bb79ddd48 77 _security_connection( new M2MConnectionSecurity( RESOLVE_SEC_MODE(mode) )),
leothedragon 0:8f0bb79ddd48 78 _connection_handler(*this, _security_connection, mode, stack),
leothedragon 0:8f0bb79ddd48 79 _nsdl_interface(*this, _connection_handler),
leothedragon 0:8f0bb79ddd48 80 _security(NULL)
leothedragon 0:8f0bb79ddd48 81 {
leothedragon 0:8f0bb79ddd48 82 tr_debug("M2MInterfaceImpl::M2MInterfaceImpl() -IN");
leothedragon 0:8f0bb79ddd48 83 //TODO:Increase the range from 1 to 100 seconds
leothedragon 0:8f0bb79ddd48 84 randLIB_seed_random();
leothedragon 0:8f0bb79ddd48 85 // Range is from 2 to 10
leothedragon 0:8f0bb79ddd48 86 _initial_reconnection_time = randLIB_get_random_in_range(2, 10);
leothedragon 0:8f0bb79ddd48 87
leothedragon 0:8f0bb79ddd48 88 tr_info("M2MInterfaceImpl::M2MInterfaceImpl() initial random time %d\n", _initial_reconnection_time);
leothedragon 0:8f0bb79ddd48 89 _reconnection_time = _initial_reconnection_time;
leothedragon 0:8f0bb79ddd48 90
leothedragon 0:8f0bb79ddd48 91 #ifndef DISABLE_ERROR_DESCRIPTION
leothedragon 0:8f0bb79ddd48 92 memset(_error_description, 0, sizeof(_error_description));
leothedragon 0:8f0bb79ddd48 93 #endif
leothedragon 0:8f0bb79ddd48 94
leothedragon 0:8f0bb79ddd48 95 _nsdl_interface.create_endpoint(ep_name,
leothedragon 0:8f0bb79ddd48 96 ep_type,
leothedragon 0:8f0bb79ddd48 97 _life_time,
leothedragon 0:8f0bb79ddd48 98 dmn,
leothedragon 0:8f0bb79ddd48 99 (uint8_t)_binding_mode,
leothedragon 0:8f0bb79ddd48 100 con_addr);
leothedragon 0:8f0bb79ddd48 101
leothedragon 0:8f0bb79ddd48 102 //Here we must use TCP still
leothedragon 0:8f0bb79ddd48 103 _connection_handler.bind_connection(_listen_port);
leothedragon 0:8f0bb79ddd48 104
leothedragon 0:8f0bb79ddd48 105 // We need this timer only in case of TCP
leothedragon 0:8f0bb79ddd48 106 if (_binding_mode == M2MInterface::TCP ||
leothedragon 0:8f0bb79ddd48 107 _binding_mode == M2MInterface::TCP_QUEUE ) {
leothedragon 0:8f0bb79ddd48 108 _registration_flow_timer = new M2MTimer(*this);
leothedragon 0:8f0bb79ddd48 109 }
leothedragon 0:8f0bb79ddd48 110 tr_debug("M2MInterfaceImpl::M2MInterfaceImpl() -OUT");
leothedragon 0:8f0bb79ddd48 111 }
leothedragon 0:8f0bb79ddd48 112
leothedragon 0:8f0bb79ddd48 113
leothedragon 0:8f0bb79ddd48 114 M2MInterfaceImpl::~M2MInterfaceImpl()
leothedragon 0:8f0bb79ddd48 115 {
leothedragon 0:8f0bb79ddd48 116 tr_debug("M2MInterfaceImpl::~M2MInterfaceImpl() - IN");
leothedragon 0:8f0bb79ddd48 117 delete _registration_flow_timer;
leothedragon 0:8f0bb79ddd48 118 _security_connection = NULL;
leothedragon 0:8f0bb79ddd48 119 tr_debug("M2MInterfaceImpl::~M2MInterfaceImpl() - OUT");
leothedragon 0:8f0bb79ddd48 120 }
leothedragon 0:8f0bb79ddd48 121
leothedragon 0:8f0bb79ddd48 122 void M2MInterfaceImpl::bootstrap(M2MSecurity *security)
leothedragon 0:8f0bb79ddd48 123 {
leothedragon 0:8f0bb79ddd48 124 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 125 tr_debug("M2MInterfaceImpl::bootstrap - IN - current state %d", _current_state);
leothedragon 0:8f0bb79ddd48 126 _retry_timer.stop_timer();
leothedragon 0:8f0bb79ddd48 127 _security = NULL;
leothedragon 0:8f0bb79ddd48 128 if(!security) {
leothedragon 0:8f0bb79ddd48 129 set_error_description(ERROR_REASON_1);
leothedragon 0:8f0bb79ddd48 130 _observer.error(M2MInterface::InvalidParameters);
leothedragon 0:8f0bb79ddd48 131 return;
leothedragon 0:8f0bb79ddd48 132 }
leothedragon 0:8f0bb79ddd48 133 // Transition to a new state based upon
leothedragon 0:8f0bb79ddd48 134 // the current state of the state machine
leothedragon 0:8f0bb79ddd48 135 _connection_handler.claim_mutex();
leothedragon 0:8f0bb79ddd48 136 M2MSecurityData data;
leothedragon 0:8f0bb79ddd48 137 data._object = security;
leothedragon 0:8f0bb79ddd48 138 BEGIN_TRANSITION_MAP // - Current State -
leothedragon 0:8f0bb79ddd48 139 TRANSITION_MAP_ENTRY (STATE_BOOTSTRAP) // state_idle
leothedragon 0:8f0bb79ddd48 140 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap
leothedragon 0:8f0bb79ddd48 141 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state__bootstrap_address_resolved
leothedragon 0:8f0bb79ddd48 142 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap_resource_created
leothedragon 0:8f0bb79ddd48 143 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap_wait
leothedragon 0:8f0bb79ddd48 144 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap_error_wait
leothedragon 0:8f0bb79ddd48 145 TRANSITION_MAP_ENTRY (STATE_BOOTSTRAP) // state_bootstrapped
leothedragon 0:8f0bb79ddd48 146 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_register
leothedragon 0:8f0bb79ddd48 147 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_register_address_resolved
leothedragon 0:8f0bb79ddd48 148 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_registered
leothedragon 0:8f0bb79ddd48 149 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_update_registration
leothedragon 0:8f0bb79ddd48 150 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_unregister
leothedragon 0:8f0bb79ddd48 151 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_unregistered
leothedragon 0:8f0bb79ddd48 152 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_sending_coap_data
leothedragon 0:8f0bb79ddd48 153 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_sent
leothedragon 0:8f0bb79ddd48 154 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_received
leothedragon 0:8f0bb79ddd48 155 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_processing_coap_data
leothedragon 0:8f0bb79ddd48 156 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_processed
leothedragon 0:8f0bb79ddd48 157 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_waiting
leothedragon 0:8f0bb79ddd48 158 END_TRANSITION_MAP(&data)
leothedragon 0:8f0bb79ddd48 159 if(_event_ignored) {
leothedragon 0:8f0bb79ddd48 160 _event_ignored = false;
leothedragon 0:8f0bb79ddd48 161 set_error_description(ERROR_REASON_2);
leothedragon 0:8f0bb79ddd48 162 _observer.error(M2MInterface::NotAllowed);
leothedragon 0:8f0bb79ddd48 163 }
leothedragon 0:8f0bb79ddd48 164 _connection_handler.release_mutex();
leothedragon 0:8f0bb79ddd48 165 tr_debug("M2MInterfaceImpl::bootstrap - OUT");
leothedragon 0:8f0bb79ddd48 166 #else
leothedragon 0:8f0bb79ddd48 167 set_error_description(ERROR_REASON_3);
leothedragon 0:8f0bb79ddd48 168 _observer.error(M2MInterface::NotAllowed);
leothedragon 0:8f0bb79ddd48 169 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 170
leothedragon 0:8f0bb79ddd48 171 }
leothedragon 0:8f0bb79ddd48 172
leothedragon 0:8f0bb79ddd48 173 void M2MInterfaceImpl::cancel_bootstrap()
leothedragon 0:8f0bb79ddd48 174 {
leothedragon 0:8f0bb79ddd48 175 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 176 //TODO: Do we need this ?
leothedragon 0:8f0bb79ddd48 177 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 178 }
leothedragon 0:8f0bb79ddd48 179
leothedragon 0:8f0bb79ddd48 180 void M2MInterfaceImpl::finish_bootstrap()
leothedragon 0:8f0bb79ddd48 181 {
leothedragon 0:8f0bb79ddd48 182 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 183 tr_debug("M2MInterfaceImpl::finish_bootstrap");
leothedragon 0:8f0bb79ddd48 184 _security = NULL;
leothedragon 0:8f0bb79ddd48 185 bootstrap_done();
leothedragon 0:8f0bb79ddd48 186 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 187 }
leothedragon 0:8f0bb79ddd48 188
leothedragon 0:8f0bb79ddd48 189 void M2MInterfaceImpl::register_object(M2MSecurity *security, const M2MObjectList &object_list)
leothedragon 0:8f0bb79ddd48 190 {
leothedragon 0:8f0bb79ddd48 191 M2MBaseList list;
leothedragon 0:8f0bb79ddd48 192 M2MObjectList::const_iterator it = object_list.begin();
leothedragon 0:8f0bb79ddd48 193 for ( ; it != object_list.end(); it++ ) {
leothedragon 0:8f0bb79ddd48 194 list.push_back(*it);
leothedragon 0:8f0bb79ddd48 195 }
leothedragon 0:8f0bb79ddd48 196 register_object(security, list);
leothedragon 0:8f0bb79ddd48 197 }
leothedragon 0:8f0bb79ddd48 198
leothedragon 0:8f0bb79ddd48 199 void M2MInterfaceImpl::register_object(M2MSecurity *security, const M2MBaseList &list)
leothedragon 0:8f0bb79ddd48 200 {
leothedragon 0:8f0bb79ddd48 201 tr_debug("M2MInterfaceImpl::register_object - IN - current state %d", _current_state);
leothedragon 0:8f0bb79ddd48 202 if(!security) {
leothedragon 0:8f0bb79ddd48 203 set_error_description(ERROR_REASON_4);
leothedragon 0:8f0bb79ddd48 204 _observer.error(M2MInterface::InvalidParameters);
leothedragon 0:8f0bb79ddd48 205 return;
leothedragon 0:8f0bb79ddd48 206 }
leothedragon 0:8f0bb79ddd48 207 // Transition to a new state based upon
leothedragon 0:8f0bb79ddd48 208 // the current state of the state machine
leothedragon 0:8f0bb79ddd48 209 //TODO: manage register object in a list.
leothedragon 0:8f0bb79ddd48 210 _connection_handler.claim_mutex();
leothedragon 0:8f0bb79ddd48 211 _register_server = security;
leothedragon 0:8f0bb79ddd48 212 M2MRegisterData data;
leothedragon 0:8f0bb79ddd48 213 data._object = security;
leothedragon 0:8f0bb79ddd48 214 data._base_list = list;
leothedragon 0:8f0bb79ddd48 215 BEGIN_TRANSITION_MAP // - Current State -
leothedragon 0:8f0bb79ddd48 216 TRANSITION_MAP_ENTRY (STATE_REGISTER) // state_idle
leothedragon 0:8f0bb79ddd48 217 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap
leothedragon 0:8f0bb79ddd48 218 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state__bootstrap_address_resolved
leothedragon 0:8f0bb79ddd48 219 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap_resource_created
leothedragon 0:8f0bb79ddd48 220 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap_wait
leothedragon 0:8f0bb79ddd48 221 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap_error_wait
leothedragon 0:8f0bb79ddd48 222 TRANSITION_MAP_ENTRY (STATE_REGISTER) // state_bootstrapped
leothedragon 0:8f0bb79ddd48 223 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_register
leothedragon 0:8f0bb79ddd48 224 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_register_address_resolved
leothedragon 0:8f0bb79ddd48 225 TRANSITION_MAP_ENTRY (STATE_REGISTER) // state_registered
leothedragon 0:8f0bb79ddd48 226 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_update_registration
leothedragon 0:8f0bb79ddd48 227 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_unregister
leothedragon 0:8f0bb79ddd48 228 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_unregistered
leothedragon 0:8f0bb79ddd48 229 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_sending_coap_data
leothedragon 0:8f0bb79ddd48 230 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_sent
leothedragon 0:8f0bb79ddd48 231 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_received
leothedragon 0:8f0bb79ddd48 232 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_processing_coap_data
leothedragon 0:8f0bb79ddd48 233 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_processed
leothedragon 0:8f0bb79ddd48 234 TRANSITION_MAP_ENTRY (STATE_REGISTER) // state_waiting
leothedragon 0:8f0bb79ddd48 235 END_TRANSITION_MAP(&data)
leothedragon 0:8f0bb79ddd48 236 if(_event_ignored) {
leothedragon 0:8f0bb79ddd48 237 _event_ignored = false;
leothedragon 0:8f0bb79ddd48 238 set_error_description(ERROR_REASON_5);
leothedragon 0:8f0bb79ddd48 239 _observer.error(M2MInterface::NotAllowed);
leothedragon 0:8f0bb79ddd48 240 }
leothedragon 0:8f0bb79ddd48 241 _connection_handler.release_mutex();
leothedragon 0:8f0bb79ddd48 242 tr_debug("M2MInterfaceImpl::register_object - OUT");
leothedragon 0:8f0bb79ddd48 243 }
leothedragon 0:8f0bb79ddd48 244
leothedragon 0:8f0bb79ddd48 245 void M2MInterfaceImpl::update_registration(M2MSecurity *security_object, const uint32_t lifetime)
leothedragon 0:8f0bb79ddd48 246 {
leothedragon 0:8f0bb79ddd48 247 tr_debug("M2MInterfaceImpl::update_registration()");
leothedragon 0:8f0bb79ddd48 248 _connection_handler.claim_mutex();
leothedragon 0:8f0bb79ddd48 249 M2MUpdateRegisterData data;
leothedragon 0:8f0bb79ddd48 250 data._object = security_object;
leothedragon 0:8f0bb79ddd48 251 data._lifetime = lifetime;
leothedragon 0:8f0bb79ddd48 252 start_register_update(&data);
leothedragon 0:8f0bb79ddd48 253 _connection_handler.release_mutex();
leothedragon 0:8f0bb79ddd48 254 }
leothedragon 0:8f0bb79ddd48 255
leothedragon 0:8f0bb79ddd48 256 void M2MInterfaceImpl::update_registration(M2MSecurity *security_object,
leothedragon 0:8f0bb79ddd48 257 const M2MObjectList &object_list,
leothedragon 0:8f0bb79ddd48 258 const uint32_t lifetime)
leothedragon 0:8f0bb79ddd48 259 {
leothedragon 0:8f0bb79ddd48 260 tr_debug("M2MInterfaceImpl::update_registration - with object list");
leothedragon 0:8f0bb79ddd48 261 _connection_handler.claim_mutex();
leothedragon 0:8f0bb79ddd48 262 M2MBaseList list;
leothedragon 0:8f0bb79ddd48 263 M2MObjectList::const_iterator it = object_list.begin();
leothedragon 0:8f0bb79ddd48 264 for ( ; it != object_list.end(); it++ ) {
leothedragon 0:8f0bb79ddd48 265 list.push_back(*it);
leothedragon 0:8f0bb79ddd48 266 }
leothedragon 0:8f0bb79ddd48 267 update_registration(security_object, list, lifetime);
leothedragon 0:8f0bb79ddd48 268 _connection_handler.release_mutex();
leothedragon 0:8f0bb79ddd48 269 }
leothedragon 0:8f0bb79ddd48 270
leothedragon 0:8f0bb79ddd48 271 void M2MInterfaceImpl::update_registration(M2MSecurity *security_object,
leothedragon 0:8f0bb79ddd48 272 const M2MBaseList &list,
leothedragon 0:8f0bb79ddd48 273 const uint32_t lifetime)
leothedragon 0:8f0bb79ddd48 274 {
leothedragon 0:8f0bb79ddd48 275 tr_debug("M2MInterfaceImpl::update_registration(M2MSecurity *security_object, const M2MObjectList &object_list, const uint32_t lifetime)");
leothedragon 0:8f0bb79ddd48 276 _connection_handler.claim_mutex();
leothedragon 0:8f0bb79ddd48 277 M2MUpdateRegisterData data;
leothedragon 0:8f0bb79ddd48 278 data._object = security_object;
leothedragon 0:8f0bb79ddd48 279 data._lifetime = lifetime;
leothedragon 0:8f0bb79ddd48 280 data._base_list = list;
leothedragon 0:8f0bb79ddd48 281 start_register_update(&data);
leothedragon 0:8f0bb79ddd48 282 _connection_handler.release_mutex();
leothedragon 0:8f0bb79ddd48 283 }
leothedragon 0:8f0bb79ddd48 284
leothedragon 0:8f0bb79ddd48 285 void M2MInterfaceImpl::unregister_object(M2MSecurity* /*security*/)
leothedragon 0:8f0bb79ddd48 286 {
leothedragon 0:8f0bb79ddd48 287 tr_debug("M2MInterfaceImpl::unregister_object - IN - current state %d", _current_state);
leothedragon 0:8f0bb79ddd48 288 if (_nsdl_interface.is_unregister_ongoing()) {
leothedragon 0:8f0bb79ddd48 289 set_error_description(ERROR_REASON_27);
leothedragon 0:8f0bb79ddd48 290 _observer.error(M2MInterface::NotAllowed);
leothedragon 0:8f0bb79ddd48 291 return;
leothedragon 0:8f0bb79ddd48 292 }
leothedragon 0:8f0bb79ddd48 293
leothedragon 0:8f0bb79ddd48 294 _connection_handler.claim_mutex();
leothedragon 0:8f0bb79ddd48 295 // Transition to a new state based upon
leothedragon 0:8f0bb79ddd48 296 // the current state of the state machine
leothedragon 0:8f0bb79ddd48 297 BEGIN_TRANSITION_MAP // - Current State -
leothedragon 0:8f0bb79ddd48 298 TRANSITION_MAP_ENTRY (STATE_UNREGISTERED) // state_idle
leothedragon 0:8f0bb79ddd48 299 TRANSITION_MAP_ENTRY (STATE_UNREGISTERED) // state_bootstrap
leothedragon 0:8f0bb79ddd48 300 TRANSITION_MAP_ENTRY (STATE_UNREGISTERED) // state__bootstrap_address_resolved
leothedragon 0:8f0bb79ddd48 301 TRANSITION_MAP_ENTRY (STATE_UNREGISTERED) // state_bootstrap_resource_created
leothedragon 0:8f0bb79ddd48 302 TRANSITION_MAP_ENTRY (STATE_UNREGISTERED) // state_bootstrap_wait
leothedragon 0:8f0bb79ddd48 303 TRANSITION_MAP_ENTRY (STATE_UNREGISTERED) // state_bootstrap_error_wait
leothedragon 0:8f0bb79ddd48 304 TRANSITION_MAP_ENTRY (STATE_UNREGISTERED) // state_bootstrapped
leothedragon 0:8f0bb79ddd48 305 TRANSITION_MAP_ENTRY (STATE_UNREGISTERED) // state_register
leothedragon 0:8f0bb79ddd48 306 TRANSITION_MAP_ENTRY (STATE_UNREGISTERED) // state_register_address_resolved
leothedragon 0:8f0bb79ddd48 307 TRANSITION_MAP_ENTRY (STATE_UNREGISTER) // state_registered
leothedragon 0:8f0bb79ddd48 308 TRANSITION_MAP_ENTRY (STATE_UNREGISTER) // state_update_registration
leothedragon 0:8f0bb79ddd48 309 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_unregister
leothedragon 0:8f0bb79ddd48 310 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_unregistered
leothedragon 0:8f0bb79ddd48 311 TRANSITION_MAP_ENTRY (STATE_UNREGISTER) // state_sending_coap_data
leothedragon 0:8f0bb79ddd48 312 TRANSITION_MAP_ENTRY (STATE_UNREGISTER) // state_coap_data_sent
leothedragon 0:8f0bb79ddd48 313 TRANSITION_MAP_ENTRY (STATE_UNREGISTER) // state_coap_data_received
leothedragon 0:8f0bb79ddd48 314 TRANSITION_MAP_ENTRY (STATE_UNREGISTER) // state_processing_coap_data
leothedragon 0:8f0bb79ddd48 315 TRANSITION_MAP_ENTRY (STATE_UNREGISTER) // state_coap_data_processed
leothedragon 0:8f0bb79ddd48 316 TRANSITION_MAP_ENTRY (STATE_UNREGISTER) // state_waiting
leothedragon 0:8f0bb79ddd48 317 END_TRANSITION_MAP(NULL)
leothedragon 0:8f0bb79ddd48 318
leothedragon 0:8f0bb79ddd48 319 _connection_handler.release_mutex();
leothedragon 0:8f0bb79ddd48 320 tr_debug("M2MInterfaceImpl::unregister_object - OUT");
leothedragon 0:8f0bb79ddd48 321 }
leothedragon 0:8f0bb79ddd48 322
leothedragon 0:8f0bb79ddd48 323 void M2MInterfaceImpl::set_queue_sleep_handler(callback_handler handler)
leothedragon 0:8f0bb79ddd48 324 {
leothedragon 0:8f0bb79ddd48 325 _callback_handler = handler;
leothedragon 0:8f0bb79ddd48 326 }
leothedragon 0:8f0bb79ddd48 327
leothedragon 0:8f0bb79ddd48 328 void M2MInterfaceImpl::set_random_number_callback(random_number_cb callback)
leothedragon 0:8f0bb79ddd48 329 {
leothedragon 0:8f0bb79ddd48 330 if(_security_connection) {
leothedragon 0:8f0bb79ddd48 331 _security_connection->set_random_number_callback(callback);
leothedragon 0:8f0bb79ddd48 332 }
leothedragon 0:8f0bb79ddd48 333 }
leothedragon 0:8f0bb79ddd48 334
leothedragon 0:8f0bb79ddd48 335 void M2MInterfaceImpl::set_entropy_callback(entropy_cb callback)
leothedragon 0:8f0bb79ddd48 336 {
leothedragon 0:8f0bb79ddd48 337 if(_security_connection) {
leothedragon 0:8f0bb79ddd48 338 _security_connection->set_entropy_callback(callback);
leothedragon 0:8f0bb79ddd48 339 }
leothedragon 0:8f0bb79ddd48 340 }
leothedragon 0:8f0bb79ddd48 341
leothedragon 0:8f0bb79ddd48 342 void M2MInterfaceImpl::set_platform_network_handler(void *handler)
leothedragon 0:8f0bb79ddd48 343 {
leothedragon 0:8f0bb79ddd48 344 _connection_handler.set_platform_network_handler(handler);
leothedragon 0:8f0bb79ddd48 345 }
leothedragon 0:8f0bb79ddd48 346
leothedragon 0:8f0bb79ddd48 347 void M2MInterfaceImpl::coap_message_ready(uint8_t *data_ptr,
leothedragon 0:8f0bb79ddd48 348 uint16_t data_len,
leothedragon 0:8f0bb79ddd48 349 sn_nsdl_addr_s *address_ptr)
leothedragon 0:8f0bb79ddd48 350 {
leothedragon 0:8f0bb79ddd48 351 tr_debug("M2MInterfaceImpl::coap_message_ready");
leothedragon 0:8f0bb79ddd48 352 if (_current_state != STATE_IDLE) {
leothedragon 0:8f0bb79ddd48 353 internal_event(STATE_SENDING_COAP_DATA);
leothedragon 0:8f0bb79ddd48 354 if(!_connection_handler.send_data(data_ptr,data_len,address_ptr)) {
leothedragon 0:8f0bb79ddd48 355 internal_event( STATE_IDLE);
leothedragon 0:8f0bb79ddd48 356 tr_error("M2MInterfaceImpl::coap_message_ready() - M2MInterface::NetworkError");
leothedragon 0:8f0bb79ddd48 357 if (!_reconnecting) {
leothedragon 0:8f0bb79ddd48 358 _queue_mode_timer_ongoing = false;
leothedragon 0:8f0bb79ddd48 359 socket_error(M2MConnectionHandler::SOCKET_SEND_ERROR, true);
leothedragon 0:8f0bb79ddd48 360 } else {
leothedragon 0:8f0bb79ddd48 361 socket_error(M2MConnectionHandler::SOCKET_ABORT);
leothedragon 0:8f0bb79ddd48 362 }
leothedragon 0:8f0bb79ddd48 363 }
leothedragon 0:8f0bb79ddd48 364 }
leothedragon 0:8f0bb79ddd48 365 }
leothedragon 0:8f0bb79ddd48 366
leothedragon 0:8f0bb79ddd48 367 void M2MInterfaceImpl::client_registered(M2MServer *server_object)
leothedragon 0:8f0bb79ddd48 368 {
leothedragon 0:8f0bb79ddd48 369 tr_info("M2MInterfaceImpl::client_registered");
leothedragon 0:8f0bb79ddd48 370 internal_event(STATE_REGISTERED);
leothedragon 0:8f0bb79ddd48 371 //Inform client is registered.
leothedragon 0:8f0bb79ddd48 372 //TODO: manage register object in a list.
leothedragon 0:8f0bb79ddd48 373 _observer.object_registered(_register_server,*server_object);
leothedragon 0:8f0bb79ddd48 374 }
leothedragon 0:8f0bb79ddd48 375
leothedragon 0:8f0bb79ddd48 376 void M2MInterfaceImpl::registration_updated(const M2MServer &server_object)
leothedragon 0:8f0bb79ddd48 377 {
leothedragon 0:8f0bb79ddd48 378 tr_info("M2MInterfaceImpl::registration_updated");
leothedragon 0:8f0bb79ddd48 379 internal_event(STATE_REGISTERED);
leothedragon 0:8f0bb79ddd48 380 _observer.registration_updated(_register_server,server_object);
leothedragon 0:8f0bb79ddd48 381 }
leothedragon 0:8f0bb79ddd48 382
leothedragon 0:8f0bb79ddd48 383 void M2MInterfaceImpl::registration_error(uint8_t error_code, bool retry, bool full_registration)
leothedragon 0:8f0bb79ddd48 384 {
leothedragon 0:8f0bb79ddd48 385 tr_error("M2MInterfaceImpl::registration_error code [%d]", error_code);
leothedragon 0:8f0bb79ddd48 386
leothedragon 0:8f0bb79ddd48 387 _nsdl_interface.set_registration_status(false);
leothedragon 0:8f0bb79ddd48 388
leothedragon 0:8f0bb79ddd48 389 // Try to register again
leothedragon 0:8f0bb79ddd48 390 if (retry) {
leothedragon 0:8f0bb79ddd48 391 _queue_mode_timer_ongoing = false;
leothedragon 0:8f0bb79ddd48 392
leothedragon 0:8f0bb79ddd48 393 if (full_registration) {
leothedragon 0:8f0bb79ddd48 394 _reconnection_state = M2MInterfaceImpl::None;
leothedragon 0:8f0bb79ddd48 395 } else if (error_code == M2MInterface::UnregistrationFailed) {
leothedragon 0:8f0bb79ddd48 396 _reconnection_state = M2MInterfaceImpl::Unregistration;
leothedragon 0:8f0bb79ddd48 397 }
leothedragon 0:8f0bb79ddd48 398
leothedragon 0:8f0bb79ddd48 399 socket_error(M2MConnectionHandler::SOCKET_SEND_ERROR);
leothedragon 0:8f0bb79ddd48 400 } else {
leothedragon 0:8f0bb79ddd48 401 _security = NULL;
leothedragon 0:8f0bb79ddd48 402 internal_event(STATE_IDLE);
leothedragon 0:8f0bb79ddd48 403 if (error_code == M2MInterface::UnregistrationFailed) {
leothedragon 0:8f0bb79ddd48 404 set_error_description(ERROR_REASON_24);
leothedragon 0:8f0bb79ddd48 405 } else {
leothedragon 0:8f0bb79ddd48 406 set_error_description(ERROR_REASON_8);
leothedragon 0:8f0bb79ddd48 407 }
leothedragon 0:8f0bb79ddd48 408 _observer.error((M2MInterface::Error)error_code);
leothedragon 0:8f0bb79ddd48 409 }
leothedragon 0:8f0bb79ddd48 410 }
leothedragon 0:8f0bb79ddd48 411
leothedragon 0:8f0bb79ddd48 412 void M2MInterfaceImpl::client_unregistered()
leothedragon 0:8f0bb79ddd48 413 {
leothedragon 0:8f0bb79ddd48 414 tr_info("M2MInterfaceImpl::client_unregistered()");
leothedragon 0:8f0bb79ddd48 415 _nsdl_interface.set_registration_status(false);
leothedragon 0:8f0bb79ddd48 416
leothedragon 0:8f0bb79ddd48 417 // Zero the flag otherwise next registered response will start unregistration process
leothedragon 0:8f0bb79ddd48 418 _reconnection_state = M2MInterfaceImpl::None;
leothedragon 0:8f0bb79ddd48 419
leothedragon 0:8f0bb79ddd48 420 internal_event(STATE_UNREGISTERED);
leothedragon 0:8f0bb79ddd48 421 //TODO: manage register object in a list.
leothedragon 0:8f0bb79ddd48 422 }
leothedragon 0:8f0bb79ddd48 423
leothedragon 0:8f0bb79ddd48 424 void M2MInterfaceImpl::bootstrap_done()
leothedragon 0:8f0bb79ddd48 425 {
leothedragon 0:8f0bb79ddd48 426 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 427 tr_info("M2MInterfaceImpl::bootstrap_done");
leothedragon 0:8f0bb79ddd48 428 _reconnection_time = _initial_reconnection_time;
leothedragon 0:8f0bb79ddd48 429 _reconnecting = false;
leothedragon 0:8f0bb79ddd48 430 _reconnection_state = M2MInterfaceImpl::None;
leothedragon 0:8f0bb79ddd48 431 _bootstrapped = true;
leothedragon 0:8f0bb79ddd48 432
leothedragon 0:8f0bb79ddd48 433 if (_registration_flow_timer) {
leothedragon 0:8f0bb79ddd48 434 _registration_flow_timer->stop_timer();
leothedragon 0:8f0bb79ddd48 435 }
leothedragon 0:8f0bb79ddd48 436
leothedragon 0:8f0bb79ddd48 437 // Force close connection since either server already closed (sent PEER_CLOSE_NOTIFY)
leothedragon 0:8f0bb79ddd48 438 // or bootstrap flow has finished.
leothedragon 0:8f0bb79ddd48 439 _connection_handler.force_close();
leothedragon 0:8f0bb79ddd48 440
leothedragon 0:8f0bb79ddd48 441 if (_bootstrap_finished) {
leothedragon 0:8f0bb79ddd48 442 // Inform to observer only if bootstrap has already been finished
leothedragon 0:8f0bb79ddd48 443 // This has to be done like this since we might get bootstrap_done
leothedragon 0:8f0bb79ddd48 444 // callback BEFORE bootstrap_finish
leothedragon 0:8f0bb79ddd48 445 internal_event(STATE_BOOTSTRAPPED);
leothedragon 0:8f0bb79ddd48 446 _observer.bootstrap_done(_nsdl_interface.get_security_object());
leothedragon 0:8f0bb79ddd48 447 }
leothedragon 0:8f0bb79ddd48 448 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 449 }
leothedragon 0:8f0bb79ddd48 450
leothedragon 0:8f0bb79ddd48 451 void M2MInterfaceImpl::bootstrap_finish()
leothedragon 0:8f0bb79ddd48 452 {
leothedragon 0:8f0bb79ddd48 453 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 454 tr_info("M2MInterfaceImpl::bootstrap_finish");
leothedragon 0:8f0bb79ddd48 455 internal_event(STATE_BOOTSTRAP_WAIT);
leothedragon 0:8f0bb79ddd48 456 _observer.bootstrap_data_ready(_nsdl_interface.get_security_object());
leothedragon 0:8f0bb79ddd48 457 _bootstrap_finished = true;
leothedragon 0:8f0bb79ddd48 458
leothedragon 0:8f0bb79ddd48 459 if (_bootstrapped) {
leothedragon 0:8f0bb79ddd48 460 // If _bootstrapped is set, we have already received the bootstrap_done
leothedragon 0:8f0bb79ddd48 461 // callback so we must inform observer now
leothedragon 0:8f0bb79ddd48 462 bootstrap_done();
leothedragon 0:8f0bb79ddd48 463 }
leothedragon 0:8f0bb79ddd48 464 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 465 }
leothedragon 0:8f0bb79ddd48 466
leothedragon 0:8f0bb79ddd48 467 void M2MInterfaceImpl::bootstrap_wait()
leothedragon 0:8f0bb79ddd48 468 {
leothedragon 0:8f0bb79ddd48 469 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 470 tr_info("M2MInterfaceImpl::bootstrap_wait");
leothedragon 0:8f0bb79ddd48 471 internal_event(STATE_BOOTSTRAP_WAIT);
leothedragon 0:8f0bb79ddd48 472 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 473 }
leothedragon 0:8f0bb79ddd48 474
leothedragon 0:8f0bb79ddd48 475 void M2MInterfaceImpl::bootstrap_error_wait(const char *reason)
leothedragon 0:8f0bb79ddd48 476 {
leothedragon 0:8f0bb79ddd48 477 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 478 tr_error("M2MInterfaceImpl::bootstrap_error_wait");
leothedragon 0:8f0bb79ddd48 479 set_error_description(reason);
leothedragon 0:8f0bb79ddd48 480 internal_event(STATE_BOOTSTRAP_ERROR_WAIT);
leothedragon 0:8f0bb79ddd48 481 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 482 }
leothedragon 0:8f0bb79ddd48 483
leothedragon 0:8f0bb79ddd48 484 void M2MInterfaceImpl::bootstrap_error(const char *reason)
leothedragon 0:8f0bb79ddd48 485 {
leothedragon 0:8f0bb79ddd48 486 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 487 tr_error("M2MInterfaceImpl::bootstrap_error(%s)", reason);
leothedragon 0:8f0bb79ddd48 488 _bootstrapped = false;
leothedragon 0:8f0bb79ddd48 489 if (_registration_flow_timer) {
leothedragon 0:8f0bb79ddd48 490 _registration_flow_timer->stop_timer();
leothedragon 0:8f0bb79ddd48 491 }
leothedragon 0:8f0bb79ddd48 492
leothedragon 0:8f0bb79ddd48 493 _reconnection_state = M2MInterfaceImpl::None;
leothedragon 0:8f0bb79ddd48 494
leothedragon 0:8f0bb79ddd48 495 set_error_description(reason);
leothedragon 0:8f0bb79ddd48 496
leothedragon 0:8f0bb79ddd48 497 _observer.error(M2MInterface::BootstrapFailed);
leothedragon 0:8f0bb79ddd48 498
leothedragon 0:8f0bb79ddd48 499 internal_event(STATE_IDLE);
leothedragon 0:8f0bb79ddd48 500 _reconnecting = true;
leothedragon 0:8f0bb79ddd48 501 _connection_handler.stop_listening();
leothedragon 0:8f0bb79ddd48 502
leothedragon 0:8f0bb79ddd48 503 _retry_timer_expired = false;
leothedragon 0:8f0bb79ddd48 504 _retry_timer.start_timer(_reconnection_time * 1000,
leothedragon 0:8f0bb79ddd48 505 M2MTimerObserver::RetryTimer);
leothedragon 0:8f0bb79ddd48 506 tr_info("M2MInterfaceImpl::bootstrap_error - reconnecting in %" PRIu64 "(s)", _reconnection_time);
leothedragon 0:8f0bb79ddd48 507 _reconnection_time = _reconnection_time * RECONNECT_INCREMENT_FACTOR;
leothedragon 0:8f0bb79ddd48 508 if(_reconnection_time >= MAX_RECONNECT_TIMEOUT) {
leothedragon 0:8f0bb79ddd48 509 _reconnection_time = MAX_RECONNECT_TIMEOUT;
leothedragon 0:8f0bb79ddd48 510 }
leothedragon 0:8f0bb79ddd48 511 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 512 }
leothedragon 0:8f0bb79ddd48 513
leothedragon 0:8f0bb79ddd48 514 void M2MInterfaceImpl::coap_data_processed()
leothedragon 0:8f0bb79ddd48 515 {
leothedragon 0:8f0bb79ddd48 516 tr_debug("M2MInterfaceImpl::coap_data_processed()");
leothedragon 0:8f0bb79ddd48 517 internal_event(STATE_COAP_DATA_PROCESSED);
leothedragon 0:8f0bb79ddd48 518 }
leothedragon 0:8f0bb79ddd48 519
leothedragon 0:8f0bb79ddd48 520 void M2MInterfaceImpl::value_updated(M2MBase *base)
leothedragon 0:8f0bb79ddd48 521 {
leothedragon 0:8f0bb79ddd48 522 tr_debug("M2MInterfaceImpl::value_updated");
leothedragon 0:8f0bb79ddd48 523 if(base) {
leothedragon 0:8f0bb79ddd48 524 M2MBase::BaseType type = base->base_type();
leothedragon 0:8f0bb79ddd48 525 _observer.value_updated(base, type);
leothedragon 0:8f0bb79ddd48 526 }
leothedragon 0:8f0bb79ddd48 527 }
leothedragon 0:8f0bb79ddd48 528
leothedragon 0:8f0bb79ddd48 529 void M2MInterfaceImpl::data_available(uint8_t* data,
leothedragon 0:8f0bb79ddd48 530 uint16_t data_size,
leothedragon 0:8f0bb79ddd48 531 const M2MConnectionObserver::SocketAddress &address)
leothedragon 0:8f0bb79ddd48 532 {
leothedragon 0:8f0bb79ddd48 533 tr_debug("M2MInterfaceImpl::data_available");
leothedragon 0:8f0bb79ddd48 534 ReceivedData event;
leothedragon 0:8f0bb79ddd48 535 event._data = data;
leothedragon 0:8f0bb79ddd48 536 event._size = data_size;
leothedragon 0:8f0bb79ddd48 537 event._address = &address;
leothedragon 0:8f0bb79ddd48 538 internal_event(STATE_COAP_DATA_RECEIVED, &event);
leothedragon 0:8f0bb79ddd48 539 }
leothedragon 0:8f0bb79ddd48 540
leothedragon 0:8f0bb79ddd48 541 void M2MInterfaceImpl::socket_error(int error_code, bool retry)
leothedragon 0:8f0bb79ddd48 542 {
leothedragon 0:8f0bb79ddd48 543 // Bootstrap completed once PEER CLOSE notify received from the server.
leothedragon 0:8f0bb79ddd48 544 if (_current_state == STATE_BOOTSTRAP_WAIT &&
leothedragon 0:8f0bb79ddd48 545 error_code == M2MConnectionHandler::SSL_PEER_CLOSE_NOTIFY) {
leothedragon 0:8f0bb79ddd48 546 _security = NULL;
leothedragon 0:8f0bb79ddd48 547 bootstrap_done();
leothedragon 0:8f0bb79ddd48 548 return;
leothedragon 0:8f0bb79ddd48 549 }
leothedragon 0:8f0bb79ddd48 550
leothedragon 0:8f0bb79ddd48 551 tr_error("M2MInterfaceImpl::socket_error: (%d), retry (%d), reconnecting (%d), reconnection_state (%d)",
leothedragon 0:8f0bb79ddd48 552 error_code, retry, _reconnecting, (int)_reconnection_state);
leothedragon 0:8f0bb79ddd48 553
leothedragon 0:8f0bb79ddd48 554 // Ignore errors while client is sleeping
leothedragon 0:8f0bb79ddd48 555 if (queue_mode()) {
leothedragon 0:8f0bb79ddd48 556 if (_callback_handler && _queue_mode_timer_ongoing) {
leothedragon 0:8f0bb79ddd48 557 tr_info("M2MInterfaceImpl::socket_error - Queue Mode - don't try to reconnect while in QueueMode");
leothedragon 0:8f0bb79ddd48 558 return;
leothedragon 0:8f0bb79ddd48 559 }
leothedragon 0:8f0bb79ddd48 560 }
leothedragon 0:8f0bb79ddd48 561 _queue_sleep_timer.stop_timer();
leothedragon 0:8f0bb79ddd48 562
leothedragon 0:8f0bb79ddd48 563 if (_registration_flow_timer) {
leothedragon 0:8f0bb79ddd48 564 _registration_flow_timer->stop_timer();
leothedragon 0:8f0bb79ddd48 565 }
leothedragon 0:8f0bb79ddd48 566
leothedragon 0:8f0bb79ddd48 567 const char *error_code_des;
leothedragon 0:8f0bb79ddd48 568 M2MInterface::Error error = M2MInterface::ErrorNone;
leothedragon 0:8f0bb79ddd48 569 switch (error_code) {
leothedragon 0:8f0bb79ddd48 570 case M2MConnectionHandler::SSL_CONNECTION_ERROR:
leothedragon 0:8f0bb79ddd48 571 error = M2MInterface::SecureConnectionFailed;
leothedragon 0:8f0bb79ddd48 572 error_code_des = ERROR_SECURE_CONNECTION;
leothedragon 0:8f0bb79ddd48 573 break;
leothedragon 0:8f0bb79ddd48 574 case M2MConnectionHandler::DNS_RESOLVING_ERROR:
leothedragon 0:8f0bb79ddd48 575 error = M2MInterface::DnsResolvingFailed;
leothedragon 0:8f0bb79ddd48 576 error_code_des = ERROR_DNS;
leothedragon 0:8f0bb79ddd48 577 break;
leothedragon 0:8f0bb79ddd48 578 case M2MConnectionHandler::SOCKET_READ_ERROR:
leothedragon 0:8f0bb79ddd48 579 error = M2MInterface::NetworkError;
leothedragon 0:8f0bb79ddd48 580 error_code_des = ERROR_NETWORK;
leothedragon 0:8f0bb79ddd48 581 break;
leothedragon 0:8f0bb79ddd48 582 case M2MConnectionHandler::SOCKET_SEND_ERROR:
leothedragon 0:8f0bb79ddd48 583 error = M2MInterface::NetworkError;
leothedragon 0:8f0bb79ddd48 584 error_code_des = ERROR_NETWORK;
leothedragon 0:8f0bb79ddd48 585 break;
leothedragon 0:8f0bb79ddd48 586 case M2MConnectionHandler::SSL_HANDSHAKE_ERROR:
leothedragon 0:8f0bb79ddd48 587 error = M2MInterface::SecureConnectionFailed;
leothedragon 0:8f0bb79ddd48 588 error_code_des = ERROR_SECURE_CONNECTION;
leothedragon 0:8f0bb79ddd48 589 break;
leothedragon 0:8f0bb79ddd48 590 case M2MConnectionHandler::SOCKET_ABORT:
leothedragon 0:8f0bb79ddd48 591 error = M2MInterface::NetworkError;
leothedragon 0:8f0bb79ddd48 592 error_code_des = ERROR_NETWORK;
leothedragon 0:8f0bb79ddd48 593 break;
leothedragon 0:8f0bb79ddd48 594 case M2MConnectionHandler::MEMORY_ALLOCATION_FAILED:
leothedragon 0:8f0bb79ddd48 595 error = M2MInterface::MemoryFail;
leothedragon 0:8f0bb79ddd48 596 error_code_des = ERROR_NO_MEMORY;
leothedragon 0:8f0bb79ddd48 597 break;
leothedragon 0:8f0bb79ddd48 598 case M2MConnectionHandler::FAILED_TO_READ_CREDENTIALS:
leothedragon 0:8f0bb79ddd48 599 error = M2MInterface::FailedToReadCredentials;
leothedragon 0:8f0bb79ddd48 600 error_code_des = ERROR_FAILED_TO_READ_CREDENTIALS;
leothedragon 0:8f0bb79ddd48 601 break;
leothedragon 0:8f0bb79ddd48 602 default:
leothedragon 0:8f0bb79ddd48 603 error_code_des = ERROR_NO;
leothedragon 0:8f0bb79ddd48 604 break;
leothedragon 0:8f0bb79ddd48 605 }
leothedragon 0:8f0bb79ddd48 606
leothedragon 0:8f0bb79ddd48 607 internal_event(STATE_IDLE);
leothedragon 0:8f0bb79ddd48 608
leothedragon 0:8f0bb79ddd48 609 // Do a reconnect
leothedragon 0:8f0bb79ddd48 610 if (retry) {
leothedragon 0:8f0bb79ddd48 611 if ((error == M2MInterface::SecureConnectionFailed || error == M2MInterface::InvalidParameters) &&
leothedragon 0:8f0bb79ddd48 612 _bootstrapped) {
leothedragon 0:8f0bb79ddd48 613 // Connector client will start the bootstrap flow again
leothedragon 0:8f0bb79ddd48 614 tr_info("M2MInterfaceImpl::socket_error - goes to re-bootstrap");
leothedragon 0:8f0bb79ddd48 615 _observer.error(M2MInterface::SecureConnectionFailed);
leothedragon 0:8f0bb79ddd48 616 return;
leothedragon 0:8f0bb79ddd48 617 }
leothedragon 0:8f0bb79ddd48 618
leothedragon 0:8f0bb79ddd48 619 _nsdl_interface.set_request_context_to_be_resend(NULL, 0);
leothedragon 0:8f0bb79ddd48 620 _reconnecting = true;
leothedragon 0:8f0bb79ddd48 621 _connection_handler.stop_listening();
leothedragon 0:8f0bb79ddd48 622 _retry_timer_expired = false;
leothedragon 0:8f0bb79ddd48 623 _retry_timer.start_timer(_reconnection_time * 1000,
leothedragon 0:8f0bb79ddd48 624 M2MTimerObserver::RetryTimer);
leothedragon 0:8f0bb79ddd48 625
leothedragon 0:8f0bb79ddd48 626 tr_info("M2MInterfaceImpl::socket_error - reconnecting in %" PRIu64 "(s)", _reconnection_time);
leothedragon 0:8f0bb79ddd48 627
leothedragon 0:8f0bb79ddd48 628 _reconnection_time = _reconnection_time * RECONNECT_INCREMENT_FACTOR;
leothedragon 0:8f0bb79ddd48 629 if (_reconnection_time >= MAX_RECONNECT_TIMEOUT) {
leothedragon 0:8f0bb79ddd48 630 _reconnection_time = MAX_RECONNECT_TIMEOUT;
leothedragon 0:8f0bb79ddd48 631 }
leothedragon 0:8f0bb79ddd48 632 #ifndef DISABLE_ERROR_DESCRIPTION
leothedragon 0:8f0bb79ddd48 633 snprintf(_error_description, sizeof(_error_description), ERROR_REASON_9, error_code_des);
leothedragon 0:8f0bb79ddd48 634 #endif
leothedragon 0:8f0bb79ddd48 635 }
leothedragon 0:8f0bb79ddd48 636 // Inform application
leothedragon 0:8f0bb79ddd48 637 if (!retry && M2MInterface::ErrorNone != error) {
leothedragon 0:8f0bb79ddd48 638 tr_info("M2MInterfaceImpl::socket_error - send error to application");
leothedragon 0:8f0bb79ddd48 639 _connection_handler.stop_listening();
leothedragon 0:8f0bb79ddd48 640 _retry_timer.stop_timer();
leothedragon 0:8f0bb79ddd48 641 _security = NULL;
leothedragon 0:8f0bb79ddd48 642 _reconnecting = false;
leothedragon 0:8f0bb79ddd48 643 _reconnection_time = _initial_reconnection_time;
leothedragon 0:8f0bb79ddd48 644 _reconnection_state = M2MInterfaceImpl::None;
leothedragon 0:8f0bb79ddd48 645 #ifndef DISABLE_ERROR_DESCRIPTION
leothedragon 0:8f0bb79ddd48 646 snprintf(_error_description, sizeof(_error_description), ERROR_REASON_10, error_code_des);
leothedragon 0:8f0bb79ddd48 647 #endif
leothedragon 0:8f0bb79ddd48 648 }
leothedragon 0:8f0bb79ddd48 649 if (M2MInterface::ErrorNone != error) {
leothedragon 0:8f0bb79ddd48 650 _observer.error(error);
leothedragon 0:8f0bb79ddd48 651 }
leothedragon 0:8f0bb79ddd48 652 }
leothedragon 0:8f0bb79ddd48 653
leothedragon 0:8f0bb79ddd48 654 void M2MInterfaceImpl::address_ready(const M2MConnectionObserver::SocketAddress &address,
leothedragon 0:8f0bb79ddd48 655 M2MConnectionObserver::ServerType server_type,
leothedragon 0:8f0bb79ddd48 656 const uint16_t server_port)
leothedragon 0:8f0bb79ddd48 657 {
leothedragon 0:8f0bb79ddd48 658 tr_debug("M2MInterfaceImpl::address_ready");
leothedragon 0:8f0bb79ddd48 659 ResolvedAddressData data;
leothedragon 0:8f0bb79ddd48 660 data._address = &address;
leothedragon 0:8f0bb79ddd48 661 data._port = server_port;
leothedragon 0:8f0bb79ddd48 662 if( M2MConnectionObserver::Bootstrap == server_type) {
leothedragon 0:8f0bb79ddd48 663 tr_info("M2MInterfaceImpl::address_ready() Server Type Bootstrap");
leothedragon 0:8f0bb79ddd48 664 internal_event(STATE_BOOTSTRAP_ADDRESS_RESOLVED, &data);
leothedragon 0:8f0bb79ddd48 665 } else {
leothedragon 0:8f0bb79ddd48 666 tr_info("M2MInterfaceImpl::address_ready() Server Type LWM2M");
leothedragon 0:8f0bb79ddd48 667 internal_event(STATE_REGISTER_ADDRESS_RESOLVED, &data);
leothedragon 0:8f0bb79ddd48 668 }
leothedragon 0:8f0bb79ddd48 669 }
leothedragon 0:8f0bb79ddd48 670
leothedragon 0:8f0bb79ddd48 671 void M2MInterfaceImpl::data_sent()
leothedragon 0:8f0bb79ddd48 672 {
leothedragon 0:8f0bb79ddd48 673 tr_debug("M2MInterfaceImpl::data_sent()");
leothedragon 0:8f0bb79ddd48 674 if(queue_mode()) {
leothedragon 0:8f0bb79ddd48 675 if(_callback_handler && (_nsdl_interface.is_unregister_ongoing() == false)) {
leothedragon 0:8f0bb79ddd48 676 _queue_sleep_timer.stop_timer();
leothedragon 0:8f0bb79ddd48 677 _queue_sleep_timer.start_timer(_nsdl_interface.total_retransmission_time(_nsdl_interface.get_resend_count()) * (uint64_t)1000,
leothedragon 0:8f0bb79ddd48 678 M2MTimerObserver::QueueSleep);
leothedragon 0:8f0bb79ddd48 679 }
leothedragon 0:8f0bb79ddd48 680 }
leothedragon 0:8f0bb79ddd48 681
leothedragon 0:8f0bb79ddd48 682 if (_current_state == STATE_BOOTSTRAP_ERROR_WAIT) {
leothedragon 0:8f0bb79ddd48 683 // bootstrap_error to be called only after we have sent the last ACK.
leothedragon 0:8f0bb79ddd48 684 // Otherwise client will goto reconnection mode before ACK has sent.
leothedragon 0:8f0bb79ddd48 685 bootstrap_error(error_description());
leothedragon 0:8f0bb79ddd48 686 } else if (_current_state != STATE_BOOTSTRAP_WAIT) {
leothedragon 0:8f0bb79ddd48 687 internal_event(STATE_COAP_DATA_SENT);
leothedragon 0:8f0bb79ddd48 688 }
leothedragon 0:8f0bb79ddd48 689
leothedragon 0:8f0bb79ddd48 690 // Delay the time when CoAP ping will be send.
leothedragon 0:8f0bb79ddd48 691 _nsdl_interface.calculate_new_coap_ping_send_time();
leothedragon 0:8f0bb79ddd48 692 }
leothedragon 0:8f0bb79ddd48 693
leothedragon 0:8f0bb79ddd48 694 void M2MInterfaceImpl::timer_expired(M2MTimerObserver::Type type)
leothedragon 0:8f0bb79ddd48 695 {
leothedragon 0:8f0bb79ddd48 696 if (M2MTimerObserver::QueueSleep == type) {
leothedragon 0:8f0bb79ddd48 697 if (_reconnecting) {
leothedragon 0:8f0bb79ddd48 698 tr_debug("M2MInterfaceImpl::timer_expired() - reconnection ongoing, continue sleep timer");
leothedragon 0:8f0bb79ddd48 699 _queue_sleep_timer.start_timer(_nsdl_interface.total_retransmission_time(_nsdl_interface.get_resend_count()) * (uint64_t)1000,
leothedragon 0:8f0bb79ddd48 700 M2MTimerObserver::QueueSleep);
leothedragon 0:8f0bb79ddd48 701 } else {
leothedragon 0:8f0bb79ddd48 702 tr_debug("M2MInterfaceImpl::timer_expired() - sleep");
leothedragon 0:8f0bb79ddd48 703 M2MTimer &timer = _nsdl_interface.get_nsdl_execution_timer();
leothedragon 0:8f0bb79ddd48 704 timer.stop_timer();
leothedragon 0:8f0bb79ddd48 705 _queue_mode_timer_ongoing = true;
leothedragon 0:8f0bb79ddd48 706 if(_callback_handler) {
leothedragon 0:8f0bb79ddd48 707 _callback_handler();
leothedragon 0:8f0bb79ddd48 708 }
leothedragon 0:8f0bb79ddd48 709 }
leothedragon 0:8f0bb79ddd48 710 } else if (M2MTimerObserver::RetryTimer == type) {
leothedragon 0:8f0bb79ddd48 711 tr_debug("M2MInterfaceImpl::timer_expired() - retry");
leothedragon 0:8f0bb79ddd48 712 _retry_timer_expired = true;
leothedragon 0:8f0bb79ddd48 713 if (_bootstrapped) {
leothedragon 0:8f0bb79ddd48 714 internal_event(STATE_REGISTER);
leothedragon 0:8f0bb79ddd48 715 } else {
leothedragon 0:8f0bb79ddd48 716 internal_event(STATE_BOOTSTRAP);
leothedragon 0:8f0bb79ddd48 717 }
leothedragon 0:8f0bb79ddd48 718 } else if (M2MTimerObserver::BootstrapFlowTimer == type) {
leothedragon 0:8f0bb79ddd48 719 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 720 tr_debug("M2MInterfaceImpl::timer_expired() - bootstrap");
leothedragon 0:8f0bb79ddd48 721 _bootstrapped = false;
leothedragon 0:8f0bb79ddd48 722 if (_registration_flow_timer) {
leothedragon 0:8f0bb79ddd48 723 _registration_flow_timer->stop_timer();
leothedragon 0:8f0bb79ddd48 724 }
leothedragon 0:8f0bb79ddd48 725 bootstrap_error(ERROR_REASON_23);
leothedragon 0:8f0bb79ddd48 726 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 727 } else if (M2MTimerObserver::RegistrationFlowTimer == type) {
leothedragon 0:8f0bb79ddd48 728 tr_debug("M2MInterfaceImpl::timer_expired() - register");
leothedragon 0:8f0bb79ddd48 729 registration_error(M2MInterface::Timeout, true);
leothedragon 0:8f0bb79ddd48 730 }
leothedragon 0:8f0bb79ddd48 731 }
leothedragon 0:8f0bb79ddd48 732
leothedragon 0:8f0bb79ddd48 733 // state machine sits here.
leothedragon 0:8f0bb79ddd48 734 void M2MInterfaceImpl::state_idle(EventData* /*data*/)
leothedragon 0:8f0bb79ddd48 735 {
leothedragon 0:8f0bb79ddd48 736 tr_debug("M2MInterfaceImpl::state_idle");
leothedragon 0:8f0bb79ddd48 737 _nsdl_interface.stop_timers();
leothedragon 0:8f0bb79ddd48 738 _nsdl_interface.clear_sent_blockwise_messages();
leothedragon 0:8f0bb79ddd48 739 _nsdl_interface.clear_received_blockwise_messages();
leothedragon 0:8f0bb79ddd48 740 _queue_sleep_timer.stop_timer();
leothedragon 0:8f0bb79ddd48 741 }
leothedragon 0:8f0bb79ddd48 742
leothedragon 0:8f0bb79ddd48 743 void M2MInterfaceImpl::state_bootstrap(EventData *data)
leothedragon 0:8f0bb79ddd48 744 {
leothedragon 0:8f0bb79ddd48 745 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 746 tr_debug("M2MInterfaceImpl::state_bootstrap");
leothedragon 0:8f0bb79ddd48 747 // Start with bootstrapping preparation
leothedragon 0:8f0bb79ddd48 748 _bootstrapped = false;
leothedragon 0:8f0bb79ddd48 749 _bootstrap_finished = false;
leothedragon 0:8f0bb79ddd48 750 _nsdl_interface.set_registration_status(false);
leothedragon 0:8f0bb79ddd48 751 M2MSecurityData *event = static_cast<M2MSecurityData *> (data);
leothedragon 0:8f0bb79ddd48 752 if(!_security) {
leothedragon 0:8f0bb79ddd48 753 M2MInterface::Error error = M2MInterface::InvalidParameters;
leothedragon 0:8f0bb79ddd48 754 if (event) {
leothedragon 0:8f0bb79ddd48 755 _security = event->_object;
leothedragon 0:8f0bb79ddd48 756 if(_security) {
leothedragon 0:8f0bb79ddd48 757 int32_t bs_id = _security->get_security_instance_id(M2MSecurity::Bootstrap);
leothedragon 0:8f0bb79ddd48 758 tr_info("M2MInterfaceImpl::state_bootstrap - bs_id = %" PRId32 "(s)", bs_id);
leothedragon 0:8f0bb79ddd48 759 if(bs_id >= 0) {
leothedragon 0:8f0bb79ddd48 760 String server_address = _security->resource_value_string(M2MSecurity::M2MServerUri, bs_id);
leothedragon 0:8f0bb79ddd48 761 _nsdl_interface.set_server_address(server_address.c_str());
leothedragon 0:8f0bb79ddd48 762 tr_info("M2MInterfaceImpl::state_bootstrap - server_address %s", server_address.c_str());
leothedragon 0:8f0bb79ddd48 763 String coap;
leothedragon 0:8f0bb79ddd48 764 if(server_address.compare(0,sizeof(COAP)-1,COAP) == 0) {
leothedragon 0:8f0bb79ddd48 765 coap = COAP;
leothedragon 0:8f0bb79ddd48 766 }
leothedragon 0:8f0bb79ddd48 767 else if(server_address.compare(0,sizeof(COAPS)-1,COAPS) == 0) {
leothedragon 0:8f0bb79ddd48 768 _security->resource_value_int(M2MSecurity::SecurityMode, bs_id) != M2MSecurity::NoSecurity ? coap = COAPS: coap = "";
leothedragon 0:8f0bb79ddd48 769 }
leothedragon 0:8f0bb79ddd48 770 if(!coap.empty()) {
leothedragon 0:8f0bb79ddd48 771 server_address = server_address.substr(coap.size(),
leothedragon 0:8f0bb79ddd48 772 server_address.size()-coap.size());
leothedragon 0:8f0bb79ddd48 773
leothedragon 0:8f0bb79ddd48 774 process_address(server_address, _server_ip_address, _server_port);
leothedragon 0:8f0bb79ddd48 775
leothedragon 0:8f0bb79ddd48 776 tr_info("M2MInterfaceImpl::state_bootstrap - IP address %s, Port %d", _server_ip_address.c_str(), _server_port);
leothedragon 0:8f0bb79ddd48 777 // If bind and resolving server address succeed then proceed else
leothedragon 0:8f0bb79ddd48 778 // return error to the application and go to Idle state.
leothedragon 0:8f0bb79ddd48 779 if(!_server_ip_address.empty()) {
leothedragon 0:8f0bb79ddd48 780 error = M2MInterface::ErrorNone;
leothedragon 0:8f0bb79ddd48 781 if (_registration_flow_timer) {
leothedragon 0:8f0bb79ddd48 782 _registration_flow_timer->stop_timer();
leothedragon 0:8f0bb79ddd48 783 _registration_flow_timer->start_timer(MBED_CLIENT_RECONNECTION_COUNT * MBED_CLIENT_RECONNECTION_INTERVAL * 8 * 1000,
leothedragon 0:8f0bb79ddd48 784 M2MTimerObserver::BootstrapFlowTimer);
leothedragon 0:8f0bb79ddd48 785 }
leothedragon 0:8f0bb79ddd48 786 _connection_handler.resolve_server_address(_server_ip_address,
leothedragon 0:8f0bb79ddd48 787 _server_port,
leothedragon 0:8f0bb79ddd48 788 M2MConnectionObserver::Bootstrap,
leothedragon 0:8f0bb79ddd48 789 _security);
leothedragon 0:8f0bb79ddd48 790 }
leothedragon 0:8f0bb79ddd48 791 }
leothedragon 0:8f0bb79ddd48 792 }
leothedragon 0:8f0bb79ddd48 793 }
leothedragon 0:8f0bb79ddd48 794 }
leothedragon 0:8f0bb79ddd48 795 if (error != M2MInterface::ErrorNone) {
leothedragon 0:8f0bb79ddd48 796 tr_error("M2MInterfaceImpl::state_bootstrap - set error as M2MInterface::InvalidParameters");
leothedragon 0:8f0bb79ddd48 797 internal_event(STATE_IDLE);
leothedragon 0:8f0bb79ddd48 798 set_error_description(ERROR_REASON_11);
leothedragon 0:8f0bb79ddd48 799 _observer.error(error);
leothedragon 0:8f0bb79ddd48 800 }
leothedragon 0:8f0bb79ddd48 801 } else {
leothedragon 0:8f0bb79ddd48 802 _listen_port = 0;
leothedragon 0:8f0bb79ddd48 803 _connection_handler.bind_connection(_listen_port);
leothedragon 0:8f0bb79ddd48 804 if (_registration_flow_timer) {
leothedragon 0:8f0bb79ddd48 805 _registration_flow_timer->stop_timer();
leothedragon 0:8f0bb79ddd48 806 _registration_flow_timer->start_timer(MBED_CLIENT_RECONNECTION_COUNT * MBED_CLIENT_RECONNECTION_INTERVAL * 8 * 1000,
leothedragon 0:8f0bb79ddd48 807 M2MTimerObserver::BootstrapFlowTimer);
leothedragon 0:8f0bb79ddd48 808 }
leothedragon 0:8f0bb79ddd48 809 tr_info("M2MInterfaceImpl::state_bootstrap (reconnect) - IP address %s, Port %d", _server_ip_address.c_str(), _server_port);
leothedragon 0:8f0bb79ddd48 810 _connection_handler.resolve_server_address(_server_ip_address,
leothedragon 0:8f0bb79ddd48 811 _server_port,
leothedragon 0:8f0bb79ddd48 812 M2MConnectionObserver::Bootstrap,
leothedragon 0:8f0bb79ddd48 813 _security);
leothedragon 0:8f0bb79ddd48 814 }
leothedragon 0:8f0bb79ddd48 815 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 816 }
leothedragon 0:8f0bb79ddd48 817
leothedragon 0:8f0bb79ddd48 818 void M2MInterfaceImpl::state_bootstrap_address_resolved( EventData *data)
leothedragon 0:8f0bb79ddd48 819 {
leothedragon 0:8f0bb79ddd48 820 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 821 tr_debug("M2MInterfaceImpl::state_bootstrap_address_resolved");
leothedragon 0:8f0bb79ddd48 822 if (data) {
leothedragon 0:8f0bb79ddd48 823 ResolvedAddressData *event = static_cast<ResolvedAddressData *> (data);
leothedragon 0:8f0bb79ddd48 824 sn_nsdl_addr_s address;
leothedragon 0:8f0bb79ddd48 825
leothedragon 0:8f0bb79ddd48 826 M2MInterface::NetworkStack stack = event->_address->_stack;
leothedragon 0:8f0bb79ddd48 827
leothedragon 0:8f0bb79ddd48 828 if(M2MInterface::LwIP_IPv4 == stack) {
leothedragon 0:8f0bb79ddd48 829 tr_info("M2MInterfaceImpl::state_bootstrap_address_resolved : IPv4 address");
leothedragon 0:8f0bb79ddd48 830 address.type = SN_NSDL_ADDRESS_TYPE_IPV4;
leothedragon 0:8f0bb79ddd48 831 } else if((M2MInterface::LwIP_IPv6 == stack) ||
leothedragon 0:8f0bb79ddd48 832 (M2MInterface::Nanostack_IPv6 == stack)) {
leothedragon 0:8f0bb79ddd48 833 tr_info("M2MInterfaceImpl::state_bootstrap_address_resolved : IPv6 address");
leothedragon 0:8f0bb79ddd48 834 address.type = SN_NSDL_ADDRESS_TYPE_IPV6;
leothedragon 0:8f0bb79ddd48 835 }
leothedragon 0:8f0bb79ddd48 836 address.port = event->_port;
leothedragon 0:8f0bb79ddd48 837 address.addr_ptr = (uint8_t*)event->_address->_address;
leothedragon 0:8f0bb79ddd48 838 address.addr_len = event->_address->_length;
leothedragon 0:8f0bb79ddd48 839 _connection_handler.start_listening_for_data();
leothedragon 0:8f0bb79ddd48 840
leothedragon 0:8f0bb79ddd48 841 if(_nsdl_interface.create_bootstrap_resource(&address)) {
leothedragon 0:8f0bb79ddd48 842 internal_event(STATE_BOOTSTRAP_RESOURCE_CREATED);
leothedragon 0:8f0bb79ddd48 843 } else{
leothedragon 0:8f0bb79ddd48 844 // If resource creation fails then inform error to application
leothedragon 0:8f0bb79ddd48 845 tr_error("M2MInterfaceImpl::state_bootstrap_address_resolved : M2MInterface::InvalidParameters");
leothedragon 0:8f0bb79ddd48 846 internal_event(STATE_IDLE);
leothedragon 0:8f0bb79ddd48 847 set_error_description(ERROR_REASON_12);
leothedragon 0:8f0bb79ddd48 848 _observer.error(M2MInterface::InvalidParameters);
leothedragon 0:8f0bb79ddd48 849 }
leothedragon 0:8f0bb79ddd48 850 }
leothedragon 0:8f0bb79ddd48 851 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 852 }
leothedragon 0:8f0bb79ddd48 853
leothedragon 0:8f0bb79ddd48 854 void M2MInterfaceImpl::state_bootstrap_resource_created( EventData */*data*/)
leothedragon 0:8f0bb79ddd48 855 {
leothedragon 0:8f0bb79ddd48 856 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 857 tr_debug("M2MInterfaceImpl::state_bootstrap_resource_created");
leothedragon 0:8f0bb79ddd48 858 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 859 }
leothedragon 0:8f0bb79ddd48 860
leothedragon 0:8f0bb79ddd48 861 void M2MInterfaceImpl::state_bootstrapped( EventData */*data*/)
leothedragon 0:8f0bb79ddd48 862 {
leothedragon 0:8f0bb79ddd48 863 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 864 tr_debug("M2MInterfaceImpl::state_bootstrapped");
leothedragon 0:8f0bb79ddd48 865 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 866 }
leothedragon 0:8f0bb79ddd48 867
leothedragon 0:8f0bb79ddd48 868 void M2MInterfaceImpl::state_register(EventData *data)
leothedragon 0:8f0bb79ddd48 869 {
leothedragon 0:8f0bb79ddd48 870 tr_debug("M2MInterfaceImpl::state_register");
leothedragon 0:8f0bb79ddd48 871 _nsdl_interface.set_registration_status(false);
leothedragon 0:8f0bb79ddd48 872 M2MRegisterData *event = static_cast<M2MRegisterData *> (data);
leothedragon 0:8f0bb79ddd48 873 if (!_security) {
leothedragon 0:8f0bb79ddd48 874 M2MInterface::Error error = M2MInterface::InvalidParameters;
leothedragon 0:8f0bb79ddd48 875 // Start with registration preparation
leothedragon 0:8f0bb79ddd48 876 if(event) {
leothedragon 0:8f0bb79ddd48 877 _security = event->_object;
leothedragon 0:8f0bb79ddd48 878 if(_security) {
leothedragon 0:8f0bb79ddd48 879 int32_t m2m_id = _security->get_security_instance_id(M2MSecurity::M2MServer);
leothedragon 0:8f0bb79ddd48 880 if(m2m_id >= 0) {
leothedragon 0:8f0bb79ddd48 881 if(_nsdl_interface.create_nsdl_list_structure(event->_base_list)) {
leothedragon 0:8f0bb79ddd48 882 // If the nsdl resource structure is created successfully
leothedragon 0:8f0bb79ddd48 883 String server_address = _security->resource_value_string(M2MSecurity::M2MServerUri, m2m_id);
leothedragon 0:8f0bb79ddd48 884 _nsdl_interface.set_server_address(server_address.c_str());
leothedragon 0:8f0bb79ddd48 885 tr_info("M2MInterfaceImpl::state_register - server_address %s", server_address.c_str());
leothedragon 0:8f0bb79ddd48 886 String coap;
leothedragon 0:8f0bb79ddd48 887 if(server_address.compare(0,sizeof(COAP)-1,COAP) == 0) {
leothedragon 0:8f0bb79ddd48 888 coap = COAP;
leothedragon 0:8f0bb79ddd48 889 }
leothedragon 0:8f0bb79ddd48 890 else if(server_address.compare(0,sizeof(COAPS)-1,COAPS) == 0) {
leothedragon 0:8f0bb79ddd48 891 _security->resource_value_int(M2MSecurity::SecurityMode, m2m_id) != M2MSecurity::NoSecurity ? coap = COAPS: coap = "";
leothedragon 0:8f0bb79ddd48 892 }
leothedragon 0:8f0bb79ddd48 893 if(!coap.empty()) {
leothedragon 0:8f0bb79ddd48 894 server_address = server_address.substr(coap.size(),
leothedragon 0:8f0bb79ddd48 895 server_address.size() - coap.size());
leothedragon 0:8f0bb79ddd48 896 process_address(server_address, _server_ip_address, _server_port);
leothedragon 0:8f0bb79ddd48 897
leothedragon 0:8f0bb79ddd48 898 tr_info("M2MInterfaceImpl::state_register - IP address %s, Port %d", _server_ip_address.c_str(), _server_port);
leothedragon 0:8f0bb79ddd48 899 if(!_server_ip_address.empty()) {
leothedragon 0:8f0bb79ddd48 900 // Connection related errors are coming through callback
leothedragon 0:8f0bb79ddd48 901 if (_registration_flow_timer) {
leothedragon 0:8f0bb79ddd48 902 _registration_flow_timer->stop_timer();
leothedragon 0:8f0bb79ddd48 903 _registration_flow_timer->start_timer(MBED_CLIENT_RECONNECTION_COUNT * MBED_CLIENT_RECONNECTION_INTERVAL * 8 * 1000,
leothedragon 0:8f0bb79ddd48 904 M2MTimerObserver::RegistrationFlowTimer);
leothedragon 0:8f0bb79ddd48 905 }
leothedragon 0:8f0bb79ddd48 906 error = M2MInterface::ErrorNone;
leothedragon 0:8f0bb79ddd48 907 _connection_handler.resolve_server_address(_server_ip_address,_server_port,
leothedragon 0:8f0bb79ddd48 908 M2MConnectionObserver::LWM2MServer,
leothedragon 0:8f0bb79ddd48 909 _security);
leothedragon 0:8f0bb79ddd48 910 }
leothedragon 0:8f0bb79ddd48 911 }
leothedragon 0:8f0bb79ddd48 912 } else {
leothedragon 0:8f0bb79ddd48 913 tr_error("M2MInterfaceImpl::state_register - fail to create nsdl list structure!");
leothedragon 0:8f0bb79ddd48 914 }
leothedragon 0:8f0bb79ddd48 915 }
leothedragon 0:8f0bb79ddd48 916 }
leothedragon 0:8f0bb79ddd48 917 }
leothedragon 0:8f0bb79ddd48 918 if (error != M2MInterface::ErrorNone) {
leothedragon 0:8f0bb79ddd48 919 tr_error("M2MInterfaceImpl::state_register - set error as M2MInterface::InvalidParameters");
leothedragon 0:8f0bb79ddd48 920 internal_event(STATE_IDLE);
leothedragon 0:8f0bb79ddd48 921 set_error_description(ERROR_REASON_13);
leothedragon 0:8f0bb79ddd48 922 _observer.error(error);
leothedragon 0:8f0bb79ddd48 923 }
leothedragon 0:8f0bb79ddd48 924 } else {
leothedragon 0:8f0bb79ddd48 925 _listen_port = 0;
leothedragon 0:8f0bb79ddd48 926 if (event) {
leothedragon 0:8f0bb79ddd48 927 _nsdl_interface.create_nsdl_list_structure(event->_base_list);
leothedragon 0:8f0bb79ddd48 928 }
leothedragon 0:8f0bb79ddd48 929 _connection_handler.bind_connection(_listen_port);
leothedragon 0:8f0bb79ddd48 930 if (_registration_flow_timer) {
leothedragon 0:8f0bb79ddd48 931 _registration_flow_timer->stop_timer();
leothedragon 0:8f0bb79ddd48 932 _registration_flow_timer->start_timer(MBED_CLIENT_RECONNECTION_COUNT * MBED_CLIENT_RECONNECTION_INTERVAL * 8 * 1000,
leothedragon 0:8f0bb79ddd48 933 M2MTimerObserver::RegistrationFlowTimer);
leothedragon 0:8f0bb79ddd48 934 }
leothedragon 0:8f0bb79ddd48 935 tr_info("M2MInterfaceImpl::state_register (reconnect) - IP address %s, Port %d", _server_ip_address.c_str(), _server_port);
leothedragon 0:8f0bb79ddd48 936 _connection_handler.resolve_server_address(_server_ip_address,_server_port,
leothedragon 0:8f0bb79ddd48 937 M2MConnectionObserver::LWM2MServer,
leothedragon 0:8f0bb79ddd48 938 _security);
leothedragon 0:8f0bb79ddd48 939 }
leothedragon 0:8f0bb79ddd48 940 }
leothedragon 0:8f0bb79ddd48 941
leothedragon 0:8f0bb79ddd48 942 void M2MInterfaceImpl::process_address(const String& server_address, String& ip_address, uint16_t& port) {
leothedragon 0:8f0bb79ddd48 943
leothedragon 0:8f0bb79ddd48 944 int colonFound = server_address.find_last_of(':'); //10
leothedragon 0:8f0bb79ddd48 945 if(colonFound != -1) {
leothedragon 0:8f0bb79ddd48 946 ip_address = server_address.substr(0,colonFound);
leothedragon 0:8f0bb79ddd48 947 port = atoi(server_address.substr(colonFound+1,
leothedragon 0:8f0bb79ddd48 948 server_address.size()-ip_address.size()).c_str());
leothedragon 0:8f0bb79ddd48 949 colonFound = ip_address.find_last_of(']');
leothedragon 0:8f0bb79ddd48 950 if(ip_address.compare(0,1,"[") == 0) {
leothedragon 0:8f0bb79ddd48 951 if(colonFound == -1) {
leothedragon 0:8f0bb79ddd48 952 ip_address.clear();
leothedragon 0:8f0bb79ddd48 953 } else {
leothedragon 0:8f0bb79ddd48 954 ip_address = ip_address.substr(1,colonFound-1);
leothedragon 0:8f0bb79ddd48 955 }
leothedragon 0:8f0bb79ddd48 956 } else if(colonFound != -1) {
leothedragon 0:8f0bb79ddd48 957 ip_address.clear();
leothedragon 0:8f0bb79ddd48 958 }
leothedragon 0:8f0bb79ddd48 959 }
leothedragon 0:8f0bb79ddd48 960 }
leothedragon 0:8f0bb79ddd48 961
leothedragon 0:8f0bb79ddd48 962 void M2MInterfaceImpl::state_register_address_resolved( EventData *data)
leothedragon 0:8f0bb79ddd48 963 {
leothedragon 0:8f0bb79ddd48 964 tr_debug("M2MInterfaceImpl::state_register_address_resolved - reconnect status %d", _reconnection_state);
leothedragon 0:8f0bb79ddd48 965 if(data) {
leothedragon 0:8f0bb79ddd48 966 ResolvedAddressData *event = static_cast<ResolvedAddressData *> (data);
leothedragon 0:8f0bb79ddd48 967
leothedragon 0:8f0bb79ddd48 968 sn_nsdl_addr_type_e address_type = SN_NSDL_ADDRESS_TYPE_IPV6;
leothedragon 0:8f0bb79ddd48 969
leothedragon 0:8f0bb79ddd48 970 M2MInterface::NetworkStack stack = event->_address->_stack;
leothedragon 0:8f0bb79ddd48 971
leothedragon 0:8f0bb79ddd48 972 if(M2MInterface::LwIP_IPv4 == stack) {
leothedragon 0:8f0bb79ddd48 973 tr_info("M2MInterfaceImpl::state_register_address_resolved : IPv4 address");
leothedragon 0:8f0bb79ddd48 974 address_type = SN_NSDL_ADDRESS_TYPE_IPV4;
leothedragon 0:8f0bb79ddd48 975 } else if((M2MInterface::LwIP_IPv6 == stack) ||
leothedragon 0:8f0bb79ddd48 976 (M2MInterface::Nanostack_IPv6 == stack)) {
leothedragon 0:8f0bb79ddd48 977 tr_info("M2MInterfaceImpl::state_register_address_resolved : IPv6 address");
leothedragon 0:8f0bb79ddd48 978 address_type = SN_NSDL_ADDRESS_TYPE_IPV6;
leothedragon 0:8f0bb79ddd48 979 }
leothedragon 0:8f0bb79ddd48 980 _connection_handler.start_listening_for_data();
leothedragon 0:8f0bb79ddd48 981 _nsdl_interface.set_server_address((uint8_t*)event->_address->_address,event->_address->_length,
leothedragon 0:8f0bb79ddd48 982 event->_port, address_type);
leothedragon 0:8f0bb79ddd48 983 switch (_reconnection_state) {
leothedragon 0:8f0bb79ddd48 984 case M2MInterfaceImpl::None:
leothedragon 0:8f0bb79ddd48 985 if (!_nsdl_interface.send_register_message()) {
leothedragon 0:8f0bb79ddd48 986 // If resource creation fails then inform error to application
leothedragon 0:8f0bb79ddd48 987 tr_error("M2MInterfaceImpl::state_register_address_resolved : M2MInterface::MemoryFail");
leothedragon 0:8f0bb79ddd48 988 internal_event(STATE_IDLE);
leothedragon 0:8f0bb79ddd48 989 set_error_description(ERROR_REASON_25);
leothedragon 0:8f0bb79ddd48 990 _observer.error(M2MInterface::MemoryFail);
leothedragon 0:8f0bb79ddd48 991 }
leothedragon 0:8f0bb79ddd48 992 break;
leothedragon 0:8f0bb79ddd48 993 case M2MInterfaceImpl::Unregistration:
leothedragon 0:8f0bb79ddd48 994 case M2MInterfaceImpl::WithUpdate:
leothedragon 0:8f0bb79ddd48 995 // Start registration update in case it is reconnection logic because of network issue.
leothedragon 0:8f0bb79ddd48 996 internal_event(STATE_UPDATE_REGISTRATION);
leothedragon 0:8f0bb79ddd48 997 break;
leothedragon 0:8f0bb79ddd48 998 }
leothedragon 0:8f0bb79ddd48 999 }
leothedragon 0:8f0bb79ddd48 1000 }
leothedragon 0:8f0bb79ddd48 1001
leothedragon 0:8f0bb79ddd48 1002 void M2MInterfaceImpl::state_registered( EventData */*data*/)
leothedragon 0:8f0bb79ddd48 1003 {
leothedragon 0:8f0bb79ddd48 1004 tr_info("M2MInterfaceImpl::state_registered");
leothedragon 0:8f0bb79ddd48 1005 if (_registration_flow_timer) {
leothedragon 0:8f0bb79ddd48 1006 _registration_flow_timer->stop_timer();
leothedragon 0:8f0bb79ddd48 1007 }
leothedragon 0:8f0bb79ddd48 1008 _reconnection_time = _initial_reconnection_time;
leothedragon 0:8f0bb79ddd48 1009 _reconnecting = false;
leothedragon 0:8f0bb79ddd48 1010 _nsdl_interface.set_registration_status(true);
leothedragon 0:8f0bb79ddd48 1011 // Continue with the unregistration process if it has failed due to connection lost
leothedragon 0:8f0bb79ddd48 1012 if (_reconnection_state == M2MInterfaceImpl::Unregistration) {
leothedragon 0:8f0bb79ddd48 1013 internal_event(STATE_UNREGISTER);
leothedragon 0:8f0bb79ddd48 1014 } else {
leothedragon 0:8f0bb79ddd48 1015 _reconnection_state = M2MInterfaceImpl::WithUpdate;
leothedragon 0:8f0bb79ddd48 1016 }
leothedragon 0:8f0bb79ddd48 1017 }
leothedragon 0:8f0bb79ddd48 1018
leothedragon 0:8f0bb79ddd48 1019 void M2MInterfaceImpl::state_update_registration(EventData *data)
leothedragon 0:8f0bb79ddd48 1020 {
leothedragon 0:8f0bb79ddd48 1021 tr_debug("M2MInterfaceImpl::state_update_registration");
leothedragon 0:8f0bb79ddd48 1022 uint32_t lifetime = 0;
leothedragon 0:8f0bb79ddd48 1023 // Set to false to allow reconnection to work.
leothedragon 0:8f0bb79ddd48 1024 _queue_mode_timer_ongoing = false;
leothedragon 0:8f0bb79ddd48 1025
leothedragon 0:8f0bb79ddd48 1026 if (data) {
leothedragon 0:8f0bb79ddd48 1027 M2MUpdateRegisterData *event = static_cast<M2MUpdateRegisterData *> (data);
leothedragon 0:8f0bb79ddd48 1028 // Create new resources if any
leothedragon 0:8f0bb79ddd48 1029 if (!event->_base_list.empty()) {
leothedragon 0:8f0bb79ddd48 1030 _nsdl_interface.create_nsdl_list_structure(event->_base_list);
leothedragon 0:8f0bb79ddd48 1031 }
leothedragon 0:8f0bb79ddd48 1032 lifetime = event->_lifetime;
leothedragon 0:8f0bb79ddd48 1033 }
leothedragon 0:8f0bb79ddd48 1034
leothedragon 0:8f0bb79ddd48 1035 bool success = _nsdl_interface.send_update_registration(lifetime);
leothedragon 0:8f0bb79ddd48 1036 if (!success) {
leothedragon 0:8f0bb79ddd48 1037 tr_error("M2MInterfaceImpl::state_update_registration : M2MInterface::MemoryFail");
leothedragon 0:8f0bb79ddd48 1038 internal_event(STATE_IDLE);
leothedragon 0:8f0bb79ddd48 1039 set_error_description(ERROR_REASON_25);
leothedragon 0:8f0bb79ddd48 1040 _observer.error(M2MInterface::MemoryFail);
leothedragon 0:8f0bb79ddd48 1041 }
leothedragon 0:8f0bb79ddd48 1042 }
leothedragon 0:8f0bb79ddd48 1043
leothedragon 0:8f0bb79ddd48 1044 void M2MInterfaceImpl::state_unregister(EventData */*data*/)
leothedragon 0:8f0bb79ddd48 1045 {
leothedragon 0:8f0bb79ddd48 1046 tr_debug("M2MInterfaceImpl::state_unregister");
leothedragon 0:8f0bb79ddd48 1047 internal_event(STATE_SENDING_COAP_DATA);
leothedragon 0:8f0bb79ddd48 1048 if(!_nsdl_interface.send_unregister_message()) {
leothedragon 0:8f0bb79ddd48 1049 tr_error("M2MInterfaceImpl::state_unregister : M2MInterface::NotRegistered");
leothedragon 0:8f0bb79ddd48 1050 internal_event(STATE_IDLE);
leothedragon 0:8f0bb79ddd48 1051 set_error_description(ERROR_REASON_16);
leothedragon 0:8f0bb79ddd48 1052 _observer.error(M2MInterface::NotRegistered);
leothedragon 0:8f0bb79ddd48 1053 }
leothedragon 0:8f0bb79ddd48 1054 }
leothedragon 0:8f0bb79ddd48 1055
leothedragon 0:8f0bb79ddd48 1056 void M2MInterfaceImpl::state_unregistered( EventData */*data*/)
leothedragon 0:8f0bb79ddd48 1057 {
leothedragon 0:8f0bb79ddd48 1058 tr_info("M2MInterfaceImpl::state_unregistered");
leothedragon 0:8f0bb79ddd48 1059 _reconnection_time = _initial_reconnection_time;
leothedragon 0:8f0bb79ddd48 1060 _connection_handler.force_close();
leothedragon 0:8f0bb79ddd48 1061 _security = NULL;
leothedragon 0:8f0bb79ddd48 1062 _observer.object_unregistered(_register_server);
leothedragon 0:8f0bb79ddd48 1063 internal_event(STATE_IDLE);
leothedragon 0:8f0bb79ddd48 1064 }
leothedragon 0:8f0bb79ddd48 1065
leothedragon 0:8f0bb79ddd48 1066 void M2MInterfaceImpl::state_sending_coap_data( EventData */*data*/)
leothedragon 0:8f0bb79ddd48 1067 {
leothedragon 0:8f0bb79ddd48 1068 tr_debug("M2MInterfaceImpl::state_sending_coap_data");
leothedragon 0:8f0bb79ddd48 1069 _nsdl_interface.start_nsdl_execution_timer();
leothedragon 0:8f0bb79ddd48 1070 internal_event(STATE_WAITING);
leothedragon 0:8f0bb79ddd48 1071 }
leothedragon 0:8f0bb79ddd48 1072
leothedragon 0:8f0bb79ddd48 1073 void M2MInterfaceImpl::state_coap_data_sent( EventData */*data*/)
leothedragon 0:8f0bb79ddd48 1074 {
leothedragon 0:8f0bb79ddd48 1075 tr_debug("M2MInterfaceImpl::state_coap_data_sent");
leothedragon 0:8f0bb79ddd48 1076 internal_event(STATE_WAITING);
leothedragon 0:8f0bb79ddd48 1077 }
leothedragon 0:8f0bb79ddd48 1078
leothedragon 0:8f0bb79ddd48 1079 void M2MInterfaceImpl::state_coap_data_received(EventData *data)
leothedragon 0:8f0bb79ddd48 1080 {
leothedragon 0:8f0bb79ddd48 1081 tr_debug("M2MInterfaceImpl::state_coap_data_received");
leothedragon 0:8f0bb79ddd48 1082 if(data) {
leothedragon 0:8f0bb79ddd48 1083 ReceivedData *event = static_cast<ReceivedData*> (data);
leothedragon 0:8f0bb79ddd48 1084 sn_nsdl_addr_s address;
leothedragon 0:8f0bb79ddd48 1085
leothedragon 0:8f0bb79ddd48 1086 M2MInterface::NetworkStack stack = event->_address->_stack;
leothedragon 0:8f0bb79ddd48 1087
leothedragon 0:8f0bb79ddd48 1088 if(M2MInterface::LwIP_IPv4 == stack) {
leothedragon 0:8f0bb79ddd48 1089 address.type = SN_NSDL_ADDRESS_TYPE_IPV4;
leothedragon 0:8f0bb79ddd48 1090 address.addr_len = 4;
leothedragon 0:8f0bb79ddd48 1091 } else if((M2MInterface::LwIP_IPv6 == stack) ||
leothedragon 0:8f0bb79ddd48 1092 (M2MInterface::Nanostack_IPv6 == stack)) {
leothedragon 0:8f0bb79ddd48 1093 address.type = SN_NSDL_ADDRESS_TYPE_IPV6;
leothedragon 0:8f0bb79ddd48 1094 address.addr_len = 16;
leothedragon 0:8f0bb79ddd48 1095 }
leothedragon 0:8f0bb79ddd48 1096 address.port = event->_address->_port;
leothedragon 0:8f0bb79ddd48 1097 address.addr_ptr = (uint8_t*)event->_address->_address;
leothedragon 0:8f0bb79ddd48 1098 address.addr_len = event->_address->_length;
leothedragon 0:8f0bb79ddd48 1099
leothedragon 0:8f0bb79ddd48 1100 // Process received data
leothedragon 0:8f0bb79ddd48 1101 internal_event(STATE_PROCESSING_COAP_DATA);
leothedragon 0:8f0bb79ddd48 1102 if(!_nsdl_interface.process_received_data(event->_data,
leothedragon 0:8f0bb79ddd48 1103 event->_size,
leothedragon 0:8f0bb79ddd48 1104 &address)) {
leothedragon 0:8f0bb79ddd48 1105 tr_error("M2MInterfaceImpl::state_coap_data_received : M2MInterface::ResponseParseFailed");
leothedragon 0:8f0bb79ddd48 1106 set_error_description(ERROR_REASON_17);
leothedragon 0:8f0bb79ddd48 1107 _observer.error(M2MInterface::ResponseParseFailed);
leothedragon 0:8f0bb79ddd48 1108 }
leothedragon 0:8f0bb79ddd48 1109 }
leothedragon 0:8f0bb79ddd48 1110 }
leothedragon 0:8f0bb79ddd48 1111
leothedragon 0:8f0bb79ddd48 1112 void M2MInterfaceImpl::state_processing_coap_data( EventData */*data*/)
leothedragon 0:8f0bb79ddd48 1113 {
leothedragon 0:8f0bb79ddd48 1114 tr_debug("M2MInterfaceImpl::state_processing_coap_data");
leothedragon 0:8f0bb79ddd48 1115 internal_event(STATE_WAITING);
leothedragon 0:8f0bb79ddd48 1116 }
leothedragon 0:8f0bb79ddd48 1117
leothedragon 0:8f0bb79ddd48 1118 void M2MInterfaceImpl::state_coap_data_processed( EventData */*data*/)
leothedragon 0:8f0bb79ddd48 1119 {
leothedragon 0:8f0bb79ddd48 1120 tr_debug("M2MInterfaceImpl::state_coap_data_processed");
leothedragon 0:8f0bb79ddd48 1121 internal_event(STATE_WAITING);
leothedragon 0:8f0bb79ddd48 1122 }
leothedragon 0:8f0bb79ddd48 1123
leothedragon 0:8f0bb79ddd48 1124 void M2MInterfaceImpl::state_waiting( EventData */*data*/)
leothedragon 0:8f0bb79ddd48 1125 {
leothedragon 0:8f0bb79ddd48 1126 tr_debug("M2MInterfaceImpl::state_waiting");
leothedragon 0:8f0bb79ddd48 1127 }
leothedragon 0:8f0bb79ddd48 1128
leothedragon 0:8f0bb79ddd48 1129 // generates an external event. called once per external event
leothedragon 0:8f0bb79ddd48 1130 // to start the state machine executing
leothedragon 0:8f0bb79ddd48 1131 void M2MInterfaceImpl::external_event(uint8_t new_state,
leothedragon 0:8f0bb79ddd48 1132 EventData* p_data)
leothedragon 0:8f0bb79ddd48 1133 {
leothedragon 0:8f0bb79ddd48 1134 tr_debug("M2MInterfaceImpl::external_event : new state %d", new_state);
leothedragon 0:8f0bb79ddd48 1135 // if we are supposed to ignore this event
leothedragon 0:8f0bb79ddd48 1136 if (new_state == EVENT_IGNORED) {
leothedragon 0:8f0bb79ddd48 1137 tr_debug("M2MInterfaceImpl::external_event : new state is EVENT_IGNORED");
leothedragon 0:8f0bb79ddd48 1138 _event_ignored = true;
leothedragon 0:8f0bb79ddd48 1139 }
leothedragon 0:8f0bb79ddd48 1140 else {
leothedragon 0:8f0bb79ddd48 1141 tr_debug("M2MInterfaceImpl::external_event : handle new state");
leothedragon 0:8f0bb79ddd48 1142 // generate the event and execute the state engine
leothedragon 0:8f0bb79ddd48 1143 internal_event(new_state, p_data);
leothedragon 0:8f0bb79ddd48 1144 }
leothedragon 0:8f0bb79ddd48 1145 }
leothedragon 0:8f0bb79ddd48 1146
leothedragon 0:8f0bb79ddd48 1147 // generates an internal event. called from within a state
leothedragon 0:8f0bb79ddd48 1148 // function to transition to a new state
leothedragon 0:8f0bb79ddd48 1149 void M2MInterfaceImpl::internal_event(uint8_t new_state,
leothedragon 0:8f0bb79ddd48 1150 EventData* p_data)
leothedragon 0:8f0bb79ddd48 1151 {
leothedragon 0:8f0bb79ddd48 1152 tr_debug("M2MInterfaceImpl::internal_event : new state %d", new_state);
leothedragon 0:8f0bb79ddd48 1153 _event_data = p_data;
leothedragon 0:8f0bb79ddd48 1154 _event_generated = true;
leothedragon 0:8f0bb79ddd48 1155 _current_state = new_state;
leothedragon 0:8f0bb79ddd48 1156 state_engine();
leothedragon 0:8f0bb79ddd48 1157 }
leothedragon 0:8f0bb79ddd48 1158
leothedragon 0:8f0bb79ddd48 1159 // the state engine executes the state machine states
leothedragon 0:8f0bb79ddd48 1160 void M2MInterfaceImpl::state_engine (void)
leothedragon 0:8f0bb79ddd48 1161 {
leothedragon 0:8f0bb79ddd48 1162 tr_debug("M2MInterfaceImpl::state_engine");
leothedragon 0:8f0bb79ddd48 1163 EventData* p_data_temp = NULL;
leothedragon 0:8f0bb79ddd48 1164
leothedragon 0:8f0bb79ddd48 1165 // while events are being generated keep executing states
leothedragon 0:8f0bb79ddd48 1166 while (_event_generated) {
leothedragon 0:8f0bb79ddd48 1167 p_data_temp = _event_data; // copy of event data pointer
leothedragon 0:8f0bb79ddd48 1168 _event_data = NULL; // event data used up, reset ptr
leothedragon 0:8f0bb79ddd48 1169 _event_generated = false; // event used up, reset flag
leothedragon 0:8f0bb79ddd48 1170
leothedragon 0:8f0bb79ddd48 1171 assert(_current_state < _max_states);
leothedragon 0:8f0bb79ddd48 1172
leothedragon 0:8f0bb79ddd48 1173 state_function( _current_state, p_data_temp );
leothedragon 0:8f0bb79ddd48 1174 }
leothedragon 0:8f0bb79ddd48 1175 }
leothedragon 0:8f0bb79ddd48 1176
leothedragon 0:8f0bb79ddd48 1177 void M2MInterfaceImpl::state_function( uint8_t current_state, EventData* data )
leothedragon 0:8f0bb79ddd48 1178 {
leothedragon 0:8f0bb79ddd48 1179 switch( current_state ) {
leothedragon 0:8f0bb79ddd48 1180 case STATE_IDLE:
leothedragon 0:8f0bb79ddd48 1181 M2MInterfaceImpl::state_idle(data);
leothedragon 0:8f0bb79ddd48 1182 break;
leothedragon 0:8f0bb79ddd48 1183 case STATE_BOOTSTRAP:
leothedragon 0:8f0bb79ddd48 1184 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 1185 M2MInterfaceImpl::state_bootstrap(data);
leothedragon 0:8f0bb79ddd48 1186 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 1187 break;
leothedragon 0:8f0bb79ddd48 1188 case STATE_BOOTSTRAP_ADDRESS_RESOLVED:
leothedragon 0:8f0bb79ddd48 1189 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 1190 M2MInterfaceImpl::state_bootstrap_address_resolved(data);
leothedragon 0:8f0bb79ddd48 1191 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 1192 break;
leothedragon 0:8f0bb79ddd48 1193 case STATE_BOOTSTRAP_RESOURCE_CREATED:
leothedragon 0:8f0bb79ddd48 1194 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 1195 M2MInterfaceImpl::state_bootstrap_resource_created(data);
leothedragon 0:8f0bb79ddd48 1196 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 1197 break;
leothedragon 0:8f0bb79ddd48 1198 case STATE_BOOTSTRAP_WAIT:
leothedragon 0:8f0bb79ddd48 1199 case STATE_BOOTSTRAP_ERROR_WAIT:
leothedragon 0:8f0bb79ddd48 1200 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 1201 // Do nothing, we're just waiting for data_sent callback
leothedragon 0:8f0bb79ddd48 1202 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 1203 break;
leothedragon 0:8f0bb79ddd48 1204 case STATE_BOOTSTRAPPED:
leothedragon 0:8f0bb79ddd48 1205 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 1206 M2MInterfaceImpl::state_bootstrapped(data);
leothedragon 0:8f0bb79ddd48 1207 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
leothedragon 0:8f0bb79ddd48 1208 break;
leothedragon 0:8f0bb79ddd48 1209 case STATE_REGISTER:
leothedragon 0:8f0bb79ddd48 1210 M2MInterfaceImpl::state_register(data);
leothedragon 0:8f0bb79ddd48 1211 break;
leothedragon 0:8f0bb79ddd48 1212 case STATE_REGISTER_ADDRESS_RESOLVED:
leothedragon 0:8f0bb79ddd48 1213 M2MInterfaceImpl::state_register_address_resolved(data);
leothedragon 0:8f0bb79ddd48 1214 break;
leothedragon 0:8f0bb79ddd48 1215 case STATE_REGISTERED:
leothedragon 0:8f0bb79ddd48 1216 M2MInterfaceImpl::state_registered(data);
leothedragon 0:8f0bb79ddd48 1217 break;
leothedragon 0:8f0bb79ddd48 1218 case STATE_UPDATE_REGISTRATION:
leothedragon 0:8f0bb79ddd48 1219 M2MInterfaceImpl::state_update_registration(data);
leothedragon 0:8f0bb79ddd48 1220 break;
leothedragon 0:8f0bb79ddd48 1221 case STATE_UNREGISTER:
leothedragon 0:8f0bb79ddd48 1222 M2MInterfaceImpl::state_unregister(data);
leothedragon 0:8f0bb79ddd48 1223 break;
leothedragon 0:8f0bb79ddd48 1224 case STATE_UNREGISTERED:
leothedragon 0:8f0bb79ddd48 1225 M2MInterfaceImpl::state_unregistered(data);
leothedragon 0:8f0bb79ddd48 1226 break;
leothedragon 0:8f0bb79ddd48 1227 case STATE_SENDING_COAP_DATA:
leothedragon 0:8f0bb79ddd48 1228 M2MInterfaceImpl::state_sending_coap_data(data);
leothedragon 0:8f0bb79ddd48 1229 break;
leothedragon 0:8f0bb79ddd48 1230 case STATE_COAP_DATA_SENT:
leothedragon 0:8f0bb79ddd48 1231 M2MInterfaceImpl::state_coap_data_sent(data);
leothedragon 0:8f0bb79ddd48 1232 break;
leothedragon 0:8f0bb79ddd48 1233 case STATE_COAP_DATA_RECEIVED:
leothedragon 0:8f0bb79ddd48 1234 M2MInterfaceImpl::state_coap_data_received(data);
leothedragon 0:8f0bb79ddd48 1235 break;
leothedragon 0:8f0bb79ddd48 1236 case STATE_PROCESSING_COAP_DATA:
leothedragon 0:8f0bb79ddd48 1237 M2MInterfaceImpl::state_processing_coap_data(data);
leothedragon 0:8f0bb79ddd48 1238 break;
leothedragon 0:8f0bb79ddd48 1239 case STATE_COAP_DATA_PROCESSED:
leothedragon 0:8f0bb79ddd48 1240 M2MInterfaceImpl::state_coap_data_processed(data);
leothedragon 0:8f0bb79ddd48 1241 break;
leothedragon 0:8f0bb79ddd48 1242 case STATE_WAITING:
leothedragon 0:8f0bb79ddd48 1243 M2MInterfaceImpl::state_waiting(data);
leothedragon 0:8f0bb79ddd48 1244 break;
leothedragon 0:8f0bb79ddd48 1245 }
leothedragon 0:8f0bb79ddd48 1246 }
leothedragon 0:8f0bb79ddd48 1247
leothedragon 0:8f0bb79ddd48 1248 void M2MInterfaceImpl::start_register_update(M2MUpdateRegisterData *data) {
leothedragon 0:8f0bb79ddd48 1249 tr_debug("M2MInterfaceImpl::start_register_update()");
leothedragon 0:8f0bb79ddd48 1250 if(!data || (data->_lifetime != 0 && (data->_lifetime < MINIMUM_REGISTRATION_TIME))) {
leothedragon 0:8f0bb79ddd48 1251 set_error_description(ERROR_REASON_18);
leothedragon 0:8f0bb79ddd48 1252 _observer.error(M2MInterface::InvalidParameters);
leothedragon 0:8f0bb79ddd48 1253 }
leothedragon 0:8f0bb79ddd48 1254
leothedragon 0:8f0bb79ddd48 1255 if (_reconnecting) {
leothedragon 0:8f0bb79ddd48 1256 //If client is in reconnection mode, ignore this call, state machine will reconnect on its own.
leothedragon 0:8f0bb79ddd48 1257 return;
leothedragon 0:8f0bb79ddd48 1258 } else if (_nsdl_interface.is_update_register_ongoing()) {
leothedragon 0:8f0bb79ddd48 1259 set_error_description(ERROR_REASON_27);
leothedragon 0:8f0bb79ddd48 1260 _observer.error(M2MInterface::NotAllowed);
leothedragon 0:8f0bb79ddd48 1261 return;
leothedragon 0:8f0bb79ddd48 1262 }
leothedragon 0:8f0bb79ddd48 1263
leothedragon 0:8f0bb79ddd48 1264 _reconnection_state = M2MInterfaceImpl::WithUpdate;
leothedragon 0:8f0bb79ddd48 1265 BEGIN_TRANSITION_MAP // - Current State -
leothedragon 0:8f0bb79ddd48 1266 TRANSITION_MAP_ENTRY (STATE_REGISTER) // state_idle
leothedragon 0:8f0bb79ddd48 1267 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap
leothedragon 0:8f0bb79ddd48 1268 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state__bootstrap_address_resolved
leothedragon 0:8f0bb79ddd48 1269 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap_resource_created
leothedragon 0:8f0bb79ddd48 1270 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap_wait
leothedragon 0:8f0bb79ddd48 1271 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap_error_wait
leothedragon 0:8f0bb79ddd48 1272 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrapped
leothedragon 0:8f0bb79ddd48 1273 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_register
leothedragon 0:8f0bb79ddd48 1274 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_register_address_resolved
leothedragon 0:8f0bb79ddd48 1275 TRANSITION_MAP_ENTRY (STATE_UPDATE_REGISTRATION) // state_registered
leothedragon 0:8f0bb79ddd48 1276 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_update_registration
leothedragon 0:8f0bb79ddd48 1277 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_unregister
leothedragon 0:8f0bb79ddd48 1278 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_unregistered
leothedragon 0:8f0bb79ddd48 1279 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_sending_coap_data
leothedragon 0:8f0bb79ddd48 1280 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_sent
leothedragon 0:8f0bb79ddd48 1281 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_received
leothedragon 0:8f0bb79ddd48 1282 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_processing_coap_data
leothedragon 0:8f0bb79ddd48 1283 TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_processed
leothedragon 0:8f0bb79ddd48 1284 TRANSITION_MAP_ENTRY (STATE_UPDATE_REGISTRATION) // state_waiting
leothedragon 0:8f0bb79ddd48 1285 END_TRANSITION_MAP(data)
leothedragon 0:8f0bb79ddd48 1286 if(_event_ignored) {
leothedragon 0:8f0bb79ddd48 1287 _event_ignored = false;
leothedragon 0:8f0bb79ddd48 1288 if (!_reconnecting) {
leothedragon 0:8f0bb79ddd48 1289 set_error_description(ERROR_REASON_19);
leothedragon 0:8f0bb79ddd48 1290 _observer.error(M2MInterface::NotAllowed);
leothedragon 0:8f0bb79ddd48 1291 }
leothedragon 0:8f0bb79ddd48 1292 }
leothedragon 0:8f0bb79ddd48 1293 }
leothedragon 0:8f0bb79ddd48 1294
leothedragon 0:8f0bb79ddd48 1295 bool M2MInterfaceImpl::remove_object(M2MBase *object)
leothedragon 0:8f0bb79ddd48 1296 {
leothedragon 0:8f0bb79ddd48 1297 return _nsdl_interface.remove_object_from_list(object);
leothedragon 0:8f0bb79ddd48 1298 }
leothedragon 0:8f0bb79ddd48 1299
leothedragon 0:8f0bb79ddd48 1300 void M2MInterfaceImpl::update_endpoint(const String &name) {
leothedragon 0:8f0bb79ddd48 1301 _nsdl_interface.update_endpoint(name);
leothedragon 0:8f0bb79ddd48 1302 }
leothedragon 0:8f0bb79ddd48 1303
leothedragon 0:8f0bb79ddd48 1304 void M2MInterfaceImpl::update_domain(const String &domain)
leothedragon 0:8f0bb79ddd48 1305 {
leothedragon 0:8f0bb79ddd48 1306 _nsdl_interface.update_domain(domain);
leothedragon 0:8f0bb79ddd48 1307 }
leothedragon 0:8f0bb79ddd48 1308
leothedragon 0:8f0bb79ddd48 1309 const String M2MInterfaceImpl::internal_endpoint_name() const
leothedragon 0:8f0bb79ddd48 1310 {
leothedragon 0:8f0bb79ddd48 1311 return _nsdl_interface.internal_endpoint_name();
leothedragon 0:8f0bb79ddd48 1312 }
leothedragon 0:8f0bb79ddd48 1313
leothedragon 0:8f0bb79ddd48 1314 const char *M2MInterfaceImpl::error_description() const
leothedragon 0:8f0bb79ddd48 1315 {
leothedragon 0:8f0bb79ddd48 1316 #ifndef DISABLE_ERROR_DESCRIPTION
leothedragon 0:8f0bb79ddd48 1317 return _error_description;
leothedragon 0:8f0bb79ddd48 1318 #else
leothedragon 0:8f0bb79ddd48 1319 return "";
leothedragon 0:8f0bb79ddd48 1320 #endif
leothedragon 0:8f0bb79ddd48 1321 }
leothedragon 0:8f0bb79ddd48 1322
leothedragon 0:8f0bb79ddd48 1323 void M2MInterfaceImpl::set_error_description(const char *description)
leothedragon 0:8f0bb79ddd48 1324 {
leothedragon 0:8f0bb79ddd48 1325 #ifndef DISABLE_ERROR_DESCRIPTION
leothedragon 0:8f0bb79ddd48 1326 if (strncmp(_error_description, description, sizeof(_error_description)) != 0) {
leothedragon 0:8f0bb79ddd48 1327 strncpy(_error_description, description, MAX_ALLOWED_ERROR_STRING_LENGTH - 1);
leothedragon 0:8f0bb79ddd48 1328 }
leothedragon 0:8f0bb79ddd48 1329 #endif
leothedragon 0:8f0bb79ddd48 1330 }
leothedragon 0:8f0bb79ddd48 1331
leothedragon 0:8f0bb79ddd48 1332 bool M2MInterfaceImpl::queue_mode() const
leothedragon 0:8f0bb79ddd48 1333 {
leothedragon 0:8f0bb79ddd48 1334 return (_binding_mode == M2MInterface::UDP_QUEUE ||
leothedragon 0:8f0bb79ddd48 1335 _binding_mode == M2MInterface::TCP_QUEUE ||
leothedragon 0:8f0bb79ddd48 1336 _binding_mode == M2MInterface::SMS_QUEUE ||
leothedragon 0:8f0bb79ddd48 1337 _binding_mode == M2MInterface::UDP_SMS_QUEUE);
leothedragon 0:8f0bb79ddd48 1338 }
leothedragon 0:8f0bb79ddd48 1339
leothedragon 0:8f0bb79ddd48 1340 void M2MInterfaceImpl::get_data_request(DownloadType type,
leothedragon 0:8f0bb79ddd48 1341 const char *uri,
leothedragon 0:8f0bb79ddd48 1342 const size_t offset,
leothedragon 0:8f0bb79ddd48 1343 const bool async,
leothedragon 0:8f0bb79ddd48 1344 get_data_cb data_cb,
leothedragon 0:8f0bb79ddd48 1345 get_data_error_cb error_cb,
leothedragon 0:8f0bb79ddd48 1346 void *context)
leothedragon 0:8f0bb79ddd48 1347 {
leothedragon 0:8f0bb79ddd48 1348 get_data_req_error_e error = FAILED_TO_SEND_MSG;
leothedragon 0:8f0bb79ddd48 1349 if (uri) {
leothedragon 0:8f0bb79ddd48 1350 _nsdl_interface.send_request(type, uri, COAP_MSG_CODE_REQUEST_GET, offset, async, 0, 0, NULL, data_cb, error_cb, context);
leothedragon 0:8f0bb79ddd48 1351 } else {
leothedragon 0:8f0bb79ddd48 1352 error_cb(error, context);
leothedragon 0:8f0bb79ddd48 1353 }
leothedragon 0:8f0bb79ddd48 1354 }
leothedragon 0:8f0bb79ddd48 1355
leothedragon 0:8f0bb79ddd48 1356 void M2MInterfaceImpl::post_data_request(const char *uri,
leothedragon 0:8f0bb79ddd48 1357 const bool async,
leothedragon 0:8f0bb79ddd48 1358 const uint16_t payload_len,
leothedragon 0:8f0bb79ddd48 1359 uint8_t *payload_ptr,
leothedragon 0:8f0bb79ddd48 1360 get_data_cb data_cb,
leothedragon 0:8f0bb79ddd48 1361 get_data_error_cb error_cb,
leothedragon 0:8f0bb79ddd48 1362 void *context)
leothedragon 0:8f0bb79ddd48 1363 {
leothedragon 0:8f0bb79ddd48 1364 get_data_req_error_e error = FAILED_TO_SEND_MSG;
leothedragon 0:8f0bb79ddd48 1365 if (uri) {
leothedragon 0:8f0bb79ddd48 1366 _nsdl_interface.send_request(GENERIC_DOWNLOAD, uri, COAP_MSG_CODE_REQUEST_POST, 0, async, 0, payload_len, payload_ptr, data_cb, error_cb, context);
leothedragon 0:8f0bb79ddd48 1367 } else {
leothedragon 0:8f0bb79ddd48 1368 error_cb(error, context);
leothedragon 0:8f0bb79ddd48 1369 }
leothedragon 0:8f0bb79ddd48 1370 }
leothedragon 0:8f0bb79ddd48 1371
leothedragon 0:8f0bb79ddd48 1372 bool M2MInterfaceImpl::set_uri_query_parameters(const char *uri_query_params)
leothedragon 0:8f0bb79ddd48 1373 {
leothedragon 0:8f0bb79ddd48 1374 return _nsdl_interface.set_uri_query_parameters(uri_query_params);
leothedragon 0:8f0bb79ddd48 1375 }
leothedragon 0:8f0bb79ddd48 1376
leothedragon 0:8f0bb79ddd48 1377 void M2MInterfaceImpl::pause()
leothedragon 0:8f0bb79ddd48 1378 {
leothedragon 0:8f0bb79ddd48 1379 tr_debug("M2MInterfaceImpl::pause");
leothedragon 0:8f0bb79ddd48 1380 _connection_handler.claim_mutex();
leothedragon 0:8f0bb79ddd48 1381 _queue_sleep_timer.stop_timer();
leothedragon 0:8f0bb79ddd48 1382 if (_registration_flow_timer) {
leothedragon 0:8f0bb79ddd48 1383 _registration_flow_timer->stop_timer();
leothedragon 0:8f0bb79ddd48 1384 }
leothedragon 0:8f0bb79ddd48 1385 _connection_handler.unregister_network_handler();
leothedragon 0:8f0bb79ddd48 1386 _nsdl_interface.set_request_context_to_be_resend(NULL, 0);
leothedragon 0:8f0bb79ddd48 1387 _connection_handler.stop_listening();
leothedragon 0:8f0bb79ddd48 1388 _retry_timer.stop_timer();
leothedragon 0:8f0bb79ddd48 1389 _reconnecting = false;
leothedragon 0:8f0bb79ddd48 1390 _reconnection_time = _initial_reconnection_time;
leothedragon 0:8f0bb79ddd48 1391 _reconnection_state = M2MInterfaceImpl::WithUpdate;
leothedragon 0:8f0bb79ddd48 1392
leothedragon 0:8f0bb79ddd48 1393 sn_nsdl_clear_coap_resending_queue(_nsdl_interface.get_nsdl_handle());
leothedragon 0:8f0bb79ddd48 1394 internal_event(STATE_IDLE);
leothedragon 0:8f0bb79ddd48 1395 _connection_handler.release_mutex();
leothedragon 0:8f0bb79ddd48 1396 }
leothedragon 0:8f0bb79ddd48 1397
leothedragon 0:8f0bb79ddd48 1398 void M2MInterfaceImpl::resume(void *iface, const M2MBaseList &list)
leothedragon 0:8f0bb79ddd48 1399 {
leothedragon 0:8f0bb79ddd48 1400 tr_debug("M2MInterfaceImpl::resume");
leothedragon 0:8f0bb79ddd48 1401 _connection_handler.set_platform_network_handler(iface);
leothedragon 0:8f0bb79ddd48 1402 register_object(_security, list);
leothedragon 0:8f0bb79ddd48 1403 }