1 /*
2 * Copyright (c) 2022 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 "test_observer_proxy.h"
17 #include "hilog_wrapper.h"
18
19 namespace OHOS {
20 namespace AAFwk {
TestObserverProxy(const sptr<IRemoteObject> & object)21 TestObserverProxy::TestObserverProxy(const sptr<IRemoteObject>& object) : IRemoteProxy<ITestObserver>(object)
22 {
23 HILOG_INFO("test observer proxy instance is created");
24 }
25
~TestObserverProxy()26 TestObserverProxy::~TestObserverProxy()
27 {
28 HILOG_INFO("test observer proxy is destroyed");
29 }
30
TestStatus(const std::string & msg,const int64_t & resultCode)31 void TestObserverProxy::TestStatus(const std::string& msg, const int64_t& resultCode)
32 {
33 HILOG_INFO("start");
34
35 MessageParcel data;
36 MessageParcel reply;
37 MessageOption option(MessageOption::TF_ASYNC);
38
39 if (!data.WriteInterfaceToken(GetDescriptor())) {
40 return;
41 }
42
43 sptr<IRemoteObject> remote = Remote();
44 if (remote == nullptr) {
45 HILOG_ERROR("Failed to send cmd to service due to remote object is null");
46 return;
47 }
48
49 if (!data.WriteString(msg)) {
50 HILOG_ERROR("Failed to write string msg");
51 return;
52 }
53
54 if (!data.WriteInt64(resultCode)) {
55 HILOG_ERROR("Failed to write resultCode");
56 return;
57 }
58
59 int32_t result = remote->SendRequest(
60 static_cast<uint32_t>(ITestObserver::Message::AA_TEST_STATUS), data, reply, option);
61 if (result != OHOS::NO_ERROR) {
62 HILOG_ERROR("Failed to SendRequest, error code: %{public}d", result);
63 return;
64 }
65 }
66
TestFinished(const std::string & msg,const int64_t & resultCode)67 void TestObserverProxy::TestFinished(const std::string& msg, const int64_t& resultCode)
68 {
69 HILOG_INFO("start");
70
71 MessageParcel data;
72 MessageParcel reply;
73 MessageOption option(MessageOption::TF_ASYNC);
74
75 if (!data.WriteInterfaceToken(GetDescriptor())) {
76 return;
77 }
78
79 sptr<IRemoteObject> remote = Remote();
80 if (remote == nullptr) {
81 HILOG_ERROR("Failed to send cmd to service due to remote object is null");
82 return;
83 }
84
85 if (!data.WriteString(msg)) {
86 HILOG_ERROR("Failed to write string msg");
87 return;
88 }
89
90 if (!data.WriteInt64(resultCode)) {
91 HILOG_ERROR("Failed to write resultCode");
92 return;
93 }
94
95 int32_t result = remote->SendRequest(
96 static_cast<uint32_t>(ITestObserver::Message::AA_TEST_FINISHED), data, reply, option);
97 if (result != OHOS::NO_ERROR) {
98 HILOG_ERROR("Failed to SendRequest, error code: %{public}d", result);
99 return;
100 }
101 }
102
ExecuteShellCommand(const std::string & cmd,const int64_t timeoutSec)103 ShellCommandResult TestObserverProxy::ExecuteShellCommand(
104 const std::string& cmd, const int64_t timeoutSec)
105 {
106 HILOG_INFO("start");
107
108 ShellCommandResult result;
109 MessageParcel data;
110 MessageParcel reply;
111 MessageOption option(MessageOption::TF_SYNC);
112
113 if (!data.WriteInterfaceToken(GetDescriptor())) {
114 return result;
115 }
116
117 sptr<IRemoteObject> remote = Remote();
118 if (remote == nullptr) {
119 HILOG_ERROR("Failed to send cmd to service due to remote object is null");
120 return result;
121 }
122
123 if (!data.WriteString(cmd)) {
124 HILOG_ERROR("Failed to write string cmd");
125 return result;
126 }
127
128 if (!data.WriteInt64(timeoutSec)) {
129 HILOG_ERROR("Failed to write timeoutSec");
130 return result;
131 }
132
133 int32_t ret = remote->SendRequest(
134 static_cast<uint32_t>(ITestObserver::Message::AA_EXECUTE_SHELL_COMMAND), data, reply, option);
135 if (ret != OHOS::NO_ERROR) {
136 HILOG_ERROR("Failed to SendRequest, error code: %{public}d", ret);
137 return result;
138 }
139 ShellCommandResult* resultPtr = reply.ReadParcelable<ShellCommandResult>();
140 if (!resultPtr) {
141 HILOG_ERROR("Failed to read result");
142 return result;
143 }
144 result = *resultPtr;
145 if (resultPtr != nullptr) {
146 delete resultPtr;
147 }
148 return result;
149 }
150 } // namespace AAFwk
151 } // namespace OHOS
152