static void hardware_init() { platform_init(); event_init(); soft_timer_init(); phy_set_power(platform_phy, PHY_POWER_m30dBm); radio_init(receive_callback); }
static char *radio_pkt(uint8_t channel, uint8_t tx_power) { static phy_packet_t tx_pkt = { .data = tx_pkt.raw_data, .length = 125, }; uint16_t i; for (i = 0; i < 125; i++) { tx_pkt.data[i] = i; } int success; char *ret = NULL; xQueueReceive(radio_queue, &success, 0); // cleanup queue phy_idle(platform_phy); /* * config radio */ if (phy_set_channel(platform_phy, channel)) ON_ERROR("err_set_channel", radio_cleanup); if (phy_set_power(platform_phy, tx_power)) ON_ERROR("err_set_power", radio_cleanup); /* * Send packet * * No interlocking because * Current queue is EVENT_QUEUE_APPLI * phy_ handlers are executed by EVENT_QUEUE_NETWORK */ if (phy_tx_now(platform_phy, &tx_pkt, radio_rx_tx_done)) ON_ERROR("err_tx", radio_cleanup); if (pdTRUE != xQueueReceive(radio_queue, &success, ONE_SECOND) || !success) ON_ERROR("tx_failed", radio_cleanup); // SUCCESS radio_cleanup: phy_reset(platform_phy); return ret; } static char *radio_ping_pong(uint8_t channel, uint8_t tx_power) { char *ret = NULL; int success; static phy_packet_t tx_pkt = { .data = tx_pkt.raw_data, .length = sizeof(RADIO_TX_STR), .raw_data = RADIO_TX_STR, }; static phy_packet_t rx_pkt = {.data = rx_pkt.raw_data}; xQueueReceive(radio_queue, &success, 0); // cleanup queue phy_idle(platform_phy); /* config radio */ if (phy_set_channel(platform_phy, channel)) ON_ERROR("err_set_channel", ping_pong_cleanup); if (phy_set_power(platform_phy, tx_power)) ON_ERROR("err_set_power", ping_pong_cleanup); /* * Send packet * * No interlocking because * Current queue is EVENT_QUEUE_APPLI * phy_ handlers are executed by EVENT_QUEUE_NETWORK */ if (phy_tx_now(platform_phy, &tx_pkt, radio_rx_tx_done)) ON_ERROR("err_tx", ping_pong_cleanup); if (pdTRUE != xQueueReceive(radio_queue, &success, ONE_SECOND) || !success) ON_ERROR("tx_failed", ping_pong_cleanup); /* * Wait for answer */ memset(rx_pkt.raw_data, 0, sizeof(rx_pkt.raw_data)); phy_rx_now(platform_phy, &rx_pkt, radio_rx_tx_done); if (pdTRUE != xQueueReceive(radio_queue, &success, ONE_SECOND) || !success) ON_ERROR("rx_timeout", ping_pong_cleanup); // Packet reception if (strcmp("RX_PKT_HELLO_WORLD", (char *)rx_pkt.raw_data)) ON_ERROR("wrong_packet_received", ping_pong_cleanup); // SUCCESS ret = NULL; ping_pong_cleanup: phy_reset(platform_phy); return ret; } #ifdef IOTLAB_M3 //////// static int test_flash_nand() { static uint8_t buf_EE[256] = {[0 ... 255] = 0xEE}; static uint8_t buf[256] = {[0 ... 255] = 0x00}; // Write subsector 200 and re-read it n25xxx_write_enable(); n25xxx_erase_subsector(0x00c80000); n25xxx_write_enable(); n25xxx_write_page(0x00c80000, buf_EE); n25xxx_read(0x00c80000, buf, sizeof(buf)); n25xxx_write_enable(); n25xxx_erase_subsector(0x00c80000); // check read values return memcmp(buf_EE, buf, sizeof(buf)); } //////// static int cmd_get_light(char *command) { if (strcmp(command, "get_light")) return 1; printf("ACK get_light %f lux\n", isl29020_read_sample()); return 0; } static int cmd_get_pressure(char *command) { if (strcmp(command, "get_pressure")) return 1; uint32_t pressure; lps331ap_read_pres(&pressure); printf("ACK get_pressure %f mbar\n", pressure / 4096.0); return 0; } static int cmd_test_flash_nand(char *command) { if (strcmp(command, "test_flash")) return 1; if (test_flash_nand()) printf("NACK test_flash read_different_write\n"); else printf("ACK test_flash\n"); return 0; } #endif // IOTLAB_M3 /* Leds Commands */ static int cmd_leds_on(char *command) { uint8_t leds = 0; if (1 != sscanf(command, "leds_on %u", &leds)) return 1; leds_on(leds); printf("ACK leds_on %x\n", leds); return 0; } static int cmd_leds_off(char *command) { uint8_t leds = 0; if (1 != sscanf(command, "leds_off %u", &leds)) return 1; leds_off(leds); printf("ACK leds_off %x\n", leds); return 0; }
static int32_t radio_injection(uint8_t cmd_type, packet_t *pkt) { // Stop all proper_stop(); /* * Expected packet format is (length:13B): * * channels bitmap [4B] * * TX period (1/200s) [2B] * * num packets per channel [2B] * * TX power [4B] * * packet size [1B] */ if (pkt->length != 13) { log_warning("Bad Packet length: %u", pkt->length); pkt->length = 0; return 0; } const uint8_t *data = pkt->data; memcpy(&radio.channels, data, 4); data += 4; uint16_t tx_period; memcpy(&tx_period, data, 2); data += 2; memcpy(&radio.injection.num_pkts_per_channel, data, 2); data += 2; float tx_power; memcpy(&tx_power, data, 4); data += 4; uint32_t pkt_size = *data++; if ((radio.channels & PHY_MAP_CHANNEL_2400_ALL) == 0) { log_warning("No channel selected %u", radio.channels); pkt->length = 0; return 0; } radio.channels &= PHY_MAP_CHANNEL_2400_ALL; if (pkt_size > 125) { log_warning("Bad injection length value: %u", pkt_size); pkt->length = 0; return 0; } if (tx_period == 0) { log_warning("Invalid injection TX period: %u", tx_period); pkt->length = 0; return 0; } log_info( "Radio Injection on channels %08x, period %u, %u pkt/ch, %fdBm, %ubytes", radio.channels, tx_period, radio.injection.num_pkts_per_channel, tx_power, pkt_size); // Prepare packet phy_prepare_packet(&radio.injection.pkt); radio.injection.pkt.length = pkt_size; int i; for (i = 0; i < pkt_size; i++) { // Set payload as ascii characters radio.injection.pkt.data[i] = (0x20 + i) % 95; } // Select first channel for (radio.current_channel = 0; (radio.channels & (1 << radio.current_channel)) == 0; radio.current_channel++) { } // Clear TX count radio.injection.current_pkts_on_channel = 0; // Wake PHY and configure phy_set_channel(platform_phy, radio.current_channel); phy_set_power(platform_phy, phy_convert_power(tx_power)); // Start sending timer soft_timer_set_handler(&radio.period_tim, injection_time, NULL); soft_timer_start(&radio.period_tim, soft_timer_ms_to_ticks(5 * tx_period), 1); // OK pkt->length = 0; return 1; }