controller_block_param.c 4.14 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
/*
	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_param.c
	
	This module contains functions for handling block parameters
	-Wrappers for safe set/get while a sample thread might be running.
	-Helper which can be used when creating a new block
 */

#include <string.h>
28
#include <stdlib.h>
29

Jeroen Vreeken's avatar
Jeroen Vreeken committed
30
31
32
#include <controller/controller_block.h>
#include <controller/controller_sample.h>
#include <log/log.h>
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

static pthread_mutex_t param_lock;
static sem_t param_sync_sem;
static void (*param_set_func)(struct controller_block *, int, va_list) = NULL;
static void (*param_get_func)(struct controller_block *, int, void *) = NULL;
static int param_nr;
static void *param_value;
static va_list param_set_value;
static struct controller_block *param_block;

void controller_block_param_init(void)
{
	pthread_mutex_init(&param_lock, NULL);
	sem_init(&param_sync_sem, 0, 0);
}

int controller_block_param_set(char *blockname, char *param, va_list value)
{
	int i, j;
	int nr_blocks = controller_block_nr();
	struct controller_block *block;
	
	for (i = 0; i < nr_blocks; i++) {
		block = controller_block_get(i);
	
		if (strcmp(block->name, blockname))
			continue;
			
		for (j = 0; j < block->params; j++) {
			if (strcmp(block->param[j].name, param))
				continue;
			
			if (!controller_sample_running() ||
			    controller_sample_context() ||
			    !block->param[j].sample) {
				block->param_set(
				    block, j, value);
				return 0;
			}
				
			pthread_mutex_lock(&param_lock);

			param_nr = j;
			va_copy(param_set_value, value);
			param_block = block;
			param_set_func = block->param_set;
				
			sem_wait(&param_sync_sem);

			pthread_mutex_unlock(&param_lock);
			return 0;
		}
	}
	return -1;
}

int controller_block_param_get(char *blockname, char *param, void *value)
{
	int i, j;
	int nr_blocks = controller_block_nr();
	struct controller_block *block;
	
	for (i = 0; i < nr_blocks; i++) {
		block = controller_block_get(i);

		if (strcmp(block->name, blockname))
			continue;

		for (j = 0; j < block->params; j++) {
			if (strcmp(block->param[j].name, param))
				continue;
					
			if (!controller_sample_running() ||
			    controller_sample_context() ||
			    !block->param[j].sample) {
				block->param_get(
				    block, j, value);
				return 0;
			}

			pthread_mutex_lock(&param_lock);
					
			param_nr = j;
			param_value = value;
			param_block = block;
			param_get_func = block->param_get;
				
			sem_wait(&param_sync_sem);
					
			pthread_mutex_unlock(&param_lock);
			return 0;
		}
	}
	return -1;
}

void controller_block_param_handle(void)
{
	if (param_set_func) {
		param_set_func(param_block, param_nr, param_set_value);
		param_set_func = NULL;
		sem_post(&param_sync_sem);
	}
	if (param_get_func) {
		param_get_func(param_block, param_nr, param_value);
		param_get_func = NULL;
		sem_post(&param_sync_sem);
	}
}    

/* helper function to initialize param lists */
int controller_block_param_list_init(struct controller_block *block,
    struct controller_block_param_list *list)
{
	int i;
	
	for (i = 0; list[i].name; i++);
	block->params = i;
	block->param = malloc(sizeof(struct controller_block_param_list) * i);
	if (!block->param)
		return 1;
	
	for (i = 0; list[i].name; i++){
		block->param[i].name = malloc(strlen(list[i].name) + 1);
		if (!block->param[i].name)
			goto err_undo;
		strcpy(block->param[i].name, list[i].name);
		block->param[i].sample = list[i].sample;
	}
	
	return 0;

err_undo:
	for (; i > 0; i--) {
		free(block->param[i - 1].name);
	}
	free(block->param);
	
	return 1;
}