|
| 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