Bug Summary

File:async.c
Warning:line 167, column 38
The right operand of '==' is a garbage value

Annotated Source Code

1/*
2 * SpanDSP - a series of DSP components for telephony
3 *
4 * async.c - Asynchronous serial bit stream encoding and decoding
5 *
6 * Written by Steve Underwood <steveu@coppice.org>
7 *
8 * Copyright (C) 2003 Steve Underwood
9 *
10 * All rights reserved.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 2.1,
14 * as published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26/*! \file */
27
28#if defined(HAVE_CONFIG_H1)
29#include "config.h"
30#endif
31
32#include <inttypes.h>
33#include <stdlib.h>
34#include <string.h>
35#include <assert.h>
36#if defined(HAVE_STDBOOL_H1)
37#include <stdbool.h>
38#else
39#include "spandsp/stdbool.h"
40#endif
41
42#include "spandsp/telephony.h"
43#include "spandsp/alloc.h"
44#include "spandsp/bit_operations.h"
45#include "spandsp/async.h"
46
47#include "spandsp/private/async.h"
48
49SPAN_DECLARE(const char *)__attribute__((visibility("default"))) const char * signal_status_to_str(int status)
50{
51 switch (status)
52 {
53 case SIG_STATUS_CARRIER_DOWN:
54 return "Carrier down";
55 case SIG_STATUS_CARRIER_UP:
56 return "Carrier up";
57 case SIG_STATUS_TRAINING_IN_PROGRESS:
58 return "Training in progress";
59 case SIG_STATUS_TRAINING_SUCCEEDED:
60 return "Training succeeded";
61 case SIG_STATUS_TRAINING_FAILED:
62 return "Training failed";
63 case SIG_STATUS_FRAMING_OK:
64 return "Framing OK";
65 case SIG_STATUS_END_OF_DATA:
66 return "End of data";
67 case SIG_STATUS_ABORT:
68 return "Abort";
69 case SIG_STATUS_BREAK:
70 return "Break";
71 case SIG_STATUS_SHUTDOWN_COMPLETE:
72 return "Shutdown complete";
73 case SIG_STATUS_OCTET_REPORT:
74 return "Octet report";
75 case SIG_STATUS_POOR_SIGNAL_QUALITY:
76 return "Poor signal quality";
77 case SIG_STATUS_MODEM_RETRAIN_OCCURRED:
78 return "Modem retrain occurred";
79 case SIG_STATUS_LINK_CONNECTED:
80 return "Link connected";
81 case SIG_STATUS_LINK_DISCONNECTED:
82 return "Link disconnected";
83 case SIG_STATUS_LINK_ERROR:
84 return "Link error";
85 case SIG_STATUS_LINK_IDLE:
86 return "Link idle";
87 }
88 /*endswitch*/
89 return "???";
90}
91/*- End of function --------------------------------------------------------*/
92
93SPAN_DECLARE(void)__attribute__((visibility("default"))) void async_rx_put_bit(void *user_data, int bit)
94{
95 async_rx_state_t *s;
96 int parity_bit_a;
97 int parity_bit_b;
1
'parity_bit_b' declared without an initial value
98
99 s = (async_rx_state_t *) user_data;
100 if (bit < 0)
2
Assuming 'bit' is >= 0
3
Taking false branch
101 {
102 /* Special conditions */
103 switch (bit)
104 {
105 case SIG_STATUS_CARRIER_UP:
106 case SIG_STATUS_CARRIER_DOWN:
107 case SIG_STATUS_TRAINING_IN_PROGRESS:
108 case SIG_STATUS_TRAINING_SUCCEEDED:
109 case SIG_STATUS_TRAINING_FAILED:
110 case SIG_STATUS_END_OF_DATA:
111 s->put_byte(s->user_data, bit);
112 s->bitpos = 0;
113 s->frame_in_progress = 0;
114 break;
115 default:
116 //printf("Eh!\n");
117 break;
118 }
119 /*endswitch*/
120 }
121 else
122 {
123 if (s->bitpos == 0)
4
Assuming the condition is false
5
Taking false branch
124 {
125 /* Search for the start bit */
126 s->bitpos += (bit ^ 1);
127 s->frame_in_progress = 0;
128 }
129 else if (s->bitpos <= s->total_data_bits)
6
Taking false branch
130 {
131 s->frame_in_progress = (s->frame_in_progress >> 1) | (bit << 15);
132 s->bitpos++;
133 }
134 else
135 {
136 /* We should be at the first stop bit */
137 if (bit == 0 && !s->use_v14)
7
Assuming 'bit' is not equal to 0
138 {
139 s->framing_errors++;
140 s->bitpos = 0;
141 }
142 else
143 {
144 /* Check and remove any parity bit */
145 if (s->parity != ASYNC_PARITY_NONE)
8
Assuming the condition is true
9
Taking true branch
146 {
147 parity_bit_a = (s->frame_in_progress >> 15) & 0x01;
148 /* Trim off the parity bit */
149 s->frame_in_progress &= 0x7FFF;
150 s->frame_in_progress >>= (16 - s->total_data_bits);
151 switch (s->parity)
10
'Default' branch taken. Execution continues on line 167
152 {
153 case ASYNC_PARITY_ODD:
154 parity_bit_b = parity8(s->frame_in_progress) ^ 1;
155 break;
156 case ASYNC_PARITY_EVEN:
157 parity_bit_b = parity8(s->frame_in_progress);
158 break;
159 case ASYNC_PARITY_MARK:
160 parity_bit_b = 1;
161 break;
162 case ASYNC_PARITY_SPACE:
163 parity_bit_b = 0;
164 break;
165 }
166 /*endswitch*/
167 if (parity_bit_a == parity_bit_b)
11
The right operand of '==' is a garbage value
168 s->put_byte(s->user_data, s->frame_in_progress);
169 else
170 s->parity_errors++;
171 /*endif*/
172 }
173 else
174 {
175 s->frame_in_progress >>= (16 - s->total_data_bits);
176 s->put_byte(s->user_data, s->frame_in_progress);
177 }
178 /*endif*/
179 if (bit == 1)
180 {
181 /* This is the first of any stop bits */
182 s->bitpos = 0;
183 }
184 else
185 {
186 /* There might be a framing error, but we have to assume the stop
187 bit has been dropped by the rate adaption mechanism described in
188 V.14. */
189 s->bitpos = 1;
190 s->frame_in_progress = 0;
191 }
192 /*endif*/
193 }
194 /*endif*/
195 }
196 /*endif*/
197 }
198 /*endif*/
199}
200/*- End of function --------------------------------------------------------*/
201
202SPAN_DECLARE(int)__attribute__((visibility("default"))) int async_rx_get_parity_errors(async_rx_state_t *s, bool_Bool reset)
203{
204 int errors;
205
206 errors = s->parity_errors;
207 if (reset)
208 s->parity_errors = 0;
209 /*endif*/
210 return errors;
211}
212/*- End of function --------------------------------------------------------*/
213
214SPAN_DECLARE(int)__attribute__((visibility("default"))) int async_rx_get_framing_errors(async_rx_state_t *s, bool_Bool reset)
215{
216 int errors;
217
218 errors = s->framing_errors;
219 if (reset)
220 s->framing_errors = 0;
221 /*endif*/
222 return errors;
223}
224/*- End of function --------------------------------------------------------*/
225
226SPAN_DECLARE(async_rx_state_t *)__attribute__((visibility("default"))) async_rx_state_t * async_rx_init(async_rx_state_t *s,
227 int data_bits,
228 int parity,
229 int stop_bits,
230 bool_Bool use_v14,
231 span_put_byte_func_t put_byte,
232 void *user_data)
233{
234 if (s == NULL((void*)0))
235 {
236 if ((s = (async_rx_state_t *) span_alloc(sizeof(*s))) == NULL((void*)0))
237 return NULL((void*)0);
238 /*endif*/
239 }
240 /*endif*/
241 /* We don't record the stop bits, as they are currently only in the function
242 call for completeness, and future compatibility. */
243 s->data_bits = data_bits;
244 s->parity = parity;
245 s->total_data_bits = data_bits;
246 if (parity != ASYNC_PARITY_NONE)
247 s->total_data_bits++;
248 /*endif*/
249 s->use_v14 = use_v14;
250
251 s->put_byte = put_byte;
252 s->user_data = user_data;
253
254 s->frame_in_progress = 0;
255 s->bitpos = 0;
256
257 s->parity_errors = 0;
258 s->framing_errors = 0;
259 return s;
260}
261/*- End of function --------------------------------------------------------*/
262
263SPAN_DECLARE(int)__attribute__((visibility("default"))) int async_rx_release(async_rx_state_t *s)
264{
265 return 0;
266}
267/*- End of function --------------------------------------------------------*/
268
269SPAN_DECLARE(int)__attribute__((visibility("default"))) int async_rx_free(async_rx_state_t *s)
270{
271 span_free(s);
272 return 0;
273}
274/*- End of function --------------------------------------------------------*/
275
276SPAN_DECLARE(int)__attribute__((visibility("default"))) int async_tx_get_bit(void *user_data)
277{
278 async_tx_state_t *s;
279 int bit;
280 int parity_bit;
281 int32_t next_byte;
282
283 s = (async_tx_state_t *) user_data;
284 if (s->bitpos == 0)
285 {
286 if (s->presend_bits > 0)
287 {
288 s->presend_bits--;
289 return 1;
290 }
291 /*endif*/
292 if ((next_byte = s->get_byte(s->user_data)) < 0)
293 {
294 if (next_byte != SIG_STATUS_LINK_IDLE)
295 return next_byte;
296 /*endif*/
297 /* Idle for a bit time. If the get byte call configured a presend
298 time we might idle for longer. */
299 return 1;
300 }
301 /*endif*/
302 s->frame_in_progress = next_byte;
303 /* Trim off any upper bits */
304 s->frame_in_progress &= (0xFFFF >> (16 - s->data_bits));
305 /* Now insert any parity bit */
306 switch (s->parity)
307 {
308 case ASYNC_PARITY_MARK:
309 s->frame_in_progress |= (1 << s->data_bits);
310 break;
311 case ASYNC_PARITY_EVEN:
312 parity_bit = parity8(s->frame_in_progress);
313 s->frame_in_progress |= (parity_bit << s->data_bits);
314 break;
315 case ASYNC_PARITY_ODD:
316 parity_bit = parity8(s->frame_in_progress) ^ 1;
317 s->frame_in_progress |= (parity_bit << s->data_bits);
318 break;
319 }
320 /*endswitch*/
321 /* Insert some stop bits above the data and parity ones */
322 s->frame_in_progress |= (0xFFFF << s->total_data_bits);
323 /* Start bit */
324 bit = 0;
325 s->bitpos++;
326 }
327 else
328 {
329 bit = s->frame_in_progress & 1;
330 s->frame_in_progress >>= 1;
331 if (++s->bitpos > s->total_bits)
332 s->bitpos = 0;
333 /*endif*/
334 }
335 /*endif*/
336 return bit;
337}
338/*- End of function --------------------------------------------------------*/
339
340SPAN_DECLARE(void)__attribute__((visibility("default"))) void async_tx_presend_bits(async_tx_state_t *s, int bits)
341{
342 s->presend_bits = bits;
343}
344/*- End of function --------------------------------------------------------*/
345
346SPAN_DECLARE(async_tx_state_t *)__attribute__((visibility("default"))) async_tx_state_t * async_tx_init(async_tx_state_t *s,
347 int data_bits,
348 int parity,
349 int stop_bits,
350 bool_Bool use_v14,
351 span_get_byte_func_t get_byte,
352 void *user_data)
353{
354 if (s == NULL((void*)0))
355 {
356 if ((s = (async_tx_state_t *) span_alloc(sizeof(*s))) == NULL((void*)0))
357 return NULL((void*)0);
358 /*endif*/
359 }
360 /*endif*/
361 /* We have a use_v14 parameter for completeness, but right now V.14 only
362 applies to the receive side. We are unlikely to have an application where
363 flow control does not exist, so V.14 stuffing is not needed. */
364 s->data_bits = data_bits;
365 s->parity = parity;
366 s->total_data_bits = data_bits;
367 if (parity != ASYNC_PARITY_NONE)
368 s->total_data_bits++;
369 /*endif*/
370 s->total_bits = s->total_data_bits + stop_bits;
371 s->get_byte = get_byte;
372 s->user_data = user_data;
373
374 s->frame_in_progress = 0;
375 s->bitpos = 0;
376 s->presend_bits = 0;
377 return s;
378}
379/*- End of function --------------------------------------------------------*/
380
381SPAN_DECLARE(int)__attribute__((visibility("default"))) int async_tx_release(async_tx_state_t *s)
382{
383 return 0;
384}
385/*- End of function --------------------------------------------------------*/
386
387SPAN_DECLARE(int)__attribute__((visibility("default"))) int async_tx_free(async_tx_state_t *s)
388{
389 span_free(s);
390 return 0;
391}
392/*- End of function --------------------------------------------------------*/
393/*- End of file ------------------------------------------------------------*/