项目标题与描述
Battery Toolkit 是一个专为 Apple Silicon Mac 设计的平台电源状态控制工具。项目旨在提供比 macOS 原生"优化电池充电"更灵活、更精确的电池管理功能,帮助用户延长电池寿命。
功能特性
安装指南
下载最新版本安装包
在终端运行以下命令进行安装:
# 安装 Battery Toolkit 守护进程sudo cp /path/to/me.mhaeuser.batterytoolkitd.plist /Library/LaunchDaemons/sudo cp /path/to/me.mhaeuser.batterytoolkitd /Library/PrivilegedHelperTools/sudo launchctl load /Library/LaunchDaemons/me.mhaeuser.batterytoolkitd.plist
复制代码
系统要求:
Apple Silicon Mac
macOS 12.0 或更高版本
使用说明
基本使用
通过系统偏好设置或命令行工具控制电池设置:
# 设置充电上限为80%batterytoolkit --upper-limit 80
# 设置充电下限为30% batterytoolkit --lower-limit 30
# 禁用电源适配器batterytoolkit --disable-adapter
复制代码
高级功能
// 通过 XPC 与服务通信NSXPCConnection *connection = [[NSXPCConnection alloc] initWithMachServiceName:@BT_DAEMON_CONN_ options:NSXPCConnectionPrivileged];connection.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(BatteryToolkitProtocol)];[connection resume];
复制代码
核心代码
1. SMC 参数结构定义
// SMCParamStruct.htypedef struct { uint32_t key; SMCVersion vers; SMCPLimitData pLimitData; SMCKeyInfoData keyInfo; uint8_t result; uint8_t status; uint8_t data8; uint32_t data32; uint8_t bytes[32];} SMCParamStruct;
复制代码
2. XPC 连接审计令牌扩展
// NSXPCConnection+AuditToken.h@interface NSXPCConnection (AuditToken)@property (nonatomic, readonly) audit_token_t auditToken;@end
复制代码
3. 电源管理接口
// IOPMLibPrivate.hIOReturn IOPMSetSystemPowerSetting(CFStringRef key, CFTypeRef value);CFDictionaryRef IOPMCopySystemPowerSettings(void);
复制代码
4. 签名信息获取
// SecCodeEx.hOSStatus SecCodeCopySigningInformationDynamic( SecCodeRef code, SecCSFlags flags, CFDictionaryRef *information);
复制代码
更多精彩内容 请关注我的个人公众号 公众号(办公 AI 智能小助手)公众号二维码
办公AI智能小助手
评论