Skip to content
This repository has been archived by the owner on Apr 24, 2019. It is now read-only.

Commit

Permalink
Initial bootstrap support (#254)
Browse files Browse the repository at this point in the history
* Initial bootstrap support
  • Loading branch information
Antti Yli-Tokola committed Jun 23, 2016
1 parent a6a4672 commit 9c6c5e9
Show file tree
Hide file tree
Showing 32 changed files with 904 additions and 503 deletions.
1 change: 1 addition & 0 deletions mbed-client/m2mconstants.h
Expand Up @@ -36,6 +36,7 @@ const float REDUCTION_FACTOR = 0.75f;

#define COAP "coap://"
#define COAPS "coaps://"
#define BOOTSTRAP_URI "bs"
// PUT attributes to be checked from server
#define EQUAL "="
#define AMP "&"
Expand Down
2 changes: 2 additions & 0 deletions mbed-client/m2msecurity.h
Expand Up @@ -211,6 +211,7 @@ friend class M2MNsdlInterface;
private:

M2MResource* get_resource(SecurityResource resource) const;
void clear_resources();

private:

Expand All @@ -223,6 +224,7 @@ friend class M2MNsdlInterface;
friend class Test_M2MConnectionHandlerPimpl_linux;
friend class Test_M2MConnectionHandlerPimpl_mbed;
friend class Test_M2MConnectionSecurityPimpl;
friend class Test_M2MNsdlInterface;
};

#endif // M2M_SECURITY_H
Expand Down
3 changes: 2 additions & 1 deletion mbed-client/m2mtimerobserver.h
Expand Up @@ -35,7 +35,8 @@ class M2MTimerObserver
PMaxTimer,
Dtls,
QueueSleep,
RetryTimer
RetryTimer,
BootstrapTimer
}Type;

/**
Expand Down
4 changes: 2 additions & 2 deletions run_unit_tests.sh
Expand Up @@ -29,8 +29,8 @@ mkdir results
mkdir coverage

find ./build -name '*.xml' | xargs cp -t ./results/
find ./build -name '*.gcno' | xargs cp -t ./coverage/
find ./build -name '*.gcda' | xargs cp -t ./coverage/
find ./build/x86-linux-native-coverage/test -name '*.gcno' | xargs cp -t ./coverage/
find ./build/x86-linux-native-coverage/test -name '*.gcda' | xargs cp -t ./coverage/
touch coverage/*.gcda
exclude_files="${PWD}/test/"
gcovr -r ./ --gcov-filter='.*source*.' --exclude-unreachable-branches --exclude $exclude_files --object-directory ./coverage -x -o ./results/gcovr.xml
Expand Down
3 changes: 1 addition & 2 deletions source/include/m2minterfaceimpl.h
Expand Up @@ -382,8 +382,6 @@ friend class M2MInterfaceFactory;
const int _max_states;
bool _event_generated;
EventData *_event_data;

String _endpoint_name;
String _endpoint_type;
String _domain;
int32_t _life_time;
Expand All @@ -398,6 +396,7 @@ friend class M2MInterfaceFactory;
bool _update_register_ongoing;
M2MTimer *_queue_sleep_timer;
M2MTimer *_retry_timer;
M2MTimer *_bootstrap_timer;
callback_handler _callback_handler;
M2MSecurity *_security;
uint8_t _retry_count;
Expand Down
71 changes: 56 additions & 15 deletions source/include/m2mnsdlinterface.h
Expand Up @@ -102,7 +102,7 @@ class M2MNsdlInterface : public M2MTimerObserver,
* @param address Bootstrap address.
* @return true if created and sent successfully else false.
*/
bool create_bootstrap_resource(sn_nsdl_addr_s *address);
bool create_bootstrap_resource(sn_nsdl_addr_s *address, const String &bootstrap_endpoint_name);

/**
* @brief Sends the register message to the server.
Expand Down Expand Up @@ -182,12 +182,6 @@ class M2MNsdlInterface : public M2MTimerObserver,
sn_nsdl_addr_s *address,
sn_nsdl_capab_e nsdl_capab);

/**
* @brief Callback when the bootstrap information is received from bootstrap server.
* @param server_info, Server information received from bootstrap server.
*/
void bootstrap_done_callback(sn_nsdl_oma_server_info_t *server_info);

/**
* @brief Callback when there is data received from server and needs to be processed.
* @param data, data received from server.
Expand All @@ -210,6 +204,12 @@ class M2MNsdlInterface : public M2MTimerObserver,
*/
nsdl_s* get_nsdl_handle();

/**
* @brief Get endpoint name
* @return endpoint name
*/
const String& endpoint_name() const;

protected: // from M2MTimerObserver

virtual void timer_expired(M2MTimerObserver::Type type);
Expand Down Expand Up @@ -298,41 +298,82 @@ class M2MNsdlInterface : public M2MTimerObserver,
const String &uri_path);

/**
* \brief Allocate (size + 1) amount of memory, copy size bytes into
* @brief Allocate (size + 1) amount of memory, copy size bytes into
* it and add zero termination.
* \param source Source string to copy, may not be NULL.
* \param size The size of memory to be reserved.
* @param source Source string to copy, may not be NULL.
* @param size The size of memory to be reserved.
*/
uint8_t* alloc_string_copy(const uint8_t* source, uint16_t size);

/**
* \brief Utility method to convert given lifetime int to ascii
* @brief Utility method to convert given lifetime int to ascii
* and allocate a buffer for it and set it to _endpoint->lifetime_ptr.
* \param lifetime A new value for lifetime.
* @param lifetime A new value for lifetime.
*/
void set_endpoint_lifetime_buffer(int lifetime);

/**
* @brief Handle incoming bootstrap PUT message.
* @param coap_header, Received CoAP message
* @param address, Server address
*/
void handle_bootstrap_put_message(sn_coap_hdr_s *coap_header, sn_nsdl_addr_s *address);

/**
* @brief Handle bootstrap finished message.
* @param coap_header, Received CoAP message
* @param address, Server address
*/
void handle_bootstrap_finished(sn_coap_hdr_s *coap_header,sn_nsdl_addr_s *address);

/**
* @brief Handle bootstrap delete message.
* @param coap_header, Received CoAP message
* @param address, Server address
*/
void handle_bootstrap_delete(sn_coap_hdr_s *coap_header,sn_nsdl_addr_s *address);

/**
* @brief Parse bootstrap TLV message.
* @param coap_header, Received CoAP message
* @return True if parsing was succesful else false
*/
bool parse_bootstrap_message(sn_coap_hdr_s *coap_header, bool is_security_object);

/**
* @brief Parse bootstrap TLV message.
* @param coap_header, Received CoAP message
* @return True if parsing was succesful else false
*/
bool validate_security_object();

/**
* @brief Handle bootstrap errors.
*/
void handle_bootstrap_error();

private:

M2MNsdlObserver &_observer;
M2MObjectList _object_list;
M2MServer *_server;
M2MServer *_server; // Not owned
M2MSecurity *_security; // Not owned
M2MTimer *_nsdl_exceution_timer;
M2MTimer *_registration_timer;
sn_nsdl_ep_parameters_s *_endpoint;
sn_nsdl_resource_info_s *_resource;
sn_nsdl_bs_ep_info_t _bootstrap_endpoint;
sn_nsdl_oma_device_t _bootstrap_device_setup;
sn_nsdl_addr_s _sn_nsdl_address;
nsdl_s *_nsdl_handle;
uint32_t _counter_for_nsdl;
uint16_t _bootstrap_id;
bool _register_ongoing;
bool _unregister_ongoing;
bool _update_register_ongoing;
String _endpoint_name;

friend class Test_M2MNsdlInterface;

};

#endif // M2MNSDLINTERFACE_H

2 changes: 1 addition & 1 deletion source/include/nsdlaccesshelper.h
Expand Up @@ -40,7 +40,7 @@ uint8_t __nsdl_c_send_to_server(struct nsdl_s * nsdl_handle,
uint8_t __nsdl_c_received_from_server(struct nsdl_s * nsdl_handle,
sn_coap_hdr_s *coap_header,
sn_nsdl_addr_s *address_ptr);
void __nsdl_c_bootstrap_done(sn_nsdl_oma_server_info_t *server_info_ptr, struct nsdl_s * nsdl_handle);

void *__socket_malloc( void * context, size_t size);
void __socket_free(void * context, void * ptr);

Expand Down
35 changes: 28 additions & 7 deletions source/m2minterfaceimpl.cpp
Expand Up @@ -43,7 +43,6 @@ M2MInterfaceImpl::M2MInterfaceImpl(M2MInterfaceObserver& observer,
_max_states( STATE_MAX_STATES ),
_event_generated(false),
_event_data(NULL),
_endpoint_name(ep_name),
_endpoint_type(ep_type),
_domain( dmn),
_life_time(l_time),
Expand All @@ -55,8 +54,9 @@ M2MInterfaceImpl::M2MInterfaceImpl(M2MInterfaceObserver& observer,
_event_ignored(false),
_register_ongoing(false),
_update_register_ongoing(false),
_queue_sleep_timer(new M2MTimer(*this)),
_queue_sleep_timer(new M2MTimer(*this)),
_retry_timer(new M2MTimer(*this)),
_bootstrap_timer(NULL),
_callback_handler(NULL),
_security(NULL),
_retry_count(0),
Expand All @@ -73,7 +73,7 @@ M2MInterfaceImpl::M2MInterfaceImpl(M2MInterfaceObserver& observer,
sec_mode = M2MConnectionSecurity::TLS;
}
tr_debug("M2MInterfaceImpl::M2MInterfaceImpl() -IN");
_nsdl_interface->create_endpoint(_endpoint_name,
_nsdl_interface->create_endpoint(ep_name,
_endpoint_type,
_life_time,
_domain,
Expand All @@ -86,6 +86,9 @@ M2MInterfaceImpl::M2MInterfaceImpl(M2MInterfaceObserver& observer,
_connection_handler = new M2MConnectionHandler(*this, _security_connection, mode, stack);
__connection_handler = _connection_handler;
_connection_handler->bind_connection(_listen_port);
#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
_bootstrap_timer = new M2MTimer(*this);
#endif
tr_debug("M2MInterfaceImpl::M2MInterfaceImpl() -OUT");
}

Expand All @@ -99,6 +102,7 @@ M2MInterfaceImpl::~M2MInterfaceImpl()
__connection_handler = NULL;
delete _connection_handler;
delete _retry_timer;
delete _bootstrap_timer;
_security_connection = NULL;
tr_debug("M2MInterfaceImpl::~M2MInterfaceImpl() - OUT");
}
Expand Down Expand Up @@ -136,6 +140,8 @@ void M2MInterfaceImpl::bootstrap(M2MSecurity *security)
_observer.error(M2MInterface::NotAllowed);
}
tr_debug("M2MInterfaceImpl::bootstrap(M2MSecurity *security) - OUT");
#else
_observer.error(M2MInterface::NotAllowed);
#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
}

Expand Down Expand Up @@ -351,7 +357,8 @@ void M2MInterfaceImpl::client_unregistered()
void M2MInterfaceImpl::bootstrap_done(M2MSecurity *security_object)
{
#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
tr_debug("M2MInterfaceImpl::bootstrap_done(M2MSecurity *security_object)");
tr_debug("M2MInterfaceImpl::bootstrap_done");
_bootstrap_timer->stop_timer();
internal_event(STATE_BOOTSTRAPPED);
_observer.bootstrap_done(security_object);
#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
Expand All @@ -361,6 +368,7 @@ void M2MInterfaceImpl::bootstrap_error()
{
#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
tr_debug("M2MInterfaceImpl::bootstrap_error()");
_bootstrap_timer->stop_timer();
internal_event(STATE_IDLE);
_observer.error(M2MInterface::BootstrapFailed);
#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
Expand Down Expand Up @@ -505,6 +513,9 @@ void M2MInterfaceImpl::timer_expired(M2MTimerObserver::Type type)
_connection_handler->bind_connection(_listen_port);
internal_event(STATE_REGISTER);
}
else if (M2MTimerObserver::BootstrapTimer == type) {
bootstrap_error();
}
}

// state machine sits here.
Expand All @@ -530,6 +541,8 @@ void M2MInterfaceImpl::state_bootstrap( EventData *data)
tr_debug("M2MInterfaceImpl::state_bootstrap - server_type : M2MSecurity::Bootstrap");
String server_address = security->resource_value_string(M2MSecurity::M2MServerUri);
tr_debug("M2MInterfaceImpl::state_bootstrap - server_address %s", server_address.c_str());
_bootstrap_timer->start_timer(MBED_CLIENT_RECONNECTION_COUNT * MBED_CLIENT_RECONNECTION_INTERVAL * 8 * 1000,
M2MTimerObserver::BootstrapTimer);
String ip_address;
String coap;
if(server_address.compare(0,sizeof(COAP)-1,COAP) == 0) {
Expand All @@ -543,7 +556,7 @@ void M2MInterfaceImpl::state_bootstrap( EventData *data)
server_address.size()-coap.size());

process_address(server_address, ip_address, _server_port);

tr_debug("M2MInterfaceImpl::state_bootstrap - IP address %s , Port %d", ip_address.c_str(), _server_port);
// If bind and resolving server address succeed then proceed else
// return error to the application and go to Idle state.
Expand Down Expand Up @@ -591,7 +604,15 @@ void M2MInterfaceImpl::state_bootstrap_address_resolved( EventData *data)
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)) {

// Include domain id to be part of endpoint name
String new_ep_name;
new_ep_name += _nsdl_interface->endpoint_name();
if (!_domain.empty()) {
new_ep_name += '@';
new_ep_name += _domain;
}
if(_nsdl_interface->create_bootstrap_resource(&address, new_ep_name)) {
tr_debug("M2MInterfaceImpl::state_bootstrap_address_resolved : create_bootstrap_resource - success");
internal_event(STATE_BOOTSTRAP_RESOURCE_CREATED);
} else{
Expand Down Expand Up @@ -672,7 +693,7 @@ void M2MInterfaceImpl::state_register( EventData *data)
}

void M2MInterfaceImpl::process_address(const String& server_address, String& ip_address, uint16_t& port) {

int colonFound = server_address.find_last_of(':'); //10
if(colonFound != -1) {
ip_address = server_address.substr(0,colonFound);
Expand Down

0 comments on commit 9c6c5e9

Please sign in to comment.