Skip to content

Commit

Permalink
ws create interface and start rpl for poc (ARMmbed#1562)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mika Tervonen committed Feb 12, 2018
1 parent 9bf1d1f commit 096aeef
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 55 deletions.
5 changes: 5 additions & 0 deletions source/6LoWPAN/adaptation_interface.c
Expand Up @@ -990,6 +990,11 @@ static bool mac_data_is_broadcast_addr(const sockaddr_t *addr)
static bool mcps_data_indication_neighbor_validate(int8_t interface_id, const sockaddr_t *addr)
{
/* If MLE is enabled, we will talk if we have an MLE association */

if (!mle_class_exists_for_interface(interface_id)) {
// No MLE for this interface all neighbours are valid
return true;
}
mle_neigh_table_entry_t *mle_entry = mle_class_get_by_link_address(interface_id, addr->address + 2, addr->addr_type);
if (mle_entry && (mle_entry->handshakeReady || mle_entry->thread_commission)) {
return true;
Expand Down
266 changes: 216 additions & 50 deletions source/6LoWPAN/ws/ws_bootstrap.c
Expand Up @@ -15,65 +15,37 @@
* limitations under the License.
*/

#include <string.h>
#include "nsconfig.h"
#include "ns_types.h"
#include "ns_trace.h"
#include "net_interface.h"
#include "eventOS_event.h"
#include "NWK_INTERFACE/Include/protocol.h"
#include "6LoWPAN/Bootstraps/protocol_6lowpan.h"
#include "6LoWPAN/Bootstraps/protocol_6lowpan_interface.h"
#include "6LoWPAN/MAC/mac_helper.h"
#include "6LoWPAN/MAC/mac_data_poll.h"
#include "6LoWPAN/ws/ws_common.h"
#include "6LoWPAN/ws/ws_bootstrap.h"
#include "RPL/rpl_control.h"
#include "RPL/rpl_data.h"
#include "Common_Protocols/icmpv6.h"
#include "Common_Protocols/icmpv6_radv.h"

#include "net_rpl.h"
#include "mac_api.h"

#define TRACE_GROUP "wsbs"

#ifdef HAVE_WS

static void ws_bootstrap_tasklet(arm_event_s *event)
{
ws_bootsrap_event_type_e event_type;
event_type = (ws_bootsrap_event_type_e)event->event_type;
protocol_interface_info_entry_t *cur;
cur = protocol_stack_interface_info_get_by_bootstrap_id(event->receiver);
if (!cur) {
return;
}

switch (event_type) {
case WS_INIT_EVENT:
tr_debug("tasklet init");
break;
case WS_DISCOVERY_START:
tr_debug("Discovery start");
ws_bootstrap_configuration_start(cur);

break;
case WS_CONFIGURATION_START:
tr_debug("Configuration start");
ws_bootstrap_authentication_start(cur);
break;
case WS_AUTHENTICATION_START:
tr_debug("authentication start");
ws_bootstrap_operation_start(cur);
break;
case WS_OPERATION_START:
tr_debug("operation start");
// Activate RPL
// Activate IPv6 stack
break;
default:
tr_err("Invalid event received");
break;
}

}
static void ws_bootstrap_event_handler(arm_event_s *event);
static void ws_bootstrap_state_change(protocol_interface_info_entry_t *cur, icmp_state_t nwk_bootstrap_state);

static int ws_bootstrap_tasklet_init(protocol_interface_info_entry_t *cur)
{
if (cur->bootStrapId < 0) {
cur->bootStrapId = eventOS_event_handler_create(&ws_bootstrap_tasklet, WS_INIT_EVENT);
cur->bootStrapId = eventOS_event_handler_create(&ws_bootstrap_event_handler, WS_INIT_EVENT);
tr_debug("WS tasklet init");
}

Expand Down Expand Up @@ -116,13 +88,13 @@ static int8_t ws_bootsrap_event_trig(ws_bootsrap_event_type_e event_type, int8_t
protocol_6lowpan_register_handlers(cur);
addr_interface_set_ll64(cur, NULL);
cur->nwk_nd_re_scan_count = 0;
//thread_interface_up(cur);
//WS_interface_up(cur);
// Trigger discovery for bootstrap
ret_val = nwk_6lowpan_up(cur);
if (ret_val) {
goto cleanup;
}
ws_bootstrap_discovery_start(cur);
ws_bootstrap_event_discovery_start(cur);

return 0;
cleanup:
Expand All @@ -149,6 +121,7 @@ void ws_bootstrap_configuration_reset(protocol_interface_info_entry_t *cur)

// Do not process beacons
cur->mac_parameters->beacon_ind = NULL;
cur->mac_parameters->mac_security_level = 0;

// Set default parameters to interface
cur->configure_flags = INTERFACE_BOOTSTRAP_DEFINED;
Expand All @@ -169,6 +142,7 @@ void ws_bootstrap_configuration_reset(protocol_interface_info_entry_t *cur)
tr_err("Invalid bootstrap_mode");
}

cur->nwk_bootstrap_state = ER_ACTIVE_SCAN;
//cur->mac_security_key_usage_update_cb = ws_management_mac_security_key_update_cb;
return;
}
Expand Down Expand Up @@ -226,21 +200,213 @@ int ws_bootstrap_init(int8_t interface_id, net_6lowpan_mode_e bootstrap_mode)
return 0;
}

void ws_bootstrap_discovery_start(protocol_interface_info_entry_t *cur)
static void ws_bootstrap_mac_activate(protocol_interface_info_entry_t *cur, uint16_t channel, uint16_t panid, bool coordinator)
{
mlme_start_t start_req;
memset(&start_req, 0, sizeof(mlme_start_t));

cur->mac_parameters->pan_id = panid;
cur->mac_parameters->mac_channel = channel;

start_req.PANId = panid;
start_req.LogicalChannel = channel;
start_req.BeaconOrder = 0x0f;
start_req.SuperframeOrder = 0x0f;
start_req.PANCoordinator = coordinator;

if (cur->mac_api) {
cur->mac_api->mlme_req(cur->mac_api, MLME_START, (void*)&start_req);
}
}
static void ws_bootstrap_mac_interface_activate(protocol_interface_info_entry_t *cur)
{
tr_debug("MAC init");
mac_helper_pib_boolean_set(cur, macRxOnWhenIdle, true);
cur->lowpan_info &= ~INTERFACE_NWK_CONF_MAC_RX_OFF_IDLE;

//mac_data_poll_init(cur);
//mac_helper_mac16_address_set(cur, 0xffff);
//mac_helper_default_security_level_set(cur, cur->mac_parameters->mac_configured_sec_level);
//mac_helper_default_security_key_id_mode_set(cur,MAC_KEY_ID_MODE_IDX);
ws_bootstrap_mac_activate(cur, 11, 0xabab, true);
return;
}
static void ws_bootstrap_ip_stack_activate(protocol_interface_info_entry_t *cur)
{
tr_debug("ip stack init");
clear_power_state(ICMP_ACTIVE);
cur->lowpan_info |= INTERFACE_NWK_BOOTSRAP_ACTIVE;
}
static void ws_bootstrap_rpl_callback(rpl_event_t event, void *handle)
{

protocol_interface_info_entry_t *cur = handle;
if (!cur->rpl_domain || cur->interface_mode != INTERFACE_UP) {
return;
}
tr_warn("RPL callback event %d", event);
}
static uint8_t dodag_id[16] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
static uint8_t dodag_prefix[16] = {0xfd,00,0xaa};

static void ws_bootstrap_rpl_root_activate(protocol_interface_info_entry_t *cur)
{
tr_debug("RPL root Activate");
rpl_dodag_conf_t new_conf;
new_conf.default_lifetime = 64;
new_conf.lifetime_unit = 60;
new_conf.dag_max_rank_increase = 3072;
new_conf.min_hop_rank_increase = 128;// 24 HOP?
new_conf.dio_interval_min = 9;
new_conf.dio_interval_doublings = 12;
new_conf.dio_redundancy_constant = 10;
new_conf.objective_code_point = 1;
new_conf.authentication = 0;
new_conf.path_control_size = 0;
rpl_data_init_root();
protocol_6lowpan_rpl_root_dodag = rpl_control_create_dodag_root(protocol_6lowpan_rpl_domain, 1, dodag_id, &new_conf, new_conf.min_hop_rank_increase, RPL_GROUNDED | RPL_MODE_NON_STORING | RPL_DODAG_PREF(0));
if (!protocol_6lowpan_rpl_root_dodag) {
tr_err("RPL dodag init failed");
}
uint8_t t_flags = PIO_A;

icmpv6_slaac_address_add(cur, dodag_prefix, 64, 0xffffffff, 0xffffffff, true, SLAAC_IID_FIXED);

rpl_control_update_dodag_prefix(protocol_6lowpan_rpl_root_dodag, dodag_prefix, 64, t_flags, 0xffffffff, 0xffffffff, false);
rpl_control_update_dodag_route(protocol_6lowpan_rpl_root_dodag, dodag_prefix, 64, 0, 0xffffffff, false);
rpl_control_update_dodag_route(protocol_6lowpan_rpl_root_dodag, NULL, 0, 0, 0xffffffff, false);
}

static void ws_bootstrap_rpl_activate(protocol_interface_info_entry_t *cur)
{
tr_debug("RPL Activate");
bool downstream = true;
bool leaf = false;

addr_add_router_groups(cur);
rpl_control_set_domain_on_interface(cur, protocol_6lowpan_rpl_domain, downstream);
rpl_control_set_callback(protocol_6lowpan_rpl_domain, ws_bootstrap_rpl_callback, cur);
// If i am router I Do this
rpl_control_force_leaf(protocol_6lowpan_rpl_domain, leaf);

if (cur->bootsrap_mode == ARM_NWK_BOOTSRAP_MODE_6LoWPAN_ROUTER) {
rpl_control_transmit_dis(cur->rpl_domain, cur, 0, 0, NULL, 0, ADDR_LINK_LOCAL_ALL_ROUTERS);
} else if (cur->bootsrap_mode == ARM_NWK_BOOTSRAP_MODE_6LoWPAN_BORDER_ROUTER) {
ws_bootstrap_rpl_root_activate(cur);
}

}


static void ws_bootstrap_icmp_rs_msg_tx(protocol_interface_info_entry_t *cur)
{
ws_bootsrap_event_trig(WS_DISCOVERY_START, cur->bootStrapId, ARM_LIB_HIGH_PRIORITY_EVENT);
tr_debug("send RS");
buffer_t *buf = icmpv6_build_rs(cur, NULL);

protocol_push(buf);
}
void ws_bootstrap_configuration_start(protocol_interface_info_entry_t *cur)

/*
* Event transitions
*
* */

void ws_bootstrap_event_discovery_start(protocol_interface_info_entry_t *cur)
{
ws_bootsrap_event_trig(WS_CONFIGURATION_START, cur->bootStrapId, ARM_LIB_HIGH_PRIORITY_EVENT);
ws_bootsrap_event_trig(WS_DISCOVERY_START, cur->bootStrapId, ARM_LIB_LOW_PRIORITY_EVENT);
}
void ws_bootstrap_authentication_start(protocol_interface_info_entry_t *cur)
void ws_bootstrap_event_configuration_start(protocol_interface_info_entry_t *cur)
{
ws_bootsrap_event_trig(WS_AUTHENTICATION_START, cur->bootStrapId, ARM_LIB_HIGH_PRIORITY_EVENT);
ws_bootsrap_event_trig(WS_CONFIGURATION_START, cur->bootStrapId, ARM_LIB_LOW_PRIORITY_EVENT);
}
void ws_bootstrap_event_authentication_start(protocol_interface_info_entry_t *cur)
{
ws_bootsrap_event_trig(WS_AUTHENTICATION_START, cur->bootStrapId, ARM_LIB_LOW_PRIORITY_EVENT);
}
void ws_bootstrap_event_operation_start(protocol_interface_info_entry_t *cur)
{
ws_bootsrap_event_trig(WS_OPERATION_START, cur->bootStrapId, ARM_LIB_LOW_PRIORITY_EVENT);
}
void ws_bootstrap_event_routing_ready(protocol_interface_info_entry_t *cur)
{
ws_bootsrap_event_trig(WS_ROUTING_READY, cur->bootStrapId, ARM_LIB_LOW_PRIORITY_EVENT);
}

static void ws_bootstrap_event_handler(arm_event_s *event)
{
ws_bootsrap_event_type_e event_type;
event_type = (ws_bootsrap_event_type_e)event->event_type;
protocol_interface_info_entry_t *cur;
cur = protocol_stack_interface_info_get_by_bootstrap_id(event->receiver);
if (!cur) {
return;
}

switch (event_type) {
case WS_INIT_EVENT:
tr_debug("tasklet init");
break;
case WS_DISCOVERY_START:
tr_info("Discovery start");
ws_bootstrap_mac_interface_activate(cur);
ws_bootstrap_state_change(cur, ER_ACTIVE_SCAN);
break;
case WS_CONFIGURATION_START:
tr_info("Configuration start");
ws_bootstrap_event_authentication_start(cur);
break;
case WS_AUTHENTICATION_START:
tr_info("authentication start");
ws_bootstrap_event_operation_start(cur);
break;
case WS_OPERATION_START:
tr_info("operation start");
// Activate RPL
// Activate IPv6 stack
ws_bootstrap_ip_stack_activate(cur);
ws_bootstrap_rpl_activate(cur);
ws_bootstrap_event_routing_ready(cur);
break;
case WS_ROUTING_READY:
tr_info("Routing ready");
ws_bootstrap_state_change(cur, ER_BOOTSRAP_DONE);
break;
default:
tr_err("Invalid event received");
break;
}

}
void ws_bootstrap_operation_start(protocol_interface_info_entry_t *cur)


/*
* State machine
*
* */

static void ws_bootstrap_state_change(protocol_interface_info_entry_t *cur, icmp_state_t nwk_bootstrap_state)
{
ws_bootsrap_event_trig(WS_OPERATION_START, cur->bootStrapId, ARM_LIB_HIGH_PRIORITY_EVENT);
cur->bootsrap_state_machine_cnt = 1;
cur->nwk_bootstrap_state = nwk_bootstrap_state;
}
void ws_bootstrap_state_machine(protocol_interface_info_entry_t *cur)
{

switch (cur->nwk_bootstrap_state) {
case ER_ACTIVE_SCAN:
tr_debug("WS SM:Active Scan");
ws_bootstrap_event_configuration_start(cur);
break;

case ER_BOOTSRAP_DONE:
tr_debug("WS SM:Bootstrap Done");
// Bootstrap_done event to application
nwk_bootsrap_state_update(ARM_NWK_BOOTSTRAP_READY, cur);
break;

default:
tr_warn("WS SM:Invalid state %d",cur->nwk_bootstrap_state);

}
}
#endif //HAVE_WS
15 changes: 10 additions & 5 deletions source/6LoWPAN/ws/ws_bootstrap.h
Expand Up @@ -24,26 +24,31 @@ typedef enum {
WS_DISCOVERY_START, /**< discovery start*/
WS_CONFIGURATION_START, /**< configuration learn start*/
WS_AUTHENTICATION_START, /**< authentication start*/
WS_OPERATION_START /**< active operation start*/
WS_OPERATION_START, /**< active operation start*/
WS_ROUTING_READY /**< RPL routing connected to BR*/
} ws_bootsrap_event_type_e;

#ifdef HAVE_WS

int ws_bootstrap_init(int8_t interface_id, net_6lowpan_mode_e bootstrap_mode);

void ws_bootstrap_state_machine(protocol_interface_info_entry_t *cur);

/*State machine transactions*/
void ws_bootstrap_discovery_start(protocol_interface_info_entry_t *cur);
void ws_bootstrap_event_discovery_start(protocol_interface_info_entry_t *cur);

void ws_bootstrap_configuration_start(protocol_interface_info_entry_t *cur);
void ws_bootstrap_event_configuration_start(protocol_interface_info_entry_t *cur);

void ws_bootstrap_authentication_start(protocol_interface_info_entry_t *cur);
void ws_bootstrap_event_authentication_start(protocol_interface_info_entry_t *cur);

void ws_bootstrap_operation_start(protocol_interface_info_entry_t *cur);
void ws_bootstrap_event_operation_start(protocol_interface_info_entry_t *cur);

void ws_bootstrap_event_routing_ready(protocol_interface_info_entry_t *cur);

#else

#define ws_bootstrap_init(interface_id, bootstrap_mode) (-1)
#define ws_bootstrap_state_machine(cur)

#endif //HAVE_WS

Expand Down
4 changes: 4 additions & 0 deletions source/NWK_INTERFACE/protocol_core.c
Expand Up @@ -64,6 +64,8 @@
#include "6LoWPAN/Thread/thread_bootstrap.h"
#include "6LoWPAN/Thread/thread_routing.h"
#include "6LoWPAN/Thread/thread_management_internal.h"
#include "6LoWPAN/ws/ws_bootstrap.h"
#include "6LoWPAN/ws/ws_common.h"
#include "ipv6_stack/protocol_ipv6.h"
#include "Service_Libs/whiteboard/whiteboard.h"

Expand Down Expand Up @@ -1059,6 +1061,8 @@ void net_bootsrap_cb_run(uint8_t event)
//eventOS_scheduler_set_active_tasklet(protocol_read_tasklet_id());
if (thread_info(cur)) {
thread_bootstrap_state_machine(cur);
} else if (ws_info(cur)) {
ws_bootstrap_state_machine(cur);
} else {
protocol_6lowpan_bootstrap(cur);
}
Expand Down

0 comments on commit 096aeef

Please sign in to comment.