dnl $Id$ dnl $URL$ dnl LCD4Linux Drivers conf part dnl dnl Copyright (C) 1999, 2000, 2001, 2002, 2003 Michael Reinelt dnl Copyright (C) 2004 The LCD4Linux Team dnl dnl This file is part of LCD4Linux. dnl dnl LCD4Linux is free software; you can redistribute it and/or modify dnl it under the terms of the GNU General Public License as published by dnl the Free Software Foundation; either version 2, or (at your option) dnl any later version. dnl dnl LCD4Linux is distributed in the hope that it will be useful, dnl but WITHOUT ANY WARRANTY; without even the implied warranty of dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the dnl GNU General Public License for more details. dnl dnl You should have received a copy of the GNU General Public License dnl along with this program; if not, write to the Free Software dnl Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. AC_MSG_CHECKING([which drivers to compile]) AC_ARG_WITH( drivers, [ --with-drivers= compile driver for displays in ,] [ drivers may be separated with commas,] [ 'all' (default) compiles all available drivers,] [ drivers may be excluded with 'all,!',] [ (try 'all,\!' if your shell complains...)] [ possible drivers are:] [ ASTUSB, BeckmannEgle, BWCT, CrystalFontz, Curses, Cwlinux, D4D, DPF] [ EA232graphic, EFN, FutabaVFD, FW8888, G15, GLCD2USB, HD44780, HD44780-I2C,] [ IRLCD, LCD2USB, LCDLinux, LEDMatrix, LCDTerm, LPH7508, LUIse,] [ LW_ABP, M50530, MatrixOrbital, MatrixOrbitalGX, MilfordInstruments, MDM166A,] [ Newhaven, Noritake, NULL, Pertelian, PHAnderson,] [ PICGraphic, picoLCD, picoLCDGraphic, PNG, PPM, RouterBoard,] [ Sample, SamsungSPF, serdisplib, ShuttleVFD, SimpleLCD, st2205, T6963,] [ TeakLCM, Trefon, ULA200, USBHUB, USBLCD, VNC, WincorNixdorf, X11], drivers=$withval, drivers=all ) drivers=`echo $drivers|sed 's/,/ /g'` for driver in $drivers; do case $driver in !*) val="no" driver=`echo $driver|cut -c 2-` ;; *) val="yes" ;; esac case "$driver" in all) ASTUSB="yes" BECKMANNEGLE="yes" BWCT="yes" CRYSTALFONTZ="yes" CURSES="yes" CWLINUX="yes" D4D="yes" DPF="yes" EA232graphic="yes" EFN="yes" FUTABAVFD="yes" FW8888="yes" G15="yes" GLCD2USB="yes" HD44780="yes" HD44780_I2C="yes" IRLCD="yes" LCD2USB="yes" LCDLINUX="yes" LCDTERM="yes" LEDMATRIX="yes" LPH7508="yes" LUISE="yes" LW_ABP="yes" M50530="yes" MATRIXORBITAL="yes" MATRIXORBITALGX="yes" MDM166A="yes" MILINST="yes" NEWHAVEN="yes" NORITAKE="yes" NULL="yes" PERTELIAN="yes" PHANDERSON="yes" PICGRAPHIC="yes" PICOLCD="yes" PICOLCDGRAPHIC="yes" PNG="yes" PPM="yes" ROUTERBOARD="yes" SAMPLE="yes" SAMSUNGSPF="yes" ST2205="yes" SERDISPLIB="yes" SHUTTLEVFD="yes" SIMPLELCD="yes" T6963="yes" TeakLCM="yes" Trefon="yes" ULA200="yes" USBHUB="yes" USBLCD="yes" VNC="yes" WINCORNIXDORF="yes" X11="yes" ;; ASTUSB) ASTUSB=$val ;; BeckmannEgle) BECKMANNEGLE=$val ;; BWCT) BWCT=$val ;; CrystalFontz) CRYSTALFONTZ=$val ;; Curses) CURSES=$val ;; Cwlinux) CWLINUX=$val ;; D4D) D4D=$val ;; DPF) DPF=$val ;; EA232graphic) EA232graphic=$val ;; EFN) EFN=$val ;; FutabaVFD) FUTABAVFD=$val ;; FW8888) FW8888=$val ;; G15) G15=$val ;; GLCD2USB) GLCD2USB=$val ;; HD44780) HD44780=$val ;; HD44780-I2C) HD44780_I2C=$val ;; IRLCD) IRLCD=$val ;; LCD2USB) LCD2USB=$val ;; LCDLinux) LCDLINUX=$val ;; LCDTerm) LCDTERM=$val ;; LEDMatrix) LEDMATRIX=$val ;; LPH7508) LPH7508=$val ;; LUIse) LUISE=$val ;; LW_ABP) LW_ABP=$val ;; M50530) M50530=$val ;; MatrixOrbital) MATRIXORBITAL=$val ;; MatrixOrbitalGX) MATRIXORBITALGX=$val ;; MDM166A) MDM166A=$val ;; MilfordInstruments) MILINST=$val ;; Newhaven) NEWHAVEN=$val ;; Noritake) NORITAKE=$val; ;; NULL) NULL=$val; ;; Pertelian) PERTELIAN=$val ;; PHAnderson) PHANDERSON=$val ;; PICGraphic) PICGRAPHIC=$val ;; picoLCD) PICOLCD=$val ;; picoLCDGraphic) PICOLCDGRAPHIC=$val ;; PNG) PNG=$val ;; PPM) PPM=$val ;; RouterBoard) ROUTERBOARD=$val ;; Sample) SAMPLE=$val ;; SamsungSPF) SAMSUNGSPF=$val ;; serdisplib) SERDISPLIB=$val; ;; ShuttleVFD) SHUTTLEVFD=$val ;; SimpleLCD) SIMPLELCD=$val ;; st2205) ST2205=$val ;; T6963) T6963=$val ;; TeakLCM) TeakLCM=$val ;; Trefon) Trefon=$val ;; ULA200) ULA200=$val ;; USBHUB) USBHUB=$val ;; USBLCD) USBLCD=$val ;; VNC) VNC=$val ;; WincorNixdorf) WINCORNIXDORF=$val ;; X11) X11=$val ;; *) AC_MSG_ERROR([Unknown driver '$driver']) ;; esac done AC_MSG_RESULT([done]) # generic display drivers TEXT="no" GRAPHIC="no" IMAGE="no" GPIO="no" # generiv I/O drivers PARPORT="no" SERIAL="no" I2C="no" KEYPAD="no" # generic libraries LIBUSB="no" LIBUSB10="no" LIBFTDI="no" if test "$ASTUSB" = "yes"; then if test "$has_usb" = "true"; then TEXT="yes" SERIAL="yes" DRIVERS="$DRIVERS drv_ASTUSB.o" LIBUSB="yes" AC_DEFINE(WITH_ASTUSB,1,[ASTUSB driver]) else AC_MSG_WARN(usb.h not found: ASTUSB driver disabled) fi fi if test "$BECKMANNEGLE" = "yes"; then TEXT="yes" GPIO="yes" SERIAL="yes" DRIVERS="$DRIVERS drv_BeckmannEgle.o" AC_DEFINE(WITH_BECKMANNEGLE,1,[Beckmann&Egle driver]) fi if test "$BWCT" = "yes"; then if test "$has_usb" = "true"; then TEXT="yes" DRIVERS="$DRIVERS drv_BWCT.o" LIBUSB="yes" AC_DEFINE(WITH_BWCT,1,[BWCT driver]) else AC_MSG_WARN(usb.h not found: BWCT driver disabled) fi fi if test "$CRYSTALFONTZ" = "yes"; then TEXT="yes" GPIO="yes" SERIAL="yes" KEYPAD="yes" DRIVERS="$DRIVERS drv_Crystalfontz.o" AC_DEFINE(WITH_CRYSTALFONTZ,1,[Crystalfontz driver]) fi if test "$CURSES" = "yes"; then if test "$has_curses" = true; then TEXT="yes" KEYPAD="yes" DRIVERS="$DRIVERS drv_Curses.o" DRVLIBS="$DRVLIBS $CURSES_LIBS" CPPFLAGS="$CPPFLAGS $CURSES_INCLUDES" AC_DEFINE(WITH_CURSES,1,[Curses driver]) else AC_MSG_WARN(curses not found: Curses driver disabled) fi fi if test "$CWLINUX" = "yes"; then TEXT="yes" GPIO="yes" SERIAL="yes" KEYPAD="yes" DRIVERS="$DRIVERS drv_Cwlinux.o" AC_DEFINE(WITH_CWLINUX,1,[CwLinux driver]) fi if test "$D4D" = "yes"; then TEXT="yes"
/* $Id$
 * $URL$
 *
 * plugin for /proc/cpuinfo parsing
 *
 * Copyright (C) 2003 Michael Reinelt <michael@reinelt.co.at>
 * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 *
 * This file is part of LCD4Linux.
 *
 * LCD4Linux 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, or (at your option)
 * any later version.
 *
 * LCD4Linux 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

/* 
 * exported functions:
 *
 * int plugin_init_cpuinfo (void)
 *  adds functions to access /proc/cpuinfo
 *
 */


#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>

#include "debug.h"
#include "plugin.h"
#include "hash.h"

#ifdef __MAC_OS_X_VERSION_10_3
#include <sys/types.h>
#include <sys/sysctl.h>
#endif

static HASH CPUinfo;
static FILE *stream = NULL;

static int parse_cpuinfo(char *oid)
{
    int age;

    /* reread every second only */
    age = hash_age(&CPUinfo, NULL);
    if (age > 0 && age <= 1000)
	return 0;

#ifndef __MAC_OS_X_VERSION_10_3

    /* Linux Kernel, /proc-filesystem */

    if (stream == NULL)
	stream = fopen("/proc/cpuinfo", "r");
    if (stream == NULL) {
	error("fopen(/proc/cpuinfo) failed: %s", strerror(errno));
	return -1;
    }
    rewind(stream);
    while (!feof(stream)) {
	char buffer[256];
	char *c, *key, *val;
	fgets(buffer, sizeof(buffer), stream);
	c = strchr(buffer, ':');
	if (c == NULL)
	    continue;
	key = buffer;
	val = c + 1;
	/* strip leading blanks from key */
	while (isspace(*key))
	    *key++ = '\0';
	/* strip trailing blanks from key */
	do
	    *c = '\0';
	while (isspace(*--c));
	/* strip leading blanks from value */
	while (isspace(*val))
	    *val++ = '\0';
	/* strip trailing blanks from value */
	for (c = val; *c != '\0'; c++);
	while (isspace(*--c))
	    *c = '\0';

	/* add entry to hash table */
	hash_put(&CPUinfo, key, val);

    }
    /* to avoid compiler unused warning */
    oid = 0;

#else

    /* MACH Kernel, MacOS X */

    char val_ret[256];
    int *val;
    size_t val_len;

    if (sysctlbyname(oid, NULL, &val_len, NULL, 0) != 0) {
    	error("Error %d by sysctl(%s): %s", errno, oid, strerror(errno));
    	return -1;
    }
    if (val_len > sizeof(val_ret)) {
    	error("Error: Result of sysctl(%s) too big (%zd > %zd)!", oid, val_len, sizeof(val_ret));
    	return -1;
    }
    sysctlbyname(oid, &val_ret, &val_len, NULL, 0);
    if (val_len == sizeof(int)) {
    	/* we got an integer instead of a string */
    	val = (int*)val_ret;
    	snprintf(val_ret, sizeof(val_ret), "%d", *val);
    }
    hash_put(&CPUinfo, oid, val_ret);
#endif

    return 0;
}


static void my_cpuinfo(RESULT * result, RESULT * arg1)
{
    char *key, *val;

    key = R2S(arg1);
    if (parse_cpuinfo(key) < 0) {
	SetResult(&result, R_STRING, "");
	return;
    }

    val = hash_get(&CPUinfo, key, NULL);
    if (val == NULL)
	val = "";

    SetResult(&result, R_STRING, val);
}


int plugin_init_cpuinfo(void)
{
    hash_create(&CPUinfo);
    AddFunction("cpuinfo", 1, my_cpuinfo);
    return 0;
}

void plugin_exit_cpuinfo(void)
{
    if (stream != NULL) {
	fclose(stream);
	stream = NULL;
    }
    hash_destroy(&CPUinfo);
}