command.c 12.9 KB
Newer Older
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
/*
	Command support

	Copyright Jeroen Vreeken (jeroen@vreeken.net), 2014

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

#include <command/command.h>
#include <command/command_def.h>

#include <log/log.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <inttypes.h>
#include <sys/ioctl.h>
#include <pthread.h>

34
35
36
37
38
#if defined(__linux__)
#include <endian.h>
#else
#include <sys/endian.h>
#endif
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

void command_initialize(struct command *command, size_t buffer)
{
	memset(command, 0, sizeof(struct command));
	command->rx_buffer = malloc(buffer);
	command->rx_buf_size = buffer;
	command->fd = -1;
}

void command_packet_initialize(struct command_pkt *pkt)
{
	memset(pkt, 0, sizeof(struct command_pkt));
}



#define PACKET_BUFLEN 256
#define PACKET_POOL_CHUNK 16

static struct command_pkt **packet_pool;
static int packet_pool_size = 0;
static int packet_pool_cur = 0;

static pthread_mutex_t command_pkt_mutex = PTHREAD_MUTEX_INITIALIZER;

struct command_pkt *command_packet_new(void)
{
	struct command_pkt *pkt = NULL;
	
	pthread_mutex_lock(&command_pkt_mutex);
	if (packet_pool_cur) {
		pkt = packet_pool[packet_pool_cur-1];
		packet_pool_cur--;
	}
	pthread_mutex_unlock(&command_pkt_mutex);
	
	if (!pkt) {
		pkt = calloc(1, sizeof(struct command_pkt));
		pkt->data = malloc(PACKET_BUFLEN);
		pkt->buflen = PACKET_BUFLEN;
	}
	
	return pkt;
}

int command_packet_resize(struct command_pkt *pkt, size_t len)
{
	if (len <= pkt->buflen)
		return 0;
	
	pkt->data = realloc(pkt->data, len);
	pkt->buflen = len;

	return 0;
}

void command_packet_put(struct command_pkt *pkt)
{
	if (pkt->buflen > PACKET_BUFLEN) {
		free(pkt->data);
		free(pkt);
		return;
	}
	
	pthread_mutex_lock(&command_pkt_mutex);

	if (packet_pool_cur >= packet_pool_size) {
		packet_pool_size += PACKET_POOL_CHUNK;
		packet_pool = realloc(packet_pool,
		    sizeof(struct command_pkt *) * packet_pool_size);
	}
	packet_pool[packet_pool_cur] = pkt;
	pkt->len = 0;
	packet_pool_cur++;

	pthread_mutex_unlock(&command_pkt_mutex);
}

int command_packet_list_add(struct command_pkt *pkt, 
    char *name, enum command_value_type type, char *unit,
    int typec, enum command_ptype typev[])
 {
	size_t entry_size = strlen(name)+1 + 1 + strlen(unit) + 1 + typec + 1;
	unsigned char *entry;
	int i;
	
	if (!pkt->len) {
		command_packet_resize(pkt, 1);
		pkt->len = 1;
		pkt->data[0] = COMMAND_PTYPE_LIST;
	}
	
	command_packet_resize(pkt, pkt->len + entry_size);
	entry = pkt->data + pkt->len;

	strcpy((char *)entry, name);
	entry += strlen(name) + 1;

	*entry = type;
	entry++;

	strcpy((char *)entry, unit);
	entry += strlen(unit) + 1;
	
	for (i = 0; i < typec; i++) {
	   *entry = typev[i];
	   entry++;
	}
	*entry = COMMAND_PTYPE_NULL;
	entry++;
	
	pkt->len += entry_size;
	
	return 0;
}

int command_packet_name_set(struct command_pkt *pkt, char *name)
{
	if (pkt->len < sizeof(struct command_header) + strlen(name) + 1) {
		pkt->len = strlen(name) + 1 + sizeof(struct command_header);
		command_packet_resize(pkt, pkt->len);
		pkt->data[0] = COMMAND_PTYPE_NAME;
	}
	strcpy((char *)pkt->data + 1, name);
	
	return 0;
}

int command_packet_id_set(struct command_pkt *pkt, char *id)
{
	if (pkt->len < sizeof(struct command_header) + strlen(id) + 1) {
		pkt->len = strlen(id) + 1 + sizeof(struct command_header);
		command_packet_resize(pkt, pkt->len);
		pkt->data[0] = COMMAND_PTYPE_ID;
	}
	strcpy((char *)pkt->data + 1, id);
	
	return 0;
}

int command_packet_entry_set(struct command_pkt *pkt,
    struct command_entry *entry, enum command_value_type type)
{
	size_t vsize;
	size_t size;
	unsigned char *p;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
185
	struct command_id *id;
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
	
	switch (type) {
		default:
		case COMMAND_VALUE_TYPE_FLOAT:
		case COMMAND_VALUE_TYPE_UINT32:
		case COMMAND_VALUE_TYPE_SINT32:
			vsize = 4;
			break;
		case COMMAND_VALUE_TYPE_BOOL:
		case COMMAND_VALUE_TYPE_UINT8:
		case COMMAND_VALUE_TYPE_SINT8:
			vsize = 1;
			break;
		case COMMAND_VALUE_TYPE_UINT16:
		case COMMAND_VALUE_TYPE_SINT16:
			vsize = 2;
			break;
	}
204
205
206
	if (entry->type == COMMAND_PTYPE_SETPOINT_TRACK) {
		vsize = 0;
	}
207
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
208
209
	size = sizeof(struct command_id);
	size += vsize;
210
	if (entry->type == COMMAND_PTYPE_SETPOINT_TIME) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
211
		size += sizeof(struct command_timestamp);
212
213
214
215
216
217
218
219
	}
	
	pkt->len = size + sizeof(struct command_header);
	command_packet_resize(pkt, pkt->len);
	pkt->data[0] = entry->type;
	
	p = &pkt->data[1];
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
220
221
222
223
	id = (void *)p;
	id->id = htobe32(entry->id);
	p += sizeof(struct command_id);
	
224
225
226
227
228
229
230
231
232
233
	if (entry->type == COMMAND_PTYPE_SETPOINT_TIME) {
		struct command_timestamp *t = (void *)p;
		
		t->sec = htobe64(entry->t.tv_sec);
		t->nsec = htobe32(entry->t.tv_nsec);
		
		p += sizeof(struct command_timestamp);
	}
	
	switch (vsize) {
234
235
		case 0:
			break;
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
		case 1:
			*p = entry->value.u8;
			break;
		case 2: {
			struct command_ptype_value *pv;
			
			pv = (void *)p;
			pv->u.u16 = htobe16(entry->value.u16);
			break;
		}
		case 4: {
			struct command_ptype_value *pv;
			
			pv = (void *)p;
			pv->u.u32 = htobe32(entry->value.u32);
			break;
		}
	}
	
	return 0;
}

bool command_fd_set(struct command *command, fd_set *set, int *high)
{
	if (command->fd < 0) {
		if (!command->recover)
			return false;

		if (command->recovertime >= time(NULL))
			return false;
		command->recovertime = time(NULL);

		log_send(LOG_T_DEBUG, "Attempt to recover command %s", 
		    command->name);
270
		command_connect(command);
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
		if (command->fd >= 0) {
			struct command_pkt *pkt;

			if (command->name) {
				pkt = command_packet_new();
				command_packet_name_set(pkt, command->name);
				command_packet_write(command, pkt);
				command_packet_put(pkt);
			}
			if (command->id) {
				pkt = command_packet_new();
				command_packet_id_set(pkt, command->id);
				command_packet_write(command, pkt);
				command_packet_put(pkt);
			}
		}
		if (command->fd < 0) {
			log_send(LOG_T_DEBUG, "Failed to recover command connection");
			return false;
		}
		log_send(LOG_T_DEBUG, "Reconnected command");
	}

	FD_SET(command->fd, set);

	if (command->fd > *high)
		*high = command->fd;
	
	return true;
}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
int command_handle_err(struct command *command, fd_set *set)
{
	if (FD_ISSET(command->fd, set)) {
		log_send(LOG_T_DEBUG, "Error, closing %d", command->fd);
		if (command->fd >= 0)
			close(command->fd);
		command->fd = -1;

		if (command->handler_close)
			command->handler_close(command);
	}

	return 0;
}

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
int command_handle(struct command *command, fd_set *set)
{
	if (FD_ISSET(command->fd, set)) {
		while (command_fd_read(command)) {
			struct command_pkt *pkt;
			struct command_entry entry;
			size_t pos = 1;
			
			pkt = command_packet_get(command);
			if (!pkt)
				break;

			switch (pkt->data[0]) {
				case COMMAND_PTYPE_LIST: {
					unsigned char *listentry;
					
					while (pos < pkt->len) {
						char *name;
						enum command_value_type type;
						char *unit;
						enum command_ptype ptypev[COMMAND_PTYPE_MAX] = { 0 };
						int i;
						int j;
						
						listentry = pkt->data + pos;
						
						name = (char *)listentry;
						type = listentry[strlen(name) + 1];
						unit = (char *)listentry+strlen(name) + 2;
						for (i = strlen(name) + strlen(unit) + 3, j = 0;
						    j < COMMAND_PTYPE_MAX && listentry[i] != COMMAND_PTYPE_NULL;
						    i++, j++)
							ptypev[j] = listentry[i];
						
						if (command->handler_list_entry) {
							command->handler_list_entry(command,
							    name, type, unit, j, ptypev);
						}
						pos += strlen(name) + strlen(unit) + 3 + j + 1;
					}
					command->list_received = true;
					break;
				}
				case COMMAND_PTYPE_SETPOINT_TIME: {
					struct command_timestamp *t;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
362
					size_t pos_id = pos + sizeof(struct command_id);
363
					
Jeroen Vreeken's avatar
Jeroen Vreeken committed
364
365
					if (pos_id < pkt->len) {
						t = (void *)pkt->data + pos_id;
366
367
368
369
370
371
372
373
374
						entry.t.tv_sec = be64toh(t->sec);
						entry.t.tv_nsec = be32toh(t->nsec);
					}
					pos += sizeof(struct command_timestamp);
				}
				/* fall through */
				case COMMAND_PTYPE_SPEED:
				case COMMAND_PTYPE_SETPOINT: {
					struct command_ptype_value *v;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
375
					size_t pos_id = pos + sizeof(struct command_id);
376
					
Jeroen Vreeken's avatar
Jeroen Vreeken committed
377
378
					if (pos_id < pkt->len) {
						v = (void *)pkt->data + pos_id;
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
						switch (command->type) {
							case COMMAND_VALUE_TYPE_FLOAT:
							case COMMAND_VALUE_TYPE_UINT32:
							case COMMAND_VALUE_TYPE_SINT32:
								entry.value.u32 = be32toh(v->u.u32);
								pos+=4;
								break;
							case COMMAND_VALUE_TYPE_UINT16:
							case COMMAND_VALUE_TYPE_SINT16:
								entry.value.u16 = be32toh(v->u.u16);
								pos+=2;
								break;
							case COMMAND_VALUE_TYPE_BOOL:
							case COMMAND_VALUE_TYPE_UINT8:
							case COMMAND_VALUE_TYPE_SINT8:
								entry.value.u8 = v->u.u8;
								pos++;
								break;
						}
					}
399
400
401
402
403
404
405
406
407
408
409
410
				}
				/* fall through */
				case COMMAND_PTYPE_SETPOINT_TRACK: {
					struct command_id *id;
					
					if (sizeof(*id) <= pkt->len) {
						id = (void *)pkt->data + 1;
						entry.id = be32toh(id->id);
					}
					entry.type = pkt->data[0];
					
					
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
					if (command->handler_entry)
						command->handler_entry(command, &entry);
					break;
				}
				case COMMAND_PTYPE_NAME: {
					char *name;
					
					name = (void*)pkt->data + 1;
					
					free(command->name);
					command->name = strdup(name);

					if (command->handler_name) {
						command->handler_name(command, name);
					}
					break;
				}
				case COMMAND_PTYPE_ID: {
					char *id;
					
					id = (void*)pkt->data + 1;
					
					free(command->id);
					command->id = strdup(id);

					if (command->handler_id) {
						command->handler_id(command, id);
					}
					break;
				}
			}
			
			command_packet_put(pkt);
		}
		return 0;
	}

	return 0;
}

enum command_state command_state_get(struct command *command)
{
	if (command->fd < 0)
		return COMMAND_STATE_DISCONNECTED;
	
	if (command->list_received)
		return COMMAND_STATE_READY;
	
	return COMMAND_STATE_CONNECTED;
}

void command_autorecover(struct command *command, bool value)
{
	command->recover = value;
}

struct command *command_open_simple(char *host, int port, char *name, char *id,
    enum command_value_type type)
{
	struct command *command = command_open(host, port);
	
	if (!command)
		goto err;

	command->type = type;

	command_id_set(command, id);
	command_name_set(command, name);
	command_autorecover(command, true);

	return command;

err:
	return NULL;
}

int command_name_set(struct command *command, char *name)
{
	struct command_pkt *pkt;

	free(command->name);
	command->name = strdup(name);
	
	pkt = command_packet_new();
	command_packet_name_set(pkt, name);
	command_packet_write(command, pkt);
	command_packet_put(pkt);
	
	return 0;
}

int command_id_set(struct command *command, char *id)
{
	struct command_pkt *pkt;

	free(command->id);
	command->id = strdup(id);
	
	pkt = command_packet_new();
	command_packet_id_set(pkt, id);
	command_packet_write(command, pkt);
	command_packet_put(pkt);
	
	return 0;
}

int command_send(struct command *command, struct command_entry *entry)
{
	fd_set dummyset;
	int dummyhigh;
	struct command_pkt *pkt;
	
	if (!command_fd_set(command, &dummyset, &dummyhigh))
		return -1;

	pkt = command_packet_new();
	command_packet_entry_set(pkt, entry, command->type);
	command_packet_write(command, pkt);
	command_packet_put(pkt);
	
	return 0;
}

char *enum_command_ptype2str(enum command_ptype ptype)
{
	switch(ptype) {
		case COMMAND_PTYPE_NULL:
			return "NULL";
		case COMMAND_PTYPE_LIST:
			return "LIST";
		case COMMAND_PTYPE_NAME:
			return "NAME";
		case COMMAND_PTYPE_ID:
			return "ID";
		case COMMAND_PTYPE_SETPOINT:
			return "SETPOINT";
		case COMMAND_PTYPE_SPEED:
			return "SPEED";
		case COMMAND_PTYPE_SETPOINT_TIME:
			return "SETPOINT_TIME";
Jeroen Vreeken's avatar
Jeroen Vreeken committed
551
552
		case COMMAND_PTYPE_SETPOINT_TRACK:
			return "SETPOINT_TRACK";
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
		case COMMAND_PTYPE_MAX:
			return "MAX";
		default:
			return "unknown";
	}
}

char *enum_command_value_type2str(enum command_value_type type)
{
	switch(type) {
		case COMMAND_VALUE_TYPE_FLOAT:
			return "float";
		case COMMAND_VALUE_TYPE_BOOL:
			return "bool";
		case COMMAND_VALUE_TYPE_UINT8:
			return "uint8";
		case COMMAND_VALUE_TYPE_UINT16:
			return "uint16";
		case COMMAND_VALUE_TYPE_UINT32:
			return "uint32";
		case COMMAND_VALUE_TYPE_SINT8:
			return "sint8";
		case COMMAND_VALUE_TYPE_SINT16:
			return "sint16";
		case COMMAND_VALUE_TYPE_SINT32:
			return "sint32";
		default:
			return "unknown";
	}
};

char *enum_command_state2str(enum command_state state)
{
	switch(state) {
		case COMMAND_STATE_DISCONNECTED:
			return "DISCONNECTED";
		case COMMAND_STATE_CONNECTED:
			return "CONNECTED";
		case COMMAND_STATE_READY:
			return "READY";
		case COMMAND_STATE_RECEIVING:
			return "RECEIVING";
		default:
			return "unknown";
	}
};