| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 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 | |
| 49 | SPAN_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 | |
| 89 | return "???"; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | SPAN_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) |
| |
| |
| 101 | { |
| 102 | |
| 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 | |
| 117 | break; |
| 118 | } |
| 119 | |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | if (s->bitpos == 0) |
| 4 | | Assuming the condition is false | |
|
| |
| 124 | { |
| 125 | |
| 126 | s->bitpos += (bit ^ 1); |
| 127 | s->frame_in_progress = 0; |
| 128 | } |
| 129 | else if (s->bitpos <= s->total_data_bits) |
| |
| 130 | { |
| 131 | s->frame_in_progress = (s->frame_in_progress >> 1) | (bit << 15); |
| 132 | s->bitpos++; |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | |
| 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 | |
| 145 | if (s->parity != ASYNC_PARITY_NONE) |
| 8 | | Assuming the condition is true | |
|
| |
| 146 | { |
| 147 | parity_bit_a = (s->frame_in_progress >> 15) & 0x01; |
| 148 | |
| 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 | |
| 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 | |
| 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 | |
| 179 | if (bit == 1) |
| 180 | { |
| 181 | |
| 182 | s->bitpos = 0; |
| 183 | } |
| 184 | else |
| 185 | { |
| 186 | |
| 187 | |
| 188 | |
| 189 | s->bitpos = 1; |
| 190 | s->frame_in_progress = 0; |
| 191 | } |
| 192 | |
| 193 | } |
| 194 | |
| 195 | } |
| 196 | |
| 197 | } |
| 198 | |
| 199 | } |
| 200 | |
| 201 | |
| 202 | SPAN_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 | |
| 210 | return errors; |
| 211 | } |
| 212 | |
| 213 | |
| 214 | SPAN_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 | |
| 222 | return errors; |
| 223 | } |
| 224 | |
| 225 | |
| 226 | SPAN_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 | |
| 239 | } |
| 240 | |
| 241 | |
| 242 | |
| 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 | |
| 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 | |
| 262 | |
| 263 | SPAN_DECLARE(int)__attribute__((visibility("default"))) int async_rx_release(async_rx_state_t *s) |
| 264 | { |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | |
| 269 | SPAN_DECLARE(int)__attribute__((visibility("default"))) int async_rx_free(async_rx_state_t *s) |
| 270 | { |
| 271 | span_free(s); |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | |
| 276 | SPAN_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 | |
| 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 | |
| 297 | |
| 298 | |
| 299 | return 1; |
| 300 | } |
| 301 | |
| 302 | s->frame_in_progress = next_byte; |
| 303 | |
| 304 | s->frame_in_progress &= (0xFFFF >> (16 - s->data_bits)); |
| 305 | |
| 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 | |
| 321 | |
| 322 | s->frame_in_progress |= (0xFFFF << s->total_data_bits); |
| 323 | |
| 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 | |
| 334 | } |
| 335 | |
| 336 | return bit; |
| 337 | } |
| 338 | |
| 339 | |
| 340 | SPAN_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 | |
| 345 | |
| 346 | SPAN_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 | |
| 359 | } |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 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 | |
| 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 | |
| 380 | |
| 381 | SPAN_DECLARE(int)__attribute__((visibility("default"))) int async_tx_release(async_tx_state_t *s) |
| 382 | { |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | |
| 387 | SPAN_DECLARE(int)__attribute__((visibility("default"))) int async_tx_free(async_tx_state_t *s) |
| 388 | { |
| 389 | span_free(s); |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | |