This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
utils.c
274 lines (222 loc) · 6.31 KB
/
utils.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/* the radian_trx SW shall not be distributed nor used for commercial product*/
/* it is exposed just to demonstrate CC1101 capability to reader water meter indexes */
/* there is no Warranty on radian_trx SW */
void show_in_hex(uint8_t* buffer, size_t len)
{
int i=0;
for (i=0 ; i<len ; i++) {
if (!(i % 16))
puts("");
printf("%02X ", buffer[i]);
}
printf("\n");
}
void show_in_hex_array(uint8_t* buffer, size_t len)
{
int i=0;
for (i=0 ; i<len ; i++) {
if (!(i % 16))printf("\r\n");
printf("0x%02X,", buffer[i]);
}
printf("\n");
}
void show_in_hex_one_line(uint8_t* buffer, size_t len)
{
int i=0;
for (i=0 ; i<len ; i++) {
printf("%02X ", buffer[i]);
}
}
void show_in_hex_one_line_GET(uint8_t* buffer, size_t len)
{
int i=0;
for (i=0 ; i<len ; i++) {
printf("%02XS", buffer[i]);
}
}
void show_in_bin(uint8_t* buffer, size_t len)
{
const uint8_t *ptr;
uint8_t mask;
for ( ptr = buffer; len--; ptr++ )
{
for ( mask = 0x80 ; mask ; mask >>= 1 )
{
(mask & *ptr) > 0 ? printf("1") : printf("0");
}
printf(" ");
}
printf("\n");
}
void echo_debug(T_BOOL l_flag,char *fmt, ...)
{
if (l_flag)
{
va_list args;
va_start (args, fmt);
vprintf (fmt, args);
fflush(stdout);
}
}
void print_time(void)
{/*
time_t mytime;
mytime = time(NULL);
printf(ctime(&mytime));*/
time_t rawtime;
struct tm * timeinfo;
char buffer [80];
time (&rawtime);
timeinfo = localtime (&rawtime);
strftime (buffer,80,"%d/%m/%Y %X",timeinfo);
printf("%s",buffer);
}
/*----------------------------------------------------------------------------*/
#define CRC_START_KERMIT 0x0000
#define CRC_POLY_KERMIT 0x8408
static uint8_t crc_tab_init = 0;
static uint16_t crc_tab[256];
/*----------------------------------------------------------------------------*/
/* https://www.libcrc.org/
* static void init_crc_tab( void );
*
* For optimal performance, the CRC Kermit routine uses a lookup table with
* values that can be used directly in the XOR arithmetic in the algorithm.
* This lookup table is calculated by the init_crc_tab() routine, the first
* time the CRC function is called.
*/
static void init_crc_tab( void ) {
uint16_t i;
uint16_t j;
uint16_t crc;
uint16_t c;
for (i=0; i<256; i++) {
crc = 0;
c = i;
for (j=0; j<8; j++) {
if ( (crc ^ c) & 0x0001 ) crc = ( crc >> 1 ) ^ CRC_POLY_KERMIT;
else crc = crc >> 1;
c = c >> 1;
}
crc_tab[i] = crc;
}
crc_tab_init = 1;
} /* init_crc_tab */
/* https://www.libcrc.org/
* uint16_t crc_kermit( const unsigned char *input_str, size_t num_bytes );
*
* The function crc_kermit() calculates the 16 bits Kermit CRC in one pass for
* a byte string of which the beginning has been passed to the function. The
* number of bytes to check is also a parameter.
*/
uint16_t crc_kermit( const unsigned char *input_ptr, size_t num_bytes ) {
uint16_t crc;
uint16_t tmp;
uint16_t short_c;
uint16_t low_byte;
uint16_t high_byte;
const unsigned char *ptr;
size_t a;
if ( ! crc_tab_init ) init_crc_tab();
crc = CRC_START_KERMIT;
ptr = input_ptr;
for (a=0; a<num_bytes; a++) {
short_c = 0x00ff & (uint16_t) *ptr;
tmp = crc ^ short_c;
crc = (crc >> 8) ^ crc_tab[ tmp & 0xff ];
ptr++;
}
low_byte = (crc & 0xff00) >> 8;
high_byte = (crc & 0x00ff) << 8;
crc = low_byte | high_byte;
return crc;
} /* crc_kermit */
/*----------------------------------------------------------------------------*/
/**
* Reverses the bit order of the input data and adds a start bit before and a stop bit
* after each byte.
*
* @param inputBuffer Points to the unencoded data.
* @param inputBufferLen Number of bytes of unencoded data.
* @param outputBuffer Points to the encoded data.
* @param outputBufferLen Number of bytes of encoded data.
*/
int encode2serial_1_3(uint8_t *inputBuffer, int inputBufferLen, uint8_t *outputBuffer) {
// Adds a start and stop bit and reverses the bit order.
// 76543210 76543210 76543210 76543210
// is encoded to:
// #0123456 7###0123 4567###0 1234567# ##012345 6s7# (# -> Start/Stop bit)
int bytepos;
int bitpos;
int i;
int j = 0;
for (i=0 ; i < (inputBufferLen * 8) ; i++) {
//printf("\r\ni=%u",i);
if (i % 8 == 0) {
if (i > 0) {
//printf(" j=%u stopBIT",j);
// Insert stop bit (3)
bytepos = j / 8;
bitpos = j % 8;
outputBuffer[bytepos] |= 1 << (7 - bitpos);
j++;
bytepos = j / 8;
bitpos = j % 8;
outputBuffer[bytepos] |= 1 << (7 - bitpos);
j++;
bytepos = j / 8;
bitpos = j % 8;
outputBuffer[bytepos] |= 1 << (7 - bitpos);
j++;
} //stop bit
// Insert start bit (0)
bytepos = j / 8;
bitpos = j % 8;
//printf(" j=%u startBIT",j);
outputBuffer[bytepos] &= ~(1 << (7 - bitpos));
j++;
}// start stop bit
bytepos = i / 8;
bitpos = i % 8;
uint8_t mask = 1 << bitpos;
if ((inputBuffer[bytepos] & mask) > 0) {
bytepos = j / 8;
bitpos = 7 - (j % 8);
outputBuffer[bytepos] |= 1 << bitpos;
} else {
bytepos = j / 8;
bitpos = 7 - (j % 8);
outputBuffer[bytepos] &= ~(1 << bitpos);
}
j++;
}//for
//insert additional stop bit until end of byte
while (j%8 > 0)
{
bytepos = j / 8;
bitpos = 7 - (j % 8);
outputBuffer[bytepos] |= 1 << bitpos;
j++;
}
outputBuffer[bytepos+1] = 0xFF;
return bytepos+2;
}
int Make_Radian_Master_req(uint8_t *outputBuffer,uint8_t year,uint32_t serial)
{
uint16_t crc;
uint8_t to_encode[] ={0x13,0x10,0x00,0x45,0xFF,0xFF,0xFF,0xFF,0x00,0x45,0x20,0x0A,0x50,0x14,0x00,0x0A,0x40,0xFF,0xFF}; //les 2 derniers octet sont en reserve pour le CKS ainsi que le serial number
uint8_t synch_pattern[] ={0x50,0x00,0x00,0x00,0x03,0xFF,0xFF,0xFF,0xFF};
uint8_t TS_len_u8;
to_encode[4] = year;
to_encode[5] = (uint8_t)((serial&0x00FF0000)>>16);
to_encode[6] = (uint8_t)((serial&0x0000FF00)>>8);
to_encode[7] = (uint8_t) (serial&0x000000FF);
crc = crc_kermit(to_encode,sizeof(to_encode)-2);
//printf("crc:%x\r\n",crc);
to_encode[sizeof(to_encode)-2]=(uint8_t)((crc&0xFF00)>>8);
to_encode[sizeof(to_encode)-1]=(uint8_t)(crc&0x00FF);
//show_in_hex_one_line(to_encode,sizeof(to_encode));
memcpy(outputBuffer,synch_pattern,sizeof(synch_pattern));
TS_len_u8=encode2serial_1_3(to_encode,sizeof(to_encode),&outputBuffer[sizeof(synch_pattern)]);
return TS_len_u8+sizeof(synch_pattern);
}