+static void ax25_putchar(AX25Ctx *ctx, uint8_t c)
+{
+ if (c == HDLC_FLAG || c == HDLC_RESET
+ || c == AX25_ESC)
+ kfile_putc(AX25_ESC, ctx->ch);
+ ctx->crc_out = updcrc_ccitt(c, ctx->crc_out);
+ kfile_putc(c, ctx->ch);
+}
+
+static void ax25_sendCall(AX25Ctx *ctx, const AX25Call *addr, bool last)
+{
+ unsigned len = MIN(sizeof(addr->call), strlen(addr->call));
+
+ for (unsigned i = 0; i < len; i++)
+ {
+ uint8_t c = addr->call[i];
+ ASSERT(isalnum(c) || c == ' ');
+ c = toupper(c);
+ ax25_putchar(ctx, c << 1);
+ }
+
+ /* Fill with spaces the rest of the CALL if it's shorter */
+ if (len < sizeof(addr->call))
+ for (unsigned i = 0; i < sizeof(addr->call) - len; i++)
+ ax25_putchar(ctx, ' ' << 1);
+
+ /* The bit0 of last call SSID should be set to 1 */
+ uint8_t ssid = addr->ssid << 1 | (last ? 0x01 : 0);
+ ax25_putchar(ctx, ssid);
+}
+
+/**
+ * Send an AX25 frame on the channel through a specific path.
+ * \param ctx AX25 context to operate on.
+ * \param path An array of callsigns used as path, \see AX25_PATH for
+ * an handy way to create a path.
+ * \param path_len callsigns path lenght.
+ * \param _buf payload buffer.
+ * \param len length of the payload.
+ */
+void ax25_sendVia(AX25Ctx *ctx, const AX25Call *path, size_t path_len, const void *_buf, size_t len)
+{
+ const uint8_t *buf = (const uint8_t *)_buf;
+ ASSERT(path);
+ ASSERT(path_len >= 2);
+
+ ctx->crc_out = CRC_CCITT_INIT_VAL;
+ kfile_putc(HDLC_FLAG, ctx->ch);
+
+
+ /* Send call */
+ for (size_t i = 0; i < path_len; i++)
+ ax25_sendCall(ctx, &path[i], (i == path_len - 1));
+
+ ax25_putchar(ctx, AX25_CTRL_UI);
+ ax25_putchar(ctx, AX25_PID_NOLAYER3);
+
+ while (len--)
+ ax25_putchar(ctx, *buf++);
+
+ /*
+ * According to AX25 protocol,
+ * CRC is sent in reverse order!
+ */
+ uint8_t crcl = (ctx->crc_out & 0xff) ^ 0xff;
+ uint8_t crch = (ctx->crc_out >> 8) ^ 0xff;
+ ax25_putchar(ctx, crcl);
+ ax25_putchar(ctx, crch);
+
+ ASSERT(ctx->crc_out == AX25_CRC_CORRECT);
+
+ kfile_putc(HDLC_FLAG, ctx->ch);
+}
+
+static void print_call(KFile *ch, const AX25Call *call)
+{
+ kfile_printf(ch, "%.6s", call->call);
+ if (call->ssid)
+ kfile_printf(ch, "-%d", call->ssid);
+}
+
+/**
+ * Print a AX25 message in TNC-2 packet monitor format.
+ * \param ch a kfile channel where the message will be printed.
+ * \param msg the message to be printed.
+ */
+void ax25_print(KFile *ch, const AX25Msg *msg)
+{
+ print_call(ch, &msg->src);
+ kfile_putc('>', ch);
+ print_call(ch, &msg->dst);
+
+ #if CONFIG_AX25_RPT_LST
+ for (int i = 0; i < msg->rpt_cnt; i++)
+ {
+ kfile_putc(',', ch);
+ print_call(ch, &msg->rpt_lst[i]);
+ // TODO: add * to the trasmitting digi
+ }
+ #endif
+
+ kfile_printf(ch, ":%.*s\n", msg->len, msg->info);
+}
+
+
+/**
+ * Init the AX25 protocol decoder.
+ *
+ * \param ctx AX25 context to init.
+ * \param channel Used to gain access to the physical medium
+ * \param hook Callback function called when a message is received
+ */