3
* BlueZ - Bluetooth protocol stack for Linux
5
* Copyright (C) 2011 Intel Corporation
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation; either version 2 of the License, or
11
* (at your option) any later version.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program; if not, write to the Free Software
20
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28
#include "monitor/crc.h"
29
#include "src/shared/tester.h"
39
static const unsigned char packet_1[] = {
40
0xd6, 0xbe, 0x89, 0x8e, 0x00, 0x17, 0x7e, 0x01,
41
0x00, 0xd0, 0x22, 0x00, 0x02, 0x01, 0x06, 0x03,
42
0x02, 0x0d, 0x18, 0x06, 0xff, 0x6b, 0x00, 0x03,
43
0x16, 0x52, 0x02, 0x0a, 0x00, 0xf4, 0x09, 0x92,
46
static const struct crc_data crc_1 = {
48
.size = sizeof(packet_1),
51
static const unsigned char packet_2[] = {
52
0xd6, 0xbe, 0x89, 0x8e, 0x00, 0x17, 0x7e, 0x01,
53
0x00, 0xd0, 0x22, 0x00, 0x02, 0x01, 0x06, 0x03,
54
0x02, 0x0d, 0x18, 0x06, 0xff, 0x6b, 0x00, 0x03,
55
0x16, 0x54, 0x02, 0x0a, 0x00, 0x95, 0x5f, 0x14,
58
static const struct crc_data crc_2 = {
60
.size = sizeof(packet_2),
63
static const unsigned char packet_3[] = {
64
0xd6, 0xbe, 0x89, 0x8e, 0x00, 0x17, 0x7e, 0x01,
65
0x00, 0xd0, 0x22, 0x00, 0x02, 0x01, 0x06, 0x03,
66
0x02, 0x0d, 0x18, 0x06, 0xff, 0x6b, 0x00, 0x03,
67
0x16, 0x55, 0x02, 0x0a, 0x00, 0x85, 0x66, 0x63,
70
static const struct crc_data crc_3 = {
72
.size = sizeof(packet_3),
75
static const unsigned char packet_4[] = {
76
0xd6, 0xbe, 0x89, 0x8e, 0x00, 0x17, 0x7e, 0x01,
77
0x00, 0xd0, 0x22, 0x00, 0x02, 0x01, 0x06, 0x03,
78
0x02, 0x0d, 0x18, 0x06, 0xff, 0x6b, 0x00, 0x03,
79
0x16, 0x53, 0x02, 0x0a, 0x00, 0xe4, 0x30, 0xe5,
82
static const struct crc_data crc_4 = {
84
.size = sizeof(packet_4),
87
static const unsigned char packet_5[] = {
88
0xd6, 0xbe, 0x89, 0x8e, 0x03, 0x0c, 0x46, 0x1c,
89
0xda, 0x72, 0x02, 0x00, 0x7e, 0x01, 0x00, 0xd0,
90
0x22, 0x00, 0x6e, 0xf4, 0x6f,
93
static const struct crc_data crc_5 = {
95
.size = sizeof(packet_5),
98
static const unsigned char packet_6[] = {
99
0xd6, 0xbe, 0x89, 0x8e, 0x04, 0x17, 0x7e, 0x01,
100
0x00, 0xd0, 0x22, 0x00, 0x10, 0x09, 0x50, 0x6f,
101
0x6c, 0x61, 0x72, 0x20, 0x48, 0x37, 0x20, 0x30,
102
0x30, 0x30, 0x31, 0x37, 0x45, 0x0f, 0x8a, 0x65,
105
static const struct crc_data crc_6 = {
107
.size = sizeof(packet_6),
110
static const unsigned char packet_7[] = {
111
0xd6, 0xbe, 0x89, 0x8e, 0x05, 0x22, 0x46, 0x1c,
112
0xda, 0x72, 0x02, 0x00, 0x7e, 0x01, 0x00, 0xd0,
113
0x22, 0x00, 0x96, 0x83, 0x9a, 0xaf, 0xbe, 0x1d,
114
0x16, 0x03, 0x05, 0x00, 0x36, 0x00, 0x00, 0x00,
115
0x2a, 0x00, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xa5,
119
static const struct crc_data crc_7 = {
121
.size = sizeof(packet_7),
124
static const unsigned char packet_8[] = {
125
0x96, 0x83, 0x9a, 0xaf, 0x01, 0x00, 0xc7, 0x15,
129
static const struct crc_data crc_8 = {
131
.size = sizeof(packet_8),
132
.crc_init = 0x161dbe, /* from packet_7 = 0xbe 0x1d 0x16 */
135
static const unsigned char packet_9[] = {
136
0x96, 0x83, 0x9a, 0xaf, 0x06, 0x14, 0x10, 0x00,
137
0x04, 0x00, 0x09, 0x07, 0x10, 0x00, 0x10, 0x11,
138
0x00, 0x37, 0x2a, 0x13, 0x00, 0x02, 0x14, 0x00,
139
0x38, 0x2a, 0x73, 0x2a, 0xa3,
142
static const struct crc_data crc_9 = {
144
.size = sizeof(packet_9),
145
.crc_init = 0x161dbe, /* from packet_7 = 0xbe 0x1d 0x16 */
148
static void test_crc(gconstpointer data)
150
const struct crc_data *test_data = data;
151
const uint8_t *buf = test_data->packet + test_data->size - 3;
152
uint32_t crc_init, crc_value, crc, rev;
154
if (test_data->crc_init)
155
crc_init = crc24_bit_reverse(test_data->crc_init);
157
crc_init = crc24_bit_reverse(0x555555);
159
crc_value = buf[0] | buf[1] << 8 | buf[2] << 16;
161
crc = crc24_calculate(crc_init, test_data->packet + 4,
162
test_data->size - 7);
164
tester_debug("CRC: 0x%6.6x, Calculated: 0x%6.6x", crc_value, crc);
166
g_assert(crc_value == crc);
168
rev = crc24_reverse(crc_value, test_data->packet + 4,
169
test_data->size - 7);
171
tester_debug("Preset: 0x%6.6x, Calculated: 0x%6.6x", crc_init, rev);
173
g_assert(crc_init == rev);
175
tester_test_passed();
178
int main(int argc, char *argv[])
180
tester_init(&argc, &argv);
182
tester_add("/crc/1", &crc_1, NULL, test_crc, NULL);
183
tester_add("/crc/2", &crc_2, NULL, test_crc, NULL);
184
tester_add("/crc/3", &crc_3, NULL, test_crc, NULL);
185
tester_add("/crc/4", &crc_4, NULL, test_crc, NULL);
186
tester_add("/crc/5", &crc_5, NULL, test_crc, NULL);
187
tester_add("/crc/6", &crc_6, NULL, test_crc, NULL);
188
tester_add("/crc/7", &crc_7, NULL, test_crc, NULL);
189
tester_add("/crc/8", &crc_8, NULL, test_crc, NULL);
190
tester_add("/crc/9", &crc_9, NULL, test_crc, NULL);