1 /*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "devicestatus_data_parse.h"
17
18 namespace OHOS {
19 namespace Msdp {
20 namespace DeviceStatus {
21 namespace {
22 constexpr int32_t FILE_SIZE_MAX = 0x5000;
23 constexpr int32_t READ_DATA_BUFF_SIZE = 256;
24 constexpr int32_t INVALID_FILE_SIZE = -1;
25 const std::string MSDP_DATA_PATH = "/data/msdp/device_status_data.json";
26 const std::string MSDP_DATA_DIR = "/data/msdp";
27 } // namespace
28
29 std::vector<int32_t> DeviceStatusDataParse::tempcount_ =
30 std::vector<int32_t> (static_cast<int32_t>(DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN),
31 static_cast<int32_t>(DevicestatusDataUtils::Value::INVALID));
32
ParseDeviceStatusData(DevicestatusDataUtils::DevicestatusData & data,DevicestatusDataUtils::DevicestatusType & type)33 bool DeviceStatusDataParse::ParseDeviceStatusData(DevicestatusDataUtils::DevicestatusData& data,
34 DevicestatusDataUtils::DevicestatusType& type)
35 {
36 std::string jsonBuf = ReadJsonFile(MSDP_DATA_PATH.c_str());
37 if (jsonBuf.empty()) {
38 DEV_HILOGE(SERVICE, "read json failed, errno is %{public}d", errno);
39 }
40 return DataInit(jsonBuf, true, type, data);
41 }
42
DataInit(const std::string & fileData,bool logStatus,DevicestatusDataUtils::DevicestatusType & type,DevicestatusDataUtils::DevicestatusData & data)43 bool DeviceStatusDataParse::DataInit(const std::string& fileData, bool logStatus,
44 DevicestatusDataUtils::DevicestatusType& type, DevicestatusDataUtils::DevicestatusData& data)
45 {
46 DEV_HILOGD(SERVICE, "Enter");
47 JsonParser parser;
48 parser.json_ = cJSON_Parse(fileData.c_str());
49 data.type = type;
50 data.value = DevicestatusDataUtils::DevicestatusValue::VALUE_INVALID;
51 if (cJSON_IsArray(parser.json_)) {
52 DEV_HILOGE(SERVICE, "parser is array");
53 return false;
54 }
55
56 if (type < DevicestatusDataUtils::DevicestatusType::TYPE_STILL ||
57 type >= DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN) {
58 DEV_HILOGE(SERVICE, "Type error");
59 return false;
60 }
61
62 cJSON* mockarray = cJSON_GetObjectItem(parser.json_, DeviceStatusJson[type].json.c_str());
63 int32_t jsonsize = cJSON_GetArraySize(mockarray);
64 if (jsonsize == 0) {
65 return false;
66 }
67 tempcount_[type] = tempcount_[type] % jsonsize;
68 cJSON* mockvalue = cJSON_GetArrayItem(mockarray, tempcount_[type]);
69 if (mockvalue == nullptr) {
70 return false;
71 }
72 tempcount_[type]++;
73 data.value = static_cast<DevicestatusDataUtils::DevicestatusValue>(mockvalue->valueint);
74 DEV_HILOGD(SERVICE, "type:%{public}d, status:%{public}d", data.type, data.value);
75 return true;
76 }
77
DisableCount(const DevicestatusDataUtils::DevicestatusType & type)78 bool DeviceStatusDataParse::DisableCount(const DevicestatusDataUtils::DevicestatusType& type)
79 {
80 DEV_HILOGD(SERVICE, "Enter");
81 tempcount_[static_cast<int32_t>(type)] =
82 static_cast<int32_t>(DevicestatusDataUtils::DevicestatusDataUtils::DevicestatusValue::VALUE_INVALID);
83 return true;
84 }
85
ReadJsonFile(const std::string & filePath)86 std::string DeviceStatusDataParse::ReadJsonFile(const std::string &filePath)
87 {
88 if (filePath.empty()) {
89 DEV_HILOGE(SERVICE, "Path is empty");
90 return "";
91 }
92 char realPath[PATH_MAX] = {};
93 if (realpath(filePath.c_str(), realPath) == nullptr) {
94 DEV_HILOGE(SERVICE, "Path is error, %{public}d", errno);
95 return "";
96 }
97 if (!CheckFileDir(realPath, MSDP_DATA_DIR)) {
98 DEV_HILOGE(SERVICE, "File dir is invalid");
99 return "";
100 }
101 if (!CheckFileExtendName(realPath, "json")) {
102 DEV_HILOGE(SERVICE, "Unable to parse files other than json format");
103 return "";
104 }
105 if (!IsFileExists(filePath)) {
106 DEV_HILOGE(SERVICE, "File not exist");
107 return "";
108 }
109 if (CheckFileSize(filePath) == INVALID_FILE_SIZE) {
110 DEV_HILOGE(SERVICE, "File size out of read range");
111 return "";
112 }
113 return ReadFile(realPath);
114 }
115
GetFileSize(const std::string & filePath)116 int32_t DeviceStatusDataParse::GetFileSize(const std::string& filePath)
117 {
118 struct stat statbuf = {0};
119 if (stat(filePath.c_str(), &statbuf) != 0) {
120 DEV_HILOGE(SERVICE, "Get file size error");
121 return INVALID_FILE_SIZE;
122 }
123 return statbuf.st_size;
124 }
125
CheckFileDir(const std::string & filePath,const std::string & dir)126 bool DeviceStatusDataParse::CheckFileDir(const std::string& filePath, const std::string& dir)
127 {
128 if (filePath.compare(0, MSDP_DATA_DIR.size(), MSDP_DATA_DIR) != 0) {
129 DEV_HILOGE(SERVICE, "FilePath dir is invalid");
130 return false;
131 }
132 return true;
133 }
134
CheckFileSize(const std::string & filePath)135 int32_t DeviceStatusDataParse::CheckFileSize(const std::string& filePath)
136 {
137 int32_t fileSize = GetFileSize(filePath);
138 if ((fileSize <= 0) || (fileSize > FILE_SIZE_MAX)) {
139 DEV_HILOGE(SERVICE, "File size out of read range");
140 return INVALID_FILE_SIZE;
141 }
142 return fileSize;
143 }
144
CheckFileExtendName(const std::string & filePath,const std::string & checkExtension)145 bool DeviceStatusDataParse::CheckFileExtendName(const std::string& filePath, const std::string& checkExtension)
146 {
147 std::string::size_type pos = filePath.find_last_of('.');
148 if (pos == std::string::npos) {
149 DEV_HILOGE(SERVICE, "File is not find extension");
150 return false;
151 }
152 return (filePath.substr(pos + 1, filePath.npos) == checkExtension);
153 }
154
IsFileExists(const std::string & fileName)155 bool DeviceStatusDataParse::IsFileExists(const std::string& fileName)
156 {
157 return (access(fileName.c_str(), F_OK) == 0);
158 }
159
ReadFile(const std::string & filePath)160 std::string DeviceStatusDataParse::ReadFile(const std::string &filePath)
161 {
162 DEV_HILOGD(SERVICE, "Enter");
163 FILE* fp = fopen(filePath.c_str(), "r");
164 if (fp == nullptr) {
165 DEV_HILOGE(SERVICE, "Open failed");
166 return "";
167 }
168 std::string dataStr;
169 char buf[READ_DATA_BUFF_SIZE] = {};
170 while (fgets(buf, sizeof(buf), fp) != nullptr) {
171 dataStr += buf;
172 }
173 if (fclose(fp) != 0) {
174 DEV_HILOGW(SERVICE, "Close file failed");
175 }
176 DEV_HILOGD(SERVICE, "Exit");
177 return dataStr;
178 }
179 } // namespace DeviceStatus
180 } // namespace Msdp
181 } // namespace OHOS
182