### Quick Start: Read CPU Temperature in Swift Source: https://github.com/srimanachanta/smckit/blob/main/README.md Use this snippet to quickly read the CPU temperature using the SMCKit Swift wrapper. Ensure SMCKit is imported. ```swift import SMCKit let temp: Float = try SMCKit.shared.read("TC0P") print("CPU Temperature: \(temp)°C") ``` -------------------------------- ### Quick Start: Read SMC Value using C Library Source: https://github.com/srimanachanta/smckit/blob/main/README.md This C code demonstrates how to open a connection to the SMC, read a key's value, and close the connection. Error handling is included. ```c #include io_connect_t conn; if (SMCOpen(&conn) != kIOReturnSuccess) { // Handle error } UInt32Char_t key = {{'T', 'C', '0', 'P', '\0' }}; SMCVal_t val; SMCResult_t result = SMCReadKey(&key, &val, conn); if (result.kern_res == kIOReturnSuccess && result.smc_res == kSMCReturnSuccess) { // Use val.bytes } SMCClose(conn); ``` -------------------------------- ### Writing Big-Endian Values with SMCKit Source: https://github.com/srimanachanta/smckit/blob/main/README.md This example shows how to write a big-endian UInt32 value to the 'RBID' SMC key using the `BigEndian` wrapper. ```swift try SMCKit.shared.write("RBID", BigEndian(4)) ``` -------------------------------- ### Reading SMC Values in Swift Source: https://github.com/srimanachanta/smckit/blob/main/README.md Examples of reading different types of SMC values including temperature, fan speed, and boolean flags. Requires SMCKit import. ```swift let temp: Float = try SMCKit.shared.read("TC0P") let fanSpeed: UInt16 = try SMCKit.shared.read("F0Ac") let flag: Bool = try SMCKit.shared.read("SOME") ``` -------------------------------- ### Getting All Available SMC Keys Source: https://github.com/srimanachanta/smckit/blob/main/README.md Retrieves an array containing all the keys currently available from the SMC. ```swift let allKeys = try SMCKit.shared.allKeys() ``` -------------------------------- ### Get SMC Key Information Source: https://context7.com/srimanachanta/smckit/llms.txt Retrieves metadata about an SMC key, including its data type and size. Requires importing SMCKit. ```swift import SMCKit // Get key information let info = try SMCKit.shared.getKeyInformation("TC0P") print("Type: \(info.type), Size: \(info.size)") // Output: Type: 1718383648 (flt ), Size: 4 ``` -------------------------------- ### Getting the Number of SMC Keys Source: https://github.com/srimanachanta/smckit/blob/main/README.md Returns the total count of available SMC keys on the system. ```swift let count = try SMCKit.shared.numKeys() ``` -------------------------------- ### Get SMC Key Information in C Source: https://context7.com/srimanachanta/smckit/llms.txt Query metadata for a specific key, such as data size and type, before performing read operations. ```c #include #include io_connect_t conn; if (SMCOpen(&conn) != kIOReturnSuccess) { return 1; } // Get key info for "TC0P" (FourCharCode: 0x54433050) UInt32 keyCode = ('T' << 24) | ('C' << 16) | ('0' << 8) | 'P'; SMCKeyData_keyInfo_t keyInfo; SMCResult_t result = SMCGetKeyInfo(keyCode, &keyInfo, conn); if (result.kern_res == kIOReturnSuccess && result.smc_res == kSMCReturnSuccess) { printf("Data size: %u bytes\n", keyInfo.dataSize); printf("Data type: 0x%08X\n", keyInfo.dataType); } SMCClose(conn); ``` -------------------------------- ### Getting SMC Key Information Source: https://github.com/srimanachanta/smckit/blob/main/README.md Retrieves information about an SMC key, including its data type and size. ```swift let info = try SMCKit.shared.getKeyInformation("TC0P") print("Type: \(info.type), Size: \(info.size)") ``` -------------------------------- ### Reading Big-Endian Values with SMCKit Source: https://github.com/srimanachanta/smckit/blob/main/README.md Use the `BigEndian` wrapper for SMC keys that use big-endian byte order. This example reads a UInt32 value for a key named '#KEY'. ```swift let count: BigEndian = try SMCKit.shared.read("#KEY") print(count.value) ``` -------------------------------- ### SMCKit Error Handling in Swift Source: https://github.com/srimanachanta/smckit/blob/main/README.md Demonstrates how to catch various `SMCError` types, including key not found, insufficient privileges, data type mismatches, connection failures, and invalid data sizes. ```swift do { let temp: Float = try SMCKit.shared.read("TC0P") } catch SMCError.keyNotFound(let key) { print("Key not found: \(key)") } catch SMCError.notPrivileged { print("Need elevated privileges") } catch SMCError.dataTypeMismatch(let key) { print("Wrong data type for key: \(key)") } catch SMCError.connectionFailed(let kIOReturn) { print("Failed to connect to SMC: \(kIOReturn)") } catch SMCError.invalidDataSize(let key, let expected, let actual) { print("Size mismatch for \(key): expected \(expected), got \(actual)") } catch SMCError.invalidStringData(let key) { print("Invalid ASCII string data for key: \(key)") } ``` -------------------------------- ### Open SMC Connection in C Source: https://context7.com/srimanachanta/smckit/llms.txt Initialize a connection to the Apple SMC service using the C library. ```c #include io_connect_t conn; kern_return_t result = SMCOpen(&conn); if (result != kIOReturnSuccess) { fprintf(stderr, "Failed to open SMC connection: %d\n", result); return 1; } // Use the connection... SMCClose(conn); ``` -------------------------------- ### Enumerate SMC Keys in Swift Source: https://context7.com/srimanachanta/smckit/llms.txt Retrieve the total count of available SMC keys and iterate through them. ```swift import SMCKit // Get total number of keys let count = try SMCKit.shared.numKeys() print("Total keys: \(count)") // Get all keys let allKeys = try SMCKit.shared.allKeys() for key in allKeys { print("Key: \(key.toString())") } ``` -------------------------------- ### Read CPU Proximity Temperature Source: https://context7.com/srimanachanta/smckit/llms.txt Reads the CPU proximity temperature in Celsius using the 'TC0P' key. Requires importing SMCKit. ```swift import SMCKit // Read CPU proximity temperature let cpuTemp: Float = try SMCKit.shared.read("TC0P") print("CPU Temperature: \(cpuTemp)°C") ``` -------------------------------- ### Read GPU Temperature Source: https://context7.com/srimanachanta/smckit/llms.txt Reads the GPU temperature in Celsius using the 'TG0P' key. Requires importing SMCKit. ```swift import SMCKit // Read GPU temperature let gpuTemp: Float = try SMCKit.shared.read("TG0P") print("GPU Temperature: \(gpuTemp)°C") ``` -------------------------------- ### Writing SMC Values in Swift Source: https://github.com/srimanachanta/smckit/blob/main/README.md Demonstrates how to write a UInt32 value to an SMC key. Ensure the key and value types are compatible. ```swift try SMCKit.shared.write("SOME", UInt32(42)) ``` -------------------------------- ### Enumerate SMC Keys by Index in C Source: https://context7.com/srimanachanta/smckit/llms.txt Iterate through SMC keys using their numerical index. ```c #include #include io_connect_t conn; if (SMCOpen(&conn) != kIOReturnSuccess) { return 1; } // Enumerate first 10 keys for (UInt32 i = 0; i < 10; i++) { UInt32Char_t key; SMCResult_t result = SMCGetKeyFromIndex(i, &key, conn); if (result.kern_res == kIOReturnSuccess && result.smc_res == kSMCReturnSuccess) { printf("Key %u: %c%c%c%c\n", i, key.chars[0], key.chars[1], key.chars[2], key.chars[3]); } } SMCClose(conn); ``` -------------------------------- ### Read Fan Speed as UInt16 Source: https://context7.com/srimanachanta/smckit/llms.txt Reads the fan speed from the SMC using the 'F0Ac' key. Requires importing SMCKit. ```swift import SMCKit // Read fan speed as UInt16 let fanSpeed: UInt16 = try SMCKit.shared.read("F0Ac") print("Fan speed: \(fanSpeed) RPM") ``` -------------------------------- ### Writing Raw Binary Data with SMCKit Source: https://github.com/srimanachanta/smckit/blob/main/README.md Writes raw binary data to an SMC key. The 'hex_' type is handled by `writeData`. ```swift try SMCKit.shared.writeData("SOME", Data([0x01, 0x02, 0x03, 0x04])) ``` -------------------------------- ### Write Platform Identifier String Source: https://context7.com/srimanachanta/smckit/llms.txt Writes an ASCII string to an SMC key of type 'ch8*'. The string is automatically null-padded to the key's size. Requires importing SMCKit. ```swift import SMCKit // Write platform identifier try SMCKit.shared.writeString("RPlt", "j614s") ``` -------------------------------- ### Writing ASCII String with SMCKit Source: https://github.com/srimanachanta/smckit/blob/main/README.md Writes an ASCII string to an SMC key. Null padding is automatically handled. The 'ch8*' type is used. ```swift try SMCKit.shared.writeString("RPlt", "j614s") ``` -------------------------------- ### Check if SMC Key Exists Source: https://context7.com/srimanachanta/smckit/llms.txt Checks if an SMC key exists on the system without attempting to read its value. Requires importing SMCKit. ```swift import SMCKit // Check if a key exists let exists = try SMCKit.shared.isKeyFound("TC0P") if exists { let temp: Float = try SMCKit.shared.read("TC0P") print("Temperature: \(temp)°C") } ``` -------------------------------- ### Write Raw Binary Data Source: https://context7.com/srimanachanta/smckit/llms.txt Writes variable-length raw binary data to an SMC key of type 'hex_'. The data size must match the key's expected size. Requires importing SMCKit. ```swift import SMCKit // Write raw binary data let bytes = Data([0x01, 0x02, 0x03, 0x04]) try SMCKit.shared.writeData("SOME", bytes) ``` -------------------------------- ### Read Platform Identifier String Source: https://context7.com/srimanachanta/smckit/llms.txt Reads an ASCII string from an SMC key of type 'ch8*'. Null padding is automatically handled. Requires importing SMCKit. ```swift import SMCKit // Read platform identifier string let platform: String = try SMCKit.shared.readString("RPlt") print("Platform: \(platform)") ``` -------------------------------- ### Reading Raw Binary Data with SMCKit Source: https://github.com/srimanachanta/smckit/blob/main/README.md Reads raw binary data from an SMC key. The 'hex_' type is handled by `readData`. ```swift let data: Data = try SMCKit.shared.readData("SOME") ``` -------------------------------- ### Reading ASCII String with SMCKit Source: https://github.com/srimanachanta/smckit/blob/main/README.md Reads an ASCII string from an SMC key. Null padding is automatically handled. The 'ch8*' type is used. ```swift let name: String = try SMCKit.shared.readString("RPlt") ``` -------------------------------- ### Checking if an SMC Key Exists Source: https://github.com/srimanachanta/smckit/blob/main/README.md Determines if a specific SMC key is present on the system using `isKeyFound`. ```swift let exists = try SMCKit.shared.isKeyFound("TC0P") ``` -------------------------------- ### Write SMC Keys in C Source: https://context7.com/srimanachanta/smckit/llms.txt Write data to an SMC key by specifying the key name, data type, and size. ```c #include #include io_connect_t conn; if (SMCOpen(&conn) != kIOReturnSuccess) { return 1; } SMCVal_t val; memset(&val, 0, sizeof(SMCVal_t)); // Set key name val.key.chars[0] = 'S'; val.key.chars[1] = 'O'; val.key.chars[2] = 'M'; val.key.chars[3] = 'E'; val.key.chars[4] = '\0'; // Set data type (ui32) val.dataType.chars[0] = 'u'; val.dataType.chars[1] = 'i'; val.dataType.chars[2] = '3'; val.dataType.chars[3] = '2'; val.dataType.chars[4] = '\0'; // Set size and value val.dataSize = 4; UInt32 value = 42; memcpy(val.bytes, &value, sizeof(UInt32)); SMCResult_t result = SMCWriteKey(&val, conn); if (result.kern_res == kIOReturnSuccess && result.smc_res == kSMCReturnSuccess) { printf("Value written successfully\n"); } SMCClose(conn); ``` -------------------------------- ### Write 32-bit Unsigned Value Source: https://context7.com/srimanachanta/smckit/llms.txt Writes a 32-bit unsigned integer value to an SMC key. Requires importing SMCKit. ```swift import SMCKit // Write a 32-bit unsigned value try SMCKit.shared.write("SOME", UInt32(42)) ``` -------------------------------- ### Read 64-bit Unsigned Value Source: https://context7.com/srimanachanta/smckit/llms.txt Reads a 64-bit unsigned integer value from an SMC key. Requires importing SMCKit. ```swift import SMCKit // Read 64-bit unsigned value let value64: UInt64 = try SMCKit.shared.read("BIGK") ``` -------------------------------- ### Cleanup SMC Cache (C) Source: https://context7.com/srimanachanta/smckit/llms.txt Call SMCCleanupCache() when completely done with SMC operations to clear the global key info cache. Ensure SMCOpen() and SMCClose() are managed appropriately in C/C++ projects. ```c #include // Cleanup the global key info cache // Call this when completely done with SMC operations SMCCleanupCache(); ``` -------------------------------- ### Read Raw Binary Data Source: https://context7.com/srimanachanta/smckit/llms.txt Reads variable-length raw binary data from an SMC key of type 'hex_'. Returns data as Foundation `Data`. Requires importing SMCKit. ```swift import SMCKit // Read raw binary data let rawData: Data = try SMCKit.shared.readData("SOME") print("Data bytes: \(rawData.map { String(format: "%02X", $0) }.joined())") ``` -------------------------------- ### C Library - Cache Cleanup Source: https://context7.com/srimanachanta/smckit/llms.txt Clean up the internal key info cache when done with SMC operations. This function should be called when you are completely finished with all SMC operations to release resources. ```APIDOC ## C Library - Cache Cleanup ### Description Clean up the internal key info cache when done with SMC operations. Call this when completely done with SMC operations. ### Method C Function Call ### Endpoint N/A (C Library Function) ### Parameters None ### Request Example ```c #include // Cleanup the global key info cache // Call this when completely done with SMC operations SMCCleanupCache(); ``` ### Response N/A (C Function) ### Error Handling N/A (C Function) ``` -------------------------------- ### Read 32-bit Unsigned Value Source: https://context7.com/srimanachanta/smckit/llms.txt Reads a 32-bit unsigned integer value from an SMC key. Requires importing SMCKit. ```swift import SMCKit // Read 32-bit unsigned value let value32: UInt32 = try SMCKit.shared.read("OTHR") ``` -------------------------------- ### Read 8-bit Unsigned Value Source: https://context7.com/srimanachanta/smckit/llms.txt Reads an 8-bit unsigned integer value from an SMC key. Requires importing SMCKit. ```swift import SMCKit // Read 8-bit unsigned value let value8: UInt8 = try SMCKit.shared.read("SOME") ``` -------------------------------- ### Write Boolean Flag Source: https://context7.com/srimanachanta/smckit/llms.txt Writes a boolean value to an SMC key. Requires importing SMCKit. ```swift import SMCKit // Write a boolean flag try SMCKit.shared.write("FLGK", true) ``` -------------------------------- ### Read Boolean Flag Source: https://context7.com/srimanachanta/smckit/llms.txt Reads a boolean flag from an SMC key. Non-zero byte values are interpreted as true. Requires importing SMCKit. ```swift import SMCKit // Read a boolean flag let isEnabled: Bool = try SMCKit.shared.read("FLGK") print("Feature enabled: \(isEnabled)") ``` -------------------------------- ### Write Big-Endian Value Source: https://context7.com/srimanachanta/smckit/llms.txt Writes a big-endian unsigned 32-bit integer value to an SMC key. Requires importing SMCKit. ```swift import SMCKit // Write a big-endian value try SMCKit.shared.write("RBID", BigEndian(4)) ``` -------------------------------- ### Read Other Big-Endian Values Source: https://context7.com/srimanachanta/smckit/llms.txt Reads other SMC keys that store integer values in big-endian byte order, such as 'RBID'. Requires importing SMCKit. ```swift import SMCKit // Read other big-endian values let rbid: BigEndian = try SMCKit.shared.read("RBID") print("RBID value: \(rbid.value)") ``` -------------------------------- ### Read SMC Keys in C Source: https://context7.com/srimanachanta/smckit/llms.txt Read values from specific SMC keys, interpreting bytes based on the expected data type. ```c #include #include #include io_connect_t conn; if (SMCOpen(&conn) != kIOReturnSuccess) { return 1; } // Read CPU temperature key "TC0P" UInt32Char_t key = {{'T', 'C', '0', 'P', '\0'}}; SMCVal_t val; SMCResult_t result = SMCReadKey(&key, &val, conn); if (result.kern_res == kIOReturnSuccess && result.smc_res == kSMCReturnSuccess) { // For float type (flt ), interpret bytes as float float temp; memcpy(&temp, val.bytes, sizeof(float)); printf("CPU Temperature: %.2f°C\n", temp); } SMCClose(conn); ``` -------------------------------- ### Read Big-Endian UInt32 Value Source: https://context7.com/srimanachanta/smckit/llms.txt Reads a 32-bit unsigned integer value stored in big-endian byte order using the `BigEndian` wrapper. Requires importing SMCKit. ```swift import SMCKit // Read key count (big-endian) let keyCount: BigEndian = try SMCKit.shared.read("#KEY") print("Total SMC keys: \(keyCount.value)") ``` -------------------------------- ### Handle SMC Errors in Swift Source: https://context7.com/srimanachanta/smckit/llms.txt Use the SMCError enum to catch and handle specific failure cases during SMC operations. ```swift import SMCKit do { let temp: Float = try SMCKit.shared.read("TC0P") print("Temperature: \(temp)°C") } catch SMCError.keyNotFound(let key) { print("Key not found: \(key)") } catch SMCError.notPrivileged { print("Need elevated privileges to access SMC") } catch SMCError.dataTypeMismatch(let key) { print("Wrong data type for key: \(key)") } catch SMCError.connectionFailed(let kIOReturn) { print("Failed to connect to SMC: \(kIOReturn)") } catch SMCError.invalidDataSize(let key, let expected, let actual) { print("Size mismatch for \(key): expected \(expected), got \(actual)") } catch SMCError.invalidStringData(let key) { print("Invalid ASCII string data for key: \(key)") } catch SMCError.unknown(let key, let kIOReturn, let smcResult) { print("Unknown error for \(key): IOReturn=\(kIOReturn), SMC=\(smcResult)") } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.