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 "vibrator_parser.h"
10 #include <securec.h>
11 #include "device_resource_if.h"
12 #include "hdf_base.h"
13 #include "hdf_device_desc.h"
14 
ParseVibratorAttr(struct DeviceResourceIface * parser,const struct DeviceResourceNode * attrNode,struct VibratorCfgData * config)15 static int32_t ParseVibratorAttr(struct DeviceResourceIface *parser, const struct DeviceResourceNode *attrNode,
16     struct VibratorCfgData *config)
17 {
18     int32_t ret;
19     CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(parser, HDF_ERR_INVALID_PARAM);
20     CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(attrNode, HDF_ERR_INVALID_PARAM);
21     CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(config, HDF_ERR_INVALID_PARAM);
22 
23     ret = parser->GetUint16(attrNode, "chipIdRegister", &config->vibratorAttr.chipIdReg, 0);
24     CHECK_VIBRATOR_PARSER_RESULT_RETURN_VALUE(ret, "chipIdRegister");
25     ret = parser->GetUint16(attrNode, "chipIdValue", &config->vibratorAttr.chipIdValue, 0);
26     CHECK_VIBRATOR_PARSER_RESULT_RETURN_VALUE(ret, "chipIdValue");
27     ret = parser->GetUint16(attrNode, "defaultIntensity", &config->vibratorAttr.defaultIntensity, 0);
28     CHECK_VIBRATOR_PARSER_RESULT_RETURN_VALUE(ret, "defaultIntensity");
29     ret = parser->GetUint16(attrNode, "defaultFrequency", &config->vibratorAttr.defaultFrequency, 0);
30     CHECK_VIBRATOR_PARSER_RESULT_RETURN_VALUE(ret, "defaultFrequency");
31 
32     return ret;
33 }
34 
ParseVibratorInfo(struct DeviceResourceIface * parser,const struct DeviceResourceNode * infoNode,struct VibratorCfgData * config)35 static int32_t ParseVibratorInfo(struct DeviceResourceIface *parser, const struct DeviceResourceNode *infoNode,
36     struct VibratorCfgData *config)
37 {
38     int32_t ret;
39     CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(parser, HDF_ERR_INVALID_PARAM);
40     CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(infoNode, HDF_ERR_INVALID_PARAM);
41     CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(config, HDF_ERR_INVALID_PARAM);
42 
43     ret = parser->GetBool(infoNode, "isSupportIntensity");
44     CHECK_VIBRATOR_PARSER_RESULT_RETURN_VALUE(ret, "isSupportIntensity");
45     ret = parser->GetBool(infoNode, "isSupportFrequency");
46     CHECK_VIBRATOR_PARSER_RESULT_RETURN_VALUE(ret, "isSupportFrequency");
47     ret = parser->GetUint16(infoNode, "intensityMaxValue", (uint16_t *)&config->vibratorInfo.intensityMaxValue, 0);
48     CHECK_VIBRATOR_PARSER_RESULT_RETURN_VALUE(ret, "intensityMaxValue");
49     ret = parser->GetUint16(infoNode, "intensityMinValue", (uint16_t *)&config->vibratorInfo.intensityMinValue, 0);
50     CHECK_VIBRATOR_PARSER_RESULT_RETURN_VALUE(ret, "intensityMinValue");
51     ret = parser->GetUint16(infoNode, "frequencyMaxValue", (uint16_t *)&config->vibratorInfo.frequencyMaxValue, 0);
52     CHECK_VIBRATOR_PARSER_RESULT_RETURN_VALUE(ret, "frequencyMaxValue");
53     ret = parser->GetUint16(infoNode, "frequencyMinValue", (uint16_t *)&config->vibratorInfo.frequencyMinValue, 0);
54     CHECK_VIBRATOR_PARSER_RESULT_RETURN_VALUE(ret, "frequencyMinValue");
55 
56     return ret;
57 }
58 
59 
ParseVibratorBus(struct DeviceResourceIface * parser,const struct DeviceResourceNode * busNode,struct VibratorCfgData * config)60 static int32_t ParseVibratorBus(struct DeviceResourceIface *parser, const struct DeviceResourceNode *busNode,
61     struct VibratorCfgData *config)
62 {
63     int32_t ret;
64     CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(parser, HDF_ERR_INVALID_PARAM);
65     CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(busNode, HDF_ERR_INVALID_PARAM);
66     CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(config, HDF_ERR_INVALID_PARAM);
67 
68     ret = parser->GetUint8(busNode, "busType", &config->vibratorBus.busType, 0);
69     CHECK_VIBRATOR_PARSER_RESULT_RETURN_VALUE(ret, "busType");
70 
71     if (config->vibratorBus.busType == VIBRATOR_BUS_I2C) {
72         ret = parser->GetUint16(busNode, "busNum", &config->vibratorBus.i2cCfg.busNum, 0);
73         CHECK_VIBRATOR_PARSER_RESULT_RETURN_VALUE(ret, "busNum");
74         ret = parser->GetUint16(busNode, "busAddr", &config->vibratorBus.i2cCfg.devAddr, 0);
75         CHECK_VIBRATOR_PARSER_RESULT_RETURN_VALUE(ret, "busAddr");
76         ret = parser->GetUint16(busNode, "regWidth", &config->vibratorBus.i2cCfg.regWidth, 0);
77         CHECK_VIBRATOR_PARSER_RESULT_RETURN_VALUE(ret, "regWidth");
78     } else if (config->vibratorBus.busType == VIBRATOR_BUS_GPIO) {
79         ret = parser->GetUint32(busNode, "busNum", &config->vibratorBus.GpioNum, 0);
80         CHECK_VIBRATOR_PARSER_RESULT_RETURN_VALUE(ret, "busNum");
81     }
82 
83     return ret;
84 }
85 
GetVibratorBaseConfigData(const struct DeviceResourceNode * node,struct VibratorCfgData * config)86 int32_t GetVibratorBaseConfigData(const struct DeviceResourceNode *node, struct VibratorCfgData *config)
87 {
88     int32_t ret;
89     struct DeviceResourceIface *parser = NULL;
90     const struct DeviceResourceNode *infoNode = NULL;
91     const struct DeviceResourceNode *busNode = NULL;
92     const struct DeviceResourceNode *attrNode = NULL;
93 
94     CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(node, HDF_ERR_INVALID_PARAM);
95     CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(config, HDF_ERR_INVALID_PARAM);
96 
97     parser = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
98     CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(parser, HDF_ERR_INVALID_PARAM);
99 
100     busNode = parser->GetChildNode(node, "VibratorBusConfig");
101     if (busNode != NULL) {
102         ret = ParseVibratorBus(parser, busNode, config);
103         CHECK_VIBRATOR_PARSER_RESULT_RETURN_VALUE(ret, "VibratorBusConfig");
104     }
105 
106     if (config->vibratorBus.busType == VIBRATOR_BUS_I2C) {
107         attrNode = parser->GetChildNode(node, "VibratorAttr");
108         if (attrNode != NULL) {
109             ret = ParseVibratorAttr(parser, attrNode, config);
110             CHECK_VIBRATOR_PARSER_RESULT_RETURN_VALUE(ret, "VibratorAttr");
111         }
112     }
113 
114     infoNode = parser->GetChildNode(node, "VibratorChipConfig");
115     if (infoNode != NULL) {
116         ret = ParseVibratorInfo(parser, infoNode, config);
117         CHECK_VIBRATOR_PARSER_RESULT_RETURN_VALUE(ret, "VibratorChipConfig");
118     }
119 
120     return ret;
121 }