|
| 1 | +// |
| 2 | +// Magsafe.swift |
| 3 | +// AIBattery |
| 4 | +// |
| 5 | +// Created by whuan132 on 2/15/25. |
| 6 | +// © 2025 COLLWEB. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | + |
| 11 | +/// Represents the state of the MagSafe LED. |
| 12 | +enum MagSafeLedState: UInt8, CustomStringConvertible { |
| 13 | + case system = 0x00 |
| 14 | + case off = 0x01 |
| 15 | + case green = 0x03 |
| 16 | + case orange = 0x04 |
| 17 | + case errorOnce = 0x05 |
| 18 | + case errorPermSlow = 0x06 |
| 19 | + case errorPermFast = 0x07 |
| 20 | + case errorPermOff = 0x19 |
| 21 | + |
| 22 | + public var description: String { |
| 23 | + switch self { |
| 24 | + case .system: "system" |
| 25 | + case .off: "off" |
| 26 | + case .green: "green" |
| 27 | + case .orange: "orange" |
| 28 | + case .errorOnce: "errorOnce" |
| 29 | + case .errorPermSlow: "errorPermSlow" |
| 30 | + case .errorPermFast: "errorPermFast" |
| 31 | + case .errorPermOff: "errorPermOff" |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// MARK: - AppleSMC Extension – MagSafe Control |
| 37 | + |
| 38 | +extension AppleSMC { |
| 39 | + /// Sets the MagSafe LED to the specified state. |
| 40 | + func setMagSafeLedState(state: MagSafeLedState) throws { |
| 41 | + Logger.debug("SetMagSafeLedState(\(state)) called") |
| 42 | + let success = write(SMCKeys.magSafeLedKey, [state.rawValue]) |
| 43 | + if success != kIOReturnSuccess { |
| 44 | + throw SMCError.writeFailed |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /// Returns the current MagSafe LED state. |
| 49 | + func getMagSafeLedState() throws -> MagSafeLedState { |
| 50 | + Logger.debug("magSafeLedState() called") |
| 51 | + |
| 52 | + guard let smcVal = getValue(SMCKeys.magSafeLedKey) else { |
| 53 | + throw SMCError.noData |
| 54 | + } |
| 55 | + guard smcVal.dataSize == 1 else { |
| 56 | + throw SMCError.invalidDataLength(Int(smcVal.dataSize)) |
| 57 | + } |
| 58 | + |
| 59 | + let raw = smcVal.bytes[0] |
| 60 | + |
| 61 | + // Handle known LED states, fallback to .orange if undefined. |
| 62 | + if let state = MagSafeLedState(rawValue: raw), |
| 63 | + [.off, .green, .orange, .errorOnce, .errorPermSlow].contains(state) |
| 64 | + { |
| 65 | + Logger.debug("magSafeLedState() returned \(state)") |
| 66 | + return state |
| 67 | + } else if raw == 0x02 { |
| 68 | + Logger.debug("magSafeLedState() returned .green (special case)") |
| 69 | + return .green |
| 70 | + } else { |
| 71 | + Logger.debug("magSafeLedState() returned default (.orange)") |
| 72 | + return .orange |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /// Returns true if a MagSafe LED exists on the device. |
| 77 | + func checkMagSafeExistence() -> Bool { |
| 78 | + getValue(SMCKeys.magSafeLedKey) != nil |
| 79 | + } |
| 80 | + |
| 81 | + /// Sets the MagSafe LED to indicate charging (orange) or not charging (green). |
| 82 | + func setMagSafeCharging(charging: Bool) throws { |
| 83 | + try setMagSafeLedState(state: charging ? .orange : .green) |
| 84 | + } |
| 85 | + |
| 86 | + /// Returns true if the MagSafe is currently indicating charging (orange LED). |
| 87 | + func isMagSafeCharging() throws -> Bool { |
| 88 | + try getMagSafeLedState() == .orange |
| 89 | + } |
| 90 | +} |
0 commit comments