Files
2026-08-24 10:03:24 +08:00

160 lines
3.7 KiB
C++

/*
* Copyright (c) 2020 Amlogic, Inc. All rights reserved.
*
* This source code is subject to the terms and conditions defined in the
* file 'LICENSE' which is part of this source code package.
*
* Description:
*/
#define LOG_TAG "AmlMpSysfsUtil"
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <strings.h>
#include <utils/AmlMpLog.h>
#include <sys/ioctl.h>
#include "Amlsysfsutils.h"
#include <unistd.h>
#include <cutils/properties.h>
#include <string.h>
static const char* mName = LOG_TAG;
#ifdef CONFIG_AMP_SERVER
#include "SystemControlClient.h"
static android::SystemControlClient *g_mSystemControl = nullptr;
static std::once_flag g_onceFlag;
#endif
namespace aml_mp {
#ifdef CONFIG_AMP_SERVER
static void initSystemControl() {
g_mSystemControl = android::SystemControlClient::getInstance();
}
static android::SystemControlClient* getSystemControl() {
std::call_once(g_onceFlag, initSystemControl);
return g_mSystemControl;
}
#endif
int amsysfs_set_sysfs_str(const char *path, const char *val) {
if (path == nullptr || val == nullptr) {
return -1;
}
#ifdef CONFIG_AMP_SERVER
if (getSystemControl() != nullptr) {
bool ret = getSystemControl()->writeSysfs(path, val, strlen(val));
return ret ? 0 : -1;
}
#endif
int fd;
int bytes;
fd = open(path, O_CREAT | O_RDWR | O_TRUNC, 0644);
if (fd >= 0) {
bytes = write(fd, val, strlen(val));
close(fd);
return 0;
} else {
MLOGW("[%s] %s failed!",__FUNCTION__,path);
return -1;
}
}
int amsysfs_get_sysfs_str(const char *path, char *valstr, unsigned size) {
if (path == nullptr || valstr == nullptr || size <= 0) {
return -1;
}
#ifdef CONFIG_AMP_SERVER
if (getSystemControl() != nullptr) {
std::string strvalue;
if (!getSystemControl()->readSysfs(path, strvalue)) {
MLOGE("systemcontrol read sys fs fail");
return -1;
}
size_t copylen = std::min(size, strvalue.length());
strncpy(valstr, strvalue.c_str(), copylen);
return 0;
}
#endif
int fd;
int bytes;
fd = open(path, O_RDONLY);
if (fd >= 0) {
memset(valstr, 0, size);
bytes = read(fd, valstr, size - 1);
valstr[size - 1] = '\0';
close(fd);
return 0;
} else {
MLOGW("[%s] %s failed!",__FUNCTION__,path);
snprintf(valstr, size, "%s", "fail");
return -1;
}
}
int amsysfs_set_sysfs_int(const char *path, int val) {
char bcmd[16] = { 0 };
snprintf(bcmd, sizeof(bcmd), "%d", val);
return amsysfs_set_sysfs_str(path, bcmd);
}
int amsysfs_get_sysfs_int(const char *path) {
int val = 0;
char bcmd[16] = { 0 };
if (amsysfs_get_sysfs_str(path, bcmd, sizeof(bcmd) - 1) != 0) {
MLOGW("[%s] %s failed!",__FUNCTION__,path);
} else {
bcmd[15] = '\0';
val = strtol(bcmd, NULL, 10);
}
return val;
}
int amsysfs_set_sysfs_int16(const char *path, int val) {
char bcmd[16] = { 0 };
snprintf(bcmd, sizeof(bcmd), "0x%x", val);
return amsysfs_set_sysfs_str(path, bcmd);
}
int amsysfs_get_sysfs_int16(const char *path) {
int val = 0;
char bcmd[16] = { 0 };
if (amsysfs_get_sysfs_str(path, bcmd, sizeof(bcmd) - 1) != 0) {
MLOGW("[%s] %s failed!",__FUNCTION__,path);
} else {
bcmd[15] = '\0';
val = strtol(bcmd, NULL, 16);
}
return val;
}
unsigned long amsysfs_get_sysfs_ulong(const char *path) {
char bcmd[24] = { 0 };
unsigned long num = 0;
if (amsysfs_get_sysfs_str(path, bcmd, sizeof(bcmd) - 1) != 0) {
MLOGW("[%s] %s failed!",__FUNCTION__,path);
} else {
bcmd[23] = '\0';
num = strtol(bcmd, NULL, 0);
}
return num;
}
}