temp work on compression

This commit is contained in:
Jm Casler 2022-04-13 19:23:35 -07:00
parent b056081d3c
commit ecc114f1cd
5 changed files with 80 additions and 32 deletions

View File

@ -125,6 +125,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define RTC_DATA_ATTR #define RTC_DATA_ATTR
#endif #endif
// -----------------------------------------------------------------------------
// Feature toggles
// -----------------------------------------------------------------------------
//#define DISABLE_NTP
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// OLED & Input // OLED & Input
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------

View File

@ -303,6 +303,14 @@ bool perhapsDecode(MeshPacket *p)
// parsing was successful // parsing was successful
p->which_payloadVariant = MeshPacket_decoded_tag; // change type to decoded p->which_payloadVariant = MeshPacket_decoded_tag; // change type to decoded
p->channel = chIndex; // change to store the index instead of the hash p->channel = chIndex; // change to store the index instead of the hash
// Decompress if needed. jm
if (p->decoded.which_payloadVariant == Data_payload_compressed_tag) {
// Decompress the file
}
printPacket("decoded message", p); printPacket("decoded message", p);
return true; return true;
} }
@ -321,35 +329,53 @@ Routing_Error perhapsEncode(MeshPacket *p)
if (p->which_payloadVariant == MeshPacket_decoded_tag) { if (p->which_payloadVariant == MeshPacket_decoded_tag) {
static uint8_t bytes[MAX_RHPACKETLEN]; // we have to use a scratch buffer because a union static uint8_t bytes[MAX_RHPACKETLEN]; // we have to use a scratch buffer because a union
// printPacket("pre encrypt", p); // portnum valid here
size_t numbytes = pb_encode_to_bytes(bytes, sizeof(bytes), Data_fields, &p->decoded); size_t numbytes = pb_encode_to_bytes(bytes, sizeof(bytes), Data_fields, &p->decoded);
// Only allow encryption on the text message app.
// TODO: Allow modules to opt into compression.
if (p->decoded.portnum == PortNum_TEXT_MESSAGE_APP) { if (p->decoded.portnum == PortNum_TEXT_MESSAGE_APP) {
char original_payload[Constants_DATA_PAYLOAD_LEN]; char original_payload[Constants_DATA_PAYLOAD_LEN];
memcpy(original_payload, p->decoded.payload.bytes, p->decoded.payload.size); memcpy(original_payload, p->decoded.payload.bytes, p->decoded.payload.size);
int compressed_len; char compressed_out[Constants_DATA_PAYLOAD_LEN] = {0};
char compressed_out[100] = {0};
compressed_len = unishox2_compress_simple(original_payload, p->decoded.payload.size, compressed_out); int compressed_len = unishox2_compress_simple(original_payload, p->decoded.payload.size, compressed_out);
Serial.print("Original length - "); Serial.print("Original length - ");
Serial.println(p->decoded.payload.size); Serial.println(p->decoded.payload.size);
Serial.print("Compressed length - "); Serial.print("Compressed length - ");
Serial.println(compressed_len); Serial.println(compressed_len);
//Serial.println(compressed_out); // Serial.println(compressed_out);
char decompressed_out[100] = {}; // If the compressed length is greater than or equal to the original size, don't use the compressed form
int decompressed_len; if (compressed_len >= p->decoded.payload.size) {
decompressed_len = unishox2_decompress_simple(compressed_out, compressed_len, decompressed_out); DEBUG_MSG("Not compressing message. Not enough benefit from doing so.\n");
// Set the uncompressed payload varient anyway. Shouldn't hurt?
p->decoded.which_payloadVariant = Data_payload_tag;
Serial.print("Decompressed length - "); // Otherwise we use the compressor
Serial.println(decompressed_len); } else {
Serial.println(decompressed_out); DEBUG_MSG("Compressing message.\n");
// Copy the compressed data into the meshpacket
p->decoded.payload_compressed.size = compressed_len;
memcpy(p->decoded.payload_compressed.bytes, compressed_out, compressed_len);
p->decoded.which_payloadVariant = Data_payload_compressed_tag;
}
if (0) {
char decompressed_out[Constants_DATA_PAYLOAD_LEN] = {};
int decompressed_len;
decompressed_len = unishox2_decompress_simple(compressed_out, compressed_len, decompressed_out);
Serial.print("Decompressed length - ");
Serial.println(decompressed_len);
Serial.println(decompressed_out);
}
} }
if (numbytes > MAX_RHPACKETLEN) if (numbytes > MAX_RHPACKETLEN)

View File

@ -500,6 +500,7 @@ typedef struct _User {
} User; } User;
typedef PB_BYTES_ARRAY_T(237) Data_payload_t; typedef PB_BYTES_ARRAY_T(237) Data_payload_t;
typedef PB_BYTES_ARRAY_T(237) Data_payload_compressed_t;
/* (Formerly called SubPacket) /* (Formerly called SubPacket)
The payload portion fo a packet, this is the actual bytes that are sent The payload portion fo a packet, this is the actual bytes that are sent
inside a radio packet (because from/to are broken out by the comms library) */ inside a radio packet (because from/to are broken out by the comms library) */
@ -507,30 +508,34 @@ typedef struct _Data {
/* Formerly named typ and of type Type */ /* Formerly named typ and of type Type */
PortNum portnum; PortNum portnum;
/* TODO: REPLACE */ /* TODO: REPLACE */
Data_payload_t payload; pb_size_t which_payloadVariant;
union {
Data_payload_t payload;
Data_payload_compressed_t payload_compressed;
};
/* TODO: REPLACE */
bool want_response;
/* Not normally used, but for testing a sender can request that recipient /* Not normally used, but for testing a sender can request that recipient
responds in kind (i.e. if it received a position, it should unicast back it's position). responds in kind (i.e. if it received a position, it should unicast back it's position).
Note: that if you set this on a broadcast you will receive many replies. */ Note: that if you set this on a broadcast you will receive many replies. */
bool want_response; uint32_t dest;
/* The address of the destination node. /* The address of the destination node.
This field is is filled in by the mesh radio device software, application This field is is filled in by the mesh radio device software, application
layer software should never need it. layer software should never need it.
RouteDiscovery messages _must_ populate this. RouteDiscovery messages _must_ populate this.
Other message types might need to if they are doing multihop routing. */ Other message types might need to if they are doing multihop routing. */
uint32_t dest; uint32_t source;
/* The address of the original sender for this message. /* The address of the original sender for this message.
This field should _only_ be populated for reliable multihop packets (to keep This field should _only_ be populated for reliable multihop packets (to keep
packets small). */ packets small). */
uint32_t source; uint32_t request_id;
/* Only used in routing or response messages. /* Only used in routing or response messages.
Indicates the original message ID that this message is reporting failure on. (formerly called original_id) */ Indicates the original message ID that this message is reporting failure on. (formerly called original_id) */
uint32_t request_id;
/* If set, this message is intened to be a reply to a previously sent message with the defined id. */
uint32_t reply_id; uint32_t reply_id;
/* If set, this message is intened to be a reply to a previously sent message with the defined id. */
uint32_t emoji;
/* Defaults to false. If true, then what is in the payload should be treated as an emoji like giving /* Defaults to false. If true, then what is in the payload should be treated as an emoji like giving
a message a heart or poop emoji. */ a message a heart or poop emoji. */
uint32_t emoji;
/* Location structure */
bool has_location; bool has_location;
Location location; Location location;
} Data; } Data;
@ -738,7 +743,7 @@ extern "C" {
#define User_init_default {"", "", "", {0}, _HardwareModel_MIN, 0, _Team_MIN, 0, 0, 0} #define User_init_default {"", "", "", {0}, _HardwareModel_MIN, 0, _Team_MIN, 0, 0, 0}
#define RouteDiscovery_init_default {0, {0, 0, 0, 0, 0, 0, 0, 0}} #define RouteDiscovery_init_default {0, {0, 0, 0, 0, 0, 0, 0, 0}}
#define Routing_init_default {0, {RouteDiscovery_init_default}} #define Routing_init_default {0, {RouteDiscovery_init_default}}
#define Data_init_default {_PortNum_MIN, {0, {0}}, 0, 0, 0, 0, 0, 0, false, Location_init_default} #define Data_init_default {_PortNum_MIN, 0, {{0, {0}}}, 0, 0, 0, 0, 0, 0, false, Location_init_default}
#define Location_init_default {0, 0, 0, 0, 0} #define Location_init_default {0, 0, 0, 0, 0}
#define MeshPacket_init_default {0, 0, 0, 0, {Data_init_default}, 0, 0, 0, 0, 0, _MeshPacket_Priority_MIN, 0, _MeshPacket_Delayed_MIN} #define MeshPacket_init_default {0, 0, 0, 0, {Data_init_default}, 0, 0, 0, 0, 0, _MeshPacket_Priority_MIN, 0, _MeshPacket_Delayed_MIN}
#define NodeInfo_init_default {0, false, User_init_default, false, Position_init_default, 0, 0, false, DeviceMetrics_init_default} #define NodeInfo_init_default {0, false, User_init_default, false, Position_init_default, 0, 0, false, DeviceMetrics_init_default}
@ -751,7 +756,7 @@ extern "C" {
#define User_init_zero {"", "", "", {0}, _HardwareModel_MIN, 0, _Team_MIN, 0, 0, 0} #define User_init_zero {"", "", "", {0}, _HardwareModel_MIN, 0, _Team_MIN, 0, 0, 0}
#define RouteDiscovery_init_zero {0, {0, 0, 0, 0, 0, 0, 0, 0}} #define RouteDiscovery_init_zero {0, {0, 0, 0, 0, 0, 0, 0, 0}}
#define Routing_init_zero {0, {RouteDiscovery_init_zero}} #define Routing_init_zero {0, {RouteDiscovery_init_zero}}
#define Data_init_zero {_PortNum_MIN, {0, {0}}, 0, 0, 0, 0, 0, 0, false, Location_init_zero} #define Data_init_zero {_PortNum_MIN, 0, {{0, {0}}}, 0, 0, 0, 0, 0, 0, false, Location_init_zero}
#define Location_init_zero {0, 0, 0, 0, 0} #define Location_init_zero {0, 0, 0, 0, 0}
#define MeshPacket_init_zero {0, 0, 0, 0, {Data_init_zero}, 0, 0, 0, 0, 0, _MeshPacket_Priority_MIN, 0, _MeshPacket_Delayed_MIN} #define MeshPacket_init_zero {0, 0, 0, 0, {Data_init_zero}, 0, 0, 0, 0, 0, _MeshPacket_Priority_MIN, 0, _MeshPacket_Delayed_MIN}
#define NodeInfo_init_zero {0, false, User_init_zero, false, Position_init_zero, 0, 0, false, DeviceMetrics_init_zero} #define NodeInfo_init_zero {0, false, User_init_zero, false, Position_init_zero, 0, 0, false, DeviceMetrics_init_zero}
@ -825,6 +830,7 @@ extern "C" {
#define User_ant_azimuth_tag 12 #define User_ant_azimuth_tag 12
#define Data_portnum_tag 1 #define Data_portnum_tag 1
#define Data_payload_tag 2 #define Data_payload_tag 2
#define Data_payload_compressed_tag 10
#define Data_want_response_tag 3 #define Data_want_response_tag 3
#define Data_dest_tag 4 #define Data_dest_tag 4
#define Data_source_tag 5 #define Data_source_tag 5
@ -923,14 +929,15 @@ X(a, STATIC, ONEOF, UENUM, (variant,error_reason,error_reason), 3)
#define Data_FIELDLIST(X, a) \ #define Data_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, UENUM, portnum, 1) \ X(a, STATIC, SINGULAR, UENUM, portnum, 1) \
X(a, STATIC, SINGULAR, BYTES, payload, 2) \ X(a, STATIC, ONEOF, BYTES, (payloadVariant,payload,payload), 2) \
X(a, STATIC, SINGULAR, BOOL, want_response, 3) \ X(a, STATIC, SINGULAR, BOOL, want_response, 3) \
X(a, STATIC, SINGULAR, FIXED32, dest, 4) \ X(a, STATIC, SINGULAR, FIXED32, dest, 4) \
X(a, STATIC, SINGULAR, FIXED32, source, 5) \ X(a, STATIC, SINGULAR, FIXED32, source, 5) \
X(a, STATIC, SINGULAR, FIXED32, request_id, 6) \ X(a, STATIC, SINGULAR, FIXED32, request_id, 6) \
X(a, STATIC, SINGULAR, FIXED32, reply_id, 7) \ X(a, STATIC, SINGULAR, FIXED32, reply_id, 7) \
X(a, STATIC, SINGULAR, FIXED32, emoji, 8) \ X(a, STATIC, SINGULAR, FIXED32, emoji, 8) \
X(a, STATIC, OPTIONAL, MESSAGE, location, 9) X(a, STATIC, OPTIONAL, MESSAGE, location, 9) \
X(a, STATIC, ONEOF, BYTES, (payloadVariant,payload_compressed,payload_compressed), 10)
#define Data_CALLBACK NULL #define Data_CALLBACK NULL
#define Data_DEFAULT NULL #define Data_DEFAULT NULL
#define Data_location_MSGTYPE Location #define Data_location_MSGTYPE Location

View File

@ -40,10 +40,10 @@ typedef struct _EnvironmentMetrics {
/* Types of Measurements the telemetry module is equipped to handle */ /* Types of Measurements the telemetry module is equipped to handle */
typedef struct _Telemetry { typedef struct _Telemetry {
/* This is usually not sent over the mesh (to save space), but it is sent /* This is usually not sent over the mesh (to save space), but it is sent
from the phone so that the local device can set its RTC If it is sent over from the phone so that the local device can set its RTC If it is sent over
the mesh (because there are devices on the mesh without GPS), it will only the mesh (because there are devices on the mesh without GPS), it will only
be sent by devices which has a hardware GPS clock (IE Mobile Phone). be sent by devices which has a hardware GPS clock (IE Mobile Phone).
seconds since 1970 */ seconds since 1970 */
uint32_t time; uint32_t time;
/* Key native device metrics such as battery level */ /* Key native device metrics such as battery level */

View File

@ -10,10 +10,13 @@
#include "target_specific.h" #include "target_specific.h"
#include <DNSServer.h> #include <DNSServer.h>
#include <ESPmDNS.h> #include <ESPmDNS.h>
#include <NTPClient.h>
#include <WiFi.h> #include <WiFi.h>
#include <WiFiUdp.h> #include <WiFiUdp.h>
#ifndef DISABLE_NTP
#include <NTPClient.h>
#endif
using namespace concurrency; using namespace concurrency;
static void WiFiEvent(WiFiEvent_t event); static void WiFiEvent(WiFiEvent_t event);
@ -23,7 +26,10 @@ DNSServer dnsServer;
// NTP // NTP
WiFiUDP ntpUDP; WiFiUDP ntpUDP;
#ifndef DISABLE_NTP
NTPClient timeClient(ntpUDP, "0.pool.ntp.org"); NTPClient timeClient(ntpUDP, "0.pool.ntp.org");
#endif
uint8_t wifiDisconnectReason = 0; uint8_t wifiDisconnectReason = 0;
@ -67,13 +73,13 @@ static int32_t reconnectWiFi()
DEBUG_MSG("... Reconnecting to WiFi access point\n"); DEBUG_MSG("... Reconnecting to WiFi access point\n");
WiFi.mode(WIFI_MODE_STA); WiFi.mode(WIFI_MODE_STA);
WiFi.begin(wifiName, wifiPsw); WiFi.begin(wifiName, wifiPsw);
// Starting timeClient; // Starting timeClient;
} }
} }
//if (*wifiName) { #ifndef DISABLE_NTP
// if (*wifiName) {
if (WiFi.isConnected()) { if (WiFi.isConnected()) {
DEBUG_MSG("Updating NTP time\n"); DEBUG_MSG("Updating NTP time\n");
if (timeClient.update()) { if (timeClient.update()) {
@ -89,6 +95,7 @@ static int32_t reconnectWiFi()
DEBUG_MSG("NTP Update failed\n"); DEBUG_MSG("NTP Update failed\n");
} }
} }
#endif
return 30 * 1000; // every 30 seconds return 30 * 1000; // every 30 seconds
} }
@ -155,9 +162,11 @@ static void onNetworkConnected()
MDNS.addService("https", "tcp", 443); MDNS.addService("https", "tcp", 443);
} }
#ifndef DISABLE_NTP
DEBUG_MSG("Starting NTP time client\n"); DEBUG_MSG("Starting NTP time client\n");
timeClient.begin(); timeClient.begin();
timeClient.setUpdateInterval(60*60); // Update once an hour timeClient.setUpdateInterval(60 * 60); // Update once an hour
#endif
initWebServer(); initWebServer();
initApiServer(); initApiServer();