Debian Codegraph @

ax25ipd/kiss.c

raw
1 /* kiss.c KISS assembly and byte-stuffing stuff
2 *
3 * Copyright 1991, Michael Westerhof, Sun Microsystems, Inc.
4 * This software may be freely used, distributed, or modified, providing
5 * this header is not removed.
6 *
7 * This is the only module that knows about the internal structure of
8 * KISS frames.
9 */
10
11 /*
12 * dual port changes Feb 95 ve3pnx & ve3djf
13 */
14
15 #include <stdio.h>
16 #include <syslog.h>
17 #include "ax25ipd.h"
18
19 #define FEND 0xc0
20 #define FESC 0xdb
21 #define TFEND 0xdc
22 #define TFESC 0xdd
23
24 static unsigned char iframe[MAX_FRAME];
25 static unsigned char *ifptr;
26 static int ifcount;
27 static int iescaped;
28
29 static unsigned char oframe[MAX_FRAME];
30 static unsigned char *ofptr;
31 static int ofcount;
32
33 #define PTABLE_SIZE 10
34
35 static struct param_table_entry {
36 unsigned char parameter;
37 unsigned char value;
38 } param_tbl[PTABLE_SIZE];
39
40 static int param_tbl_top;
41
42
43 /*
44 * Initialize the KISS variables
45 */
46
47 void kiss_init(void)
48 {
49 ifptr = iframe;
50 ifcount = 0;
51 iescaped = 0;
52 ofptr = oframe;
53 ofcount = 0;
54 param_tbl_top = 0;
55 }
56
57 /*
58 * Assemble a kiss frame from random hunks of incoming data
59 * Calls the "from_kiss" routine with the kiss frame when a
60 * frame has been assembled.
61 */
62
63 void assemble_kiss(unsigned char *buf, int l)
64 {
65 int i;
66 unsigned char c;
67
68 for (i = 0; i < l; i++, buf++) {
69 c = *buf;
70 if (c == FEND) {
71 if (ifcount > 0) {
72 /* Make sure that the control byte is zero */
73 if (*iframe == '\0' || *iframe == 0x10) {
74 /* Room for CRC in buffer? */
75 if (ifcount < (MAX_FRAME - 2)) {
76 stats.kiss_in++;
77 from_kiss(iframe +
78 1, ifcount - 1);
79 } else {
80 stats.kiss_toobig++;
81 LOGL2
82 ("assemble_kiss: dumped - frame too large\n");
83 }
84 } else {
85 stats.kiss_badtype++;
86 LOGL2
87 ("assemble_kiss: dumped - control byte non-zero\n");
88 }
89 }
90 ifcount = 0;
91 iescaped = 0;
92 ifptr = iframe;
93 continue;
94 }
95 if (c == FESC) {
96 iescaped = 1;
97 continue;
98 }
99 if (iescaped) {
100 if (c == TFEND)
101 c = FEND;
102 if (c == TFESC)
103 c = FESC;
104 iescaped = 0;
105 }
106 if (ifcount < MAX_FRAME) {
107 *ifptr = c;
108 ifptr++;
109 ifcount++;
110 }
111 } /* for every character in the buffer */
112 }
113
114 /* convert a standard AX25 frame into a kiss frame */
115 void send_kiss(unsigned char type, unsigned char *buf, int l)
116 {
117 #define KISSEMIT(x) if (ofcount<MAX_FRAME) {*ofptr=(x);ofptr++;ofcount++;}
118
119 int i;
120
121 ofptr = oframe;
122 ofcount = 0;
123
124 KISSEMIT(FEND);
125
126 if (type == FEND) {
127 KISSEMIT(FESC);
128 KISSEMIT(TFEND);
129 } else if (type == FESC) {
130 KISSEMIT(FESC);
131 KISSEMIT(TFESC);
132 } else {
133 KISSEMIT(type);
134 }
135
136 for (i = 0; i < l; i++, buf++) {
137 if (*buf == FEND) {
138 KISSEMIT(FESC);
139 KISSEMIT(TFEND);
140 } else if (*buf == FESC) {
141 KISSEMIT(FESC);
142 KISSEMIT(TFESC);
143 } else {
144 KISSEMIT(*buf);
145 }
146 } /* for each character in the incoming AX25 frame */
147
148 KISSEMIT(FEND);
149
150 send_tty(oframe, ofcount);
151 }
152
153 /* Add an entry to the parameter table */
154 void param_add(int p, int v)
155 {
156 if (param_tbl_top >= PTABLE_SIZE) {
157 fprintf(stderr, "param table is full; entry ignored.\n");
158 }
159 param_tbl[param_tbl_top].parameter = p & 0xff;
160 param_tbl[param_tbl_top].value = v & 0xff;
161 LOGL4("added param: %d\t%d\n",
162 param_tbl[param_tbl_top].parameter,
163 param_tbl[param_tbl_top].value);
164 param_tbl_top++;
165 }
166
167 /* dump the contents of the parameter table */
168 void dump_params(void)
169 {
170 int i;
171
172 LOGL1("\n%d parameters\n", param_tbl_top);
173 for (i = 0; i < param_tbl_top; i++) {
174 LOGL1(" %d\t%d\n",
175 param_tbl[i].parameter, param_tbl[i].value);
176 }
177 fflush(stdout);
178 }
179
180 /* send the parameters to the TNC */
181 void send_params(void)
182 {
183 int i;
184 unsigned char p, v;
185
186 for (i = 0; i < param_tbl_top; i++) {
187 p = param_tbl[i].parameter;
188 v = param_tbl[i].value;
189 send_kiss(p, &v, 1);
190 LOGL2("send_params: param %d %d\n", p, v);
191 }
192 }