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
19 namespace Hdc {
20
21 std::unique_ptr<ConnectManagement> g_connectManagement = nullptr;
22 static HdcJdwpSimulator *g_clsHdcJdwpSimulator = nullptr;
23
SetProcessName(const std::string & processName)24 void ConnectManagement::SetProcessName(const std::string &processName)
25 {
26 processName_ = processName;
27 }
28
GetProcessName() const29 std::string ConnectManagement::GetProcessName() const
30 {
31 return processName_;
32 }
33
SetPkgName(const std::string & pkgName)34 void ConnectManagement::SetPkgName(const std::string &pkgName)
35 {
36 pkgName_ = pkgName;
37 }
38
GetPkgName() const39 std::string ConnectManagement::GetPkgName() const
40 {
41 return pkgName_;
42 }
43
SetDebug(bool isDebug)44 void ConnectManagement::SetDebug(bool isDebug)
45 {
46 isDebug_ = isDebug;
47 }
48
GetDebug() const49 bool ConnectManagement::GetDebug() const
50 {
51 return isDebug_;
52 }
53
SetCallback(Callback cb)54 void ConnectManagement::SetCallback(Callback cb)
55 {
56 cb_ = cb;
57 }
58
GetCallback() const59 Callback ConnectManagement::GetCallback() const
60 {
61 return cb_;
62 }
63
FreeInstance()64 void FreeInstance()
65 {
66 if (g_clsHdcJdwpSimulator == nullptr) {
67 return;
68 }
69 g_clsHdcJdwpSimulator->Disconnect();
70 delete g_clsHdcJdwpSimulator;
71 g_clsHdcJdwpSimulator = nullptr;
72 }
73
Stop(int signo)74 void Stop(int signo)
75 {
76 FreeInstance();
77 _exit(0);
78 }
79
StopConnect()80 void StopConnect()
81 {
82 #ifdef JS_JDWP_CONNECT
83 FreeInstance();
84 #endif // JS_JDWP_CONNECT
85 }
86
HdcConnectRun(void * pkgContent)87 void* HdcConnectRun(void* pkgContent)
88 {
89 if (signal(SIGINT, Stop) == SIG_ERR) {
90 OHOS::HiviewDFX::HiLog::Fatal(LOG_LABEL, "jdwp_process signal fail.");
91 }
92 int ret = pthread_setname_np(pthread_self(), "OS_hdcRegitser");
93 if (ret != 0) {
94 OHOS::HiviewDFX::HiLog::Fatal(LOG_LABEL, "set Thread name failed.");
95 }
96 std::string processName = static_cast<ConnectManagement*>(pkgContent)->GetProcessName();
97 std::string pkgName = static_cast<ConnectManagement*>(pkgContent)->GetPkgName();
98 bool isDebug = static_cast<ConnectManagement*>(pkgContent)->GetDebug();
99 Callback cb = static_cast<ConnectManagement*>(pkgContent)->GetCallback();
100 g_clsHdcJdwpSimulator = new (std::nothrow) HdcJdwpSimulator(processName, pkgName, isDebug, cb);
101 if (!g_clsHdcJdwpSimulator->Connect()) {
102 OHOS::HiviewDFX::HiLog::Fatal(LOG_LABEL, "Connect fail.");
103 return nullptr;
104 }
105 return nullptr;
106 }
107
StartConnect(const std::string & processName,const std::string & pkgName,bool isDebug,Callback cb)108 void StartConnect(const std::string& processName, const std::string& pkgName, bool isDebug, Callback cb)
109 {
110 if (g_clsHdcJdwpSimulator != nullptr) {
111 return;
112 }
113 pthread_t tid;
114 g_connectManagement = std::make_unique<ConnectManagement>();
115 g_connectManagement->SetProcessName(processName);
116 g_connectManagement->SetPkgName(pkgName);
117 g_connectManagement->SetDebug(isDebug);
118 g_connectManagement->SetCallback(cb);
119 if (pthread_create(&tid, nullptr, &HdcConnectRun, static_cast<void*>(g_connectManagement.get())) != 0) {
120 OHOS::HiviewDFX::HiLog::Fatal(LOG_LABEL, "pthread_create fail!");
121 return;
122 }
123 }
124 } // namespace Hdc
125