librobotcontrol-sys 0.4.0

Rust port of librobotcontrol
Documentation
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/**
 * @file uart.c
 *
 *
 *
 * @author     James Strawson
 * @date       3/6/2018
 */

#include <stdio.h>
#include <termios.h>
#include <errno.h>
#include <time.h>
#include <sys/time.h> // for timeval
#include <fcntl.h> // for open
#include <unistd.h> // for close
#include <string.h>
#include <sys/ioctl.h>
#include <math.h>

#include <rc/uart.h>

#define MAX_BUS		16
#define STRING_BUF	64

// Most bytes to read at once. This is the size of the Sitara UART FIFO buffer.
#define MAX_READ_LEN	128


static int   rc_uart_fd[MAX_BUS+1]; // file descriptors for all ports
static float rc_uart_bus_timeout_s[MAX_BUS+1]; // user-requested timeout in seconds for each bus
static int   rc_uart_shutdown_flag[MAX_BUS+1];


int rc_uart_init(int bus, int baudrate, float timeout_s, int canonical_en, int stop_bits, int parity_en)
{
	int tmpfd, tenths;
	char buf[STRING_BUF];
	struct termios config;
	speed_t speed; //baudrate

	// sanity checks
	if(bus<0 || bus>MAX_BUS){
		fprintf(stderr,"ERROR in rc_uart_init, bus must be between 0 & %d\n", MAX_BUS);
		return -1;
	}
	if(timeout_s<0.1f){
		fprintf(stderr,"ERROR in rc_uart_init, timeout must be >=0.1 seconds\n");
		return -1;
	}
	if(stop_bits!=1 && stop_bits!=2){
		fprintf(stderr,"ERROR in rc_uart_init, stop bits must be 1 or 2\n");
	}

	switch(baudrate){
	case (230400):
		speed=B230400;
		break;
	case (115200):
		speed=B115200;
		break;
	case (57600):
		speed=B57600;
		break;
	case (38400):
		speed=B38400;
		break;
	case (19200):
		speed=B19200;
		break;
	case (9600):
		speed=B9600;
		break;
	case (4800):
		speed=B4800;
		break;
	case (2400):
		speed=B2400;
		break;
	case (1800):
		speed=B1800;
		break;
	case (1200):
		speed=B1200;
		break;
	case (600):
		speed=B600;
		break;
	case (300):
		speed=B300;
		break;
	case (200):
		speed=B200;
		break;
	case (150):
		speed=B150;
		break;
	case (134):
		speed=B134;
		break;
	case (110):
		speed=B110;
		break;
	case (75):
		speed=B75;
		break;
	case (50):
		speed=B50;
		break;
	default:
		fprintf(stderr,"ERROR: int rc_uart_init, invalid baudrate. Please use a standard baudrate\n");
		return -1;
	}

	// close the bus in case it was already open
	rc_uart_close(bus);

	// open file descriptor for blocking reads
	snprintf(buf,sizeof(buf),"/dev/ttyO%d",bus);
	tmpfd = open(buf, O_RDWR | O_NOCTTY | O_NDELAY);
	if(tmpfd==-1){
		perror("ERROR: int rc_uart_init while opening file descriptor");
		fprintf(stderr,"device tree probably isn't loaded\n");
		return -1;
	}

	// get current attributes
	if(tcgetattr(tmpfd,&config)==-1){
		fprintf(stderr,"ERROR: int rc_uart_init, Cannot get uart attributes\n");
		close(tmpfd);
		return -1;
	}

	// wipe tc_config and start setting flags
	memset(&config,0,sizeof(config));

	// the following lines technically do nothing since we just wiped config
	// but they exist to allow easy fiddling and be more explicit about
	// which settings are in use
	if(canonical_en) config.c_lflag |= ICANON;
	else config.c_lflag &= ~ICANON;
	if(parity_en) config.c_cflag |= PARENB;
	else config.c_cflag &= ~PARENB;
	if(stop_bits==1) config.c_cflag &= ~CSTOPB;	// disable 2 stop bits (use just 1)
	else config.c_cflag |= CSTOPB; // enable 2 stop bits

	config.c_cflag &= ~CSIZE;	// wipe all size masks
	config.c_cflag |= CS8;		// set size to 8 bit characters
	config.c_cflag |= CREAD;	// enable reading
	config.c_cflag |= CLOCAL;	// ignore modem status lines

	// convert float timeout in seconds to int timeout in tenths of a second
	tenths = (timeout_s*10);

	// if VTIME>0 & VMIN>0, read() will return when either the requested number
	// of bytes are ready or when VMIN bytes are ready, whichever is smaller.
	// since we set VMIN to the size of the buffer, read() should always return
	// when the user's requested number of bytes are ready.
	config.c_cc[VMIN]=MAX_READ_LEN;
	config.c_cc[VTIME] = tenths+1;

	// set speed in config struct
	if(cfsetispeed(&config, speed)==-1){
		perror("ERROR: in rc_uart_init calling cfsetispeed");
		close(tmpfd);
		return -1;
	}
	if(cfsetospeed(&config, speed)==-1){
		perror("ERROR: in rc_uart_init calling cfsetospeed");
		close(tmpfd);
		return -1;
	}

	// flush and set attributes
	if(tcflush(tmpfd,TCIOFLUSH)==-1){
		perror("ERROR: in rc_uart_init calling tcflush");
		close(tmpfd);
	}
	if(tcsetattr(tmpfd, TCSANOW, &config) < 0) {
		fprintf(stderr,"cannot set uart%d attributes\n", bus);
		close(rc_uart_fd[bus]);
		return -1;
	}
	if(tcflush(tmpfd,TCIOFLUSH)==-1){
		perror("ERROR: in rc_uart_init calling tcflush");
		close(tmpfd);
		return -1;
	}

	// turn off the FNDELAY flag
	if(fcntl(tmpfd, F_SETFL, 0)==-1){
		perror("ERROR: in rc_uart_init calling fcntl");
		close(tmpfd);
		return -1;
	}
	if(tcflush(tmpfd,TCIOFLUSH)==-1){
		perror("ERROR: in rc_uart_init calling tcflush");
		close(tmpfd);
		return -1;
	}

	rc_uart_fd[bus]=tmpfd;
	rc_uart_bus_timeout_s[bus]=timeout_s;
	rc_uart_shutdown_flag[bus]=0;
	return 0;
}


int rc_uart_close(int bus)
{
	// sanity checks
	if(bus<0 || bus>MAX_BUS){
		fprintf(stderr,"ERROR: uart bus must be between 0 & %d\n", MAX_BUS);
		return -1;
	}
	rc_uart_shutdown_flag[bus]=1;
	// if not initialized already, return
	if(rc_uart_fd[bus]==0) return 0;
	// flush and close
	tcflush(rc_uart_fd[bus],TCIOFLUSH);
	close(rc_uart_fd[bus]);
	rc_uart_fd[bus]=0;
	return 0;
}


int rc_uart_get_fd(int bus)
{
	// sanity checks
	if(bus<0 || bus>MAX_BUS){
		fprintf(stderr,"ERROR: in rc_uart_get_fd, bus must be between 0 & %d\n", MAX_BUS);
		return -1;
	}
	if(rc_uart_fd[bus]==0){
		fprintf(stderr,"ERROR: in rc_uart_get_fd, uart%d not initialized yet\n", bus);
		return -1;
	}
	return rc_uart_fd[bus];
}


int rc_uart_flush(int bus)
{
	// sanity checks
	if(bus<0 || bus>MAX_BUS){
		fprintf(stderr,"ERROR: in rc_uart_flush, bus must be between 0 & %d\n", MAX_BUS);
		return -1;
	}
	if(rc_uart_fd[bus]==0){
		fprintf(stderr,"ERROR: in rc_uart_flush, uart%d must be initialized first\n", bus);
		return -1;
	}
	if(tcflush(rc_uart_fd[bus],TCIOFLUSH)==-1){
		perror("ERROR in rc_uart_flush:");
		return -1;
	}
	return 0;
}


int rc_uart_write(int bus, uint8_t* data, size_t bytes)
{
	int ret;
	// sanity checks
	if(bus<0 || bus>MAX_BUS){
		fprintf(stderr,"ERROR: uart bus must be between 0 & %d\n", MAX_BUS);
		return -1;
	}
	if(bytes<1){
		fprintf(stderr,"ERROR: number of bytes to send must be >1\n");
		return -1;
	}
	if(rc_uart_fd[bus]==0){
		fprintf(stderr,"ERROR: uart%d must be initialized first\n", bus);
		return -1;
	}
	ret=write(rc_uart_fd[bus], data, bytes);
	if(ret==-1) perror("ERROR in rc_uart_write");
	return ret;
}


int rc_uart_read_bytes(int bus, uint8_t* buf, size_t bytes)
{
	int bytes_to_read, ret;
	// sanity checks
	if(bus<0 || bus>MAX_BUS){
		fprintf(stderr,"ERROR: uart bus must be between 0 & %d\n", MAX_BUS);
		return -1;
	}
	if(bytes<1){
		fprintf(stderr,"ERROR: number of bytes to read must be >=1\n");
		return -1;
	}
	if(rc_uart_fd[bus]==0){
		fprintf(stderr,"ERROR: uart%d must be initialized first\n", bus);
		return -1;
	}

	/*
	// A single call to 'read' just isn't reliable, don't do it.
	// But if you really want to, this commented section is how you do it
	if(bytes<=MAX_READ_LEN){
		// small read, return in one read() call
		// this uses built-in timeout instead of select()
		ret = read(rc_uart_fd[bus], buf, bytes);
		return ret;
	}
	*/

	// any read under 128 bytes should have returned by now.
	// everything below this line is for longer extended reads >128 bytes
	fd_set set; // for select()
	struct timeval timeout;
	int bytes_read; // number of bytes read so far
	int bytes_left; // number of bytes still need to be read

	bytes_read = 0;
	bytes_left = bytes;

	// set up the timeout OUTSIDE of the read loop. We will likely be calling
	// select() multiple times and that will decrease the timeout struct each
	// time ensuring the TOTAL timeout requested by the user is honoured instead
	// of the timeout value compounding each loop.
	timeout.tv_sec = (int)rc_uart_bus_timeout_s[bus];
	timeout.tv_usec = (int)(1000000*fmod(rc_uart_bus_timeout_s[bus],1));

	// exit the read loop once enough bytes have been read
	// or the the shutdown signal flag is set
	while((bytes_left>0) && rc_uart_shutdown_flag[bus]==0){
		FD_ZERO(&set); /* clear the set */
		FD_SET(rc_uart_fd[bus], &set); /* add our file descriptor to the set */
		ret = select(rc_uart_fd[bus] + 1, &set, NULL, NULL, &timeout);
		if(ret == -1){
			// select returned and error. EINTR means interrupted by SIGINT
			// aka ctrl-c. Don't print anything as this happens normally
			// in case of EINTR/Ctrl-C just return how many bytes got read up
			// until then without raising alarms.
			if(errno!=EINTR){
				perror("ERROR in rc_uart_read_bytes calling select");
				return -1;
			}
			return bytes_read;
		}
		else if(ret == 0){
			// timeout
			return bytes_read;
		}
		else{
			// There was data to read. Read up to the number of bytes left
			// and no more. This most likely will return fewer bytes than
			// bytes_left, but we will loop back to get the rest.

			// read no more than MAX_READ_LEN at a time
			if(bytes_left>MAX_READ_LEN)	bytes_to_read = MAX_READ_LEN;
			else bytes_to_read = bytes_left;
			ret=read(rc_uart_fd[bus], buf+bytes_read, bytes_to_read);
			if(ret<0){
				perror("ERROR: in uart_read_bytes");
				return -1;
			}
			else if(ret>0){
				// success, actually read something
				bytes_read += ret;
				bytes_left -= ret;
			}
		}
	}
	return bytes_read;
}


int rc_uart_read_line(int bus, uint8_t* buf, size_t max_bytes)
{
	int ret;
	char temp;
	fd_set set;
	struct timeval timeout;
	int bytes_read=0;

	// sanity checks
	if(bus<0 || bus>MAX_BUS){
		fprintf(stderr,"ERROR: in rc_uart_read_line, bus must be between 0 & %d\n", MAX_BUS);
		return -1;
	}
	if(max_bytes<1){
		fprintf(stderr,"ERROR: in rc_uart_read_line, max_bytes must be >=1\n");
		return -1;
	}
	if(rc_uart_fd[bus]==0){
		fprintf(stderr,"ERROR: in rc_uart_read_line, uart%d must be initialized first\n", bus);
		return -1;
	}

	// set up the timeout OUTSIDE of the read loop. We will likely be calling
	// select() multiple times and that will decrease the timeout struct each
	// time ensuring the TOTAL timeout requested by the user is honoured instead
	// of the timeout value compounding each loop.
	timeout.tv_sec = (int)rc_uart_bus_timeout_s[bus];
	timeout.tv_usec = (int)(1000000*fmod(rc_uart_bus_timeout_s[bus],1));

	// exit the read loop once enough bytes have been read
	// or the shutdown flag is set
	while(bytes_read<(signed)max_bytes && rc_uart_shutdown_flag[bus]==0){
		FD_ZERO(&set); /* clear the set */
		FD_SET(rc_uart_fd[bus], &set); /* add our file descriptor to the set */
		ret = select(rc_uart_fd[bus] + 1, &set, NULL, NULL, &timeout);
		if(ret==-1){
			// select returned and error. EINTR means interrupted by SIGINT
			// aka ctrl-c. Don't print anything as this happens normally
			// in case of EINTR/Ctrl-C just return how many bytes got read up
			// until then without raising alarms.
			if(errno!=EINTR){
				perror("ERROR in rc_uart_read_line calling select");
				return -1;
			}
			return bytes_read;
		}
		else if(ret == 0){
			// timeout
			return bytes_read;
		}
		else{
			// There was data to read. Read one byte;
			ret=read(rc_uart_fd[bus], &temp, 1);
			if(ret<0){
				perror("ERROR in rc_uart_read_line calling read");
				return -1;
			}
			else if(ret==1){
				// success, actually read something
				if(temp=='\n') return bytes_read;
				else{
					*(buf+bytes_read)=temp;
					bytes_read++;
				}
			}
		}
	}
	return bytes_read;
}


int rc_uart_bytes_available(int bus)
{
	int out;
	// sanity checks
	if(bus<0 || bus>MAX_BUS){
		fprintf(stderr,"ERROR: uart bus must be between 0 & %d\n", MAX_BUS);
		return -1;
	}
	if(rc_uart_fd[bus]==0){
		fprintf(stderr,"ERROR: uart%d must be initialized first\n", bus);
		return -1;
	}
	if(ioctl(rc_uart_fd[bus], FIONREAD, &out)==-1){
		perror("ERROR in rc_uart_bytes_available calling ioctl");
		return -1;
	}
	return out;
}