From fec0b2fcc641a9a7cb490800be088566fc18b2c0 Mon Sep 17 00:00:00 2001 From: Alessandro De Angelis Date: Fri, 5 Jul 2024 15:52:32 +0200 Subject: [PATCH 1/2] fix: read temperature sensors darwin arm --- sensors/darwin_arm_sensors.h | 139 ++++++++++++++++++++++++++++++++++ sensors/sensors_darwin_cgo.go | 45 ++++++++++- 2 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 sensors/darwin_arm_sensors.h diff --git a/sensors/darwin_arm_sensors.h b/sensors/darwin_arm_sensors.h new file mode 100644 index 000000000..a0e6cb421 --- /dev/null +++ b/sensors/darwin_arm_sensors.h @@ -0,0 +1,139 @@ +// This code is originally from https://github.com/freedomtan/sensors/blob/master/sensors/sensors.m +// Here is the original code's license + +// BSD 3-Clause License + +// Copyright (c) 2016-2018, "freedom" Koan-Sin Tan +// All rights reserved. + +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: + +// * Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. + +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. + +// * Neither the name of the copyright holder nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. + +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#import +#import +#include + +typedef struct __IOHIDEvent *IOHIDEventRef; +typedef struct __IOHIDServiceClient *IOHIDServiceClientRef; +typedef double IOHIDFloat; + +IOHIDEventSystemClientRef IOHIDEventSystemClientCreate(CFAllocatorRef allocator); + +int IOHIDEventSystemClientSetMatching(IOHIDEventSystemClientRef client, CFDictionaryRef match); + +IOHIDEventRef IOHIDServiceClientCopyEvent(IOHIDServiceClientRef, int64_t, int32_t, int64_t); + +CFStringRef IOHIDServiceClientCopyProperty(IOHIDServiceClientRef service, CFStringRef property); + +IOHIDFloat IOHIDEventGetFloatValue(IOHIDEventRef event, int32_t field); + +NSDictionary *matching(int page, int usage) { + NSDictionary *dict = @{ + @"PrimaryUsagePage" : [NSNumber numberWithInt:page], + @"PrimaryUsage" : [NSNumber numberWithInt:usage], + }; + + return dict; +} + +NSArray *getProductNames(NSDictionary *sensors) { + IOHIDEventSystemClientRef system = IOHIDEventSystemClientCreate(kCFAllocatorDefault); + + IOHIDEventSystemClientSetMatching(system, (__bridge CFDictionaryRef)sensors); + NSArray *matchingsrvs = (__bridge NSArray *)IOHIDEventSystemClientCopyServices(system); + + long count = [matchingsrvs count]; + NSMutableArray *array = [[NSMutableArray alloc] init]; + + for (int i = 0; i < count; i++) { + IOHIDServiceClientRef sc = (IOHIDServiceClientRef)matchingsrvs[i]; + NSString *name = (NSString *)IOHIDServiceClientCopyProperty(sc, (__bridge CFStringRef)@"Product"); + + if (name) { + [array addObject:name]; + } else { + [array addObject:@"noname"]; + } + } + + return array; +} + +#define IOHIDEventFieldBase(type) (type << 16) +#define kIOHIDEventTypeTemperature 15 +#define kIOHIDEventTypePower 25 + +NSArray *getThermalValues(NSDictionary *sensors) { + IOHIDEventSystemClientRef system = IOHIDEventSystemClientCreate(kCFAllocatorDefault); + + IOHIDEventSystemClientSetMatching(system, (__bridge CFDictionaryRef)sensors); + NSArray *matchingsrvs = (__bridge NSArray *)IOHIDEventSystemClientCopyServices(system); + + long count = [matchingsrvs count]; + NSMutableArray *array = [[NSMutableArray alloc] init]; + + for (int i = 0; i < count; i++) { + IOHIDServiceClientRef sc = (IOHIDServiceClientRef)matchingsrvs[i]; + IOHIDEventRef event = IOHIDServiceClientCopyEvent(sc, kIOHIDEventTypeTemperature, 0, 0); + + NSNumber *value; + double temp = 0.0; + + if (event != 0) { + temp = IOHIDEventGetFloatValue(event, IOHIDEventFieldBase(kIOHIDEventTypeTemperature)); + } + + value = [NSNumber numberWithDouble:temp]; + [array addObject:value]; + } + + return array; +} + +NSString *dumpNamesValues(NSArray *kvsN, NSArray *kvsV) { + NSMutableString *valueString = [[NSMutableString alloc] init]; + int count = [kvsN count]; + + for (int i = 0; i < count; i++) { + NSString *output = [NSString stringWithFormat:@"%s:%lf\n", [kvsN[i] UTF8String], [kvsV[i] doubleValue]]; + [valueString appendString:output]; + } + + return valueString; +} + +char *getThermals() { + NSDictionary *thermalSensors = matching(0xff00, 5); + NSArray *thermalNames = getProductNames(thermalSensors); + NSArray *thermalValues = getThermalValues(thermalSensors); + NSString *result = dumpNamesValues(thermalNames, thermalValues); + char *finalStr = strdup([result UTF8String]); + + CFRelease(thermalSensors); + CFRelease(thermalNames); + CFRelease(thermalValues); + CFRelease(result); + + return finalStr; +} diff --git a/sensors/sensors_darwin_cgo.go b/sensors/sensors_darwin_cgo.go index 153cf0721..aa3d29120 100644 --- a/sensors/sensors_darwin_cgo.go +++ b/sensors/sensors_darwin_cgo.go @@ -3,15 +3,57 @@ package sensors -// #cgo LDFLAGS: -framework IOKit +// #cgo CFLAGS: -x objective-c +// #cgo LDFLAGS: -framework Foundation -framework IOKit // #include "smc_darwin.h" +// #include "darwin_arm_sensors.h" import "C" import ( + "bufio" "context" + "math" + "runtime" + "strconv" + "strings" "unsafe" ) +func ReadTemperaturesArm() []TemperatureStat { + cStr := C.getThermals() + defer C.free(unsafe.Pointer(cStr)) + + var stats []TemperatureStat + goStr := C.GoString(cStr) + scanner := bufio.NewScanner(strings.NewReader(goStr)) + for scanner.Scan() { + split := strings.Split(scanner.Text(), ":") + if len(split) != 2 { + continue + } + + val, err := strconv.ParseFloat(split[1], 32) + if err != nil { + continue + } + + sensorKey := strings.Split(split[0], " ")[0] + + val = math.Abs(val) + + stats = append(stats, TemperatureStat{ + SensorKey: sensorKey, + Temperature: float64(val), + }) + } + + return stats +} + func TemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) { + if runtime.GOARCH == "arm64" { + return ReadTemperaturesArm(), nil + } + temperatureKeys := []string{ C.AMBIENT_AIR_0, C.AMBIENT_AIR_1, @@ -48,5 +90,6 @@ func TemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) { Temperature: float64(C.gopsutil_v4_get_temperature(ckey)), }) } + return temperatures, nil } From ba82fefdee75414877f279c676dd5c52654849de Mon Sep 17 00:00:00 2001 From: Alessandro De Angelis Date: Mon, 8 Jul 2024 16:09:48 +0200 Subject: [PATCH 2/2] fix: add SPDX --- sensors/darwin_arm_sensors.h | 35 +++-------------------------------- 1 file changed, 3 insertions(+), 32 deletions(-) diff --git a/sensors/darwin_arm_sensors.h b/sensors/darwin_arm_sensors.h index a0e6cb421..b1d2ebb81 100644 --- a/sensors/darwin_arm_sensors.h +++ b/sensors/darwin_arm_sensors.h @@ -1,35 +1,6 @@ -// This code is originally from https://github.com/freedomtan/sensors/blob/master/sensors/sensors.m -// Here is the original code's license - -// BSD 3-Clause License - -// Copyright (c) 2016-2018, "freedom" Koan-Sin Tan -// All rights reserved. - -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: - -// * Redistributions of source code must retain the above copyright notice, this -// list of conditions and the following disclaimer. - -// * Redistributions in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. - -// * Neither the name of the copyright holder nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. - -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// SPDX-FileCopyrightText: Copyright (c) 2016-2018, "freedom" Koan-Sin Tan +// SPDX-License-Identifier: BSD-3-Clause +// https://github.com/freedomtan/sensors/blob/master/sensors/sensors.m #import #import #include