• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "hdc_connect.h"
17 #include "hdc_jdwp.h"
18 #include "parameter.h"
19 
20 namespace Hdc {
21 
22 std::unique_ptr<ConnectManagement> g_connectManagement = nullptr;
23 static std::atomic<bool> g_jdwpSimulatorFree = false;
24 static HdcJdwpSimulator *g_clsHdcJdwpSimulator = nullptr;
25 
SetProcessName(const std::string & processName)26 void ConnectManagement::SetProcessName(const std::string &processName)
27 {
28     processName_ = processName;
29 }
30 
GetProcessName() const31 std::string ConnectManagement::GetProcessName() const
32 {
33     return processName_;
34 }
35 
SetPkgName(const std::string & pkgName)36 void ConnectManagement::SetPkgName(const std::string &pkgName)
37 {
38     pkgName_ = pkgName;
39 }
40 
GetPkgName() const41 std::string ConnectManagement::GetPkgName() const
42 {
43     return pkgName_;
44 }
45 
SetDebug(bool isDebug)46 void ConnectManagement::SetDebug(bool isDebug)
47 {
48     isDebug_ = isDebug;
49 }
50 
GetDebug() const51 bool ConnectManagement::GetDebug() const
52 {
53     return isDebug_;
54 }
55 
SetCallback(Callback cb)56 void ConnectManagement::SetCallback(Callback cb)
57 {
58     cb_ = cb;
59 }
60 
GetCallback() const61 Callback ConnectManagement::GetCallback() const
62 {
63     return cb_;
64 }
65 
GetDevItem(const char * key,std::string & out,const char * preDefine=nullptr)66 static void GetDevItem(const char *key, std::string &out, const char *preDefine = nullptr)
67 {
68     constexpr int len = 512;
69     char buf[len] = "";
70     if (memset_s(buf, len, 0, len) != EOK) {
71         HILOG_WARN(LOG_CORE, "memset_s failed");
72         return;
73     }
74     auto res = GetParameter(key, preDefine, buf, len);
75     if (res <= 0) {
76         return;
77     }
78     out = buf;
79 }
80 
IsDeveloperMode()81 static bool IsDeveloperMode()
82 {
83     std::string developerMode;
84     GetDevItem("const.security.developermode.state", developerMode);
85     return developerMode == "true";
86 }
87 
FreeInstance()88 void FreeInstance()
89 {
90     if (g_jdwpSimulatorFree) {
91         return;
92     }
93     if (g_clsHdcJdwpSimulator == nullptr) {
94         return;
95     }
96     g_jdwpSimulatorFree = true;
97     g_clsHdcJdwpSimulator->Disconnect();
98     delete g_clsHdcJdwpSimulator;
99     g_clsHdcJdwpSimulator = nullptr;
100     g_jdwpSimulatorFree = false;
101 }
102 
Stop(int signo)103 void Stop(int signo)
104 {
105     FreeInstance();
106     _exit(0);
107 }
108 
StopConnect()109 void StopConnect()
110 {
111     if (!IsDeveloperMode()) {
112         HILOG_INFO(LOG_CORE, "non developer mode not to stop connect");
113         return;
114     }
115 #ifdef JS_JDWP_CONNECT
116     FreeInstance();
117 #endif // JS_JDWP_CONNECT
118 }
119 
HdcConnectRun(void * pkgContent)120 void* HdcConnectRun(void* pkgContent)
121 {
122     if (signal(SIGINT, Stop) == SIG_ERR) {
123         HILOG_FATAL(LOG_CORE, "jdwp_process signal fail.");
124     }
125     int ret = pthread_setname_np(pthread_self(), "OS_hdcRegister");
126     if (ret != 0) {
127         HILOG_FATAL(LOG_CORE, "set Thread name failed.");
128     }
129     std::string processName = static_cast<ConnectManagement*>(pkgContent)->GetProcessName();
130     std::string pkgName = static_cast<ConnectManagement*>(pkgContent)->GetPkgName();
131     bool isDebug = static_cast<ConnectManagement*>(pkgContent)->GetDebug();
132     Callback cb = static_cast<ConnectManagement*>(pkgContent)->GetCallback();
133     g_clsHdcJdwpSimulator = new (std::nothrow) HdcJdwpSimulator(processName, pkgName, isDebug, cb);
134     if (!g_clsHdcJdwpSimulator->Connect()) {
135         HILOG_FATAL(LOG_CORE, "Connect fail.");
136         return nullptr;
137     }
138     return nullptr;
139 }
140 
StartConnect(const std::string & processName,const std::string & pkgName,bool isDebug,Callback cb)141 void StartConnect(const std::string& processName, const std::string& pkgName, bool isDebug, Callback cb)
142 {
143     if (!IsDeveloperMode()) {
144         HILOG_INFO(LOG_CORE, "non developer mode not to start connect");
145         return;
146     }
147     if (g_clsHdcJdwpSimulator != nullptr) {
148         return;
149     }
150     pthread_t tid;
151     g_connectManagement = std::make_unique<ConnectManagement>();
152     g_connectManagement->SetProcessName(processName);
153     g_connectManagement->SetPkgName(pkgName);
154     g_connectManagement->SetDebug(isDebug);
155     g_connectManagement->SetCallback(cb);
156     if (pthread_create(&tid, nullptr, &HdcConnectRun, static_cast<void*>(g_connectManagement.get())) != 0) {
157         HILOG_FATAL(LOG_CORE, "pthread_create fail!");
158         return;
159     }
160 }
161 } // namespace Hdc
162