• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "hdf_io_service_if.h"
10 #include "hdf_log.h"
11 #include "adc_if.h"
12 
13 #define HDF_LOG_TAG adc_if_u_c
14 #define ADC_SERVICE_NAME "HDF_PLATFORM_ADC_MANAGER"
15 
AdcManagerGetService(void)16 static void *AdcManagerGetService(void)
17 {
18     static void *manager = NULL;
19 
20     if (manager != NULL) {
21         return manager;
22     }
23     manager = (void *)HdfIoServiceBind(ADC_SERVICE_NAME);
24     if (manager == NULL) {
25         HDF_LOGE("%s: fail to get adc manager!", __func__);
26     }
27     return manager;
28 }
29 
AdcOpen(uint32_t number)30 DevHandle AdcOpen(uint32_t number)
31 {
32     int32_t ret;
33     struct HdfIoService *service = NULL;
34     struct HdfSBuf *data = NULL;
35     struct HdfSBuf *reply = NULL;
36     uint32_t handle;
37 
38     service = (struct HdfIoService *)AdcManagerGetService();
39     if (service == NULL) {
40         return NULL;
41     }
42     data = HdfSbufObtainDefaultSize();
43     if (data == NULL) {
44         HDF_LOGE("%s: malloc data fail!", __func__);
45         return NULL;
46     }
47     reply = HdfSbufObtainDefaultSize();
48     if (reply == NULL) {
49         HDF_LOGE("%s: malloc reply fail!", __func__);
50         HdfSbufRecycle(data);
51         return NULL;
52     }
53 
54     if (!HdfSbufWriteUint32(data, (uint32_t)number)) {
55         HDF_LOGE("%s: write number fail!", __func__);
56         goto ERR;
57     }
58 
59     ret = service->dispatcher->Dispatch(&service->object, ADC_IO_OPEN, data, reply);
60     if (ret != HDF_SUCCESS) {
61         HDF_LOGE("%s: service call open fail:%d", __func__, ret);
62         goto ERR;
63     }
64 
65     if (!HdfSbufReadUint32(reply, &handle)) {
66         HDF_LOGE("%s: read handle fail!", __func__);
67         goto ERR;
68     }
69     HdfSbufRecycle(data);
70     HdfSbufRecycle(reply);
71     return (DevHandle)(uintptr_t)handle;
72 ERR:
73     HdfSbufRecycle(data);
74     HdfSbufRecycle(reply);
75     return NULL;
76 }
77 
AdcClose(DevHandle handle)78 void AdcClose(DevHandle handle)
79 {
80     int32_t ret;
81     struct HdfIoService *service = NULL;
82     struct HdfSBuf *data = NULL;
83 
84     service = (struct HdfIoService *)AdcManagerGetService();
85     if (service == NULL) {
86         return;
87     }
88 
89     data = HdfSbufObtainDefaultSize();
90     if (data == NULL) {
91         return;
92     }
93 
94     if (!HdfSbufWriteUint32(data, (uint32_t)(uintptr_t)handle)) {
95         HDF_LOGE("%s: write handle fail!", __func__);
96         HdfSbufRecycle(data);
97         return;
98     }
99 
100     ret = service->dispatcher->Dispatch(&service->object, ADC_IO_CLOSE, data, NULL);
101     if (ret != HDF_SUCCESS) {
102         HDF_LOGE("%s: close handle fail:%d", __func__, ret);
103     }
104     HdfSbufRecycle(data);
105 }
106 
AdcRead(DevHandle handle,uint32_t channel,uint32_t * val)107 int32_t AdcRead(DevHandle handle, uint32_t channel, uint32_t *val)
108 {
109     int32_t ret;
110     struct HdfSBuf *data = NULL;
111     struct HdfSBuf *reply = NULL;
112     struct HdfIoService *service = NULL;
113 
114     service = (struct HdfIoService *)AdcManagerGetService();
115     if (service == NULL) {
116         return HDF_PAL_ERR_DEV_CREATE;
117     }
118 
119     data = HdfSbufObtainDefaultSize();
120     if (data == NULL) {
121         HDF_LOGE("%s: failed to obtain data!", __func__);
122         return HDF_ERR_MALLOC_FAIL;
123     }
124 
125     reply = HdfSbufObtainDefaultSize();
126     if (reply == NULL) {
127         HdfSbufRecycle(data);
128         return HDF_ERR_MALLOC_FAIL;
129     }
130 
131     if (!HdfSbufWriteUint32(data, (uint32_t)(uintptr_t)handle)) {
132         HDF_LOGE("%s: write handle fail!", __func__);
133         ret = HDF_ERR_IO;
134         goto EXIT;
135     }
136     if (!HdfSbufWriteUint32(data, (uint32_t)channel)) {
137         HDF_LOGE("%s: write adc number failed!", __func__);
138         ret = HDF_ERR_IO;
139         goto EXIT;
140     }
141     ret = service->dispatcher->Dispatch(&service->object, ADC_IO_READ, data, reply);
142     if (ret != HDF_SUCCESS) {
143         HDF_LOGE("%s: failed to send service call:%d", __func__, ret);
144         goto EXIT;
145     }
146 
147     if (!HdfSbufReadUint32(reply, val)) {
148         HDF_LOGE("%s: read sbuf failed", __func__);
149         ret = HDF_ERR_IO;
150         goto EXIT;
151     }
152 
153     goto EXIT;
154 EXIT:
155     HdfSbufRecycle(data);
156     HdfSbufRecycle(reply);
157     return ret;
158 }
159