зеркало из https://github.com/mozilla/gecko-dev.git
Bug 819016 - Create some helper functions for reading /sys files. r=bsmedberg
This commit is contained in:
Родитель
b615697566
Коммит
02a2ed38cd
|
@ -89,48 +89,6 @@ class AutoMounter;
|
|||
// try to automount. Any other volumes will be ignored.
|
||||
static const nsDependentCString sAutoVolumeName[] = { NS_LITERAL_CSTRING("sdcard") };
|
||||
|
||||
/**************************************************************************
|
||||
*
|
||||
* Some helper functions for reading/writing files in /sys
|
||||
*
|
||||
**************************************************************************/
|
||||
|
||||
static bool
|
||||
ReadSysFile(const char* aFilename, char* aBuf, size_t aBufSize)
|
||||
{
|
||||
int fd = open(aFilename, O_RDONLY);
|
||||
if (fd < 0) {
|
||||
ERR("Unable to open file '%s' for reading", aFilename);
|
||||
return false;
|
||||
}
|
||||
ScopedClose autoClose(fd);
|
||||
ssize_t bytesRead = read(fd, aBuf, aBufSize - 1);
|
||||
if (bytesRead < 0) {
|
||||
ERR("Unable to read from file '%s'", aFilename);
|
||||
return false;
|
||||
}
|
||||
if (aBuf[bytesRead - 1] == '\n') {
|
||||
bytesRead--;
|
||||
}
|
||||
aBuf[bytesRead] = '\0';
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
ReadSysFile(const char* aFilename, bool* aVal)
|
||||
{
|
||||
char valBuf[20];
|
||||
if (!ReadSysFile(aFilename, valBuf, sizeof(valBuf))) {
|
||||
return false;
|
||||
}
|
||||
int intVal;
|
||||
if (sscanf(valBuf, "%d", &intVal) != 1) {
|
||||
return false;
|
||||
}
|
||||
*aVal = (intVal != 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
inline const char* SwitchStateStr(const SwitchEvent& aEvent)
|
||||
|
@ -151,13 +109,18 @@ IsUsbCablePluggedIn()
|
|||
// Until then, just go read the file directly
|
||||
if (access(ICS_SYS_USB_STATE, F_OK) == 0) {
|
||||
char usbState[20];
|
||||
return ReadSysFile(ICS_SYS_USB_STATE,
|
||||
usbState, sizeof(usbState)) &&
|
||||
(strcmp(usbState, "CONFIGURED") == 0);
|
||||
if (ReadSysFile(ICS_SYS_USB_STATE, usbState, sizeof(usbState))) {
|
||||
return strcmp(usbState, "CONFIGURED") == 0;
|
||||
}
|
||||
ERR("Error reading file '%s': %s", ICS_SYS_USB_STATE, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
bool configured;
|
||||
return ReadSysFile(GB_SYS_USB_CONFIGURED, &configured) &&
|
||||
configured;
|
||||
if (ReadSysFile(GB_SYS_USB_CONFIGURED, &configured)) {
|
||||
return configured;
|
||||
}
|
||||
ERR("Error reading file '%s': %s", GB_SYS_USB_CONFIGURED, strerror(errno));
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -370,10 +333,17 @@ AutoMounter::UpdateState()
|
|||
|
||||
if (access(ICS_SYS_USB_FUNCTIONS, F_OK) == 0) {
|
||||
umsAvail = (access(ICS_SYS_UMS_DIRECTORY, F_OK) == 0);
|
||||
char functionsStr[60];
|
||||
umsEnabled = umsAvail &&
|
||||
ReadSysFile(ICS_SYS_USB_FUNCTIONS, functionsStr, sizeof(functionsStr)) &&
|
||||
!!strstr(functionsStr, "mass_storage");
|
||||
if (umsAvail) {
|
||||
char functionsStr[60];
|
||||
if (ReadSysFile(ICS_SYS_USB_FUNCTIONS, functionsStr, sizeof(functionsStr))) {
|
||||
umsEnabled = strstr(functionsStr, "mass_storage") != NULL;
|
||||
} else {
|
||||
ERR("Error reading file '%s': %s", ICS_SYS_USB_FUNCTIONS, strerror(errno));
|
||||
umsEnabled = false;
|
||||
}
|
||||
} else {
|
||||
umsEnabled = false;
|
||||
}
|
||||
} else {
|
||||
umsAvail = ReadSysFile(GB_SYS_UMS_ENABLE, &umsEnabled);
|
||||
}
|
||||
|
|
|
@ -355,43 +355,6 @@ DisableBatteryNotifications()
|
|||
NewRunnableFunction(UnregisterBatteryObserverIOThread));
|
||||
}
|
||||
|
||||
// See bug 819016 about moving the ReadSysFile functions to a
|
||||
// central location.
|
||||
static bool
|
||||
ReadSysFile(const char *aFilename, char *aBuf, size_t aBufSize,
|
||||
size_t *aBytesRead = NULL)
|
||||
{
|
||||
int fd = TEMP_FAILURE_RETRY(open(aFilename, O_RDONLY));
|
||||
if (fd < 0) {
|
||||
HAL_LOG(("Unable to open file '%s' for reading", aFilename));
|
||||
return false;
|
||||
}
|
||||
ScopedClose autoClose(fd);
|
||||
ssize_t bytesRead = TEMP_FAILURE_RETRY(read(fd, aBuf, aBufSize - 1));
|
||||
if (bytesRead < 0) {
|
||||
HAL_LOG(("Unable to read from file '%s'", aFilename));
|
||||
return false;
|
||||
}
|
||||
if (bytesRead && (aBuf[bytesRead - 1] == '\n')) {
|
||||
bytesRead--;
|
||||
}
|
||||
aBuf[bytesRead] = '\0';
|
||||
if (aBytesRead) {
|
||||
*aBytesRead = bytesRead;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
ReadSysFile(const char *aFilename, int *aVal)
|
||||
{
|
||||
char valBuf[20];
|
||||
if (!ReadSysFile(aFilename, valBuf, sizeof(valBuf))) {
|
||||
return false;
|
||||
}
|
||||
return sscanf(valBuf, "%d", aVal) == 1;
|
||||
}
|
||||
|
||||
static bool
|
||||
GetCurrentBatteryCharge(int* aCharge)
|
||||
{
|
||||
|
@ -403,7 +366,7 @@ GetCurrentBatteryCharge(int* aCharge)
|
|||
|
||||
#ifdef DEBUG
|
||||
if ((*aCharge < 0) || (*aCharge > 100)) {
|
||||
HAL_LOG(("charge level containes unknown value: %d", *aCharge));
|
||||
HAL_LOG(("charge level contains unknown value: %d", *aCharge));
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -440,14 +403,12 @@ GetCurrentBatteryCharging(int* aCharging)
|
|||
// Otoro device support
|
||||
|
||||
char chargingSrcString[16];
|
||||
size_t chargingSrcLen;
|
||||
|
||||
success = ReadSysFile("/sys/class/power_supply/battery/status",
|
||||
chargingSrcString, sizeof(chargingSrcString),
|
||||
&chargingSrcLen);
|
||||
chargingSrcString, sizeof(chargingSrcString));
|
||||
if (success) {
|
||||
*aCharging = !memcmp(chargingSrcString, "Charging", chargingSrcLen) ||
|
||||
!memcmp(chargingSrcString, "Full", chargingSrcLen);
|
||||
*aCharging = strcmp(chargingSrcString, "Charging") == 0 ||
|
||||
strcmp(chargingSrcString, "Full") == 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -110,8 +110,8 @@ static const char* kWakeFile = "/sys/power/wait_for_fb_wake";
|
|||
|
||||
static void *frameBufferWatcher(void *) {
|
||||
|
||||
int len = 0;
|
||||
char buf;
|
||||
bool ret;
|
||||
|
||||
nsRefPtr<ScreenOnOffEvent> mScreenOnEvent = new ScreenOnOffEvent(true);
|
||||
nsRefPtr<ScreenOnOffEvent> mScreenOffEvent = new ScreenOnOffEvent(false);
|
||||
|
@ -119,23 +119,13 @@ static void *frameBufferWatcher(void *) {
|
|||
while (true) {
|
||||
// Cannot use epoll here because kSleepFile and kWakeFile are
|
||||
// always ready to read and blocking.
|
||||
{
|
||||
ScopedClose fd(open(kSleepFile, O_RDONLY, 0));
|
||||
do {
|
||||
len = read(fd.get(), &buf, 1);
|
||||
} while (len < 0 && errno == EINTR);
|
||||
NS_WARN_IF_FALSE(len >= 0, "WAIT_FOR_FB_SLEEP failed");
|
||||
NS_DispatchToMainThread(mScreenOffEvent);
|
||||
}
|
||||
ret = ReadSysFile(kSleepFile, &buf, sizeof(buf));
|
||||
NS_WARN_IF_FALSE(ret, "WAIT_FOR_FB_SLEEP failed");
|
||||
NS_DispatchToMainThread(mScreenOffEvent);
|
||||
|
||||
{
|
||||
ScopedClose fd(open(kWakeFile, O_RDONLY, 0));
|
||||
do {
|
||||
len = read(fd.get(), &buf, 1);
|
||||
} while (len < 0 && errno == EINTR);
|
||||
NS_WARN_IF_FALSE(len >= 0, "WAIT_FOR_FB_WAKE failed");
|
||||
NS_DispatchToMainThread(mScreenOnEvent);
|
||||
}
|
||||
ret = ReadSysFile(kWakeFile, &buf, sizeof(buf));
|
||||
NS_WARN_IF_FALSE(ret, "WAIT_FOR_FB_WAKE failed");
|
||||
NS_DispatchToMainThread(mScreenOnEvent);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
|
|
@ -3,9 +3,13 @@
|
|||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "nscore.h"
|
||||
#include "nsStringGlue.h"
|
||||
#include "private/pprio.h"
|
||||
#include "mozilla/Assertions.h"
|
||||
#include "mozilla/FileUtils.h"
|
||||
|
||||
#if defined(XP_MACOSX)
|
||||
|
@ -113,6 +117,79 @@ mozilla::fallocate(PRFileDesc *aFD, int64_t aLength)
|
|||
return false;
|
||||
}
|
||||
|
||||
#ifdef MOZ_WIDGET_GONK
|
||||
|
||||
#undef TEMP_FAILURE_RETRY
|
||||
#define TEMP_FAILURE_RETRY(exp) (__extension__({ \
|
||||
typeof (exp) _rc; \
|
||||
do { \
|
||||
_rc = (exp); \
|
||||
} while (_rc == -1 && errno == EINTR); \
|
||||
_rc; \
|
||||
}))
|
||||
|
||||
bool
|
||||
mozilla::ReadSysFile(
|
||||
const char* aFilename,
|
||||
char* aBuf,
|
||||
size_t aBufSize)
|
||||
{
|
||||
int fd = TEMP_FAILURE_RETRY(open(aFilename, O_RDONLY));
|
||||
if (fd < 0) {
|
||||
return false;
|
||||
}
|
||||
ScopedClose autoClose(fd);
|
||||
if (aBufSize == 0) {
|
||||
return true;
|
||||
}
|
||||
ssize_t bytesRead;
|
||||
size_t offset = 0;
|
||||
do {
|
||||
bytesRead = TEMP_FAILURE_RETRY(read(fd, aBuf + offset, aBufSize - offset));
|
||||
if (bytesRead == -1) {
|
||||
return false;
|
||||
}
|
||||
offset += bytesRead;
|
||||
} while (bytesRead > 0 && offset < aBufSize);
|
||||
MOZ_ASSERT(offset <= aBufSize);
|
||||
if (offset > 0 && aBuf[offset - 1] == '\n') {
|
||||
offset--;
|
||||
}
|
||||
if (offset == aBufSize) {
|
||||
MOZ_ASSERT(offset > 0);
|
||||
offset--;
|
||||
}
|
||||
aBuf[offset] = '\0';
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
mozilla::ReadSysFile(
|
||||
const char* aFilename,
|
||||
int* aVal)
|
||||
{
|
||||
char valBuf[32];
|
||||
if (!ReadSysFile(aFilename, valBuf, sizeof(valBuf))) {
|
||||
return false;
|
||||
}
|
||||
return sscanf(valBuf, "%d", aVal) == 1;
|
||||
}
|
||||
|
||||
bool
|
||||
mozilla::ReadSysFile(
|
||||
const char* aFilename,
|
||||
bool* aVal)
|
||||
{
|
||||
int v;
|
||||
if (!ReadSysFile(aFilename, &v)) {
|
||||
return false;
|
||||
}
|
||||
*aVal = (v != 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif /* MOZ_WIDGET_GONK */
|
||||
|
||||
void
|
||||
mozilla::ReadAheadLib(nsIFile* aFile)
|
||||
{
|
||||
|
|
|
@ -145,5 +145,46 @@ NS_COM_GLUE void ReadAhead(filedesc_t aFd, const size_t aOffset = 0,
|
|||
const size_t aCount = SIZE_MAX);
|
||||
|
||||
|
||||
#ifdef MOZ_WIDGET_GONK
|
||||
|
||||
/**
|
||||
* Read the contents of a file.
|
||||
* This function is intended for reading a single-lined text files from
|
||||
* /sys/. If the file ends with a newline ('\n') then it will be discarded.
|
||||
* The output buffer will always be '\0'-terminated on successful completion.
|
||||
* If aBufSize == 0, then this function will return true if the file exists
|
||||
* and is readable (it will not attempt to read anything from it).
|
||||
* On failure the contents of aBuf after this call will be undefined and the
|
||||
* value of the global variable errno will be set accordingly.
|
||||
* @return true on success, notice that less than requested bytes could have
|
||||
* been read if the file was smaller
|
||||
*/
|
||||
bool
|
||||
ReadSysFile(
|
||||
const char* aFilename,
|
||||
char* aBuf,
|
||||
size_t aBufSize);
|
||||
|
||||
/**
|
||||
* Parse the contents of a file, assuming it contains a decimal integer.
|
||||
* @return true on success
|
||||
*/
|
||||
bool
|
||||
ReadSysFile(
|
||||
const char* aFilename,
|
||||
int* aVal);
|
||||
|
||||
/**
|
||||
* Parse the contents of a file, assuming it contains a boolean value
|
||||
* (either 0 or 1).
|
||||
* @return true on success
|
||||
*/
|
||||
bool
|
||||
ReadSysFile(
|
||||
const char* aFilename,
|
||||
bool* aVal);
|
||||
|
||||
#endif /* MOZ_WIDGET_GONK */
|
||||
|
||||
} // namespace mozilla
|
||||
#endif
|
||||
|
|
Загрузка…
Ссылка в новой задаче