aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--smc-help.pd59
-rw-r--r--smc.c405
-rw-r--r--smc.h107
4 files changed, 572 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 6ead937..9498e23 100644
--- a/Makefile
+++ b/Makefile
@@ -8,7 +8,7 @@ SOURCES =
# For objects that only build on certain platforms, add those to the SOURCES
# line for the right platforms.
-SOURCES_Darwin = ambient_light_sensor.c iodisplay.c keyboard_light.c sudden_motion_sensor.c
+SOURCES_Darwin = ambient_light_sensor.c iodisplay.c keyboard_light.c sudden_motion_sensor.c smc.c
SOURCES_Linux =
SOURCES_Windows =
diff --git a/smc-help.pd b/smc-help.pd
new file mode 100644
index 0000000..8e34c16
--- /dev/null
+++ b/smc-help.pd
@@ -0,0 +1,59 @@
+#N canvas 139 41 559 441 10;
+#X obj 4 398 cnv 15 550 20 empty \$0-pddp.cnv.footer empty 20 12 0
+14 -228856 -66577 0;
+#X obj 4 -17 cnv 15 550 40 empty \$0-pddp.cnv.header smc 3 12 0 18
+-204280 -1 0;
+#X obj 3 189 cnv 3 550 3 empty \$0-pddp.cnv.inlets inlet 15 12 0 13
+-228856 -1 0;
+#N canvas 779 22 494 344 META 0;
+#X text 12 25 KEYWORDS control number float store;
+#X text 12 45 LICENSE GPL;
+#X text 12 65 TEMPLATE template-help.pd v0.1;
+#X text 12 85 PLATFORM windows macosx gnulinux;
+#X text 12 105 DATATYPE float list;
+#X text 12 5 GENRE storage;
+#X text 12 125 LIBRARY internal;
+#X text 12 145 WEBSITE http://crca.ucsd.edu/~msp/;
+#X text 12 165 RELEASE_DATE 2007-08-24;
+#X text 12 185 RELEASE_VERSION 0.40-test05;
+#X restore 504 400 pd META;
+#X obj 3 258 cnv 3 550 3 empty \$0-pddp.cnv.outlets outlets 15 12 0
+13 -228856 -1 0;
+#X obj 3 338 cnv 3 550 3 empty \$0-pddp.cnv.more_info more_info 15
+12 0 13 -228856 -1 0;
+#X text 102 208 BANG - output current screen brightness;
+#X msg 146 48 bang;
+#X text 183 46 output sensor values;
+#X text 102 227 "info" - outputs info about the sensor on the right
+outlet;
+#X obj 72 305 cnv 17 3 20 empty \$0-pddp.cnv.let.1 1 5 9 0 16 -228856
+-162280 0;
+#X obj 72 279 cnv 17 3 20 empty \$0-pddp.cnv.let.0 0 5 9 0 16 -228856
+-162280 0;
+#X text 103 276 LIST - a list with the values of the left and right
+sensors;
+#X text 103 306 selector series with info about the sensor;
+#X obj 489 -14 apple/smc;
+#X obj 146 111 apple/smc;
+#X obj 197 134 pddp/print;
+#X obj 439 4 pddp/pddplink http://wiki.puredata.info/en/apple/smc -text
+pdpedia: apple/smc;
+#X text 15 6 description: interface to Apple System Management Controller
+;
+#X obj 69 163 pddp/print;
+#X obj 105 360 pddp/pddplink http://en.wikipedia.org/wiki/System_Management_Controller
+;
+#X obj 143 142 print;
+#X msg 158 70 keys;
+#X text 196 71 output a list of all available keys in the right outlet
+;
+#X msg 53 64 symbol TC0D;
+#X msg 167 90 info;
+#X text 203 89 output all status info;
+#X connect 7 0 15 0;
+#X connect 15 0 19 0;
+#X connect 15 0 21 0;
+#X connect 15 1 16 0;
+#X connect 22 0 15 0;
+#X connect 24 0 15 0;
+#X connect 25 0 15 0;
diff --git a/smc.c b/smc.c
new file mode 100644
index 0000000..d5acf7a
--- /dev/null
+++ b/smc.c
@@ -0,0 +1,405 @@
+/* --------------------------------------------------------------------------*/
+/* */
+/* read the System Management Controller on Apple Mac OS X */
+/* Written by Hans-Christoph Steiner <hans@eds.org> */
+/* */
+/* Copyright (C) 2008-2009 Hans-Christoph Steiner */
+/* Copyright (C) 2006 devnull */
+/* */
+/* This program is free software; you can redistribute it and/or */
+/* modify it under the terms of the GNU General Public License */
+/* as published by the Free Software Foundation; either version 3 */
+/* of the License, or (at your option) any later version. */
+/* */
+/* See file LICENSE for further informations on licensing terms. */
+/* */
+/* This program is distributed in the hope that it will be useful, */
+/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
+/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
+/* GNU General Public License for more details. */
+/* */
+/* You should have received a copy of the GNU General Public License */
+/* along with this program; if not, write to the Free Software Foundation, */
+/* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */
+/* */
+/* --------------------------------------------------------------------------*/
+
+#include <mach/mach.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <IOKit/IOKitLib.h>
+
+#include "m_pd.h"
+#include "smc.h"
+
+//#define DEBUG(x)
+#define DEBUG(x) x
+
+/*------------------------------------------------------------------------------
+ * CLASS DEF
+ */
+
+static t_class *smc_class;
+
+typedef struct _smc {
+ t_object x_obj;
+ t_symbol* key;
+ t_outlet* data_outlet;
+ t_outlet* status_outlet;
+} t_smc;
+
+static io_connect_t conn;
+
+
+/*------------------------------------------------------------------------------
+ * WEIRD SHIT TO GET RID OF (why not just use the standard functions?)
+ */
+
+UInt32 _strtoul(char *str, int size, int base)
+{
+ UInt32 total = 0;
+ int i;
+
+ for (i = 0; i < size; i++)
+ {
+ if (base == 16)
+ total += str[i] << (size - 1 - i) * 8;
+ else
+ total += (unsigned char) (str[i] << (size - 1 - i) * 8);
+ }
+ return total;
+}
+
+void _ultostr(char *str, UInt32 val)
+{
+ str[0] = '\0';
+ sprintf(str, "%c%c%c%c",
+ (unsigned int) val >> 24,
+ (unsigned int) val >> 16,
+ (unsigned int) val >> 8,
+ (unsigned int) val);
+}
+
+float _strtof(char *str, int size, int e)
+{
+ float total = 0;
+ int i;
+
+ for (i = 0; i < size; i++)
+ {
+ if (i == (size - 1))
+ total += (str[i] & 0xff) >> e;
+ else
+ total += str[i] << (size - 1 - i) * (8 - e);
+ }
+
+ return total;
+}
+
+/*------------------------------------------------------------------------------
+ * SUPPORT FUNCTIONS
+ */
+
+kern_return_t SMCOpen(void)
+{
+ kern_return_t result;
+ mach_port_t masterPort;
+ io_iterator_t iterator;
+ io_object_t device;
+
+ result = IOMasterPort(MACH_PORT_NULL, &masterPort);
+
+ CFMutableDictionaryRef matchingDictionary = IOServiceMatching("AppleSMC");
+ result = IOServiceGetMatchingServices(masterPort, matchingDictionary, &iterator);
+ if (result != kIOReturnSuccess)
+ {
+ post("Error: IOServiceGetMatchingServices() = %08x\n", result);
+ return 1;
+ }
+
+ device = IOIteratorNext(iterator);
+ IOObjectRelease(iterator);
+ if (device == 0)
+ {
+ post("Error: no SMC found\n");
+ return 1;
+ }
+
+ result = IOServiceOpen(device, mach_task_self(), 0, &conn);
+ IOObjectRelease(device);
+ if (result != kIOReturnSuccess)
+ {
+ post("Error: IOServiceOpen() = %08x\n", result);
+ return 1;
+ }
+
+ return kIOReturnSuccess;
+}
+
+kern_return_t SMCClose()
+{
+ return IOServiceClose(conn);
+}
+
+
+kern_return_t SMCCall(int index, SMCKeyData_t *inputStructure, SMCKeyData_t *outputStructure)
+{
+ IOItemCount structureInputSize;
+ IOByteCount structureOutputSize;
+
+ structureInputSize = sizeof(SMCKeyData_t);
+ structureOutputSize = sizeof(SMCKeyData_t);
+
+ return IOConnectMethodStructureIStructureO(
+ conn,
+ index,
+ structureInputSize,
+ &structureOutputSize,
+ inputStructure,
+ outputStructure
+ );
+}
+
+kern_return_t SMCReadKey(UInt32Char_t key, SMCVal_t *val)
+{
+ kern_return_t result;
+ SMCKeyData_t inputStructure;
+ SMCKeyData_t outputStructure;
+
+ memset(&inputStructure, 0, sizeof(SMCKeyData_t));
+ memset(&outputStructure, 0, sizeof(SMCKeyData_t));
+ memset(val, 0, sizeof(SMCVal_t));
+
+ inputStructure.key = _strtoul(key, 4, 16);
+ sprintf(val->key, key);
+ inputStructure.data8 = SMC_CMD_READ_KEYINFO;
+
+ result = SMCCall(KERNEL_INDEX_SMC, &inputStructure, &outputStructure);
+ if (result != kIOReturnSuccess)
+ return result;
+
+ val->dataSize = outputStructure.keyInfo.dataSize;
+ _ultostr(val->dataType, outputStructure.keyInfo.dataType);
+ inputStructure.keyInfo.dataSize = val->dataSize;
+ inputStructure.data8 = SMC_CMD_READ_BYTES;
+
+ result = SMCCall(KERNEL_INDEX_SMC, &inputStructure, &outputStructure);
+ if (result != kIOReturnSuccess)
+ return result;
+
+ memcpy(val->bytes, outputStructure.bytes, sizeof(outputStructure.bytes));
+
+ return kIOReturnSuccess;
+}
+
+kern_return_t SMCWriteKey(SMCVal_t writeVal)
+{
+ kern_return_t result;
+ SMCKeyData_t inputStructure;
+ SMCKeyData_t outputStructure;
+
+ SMCVal_t readVal;
+
+ result = SMCReadKey(writeVal.key, &readVal);
+ if (result != kIOReturnSuccess)
+ return result;
+
+ if (readVal.dataSize != writeVal.dataSize)
+ return kIOReturnError;
+
+ memset(&inputStructure, 0, sizeof(SMCKeyData_t));
+ memset(&outputStructure, 0, sizeof(SMCKeyData_t));
+
+ inputStructure.key = _strtoul(writeVal.key, 4, 16);
+ inputStructure.data8 = SMC_CMD_WRITE_BYTES;
+ inputStructure.keyInfo.dataSize = writeVal.dataSize;
+ memcpy(inputStructure.bytes, writeVal.bytes, sizeof(writeVal.bytes));
+
+ result = SMCCall(KERNEL_INDEX_SMC, &inputStructure, &outputStructure);
+ if (result != kIOReturnSuccess)
+ return result;
+
+ return kIOReturnSuccess;
+}
+
+UInt32 SMCReadIndexCount(void)
+{
+ SMCVal_t val;
+
+ SMCReadKey("#KEY", &val);
+ return _strtoul(val.bytes, val.dataSize, 10);
+}
+
+double SMCGetTemperature(char *key)
+{
+ SMCVal_t val;
+ kern_return_t result;
+
+ result = SMCReadKey(key, &val);
+ if (result == kIOReturnSuccess) {
+ // read succeeded - check returned value
+ if (val.dataSize > 0) {
+ if (strcmp(val.dataType, DATATYPE_SP78) == 0) {
+ // convert fp78 value to temperature
+ int intValue = (val.bytes[0] * 256 + val.bytes[1]) >> 2;
+ return intValue / 64.0;
+ }
+ }
+ }
+ // read failed
+ return 0.0;
+}
+
+kern_return_t SMCSetFanRpm(char *key, int rpm)
+{
+ SMCVal_t val;
+
+ strcpy(val.key, key);
+ val.bytes[0] = (rpm << 2) / 256;
+ val.bytes[1] = (rpm << 2) % 256;
+ val.dataSize = 2;
+ return SMCWriteKey(val);
+}
+
+int SMCGetFanRpm(char *key)
+{
+ SMCVal_t val;
+ kern_return_t result;
+
+ result = SMCReadKey(key, &val);
+ if (result == kIOReturnSuccess) {
+ // read succeeded - check returned value
+ if (val.dataSize > 0) {
+ if (strcmp(val.dataType, DATATYPE_FPE2) == 0) {
+ // convert FPE2 value to int value
+ return (int)_strtof(val.bytes, val.dataSize, 2);
+ }
+ }
+ }
+ // read failed
+ return -1;
+}
+
+
+
+/*------------------------------------------------------------------------------
+ * IMPLEMENTATION
+ */
+
+
+static void smc_symbol(t_smc* x, t_symbol* key)
+{
+ DEBUG(post("smc_symbol"););
+ kern_return_t result;
+ SMCVal_t val;
+ t_atom output_atom;
+
+ SMCOpen();
+ result = SMCReadKey(key->s_name, &val);
+ if (result == kIOReturnSuccess) {
+ // read succeeded - check returned value
+ x->key = key;
+ if (val.dataSize > 0) {
+ if (strcmp(val.dataType, DATATYPE_SP78) == 0) {
+ // convert sp78 value to temperature
+ int intValue = (val.bytes[0] * 256 + val.bytes[1]) >> 2;
+ SETFLOAT(&output_atom, intValue / 64.0);
+ }
+ outlet_anything(x->data_outlet, key, 1, &output_atom);
+ }
+/*
+ if ((strcmp(val.dataType, DATATYPE_UINT8) == 0) ||
+ (strcmp(val.dataType, DATATYPE_UINT16) == 0) ||
+ (strcmp(val.dataType, DATATYPE_UINT32) == 0)) {
+ SETFLOAT(&output_atom, _strtoul(val.bytes, val.dataSize, 10));
+ outlet_anything(x->status_outlet, gensym("sensor"), 1, &output_atom);
+ } else if (strcmp(val.dataType, DATATYPE_FPE2) == 0) {
+ SETFLOAT(&output_atom, _strtof(val.bytes, val.dataSize, 2));
+ outlet_anything(x->status_outlet, gensym("sensor"), 1, &output_atom);
+ } else {
+ t_atom output_list[val.dataSize];
+ for (i = 0; i < val.dataSize; i++)
+ SETFLOAT(&output_atom + i, (unsigned char) val.bytes[i]);
+ outlet_anything(x->status_outlet, gensym("sensor"),
+ val.dataSize, &output_list);
+ }
+*/
+ }
+ SMCClose();
+}
+
+static void smc_bang(t_smc* x)
+{
+ smc_symbol(x, x->key);
+}
+
+static void smc_keys(t_smc* x)
+{
+ DEBUG(post("smc_keys"););
+ kern_return_t result;
+ SMCKeyData_t inputStructure;
+ SMCKeyData_t outputStructure;
+
+ int totalKeys, i;
+ UInt32Char_t key;
+ SMCVal_t val;
+
+ SMCOpen();
+ totalKeys = SMCReadIndexCount();
+ t_atom output_list[totalKeys];
+ for (i = 0; i < totalKeys; i++)
+ {
+ memset(&inputStructure, 0, sizeof(SMCKeyData_t));
+ memset(&outputStructure, 0, sizeof(SMCKeyData_t));
+ memset(&val, 0, sizeof(SMCVal_t));
+
+ inputStructure.data8 = SMC_CMD_READ_INDEX;
+ inputStructure.data32 = i;
+
+ result = SMCCall(KERNEL_INDEX_SMC, &inputStructure, &outputStructure);
+ if (result != kIOReturnSuccess)
+ continue;
+
+ _ultostr(key, outputStructure.key);
+ SETSYMBOL(output_list + i, gensym(key));
+ }
+ SMCClose();
+ outlet_anything(x->status_outlet, gensym("keys"), totalKeys, output_list);
+}
+
+static void smc_info(t_smc* x)
+{
+ t_atom output_atom;
+ SETSYMBOL(&output_atom, x->key);
+ outlet_anything(x->status_outlet, gensym("key"), 1, &output_atom);
+ smc_keys(x);
+}
+
+static void *smc_new(void)
+{
+ DEBUG(post("smc_new"););
+ t_smc *x = (t_smc *)pd_new(smc_class);
+
+ x->key = &s_;
+ x->data_outlet = outlet_new(&x->x_obj, &s_list);
+ x->status_outlet = outlet_new(&x->x_obj, &s_anything);
+
+ return (x);
+}
+
+void smc_setup(void)
+{
+ smc_class = class_new(gensym("smc"),
+ (t_newmethod)smc_new,
+ NULL,
+ sizeof(t_smc),
+ CLASS_DEFAULT,
+ 0);
+ /* add inlet datatype methods */
+ class_addbang(smc_class,(t_method) smc_bang);
+ class_addsymbol(smc_class,(t_method) smc_symbol);
+ class_addmethod(smc_class,(t_method) smc_info, gensym("info"), 0);
+ class_addmethod(smc_class,(t_method) smc_keys, gensym("keys"), 0);
+}
diff --git a/smc.h b/smc.h
new file mode 100644
index 0000000..8c2a227
--- /dev/null
+++ b/smc.h
@@ -0,0 +1,107 @@
+/*
+ * Apple System Management Control (SMC) Tool
+ * Copyright (C) 2006 devnull
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef __SMC_H__
+#define __SMC_H__
+#endif
+
+#define VERSION "0.01"
+
+#define OP_NONE 0
+#define OP_LIST 1
+#define OP_READ 2
+#define OP_READ_FAN 3
+#define OP_WRITE 4
+
+#define KERNEL_INDEX_SMC 2
+
+#define SMC_CMD_READ_BYTES 5
+#define SMC_CMD_WRITE_BYTES 6
+#define SMC_CMD_READ_INDEX 8
+#define SMC_CMD_READ_KEYINFO 9
+#define SMC_CMD_READ_PLIMIT 11
+#define SMC_CMD_READ_VERS 12
+
+#define DATATYPE_FPE2 "fpe2"
+#define DATATYPE_UINT8 "ui8 "
+#define DATATYPE_UINT16 "ui16"
+#define DATATYPE_UINT32 "ui32"
+#define DATATYPE_SP78 "sp78"
+
+// key values
+// lots more here:
+// http://discussions.apple.com/thread.jspa?threadID=734247&tstart=0
+#define SMC_KEY_CPU_TEMP "TC0D"
+#define SMC_KEY_FAN0_RPM_MIN "F0Mn"
+#define SMC_KEY_FAN1_RPM_MIN "F1Mn"
+#define SMC_KEY_FAN0_RPM_CUR "F0Ac"
+#define SMC_KEY_FAN1_RPM_CUR "F1Ac"
+
+
+typedef struct {
+ char major;
+ char minor;
+ char build;
+ char reserved[1];
+ UInt16 release;
+} SMCKeyData_vers_t;
+
+typedef struct {
+ UInt16 version;
+ UInt16 length;
+ UInt32 cpuPLimit;
+ UInt32 gpuPLimit;
+ UInt32 memPLimit;
+} SMCKeyData_pLimitData_t;
+
+typedef struct {
+ UInt32 dataSize;
+ UInt32 dataType;
+ char dataAttributes;
+} SMCKeyData_keyInfo_t;
+
+typedef char SMCBytes_t[32];
+
+typedef struct {
+ UInt32 key;
+ SMCKeyData_vers_t vers;
+ SMCKeyData_pLimitData_t pLimitData;
+ SMCKeyData_keyInfo_t keyInfo;
+ char result;
+ char status;
+ char data8;
+ UInt32 data32;
+ SMCBytes_t bytes;
+} SMCKeyData_t;
+
+typedef char UInt32Char_t[5];
+
+typedef struct {
+ UInt32Char_t key;
+ UInt32 dataSize;
+ UInt32Char_t dataType;
+ SMCBytes_t bytes;
+} SMCVal_t;
+
+
+// prototypes
+//double SMCGetTemperature(char *key);
+//kern_return_t SMCSetFanRpm(char *key, int rpm);
+//int SMCGetFanRpm(char *key);
+