提交 3eb4700a 编写于 作者: C chengjinsong2

add simulator for sdk

Signed-off-by: Nchengjinsong2 <chengjinsong2@huawei.com>
上级 eecebad4
# Copyright (c) 2020 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/ohos.gni")
config("sysparam_simulator_public_config") {
include_dirs = [
"../interfaces/innerkits/include/syspara",
"../interfaces/hals",
]
}
config("sysparam_simulator_config") {
cflags = [
"-Wno-error",
"-D_INC_STDIO_S",
"-D_INC_STDLIB_S",
"-D_INC_MEMORY_S",
"-D_INC_STRING_S",
"-D_INC_WCHAR_S",
"-D_SECTMP=//",
"-D_STDIO_S_DEFINED",
]
include_dirs = [
"../../../../commonlibrary/utils_lite/include",
"//third_party/bounds_checking_function/include",
"./parameter/src",
"../interfaces/innerkits/include/syspara",
]
}
ohos_static_library("sysparam_simulator") {
public_configs = [ ":sysparam_simulator_public_config" ]
configs = [ ":sysparam_simulator_config" ]
sources = [
"./parameter/src/param_impl_posix/param_impl_posix.c",
"./parameter/src/parameter_common.c",
]
deps = [ "//third_party/bounds_checking_function:libsec_static" ]
defines = [
"INCREMENTAL_VERSION=\"\"",
"BUILD_TYPE=\"\"",
"BUILD_USER=\"\"",
"BUILD_TIME=\"\"",
"BUILD_HOST=\"\"",
"BUILD_ROOTHASH=\"\"",
]
part_name = "init"
subsystem_name = "startup"
}
/*
* Copyright (c) 2020 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef IOT_SYSPARA_API_H
#define IOT_SYSPARA_API_H
#include "ohos_types.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif /* __cplusplus */
#define MAX_GET_VALUE_LEN 0x7FFFFFFF
#define MAX_KEY_LEN 32
#define MAX_VALUE_LEN 128
int GetSysParam(const char* key, char* value, unsigned int len);
int SetSysParam(const char* key, const char* value);
boolean CheckPermission(void);
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */
#endif // IOT_SYSPARA_API_H
/*
* Copyright (c) 2020 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <ctype.h>
#include <fcntl.h>
#include <limits.h>
#include <securec.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "ohos_errno.h"
#include "param_adaptor.h"
#ifndef __LITEOS_M__
#define DATA_PATH "/storage/data/system/param/"
#define SYS_UID_INDEX 1000
#else
#ifndef DATA_PATH
#define DATA_PATH ""
#endif
#endif
#define MAX_KEY_PATH 128
static boolean IsValidChar(const char ch)
{
if (islower(ch) || isdigit(ch) || (ch == '_') || (ch == '.')) {
return TRUE;
}
return FALSE;
}
static boolean IsValidValue(const char* value, unsigned int len)
{
if ((value == NULL) || !strlen(value) || (strlen(value) >= len)) {
return FALSE;
}
return TRUE;
}
static boolean IsValidKey(const char* key)
{
if (!IsValidValue(key, MAX_KEY_LEN)) {
return FALSE;
}
int keyLen = strlen(key);
for (int i = 0; i < keyLen; i++) {
if (!IsValidChar(key[i])) {
return FALSE;
}
}
return TRUE;
}
int GetSysParam(const char* key, char* value, unsigned int len)
{
if (!IsValidKey(key) || (value == NULL) || (len > MAX_GET_VALUE_LEN)) {
return EC_INVALID;
}
char* keyPath = (char *)malloc(MAX_KEY_PATH + 1);
if (keyPath == NULL) {
return EC_FAILURE;
}
if (sprintf_s(keyPath, MAX_KEY_PATH + 1, "%s%s", DATA_PATH, key) < 0) {
free(keyPath);
return EC_FAILURE;
}
struct stat info = {0};
if (stat(keyPath, &info) != F_OK) {
free(keyPath);
return EC_FAILURE;
}
if (info.st_size >= len) {
free(keyPath);
return EC_INVALID;
}
int fd = open(keyPath, O_RDONLY, S_IRUSR);
free(keyPath);
keyPath = NULL;
if (fd < 0) {
return EC_FAILURE;
}
int ret = read(fd, value, (size_t)info.st_size);
close(fd);
fd = -1;
if (ret < 0) {
return EC_FAILURE;
}
value[info.st_size] = '\0';
return info.st_size;
}
int SetSysParam(const char* key, const char* value)
{
if (!IsValidKey(key) || !IsValidValue(value, MAX_VALUE_LEN)) {
return EC_INVALID;
}
char* keyPath = (char *)malloc(MAX_KEY_PATH + 1);
if (keyPath == NULL) {
return EC_FAILURE;
}
if (sprintf_s(keyPath, MAX_KEY_PATH + 1, "%s%s", DATA_PATH, key) < 0) {
free(keyPath);
return EC_FAILURE;
}
int fd = open(keyPath, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
free(keyPath);
keyPath = NULL;
if (fd < 0) {
return EC_FAILURE;
}
int ret = write(fd, value, strlen(value));
close(fd);
fd = -1;
return (ret < 0) ? EC_FAILURE : EC_SUCCESS;
}
boolean CheckPermission(void)
{
#if (!defined(_WIN32) && !defined(_WIN64) && !defined(__LITEOS_M__))
uid_t uid = getuid();
if (uid <= SYS_UID_INDEX) {
return TRUE;
}
#endif
#if defined(__LITEOS_M__)
return TRUE;
#else
return FALSE;
#endif
}
/*
* Copyright (c) 2020 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <fcntl.h>
#include <securec.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include "hal_sys_param.h"
#ifdef USE_MBEDTLS
#include "mbedtls/sha256.h"
#endif
#include "ohos_errno.h"
#include "param_adaptor.h"
#include "parameter.h"
#define FILE_RO "ro."
#define OS_FULL_NAME_LEN 128
#define VERSION_ID_LEN 256
#define HASH_LENGTH 32
#define DEV_BUF_LENGTH 3
#define DEV_BUF_MAX_LENGTH 1024
#define DEV_UUID_LENGTH 65
#define OHOS_DISPLAY_VERSION_LEN 128
#define OHOS_PATCH_VERSION_LEN 64
#define OHOS_PATCH_VERSION_FILE "/patch/pversion"
static const char OHOS_OS_NAME[] = { "OpenHarmony" };
static const int OHOS_SDK_API_VERSION = 6;
static const char OHOS_SECURITY_PATCH_TAG[] = {"2021-09-01"};
static const char OHOS_RELEASE_TYPE[] = { "Beta" };
static const int MAJOR_VERSION = 1;
static const int SENIOR_VERSION = 0;
static const int FEATURE_VERSION = 1;
static const int BUILD_VERSION = 0;
static boolean IsValidValue(const char *value, unsigned int len)
{
if ((value == NULL) || !strlen(value) || (strlen(value) + 1 > len)) {
return FALSE;
}
return TRUE;
}
int GetParameter(const char *key, const char *def, char *value, unsigned int len)
{
if ((key == NULL) || (value == NULL)) {
return EC_INVALID;
}
if (!CheckPermission()) {
return EC_FAILURE;
}
int ret = GetSysParam(key, value, len);
if (ret == EC_INVALID) {
return EC_INVALID;
}
if ((ret < 0) && IsValidValue(def, len)) {
if (strncpy_s(value, len, def, len - 1) != 0) {
return EC_FAILURE;
}
ret = (int)strlen(def);
}
return ret;
}
int SetParameter(const char *key, const char *value)
{
if ((key == NULL) || (value == NULL)) {
return EC_INVALID;
}
if (!CheckPermission()) {
return EC_FAILURE;
}
if (strncmp(key, FILE_RO, strlen(FILE_RO)) == 0) {
return EC_INVALID;
}
return SetSysParam(key, value);
}
const char *GetDeviceType(void)
{
return HalGetDeviceType();
}
const char *GetManufacture(void)
{
return HalGetManufacture();
}
const char *GetBrand(void)
{
return HalGetBrand();
}
const char *GetMarketName(void)
{
return HalGetMarketName();
}
const char *GetProductSeries(void)
{
return HalGetProductSeries();
}
const char *GetProductModel(void)
{
return HalGetProductModel();
}
const char *GetSoftwareModel(void)
{
return HalGetSoftwareModel();
}
const char *GetHardwareModel(void)
{
return HalGetHardwareModel();
}
const char *GetHardwareProfile(void)
{
return HalGetHardwareProfile();
}
const char *GetSerial(void)
{
return HalGetSerial();
}
const char *GetBootloaderVersion(void)
{
return HalGetBootloaderVersion();
}
const char *GetSecurityPatchTag(void)
{
return OHOS_SECURITY_PATCH_TAG;
}
const char *GetAbiList(void)
{
return HalGetAbiList();
}
static const char *BuildOSFullName(void)
{
const char release[] = "Release";
char value[OS_FULL_NAME_LEN];
const char *releaseType = GetOsReleaseType();
int length;
if (strncmp(releaseType, release, sizeof(release) - 1) == 0) {
length = sprintf_s(value, OS_FULL_NAME_LEN, "%s-%d.%d.%d.%d",
OHOS_OS_NAME, MAJOR_VERSION, SENIOR_VERSION, FEATURE_VERSION, BUILD_VERSION);
} else {
length = sprintf_s(value, OS_FULL_NAME_LEN, "%s-%d.%d.%d.%d(%s)",
OHOS_OS_NAME, MAJOR_VERSION, SENIOR_VERSION, FEATURE_VERSION, BUILD_VERSION, releaseType);
}
if (length < 0) {
return EMPTY_STR;
}
const char *osFullName = strdup(value);
return osFullName;
}
const char *GetOSFullName(void)
{
static const char *osFullName = NULL;
if (osFullName != NULL) {
return osFullName;
}
osFullName = BuildOSFullName();
if (osFullName == NULL) {
return EMPTY_STR;
}
return osFullName;
}
static const char *BuildDisplayVersion(void)
{
ssize_t len;
char patchValue[OHOS_PATCH_VERSION_LEN] = {0};
char displayValue[OHOS_DISPLAY_VERSION_LEN] = {0};
int fd = open(OHOS_PATCH_VERSION_FILE, O_RDONLY);
if (fd < 0) {
return NULL;
}
len = read(fd, patchValue, OHOS_PATCH_VERSION_LEN);
if (len < (ssize_t)strlen("version=")) {
close(fd);
return NULL;
}
close(fd);
if (patchValue[len - 1] == '\n') {
patchValue[len - 1] = '\0';
}
const char *versionValue = HalGetDisplayVersion();
const int versionLen = strlen(versionValue);
if (versionLen > 0) {
if (versionValue[versionLen - 1] != ')') {
len = sprintf_s(displayValue, OHOS_DISPLAY_VERSION_LEN, "%s(%s)", versionValue,
patchValue + strlen("version="));
} else {
char tempValue[versionLen];
(void)memset_s(tempValue, versionLen, 0, versionLen);
if (strncpy_s(tempValue, versionLen, versionValue, versionLen - 1) != 0) {
return NULL;
}
tempValue[versionLen - 1] = '\0';
len = sprintf_s(displayValue, OHOS_DISPLAY_VERSION_LEN, "%s%s)", tempValue,
patchValue + strlen("version="));
}
}
if (len < 0) {
return NULL;
}
return strdup(displayValue);
}
const char *GetDisplayVersion(void)
{
static const char *displayVersion = NULL;
if (displayVersion != NULL) {
return displayVersion;
}
displayVersion = BuildDisplayVersion();
if (displayVersion == NULL) {
return HalGetDisplayVersion();
}
return displayVersion;
}
int GetSdkApiVersion(void)
{
return OHOS_SDK_API_VERSION;
}
int GetFirstApiVersion(void)
{
return HalGetFirstApiVersion();
}
const char *GetIncrementalVersion(void)
{
return HalGetIncrementalVersion();
}
static const char *BuildVersionId(void)
{
char value[VERSION_ID_LEN];
int len = sprintf_s(value, VERSION_ID_LEN, "%s/%s/%s/%s/%s/%s/%s/%d/%s/%s",
GetDeviceType(), GetManufacture(), GetBrand(), GetProductSeries(),
GetOSFullName(), GetProductModel(), GetSoftwareModel(),
OHOS_SDK_API_VERSION, GetIncrementalVersion(), GetBuildType());
if (len < 0) {
return EMPTY_STR;
}
const char *versionId = strdup(value);
return versionId;
}
const char *GetVersionId(void)
{
static const char *versionId = NULL;
if (versionId != NULL) {
return versionId;
}
versionId = BuildVersionId();
if (versionId == NULL) {
return EMPTY_STR;
}
return versionId;
}
const char *GetBuildType(void)
{
return HalGetBuildType();
}
const char *GetBuildUser(void)
{
return HalGetBuildUser();
}
const char *GetBuildHost(void)
{
return HalGetBuildHost();
}
const char *GetBuildTime(void)
{
return HalGetBuildTime();
}
const char *GetBuildRootHash(void)
{
return BUILD_ROOTHASH;
}
const char *GetOsReleaseType(void)
{
return OHOS_RELEASE_TYPE;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册