Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore 'free text' encoding support #40

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions demo/decode_ft8.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#define _POSIX_C_SOURCE 199309L

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
Expand Down
13 changes: 10 additions & 3 deletions demo/gen_ft8.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ void usage()
printf("(Note that you might have to enclose your message in quote marks if it contains spaces)\n");
}

void packtext77(const char* text, uint8_t* b77);

int main(int argc, char** argv)
{
// Expect two command-line arguments
Expand All @@ -134,9 +136,14 @@ int main(int argc, char** argv)
ftx_message_rc_t rc = ftx_message_encode(&msg, NULL, message);
if (rc != FTX_MESSAGE_RC_OK)
{
printf("Cannot parse message!\n");
printf("RC = %d\n", (int)rc);
return -2;
// Try 'free text' encoding
if (strlen(message) <= 13)
packtext77(message, (uint8_t *)&msg.payload);
else {
printf("Cannot parse message!\n");
printf("RC = %d\n", (int)rc);
return -2;
}
}

printf("Packed data: ");
Expand Down
64 changes: 64 additions & 0 deletions ft8/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -992,3 +992,67 @@ static int unpackgrid(uint16_t igrid4, uint8_t ir, char* extra)

return 0;
}

void packtext77(const char* text, uint8_t* b77)
{
int length = strlen(text);

// Skip leading and trailing spaces
while (*text == ' ' && *text != 0)
{
++text;
--length;
}
while (length > 0 && text[length - 1] == ' ')
{
--length;
}

// Clear the first 72 bits representing a long number
for (int i = 0; i < 9; ++i)
{
b77[i] = 0;
}

// Now express the text as base-42 number stored
// in the first 72 bits of b77
for (int j = 0; j < 13; ++j)
{
// Multiply the long integer in b77 by 42
uint16_t x = 0;
for (int i = 8; i >= 0; --i)
{
x += b77[i] * (uint16_t)42;
b77[i] = (x & 0xFF);
x >>= 8;
}

// Get the index of the current char
if (j < length)
{
int q = nchar(text[j], FT8_CHAR_TABLE_FULL);
x = (q > 0) ? q : 0;
}
else
{
x = 0;
}
// Here we double each added number in order to have the result multiplied
// by two as well, so that it's a 71 bit number left-aligned in 72 bits (9 bytes)
x <<= 1;

// Now add the number to our long number
for (int i = 8; i >= 0; --i)
{
if (x == 0)
break;
x += b77[i];
b77[i] = (x & 0xFF);
x >>= 8;
}
}

// Set n3=0 (bits 71..73) and i3=0 (bits 74..76)
b77[8] &= 0xFE;
b77[9] &= 0x00;
}