Skip to content

Commit

Permalink
Temperature on Apple M1 macs. Should fix yujitach#221 .
Browse files Browse the repository at this point in the history
  • Loading branch information
yujitach committed Jan 25, 2021
1 parent b1112f6 commit 5604e76
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 4 deletions.
1 change: 0 additions & 1 deletion MenuExtras/MenuMeterCPU/MenuMeterCPUStats.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#import <mach/mach.h>
#import <mach/mach_error.h>
#import "MenuMeterCPU.h"
#import "../../smc_reader/smc_reader.h"
#import "LocalizedStrings.h"

@interface MenuMeterCPULoad : NSObject
Expand Down
13 changes: 13 additions & 0 deletions MenuExtras/MenuMeterCPU/MenuMeterCPUStats.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

#import "MenuMeterCPUStats.h"
#import <IOKit/pwr_mgt/IOPM.h>
#import "../../hardware_reader/smc_reader.h"
#import "../../hardware_reader/applesilicon_hardware_reader.h"
#include <TargetConditionals.h>

@implementation MenuMeterCPULoad
@end
Expand Down Expand Up @@ -325,6 +328,7 @@ - (NSArray *)currentLoadBySorting: (BOOL)sorted {

- (float_t)cpuProximityTemperature {
float_t celsius = -273.15F;
#if TARGET_CPU_X86_64
if (kIOReturnSuccess == SMCOpen()) {
SMCKeyValue value;
//use harcoded value for a while
Expand All @@ -334,6 +338,15 @@ - (float_t)cpuProximityTemperature {
}
SMCClose();
}
#elif TARGET_CPU_ARM64
NSDictionary*dict=AppleSiliconTemperatureDictionary();
//use harcoded value for a while
//TODO: implement tab to select which sensor to display
NSNumber*temp=dict[@"SOC MTR Temp Sensor0"];
if(temp){
celsius=[temp floatValue];
}
#endif
return celsius;
} // cpuProximityTemperature

Expand Down
14 changes: 11 additions & 3 deletions MenuMeters.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
330247672366CEA800F88D61 /* dsa_pub.pem in Resources */ = {isa = PBXBuildFile; fileRef = 330247662366CEA800F88D61 /* dsa_pub.pem */; };
3302476A2366CF7F00F88D61 /* Sparkle.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 330247642366CB7A00F88D61 /* Sparkle.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
330611032367EA4C0016E763 /* MenuMetersMainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 330611022367EA4C0016E763 /* MenuMetersMainMenu.xib */; };
3325978C25BE7DB10070CE37 /* applesilicon_hardware_reader.m in Sources */ = {isa = PBXBuildFile; fileRef = 3325978B25BE7DB10070CE37 /* applesilicon_hardware_reader.m */; };
3325979325BE7DD00070CE37 /* applesilicon_hardware_reader.m in Sources */ = {isa = PBXBuildFile; fileRef = 3325978B25BE7DB10070CE37 /* applesilicon_hardware_reader.m */; };
3330C5B4257DFAED00BD5164 /* LocalizedStrings.h in Headers */ = {isa = PBXBuildFile; fileRef = 3330C5B2257DFAED00BD5164 /* LocalizedStrings.h */; };
3330C5B5257DFAED00BD5164 /* LocalizedStrings.h in Headers */ = {isa = PBXBuildFile; fileRef = 3330C5B2257DFAED00BD5164 /* LocalizedStrings.h */; };
3330C5B6257DFAED00BD5164 /* LocalizedStrings.m in Sources */ = {isa = PBXBuildFile; fileRef = 3330C5B3257DFAED00BD5164 /* LocalizedStrings.m */; };
Expand Down Expand Up @@ -151,6 +153,8 @@
330247662366CEA800F88D61 /* dsa_pub.pem */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = dsa_pub.pem; sourceTree = "<group>"; };
330611022367EA4C0016E763 /* MenuMetersMainMenu.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = MenuMetersMainMenu.xib; sourceTree = "<group>"; };
330D68B72105AB950041B689 /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/DiskImageSet.strings"; sourceTree = "<group>"; };
3325978B25BE7DB10070CE37 /* applesilicon_hardware_reader.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = applesilicon_hardware_reader.m; sourceTree = "<group>"; };
3325978F25BE7DCA0070CE37 /* applesilicon_hardware_reader.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = applesilicon_hardware_reader.h; sourceTree = "<group>"; };
3330C5B2257DFAED00BD5164 /* LocalizedStrings.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LocalizedStrings.h; sourceTree = "<group>"; };
3330C5B3257DFAED00BD5164 /* LocalizedStrings.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LocalizedStrings.m; sourceTree = "<group>"; };
3345DD2D1B6BEFDE003843FC /* MenuMetersMenuExtraBase.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MenuMetersMenuExtraBase.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -343,7 +347,7 @@
children = (
33F092172367387B00953CAB /* MenuMeters.entitlements */,
9023745A1E91612100B096A8 /* InfoPlistPreprocessor.h */,
F75EBE2D235B6F3600876CF7 /* smc_reader */,
F75EBE2D235B6F3600876CF7 /* hardware_reader */,
336B59401E03BA68006DA445 /* MenuMeters.pch */,
D4510D7304F2F06100A87BC9 /* Docs */,
D40DB4B303D25F9E00A87BC9 /* Common */,
Expand Down Expand Up @@ -573,13 +577,15 @@
path = Common/Resources;
sourceTree = "<group>";
};
F75EBE2D235B6F3600876CF7 /* smc_reader */ = {
F75EBE2D235B6F3600876CF7 /* hardware_reader */ = {
isa = PBXGroup;
children = (
F75EBE2E235B6F3600876CF7 /* smc_reader.h */,
F75EBE2F235B6F3600876CF7 /* smc_reader.c */,
3325978B25BE7DB10070CE37 /* applesilicon_hardware_reader.m */,
3325978F25BE7DCA0070CE37 /* applesilicon_hardware_reader.h */,
);
path = smc_reader;
path = hardware_reader;
sourceTree = "<group>";
};
/* End PBXGroup section */
Expand Down Expand Up @@ -812,6 +818,7 @@
3330C5B6257DFAED00BD5164 /* LocalizedStrings.m in Sources */,
33883CF723655C9900B8AC14 /* MenuMeterCPUStats.m in Sources */,
33883CF823655C9900B8AC14 /* MenuMetersMenuExtraBase.m in Sources */,
3325978C25BE7DB10070CE37 /* applesilicon_hardware_reader.m in Sources */,
33883CF923655C9900B8AC14 /* MenuMeterUptime.m in Sources */,
33883CFA23655C9900B8AC14 /* smc_reader.c in Sources */,
33883CFB23655C9900B8AC14 /* MenuMeterCPUTopProcesses.m in Sources */,
Expand Down Expand Up @@ -842,6 +849,7 @@
3330C5B7257DFAED00BD5164 /* LocalizedStrings.m in Sources */,
6349C11124BB733300C6FC99 /* MenuMeterCPUStats.m in Sources */,
6349C11224BB733300C6FC99 /* MenuMetersMenuExtraBase.m in Sources */,
3325979325BE7DD00070CE37 /* applesilicon_hardware_reader.m in Sources */,
6349C11324BB733300C6FC99 /* MenuMeterUptime.m in Sources */,
6349C11424BB733300C6FC99 /* smc_reader.c in Sources */,
6349C11524BB733300C6FC99 /* MenuMeterCPUTopProcesses.m in Sources */,
Expand Down
13 changes: 13 additions & 0 deletions hardware_reader/applesilicon_hardware_reader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// applesilicon_hardware_reader.h
// MenuMeters
//
// Created by Yuji on 1/25/21.
//

#ifndef applesilicon_hardware_reader_h
#define applesilicon_hardware_reader_h

extern NSDictionary*AppleSiliconTemperatureDictionary(void);

#endif /* applesilicon_hardware_reader_h */
83 changes: 83 additions & 0 deletions hardware_reader/applesilicon_hardware_reader.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//
// applesilicon_hardware_reader.m
// MenuMeters
//
// Created by Yuji on 1/25/21.
//

#import <Foundation/Foundation.h>
#import "applesilicon_hardware_reader.h"

// This code is based on https://github.com/fermion-star/apple_sensors/blob/master/temp_sensor.m
// which was in turn based on https://github.com/freedomtan/sensors/blob/master/sensors/sensors.m
// whose detail can be found in https://www2.slideshare.net/kstan2/exploring-thermal-related-stuff-in-idevices-using-opensource-tool


#include <IOKit/hidsystem/IOHIDEventSystemClient.h>

// Declarations from other IOKit source code

typedef struct __IOHIDEvent *IOHIDEventRef;
typedef struct __IOHIDServiceClient *IOHIDServiceClientRef;
#ifdef __LP64__
typedef double IOHIDFloat;
#else
typedef float IOHIDFloat;
#endif

IOHIDEventSystemClientRef IOHIDEventSystemClientCreate(CFAllocatorRef allocator);
int IOHIDEventSystemClientSetMatching(IOHIDEventSystemClientRef client, CFDictionaryRef match);
int IOHIDEventSystemClientSetMatchingMultiple(IOHIDEventSystemClientRef client, CFArrayRef match);
IOHIDEventRef IOHIDServiceClientCopyEvent(IOHIDServiceClientRef, int64_t , int32_t, int64_t);
CFStringRef IOHIDServiceClientCopyProperty(IOHIDServiceClientRef service, CFStringRef property);
IOHIDFloat IOHIDEventGetFloatValue(IOHIDEventRef event, int32_t field);

//extern uint64_t my_mhz(void);
//extern void mybat(void);
// Primary Usage Page:
// kHIDPage_AppleVendor = 0xff00,
// kHIDPage_AppleVendorTemperatureSensor = 0xff05,
// kHIDPage_AppleVendorPowerSensor = 0xff08,
//
// Primary Usage:
// kHIDUsage_AppleVendor_TemperatureSensor = 0x0005,
// kHIDUsage_AppleVendorPowerSensor_Current = 0x0002,
// kHIDUsage_AppleVendorPowerSensor_Voltage = 0x0003,
// See IOHIDFamily/AppleHIDUsageTables.h for more information
// https://opensource.apple.com/source/IOHIDFamily/IOHIDFamily-701.60.2/IOHIDFamily/AppleHIDUsageTables.h.auto.html


#define IOHIDEventFieldBase(type) (type << 16)
#define kIOHIDEventTypeTemperature 15
#define kIOHIDEventTypePower 25

NSDictionary*AppleSiliconTemperatureDictionary(void)
{
NSDictionary*thermalSensors=@{@"PrimaryUsagePage":@(0xff00),@"PrimaryUsage":@(5)};


IOHIDEventSystemClientRef system = IOHIDEventSystemClientCreate(kCFAllocatorDefault); // in CFBase.h = NULL
// ... this is the same as using kCFAllocatorDefault or the return value from CFAllocatorGetDefault()
IOHIDEventSystemClientSetMatching(system, (__bridge CFDictionaryRef)thermalSensors);
CFArrayRef matchingsrvs = IOHIDEventSystemClientCopyServices(system); // matchingsrvs = matching services


NSMutableDictionary*dict=[NSMutableDictionary dictionary];
long count = CFArrayGetCount(matchingsrvs);
for (int i = 0; i < count; i++) {
IOHIDServiceClientRef sc = (IOHIDServiceClientRef)CFArrayGetValueAtIndex(matchingsrvs, i);
NSString* name = CFBridgingRelease(IOHIDServiceClientCopyProperty(sc, CFSTR("Product"))); // here we use ...CopyProperty
IOHIDEventRef event = IOHIDServiceClientCopyEvent(sc, kIOHIDEventTypeTemperature, 0, 0); // here we use ...CopyEvent
if (name && event) {
double temp = IOHIDEventGetFloatValue(event, IOHIDEventFieldBase(kIOHIDEventTypeTemperature));
dict[name]=@(temp);
}
CFRelease(event);
}

CFRelease(matchingsrvs);
CFRelease(system);

return dict;

}
File renamed without changes.
File renamed without changes.

0 comments on commit 5604e76

Please sign in to comment.