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 "test_audio_ability_proxy.h"
17 
18 #include <cinttypes>
19 
20 #include "iremote_object.h"
21 #include "message_option.h"
22 #include "message_parcel.h"
23 
24 using namespace OHOS::HiviewDFX;
25 
26 namespace OHOS {
27 namespace {
28     const int DEFAULT_INT_RET = -1;
29     const int32_t DEFAULT_INT32_RET = -1;
30     const uint32_t DEFAULT_UINT32_RET = 9999;
31     const int64_t DEFAULT_INT64_RET = -1;
32     const int64_t DEFAULT_UINT64_RET = 9999;
33     const float DEFAULT_FLOAT_RET = -1.0f;
34     const double DEFAULT_DOUBLE_RET = -1.0;
35     const std::u16string DEFAULT_U16STRING_RET = u"";
36 }
37 
AddVolume(int volume)38 int TestAudioAbilityProxy::AddVolume(int volume)
39 {
40     HiLog::Info(label_, "%{public}s called", __func__);
41     auto remote = Remote();
42     if (remote == nullptr) {
43         HiLog::Error(label_, "%{public}s: object is null", __func__);
44         return DEFAULT_INT_RET;
45     }
46     MessageParcel data;
47     data.WriteInt32(volume);
48     MessageParcel reply;
49     MessageOption option;
50     remote->SendRequest(ADD_VOLUME, data, reply, option);
51 
52     int32_t result = reply.ReadInt32();
53     HiLog::Info(label_, "%{public}s:finish = %{public}d", __func__, result);
54     return result;
55 }
56 
ReduceVolume(int volume)57 int32_t TestAudioAbilityProxy::ReduceVolume(int volume)
58 {
59     HiLog::Info(label_, "%{public}s called", __func__);
60     auto remote = Remote();
61     if (remote == nullptr) {
62         HiLog::Error(label_, "ReduceVolume remote is NULL !");
63         return DEFAULT_INT32_RET;
64     }
65 
66     MessageParcel data;
67     data.WriteInt32(volume);
68 
69     MessageParcel reply;
70     MessageOption option { MessageOption::TF_ASYNC };
71     int32_t ret = remote->SendRequest(REDUCE_VOLUME, data, reply, option);
72 
73     HiLog::Info(label_, "%{public}s:finish = %{public}d", __func__, ret);
74     return ret;
75 }
76 
TestRpcInt32(int32_t value)77 int32_t TestAudioAbilityProxy::TestRpcInt32(int32_t value)
78 {
79     HiLog::Info(label_, "%{public}s called", __func__);
80     auto remote = Remote();
81     if (remote == nullptr) {
82         HiLog::Error(label_, "TestRpcInt32 remote is NULL !");
83         return DEFAULT_INT32_RET;
84     }
85     MessageParcel data;
86     data.WriteInt32(value);
87     MessageParcel reply;
88     MessageOption option;
89     remote->SendRequest(TEST_RPCINT32, data, reply, option);
90 
91     int32_t result = reply.ReadInt32();
92     HiLog::Info(label_, "%{public}s:finish = %{public}d", __func__, result);
93     return result;
94 }
95 
TestRpcUInt32(uint32_t value)96 uint32_t TestAudioAbilityProxy::TestRpcUInt32(uint32_t value)
97 {
98     HiLog::Info(label_, "%{public}s called", __func__);
99     auto remote = Remote();
100     if (remote == nullptr) {
101         HiLog::Error(label_, "TestRpcUInt32 remote is NULL !");
102         return DEFAULT_UINT32_RET;
103     }
104     MessageParcel data;
105     data.WriteUint32(value);
106     MessageParcel reply;
107     MessageOption option;
108     remote->SendRequest(TEST_RPCUINT32, data, reply, option);
109 
110     uint32_t result = reply.ReadUint32();
111     HiLog::Info(label_, "%{public}s:finish = %{public}u", __func__, result);
112     return result;
113 }
114 
TestRpcInt64(int64_t value)115 int64_t TestAudioAbilityProxy::TestRpcInt64(int64_t value)
116 {
117     HiLog::Info(label_, "%{public}s called", __func__);
118     auto remote = Remote();
119     if (remote == nullptr) {
120         HiLog::Error(label_, "TestRpcInt64 remote is NULL !");
121         return DEFAULT_INT64_RET;
122     }
123     MessageParcel data;
124     data.WriteInt64(value);
125     MessageParcel reply;
126     MessageOption option;
127     remote->SendRequest(TEST_RPCINT64, data, reply, option);
128 
129     int64_t result = reply.ReadInt64();
130     HiLog::Info(label_, "%{public}s:finish = %{public}" PRId64, __func__, result);
131     return result;
132 }
133 
TestRpcUInt64(uint64_t value)134 uint64_t TestAudioAbilityProxy::TestRpcUInt64(uint64_t value)
135 {
136     HiLog::Info(label_, "%{public}s called", __func__);
137     auto remote = Remote();
138     if (remote == nullptr) {
139         HiLog::Error(label_, "TestRpcInt64 remote is NULL !");
140         return DEFAULT_UINT64_RET;
141     }
142     MessageParcel data;
143     data.WriteUint64(value);
144     MessageParcel reply;
145     MessageOption option;
146     remote->SendRequest(TEST_RPCUINT64, data, reply, option);
147 
148     uint64_t result = reply.ReadUint64();
149     HiLog::Info(label_, "%{public}s:finish = %{public}" PRIu64, __func__, result);
150     return result;
151 }
152 
TestRpcFloat(float value)153 float TestAudioAbilityProxy::TestRpcFloat(float value)
154 {
155     HiLog::Info(label_, "%{public}s called", __func__);
156     auto remote = Remote();
157     if (remote == nullptr) {
158         HiLog::Error(label_, "TestRpcFloat remote is NULL !");
159         return DEFAULT_FLOAT_RET;
160     }
161     MessageParcel data;
162     data.WriteFloat(value);
163     MessageParcel reply;
164     MessageOption option;
165     remote->SendRequest(TEST_RPCFLOAT, data, reply, option);
166 
167     float result = reply.ReadFloat();
168     HiLog::Info(label_, "%{public}s:finish = %{public}f", __func__, result);
169     return result;
170 }
171 
TestRpcDouble(double value)172 double TestAudioAbilityProxy::TestRpcDouble(double value)
173 {
174     HiLog::Info(label_, "%{public}s called", __func__);
175     auto remote = Remote();
176     if (remote == nullptr) {
177         HiLog::Error(label_, "TestRpcDouble remote is NULL !");
178         return DEFAULT_DOUBLE_RET;
179     }
180     MessageParcel data;
181     data.WriteDouble(value);
182     MessageParcel reply;
183     MessageOption option;
184     remote->SendRequest(TEST_RPCDOUBLE, data, reply, option);
185 
186     double result = reply.ReadDouble();
187     HiLog::Info(label_, "%{public}s:finish = %{public}lf", __func__, result);
188     return result;
189 }
190 
TestRpcString16(const std::u16string & name)191 const std::u16string TestAudioAbilityProxy::TestRpcString16(const std::u16string& name)
192 {
193     HiLog::Info(label_, "%{public}s called", __func__);
194     auto remote = Remote();
195     if (remote == nullptr) {
196         HiLog::Error(label_, "TestRpcString16 remote is NULL !");
197         return DEFAULT_U16STRING_RET;
198     }
199     MessageParcel data;
200     std::u16string strTmp = name;
201     data.WriteString16(strTmp.c_str());
202     MessageParcel reply;
203     MessageOption option;
204     remote->SendRequest(TEST_RPCSTRING16, data, reply, option);
205 
206     return reply.ReadString16();
207 }
208 }
209