From 547e7dc094030c2f7e2ac4e3f0292ff9d3488e93 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Thu, 10 Jan 2013 23:11:23 +0100 Subject: [PATCH] dmi: implementation for Mac OS X. We use the IOKit framework. --- src/daemon/Makefile.am | 5 +- src/daemon/dmi-osx.c | 106 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 src/daemon/dmi-osx.c diff --git a/src/daemon/Makefile.am b/src/daemon/Makefile.am index 2f27b3b9..a0dc7bf5 100644 --- a/src/daemon/Makefile.am +++ b/src/daemon/Makefile.am @@ -45,7 +45,10 @@ endif if HOST_OS_OSX liblldpd_la_SOURCES += \ interfaces-bsd.c \ - dmi-dummy.c + dmi-osx.c +liblldpd_la_LDFLAGS = -framework Foundation +liblldpd_la_LDFLAGS += -framework CoreFoundation -framework IOKit +liblldpd_la_LDFLAGS += -framework IOKit endif # Add SNMP support if needed diff --git a/src/daemon/dmi-osx.c b/src/daemon/dmi-osx.c new file mode 100644 index 00000000..65d476d0 --- /dev/null +++ b/src/daemon/dmi-osx.c @@ -0,0 +1,106 @@ +/* -*- mode: c; c-file-style: "openbsd" -*- */ +/* + * Copyright (c) 2012 Vincent Bernat + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include "lldpd.h" + +#include +#include + +#ifdef ENABLE_LLDPMED +static char* +dmi_get(const char *classname, CFStringRef property) +{ + char *result = NULL; + CFMutableDictionaryRef matching = NULL; + CFTypeRef cfres = NULL; + io_service_t service = 0; + matching = IOServiceMatching(classname); + if (!matching) { + log_debug("localchassis", "cannot get %s class from registry", + classname); + goto end; + } + service = IOServiceGetMatchingService(kIOMasterPortDefault, matching); + if (!service) { + log_warnx("localchassis", "cannot get matching %s class from registry", + classname); + goto end; + } + cfres = IORegistryEntryCreateCFProperty(service, property, + kCFAllocatorDefault, kNilOptions); + if (!cfres) { + log_debug("localchassis", "cannot find property %s in class %s in registry", + CFStringGetCStringPtr(property, kCFStringEncodingMacRoman), + classname); + goto end; + } + + if (CFGetTypeID(cfres) == CFStringGetTypeID()) + result = strdup(CFStringGetCStringPtr((CFStringRef)cfres, kCFStringEncodingMacRoman)); + else if (CFGetTypeID(cfres) == CFDataGetTypeID()) { + /* OK, we know this is a string. */ + result = calloc(1, CFDataGetLength((CFDataRef)cfres) + 1); + if (!result) goto end; + memcpy(result, CFDataGetBytePtr((CFDataRef)cfres), + CFDataGetLength((CFDataRef)cfres)); + } else log_debug("localchassis", "unknown type for property %s in class %s", + CFStringGetCStringPtr(property, kCFStringEncodingMacRoman), + classname); + +end: + if (cfres) CFRelease(cfres); + if (service) IOObjectRelease(service); + return result; +} + +char* +dmi_hw() +{ + return dmi_get("IOPlatformExpertDevice", CFSTR("version")); +} + +char* +dmi_fw() +{ + /* Dunno where it is. Maybe in SMC? */ + return NULL; +} + +char* +dmi_sn() +{ + return dmi_get("IOPlatformExpertDevice", CFSTR("IOPlatformSerialNumber")); +} + +char* +dmi_manuf() +{ + return dmi_get("IOPlatformExpertDevice", CFSTR("manufacturer")); +} + +char* +dmi_model() +{ + return dmi_get("IOPlatformExpertDevice", CFSTR("model")); +} + +char* +dmi_asset() +{ + return dmi_get("IOPlatformExpertDevice", CFSTR("board-id")); +} +#endif -- 2.39.5