diff --git a/impl/CMakeLists.txt b/impl/CMakeLists.txt index a819d87..ae1edbb 100644 --- a/impl/CMakeLists.txt +++ b/impl/CMakeLists.txt @@ -79,3 +79,4 @@ target_link_libraries( ) add_subdirectory(test) +add_subdirectory(utils/ham64) diff --git a/impl/utils/ham64/CMakeLists.txt b/impl/utils/ham64/CMakeLists.txt new file mode 100644 index 0000000..f6a6999 --- /dev/null +++ b/impl/utils/ham64/CMakeLists.txt @@ -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 +) diff --git a/impl/utils/ham64/call2ham64.c b/impl/utils/ham64/call2ham64.c new file mode 100644 index 0000000..f8b185f --- /dev/null +++ b/impl/utils/ham64/call2ham64.c @@ -0,0 +1,59 @@ +#include +#include + +#include +#include +#include + +#include + +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 ", 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); +}