static int test_write_at(struct harness_t *harness_p) { struct xbee_frame_t frame; uint8_t buf[7]; /* Prepare a frame reading the AT frame "DL". */ frame.type = XBEE_FRAME_TYPE_AT_COMMAND; frame.data.buf[0] = 0x52; frame.data.buf[1] = 'D'; frame.data.buf[2] = 'L'; frame.data.size = 3; BTASSERT(xbee_write(&xbee, &frame) == 0); /* Validate the written frame. */ harness_mock_read("chan_write(buf_p)", &buf[0], 1); harness_mock_read("chan_write(buf_p)", &buf[1], 1); harness_mock_read("chan_write(buf_p)", &buf[2], 1); harness_mock_read("chan_write(buf_p)", &buf[3], 1); harness_mock_read("chan_write(buf_p)", &buf[4], 1); harness_mock_read("chan_write(buf_p)", &buf[5], 1); harness_mock_read("chan_write(buf_p)", &buf[6], 1); harness_mock_read("chan_write(buf_p)", &buf[7], 1); BTASSERTM(&buf[0], "\x7e\x00\x04\x08\x52\x44\x4c\x15", 8); return (0); }
static int test_write_tx_request(struct harness_t *harness_p) { struct xbee_frame_t frame; uint8_t buf[14]; /* Prepare a frame. */ frame.type = XBEE_FRAME_TYPE_TX_REQUEST_16_BIT_ADDRESS; frame.data.buf[0] = 0x01; frame.data.buf[1] = 0x50; frame.data.buf[2] = 0x01; frame.data.buf[3] = 0x00; frame.data.buf[4] = 0x48; frame.data.buf[5] = 0x65; frame.data.buf[6] = 0x6c; frame.data.buf[7] = 0x6c; frame.data.buf[8] = 0x6f; frame.data.size = 9; BTASSERT(xbee_write(&xbee, &frame) == 0); /* Validate the written frame. */ harness_mock_read("chan_write(buf_p)", &buf[0], 1); harness_mock_read("chan_write(buf_p)", &buf[1], 1); harness_mock_read("chan_write(buf_p)", &buf[2], 1); harness_mock_read("chan_write(buf_p)", &buf[3], 1); harness_mock_read("chan_write(buf_p)", &buf[4], 1); harness_mock_read("chan_write(buf_p)", &buf[5], 1); harness_mock_read("chan_write(buf_p)", &buf[6], 1); harness_mock_read("chan_write(buf_p)", &buf[7], 1); harness_mock_read("chan_write(buf_p)", &buf[8], 1); harness_mock_read("chan_write(buf_p)", &buf[9], 1); harness_mock_read("chan_write(buf_p)", &buf[10], 1); harness_mock_read("chan_write(buf_p)", &buf[11], 1); harness_mock_read("chan_write(buf_p)", &buf[12], 1); harness_mock_read("chan_write(buf_p)", &buf[13], 1); BTASSERTM(&buf[0], "\x7e\x00\x0a\x01\x01\x50\x01\x00\x48\x65\x6c\x6c\x6f\xb8", 14); return (0); }
static int test_write_escape(struct harness_t *harness_p) { struct xbee_frame_t frame; uint8_t buf[7]; /* Prepare a frame where 0x11 will be escaped to 0x7d 0x31. */ frame.type = 0x23; frame.data.buf[0] = 0x11; frame.data.size = 1; BTASSERT(xbee_write(&xbee, &frame) == 0); /* Validate the written frame. */ harness_mock_read("chan_write(buf_p)", &buf[0], 1); harness_mock_read("chan_write(buf_p)", &buf[1], 1); harness_mock_read("chan_write(buf_p)", &buf[2], 1); harness_mock_read("chan_write(buf_p)", &buf[3], 1); harness_mock_read("chan_write(buf_p)", &buf[4], 2); harness_mock_read("chan_write(buf_p)", &buf[6], 1); BTASSERTM(&buf[0], "\x7e\x00\x02\x23\x7d\x31\xcb", 7); return (0); }
/** * Task to emit beacons which are received by passing cars. */ void beacon_task(void * pvParameters) { //transmit used to mark if module should be configured as transmitter or receiver //int transmit = 1; //value used to hold value of set-up, start at '0' then set to '1' if done correctly int set_up_okay = 0; char buffer[512]; //AT device for use with hayes commands ATDevice xbee_transmit; xbee_transmit.api.count = xbee_count; xbee_transmit.api.getc = xbee_getc; xbee_transmit.api.write = xbee_write; xbee_transmit.buffer = buffer; xbee_transmit.length = 512; //variable to track number of bad transmissions and allowed threshold before a module reset int threshold = 10; int error_count = 0; //speed used to hold speed value to transmit through xbee and set constants for string construction int speed = 100; int prev_speed = 100; int u_limit = 255; int l_limit = 1; char text_start[5] = "SL: \0"; char text_end[7] = " km/hr\0"; char speed_string[4]; char speed_cat[4]; char send_string[14]; char zero_one[2] = "0\0"; char zero_two[3] = "00\0"; char dollar[2]= "$\0"; /* Initialize the peripherals and state for this task. */ trace_printf("Update check \n"); if (!beacon_task_setup()) { trace_printf("beacon_task: setup failed\n"); vTaskDelete(NULL); return; } else { trace_printf("beacon_task: started\n"); } //once task has started, run continuously for(;;){ //configuration for module acting as transmitter //first, check to see if configuration has been done properly if (set_up_okay == 0){ //call function to configure transmitting xbee module as well as re-configuring serial just in case //if ending back here because not writing to module, figured it was better to reset both xbee_init(); if(!xbee_transmit_setup(&xbee_transmit)){ trace_printf("Xbee configuration failed, try again\n"); } else{ trace_printf("Xbee configured okay \n"); set_up_okay = 1; } } /*after configuring module, want to periodically output transmission /** * The Skywire task will periodically communicate with the MCC. * When updated SL values are received, they will be communicated to * this task via IPC mailbox. */ /** * See: xbee.h and dma_serial.h * xbee_count() - number of bytes available from XBee UART * xbee_getc() - get one byte form XBee UART * xbee_read() - read bytes into a buffer, non-blocking, **not implemented yet** * xbee_write() - blocking write - uses HAL_UART_Transmit */ /* Echo received bytes from UART6 to the console. */ //convert integer speed value to string and pad if needed if (set_up_okay == 1){ trace_printf("Constructing string \n"); SLUpdate slUpdate; if (xQueueReceive(xSLUpdatesQueue, &slUpdate, 0) == pdTRUE) { trace_printf("beacon task: SL = %d\n", slUpdate.limit); speed = (int)slUpdate.limit; } //check that speed limit value is within limits. If not, use previous if (speed >= l_limit && speed <= u_limit) { snprintf(speed_string, 4, "%d", speed); trace_printf("%s \n", speed_string); prev_speed = speed; } else { snprintf(speed_string, 4, "%d", prev_speed); trace_printf("%s \n", speed_string); } speed_cat[0] = '\0'; send_string[0] = '\0'; if (strlen(speed_string) == 1){ trace_printf("Copying string \n"); strcpy(speed_cat, zero_two); trace_printf("%s \n", speed_cat); trace_printf("Concatenating \n"); strcat(speed_cat, speed_string); } else if (strlen(speed_string) == 2){ strcpy(speed_cat, zero_one); strcat(speed_cat, speed_string); } else if (strlen(speed_string) == 3){ strcpy(speed_cat, speed_string); } trace_printf("concatenated speed value \n"); trace_printf("%s \n", speed_cat); //construct phrase to be sent, '$' sign on end to avoid passing along junk strcat(send_string, text_start); strcat(send_string, speed_cat); strcat(send_string, text_end); strcat(send_string, dollar); trace_printf("Sending phrase: %s", send_string); trace_printf("\n"); //send string and do check to see if there was error if (xbee_write((uint8_t*)send_string, 0, 14) != XBEE_OK){ //if error, increment count trace_printf("Error on writing to Xbee \n"); error_count++; } //otherwise, reset error count else { error_count = 0; speed++; } //if error count exceeds threshold, set back to 0 and flag module to be re-configured if (error_count >= threshold) { trace_printf("Resetting module configuration \n"); error_count = 0; set_up_okay = 0; } //incrementing speed value for test //speed++; /* Run task at ~1Hz for now. */ } vTaskDelay(10000); } }