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