106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License.
506f6ba60Sopenharmony_ci * You may obtain a copy of the License at
606f6ba60Sopenharmony_ci *
706f6ba60Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
806f6ba60Sopenharmony_ci *
906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and
1306f6ba60Sopenharmony_ci * limitations under the License.
1406f6ba60Sopenharmony_ci */
1506f6ba60Sopenharmony_ci#include "xpower_decoder.h"
1606f6ba60Sopenharmony_ci#include <securec.h>
1706f6ba60Sopenharmony_ci#include <climits>
1806f6ba60Sopenharmony_ci#include <cstring>
1906f6ba60Sopenharmony_ci#include "xpower_common.h"
2006f6ba60Sopenharmony_ciXpowerDecoder::XpowerDecoder() {}
2106f6ba60Sopenharmony_ci
2206f6ba60Sopenharmony_ciXpowerDecoder::~XpowerDecoder() {}
2306f6ba60Sopenharmony_ci
2406f6ba60Sopenharmony_cicJSON* XpowerDecoder::CreateAppDetail(const AppDetail& appDetail)
2506f6ba60Sopenharmony_ci{
2606f6ba60Sopenharmony_ci    cJSON* cjsonAppDetail = cJSON_CreateObject();
2706f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonAppDetail, "cpu", CreateAppDetailCpu(appDetail.cpu()));
2806f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonAppDetail, "gpu", CreateAppDetailGpu(appDetail.gpu()));
2906f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonAppDetail, "wifi", CreateAppDetailWifi(appDetail.wifi()));
3006f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonAppDetail, "display", CreateAppDetailDisplay(appDetail.display()));
3106f6ba60Sopenharmony_ci    return cjsonAppDetail;
3206f6ba60Sopenharmony_ci}
3306f6ba60Sopenharmony_ci
3406f6ba60Sopenharmony_cicJSON* XpowerDecoder::CreateAppStatistic(const AppStatistic& appStatistic)
3506f6ba60Sopenharmony_ci{
3606f6ba60Sopenharmony_ci    cJSON* cjsonAppStatistic = cJSON_CreateObject();
3706f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonAppStatistic, "audio", CreatAppStatisticCommon(appStatistic.audio()));
3806f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonAppStatistic, "bluetooth", CreatAppStatisticCommon(appStatistic.audio()));
3906f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonAppStatistic, "camera", CreatAppStatisticCommon(appStatistic.camera()));
4006f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonAppStatistic, "cpu", CreatAppStatisticCommon(appStatistic.cpu()));
4106f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonAppStatistic, "display", CreatAppStatisticCommon(appStatistic.display()));
4206f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonAppStatistic, "flashlight", CreatAppStatisticCommon(appStatistic.flashlight()));
4306f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonAppStatistic, "gpu", CreatAppStatisticCommon(appStatistic.gpu()));
4406f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonAppStatistic, "location", CreatAppStatisticCommon(appStatistic.location()));
4506f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonAppStatistic, "wifiscan", CreatAppStatisticCommon(appStatistic.wifiscan()));
4606f6ba60Sopenharmony_ci    return cjsonAppStatistic;
4706f6ba60Sopenharmony_ci}
4806f6ba60Sopenharmony_ci
4906f6ba60Sopenharmony_cicJSON* XpowerDecoder::CreateAppDetailGpu(const AppDetailGPU& appDeGpu)
5006f6ba60Sopenharmony_ci{
5106f6ba60Sopenharmony_ci    cJSON* cjsonGpu = cJSON_CreateObject();
5206f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(cjsonGpu, "frequency_count", appDeGpu.frequency_count());
5306f6ba60Sopenharmony_ci    cJSON* frequencyArr = cJSON_CreateArray();
5406f6ba60Sopenharmony_ci    for (int i = 0; i < appDeGpu.frequency().size(); i++) {
5506f6ba60Sopenharmony_ci        cJSON_AddItemToArray(frequencyArr, cJSON_CreateNumber(appDeGpu.frequency(i)));
5606f6ba60Sopenharmony_ci    }
5706f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonGpu, "frequency", frequencyArr);
5806f6ba60Sopenharmony_ci
5906f6ba60Sopenharmony_ci    cJSON* idleTimeArr = cJSON_CreateArray();
6006f6ba60Sopenharmony_ci    for (int i = 0; i < appDeGpu.idle_time().size(); i++) {
6106f6ba60Sopenharmony_ci        cJSON_AddItemToArray(idleTimeArr, cJSON_CreateNumber(appDeGpu.idle_time(i)));
6206f6ba60Sopenharmony_ci    }
6306f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonGpu, "idle_time", idleTimeArr);
6406f6ba60Sopenharmony_ci
6506f6ba60Sopenharmony_ci    cJSON* runTimeArr = cJSON_CreateArray();
6606f6ba60Sopenharmony_ci    for (int i = 0; i < appDeGpu.run_time().size(); i++) {
6706f6ba60Sopenharmony_ci        cJSON_AddItemToArray(runTimeArr, cJSON_CreateNumber(appDeGpu.run_time(i)));
6806f6ba60Sopenharmony_ci    }
6906f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonGpu, "run_time", runTimeArr);
7006f6ba60Sopenharmony_ci    return cjsonGpu;
7106f6ba60Sopenharmony_ci}
7206f6ba60Sopenharmony_ci
7306f6ba60Sopenharmony_cicJSON* XpowerDecoder::CreateAppDetailWifi(const AppDetailWifi& appWifi)
7406f6ba60Sopenharmony_ci{
7506f6ba60Sopenharmony_ci    cJSON* cjsonWifi = cJSON_CreateObject();
7606f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(cjsonWifi, "tx_packets", appWifi.tx_packets());
7706f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(cjsonWifi, "rx_packets", appWifi.rx_packets());
7806f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(cjsonWifi, "tx_bytes", appWifi.tx_bytes());
7906f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(cjsonWifi, "rx_bytes", appWifi.rx_bytes());
8006f6ba60Sopenharmony_ci    return cjsonWifi;
8106f6ba60Sopenharmony_ci}
8206f6ba60Sopenharmony_ci
8306f6ba60Sopenharmony_cicJSON* XpowerDecoder::CreateAppDetailDisplay(const AppDetailDisplay& appDisplay)
8406f6ba60Sopenharmony_ci{
8506f6ba60Sopenharmony_ci    cJSON* cjsonDisplay = cJSON_CreateObject();
8606f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(cjsonDisplay, "count_1hz", appDisplay.count_1hz());
8706f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(cjsonDisplay, "count_10hz", appDisplay.count_10hz());
8806f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(cjsonDisplay, "count_15hz", appDisplay.count_15hz());
8906f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(cjsonDisplay, "count_24hz", appDisplay.count_24hz());
9006f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(cjsonDisplay, "count_30hz", appDisplay.count_30hz());
9106f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(cjsonDisplay, "count_45hz", appDisplay.count_45hz());
9206f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(cjsonDisplay, "count_60hz", appDisplay.count_60hz());
9306f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(cjsonDisplay, "count_90hz", appDisplay.count_90hz());
9406f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(cjsonDisplay, "count_120hz", appDisplay.count_120hz());
9506f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(cjsonDisplay, "count_180hz", appDisplay.count_180hz());
9606f6ba60Sopenharmony_ci    return cjsonDisplay;
9706f6ba60Sopenharmony_ci}
9806f6ba60Sopenharmony_ci
9906f6ba60Sopenharmony_cicJSON* XpowerDecoder::CreateAppDetailCpu(const AppDetailCPU& appDeCpu)
10006f6ba60Sopenharmony_ci{
10106f6ba60Sopenharmony_ci    cJSON* cjsonCpu = cJSON_CreateObject();
10206f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(cjsonCpu, "thread_count", appDeCpu.thread_count());
10306f6ba60Sopenharmony_ci    cJSON* threadLoadArr = cJSON_CreateArray();
10406f6ba60Sopenharmony_ci    for (int i = 0; i < appDeCpu.thread_load().size(); i++) {
10506f6ba60Sopenharmony_ci        cJSON_AddItemToArray(threadLoadArr, cJSON_CreateNumber(appDeCpu.thread_load(i)));
10606f6ba60Sopenharmony_ci    }
10706f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonCpu, "thread_load", threadLoadArr);
10806f6ba60Sopenharmony_ci
10906f6ba60Sopenharmony_ci    cJSON* threadNameArr = cJSON_CreateArray();
11006f6ba60Sopenharmony_ci    for (int i = 0; i < appDeCpu.thread_name().size(); i++) {
11106f6ba60Sopenharmony_ci        cJSON_AddItemToArray(threadNameArr, cJSON_CreateString(appDeCpu.thread_name(i).c_str()));
11206f6ba60Sopenharmony_ci    }
11306f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonCpu, "thread_name", threadNameArr);
11406f6ba60Sopenharmony_ci
11506f6ba60Sopenharmony_ci    cJSON* threadTimeArr = cJSON_CreateArray();
11606f6ba60Sopenharmony_ci    for (int i = 0; i < appDeCpu.thread_time().size(); i++) {
11706f6ba60Sopenharmony_ci        cJSON_AddItemToArray(threadTimeArr, cJSON_CreateNumber(appDeCpu.thread_time(i)));
11806f6ba60Sopenharmony_ci    }
11906f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonCpu, "thread_time", threadTimeArr);
12006f6ba60Sopenharmony_ci
12106f6ba60Sopenharmony_ci    cJSON* threadEnergyArr = cJSON_CreateArray();
12206f6ba60Sopenharmony_ci    for (int i = 0; i < appDeCpu.thread_energy().size(); i++) {
12306f6ba60Sopenharmony_ci        cJSON_AddItemToArray(threadEnergyArr, cJSON_CreateNumber(appDeCpu.thread_energy(i)));
12406f6ba60Sopenharmony_ci    }
12506f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonCpu, "thread_energy", threadEnergyArr);
12606f6ba60Sopenharmony_ci    return cjsonCpu;
12706f6ba60Sopenharmony_ci}
12806f6ba60Sopenharmony_ci
12906f6ba60Sopenharmony_cicJSON* XpowerDecoder::CreatAppStatisticCommon(const AppStatisticCommon& appCom)
13006f6ba60Sopenharmony_ci{
13106f6ba60Sopenharmony_ci    cJSON* cjsonAppCom = cJSON_CreateObject();
13206f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(cjsonAppCom, "energy", appCom.energy());
13306f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(cjsonAppCom, "time", appCom.time());
13406f6ba60Sopenharmony_ci    return cjsonAppCom;
13506f6ba60Sopenharmony_ci}
13606f6ba60Sopenharmony_ci
13706f6ba60Sopenharmony_cicJSON* XpowerDecoder::CreateRealBattery(const RealBattery& realBattery)
13806f6ba60Sopenharmony_ci{
13906f6ba60Sopenharmony_ci    cJSON* cjsonBattery = cJSON_CreateObject();
14006f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(cjsonBattery, "capacity", realBattery.capacity());
14106f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(cjsonBattery, "charge", realBattery.charge());
14206f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(cjsonBattery, "current", realBattery.current());
14306f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(cjsonBattery, "gas_gauge", realBattery.gas_gauge());
14406f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(cjsonBattery, "level", realBattery.level());
14506f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(cjsonBattery, "screen", realBattery.screen());
14606f6ba60Sopenharmony_ci    return cjsonBattery;
14706f6ba60Sopenharmony_ci}
14806f6ba60Sopenharmony_ci
14906f6ba60Sopenharmony_cicJSON* XpowerDecoder::CreateComponentTopCommon(const ComponentTopCommon& topCommon)
15006f6ba60Sopenharmony_ci{
15106f6ba60Sopenharmony_ci    cJSON* cjsonTopCommon = cJSON_CreateObject();
15206f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(cjsonTopCommon, "count", topCommon.count());
15306f6ba60Sopenharmony_ci    cJSON* appnameArr = cJSON_CreateArray();
15406f6ba60Sopenharmony_ci    for (int i = 0; i < topCommon.appname().size(); i++) {
15506f6ba60Sopenharmony_ci        cJSON_AddItemToArray(appnameArr, cJSON_CreateString(topCommon.appname(i).c_str()));
15606f6ba60Sopenharmony_ci    }
15706f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "appname", appnameArr);
15806f6ba60Sopenharmony_ci
15906f6ba60Sopenharmony_ci    cJSON* durationArr = cJSON_CreateArray();
16006f6ba60Sopenharmony_ci    for (int i = 0; i < topCommon.background_duration().size(); i++) {
16106f6ba60Sopenharmony_ci        cJSON_AddItemToArray(durationArr, cJSON_CreateNumber(topCommon.background_duration(i)));
16206f6ba60Sopenharmony_ci    }
16306f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "background_duration", durationArr);
16406f6ba60Sopenharmony_ci
16506f6ba60Sopenharmony_ci    cJSON* energyArr = cJSON_CreateArray();
16606f6ba60Sopenharmony_ci    for (int i = 0; i < topCommon.background_energy().size(); i++) {
16706f6ba60Sopenharmony_ci        cJSON_AddItemToArray(energyArr, cJSON_CreateNumber(topCommon.background_energy(i)));
16806f6ba60Sopenharmony_ci    }
16906f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "background_energy", energyArr);
17006f6ba60Sopenharmony_ci
17106f6ba60Sopenharmony_ci    cJSON* foreDurationArr = cJSON_CreateArray();
17206f6ba60Sopenharmony_ci    for (int i = 0; i < topCommon.foreground_duration().size(); i++) {
17306f6ba60Sopenharmony_ci        cJSON_AddItemToArray(foreDurationArr, cJSON_CreateNumber(topCommon.foreground_duration(i)));
17406f6ba60Sopenharmony_ci    }
17506f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "foreground_duration", foreDurationArr);
17606f6ba60Sopenharmony_ci
17706f6ba60Sopenharmony_ci    cJSON* foreEnergyArr = cJSON_CreateArray();
17806f6ba60Sopenharmony_ci    for (int i = 0; i < topCommon.foreground_energy().size(); i++) {
17906f6ba60Sopenharmony_ci        cJSON_AddItemToArray(foreEnergyArr, cJSON_CreateNumber(topCommon.foreground_energy(i)));
18006f6ba60Sopenharmony_ci    }
18106f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "foreground_energy", foreEnergyArr);
18206f6ba60Sopenharmony_ci
18306f6ba60Sopenharmony_ci    cJSON* screenDuraArr = cJSON_CreateArray();
18406f6ba60Sopenharmony_ci    for (int i = 0; i < topCommon.screen_off_duration().size(); i++) {
18506f6ba60Sopenharmony_ci        cJSON_AddItemToArray(screenDuraArr, cJSON_CreateNumber(topCommon.screen_off_duration(i)));
18606f6ba60Sopenharmony_ci    }
18706f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "screen_off_duration", screenDuraArr);
18806f6ba60Sopenharmony_ci
18906f6ba60Sopenharmony_ci    cJSON* screenEnergyArr = cJSON_CreateArray();
19006f6ba60Sopenharmony_ci    for (int i = 0; i < topCommon.screen_off_energy().size(); i++) {
19106f6ba60Sopenharmony_ci        cJSON_AddItemToArray(screenEnergyArr, cJSON_CreateNumber(topCommon.screen_off_energy(i)));
19206f6ba60Sopenharmony_ci    }
19306f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "screen_off_energy", screenEnergyArr);
19406f6ba60Sopenharmony_ci
19506f6ba60Sopenharmony_ci    cJSON* screenOnDuraArr = cJSON_CreateArray();
19606f6ba60Sopenharmony_ci    for (int i = 0; i < topCommon.screen_on_duration().size(); i++) {
19706f6ba60Sopenharmony_ci        cJSON_AddItemToArray(screenOnDuraArr, cJSON_CreateNumber(topCommon.screen_on_duration(i)));
19806f6ba60Sopenharmony_ci    }
19906f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "screen_on_duration", screenOnDuraArr);
20006f6ba60Sopenharmony_ci
20106f6ba60Sopenharmony_ci    cJSON* screenOnEnergyArr = cJSON_CreateArray();
20206f6ba60Sopenharmony_ci    for (int i = 0; i < topCommon.screen_on_energy().size(); i++) {
20306f6ba60Sopenharmony_ci        cJSON_AddItemToArray(screenOnEnergyArr, cJSON_CreateNumber(topCommon.screen_on_energy(i)));
20406f6ba60Sopenharmony_ci    }
20506f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "screen_on_energy", screenOnEnergyArr);
20606f6ba60Sopenharmony_ci    return cjsonTopCommon;
20706f6ba60Sopenharmony_ci}
20806f6ba60Sopenharmony_cicJSON* XpowerDecoder::CreateComponentTopCamera(const ComponentTopCamera& topCamera)
20906f6ba60Sopenharmony_ci{
21006f6ba60Sopenharmony_ci    cJSON* cjsonTopCommon = cJSON_CreateObject();
21106f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(cjsonTopCommon, "count", topCamera.count());
21206f6ba60Sopenharmony_ci    cJSON* appnameArr = cJSON_CreateArray();
21306f6ba60Sopenharmony_ci    for (int i = 0; i < topCamera.appname().size(); i++) {
21406f6ba60Sopenharmony_ci        cJSON_AddItemToArray(appnameArr, cJSON_CreateString(topCamera.appname(i).c_str()));
21506f6ba60Sopenharmony_ci    }
21606f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "appname", appnameArr);
21706f6ba60Sopenharmony_ci
21806f6ba60Sopenharmony_ci    cJSON* cameryIdArr = cJSON_CreateArray();
21906f6ba60Sopenharmony_ci    for (int i = 0; i < topCamera.camera_id().size(); i++) {
22006f6ba60Sopenharmony_ci        cJSON_AddItemToArray(cameryIdArr, cJSON_CreateNumber(topCamera.camera_id(i)));
22106f6ba60Sopenharmony_ci    }
22206f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "camera_id", cameryIdArr);
22306f6ba60Sopenharmony_ci
22406f6ba60Sopenharmony_ci    cJSON* durationArr = cJSON_CreateArray();
22506f6ba60Sopenharmony_ci    for (int i = 0; i < topCamera.background_duration().size(); i++) {
22606f6ba60Sopenharmony_ci        cJSON_AddItemToArray(durationArr, cJSON_CreateNumber(topCamera.background_duration(i)));
22706f6ba60Sopenharmony_ci    }
22806f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "background_duration", durationArr);
22906f6ba60Sopenharmony_ci
23006f6ba60Sopenharmony_ci    cJSON* energyArr = cJSON_CreateArray();
23106f6ba60Sopenharmony_ci    for (int i = 0; i < topCamera.background_energy().size(); i++) {
23206f6ba60Sopenharmony_ci        cJSON_AddItemToArray(energyArr, cJSON_CreateNumber(topCamera.background_energy(i)));
23306f6ba60Sopenharmony_ci    }
23406f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "background_energy", energyArr);
23506f6ba60Sopenharmony_ci
23606f6ba60Sopenharmony_ci    cJSON* foreDurationArr = cJSON_CreateArray();
23706f6ba60Sopenharmony_ci    for (int i = 0; i < topCamera.foreground_duration().size(); i++) {
23806f6ba60Sopenharmony_ci        cJSON_AddItemToArray(foreDurationArr, cJSON_CreateNumber(topCamera.foreground_duration(i)));
23906f6ba60Sopenharmony_ci    }
24006f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "foreground_duration", foreDurationArr);
24106f6ba60Sopenharmony_ci
24206f6ba60Sopenharmony_ci    cJSON* foreEnergyArr = cJSON_CreateArray();
24306f6ba60Sopenharmony_ci    for (int i = 0; i < topCamera.foreground_energy().size(); i++) {
24406f6ba60Sopenharmony_ci        cJSON_AddItemToArray(foreEnergyArr, cJSON_CreateNumber(topCamera.foreground_energy(i)));
24506f6ba60Sopenharmony_ci    }
24606f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "foreground_energy", foreEnergyArr);
24706f6ba60Sopenharmony_ci
24806f6ba60Sopenharmony_ci    cJSON* screenDuraArr = cJSON_CreateArray();
24906f6ba60Sopenharmony_ci    for (int i = 0; i < topCamera.screen_off_duration().size(); i++) {
25006f6ba60Sopenharmony_ci        cJSON_AddItemToArray(screenDuraArr, cJSON_CreateNumber(topCamera.screen_off_duration(i)));
25106f6ba60Sopenharmony_ci    }
25206f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "screen_off_duration", screenDuraArr);
25306f6ba60Sopenharmony_ci
25406f6ba60Sopenharmony_ci    cJSON* screenEnergyArr = cJSON_CreateArray();
25506f6ba60Sopenharmony_ci    for (int i = 0; i < topCamera.screen_off_energy().size(); i++) {
25606f6ba60Sopenharmony_ci        cJSON_AddItemToArray(screenEnergyArr, cJSON_CreateNumber(topCamera.screen_off_energy(i)));
25706f6ba60Sopenharmony_ci    }
25806f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "screen_off_energy", screenEnergyArr);
25906f6ba60Sopenharmony_ci
26006f6ba60Sopenharmony_ci    cJSON* screenOnDuraArr = cJSON_CreateArray();
26106f6ba60Sopenharmony_ci    for (int i = 0; i < topCamera.screen_on_duration().size(); i++) {
26206f6ba60Sopenharmony_ci        cJSON_AddItemToArray(screenOnDuraArr, cJSON_CreateNumber(topCamera.screen_on_duration(i)));
26306f6ba60Sopenharmony_ci    }
26406f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "screen_on_duration", screenOnDuraArr);
26506f6ba60Sopenharmony_ci
26606f6ba60Sopenharmony_ci    cJSON* screenOnEnergyArr = cJSON_CreateArray();
26706f6ba60Sopenharmony_ci    for (int i = 0; i < topCamera.screen_on_energy().size(); i++) {
26806f6ba60Sopenharmony_ci        cJSON_AddItemToArray(screenOnEnergyArr, cJSON_CreateNumber(topCamera.screen_on_energy(i)));
26906f6ba60Sopenharmony_ci    }
27006f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "screen_on_energy", screenOnEnergyArr);
27106f6ba60Sopenharmony_ci    return cjsonTopCommon;
27206f6ba60Sopenharmony_ci}
27306f6ba60Sopenharmony_ci
27406f6ba60Sopenharmony_cicJSON* XpowerDecoder::CreateComponentTopCpu(const ComponentTopCpu& topCpu)
27506f6ba60Sopenharmony_ci{
27606f6ba60Sopenharmony_ci    cJSON* cjsonTopCommon = cJSON_CreateObject();
27706f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(cjsonTopCommon, "count", topCpu.count());
27806f6ba60Sopenharmony_ci    cJSON* appnameArr = cJSON_CreateArray();
27906f6ba60Sopenharmony_ci    for (int i = 0; i < topCpu.appname().size(); i++) {
28006f6ba60Sopenharmony_ci        cJSON_AddItemToArray(appnameArr, cJSON_CreateString(topCpu.appname(i).c_str()));
28106f6ba60Sopenharmony_ci    }
28206f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "appname", appnameArr);
28306f6ba60Sopenharmony_ci
28406f6ba60Sopenharmony_ci    cJSON* userIdArr = cJSON_CreateArray();
28506f6ba60Sopenharmony_ci    for (int i = 0; i < topCpu.uid().size(); i++) {
28606f6ba60Sopenharmony_ci        cJSON_AddItemToArray(userIdArr, cJSON_CreateNumber(topCpu.uid(i)));
28706f6ba60Sopenharmony_ci    }
28806f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "uid", userIdArr);
28906f6ba60Sopenharmony_ci
29006f6ba60Sopenharmony_ci    cJSON* durationArr = cJSON_CreateArray();
29106f6ba60Sopenharmony_ci    for (int i = 0; i < topCpu.background_duration().size(); i++) {
29206f6ba60Sopenharmony_ci        cJSON_AddItemToArray(durationArr, cJSON_CreateNumber(topCpu.background_duration(i)));
29306f6ba60Sopenharmony_ci    }
29406f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "background_duration", durationArr);
29506f6ba60Sopenharmony_ci
29606f6ba60Sopenharmony_ci    cJSON* energyArr = cJSON_CreateArray();
29706f6ba60Sopenharmony_ci    for (int i = 0; i < topCpu.background_energy().size(); i++) {
29806f6ba60Sopenharmony_ci        cJSON_AddItemToArray(energyArr, cJSON_CreateNumber(topCpu.background_energy(i)));
29906f6ba60Sopenharmony_ci    }
30006f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "background_energy", energyArr);
30106f6ba60Sopenharmony_ci
30206f6ba60Sopenharmony_ci    cJSON* foreDurationArr = cJSON_CreateArray();
30306f6ba60Sopenharmony_ci    for (int i = 0; i < topCpu.foreground_duration().size(); i++) {
30406f6ba60Sopenharmony_ci        cJSON_AddItemToArray(foreDurationArr, cJSON_CreateNumber(topCpu.foreground_duration(i)));
30506f6ba60Sopenharmony_ci    }
30606f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "foreground_duration", foreDurationArr);
30706f6ba60Sopenharmony_ci
30806f6ba60Sopenharmony_ci    cJSON* foreEnergyArr = cJSON_CreateArray();
30906f6ba60Sopenharmony_ci    for (int i = 0; i < topCpu.foreground_energy().size(); i++) {
31006f6ba60Sopenharmony_ci        cJSON_AddItemToArray(foreEnergyArr, cJSON_CreateNumber(topCpu.foreground_energy(i)));
31106f6ba60Sopenharmony_ci    }
31206f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "foreground_energy", foreEnergyArr);
31306f6ba60Sopenharmony_ci
31406f6ba60Sopenharmony_ci    cJSON* screenDuraArr = cJSON_CreateArray();
31506f6ba60Sopenharmony_ci    for (int i = 0; i < topCpu.screen_off_duration().size(); i++) {
31606f6ba60Sopenharmony_ci        cJSON_AddItemToArray(screenDuraArr, cJSON_CreateNumber(topCpu.screen_off_duration(i)));
31706f6ba60Sopenharmony_ci    }
31806f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "screen_off_duration", screenDuraArr);
31906f6ba60Sopenharmony_ci
32006f6ba60Sopenharmony_ci    cJSON* screenEnergyArr = cJSON_CreateArray();
32106f6ba60Sopenharmony_ci    for (int i = 0; i < topCpu.screen_off_energy().size(); i++) {
32206f6ba60Sopenharmony_ci        cJSON_AddItemToArray(screenEnergyArr, cJSON_CreateNumber(topCpu.screen_off_energy(i)));
32306f6ba60Sopenharmony_ci    }
32406f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "screen_off_energy", screenEnergyArr);
32506f6ba60Sopenharmony_ci
32606f6ba60Sopenharmony_ci    cJSON* screenOnDuraArr = cJSON_CreateArray();
32706f6ba60Sopenharmony_ci    for (int i = 0; i < topCpu.screen_on_duration().size(); i++) {
32806f6ba60Sopenharmony_ci        cJSON_AddItemToArray(screenOnDuraArr, cJSON_CreateNumber(topCpu.screen_on_duration(i)));
32906f6ba60Sopenharmony_ci    }
33006f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "screen_on_duration", screenOnDuraArr);
33106f6ba60Sopenharmony_ci
33206f6ba60Sopenharmony_ci    cJSON* screenOnEnergyArr = cJSON_CreateArray();
33306f6ba60Sopenharmony_ci    for (int i = 0; i < topCpu.screen_on_energy().size(); i++) {
33406f6ba60Sopenharmony_ci        cJSON_AddItemToArray(screenOnEnergyArr, cJSON_CreateNumber(topCpu.screen_on_energy(i)));
33506f6ba60Sopenharmony_ci    }
33606f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "screen_on_energy", screenOnEnergyArr);
33706f6ba60Sopenharmony_ci
33806f6ba60Sopenharmony_ci    cJSON* loadArr = cJSON_CreateArray();
33906f6ba60Sopenharmony_ci    for (int i = 0; i < topCpu.load().size(); i++) {
34006f6ba60Sopenharmony_ci        cJSON_AddItemToArray(loadArr, cJSON_CreateNumber(topCpu.load(i)));
34106f6ba60Sopenharmony_ci    }
34206f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "load", loadArr);
34306f6ba60Sopenharmony_ci
34406f6ba60Sopenharmony_ci    return cjsonTopCommon;
34506f6ba60Sopenharmony_ci}
34606f6ba60Sopenharmony_ci
34706f6ba60Sopenharmony_cicJSON* XpowerDecoder::CreateComponentTopDisplay(const ComponentTopDisplay& topDisplay)
34806f6ba60Sopenharmony_ci{
34906f6ba60Sopenharmony_ci    cJSON* cjsonTopCommon = cJSON_CreateObject();
35006f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(cjsonTopCommon, "count", topDisplay.count());
35106f6ba60Sopenharmony_ci    cJSON* appnameArr = cJSON_CreateArray();
35206f6ba60Sopenharmony_ci    for (int i = 0; i < topDisplay.appname().size(); i++) {
35306f6ba60Sopenharmony_ci        cJSON_AddItemToArray(appnameArr, cJSON_CreateString(topDisplay.appname(i).c_str()));
35406f6ba60Sopenharmony_ci    }
35506f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "appname", appnameArr);
35606f6ba60Sopenharmony_ci
35706f6ba60Sopenharmony_ci    cJSON* timeArr = cJSON_CreateArray();
35806f6ba60Sopenharmony_ci    for (int i = 0; i < topDisplay.time().size(); i++) {
35906f6ba60Sopenharmony_ci        cJSON_AddItemToArray(timeArr, cJSON_CreateNumber(topDisplay.time(i)));
36006f6ba60Sopenharmony_ci    }
36106f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "time", timeArr);
36206f6ba60Sopenharmony_ci
36306f6ba60Sopenharmony_ci    cJSON* energyArr = cJSON_CreateArray();
36406f6ba60Sopenharmony_ci    for (int i = 0; i < topDisplay.energy().size(); i++) {
36506f6ba60Sopenharmony_ci        cJSON_AddItemToArray(energyArr, cJSON_CreateNumber(topDisplay.energy(i)));
36606f6ba60Sopenharmony_ci    }
36706f6ba60Sopenharmony_ci    cJSON_AddItemToObject(cjsonTopCommon, "energy", energyArr);
36806f6ba60Sopenharmony_ci    return cjsonTopCommon;
36906f6ba60Sopenharmony_ci}
37006f6ba60Sopenharmony_ci
37106f6ba60Sopenharmony_cicJSON* XpowerDecoder::CreateComponentTop(const ComponentTop& componentTop)
37206f6ba60Sopenharmony_ci{
37306f6ba60Sopenharmony_ci    cJSON* jsonComTop = cJSON_CreateObject();
37406f6ba60Sopenharmony_ci    cJSON_AddItemToObject(jsonComTop, "audio", CreateComponentTopCommon(componentTop.audio()));
37506f6ba60Sopenharmony_ci    cJSON_AddItemToObject(jsonComTop, "bluetooth", CreateComponentTopCommon(componentTop.bluetooth()));
37606f6ba60Sopenharmony_ci    cJSON_AddItemToObject(jsonComTop, "camera", CreateComponentTopCamera(componentTop.camera()));
37706f6ba60Sopenharmony_ci    cJSON_AddItemToObject(jsonComTop, "cpu", CreateComponentTopCpu(componentTop.cpu()));
37806f6ba60Sopenharmony_ci    cJSON_AddItemToObject(jsonComTop, "display", CreateComponentTopDisplay(componentTop.display()));
37906f6ba60Sopenharmony_ci    cJSON_AddItemToObject(jsonComTop, "flashlight", CreateComponentTopCommon(componentTop.flashlight()));
38006f6ba60Sopenharmony_ci    cJSON_AddItemToObject(jsonComTop, "gpu", CreateComponentTopDisplay(componentTop.gpu()));
38106f6ba60Sopenharmony_ci    cJSON_AddItemToObject(jsonComTop, "location", CreateComponentTopCommon(componentTop.location()));
38206f6ba60Sopenharmony_ci    cJSON_AddItemToObject(jsonComTop, "wifiscan", CreateComponentTopCommon(componentTop.wifiscan()));
38306f6ba60Sopenharmony_ci    return jsonComTop;
38406f6ba60Sopenharmony_ci}
38506f6ba60Sopenharmony_ci
38606f6ba60Sopenharmony_cicJSON* XpowerDecoder::CreateAbnormalEventInfo(const AbnormalEventInfo& eventInfo)
38706f6ba60Sopenharmony_ci{
38806f6ba60Sopenharmony_ci    cJSON* jsonEventInfo = cJSON_CreateObject();
38906f6ba60Sopenharmony_ci    cJSON_AddStringToObject(jsonEventInfo, "abnormal_type",
39006f6ba60Sopenharmony_ci                            eventInfo.AbnormalType_Name(eventInfo.abnormal_type()).c_str());
39106f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(jsonEventInfo, "usage_time", eventInfo.usage_time());
39206f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(jsonEventInfo, "usage_energy", eventInfo.usage_energy());
39306f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(jsonEventInfo, "usage_load", eventInfo.usage_load());
39406f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(jsonEventInfo, "usage_freq", eventInfo.usage_freq());
39506f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(jsonEventInfo, "usage_count", eventInfo.usage_count());
39606f6ba60Sopenharmony_ci    return jsonEventInfo;
39706f6ba60Sopenharmony_ci}
39806f6ba60Sopenharmony_ci
39906f6ba60Sopenharmony_cicJSON* XpowerDecoder::CreateAbnormalEvents(const AbnormalEvents& abnormEvent)
40006f6ba60Sopenharmony_ci{
40106f6ba60Sopenharmony_ci    cJSON* jsonEvent = cJSON_CreateObject();
40206f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(jsonEvent, "anomaly_start_time", abnormEvent.anomaly_start_time());
40306f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(jsonEvent, "anomaly_end_time", abnormEvent.anomaly_end_time());
40406f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(jsonEvent, "count", abnormEvent.count());
40506f6ba60Sopenharmony_ci    cJSON* eventsArr = cJSON_CreateArray();
40606f6ba60Sopenharmony_ci    for (int i = 0; i < abnormEvent.events().size(); i++) {
40706f6ba60Sopenharmony_ci        cJSON_AddItemToArray(eventsArr, CreateAbnormalEventInfo(abnormEvent.events(i)));
40806f6ba60Sopenharmony_ci    }
40906f6ba60Sopenharmony_ci    return jsonEvent;
41006f6ba60Sopenharmony_ci}
41106f6ba60Sopenharmony_ci
41206f6ba60Sopenharmony_cistd::string XpowerDecoder::DecodeXpowerMessage(OptimizeReport& dataProto)
41306f6ba60Sopenharmony_ci{
41406f6ba60Sopenharmony_ci    std::string retStr;
41506f6ba60Sopenharmony_ci    cJSON* optReport = cJSON_CreateObject();
41606f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(optReport, "start_time", dataProto.start_time());
41706f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(optReport, "end_time", dataProto.end_time());
41806f6ba60Sopenharmony_ci    cJSON_AddStringToObject(optReport, "bundle_name", dataProto.bundle_name().c_str());
41906f6ba60Sopenharmony_ci    cJSON_AddNumberToObject(optReport, "message_type", dataProto.message_type());
42006f6ba60Sopenharmony_ci    uint32_t mesType = dataProto.message_type();
42106f6ba60Sopenharmony_ci    if ((mesType & OptimizeMessageType::MESSAGE_REAL_BATTERY) != 0) {
42206f6ba60Sopenharmony_ci        cJSON_AddItemToObject(optReport, "real_battery", CreateRealBattery(dataProto.real_battery()));
42306f6ba60Sopenharmony_ci    }
42406f6ba60Sopenharmony_ci    if ((mesType & OptimizeMessageType::MESSAGE_APP_STATISTIC) != 0) {
42506f6ba60Sopenharmony_ci        cJSON_AddItemToObject(optReport, "app_statistic", CreateAppStatistic(dataProto.app_statistic()));
42606f6ba60Sopenharmony_ci    }
42706f6ba60Sopenharmony_ci    if ((mesType & OptimizeMessageType::MESSAGE_APP_DETAIL) != 0) {
42806f6ba60Sopenharmony_ci        cJSON_AddItemToObject(optReport, "app_detail", CreateAppDetail(dataProto.app_detail()));
42906f6ba60Sopenharmony_ci    }
43006f6ba60Sopenharmony_ci    if ((mesType & OptimizeMessageType::MESSAGE_COMPONENT_TOP) != 0) {
43106f6ba60Sopenharmony_ci        cJSON_AddItemToObject(optReport, "component_top", CreateComponentTop(dataProto.component_top()));
43206f6ba60Sopenharmony_ci    }
43306f6ba60Sopenharmony_ci    if ((mesType & OptimizeMessageType::MESSAGE_ABNORMAL_EVENTS) != 0) {
43406f6ba60Sopenharmony_ci        cJSON_AddItemToObject(optReport, "abnormal_events", CreateAbnormalEvents(dataProto.abnormal_events()));
43506f6ba60Sopenharmony_ci    }
43606f6ba60Sopenharmony_ci    retStr = cJSON_Print(optReport);
43706f6ba60Sopenharmony_ci    cJSON_Delete(optReport);
43806f6ba60Sopenharmony_ci    return retStr;
43906f6ba60Sopenharmony_ci}
440