Commit 1add0517 authored by Jeroen Vreeken's avatar Jeroen Vreeken
Browse files

move parameter related stuff from controller_block.c to controller_block_param.c

parent 55846590
...@@ -45,6 +45,7 @@ BLOCKSRCS= \ ...@@ -45,6 +45,7 @@ BLOCKSRCS= \
CONTROLLERSRCS= \ CONTROLLERSRCS= \
controller_block.c \ controller_block.c \
controller_block_param.c \
controller_setpoint_command.c \ controller_setpoint_command.c \
controller_trace.c \ controller_trace.c \
controller_sample.c \ controller_sample.c \
......
...@@ -203,7 +203,6 @@ void controller_block_context_set(char *new_context) ...@@ -203,7 +203,6 @@ void controller_block_context_set(char *new_context)
Sample processing Sample processing
*/ */
static int sample_running = 0;
int controller_block_sample_init(void) int controller_block_sample_init(void)
{ {
...@@ -307,98 +306,10 @@ int controller_block_sample_init(void) ...@@ -307,98 +306,10 @@ int controller_block_sample_init(void)
} }
log_send(LOG_T_DEBUG, "Found %d calculate functions in %d blocks", log_send(LOG_T_DEBUG, "Found %d calculate functions in %d blocks",
nr_calculates, nr_blocks); nr_calculates, nr_blocks);
sample_running = 1;
return 0; return 0;
} }
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 *block, char *param, va_list value)
{
int i, j;
for (i = 0; i < nr_blocks; i++) {
if (strcmp(blocks[i]->name, block))
continue;
for (j = 0; j < blocks[i]->params; j++) {
if (strcmp(blocks[i]->param[j].name, param))
continue;
if (!sample_running ||
controller_sample_context() ||
!blocks[i]->param[j].sample) {
blocks[i]->param_set(
blocks[i], j, value);
return 0;
}
pthread_mutex_lock(&param_lock);
param_nr = j;
va_copy(param_set_value, value);
param_block = blocks[i];
param_set_func = blocks[i]->param_set;
sem_wait(&param_sync_sem);
pthread_mutex_unlock(&param_lock);
return 0;
}
}
return -1;
}
int controller_block_param_get(char *block, char *param, void *value)
{
int i, j;
for (i = 0; i < nr_blocks; i++) {
if (strcmp(blocks[i]->name, block))
continue;
for (j = 0; j < blocks[i]->params; j++) {
if (strcmp(blocks[i]->param[j].name, param))
continue;
if (!sample_running ||
controller_sample_context() ||
!blocks[i]->param[j].sample) {
blocks[i]->param_get(
blocks[i], j, value);
return 0;
}
pthread_mutex_lock(&param_lock);
param_nr = j;
param_value = value;
param_block = blocks[i];
param_get_func = blocks[i]->param_get;
sem_wait(&param_sync_sem);
pthread_mutex_unlock(&param_lock);
return 0;
}
}
return -1;
}
int controller_block_output_get_float(char *block, char *param, float *value) int controller_block_output_get_float(char *block, char *param, float *value)
{ {
int i, j, ret = -1; int i, j, ret = -1;
...@@ -657,20 +568,6 @@ void controller_block_calculate(void) ...@@ -657,20 +568,6 @@ void controller_block_calculate(void)
} }
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);
}
}
void controller_block_trace(void) void controller_block_trace(void)
{ {
...@@ -743,36 +640,6 @@ void controller_block_trace(void) ...@@ -743,36 +640,6 @@ void controller_block_trace(void)
} }
} }
/* 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;
}
int controller_block_interm_list_init(struct controller_block *block, int controller_block_interm_list_init(struct controller_block *block,
struct controller_block_interm_list *list) struct controller_block_interm_list *list)
......
...@@ -121,7 +121,7 @@ struct controller_block_outterm_list { ...@@ -121,7 +121,7 @@ struct controller_block_outterm_list {
/* List of params which must be initialized in the block's param pointer */ /* List of params which must be initialized in the block's param pointer */
struct controller_block_param_list { struct controller_block_param_list {
char *name; char *name;
bool sample; bool sample; /* True: all access to params must be 'sample safe' */
}; };
/* Starting point for a block. /* Starting point for a block.
......
/*
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>
#include <malloc.h>
#include "controller_block.h"
#include "controller_sample.h"
#include "log.h"
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;
}
...@@ -41,6 +41,7 @@ static int controller_sampleoverruns = 0; ...@@ -41,6 +41,7 @@ static int controller_sampleoverruns = 0;
static double controller_frequency = 1.0; static double controller_frequency = 1.0;
static pthread_t controller_sample_thread; static pthread_t controller_sample_thread;
static bool controller_sample_thread_running = false;
struct sample_timing { struct sample_timing {
double min; double min;
...@@ -137,6 +138,7 @@ static void *sample_thread(void *arg) ...@@ -137,6 +138,7 @@ static void *sample_thread(void *arg)
param.sched_priority = 99; param.sched_priority = 99;
log_send(LOG_T_DEBUG, "Starting sample thread"); log_send(LOG_T_DEBUG, "Starting sample thread");
controller_sample_thread_running = true;
if (sched_setscheduler(0, SCHED_FIFO, &param) == -1) { if (sched_setscheduler(0, SCHED_FIFO, &param) == -1) {
log_send(LOG_T_WARNING, "sched_setscheduler() failed: %s", log_send(LOG_T_WARNING, "sched_setscheduler() failed: %s",
...@@ -197,6 +199,11 @@ bool controller_sample_context(void) ...@@ -197,6 +199,11 @@ bool controller_sample_context(void)
return pthread_equal(controller_sample_thread, pthread_self()); return pthread_equal(controller_sample_thread, pthread_self());
} }
bool controller_sample_running(void)
{
return false;
}
static void *sample_monitor(void *arg) static void *sample_monitor(void *arg)
{ {
......
...@@ -31,6 +31,7 @@ double controller_sample_period(void); ...@@ -31,6 +31,7 @@ double controller_sample_period(void);
void controller_sample_frequency_set(double frequency); void controller_sample_frequency_set(double frequency);
bool controller_sample_context(void); bool controller_sample_context(void);
bool controller_sample_running(void);
int controller_sample_shell_add(void); int controller_sample_shell_add(void);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment