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_stub.h"
17#include "hilog_tag_wrapper.h"
18
19namespace OHOS {
20namespace AAFwk {
21TestObserverStub::TestObserverStub()
22{
23    TAG_LOGI(AAFwkTag::AA_TOOL, "created");
24}
25
26TestObserverStub::~TestObserverStub()
27{
28    TAG_LOGI(AAFwkTag::AA_TOOL, "destroyed");
29}
30
31int TestObserverStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
32{
33    if (data.ReadInterfaceToken() != GetDescriptor()) {
34        TAG_LOGE(AAFwkTag::AA_TOOL, "invalid descriptor");
35        return ERR_TRANSACTION_FAILED;
36    }
37    switch (code) {
38        case static_cast<uint32_t>(ITestObserver::Message::AA_TEST_STATUS): {
39            std::string msg = data.ReadString();
40            int64_t resultCode = data.ReadInt64();
41            TestStatus(msg, resultCode);
42            break;
43        }
44        case static_cast<uint32_t>(ITestObserver::Message::AA_TEST_FINISHED): {
45            std::string msg = data.ReadString();
46            int64_t resultCode = data.ReadInt64();
47            TestFinished(msg, resultCode);
48            break;
49        }
50        case static_cast<uint32_t>(ITestObserver::Message::AA_EXECUTE_SHELL_COMMAND): {
51            std::string cmd = data.ReadString();
52            int64_t timeoutSecs = data.ReadInt64();
53            ShellCommandResult result = ExecuteShellCommand(cmd, timeoutSecs);
54            if (!reply.WriteParcelable(&result)) {
55                TAG_LOGE(AAFwkTag::AA_TOOL, "write ShellCommandResult failed");
56                return ERR_INVALID_VALUE;
57            }
58            break;
59        }
60        default:
61            TAG_LOGW(AAFwkTag::AA_TOOL, "event receive stub receives unknown code: %{public}u", code);
62            return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
63    }
64
65    return NO_ERROR;
66}
67}  // namespace AAFwk
68}  // namespace OHOS
69