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:
105:d9f83743ed4f
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 105:d9f83743ed4f 2 * Copyright (c) 2016-2019 ARM Limited. All rights reserved.
mbed_official 0:85f4174a8e29 3 */
mbed_official 0:85f4174a8e29 4
mbed_official 0:85f4174a8e29 5 #include <string.h>
mbed_official 0:85f4174a8e29 6
mbed_official 0:85f4174a8e29 7
mbed_official 0:85f4174a8e29 8 #include "mbed.h"
mbed_official 0:85f4174a8e29 9 #include "borderrouter_tasklet.h"
mbed_official 0:85f4174a8e29 10 #include "drivers/eth_driver.h"
mbed_official 0:85f4174a8e29 11 #include "sal-stack-nanostack-slip/Slip.h"
mbed_official 0:85f4174a8e29 12
mbed_official 65:92e581c01e8c 13 #include "Nanostack.h"
mbed_official 65:92e581c01e8c 14 #include "NanostackEthernetInterface.h"
mbed_official 65:92e581c01e8c 15 #include "MeshInterfaceNanostack.h"
mbed_official 65:92e581c01e8c 16 #include "EMACInterface.h"
mbed_official 65:92e581c01e8c 17 #include "EMAC.h"
mbed_official 65:92e581c01e8c 18
mbed_official 105:d9f83743ed4f 19 #undef ETH
mbed_official 105:d9f83743ed4f 20 #undef SLIP
mbed_official 105:d9f83743ed4f 21 #undef EMAC
mbed_official 105:d9f83743ed4f 22 #undef CELL
mbed_official 105:d9f83743ed4f 23 #define ETH 1
mbed_official 105:d9f83743ed4f 24 #define SLIP 2
mbed_official 105:d9f83743ed4f 25 #define EMAC 3
mbed_official 105:d9f83743ed4f 26 #define CELL 4
mbed_official 105:d9f83743ed4f 27
mbed_official 105:d9f83743ed4f 28 #if MBED_CONF_APP_BACKHAUL_DRIVER == CELL
mbed_official 105:d9f83743ed4f 29 #include "NanostackPPPInterface.h"
mbed_official 105:d9f83743ed4f 30 #include "PPPInterface.h"
mbed_official 105:d9f83743ed4f 31 #include "PPP.h"
mbed_official 105:d9f83743ed4f 32 #endif
mbed_official 105:d9f83743ed4f 33
mbed_official 0:85f4174a8e29 34 #ifdef MBED_CONF_APP_DEBUG_TRACE
mbed_official 0:85f4174a8e29 35 #if MBED_CONF_APP_DEBUG_TRACE == 1
mbed_official 0:85f4174a8e29 36 #define APP_TRACE_LEVEL TRACE_ACTIVE_LEVEL_DEBUG
mbed_official 0:85f4174a8e29 37 #else
mbed_official 0:85f4174a8e29 38 #define APP_TRACE_LEVEL TRACE_ACTIVE_LEVEL_INFO
mbed_official 0:85f4174a8e29 39 #endif
mbed_official 0:85f4174a8e29 40 #endif //MBED_CONF_APP_DEBUG_TRACE
mbed_official 0:85f4174a8e29 41
mbed_official 0:85f4174a8e29 42 #include "ns_hal_init.h"
mbed_official 65:92e581c01e8c 43 #include "mesh_system.h"
mbed_official 0:85f4174a8e29 44 #include "cmsis_os.h"
mbed_official 0:85f4174a8e29 45 #include "arm_hal_interrupt.h"
mbed_official 105:d9f83743ed4f 46 #include "nanostack_heap_region.h"
mbed_official 0:85f4174a8e29 47
mbed_official 5:9c0320afe010 48 #include "mbed_trace.h"
mbed_official 0:85f4174a8e29 49 #define TRACE_GROUP "app"
mbed_official 0:85f4174a8e29 50
mbed_official 65:92e581c01e8c 51 #define BOARD 1
mbed_official 65:92e581c01e8c 52 #define CONFIG 2
mbed_official 65:92e581c01e8c 53 #if MBED_CONF_APP_BACKHAUL_MAC_SRC == BOARD
mbed_official 65:92e581c01e8c 54 static uint8_t mac[6];
mbed_official 65:92e581c01e8c 55 #elif MBED_CONF_APP_BACKHAUL_MAC_SRC == CONFIG
mbed_official 65:92e581c01e8c 56 static const uint8_t mac[] = MBED_CONF_APP_BACKHAUL_MAC;
mbed_official 65:92e581c01e8c 57 #else
mbed_official 65:92e581c01e8c 58 #error "MAC address not defined"
mbed_official 65:92e581c01e8c 59 #endif
mbed_official 65:92e581c01e8c 60
mbed_official 0:85f4174a8e29 61 static DigitalOut led1(MBED_CONF_APP_LED);
mbed_official 0:85f4174a8e29 62
mbed_official 0:85f4174a8e29 63 static Ticker led_ticker;
mbed_official 0:85f4174a8e29 64
mbed_official 0:85f4174a8e29 65 static void toggle_led1()
mbed_official 0:85f4174a8e29 66 {
mbed_official 0:85f4174a8e29 67 led1 = !led1;
mbed_official 0:85f4174a8e29 68 }
mbed_official 0:85f4174a8e29 69
mbed_official 0:85f4174a8e29 70 /**
mbed_official 0:85f4174a8e29 71 * \brief Prints string to serial (adds CRLF).
mbed_official 0:85f4174a8e29 72 */
mbed_official 0:85f4174a8e29 73 static void trace_printer(const char *str)
mbed_official 0:85f4174a8e29 74 {
mbed_official 0:85f4174a8e29 75 printf("%s\n", str);
mbed_official 0:85f4174a8e29 76 }
mbed_official 0:85f4174a8e29 77
mbed_official 65:92e581c01e8c 78 #if MBED_CONF_APP_BACKHAUL_DRIVER == EMAC
mbed_official 65:92e581c01e8c 79 static void (*emac_actual_cb)(uint8_t, int8_t);
mbed_official 65:92e581c01e8c 80 static int8_t emac_driver_id;
mbed_official 65:92e581c01e8c 81 static void emac_link_cb(bool up)
mbed_official 65:92e581c01e8c 82 {
mbed_official 65:92e581c01e8c 83 if (emac_actual_cb) {
mbed_official 65:92e581c01e8c 84 emac_actual_cb(up, emac_driver_id);
mbed_official 65:92e581c01e8c 85 }
mbed_official 65:92e581c01e8c 86 }
mbed_official 65:92e581c01e8c 87 #endif
mbed_official 65:92e581c01e8c 88
mbed_official 0:85f4174a8e29 89 /**
mbed_official 82:3d9e3b7b3dcf 90 * \brief Initializes the MAC backhaul driver.
mbed_official 0:85f4174a8e29 91 * This function is called by the border router module.
mbed_official 0:85f4174a8e29 92 */
mbed_official 0:85f4174a8e29 93 void backhaul_driver_init(void (*backhaul_driver_status_cb)(uint8_t, int8_t))
mbed_official 0:85f4174a8e29 94 {
mbed_official 0:85f4174a8e29 95 // Values allowed in "backhaul-driver" option
mbed_official 0:85f4174a8e29 96 #if MBED_CONF_APP_BACKHAUL_DRIVER == SLIP
mbed_official 0:85f4174a8e29 97 SlipMACDriver *pslipmacdriver;
mbed_official 0:85f4174a8e29 98 int8_t slipdrv_id = -1;
mbed_official 0:85f4174a8e29 99 #if defined(MBED_CONF_APP_SLIP_HW_FLOW_CONTROL)
mbed_official 0:85f4174a8e29 100 pslipmacdriver = new SlipMACDriver(SERIAL_TX, SERIAL_RX, SERIAL_RTS, SERIAL_CTS);
mbed_official 0:85f4174a8e29 101 #else
mbed_official 0:85f4174a8e29 102 pslipmacdriver = new SlipMACDriver(SERIAL_TX, SERIAL_RX);
mbed_official 0:85f4174a8e29 103 #endif
mbed_official 0:85f4174a8e29 104
mbed_official 0:85f4174a8e29 105 if (pslipmacdriver == NULL) {
mbed_official 0:85f4174a8e29 106 tr_error("Unable to create SLIP driver");
mbed_official 0:85f4174a8e29 107 return;
mbed_official 0:85f4174a8e29 108 }
mbed_official 0:85f4174a8e29 109
mbed_official 0:85f4174a8e29 110 tr_info("Using SLIP backhaul driver...");
mbed_official 0:85f4174a8e29 111
mbed_official 0:85f4174a8e29 112 #ifdef MBED_CONF_APP_SLIP_SERIAL_BAUD_RATE
mbed_official 0:85f4174a8e29 113 slipdrv_id = pslipmacdriver->Slip_Init(mac, MBED_CONF_APP_SLIP_SERIAL_BAUD_RATE);
mbed_official 0:85f4174a8e29 114 #else
mbed_official 0:85f4174a8e29 115 tr_warning("baud rate for slip not defined");
mbed_official 0:85f4174a8e29 116 #endif
mbed_official 0:85f4174a8e29 117
mbed_official 0:85f4174a8e29 118 if (slipdrv_id >= 0) {
mbed_official 0:85f4174a8e29 119 backhaul_driver_status_cb(1, slipdrv_id);
mbed_official 0:85f4174a8e29 120 return;
mbed_official 0:85f4174a8e29 121 }
mbed_official 0:85f4174a8e29 122
mbed_official 0:85f4174a8e29 123 tr_error("Backhaul driver init failed, retval = %d", slipdrv_id);
mbed_official 65:92e581c01e8c 124 #elif MBED_CONF_APP_BACKHAUL_DRIVER == EMAC
mbed_official 65:92e581c01e8c 125 #undef EMAC
mbed_official 65:92e581c01e8c 126 tr_info("Using EMAC backhaul driver...");
mbed_official 65:92e581c01e8c 127 NetworkInterface *net = NetworkInterface::get_default_instance();
mbed_official 65:92e581c01e8c 128 if (!net) {
mbed_official 65:92e581c01e8c 129 tr_error("Default network interface not found");
mbed_official 65:92e581c01e8c 130 exit(1);
mbed_official 65:92e581c01e8c 131 }
mbed_official 65:92e581c01e8c 132 EMACInterface *emacif = net->emacInterface();
mbed_official 65:92e581c01e8c 133 if (!emacif) {
mbed_official 65:92e581c01e8c 134 tr_error("Default interface is not EMAC-based");
mbed_official 65:92e581c01e8c 135 exit(1);
mbed_official 65:92e581c01e8c 136 }
mbed_official 65:92e581c01e8c 137 EMAC &emac = emacif->get_emac();
mbed_official 65:92e581c01e8c 138 Nanostack::EthernetInterface *ns_if;
mbed_official 65:92e581c01e8c 139 #if MBED_CONF_APP_BACKHAUL_MAC_SRC == BOARD
mbed_official 65:92e581c01e8c 140 /* Let the core code choose address - either from board or EMAC (for
mbed_official 65:92e581c01e8c 141 * ETH and SLIP we pass in the board address already in mac[]) */
mbed_official 65:92e581c01e8c 142 nsapi_error_t err = Nanostack::get_instance().add_ethernet_interface(emac, true, &ns_if);
mbed_official 65:92e581c01e8c 143 /* Read back what they chose into our mac[] */
mbed_official 65:92e581c01e8c 144 ns_if->get_mac_address(mac);
mbed_official 65:92e581c01e8c 145 #else
mbed_official 65:92e581c01e8c 146 nsapi_error_t err = Nanostack::get_instance().add_ethernet_interface(emac, true, &ns_if, mac);
mbed_official 65:92e581c01e8c 147 #endif
mbed_official 65:92e581c01e8c 148 if (err < 0) {
mbed_official 65:92e581c01e8c 149 tr_error("Backhaul driver init failed, retval = %d", err);
mbed_official 65:92e581c01e8c 150 } else {
mbed_official 65:92e581c01e8c 151 emac_actual_cb = backhaul_driver_status_cb;
mbed_official 65:92e581c01e8c 152 emac_driver_id = ns_if->get_driver_id();
mbed_official 65:92e581c01e8c 153 emac.set_link_state_cb(emac_link_cb);
mbed_official 65:92e581c01e8c 154 }
mbed_official 105:d9f83743ed4f 155 #elif MBED_CONF_APP_BACKHAUL_DRIVER == CELL
mbed_official 105:d9f83743ed4f 156 tr_info("Using CELLULAR backhaul driver...");
mbed_official 105:d9f83743ed4f 157 /* Creates PPP service and onboard network stack already here for cellular
mbed_official 105:d9f83743ed4f 158 * connection to be able to override the link state changed callback */
mbed_official 105:d9f83743ed4f 159 PPP *ppp = &PPP::get_default_instance();
mbed_official 105:d9f83743ed4f 160 if (!ppp) {
mbed_official 105:d9f83743ed4f 161 tr_error("PPP not found");
mbed_official 105:d9f83743ed4f 162 exit(1);
mbed_official 105:d9f83743ed4f 163 }
mbed_official 105:d9f83743ed4f 164 OnboardNetworkStack *stack = &OnboardNetworkStack::get_default_instance();
mbed_official 105:d9f83743ed4f 165 if (!stack) {
mbed_official 105:d9f83743ed4f 166 tr_error("Onboard network stack not found");
mbed_official 105:d9f83743ed4f 167 exit(1);
mbed_official 105:d9f83743ed4f 168 }
mbed_official 105:d9f83743ed4f 169 OnboardNetworkStack::Interface *interface;
mbed_official 105:d9f83743ed4f 170 if (stack->add_ppp_interface(*ppp, true, &interface) != NSAPI_ERROR_OK) {
mbed_official 105:d9f83743ed4f 171 tr_error("Cannot add PPP interface");
mbed_official 105:d9f83743ed4f 172 exit(1);
mbed_official 105:d9f83743ed4f 173 }
mbed_official 105:d9f83743ed4f 174 Nanostack::PPPInterface *ns_if = static_cast<Nanostack::PPPInterface *>(interface);
mbed_official 105:d9f83743ed4f 175 ns_if->set_link_state_changed_callback(backhaul_driver_status_cb);
mbed_official 105:d9f83743ed4f 176
mbed_official 105:d9f83743ed4f 177 // Cellular interface configures it to PPP service and onboard stack created above
mbed_official 105:d9f83743ed4f 178 CellularInterface *net = CellularInterface::get_default_instance();
mbed_official 105:d9f83743ed4f 179 if (!net) {
mbed_official 105:d9f83743ed4f 180 tr_error("Default cellular interface not found");
mbed_official 105:d9f83743ed4f 181 exit(1);
mbed_official 105:d9f83743ed4f 182 }
mbed_official 105:d9f83743ed4f 183 net->set_default_parameters(); // from cellular nsapi .json configuration
mbed_official 105:d9f83743ed4f 184 net->set_blocking(false);
mbed_official 105:d9f83743ed4f 185 if (net->connect() != NSAPI_ERROR_OK) {
mbed_official 105:d9f83743ed4f 186 tr_error("Connect failure");
mbed_official 105:d9f83743ed4f 187 exit(1);
mbed_official 105:d9f83743ed4f 188 }
mbed_official 0:85f4174a8e29 189 #elif MBED_CONF_APP_BACKHAUL_DRIVER == ETH
mbed_official 0:85f4174a8e29 190 tr_info("Using ETH backhaul driver...");
mbed_official 0:85f4174a8e29 191 arm_eth_phy_device_register(mac, backhaul_driver_status_cb);
mbed_official 0:85f4174a8e29 192 return;
mbed_official 0:85f4174a8e29 193 #else
mbed_official 0:85f4174a8e29 194 #error "Unsupported backhaul driver"
mbed_official 0:85f4174a8e29 195 #endif
mbed_official 105:d9f83743ed4f 196 #undef ETH
mbed_official 65:92e581c01e8c 197 #undef SLIP
mbed_official 65:92e581c01e8c 198 #undef EMAC
mbed_official 105:d9f83743ed4f 199 #undef CELL
mbed_official 0:85f4174a8e29 200 }
mbed_official 0:85f4174a8e29 201
mbed_official 82:3d9e3b7b3dcf 202
mbed_official 82:3d9e3b7b3dcf 203 void appl_info_trace(void)
mbed_official 82:3d9e3b7b3dcf 204 {
mbed_official 82:3d9e3b7b3dcf 205 tr_info("Starting NanoStack Border Router...");
mbed_official 82:3d9e3b7b3dcf 206 tr_info("Build date: %s %s", __DATE__, __TIME__);
mbed_official 82:3d9e3b7b3dcf 207 #ifdef MBED_MAJOR_VERSION
mbed_official 93:6c98c32f59e1 208 tr_info("Mbed OS: %d", MBED_VERSION);
mbed_official 93:6c98c32f59e1 209 #endif
mbed_official 93:6c98c32f59e1 210
mbed_official 93:6c98c32f59e1 211 #if defined(MBED_SYS_STATS_ENABLED)
mbed_official 93:6c98c32f59e1 212 mbed_stats_sys_t stats;
mbed_official 93:6c98c32f59e1 213 mbed_stats_sys_get(&stats);
mbed_official 93:6c98c32f59e1 214
mbed_official 93:6c98c32f59e1 215 /* ARM = 1, GCC_ARM = 2, IAR = 3 */
mbed_official 93:6c98c32f59e1 216 tr_info("Compiler ID: %d", stats.compiler_id);
mbed_official 93:6c98c32f59e1 217 /* Compiler versions:
mbed_official 93:6c98c32f59e1 218 ARM: PVVbbbb (P = Major; VV = Minor; bbbb = build number)
mbed_official 93:6c98c32f59e1 219 GCC: VVRRPP (VV = Version; RR = Revision; PP = Patch)
mbed_official 93:6c98c32f59e1 220 IAR: VRRRPPP (V = Version; RRR = Revision; PPP = Patch)
mbed_official 93:6c98c32f59e1 221 */
mbed_official 93:6c98c32f59e1 222 tr_info("Compiler Version: %d", stats.compiler_version);
mbed_official 82:3d9e3b7b3dcf 223 #endif
mbed_official 82:3d9e3b7b3dcf 224 }
mbed_official 82:3d9e3b7b3dcf 225
mbed_official 0:85f4174a8e29 226 /**
mbed_official 0:85f4174a8e29 227 * \brief The entry point for this application.
mbed_official 0:85f4174a8e29 228 * Sets up the application and starts the border router module.
mbed_official 0:85f4174a8e29 229 */
mbed_official 108:0c14bd1d3334 230 int main()
mbed_official 0:85f4174a8e29 231 {
mbed_official 5:9c0320afe010 232 mbed_trace_init(); // set up the tracing library
mbed_official 5:9c0320afe010 233 mbed_trace_print_function_set(trace_printer);
mbed_official 15:173f354b131f 234 mbed_trace_config_set(TRACE_MODE_COLOR | APP_TRACE_LEVEL | TRACE_CARRIAGE_RETURN);
mbed_official 0:85f4174a8e29 235
mbed_official 65:92e581c01e8c 236 // Have to let mesh_system do net_init_core in case we use
mbed_official 65:92e581c01e8c 237 // Nanostack::add_ethernet_interface()
mbed_official 65:92e581c01e8c 238 mesh_system_init();
mbed_official 0:85f4174a8e29 239
mbed_official 105:d9f83743ed4f 240 nanostack_heap_region_add();
mbed_official 105:d9f83743ed4f 241
mbed_official 0:85f4174a8e29 242 #if MBED_CONF_APP_BACKHAUL_MAC_SRC == BOARD
mbed_official 0:85f4174a8e29 243 mbed_mac_address((char *)mac);
mbed_official 0:85f4174a8e29 244 #endif
mbed_official 0:85f4174a8e29 245
mbed_official 2:1411fd11ccdd 246 if (MBED_CONF_APP_LED != NC) {
mbed_official 2:1411fd11ccdd 247 led_ticker.attach_us(toggle_led1, 500000);
mbed_official 2:1411fd11ccdd 248 }
mbed_official 7:571f9a90b972 249 border_router_tasklet_start();
mbed_official 0:85f4174a8e29 250 }