449 lines
11 KiB
C++
449 lines
11 KiB
C++
/*
|
|
* Copyright (c) 2014 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: c++ file
|
|
*/
|
|
|
|
#define LOG_MODULE_TAG "TV"
|
|
#define LOG_CLASS_TAG "tvutils"
|
|
|
|
#include <unistd.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <errno.h>
|
|
#include <unistd.h>
|
|
#include <sys/ioctl.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/un.h>
|
|
#include <fcntl.h>
|
|
#include <ctype.h>
|
|
#include <sys/prctl.h>
|
|
#include <stdlib.h>
|
|
#include <ubootenv.h>
|
|
|
|
#include "tvutils.h"
|
|
|
|
#include <vector>
|
|
#include <map>
|
|
#include <string>
|
|
#include "CTvLog.h"
|
|
#include <pthread.h>
|
|
|
|
|
|
static const char *GetKeyDataWriteDevPath() {
|
|
return "/sys/class/unifykeys/write";
|
|
}
|
|
|
|
static const char *GetKeyDataReadDevPath() {
|
|
return "/sys/class/unifykeys/read";
|
|
}
|
|
|
|
static int checkKeyNameInList(const char *key_name) {
|
|
FILE *dev_fp = NULL;
|
|
char *tmp_ptr = NULL;
|
|
char lineStr[1024];
|
|
|
|
dev_fp = fopen(CS_KEY_DATA_LIST_DEV_PATH, "r");
|
|
if (dev_fp == NULL) {
|
|
LOGE("%s, open %s ERROR(%s)!!\n", __FUNCTION__,
|
|
CS_KEY_DATA_LIST_DEV_PATH, strerror(errno));
|
|
return -1;
|
|
}
|
|
|
|
while (fgets(lineStr, 1024, dev_fp) != NULL) {
|
|
tmp_ptr = strstr(lineStr, key_name);
|
|
if (tmp_ptr != NULL) {
|
|
break;
|
|
}
|
|
|
|
tmp_ptr = NULL;
|
|
}
|
|
|
|
fclose(dev_fp);
|
|
dev_fp = NULL;
|
|
|
|
if (tmp_ptr == NULL) {
|
|
return -1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
int readUKeyData(const char *key_name, unsigned char data_buf[], int rd_size) {
|
|
int rd_cnt = 0;
|
|
FILE *dev_fp = NULL;
|
|
|
|
if (checkKeyNameInList(key_name) < 0) {
|
|
|
|
LOGD("%s, key \"%s\" isn't exist in unifykeys list\n", __FUNCTION__, key_name);
|
|
return -1;
|
|
}
|
|
|
|
dev_fp = fopen(CS_KEY_DATA_NAME_DEV_PATH, "w");
|
|
if (dev_fp == NULL) {
|
|
LOGD("%s, open %s ERROR(%s)!!\n", __FUNCTION__,
|
|
CS_KEY_DATA_NAME_DEV_PATH, strerror(errno));
|
|
return -1;
|
|
}
|
|
|
|
fprintf(dev_fp, "%s", key_name);
|
|
|
|
fclose(dev_fp);
|
|
dev_fp = NULL;
|
|
|
|
/*int mode = 1;
|
|
|
|
if (mode == 0) {
|
|
dev_fp = fopen(GetKeyDataReadDevPath(), "r");
|
|
if (dev_fp == NULL) {
|
|
LOGD("%s, open %s ERROR(%s)!!\n", __FUNCTION__,
|
|
GetKeyDataReadDevPath(), strerror(errno));
|
|
return -1;
|
|
}
|
|
|
|
fscanf(dev_fp, "%s", data_buf);
|
|
rd_cnt = strlen((char *) data_buf);
|
|
} else {
|
|
dev_fp = fopen(GetKeyDataReadDevPath(), "rb");
|
|
if (dev_fp == NULL) {
|
|
LOGD("%s, open %s ERROR(%s)!!\n", __FUNCTION__,
|
|
GetKeyDataReadDevPath(), strerror(errno));
|
|
return -1;
|
|
}
|
|
|
|
rd_cnt = fread(data_buf, 1, CC_ONE_SECTION_SIZE, dev_fp);
|
|
}*/
|
|
|
|
dev_fp = fopen(GetKeyDataReadDevPath(), "rb");
|
|
if (dev_fp == NULL) {
|
|
LOGD("%s, open %s ERROR(%s)!!\n", __FUNCTION__,
|
|
GetKeyDataReadDevPath(), strerror(errno));
|
|
return -1;
|
|
}
|
|
|
|
rd_cnt = fread(data_buf, 1, CC_ONE_SECTION_SIZE, dev_fp);
|
|
|
|
fclose(dev_fp);
|
|
dev_fp = NULL;
|
|
|
|
return rd_cnt;
|
|
}
|
|
|
|
int writeUKeyData(const char *key_name, unsigned char data_buf[], int wr_size) {
|
|
int wr_cnt = 0;
|
|
int dev_fd = -1;
|
|
FILE *dev_fp = NULL;
|
|
|
|
if (checkKeyNameInList(key_name) < 0) {
|
|
LOGD("%s, key \"%s\" isn't exist in unifykeys list\n", __FUNCTION__, key_name);
|
|
return -1;
|
|
}
|
|
|
|
pthread_mutex_lock(&ukey_write_mutex);
|
|
dev_fp = fopen(CS_KEY_DATA_NAME_DEV_PATH, "w");
|
|
if (dev_fp == NULL) {
|
|
LOGD("%s, open %s ERROR(%s)!!\n", __FUNCTION__,
|
|
CS_KEY_DATA_NAME_DEV_PATH, strerror(errno));
|
|
pthread_mutex_unlock(&ukey_write_mutex);
|
|
return -1;
|
|
}
|
|
|
|
fprintf(dev_fp, "%s", key_name);
|
|
|
|
fclose(dev_fp);
|
|
dev_fp = NULL;
|
|
|
|
dev_fd = open(GetKeyDataWriteDevPath(), O_WRONLY | O_SYNC | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG | S_IRWXO);
|
|
|
|
if (dev_fd < 0) {
|
|
LOGD("%s, open %s ERROR(%s)!!\n", __FUNCTION__,
|
|
GetKeyDataWriteDevPath(), strerror(errno));
|
|
pthread_mutex_unlock(&ukey_write_mutex);
|
|
return -1;
|
|
}
|
|
|
|
wr_cnt = write(dev_fd, data_buf, wr_size);
|
|
|
|
fsync(dev_fd);
|
|
|
|
close(dev_fd);
|
|
dev_fd = -1;
|
|
pthread_mutex_unlock(&ukey_write_mutex);
|
|
|
|
return wr_cnt;
|
|
}
|
|
|
|
#include <iostream>
|
|
#include <dirent.h>
|
|
|
|
int writeSys(const char *path, const char *val) {
|
|
int fd;
|
|
if ((fd = open(path, O_RDWR)) < 0) {
|
|
LOGE("writeSys, open %s error(%s)", path, strerror (errno));
|
|
return -1;
|
|
}
|
|
|
|
LOGD("write %s, val:%s\n", path, val);
|
|
|
|
int len = write(fd, val, strlen(val));
|
|
close(fd);
|
|
return len;
|
|
}
|
|
|
|
int readSys(const char *path, char *buf, int count) {
|
|
int fd, len;
|
|
|
|
if ( NULL == buf ) {
|
|
LOGE("buf is NULL");
|
|
return -1;
|
|
}
|
|
|
|
if ((fd = open(path, O_RDONLY)) < 0) {
|
|
LOGE("readSys, open %s error(%s)", path, strerror (errno));
|
|
return -1;
|
|
}
|
|
|
|
len = read(fd, buf, count);
|
|
if (len < 0) {
|
|
LOGE("read %s error, %s\n", path, strerror(errno));
|
|
goto exit;
|
|
}
|
|
|
|
int i , j;
|
|
for (i = 0, j = 0; i <= len -1; i++) {
|
|
//change '\0' to 0x20(spacing), otherwise the string buffer will be cut off ,if the last char is '\0' should not replace it
|
|
if (0x0 == buf[i] && i < len - 1) {
|
|
buf[i] = 0x20;
|
|
}
|
|
/* delete all the character of '\n' */
|
|
if (0x0a != buf[i]) {
|
|
buf[j++] = buf[i];
|
|
}
|
|
}
|
|
buf[j] = 0x0;
|
|
|
|
//LOGI("read %s, result length:%d, val:%s\n", path, len, buf);
|
|
|
|
exit:
|
|
close(fd);
|
|
return len;
|
|
}
|
|
|
|
int tvReadSysfs(const char *path, char *value) {
|
|
int ret = -1;
|
|
if (value == NULL) {
|
|
LOGD("%s: buf is NULL!.\n", __FUNCTION__);
|
|
} else {
|
|
char buf[SYS_STR_LEN+1] = {0};
|
|
ret = readSys(path, (char*)buf, SYS_STR_LEN);
|
|
strcpy(value, buf);
|
|
}
|
|
LOGD("%s: fileName is: %s, readLength is %d.\n", __FUNCTION__, path, ret);
|
|
return ret;
|
|
}
|
|
|
|
int tvWriteSysfs(const char *path, const char *value) {
|
|
return writeSys(path, value);
|
|
}
|
|
|
|
int tvWriteSysfs(const char *path, int value, int base)
|
|
{
|
|
char str_value[64] = {0};
|
|
if (base == 16) {
|
|
sprintf(str_value, "0x%-x", value);
|
|
} else {
|
|
sprintf(str_value, "%d", value);
|
|
}
|
|
LOGD("tvWriteSysfs, str_value = %s.\n", str_value);
|
|
return writeSys(path, str_value);
|
|
}
|
|
|
|
//check file exist or not
|
|
bool isFileExist(const char *file_name)
|
|
{
|
|
struct stat tmp_st;
|
|
|
|
return stat(file_name, &tmp_st) == 0;
|
|
}
|
|
|
|
int GetFileAttrIntValue(const char *fp, int flag)
|
|
{
|
|
int fd = -1;
|
|
int temp = -1;
|
|
char temp_str[32];
|
|
|
|
memset(temp_str, 0, 32);
|
|
|
|
fd = open(fp, flag);
|
|
|
|
if (fd < 0) {
|
|
LOGE("open %s ERROR(%s)!!\n", fp, strerror(errno));
|
|
return -1;
|
|
}
|
|
|
|
ssize_t bytesRead = read(fd, temp_str, sizeof(temp_str));
|
|
if (bytesRead < 0) {
|
|
LOGE("read %s ERROR(%s)!!\n", fp, strerror(errno));
|
|
close(fd);
|
|
return -1;
|
|
}
|
|
|
|
if (sscanf(temp_str, "%d", &temp) >= 0) {
|
|
LOGD("%s -> get %s value =%d!\n", "TV", fp, temp);
|
|
close(fd);
|
|
return temp;
|
|
} else {
|
|
LOGD("%s -> get %s value error(%s)\n", "TV", fp, strerror(errno));
|
|
close(fd);
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
int ReadDataFromFile(char *fileName, int offset, int nsize, char *dataBuf)
|
|
{
|
|
int deviceFd = -1;
|
|
int ret = 0;
|
|
|
|
if ((dataBuf == NULL) || (fileName == NULL)) {
|
|
LOGE("%s: file_name or data_buf is NULL!\n", __FUNCTION__);
|
|
ret = -1;
|
|
} else {
|
|
LOGD("%s: file_name is %s, offset is %d, size is %d.!\n", __FUNCTION__, fileName, offset, nsize);
|
|
deviceFd = open(fileName, O_RDONLY);
|
|
if (deviceFd < 0) {
|
|
LOGE("%s: open %s error(%s).\n", __FUNCTION__, fileName, strerror(errno));
|
|
ret = -1;
|
|
} else {
|
|
ret = lseek(deviceFd, offset, SEEK_SET);
|
|
if (ret == -1) {
|
|
LOGE("%s: lseek file error(%s).\n", __FUNCTION__, strerror(errno));
|
|
} else {
|
|
ret = read(deviceFd, dataBuf, nsize);
|
|
}
|
|
close(deviceFd);
|
|
deviceFd = -1;
|
|
}
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
int SaveDataToFile(char *fileName, int offset, int nsize, char *dataBuf)
|
|
{
|
|
int deviceFd = -1;
|
|
int ret = 0;
|
|
|
|
if ((dataBuf == NULL) || (fileName == NULL)) {
|
|
LOGE("%s: file_name or data_buf is NULL!\n", __FUNCTION__);
|
|
ret = -1;
|
|
} else {
|
|
LOGD("%s: file_name is %s, offset is %d, size is %d.!\n", __FUNCTION__, fileName, offset, nsize);
|
|
deviceFd = open(fileName, O_RDWR | O_SYNC);
|
|
if (deviceFd < 0) {
|
|
LOGE("%s: open file %s error(%s).\n", __FUNCTION__, fileName, strerror(errno));
|
|
ret = -1;
|
|
} else {
|
|
ret = lseek(deviceFd, offset, SEEK_SET);
|
|
if (ret == -1) {
|
|
ret = -1;
|
|
} else {
|
|
ret = write(deviceFd, dataBuf, nsize);
|
|
fsync(deviceFd);
|
|
}
|
|
|
|
close(deviceFd);
|
|
deviceFd = -1;
|
|
}
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
int GetFileSize(char *fileName) {
|
|
|
|
if (fileName == NULL) {
|
|
LOGE("Fail path(%s) is null\n", fileName);
|
|
return 0;
|
|
}
|
|
|
|
if (access(fileName, F_OK) == 0) {
|
|
struct stat statbuf;
|
|
stat(fileName, &statbuf);
|
|
int filesize = (int)statbuf.st_size;
|
|
LOGD("file(%s) size is %d\n", fileName, filesize);
|
|
return filesize;
|
|
} else {
|
|
LOGE("read file (%s) error\n", fileName);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
void UenvInit(void)
|
|
{
|
|
int ret = bootenv_init();
|
|
if (ret < 0) {
|
|
LOGD("%s: init uenv failed, ret = %d\n", __FUNCTION__, ret);
|
|
}
|
|
}
|
|
|
|
const char* GetUenv(const char *key)
|
|
{
|
|
const char *buf = bootenv_get(key);
|
|
if (buf) {
|
|
LOGD("%s: (%s = %s)\n", __FUNCTION__, key, buf);
|
|
} else {
|
|
LOGD("%s: Can not read uboot env \"%s\".", __FUNCTION__, key);
|
|
}
|
|
return buf;
|
|
}
|
|
|
|
int SetUenv (const char *name, const char *value)
|
|
{
|
|
int ret = -1;
|
|
ret = bootenv_update(name, value);
|
|
if (ret < 0) {
|
|
LOGD("%s: (%s = %s) failed!", __FUNCTION__, name, value);
|
|
} else {
|
|
LOGD("%s: (%s = %s) success!", __FUNCTION__, name, value);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
bool hasFileWithSubstring(const std::string& directory, const std::string& substring) {
|
|
DIR* dir = opendir(directory.c_str());
|
|
if (!dir) {
|
|
std::cerr << "Error opening directory: " << directory << std::endl;
|
|
return false;
|
|
}
|
|
|
|
struct dirent* entry;
|
|
while ((entry = readdir(dir)) != nullptr) {
|
|
std::string filename = entry->d_name;
|
|
|
|
if (entry->d_type == DT_REG && filename.find(substring) != std::string::npos) {
|
|
closedir(dir);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
closedir(dir);
|
|
return false;
|
|
}
|
|
|
|
bool isDiBackend()
|
|
{
|
|
char buf[32] = {0};
|
|
int readRet = tvReadSysfs(DI_RPOC_ENABLE_PATH, buf);
|
|
if (readRet >= 0 && strcmp("1", buf) == 0) {//back_DI
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|