From 1b2bd1d7863a84f938ecd9a18d0e976025ba46ac Mon Sep 17 00:00:00 2001 From: Marco Nelissen Date: Thu, 30 Apr 2009 14:11:03 -0700 Subject: [PATCH] Move some simulator fixes from master to donut. In git master they were part of change 700ccfcc01d3081ff74ef5006e8f0dec5ac52a96, which was an auto import from //branches/master/...@140412 The original change was 134529 in perforce. --- simulator/wrapsim/Android.mk | 3 +- simulator/wrapsim/FakeDev.c | 3 + simulator/wrapsim/FakeDev.h | 1 + simulator/wrapsim/SysPower.c | 140 +++++++++++++++++++++++++++++++++++ 4 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 simulator/wrapsim/SysPower.c diff --git a/simulator/wrapsim/Android.mk b/simulator/wrapsim/Android.mk index ca9a5928c..0b7890d23 100644 --- a/simulator/wrapsim/Android.mk +++ b/simulator/wrapsim/Android.mk @@ -22,7 +22,8 @@ LOCAL_SRC_FILES := \ Init.c \ Intercept.c \ Log.c \ - SimMgr.c + SimMgr.c \ + SysPower.c LOCAL_C_INCLUDES += prebuilt/common/esd diff --git a/simulator/wrapsim/FakeDev.c b/simulator/wrapsim/FakeDev.c index 3e223d363..7d2494eb4 100644 --- a/simulator/wrapsim/FakeDev.c +++ b/simulator/wrapsim/FakeDev.c @@ -67,6 +67,9 @@ FakedPath fakedpaths[] = { "/dev/input/*", NULL }, { "/dev/log/*", wsOpenDevLog }, { "/sys/class/power_supply/*", wsOpenDevPower }, + { "/sys/power/state", wsOpenSysPower }, + { "/sys/power/wake_lock", wsOpenSysPower }, + { "/sys/power/wake_unlock", wsOpenSysPower }, { "/sys/devices/platform/android-vibrator/enable", wsOpenDevVibrator }, { "/sys/qemu_trace/*", NULL }, { NULL, NULL } diff --git a/simulator/wrapsim/FakeDev.h b/simulator/wrapsim/FakeDev.h index eacbbf55d..4781cfc4d 100644 --- a/simulator/wrapsim/FakeDev.h +++ b/simulator/wrapsim/FakeDev.h @@ -108,6 +108,7 @@ FakeDev* wsOpenDevEvent(const char* pathName, int flags); FakeDev* wsOpenDevFb(const char* pathName, int flags); FakeDev* wsOpenDevLog(const char* pathName, int flags); FakeDev* wsOpenDevPower(const char* pathName, int flags); +FakeDev* wsOpenSysPower(const char* pathName, int flags); FakeDev* wsOpenDevVibrator(const char* pathName, int flags); /* diff --git a/simulator/wrapsim/SysPower.c b/simulator/wrapsim/SysPower.c new file mode 100644 index 000000000..fa7ae0ad3 --- /dev/null +++ b/simulator/wrapsim/SysPower.c @@ -0,0 +1,140 @@ +/* + * Copyright 2009 The Android Open Source Project + * + * Magic entries in /sys/power/. + */ +#include "Common.h" + +#include +#include +#include + +/* + * Map filename to device index. + * + * [ not using DeviceIndex -- would be useful if we need to return something + * other than a static string ] + */ +static const struct { + const char* name; + //DeviceIndex idx; + const char* data; +} gDeviceMap[] = { + { "state", + "mem\n" }, + { "wake_lock", + "\n" }, + { "wake_unlock", + "KeyEvents PowerManagerService radio-interface\n" }, +}; + +/* + * Power driver state. + * + * Right now we just ignore everything written. + */ +typedef struct PowerState { + int which; +} PowerState; + + +/* + * Figure out who we are, based on "pathName". + */ +static void configureInitialState(const char* pathName, PowerState* powerState) +{ + const char* cp = pathName + strlen("/sys/power/"); + int i; + + powerState->which = -1; + for (i = 0; i < (int) (sizeof(gDeviceMap) / sizeof(gDeviceMap[0])); i++) { + if (strcmp(cp, gDeviceMap[i].name) == 0) { + powerState->which = i; + break; + } + } + + if (powerState->which == -1) { + wsLog("Warning: access to unknown power device '%s'\n", pathName); + return; + } +} + +/* + * Free up the state structure. + */ +static void freeState(PowerState* powerState) +{ + free(powerState); +} + +/* + * Read data from the device. + * + * We don't try to keep track of how much was read -- existing clients just + * try to read into a large buffer. + */ +static ssize_t readPower(FakeDev* dev, int fd, void* buf, size_t count) +{ + PowerState* state = (PowerState*) dev->state; + int dataLen; + + wsLog("%s: read %d\n", dev->debugName, count); + + if (state->which < 0 || + state->which >= (int) (sizeof(gDeviceMap)/sizeof(gDeviceMap[0]))) + { + return 0; + } + + const char* data = gDeviceMap[state->which].data; + size_t strLen = strlen(data); + + while(strLen == 0) + sleep(10); // block forever + + ssize_t copyCount = (strLen < count) ? strLen : count; + memcpy(buf, data, copyCount); + return copyCount; +} + +/* + * Ignore the request. + */ +static ssize_t writePower(FakeDev* dev, int fd, const void* buf, size_t count) +{ + wsLog("%s: write %d bytes\n", dev->debugName, count); + return count; +} + +/* + * Free up our state before closing down the fake descriptor. + */ +static int closePower(FakeDev* dev, int fd) +{ + freeState((PowerState*)dev->state); + dev->state = NULL; + return 0; +} + +/* + * Open a power device. + */ +FakeDev* wsOpenSysPower(const char* pathName, int flags) +{ + FakeDev* newDev = wsCreateFakeDev(pathName); + if (newDev != NULL) { + newDev->read = readPower; + newDev->write = writePower; + newDev->ioctl = NULL; + newDev->close = closePower; + + PowerState* powerState = calloc(1, sizeof(PowerState)); + + configureInitialState(pathName, powerState); + newDev->state = powerState; + } + + return newDev; +} +