Compare commits

..

No commits in common. "77a0530e410aa19bb76e4dcbc1be6138d0d9677e" and "38f68c0418413ef4a9b0b78c9eb2763a1fd1a0f8" have entirely different histories.

10 changed files with 112 additions and 231 deletions

View File

@ -11,7 +11,27 @@
* with this program. If not, see <https://www.gnu.org/licenses/>. * with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <md5.h>
#include <openssl/sha.h>
#include <libgen.h>
#include <time.h>
#include "librnode.h" #include "librnode.h"
#include "libs/framing.h"
#include "libs/serial.h"
#include "libs/logging/log.h"
#include "libs/util.h"
#include "libs/slip_enc.h"
#include "libs/eeprom.h"
#include "libs/zip.h"
// flashing
#include "libs/flashers/nrf/dfu.h"
#include "libs/flashers/nrf/uart_drv.h"
#include "libs/flashers/esp32/esputil.h"
/* Handles incoming RNode communications from serial /* Handles incoming RNode communications from serial
* Scope: private * Scope: private
@ -25,7 +45,7 @@ int rnode_handle_resp(struct RNode* rn, bool* in_frame, uint8_t* cmd, uint8_t* c
uint8_t sbyte; uint8_t sbyte;
uint8_t resp_buf[RESP_BUF_SIZE] = {0}; uint8_t resp_buf[RESP_BUF_SIZE] = {0};
len = read_port(&rn->handle, resp_buf, RESP_BUF_SIZE); len = read_port(rn->fd, resp_buf, RESP_BUF_SIZE);
if (len > 0) { if (len > 0) {
log_trace("Read %d bytes", len); log_trace("Read %d bytes", len);
@ -261,9 +281,9 @@ int rnode_detect(struct RNode* rn) {
uint16_t frame_len = 0; 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)); 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->handle, tx_buf, 16); err_code = write_port(rn->fd, tx_buf, 16);
if (!err_code) { if (!err_code) {
sleep_mils(100); sleep_ms(100);
err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len);
return (err_code == -1) ? -1 : rn->connected; return (err_code == -1) ? -1 : rn->connected;
} else { } else {
@ -283,9 +303,9 @@ int rnode_detect(struct RNode* rn) {
int rnode_init(struct RNode* rn, char* port, uint32_t baud, bool detect, bool force_detect) { int rnode_init(struct RNode* rn, char* port, uint32_t baud, bool detect, bool force_detect) {
rn->port = port; rn->port = port;
rn->baud = baud; rn->baud = baud;
open_port(&rn->handle, rn->port, baud); rn->fd = open_port(rn->port, baud);
if (&rn->handle > 0) { if (rn->fd > 0) {
if (detect) { if (detect) {
if (rnode_detect(rn)) { if (rnode_detect(rn)) {
log_info("RNode detected successfully on port %s", port); log_info("RNode detected successfully on port %s", port);
@ -315,7 +335,7 @@ int rnode_init(struct RNode* rn, char* port, uint32_t baud, bool detect, bool fo
int rnode_reset(struct RNode* rn) { int rnode_reset(struct RNode* rn) {
uint8_t tx_buf[4]; uint8_t tx_buf[4];
memcpy(tx_buf, (uint8_t[4]){FEND, CMD_RESET, CMD_RESET_BYTE, FEND}, 4*sizeof(uint8_t)); memcpy(tx_buf, (uint8_t[4]){FEND, CMD_RESET, CMD_RESET_BYTE, FEND}, 4*sizeof(uint8_t));
return write_port(&rn->handle, tx_buf, 4); return write_port(rn->fd, tx_buf, 4);
} }
/* Sets an RNode's platform attribute manually /* Sets an RNode's platform attribute manually
@ -347,7 +367,7 @@ int rnode_set_bt(struct RNode* rn, uint8_t val) {
} }
uint8_t tx_buf[4]; uint8_t tx_buf[4];
memcpy(tx_buf, (uint8_t[4]){FEND, CMD_BT_CTRL, val, FEND}, 4*sizeof(uint8_t)); memcpy(tx_buf, (uint8_t[4]){FEND, CMD_BT_CTRL, val, FEND}, 4*sizeof(uint8_t));
err_code = write_port(&rn->handle, tx_buf, 4); err_code = write_port(rn->fd, tx_buf, 4);
return err_code; return err_code;
} }
@ -379,7 +399,7 @@ int rnode_get_bt_pin(struct RNode* rn, uint32_t timeout) {
while (current <= start + timeout) { while (current <= start + timeout) {
if (err_code >= 0) { if (err_code >= 0) {
sleep_mils(50); sleep_ms(50);
err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len);
time(&ltime); current = ltime; time(&ltime); current = ltime;
if (rn->bt_pairing_pin != 0) { if (rn->bt_pairing_pin != 0) {
@ -406,7 +426,7 @@ int rnode_set_disp_int(struct RNode* rn, uint8_t disp_int) {
int err_code = 0; int err_code = 0;
uint8_t tx_buf[4]; uint8_t tx_buf[4];
memcpy(tx_buf, (uint8_t[4]){FEND, CMD_DISP_INT, disp_int, FEND}, 4*sizeof(uint8_t)); memcpy(tx_buf, (uint8_t[4]){FEND, CMD_DISP_INT, disp_int, FEND}, 4*sizeof(uint8_t));
err_code = write_port(&rn->handle, tx_buf, 4); err_code = write_port(rn->fd, tx_buf, 4);
return err_code; return err_code;
} }
@ -421,7 +441,7 @@ int rnode_set_disp_timeout(struct RNode* rn, uint8_t timeout) {
int err_code = 0; int err_code = 0;
uint8_t tx_buf[4]; uint8_t tx_buf[4];
memcpy(tx_buf, (uint8_t[4]){FEND, CMD_DISP_INT, timeout, FEND}, 4*sizeof(uint8_t)); memcpy(tx_buf, (uint8_t[4]){FEND, CMD_DISP_INT, timeout, FEND}, 4*sizeof(uint8_t));
err_code = write_port(&rn->handle, tx_buf, 4); err_code = write_port(rn->fd, tx_buf, 4);
return err_code; return err_code;
} }
@ -436,7 +456,7 @@ int rnode_set_disp_addr(struct RNode* rn, uint8_t addr) {
int err_code = 0; int err_code = 0;
uint8_t tx_buf[4]; uint8_t tx_buf[4];
memcpy(tx_buf, (uint8_t[4]){FEND, CMD_DISP_ADDR, addr, FEND}, 4*sizeof(uint8_t)); memcpy(tx_buf, (uint8_t[4]){FEND, CMD_DISP_ADDR, addr, FEND}, 4*sizeof(uint8_t));
err_code = write_port(&rn->handle, tx_buf, 4); err_code = write_port(rn->fd, tx_buf, 4);
return err_code; return err_code;
} }
@ -454,7 +474,7 @@ int rnode_set_disp_rot(struct RNode* rn, uint8_t rot) {
uint8_t tx_buf[4]; uint8_t tx_buf[4];
memcpy(tx_buf, (uint8_t[4]){FEND, CMD_DISP_ROT, rot, FEND}, 4*sizeof(uint8_t)); memcpy(tx_buf, (uint8_t[4]){FEND, CMD_DISP_ROT, rot, FEND}, 4*sizeof(uint8_t));
err_code = write_port(&rn->handle, tx_buf, 4); err_code = write_port(rn->fd, tx_buf, 4);
return err_code; return err_code;
} }
@ -469,7 +489,7 @@ int rnode_start_disp_recon(struct RNode* rn) {
int err_code = 0; int err_code = 0;
uint8_t tx_buf[4]; uint8_t tx_buf[4];
memcpy(tx_buf, (uint8_t[4]){FEND, CMD_DISP_RCND, 1, FEND}, 4*sizeof(uint8_t)); memcpy(tx_buf, (uint8_t[4]){FEND, CMD_DISP_RCND, 1, FEND}, 4*sizeof(uint8_t));
err_code = write_port(&rn->handle, tx_buf, 4); err_code = write_port(rn->fd, tx_buf, 4);
return err_code; return err_code;
} }
@ -484,7 +504,7 @@ int rnode_set_np_int(struct RNode* rn, uint8_t np_int) {
int err_code; int err_code;
uint8_t tx_buf[4]; uint8_t tx_buf[4];
memcpy(tx_buf, (uint8_t[4]){FEND, CMD_NP_INT, np_int, FEND}, 4*sizeof(uint8_t)); memcpy(tx_buf, (uint8_t[4]){FEND, CMD_NP_INT, np_int, FEND}, 4*sizeof(uint8_t));
err_code = write_port(&rn->handle, tx_buf, 4); err_code = write_port(rn->fd, tx_buf, 4);
return err_code; return err_code;
} }
@ -505,10 +525,10 @@ int rnode_get_interfaces(struct RNode* rn) {
uint16_t frame_len = 0; uint16_t frame_len = 0;
memcpy(tx_buf, (uint8_t[4]){FEND, CMD_INTERFACES, 0x00, FEND}, 4*sizeof(uint8_t)); memcpy(tx_buf, (uint8_t[4]){FEND, CMD_INTERFACES, 0x00, FEND}, 4*sizeof(uint8_t));
err_code = write_port(&rn->handle, tx_buf, 4); err_code = write_port(rn->fd, tx_buf, 4);
if (!err_code) { if (!err_code) {
sleep_mils(100); sleep_ms(100);
err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len);
} }
@ -536,7 +556,7 @@ int rnode_select_interface(struct RNode* rn, uint8_t interface) {
if (rn->interfaces[interface] != 0) { if (rn->interfaces[interface] != 0) {
memcpy(tx_buf, (uint8_t[4]){FEND, CMD_SEL_INT, interface, FEND}, 4*sizeof(uint8_t)); memcpy(tx_buf, (uint8_t[4]){FEND, CMD_SEL_INT, interface, FEND}, 4*sizeof(uint8_t));
err_code = write_port(&rn->handle, tx_buf, 4); err_code = write_port(rn->fd, tx_buf, 4);
} else { } else {
return err_code; return err_code;
} }
@ -574,10 +594,10 @@ int rnode_set_freq(struct RNode* rn, uint32_t freq) {
memcpy(tx_buf + 2, escaped_freq, escaped_freq_size*sizeof(uint8_t)); memcpy(tx_buf + 2, escaped_freq, escaped_freq_size*sizeof(uint8_t));
tx_buf[2 + escaped_freq_size] = FEND; tx_buf[2 + escaped_freq_size] = FEND;
err_code = write_port(&rn->handle, tx_buf, 2 + escaped_freq_size + 1); err_code = write_port(rn->fd, tx_buf, 2 + escaped_freq_size + 1);
if (!err_code) { if (!err_code) {
sleep_mils(100); sleep_ms(100);
err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len);
if (!err_code) { if (!err_code) {
return rn->freq; return rn->freq;
@ -606,10 +626,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)); memcpy(tx_buf, (uint8_t[7]){FEND, CMD_FREQUENCY, 0, 0, 0, 0, FEND}, 7*sizeof(uint8_t));
err_code = write_port(&rn->handle, tx_buf, 7); err_code = write_port(rn->fd, tx_buf, 7);
if (!err_code) { if (!err_code) {
sleep_mils(100); sleep_ms(100);
err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len);
if (!err_code) { if (!err_code) {
return rn->freq; return rn->freq;
@ -646,10 +666,10 @@ int rnode_set_bw(struct RNode* rn, uint32_t bw) {
memcpy(tx_buf + 2, escaped_bw, escaped_bw_size*sizeof(uint8_t)); memcpy(tx_buf + 2, escaped_bw, escaped_bw_size*sizeof(uint8_t));
tx_buf[2 + escaped_bw_size] = FEND; tx_buf[2 + escaped_bw_size] = FEND;
err_code = write_port(&rn->handle, tx_buf, 2 + escaped_bw_size + 1); err_code = write_port(rn->fd, tx_buf, 2 + escaped_bw_size + 1);
if (!err_code) { if (!err_code) {
sleep_mils(100); sleep_ms(100);
err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len);
if (!err_code) { if (!err_code) {
return rn->bw; return rn->bw;
@ -678,10 +698,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)); memcpy(tx_buf, (uint8_t[7]){FEND, CMD_BANDWIDTH, 0, 0, 0, 0, FEND}, 7*sizeof(uint8_t));
err_code = write_port(&rn->handle, tx_buf, 7); err_code = write_port(rn->fd, tx_buf, 7);
if (!err_code) { if (!err_code) {
sleep_mils(100); sleep_ms(100);
err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len);
if (!err_code) { if (!err_code) {
return rn->bw; return rn->bw;
@ -710,10 +730,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)); memcpy(tx_buf, (uint8_t[4]){FEND, CMD_TXPOWER, txp, FEND}, 4*sizeof(uint8_t));
err_code = write_port(&rn->handle, tx_buf, 4); err_code = write_port(rn->fd, tx_buf, 4);
if (!err_code) { if (!err_code) {
sleep_mils(100); sleep_ms(100);
err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len);
if (err_code > 0) { if (err_code > 0) {
return txp == rn->txp ? 0 : -1; return txp == rn->txp ? 0 : -1;
@ -742,10 +762,10 @@ int rnode_get_txp(struct RNode* rn) {
memcpy(tx_buf, (uint8_t[4]){FEND, CMD_TXPOWER, 0xFF, FEND}, 4*sizeof(uint8_t)); memcpy(tx_buf, (uint8_t[4]){FEND, CMD_TXPOWER, 0xFF, FEND}, 4*sizeof(uint8_t));
err_code = write_port(&rn->handle, tx_buf, 4); err_code = write_port(rn->fd, tx_buf, 4);
if (!err_code) { if (!err_code) {
sleep_mils(100); sleep_ms(100);
err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len);
if (!err_code) { if (!err_code) {
return rn->txp; return rn->txp;
@ -774,10 +794,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)); memcpy(tx_buf, (uint8_t[4]){FEND, CMD_SF, sf, FEND}, 4*sizeof(uint8_t));
err_code = write_port(&rn->handle, tx_buf, 4); err_code = write_port(rn->fd, tx_buf, 4);
if (!err_code) { if (!err_code) {
sleep_mils(100); sleep_ms(100);
err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len);
if (err_code > 0) { if (err_code > 0) {
return sf == rn->sf ? 0 : -1; return sf == rn->sf ? 0 : -1;
@ -807,10 +827,10 @@ int rnode_get_sf(struct RNode* rn) {
memcpy(tx_buf, (uint8_t[4]){FEND, CMD_SF, 0xFF, FEND}, 4*sizeof(uint8_t)); memcpy(tx_buf, (uint8_t[4]){FEND, CMD_SF, 0xFF, FEND}, 4*sizeof(uint8_t));
err_code = write_port(&rn->handle, tx_buf, 4); err_code = write_port(rn->fd, tx_buf, 4);
if (!err_code) { if (!err_code) {
sleep_mils(100); sleep_ms(100);
err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len);
if (!err_code) { if (!err_code) {
return rn->sf; return rn->sf;
@ -839,10 +859,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)); memcpy(tx_buf, (uint8_t[4]){FEND, CMD_CR, cr, FEND}, 4*sizeof(uint8_t));
err_code = write_port(&rn->handle, tx_buf, 4); err_code = write_port(rn->fd, tx_buf, 4);
if (!err_code) { if (!err_code) {
sleep_mils(100); sleep_ms(100);
err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len);
if (err_code > 0) { if (err_code > 0) {
return cr == rn->cr ? 0 : -1; return cr == rn->cr ? 0 : -1;
@ -872,10 +892,10 @@ int rnode_get_cr(struct RNode* rn) {
memcpy(tx_buf, (uint8_t[4]){FEND, CMD_CR, 0xFF, FEND}, 4*sizeof(uint8_t)); memcpy(tx_buf, (uint8_t[4]){FEND, CMD_CR, 0xFF, FEND}, 4*sizeof(uint8_t));
err_code = write_port(&rn->handle, tx_buf, 4); err_code = write_port(rn->fd, tx_buf, 4);
if (!err_code) { if (!err_code) {
sleep_mils(100); sleep_ms(100);
err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len);
if (err_code > 0) { if (err_code > 0) {
return rn->cr; return rn->cr;
@ -907,10 +927,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)); memcpy(tx_buf + 2, (uint8_t[2]){(at >> 8 & 0xFF), (at & 0xFF)}, 1*sizeof(uint8_t));
tx_buf[2 + 1] = FEND; tx_buf[2 + 1] = FEND;
err_code = write_port(&rn->handle, tx_buf, 2 + 1 + 1); err_code = write_port(rn->fd, tx_buf, 2 + 1 + 1);
if (!err_code) { if (!err_code) {
sleep_mils(100); sleep_ms(100);
err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len);
if (err_code > 0) { if (err_code > 0) {
return at_l == rn->st_alock ? 0 : -1; return at_l == rn->st_alock ? 0 : -1;
@ -942,10 +962,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)); memcpy(tx_buf + 2, (uint8_t[2]){(at >> 8 & 0xFF), (at & 0xFF)}, 1*sizeof(uint8_t));
tx_buf[2 + 1] = FEND; tx_buf[2 + 1] = FEND;
err_code = write_port(&rn->handle, tx_buf, 2 + 1 + 1); err_code = write_port(rn->fd, tx_buf, 2 + 1 + 1);
if (!err_code) { if (!err_code) {
sleep_mils(100); sleep_ms(100);
err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len);
if (err_code > 0) { if (err_code > 0) {
return at_l == rn->lt_alock ? 0 : -1; return at_l == rn->lt_alock ? 0 : -1;
@ -974,10 +994,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)); memcpy(tx_buf, (uint8_t[4]){FEND, CMD_RADIO_STATE, state, FEND}, 4*sizeof(uint8_t));
err_code = write_port(&rn->handle, tx_buf, 4); err_code = write_port(rn->fd, tx_buf, 4);
if (!err_code) { if (!err_code) {
sleep_mils(100); sleep_ms(100);
err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len);
if (err_code > 0) { if (err_code > 0) {
return state == rn->int_state[rn->sel_int] ? 0 : -1; return state == rn->int_state[rn->sel_int] ? 0 : -1;
@ -1010,7 +1030,7 @@ int rnode_get_interface_state(struct RNode* rn) {
int rnode_set_normal_mode(struct RNode* rn) { int rnode_set_normal_mode(struct RNode* rn) {
uint8_t tx_buf[4]; uint8_t tx_buf[4];
memcpy(tx_buf, (uint8_t[4]){FEND, CMD_CONF_DELETE, 0x00, FEND}, 4*sizeof(uint8_t)); memcpy(tx_buf, (uint8_t[4]){FEND, CMD_CONF_DELETE, 0x00, FEND}, 4*sizeof(uint8_t));
return write_port(&rn->handle, tx_buf, 4); return write_port(rn->fd, tx_buf, 4);
} }
/* Enable the serial TNC mode of operation on an RNode /* Enable the serial TNC mode of operation on an RNode
@ -1023,7 +1043,7 @@ int rnode_set_tnc_mode(struct RNode* rn) {
int err_code = 0; int err_code = 0;
uint8_t tx_buf[4]; uint8_t tx_buf[4];
memcpy(tx_buf, (uint8_t[4]){FEND, CMD_CONF_SAVE, 0x00, FEND}, 4*sizeof(uint8_t)); memcpy(tx_buf, (uint8_t[4]){FEND, CMD_CONF_SAVE, 0x00, FEND}, 4*sizeof(uint8_t));
err_code = write_port(&rn->handle, tx_buf, 4); err_code = write_port(rn->fd, tx_buf, 4);
return err_code; return err_code;
} }
@ -1033,7 +1053,7 @@ int rnode_set_int_avoid(struct RNode* rn, bool avoid) {
int err_code = 0; int err_code = 0;
uint8_t tx_buf[4]; uint8_t tx_buf[4];
memcpy(tx_buf, (uint8_t[4]){FEND, CMD_DIS_IA, !avoid, FEND}, 4*sizeof(uint8_t)); memcpy(tx_buf, (uint8_t[4]){FEND, CMD_DIS_IA, !avoid, FEND}, 4*sizeof(uint8_t));
err_code = write_port(&rn->handle, tx_buf, 4); err_code = write_port(rn->fd, tx_buf, 4);
return err_code; return err_code;
} }
@ -1064,7 +1084,7 @@ int rnode_set_fw_hash(struct RNode* rn, uint8_t* hash) {
encode_slip(escaped_hash, &escaped_hash_size, hash, FW_HASH_SIZE, false); encode_slip(escaped_hash, &escaped_hash_size, hash, FW_HASH_SIZE, false);
memcpy(tx_buf + 2, escaped_hash, escaped_hash_size*sizeof(uint8_t)); memcpy(tx_buf + 2, escaped_hash, escaped_hash_size*sizeof(uint8_t));
tx_buf[2 + escaped_hash_size] = FEND; tx_buf[2 + escaped_hash_size] = FEND;
return write_port(&rn->handle, tx_buf, 2 + escaped_hash_size + 1); return write_port(rn->fd, tx_buf, 2 + escaped_hash_size + 1);
} }
/* Write to an arbitrary address in an RNode's EEPROM /* Write to an arbitrary address in an RNode's EEPROM
@ -1091,10 +1111,10 @@ int rnode_write_eeprom(struct RNode* rn, uint8_t address, uint8_t value) {
tx_buf[2 + escaped_data_size] = FEND; tx_buf[2 + escaped_data_size] = FEND;
err_code = write_port(&rn->handle, tx_buf, 2 + escaped_data_size + 1); err_code = write_port(rn->fd, tx_buf, 2 + escaped_data_size + 1);
if (!err_code) { if (!err_code) {
sleep_mils(6); sleep_ms(6);
} }
return err_code; return err_code;
} }
@ -1359,10 +1379,10 @@ int rnode_dump_eeprom(struct RNode* rn) {
uint16_t frame_len = 0; uint16_t frame_len = 0;
memcpy(tx_buf, (uint8_t[4]){FEND, CMD_ROM_READ, 0x00, FEND}, 4*sizeof(uint8_t)); memcpy(tx_buf, (uint8_t[4]){FEND, CMD_ROM_READ, 0x00, FEND}, 4*sizeof(uint8_t));
err_code = write_port(&rn->handle, tx_buf, 4); err_code = write_port(rn->fd, tx_buf, 4);
if (!err_code) { if (!err_code) {
sleep_mils(100); sleep_ms(100);
err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len); err_code = rnode_handle_resp(rn, &in_frame, &cmd, cmd_buf, &cmd_buf_l, &frame_len);
} }
@ -1471,28 +1491,28 @@ int rnode_wipe_eeprom(struct RNode* rn) {
log_trace("RNode firmware version too low!"); log_trace("RNode firmware version too low!");
return -2; return -2;
} else { } else {
err_code = write_port(&rn->handle, tx_buf, 4); err_code = write_port(rn->fd, tx_buf, 4);
} }
if (err_code) { if (err_code) {
return err_code; return err_code;
} }
err_code = close_port(&rn->handle); err_code = close_port(rn->fd);
if (!err_code) { if (!err_code) {
sleep_mils(13000); sleep_ms(13000);
if (rn->platform == PLATFORM_NRF52) { if (rn->platform == PLATFORM_NRF52) {
// Due to the current janky emulated EEPROM implementation for the // Due to the current janky emulated EEPROM implementation for the
// RAK4631, extra time must be given to allow for writing. // RAK4631, extra time must be given to allow for writing.
sleep_mils(10000); sleep_ms(10000);
} }
} else { } else {
return err_code; return err_code;
} }
open_port(&rn->handle, rn->port, rn->baud); rn->fd = open_port(rn->port, rn->baud);
return (err_code == 0) ? 0 : -1; return (err_code == 0) ? 0 : -1;
} }
@ -1516,12 +1536,12 @@ int rnode_flash(struct RNode* rn, char* zip_path, bool update, uint8_t* serial,
// show update logo on RNode display // show update logo on RNode display
uint8_t tx_buf[4]; uint8_t tx_buf[4];
memcpy(tx_buf, (uint8_t[4]){FEND, CMD_FW_UPD, 0x01, FEND}, 4*sizeof(uint8_t)); memcpy(tx_buf, (uint8_t[4]){FEND, CMD_FW_UPD, 0x01, FEND}, 4*sizeof(uint8_t));
err_code = write_port(&rn->handle, tx_buf, 4); err_code = write_port(rn->fd, tx_buf, 4);
} }
// Close port to prepare for flashing // Close port to prepare for flashing
if (!err_code) { if (!err_code) {
err_code = close_port(&rn->handle); err_code = close_port(rn->fd);
} }
if (!err_code) { if (!err_code) {
@ -1697,11 +1717,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 // Wait for reset and boot. This value may have to be adjusted in the
// future if firmware becomes more complex. // future if firmware becomes more complex.
sleep_mils(15000); sleep_ms(15000);
open_port(&rn->handle, rn->port, rn->baud); rn->fd = open_port(rn->port, rn->baud);
if (&rn->handle > 0) { if (rn->fd > 0) {
if (!update) { if (!update) {
uint8_t checksum[CHECKSUM_SIZE]; uint8_t checksum[CHECKSUM_SIZE];
void* signature; void* signature;
@ -1752,7 +1772,7 @@ int rnode_flash(struct RNode* rn, char* zip_path, bool update, uint8_t* serial,
} }
if (!err_code) { if (!err_code) {
sleep_mils(750); sleep_ms(750);
err_code = rnode_set_fw_hash(rn, hash); err_code = rnode_set_fw_hash(rn, hash);
} else { } else {
return err_code; return err_code;
@ -1761,18 +1781,18 @@ int rnode_flash(struct RNode* rn, char* zip_path, bool update, uint8_t* serial,
if (!err_code) { if (!err_code) {
log_info("Waiting 10 seconds for RNode to come online..."); log_info("Waiting 10 seconds for RNode to come online...");
sleep_mils(10000); sleep_ms(10000);
if (rn->platform == PLATFORM_NRF52) { if (rn->platform == PLATFORM_NRF52) {
err_code = close_port(&rn->handle); err_code = close_port(rn->fd);
// Due to the current janky emulated EEPROM implementation for the // Due to the current janky emulated EEPROM implementation for the
// RAK4631, extra time must be given to allow for writing. // RAK4631, extra time must be given to allow for writing.
log_info("Waiting an extra 15 seconds for RNode to come online..."); log_info("Waiting an extra 15 seconds for RNode to come online...");
sleep_mils(15000); sleep_ms(15000);
} else if (rn->platform == PLATFORM_ESP32) { } else if (rn->platform == PLATFORM_ESP32) {
rnode_reset(rn); rnode_reset(rn);
err_code = close_port(&rn->handle); err_code = close_port(rn->fd);
log_info("Waiting for ESP32 reset..."); log_info("Waiting for ESP32 reset...");
sleep_mils(7000); sleep_ms(7000);
} }
err_code = rnode_init(rn, rn->port, rn->baud, true, true); err_code = rnode_init(rn, rn->port, rn->baud, true, true);
} else { } else {
@ -1789,9 +1809,9 @@ int rnode_flash(struct RNode* rn, char* zip_path, bool update, uint8_t* serial,
int rnode_disconnect(struct RNode* rn) { int rnode_disconnect(struct RNode* rn) {
uint8_t tx_buf[4]; uint8_t tx_buf[4];
memcpy(tx_buf, (uint8_t[4]){FEND, CMD_LEAVE, 0xFF, FEND}, 4*sizeof(uint8_t)); memcpy(tx_buf, (uint8_t[4]){FEND, CMD_LEAVE, 0xFF, FEND}, 4*sizeof(uint8_t));
return write_port(&rn->handle, tx_buf, 4); return write_port(rn->fd, tx_buf, 4);
} }
int rnode_cleanup(struct RNode* rn) { int rnode_cleanup(struct RNode* rn) {
close_port(&rn->handle); close_port(rn->fd);
} }

View File

@ -15,26 +15,11 @@
#define LIBRNODE_H #define LIBRNODE_H
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <md5.h>
#include <openssl/sha.h>
#include <libgen.h>
#include <time.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <openssl/evp.h> #include <openssl/evp.h>
#include <openssl/rsa.h> #include <openssl/rsa.h>
#include "libs/framing.h"
#include "libs/serial.h"
#include "libs/logging/log.h"
#include "libs/util.h"
#include "libs/slip_enc.h"
#include "libs/eeprom.h"
#include "libs/zip.h"
#define MIN_FW_VER 1.66 #define MIN_FW_VER 1.66
// PLATFORMS // PLATFORMS
@ -68,7 +53,7 @@
struct RNode { struct RNode {
char* port; char* port;
struct serial handle; int fd;
uint32_t baud; uint32_t baud;
uint8_t platform; uint8_t platform;
@ -534,9 +519,5 @@ int rnode_flash(struct RNode* rn, char* zip_path, bool update, uint8_t* serial,
int rnode_cleanup(struct RNode* rn); int rnode_cleanup(struct RNode* rn);
// flashing
#include "libs/flashers/nrf/dfu.h"
#include "libs/flashers/nrf/uart_drv.h"
#include "libs/flashers/esp32/esputil.h"
#endif #endif

View File

@ -1,16 +1,3 @@
/* Copyright (c) 2025 - Jacob Eva (Liberated Embedded Systems)
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef EEPROM_H #ifndef EEPROM_H
#define EEPROM_H #define EEPROM_H
#define ADDR_PRODUCT 0x00 #define ADDR_PRODUCT 0x00

View File

@ -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 // 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) { int dfu_serial_wait_for_erase(uint32_t total_img_size) {
return sleep_mils(MAX(0.5, (total_img_size / FLASH_PAGE_SIZE) * FLASH_PAGE_ERASE_TIME)); return sleep_ms(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) { 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 // nrf52 is erasing and writing to flash
if (i % 8 == 0) { if (i % 8 == 0) {
err_code = sleep_mils(FLASH_PAGE_WRITE_TIME); err_code = sleep_ms(FLASH_PAGE_WRITE_TIME);
if (err_code) { if (err_code) {
log_error("Timing failed during flash, please try flashing again!"); log_error("Timing failed during flash, please try flashing again!");
break; break;
@ -339,7 +339,7 @@ int dfu_serial_send_firmware(uart_drv_t *p_uart, struct RNode* rn, const uint8_t
} }
if (!err_code) { if (!err_code) {
err_code = sleep_mils(FLASH_PAGE_WRITE_TIME); err_code = sleep_ms(FLASH_PAGE_WRITE_TIME);
if (err_code) { if (err_code) {
log_error("Timing failed during flash, please try flashing again!"); log_error("Timing failed during flash, please try flashing again!");

View File

@ -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 // Opening and closing serial port once is necessary to enter DFU mode
if (touch) { if (touch) {
sleep_mils(SERIAL_WAIT_TIME); sleep_ms(SERIAL_WAIT_TIME);
close(fd); close(fd);
// Wait, then open it again // Wait, then open it again
sleep_mils(TOUCH_RESET_WAIT_TIME); sleep_ms(TOUCH_RESET_WAIT_TIME);
fd = open(tty_path, O_RDWR | O_NOCTTY); fd = open(tty_path, O_RDWR | O_NOCTTY);

View File

@ -11,33 +11,16 @@
* with this program. If not, see <https://www.gnu.org/licenses/>. * with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef SERIAL_H #ifndef SERIAL_H
#include <stdint.h>
#define SERIAL_H #define SERIAL_H
#ifdef linux int open_port(char* path, uint32_t baud);
struct serial {
int fd;
};
#endif
#ifdef _WIN32 int write_port(int fd, uint8_t* data, uint16_t data_len);
#include <windows.h>
struct serial { int read_port(int fd, uint8_t* rx_data, uint16_t size);
HANDLE fd;
};
#endif
int open_port(struct serial* handle, char* path, uint32_t baud); int close_port(int fd);
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 #endif

View File

@ -11,15 +11,13 @@
* with this program. If not, see <https://www.gnu.org/licenses/>. * with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include "serial.h"
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <termios.h> #include <termios.h>
#include <stdint.h> #include <stdint.h>
#include "logging/log.h" #include "logging/log.h"
int open_port(struct serial* handle, char* path, uint32_t baud) { int open_port(char* path, uint32_t baud) {
struct termios options; struct termios options;
speed_t baud_s; speed_t baud_s;
@ -33,9 +31,9 @@ int open_port(struct serial* handle, char* path, uint32_t baud) {
break; break;
} }
handle->fd = open(path, O_RDWR | O_NOCTTY); int fd = open(path, O_RDWR | O_NOCTTY);
tcgetattr(handle->fd, &options); tcgetattr(fd, &options);
cfsetispeed(&options, baud_s); cfsetispeed(&options, baud_s);
cfsetospeed(&options, baud_s); cfsetospeed(&options, baud_s);
@ -59,17 +57,17 @@ int open_port(struct serial* handle, char* path, uint32_t baud) {
options.c_cc[VMIN] = 0; options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 5; // 0.5 seconds options.c_cc[VTIME] = 5; // 0.5 seconds
if (tcsetattr(handle->fd, TCSANOW, &options)) if (tcsetattr(fd, TCSANOW, &options))
{ {
log_error("Cannot set RNode port options!"); log_error("Cannot set RNode port options!");
return -1; return -1;
} else { } else {
return 0; return fd;
} }
} }
int write_port(struct serial* handle, uint8_t* data, uint16_t data_len) { int write_port(int fd, uint8_t* data, uint16_t data_len) {
unsigned char data_str[1024] = ""; unsigned char data_str[1024] = "";
for (int i = 0; i < data_len; i++) { for (int i = 0; i < data_len; i++) {
sprintf(data_str+(i*3), "%02x ", data[i]); sprintf(data_str+(i*3), "%02x ", data[i]);
@ -77,7 +75,7 @@ int write_port(struct serial* handle, uint8_t* data, uint16_t data_len) {
log_trace("Writing [%s] to RNode serial port.", data_str); log_trace("Writing [%s] to RNode serial port.", data_str);
if (write(handle->fd, data, data_len) != data_len) { if (write(fd, data, data_len) != data_len) {
log_error("Could not write all data to the RNode port! Is the RNode still connected?"); log_error("Could not write all data to the RNode port! Is the RNode still connected?");
return -1; return -1;
} else { } else {
@ -85,19 +83,14 @@ 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 read_port(int fd, uint8_t* rx_data, uint16_t size) {
int ret = read(handle->fd, rx_data, size); int ret = read(fd, rx_data, size);
if (ret == -1) { if (ret == -1) {
log_error("Could not read from RNode port! Is the RNode still connected?"); log_error("Could not read from RNode port! Is the RNode still connected?");
} }
return ret; return ret;
} }
int close_port(struct serial* handle) { int close_port(int fd) {
if (!close(handle->fd)) { return close(fd);
log_error("Error closing RNode port! Is the RNode still connected?");
return -1;
} else {
return 0;
}
} }

View File

@ -10,86 +10,3 @@
* You should have received a copy of the GNU General Public License along * You should have received a copy of the GNU General Public License along
* with this program. If not, see <https://www.gnu.org/licenses/>. * with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#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;
}

View File

@ -13,7 +13,7 @@
#include "util.h" #include "util.h"
int sleep_mils(int ms) int sleep_ms(int ms)
{ {
#ifdef WIN32 #ifdef WIN32
Sleep(ms); Sleep(ms);

View File

@ -17,6 +17,6 @@
#include <unistd.h> #include <unistd.h>
#include <time.h> #include <time.h>
int sleep_mils(int ms); int sleep_ms(int ms);
#endif #endif