Skip to content
Snippets Groups Projects
Select Git revision
  • ab168e9a423fedb260f18a8960d109b24d22fca5
  • master default protected
  • release
  • experimental
4 results

MapView.vue

Blame
  • cpu_temp.c 1.54 KiB
    #include "../../../common/include/measurement.h"
    #include "../../../common/include/userspace_comm.h"
    #include <errno.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <unistd.h>
    
    int main(int argc, char *argv[]) {
      if (argc < 3 || argc > 3) {
        printf("Invalid arguments provided");
        return EXIT_FAILURE;
      }
    
      userspace_send_handle_t userspace_send_handle;
      if (create_userspace_send_handle(&userspace_send_handle) == -1) {
        perror("Failed to open message queue");
        return EXIT_FAILURE;
      }
    
      int file_fd = open("/sys/class/thermal/thermal_zone0/temp", O_RDONLY);
      if (file_fd == -1) {
        perror("Failed to open file");
        return EXIT_FAILURE;
      }
    
      double cpu_temp;
      char buffer[6];
    
      int id = atoi(argv[1]);
      int sleep_ms = atoi(argv[2]);
    
      printf("Started cpu temp (id: %d, delay: %dms)\n", id, sleep_ms);
    
      while (1) {
        if (pread(file_fd, buffer, sizeof(buffer), 0) == -1) {
          perror("Failed to read file");
          continue;
        }
    
        cpu_temp = (double)atoi(buffer) / 1000;
    
        measurement_t measurement = {
            .data.float64 = cpu_temp,
            .datatype = FLOAT64,
            .id = id,
        };
    
        if (send_userspace_message(&userspace_send_handle, measurement) == -1) {
          if (errno == EIDRM || errno == EINVAL) {
            printf("Message queue got closed\n");
            break;
          }
          perror("Failed to send message");
        }
    
        usleep(sleep_ms * 1000);
      }
    
      destroy_userspace_send_handle(&userspace_send_handle);
      close(file_fd);
    
      return EXIT_SUCCESS;
    }