1/*
2 * Copyright 2017, Data61
3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4 * ABN 41 687 119 230.
5 *
6 * This software may be distributed and modified according to the terms of
7 * the GNU General Public License version 2. Note that NO WARRANTY is provided.
8 * See "LICENSE_GPLv2.txt" for details.
9 *
10 * @TAG(DATA61_GPL)
11 */
12
13#pragma once
14
15#include <stdint.h>
16#include <utils/util.h>
17//#define DBG_MAP
18//#define DBG_PKT
19//#define DBG_PRINT_PAYLOAD
20//#define DBG_REG
21//#define DBG_FEC
22//#define DBG_BUF
23//#define DBG_NET
24//#define DBG_CLK
25
26#ifdef DBG_MAP
27#  define MAP_DEBUG(x) do{x;}while(0)
28#else
29#  define MAP_DEBUG(x) do{;}while(0)
30#endif
31
32#ifdef DBG_PKT
33#  define PKT_DEBUG(x) do{x;}while(0)
34#  ifdef DBG_PRINT_PAYLOAD
35#    define DEBUG_PRINT_PAYLOAD(DEBUG_PRINT_PAYLOAD)
36#  endif
37#else
38#  define PKT_DEBUG(x) do{;}while(0)
39#endif
40
41#ifdef DBG_FEC
42#define FEC_DEBUG(x) do{x;}while(0)
43#else
44#define FEC_DEBUG(x) do{;}while(0)
45#endif
46
47#ifdef DBG_BUF
48#define BUF_DEBUG(x) do{x;}while(0)
49#else
50#define BUF_DEBUG(x) do{;}while(0)
51#endif
52
53#ifdef DBG_REG
54#define REG_DEBUG(x) do{x;}while(0)
55#else
56#define REG_DEBUG(x) do{;}while(0)
57#endif
58
59#ifdef DBG_NET
60#define NET_DEBUG(x) do{x;}while(0)
61#else
62#define NET_DEBUG(x) do{;}while(0)
63#endif
64
65#ifdef DBG_CLK
66#define CLK_DEBUG(x) do{x;} while(0)
67#else
68#define CLK_DEBUG(x) do{;} while(0)
69#endif
70
71#include <stdio.h>
72#include <stdint.h>
73
74#define COL_NET "\e[1;34m"
75#define COL_IMP "\e[1;31m"
76#define COL_FEC "\e[1;32m"
77#define COL_RX  "\e[42;30m"
78#define COL_TX  "\e[43;30m"
79#define COL_ARP "\e[1;28m"
80#define COL_PKT "\e[1;36m"
81#define COL_DEF "\e[0;0m"
82
83#define set_colour(x) printf(x);
84
85#define cprintf(col, ...) do { \
86        set_colour(col);       \
87        ZF_LOGD(__VA_ARGS__);  \
88        set_colour(COL_DEF);   \
89        printf("\n");          \
90    }while(0)
91
92#define UNIMPLEMENTED() \
93    do{\
94        ZF_LOGF("unimplemented"); \
95        while(1); \
96    } while(0)
97
98#ifdef DBG_PKT
99
100static inline void print_mac(uint8_t* mac){
101    int i;
102    printf("%02x", *mac++);
103    for(i = 0; i < 5; i++){
104        printf(":%02x", *mac++);
105    }
106}
107
108static inline void
109print_type(uint8_t* p){
110    uint32_t type = 0;
111    int i;
112    p += 6*2;
113    for(i = 0; i < 2; i++){
114        type = type << 8  | *p++;
115    }
116    switch(type){
117    case 0x0806: printf("ARP"); break;
118    case 0x0800: printf(" IP"); break;
119    default: printf("UNKNOWN");
120    }
121    printf(" (0x%04x)", type);
122}
123
124static inline void print_val(uint8_t* p, int len){
125    uint32_t val = 0;
126    int i;
127    for(i = 0; i < len; i++){
128        val = val << 8 | *p++;
129    }
130    printf("%d", val);
131}
132
133static inline void print_packet(const char* col, void* packet, int length){
134    cprintf(col, "packet 0x%x (%d bytes)", (uint32_t)packet, length);
135    unsigned char* p = packet;
136    printf(" dst MAC : ");  print_mac(p + 0);
137    printf(" src MAC : ");  print_mac(p + 6); printf("\n");
138    printf(" src port: ");  print_val(p +0x22, 2);
139    printf(" dst port: ");  print_val(p +0x24, 2); printf("\n");
140    printf(" type    : "); print_type(p + 0); printf("\n");
141#if defined(DBG_PRINT_PAYLOAD)
142    int i,j;
143    for(i=0;i<length;i+=32){
144        for(j=0;j<32 && length > i+j;j++){
145            printf("%02x", *p++);
146        }
147        printf("\n");
148    }
149#endif
150    printf("\n");
151}
152#endif
153
154