mbed client on ethernet with LWIP

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by sandbox

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

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