1 /*
2 * Copyright (c) 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 <hdf_base.h>
17 #include <hdf_log.h>
18 #include <hdf_sbuf_ipc.h>
19 #include <hdi_support.h>
20 #include <object_collector.h>
21 #include <string_ex.h>
22
23 #include "foo_service.h"
24
25 #include "sample_service_stub.h"
26
27 #define HDF_LOG_TAG sample_stub
28
29 namespace OHOS {
30 namespace HDI {
31 namespace Sample {
32 namespace V1_0 {
Get(const std::string & serviceName,bool isStub)33 sptr<ISample> ISample::Get(const std::string &serviceName, bool isStub)
34 {
35 if (!isStub) {
36 return nullptr;
37 }
38
39 std::string desc = Str16ToStr8(ISample::GetDescriptor());
40 void *impl = LoadHdiImpl(desc.data(), serviceName.c_str());
41 if (impl == nullptr) {
42 HDF_LOGE("failed to load hdi impl %{public}s", desc.data());
43 return nullptr;
44 }
45
46 return reinterpret_cast<ISample *>(impl);
47 }
48
Get(bool isStub)49 sptr<ISample> ISample::Get(bool isStub)
50 {
51 std::string defaultName = "sample_service";
52 return ISample::Get(defaultName, isStub);
53 }
54
SampleServiceStub(const sptr<ISample> & serviceImpl)55 SampleServiceStub::SampleServiceStub(const sptr<ISample> &serviceImpl)
56 : IPCObjectStub(ISample::GetDescriptor()), impl_(serviceImpl)
57 {
58 }
59
~SampleServiceStub()60 SampleServiceStub::~SampleServiceStub()
61 {
62 ObjectCollector::GetInstance().RemoveObject(impl_);
63 }
64
SampleStubGetInterface(MessageParcel & data,MessageParcel & reply,MessageOption & option) const65 int32_t SampleServiceStub::SampleStubGetInterface(
66 MessageParcel &data, MessageParcel &reply, MessageOption &option) const
67 {
68 if (data.ReadInterfaceToken() != ISample::GetDescriptor()) {
69 HDF_LOGE("failed to check interface");
70 return HDF_ERR_INVALID_PARAM;
71 }
72 sptr<IFoo> interfaceObj;
73 auto ret = impl_->GetInterface(interfaceObj);
74 if (ret != HDF_SUCCESS || interfaceObj == nullptr) {
75 HDF_LOGE("%{public}s: call failed", __func__);
76 return HDF_FAILURE;
77 }
78
79 sptr<IRemoteObject> stubObject = ObjectCollector::GetInstance().GetOrNewObject(interfaceObj, IFoo::GetDescriptor());
80 if (stubObject == nullptr) {
81 HDF_LOGE("%{public}s: failed to cast interface to stub object", __func__);
82 return HDF_ERR_INVALID_OBJECT;
83 }
84
85 reply.WriteRemoteObject(stubObject);
86 return HDF_SUCCESS;
87 }
88
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)89 int32_t SampleServiceStub::OnRemoteRequest(
90 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
91 {
92 switch (code) {
93 case CMD_INTERFACE_TRANS_TEST:
94 return SampleStubGetInterface(data, reply, option);
95 default: {
96 HDF_LOGE("%{public}s: not support cmd %{public}d", __func__, code);
97 return HDF_ERR_INVALID_PARAM;
98 }
99 }
100 return HDF_SUCCESS;
101 }
102 } // namespace V1_0
103 } // namespace Sample
104 } // namespace HDI
105 } // namespace OHOS
106