• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include "pwm_test.h"
10 #include "device_resource_if.h"
11 #include "hdf_base.h"
12 #include "hdf_device_desc.h"
13 #include "hdf_log.h"
14 
15 #define HDF_LOG_TAG pwm_driver_test_c
16 
17 static struct PwmTestConfig g_config;
18 
PwmTestDispatch(struct HdfDeviceIoClient * client,int cmd,struct HdfSBuf * data,struct HdfSBuf * reply)19 static int32_t PwmTestDispatch(struct HdfDeviceIoClient *client, int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
20 {
21     (void)client;
22     (void)data;
23     if (cmd == 0) {
24         if (reply == NULL) {
25             HDF_LOGE("PwmTestDispatch: reply is null!");
26             return HDF_ERR_INVALID_PARAM;
27         }
28         if (!HdfSbufWriteBuffer(reply, &g_config, sizeof(g_config))) {
29             HDF_LOGE("PwmTestDispatch: write reply fail!");
30             return HDF_ERR_IO;
31         }
32     } else {
33         HDF_LOGE("PwmTestDispatch: cmd %d is not support!\n", cmd);
34         return HDF_ERR_NOT_SUPPORT;
35     }
36 
37     return HDF_SUCCESS;
38 }
39 
PwmTestReadConfig(struct PwmTestConfig * config,const struct DeviceResourceNode * node)40 static int32_t PwmTestReadConfig(struct PwmTestConfig *config, const struct DeviceResourceNode *node)
41 {
42     int32_t ret;
43     struct DeviceResourceIface *drsOps = NULL;
44     uint32_t temp;
45 
46     drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
47     if (drsOps == NULL) {
48         HDF_LOGE("PwmTestReadConfig: drsOps is null!");
49         return HDF_FAILURE;
50     }
51 
52     if (drsOps->GetUint32 == NULL) {
53         HDF_LOGE("PwmTestReadConfig: GetUint32 not support!");
54         return HDF_ERR_NOT_SUPPORT;
55     }
56 
57     ret = drsOps->GetUint32(node, "num", &(config->num), 0);
58     if (ret != HDF_SUCCESS) {
59         HDF_LOGE("PwmTestReadConfig: read num fail!");
60         return ret;
61     }
62 
63     ret = drsOps->GetUint32(node, "period", &(config->cfg.period), 0);
64     if (ret != HDF_SUCCESS) {
65         HDF_LOGE("PwmTestReadConfig: read period fail!");
66         return ret;
67     }
68 
69     ret = drsOps->GetUint32(node, "duty", &(config->cfg.duty), 0);
70     if (ret != HDF_SUCCESS) {
71         HDF_LOGE("PwmTestReadConfig: read duty fail!");
72         return ret;
73     }
74 
75     ret = drsOps->GetUint32(node, "polarity", &(temp), 0);
76     if (ret != HDF_SUCCESS) {
77         HDF_LOGE("PwmTestReadConfig: read polarity fail!");
78         return ret;
79     }
80     config->cfg.polarity = temp;
81 
82     ret = drsOps->GetUint32(node, "status", &(temp), 0);
83     if (ret != HDF_SUCCESS) {
84         HDF_LOGE("PwmTestReadConfig: read status fail!");
85         return ret;
86     }
87     config->cfg.status = temp;
88 
89     return HDF_SUCCESS;
90 }
91 
PwmTestBind(struct HdfDeviceObject * device)92 static int32_t PwmTestBind(struct HdfDeviceObject *device)
93 {
94     int32_t ret;
95     static struct IDeviceIoService service;
96 
97     if (device == NULL || device->property == NULL) {
98         HDF_LOGE("PwmTestBind: device or config is null!");
99         return HDF_ERR_INVALID_OBJECT;
100     }
101 
102     ret = PwmTestReadConfig(&g_config, device->property);
103     if (ret != HDF_SUCCESS) {
104         HDF_LOGE("PwmTestBind: read config fail!");
105         return ret;
106     }
107 
108     service.Dispatch = PwmTestDispatch;
109     device->service = &service;
110     return HDF_SUCCESS;
111 }
112 
PwmTestInit(struct HdfDeviceObject * device)113 static int32_t PwmTestInit(struct HdfDeviceObject *device)
114 {
115     (void)device;
116     return HDF_SUCCESS;
117 }
118 
PwmTestRelease(struct HdfDeviceObject * device)119 static void PwmTestRelease(struct HdfDeviceObject *device)
120 {
121     if (device != NULL) {
122         device->service = NULL;
123     }
124     HDF_LOGI("PwmTestRelease: done!");
125     return;
126 }
127 
128 struct HdfDriverEntry g_pwmTestEntry = {
129     .moduleVersion = 1,
130     .Bind = PwmTestBind,
131     .Init = PwmTestInit,
132     .Release = PwmTestRelease,
133     .moduleName = "PLATFORM_PWM_TEST",
134 };
135 HDF_INIT(g_pwmTestEntry);