/** * Send BGAPI packet using UART interface * * @param len1 Length of fixed portion of packet (always at least 4) * @param data1 Fixed-length portion of BGAPI packet (should always be <len1> bytes long) * @param len2 Length of variable portion of packet data payload (trailing uint8array or uint16array) * @param data2 Variable-length portion of data payload (should always be <len2> bytes long) */ void send_api_packet(uint8 len1, uint8 *data1, uint16 len2, uint8 *data2) { #ifdef DEBUG // display outgoing BGAPI packet print_raw_packet((struct ble_header *)data1, data2, 1); #endif if (uart_tx(len1, data1) || uart_tx(len2, data2)) { // uart_tx returns non-zero on failure fprintf(stderr, "ERROR: Writing to serial port failed\n"); exit(1); } }
// read and parse message from usb dongle over uart void *read_message() { fprintf(stderr,"reading thread started\n"); unsigned char data[256]; // enough for BLE struct ble_header hdr; int r; while (state != state_finish) { r = uart_rx(sizeof(hdr), (unsigned char *)&hdr, UART_TIMEOUT); if (!r) { //printf("ERROR: UART timeout. Error code:%d\n", r); continue; // return NULL; // timeout } else if (r < 0) { fprintf(stderr,"ERROR: Reading header failed. Error code:%d\n", r); pthread_exit(NULL); } if (hdr.lolen) { r = uart_rx(hdr.lolen, data, UART_TIMEOUT); if (r <= 0) { fprintf(stderr,"ERROR: Reading data failed. Error code:%d\n", r); pthread_exit(NULL); } } const struct ble_msg *msg = ble_get_msg_hdr(hdr); #ifdef DEBUG print_raw_packet(&hdr, data); #endif if (!msg) { fprintf(stderr,"ERROR: Unknown message received\n"); pthread_exit(NULL); } msg->handler(data); //usleep(500); } pthread_exit(NULL); //return 0; }
int read_message(int timeout_ms) { unsigned char data[256]; // enough for BLE struct ble_header hdr; int r; r = uart_rx(sizeof(hdr), (unsigned char *)&hdr, UART_TIMEOUT); if (!r) { return -1; // timeout } else if (r < 0) { printf("ERROR: Reading header failed. Error code: %d\n", r); return 1; } if (hdr.lolen) { r = uart_rx(hdr.lolen, data, UART_TIMEOUT); if (r <= 0) { printf("ERROR: Reading data failed. Error code: %d\n", r); return 1; } } const struct ble_msg *msg = ble_get_msg_hdr(hdr); #ifdef DEBUG print_raw_packet(&hdr, data); #endif if (!msg) { printf("ERROR: Unknown message received\n"); exit(1); } msg->handler(data); return 0; }
/** * Receive BGAPI packet using UART interface * * @param timeout_ms Milliseconds to wait before timing out on the UART RX operation */ int read_api_packet(int timeout_ms) { unsigned char data[256]; // enough for BLE struct ble_header hdr; int r; r = uart_rx(sizeof(hdr), (unsigned char *)&hdr, timeout_ms); if (!r) { return -1; // timeout } else if (r < 0) { printf("ERROR: Reading header failed. Error code:%d\n", r); return 1; } if (hdr.lolen) { r = uart_rx(hdr.lolen, data, timeout_ms); if (r <= 0) { printf("ERROR: Reading data failed. Error code:%d\n", r); return 1; } } // use BGLib function to create correct ble_msg structure based on the header // (header contains command class/ID info, used to identify the right structure to apply) const struct ble_msg *msg = ble_get_msg_hdr(hdr); #ifdef DEBUG // display incoming BGAPI packet print_raw_packet(&hdr, data); #endif if (!msg) { printf("ERROR: Unknown message received\n"); exit(1); } // call the appropriate handler function with any payload data // (this is what triggers the ble_evt_* and ble_rsp_* functions) msg -> handler(data); return 0; }