diff options
Diffstat (limited to 'libarmep/service.c')
-rw-r--r-- | libarmep/service.c | 135 |
1 files changed, 40 insertions, 95 deletions
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++; -} |