Revert "Fix lwip implementation of retransmission timers."
[bertos.git] / bertos / net / ax25.c
index 889136595f52997830d310eb5cf342bce1d6f12e..3bd3e75ce2376c3d7220ee056daae962e3e21917 100644 (file)
@@ -34,7 +34,6 @@
  * For now, only UI frames without any Layer 3 protocol are handled.
  * This however is enough to send/receive APRS packets.
  *
- * \version $Id$
  * \author Francesco Sacchi <batt@develer.com>
  *
  */
 #include <string.h> //memset, memcmp
 #include <ctype.h>  //isalnum, toupper
 
+#if CONFIG_AX25_RPT_LST
+       #define AX25_SET_REPEATED(msg, idx, val) \
+               do \
+               { \
+                       if (val) \
+                               (msg)->rpt_flags |= BV(idx) ; \
+                       else \
+                               (msg)->rpt_flags &= ~BV(idx) ; \
+               } while(0) 
+#endif
+
 #define DECODE_CALL(buf, addr) \
        for (unsigned i = 0; i < sizeof((addr)); i++) \
        { \
-               (addr)[i] = *(buf)++ >> 1; \
+               char c = (*(buf)++ >> 1); \
+               (addr)[i] = (c == ' ') ? '\x0' : c; \
        }
 
 static void ax25_decode(AX25Ctx *ctx)
@@ -63,7 +74,6 @@ static void ax25_decode(AX25Ctx *ctx)
        uint8_t *buf = ctx->buf;
 
        DECODE_CALL(buf, msg.dst.call);
-       ASSERT(!(*buf & 0x01));
        msg.dst.ssid = (*buf++ >> 1) & 0x0F;
 
        DECODE_CALL(buf, msg.src.call);
@@ -77,7 +87,12 @@ static void ax25_decode(AX25Ctx *ctx)
                {
                        DECODE_CALL(buf, msg.rpt_lst[msg.rpt_cnt].call);
                        msg.rpt_lst[msg.rpt_cnt].ssid = (*buf >> 1) & 0x0F;
-                       LOG_INFO("RPT%d[%.6s-%d]\n", msg.rpt_cnt, msg.rpt_lst[msg.rpt_cnt].call, msg.rpt_lst[msg.rpt_cnt].ssid);
+                       AX25_SET_REPEATED(&msg, msg.rpt_cnt, (*buf & 0x80));
+
+                       LOG_INFO("RPT%d[%.6s-%d]%c\n", msg.rpt_cnt, 
+                               msg.rpt_lst[msg.rpt_cnt].call, 
+                               msg.rpt_lst[msg.rpt_cnt].ssid,
+                               (AX25_REPEATED(&msg, msg.rpt_cnt) ? '*' : ' '));
                }
        #else
                while (!(*buf++ & 0x01))
@@ -112,6 +127,17 @@ static void ax25_decode(AX25Ctx *ctx)
                ctx->hook(&msg);
 }
 
+
+/**
+ * Check if there are any AX25 messages to be processed.
+ * This function read available characters from the medium and search for
+ * any AX25 messages.
+ * If a message is found it is decoded and the linked callback executed.
+ * This function may be blocking if there are no available chars and the KFile
+ * used in \a ctx to access the medium is configured in blocking mode.
+ *
+ * \param ctx AX25 context to operate on.
+ */
 void ax25_poll(AX25Ctx *ctx)
 {
        int c;
@@ -183,7 +209,7 @@ static void ax25_putchar(AX25Ctx *ctx, uint8_t c)
        kfile_putc(c, ctx->ch);
 }
 
-static void ax25_sendCall(AX25Ctx *ctx, const AX25Call *addr)
+static void ax25_sendCall(AX25Ctx *ctx, const AX25Call *addr, bool last)
 {
        unsigned len = MIN(sizeof(addr->call), strlen(addr->call));
 
@@ -199,20 +225,37 @@ static void ax25_sendCall(AX25Ctx *ctx, const AX25Call *addr)
        if (len < sizeof(addr->call))
                for (unsigned i = 0; i < sizeof(addr->call) - len; i++)
                        ax25_putchar(ctx, ' ' << 1);
+
+       /* The bit7 "has-been-repeated" flag is not implemented here */
+       /* Bits6:5 should be set to 1 for all SSIDs (0x60) */
+       /* The bit0 of last call SSID should be set to 1 */
+       uint8_t ssid = 0x60 | (addr->ssid << 1) | (last ? 0x01 : 0);
+       ax25_putchar(ctx, ssid);
 }
 
-void ax25_send(AX25Ctx *ctx, const AX25Call *dst, const AX25Call *src, const void *_buf, size_t len)
+/**
+ * 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);
 
-       ax25_sendCall(ctx, dst);
-       ax25_putchar(ctx, dst->ssid << 1);
 
-       ax25_sendCall(ctx, src);
-       ax25_putchar(ctx, (src->ssid << 1) | 0x01);
+       /* 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);
 
@@ -233,6 +276,47 @@ void ax25_send(AX25Ctx *ctx, const AX25Call *dst, const AX25Call *src, const voi
        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]);
+               /* Print a '*' if packet has already been transmitted 
+                * by this repeater */
+               if (AX25_REPEATED(msg, i))
+                       kfile_putc('*', ch);
+       }
+       #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
+ */
 void ax25_init(AX25Ctx *ctx, KFile *channel, ax25_callback_t hook)
 {
        ASSERT(ctx);