Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-client by
Diff: source/m2minterfaceimpl.cpp
- Revision:
- 4:ae5178938864
- Parent:
- 1:79b6cc67d8b4
- Child:
- 5:e36098b177a4
--- a/source/m2minterfaceimpl.cpp Fri Feb 19 17:44:50 2016 +0000 +++ b/source/m2minterfaceimpl.cpp Sat Apr 02 00:31:13 2016 +0300 @@ -23,7 +23,9 @@ #include "mbed-client/m2msecurity.h" #include "mbed-client/m2mconstants.h" #include "mbed-client/m2mtimer.h" -#include "ns_trace.h" +#include "mbed-trace/mbed_trace.h" + +#define TRACE_GROUP "mClt" M2MInterfaceImpl::M2MInterfaceImpl(M2MInterfaceObserver& observer, const String &ep_name, @@ -89,8 +91,10 @@ tr_debug("M2MInterfaceImpl::~M2MInterfaceImpl() - OUT"); } + void M2MInterfaceImpl::bootstrap(M2MSecurity *security) { +#ifndef YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE tr_debug("M2MInterfaceImpl::bootstrap(M2MSecurity *security) - IN"); // Transition to a new state based upon // the current state of the state machine @@ -121,11 +125,14 @@ _observer.error(M2MInterface::NotAllowed); } tr_debug("M2MInterfaceImpl::bootstrap(M2MSecurity *security) - OUT"); +#endif //YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE } void M2MInterfaceImpl::cancel_bootstrap() { +#ifndef YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE //TODO: Do we need this ? +#endif //YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE } void M2MInterfaceImpl::register_object(M2MSecurity *security, const M2MObjectList &object_list) @@ -284,7 +291,7 @@ void M2MInterfaceImpl::registration_error(uint8_t error_code) { - tr_debug("M2MInterfaceImpl::registration_error(uint8_t error_code) %d", error_code); + tr_debug("M2MInterfaceImpl::registration_error code [%d]", error_code); internal_event(STATE_IDLE); _observer.error((M2MInterface::Error)error_code); } @@ -297,18 +304,23 @@ _observer.object_unregistered(_register_server); } + void M2MInterfaceImpl::bootstrap_done(M2MSecurity *security_object) { +#ifndef YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE tr_debug("M2MInterfaceImpl::bootstrap_done(M2MSecurity *security_object)"); internal_event(STATE_BOOTSTRAPPED); _observer.bootstrap_done(security_object); +#endif //YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE } void M2MInterfaceImpl::bootstrap_error() { +#ifndef YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE tr_debug("M2MInterfaceImpl::bootstrap_error()"); internal_event(STATE_IDLE); _observer.error(M2MInterface::BootstrapFailed); +#endif //YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE } void M2MInterfaceImpl::coap_data_processed() @@ -338,9 +350,9 @@ internal_event(STATE_COAP_DATA_RECEIVED, event); } -void M2MInterfaceImpl::socket_error(uint8_t /*error_code*/) +void M2MInterfaceImpl::socket_error(uint8_t error_code) { - tr_debug("M2MInterfaceImpl::socket_error(uint8_t error_code)"); + tr_debug("M2MInterfaceImpl::socket_error code [%d]", error_code); internal_event(STATE_IDLE); M2MInterface::Error error = M2MInterface::NetworkError; _observer.error(error); @@ -401,8 +413,10 @@ tr_debug("M2MInterfaceImpl::state_idle"); } + void M2MInterfaceImpl::state_bootstrap( EventData *data) { +#ifndef YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE tr_debug("M2MInterfaceImpl::state_bootstrap"); // Start with bootstrapping preparation bool success = false; @@ -415,27 +429,46 @@ String server_address = security->resource_value_string(M2MSecurity::M2MServerUri); tr_debug("M2MInterfaceImpl::state_bootstrap - server_address %s", server_address.c_str()); String ip_address; - uint16_t port; + uint16_t port = 0; + String coap; if(server_address.compare(0,COAP.size(),COAP) == 0) { - server_address = server_address.substr(COAP.size(), - server_address.size()-COAP.size()); - int colonFound = server_address.find_last_of(':'); + coap = COAP; + } + else if(server_address.compare(0,COAPS.size(),COAPS) == 0) { + security->resource_value_int(M2MSecurity::SecurityMode) != M2MSecurity::NoSecurity ? coap = COAPS: coap = ""; + } + if(!coap.empty()) { + server_address = server_address.substr(coap.size(), + server_address.size()-coap.size()); + int colonFound = server_address.find_last_of(':'); //10 if(colonFound != -1) { - ip_address = server_address.substr(0,colonFound); - port = atoi(server_address.substr(colonFound+1, + ip_address = server_address.substr(0,colonFound); + port = atoi(server_address.substr(colonFound+1, server_address.size()-ip_address.size()).c_str()); - - tr_debug("M2MInterfaceImpl::state_bootstrap - IP address %s , Port %d", ip_address.c_str(), port); - // If bind and resolving server address succeed then proceed else - // return error to the application and go to Idle state. - if(_connection_handler->resolve_server_address(ip_address, - port, - M2MConnectionObserver::Bootstrap, - security)) { - tr_debug("M2MInterfaceImpl::state_bootstrap - resolve_server_address - success"); - success = true; - } + colonFound = ip_address.find_last_of(']'); + if(ip_address.compare(0,1,"[") == 0) { + if(colonFound == -1) { + ip_address.clear(); + } else { + ip_address = ip_address.substr(1,colonFound-1); + } + } else if(colonFound != -1) { + ip_address.clear(); + } } + tr_debug("M2MInterfaceImpl::state_bootstrap - IP address %s , Port %d", ip_address.c_str(), port); + // If bind and resolving server address succeed then proceed else + // return error to the application and go to Idle state. + if(ip_address.empty()) { + tr_error("M2MInterfaceImpl::state_bootstrap - set error as M2MInterface::InvalidParameters"); + success = false; + } else if(_connection_handler->resolve_server_address(ip_address, + port, + M2MConnectionObserver::Bootstrap, + security)) { + tr_debug("M2MInterfaceImpl::state_bootstrap - resolve_server_address - success"); + success = true; + } } } } @@ -445,10 +478,12 @@ _observer.error(M2MInterface::InvalidParameters); internal_event(STATE_IDLE); } +#endif //YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE } void M2MInterfaceImpl::state_bootstrap_address_resolved( EventData *data) { +#ifndef YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE tr_debug("M2MInterfaceImpl::state_bootstrap_address_resolved"); ResolvedAddressData *event = (ResolvedAddressData *)data; sn_nsdl_addr_s address; @@ -467,26 +502,31 @@ } address.port = event->_port; address.addr_ptr = (uint8_t*)event->_address->_address; - _connection_handler->start_listening_for_data(); if(_nsdl_interface->create_bootstrap_resource(&address)) { tr_debug("M2MInterfaceImpl::state_bootstrap_address_resolved : create_bootstrap_resource - success"); internal_event(STATE_BOOTSTRAP_RESOURCE_CREATED); + _connection_handler->start_listening_for_data(); } else{ // If resource creation fails then inform error to application tr_error("M2MInterfaceImpl::state_bootstrap_address_resolved : M2MInterface::InvalidParameters"); internal_event(STATE_IDLE); _observer.error(M2MInterface::InvalidParameters); } +#endif //YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE } void M2MInterfaceImpl::state_bootstrap_resource_created( EventData */*data*/) { +#ifndef YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE tr_debug("M2MInterfaceImpl::state_bootstrap_resource_created"); +#endif //YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE } void M2MInterfaceImpl::state_bootstrapped( EventData */*data*/) { +#ifndef YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE tr_debug("M2MInterfaceImpl::state_bootstrapped"); +#endif //YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE } void M2MInterfaceImpl::state_register( EventData *data) @@ -507,30 +547,49 @@ String server_address = security->resource_value_string(M2MSecurity::M2MServerUri); tr_debug("M2MInterfaceImpl::state_register - server_address %s", server_address.c_str()); String ip_address; - uint16_t port; + uint16_t port = 0; + String coap; if(server_address.compare(0,COAP.size(),COAP) == 0) { - server_address = server_address.substr(COAP.size(), - server_address.size()-COAP.size()); + coap = COAP; + } + else if(server_address.compare(0,COAPS.size(),COAPS) == 0) { + security->resource_value_int(M2MSecurity::SecurityMode) != M2MSecurity::NoSecurity ? coap = COAPS: coap = ""; + } + if(!coap.empty()) { + server_address = server_address.substr(coap.size(), + server_address.size()-coap.size()); int colonFound = server_address.find_last_of(':'); //10 if(colonFound != -1) { - ip_address = server_address.substr(0,colonFound); - port = atoi(server_address.substr(colonFound+1, + ip_address = server_address.substr(0,colonFound); + port = atoi(server_address.substr(colonFound+1, server_address.size()-ip_address.size()).c_str()); - - tr_debug("M2MInterfaceImpl::state_register - IP address %s , Port %d", ip_address.c_str(), port); - // If bind and resolving server address succeed then proceed else - // return error to the application and go to Idle state. - if(_connection_handler->resolve_server_address(ip_address, - port, - M2MConnectionObserver::LWM2MServer, - security)) { - tr_debug("M2MInterfaceImpl::state_register - resolve_server_address - success"); - success = true; - } else { - tr_error("M2MInterfaceImpl::state_register - set error as M2MInterface::NetworkError"); - error = M2MInterface::NetworkError; + colonFound = ip_address.find_last_of(']'); + if(ip_address.compare(0,1,"[") == 0) { + if(colonFound == -1) { + ip_address.clear(); + } else { + ip_address = ip_address.substr(1,colonFound-1); + } + } else if(colonFound != -1) { + ip_address.clear(); } } + tr_debug("M2MInterfaceImpl::state_register - IP address %s , Port %d", ip_address.c_str(), port); + // If bind and resolving server address succeed then proceed else + // return error to the application and go to Idle state. + if(ip_address.empty()) { + tr_error("M2MInterfaceImpl::state_register - set error as M2MInterface::InvalidParameters"); + error = M2MInterface::InvalidParameters; + }else if(_connection_handler->resolve_server_address(ip_address, + port, + M2MConnectionObserver::LWM2MServer, + security)) { + tr_debug("M2MInterfaceImpl::state_register - resolve_server_address - success"); + success = true; + } else { + tr_error("M2MInterfaceImpl::state_register - set error as M2MInterface::NetworkError"); + error = M2MInterface::NetworkError; + } } } } @@ -561,9 +620,10 @@ tr_debug("M2MInterfaceImpl::state_register_address_resolved : IPv6 address"); address_type = SN_NSDL_ADDRESS_TYPE_IPV6; } - internal_event(STATE_REGISTER_RESOURCE_CREATED); - _connection_handler->start_listening_for_data(); - if(!_nsdl_interface->send_register_message((uint8_t*)event->_address->_address,event->_port, address_type)) { + if(_nsdl_interface->send_register_message((uint8_t*)event->_address->_address,event->_port, address_type)) { + internal_event(STATE_REGISTER_RESOURCE_CREATED); + _connection_handler->start_listening_for_data(); + } else { // If resource creation fails then inform error to application tr_error("M2MInterfaceImpl::state_register_address_resolved : M2MInterface::InvalidParameters"); internal_event(STATE_IDLE); @@ -746,16 +806,24 @@ M2MInterfaceImpl::state_idle(data); break; case STATE_BOOTSTRAP: + #ifndef YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE M2MInterfaceImpl::state_bootstrap(data); + #endif //YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE break; case STATE_BOOTSTRAP_ADDRESS_RESOLVED: + #ifndef YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE M2MInterfaceImpl::state_bootstrap_address_resolved(data); + #endif //YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE break; case STATE_BOOTSTRAP_RESOURCE_CREATED: + #ifndef YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE M2MInterfaceImpl::state_bootstrap_resource_created(data); + #endif //YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE break; case STATE_BOOTSTRAPPED: + #ifndef YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE M2MInterfaceImpl::state_bootstrapped(data); + #endif //YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE break; case STATE_REGISTER: M2MInterfaceImpl::state_register(data);