• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-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 "sensor_config_parser.h"
10 #include <securec.h>
11 #include "device_resource_if.h"
12 #include "osal_mem.h"
13 #include "sensor_platform_if.h"
14 
15 #define HDF_LOG_TAG    khdf_sensor_common_driver
16 
17 static char *g_sensorRegGroupName[SENSOR_GROUP_MAX] = {
18     "initSeqConfig",
19     "enableSeqConfig",
20     "disableSeqConfig",
21 };
22 
GetSensorRegGroupNameIndex(const char * name)23 static uint32_t GetSensorRegGroupNameIndex(const char *name)
24 {
25     uint32_t index;
26 
27     if (name == NULL) {
28         return SENSOR_GROUP_MAX;
29     }
30 
31     for (index = 0; index < SENSOR_GROUP_MAX; ++index) {
32         if ((g_sensorRegGroupName[index] != NULL) && (strcmp(name, g_sensorRegGroupName[index]) == 0)) {
33             break;
34         }
35     }
36 
37     return index;
38 }
39 
ReleaseSensorAllRegConfig(struct SensorCfgData * config)40 void ReleaseSensorAllRegConfig(struct SensorCfgData *config)
41 {
42     int32_t index;
43 
44     if (config == NULL || config->regCfgGroup == NULL) {
45         return;
46     }
47 
48     for (index = 0; index < SENSOR_GROUP_MAX; ++index) {
49         if (config->regCfgGroup[index] != NULL) {
50             if (config->regCfgGroup[index]->regCfgItem != NULL) {
51                 OsalMemFree(config->regCfgGroup[index]->regCfgItem);
52                 config->regCfgGroup[index]->regCfgItem = NULL;
53             }
54             OsalMemFree(config->regCfgGroup[index]);
55             config->regCfgGroup[index] = NULL;
56         }
57     }
58 }
59 
ParseSensorRegItem(struct DeviceResourceIface * parser,const struct DeviceResourceNode * regNode,const char * groupName,struct SensorRegCfgGroupNode * group)60 static int32_t ParseSensorRegItem(struct DeviceResourceIface *parser, const struct DeviceResourceNode *regNode,
61     const char *groupName, struct SensorRegCfgGroupNode *group)
62 {
63     int32_t ret;
64     int32_t step;
65     uint32_t index;
66     int32_t num;
67     uint32_t itemNum = group->itemNum;
68     uint16_t *buf = NULL;
69 
70     CHECK_NULL_PTR_RETURN_VALUE(group->regCfgItem, HDF_ERR_INVALID_PARAM);
71     CHECK_NULL_PTR_RETURN_VALUE(groupName, HDF_ERR_INVALID_PARAM);
72 
73     num = parser->GetElemNum(regNode, groupName);
74     if (num <= 0 || num > SENSOR_CONFIG_MAX_ITEM) {
75         HDF_LOGE("%s: parser %s element num failed", __func__, groupName);
76         return HDF_SUCCESS;
77     }
78 
79     buf = (uint16_t *)OsalMemCalloc(sizeof(uint16_t) * num);
80     CHECK_NULL_PTR_RETURN_VALUE(buf, HDF_ERR_MALLOC_FAIL);
81 
82     ret = parser->GetUint16Array(regNode, groupName, buf, num, 0);
83     if (ret != HDF_SUCCESS) {
84         HDF_LOGE("%s: parser %s reg array failed", __func__, groupName);
85         OsalMemFree(buf);
86         return HDF_SUCCESS;
87     }
88 
89     for (index = 0; index < itemNum; ++index) {
90         step = SENSOR_REG_CFG_INDEX_MAX * index;
91         if (step + SENSOR_REG_CFG_SAVE_INDEX >= num) {
92             break;
93         }
94         group->regCfgItem[index].regAddr = buf[step + SENSOR_REG_CFG_ADDR_INDEX];
95         group->regCfgItem[index].value = buf[step + SENSOR_REG_CFG_VALUE_INDEX];
96         group->regCfgItem[index].mask = buf[step + SENSOR_REG_CFG_MASK_INDEX];
97         group->regCfgItem[index].len = buf[step + SENSOR_REG_CFG_LEN_INDEX];
98         group->regCfgItem[index].delay = buf[step + SENSOR_REG_CFG_DELAY_INDEX];
99         group->regCfgItem[index].opsType = buf[step + SENSOR_REG_CFG_OPS_INDEX];
100         group->regCfgItem[index].calType = buf[step + SENSOR_REG_CFG_CAL_INDEX];
101         group->regCfgItem[index].shiftNum = buf[step + SENSOR_REG_CFG_SHIFT_INDEX];
102         group->regCfgItem[index].debug = buf[step + SENSOR_REG_CFG_DEBUG_INDEX];
103         group->regCfgItem[index].save = buf[step + SENSOR_REG_CFG_SAVE_INDEX];
104     }
105     OsalMemFree(buf);
106 
107     return HDF_SUCCESS;
108 }
109 
ParseSensorRegGroup(struct DeviceResourceIface * parser,const struct DeviceResourceNode * regCfgNode,const char * groupName,struct SensorRegCfgGroupNode ** groupNode)110 int32_t ParseSensorRegGroup(struct DeviceResourceIface *parser, const struct DeviceResourceNode *regCfgNode,
111     const char *groupName, struct SensorRegCfgGroupNode **groupNode)
112 {
113     int32_t num;
114     struct SensorRegCfgGroupNode *group = NULL;
115 
116     CHECK_NULL_PTR_RETURN_VALUE(parser, HDF_ERR_INVALID_PARAM);
117     CHECK_NULL_PTR_RETURN_VALUE(regCfgNode, HDF_ERR_INVALID_PARAM);
118     CHECK_NULL_PTR_RETURN_VALUE(groupName, HDF_ERR_INVALID_PARAM);
119     CHECK_NULL_PTR_RETURN_VALUE(groupNode, HDF_ERR_INVALID_PARAM);
120 
121     num = parser->GetElemNum(regCfgNode, groupName);
122     group = *groupNode;
123 
124     if (num > 0) {
125         if (group != NULL) {
126             if (group->regCfgItem != NULL) {
127                 OsalMemFree(group->regCfgItem);
128             }
129             OsalMemFree(group);
130         }
131 
132         group = (struct SensorRegCfgGroupNode*)OsalMemCalloc(sizeof(*group));
133         if (group == NULL) {
134             HDF_LOGE("%s: malloc sensor reg config group failed", __func__);
135             return HDF_ERR_MALLOC_FAIL;
136         }
137 
138         *groupNode = group;
139         group->itemNum = (uint32_t)(num / SENSOR_REG_CFG_INDEX_MAX);
140         group->itemNum = ((SENSOR_REG_CFG_INDEX_MAX * group->itemNum) < (uint32_t)num) ?
141             (group->itemNum + 1) : group->itemNum;
142 
143         group->regCfgItem = (struct SensorRegCfg*)OsalMemCalloc(group->itemNum * sizeof(*(group->regCfgItem)));
144         if (group->regCfgItem == NULL) {
145             HDF_LOGE("%s: malloc sensor reg config item failed", __func__);
146             return HDF_ERR_MALLOC_FAIL;
147         }
148 
149         if (ParseSensorRegItem(parser, regCfgNode, groupName, group) != HDF_SUCCESS) {
150             HDF_LOGE("%s: malloc sensor reg config item data failed", __func__);
151             return HDF_FAILURE;
152         }
153     }
154 
155     return HDF_SUCCESS;
156 }
157 
ParseSensorRegConfig(struct SensorCfgData * config)158 int32_t ParseSensorRegConfig(struct SensorCfgData *config)
159 {
160     uint32_t index;
161     const struct DeviceResourceNode *regCfgNode = NULL;
162     struct DeviceResourceIface *parser = NULL;
163     const struct DeviceResourceAttr *regAttr = NULL;
164 
165     CHECK_NULL_PTR_RETURN_VALUE(config->root, HDF_ERR_INVALID_PARAM);
166     parser = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
167     CHECK_NULL_PTR_RETURN_VALUE(parser, HDF_ERR_INVALID_PARAM);
168 
169     regCfgNode = parser->GetChildNode(config->root, "sensorRegConfig");
170     CHECK_NULL_PTR_RETURN_VALUE(regCfgNode, HDF_ERR_INVALID_PARAM);
171 
172     DEV_RES_NODE_FOR_EACH_ATTR(regCfgNode, regAttr) {
173         if (regAttr == NULL || regAttr->name == NULL) {
174             HDF_LOGE("%s:sensor reg node attr is null", __func__);
175             break;
176         }
177 
178         index = GetSensorRegGroupNameIndex(regAttr->name);
179         if (index >= SENSOR_GROUP_MAX) {
180             HDF_LOGE("%s: get sensor register group index failed", __func__);
181             goto error;
182         }
183 
184         if (ParseSensorRegGroup(parser, regCfgNode, regAttr->name, &config->regCfgGroup[index]) != HDF_SUCCESS) {
185             HDF_LOGE("%s: parse sensor register group failed", __func__);
186             goto error;
187         }
188     }
189     return HDF_SUCCESS;
190 
191 error:
192     ReleaseSensorAllRegConfig(config);
193     HDF_LOGE("%s: parse sensor reg config failed", __func__);
194     return HDF_FAILURE;
195 }
196 
GetSensorI2cHandle(struct SensorBusCfg * busCfg)197 static int32_t GetSensorI2cHandle(struct SensorBusCfg *busCfg)
198 {
199     CHECK_NULL_PTR_RETURN_VALUE(busCfg, HDF_ERR_INVALID_PARAM);
200     int16_t busNum = busCfg->i2cCfg.busNum;
201     busCfg->i2cCfg.handle = I2cOpen(busNum);
202     if (busCfg->i2cCfg.handle == NULL) {
203         HDF_LOGE("%s: sensor i2c Handle invalid", __func__);
204         return HDF_FAILURE;
205     }
206 
207     return HDF_SUCCESS;
208 }
209 
210 #if defined(LOSCFG_DRIVERS_HDF_PLATFORM_SPI) || defined(CONFIG_DRIVERS_HDF_PLATFORM_SPI)
GetSensorSpiHandle(struct SensorBusCfg * busCfg)211 static int32_t GetSensorSpiHandle(struct SensorBusCfg *busCfg)
212 {
213     int32_t ret;
214     struct SpiDevInfo spiDevinfo;
215     CHECK_NULL_PTR_RETURN_VALUE	(busCfg, HDF_ERR_INVALID_PARAM);
216 
217     spiDevinfo.busNum = busCfg->spiCfg.busNum;
218     spiDevinfo.csNum = busCfg->spiCfg.csNum;
219     busCfg->spiCfg.handle = SpiOpen(&spiDevinfo);
220 
221     ret = SpiSetCfg(busCfg->spiCfg.handle, &busCfg->spiCfg.spi);
222     if (ret != HDF_SUCCESS) {
223         HDF_LOGE("%s: SpiSetCfg failed", __func__);
224         SpiClose(busCfg->spiCfg.handle);
225         return ret;
226     }
227 
228     return HDF_SUCCESS;
229 }
230 #endif // LOSCFG_DRIVERS_HDF_PLATFORM_SPI || CONFIG_DRIVERS_HDF_PLATFORM_SPI
231 
GetSensorBusHandle(struct SensorBusCfg * busCfg)232 int32_t GetSensorBusHandle(struct SensorBusCfg *busCfg)
233 {
234     CHECK_NULL_PTR_RETURN_VALUE(busCfg, HDF_ERR_INVALID_PARAM);
235 
236     if (busCfg->busType == SENSOR_BUS_I2C) {
237         return GetSensorI2cHandle(busCfg);
238 #if defined(LOSCFG_DRIVERS_HDF_PLATFORM_SPI) || defined(CONFIG_DRIVERS_HDF_PLATFORM_SPI)
239     } else if (busCfg->busType == SENSOR_BUS_SPI) {
240         return GetSensorSpiHandle(busCfg);
241 #endif // LOSCFG_DRIVERS_HDF_PLATFORM_SPI || CONFIG_DRIVERS_HDF_PLATFORM_SPI
242     }
243 
244     return HDF_SUCCESS;
245 }
246 
ReleaseSensorBusHandle(struct SensorBusCfg * busCfg)247 int32_t ReleaseSensorBusHandle(struct SensorBusCfg *busCfg)
248 {
249     if (busCfg == NULL) {
250         return HDF_SUCCESS;
251     }
252 
253     if (busCfg->busType == SENSOR_BUS_I2C && busCfg->i2cCfg.handle != NULL) {
254         I2cClose(busCfg->i2cCfg.handle);
255         busCfg->i2cCfg.handle = NULL;
256 
257 #if defined(LOSCFG_DRIVERS_HDF_PLATFORM_SPI) || defined(CONFIG_DRIVERS_HDF_PLATFORM_SPI)
258     } else if (busCfg->busType == SENSOR_BUS_SPI) {
259         SpiClose(busCfg->spiCfg.handle);
260         busCfg->spiCfg.handle = NULL;
261 #endif
262     }
263 
264     return HDF_SUCCESS;
265 }
266 
DetectSensorDevice(struct SensorCfgData * config)267 int32_t DetectSensorDevice(struct SensorCfgData *config)
268 {
269     uint8_t value = 0;
270     uint16_t chipIdReg;
271     uint16_t chipIdValue;
272     int32_t ret;
273 
274     CHECK_NULL_PTR_RETURN_VALUE(config, HDF_ERR_INVALID_PARAM);
275 
276     chipIdReg = config->sensorAttr.chipIdReg;
277     chipIdValue = config->sensorAttr.chipIdValue;
278 
279     ret = GetSensorBusHandle(&config->busCfg);
280     if (ret != HDF_SUCCESS) {
281         HDF_LOGE("%s: get sensor bus handle failed", __func__);
282         (void)ReleaseSensorBusHandle(&config->busCfg);
283         return HDF_FAILURE;
284     }
285 
286     ret = ReadSensor(&config->busCfg, chipIdReg, &value, sizeof(value));
287     if (ret != HDF_SUCCESS) {
288         HDF_LOGE("%s: i2c read chip id failed", __func__);
289         (void)ReleaseSensorBusHandle(&config->busCfg);
290         return HDF_FAILURE;
291     }
292 
293     if (value != chipIdValue) {
294         HDF_LOGE("%s: sensor chip[0x%x] id [0x%x] detect value[%hhu]", __func__, chipIdReg, chipIdValue, value);
295         (void)ReleaseSensorBusHandle(&config->busCfg);
296         return HDF_FAILURE;
297     }
298 
299     HDF_LOGD("%s: sensor [%s] detect chip success", __func__, config->sensorInfo.sensorName);
300     return HDF_SUCCESS;
301 }
302 
ParseSensorInfo(struct DeviceResourceIface * parser,const struct DeviceResourceNode * infoNode,struct SensorCfgData * config)303 static int32_t ParseSensorInfo(struct DeviceResourceIface *parser, const struct DeviceResourceNode *infoNode,
304     struct SensorCfgData *config)
305 {
306     int32_t ret;
307     uint16_t id;
308     int32_t value;
309     const char *name = NULL;
310 
311     ret = parser->GetString(infoNode, "sensorName", &name, NULL);
312     CHECK_PARSER_RESULT_RETURN_VALUE(ret, "sensorName");
313     if (strcpy_s(config->sensorInfo.sensorName, SENSOR_INFO_NAME_MAX_LEN, name) != EOK) {
314         HDF_LOGE("%s:copy sensorName failed!", __func__);
315         return HDF_FAILURE;
316     }
317 
318     ret = parser->GetString(infoNode, "vendorName", &name, NULL);
319     CHECK_PARSER_RESULT_RETURN_VALUE(ret, "vendorName");
320     if (strcpy_s(config->sensorInfo.vendorName, SENSOR_INFO_NAME_MAX_LEN, name) != EOK) {
321         HDF_LOGE("%s:copy vendorName failed!", __func__);
322         return HDF_FAILURE;
323     }
324 
325     ret = parser->GetString(infoNode, "firmwareVersion", &name, NULL);
326     CHECK_PARSER_RESULT_RETURN_VALUE(ret, "firmwareVersion");
327     if (strcpy_s(config->sensorInfo.firmwareVersion, SENSOR_INFO_VERSION_MAX_LEN, name) != EOK) {
328         HDF_LOGE("%s:copy firmwareVersion failed!", __func__);
329         return HDF_FAILURE;
330     }
331 
332     ret = parser->GetString(infoNode, "hardwareVersion", &name, NULL);
333     CHECK_PARSER_RESULT_RETURN_VALUE(ret, "hardwareVersion");
334     if (strcpy_s(config->sensorInfo.hardwareVersion, SENSOR_INFO_VERSION_MAX_LEN, name) != EOK) {
335         HDF_LOGE("%s:copy hardwareVersion failed!", __func__);
336         return HDF_FAILURE;
337     }
338 
339     ret = parser->GetUint16(infoNode, "sensorTypeId", &id, 0);
340     CHECK_PARSER_RESULT_RETURN_VALUE(ret, "sensorTypeId");
341     config->sensorInfo.sensorTypeId = id;
342     ret = parser->GetUint16(infoNode, "sensorId", &id, 0);
343     CHECK_PARSER_RESULT_RETURN_VALUE(ret, "sensorId");
344     config->sensorInfo.sensorId = id;
345 
346     ret = parser->GetUint32(infoNode, "maxRange", (uint32_t *)&value, 0);
347     CHECK_PARSER_RESULT_RETURN_VALUE(ret, "maxRange");
348     config->sensorInfo.maxRange = value;
349     ret = parser->GetUint32(infoNode, "accuracy", (uint32_t *)&value, 0);
350     CHECK_PARSER_RESULT_RETURN_VALUE(ret, "accuracy");
351     config->sensorInfo.accuracy = value;
352     ret = parser->GetUint32(infoNode, "power", (uint32_t *)&value, 0);
353     CHECK_PARSER_RESULT_RETURN_VALUE(ret, "power");
354     config->sensorInfo.power = value;
355     ret = parser->GetUint64(infoNode, "minDelay", (uint64_t *)&value, 0);
356     CHECK_PARSER_RESULT_RETURN_VALUE(ret, "minDelay");
357     config->sensorInfo.minDelay = value;
358     ret = parser->GetUint64(infoNode, "maxDelay", (uint64_t *)&value, 0);
359     CHECK_PARSER_RESULT_RETURN_VALUE(ret, "maxDelay");
360     config->sensorInfo.maxDelay = value;
361 
362     return ret;
363 }
364 
ParseSensorSpiBus(struct DeviceResourceIface * parser,const struct DeviceResourceNode * spiBusNode,struct SensorBusCfg * busConfig)365 static int32_t ParseSensorSpiBus(struct DeviceResourceIface *parser, const struct DeviceResourceNode *spiBusNode,
366     struct SensorBusCfg *busConfig)
367 {
368     int32_t ret;
369 
370     ret = parser->GetUint32(spiBusNode, "maxSpeedHz", &busConfig->spiCfg.spi.maxSpeedHz, 0);
371     CHECK_PARSER_RESULT_RETURN_VALUE(ret, "maxSpeedHz");
372     ret = parser->GetUint16(spiBusNode, "mode", &busConfig->spiCfg.spi.mode, 0);
373     CHECK_PARSER_RESULT_RETURN_VALUE(ret, "mode");
374     ret = parser->GetUint8(spiBusNode, "transferMode", &busConfig->spiCfg.spi.transferMode, 0);
375     CHECK_PARSER_RESULT_RETURN_VALUE(ret, "transferMode");
376     ret = parser->GetUint8(spiBusNode, "bitsPerWord", &busConfig->spiCfg.spi.bitsPerWord, 0);
377     CHECK_PARSER_RESULT_RETURN_VALUE(ret, "bitsPerWord");
378 
379     return HDF_SUCCESS;
380 }
381 
ParseSensorBus(struct DeviceResourceIface * parser,const struct DeviceResourceNode * busNode,struct SensorCfgData * config)382 static int32_t ParseSensorBus(struct DeviceResourceIface *parser, const struct DeviceResourceNode *busNode,
383     struct SensorCfgData *config)
384 {
385     int32_t ret;
386     const struct DeviceResourceNode *spiBusNode = NULL;
387 
388     ret = parser->GetUint8(busNode, "busType", &config->busCfg.busType, 0);
389     CHECK_PARSER_RESULT_RETURN_VALUE(ret, "busType");
390     ret = parser->GetUint8(busNode, "regBigEndian", &config->busCfg.regBigEndian, 0);
391     CHECK_PARSER_RESULT_RETURN_VALUE(ret, "regBigEndian");
392 
393     if (config->busCfg.busType == SENSOR_BUS_I2C) {
394         ret = parser->GetUint16(busNode, "busNum", &config->busCfg.i2cCfg.busNum, 0);
395         CHECK_PARSER_RESULT_RETURN_VALUE(ret, "busNum");
396         ret = parser->GetUint16(busNode, "busAddr", &config->busCfg.i2cCfg.devAddr, 0);
397         CHECK_PARSER_RESULT_RETURN_VALUE(ret, "busAddr");
398         ret = parser->GetUint16(busNode, "regWidth", &config->busCfg.i2cCfg.regWidth, 0);
399         CHECK_PARSER_RESULT_RETURN_VALUE(ret, "regWidth");
400     } else if (config->busCfg.busType == SENSOR_BUS_SPI) {
401         ret = parser->GetUint32(busNode, "busNum", &config->busCfg.spiCfg.busNum, 0);
402         CHECK_PARSER_RESULT_RETURN_VALUE(ret, "busNum");
403         ret = parser->GetUint32(busNode, "busAddr", &config->busCfg.spiCfg.csNum, 0);
404         CHECK_PARSER_RESULT_RETURN_VALUE(ret, "busAddr");
405         spiBusNode = parser->GetChildNode(busNode, "spiBusCfg");
406         if (spiBusNode != NULL) {
407             ret = ParseSensorSpiBus(parser, spiBusNode, &config->busCfg);
408             CHECK_PARSER_RESULT_RETURN_VALUE(ret, "sensorBusConfig.spiBusCfg");
409         }
410     } else if (config->busCfg.busType == SENSOR_BUS_GPIO) {
411         ret = parser->GetUint32(busNode, "gpioIrq1", &config->busCfg.GpioNum[SENSOR_GPIO_NUM1], 0);
412         CHECK_PARSER_RESULT_RETURN_VALUE(ret, "gpioIrq1");
413         ret = parser->GetUint32(busNode, "gpioIrq2", &config->busCfg.GpioNum[SENSOR_GPIO_NUM2], 0);
414         CHECK_PARSER_RESULT_RETURN_VALUE(ret, "gpioIrq2");
415     }
416 
417     return HDF_SUCCESS;
418 }
419 
ParseSensorAttr(struct DeviceResourceIface * parser,const struct DeviceResourceNode * attrNode,struct SensorCfgData * config)420 static int32_t ParseSensorAttr(struct DeviceResourceIface *parser, const struct DeviceResourceNode *attrNode,
421     struct SensorCfgData *config)
422 {
423     int32_t ret;
424     ret = parser->GetString(attrNode, "chipName", &config->sensorAttr.chipName, NULL);
425     CHECK_PARSER_RESULT_RETURN_VALUE(ret, "chipName");
426     ret = parser->GetUint16(attrNode, "chipIdRegister", &config->sensorAttr.chipIdReg, 0);
427     CHECK_PARSER_RESULT_RETURN_VALUE(ret, "chipIdRegister");
428     ret = parser->GetUint16(attrNode, "chipIdValue", &config->sensorAttr.chipIdValue, 0);
429     CHECK_PARSER_RESULT_RETURN_VALUE(ret, "chipIdValue");
430 
431     return ret;
432 }
433 
ReleaseSensorDirectionConfig(struct SensorCfgData * config)434 void ReleaseSensorDirectionConfig(struct SensorCfgData *config)
435 {
436     CHECK_NULL_PTR_RETURN(config);
437 
438     if (config->direction != NULL) {
439         OsalMemFree(config->direction);
440         config->direction = NULL;
441     }
442 }
443 
ParseSensorDirection(struct SensorCfgData * config)444 int32_t ParseSensorDirection(struct SensorCfgData *config)
445 {
446     int32_t num;
447     int32_t ret;
448     uint32_t index;
449     uint32_t *buf = NULL;
450     const struct DeviceResourceNode *directionNode = NULL;
451     struct DeviceResourceIface *parser = NULL;
452 
453     CHECK_NULL_PTR_RETURN_VALUE(config->root, HDF_ERR_INVALID_PARAM);
454     parser = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
455     CHECK_NULL_PTR_RETURN_VALUE(parser, HDF_ERR_INVALID_PARAM);
456 
457     directionNode = parser->GetChildNode(config->root, "sensorDirection");
458     CHECK_NULL_PTR_RETURN_VALUE(directionNode, HDF_ERR_INVALID_PARAM);
459 
460     num = parser->GetElemNum(directionNode, "convert");
461     ret = parser->GetUint32(directionNode, "direction", &index, 0);
462     CHECK_PARSER_RESULT_RETURN_VALUE(ret, "direction");
463     if ((num <= 0 || num > MAX_SENSOR_INDEX_NUM) || (index > num / AXIS_INDEX_MAX)) {
464         HDF_LOGE("%s: parser %d element num failed", __func__, num);
465         return HDF_FAILURE;
466     }
467 
468     buf = (uint32_t *)OsalMemCalloc(sizeof(uint32_t) * num);
469     CHECK_NULL_PTR_RETURN_VALUE(buf, HDF_ERR_MALLOC_FAIL);
470 
471     ret = parser->GetUint32Array(directionNode, "convert", buf, num, 0);
472     if (ret != HDF_SUCCESS) {
473         HDF_LOGE("%s: parser %s convert failed", __func__, "convert");
474         OsalMemFree(buf);
475         return HDF_FAILURE;
476     }
477 
478     config->direction = (struct SensorDirection*)OsalMemCalloc(sizeof(struct SensorDirection));
479     if (config->direction == NULL) {
480         HDF_LOGE("%s: malloc sensor direction config item failed", __func__);
481         OsalMemFree(buf);
482         return HDF_ERR_MALLOC_FAIL;
483     }
484 
485     index = index * AXIS_INDEX_MAX;
486     config->direction->sign[AXIS_X] = buf[index + SIGN_X_INDEX];
487     config->direction->sign[AXIS_Y] = buf[index + SIGN_Y_INDEX];
488     config->direction->sign[AXIS_Z] = buf[index + SIGN_Z_INDEX];
489     config->direction->map[AXIS_X] = buf[index + AXIS_X_INDEX];
490     config->direction->map[AXIS_Y] = buf[index + AXIS_Y_INDEX];
491     config->direction->map[AXIS_Z] = buf[index + AXIS_Z_INDEX];
492 
493     OsalMemFree(buf);
494     return HDF_SUCCESS;
495 }
496 
SensorRawDataToRemapData(struct SensorDirection * direction,int32_t * remapData,uint32_t num)497 int32_t SensorRawDataToRemapData(struct SensorDirection *direction, int32_t *remapData, uint32_t num)
498 {
499     uint32_t axis;
500     int32_t directionSign[MAX_SENSOR_AXIS_NUM];
501     int32_t newData[MAX_SENSOR_AXIS_NUM];
502 
503     CHECK_NULL_PTR_RETURN_VALUE(direction, HDF_ERR_INVALID_PARAM);
504     CHECK_NULL_PTR_RETURN_VALUE(remapData, HDF_ERR_INVALID_PARAM);
505     if (num != MAX_SENSOR_AXIS_NUM) {
506         HDF_LOGE("%s: afferent num failed", __func__);
507         return HDF_FAILURE;
508     }
509 
510     for (axis = 0; axis < num; axis++) {
511         if (direction->sign[axis] == 0) {
512             directionSign[axis] = 1;
513         } else {
514             directionSign[axis] = -1;
515         }
516     }
517 
518     newData[direction->map[AXIS_X]] = directionSign[AXIS_X] * remapData[AXIS_X];
519     newData[direction->map[AXIS_Y]] = directionSign[AXIS_Y] * remapData[AXIS_Y];
520     newData[direction->map[AXIS_Z]] = directionSign[AXIS_Z] * remapData[AXIS_Z];
521 
522     remapData[AXIS_X] = newData[direction->map[AXIS_X]];
523     remapData[AXIS_Y] = newData[direction->map[AXIS_Y]];
524     remapData[AXIS_Z] = newData[direction->map[AXIS_Z]];
525 
526     return HDF_SUCCESS;
527 }
528 
GetSensorBaseConfigData(const struct DeviceResourceNode * node,struct SensorCfgData * config)529 int32_t GetSensorBaseConfigData(const struct DeviceResourceNode *node, struct SensorCfgData *config)
530 {
531     int32_t ret;
532     struct DeviceResourceIface *parser = NULL;
533     const struct DeviceResourceNode *infoNode = NULL;
534     const struct DeviceResourceNode *busNode = NULL;
535     const struct DeviceResourceNode *attrNode = NULL;
536 
537     CHECK_NULL_PTR_RETURN_VALUE(node, HDF_ERR_INVALID_PARAM);
538     CHECK_NULL_PTR_RETURN_VALUE(config, HDF_ERR_INVALID_PARAM);
539 
540     parser = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
541     CHECK_NULL_PTR_RETURN_VALUE(parser, HDF_ERR_INVALID_PARAM);
542 
543     config->root = node;
544     CHECK_NULL_PTR_RETURN_VALUE(parser->GetChildNode, HDF_ERR_INVALID_PARAM);
545 
546     infoNode = parser->GetChildNode(node, "sensorInfo");
547     if (infoNode != NULL) {
548         ret = ParseSensorInfo(parser, infoNode, config);
549         CHECK_PARSER_RESULT_RETURN_VALUE(ret, "sensorInfo");
550     }
551 
552     busNode = parser->GetChildNode(node, "sensorBusConfig");
553     if (busNode != NULL) {
554         ret = ParseSensorBus(parser, busNode, config);
555         CHECK_PARSER_RESULT_RETURN_VALUE(ret, "sensorBusConfig");
556     }
557 
558     attrNode = parser->GetChildNode(node, "sensorIdAttr");
559     if (attrNode != NULL) {
560         ret = ParseSensorAttr(parser, attrNode, config);
561         CHECK_PARSER_RESULT_RETURN_VALUE(ret, "sensorIdAttr");
562     }
563 
564     return HDF_SUCCESS;
565 }
566