commit d04fa108b53d9c0a24b3a42c21257281240e7c61
parent be9e7a3db765ad5bc860b8a6311725996756f0e1
Author: amin <dev@aminmesbah.com>
Date: Sat, 15 Jan 2022 07:01:20 +0000
Gracefully handle unplugging and replugging device
FossilOrigin-Name: 1b34ff13628749a07f18844c9594d688b9e3fd84af1a181b64d9c2cb7c3a8fd7
Diffstat:
M | main.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))