diff --git a/src/librnode.c b/src/librnode.c index 73d5bf4..c5e0d75 100755 --- a/src/librnode.c +++ b/src/librnode.c @@ -25,7 +25,7 @@ int rnode_handle_resp(struct RNode* rn, bool* in_frame, uint8_t* cmd, uint8_t* c uint8_t sbyte; uint8_t resp_buf[RESP_BUF_SIZE] = {0}; - len = read_port(rn->fd, resp_buf, RESP_BUF_SIZE); + len = read_port(&rn->handle, resp_buf, RESP_BUF_SIZE); if (len > 0) { log_trace("Read %d bytes", len); @@ -261,9 +261,9 @@ int rnode_detect(struct RNode* rn) { uint16_t frame_len = 0; memcpy(tx_buf, (uint8_t[16]){FEND, CMD_DETECT, DETECT_REQ, FEND, FEND, CMD_FW_VERSION, 0x00, FEND, FEND, CMD_PLATFORM, 0x00, FEND, FEND, CMD_MCU, 0x00, FEND}, 16*sizeof(uint8_t)); - err_code = write_port(rn->fd, tx_buf, 16); + err_code = write_port(&rn->handle, tx_buf, 16); if (!err_code) { - sleep_ms(100); + sleep_mils(100); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); return (err_code == -1) ? -1 : rn->connected; } else { @@ -283,9 +283,9 @@ int rnode_detect(struct RNode* rn) { int rnode_init(struct RNode* rn, char* port, uint32_t baud, bool detect, bool force_detect) { rn->port = port; rn->baud = baud; - rn->fd = open_port(rn->port, baud); + open_port(&rn->handle, rn->port, baud); - if (rn->fd > 0) { + if (&rn->handle > 0) { if (detect) { if (rnode_detect(rn)) { log_info("RNode detected successfully on port %s", port); @@ -315,7 +315,7 @@ int rnode_init(struct RNode* rn, char* port, uint32_t baud, bool detect, bool fo int rnode_reset(struct RNode* rn) { uint8_t tx_buf[4]; memcpy(tx_buf, (uint8_t[4]){FEND, CMD_RESET, CMD_RESET_BYTE, FEND}, 4*sizeof(uint8_t)); - return write_port(rn->fd, tx_buf, 4); + return write_port(&rn->handle, tx_buf, 4); } /* Sets an RNode's platform attribute manually @@ -347,7 +347,7 @@ int rnode_set_bt(struct RNode* rn, uint8_t val) { } uint8_t tx_buf[4]; memcpy(tx_buf, (uint8_t[4]){FEND, CMD_BT_CTRL, val, FEND}, 4*sizeof(uint8_t)); - err_code = write_port(rn->fd, tx_buf, 4); + err_code = write_port(&rn->handle, tx_buf, 4); return err_code; } @@ -379,7 +379,7 @@ int rnode_get_bt_pin(struct RNode* rn, uint32_t timeout) { while (current <= start + timeout) { if (err_code >= 0) { - sleep_ms(50); + sleep_mils(50); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); time(<ime); current = ltime; if (rn->bt_pairing_pin != 0) { @@ -406,7 +406,7 @@ int rnode_set_disp_int(struct RNode* rn, uint8_t disp_int) { int err_code = 0; uint8_t tx_buf[4]; memcpy(tx_buf, (uint8_t[4]){FEND, CMD_DISP_INT, disp_int, FEND}, 4*sizeof(uint8_t)); - err_code = write_port(rn->fd, tx_buf, 4); + err_code = write_port(&rn->handle, tx_buf, 4); return err_code; } @@ -421,7 +421,7 @@ int rnode_set_disp_timeout(struct RNode* rn, uint8_t timeout) { int err_code = 0; uint8_t tx_buf[4]; memcpy(tx_buf, (uint8_t[4]){FEND, CMD_DISP_INT, timeout, FEND}, 4*sizeof(uint8_t)); - err_code = write_port(rn->fd, tx_buf, 4); + err_code = write_port(&rn->handle, tx_buf, 4); return err_code; } @@ -436,7 +436,7 @@ int rnode_set_disp_addr(struct RNode* rn, uint8_t addr) { int err_code = 0; uint8_t tx_buf[4]; memcpy(tx_buf, (uint8_t[4]){FEND, CMD_DISP_ADDR, addr, FEND}, 4*sizeof(uint8_t)); - err_code = write_port(rn->fd, tx_buf, 4); + err_code = write_port(&rn->handle, tx_buf, 4); return err_code; } @@ -454,7 +454,7 @@ int rnode_set_disp_rot(struct RNode* rn, uint8_t rot) { uint8_t tx_buf[4]; memcpy(tx_buf, (uint8_t[4]){FEND, CMD_DISP_ROT, rot, FEND}, 4*sizeof(uint8_t)); - err_code = write_port(rn->fd, tx_buf, 4); + err_code = write_port(&rn->handle, tx_buf, 4); return err_code; } @@ -469,7 +469,7 @@ int rnode_start_disp_recon(struct RNode* rn) { int err_code = 0; uint8_t tx_buf[4]; memcpy(tx_buf, (uint8_t[4]){FEND, CMD_DISP_RCND, 1, FEND}, 4*sizeof(uint8_t)); - err_code = write_port(rn->fd, tx_buf, 4); + err_code = write_port(&rn->handle, tx_buf, 4); return err_code; } @@ -484,7 +484,7 @@ int rnode_set_np_int(struct RNode* rn, uint8_t np_int) { int err_code; uint8_t tx_buf[4]; memcpy(tx_buf, (uint8_t[4]){FEND, CMD_NP_INT, np_int, FEND}, 4*sizeof(uint8_t)); - err_code = write_port(rn->fd, tx_buf, 4); + err_code = write_port(&rn->handle, tx_buf, 4); return err_code; } @@ -505,10 +505,10 @@ int rnode_get_interfaces(struct RNode* rn) { uint16_t frame_len = 0; memcpy(tx_buf, (uint8_t[4]){FEND, CMD_INTERFACES, 0x00, FEND}, 4*sizeof(uint8_t)); - err_code = write_port(rn->fd, tx_buf, 4); + err_code = write_port(&rn->handle, tx_buf, 4); if (!err_code) { - sleep_ms(100); + sleep_mils(100); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); } @@ -536,7 +536,7 @@ int rnode_select_interface(struct RNode* rn, uint8_t interface) { if (rn->interfaces[interface] != 0) { memcpy(tx_buf, (uint8_t[4]){FEND, CMD_SEL_INT, interface, FEND}, 4*sizeof(uint8_t)); - err_code = write_port(rn->fd, tx_buf, 4); + err_code = write_port(&rn->handle, tx_buf, 4); } else { return err_code; } @@ -574,10 +574,10 @@ int rnode_set_freq(struct RNode* rn, uint32_t freq) { memcpy(tx_buf + 2, escaped_freq, escaped_freq_size*sizeof(uint8_t)); tx_buf[2 + escaped_freq_size] = FEND; - err_code = write_port(rn->fd, tx_buf, 2 + escaped_freq_size + 1); + err_code = write_port(&rn->handle, tx_buf, 2 + escaped_freq_size + 1); if (!err_code) { - sleep_ms(100); + sleep_mils(100); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); if (!err_code) { return rn->freq; @@ -606,10 +606,10 @@ int rnode_get_freq(struct RNode* rn) { memcpy(tx_buf, (uint8_t[7]){FEND, CMD_FREQUENCY, 0, 0, 0, 0, FEND}, 7*sizeof(uint8_t)); - err_code = write_port(rn->fd, tx_buf, 7); + err_code = write_port(&rn->handle, tx_buf, 7); if (!err_code) { - sleep_ms(100); + sleep_mils(100); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); if (!err_code) { return rn->freq; @@ -646,10 +646,10 @@ int rnode_set_bw(struct RNode* rn, uint32_t bw) { memcpy(tx_buf + 2, escaped_bw, escaped_bw_size*sizeof(uint8_t)); tx_buf[2 + escaped_bw_size] = FEND; - err_code = write_port(rn->fd, tx_buf, 2 + escaped_bw_size + 1); + err_code = write_port(&rn->handle, tx_buf, 2 + escaped_bw_size + 1); if (!err_code) { - sleep_ms(100); + sleep_mils(100); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); if (!err_code) { return rn->bw; @@ -678,10 +678,10 @@ int rnode_get_bw(struct RNode* rn) { memcpy(tx_buf, (uint8_t[7]){FEND, CMD_BANDWIDTH, 0, 0, 0, 0, FEND}, 7*sizeof(uint8_t)); - err_code = write_port(rn->fd, tx_buf, 7); + err_code = write_port(&rn->handle, tx_buf, 7); if (!err_code) { - sleep_ms(100); + sleep_mils(100); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); if (!err_code) { return rn->bw; @@ -710,10 +710,10 @@ int rnode_set_txp(struct RNode* rn, uint8_t txp) { memcpy(tx_buf, (uint8_t[4]){FEND, CMD_TXPOWER, txp, FEND}, 4*sizeof(uint8_t)); - err_code = write_port(rn->fd, tx_buf, 4); + err_code = write_port(&rn->handle, tx_buf, 4); if (!err_code) { - sleep_ms(100); + sleep_mils(100); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); if (err_code > 0) { return txp == rn->txp ? 0 : -1; @@ -742,10 +742,10 @@ int rnode_get_txp(struct RNode* rn) { memcpy(tx_buf, (uint8_t[4]){FEND, CMD_TXPOWER, 0xFF, FEND}, 4*sizeof(uint8_t)); - err_code = write_port(rn->fd, tx_buf, 4); + err_code = write_port(&rn->handle, tx_buf, 4); if (!err_code) { - sleep_ms(100); + sleep_mils(100); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); if (!err_code) { return rn->txp; @@ -774,10 +774,10 @@ int rnode_set_sf(struct RNode* rn, uint8_t sf) { memcpy(tx_buf, (uint8_t[4]){FEND, CMD_SF, sf, FEND}, 4*sizeof(uint8_t)); - err_code = write_port(rn->fd, tx_buf, 4); + err_code = write_port(&rn->handle, tx_buf, 4); if (!err_code) { - sleep_ms(100); + sleep_mils(100); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); if (err_code > 0) { return sf == rn->sf ? 0 : -1; @@ -807,10 +807,10 @@ int rnode_get_sf(struct RNode* rn) { memcpy(tx_buf, (uint8_t[4]){FEND, CMD_SF, 0xFF, FEND}, 4*sizeof(uint8_t)); - err_code = write_port(rn->fd, tx_buf, 4); + err_code = write_port(&rn->handle, tx_buf, 4); if (!err_code) { - sleep_ms(100); + sleep_mils(100); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); if (!err_code) { return rn->sf; @@ -839,10 +839,10 @@ int rnode_set_cr(struct RNode* rn, uint8_t cr) { memcpy(tx_buf, (uint8_t[4]){FEND, CMD_CR, cr, FEND}, 4*sizeof(uint8_t)); - err_code = write_port(rn->fd, tx_buf, 4); + err_code = write_port(&rn->handle, tx_buf, 4); if (!err_code) { - sleep_ms(100); + sleep_mils(100); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); if (err_code > 0) { return cr == rn->cr ? 0 : -1; @@ -872,10 +872,10 @@ int rnode_get_cr(struct RNode* rn) { memcpy(tx_buf, (uint8_t[4]){FEND, CMD_CR, 0xFF, FEND}, 4*sizeof(uint8_t)); - err_code = write_port(rn->fd, tx_buf, 4); + err_code = write_port(&rn->handle, tx_buf, 4); if (!err_code) { - sleep_ms(100); + sleep_mils(100); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); if (err_code > 0) { return rn->cr; @@ -907,10 +907,10 @@ float rnode_set_st_alock(struct RNode* rn, float at_l) { memcpy(tx_buf + 2, (uint8_t[2]){(at >> 8 & 0xFF), (at & 0xFF)}, 1*sizeof(uint8_t)); tx_buf[2 + 1] = FEND; - err_code = write_port(rn->fd, tx_buf, 2 + 1 + 1); + err_code = write_port(&rn->handle, tx_buf, 2 + 1 + 1); if (!err_code) { - sleep_ms(100); + sleep_mils(100); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); if (err_code > 0) { return at_l == rn->st_alock ? 0 : -1; @@ -942,10 +942,10 @@ float rnode_set_lt_alock(struct RNode* rn, float at_l) { memcpy(tx_buf + 2, (uint8_t[2]){(at >> 8 & 0xFF), (at & 0xFF)}, 1*sizeof(uint8_t)); tx_buf[2 + 1] = FEND; - err_code = write_port(rn->fd, tx_buf, 2 + 1 + 1); + err_code = write_port(&rn->handle, tx_buf, 2 + 1 + 1); if (!err_code) { - sleep_ms(100); + sleep_mils(100); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); if (err_code > 0) { return at_l == rn->lt_alock ? 0 : -1; @@ -974,10 +974,10 @@ int rnode_set_interface_state(struct RNode* rn, bool state) { memcpy(tx_buf, (uint8_t[4]){FEND, CMD_RADIO_STATE, state, FEND}, 4*sizeof(uint8_t)); - err_code = write_port(rn->fd, tx_buf, 4); + err_code = write_port(&rn->handle, tx_buf, 4); if (!err_code) { - sleep_ms(100); + sleep_mils(100); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); if (err_code > 0) { return state == rn->int_state[rn->sel_int] ? 0 : -1; @@ -1010,7 +1010,7 @@ int rnode_get_interface_state(struct RNode* rn) { int rnode_set_normal_mode(struct RNode* rn) { uint8_t tx_buf[4]; memcpy(tx_buf, (uint8_t[4]){FEND, CMD_CONF_DELETE, 0x00, FEND}, 4*sizeof(uint8_t)); - return write_port(rn->fd, tx_buf, 4); + return write_port(&rn->handle, tx_buf, 4); } /* Enable the serial TNC mode of operation on an RNode @@ -1023,7 +1023,7 @@ int rnode_set_tnc_mode(struct RNode* rn) { int err_code = 0; uint8_t tx_buf[4]; memcpy(tx_buf, (uint8_t[4]){FEND, CMD_CONF_SAVE, 0x00, FEND}, 4*sizeof(uint8_t)); - err_code = write_port(rn->fd, tx_buf, 4); + err_code = write_port(&rn->handle, tx_buf, 4); return err_code; } @@ -1033,7 +1033,7 @@ int rnode_set_int_avoid(struct RNode* rn, bool avoid) { int err_code = 0; uint8_t tx_buf[4]; memcpy(tx_buf, (uint8_t[4]){FEND, CMD_DIS_IA, !avoid, FEND}, 4*sizeof(uint8_t)); - err_code = write_port(rn->fd, tx_buf, 4); + err_code = write_port(&rn->handle, tx_buf, 4); return err_code; } @@ -1064,7 +1064,7 @@ int rnode_set_fw_hash(struct RNode* rn, uint8_t* hash) { encode_slip(escaped_hash, &escaped_hash_size, hash, FW_HASH_SIZE, false); memcpy(tx_buf + 2, escaped_hash, escaped_hash_size*sizeof(uint8_t)); tx_buf[2 + escaped_hash_size] = FEND; - return write_port(rn->fd, tx_buf, 2 + escaped_hash_size + 1); + return write_port(&rn->handle, tx_buf, 2 + escaped_hash_size + 1); } /* Write to an arbitrary address in an RNode's EEPROM @@ -1091,10 +1091,10 @@ int rnode_write_eeprom(struct RNode* rn, uint8_t address, uint8_t value) { tx_buf[2 + escaped_data_size] = FEND; - err_code = write_port(rn->fd, tx_buf, 2 + escaped_data_size + 1); + err_code = write_port(&rn->handle, tx_buf, 2 + escaped_data_size + 1); if (!err_code) { - sleep_ms(6); + sleep_mils(6); } return err_code; } @@ -1359,10 +1359,10 @@ int rnode_dump_eeprom(struct RNode* rn) { uint16_t frame_len = 0; memcpy(tx_buf, (uint8_t[4]){FEND, CMD_ROM_READ, 0x00, FEND}, 4*sizeof(uint8_t)); - err_code = write_port(rn->fd, tx_buf, 4); + err_code = write_port(&rn->handle, tx_buf, 4); if (!err_code) { - sleep_ms(100); + sleep_mils(100); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); } @@ -1471,28 +1471,28 @@ int rnode_wipe_eeprom(struct RNode* rn) { log_trace("RNode firmware version too low!"); return -2; } else { - err_code = write_port(rn->fd, tx_buf, 4); + err_code = write_port(&rn->handle, tx_buf, 4); } if (err_code) { return err_code; } - err_code = close_port(rn->fd); + err_code = close_port(&rn->handle); if (!err_code) { - sleep_ms(13000); + sleep_mils(13000); if (rn->platform == PLATFORM_NRF52) { // Due to the current janky emulated EEPROM implementation for the // RAK4631, extra time must be given to allow for writing. - sleep_ms(10000); + sleep_mils(10000); } } else { return err_code; } - rn->fd = open_port(rn->port, rn->baud); + open_port(&rn->handle, rn->port, rn->baud); return (err_code == 0) ? 0 : -1; } @@ -1516,12 +1516,12 @@ int rnode_flash(struct RNode* rn, char* zip_path, bool update, uint8_t* serial, // show update logo on RNode display uint8_t tx_buf[4]; memcpy(tx_buf, (uint8_t[4]){FEND, CMD_FW_UPD, 0x01, FEND}, 4*sizeof(uint8_t)); - err_code = write_port(rn->fd, tx_buf, 4); + err_code = write_port(&rn->handle, tx_buf, 4); } // Close port to prepare for flashing if (!err_code) { - err_code = close_port(rn->fd); + err_code = close_port(&rn->handle); } if (!err_code) { @@ -1697,11 +1697,11 @@ int rnode_flash(struct RNode* rn, char* zip_path, bool update, uint8_t* serial, } // Wait for reset and boot. This value may have to be adjusted in the // future if firmware becomes more complex. - sleep_ms(15000); + sleep_mils(15000); - rn->fd = open_port(rn->port, rn->baud); + open_port(&rn->handle, rn->port, rn->baud); - if (rn->fd > 0) { + if (&rn->handle > 0) { if (!update) { uint8_t checksum[CHECKSUM_SIZE]; void* signature; @@ -1752,7 +1752,7 @@ int rnode_flash(struct RNode* rn, char* zip_path, bool update, uint8_t* serial, } if (!err_code) { - sleep_ms(750); + sleep_mils(750); err_code = rnode_set_fw_hash(rn, hash); } else { return err_code; @@ -1761,18 +1761,18 @@ int rnode_flash(struct RNode* rn, char* zip_path, bool update, uint8_t* serial, if (!err_code) { log_info("Waiting 10 seconds for RNode to come online..."); - sleep_ms(10000); + sleep_mils(10000); if (rn->platform == PLATFORM_NRF52) { - err_code = close_port(rn->fd); + err_code = close_port(&rn->handle); // Due to the current janky emulated EEPROM implementation for the // RAK4631, extra time must be given to allow for writing. log_info("Waiting an extra 15 seconds for RNode to come online..."); - sleep_ms(15000); + sleep_mils(15000); } else if (rn->platform == PLATFORM_ESP32) { rnode_reset(rn); - err_code = close_port(rn->fd); + err_code = close_port(&rn->handle); log_info("Waiting for ESP32 reset..."); - sleep_ms(7000); + sleep_mils(7000); } err_code = rnode_init(rn, rn->port, rn->baud, true, true); } else { @@ -1789,9 +1789,9 @@ int rnode_flash(struct RNode* rn, char* zip_path, bool update, uint8_t* serial, int rnode_disconnect(struct RNode* rn) { uint8_t tx_buf[4]; memcpy(tx_buf, (uint8_t[4]){FEND, CMD_LEAVE, 0xFF, FEND}, 4*sizeof(uint8_t)); - return write_port(rn->fd, tx_buf, 4); + return write_port(&rn->handle, tx_buf, 4); } int rnode_cleanup(struct RNode* rn) { - close_port(rn->fd); + close_port(&rn->handle); } diff --git a/src/librnode.h b/src/librnode.h index e2e58e2..4469ab2 100644 --- a/src/librnode.h +++ b/src/librnode.h @@ -68,7 +68,7 @@ struct RNode { char* port; - int fd; + struct serial handle; uint32_t baud; uint8_t platform; diff --git a/src/libs/flashers/nrf/dfu_serial.c b/src/libs/flashers/nrf/dfu_serial.c index 58b08e8..c46f9b3 100644 --- a/src/libs/flashers/nrf/dfu_serial.c +++ b/src/libs/flashers/nrf/dfu_serial.c @@ -180,7 +180,7 @@ static void dfu_serial_u32_to_bytes(uint32_t val, uint8_t* array, uint16_t offse // Waits the time necessary for the MCU to erase its flash before we begin sending the new image int dfu_serial_wait_for_erase(uint32_t total_img_size) { - return sleep_ms(MAX(0.5, (total_img_size / FLASH_PAGE_SIZE) * FLASH_PAGE_ERASE_TIME)); + return sleep_mils(MAX(0.5, (total_img_size / FLASH_PAGE_SIZE) * FLASH_PAGE_ERASE_TIME)); } int dfu_serial_send_start_dfu(uart_drv_t *p_uart, uint8_t mode, uint32_t softdevice_size, uint32_t bootloader_size, uint32_t application_size) { @@ -330,7 +330,7 @@ int dfu_serial_send_firmware(uart_drv_t *p_uart, struct RNode* rn, const uint8_t // nrf52 is erasing and writing to flash if (i % 8 == 0) { - err_code = sleep_ms(FLASH_PAGE_WRITE_TIME); + err_code = sleep_mils(FLASH_PAGE_WRITE_TIME); if (err_code) { log_error("Timing failed during flash, please try flashing again!"); break; @@ -339,7 +339,7 @@ int dfu_serial_send_firmware(uart_drv_t *p_uart, struct RNode* rn, const uint8_t } if (!err_code) { - err_code = sleep_ms(FLASH_PAGE_WRITE_TIME); + err_code = sleep_mils(FLASH_PAGE_WRITE_TIME); if (err_code) { log_error("Timing failed during flash, please try flashing again!"); diff --git a/src/libs/flashers/nrf/uart_linux.c b/src/libs/flashers/nrf/uart_linux.c index 291d1ad..36e66a6 100644 --- a/src/libs/flashers/nrf/uart_linux.c +++ b/src/libs/flashers/nrf/uart_linux.c @@ -135,12 +135,12 @@ int uart_drv_open(uart_drv_t *p_uart, bool touch) // Opening and closing serial port once is necessary to enter DFU mode if (touch) { - sleep_ms(SERIAL_WAIT_TIME); + sleep_mils(SERIAL_WAIT_TIME); close(fd); // Wait, then open it again - sleep_ms(TOUCH_RESET_WAIT_TIME); + sleep_mils(TOUCH_RESET_WAIT_TIME); fd = open(tty_path, O_RDWR | O_NOCTTY); diff --git a/src/libs/serial.h b/src/libs/serial.h index 9934f19..fa27933 100644 --- a/src/libs/serial.h +++ b/src/libs/serial.h @@ -11,16 +11,33 @@ * with this program. If not, see . */ + #ifndef SERIAL_H +#include + #define SERIAL_H -int open_port(char* path, uint32_t baud); +#ifdef linux +struct serial { + int fd; +}; +#endif -int write_port(int fd, uint8_t* data, uint16_t data_len); +#ifdef _WIN32 +#include -int read_port(int fd, uint8_t* rx_data, uint16_t size); +struct serial { + HANDLE fd; +}; +#endif -int close_port(int fd); +int open_port(struct serial* handle, char* path, uint32_t baud); + +int write_port(struct serial* handle, uint8_t* data, uint16_t data_len); + +int read_port(struct serial* handle, uint8_t* rx_data, uint16_t size); + +int close_port(struct serial* handle); #endif diff --git a/src/libs/serial_linux.c b/src/libs/serial_linux.c index 8b5268c..190ccc8 100644 --- a/src/libs/serial_linux.c +++ b/src/libs/serial_linux.c @@ -11,13 +11,15 @@ * with this program. If not, see . */ +#include "serial.h" + #include #include #include #include #include "logging/log.h" -int open_port(char* path, uint32_t baud) { +int open_port(struct serial* handle, char* path, uint32_t baud) { struct termios options; speed_t baud_s; @@ -31,9 +33,9 @@ int open_port(char* path, uint32_t baud) { break; } - int fd = open(path, O_RDWR | O_NOCTTY); + handle->fd = open(path, O_RDWR | O_NOCTTY); - tcgetattr(fd, &options); + tcgetattr(handle->fd, &options); cfsetispeed(&options, baud_s); cfsetospeed(&options, baud_s); @@ -57,17 +59,17 @@ int open_port(char* path, uint32_t baud) { options.c_cc[VMIN] = 0; options.c_cc[VTIME] = 5; // 0.5 seconds - if (tcsetattr(fd, TCSANOW, &options)) + if (tcsetattr(handle->fd, TCSANOW, &options)) { log_error("Cannot set RNode port options!"); return -1; } else { - return fd; + return 0; } } -int write_port(int fd, uint8_t* data, uint16_t data_len) { +int write_port(struct serial* handle, uint8_t* data, uint16_t data_len) { unsigned char data_str[1024] = ""; for (int i = 0; i < data_len; i++) { sprintf(data_str+(i*3), "%02x ", data[i]); @@ -75,7 +77,7 @@ int write_port(int fd, uint8_t* data, uint16_t data_len) { log_trace("Writing [%s] to RNode serial port.", data_str); - if (write(fd, data, data_len) != data_len) { + if (write(handle->fd, data, data_len) != data_len) { log_error("Could not write all data to the RNode port! Is the RNode still connected?"); return -1; } else { @@ -83,14 +85,19 @@ int write_port(int fd, uint8_t* data, uint16_t data_len) { } } -int read_port(int fd, uint8_t* rx_data, uint16_t size) { - int ret = read(fd, rx_data, size); +int read_port(struct serial* handle, uint8_t* rx_data, uint16_t size) { + int ret = read(handle->fd, rx_data, size); if (ret == -1) { log_error("Could not read from RNode port! Is the RNode still connected?"); } return ret; } -int close_port(int fd) { - return close(fd); +int close_port(struct serial* handle) { + if (!close(handle->fd)) { + log_error("Error closing RNode port! Is the RNode still connected?"); + return -1; + } else { + return 0; + } } diff --git a/src/libs/serial_win32.c b/src/libs/serial_win32.c index 228850c..1049b05 100644 --- a/src/libs/serial_win32.c +++ b/src/libs/serial_win32.c @@ -10,3 +10,86 @@ * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ + +#include "serial.h" + +int open_port(struct serial* handle, char* path, uint32_t baud) { + DCB dcbSerialParams = {0}; + COMMTIMEOUTS timeouts = {0}; + uint32_t baud_s; + + // Currently RNodes only support a baud rate of 115200, add more support as needed in future. + switch (baud) { + case 115200: + baud_s = CBR_115200; + break; + default: + baud_s = CBR_115200; + break; + } + + handle.fd = CreateFile( + path, GENERIC_READ|GENERIC_WRITE, 0, NULL, + OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); + + if (handle.fd == INVALID_HANDLE_VALUE) { + log_error("Cannot open RNode port!"); + return -1; + } + + dcbSerialParams.DCBLength = sizeof(dcbSerialParams); + dcbSerialParams.BaudRate = baud_s; + + if (GetCommState(handle.fd, &dcbSerialParams) == 0) { + CloseHandle(handle.fd); + log_error("Cannot set RNode port options!"); + return -1; + } + + timeouts.ReadIntervalTimeout = 50; + timeouts.ReadTotalTimeoutConstant = 50; + timeouts.ReadTotalTimeoutMultiplier = 10; + timeouts.WriteTotalTimeoutConstant = 50; + timeouts.WriteTotalTimeoutMultiplier = 10; + + if (SetCommTimeouts(handle.fd, &timeouts) == 0) { + CloseHandle(handle.fd); + log_error("Cannot set RNode port timeouts!"); + return -1; + } +} + +int write_port(struct serial* handle, uint8_t* data, uint16_t data_len) { + unsigned char data_str[1024] = ""; + + for (int i = 0; i < data_len; i++) { + sprintf(data_str+(i*3), "%02x ", data[i]); + } + + log_trace("Writing [%s] to RNode serial port.", data_str); + + if (!WriteFile(handle.fd, data_len, NULL, NULL)) { + log_error("Could not write all data to the RNode port! Is the RNode still connected?"); + CloseHandle(handle.fd); + return -1; + } + return 0; +} + +int read_port(struct serial* handle, uint8_t* rx_data, uint16_t size) { + int ret; + if (!ReadFile(handle.fd, rx_data, size, &ret, NULL)) { + log_error("Could not read from RNode port! Is the RNode still connected?"); + return -1; + } + return ret; +} + +int close_port(struct serial* handle) { + if (CloseHandle(handle.fd) == 0) { + log_error("Error closing RNode port! Is the RNode still connected?"); + return -1; + } + + return 0; +} diff --git a/src/libs/util.c b/src/libs/util.c index 33554e4..4def7be 100644 --- a/src/libs/util.c +++ b/src/libs/util.c @@ -13,7 +13,7 @@ #include "util.h" -int sleep_ms(int ms) +int sleep_mils(int ms) { #ifdef WIN32 Sleep(ms); diff --git a/src/libs/util.h b/src/libs/util.h index 61ed827..0ad0867 100644 --- a/src/libs/util.h +++ b/src/libs/util.h @@ -17,6 +17,6 @@ #include #include -int sleep_ms(int ms); +int sleep_mils(int ms); #endif