MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / rt_hw_sensor_register

Function rt_hw_sensor_register

components/drivers/sensor/v1/sensor.c:431–498  ·  view source on GitHub ↗

* sensor register */

Source from the content-addressed store, hash-verified

429 * sensor register
430 */
431int rt_hw_sensor_register(rt_sensor_t sensor,
432 const char *name,
433 rt_uint32_t flag,
434 void *data)
435{
436 rt_int8_t result;
437 rt_device_t device;
438 RT_ASSERT(sensor != RT_NULL);
439
440 char *sensor_name = RT_NULL, *device_name = RT_NULL;
441
442 if (sensor->ops == RT_NULL)
443 {
444 sensor->ops = &local_ops;
445 }
446
447 /* Add a type name for the sensor device */
448 sensor_name = sensor_name_str[sensor->info.type];
449 device_name = (char *)rt_calloc(1, rt_strlen(sensor_name) + 1 + rt_strlen(name));
450 if (device_name == RT_NULL)
451 {
452 LOG_E("device_name calloc failed!");
453 return -RT_ERROR;
454 }
455
456 rt_memcpy(device_name, sensor_name, rt_strlen(sensor_name) + 1);
457 strcat(device_name, name);
458
459 if (sensor->module != RT_NULL && sensor->module->lock == RT_NULL)
460 {
461 /* Create a mutex lock for the module */
462 sensor->module->lock = rt_mutex_create(name, RT_IPC_FLAG_PRIO);
463 if (sensor->module->lock == RT_NULL)
464 {
465 rt_free(device_name);
466 return -RT_ERROR;
467 }
468 }
469
470 device = &sensor->parent;
471
472#ifdef RT_USING_DEVICE_OPS
473 device->ops = &rt_sensor_ops;
474#else
475 device->init = RT_NULL;
476 device->open = rt_sensor_open;
477 device->close = rt_sensor_close;
478 device->read = rt_sensor_read;
479 device->write = RT_NULL;
480 device->control = rt_sensor_control;
481#endif
482 device->type = RT_Device_Class_Sensor;
483 device->rx_indicate = RT_NULL;
484 device->tx_complete = RT_NULL;
485 device->user_data = data;
486
487 result = rt_device_register(device, device_name, flag | RT_DEVICE_FLAG_STANDALONE);
488 if (result != RT_EOK)

Callers

nothing calls this directly

Calls 6

rt_callocFunction · 0.85
rt_strlenFunction · 0.85
rt_memcpyFunction · 0.85
rt_mutex_createFunction · 0.85
rt_freeFunction · 0.85
rt_device_registerFunction · 0.85

Tested by

no test coverage detected