trace.c 13.2 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
	Trace support

	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2011
	Copyright Stichting C.A. Muller Radioastronomiestation, 2011

	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/>.

*/

Jeroen Vreeken's avatar
Jeroen Vreeken committed
22
#include <trace/trace.h>
23
#include <trace/trace_def.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
24

25
#include <utils/tcp_connect.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
26
27
#include <log/log.h>

Jeroen Vreeken's avatar
Jeroen Vreeken committed
28
#include <stdio.h>
29
#include <stdlib.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
30
31
32
#include <string.h>
#include <unistd.h>
#include <inttypes.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
33
#include <sys/ioctl.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
34
#include <pthread.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
35
36


Jeroen Vreeken's avatar
Jeroen Vreeken committed
37
static bool timespec_older(struct timespec *t1, struct timespec *t2)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
38
{
Jeroen Vreeken's avatar
Jeroen Vreeken committed
39
	if (t1->tv_sec < t2->tv_sec)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
40
41
		return true;
	if (t1->tv_sec > t2->tv_sec)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
42
		return false;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
43
	if (t1->tv_nsec < t2->tv_nsec)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
44
45
		return true;
	return false;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
46
47
}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
48
static void timespec_normalize(struct timespec *t)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
49
{
Jeroen Vreeken's avatar
Jeroen Vreeken committed
50
51
52
53
	long newsec = t->tv_nsec / 1000000000;
	
	t->tv_sec += newsec;
	t->tv_nsec -= newsec * 1000000000;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
54
55
}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
56
static void timespec_add(struct timespec *dst, struct timespec *add)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
57
{
Jeroen Vreeken's avatar
Jeroen Vreeken committed
58
59
60
	dst->tv_sec += add->tv_sec;
	dst->tv_nsec += add->tv_nsec;
	timespec_normalize(dst);
61
62
63
64
65
66
67
68
69
70
}

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

71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
int trace_interval_set(struct trace *trace, struct timespec *interval, enum trace_interval_type type)
{
	struct trace_pkt *pkt;

	trace->interval_req.tv_sec = interval->tv_sec;
	trace->interval_req.tv_nsec = interval->tv_nsec;
	trace->interval_type_req = type;
	trace->interval_set = true;
	
	pkt = trace_packet_new();
	trace_packet_interval_set(pkt, interval, type);
	trace_packet_write(trace, pkt);
	trace_packet_put(pkt);
	
	return 0;
}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
88
int trace_name_set(struct trace *trace, char *name)
89
{
Jeroen Vreeken's avatar
Jeroen Vreeken committed
90
91
92
93
94
95
96
97
98
99
100
101
	struct trace_pkt *pkt;

	if (trace->name)
		free(trace->name);
	trace->name = strdup(name);
	
	pkt = trace_packet_new();
	trace_packet_name_set(pkt, name);
	trace_packet_write(trace, pkt);
	trace_packet_put(pkt);
	
	return 0;
102
103
}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
104
105
106
107
void trace_autorecover(struct trace *trace, bool value)
{
	trace->recover = value;
}
108

Jeroen Vreeken's avatar
Jeroen Vreeken committed
109
110

void trace_packet_initialize(struct trace_pkt *pkt)
111
{
Jeroen Vreeken's avatar
Jeroen Vreeken committed
112
	memset(pkt, 0, sizeof(struct trace_pkt));
113
114
}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
115
116
117
118
119
120
121
122
123


#define PACKET_BUFLEN 256
#define PACKET_POOL_CHUNK 16

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

Jeroen Vreeken's avatar
Jeroen Vreeken committed
124
125
static pthread_mutex_t trace_pkt_mutex = PTHREAD_MUTEX_INITIALIZER;

126
127
struct trace_pkt *trace_packet_new(void)
{
Jeroen Vreeken's avatar
Jeroen Vreeken committed
128
	struct trace_pkt *pkt = NULL;
129
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
130
	pthread_mutex_lock(&trace_pkt_mutex);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
131
132
133
134
	if (packet_pool_cur) {
		pkt = packet_pool[packet_pool_cur-1];
		packet_pool_cur--;
	}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
135
	pthread_mutex_unlock(&trace_pkt_mutex);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
136
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
137
138
139
140
141
	if (!pkt) {
		pkt = calloc(1, sizeof(struct trace_pkt));
		pkt->data = malloc(PACKET_BUFLEN);
		pkt->buflen = PACKET_BUFLEN;
	}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
142
	
143
144
145
	return pkt;
}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
int trace_packet_resize(struct trace_pkt *pkt, size_t len)
{
	if (len <= pkt->buflen)
		return 0;
	
	pkt->data = realloc(pkt->data, len);
	pkt->buflen = len;

	return 0;
}

void trace_packet_put(struct trace_pkt *pkt)
{
	if (pkt->buflen > PACKET_BUFLEN) {
		free(pkt->data);
		free(pkt);
		return;
	}
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
165
166
	pthread_mutex_lock(&trace_pkt_mutex);

Jeroen Vreeken's avatar
Jeroen Vreeken committed
167
168
169
170
171
172
173
174
	if (packet_pool_cur >= packet_pool_size) {
		packet_pool_size += PACKET_POOL_CHUNK;
		packet_pool = realloc(packet_pool,
		    sizeof(struct trace_pkt *) * packet_pool_size);
	}
	packet_pool[packet_pool_cur] = pkt;
	pkt->len = 0;
	packet_pool_cur++;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
175
176

	pthread_mutex_unlock(&trace_pkt_mutex);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
177
178
}

179
180
181
182
int trace_packet_list_add(struct trace_pkt *pkt, 
    char *name, enum trace_value_type type, char *unit)
{
	size_t entry_size = strlen(name)+1 + 1 + strlen(unit)+1;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
183
	unsigned char *entry;
184
185

	if (!pkt->len) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
186
		trace_packet_resize(pkt, 1);
187
188
		pkt->len = 1;
		pkt->data[0] = TRACE_PTYPE_LIST;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
189
	}
190
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
191
	trace_packet_resize(pkt, pkt->len + entry_size);
192
193
	entry = pkt->data + pkt->len;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
194
	strcpy((char *)entry, name);
195
196
197
198
199
	entry += strlen(name) + 1;

	*entry = type;
	entry++;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
200
	strcpy((char *)entry, unit);
201
202
203
204
205
206
	
	pkt->len += entry_size;
		
	return 0;
}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
207
208
209
int trace_packet_value_add(struct trace_pkt *pkt, struct trace_value *value, enum trace_value_type type)
{
	size_t entry_size;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
210
211
212
213
214
215
216
	struct {
		uint16_t u16;
	} __packed *entry16;
	struct {
		uint32_t u32;
	} __packed *entry32;
	unsigned char *entry;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236

	switch (type) {
		case TRACE_VALUE_TYPE_BOOL:
		case TRACE_VALUE_TYPE_UINT8:
		case TRACE_VALUE_TYPE_SINT8:
			entry_size = 1;
			break;
		case TRACE_VALUE_TYPE_SINT16:
		case TRACE_VALUE_TYPE_UINT16:
			entry_size = 2;
			break;
		case TRACE_VALUE_TYPE_UINT32:
		case TRACE_VALUE_TYPE_SINT32:
		case TRACE_VALUE_TYPE_FLOAT:
		default:
			entry_size = 4;
			break;
	}

	if (!pkt->len) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
237
		trace_packet_resize(pkt, 1);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
238
239
240
241
		pkt->len = 1;
		pkt->data[0] = TRACE_PTYPE_DATA;
	}
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
242
	trace_packet_resize(pkt, pkt->len + entry_size);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
243
244
245
246
247
248
249
250
251
252
	entry = pkt->data + pkt->len;

	switch (type) {
		case TRACE_VALUE_TYPE_BOOL:
		case TRACE_VALUE_TYPE_UINT8:
		case TRACE_VALUE_TYPE_SINT8:
			*entry = value->value.u8;
			break;
		case TRACE_VALUE_TYPE_SINT16:
		case TRACE_VALUE_TYPE_UINT16: {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
253
			entry16 = (void *)entry;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
254
			
Jeroen Vreeken's avatar
Jeroen Vreeken committed
255
			entry16->u16 = htobe16(value->value.u16);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
256
257
258
259
260
261
262
			
			break;
		}
		case TRACE_VALUE_TYPE_UINT32:
		case TRACE_VALUE_TYPE_SINT32:
		case TRACE_VALUE_TYPE_FLOAT:
		default: {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
263
			entry32 = (void *)entry;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
264
			
Jeroen Vreeken's avatar
Jeroen Vreeken committed
265
			entry32->u32 = htobe32(value->value.u32);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
266
267
268
269
270
271
272
273
274
275
276
			
			break;
		}
	}
	
	
	pkt->len += entry_size;
		
	return 0;
}

277
278
279
280
281
282
283
int trace_packet_interval_set(struct trace_pkt *pkt, 
    struct timespec *interval, enum trace_interval_type type)
{
	struct trace_ptype_interval *pinterval;
	
	if (pkt->len < sizeof(struct trace_ptype_interval) + sizeof(struct trace_header)) {
		pkt->len = sizeof(struct trace_ptype_interval) + sizeof(struct trace_header);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
284
		trace_packet_resize(pkt, pkt->len);
285
		pkt->data[0] = TRACE_PTYPE_INTERVAL;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
286
	}
287
288
289
290
291
292
	pinterval = (struct trace_ptype_interval *)&pkt->data[1];
	
	pinterval->sec = htobe64(interval->tv_sec);
	pinterval->nsec = htobe32(interval->tv_nsec);
	pinterval->type = type;
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
293
294
295
	return 0;
}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
296
297
298
299
300
301
302
int trace_packet_timestamp_set(struct trace_pkt *pkt, 
    struct timespec *timestamp)
{
	struct trace_ptype_timestamp *ptimestamp;
	
	if (pkt->len < sizeof(struct trace_ptype_timestamp) + sizeof(struct trace_header)) {
		pkt->len = sizeof(struct trace_ptype_timestamp) + sizeof(struct trace_header);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
303
		trace_packet_resize(pkt, pkt->len);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
304
305
306
307
308
309
310
311
312
313
		pkt->data[0] = TRACE_PTYPE_TIMESTAMP;
	}
	ptimestamp = (struct trace_ptype_timestamp *)&pkt->data[1];
	
	ptimestamp->sec = htobe64(timestamp->tv_sec);
	ptimestamp->nsec = htobe32(timestamp->tv_nsec);
	
	return 0;
}

314
int trace_packet_name_set(struct trace_pkt *pkt, char *name)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
315
{
316
317
	if (pkt->len < sizeof(struct trace_header) + strlen(name) + 1) {
		pkt->len = strlen(name) + 1 + sizeof(struct trace_header);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
318
		trace_packet_resize(pkt, pkt->len);
319
320
		pkt->data[0] = TRACE_PTYPE_NAME;
	}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
321
	strcpy((char *)pkt->data + 1, name);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
322
323
324
325
	
	return 0;
}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
326
327
328
329
330
331
int trace_packet_type_set(struct trace_pkt *pkt, enum trace_value_type type)
{
	struct trace_ptype_value_type *vt;

	if (pkt->len < sizeof(struct trace_header) + sizeof(struct trace_ptype_value_type)) {
		pkt->len = sizeof(struct trace_header) + sizeof(struct trace_ptype_value_type);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
332
		trace_packet_resize(pkt, pkt->len);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
333
334
335
336
337
338
339
340
341
		pkt->data[0] = TRACE_PTYPE_VALUE_TYPE;
	}
	vt = (void *)pkt->data + sizeof(struct trace_header);
	
	vt->type = type;
		
	return 0;
}

342
bool trace_fd_set(struct trace *trace, fd_set *set, int *high)
343
{
Jeroen Vreeken's avatar
Jeroen Vreeken committed
344
345
	if (trace->fd < 0) {
		if (!trace->recover)
346
			return false;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
347
348

		if (trace->recovertime >= time(NULL))
349
			return false;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
350
351
		trace->recovertime = time(NULL);

352
353
		log_send(LOG_T_DEBUG, "Attempt to recover trace %s", 
		    trace->name);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
354
355
356
357
358
359
360
361
362
363
364
		trace->fd = tcp_connect(trace->host, trace->port);
		if (trace->fd >= 0) {
			struct trace_pkt *pkt;
		
			ioctl(trace->fd, FIONBIO, &(int){1});

			if (trace->name) {
				pkt = trace_packet_new();
				trace_packet_name_set(pkt, trace->name);
				trace_packet_write(trace, pkt);
				trace_packet_put(pkt);
365
366
			}
			if (trace->interval_set) {
367
368
				pkt = trace_packet_new();
				trace_packet_interval_set(pkt, 
369
370
				    &trace->interval_req, 
				    trace->interval_type_req);
371
372
				trace_packet_write(trace, pkt);
				trace_packet_put(pkt);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
373
374
375
376
			}
		}
		if (trace->fd < 0) {
			log_send(LOG_T_DEBUG, "Failed to recover");
377
			return false;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
378
		}
379
		log_send(LOG_T_DEBUG, "Reconnected trace");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
380
381
	}

382
383
384
385
	FD_SET(trace->fd, set);

	if (trace->fd > *high)
		*high = trace->fd;
386
387
	
	return true;
388
389
390
391
392
393
394
395
396
}

int trace_handle(struct trace *trace, fd_set *set)
{
	if (FD_ISSET(trace->fd, set)) {
		while (trace_fd_read(trace)) {
			struct trace_pkt *pkt;
			
			pkt = trace_packet_get(trace);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
397
398
			if (!pkt)
				break;
399
400
401
402
403
404
405
			switch (pkt->data[0]) {
				case TRACE_PTYPE_INTERVAL: {
					struct trace_ptype_interval *pinterval;
					enum trace_interval_type type;
					
					pinterval = (void*)pkt->data + 1;
					
Jeroen Vreeken's avatar
Jeroen Vreeken committed
406
407
					trace->interval.tv_sec = be64toh(pinterval->sec);
					trace->interval.tv_nsec = be32toh(pinterval->nsec);
408
					type = pinterval->type;
409
					trace->interval_type = type;
410
411
					
					if (trace->handler_interval) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
412
413
414
415
416
417
						trace->handler_interval(trace, &trace->interval, type);
					}
					break;
				}
				case TRACE_PTYPE_TIMESTAMP: {
					struct trace_ptype_timestamp *ptimestamp;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
418
										
Jeroen Vreeken's avatar
Jeroen Vreeken committed
419
420
421
422
423
424
425
					ptimestamp = (void*)pkt->data + 1;
					
					trace->timestamp.tv_sec = be64toh(ptimestamp->sec);
					trace->timestamp.tv_nsec = be32toh(ptimestamp->nsec);
					
					if (trace->handler_timestamp) {
						trace->handler_timestamp(trace, &trace->timestamp);
426
427
428
429
					}
					break;
				}
				case TRACE_PTYPE_LIST: {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
430
					unsigned char *listentry;
431
432
433
434
435
436
437
438
439
					size_t pos = 1;
					
					while (pos < pkt->len) {
						char *name;
						enum trace_value_type type;
						char *unit;
						
						listentry = pkt->data + pos;
						
Jeroen Vreeken's avatar
Jeroen Vreeken committed
440
						name = (char *)listentry;
441
						type = listentry[strlen(name) + 1];
Jeroen Vreeken's avatar
Jeroen Vreeken committed
442
						unit = (char *)listentry+strlen(name) + 2;
443
444
445
446
447
448
449
						
						if (trace->handler_list_entry) {
							trace->handler_list_entry(trace,
							    name, type, unit);
						}
						pos += strlen(name) + strlen(unit) + 3;
					}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
450
451
452
453
454
455
456
457
458
459
460
					trace->list_received = true;
					break;
				}
				case TRACE_PTYPE_VALUE_TYPE: {
					struct trace_ptype_value_type *vt;
					enum trace_value_type type;
					
					vt = (void*)pkt->data + 1;
					type = vt->type;
					
					trace->type = type;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
461
					trace->type_set = type;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
462
463
464
465
466
467
468
469
470
471
472
					if (trace->handler_type) {
						trace->handler_type(trace, type);
					}
					break;
				}
				case TRACE_PTYPE_NAME: {
					char *name;
					
					name = (void*)pkt->data + 1;
					
					trace->name_set = true;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
473
					trace->name = strdup(name);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489

					if (trace->handler_name) {
						trace->handler_name(trace, name);
					}
					break;
				}
				case TRACE_PTYPE_DATA: {
					size_t pos = 1;
					struct trace_ptype_value *v;
					
					while (pos < pkt->len) {
						v = (void *)pkt->data + pos;
						switch (trace->type) {
							case TRACE_VALUE_TYPE_FLOAT:
							case TRACE_VALUE_TYPE_UINT32:
							case TRACE_VALUE_TYPE_SINT32:
Jeroen Vreeken's avatar
Jeroen Vreeken committed
490
								trace->value.value.u32 = be32toh(v->u.u32);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
491
492
493
494
								pos+=4;
								break;
							case TRACE_VALUE_TYPE_UINT16:
							case TRACE_VALUE_TYPE_SINT16:
Jeroen Vreeken's avatar
Jeroen Vreeken committed
495
								trace->value.value.u16 = be32toh(v->u.u16);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
496
497
498
499
500
								pos+=2;
								break;
							case TRACE_VALUE_TYPE_BOOL:
							case TRACE_VALUE_TYPE_UINT8:
							case TRACE_VALUE_TYPE_SINT8:
Jeroen Vreeken's avatar
Jeroen Vreeken committed
501
								trace->value.value.u8 = v->u.u8;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
502
503
504
								pos++;
								break;
						}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
505

Jeroen Vreeken's avatar
Jeroen Vreeken committed
506
507
508
509
510
						if (timespec_older(&trace->value.t, &trace->timestamp)) {
							trace->value.t = trace->timestamp;
						} else {
							timespec_add(&trace->value.t, &trace->interval);
						}
511
						trace->value_set = true;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
512
513
514
515
516
						if (trace->handler_value) {
							trace->handler_value(trace, &trace->value);
						}
					}
					break;
517
518
519
520
				}
			}
			trace_packet_put(pkt);
		}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
521
		return 0;
522
523
	}

524
	return 0;
525
526
527
528
529
530
531
}

enum trace_state trace_state_get(struct trace *trace)
{
	if (trace->fd < 0)
		return TRACE_STATE_DISCONNECTED;
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
532
533
534
	if (trace->list_received)
		return TRACE_STATE_READY;
	
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
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
	return TRACE_STATE_CONNECTED;
}


char *enum_trace_value_type2str(enum trace_value_type type)
{
	switch(type) {
		case TRACE_VALUE_TYPE_FLOAT:
			return "FLOAT";
		case TRACE_VALUE_TYPE_BOOL:
			return "BOOL";
		case TRACE_VALUE_TYPE_UINT8:
			return "UINT8";
		case TRACE_VALUE_TYPE_UINT16:
			return "UINT16";
		case TRACE_VALUE_TYPE_UINT32:
			return "UINT32";
		case TRACE_VALUE_TYPE_SINT8:
			return "SINT8";
		case TRACE_VALUE_TYPE_SINT16:
			return "SINT16";
		case TRACE_VALUE_TYPE_SINT32:
			return "SINT32";
		default:
			return "unknown";
	}
}


char *enum_trace_interval_type2str(enum trace_interval_type type)
{
	switch(type) {
		case TRACE_INTERVAL_TYPE_INTERVAL:
			return "INTERVAL";
		case TRACE_INTERVAL_TYPE_CHANGED:
			return "CHANGED";
		case TRACE_INTERVAL_TYPE_CHANGED_INTERVAL:
			return "CHANGED_INTERVAL";
		default:
			return "unknown";
	}
}


char *enum_trace_state2str(enum trace_state state)
{
	switch(state) {
		case TRACE_STATE_DISCONNECTED:
			return "DISCONECTED";
		case TRACE_STATE_CONNECTED:
			return "CONNECTED";
		case TRACE_STATE_READY:
			return "READY";
		case TRACE_STATE_RECEIVING:
			return "RECEIVING";
		default:
			return "unknown";
	}
}