forked from hharte/mm_manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mm_util.c
73 lines (66 loc) · 2.02 KB
/
mm_util.c
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* This is a "Manager" for the Nortel Millennium payhone.
*
* It can provision a Nortel Millennium payphone with Rev 1.0 or 1.3
* Control PCP. CDRs, Alarms, and Maintenance Reports can also be
* retieved.
*
* www.github.com/hharte/mm_manager
*
* (c) 2020, Howard M. Harte
*/
#include <stdio.h> /* Standard input/output definitions */
#include <stdlib.h>
#include <stdint.h>
#include <string.h> /* String function definitions */
#define POLY 0xa001 /* Polynomial to use for CRC-16 calculation */
/* Calculate CRC-16 checksum using 0xA001 polynomial. */
unsigned crc16(unsigned crc, uint8_t *buf, size_t len)
{
while (len--) {
crc ^= *buf++;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
}
return crc;
}
void dump_hex(uint8_t *data, int len)
{
uint8_t ascii[32];
uint8_t *pascii = ascii;
int i;
printf("\n");
if (len > 0) {
printf("\tData: ");
for (i = 0; i < len; i++) {
if (i % 16 == 0) {
if (i > 0) {
*pascii++ = '\0';
printf("%s", ascii);
}
printf("\n\t%03d: ", i);
pascii = ascii;
}
printf("%02x, ", data[i]);
if ((data[i] >= 0x20) && (data[i] < 0x7F)) {
*pascii++ = data[i];
} else {
*pascii++ = '.';
}
}
*pascii++ = '\0';
if (strlen((char *)ascii) > 0) {
for (i = 0; i < 16 - strlen((char *)ascii); i++) {
printf(" ");
}
printf("%s", ascii);
}
}
printf("\n");
}