controller_block_trace.c 5.09 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
/*
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2013
	Copyright Stichting C.A. Muller Radioastronomiestation, 2013

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

 */
/*
	controller_block_trace.c
	
	This module takes care of tracing block outputs.
 */

25
#include <stdlib.h>
26
27
#include <string.h>

Jeroen Vreeken's avatar
Jeroen Vreeken committed
28
29
#include <controller/controller_block.h>
#include <log/log.h>
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

static int nr_traces = 0;
static struct controller_trace **traces;
static int max_traces = 0;

static pthread_mutex_t trace_lock;
static sem_t trace_sync_sem;
static sem_t trace_waitsem;
static struct controller_trace *trace_add_ptr = NULL;
static struct controller_trace *trace_del_ptr = NULL;

void controller_block_trace_init(int max)
{
	int i;
	
	traces = malloc(sizeof(struct controller_trace *) * max);
	max_traces = max;

	/* touch entire trace list */
	for (i = 0; i < max; i++) {
		traces[i] = NULL;
	}

	pthread_mutex_init(&trace_lock, NULL);
	sem_init(&trace_sync_sem, 0, 0);
	sem_init(&trace_waitsem, 0, 0);
}

int controller_block_trace_add(char *blockname, char *outterm, 
    struct controller_trace *trace)
{
	void *ptr = NULL;
	enum controller_block_term_type type = 0;
	int i, j;
	int ret = 0;
	int nr_blocks = controller_block_nr();
	
	for (i = 0; i < nr_blocks; i++) {
		struct controller_block *block;
		
		block = controller_block_get(i);
	
		if (strcmp(blockname, block->name))
			continue;
			
		for (j = 0; j < block->outputs; j++) {
			if (!strcmp(outterm, block->output[j].name)){
				ptr = block->output[j].value.v;
				type = block->output[j].type;
			}
		}
	}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
82
83
84
85
86
87
	if (!ptr) {
		log_send(LOG_T_ERROR, 
		    "Terminal %s of block %s not found, cannot add trace",
		    blockname, outterm);
		return -1;
	}
88

89
	trace->timestamp = controller_time_seconds;
90
91
92
93
94
95
96
	trace->rd_pos = 0;
	trace->wr_pos = 0;
	trace->type = type;
	trace->ptr = ptr;
	
	/* touch the entire buffer atleast once */
	for (i = 0; i < trace->len; i++) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
97
		trace->buffer[i].s32 = i;
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
	}

	pthread_mutex_lock(&trace_lock);

	if (nr_traces < max_traces) {
		trace_add_ptr = trace;

		log_send(LOG_T_DEBUG, "Addded %s.%s to trace list",
		    blockname, outterm);
	} else {
		ret = 1;
	}
	sem_wait(&trace_sync_sem);

	pthread_mutex_unlock(&trace_lock);

	return ret;
}

void controller_block_trace_del(struct controller_trace *trace)
{
	pthread_mutex_lock(&trace_lock);

	trace_del_ptr = trace;
	sem_wait(&trace_sync_sem);

	pthread_mutex_unlock(&trace_lock);

	return;
}

void controller_block_trace_wait(void)
{
	sem_wait(&trace_waitsem);
}

void controller_block_trace(void)
{
	int i;
	int semval;
	
	if (trace_add_ptr) {
		traces[nr_traces] = trace_add_ptr;
		nr_traces++;

		trace_add_ptr = NULL;
		sem_post(&trace_sync_sem);
	}
	if (trace_del_ptr) {
		for (i = 0; i < nr_traces; i++) {
			if (traces[i] == trace_del_ptr) {
				if (i + 1 < nr_traces) {
					memmove(&traces[i + 0],
					    &traces[i + 1],
					    sizeof(struct controller_trace *) *
					    (nr_traces - i - 1));
				}
				break;
			}
		}
		nr_traces--;
		trace_del_ptr = NULL;
		sem_post(&trace_sync_sem);
	}
	
	for (i = 0; i < nr_traces; i++) {
		traces[i]->wr_pos++;
		if (traces[i]->wr_pos == traces[i]->len)
			traces[i]->wr_pos = 0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
167
168
169
170
171
172
173
		if (controller_time_samplenr == 0)
		{
			traces[i]->timestamp_pos = traces[i]->wr_pos;
			traces[i]->timestamp = controller_time_seconds;
		} else if (traces[i]->timestamp_pos == traces[i]->wr_pos) {
			traces[i]->timestamp_pos = traces[i]->len;
		}
174
175
		switch (traces[i]->type) {
			case CONTROLLER_BLOCK_TERM_BOOL:
Jeroen Vreeken's avatar
Jeroen Vreeken committed
176
				traces[i]->buffer[traces[i]->wr_pos].b = 
177
178
179
				    *(bool*)traces[i]->ptr;
				break;
			case CONTROLLER_BLOCK_TERM_FLOAT:
Jeroen Vreeken's avatar
Jeroen Vreeken committed
180
				traces[i]->buffer[traces[i]->wr_pos].f = 
181
182
183
				    *(float*)traces[i]->ptr;
				break;
			case CONTROLLER_BLOCK_TERM_UINT32:
Jeroen Vreeken's avatar
Jeroen Vreeken committed
184
				traces[i]->buffer[traces[i]->wr_pos].u32 = 
185
186
				    *(uint32_t*)traces[i]->ptr;
				break;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
			case CONTROLLER_BLOCK_TERM_UINT16:
				traces[i]->buffer[traces[i]->wr_pos].u16 = 
				    *(uint16_t*)traces[i]->ptr;
				break;
			case CONTROLLER_BLOCK_TERM_UINT8:
				traces[i]->buffer[traces[i]->wr_pos].u8 = 
				    *(uint8_t*)traces[i]->ptr;
				break;
			case CONTROLLER_BLOCK_TERM_SINT32:
				traces[i]->buffer[traces[i]->wr_pos].s32 = 
				    *(int32_t*)traces[i]->ptr;
				break;
			case CONTROLLER_BLOCK_TERM_SINT16:
				traces[i]->buffer[traces[i]->wr_pos].s16 = 
				    *(int16_t*)traces[i]->ptr;
				break;
			case CONTROLLER_BLOCK_TERM_SINT8:
				traces[i]->buffer[traces[i]->wr_pos].s8 = 
				    *(int8_t*)traces[i]->ptr;
				break;
207
			default:
Jeroen Vreeken's avatar
Jeroen Vreeken committed
208
				traces[i]->buffer[traces[i]->wr_pos].s32 = -1;
209
210
211
212
213
214
215
216
217
218
		}
		
	}

	sem_getvalue(&trace_waitsem, &semval);
	if (!semval) {
		sem_post(&trace_waitsem);
	}
}