108 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			108 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| /*
 | |
|  * ths.c - Utilities for reading sysctl based temp. and humidity sensors
 | |
|  * Copyright (C) 2024  Alexander Rosenberg
 | |
|  *
 | |
|  * This program is free software: you can redistribute it and/or modify it under
 | |
|  * the terms of the GNU General Public License as published by the Free Software
 | |
|  * Foundation, either version 3 of the License, or (at your option) any later
 | |
|  * version. See the LICENSE file for more information.
 | |
|  */
 | |
| #include "ths.h"
 | |
| #include "util.h"
 | |
| 
 | |
| #include <err.h>
 | |
| #include <sys/sysctl.h>
 | |
| #include <inttypes.h>
 | |
| 
 | |
| THS *ths_open(const char *temp_key, const char *humid_key, const char *fail_key) {
 | |
|     THS *ths = malloc_checked(sizeof(THS));
 | |
|     if (temp_key) {
 | |
|         ths->temp_mib_len = THS_MIB_BUF_SIZE;
 | |
|         if (sysctlnametomib(temp_key, ths->temp_mib, &ths->temp_mib_len) < 0) {
 | |
|             warn("could not look up temp. key name: \"%s\"", temp_key);
 | |
|             free(ths);
 | |
|             return NULL;
 | |
|         }
 | |
|     } else {
 | |
|         ths->temp_mib_len = 0;
 | |
|     }
 | |
|     if (humid_key) {
 | |
|         ths->humid_mib_len = THS_MIB_BUF_SIZE;
 | |
|         if (sysctlnametomib(humid_key, ths->humid_mib, &ths->humid_mib_len) < 0) {
 | |
|             warn("could not look up humid. key name: \"%s\"", humid_key);
 | |
|             free(ths);
 | |
|             return NULL;
 | |
|         }
 | |
|     } else {
 | |
|         ths->humid_mib_len = 0;
 | |
|     }
 | |
|     if (fail_key) {
 | |
|         ths->fail_mib_len = THS_MIB_BUF_SIZE;
 | |
|         if (sysctlnametomib(fail_key, ths->fail_mib, &ths->fail_mib_len) < 0) {
 | |
|             warn("could not look up fail key name: \"%s\"", fail_key);
 | |
|             free(ths);
 | |
|             return NULL;
 | |
|         }
 | |
|     } else {
 | |
|         ths->fail_mib_len = 0;
 | |
|     }
 | |
|     LOG_VERBOSE("Opened THS with temp. key: \"%s\"\n"
 | |
|                 "               humid. key: \"%s\"\n"
 | |
|                 "                 fail key: \"%s\"\n",
 | |
|                 temp_key, humid_key, fail_key);
 | |
|     return ths;
 | |
| }
 | |
| 
 | |
| void ths_close(THS *ths) {
 | |
|     free(ths);
 | |
| }
 | |
| 
 | |
| int32_t ths_read_fails(const THS *ths) {
 | |
|     if (!ths->fail_mib_len) {
 | |
|         warnx("THS sensor does not support reading failure count");
 | |
|         return -1;
 | |
|     }
 | |
|     uint32_t ret_buf;
 | |
|     size_t ret_buf_size = sizeof(uint32_t);
 | |
|     if (sysctl(ths->fail_mib, ths->fail_mib_len, &ret_buf,
 | |
|                &ret_buf_size, NULL, 0) < 0) {
 | |
|         warn("could not read THS fail count");
 | |
|         return -1;
 | |
|     }
 | |
|     if (ret_buf > 0) {
 | |
|         LOG_VERBOSE("THS sensor reported: %" PRIu32 " fails\n", ret_buf)
 | |
|     }
 | |
|     return ret_buf;
 | |
| }
 | |
| 
 | |
| int32_t ths_read_temp(const THS *ths) {
 | |
|     if (!ths->temp_mib_len) {
 | |
|         warnx("THS sensor does not support reading temperature");
 | |
|         return -1;
 | |
|     }
 | |
|     uint32_t ret_buf;
 | |
|     size_t ret_buf_size = sizeof(uint32_t);
 | |
|     if (sysctl(ths->temp_mib, ths->temp_mib_len, &ret_buf,
 | |
|                &ret_buf_size, NULL, 0) < 0) {
 | |
|         warn("could not read THS temperature");
 | |
|         return -1;
 | |
|     }
 | |
|     return ret_buf;
 | |
| }
 | |
| 
 | |
| int32_t ths_read_humid(const THS *ths) {
 | |
|     if (!ths->humid_mib_len) {
 | |
|         warnx("THS sensor does not support reading humidity");
 | |
|         return -1;
 | |
|     }
 | |
|     uint32_t ret_buf;
 | |
|     size_t ret_buf_size = sizeof(uint32_t);
 | |
|     if (sysctl(ths->humid_mib, ths->humid_mib_len, &ret_buf,
 | |
|                &ret_buf_size, NULL, 0) < 0) {
 | |
|         warn("could not read THS humidity");
 | |
|         return -1;
 | |
|     }
 | |
|     return ret_buf;
 | |
|     
 | |
| }
 |