1 /*
2  * Copyright (c) 2021-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 "itest_transaction_service.h"
17 
18 #include "ipc_object_stub.h"
19 #include "ipc_types.h"
20 #include "message_option.h"
21 #include "message_parcel.h"
22 
23 using namespace std;
24 
25 namespace OHOS {
26 namespace {
27 constexpr int32_t REVERSE_INT = 1;
28 constexpr int32_t RESULT_STEP = 10;
29 }
ReverseInt(int32_t data,int32_t & rep)30 int32_t TestTransactionServiceProxy::ReverseInt(int32_t data, int32_t& rep)
31 {
32     MessageParcel dataParcel;
33     MessageParcel replyParcel;
34     MessageOption option;
35     sptr<IRemoteObject> remote = Remote();
36     if (remote == nullptr) {
37         HILOGE("TestTransactionServiceProxy remote null!");
38         return -1;
39     }
40     HILOGI("TestTransactionServiceProxy:send to server data = %d", data);
41     int32_t result = remote->SendRequest(REVERSE_INT, dataParcel, replyParcel, option);
42     if (result != ERR_NONE) {
43         HILOGE("TestTransactionServiceProxy SendRequest failed, errno:%d!", result);
44         return -1;
45     }
46     result = replyParcel.ReadInt32();
47     HILOGI("TestTransactionServiceProxy:get result from server data = %d", result);
48     return ERR_NONE;
49 }
50 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)51 int32_t TestTransactionServiceStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
52     MessageOption& option)
53 {
54     HILOGI("TestTransactionServiceStub::OnRemoteRequest, code = %u, flags = %d", code, option.GetFlags());
55     switch (code) {
56         case REVERSE_INT: {
57             int32_t result = 0;
58             int32_t value = data.ReadInt32();
59             int32_t ret = ReverseInt(value, result);
60             reply.WriteInt32(result);
61             HILOGI("ReverseInt result = %d", result);
62             return ret;
63         }
64         default:
65             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
66     }
67 }
68 
ReverseInt(int32_t data,int32_t & rep)69 int32_t TestTransactionService::ReverseInt(int32_t data, int32_t& rep)
70 {
71     int32_t result = 0;
72     while (data != 0) {
73         result = result * RESULT_STEP + data % RESULT_STEP;
74         data = data / RESULT_STEP;
75     }
76     rep = result;
77     return ERR_NONE;
78 }
79 } // namespace OHOS
80 
81