1 /*
2 * Copyright (c) 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 <VirtualScreen.h>
17 #include <cstdint>
18 #include <cstdlib>
19 #include <fstream>
20 #include <vector>
21
22 #include "CommandLineFactory.h"
23 #include "CommandLineInterface.h"
24 #include "CommandParser.h"
25 #include "CppTimerManager.h"
26 #include "CrashHandler.h"
27 #include "Interrupter.h"
28 #include "JsAppImpl.h"
29 #include "ModelManager.h"
30 #include "PreviewerEngineLog.h"
31 #include "SharedData.h"
32 #include "TimerTaskHandler.h"
33 #include "TraceTool.h"
34 #include "VirtualScreenImpl.h"
35 #include "jsi.h"
36
37 using namespace std;
38 static const int NOTIFY_INTERVAL_TIME = 1000; // Unit millisecond
39
InitSettings()40 static void InitSettings()
41 {
42 // Setting the Simulator Model
43 ModelManager::SetCurrentDevice(CommandParser::GetInstance().GetDeviceType());
44 VirtualScreenImpl::GetInstance().InitResolution();
45 VirtualScreenImpl::GetInstance().InitFrameCountTimer();
46 }
47
ApplyConfig()48 static void ApplyConfig()
49 {
50 string liteConfigArgs = CommandParser::GetInstance().GetConfigPath();
51 if (liteConfigArgs.empty()) {
52 ELOG("No persistent properties path found.");
53 }
54 CommandLineInterface::GetInstance().ReadAndApplyConfig(liteConfigArgs);
55 }
56
DataChangeCheck()57 static void DataChangeCheck()
58 {
59 SharedDataManager::CheckTick();
60 }
61
InitSharedData()62 static void InitSharedData()
63 {
64 // The brightness ranges from 1 to 255. The default value is 255.
65 SharedData<uint8_t>(SharedDataType::BRIGHTNESS_VALUE, 255, 1, 255);
66 SharedData<uint8_t>(SharedDataType::BRIGHTNESS_MODE, (uint8_t)BrightnessMode::MANUAL,
67 (uint8_t)BrightnessMode::MANUAL, (uint8_t)BrightnessMode::AUTO);
68 SharedData<bool>(SharedDataType::KEEP_SCREEN_ON, true);
69 SharedData<uint8_t>(SharedDataType::BATTERY_STATUS, (uint8_t)ChargeState::NOCHARGE,
70 (uint8_t)ChargeState::NOCHARGE, (uint8_t)ChargeState::CHARGING);
71 // Battery level range: 0.0–1.0; default: 1.0
72 SharedData<double>(SharedDataType::BATTERY_LEVEL, 1.0, 0.0, 1.0);
73 // Heart rate range: 0 to 255. The default value is 80.
74 SharedData<uint8_t>(SharedDataType::HEARTBEAT_VALUE, 80, 0, 255);
75 // The value ranges from 0 to 999999. The default value is 0.
76 SharedData<uint32_t>(SharedDataType::SUMSTEP_VALUE, 0, 0, 999999);
77 // The volume ranges from 0.0 to 1.0. The default value is 1.0.
78 SharedData<double>(SharedDataType::VOLUME_VALUE, 1.0, 0.0, 1.0);
79 // The atmospheric pressure ranges from 0 to 999900. The default value is 101325.
80 SharedData<uint32_t>(SharedDataType::PRESSURE_VALUE, 101325, 0, 999900);
81 SharedData<bool>(SharedDataType::WEARING_STATE, true);
82 SharedData<string>(SharedDataType::LANGUAGE, "zh-CN");
83 // The value ranges from 180 to 180. The default value is 0.
84 SharedData<double>(SharedDataType::LONGITUDE, 0, -180, 180);
85 // The value ranges from -90 to 90. The default value is 0.
86 SharedData<double>(SharedDataType::LATITUDE, 0, -90, 90);
87 }
88
SendJsHeapData()89 static void SendJsHeapData()
90 {
91 OHOS::ACELite::JSHeapStatus status;
92 OHOS::ACELite::JSI::GetJSHeapStatus(status);
93 CommandLineInterface::GetInstance().SendJSHeapMemory(status.totalBytes, status.allocBytes, status.peakAllocBytes);
94 }
95
main(int argc,char * argv[])96 int main(int argc, char* argv[])
97 {
98 ILOG("ThinPreviewer enter the main function.");
99 // thin device global exception handler
100 auto thinCrashHandler = std::make_unique<CrashHandler>();
101 if (thinCrashHandler == nullptr) {
102 ELOG("ThinPreviewer crashHandler new fail.");
103 return 0;
104 }
105 thinCrashHandler->InitExceptionHandler();
106 // Creating a Main Thread Timer Manager
107 CppTimerManager& manager = CppTimerManager::GetTimerManager();
108 // Parsing User Commands
109 CommandParser& parser = CommandParser::GetInstance();
110 int ret = parser.ParseArgs(argc, argv);
111 if (ret >= 0) {
112 return ret;
113 }
114 InitSharedData();
115 InitSettings();
116 if (parser.IsSet("s")) {
117 CommandLineInterface::GetInstance().Init(parser.Value("s"));
118 }
119 ApplyConfig();
120 JsAppImpl::GetInstance().InitJsApp();
121 TraceTool::GetInstance().HandleTrace("Enter the main function");
122 CppTimer dataCheckTimer(DataChangeCheck);
123 manager.AddCppTimer(dataCheckTimer);
124 dataCheckTimer.Start(100); // 100ms Timer polling period
125 CppTimer jsHeapSendTimer(SendJsHeapData);
126 if (parser.IsSendJSHeap()) {
127 manager.AddCppTimer(jsHeapSendTimer);
128 jsHeapSendTimer.Start(NOTIFY_INTERVAL_TIME); // 1000ms Timer polling period
129 }
130 // Registering and monitoring the changes of the brightness and volume
131 thread::id curThreadId = this_thread::get_id();
132 SharedData<uint8_t>::AppendNotify(SharedDataType::BRIGHTNESS_VALUE,
133 TimerTaskHandler::CheckBrightnessValueChanged, curThreadId);
134 while (!Interrupter::IsInterrupt()) {
135 CommandLineInterface::GetInstance().ProcessCommand();
136 manager.RunTimerTick();
137 this_thread::sleep_for(chrono::milliseconds(1));
138 }
139 JsAppImpl::GetInstance().Stop();
140 this_thread::sleep_for(chrono::milliseconds(500)); // sleep 500 ms
141 return 0;
142 }
143