Newer
Older
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include <time.h>
#include <unistd.h>
#include "cli_monitor.h"
static void timespec_diff(struct timespec *start, struct timespec *result);
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
int cli_monitor(struct backend_conn * conn, int argc, char **argv) {
int c, result;
int timeFlag = 0;
static struct timespec startTime;
static struct timespec elapsedTime;
static struct timespec lastChecked;
static int nsecs, rate = 10;
while ((c = getopt(argc, argv, "t:r:")) != -1) {
switch(c) {
case 't' :
nsecs = atoi(optarg);
timeFlag = 1;
break;
case 'r' :
rate = atoi(optarg) + 1;
break;
default :
break;
}
}
if(timeFlag) {
clock_gettime(CLOCK_REALTIME, &startTime);
clock_gettime(CLOCK_REALTIME, &lastChecked);
while((result = monitor(conn)) == 0) {
timespec_diff(&startTime, &elapsedTime);
if(elapsedTime.tv_sec > nsecs) {
break;
}
while(1) {
timespec_diff(&lastChecked, &elapsedTime);
if(elapsedTime.tv_nsec >= (long) ((SECOND_IN_NANO * 2) / rate)) {
clock_gettime(CLOCK_REALTIME, &lastChecked);
break;
}
}
}
} else {
return monitor(conn);
}
return result;
}
int monitor(struct backend_conn * conn) {
static char * line = "monitor\n";
printf("monitoring\n");
int size;
if((size = ucart_backendWrite(conn, line)) < 0 ) {
return 1;
}
static void timespec_diff(struct timespec *start, struct timespec *result)
{
struct timespec stop;
clock_gettime(CLOCK_REALTIME, &stop);
if ((stop.tv_nsec - start->tv_nsec) < 0) {
result->tv_sec = stop.tv_sec - start->tv_sec - 1;
result->tv_nsec = stop.tv_nsec - start->tv_nsec + 1000000000;
} else {
result->tv_sec = stop.tv_sec - start->tv_sec;
result->tv_nsec = stop.tv_nsec - start->tv_nsec;
}
return;
}