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 "allocator_service_stub.h"
17#include "hdf_base.h"
18#include "hdf_device_desc.h"
19#include "hdf_log.h"
20#include "osal_mem.h"
21
22using HdfAllocatorService = struct HdfAllocatorService_ {
23    struct IDeviceIoService ioservice;
24    void *instance;
25};
26
27static int32_t AllocatorServiceDispatch(struct HdfDeviceIoClient *client, int cmdId,
28    struct HdfSBuf *data, struct HdfSBuf *reply)
29{
30    HdfAllocatorService *allocatorService = CONTAINER_OF(
31        client->device->service, HdfAllocatorService, ioservice);
32    return AllocatorServiceOnRemoteRequest(allocatorService->instance, cmdId, *data, *reply);
33}
34
35int HdfAllocatorDriverInit(struct HdfDeviceObject *deviceObject)
36{
37    (void)deviceObject;
38    return HDF_SUCCESS;
39}
40
41int HdfAllocatorDriverBind(struct HdfDeviceObject *deviceObject)
42{
43    HdfAllocatorService *allocatorService =
44        reinterpret_cast<HdfAllocatorService *>(OsalMemAlloc(sizeof(HdfAllocatorService)));
45    if (allocatorService == nullptr) {
46        HDF_LOGE("%{public}s: OsalMemAlloc failed", __func__);
47        return HDF_FAILURE;
48    }
49
50    allocatorService->ioservice.Dispatch = AllocatorServiceDispatch;
51    allocatorService->ioservice.Open = NULL;
52    allocatorService->ioservice.Release = NULL;
53    allocatorService->instance = AllocatorServiceInstance();
54
55    deviceObject->service = &allocatorService->ioservice;
56    HDF_LOGI("%{public}s: exit succ", __func__);
57    return HDF_SUCCESS;
58}
59
60void HdfAllocatorDriverRelease(struct HdfDeviceObject *deviceObject)
61{
62    HdfAllocatorService *allocatorService = CONTAINER_OF(deviceObject->service, HdfAllocatorService, ioservice);
63    AllocatorServiceRelease(allocatorService->instance);
64    OsalMemFree(allocatorService);
65}
66
67struct HdfDriverEntry g_AllocatorDriverEntry = {
68    .moduleVersion = 1,
69    .moduleName = "allocator_service",
70    .Bind = HdfAllocatorDriverBind,
71    .Init = HdfAllocatorDriverInit,
72    .Release = HdfAllocatorDriverRelease,
73};
74
75#ifdef __cplusplus
76extern "C" {
77#endif /* __cplusplus */
78
79HDF_INIT(g_AllocatorDriverEntry);
80
81#ifdef __cplusplus
82}
83#endif /* __cplusplus */
84