• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "adc_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 adc_test_driver_c
16 
17 static struct AdcTestConfig g_config;
18 
AdcTestDispatch(struct HdfDeviceIoClient * client,int cmd,struct HdfSBuf * data,struct HdfSBuf * reply)19 static int32_t AdcTestDispatch(struct HdfDeviceIoClient *client, int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
20 {
21     (void)data;
22     (void)client;
23 
24     if (cmd == 0) {
25         if (reply == NULL) {
26             HDF_LOGE("AdcTestDispatch: reply is null!");
27             return HDF_ERR_INVALID_PARAM;
28         }
29         if (!HdfSbufWriteBuffer(reply, &g_config, sizeof(g_config))) {
30             HDF_LOGE("AdcTestDispatch: write reply fail!");
31             return HDF_ERR_IO;
32         }
33     } else {
34         HDF_LOGE("AdcTestDispatch: cmd %d is not support!", cmd);
35         return HDF_ERR_NOT_SUPPORT;
36     }
37 
38     return HDF_SUCCESS;
39 }
40 
AdcTestReadConfig(struct AdcTestConfig * config,const struct DeviceResourceNode * node)41 static int32_t AdcTestReadConfig(struct AdcTestConfig *config, const struct DeviceResourceNode *node)
42 {
43     int32_t ret;
44     struct DeviceResourceIface *drsOps = NULL;
45 
46     drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
47     if (drsOps == NULL || drsOps->GetUint32 == NULL) {
48         HDF_LOGE("AdcTestReadConfig: invalid drs ops!");
49         return HDF_FAILURE;
50     }
51 
52     ret = drsOps->GetUint32(node, "devNum", &config->devNum, 0);
53     if (ret != HDF_SUCCESS) {
54         HDF_LOGE("AdcTestReadConfig: read devNum fail!");
55         return ret;
56     }
57 
58     ret = drsOps->GetUint32(node, "channel", &config->channel, 0);
59     if (ret != HDF_SUCCESS) {
60         HDF_LOGE("AdcTestReadConfig: read channel fail!");
61         return ret;
62     }
63 
64     ret = drsOps->GetUint32(node, "maxChannel", &config->maxChannel, 0);
65     if (ret != HDF_SUCCESS) {
66         HDF_LOGE("AdcTestReadConfig: read maxChannel fail!");
67         return ret;
68     }
69 
70     ret = drsOps->GetUint32(node, "dataWidth", &config->dataWidth, 0);
71     if (ret != HDF_SUCCESS) {
72         HDF_LOGE("AdcTestReadConfig: read dataWidth fail!");
73         return ret;
74     }
75 
76     ret = drsOps->GetUint32(node, "rate", &config->rate, 0);
77     if (ret != HDF_SUCCESS) {
78         HDF_LOGE("AdcTestReadConfig: read rate fail!");
79         return ret;
80     }
81 
82     return HDF_SUCCESS;
83 }
84 
AdcTestBind(struct HdfDeviceObject * device)85 static int32_t AdcTestBind(struct HdfDeviceObject *device)
86 {
87     int32_t ret;
88     static struct IDeviceIoService service;
89 
90     if (device == NULL || device->property == NULL) {
91         HDF_LOGE("AdcTestBind: device or config is null!");
92         return HDF_ERR_IO;
93     }
94 
95     ret = AdcTestReadConfig(&g_config, device->property);
96     if (ret != HDF_SUCCESS) {
97         HDF_LOGE("AdcTestBind: read config fail!");
98         return ret;
99     }
100     service.Dispatch = AdcTestDispatch;
101     device->service = &service;
102     HDF_LOGI("AdcTestBind: done!");
103     return HDF_SUCCESS;
104 }
105 
AdcTestInit(struct HdfDeviceObject * device)106 static int32_t AdcTestInit(struct HdfDeviceObject *device)
107 {
108     (void)device;
109     HDF_LOGI("AdcTestInit: done!");
110     return HDF_SUCCESS;
111 }
112 
AdcTestRelease(struct HdfDeviceObject * device)113 static void AdcTestRelease(struct HdfDeviceObject *device)
114 {
115     if (device != NULL) {
116         device->service = NULL;
117     }
118     HDF_LOGI("AdcTestRelease: done!");
119     return;
120 }
121 
122 struct HdfDriverEntry g_adcTestEntry = {
123     .moduleVersion = 1,
124     .Bind = AdcTestBind,
125     .Init = AdcTestInit,
126     .Release = AdcTestRelease,
127     .moduleName = "PLATFORM_ADC_TEST",
128 };
129 HDF_INIT(g_adcTestEntry);
130