File: sensors_darwin_arm64.go

package info (click to toggle)
golang-github-shirou-gopsutil 4.25.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,824 kB
  • sloc: makefile: 76; ansic: 19; sh: 11
file content (196 lines) | stat: -rw-r--r-- 6,882 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// SPDX-License-Identifier: BSD-3-Clause
//go:build darwin && arm64

package sensors

import (
	"context"
	"unsafe"

	"github.com/shirou/gopsutil/v4/internal/common"
)

func ReadTemperaturesArm() []TemperatureStat {
	temperatures, _ := TemperaturesWithContext(context.Background())
	return temperatures
}

func TemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
	ioKit, err := common.NewLibrary(common.IOKit)
	if err != nil {
		return nil, err
	}
	defer ioKit.Close()

	coreFoundation, err := common.NewLibrary(common.CoreFoundation)
	if err != nil {
		return nil, err
	}
	defer coreFoundation.Close()

	ta := &temperatureArm{
		ioKit:                              ioKit,
		cf:                                 coreFoundation,
		cfRelease:                          common.GetFunc[common.CFReleaseFunc](coreFoundation, common.CFReleaseSym),
		cfStringCreateWithCString:          common.GetFunc[common.CFStringCreateWithCStringFunc](coreFoundation, common.CFStringCreateWithCStringSym),
		cfArrayGetCount:                    common.GetFunc[common.CFArrayGetCountFunc](coreFoundation, common.CFArrayGetCountSym),
		cfArrayGetValueAtIndex:             common.GetFunc[common.CFArrayGetValueAtIndexFunc](coreFoundation, common.CFArrayGetValueAtIndexSym),
		ioHIDEventSystemClientCreate:       common.GetFunc[common.IOHIDEventSystemClientCreateFunc](ioKit, common.IOHIDEventSystemClientCreateSym),
		ioHIDEventSystemClientSetMatching:  common.GetFunc[common.IOHIDEventSystemClientSetMatchingFunc](ioKit, common.IOHIDEventSystemClientSetMatchingSym),
		ioHIDEventSystemClientCopyServices: common.GetFunc[common.IOHIDEventSystemClientCopyServicesFunc](ioKit, common.IOHIDEventSystemClientCopyServicesSym),
	}

	ta.matching(0xff00, 5)
	defer ta.cfRelease(uintptr(ta.sensors))

	// Create HID system client
	system := ta.ioHIDEventSystemClientCreate(common.KCFAllocatorDefault)
	defer ta.cfRelease(uintptr(system))

	thermalNames := ta.getProductNames(system)
	thermalValues := ta.getThermalValues(system)
	result := dumpNameValues(thermalNames, thermalValues)

	return result, nil
}

func dumpNameValues(kvsN []string, kvsV []float64) []TemperatureStat {
	count := len(kvsN)
	temperatureMap := make(map[string]TemperatureStat)

	for i := 0; i < count; i++ {
		temperatureMap[kvsN[i]] = TemperatureStat{
			SensorKey:   kvsN[i],
			Temperature: kvsV[i],
		}
	}

	temperatures := make([]TemperatureStat, 0, len(temperatureMap))
	for _, stat := range temperatureMap {
		temperatures = append(temperatures, stat)
	}

	return temperatures
}

type temperatureArm struct {
	ioKit *common.Library
	cf    *common.Library

	cfRelease                 common.CFReleaseFunc
	cfStringCreateWithCString common.CFStringCreateWithCStringFunc
	cfArrayGetCount           common.CFArrayGetCountFunc
	cfArrayGetValueAtIndex    common.CFArrayGetValueAtIndexFunc

	ioHIDEventSystemClientCreate       common.IOHIDEventSystemClientCreateFunc
	ioHIDEventSystemClientSetMatching  common.IOHIDEventSystemClientSetMatchingFunc
	ioHIDEventSystemClientCopyServices common.IOHIDEventSystemClientCopyServicesFunc

	sensors unsafe.Pointer
}

func (ta *temperatureArm) getProductNames(system unsafe.Pointer) []string {
	ioHIDServiceClientCopyProperty := common.GetFunc[common.IOHIDServiceClientCopyPropertyFunc](ta.ioKit, common.IOHIDServiceClientCopyPropertySym)
	cfStringGetLength := common.GetFunc[common.CFStringGetLengthFunc](ta.cf, common.CFStringGetLengthSym)
	cfStringGetCString := common.GetFunc[common.CFStringGetCStringFunc](ta.cf, common.CFStringGetCStringSym)

	ta.ioHIDEventSystemClientSetMatching(uintptr(system), uintptr(ta.sensors))
	matchingsrvs := ta.ioHIDEventSystemClientCopyServices(uintptr(system))

	if matchingsrvs == nil {
		return nil
	}
	defer ta.cfRelease(uintptr(matchingsrvs))

	count := ta.cfArrayGetCount(uintptr(matchingsrvs))

	var i int32
	str := ta.cfStr("Product")
	defer ta.cfRelease(uintptr(str))

	names := make([]string, 0, count)
	for i = 0; i < count; i++ {
		sc := ta.cfArrayGetValueAtIndex(uintptr(matchingsrvs), i)
		name := ioHIDServiceClientCopyProperty(uintptr(sc), uintptr(str))

		if name != nil {
			buf := common.NewCStr(cfStringGetLength(uintptr(name)))
			cfStringGetCString(uintptr(name), buf, buf.Length(), common.KCFStringEncodingUTF8)

			names = append(names, buf.GoString())
			ta.cfRelease(uintptr(name))
		} else {
			// make sure the number of names and values are consistent
			names = append(names, "noname")
		}
	}

	return names
}

func (ta *temperatureArm) getThermalValues(system unsafe.Pointer) []float64 {
	ioHIDServiceClientCopyEvent := common.GetFunc[common.IOHIDServiceClientCopyEventFunc](ta.ioKit, common.IOHIDServiceClientCopyEventSym)
	ioHIDEventGetFloatValue := common.GetFunc[common.IOHIDEventGetFloatValueFunc](ta.ioKit, common.IOHIDEventGetFloatValueSym)

	ta.ioHIDEventSystemClientSetMatching(uintptr(system), uintptr(ta.sensors))
	matchingsrvs := ta.ioHIDEventSystemClientCopyServices(uintptr(system))

	if matchingsrvs == nil {
		return nil
	}
	defer ta.cfRelease(uintptr(matchingsrvs))

	count := ta.cfArrayGetCount(uintptr(matchingsrvs))

	var values []float64
	var i int32
	for i = 0; i < count; i++ {
		sc := ta.cfArrayGetValueAtIndex(uintptr(matchingsrvs), i)
		event := ioHIDServiceClientCopyEvent(uintptr(sc), common.KIOHIDEventTypeTemperature, 0, 0)
		temp := 0.0

		if event != nil {
			temp = ioHIDEventGetFloatValue(uintptr(event), ioHIDEventFieldBase(common.KIOHIDEventTypeTemperature))
			ta.cfRelease(uintptr(event))
		}

		values = append(values, temp)
	}

	return values
}

func (ta *temperatureArm) matching(page, usage int) {
	cfNumberCreate := common.GetFunc[common.CFNumberCreateFunc](ta.cf, common.CFNumberCreateSym)
	cfDictionaryCreate := common.GetFunc[common.CFDictionaryCreateFunc](ta.cf, common.CFDictionaryCreateSym)

	pageNum := cfNumberCreate(common.KCFAllocatorDefault, common.KCFNumberIntType, uintptr(unsafe.Pointer(&page)))
	defer ta.cfRelease(uintptr(pageNum))

	usageNum := cfNumberCreate(common.KCFAllocatorDefault, common.KCFNumberIntType, uintptr(unsafe.Pointer(&usage)))
	defer ta.cfRelease(uintptr(usageNum))

	k1 := ta.cfStr("PrimaryUsagePage")
	k2 := ta.cfStr("PrimaryUsage")

	defer ta.cfRelease(uintptr(k1))
	defer ta.cfRelease(uintptr(k2))

	keys := []unsafe.Pointer{k1, k2}
	values := []unsafe.Pointer{pageNum, usageNum}

	kCFTypeDictionaryKeyCallBacks, _ := ta.cf.Dlsym("kCFTypeDictionaryKeyCallBacks")
	kCFTypeDictionaryValueCallBacks, _ := ta.cf.Dlsym("kCFTypeDictionaryValueCallBacks")

	ta.sensors = cfDictionaryCreate(common.KCFAllocatorDefault, &keys[0], &values[0], 2,
		kCFTypeDictionaryKeyCallBacks,
		kCFTypeDictionaryValueCallBacks)
}

func (ta *temperatureArm) cfStr(str string) unsafe.Pointer {
	return ta.cfStringCreateWithCString(common.KCFAllocatorDefault, str, common.KCFStringEncodingUTF8)
}

func ioHIDEventFieldBase(i int32) int32 {
	return i << 16
}