Add ham64 conversion utility
All checks were successful
/ build-hamnet70 (push) Successful in 33s
/ build-doc (push) Successful in 17s
/ deploy-doc (push) Has been skipped

This commit is contained in:
Thomas Kolb 2025-01-04 17:19:16 +01:00
parent 176770eef5
commit 6483baa74c
3 changed files with 73 additions and 0 deletions

View file

@ -79,3 +79,4 @@ target_link_libraries(
)
add_subdirectory(test)
add_subdirectory(utils/ham64)

View file

@ -0,0 +1,13 @@
add_executable(
call2ham64
../../src/layer2/ham64.c
../../src/layer2/ham64.h
../../src/logger.c
../../src/logger.h
call2ham64.c
)
target_link_libraries(
call2ham64
m
)

View file

@ -0,0 +1,59 @@
#include <layer2/ham64.h>
#include <logger.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
int main(int argc, char **argv)
{
logger_init();
if(argc < 2) {
LOG(LVL_FATAL, "Not enough arguments!");
LOG(LVL_FATAL, "");
LOG(LVL_FATAL, "usage: %s <callsign>", argv[0]);
return EXIT_FAILURE;
}
const char *callsign = argv[1];
size_t len = strlen(callsign);
if(len > 12) {
LOG(LVL_ERR, "Call sign too long: %zu chars (allowed: 12)", len);
return EXIT_FAILURE;
}
ham64_t ham64;
size_t encoded_len = ham64_encode(callsign, &ham64);
if(encoded_len == 0) {
LOG(LVL_ERR, "Encoding the call sign failed.");
return EXIT_FAILURE;
}
LOG(LVL_INFO, "Call sign encoded successfully: %zu words.", encoded_len);
char formatted[HAM64_FMT_MAX_LEN];
ham64_format(&ham64, formatted);
printf("ham64: %s\n", formatted);
// calculate IPv6 address
struct in6_addr ipv6_addr;
memset(&ipv6_addr, 0, sizeof(struct in6_addr));
// fill the host part from the ham64 address. The bytes are filled in
// reverse order to make the „readable“ IPv6 address as short as possible.
for(uint8_t i = 0; i < ham64.length; i++) {
ipv6_addr.s6_addr[15 - 2*i] = (ham64.addr[i] >> 8) & 0xFF;
ipv6_addr.s6_addr[14 - 2*i] = (ham64.addr[i] >> 0) & 0xFF;
}
// print the address
char ipv6_str[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, &ipv6_addr, ipv6_str, sizeof(ipv6_str));
printf("ipv6: %s\n", ipv6_str);
}