1 /*
2 * Copyright (C) 2025-2025 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 #include "raw_parcel_callback_stub.h"
16 #include "telephony_log_wrapper.h"
17 #include "telephony_errors.h"
18
19 namespace OHOS {
20 namespace Telephony {
RawParcelCallbackStub(std::function<void (MessageParcel & data)> callback)21 RawParcelCallbackStub::RawParcelCallbackStub(std::function<void(MessageParcel &data)> callback)
22 : reader_(callback)
23 {
24 }
25
Transfer(std::function<void (MessageParcel &)> writer,MessageParcel & data)26 void RawParcelCallbackStub::Transfer(std::function<void(MessageParcel&)> writer, MessageParcel &data)
27 {
28 if (writer) { // same process ipc call
29 writer(data);
30 } else {
31 if (!CheckCurrentDescriptor(data)) {
32 return;
33 }
34 }
35 if (reader_) {
36 reader_(data);
37 NotifyReceiveDone();
38 }
39 }
40
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)41 int RawParcelCallbackStub::OnRemoteRequest(
42 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
43 {
44 Transfer(nullptr, data);
45 return TELEPHONY_ERR_SUCCESS;
46 }
47
WaitForResult(int64_t timeoutMs)48 bool RawParcelCallbackStub::WaitForResult(int64_t timeoutMs)
49 {
50 std::unique_lock<std::mutex> lock(mtx_);
51 return cv_.wait_for(lock, std::chrono::microseconds(timeoutMs), [this] { return done_; });
52 }
53
CheckCurrentDescriptor(MessageParcel & data)54 bool RawParcelCallbackStub::CheckCurrentDescriptor(MessageParcel &data)
55 {
56 std::u16string myDescriptor = GetDescriptor();
57 std::u16string remoteDescriptor = data.ReadInterfaceToken();
58 if (myDescriptor != remoteDescriptor) {
59 TELEPHONY_LOGE("descriptor check fail!");
60 return false;
61 }
62 return true;
63 }
64
NotifyReceiveDone()65 void RawParcelCallbackStub::NotifyReceiveDone()
66 {
67 std::unique_lock<std::mutex> lock(mtx_);
68 done_ = true;
69 cv_.notify_one();
70 }
71
72 } // namespace Telephony
73 } // namespace OHOS