aboutsummaryrefslogtreecommitdiff
path: root/software/main/MU80X.cpp
blob: c50ac07d22e6df09812120ca564dcd1ca51c917b (plain)
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
 * clou.cpp
 *
 *  Created on: 15.07.2021
 *      Author: steffen
 */
#include "MU80X.h"

//#define DEBUG
#ifdef ESP32
MU80X::MU80X(int fh) {
	ProtStatus = 0;
	ConnectionStatus=0;
	TagsInBuffer=0;
	TagsSeen=0;
	_devid = 0xFF;

	_devh = fh;

	int res = iGetReaderInformation(_devid);
	if (res >= 0) {
//		printf("RDR Info:%d\n",res);
//		DumpBuffer(RecvBuf,res);
//		fflush (stdout);
		_devid = RecvBuf[1];
	}
}
#else
MU80X::MU80X(const char *dev, const uint16_t baud) {
	struct termios t1, t2;

	ProtStatus = 0;
	_devid = 0xFF;

	int bd = B115200;
	if (baud == 57600)
		bd = B57600;

	_devh = open(dev, O_RDWR | O_NONBLOCK);
	if (_devh < 0) {
		fprintf(stderr, "Unable to open serial device %s", dev);
	} else {

		tcgetattr(_devh, &t1);
		tcgetattr(_devh, &t2);
		cfmakeraw(&t1);

		t1.c_cflag = (CLOCAL | CS8 | CREAD);
		cfsetspeed(&t1, bd);
		t1.c_cc[VMIN] = 0;
		t1.c_cc[VTIME] = 0;
		tcsetattr(_devh, TCSADRAIN, &t1);

		int res = iGetReaderInformation(_devid);
		if (res >= 0) {
			fflush (stdout);
			_devid = RecvBuf[1];
		}
	}

}
#endif

MU80X::~MU80X(void) {
#ifdef ESP32

#else
	if (_devh > 0)
		close(_devh);
#endif
}
// internal
void MU80X::DumpBuffer(unsigned char *buf, int len) {
	int i, c;
	char *lb = (char*) buf;
	for (i = 0; i < len; i++) {
		if (i % 32 == 0)
			printf("\n");
		c = (char) *lb++;
		printf("%02X ", (unsigned char) c);
		if (c > 32)
			printf(" %c ", c);
		else
			printf("   ");

	}
	printf("\n");
}

void MU80X::PrintTag(Tag_t tag) {
	int i;
	for (i = 0; i < 12; i++) {
		printf("%02X ", tag.b[i]);
	}
	printf("Ant:%d RSSI:%d Count:%d ", tag.ant, tag.rssi, tag.count);
	printf("\n");
}

int MU80X::_DecodeBuffer(int todecode, int start, unsigned char *buf,
		Tag_t *tagbuf) {
	int i;
	uint8_t len, ant, rssi, count;
	for (i = start; i < start + todecode; i++) {
//		DumpBuffer(buf,s);
		ant = *buf;
		buf++;
		len = *buf;
		buf++;
		tagbuf[i].ant = ant;
#ifdef DEBUG
		printf("Ant: %d Len: %d",ant,len);
#endif
		if (len == 8) {
			tagbuf[i].b[0] = 0;
			tagbuf[i].b[1] = 0;
			tagbuf[i].b[2] = 0;
			tagbuf[i].b[3] = 0;
			memcpy(&tagbuf[i].b[4], buf, len);
		} else if (len == 12) {
			memcpy(&tagbuf[i].b[0], buf, len);
		} else {
			fprintf(stderr, "Unknown Taglength:%d :\n", len);
		}
		buf += len;
		rssi = *buf;
		buf++;
		count = *buf;
		buf++;
		tagbuf[i].rssi = rssi;
		tagbuf[i].count = count;
#ifdef DEBUG
		printf("Ant: %d Len: %d\n",ant,len);
//		PrintTag(tagbuf[i]);
#endif

	}
	return i - start;
}

#define PRESET_VALUE 0xFFFF
#define POLYNOMIAL 0x8408
uint16_t MU80X::_ui16CalculateCrc(unsigned char *data, unsigned char len) {

	unsigned char ucI, ucJ;
	uint16_t uiCrcValue = PRESET_VALUE;
	for (ucI = 0; ucI < len; ucI++) {
		uiCrcValue = uiCrcValue ^ *(data + ucI);
		for (ucJ = 0; ucJ < 8; ucJ++) {
			if (uiCrcValue & 0x0001) {
				uiCrcValue = (uiCrcValue >> 1) ^ POLYNOMIAL;
			} else {
				uiCrcValue = (uiCrcValue >> 1);
			}
		}
	}
	return uiCrcValue;
}

int MU80X::serial_read(int fh, unsigned char *buf) {
#ifdef ESP32
	return (uart_read_bytes(fh, buf, 1, 1));
#else
	return (read(fh,buf,1));
#endif
}

int MU80X::serial_write(int fh, unsigned char *buf, int len) {
#ifdef ESP32
	return (uart_write_bytes(fh, buf, len));
#else
	return(write(fh,buf,len));
#endif
}

// low level read and write
int MU80X::iReadProt(int ms) {
	int cnt = ms;
	int cc = 0;
	uint8_t len = 0;
	int i;
	while (cnt > 0) {
		if ((cc == 0) & (serial_read(_devh, &RecvBuf[0]) == 1)) {
			len = (uint8_t) RecvBuf[0];
#ifdef DEBUG
			printf("\nNeed to receive:%d\n", len+1);
#endif
			fflush (stdout);
			cc = 1;
			cnt = ms;
			i = 1;
			while (i <= len) {
				if (serial_read(_devh, &RecvBuf[i]) == 1) {
					i++;
					cnt = ms;
				} else {
//					printf(" X ");fflush(stdout);
					cnt--;
					if (cnt == 0)
						return ERR_NOANSWER;
#ifdef ESP32
					vTaskDelay(1);
#else
					usleep(1000);
#endif
				}
			}
#ifdef DEBUG
			printf("Received:%d\n", len+1);
			fflush(stdout);
#endif
			return len + 1;
		}

		cnt--;
#ifdef ESP32
		vTaskDelay(1);
#else
		usleep(1000);
#endif
	}
	return -1;
}

int MU80X::iSendProt(uint8_t len, uint8_t adr, uint8_t cmd,
		unsigned char *data) {
	int res = 0;
	uint16_t CRC = 0;
	SendBuf[0] = len + 4;
	SendBuf[1] = adr;
	SendBuf[2] = cmd;
	memcpy(&SendBuf[3], data, len);
	CRC = _ui16CalculateCrc(SendBuf, len + 3);
	SendBuf[3 + len] = CRC & 0xFF;
	SendBuf[4 + len] = CRC >> 8;
#ifdef DEBUG
	printf("Send:");
	DumpBuffer(SendBuf, len + 5);
#endif
	res = serial_write(_devh, (unsigned char *)SendBuf, (int)len + 5);
#ifdef DEBUG
	printf("Written:%d\n", res);
#endif
	if (res == len + 5) {
		res = iReadProt(1000);
		if (res > 0) {
			uint8_t LEN = RecvBuf[0];
			uint16_t RCRC = RecvBuf[LEN - 1] + (RecvBuf[LEN] << 8);
			uint16_t CCRC = _ui16CalculateCrc(RecvBuf, LEN - 1);
#ifdef DEBUG
			printf("CRC: %04X  %04X\n", RCRC, CCRC);
#endif
			if (RCRC == CCRC) {
				ProtStatus = RecvBuf[2];
				return res;
			} else
				return ERR_CRC;
		} else
			return ERR_NOANSWER;

	} else
		return ERR_WRITE;
	return res;
}

int MU80X::iSendProt(uint8_t len, uint8_t adr, uint8_t cmd, unsigned char *data,
		int wait) {
	int res = 0;
	uint16_t CRC = 0;
	SendBuf[0] = len + 4;
	SendBuf[1] = adr;
	SendBuf[2] = cmd;
	memcpy(&SendBuf[3], data, len);
	CRC = _ui16CalculateCrc(SendBuf, len + 3);
	SendBuf[3 + len] = CRC & 0xFF;
	SendBuf[4 + len] = CRC >> 8;
#ifdef DEBUG
	printf("Send:");
	DumpBuffer(SendBuf, len + 5);
#endif
	res = serial_write(_devh, (unsigned char*)SendBuf, (int)len + 5);
#ifdef DEBUG
	printf("SendProt wrtn:%d  ", res);
#endif
	if (res == len + 5) {
		res = iReadProt(wait);
#ifdef DEBUG
		printf(" rcvd:%d\n",res);fflush(stderr);
#endif
		if (res > 0) {
			uint8_t LEN = RecvBuf[0];
			uint16_t RCRC = RecvBuf[LEN - 1] + (RecvBuf[LEN] << 8);
			uint16_t CCRC = _ui16CalculateCrc(RecvBuf, LEN - 1);
			if (RCRC == CCRC) {
				ProtStatus = RecvBuf[2];
				return res;
			} else
				return ERR_CRC;
		} else
			return ERR_NOANSWER;

	} else
		return ERR_WRITE;
	return res;
}

// protocol implementation
int MU80X::iBufferInventory(uint8_t Q, uint8_t antenna, uint8_t time) {
	int res = 0;
	unsigned char param[16];
	param[0] = Q;		// QV
	param[1] = 0xFF;  	// SS = TID:01 EPC:FF
	param[2] = 0x00;	// TG no clue
	param[3] = 0x80 + antenna;
	param[4] = time;
	res = iSendProt(5, _devid, 0x18, param, 20000);
	if (res > 0) {
#ifdef DEBUG
		printf("INV:%d Stat:%d\n",res,ProtStatus);fflush(stdout);
#endif
//		DumpBuffer(RecvBuf, res);
		printf("-------------------------\n");
		fflush (stdout);
		if (RecvBuf[0] == 9) {
			TagsInBuffer = RecvBuf[5] + (RecvBuf[4] << 8);
			TagsSeen = RecvBuf[7] + (RecvBuf[6] << 8);
		}
	}
	return res;

}

int MU80X::iClearBuffer() {
	int res;
	res = iSendProt(0, _devid, 0x73, NULL);
	return res;
}

int MU80X::iGetBuffer(Tag_t *tagbuf) {
	int res = 0;
	int havedata = 1;
	int tagcount = 0;
	int todecode = 0;
	int decoded = 0;
	int readmore = 0;
	res = iSendProt(0, _devid, 0x72, NULL);
	if (res < 0)
		return res;
	while (havedata) {
		todecode = RecvBuf[4];
		readmore = (RecvBuf[3] == 3); // == 3 !
		decoded = _DecodeBuffer(todecode, tagcount, &RecvBuf[5], tagbuf);
#ifdef DEBUG
		DumpBuffer(RecvBuf,res);
		printf("To decode:%d decoded:%d \n", todecode, decoded);
		fflush(stdout);
#endif
		if (decoded == todecode) {
			tagcount += decoded;
			if (readmore) {
				res = iReadProt(100);
				if (res < 0)
					return tagcount;
				todecode = RecvBuf[4];
				readmore = (RecvBuf[3] == 3); // == 3 !
			} else
				havedata = 0;
		} else {
			// sometingwong
			return ERR_DECODE;
		}
	}
	return tagcount;
}

int MU80X::iGetReaderInformation(uint8_t addr) {
	int res;
	res = iSendProt(0, addr, 0x21, NULL);
	return res;
}

int MU80X::iGetReaderInformation() {
	int res;
	res = iSendProt(0, _devid, 0x21, NULL);
	return res;
}

int MU80X::iGetReaderSerial() {
	int res;
	res = iSendProt(0, _devid, 0x4C, NULL);
	return res;
}

int MU80X::iSetBaud(uint8_t bd) { // 0=9600,1=19200,2=38400,5=57600,115200
	int res;
	unsigned char param[2];
	param[0] = bd;
	param[1] = 0;
	res = iSendProt(1, _devid, 0x28, param);
	return res;
}

int MU80X::iSetPower(uint8_t dBm) {
	int res;
	unsigned char param[2];
	param[0] = dBm;
	param[1] = 0;
	res = iSendProt(1, _devid, 0x2F, param);
	return res;
}

int MU80X::iSetRFRegion() {
	int res;
	unsigned char param[2];
	param[0] = 0x4E; // 0x01 001110 maximum band = 1 + 865,1 + (14x200KHz=2,8 MHz) = 867,9
	param[1] = 0x00; // 0x00 000000 minimum band = 0 = 865,1 MHz + (0 x 200KHz)
	param[2] = 0;
	res = iSendProt(2, _devid, 0x22, param);
	return res;
}

int MU80X::iSetScantime(uint8_t time) { // x * 100ms
	int res;
	unsigned char param[2];
	param[0] = time;
	param[1] = 0;
	res = iSendProt(1, _devid, 0x25, param);
	return res;
}