Nanostack Border Router is a generic mbed border router implementation that provides the 6LoWPAN ND or Thread border router initialization logic.

Committer:
mbed_official
Date:
Wed Nov 27 10:02:22 2019 +0000
Revision:
108:0c14bd1d3334
Parent:
89:2f7ae1d76d7a
Fix conflicting declarations of main() (#197)

Update the main() to be compatible with the declaration from
platform/mbed_toolchain.h that adds the MBED_USED attribute.
Without the attribute the main() symbol is not emitted with the
GCC toolchain using "-Wl,--wrap,main" and "-flto" flags.
.
Commit copied from https://github.com/ARMmbed/nanostack-border-router

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:85f4174a8e29 1 /*
mbed_official 0:85f4174a8e29 2 * Copyright (c) 2016 ARM Limited. All rights reserved.
mbed_official 0:85f4174a8e29 3 */
mbed_official 0:85f4174a8e29 4
mbed_official 0:85f4174a8e29 5 #define LOWPAN_ND 0
mbed_official 0:85f4174a8e29 6 #define THREAD 1
mbed_official 89:2f7ae1d76d7a 7 #define LOWPAN_WS 2
mbed_official 89:2f7ae1d76d7a 8
mbed_official 0:85f4174a8e29 9 #if MBED_CONF_APP_MESH_MODE == THREAD
mbed_official 0:85f4174a8e29 10
mbed_official 0:85f4174a8e29 11 #include <string.h>
mbed_official 0:85f4174a8e29 12 #include <stdlib.h>
mbed_official 0:85f4174a8e29 13 #include <mbed_assert.h>
mbed_official 0:85f4174a8e29 14 #include "ns_types.h"
mbed_official 0:85f4174a8e29 15 #include "eventOS_event.h"
mbed_official 0:85f4174a8e29 16 #include "eventOS_event_timer.h"
mbed_official 0:85f4174a8e29 17 #include "eventOS_scheduler.h"
mbed_official 0:85f4174a8e29 18 #include "platform/arm_hal_timer.h"
mbed_official 0:85f4174a8e29 19 #include "borderrouter_tasklet.h"
mbed_official 0:85f4174a8e29 20 #include "borderrouter_helpers.h"
mbed_official 0:85f4174a8e29 21 #include "net_interface.h"
mbed_official 0:85f4174a8e29 22 #include "rf_wrapper.h"
mbed_official 0:85f4174a8e29 23 #include "nwk_stats_api.h"
mbed_official 0:85f4174a8e29 24 #include "ip6string.h"
mbed_official 0:85f4174a8e29 25 #include "ethernet_mac_api.h"
mbed_official 0:85f4174a8e29 26 #include "mac_api.h"
mbed_official 0:85f4174a8e29 27 #include "sw_mac.h"
mbed_official 0:85f4174a8e29 28 #include "mbed_interface.h"
mbed_official 0:85f4174a8e29 29 #include "common_functions.h"
mbed_official 0:85f4174a8e29 30 #include "thread_management_if.h"
mbed_official 0:85f4174a8e29 31 #include "thread_br_conn_handler.h"
mbed_official 0:85f4174a8e29 32 #include "randLIB.h"
mbed_official 0:85f4174a8e29 33
mbed_official 0:85f4174a8e29 34 #include "ns_trace.h"
mbed_official 0:85f4174a8e29 35 #define TRACE_GROUP "brro"
mbed_official 0:85f4174a8e29 36
mbed_official 0:85f4174a8e29 37 #define NR_BACKHAUL_INTERFACE_PHY_DRIVER_READY 2
mbed_official 0:85f4174a8e29 38 #define NR_BACKHAUL_INTERFACE_PHY_DOWN 3
mbed_official 0:85f4174a8e29 39 #define MESH_LINK_TIMEOUT 100
mbed_official 0:85f4174a8e29 40 #define MESH_METRIC 1000
mbed_official 0:85f4174a8e29 41 #define THREAD_MAX_CHILD_COUNT 32
mbed_official 0:85f4174a8e29 42
mbed_official 82:3d9e3b7b3dcf 43 const uint8_t addr_unspecified[16] = {0};
mbed_official 0:85f4174a8e29 44 static mac_api_t *api;
mbed_official 0:85f4174a8e29 45 static eth_mac_api_t *eth_mac_api;
mbed_official 0:85f4174a8e29 46
mbed_official 0:85f4174a8e29 47 typedef enum {
mbed_official 0:85f4174a8e29 48 STATE_UNKNOWN,
mbed_official 0:85f4174a8e29 49 STATE_DISCONNECTED,
mbed_official 0:85f4174a8e29 50 STATE_LINK_READY,
mbed_official 0:85f4174a8e29 51 STATE_BOOTSTRAP,
mbed_official 0:85f4174a8e29 52 STATE_CONNECTED,
mbed_official 0:85f4174a8e29 53 STATE_MAX_VALUE
mbed_official 0:85f4174a8e29 54 } connection_state_e;
mbed_official 0:85f4174a8e29 55
mbed_official 0:85f4174a8e29 56 typedef struct {
mbed_official 0:85f4174a8e29 57 int8_t prefix_len;
mbed_official 0:85f4174a8e29 58 uint8_t prefix[16];
mbed_official 0:85f4174a8e29 59 uint8_t next_hop[16];
mbed_official 0:85f4174a8e29 60 } route_info_t;
mbed_official 0:85f4174a8e29 61
mbed_official 0:85f4174a8e29 62 /* Backhaul prefix */
mbed_official 0:85f4174a8e29 63 static uint8_t backhaul_prefix[16] = {0};
mbed_official 0:85f4174a8e29 64
mbed_official 0:85f4174a8e29 65 /* Backhaul default route information */
mbed_official 0:85f4174a8e29 66 static route_info_t backhaul_route;
mbed_official 0:85f4174a8e29 67 static int8_t br_tasklet_id = -1;
mbed_official 0:85f4174a8e29 68
mbed_official 0:85f4174a8e29 69 /* Network statistics */
mbed_official 0:85f4174a8e29 70 static nwk_stats_t nwk_stats;
mbed_official 0:85f4174a8e29 71
mbed_official 0:85f4174a8e29 72 /* Function forward declarations */
mbed_official 0:85f4174a8e29 73
mbed_official 82:3d9e3b7b3dcf 74 static link_configuration_s *thread_link_configuration_get(link_configuration_s *link_configuration);
mbed_official 0:85f4174a8e29 75 static void network_interface_event_handler(arm_event_s *event);
mbed_official 0:85f4174a8e29 76 static void mesh_network_up(void);
mbed_official 0:85f4174a8e29 77 static void eth_network_data_init(void);
mbed_official 0:85f4174a8e29 78 static net_ipv6_mode_e backhaul_bootstrap_mode = NET_IPV6_BOOTSTRAP_STATIC;
mbed_official 0:85f4174a8e29 79 static void borderrouter_tasklet(arm_event_s *event);
mbed_official 0:85f4174a8e29 80
mbed_official 0:85f4174a8e29 81 static void print_interface_addr(int id)
mbed_official 0:85f4174a8e29 82 {
mbed_official 0:85f4174a8e29 83 uint8_t address_buf[128];
mbed_official 0:85f4174a8e29 84 int address_count = 0;
mbed_official 0:85f4174a8e29 85 char buf[128];
mbed_official 0:85f4174a8e29 86
mbed_official 0:85f4174a8e29 87 if (arm_net_address_list_get(id, 128, address_buf, &address_count) == 0) {
mbed_official 0:85f4174a8e29 88 uint8_t *t_buf = address_buf;
mbed_official 0:85f4174a8e29 89 for (int i = 0; i < address_count; ++i) {
mbed_official 0:85f4174a8e29 90 ip6tos(t_buf, buf);
mbed_official 0:85f4174a8e29 91 tr_info(" [%d] %s", i, buf);
mbed_official 0:85f4174a8e29 92 t_buf += 16;
mbed_official 0:85f4174a8e29 93 }
mbed_official 0:85f4174a8e29 94 }
mbed_official 0:85f4174a8e29 95 }
mbed_official 0:85f4174a8e29 96
mbed_official 0:85f4174a8e29 97 static void eth_network_data_init()
mbed_official 0:85f4174a8e29 98 {
mbed_official 0:85f4174a8e29 99 memset(&backhaul_prefix[8], 0, 8);
mbed_official 0:85f4174a8e29 100
mbed_official 0:85f4174a8e29 101 /* Bootstrap mode for the backhaul interface */
mbed_official 0:85f4174a8e29 102 #if MBED_CONF_APP_BACKHAUL_DYNAMIC_BOOTSTRAP == 1
mbed_official 0:85f4174a8e29 103 backhaul_bootstrap_mode = NET_IPV6_BOOTSTRAP_AUTONOMOUS;
mbed_official 0:85f4174a8e29 104 tr_info("NET_IPV6_BOOTSTRAP_AUTONOMOUS");
mbed_official 0:85f4174a8e29 105
mbed_official 0:85f4174a8e29 106 #else
mbed_official 0:85f4174a8e29 107 tr_info("NET_IPV6_BOOTSTRAP_STATIC");
mbed_official 0:85f4174a8e29 108 backhaul_bootstrap_mode = NET_IPV6_BOOTSTRAP_STATIC;
mbed_official 0:85f4174a8e29 109 // done like this so that prefix can be left out in the dynamic case.
mbed_official 0:85f4174a8e29 110 const char *param = MBED_CONF_APP_BACKHAUL_PREFIX;
mbed_official 0:85f4174a8e29 111 stoip6(param, strlen(param), backhaul_prefix);
mbed_official 0:85f4174a8e29 112 tr_info("backhaul_prefix: %s", print_ipv6(backhaul_prefix));
mbed_official 0:85f4174a8e29 113
mbed_official 0:85f4174a8e29 114 /* Backhaul route configuration*/
mbed_official 0:85f4174a8e29 115 memset(&backhaul_route, 0, sizeof(backhaul_route));
mbed_official 0:85f4174a8e29 116 #ifdef MBED_CONF_APP_BACKHAUL_NEXT_HOP
mbed_official 0:85f4174a8e29 117 param = MBED_CONF_APP_BACKHAUL_NEXT_HOP;
mbed_official 0:85f4174a8e29 118 stoip6(param, strlen(param), backhaul_route.next_hop);
mbed_official 0:85f4174a8e29 119 tr_info("next hop: %s", print_ipv6(backhaul_route.next_hop));
mbed_official 0:85f4174a8e29 120 #endif
mbed_official 0:85f4174a8e29 121 param = MBED_CONF_APP_BACKHAUL_DEFAULT_ROUTE;
mbed_official 0:85f4174a8e29 122 char *prefix, route_buf[255] = {0};
mbed_official 0:85f4174a8e29 123 /* copy the config value to a non-const buffer */
mbed_official 0:85f4174a8e29 124 strncpy(route_buf, param, sizeof(route_buf) - 1);
mbed_official 0:85f4174a8e29 125 prefix = strtok(route_buf, "/");
mbed_official 0:85f4174a8e29 126 backhaul_route.prefix_len = atoi(strtok(NULL, "/"));
mbed_official 0:85f4174a8e29 127 stoip6(prefix, strlen(prefix), backhaul_route.prefix);
mbed_official 0:85f4174a8e29 128 tr_info("backhaul route prefix: %s", print_ipv6(backhaul_route.prefix));
mbed_official 0:85f4174a8e29 129 #endif
mbed_official 0:85f4174a8e29 130 }
mbed_official 0:85f4174a8e29 131
mbed_official 0:85f4174a8e29 132 static int thread_interface_up(void)
mbed_official 0:85f4174a8e29 133 {
mbed_official 0:85f4174a8e29 134 int32_t val;
mbed_official 0:85f4174a8e29 135 device_configuration_s device_config;
mbed_official 0:85f4174a8e29 136 link_configuration_s link_setup;
mbed_official 12:04fcf9df91e2 137 link_configuration_s *link_setup_ptr;
mbed_official 0:85f4174a8e29 138 int8_t thread_if_id = thread_br_conn_handler_thread_interface_id_get();
mbed_official 0:85f4174a8e29 139
mbed_official 0:85f4174a8e29 140 tr_info("thread_interface_up");
mbed_official 0:85f4174a8e29 141 memset(&device_config, 0, sizeof(device_config));
mbed_official 0:85f4174a8e29 142
mbed_official 0:85f4174a8e29 143 const char *param = MBED_CONF_APP_PSKD;
mbed_official 0:85f4174a8e29 144 uint16_t len = strlen(param);
mbed_official 0:85f4174a8e29 145 MBED_ASSERT(len > 5 && len < 33);
mbed_official 0:85f4174a8e29 146
mbed_official 0:85f4174a8e29 147 device_config.PSKd_ptr = malloc(len + 1);
mbed_official 0:85f4174a8e29 148 device_config.PSKd_len = len;
mbed_official 0:85f4174a8e29 149 memset(device_config.PSKd_ptr, 0, len + 1);
mbed_official 0:85f4174a8e29 150 memcpy(device_config.PSKd_ptr, param, len);
mbed_official 0:85f4174a8e29 151
mbed_official 12:04fcf9df91e2 152 link_setup_ptr = thread_link_configuration_get(&link_setup);
mbed_official 12:04fcf9df91e2 153 val = thread_management_node_init(thread_if_id, NULL, &device_config, link_setup_ptr);
mbed_official 0:85f4174a8e29 154
mbed_official 0:85f4174a8e29 155 if (val) {
mbed_official 0:85f4174a8e29 156 tr_error("Thread init error with code: %is\r\n", (int)val);
mbed_official 0:85f4174a8e29 157 return val;
mbed_official 0:85f4174a8e29 158 }
mbed_official 0:85f4174a8e29 159
mbed_official 0:85f4174a8e29 160 // Additional thread configurations
mbed_official 0:85f4174a8e29 161 thread_management_set_link_timeout(thread_if_id, MESH_LINK_TIMEOUT);
mbed_official 0:85f4174a8e29 162 thread_management_max_child_count(thread_if_id, THREAD_MAX_CHILD_COUNT);
mbed_official 0:85f4174a8e29 163
mbed_official 0:85f4174a8e29 164 val = arm_nwk_interface_up(thread_if_id);
mbed_official 0:85f4174a8e29 165 if (val != 0) {
mbed_official 0:85f4174a8e29 166 tr_error("mesh0 up Fail with code: %i\r\n", (int)val);
mbed_official 0:85f4174a8e29 167 return -1;
mbed_official 0:85f4174a8e29 168 } else {
mbed_official 0:85f4174a8e29 169 tr_info("mesh0 bootstrap ongoing...");
mbed_official 0:85f4174a8e29 170 }
mbed_official 0:85f4174a8e29 171 return 0;
mbed_official 0:85f4174a8e29 172 }
mbed_official 0:85f4174a8e29 173
mbed_official 82:3d9e3b7b3dcf 174 static link_configuration_s *thread_link_configuration_get(link_configuration_s *link_configuration)
mbed_official 0:85f4174a8e29 175 {
mbed_official 14:de99c96a9fae 176 #ifdef MBED_CONF_APP_THREAD_USE_STATIC_LINK_CONFIG
mbed_official 12:04fcf9df91e2 177 #if (false == MBED_CONF_APP_THREAD_USE_STATIC_LINK_CONFIG)
mbed_official 82:3d9e3b7b3dcf 178 // NOT using static link configuration values, return NULL
mbed_official 82:3d9e3b7b3dcf 179 return NULL;
mbed_official 12:04fcf9df91e2 180 #endif
mbed_official 14:de99c96a9fae 181 #endif
mbed_official 12:04fcf9df91e2 182
mbed_official 0:85f4174a8e29 183 memset(link_configuration, 0, sizeof(link_configuration_s));
mbed_official 0:85f4174a8e29 184
mbed_official 0:85f4174a8e29 185 MBED_ASSERT(strlen(MBED_CONF_APP_NETWORK_NAME) > 0 && strlen(MBED_CONF_APP_NETWORK_NAME) < 17);
mbed_official 0:85f4174a8e29 186 const uint8_t master_key[] = MBED_CONF_APP_THREAD_MASTER_KEY;
mbed_official 0:85f4174a8e29 187 MBED_ASSERT(sizeof(master_key) == 16);
mbed_official 0:85f4174a8e29 188 const uint8_t pskc[] = MBED_CONF_APP_PSKC;
mbed_official 0:85f4174a8e29 189 MBED_ASSERT(sizeof(pskc) == 16);
mbed_official 0:85f4174a8e29 190
mbed_official 0:85f4174a8e29 191 const uint8_t extented_panid[] = MBED_CONF_APP_EXTENDED_PAN_ID;
mbed_official 0:85f4174a8e29 192 MBED_ASSERT(sizeof(extented_panid) == 8);
mbed_official 0:85f4174a8e29 193 const uint8_t mesh_local_prefix[] = MBED_CONF_APP_MESH_LOCAL_PREFIX;
mbed_official 0:85f4174a8e29 194 MBED_ASSERT(sizeof(mesh_local_prefix) == 8);
mbed_official 0:85f4174a8e29 195
mbed_official 0:85f4174a8e29 196 memcpy(link_configuration->extented_pan_id, extented_panid, sizeof(extented_panid));
mbed_official 0:85f4174a8e29 197 memcpy(link_configuration->mesh_local_ula_prefix, mesh_local_prefix, sizeof(mesh_local_prefix));
mbed_official 0:85f4174a8e29 198
mbed_official 0:85f4174a8e29 199 link_configuration->panId = MBED_CONF_APP_PAN_ID;
mbed_official 0:85f4174a8e29 200 tr_info("PAN ID %x", link_configuration->panId);
mbed_official 0:85f4174a8e29 201 memcpy(link_configuration->name, MBED_CONF_APP_NETWORK_NAME, strlen(MBED_CONF_APP_NETWORK_NAME));
mbed_official 0:85f4174a8e29 202 link_configuration->timestamp = MBED_CONF_APP_COMMISSIONING_DATASET_TIMESTAMP;
mbed_official 0:85f4174a8e29 203
mbed_official 0:85f4174a8e29 204 memcpy(link_configuration->PSKc, pskc, sizeof(pskc));
mbed_official 0:85f4174a8e29 205 memcpy(link_configuration->master_key, master_key, sizeof(master_key));
mbed_official 57:b201a7a8ddc9 206 link_configuration->securityPolicy = MBED_CONF_APP_THREAD_SECURITY_POLICY;
mbed_official 0:85f4174a8e29 207
mbed_official 0:85f4174a8e29 208 link_configuration->rfChannel = MBED_CONF_APP_RF_CHANNEL;
mbed_official 0:85f4174a8e29 209 tr_info("RF channel %d", link_configuration->rfChannel);
mbed_official 0:85f4174a8e29 210 link_configuration->channel_page = MBED_CONF_APP_RF_CHANNEL_PAGE;
mbed_official 0:85f4174a8e29 211 uint32_t channel_mask = MBED_CONF_APP_RF_CHANNEL_MASK;
mbed_official 0:85f4174a8e29 212 common_write_32_bit(channel_mask, link_configuration->channel_mask);
mbed_official 0:85f4174a8e29 213
mbed_official 0:85f4174a8e29 214 link_configuration->key_rotation = 3600;
mbed_official 0:85f4174a8e29 215 link_configuration->key_sequence = 0;
mbed_official 12:04fcf9df91e2 216
mbed_official 12:04fcf9df91e2 217 return link_configuration;
mbed_official 0:85f4174a8e29 218 }
mbed_official 0:85f4174a8e29 219
mbed_official 0:85f4174a8e29 220 // ethernet interface
mbed_official 0:85f4174a8e29 221 static void network_interface_event_handler(arm_event_s *event)
mbed_official 0:85f4174a8e29 222 {
mbed_official 0:85f4174a8e29 223 bool connectStatus = false;
mbed_official 0:85f4174a8e29 224 arm_nwk_interface_status_type_e status = (arm_nwk_interface_status_type_e)event->event_data;
mbed_official 0:85f4174a8e29 225 switch (status) {
mbed_official 0:85f4174a8e29 226 case (ARM_NWK_BOOTSTRAP_READY): { // Interface configured Bootstrap is ready
mbed_official 0:85f4174a8e29 227
mbed_official 0:85f4174a8e29 228 connectStatus = true;
mbed_official 0:85f4174a8e29 229 tr_info("BR interface_id: %d", thread_br_conn_handler_eth_interface_id_get());
mbed_official 0:85f4174a8e29 230 if (-1 != thread_br_conn_handler_eth_interface_id_get()) {
mbed_official 0:85f4174a8e29 231 // metric set to high priority
mbed_official 0:85f4174a8e29 232 if (0 != arm_net_interface_set_metric(thread_br_conn_handler_eth_interface_id_get(), 0)) {
mbed_official 0:85f4174a8e29 233 tr_warn("Failed to set metric for eth0.");
mbed_official 0:85f4174a8e29 234 }
mbed_official 0:85f4174a8e29 235
mbed_official 0:85f4174a8e29 236 if (backhaul_bootstrap_mode == NET_IPV6_BOOTSTRAP_STATIC) {
mbed_official 0:85f4174a8e29 237 uint8_t *next_hop_ptr;
mbed_official 82:3d9e3b7b3dcf 238 if (memcmp(backhaul_route.next_hop, addr_unspecified, 16) == 0) {
mbed_official 0:85f4174a8e29 239 next_hop_ptr = NULL;
mbed_official 82:3d9e3b7b3dcf 240 } else {
mbed_official 0:85f4174a8e29 241 next_hop_ptr = backhaul_route.next_hop;
mbed_official 0:85f4174a8e29 242 }
mbed_official 0:85f4174a8e29 243 tr_debug("Default route prefix: %s/%d", print_ipv6(backhaul_route.prefix),
mbed_official 0:85f4174a8e29 244 backhaul_route.prefix_len);
mbed_official 0:85f4174a8e29 245 tr_debug("Default route next hop: %s", print_ipv6(backhaul_route.next_hop));
mbed_official 0:85f4174a8e29 246 arm_net_route_add(backhaul_route.prefix,
mbed_official 0:85f4174a8e29 247 backhaul_route.prefix_len,
mbed_official 0:85f4174a8e29 248 next_hop_ptr, 0xffffffff, 128,
mbed_official 0:85f4174a8e29 249 thread_br_conn_handler_eth_interface_id_get());
mbed_official 0:85f4174a8e29 250 }
mbed_official 0:85f4174a8e29 251 tr_info("Backhaul interface addresses:");
mbed_official 0:85f4174a8e29 252 print_interface_addr(thread_br_conn_handler_eth_interface_id_get());
mbed_official 0:85f4174a8e29 253 thread_br_conn_handler_ethernet_connection_update(connectStatus);
mbed_official 0:85f4174a8e29 254 }
mbed_official 0:85f4174a8e29 255 break;
mbed_official 0:85f4174a8e29 256 }
mbed_official 0:85f4174a8e29 257 case (ARM_NWK_RPL_INSTANCE_FLOODING_READY): // RPL instance have been flooded
mbed_official 0:85f4174a8e29 258 tr_info("\rRPL instance have been flooded\r\n");
mbed_official 0:85f4174a8e29 259 break;
mbed_official 0:85f4174a8e29 260 case (ARM_NWK_SET_DOWN_COMPLETE): // Interface DOWN command successfully
mbed_official 0:85f4174a8e29 261 break;
mbed_official 0:85f4174a8e29 262 case (ARM_NWK_NWK_SCAN_FAIL): // Interface have not detect any valid network
mbed_official 0:85f4174a8e29 263 tr_warning("\rmesh0 haven't detect any valid nw\r\n");
mbed_official 0:85f4174a8e29 264 break;
mbed_official 0:85f4174a8e29 265 case (ARM_NWK_IP_ADDRESS_ALLOCATION_FAIL): // IP address allocation fail(ND, DHCPv4 or DHCPv6)
mbed_official 0:85f4174a8e29 266 tr_error("\rNO GP address detected");
mbed_official 0:85f4174a8e29 267 break;
mbed_official 0:85f4174a8e29 268 case (ARM_NWK_DUPLICATE_ADDRESS_DETECTED): // User specific GP16 was not valid
mbed_official 0:85f4174a8e29 269 tr_error("\rEthernet IPv6 Duplicate addr detected!\r\n");
mbed_official 0:85f4174a8e29 270 break;
mbed_official 0:85f4174a8e29 271 case (ARM_NWK_AUHTENTICATION_START_FAIL): // No valid Authentication server detected behind access point ;
mbed_official 0:85f4174a8e29 272 tr_error("\rNo valid ath server detected behind AP\r\n");
mbed_official 0:85f4174a8e29 273 break;
mbed_official 0:85f4174a8e29 274 case (ARM_NWK_AUHTENTICATION_FAIL): // Network authentication fail by Handshake
mbed_official 0:85f4174a8e29 275 tr_error("\rNetwork authentication fail");
mbed_official 0:85f4174a8e29 276 break;
mbed_official 0:85f4174a8e29 277 case (ARM_NWK_NWK_CONNECTION_DOWN): // No connection between Access point or Default Router
mbed_official 0:85f4174a8e29 278 tr_warning("\rPrefix timeout\r\n");
mbed_official 0:85f4174a8e29 279 break;
mbed_official 0:85f4174a8e29 280 case (ARM_NWK_NWK_PARENT_POLL_FAIL): // Sleepy host poll fail 3 time
mbed_official 0:85f4174a8e29 281 tr_warning("\rParent poll fail\r\n");
mbed_official 0:85f4174a8e29 282 break;
mbed_official 0:85f4174a8e29 283 case (ARM_NWK_PHY_CONNECTION_DOWN): // Interface PHY cable off or serial port interface not respond anymore
mbed_official 0:85f4174a8e29 284 tr_error("\reth0 down\r\n");
mbed_official 0:85f4174a8e29 285 break;
mbed_official 0:85f4174a8e29 286 default:
mbed_official 0:85f4174a8e29 287 tr_warning("\rUnkown nw if event (type: %02x, id: %02x, data: %02x)\r\n", event->event_type, event->event_id, (unsigned int)event->event_data);
mbed_official 0:85f4174a8e29 288 break;
mbed_official 0:85f4174a8e29 289 }
mbed_official 0:85f4174a8e29 290 }
mbed_official 0:85f4174a8e29 291
mbed_official 0:85f4174a8e29 292 void thread_interface_event_handler(arm_event_s *event)
mbed_official 0:85f4174a8e29 293 {
mbed_official 0:85f4174a8e29 294 bool connectStatus = false;
mbed_official 0:85f4174a8e29 295 arm_nwk_interface_status_type_e status = (arm_nwk_interface_status_type_e)event->event_data;
mbed_official 0:85f4174a8e29 296 switch (status) {
mbed_official 0:85f4174a8e29 297 case (ARM_NWK_BOOTSTRAP_READY): { // Interface configured Bootstrap is ready
mbed_official 0:85f4174a8e29 298 connectStatus = true;
mbed_official 0:85f4174a8e29 299 tr_info("Thread bootstrap ready");
mbed_official 0:85f4174a8e29 300
mbed_official 0:85f4174a8e29 301 if (arm_net_interface_set_metric(thread_br_conn_handler_thread_interface_id_get(), MESH_METRIC) != 0) {
mbed_official 0:85f4174a8e29 302 tr_warn("Failed to set metric for mesh0.");
mbed_official 0:85f4174a8e29 303 }
mbed_official 0:85f4174a8e29 304
mbed_official 0:85f4174a8e29 305 tr_info("RF interface addresses:");
mbed_official 0:85f4174a8e29 306 print_interface_addr(thread_br_conn_handler_thread_interface_id_get());
mbed_official 0:85f4174a8e29 307
mbed_official 0:85f4174a8e29 308 break;
mbed_official 0:85f4174a8e29 309 }
mbed_official 0:85f4174a8e29 310 case (ARM_NWK_SET_DOWN_COMPLETE):
mbed_official 0:85f4174a8e29 311 tr_info("Thread interface down");
mbed_official 0:85f4174a8e29 312 break;
mbed_official 0:85f4174a8e29 313 default:
mbed_official 0:85f4174a8e29 314 tr_warning("Unkown nw if event (type: %02x, id: %02x, data: %02x)\r\n", event->event_type, event->event_id, (unsigned int)event->event_data);
mbed_official 0:85f4174a8e29 315 break;
mbed_official 0:85f4174a8e29 316 }
mbed_official 0:85f4174a8e29 317
mbed_official 0:85f4174a8e29 318 thread_br_conn_handler_thread_connection_update(connectStatus);
mbed_official 0:85f4174a8e29 319 }
mbed_official 0:85f4174a8e29 320
mbed_official 0:85f4174a8e29 321 static void mesh_network_up()
mbed_official 0:85f4174a8e29 322 {
mbed_official 0:85f4174a8e29 323 tr_debug("Create Mesh Interface");
mbed_official 0:85f4174a8e29 324
mbed_official 0:85f4174a8e29 325 int status;
mbed_official 0:85f4174a8e29 326 int8_t thread_if_id;
mbed_official 0:85f4174a8e29 327
mbed_official 0:85f4174a8e29 328 thread_if_id = arm_nwk_interface_lowpan_init(api, "ThreadInterface");
mbed_official 0:85f4174a8e29 329 tr_info("thread_if_id: %d", thread_if_id);
mbed_official 82:3d9e3b7b3dcf 330 MBED_ASSERT(thread_if_id >= 0);
mbed_official 0:85f4174a8e29 331
mbed_official 0:85f4174a8e29 332 if (thread_if_id < 0) {
mbed_official 0:85f4174a8e29 333 tr_error("arm_nwk_interface_lowpan_init() failed");
mbed_official 0:85f4174a8e29 334 return;
mbed_official 0:85f4174a8e29 335 }
mbed_official 0:85f4174a8e29 336
mbed_official 0:85f4174a8e29 337 status = arm_nwk_interface_configure_6lowpan_bootstrap_set(
mbed_official 82:3d9e3b7b3dcf 338 thread_if_id,
mbed_official 82:3d9e3b7b3dcf 339 NET_6LOWPAN_ROUTER,
mbed_official 82:3d9e3b7b3dcf 340 NET_6LOWPAN_THREAD);
mbed_official 0:85f4174a8e29 341
mbed_official 0:85f4174a8e29 342 if (status < 0) {
mbed_official 0:85f4174a8e29 343 tr_error("arm_nwk_interface_configure_6lowpan_bootstrap_set() failed");
mbed_official 0:85f4174a8e29 344 return;
mbed_official 0:85f4174a8e29 345 }
mbed_official 0:85f4174a8e29 346
mbed_official 0:85f4174a8e29 347 thread_br_conn_handler_thread_interface_id_set(thread_if_id);
mbed_official 0:85f4174a8e29 348
mbed_official 0:85f4174a8e29 349 status = thread_interface_up();
mbed_official 0:85f4174a8e29 350 MBED_ASSERT(!status);
mbed_official 0:85f4174a8e29 351 if (status) {
mbed_official 0:85f4174a8e29 352 tr_error("thread_interface_up() failed: %d", status);
mbed_official 0:85f4174a8e29 353 }
mbed_official 0:85f4174a8e29 354 }
mbed_official 0:85f4174a8e29 355
mbed_official 0:85f4174a8e29 356 void thread_rf_init()
mbed_official 0:85f4174a8e29 357 {
mbed_official 0:85f4174a8e29 358 mac_description_storage_size_t storage_sizes;
mbed_official 0:85f4174a8e29 359 storage_sizes.key_lookup_size = 1;
mbed_official 0:85f4174a8e29 360 storage_sizes.key_usage_size = 1;
mbed_official 0:85f4174a8e29 361 storage_sizes.device_decription_table_size = 32;
mbed_official 0:85f4174a8e29 362 storage_sizes.key_description_table_size = 6;
mbed_official 0:85f4174a8e29 363
mbed_official 0:85f4174a8e29 364 int8_t rf_driver_id = rf_device_register();
mbed_official 82:3d9e3b7b3dcf 365 MBED_ASSERT(rf_driver_id >= 0);
mbed_official 82:3d9e3b7b3dcf 366 if (rf_driver_id >= 0) {
mbed_official 0:85f4174a8e29 367 randLIB_seed_random();
mbed_official 0:85f4174a8e29 368
mbed_official 0:85f4174a8e29 369 if (!api) {
mbed_official 0:85f4174a8e29 370 api = ns_sw_mac_create(rf_driver_id, &storage_sizes);
mbed_official 0:85f4174a8e29 371 }
mbed_official 0:85f4174a8e29 372 }
mbed_official 0:85f4174a8e29 373 }
mbed_official 0:85f4174a8e29 374
mbed_official 7:571f9a90b972 375 void border_router_tasklet_start(void)
mbed_official 0:85f4174a8e29 376 {
mbed_official 0:85f4174a8e29 377 thread_rf_init();
mbed_official 0:85f4174a8e29 378 protocol_stats_start(&nwk_stats);
mbed_official 0:85f4174a8e29 379
mbed_official 0:85f4174a8e29 380 eventOS_event_handler_create(
mbed_official 0:85f4174a8e29 381 &borderrouter_tasklet,
mbed_official 0:85f4174a8e29 382 ARM_LIB_TASKLET_INIT_EVENT);
mbed_official 0:85f4174a8e29 383 }
mbed_official 0:85f4174a8e29 384
mbed_official 0:85f4174a8e29 385
mbed_official 0:85f4174a8e29 386 static void borderrouter_backhaul_phy_status_cb(uint8_t link_up, int8_t driver_id)
mbed_official 0:85f4174a8e29 387 {
mbed_official 0:85f4174a8e29 388 arm_event_s event = {
mbed_official 0:85f4174a8e29 389 .sender = br_tasklet_id,
mbed_official 0:85f4174a8e29 390 .receiver = br_tasklet_id,
mbed_official 0:85f4174a8e29 391 .priority = ARM_LIB_MED_PRIORITY_EVENT,
mbed_official 0:85f4174a8e29 392 .event_type = APPLICATION_EVENT,
mbed_official 82:3d9e3b7b3dcf 393 .event_id = NR_BACKHAUL_INTERFACE_PHY_DOWN,
mbed_official 0:85f4174a8e29 394 .event_data = driver_id
mbed_official 0:85f4174a8e29 395 };
mbed_official 0:85f4174a8e29 396
mbed_official 0:85f4174a8e29 397 if (link_up) {
mbed_official 0:85f4174a8e29 398 event.event_id = NR_BACKHAUL_INTERFACE_PHY_DRIVER_READY;
mbed_official 0:85f4174a8e29 399 }
mbed_official 0:85f4174a8e29 400
mbed_official 0:85f4174a8e29 401 eventOS_event_send(&event);
mbed_official 0:85f4174a8e29 402 }
mbed_official 0:85f4174a8e29 403
mbed_official 0:85f4174a8e29 404 static int backhaul_interface_up(int8_t driver_id)
mbed_official 0:85f4174a8e29 405 {
mbed_official 0:85f4174a8e29 406 int retval = -1;
mbed_official 0:85f4174a8e29 407 tr_debug("backhaul_interface_up: %i\n", driver_id);
mbed_official 0:85f4174a8e29 408 int8_t backhaul_if_id = thread_br_conn_handler_eth_interface_id_get();
mbed_official 0:85f4174a8e29 409 if (backhaul_if_id != -1) {
mbed_official 0:85f4174a8e29 410 tr_debug("Border RouterInterface already at active state\n");
mbed_official 0:85f4174a8e29 411 } else {
mbed_official 0:85f4174a8e29 412
mbed_official 0:85f4174a8e29 413 if (!eth_mac_api) {
mbed_official 0:85f4174a8e29 414 eth_mac_api = ethernet_mac_create(driver_id);
mbed_official 0:85f4174a8e29 415 }
mbed_official 0:85f4174a8e29 416
mbed_official 0:85f4174a8e29 417 backhaul_if_id = arm_nwk_interface_ethernet_init(eth_mac_api, "bh0");
mbed_official 0:85f4174a8e29 418
mbed_official 0:85f4174a8e29 419 MBED_ASSERT(backhaul_if_id >= 0);
mbed_official 0:85f4174a8e29 420 if (backhaul_if_id >= 0) {
mbed_official 0:85f4174a8e29 421 tr_debug("Backhaul interface ID: %d", backhaul_if_id);
mbed_official 0:85f4174a8e29 422 thread_br_conn_handler_eth_interface_id_set(backhaul_if_id);
mbed_official 0:85f4174a8e29 423 arm_nwk_interface_configure_ipv6_bootstrap_set(
mbed_official 0:85f4174a8e29 424 backhaul_if_id, backhaul_bootstrap_mode, backhaul_prefix);
mbed_official 0:85f4174a8e29 425 arm_nwk_interface_up(backhaul_if_id);
mbed_official 0:85f4174a8e29 426 retval = 0;
mbed_official 82:3d9e3b7b3dcf 427 } else {
mbed_official 0:85f4174a8e29 428 tr_debug("Could not init ethernet");
mbed_official 0:85f4174a8e29 429 }
mbed_official 0:85f4174a8e29 430 }
mbed_official 0:85f4174a8e29 431 return retval;
mbed_official 0:85f4174a8e29 432 }
mbed_official 0:85f4174a8e29 433
mbed_official 0:85f4174a8e29 434 static int backhaul_interface_down(void)
mbed_official 0:85f4174a8e29 435 {
mbed_official 0:85f4174a8e29 436 int retval = -1;
mbed_official 0:85f4174a8e29 437 if (thread_br_conn_handler_eth_interface_id_get() != -1) {
mbed_official 0:85f4174a8e29 438 arm_nwk_interface_down(thread_br_conn_handler_eth_interface_id_get());
mbed_official 0:85f4174a8e29 439 thread_br_conn_handler_eth_interface_id_set(-1);
mbed_official 0:85f4174a8e29 440 thread_br_conn_handler_ethernet_connection_update(false);
mbed_official 0:85f4174a8e29 441 retval = 0;
mbed_official 82:3d9e3b7b3dcf 442 } else {
mbed_official 0:85f4174a8e29 443 tr_debug("Could not set eth down");
mbed_official 0:85f4174a8e29 444 }
mbed_official 0:85f4174a8e29 445 return retval;
mbed_official 0:85f4174a8e29 446 }
mbed_official 0:85f4174a8e29 447
mbed_official 16:a7a08d8ec5ac 448 #if MBED_CONF_APP_DEBUG_TRACE
mbed_official 16:a7a08d8ec5ac 449 static void print_interface_addresses(void)
mbed_official 16:a7a08d8ec5ac 450 {
mbed_official 16:a7a08d8ec5ac 451 tr_info("Backhaul interface addresses:");
mbed_official 16:a7a08d8ec5ac 452 print_interface_addr(thread_br_conn_handler_eth_interface_id_get());
mbed_official 16:a7a08d8ec5ac 453
mbed_official 16:a7a08d8ec5ac 454 tr_info("RF interface addresses:");
mbed_official 16:a7a08d8ec5ac 455 print_interface_addr(thread_br_conn_handler_thread_interface_id_get());
mbed_official 16:a7a08d8ec5ac 456 }
mbed_official 16:a7a08d8ec5ac 457 #endif
mbed_official 16:a7a08d8ec5ac 458
mbed_official 0:85f4174a8e29 459 /**
mbed_official 0:85f4174a8e29 460 * \brief Border Router Main Tasklet
mbed_official 0:85f4174a8e29 461 *
mbed_official 0:85f4174a8e29 462 * Tasklet Handle next items:
mbed_official 0:85f4174a8e29 463 *
mbed_official 0:85f4174a8e29 464 * - EV_INIT event: Set Certificate Chain, RF Interface Boot UP, multicast Init
mbed_official 0:85f4174a8e29 465 * - SYSTEM_TIMER event: For RF interface Handshake purpose
mbed_official 0:85f4174a8e29 466 *
mbed_official 0:85f4174a8e29 467 */
mbed_official 0:85f4174a8e29 468 static void borderrouter_tasklet(arm_event_s *event)
mbed_official 0:85f4174a8e29 469 {
mbed_official 0:85f4174a8e29 470 arm_library_event_type_e event_type;
mbed_official 0:85f4174a8e29 471 event_type = (arm_library_event_type_e)event->event_type;
mbed_official 0:85f4174a8e29 472
mbed_official 0:85f4174a8e29 473 switch (event_type) {
mbed_official 0:85f4174a8e29 474 case ARM_LIB_NWK_INTERFACE_EVENT:
mbed_official 0:85f4174a8e29 475
mbed_official 0:85f4174a8e29 476 if (event->event_id == thread_br_conn_handler_eth_interface_id_get()) {
mbed_official 0:85f4174a8e29 477 network_interface_event_handler(event);
mbed_official 0:85f4174a8e29 478 } else {
mbed_official 0:85f4174a8e29 479 thread_interface_event_handler(event);
mbed_official 0:85f4174a8e29 480 }
mbed_official 0:85f4174a8e29 481
mbed_official 0:85f4174a8e29 482 break;
mbed_official 0:85f4174a8e29 483 // comes from the backhaul_driver_init.
mbed_official 0:85f4174a8e29 484 case APPLICATION_EVENT:
mbed_official 0:85f4174a8e29 485 if (event->event_id == NR_BACKHAUL_INTERFACE_PHY_DRIVER_READY) {
mbed_official 0:85f4174a8e29 486 int8_t net_backhaul_id = (int8_t) event->event_data;
mbed_official 0:85f4174a8e29 487
mbed_official 82:3d9e3b7b3dcf 488 tr_debug("Backhaul driver ID: %d", net_backhaul_id);
mbed_official 82:3d9e3b7b3dcf 489
mbed_official 0:85f4174a8e29 490 if (backhaul_interface_up(net_backhaul_id) != 0) {
mbed_official 0:85f4174a8e29 491 tr_debug("Backhaul bootstrap start failed");
mbed_official 0:85f4174a8e29 492 } else {
mbed_official 0:85f4174a8e29 493 tr_debug("Backhaul bootstrap started");
mbed_official 0:85f4174a8e29 494 }
mbed_official 0:85f4174a8e29 495 } else if (event->event_id == NR_BACKHAUL_INTERFACE_PHY_DOWN) {
mbed_official 82:3d9e3b7b3dcf 496 tr_debug("Backhaul driver ID: %d", (int8_t) event->event_data);
mbed_official 0:85f4174a8e29 497 if (backhaul_interface_down() != 0) {
mbed_official 0:85f4174a8e29 498 } else {
mbed_official 0:85f4174a8e29 499 tr_debug("Backhaul interface is down");
mbed_official 0:85f4174a8e29 500 }
mbed_official 0:85f4174a8e29 501 }
mbed_official 0:85f4174a8e29 502 break;
mbed_official 0:85f4174a8e29 503
mbed_official 0:85f4174a8e29 504 case ARM_LIB_TASKLET_INIT_EVENT:
mbed_official 82:3d9e3b7b3dcf 505 appl_info_trace();
mbed_official 0:85f4174a8e29 506 br_tasklet_id = event->receiver;
mbed_official 0:85f4174a8e29 507 thread_br_conn_handler_init();
mbed_official 0:85f4174a8e29 508 eth_network_data_init();
mbed_official 0:85f4174a8e29 509 backhaul_driver_init(borderrouter_backhaul_phy_status_cb);
mbed_official 0:85f4174a8e29 510 mesh_network_up();
mbed_official 0:85f4174a8e29 511 eventOS_event_timer_request(9, ARM_LIB_SYSTEM_TIMER_EVENT, br_tasklet_id, 20000);
mbed_official 0:85f4174a8e29 512 break;
mbed_official 0:85f4174a8e29 513
mbed_official 0:85f4174a8e29 514 case ARM_LIB_SYSTEM_TIMER_EVENT:
mbed_official 0:85f4174a8e29 515 eventOS_event_timer_cancel(event->event_id, event->receiver);
mbed_official 0:85f4174a8e29 516
mbed_official 0:85f4174a8e29 517 if (event->event_id == 9) {
mbed_official 16:a7a08d8ec5ac 518 #if MBED_CONF_APP_DEBUG_TRACE
mbed_official 0:85f4174a8e29 519 arm_print_routing_table();
mbed_official 0:85f4174a8e29 520 arm_print_neigh_cache();
mbed_official 13:993808eb2e9c 521 print_memory_stats();
mbed_official 16:a7a08d8ec5ac 522 // Trace interface addresses. This trace can be removed if nanostack prints added/removed
mbed_official 16:a7a08d8ec5ac 523 // addresses.
mbed_official 16:a7a08d8ec5ac 524 print_interface_addresses();
mbed_official 0:85f4174a8e29 525 #endif
mbed_official 0:85f4174a8e29 526 eventOS_event_timer_request(9, ARM_LIB_SYSTEM_TIMER_EVENT, br_tasklet_id, 20000);
mbed_official 0:85f4174a8e29 527 }
mbed_official 0:85f4174a8e29 528 break;
mbed_official 0:85f4174a8e29 529
mbed_official 0:85f4174a8e29 530 default:
mbed_official 0:85f4174a8e29 531 break;
mbed_official 0:85f4174a8e29 532 }
mbed_official 0:85f4174a8e29 533 }
mbed_official 0:85f4174a8e29 534
mbed_official 0:85f4174a8e29 535 #endif // MBED_CONF_APP_MESH_MODE
mbed_official 0:85f4174a8e29 536