aboutsummaryrefslogtreecommitdiffstats
path: root/tracker-stm32/src/compression.cpp
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2023-05-19 15:06:43 +0200
committerMatthias P. Braendli <matthias.braendli@mpb.li>2023-05-19 15:06:43 +0200
commit94655a6999731f3d76ce21b7ad76568b9657e6ea (patch)
tree13c3b1ca69f3aac7563c241e0bd4e7385564e89d /tracker-stm32/src/compression.cpp
parent41b68b70309ed40565204d2d8985f708151bb9d6 (diff)
downloadlora-aprs-hb9egm-94655a6999731f3d76ce21b7ad76568b9657e6ea.tar.gz
lora-aprs-hb9egm-94655a6999731f3d76ce21b7ad76568b9657e6ea.tar.bz2
lora-aprs-hb9egm-94655a6999731f3d76ce21b7ad76568b9657e6ea.zip
Add text message
Diffstat (limited to 'tracker-stm32/src/compression.cpp')
-rw-r--r--tracker-stm32/src/compression.cpp97
1 files changed, 97 insertions, 0 deletions
diff --git a/tracker-stm32/src/compression.cpp b/tracker-stm32/src/compression.cpp
new file mode 100644
index 0000000..9b108fd
--- /dev/null
+++ b/tracker-stm32/src/compression.cpp
@@ -0,0 +1,97 @@
+#include "compression.h"
+
+using math::wide_integer::uint512_t;
+
+uint512_t decodeBase(int b, const char *str);
+
+const char* digits = " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-./?@";
+
+// This function is from aprs434.github.io,
+// MIT License, Copyright (c) 2022 Serge Y. Stroobandt, ON4AA
+uint32_t encodeCallsign(const char *callsign) {
+ // Encode CCCC according to aprs434.github.io/code/codec.cpp
+ const int b = 37;
+
+ uint32_t result;
+ uint32_t weight;
+ int i, j, k, ch;
+
+ result = 0, weight = 1;
+ for (i = j = strlen(callsign); i > 0; i--) { // iterate over input string
+ ch = toupper(callsign[i-1]);
+ for (k = 0; k < strlen(digits); k++) { // lookup char index
+ if (digits[k] == ch) {
+ break;
+ }
+ }
+ if (k == strlen(digits)) // ignore if ch not found in digits
+ continue;
+ result = result + ((k) * weight);
+ weight = weight * b;
+ }
+
+ return result; // result can be pow(42,51)!
+}
+
+// This function is from ESP32 lora.tracker,
+// MIT License, Copyright (c) 2020 Peter Buchegger
+void ax25_base91enc(char *s, uint8_t n, uint32_t v) {
+ /* Creates a Base-91 representation of the value in v in the string
+ * pointed to by s, n-characters long. String length should be n+1.
+ */
+
+ for (s += n, *s = '\0'; n; n--) {
+ *(--s) = v % 91 + 33;
+ v /= 91;
+ }
+}
+
+static void strrev(char s[])
+{
+ int length = strlen(s) ;
+ int c, i, j;
+
+ for (i = 0, j = length - 1; i < j; i++, j--)
+ {
+ c = s[i];
+ s[i] = s[j];
+ s[j] = c;
+ }
+}
+
+inline void assert(bool cond) {
+ while (!cond) {}
+}
+
+static uint8_t bytes[100];
+
+uint512_t decodeBase(int b, const char* str) { // base b, str 64 bytes max '\0' terminated
+
+ uint512_t result;
+ uint512_t weight;
+ int i, j, k, ch;
+
+ assert(b <= strlen(digits));
+ assert(b > 1);
+
+ result = 0, weight = 1;
+ for (i = j = strlen(str); i > 0; i--) // iterate over input string
+ {
+ ch=toupper(str[i-1]);
+ for (k = 0; k < strlen(digits); k++) { //lookup char index
+ if (digits[k] == ch) {
+ break;
+ }
+ }
+ if (k == strlen(digits)) // ignore if ch not found in digits
+ continue;
+ result = result + ((k) * weight);
+ weight = weight * b;
+ }
+
+ return result; // result can be pow(42,51)!
+}
+
+uint512_t encodetttt(const char *s) {
+ return decodeBase(42, s);
+}