1 /*
2 * Copyright (c) 2022-2024 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_tag_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 TAG_LOGI(AAFwkTag::AA_TOOL, "created");
24 }
25
~TestObserverProxy()26 TestObserverProxy::~TestObserverProxy()
27 {
28 TAG_LOGI(AAFwkTag::AA_TOOL, "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 TAG_LOGI(AAFwkTag::AA_TOOL, "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 if (!data.WriteString(msg)) {
44 TAG_LOGE(AAFwkTag::AA_TOOL, "WriteString msg failed");
45 return;
46 }
47
48 if (!data.WriteInt64(resultCode)) {
49 TAG_LOGE(AAFwkTag::AA_TOOL, "Write resultCode failed");
50 return;
51 }
52
53 int32_t result = SendTransactCmd(
54 static_cast<uint32_t>(ITestObserver::Message::AA_TEST_STATUS), data, reply, option);
55 if (result != OHOS::NO_ERROR) {
56 TAG_LOGE(AAFwkTag::AA_TOOL, "SendRequest error: %{public}d", result);
57 return;
58 }
59 }
60
TestFinished(const std::string & msg,const int64_t & resultCode)61 void TestObserverProxy::TestFinished(const std::string& msg, const int64_t& resultCode)
62 {
63 TAG_LOGI(AAFwkTag::AA_TOOL, "start");
64
65 MessageParcel data;
66 MessageParcel reply;
67 MessageOption option(MessageOption::TF_ASYNC);
68
69 if (!data.WriteInterfaceToken(GetDescriptor())) {
70 return;
71 }
72
73 if (!data.WriteString(msg)) {
74 TAG_LOGE(AAFwkTag::AA_TOOL, "WriteString msg failed");
75 return;
76 }
77
78 if (!data.WriteInt64(resultCode)) {
79 TAG_LOGE(AAFwkTag::AA_TOOL, "Write resultCode failed");
80 return;
81 }
82
83 int32_t result = SendTransactCmd(
84 static_cast<uint32_t>(ITestObserver::Message::AA_TEST_FINISHED), data, reply, option);
85 if (result != OHOS::NO_ERROR) {
86 TAG_LOGE(AAFwkTag::AA_TOOL, "SendRequest error: %{public}d", result);
87 return;
88 }
89 }
90
ExecuteShellCommand(const std::string & cmd,const int64_t timeoutSec)91 ShellCommandResult TestObserverProxy::ExecuteShellCommand(
92 const std::string& cmd, const int64_t timeoutSec)
93 {
94 TAG_LOGI(AAFwkTag::AA_TOOL, "start");
95
96 ShellCommandResult result;
97 MessageParcel data;
98 MessageParcel reply;
99 MessageOption option(MessageOption::TF_SYNC);
100
101 if (!data.WriteInterfaceToken(GetDescriptor())) {
102 return result;
103 }
104
105 if (!data.WriteString(cmd)) {
106 TAG_LOGE(AAFwkTag::AA_TOOL, "WriteString cmd failed");
107 return result;
108 }
109
110 if (!data.WriteInt64(timeoutSec)) {
111 TAG_LOGE(AAFwkTag::AA_TOOL, "Write timeoutSec failed");
112 return result;
113 }
114
115 int32_t ret = SendTransactCmd(
116 static_cast<uint32_t>(ITestObserver::Message::AA_EXECUTE_SHELL_COMMAND), data, reply, option);
117 if (ret != OHOS::NO_ERROR) {
118 TAG_LOGE(AAFwkTag::AA_TOOL, "SendRequest error: %{public}d", ret);
119 return result;
120 }
121 ShellCommandResult* resultPtr = reply.ReadParcelable<ShellCommandResult>();
122 if (!resultPtr) {
123 TAG_LOGE(AAFwkTag::AA_TOOL, "Read result failed");
124 return result;
125 }
126 result = *resultPtr;
127 if (resultPtr != nullptr) {
128 delete resultPtr;
129 }
130 return result;
131 }
132
SendTransactCmd(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)133 int32_t TestObserverProxy::SendTransactCmd(uint32_t code, MessageParcel &data,
134 MessageParcel &reply, MessageOption &option)
135 {
136 sptr<IRemoteObject> remote = Remote();
137 if (remote == nullptr) {
138 TAG_LOGE(AAFwkTag::AA_TOOL, "null remote");
139 return ERR_NULL_OBJECT;
140 }
141
142 return remote->SendRequest(code, data, reply, option);
143 }
144
145 } // namespace AAFwk
146 } // namespace OHOS
147