|
|
@ -2,6 +2,7 @@ |
|
|
|
#include "ugv_config.h" |
|
|
|
#include "ugv_config.h" |
|
|
|
|
|
|
|
|
|
|
|
#include <esp_log.h> |
|
|
|
#include <esp_log.h> |
|
|
|
|
|
|
|
#include <mbedtls/base64.h> |
|
|
|
|
|
|
|
|
|
|
|
#include "messages.pb.h" |
|
|
|
#include "messages.pb.h" |
|
|
|
|
|
|
|
|
|
|
@ -70,7 +71,6 @@ void CommsClass::Init() { |
|
|
|
ESP_LOGE(TAG, "E32 LoRa Init failed: %s (%d)", error_name, ret); |
|
|
|
ESP_LOGE(TAG, "E32 LoRa Init failed: %s (%d)", error_name, ret); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
packet_len = -1; |
|
|
|
|
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
xTaskCreate(CommsClass::CommsTask, "ugv_comms", 8 * 1024, this, 2, |
|
|
|
xTaskCreate(CommsClass::CommsTask, "ugv_comms", 8 * 1024, this, 2, |
|
|
@ -89,7 +89,7 @@ int32_t CommsClass::ReadRssi() { |
|
|
|
#ifdef COMMS_SX127X |
|
|
|
#ifdef COMMS_SX127X |
|
|
|
sx127x_read_rssi(lora, &rssi); |
|
|
|
sx127x_read_rssi(lora, &rssi); |
|
|
|
#else |
|
|
|
#else |
|
|
|
rssi = 0; |
|
|
|
rssi = 0; |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
return rssi; |
|
|
|
return rssi; |
|
|
|
} |
|
|
|
} |
|
|
@ -99,7 +99,7 @@ uint8_t CommsClass::ReadLnaGain() { |
|
|
|
#ifdef COMMS_SX127X |
|
|
|
#ifdef COMMS_SX127X |
|
|
|
sx127x_read_lna_gain(lora, &lna_gain); |
|
|
|
sx127x_read_lna_gain(lora, &lna_gain); |
|
|
|
#else |
|
|
|
#else |
|
|
|
lna_gain = 0; |
|
|
|
lna_gain = 0; |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
return lna_gain; |
|
|
|
return lna_gain; |
|
|
|
} |
|
|
|
} |
|
|
@ -124,10 +124,8 @@ void CommsClass::RunTask() { |
|
|
|
#ifdef COMMS_SX127X |
|
|
|
#ifdef COMMS_SX127X |
|
|
|
sx127x_rx_packet_t rx_packet; |
|
|
|
sx127x_rx_packet_t rx_packet; |
|
|
|
#else |
|
|
|
#else |
|
|
|
static const int rx_buf_len = MAX_PACKET_LEN * 2; |
|
|
|
std::string rx_buf; |
|
|
|
|
|
|
|
int rx_len; |
|
|
|
uint8_t *rx_buf = new uint8_t[rx_buf_len]; |
|
|
|
|
|
|
|
int rx_len; |
|
|
|
|
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
UGV_Message ugv_message; |
|
|
|
UGV_Message ugv_message; |
|
|
@ -145,27 +143,14 @@ void CommsClass::RunTask() { |
|
|
|
sx127x_packet_rx_free(&rx_packet); |
|
|
|
sx127x_packet_rx_free(&rx_packet); |
|
|
|
} |
|
|
|
} |
|
|
|
#else |
|
|
|
#else |
|
|
|
if (packet_len <= 0) { // need to receive packet size first
|
|
|
|
// receiving packet data now
|
|
|
|
rx_len = lora.Read(rx_buf, 1, delay_ticks); |
|
|
|
rx_len = lora.ReadLn(rx_buf, PACKET_RX_TIMEOUT); |
|
|
|
if (rx_len > 0) { |
|
|
|
if (rx_len <= 0) { |
|
|
|
packet_len = rx_buf[0]; |
|
|
|
ESP_LOGE(TAG, "timeout for packet rx"); |
|
|
|
if (packet_len > MAX_PACKET_LEN) { // should not be possible
|
|
|
|
lora.Flush(); |
|
|
|
ESP_LOGE(TAG, "invalid packet len received: %d", packet_len); |
|
|
|
} else { |
|
|
|
packet_len = -1; |
|
|
|
ESP_LOGV(TAG, "rx data: %.*s", rx_buf.size(), rx_buf.c_str()); |
|
|
|
} else { |
|
|
|
HandlePacket((uint8_t *)rx_buf.c_str(), rx_buf.size()); |
|
|
|
ESP_LOGV(TAG, "rx packet len: %d", packet_len); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { // receiving packet data now
|
|
|
|
|
|
|
|
rx_len = lora.Read(rx_buf, packet_len, PACKET_RX_TIMEOUT); |
|
|
|
|
|
|
|
if (rx_len < packet_len) { |
|
|
|
|
|
|
|
ESP_LOGE(TAG, "timeout for packet rx"); |
|
|
|
|
|
|
|
lora.Flush(); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
ESP_LOGV(TAG, "rx data: %.*s", packet_len, rx_buf); |
|
|
|
|
|
|
|
HandlePacket(rx_buf, packet_len); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
packet_len = -1; // wait for new packet len
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
// TODO: checksum?
|
|
|
|
// TODO: checksum?
|
|
|
|
#endif |
|
|
|
#endif |
|
|
@ -192,9 +177,6 @@ void CommsClass::RunTask() { |
|
|
|
current_tick = xTaskGetTickCount(); |
|
|
|
current_tick = xTaskGetTickCount(); |
|
|
|
next_send = current_tick + send_period; |
|
|
|
next_send = current_tick + send_period; |
|
|
|
} |
|
|
|
} |
|
|
|
#ifndef COMMS_SX127X |
|
|
|
|
|
|
|
delete[] rx_buf; |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#ifdef COMMS_SX127X |
|
|
|
#ifdef COMMS_SX127X |
|
|
@ -219,9 +201,23 @@ void CommsClass::HandlePacket(const uint8_t *data, size_t data_size) { |
|
|
|
last_packet_tick = xTaskGetTickCount(); |
|
|
|
last_packet_tick = xTaskGetTickCount(); |
|
|
|
xSemaphoreGive(mutex); |
|
|
|
xSemaphoreGive(mutex); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int ret; |
|
|
|
|
|
|
|
size_t decoded_size = (data_size * 4 + 2) / 3; |
|
|
|
|
|
|
|
uint8_t *decoded = new uint8_t[decoded_size]; |
|
|
|
|
|
|
|
size_t decoded_len; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ret = mbedtls_base64_decode(decoded, decoded_size, &decoded_len, data, |
|
|
|
|
|
|
|
data_size); |
|
|
|
|
|
|
|
if (ret != 0) { |
|
|
|
|
|
|
|
ESP_LOGE(TAG, "error with base64 decode: %d", ret); |
|
|
|
|
|
|
|
delete[] decoded; |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
GroundMessage ground_message; |
|
|
|
GroundMessage ground_message; |
|
|
|
|
|
|
|
|
|
|
|
bool pb_ret = ground_message.ParseFromArray(data, data_size); |
|
|
|
bool pb_ret = ground_message.ParseFromArray(decoded, decoded_len); |
|
|
|
|
|
|
|
delete[] decoded; |
|
|
|
if (!pb_ret) { |
|
|
|
if (!pb_ret) { |
|
|
|
ESP_LOGE(TAG, "rx invalid protobuf"); |
|
|
|
ESP_LOGE(TAG, "rx invalid protobuf"); |
|
|
|
return; |
|
|
|
return; |
|
|
@ -261,7 +257,7 @@ void CommsClass::HandleCommand(const messages::GroundCommand &command) { |
|
|
|
SendPacket(ugv_message); |
|
|
|
SendPacket(ugv_message); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
esp_err_t CommsClass::SendPacket(const char *data, size_t size) { |
|
|
|
esp_err_t CommsClass::SendPacket(const uint8_t *data, size_t size) { |
|
|
|
#ifdef COMMS_SX127X |
|
|
|
#ifdef COMMS_SX127X |
|
|
|
return sx127x_send_packet(lora, data, size, 0); |
|
|
|
return sx127x_send_packet(lora, data, size, 0); |
|
|
|
#else |
|
|
|
#else |
|
|
@ -269,15 +265,25 @@ esp_err_t CommsClass::SendPacket(const char *data, size_t size) { |
|
|
|
ESP_LOGE(TAG, "SendPacket size too long: %d", size); |
|
|
|
ESP_LOGE(TAG, "SendPacket size too long: %d", size); |
|
|
|
return ESP_ERR_INVALID_SIZE; |
|
|
|
return ESP_ERR_INVALID_SIZE; |
|
|
|
} |
|
|
|
} |
|
|
|
uint8_t sz = size; |
|
|
|
size_t encoded_size = ((size + 2) / 3) * 4; |
|
|
|
lora.Write(&sz, sizeof(sz)); |
|
|
|
uint8_t *encoded = new uint8_t[encoded_size]; |
|
|
|
lora.Write((uint8_t *)data, size); |
|
|
|
size_t encoded_len; |
|
|
|
|
|
|
|
int ret = |
|
|
|
|
|
|
|
mbedtls_base64_encode(encoded, encoded_size, &encoded_len, data, size); |
|
|
|
|
|
|
|
if (ret != 0) { |
|
|
|
|
|
|
|
delete[] encoded; |
|
|
|
|
|
|
|
ESP_LOGE(TAG, "base64 encode error: %d", ret); |
|
|
|
|
|
|
|
return ESP_FAIL; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// TODO: checksum
|
|
|
|
|
|
|
|
lora.WriteLn(encoded, encoded_len); |
|
|
|
|
|
|
|
delete[] encoded; |
|
|
|
return ESP_OK; |
|
|
|
return ESP_OK; |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
esp_err_t CommsClass::SendPacket(const std::string &str) { |
|
|
|
esp_err_t CommsClass::SendPacket(const std::string &str) { |
|
|
|
return SendPacket(str.c_str(), str.size()); |
|
|
|
return SendPacket((const uint8_t *)str.c_str(), str.size()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
esp_err_t CommsClass::SendPacket(const google::protobuf::MessageLite &message) { |
|
|
|
esp_err_t CommsClass::SendPacket(const google::protobuf::MessageLite &message) { |
|
|
|