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 "can_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 static struct CanTestConfig g_config;
16
CanTestDispatch(struct HdfDeviceIoClient * client,int cmd,struct HdfSBuf * data,struct HdfSBuf * reply)17 static int32_t CanTestDispatch(struct HdfDeviceIoClient *client, int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
18 {
19 (void)client;
20 (void)data;
21 HDF_LOGD("CanTestDispatch: enter!");
22
23 if (cmd != 0) {
24 HDF_LOGE("CanTestDispatch: cmd: %d is not support!", cmd);
25 return HDF_ERR_NOT_SUPPORT;
26 }
27
28 if (reply == NULL) {
29 HDF_LOGE("CanTestDispatch: reply is null!");
30 return HDF_ERR_INVALID_PARAM;
31 }
32 if (!HdfSbufWriteBuffer(reply, &g_config, sizeof(g_config))) {
33 HDF_LOGE("CanTestDispatch: write reply fail!");
34 return HDF_ERR_IO;
35 }
36
37 return HDF_SUCCESS;
38 }
39
CanTestSetDftConfig(struct CanTestConfig * config)40 static void CanTestSetDftConfig(struct CanTestConfig *config)
41 {
42 config->busNum = CAN_TEST_BUS_NUM;
43 config->bitRate = CAN_TEST_BIT_RATE;
44 config->workMode = CAN_TEST_WORK_MODE;
45 }
46
CanTestReadConfig(struct CanTestConfig * config,const struct DeviceResourceNode * node)47 static int32_t CanTestReadConfig(struct CanTestConfig *config, const struct DeviceResourceNode *node)
48 {
49 int32_t ret;
50 struct DeviceResourceIface *drsOps = NULL;
51
52 drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
53 if (drsOps == NULL) {
54 HDF_LOGE("CanTestReadConfig: invalid drs ops!");
55 return HDF_FAILURE;
56 }
57
58 ret = drsOps->GetUint16(node, "bus_num", &config->busNum, CAN_TEST_BUS_NUM);
59 if (ret != HDF_SUCCESS) {
60 HDF_LOGW("CanTestReadConfig: read bus num failed, using default ...");
61 }
62
63 ret = drsOps->GetUint32(node, "bit_rate", &config->bitRate, CAN_TEST_BIT_RATE);
64 if (ret != HDF_SUCCESS) {
65 HDF_LOGW("CanTestReadConfig: read bit rate failed, using default ...");
66 }
67
68 ret = drsOps->GetUint8(node, "work_mode", &config->workMode, CAN_TEST_WORK_MODE);
69 if (ret != HDF_SUCCESS) {
70 HDF_LOGW("CanTestReadConfig: read reg len failed, using default ...");
71 }
72
73 return HDF_SUCCESS;
74 }
75
CanTestBind(struct HdfDeviceObject * device)76 static int32_t CanTestBind(struct HdfDeviceObject *device)
77 {
78 int32_t ret;
79 static struct IDeviceIoService service;
80
81 if (device == NULL) {
82 HDF_LOGE("CanTestBind: device is null!");
83 return HDF_ERR_IO;
84 }
85
86 if (device->property == NULL) {
87 HDF_LOGI("CanTestBind: property not configed, using default!");
88 CanTestSetDftConfig(&g_config);
89 } else {
90 ret = CanTestReadConfig(&g_config, device->property);
91 if (ret != HDF_SUCCESS) {
92 HDF_LOGE("CanTestBind: read config fail, ret: %d!", ret);
93 return ret;
94 }
95 }
96
97 service.Dispatch = CanTestDispatch;
98 device->service = &service;
99 return HDF_SUCCESS;
100 }
101
CanTestInit(struct HdfDeviceObject * device)102 static int32_t CanTestInit(struct HdfDeviceObject *device)
103 {
104 (void)device;
105 return HDF_SUCCESS;
106 }
107
CanTestRelease(struct HdfDeviceObject * device)108 static void CanTestRelease(struct HdfDeviceObject *device)
109 {
110 if (device != NULL) {
111 device->service = NULL;
112 }
113 return;
114 }
115
116 struct HdfDriverEntry g_canTestEntry = {
117 .moduleVersion = 1,
118 .Bind = CanTestBind,
119 .Init = CanTestInit,
120 .Release = CanTestRelease,
121 .moduleName = "PLATFORM_CAN_TEST",
122 };
123 HDF_INIT(g_canTestEntry);
124