• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 START_PARAM_INVALID_CODE = 11;
39 static const int NOTIFY_INTERVAL_TIME = 1000; // Unit millisecond
40 
InitSettings()41 static void InitSettings()
42 {
43     CommandParser& parser = CommandParser::GetInstance();
44     // Setting the Simulator Model
45     ModelManager::SetCurrentDevice(parser.GetDeviceType());
46 
47     // Setting the screen resolution
48     VirtualScreenImpl::GetInstance().SetOrignalWidth(parser.GetOrignalResolutionWidth());
49     VirtualScreenImpl::GetInstance().SetOrignalHeight(parser.GetOrignalResolutionHeight());
50     VirtualScreenImpl::GetInstance().SetCompressionWidth(parser.GetCompressionResolutionWidth());
51     VirtualScreenImpl::GetInstance().SetCompressionHeight(parser.GetCompressionResolutionHeight());
52     VirtualScreenImpl::GetInstance().SetCurrentResolution(parser.GetOrignalResolutionWidth(),
53         parser.GetOrignalResolutionHeight());
54     VirtualScreenImpl::GetInstance().InitFrameCountTimer();
55 }
56 
InitJsApp()57 static void InitJsApp()
58 {
59     CommandParser& parser = CommandParser::GetInstance();
60     string args = parser.GetConfigPath();
61     if (args.empty()) {
62         ELOG("No persistent properties path found.");
63     }
64 
65     CommandLineInterface::GetInstance().ReadAndApplyConfig(args);
66     // Initialize Image Pipeline Name
67     if (parser.IsSet("s")) {
68         JsAppImpl::GetInstance().SetPipeName(parser.Value("s"));
69     }
70 
71     if (parser.IsSet("lws")) {
72         JsAppImpl::GetInstance().SetPipePort(parser.Value("lws"));
73     }
74 
75     // Set the application name.
76     JsAppImpl::GetInstance().SetBundleName(parser.GetAppName());
77 
78     // Processing JSheap
79     JsAppImpl::GetInstance().SetJSHeapSize(parser.GetJsHeapSize());
80 
81     // Start JSApp
82     if (!parser.IsSet("t")) {
83         if (parser.IsSet("d")) {
84             JsAppImpl::GetInstance().SetIsDebug(true);
85             if (parser.IsSet("p")) {
86                 JsAppImpl::GetInstance().SetDebugServerPort(static_cast<uint16_t>(atoi(parser.Value("p").c_str())));
87             }
88         }
89         JsAppImpl::GetInstance().SetJsAppPath(parser.Value("j"));
90         if (parser.IsSet("url")) {
91             JsAppImpl::GetInstance().SetUrlPath(parser.Value("url"));
92         }
93         JsAppImpl::GetInstance().Start();
94     }
95 }
96 
DataChangeCheck()97 static void DataChangeCheck()
98 {
99     SharedDataManager::CheckTick();
100 }
101 
InitSharedData()102 static void InitSharedData()
103 {
104     // The brightness ranges from 1 to 255. The default value is 255.
105     SharedData<uint8_t>(SharedDataType::BRIGHTNESS_VALUE, 255, 1, 255);
106     SharedData<uint8_t>(SharedDataType::BRIGHTNESS_MODE, (uint8_t)BrightnessMode::MANUAL,
107                         (uint8_t)BrightnessMode::MANUAL, (uint8_t)BrightnessMode::AUTO);
108     SharedData<bool>(SharedDataType::KEEP_SCREEN_ON, true);
109     SharedData<uint8_t>(SharedDataType::BATTERY_STATUS, (uint8_t)ChargeState::NOCHARGE,
110                         (uint8_t)ChargeState::NOCHARGE, (uint8_t)ChargeState::CHARGING);
111     // Battery level range: 0.0–1.0; default: 1.0
112     SharedData<double>(SharedDataType::BATTERY_LEVEL, 1.0, 0.0, 1.0);
113     // Heart rate range: 0 to 255. The default value is 80.
114     SharedData<uint8_t>(SharedDataType::HEARTBEAT_VALUE, 80, 0, 255);
115     // The value ranges from 0 to 999999. The default value is 0.
116     SharedData<uint32_t>(SharedDataType::SUMSTEP_VALUE, 0, 0, 999999);
117     // The volume ranges from 0.0 to 1.0. The default value is 1.0.
118     SharedData<double>(SharedDataType::VOLUME_VALUE, 1.0, 0.0, 1.0);
119     // The atmospheric pressure ranges from 0 to 999900. The default value is 101325.
120     SharedData<uint32_t>(SharedDataType::PRESSURE_VALUE, 101325, 0, 999900);
121     SharedData<bool>(SharedDataType::WEARING_STATE, true);
122     SharedData<string>(SharedDataType::LANGUAGE, "zh-CN");
123     // The value ranges from 180 to 180. The default value is 0.
124     SharedData<double>(SharedDataType::LONGITUDE, 0, -180, 180);
125     // The value ranges from -90 to 90. The default value is 0.
126     SharedData<double>(SharedDataType::LATITUDE, 0, -90, 90);
127 }
128 
SendJsHeapData()129 static void SendJsHeapData()
130 {
131     OHOS::ACELite::JSHeapStatus status;
132     OHOS::ACELite::JSI::GetJSHeapStatus(status);
133     CommandLineInterface::GetInstance().SendJSHeapMemory(status.totalBytes, status.allocBytes, status.peakAllocBytes);
134 }
135 
ParseArgs(CommandParser & parser,int argc,char * argv[])136 int ParseArgs(CommandParser& parser, int argc, char* argv[])
137 {
138     int defaultReturnVal = -1;
139     vector<string> strs;
140     for (int i = 1; i < argc; ++i) {
141         if (parser.IsMainArgLengthInvalid(argv[i])) {
142             return START_PARAM_INVALID_CODE;
143         }
144         strs.push_back(argv[i]);
145     }
146     if (!parser.ProcessCommand(strs)) {
147         return 0;
148     }
149     if (!parser.IsCommandValid()) {
150         FLOG("Start args is invalid.");
151         return START_PARAM_INVALID_CODE;
152     }
153     return defaultReturnVal;
154 }
155 
main(int argc,char * argv[])156 int main(int argc, char* argv[])
157 {
158     ILOG("ThinPreviewer enter the main function.");
159     // thin device global exception handler
160     auto thinCrashHandler = std::make_unique<CrashHandler>();
161     if (thinCrashHandler == nullptr) {
162         ELOG("ThinPreviewer crashHandler new fail.");
163         return 0;
164     }
165     thinCrashHandler->InitExceptionHandler();
166     // Creating a Main Thread Timer Manager
167     CppTimerManager& manager = CppTimerManager::GetTimerManager();
168     // Parsing User Commands
169     CommandParser& parser = CommandParser::GetInstance();
170     int ret = ParseArgs(parser, argc, argv);
171     if (ret >= 0) {
172         return ret;
173     }
174     InitSharedData();
175     InitSettings();
176     if (parser.IsSet("s")) {
177         CommandLineInterface::GetInstance().Init(parser.Value("s"));
178     }
179     InitJsApp();
180     TraceTool::GetInstance().HandleTrace("Enter the main function");
181     CppTimer dataCheckTimer(DataChangeCheck);
182     manager.AddCppTimer(dataCheckTimer);
183     dataCheckTimer.Start(100); // 100ms Timer polling period
184     CppTimer jsHeapSendTimer(SendJsHeapData);
185     if (parser.IsSendJSHeap()) {
186         manager.AddCppTimer(jsHeapSendTimer);
187         jsHeapSendTimer.Start(NOTIFY_INTERVAL_TIME); // 1000ms Timer polling period
188     }
189     // Registering and monitoring the changes of the brightness and volume
190     thread::id curThreadId = this_thread::get_id();
191     SharedData<uint8_t>::AppendNotify(SharedDataType::BRIGHTNESS_VALUE,
192                                       TimerTaskHandler::CheckBrightnessValueChanged, curThreadId);
193     while (!Interrupter::IsInterrupt()) {
194         CommandLineInterface::GetInstance().ProcessCommand();
195         manager.RunTimerTick();
196         this_thread::sleep_for(chrono::milliseconds(1));
197     }
198     JsAppImpl::GetInstance().Stop();
199     this_thread::sleep_for(chrono::milliseconds(500)); // sleep 500 ms
200     return 0;
201 }
202