co2minimon

Get temperature and CO2 concentration data from a CO2Mini sensor.
Log | Files | Refs | README | LICENSE

commit 4646200c37d152830ee44c219d9facaf08ef93ba
parent f16a57f00ab8c4ab6731133e0766bce03df1e333
Author: Amin Mesbah <dev@aminmesbah.com>
Date:   Fri, 14 Jan 2022 23:01:21 -0800

Gracefully handle unplugging and replugging device

Diffstat:
Mmain.c | 55+++++++++++++++++++++++++++++++++++++------------------
1 file changed, 37 insertions(+), 18 deletions(-)

diff --git a/main.c b/main.c @@ -24,8 +24,9 @@ void stop_running(int sig) int main(void) { { - struct sigaction act = {0}; - act.sa_handler = stop_running; + struct sigaction act = { + .sa_handler = stop_running; + }; sigaction(SIGINT, &act, NULL); sigaction(SIGKILL, &act, NULL); sigaction(SIGTERM, &act, NULL); @@ -33,28 +34,46 @@ int main(void) // NOTE: Included udev rules create this symlink to the appropriate hidraw // entry. - int device_handle = open("/dev/co2mini0", O_RDWR); - if (device_handle < 0) - { - printf("ERROR: Failed to open HID.\n"); - return 1; - } + const char *hid_file_path = "/dev/co2mini0"; + const char *temperature_file_path = "/tmp/co2mini_temp"; + const char *co2_file_path = "/tmp/co2mini_co2"; + + int device_handle = -1; + while (running) { - uint8_t key[8] = {0}; - int result = ioctl(device_handle, HIDIOCSFEATURE(sizeof(key)), key); - if (result < 0 || result != sizeof(key)) + if (access(hid_file_path, F_OK) != 0) { - printf("ERROR: Failed to send feature report.\n"); - return 1; + if (close(device_handle) == 0) + { + device_handle = -1; + } + + unlink(co2_file_path); + unlink(temperature_file_path); + + sleep(30); + continue; } - } - const char *temperature_file_path = "/tmp/co2mini_temp"; - const char *co2_file_path = "/tmp/co2mini_co2"; + if (device_handle == -1) + { + device_handle = open(hid_file_path, O_RDWR); + if (device_handle < 0) + { + printf("ERROR: Failed to open HID.\n"); + return 1; + } + + uint8_t key[8] = {0}; + int result = ioctl(device_handle, HIDIOCSFEATURE(sizeof(key)), key); + if (result < 0 || result != sizeof(key)) + { + printf("ERROR: Failed to send feature report.\n"); + return 1; + } + } - while (running) - { uint8_t data[8] = {0}; int bytes_read = read(device_handle, &data, sizeof(data)); if (bytes_read < 0 && (errno == EAGAIN || errno == EINPROGRESS))