diff options
Diffstat (limited to 'libarmep')
-rw-r--r-- | libarmep/Makefile.am | 6 | ||||
-rw-r--r-- | libarmep/aepd-interface.c | 104 | ||||
-rw-r--r-- | libarmep/aepd-interface.h | 73 | ||||
-rw-r--r-- | libarmep/libarmep.h | 34 | ||||
-rw-r--r-- | libarmep/sample.c | 26 | ||||
-rw-r--r-- | libarmep/service.c | 135 |
6 files changed, 238 insertions, 140 deletions
diff --git a/libarmep/Makefile.am b/libarmep/Makefile.am index e1ed7fc..79dc801 100644 --- a/libarmep/Makefile.am +++ b/libarmep/Makefile.am @@ -1,12 +1,14 @@ lib_LTLIBRARIES=libarmep.la -include_HEADERS=libarmep.h +include_HEADERS=libarmep.h aepd-interface.h dist_libarmep_la_SOURCES=sample.c \ interpolation.c \ configuration.c \ avg-mean-us.c \ protocol.c \ service.c \ - libarmep.h + aepd-interface.c \ + libarmep.h \ + aepd-interface.h libarmep_la_CFLAGS=-Wall -Werror -std=gnu99 -pedantic -pthread -D_FORTIFY_SOURCE=2 -fstack-protector libarmep_la_LDFLAGS=-pthread diff --git a/libarmep/aepd-interface.c b/libarmep/aepd-interface.c new file mode 100644 index 0000000..e78b308 --- /dev/null +++ b/libarmep/aepd-interface.c @@ -0,0 +1,104 @@ +/* + * Author: Andy Green <andy.green@linaro.org> + * Copyright (C) 2012 Linaro, LTD + * + * 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 2 + * 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +#include <unistd.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <getopt.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <sys/time.h> +#include <semaphore.h> +#include <sys/mman.h> + +#include "aepd-interface.h" + +struct aepd_interface *aepd_interface_create(void) +{ + struct aepd_interface *aepd_interface; + int n; + + /* + * create our lump of shared memory + * which is usable by all forked threads + */ + + n = open("/dev/zero", O_RDWR); + aepd_interface = mmap(NULL, sizeof (struct aepd_interface), + PROT_READ | PROT_WRITE, MAP_SHARED, n, 0); + close(n); + + aepd_interface->head = 0; + aepd_interface->tail = 0; + + sprintf(aepd_interface->semname, "linaro.aep.%u\n", getpid()); + + aepd_interface->semaphore = sem_open(aepd_interface->semname, O_CREAT | O_RDWR, 0600, 0); + if (aepd_interface->semaphore == SEM_FAILED) { + fprintf(stderr, "Failed to open sem %s\n", aepd_interface->semname); + return NULL; + } + + return aepd_interface; +} + +void aepd_interface_destroy(struct aepd_interface *aepd_interface) +{ + sem_close(aepd_interface->semaphore); + if (munmap(aepd_interface, sizeof (struct aepd_interface)) < 0) + fprintf(stderr, "munmap failed\n"); +} + +/* + * helper for user code to block until next aep_result available + */ + +struct aepd_interface_result * aep_wait_for_next_result(struct aepd_interface *aepd_interface) +{ + struct timespec ts; + struct timeval tv; + + gettimeofday(&tv, NULL); + ts.tv_sec = tv.tv_sec + 2; + ts.tv_nsec = 0; + + if (sem_timedwait(aepd_interface->semaphore, &ts) < 0) + return NULL; + + if (aepd_interface->tail == aepd_interface->head) + return NULL; + + return &aepd_interface->aepd_interface_result[aepd_interface->tail]; +} + +/* + * helper for user code to deal with ringbuffer + */ + +void aep_free_result(struct aepd_interface *aepd_interface) +{ + if (aepd_interface->tail == sizeof(aepd_interface->aepd_interface_result) / sizeof(aepd_interface->aepd_interface_result[0]) - 1) + aepd_interface->tail = 0; + else + aepd_interface->tail++; +} + diff --git a/libarmep/aepd-interface.h b/libarmep/aepd-interface.h new file mode 100644 index 0000000..9b43352 --- /dev/null +++ b/libarmep/aepd-interface.h @@ -0,0 +1,73 @@ +/* + * Author: Andy Green <andy.green@linaro.org> + * Copyright (C) 2012 Linaro, LTD + * + * 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 2 + * 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + * + * These are the interface structs between the acquisition-specific + * code and the shared memory buffers, which are just abstracted + * sample and channel descriptions with no knowledge of what + * produced them + */ + +#define AEPD_SHARED_MAX_REAL_CHANNELS (8 * 3) +#define AEPD_SHARED_MAX_VIRTUAL_CHANNELS (8) + +struct aepd_interface_result { + int triggered; /* 0 = pretrigger 1 = triggered */ + int chans; /* number of channels below with data */ + double samtime; /* sample time in s */ + + /* For each channel: { common-mode voltage in V, current in A } */ + double buf[AEPD_SHARED_MAX_REAL_CHANNELS * 2]; +}; + +struct aepd_interface { + + /* ringbuffer to hold channel_results until we can deal with them */ + + int head; + int tail; + struct aepd_interface_result aepd_interface_result[1000]; + + /* synchronization object signalling new result written */ + sem_t * semaphore; + char semname[64]; + + /* metadata about the channels */ + + char channel_name[AEPD_SHARED_MAX_VIRTUAL_CHANNELS + AEPD_SHARED_MAX_REAL_CHANNELS][64]; + char channel_name_pretty[AEPD_SHARED_MAX_VIRTUAL_CHANNELS + AEPD_SHARED_MAX_REAL_CHANNELS][64]; + char supply[AEPD_SHARED_MAX_VIRTUAL_CHANNELS + AEPD_SHARED_MAX_REAL_CHANNELS][64]; + char colour[AEPD_SHARED_MAX_VIRTUAL_CHANNELS + AEPD_SHARED_MAX_REAL_CHANNELS][16]; + char class[AEPD_SHARED_MAX_VIRTUAL_CHANNELS + AEPD_SHARED_MAX_REAL_CHANNELS][16]; + int chans; /* number of active acquisition channels above */ + int vchans; /* virtual channels (summing supplies) appear after probed physical channels in the arrays */ + int finished; /* are we going down? */ + + /* simple stats on what was captured */ + + double averages[AEPD_SHARED_MAX_REAL_CHANNELS][3]; + double min[AEPD_SHARED_MAX_REAL_CHANNELS][3]; + double max[AEPD_SHARED_MAX_REAL_CHANNELS][3]; + +}; + +extern struct aepd_interface *aepd_interface_create(void); +extern void aepd_interface_destroy(struct aepd_interface *aepd_interface); +extern struct aepd_interface_result * aep_wait_for_next_result(struct aepd_interface *aepd_interface); +extern void aep_free_result(struct aepd_interface *aepd_interface); + diff --git a/libarmep/libarmep.h b/libarmep/libarmep.h index 8811fce..2247b46 100644 --- a/libarmep/libarmep.h +++ b/libarmep/libarmep.h @@ -36,6 +36,8 @@ #include <time.h> #include <semaphore.h> +#include "aepd-interface.h" + #define MAX_PROBES 8 #define CHANNELS_PER_PROBE 3 @@ -227,31 +229,6 @@ struct aep { struct aep_channel ch[CHANNELS_PER_PROBE]; }; -struct aep_result { - int triggered; - int chans; - double samtime; - double buf[MAX_PROBES * CHANNELS_PER_PROBE * 2]; - -}; - -struct aep_shared { - int head; - int tail; - struct aep_result aep_result[1000]; - char channel_name[MAX_VIRTUAL_CHANNELS + MAX_PROBES * CHANNELS_PER_PROBE][64]; - char channel_name_pretty[MAX_VIRTUAL_CHANNELS + MAX_PROBES * CHANNELS_PER_PROBE][64]; - char supply[MAX_VIRTUAL_CHANNELS + MAX_PROBES * CHANNELS_PER_PROBE][64]; - char colour[MAX_VIRTUAL_CHANNELS + MAX_PROBES * CHANNELS_PER_PROBE][16]; - char class[MAX_VIRTUAL_CHANNELS + MAX_PROBES * CHANNELS_PER_PROBE][16]; - int chans; - int vchans; /* appear after probed physical channels in the arrays*/ - int finished; - double averages[MAX_PROBES * CHANNELS_PER_PROBE][3]; - double min[MAX_PROBES * CHANNELS_PER_PROBE][3]; - double max[MAX_PROBES * CHANNELS_PER_PROBE][3]; - -}; struct aep_context { struct aep aeps[MAX_PROBES]; @@ -289,10 +266,9 @@ struct aep_context { int poll_timeout_ms; double do_average; - /* shared memory output buffer */ + /* shared memory output buffer + metadata */ - struct aep_shared *aep_shared; - sem_t * semaphore; + struct aepd_interface *aepd_interface; }; @@ -338,6 +314,4 @@ extern void init_interpolation(void); extern int service_aeps(struct aep_context *aep_context, int fd); extern int aep_init_and_fork(struct aep_context *aep_context, char *argv[]); -extern struct aep_result * aep_wait_for_next_result(struct aep_context *aep_context); -extern void aep_free_result(struct aep_context *aep_context); diff --git a/libarmep/sample.c b/libarmep/sample.c index f17a803..df6b278 100644 --- a/libarmep/sample.c +++ b/libarmep/sample.c @@ -90,8 +90,8 @@ int process_sample(struct aep *aep, int ch_index) struct aep_channel *ch = &aep->ch[ch_index], *ich; double v1, v2, v1a, v2a, vo0 = 0, vo1 = 0; int m, i, from_pretrig = 0, n, hit; - struct aep_shared *aep_shared; - struct aep_result *aep_result; + struct aepd_interface *aepd_interface; + struct aepd_interface_result *aepd_interface_result; if (!aep->aep_context->no_correction) { vo0 = ch->voffset[0]; @@ -302,12 +302,12 @@ unripe: /* if so, output it all together */ - aep_shared = aep->aep_context->aep_shared; - aep_result = &aep_shared->aep_result[aep_shared->head]; + aepd_interface = aep->aep_context->aepd_interface; + aepd_interface_result = &aepd_interface->aepd_interface_result[aepd_interface->head]; - aep_result->triggered = ch->triggered; - aep_result->chans = hit; - aep_result->samtime = ((double)ch->samples_seen - (double)ch->ring_samples) / 10000.0; + aepd_interface_result->triggered = ch->triggered; + aepd_interface_result->chans = hit; + aepd_interface_result->samtime = ((double)ch->samples_seen - (double)ch->ring_samples) / 10000.0; n = 0; @@ -332,17 +332,17 @@ unripe: if (!ich->requested) continue; - aep_result->buf[n++] = v1a; - aep_result->buf[n++] = v2a; + aepd_interface_result->buf[n++] = v1a; + aepd_interface_result->buf[n++] = v2a; } } - if (aep_shared->head == (sizeof(aep_shared->aep_result) / sizeof(aep_shared->aep_result[0])) - 1) - aep_shared->head = 0; + if (aepd_interface->head == (sizeof(aepd_interface->aepd_interface_result) / sizeof(aepd_interface->aepd_interface_result[0])) - 1) + aepd_interface->head = 0; else - aep_shared->head++; + aepd_interface->head++; - if (sem_post(aep->aep_context->semaphore) < 0) + if (sem_post(aep->aep_context->aepd_interface->semaphore) < 0) fprintf(stderr, "failed to set semaphore\n"); done: diff --git a/libarmep/service.c b/libarmep/service.c index 68a7028..d6d40b0 100644 --- a/libarmep/service.c +++ b/libarmep/service.c @@ -21,7 +21,6 @@ #include "libarmep.h" #include <linux/serial.h> -#include <sys/mman.h> void add_pollfd(struct aep_context *aep_context, int fd, int events) { @@ -150,18 +149,18 @@ void probe_close(struct aep *aep) close(aep->fd); } -static void copy_public_ch_info_to_shared(struct aep_shared *aep_shared, int chan, struct aep_channel *ch) +static void copy_public_ch_info_to_shared(struct aepd_interface *aepd_interface, int chan, struct aep_channel *ch) { - strncpy(aep_shared->channel_name[chan], ch->channel_name, sizeof(aep_shared->channel_name[0])); - aep_shared->channel_name[chan][sizeof(aep_shared->channel_name[0]) - 1] = '\0'; - strncpy(aep_shared->channel_name_pretty[chan], ch->channel_name_pretty, sizeof(aep_shared->channel_name_pretty[0])); - aep_shared->channel_name_pretty[chan][sizeof(aep_shared->channel_name_pretty[0]) - 1] = '\0'; - strncpy(aep_shared->supply[chan], ch->supply, sizeof(aep_shared->supply[0])); - aep_shared->supply[chan][sizeof(aep_shared->supply[0]) - 1] = '\0'; - strncpy(aep_shared->colour[chan], ch->colour, sizeof(aep_shared->colour[0])); - aep_shared->colour[chan][sizeof(aep_shared->colour[0]) - 1] = '\0'; - strncpy(aep_shared->class[chan], ch->class, sizeof(aep_shared->class[0])); - aep_shared->class[chan][sizeof(aep_shared->class[0]) - 1] = '\0'; + strncpy(aepd_interface->channel_name[chan], ch->channel_name, sizeof(aepd_interface->channel_name[0])); + aepd_interface->channel_name[chan][sizeof(aepd_interface->channel_name[0]) - 1] = '\0'; + strncpy(aepd_interface->channel_name_pretty[chan], ch->channel_name_pretty, sizeof(aepd_interface->channel_name_pretty[0])); + aepd_interface->channel_name_pretty[chan][sizeof(aepd_interface->channel_name_pretty[0]) - 1] = '\0'; + strncpy(aepd_interface->supply[chan], ch->supply, sizeof(aepd_interface->supply[0])); + aepd_interface->supply[chan][sizeof(aepd_interface->supply[0]) - 1] = '\0'; + strncpy(aepd_interface->colour[chan], ch->colour, sizeof(aepd_interface->colour[0])); + aepd_interface->colour[chan][sizeof(aepd_interface->colour[0]) - 1] = '\0'; + strncpy(aepd_interface->class[chan], ch->class, sizeof(aepd_interface->class[0])); + aepd_interface->class[chan][sizeof(aepd_interface->class[0]) - 1] = '\0'; } @@ -374,10 +373,10 @@ bail: if (!ch->requested) continue; - copy_public_ch_info_to_shared(aep_context->aep_shared, chan, ch); + copy_public_ch_info_to_shared(aep_context->aepd_interface, chan, ch); chan++; - aep_context->aep_shared->chans = chan; + aep_context->aepd_interface->chans = chan; } aep_context->aeps[m].started = 1; @@ -386,8 +385,8 @@ bail: } for (m = 0; m < aep_context->count_virtual_channels; m++) { - copy_public_ch_info_to_shared(aep_context->aep_shared, chan++, &aep_context->vch[m]); - aep_context->aep_shared->vchans++; + copy_public_ch_info_to_shared(aep_context->aepd_interface, chan++, &aep_context->vch[m]); + aep_context->aepd_interface->vchans++; } } @@ -530,7 +529,7 @@ service: if (aep_context->scans > (5 * MAX_PROBES) && aep_context->awaiting_capture == 0 && aep_context->exit_after_capture) { fprintf(stderr, "done all capture\n"); - aep_context->aep_shared->finished = 1; + aep_context->aepd_interface->finished = 1; return -1; } @@ -556,37 +555,21 @@ void sighandler(int sig) } +/* + * you should have called aepd_interface_create() above + * and set aep_context->aepd_interface to the result + * before calling this + */ + int aep_init_and_fork(struct aep_context *aep_context, char *argv[]) { int n, m, i; struct aep_channel *ch; - char semname[64]; loop = 1; init_interpolation(); - /* - * create our lump of shared memory - * which is usable by all forked threads - */ - - n = open("/dev/zero", O_RDWR); - aep_context->aep_shared = mmap(NULL, sizeof (struct aep_shared), - PROT_READ | PROT_WRITE, MAP_SHARED, n, 0); - close(n); - - aep_context->aep_shared->head = 0; - aep_context->aep_shared->tail = 0; - - sprintf(semname, "linaro.aep.%u\n", getpid()); - - aep_context->semaphore = sem_open(semname, O_CREAT | O_RDWR, 0600, 0); - if (aep_context->semaphore == SEM_FAILED) { - fprintf(stderr, "Failed to open sem %s\n", semname); - return -1; - } - /* fork off aep service loop */ n = fork(); @@ -603,31 +586,31 @@ int aep_init_and_fork(struct aep_context *aep_context, char *argv[]) if (argv) strcpy(argv[0] + strlen(argv[0]), " - AEP server"); - aep_context->semaphore = sem_open(semname, O_RDWR, 0600, 0); - if (aep_context->semaphore == SEM_FAILED) { - fprintf(stderr, "Child failed to open sem %s\n", semname); + aep_context->aepd_interface->semaphore = sem_open(aep_context->aepd_interface->semname, O_RDWR, 0600, 0); + if (aep_context->aepd_interface->semaphore == SEM_FAILED) { + fprintf(stderr, "Child failed to open sem %s\n", aep_context->aepd_interface->semname); return -1; } - aep_context->aep_shared->finished = 0; + aep_context->aepd_interface->finished = 0; aep_context->poll_timeout_ms = 10; - while (!aep_context->aep_shared->finished && loop && getppid() != 1) { + while (!aep_context->aepd_interface->finished && loop && getppid() != 1) { n = poll(aep_context->pollfds, aep_context->count_pollfds, aep_context->poll_timeout_ms); if (n < 0) - aep_context->aep_shared->finished = 1; + aep_context->aepd_interface->finished = 1; for (n = 0; n < aep_context->count_pollfds; n++) if (aep_context->pollfds[n].revents) if (service_aeps(aep_context, aep_context->pollfds[n].fd) < 0) - aep_context->aep_shared->finished = 1; + aep_context->aepd_interface->finished = 1; if (service_aeps(aep_context, -1) < 0) - aep_context->aep_shared->finished = 1; + aep_context->aepd_interface->finished = 1; } - sem_close(aep_context->semaphore); + sem_close(aep_context->aepd_interface->semaphore); /* * Compute and stash the averages @@ -645,15 +628,15 @@ int aep_init_and_fork(struct aep_context *aep_context, char *argv[]) aep_context->config_filepath, ch) < 0) fprintf(stderr, "failed to update config\n"); - aep_context->aep_shared->averages[m][0] = ch->simple_avg[0] / ch->avg_count; - aep_context->aep_shared->averages[m][1] = ch->simple_avg[1] / ch->avg_count; - aep_context->aep_shared->averages[m][2] = ch->simple_avg[2] / ch->avg_count; - aep_context->aep_shared->min[m][0] = ch->min[0]; - aep_context->aep_shared->min[m][1] = ch->min[1]; - aep_context->aep_shared->min[m][2] = ch->min[2]; - aep_context->aep_shared->max[m][0] = ch->max[0]; - aep_context->aep_shared->max[m][1] = ch->max[1]; - aep_context->aep_shared->max[m][2] = ch->max[2]; + aep_context->aepd_interface->averages[m][0] = ch->simple_avg[0] / ch->avg_count; + aep_context->aepd_interface->averages[m][1] = ch->simple_avg[1] / ch->avg_count; + aep_context->aepd_interface->averages[m][2] = ch->simple_avg[2] / ch->avg_count; + aep_context->aepd_interface->min[m][0] = ch->min[0]; + aep_context->aepd_interface->min[m][1] = ch->min[1]; + aep_context->aepd_interface->min[m][2] = ch->min[2]; + aep_context->aepd_interface->max[m][0] = ch->max[0]; + aep_context->aepd_interface->max[m][1] = ch->max[1]; + aep_context->aepd_interface->max[m][2] = ch->max[2]; m++; } @@ -663,43 +646,5 @@ int aep_init_and_fork(struct aep_context *aep_context, char *argv[]) return 0; } -/* - * helper for user code to block until next aep_result available - */ - -struct aep_result * aep_wait_for_next_result(struct aep_context *aep_context) -{ - struct timespec ts; - struct timeval tv; - struct aep_shared *aep_shared = aep_context->aep_shared; - - gettimeofday(&tv, NULL); - ts.tv_sec = tv.tv_sec + 2; - ts.tv_nsec = 0; - - if (sem_timedwait(aep_context->semaphore, &ts) < 0) - return NULL; - - if (aep_shared->tail == aep_shared->head) - return NULL; - -// fprintf(stderr, "%d %d, ", aep_shared->tail, aep_shared->head); - - return &aep_shared->aep_result[aep_shared->tail]; -} - -/* - * helper for user code to deal with ringbuffer - */ - -void aep_free_result(struct aep_context *aep_context) -{ - struct aep_shared *aep_shared = aep_context->aep_shared; - - if (aep_shared->tail == sizeof(aep_shared->aep_result) / sizeof(aep_shared->aep_result[0]) - 1) - aep_shared->tail = 0; - else - aep_shared->tail++; -} |