aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--arm-probe/arm-probe.c50
-rw-r--r--libarmep/libarmep.h6
-rw-r--r--libarmep/sample.c86
3 files changed, 78 insertions, 64 deletions
diff --git a/arm-probe/arm-probe.c b/arm-probe/arm-probe.c
index 1d8c6c7..b730d40 100644
--- a/arm-probe/arm-probe.c
+++ b/arm-probe/arm-probe.c
@@ -81,6 +81,28 @@ int get_stdin_line(char *buf, int maxlen)
return -1;
}
+void add_pollfd(struct aep_context *aep_context, int fd, int events)
+{
+ aep_context->pollfds[aep_context->count_pollfds].fd = fd;
+ aep_context->pollfds[aep_context->count_pollfds].events = events;
+ aep_context->pollfds[aep_context->count_pollfds++].revents = 0;
+}
+
+void remove_pollfd(struct aep_context *aep_context, int fd)
+{
+ int n;
+
+ for (n = 0; n < aep_context->count_pollfds; n++)
+ if (aep_context->pollfds[n].fd == fd) {
+ while (n < aep_context->count_pollfds) {
+ aep_context->pollfds[n] = aep_context->pollfds[n + 1];
+ n++;
+ }
+ aep_context->count_pollfds--;
+ }
+}
+
+
/*
* this is the callback where the results appear
*/
@@ -94,6 +116,16 @@ static void issue_samples(enum samples_actions sa, struct aep_channel *ch,
switch (sa) {
+ case AEPSA_ADD_POLLFD:
+ fprintf(stderr, "adding pollfd %d %d\n", (int)voltage, (int)current);
+ add_pollfd(&aep_context, (int)voltage, (int)current);
+ break;
+
+ case AEPSA_REMOVE_POLLFD:
+ fprintf(stderr, "remove pollfd %d\n", (int)voltage);
+ remove_pollfd(&aep_context, (int)voltage);
+ break;
+
case AEPSA_DATA_STARTING:
printf("#\n");
pos = 0;
@@ -411,10 +443,24 @@ int main(int argc, char *argv[])
* the main service loop
*/
- while (loop)
- if (service_aeps(&aep_context) < 0)
+ aep_context.poll_timeout_ms = 10;
+
+ while (loop) {
+
+ n = poll(aep_context.pollfds, aep_context.count_pollfds, aep_context.poll_timeout_ms);
+ if (n < 0)
+ return -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)
+ loop = 0;
+
+ if (service_aeps(&aep_context, -1) < 0)
loop = 0;
+ }
+
/*
* we are finished if we reach here...
* append simple average to results if requested
diff --git a/libarmep/libarmep.h b/libarmep/libarmep.h
index ad59f01..742cb0b 100644
--- a/libarmep/libarmep.h
+++ b/libarmep/libarmep.h
@@ -78,6 +78,8 @@ enum samples_actions {
AEPSA_DATA_STARTING,
AEPSA_DATA_STARTING_CHANNELS,
AEPSA_DATA_STARTING_DONE,
+ AEPSA_ADD_POLLFD,
+ AEPSA_REMOVE_POLLFD
};
struct aep_channel;
@@ -251,6 +253,7 @@ struct aep_context {
struct pollfd pollfds[MAX_PROBES];
int count_pollfds;
int exit_after_capture;
+ int poll_timeout_ms;
};
@@ -283,10 +286,9 @@ extern double correct(int no_correction, double common_mode, double in, struct i
extern int get_stdin_line(char *buf, int maxlen);
extern int process_sample(struct aep *aep, int ch_index);
-extern int service_aep(struct aep *aep, int samples);
extern int configure(struct aep_context *aep_context, struct aep *aep, const char *dev_filepath,
const char *config_filepath, struct aep_channel *wch);
extern char configuration_name[64];
-extern int service_aeps(struct aep_context *aep_context);
+extern int service_aeps(struct aep_context *aep_context, int fd);
extern void probe_close(struct aep *aep);
diff --git a/libarmep/sample.c b/libarmep/sample.c
index def49f3..63fccf0 100644
--- a/libarmep/sample.c
+++ b/libarmep/sample.c
@@ -837,30 +837,7 @@ void probe_close(struct aep *aep)
close(aep->fd);
}
-
-void add_pollfd(struct aep_context *aep_context, int fd, int events)
-{
- aep_context->pollfds[aep_context->count_pollfds].fd = fd;
- aep_context->pollfds[aep_context->count_pollfds].events = events;
- aep_context->pollfds[aep_context->count_pollfds++].revents = 0;
-}
-
-void remove_pollfd(struct aep_context *aep_context, int fd)
-{
- int n;
-
- for (n = 0; n < aep_context->count_pollfds; n++)
- if (aep_context->pollfds[n].fd == fd) {
- while (n < aep_context->count_pollfds) {
- aep_context->pollfds[n] = aep_context->pollfds[n + 1];
- n++;
- }
- aep_context->count_pollfds--;
- }
-}
-
-
-int service_aeps(struct aep_context *aep_context)
+int service_aeps(struct aep_context *aep_context, int fd_with_rx)
{
static unsigned char zero[8] = { 0, 0, 0, 0, 0, 0, 0, 0, };
int fd;
@@ -875,9 +852,11 @@ int service_aeps(struct aep_context *aep_context)
int n, m, i;
struct aep *aep;
struct aep_channel *ch;
- int timeout = 20;
unsigned char buf[AEP_INPUT_QUEUE];
+ if (fd_with_rx >= 0)
+ goto post_start;
+
gettimeofday(&tv, NULL);
/* look for any new devices appearing */
@@ -898,6 +877,9 @@ int service_aeps(struct aep_context *aep_context)
goto bail;
}
+
+ fprintf(stderr, "+");
+
tcflush(fd, TCIOFLUSH);
if (ioctl(fd, TIOCGSERIAL, &sinfo) == 0) {
@@ -962,7 +944,8 @@ int service_aeps(struct aep_context *aep_context)
if (aep_context->scan > aep_context->highest)
aep_context->highest = aep_context->scan;
- add_pollfd(aep_context, fd, POLLIN | POLLERR);
+
+ aep_context->samples(AEPSA_ADD_POLLFD, &aep_context->aeps[aep_context->scan].ch[0], fd, POLLIN | POLLERR);
for (n = 0; n < CHANNELS_PER_PROBE; n++) {
struct aep_channel *ch = &aep_context->aeps[aep_context->scan].ch[n];
@@ -994,27 +977,6 @@ bail:
if (aep_context->scan == MAX_PROBES)
aep_context->scan = 0;
- /* if nothing waiting fail immediately if anything has buffered data to service */
-
- for (n = 0; n <= aep_context->highest; n++) {
- if (aep_context->aeps[n].fd <= 0)
- continue;
-
- if (aep_context->aeps[n].head != aep_context->aeps[n].tail)
- timeout = 0;
- }
-
- if (aep_context->verbose)
- fprintf(stderr, "count_pollfds=%d, [0].events=%d, ",
- aep_context->count_pollfds, aep_context->pollfds[0].events);
- n = poll(aep_context->pollfds, aep_context->count_pollfds, timeout);
- if (aep_context->verbose)
- fprintf(stderr, "n = %d %d\n", n, aep_context->pollfds[0].fd);
- if (n < 0) {
- fprintf(stderr, "poll failed %d\n", aep_context->count_pollfds);
- return -1;
- }
-
/* if we're waiting to start but saw every channel / probe */
// fprintf(stderr, "* %d %d %d\n", aep_context->has_started, aep_context->count_channel_names, aep_context->matched_channel_names);
@@ -1107,15 +1069,15 @@ bail:
}
post_start:
+ if (fd_with_rx < 0)
+ goto service;
+
/* somebody had something for us */
for (m = 0; m <= aep_context->highest; m++) {
aep = &aep_context->aeps[m];
- if (aep->fd < 1)
- continue;
-
/* check for timeout */
if (tv.tv_sec > aep->sec_last_traffic + 2) {
@@ -1126,13 +1088,7 @@ post_start:
}
}
- for (n = 0; n < aep_context->count_pollfds; n++)
- if (aep->fd == aep_context->pollfds[n].fd)
- break;
- if (n == aep_context->count_pollfds)
- continue;
-
- if (!aep_context->pollfds[n].revents)
+ if (aep->fd != fd_with_rx)
continue;
if (aep_context->verbose)
@@ -1203,11 +1159,12 @@ died:
aep_context->matched_channel_names--;
}
probe_close(aep);
- remove_pollfd(aep_context, aep->fd);
+ aep_context->samples(AEPSA_REMOVE_POLLFD, &aep->ch[0], aep->fd, 0);
aep->fd = 0;
m = aep_context->highest;
}
+service:
/*
* service the existing devices
@@ -1239,7 +1196,7 @@ died:
aep_context->matched_channel_names--;
}
probe_close(aep);
- remove_pollfd(aep_context, aep->fd);
+ aep_context->samples(AEPSA_REMOVE_POLLFD, &aep->ch[0], aep->fd, 0);
aep->fd = 0;
}
@@ -1247,9 +1204,18 @@ died:
aep_context->awaiting_capture == 0 && aep_context->exit_after_capture) {
fprintf(stderr, "done all capture\n");
+ return -1;
+ }
+ /* if nothing waiting fail immediately if anything has buffered data to service */
- return -1;
+ aep_context->poll_timeout_ms = 10;
+ for (n = 0; n <= aep_context->highest; n++) {
+ if (aep_context->aeps[n].fd <= 0)
+ continue;
+
+ if (aep_context->aeps[n].head != aep_context->aeps[n].tail)
+ aep_context->poll_timeout_ms = 0;
}
return 0;