Skip to content

Commit d69ae98

Browse files
Listen for messages to monitors coming from RAW sockets.
Update Makefile to create new executable, and gitignore to ignore the resulting binary. Signed-off-by: Ivan Perez <[email protected]>
1 parent 689fc20 commit d69ae98

File tree

3 files changed

+138
-1
lines changed

3 files changed

+138
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ demos/copilot/src/monitors/Image
1717
demos/copilot/src/monitors/aarch64-*
1818
demos/copilot/src/monitors/archive.tar
1919
demos/copilot/src/monitors/extracted_cpio/
20+
demos/copilot/src/monitors/main_raw_time
2021
demos/copilot/src/monitors/main_syslog_time_cross
2122
demos/copilot/src/monitors/rootfs.cpio*

demos/copilot/src/monitors/Makefile

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ main_syslog_local: main_syslog.c elisa-v2.c elisa-v2.h elisa-v2_types.h
5656
main_syslog_time: main_syslog_time.c elisa-time.c elisa-time.h elisa-time_types.h
5757
$(RUN_PREFIX) sh -c '$(CMD_PREFIX) gcc -DLOG_PATH=\"syslog_file\" main_syslog_time.c elisa-time.c -o $@'
5858

59+
main_raw_time: main_raw_time.c elisa-time.c elisa-time.h elisa-time_types.h
60+
$(RUN_PREFIX) sh -c '$(CMD_PREFIX) gcc main_raw_time.c elisa-time.c -o $@'
61+
5962
main_syslog_time_cross: main_syslog_time.c elisa-time.c elisa-time.h elisa-time_types.h
6063
$(RUN_PREFIX) sh -c '$(CMD_PREFIX) cd /demo/monitors/aarch64-buildroot-linux-musl_sdk-buildroot && ./relocate-sdk.sh && cd .. && /demo/monitors/aarch64-buildroot-linux-musl_sdk-buildroot/bin/aarch64-linux-gcc -DLOG_PATH=\"syslog_file\" main_syslog_time.c elisa-time.c -o $@'
6164
sudo rm -rf extracted_cpio && mkdir -p extracted_cpio && sudo gunzip -c rootfs.cpio.gz > rootfs.cpio_uncompressed
@@ -69,6 +72,19 @@ main_syslog_time_cross: main_syslog_time.c elisa-time.c elisa-time.h elisa-time_
6972
main_syslog_time_cross_run: main_syslog_time_cross
7073
qemu-system-aarch64 -M virt -m 512M -cpu cortex-a57 -smp 4 -nographic -kernel Image -initrd rootfs.cpio.gz_new -append "root=/dev/ram0 console=ttyAMA0"
7174

75+
main_raw_time_cross: main_raw_time.c elisa-time.c elisa-time.h elisa-time_types.h
76+
$(RUN_PREFIX) sh -c '$(CMD_PREFIX) cd /demo/monitors/aarch64-buildroot-linux-musl_sdk-buildroot && ./relocate-sdk.sh && cd .. && /demo/monitors/aarch64-buildroot-linux-musl_sdk-buildroot/bin/aarch64-linux-gcc main_raw_time.c elisa-time.c -o $@'
77+
sudo rm -rf extracted_cpio && mkdir -p extracted_cpio && sudo gunzip -c rootfs.cpio.gz > rootfs.cpio_uncompressed
78+
sudo cpio -i -R +0:+0 -n -F rootfs.cpio_uncompressed --make-directories --preserve-modification-time --no-absolute-filenames --directory=./extracted_cpio
79+
sudo cp -rf ../python ../tests ./extracted_cpio/root/
80+
sudo mkdir ./extracted_cpio/root/monitors
81+
sudo cp -f main_raw_time_cross ./extracted_cpio/root/monitors/main_raw_time
82+
sudo chmod 0755 ./extracted_cpio/root/monitors/main_raw_time
83+
sh -c 'cd extracted_cpio && sudo find . | sudo cpio -H newc -o | gzip -c > ../rootfs.cpio.gz_new'
84+
85+
main_raw_time_cross_run: main_raw_time_cross
86+
qemu-system-aarch64 -M virt -m 512M -cpu cortex-a57 -smp 4 -nographic -kernel Image -initrd rootfs.cpio.gz_new -append "root=/dev/ram0 console=ttyAMA0"
87+
7288
main_syslog_test: main_syslog_time.c elisa-time.c elisa-time.h elisa-time_types.h
7389
$(RUN_PREFIX) sh -c '$(CMD_PREFIX) gcc -DLOG_PATH=\"syslog_file\" -DCLOSE_AT_END -DNOTAIL main_syslog_time.c elisa-time.c -o $@'
7490

@@ -104,4 +120,4 @@ test_monitors: main_syslog_test
104120

105121
systemtest: main_syslog_time
106122
$(RUN_PREFIX_TEST) sudo sh -c "cd /demo/copilot/src/monitors \
107-
&& ../tests/runtests.sh"
123+
&& ../tests/runtests.sh"
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// SPDX-License-Identifier: MIT
2+
/*
3+
* This file implements a main driver for the test of the light switch
4+
* and the lights turning on within a certain time.
5+
*
6+
* Limitations: This driver assumes that time is mononotic.
7+
* If an event is read from a file that corresponds to a past time, the time of
8+
* the event is adjusted (overwritten) so that it is assumed to have occurred
9+
* at a strictly later time.
10+
*/
11+
#include <stdio.h>
12+
#include <stdlib.h>
13+
#include <string.h>
14+
#include <unistd.h>
15+
#include <inttypes.h>
16+
#include <stdbool.h>
17+
#include <syslog.h>
18+
#include <sys/time.h>
19+
#include <sys/socket.h>
20+
#include <netinet/in.h>
21+
#include <net/ethernet.h>
22+
23+
#include "elisa-time.h"
24+
25+
#define LIGHTS_ON_STRING "lights: on"
26+
#define LIGHTS_OFF_STRING "lights: off"
27+
#define SWITCH_ON_STRING "switch: on"
28+
#define SWITCH_OFF_STRING "switch: off"
29+
30+
/*
31+
* Clock used by the monitors.
32+
*/
33+
int64_t external_clock = 0;
34+
35+
/*
36+
* Globals needed by the monitors to know the status of the system.
37+
*/
38+
bool lights = false;
39+
bool lightSwitch = false;
40+
41+
/*
42+
* Fault handler: the light switch didn't turn on or took too long.
43+
*/
44+
void violation1 () {
45+
syslog(LOG_ERR, "Monitor violation: light switch didn't turn on on time.");
46+
printf("Monitor violation: light switch didn't turn on on time.\n");
47+
exit(1);
48+
}
49+
50+
/*
51+
* Fault handler: the light switch didn't turn off or took too long.
52+
*/
53+
void violation2 () {
54+
syslog(LOG_ERR, "Monitor violation: light switch didn't turn off on time.");
55+
printf("Monitor violation: light switch didn't turn off on time.\n");
56+
exit(1);
57+
}
58+
59+
/*
60+
* Update the internal clock to a new time.
61+
*
62+
* This function ensures that the clock is monotonic.
63+
*/
64+
void update_time(int64_t new_time) {
65+
if (new_time > external_clock) {
66+
external_clock = new_time;
67+
}
68+
}
69+
70+
// Main function
71+
int main(int argc, char *argv[]) {
72+
// Used if we need to get the system clock;
73+
struct timeval tv;
74+
int64_t milliseconds;
75+
76+
int sockfd;
77+
ssize_t recv_len;
78+
79+
// Set up socket
80+
sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
81+
if (sockfd < 0) {
82+
perror("Error creating socket");
83+
exit(EXIT_FAILURE);
84+
}
85+
86+
printf("Listening for raw packets\n");
87+
88+
89+
// Open the syslog for publishing notifications
90+
openlog("Copilot", LOG_PID | LOG_CONS, LOG_USER);
91+
92+
// Process each packet received and re-run the monitors.
93+
char text[1024];
94+
double lineTime;
95+
while (1) {
96+
97+
if (recv(sockfd, &text, sizeof(text), 0)) {
98+
if (strstr(text, LIGHTS_ON_STRING) != NULL)
99+
lights = true;
100+
if (strstr(text, LIGHTS_OFF_STRING) != NULL)
101+
lights = false;
102+
if (strstr(text, SWITCH_ON_STRING) != NULL)
103+
lightSwitch = true;
104+
if (strstr(text, SWITCH_OFF_STRING) != NULL)
105+
lightSwitch = false;
106+
if (sscanf(text, "[%lf]", &lineTime) == 1) {
107+
update_time((int64_t)(lineTime*1000));
108+
}
109+
110+
// Re-evaluate monitors only when new data is received.
111+
step();
112+
}
113+
}
114+
115+
// Close the socket, and the syslog for writing. Presumably, this is never
116+
// reached.
117+
closelog();
118+
close(sockfd);
119+
return 0;
120+
}

0 commit comments

Comments
 (0)