Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor for Swift 5 / Xcode 11 #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014-2017 beltex <https://github.com/beltex>
Copyright (c) 2014-2020 beltex <https://github.com/beltex>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ For more see:

### Requirements

- [Xcode 9.0 (Swift 4.0)](https://developer.apple.com/xcode/downloads/)
- macOS 10.12 Sierra and above **for development** (due to Xcode)
- [Xcode 11.3.1 (Swift 5.0)](https://developer.apple.com/xcode/downloads/)
- macOS 10.15 Sierra and above **for development** (due to Xcode)
- OS X 10.9 Mavericks and above **for use** (due to Swift)


Expand Down Expand Up @@ -84,7 +84,11 @@ HEATSINK_1 34.0°C
MEM_SLOTS_PROXIMITY 36.0°C
PALM_REST 27.0°C
-- Fan --
[id 0] Right Side
[id 0] Left side
Min: 2160 RPM
Max: 5927 RPM
Current: 2158 RPM
[id 1] Right Side
Min: 1299 RPM
Max: 6199 RPM
Current: 1292 RPM
Expand All @@ -96,6 +100,7 @@ Battery Ok: true
Battery Count: 1
-- Misc --
Disc in ODD: false
SMC Clock: 32998
```


Expand Down
29 changes: 21 additions & 8 deletions SMCKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -293,16 +293,16 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0700;
LastUpgradeCheck = 0900;
LastUpgradeCheck = 1130;
ORGANIZATIONNAME = beltex;
TargetAttributes = {
4C982BDC1A040CD500F4A639 = {
CreatedOnToolsVersion = 6.1;
LastSwiftMigration = 0900;
LastSwiftMigration = 1130;
};
4CDDA69C1AEC0D010037CE7B = {
CreatedOnToolsVersion = 6.3.1;
LastSwiftMigration = 0900;
LastSwiftMigration = 1130;
};
4CE5668A1A00A48800787F6B = {
CreatedOnToolsVersion = 6.1;
Expand All @@ -316,10 +316,11 @@
};
buildConfigurationList = 4CE566851A00A48800787F6B /* Build configuration list for PBXProject "SMCKit" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
en,
Base,
);
mainGroup = 4CE566811A00A48800787F6B;
productRefGroup = 4CE5668C1A00A48800787F6B /* Products */;
Expand Down Expand Up @@ -406,6 +407,7 @@
4C982BE11A040CD500F4A639 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_IDENTITY = "-";
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
Expand All @@ -414,24 +416,26 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
};
name = Debug;
};
4C982BE21A040CD500F4A639 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_IDENTITY = "-";
OTHER_SWIFT_FLAGS = "";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
};
name = Release;
};
4CDDA6A11AEC0D010037CE7B /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_IDENTITY = "-";
DEBUG_INFORMATION_FORMAT = dwarf;
GCC_NO_COMMON_BLOCKS = YES;
GCC_PREPROCESSOR_DEFINITIONS = (
Expand All @@ -441,26 +445,28 @@
MACOSX_DEPLOYMENT_TARGET = 10.10;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
};
name = Debug;
};
4CDDA6A21AEC0D010037CE7B /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_IDENTITY = "-";
COPY_PHASE_STRIP = NO;
GCC_NO_COMMON_BLOCKS = YES;
MACOSX_DEPLOYMENT_TARGET = 10.10;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
};
name = Release;
};
4CE5669F1A00A48800787F6B /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
Expand All @@ -469,12 +475,14 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
Expand Down Expand Up @@ -515,6 +523,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
Expand All @@ -523,12 +532,14 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
Expand Down Expand Up @@ -571,6 +582,7 @@
INFOPLIST_FILE = SMCKit/Info.plist;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.15;
OTHER_SWIFT_FLAGS = "-D DEBUG";
PRODUCT_BUNDLE_IDENTIFIER = "beltex.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -594,6 +606,7 @@
INFOPLIST_FILE = SMCKit/Info.plist;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.15;
PRODUCT_BUNDLE_IDENTIFIER = "beltex.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0900"
LastUpgradeVersion = "1130"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -26,10 +26,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
Expand All @@ -39,15 +36,14 @@
ReferencedContainer = "container:SMCKit.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
<Testables>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
debugAsWhichUser = "root"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand All @@ -64,8 +60,6 @@
ReferencedContainer = "container:SMCKit.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand Down
4 changes: 2 additions & 2 deletions SMCKit/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>0.2.0-dev</string>
<string>0.3.0-dev</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2014 beltex. All rights reserved.</string>
<string>Copyright © 2020 beltex. All rights reserved.</string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
Expand Down
63 changes: 49 additions & 14 deletions SMCKit/SMC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ extension Double {
public extension FourCharCode {

init(fromString str: String) {
precondition(str.characters.count == 4)
precondition(str.count == 4)

self = str.utf8.reduce(0) { sum, character in
return sum << 8 | UInt32(character)
Expand Down Expand Up @@ -434,6 +434,19 @@ extension SMCKit {

return true
}

public static func hexStringToAscii(_ hexString : String) -> String {

let pattern = "(0x)?([0-9a-f]{2})"
let regex = try! NSRegularExpression(pattern: pattern, options: .caseInsensitive)
let nsString = hexString as NSString
let matches = regex.matches(in: hexString, options: [], range: NSMakeRange(0, nsString.length))
let characters = matches.map {
Character(UnicodeScalar(UInt32(nsString.substring(with: $0.range(at: 2)), radix: 16)!)!)
}
return String(characters)
}

}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -560,18 +573,18 @@ public struct TemperatureSensor {
}

public enum TemperatureUnit {
case celius
case celsius
case fahrenheit
case kelvin

public static func toFahrenheit(_ celius: Double) -> Double {
public static func toFahrenheit(_ celsius: Double) -> Double {
// https://en.wikipedia.org/wiki/Fahrenheit#Definition_and_conversions
return (celius * 1.8) + 32
return (celsius * 1.8) + 32
}

public static func toKelvin(_ celius: Double) -> Double {
public static func toKelvin(_ celsius: Double) -> Double {
// https://en.wikipedia.org/wiki/Kelvin
return celius + 273.15
return celsius + 273.15
}
}

Expand All @@ -590,27 +603,26 @@ extension SMCKit {

public static func allUnknownTemperatureSensors() throws -> [TemperatureSensor] {
let keys = try allKeys()

return keys.filter { $0.code.toString().hasPrefix("T") &&
$0.info == DataTypes.SP78 &&
TemperatureSensors.all[$0.code] == nil }
.map { TemperatureSensor(name: "Unknown", code: $0.code) }
.map { TemperatureSensor(name: "Unknown - \($0.code.toString())", code: $0.code) }
}

/// Get current temperature of a sensor
public static func temperature(_ sensorCode: FourCharCode,
unit: TemperatureUnit = .celius) throws -> Double {
unit: TemperatureUnit = .celsius) throws -> Double {
let data = try readData(SMCKey(code: sensorCode, info: DataTypes.SP78))

let temperatureInCelius = Double(fromSP78: (data.0, data.1))
let temperatureInCelsius = Double(fromSP78: (data.0, data.1))

switch unit {
case .celius:
return temperatureInCelius
case .celsius:
return temperatureInCelsius
case .fahrenheit:
return TemperatureUnit.toFahrenheit(temperatureInCelius)
return TemperatureUnit.toFahrenheit(temperatureInCelsius)
case .kelvin:
return TemperatureUnit.toKelvin(temperatureInCelius)
return TemperatureUnit.toKelvin(temperatureInCelsius)
}
}
}
Expand Down Expand Up @@ -732,6 +744,11 @@ extension SMCKit {
}
}

//------------------------------------------------------------------------------
// MARK: Power
//------------------------------------------------------------------------------


//------------------------------------------------------------------------------
// MARK: Miscellaneous
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -785,3 +802,21 @@ extension SMCKit {
isCharging: isCharging)
}
}

public struct clockInfo {
public let timeSeconds: UInt32
}

extension SMCKit {
public static func getSMCClockTimeInSeconds() throws -> clockInfo {
let clockKey = SMCKey(code: FourCharCode(fromStaticString: "CLKT"),
info: DataTypes.UInt32)

let timeSecondsData = try readData(clockKey)

let timeSecondsCount = UInt32(uint32(fromBytes: (timeSecondsData.0, timeSecondsData.1, timeSecondsData.2, timeSecondsData.3)))

return clockInfo(timeSeconds: timeSecondsCount)
}

}
Loading