1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#include <hdf_log.h>
17094332d3Sopenharmony_ci
18094332d3Sopenharmony_ci#include "hdf_base.h"
19094332d3Sopenharmony_ci#include "hdf_device_desc.h"
20094332d3Sopenharmony_ci#include "hdf_sbuf_ipc.h"
21094332d3Sopenharmony_ci#include "v1_3/ril_stub.h"
22094332d3Sopenharmony_ci#include "hril_hdf.h"
23094332d3Sopenharmony_ci
24094332d3Sopenharmony_ciusing namespace OHOS::HDI::Ril::V1_3;
25094332d3Sopenharmony_ciusing namespace OHOS::HDI::Ril;
26094332d3Sopenharmony_ci
27094332d3Sopenharmony_cistruct HdfRilHost {
28094332d3Sopenharmony_ci    struct IDeviceIoService ioService;
29094332d3Sopenharmony_ci    OHOS::sptr<OHOS::IRemoteObject> stub;
30094332d3Sopenharmony_ci};
31094332d3Sopenharmony_ci
32094332d3Sopenharmony_cistatic int32_t RilDriverDispatch(
33094332d3Sopenharmony_ci    struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data, struct HdfSBuf *reply)
34094332d3Sopenharmony_ci{
35094332d3Sopenharmony_ci    auto *hdfRilHost = CONTAINER_OF(client->device->service, struct HdfRilHost, ioService);
36094332d3Sopenharmony_ci
37094332d3Sopenharmony_ci    OHOS::MessageParcel *dataParcel = nullptr;
38094332d3Sopenharmony_ci    OHOS::MessageParcel *replyParcel = nullptr;
39094332d3Sopenharmony_ci    OHOS::MessageOption option;
40094332d3Sopenharmony_ci
41094332d3Sopenharmony_ci    if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
42094332d3Sopenharmony_ci        HDF_LOGE("invalid data sbuf object to dispatch");
43094332d3Sopenharmony_ci        return HDF_ERR_INVALID_PARAM;
44094332d3Sopenharmony_ci    }
45094332d3Sopenharmony_ci    if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
46094332d3Sopenharmony_ci        HDF_LOGE("invalid reply sbuf object to dispatch");
47094332d3Sopenharmony_ci        return HDF_ERR_INVALID_PARAM;
48094332d3Sopenharmony_ci    }
49094332d3Sopenharmony_ci    int ret = hdfRilHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
50094332d3Sopenharmony_ci    return ret;
51094332d3Sopenharmony_ci}
52094332d3Sopenharmony_ci
53094332d3Sopenharmony_cistatic int32_t HdfRilDriverInit(struct HdfDeviceObject *deviceObject)
54094332d3Sopenharmony_ci{
55094332d3Sopenharmony_ci    InitRilAdapter();
56094332d3Sopenharmony_ci    return HDF_SUCCESS;
57094332d3Sopenharmony_ci}
58094332d3Sopenharmony_ci
59094332d3Sopenharmony_cistatic int32_t HdfRilDriverBind(struct HdfDeviceObject *deviceObject)
60094332d3Sopenharmony_ci{
61094332d3Sopenharmony_ci    auto *hdfRilHost = new (std::nothrow) HdfRilHost;
62094332d3Sopenharmony_ci    if (hdfRilHost == nullptr) {
63094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: failed to create HdfRilHost object", __func__);
64094332d3Sopenharmony_ci        return HDF_FAILURE;
65094332d3Sopenharmony_ci    }
66094332d3Sopenharmony_ci
67094332d3Sopenharmony_ci    hdfRilHost->ioService.Dispatch = RilDriverDispatch;
68094332d3Sopenharmony_ci    hdfRilHost->ioService.Open = nullptr;
69094332d3Sopenharmony_ci    hdfRilHost->ioService.Release = nullptr;
70094332d3Sopenharmony_ci
71094332d3Sopenharmony_ci    auto serviceImpl = V1_3::IRil::Get(true);
72094332d3Sopenharmony_ci    if (serviceImpl == nullptr) {
73094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: failed to get of implement service", __func__);
74094332d3Sopenharmony_ci        delete hdfRilHost;
75094332d3Sopenharmony_ci        hdfRilHost = nullptr;
76094332d3Sopenharmony_ci        return HDF_FAILURE;
77094332d3Sopenharmony_ci    }
78094332d3Sopenharmony_ci
79094332d3Sopenharmony_ci    hdfRilHost->stub =
80094332d3Sopenharmony_ci        OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl, V1_3::IRil::GetDescriptor());
81094332d3Sopenharmony_ci    if (hdfRilHost->stub == nullptr) {
82094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: failed to get stub object", __func__);
83094332d3Sopenharmony_ci        delete hdfRilHost;
84094332d3Sopenharmony_ci        hdfRilHost = nullptr;
85094332d3Sopenharmony_ci        return HDF_FAILURE;
86094332d3Sopenharmony_ci    }
87094332d3Sopenharmony_ci
88094332d3Sopenharmony_ci    if (deviceObject == nullptr) {
89094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: failed to get device object", __func__);
90094332d3Sopenharmony_ci        delete hdfRilHost;
91094332d3Sopenharmony_ci        hdfRilHost = nullptr;
92094332d3Sopenharmony_ci        return HDF_FAILURE;
93094332d3Sopenharmony_ci    }
94094332d3Sopenharmony_ci    deviceObject->service = &hdfRilHost->ioService;
95094332d3Sopenharmony_ci    return HDF_SUCCESS;
96094332d3Sopenharmony_ci}
97094332d3Sopenharmony_ci
98094332d3Sopenharmony_cistatic void HdfRilDriverRelease(struct HdfDeviceObject *deviceObject)
99094332d3Sopenharmony_ci{
100094332d3Sopenharmony_ci    if (deviceObject == nullptr || deviceObject->service == nullptr) {
101094332d3Sopenharmony_ci        HDF_LOGE("HdfRilDriverRelease not initted");
102094332d3Sopenharmony_ci        return;
103094332d3Sopenharmony_ci    }
104094332d3Sopenharmony_ci    ReleaseRilAdapter();
105094332d3Sopenharmony_ci    auto *hdfRilHost = CONTAINER_OF(deviceObject->service, struct HdfRilHost, ioService);
106094332d3Sopenharmony_ci    delete hdfRilHost;
107094332d3Sopenharmony_ci}
108094332d3Sopenharmony_ci
109094332d3Sopenharmony_cistatic struct HdfDriverEntry g_RilDriverEntry = {
110094332d3Sopenharmony_ci    .moduleVersion = 1,
111094332d3Sopenharmony_ci    .moduleName = "ril_service",
112094332d3Sopenharmony_ci    .Bind = HdfRilDriverBind,
113094332d3Sopenharmony_ci    .Init = HdfRilDriverInit,
114094332d3Sopenharmony_ci    .Release = HdfRilDriverRelease,
115094332d3Sopenharmony_ci};
116094332d3Sopenharmony_ci
117094332d3Sopenharmony_ci#ifndef __cplusplus
118094332d3Sopenharmony_ciextern "C" {
119094332d3Sopenharmony_ci#endif
120094332d3Sopenharmony_ciHDF_INIT(g_RilDriverEntry);
121094332d3Sopenharmony_ci#ifndef __cplusplus
122094332d3Sopenharmony_ci}
123094332d3Sopenharmony_ci#endif