Skip to content

Commit ac3d72e

Browse files
committed
Table based crc for computing message checksum
1 parent c3c0117 commit ac3d72e

File tree

1 file changed

+170
-0
lines changed

1 file changed

+170
-0
lines changed

analysis/table_crc.ipynb

+170
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {
7+
"collapsed": true
8+
},
9+
"outputs": [],
10+
"source": [
11+
"import pandas as pd\n",
12+
"%matplotlib inline\n",
13+
"import matplotlib\n",
14+
"import numpy as np\n",
15+
"import matplotlib.pyplot as plt\n",
16+
"import dateutil\n",
17+
"import analysis\n",
18+
"\n",
19+
"packets = analysis.parse_packet_file('data/all.txt')\n",
20+
"packets += analysis.parse_packet_file('data/temp_basals.txt')\n",
21+
"\n",
22+
"# Select valid packets of a particular length\n",
23+
"packets = filter(lambda x: x.is_valid() and x.body_len == 10, packets)\n",
24+
"\n",
25+
"# Uncomment if you want to work with less data (faster while developing)\n",
26+
"#packets = packets[0:500]"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": 16,
32+
"metadata": {
33+
"collapsed": true
34+
},
35+
"outputs": [],
36+
"source": [
37+
"def message_hash_split(data):\n",
38+
" # Everything except ID1, byte 4 (sequence & flags), packet crc, and 16bit chksum\n",
39+
" msg = data[5:-3]\n",
40+
" chksum = data[-3:-1]\n",
41+
" return (msg, chksum)\n"
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": 21,
47+
"metadata": {
48+
"collapsed": false
49+
},
50+
"outputs": [
51+
{
52+
"data": {
53+
"text/plain": [
54+
"'1f014829280a1d1802a82800002b7bff'"
55+
]
56+
},
57+
"execution_count": 21,
58+
"metadata": {},
59+
"output_type": "execute_result"
60+
}
61+
],
62+
"source": [
63+
"msg, chksum = message_hash_split(packets[0].tx_data())\n",
64+
"msg.encode('hex')"
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": 25,
70+
"metadata": {
71+
"collapsed": false
72+
},
73+
"outputs": [
74+
{
75+
"data": {
76+
"text/plain": [
77+
"'\\x1f\\x01H)\\xf9\\x1f\\x01H)(\\n\\x1d\\x18\\x02\\xa8(\\x00\\x00+{\\xff\\x814E'"
78+
]
79+
},
80+
"execution_count": 25,
81+
"metadata": {},
82+
"output_type": "execute_result"
83+
}
84+
],
85+
"source": [
86+
"packets[0].tx_data()"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": 23,
92+
"metadata": {
93+
"collapsed": false
94+
},
95+
"outputs": [],
96+
"source": [
97+
"crc_table16 = [0, 32773, 32783, 10, 32795, 30, 20, 32785, 32819, 54, 60, 32825, 40, 32813, 32807, 34, 32867, 102, 108, 32873, 120, 32893, 32887, 114, 80, 32853, 32863, 90, 32843, 78, 68, 32833, 32963, 198, 204, 32969, 216, 32989, 32983, 210, 240, 33013, 33023, 250, 33003, 238, 228, 32993, 160, 32933, 32943, 170, 32955, 190, 180, 32945, 32915, 150, 156, 32921, 136, 32909, 32903, 130, 33155, 390, 396, 33161, 408, 33181, 33175, 402, 432, 33205, 33215, 442, 33195, 430, 420, 33185, 480, 33253, 33263, 490, 33275, 510, 500, 33265, 33235, 470, 476, 33241, 456, 33229, 33223, 450, 320, 33093, 33103, 330, 33115, 350, 340, 33105, 33139, 374, 380, 33145, 360, 33133, 33127, 354, 33059, 294, 300, 33065, 312, 33085, 33079, 306, 272, 33045, 33055, 282, 33035, 270, 260, 33025, 33539, 774, 780, 33545, 792, 33565, 33559, 786, 816, 33589, 33599, 826, 33579, 814, 804, 33569, 864, 33637, 33647, 874, 33659, 894, 884, 33649, 33619, 854, 860, 33625, 840, 33613, 33607, 834, 960, 33733, 33743, 970, 33755, 990, 980, 33745, 33779, 1014, 1020, 33785, 1000, 33773, 33767, 994, 33699, 934, 940, 33705, 952, 33725, 33719, 946, 912, 33685, 33695, 922, 33675, 910, 900, 33665, 640, 33413, 33423, 650, 33435, 670, 660, 33425, 33459, 694, 700, 33465, 680, 33453, 33447, 674, 33507, 742, 748, 33513, 760, 33533, 33527, 754, 720, 33493, 33503, 730, 33483, 718, 708, 33473, 33347, 582, 588, 33353, 600, 33373, 33367, 594, 624, 33397, 33407, 634, 33387, 622, 612, 33377, 544, 33317, 33327, 554, 33339, 574, 564, 33329, 33299, 534, 540, 33305, 520, 33293, 33287, 514]"
98+
]
99+
},
100+
{
101+
"cell_type": "code",
102+
"execution_count": 47,
103+
"metadata": {
104+
"collapsed": true
105+
},
106+
"outputs": [],
107+
"source": [
108+
"def crc16(msg):\n",
109+
" acc = 0x00\n",
110+
" for x in msg:\n",
111+
" acc = (acc >> 8) ^ crc_table16[(acc ^ ord(x)) & 0xff]\n",
112+
" return acc"
113+
]
114+
},
115+
{
116+
"cell_type": "code",
117+
"execution_count": 48,
118+
"metadata": {
119+
"collapsed": true
120+
},
121+
"outputs": [],
122+
"source": [
123+
"def test(packet):\n",
124+
" msg, chksum = message_hash_split(packet.tx_data())\n",
125+
" crc = crc16(msg)\n",
126+
" print \"0x%s, 0x%04x\" % (chksum.encode('hex'), crc)"
127+
]
128+
},
129+
{
130+
"cell_type": "code",
131+
"execution_count": 51,
132+
"metadata": {
133+
"collapsed": false
134+
},
135+
"outputs": [
136+
{
137+
"name": "stdout",
138+
"output_type": "stream",
139+
"text": [
140+
"0x020a, 0x020a\n"
141+
]
142+
}
143+
],
144+
"source": [
145+
"test(packets[6])"
146+
]
147+
}
148+
],
149+
"metadata": {
150+
"kernelspec": {
151+
"display_name": "Python 2",
152+
"language": "python",
153+
"name": "python2"
154+
},
155+
"language_info": {
156+
"codemirror_mode": {
157+
"name": "ipython",
158+
"version": 2
159+
},
160+
"file_extension": ".py",
161+
"mimetype": "text/x-python",
162+
"name": "python",
163+
"nbconvert_exporter": "python",
164+
"pygments_lexer": "ipython2",
165+
"version": "2.7.12"
166+
}
167+
},
168+
"nbformat": 4,
169+
"nbformat_minor": 0
170+
}

0 commit comments

Comments
 (0)