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 May 31 11:45:18 2017 +0100
Revision:
14:de99c96a9fae
Parent:
13:993808eb2e9c
Child:
16:a7a08d8ec5ac
Merge pull request #39 from ARMmbed/radio_driver_updates

Radio driver updates
.
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 0:85f4174a8e29 7 #if MBED_CONF_APP_MESH_MODE == THREAD
mbed_official 0:85f4174a8e29 8
mbed_official 0:85f4174a8e29 9 #include <string.h>
mbed_official 0:85f4174a8e29 10 #include <stdlib.h>
mbed_official 0:85f4174a8e29 11 #include <mbed_assert.h>
mbed_official 0:85f4174a8e29 12 #include "ns_types.h"
mbed_official 0:85f4174a8e29 13 #include "eventOS_event.h"
mbed_official 0:85f4174a8e29 14 #include "eventOS_event_timer.h"
mbed_official 0:85f4174a8e29 15 #include "eventOS_scheduler.h"
mbed_official 0:85f4174a8e29 16 #include "platform/arm_hal_timer.h"
mbed_official 0:85f4174a8e29 17 #include "borderrouter_tasklet.h"
mbed_official 0:85f4174a8e29 18 #include "borderrouter_helpers.h"
mbed_official 0:85f4174a8e29 19 #include "net_interface.h"
mbed_official 0:85f4174a8e29 20 #include "rf_wrapper.h"
mbed_official 0:85f4174a8e29 21 #include "nwk_stats_api.h"
mbed_official 0:85f4174a8e29 22 #include "ip6string.h"
mbed_official 0:85f4174a8e29 23 #include "ethernet_mac_api.h"
mbed_official 0:85f4174a8e29 24 #include "mac_api.h"
mbed_official 0:85f4174a8e29 25 #include "sw_mac.h"
mbed_official 0:85f4174a8e29 26 #include "mbed_interface.h"
mbed_official 0:85f4174a8e29 27 #include "common_functions.h"
mbed_official 0:85f4174a8e29 28 #include "thread_management_if.h"
mbed_official 0:85f4174a8e29 29 #include "thread_br_conn_handler.h"
mbed_official 0:85f4174a8e29 30 #include "randLIB.h"
mbed_official 0:85f4174a8e29 31
mbed_official 0:85f4174a8e29 32 #include "ns_trace.h"
mbed_official 0:85f4174a8e29 33 #define TRACE_GROUP "brro"
mbed_official 0:85f4174a8e29 34
mbed_official 0:85f4174a8e29 35 #define NR_BACKHAUL_INTERFACE_PHY_DRIVER_READY 2
mbed_official 0:85f4174a8e29 36 #define NR_BACKHAUL_INTERFACE_PHY_DOWN 3
mbed_official 0:85f4174a8e29 37 #define MESH_LINK_TIMEOUT 100
mbed_official 0:85f4174a8e29 38 #define MESH_METRIC 1000
mbed_official 0:85f4174a8e29 39 #define THREAD_MAX_CHILD_COUNT 32
mbed_official 0:85f4174a8e29 40
mbed_official 0:85f4174a8e29 41 static mac_api_t *api;
mbed_official 0:85f4174a8e29 42 static eth_mac_api_t *eth_mac_api;
mbed_official 0:85f4174a8e29 43
mbed_official 0:85f4174a8e29 44 typedef enum {
mbed_official 0:85f4174a8e29 45 STATE_UNKNOWN,
mbed_official 0:85f4174a8e29 46 STATE_DISCONNECTED,
mbed_official 0:85f4174a8e29 47 STATE_LINK_READY,
mbed_official 0:85f4174a8e29 48 STATE_BOOTSTRAP,
mbed_official 0:85f4174a8e29 49 STATE_CONNECTED,
mbed_official 0:85f4174a8e29 50 STATE_MAX_VALUE
mbed_official 0:85f4174a8e29 51 } connection_state_e;
mbed_official 0:85f4174a8e29 52
mbed_official 0:85f4174a8e29 53 typedef struct {
mbed_official 0:85f4174a8e29 54 int8_t prefix_len;
mbed_official 0:85f4174a8e29 55 uint8_t prefix[16];
mbed_official 0:85f4174a8e29 56 uint8_t next_hop[16];
mbed_official 0:85f4174a8e29 57 } route_info_t;
mbed_official 0:85f4174a8e29 58
mbed_official 0:85f4174a8e29 59 /* Backhaul prefix */
mbed_official 0:85f4174a8e29 60 static uint8_t backhaul_prefix[16] = {0};
mbed_official 0:85f4174a8e29 61
mbed_official 0:85f4174a8e29 62 /* Backhaul default route information */
mbed_official 0:85f4174a8e29 63 static route_info_t backhaul_route;
mbed_official 0:85f4174a8e29 64 static int8_t br_tasklet_id = -1;
mbed_official 0:85f4174a8e29 65
mbed_official 0:85f4174a8e29 66 /* Network statistics */
mbed_official 0:85f4174a8e29 67 static nwk_stats_t nwk_stats;
mbed_official 0:85f4174a8e29 68
mbed_official 0:85f4174a8e29 69 /* Function forward declarations */
mbed_official 0:85f4174a8e29 70
mbed_official 12:04fcf9df91e2 71 static link_configuration_s* thread_link_configuration_get(link_configuration_s *link_configuration);
mbed_official 0:85f4174a8e29 72 static void network_interface_event_handler(arm_event_s *event);
mbed_official 0:85f4174a8e29 73 static void mesh_network_up(void);
mbed_official 0:85f4174a8e29 74 static void eth_network_data_init(void);
mbed_official 0:85f4174a8e29 75 static net_ipv6_mode_e backhaul_bootstrap_mode = NET_IPV6_BOOTSTRAP_STATIC;
mbed_official 0:85f4174a8e29 76 static void borderrouter_tasklet(arm_event_s *event);
mbed_official 0:85f4174a8e29 77
mbed_official 0:85f4174a8e29 78 static void print_interface_addr(int id)
mbed_official 0:85f4174a8e29 79 {
mbed_official 0:85f4174a8e29 80 uint8_t address_buf[128];
mbed_official 0:85f4174a8e29 81 int address_count = 0;
mbed_official 0:85f4174a8e29 82 char buf[128];
mbed_official 0:85f4174a8e29 83
mbed_official 0:85f4174a8e29 84 if (arm_net_address_list_get(id, 128, address_buf, &address_count) == 0) {
mbed_official 0:85f4174a8e29 85 uint8_t *t_buf = address_buf;
mbed_official 0:85f4174a8e29 86 for (int i = 0; i < address_count; ++i) {
mbed_official 0:85f4174a8e29 87 ip6tos(t_buf, buf);
mbed_official 0:85f4174a8e29 88 tr_info(" [%d] %s", i, buf);
mbed_official 0:85f4174a8e29 89 t_buf += 16;
mbed_official 0:85f4174a8e29 90 }
mbed_official 0:85f4174a8e29 91 }
mbed_official 0:85f4174a8e29 92 }
mbed_official 0:85f4174a8e29 93
mbed_official 0:85f4174a8e29 94 static void eth_network_data_init()
mbed_official 0:85f4174a8e29 95 {
mbed_official 0:85f4174a8e29 96 memset(&backhaul_prefix[8], 0, 8);
mbed_official 0:85f4174a8e29 97
mbed_official 0:85f4174a8e29 98 /* Bootstrap mode for the backhaul interface */
mbed_official 0:85f4174a8e29 99 #if MBED_CONF_APP_BACKHAUL_DYNAMIC_BOOTSTRAP == 1
mbed_official 0:85f4174a8e29 100 backhaul_bootstrap_mode = NET_IPV6_BOOTSTRAP_AUTONOMOUS;
mbed_official 0:85f4174a8e29 101 tr_info("NET_IPV6_BOOTSTRAP_AUTONOMOUS");
mbed_official 0:85f4174a8e29 102
mbed_official 0:85f4174a8e29 103 #else
mbed_official 0:85f4174a8e29 104 tr_info("NET_IPV6_BOOTSTRAP_STATIC");
mbed_official 0:85f4174a8e29 105 backhaul_bootstrap_mode = NET_IPV6_BOOTSTRAP_STATIC;
mbed_official 0:85f4174a8e29 106 // done like this so that prefix can be left out in the dynamic case.
mbed_official 0:85f4174a8e29 107 const char *param = MBED_CONF_APP_BACKHAUL_PREFIX;
mbed_official 0:85f4174a8e29 108 stoip6(param, strlen(param), backhaul_prefix);
mbed_official 0:85f4174a8e29 109 tr_info("backhaul_prefix: %s", print_ipv6(backhaul_prefix));
mbed_official 0:85f4174a8e29 110
mbed_official 0:85f4174a8e29 111 /* Backhaul route configuration*/
mbed_official 0:85f4174a8e29 112 memset(&backhaul_route, 0, sizeof(backhaul_route));
mbed_official 0:85f4174a8e29 113 #ifdef MBED_CONF_APP_BACKHAUL_NEXT_HOP
mbed_official 0:85f4174a8e29 114 param = MBED_CONF_APP_BACKHAUL_NEXT_HOP;
mbed_official 0:85f4174a8e29 115 stoip6(param, strlen(param), backhaul_route.next_hop);
mbed_official 0:85f4174a8e29 116 tr_info("next hop: %s", print_ipv6(backhaul_route.next_hop));
mbed_official 0:85f4174a8e29 117 #endif
mbed_official 0:85f4174a8e29 118 param = MBED_CONF_APP_BACKHAUL_DEFAULT_ROUTE;
mbed_official 0:85f4174a8e29 119 char *prefix, route_buf[255] = {0};
mbed_official 0:85f4174a8e29 120 /* copy the config value to a non-const buffer */
mbed_official 0:85f4174a8e29 121 strncpy(route_buf, param, sizeof(route_buf) - 1);
mbed_official 0:85f4174a8e29 122 prefix = strtok(route_buf, "/");
mbed_official 0:85f4174a8e29 123 backhaul_route.prefix_len = atoi(strtok(NULL, "/"));
mbed_official 0:85f4174a8e29 124 stoip6(prefix, strlen(prefix), backhaul_route.prefix);
mbed_official 0:85f4174a8e29 125 tr_info("backhaul route prefix: %s", print_ipv6(backhaul_route.prefix));
mbed_official 0:85f4174a8e29 126 #endif
mbed_official 0:85f4174a8e29 127 }
mbed_official 0:85f4174a8e29 128
mbed_official 0:85f4174a8e29 129 static int thread_interface_up(void)
mbed_official 0:85f4174a8e29 130 {
mbed_official 0:85f4174a8e29 131 int32_t val;
mbed_official 0:85f4174a8e29 132 device_configuration_s device_config;
mbed_official 0:85f4174a8e29 133 link_configuration_s link_setup;
mbed_official 12:04fcf9df91e2 134 link_configuration_s *link_setup_ptr;
mbed_official 0:85f4174a8e29 135 int8_t thread_if_id = thread_br_conn_handler_thread_interface_id_get();
mbed_official 0:85f4174a8e29 136
mbed_official 0:85f4174a8e29 137 tr_info("thread_interface_up");
mbed_official 0:85f4174a8e29 138 memset(&device_config, 0, sizeof(device_config));
mbed_official 0:85f4174a8e29 139
mbed_official 0:85f4174a8e29 140 const char *param = MBED_CONF_APP_PSKD;
mbed_official 0:85f4174a8e29 141 uint16_t len = strlen(param);
mbed_official 0:85f4174a8e29 142 MBED_ASSERT(len > 5 && len < 33);
mbed_official 0:85f4174a8e29 143
mbed_official 0:85f4174a8e29 144 device_config.PSKd_ptr = malloc(len + 1);
mbed_official 0:85f4174a8e29 145 device_config.PSKd_len = len;
mbed_official 0:85f4174a8e29 146 memset(device_config.PSKd_ptr, 0, len + 1);
mbed_official 0:85f4174a8e29 147 memcpy(device_config.PSKd_ptr, param, len);
mbed_official 0:85f4174a8e29 148
mbed_official 12:04fcf9df91e2 149 link_setup_ptr = thread_link_configuration_get(&link_setup);
mbed_official 12:04fcf9df91e2 150 val = thread_management_node_init(thread_if_id, NULL, &device_config, link_setup_ptr);
mbed_official 0:85f4174a8e29 151
mbed_official 0:85f4174a8e29 152 if (val) {
mbed_official 0:85f4174a8e29 153 tr_error("Thread init error with code: %is\r\n", (int)val);
mbed_official 0:85f4174a8e29 154 return val;
mbed_official 0:85f4174a8e29 155 }
mbed_official 0:85f4174a8e29 156
mbed_official 0:85f4174a8e29 157 // Additional thread configurations
mbed_official 0:85f4174a8e29 158 thread_management_set_link_timeout(thread_if_id, MESH_LINK_TIMEOUT);
mbed_official 0:85f4174a8e29 159 thread_management_max_child_count(thread_if_id, THREAD_MAX_CHILD_COUNT);
mbed_official 0:85f4174a8e29 160
mbed_official 0:85f4174a8e29 161 val = arm_nwk_interface_up(thread_if_id);
mbed_official 0:85f4174a8e29 162 if (val != 0) {
mbed_official 0:85f4174a8e29 163 tr_error("mesh0 up Fail with code: %i\r\n", (int)val);
mbed_official 0:85f4174a8e29 164 return -1;
mbed_official 0:85f4174a8e29 165 } else {
mbed_official 0:85f4174a8e29 166 tr_info("mesh0 bootstrap ongoing...");
mbed_official 0:85f4174a8e29 167 }
mbed_official 0:85f4174a8e29 168 return 0;
mbed_official 0:85f4174a8e29 169 }
mbed_official 0:85f4174a8e29 170
mbed_official 12:04fcf9df91e2 171 static link_configuration_s* thread_link_configuration_get(link_configuration_s *link_configuration)
mbed_official 0:85f4174a8e29 172 {
mbed_official 14:de99c96a9fae 173 #ifdef MBED_CONF_APP_THREAD_USE_STATIC_LINK_CONFIG
mbed_official 12:04fcf9df91e2 174 #if (false == MBED_CONF_APP_THREAD_USE_STATIC_LINK_CONFIG)
mbed_official 12:04fcf9df91e2 175 // NOT using static link configuration values, return NULL
mbed_official 12:04fcf9df91e2 176 return NULL;
mbed_official 12:04fcf9df91e2 177 #endif
mbed_official 14:de99c96a9fae 178 #endif
mbed_official 12:04fcf9df91e2 179
mbed_official 0:85f4174a8e29 180 memset(link_configuration, 0, sizeof(link_configuration_s));
mbed_official 0:85f4174a8e29 181
mbed_official 0:85f4174a8e29 182 MBED_ASSERT(strlen(MBED_CONF_APP_NETWORK_NAME) > 0 && strlen(MBED_CONF_APP_NETWORK_NAME) < 17);
mbed_official 0:85f4174a8e29 183 const uint8_t master_key[] = MBED_CONF_APP_THREAD_MASTER_KEY;
mbed_official 0:85f4174a8e29 184 MBED_ASSERT(sizeof(master_key) == 16);
mbed_official 0:85f4174a8e29 185 const uint8_t pskc[] = MBED_CONF_APP_PSKC;
mbed_official 0:85f4174a8e29 186 MBED_ASSERT(sizeof(pskc) == 16);
mbed_official 0:85f4174a8e29 187
mbed_official 0:85f4174a8e29 188 const uint8_t extented_panid[] = MBED_CONF_APP_EXTENDED_PAN_ID;
mbed_official 0:85f4174a8e29 189 MBED_ASSERT(sizeof(extented_panid) == 8);
mbed_official 0:85f4174a8e29 190 const uint8_t mesh_local_prefix[] = MBED_CONF_APP_MESH_LOCAL_PREFIX;
mbed_official 0:85f4174a8e29 191 MBED_ASSERT(sizeof(mesh_local_prefix) == 8);
mbed_official 0:85f4174a8e29 192
mbed_official 0:85f4174a8e29 193 memcpy(link_configuration->extented_pan_id, extented_panid, sizeof(extented_panid));
mbed_official 0:85f4174a8e29 194 memcpy(link_configuration->mesh_local_ula_prefix, mesh_local_prefix, sizeof(mesh_local_prefix));
mbed_official 0:85f4174a8e29 195
mbed_official 0:85f4174a8e29 196 link_configuration->panId = MBED_CONF_APP_PAN_ID;
mbed_official 0:85f4174a8e29 197 tr_info("PAN ID %x", link_configuration->panId);
mbed_official 0:85f4174a8e29 198 memcpy(link_configuration->name, MBED_CONF_APP_NETWORK_NAME, strlen(MBED_CONF_APP_NETWORK_NAME));
mbed_official 0:85f4174a8e29 199 link_configuration->timestamp = MBED_CONF_APP_COMMISSIONING_DATASET_TIMESTAMP;
mbed_official 0:85f4174a8e29 200
mbed_official 0:85f4174a8e29 201 memcpy(link_configuration->PSKc, pskc, sizeof(pskc));
mbed_official 0:85f4174a8e29 202 memcpy(link_configuration->master_key, master_key, sizeof(master_key));
mbed_official 0:85f4174a8e29 203 link_configuration->securityPolicy = SECURITY_POLICY_ALL_SECURITY;
mbed_official 0:85f4174a8e29 204
mbed_official 0:85f4174a8e29 205 link_configuration->rfChannel = MBED_CONF_APP_RF_CHANNEL;
mbed_official 0:85f4174a8e29 206 tr_info("RF channel %d", link_configuration->rfChannel);
mbed_official 0:85f4174a8e29 207 link_configuration->channel_page = MBED_CONF_APP_RF_CHANNEL_PAGE;
mbed_official 0:85f4174a8e29 208 uint32_t channel_mask = MBED_CONF_APP_RF_CHANNEL_MASK;
mbed_official 0:85f4174a8e29 209 common_write_32_bit(channel_mask, link_configuration->channel_mask);
mbed_official 0:85f4174a8e29 210
mbed_official 0:85f4174a8e29 211 link_configuration->key_rotation = 3600;
mbed_official 0:85f4174a8e29 212 link_configuration->key_sequence = 0;
mbed_official 12:04fcf9df91e2 213
mbed_official 12:04fcf9df91e2 214 return link_configuration;
mbed_official 0:85f4174a8e29 215 }
mbed_official 0:85f4174a8e29 216
mbed_official 0:85f4174a8e29 217 // ethernet interface
mbed_official 0:85f4174a8e29 218 static void network_interface_event_handler(arm_event_s *event)
mbed_official 0:85f4174a8e29 219 {
mbed_official 0:85f4174a8e29 220 bool connectStatus = false;
mbed_official 0:85f4174a8e29 221 arm_nwk_interface_status_type_e status = (arm_nwk_interface_status_type_e)event->event_data;
mbed_official 0:85f4174a8e29 222 switch (status) {
mbed_official 0:85f4174a8e29 223 case (ARM_NWK_BOOTSTRAP_READY): { // Interface configured Bootstrap is ready
mbed_official 0:85f4174a8e29 224
mbed_official 0:85f4174a8e29 225 connectStatus = true;
mbed_official 0:85f4174a8e29 226 tr_info("BR interface_id: %d", thread_br_conn_handler_eth_interface_id_get());
mbed_official 0:85f4174a8e29 227 if (-1 != thread_br_conn_handler_eth_interface_id_get()) {
mbed_official 0:85f4174a8e29 228 // metric set to high priority
mbed_official 0:85f4174a8e29 229 if (0 != arm_net_interface_set_metric(thread_br_conn_handler_eth_interface_id_get(), 0)) {
mbed_official 0:85f4174a8e29 230 tr_warn("Failed to set metric for eth0.");
mbed_official 0:85f4174a8e29 231 }
mbed_official 0:85f4174a8e29 232
mbed_official 0:85f4174a8e29 233 if (backhaul_bootstrap_mode == NET_IPV6_BOOTSTRAP_STATIC) {
mbed_official 0:85f4174a8e29 234 uint8_t *next_hop_ptr;
mbed_official 0:85f4174a8e29 235
mbed_official 0:85f4174a8e29 236 if (memcmp(backhaul_route.next_hop, (const uint8_t[16]) {0}, 16) == 0) {
mbed_official 0:85f4174a8e29 237 next_hop_ptr = NULL;
mbed_official 0:85f4174a8e29 238 }
mbed_official 0:85f4174a8e29 239 else {
mbed_official 0:85f4174a8e29 240 next_hop_ptr = backhaul_route.next_hop;
mbed_official 0:85f4174a8e29 241 }
mbed_official 0:85f4174a8e29 242 tr_debug("Default route prefix: %s/%d", print_ipv6(backhaul_route.prefix),
mbed_official 0:85f4174a8e29 243 backhaul_route.prefix_len);
mbed_official 0:85f4174a8e29 244 tr_debug("Default route next hop: %s", print_ipv6(backhaul_route.next_hop));
mbed_official 0:85f4174a8e29 245 arm_net_route_add(backhaul_route.prefix,
mbed_official 0:85f4174a8e29 246 backhaul_route.prefix_len,
mbed_official 0:85f4174a8e29 247 next_hop_ptr, 0xffffffff, 128,
mbed_official 0:85f4174a8e29 248 thread_br_conn_handler_eth_interface_id_get());
mbed_official 0:85f4174a8e29 249 }
mbed_official 0:85f4174a8e29 250 thread_br_conn_handler_eth_ready();
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 0:85f4174a8e29 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 0:85f4174a8e29 338 thread_if_id,
mbed_official 0:85f4174a8e29 339 NET_6LOWPAN_ROUTER,
mbed_official 0:85f4174a8e29 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 0:85f4174a8e29 365 MBED_ASSERT(rf_driver_id>=0);
mbed_official 0:85f4174a8e29 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 net_init_core();
mbed_official 0:85f4174a8e29 378 thread_rf_init();
mbed_official 0:85f4174a8e29 379 protocol_stats_start(&nwk_stats);
mbed_official 0:85f4174a8e29 380
mbed_official 0:85f4174a8e29 381 eventOS_event_handler_create(
mbed_official 0:85f4174a8e29 382 &borderrouter_tasklet,
mbed_official 0:85f4174a8e29 383 ARM_LIB_TASKLET_INIT_EVENT);
mbed_official 0:85f4174a8e29 384 }
mbed_official 0:85f4174a8e29 385
mbed_official 0:85f4174a8e29 386
mbed_official 0:85f4174a8e29 387 static void borderrouter_backhaul_phy_status_cb(uint8_t link_up, int8_t driver_id)
mbed_official 0:85f4174a8e29 388 {
mbed_official 0:85f4174a8e29 389 arm_event_s event = {
mbed_official 0:85f4174a8e29 390 .sender = br_tasklet_id,
mbed_official 0:85f4174a8e29 391 .receiver = br_tasklet_id,
mbed_official 0:85f4174a8e29 392 .priority = ARM_LIB_MED_PRIORITY_EVENT,
mbed_official 0:85f4174a8e29 393 .event_type = APPLICATION_EVENT,
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 } else {
mbed_official 0:85f4174a8e29 400 event.event_id = NR_BACKHAUL_INTERFACE_PHY_DOWN;
mbed_official 0:85f4174a8e29 401 }
mbed_official 0:85f4174a8e29 402
mbed_official 0:85f4174a8e29 403 tr_debug("Backhaul driver ID: %d", driver_id);
mbed_official 0:85f4174a8e29 404
mbed_official 0:85f4174a8e29 405 eventOS_event_send(&event);
mbed_official 0:85f4174a8e29 406 }
mbed_official 0:85f4174a8e29 407
mbed_official 0:85f4174a8e29 408 static int backhaul_interface_up(int8_t driver_id)
mbed_official 0:85f4174a8e29 409 {
mbed_official 0:85f4174a8e29 410 int retval = -1;
mbed_official 0:85f4174a8e29 411 tr_debug("backhaul_interface_up: %i\n", driver_id);
mbed_official 0:85f4174a8e29 412 int8_t backhaul_if_id = thread_br_conn_handler_eth_interface_id_get();
mbed_official 0:85f4174a8e29 413 if (backhaul_if_id != -1) {
mbed_official 0:85f4174a8e29 414 tr_debug("Border RouterInterface already at active state\n");
mbed_official 0:85f4174a8e29 415 } else {
mbed_official 0:85f4174a8e29 416
mbed_official 0:85f4174a8e29 417 if (!eth_mac_api) {
mbed_official 0:85f4174a8e29 418 eth_mac_api = ethernet_mac_create(driver_id);
mbed_official 0:85f4174a8e29 419 }
mbed_official 0:85f4174a8e29 420
mbed_official 0:85f4174a8e29 421 backhaul_if_id = arm_nwk_interface_ethernet_init(eth_mac_api, "bh0");
mbed_official 0:85f4174a8e29 422
mbed_official 0:85f4174a8e29 423 MBED_ASSERT(backhaul_if_id >= 0);
mbed_official 0:85f4174a8e29 424 if (backhaul_if_id >= 0) {
mbed_official 0:85f4174a8e29 425 tr_debug("Backhaul interface ID: %d", backhaul_if_id);
mbed_official 0:85f4174a8e29 426 thread_br_conn_handler_eth_interface_id_set(backhaul_if_id);
mbed_official 0:85f4174a8e29 427 arm_nwk_interface_configure_ipv6_bootstrap_set(
mbed_official 0:85f4174a8e29 428 backhaul_if_id, backhaul_bootstrap_mode, backhaul_prefix);
mbed_official 0:85f4174a8e29 429 arm_nwk_interface_up(backhaul_if_id);
mbed_official 0:85f4174a8e29 430 retval = 0;
mbed_official 0:85f4174a8e29 431 }
mbed_official 0:85f4174a8e29 432 else {
mbed_official 0:85f4174a8e29 433 tr_debug("Could not init ethernet");
mbed_official 0:85f4174a8e29 434 }
mbed_official 0:85f4174a8e29 435 }
mbed_official 0:85f4174a8e29 436 return retval;
mbed_official 0:85f4174a8e29 437 }
mbed_official 0:85f4174a8e29 438
mbed_official 0:85f4174a8e29 439 static int backhaul_interface_down(void)
mbed_official 0:85f4174a8e29 440 {
mbed_official 0:85f4174a8e29 441 int retval = -1;
mbed_official 0:85f4174a8e29 442 if (thread_br_conn_handler_eth_interface_id_get() != -1) {
mbed_official 0:85f4174a8e29 443 arm_nwk_interface_down(thread_br_conn_handler_eth_interface_id_get());
mbed_official 0:85f4174a8e29 444 thread_br_conn_handler_eth_interface_id_set(-1);
mbed_official 0:85f4174a8e29 445 thread_br_conn_handler_ethernet_connection_update(false);
mbed_official 0:85f4174a8e29 446 retval = 0;
mbed_official 0:85f4174a8e29 447 }
mbed_official 0:85f4174a8e29 448 else {
mbed_official 0:85f4174a8e29 449 tr_debug("Could not set eth down");
mbed_official 0:85f4174a8e29 450 }
mbed_official 0:85f4174a8e29 451 return retval;
mbed_official 0:85f4174a8e29 452 }
mbed_official 0:85f4174a8e29 453
mbed_official 0:85f4174a8e29 454 /**
mbed_official 0:85f4174a8e29 455 * \brief Border Router Main Tasklet
mbed_official 0:85f4174a8e29 456 *
mbed_official 0:85f4174a8e29 457 * Tasklet Handle next items:
mbed_official 0:85f4174a8e29 458 *
mbed_official 0:85f4174a8e29 459 * - EV_INIT event: Set Certificate Chain, RF Interface Boot UP, multicast Init
mbed_official 0:85f4174a8e29 460 * - SYSTEM_TIMER event: For RF interface Handshake purpose
mbed_official 0:85f4174a8e29 461 *
mbed_official 0:85f4174a8e29 462 */
mbed_official 0:85f4174a8e29 463 static void borderrouter_tasklet(arm_event_s *event)
mbed_official 0:85f4174a8e29 464 {
mbed_official 0:85f4174a8e29 465 arm_library_event_type_e event_type;
mbed_official 0:85f4174a8e29 466 event_type = (arm_library_event_type_e)event->event_type;
mbed_official 0:85f4174a8e29 467
mbed_official 0:85f4174a8e29 468 switch (event_type) {
mbed_official 0:85f4174a8e29 469 case ARM_LIB_NWK_INTERFACE_EVENT:
mbed_official 0:85f4174a8e29 470
mbed_official 0:85f4174a8e29 471 if (event->event_id == thread_br_conn_handler_eth_interface_id_get()) {
mbed_official 0:85f4174a8e29 472 network_interface_event_handler(event);
mbed_official 0:85f4174a8e29 473 } else {
mbed_official 0:85f4174a8e29 474 thread_interface_event_handler(event);
mbed_official 0:85f4174a8e29 475 }
mbed_official 0:85f4174a8e29 476
mbed_official 0:85f4174a8e29 477 break;
mbed_official 0:85f4174a8e29 478 // comes from the backhaul_driver_init.
mbed_official 0:85f4174a8e29 479 case APPLICATION_EVENT:
mbed_official 0:85f4174a8e29 480 if (event->event_id == NR_BACKHAUL_INTERFACE_PHY_DRIVER_READY) {
mbed_official 0:85f4174a8e29 481 int8_t net_backhaul_id = (int8_t) event->event_data;
mbed_official 0:85f4174a8e29 482
mbed_official 0:85f4174a8e29 483 if (backhaul_interface_up(net_backhaul_id) != 0) {
mbed_official 0:85f4174a8e29 484 tr_debug("Backhaul bootstrap start failed");
mbed_official 0:85f4174a8e29 485 } else {
mbed_official 0:85f4174a8e29 486 tr_debug("Backhaul bootstrap started");
mbed_official 0:85f4174a8e29 487 }
mbed_official 0:85f4174a8e29 488 } else if (event->event_id == NR_BACKHAUL_INTERFACE_PHY_DOWN) {
mbed_official 0:85f4174a8e29 489 if (backhaul_interface_down() != 0) {
mbed_official 0:85f4174a8e29 490 // may happend when booting first time.
mbed_official 0:85f4174a8e29 491 tr_warning("Backhaul interface down failed");
mbed_official 0:85f4174a8e29 492 } else {
mbed_official 0:85f4174a8e29 493 tr_debug("Backhaul interface is down");
mbed_official 0:85f4174a8e29 494 }
mbed_official 0:85f4174a8e29 495 }
mbed_official 0:85f4174a8e29 496 break;
mbed_official 0:85f4174a8e29 497
mbed_official 0:85f4174a8e29 498 case ARM_LIB_TASKLET_INIT_EVENT:
mbed_official 0:85f4174a8e29 499 print_appl_info();
mbed_official 0:85f4174a8e29 500 br_tasklet_id = event->receiver;
mbed_official 0:85f4174a8e29 501 thread_br_conn_handler_init();
mbed_official 0:85f4174a8e29 502 eth_network_data_init();
mbed_official 0:85f4174a8e29 503 backhaul_driver_init(borderrouter_backhaul_phy_status_cb);
mbed_official 0:85f4174a8e29 504 mesh_network_up();
mbed_official 0:85f4174a8e29 505 eventOS_event_timer_request(9, ARM_LIB_SYSTEM_TIMER_EVENT, br_tasklet_id, 20000);
mbed_official 0:85f4174a8e29 506 break;
mbed_official 0:85f4174a8e29 507
mbed_official 0:85f4174a8e29 508 case ARM_LIB_SYSTEM_TIMER_EVENT:
mbed_official 0:85f4174a8e29 509 eventOS_event_timer_cancel(event->event_id, event->receiver);
mbed_official 0:85f4174a8e29 510
mbed_official 0:85f4174a8e29 511 if (event->event_id == 9) {
mbed_official 0:85f4174a8e29 512 #ifdef MBED_CONF_APP_DEBUG_TRACE
mbed_official 0:85f4174a8e29 513 #if MBED_CONF_APP_DEBUG_TRACE == 1
mbed_official 0:85f4174a8e29 514 arm_print_routing_table();
mbed_official 0:85f4174a8e29 515 arm_print_neigh_cache();
mbed_official 13:993808eb2e9c 516 print_memory_stats();
mbed_official 0:85f4174a8e29 517 #endif
mbed_official 0:85f4174a8e29 518 #endif
mbed_official 0:85f4174a8e29 519 eventOS_event_timer_request(9, ARM_LIB_SYSTEM_TIMER_EVENT, br_tasklet_id, 20000);
mbed_official 0:85f4174a8e29 520 }
mbed_official 0:85f4174a8e29 521 break;
mbed_official 0:85f4174a8e29 522
mbed_official 0:85f4174a8e29 523 default:
mbed_official 0:85f4174a8e29 524 break;
mbed_official 0:85f4174a8e29 525 }
mbed_official 0:85f4174a8e29 526 }
mbed_official 0:85f4174a8e29 527
mbed_official 0:85f4174a8e29 528 #endif // MBED_CONF_APP_MESH_MODE
mbed_official 0:85f4174a8e29 529