You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ac...@apache.org on 2022/07/23 19:58:07 UTC
[incubator-nuttx-apps] 04/20: apps/system/uorb: support orb_get_meta by name string
This is an automated email from the ASF dual-hosted git repository.
acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git
commit 4ec3f70f1d1685e405dd12d28054f5f94c44796b
Author: Jiuzhu Dong <do...@xiaomi.com>
AuthorDate: Fri May 6 13:18:56 2022 +0800
apps/system/uorb: support orb_get_meta by name string
Signed-off-by: Jiuzhu Dong <do...@xiaomi.com>
---
system/uorb/Makefile | 1 +
system/uorb/sensor/topics.c | 164 ++++++++++++++++++++++++++++++++++++++++++++
system/uorb/uORB/uORB.h | 15 ++++
3 files changed, 180 insertions(+)
diff --git a/system/uorb/Makefile b/system/uorb/Makefile
index 7194e0db4..feb09b7da 100644
--- a/system/uorb/Makefile
+++ b/system/uorb/Makefile
@@ -21,6 +21,7 @@
include $(APPDIR)/Make.defs
CSRCS += uORB/uORB.c
+CSRCS += $(wildcard sensor/*.c)
ifneq ($(CONFIG_UORB_TESTS),)
CSRCS += test/utility.c
diff --git a/system/uorb/sensor/topics.c b/system/uorb/sensor/topics.c
new file mode 100644
index 000000000..b291170de
--- /dev/null
+++ b/system/uorb/sensor/topics.c
@@ -0,0 +1,164 @@
+/****************************************************************************
+ * apps/system/uorb/sensor/topics.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <sys/ioctl.h>
+
+#include <sensor/accel.h>
+#include <sensor/baro.h>
+#include <sensor/cap.h>
+#include <sensor/co2.h>
+#include <sensor/dust.h>
+#include <sensor/ecg.h>
+#include <sensor/gps.h>
+#include <sensor/gyro.h>
+#include <sensor/gesture.h>
+#include <sensor/hall.h>
+#include <sensor/hbeat.h>
+#include <sensor/hcho.h>
+#include <sensor/hrate.h>
+#include <sensor/humi.h>
+#include <sensor/impd.h>
+#include <sensor/ir.h>
+#include <sensor/light.h>
+#include <sensor/mag.h>
+#include <sensor/noise.h>
+#include <sensor/ots.h>
+#include <sensor/ph.h>
+#include <sensor/pm25.h>
+#include <sensor/pm1p0.h>
+#include <sensor/pm10.h>
+#include <sensor/ppgd.h>
+#include <sensor/ppgq.h>
+#include <sensor/prox.h>
+#include <sensor/rgb.h>
+#include <sensor/temp.h>
+#include <sensor/tvoc.h>
+#include <sensor/uv.h>
+
+#include <uORB/uORB.h>
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static FAR const struct orb_metadata *g_sensor_list[] =
+{
+ ORB_ID(sensor_accel),
+ ORB_ID(sensor_accel_uncal),
+ ORB_ID(sensor_baro),
+ ORB_ID(sensor_cap),
+ ORB_ID(sensor_co2),
+ ORB_ID(sensor_dust),
+ ORB_ID(sensor_ecg),
+ ORB_ID(sensor_gps),
+ ORB_ID(sensor_gps_satellite),
+ ORB_ID(sensor_gyro),
+ ORB_ID(sensor_gyro_uncal),
+ ORB_ID(sensor_hall),
+ ORB_ID(sensor_hbeat),
+ ORB_ID(sensor_hcho),
+ ORB_ID(sensor_humi),
+ ORB_ID(sensor_hrate),
+ ORB_ID(sensor_impd),
+ ORB_ID(sensor_ir),
+ ORB_ID(sensor_light),
+ ORB_ID(sensor_light_uncal),
+ ORB_ID(sensor_mag),
+ ORB_ID(sensor_mag_uncal),
+ ORB_ID(sensor_noise),
+ ORB_ID(sensor_ots),
+ ORB_ID(sensor_ph),
+ ORB_ID(sensor_pm10),
+ ORB_ID(sensor_pm1p0),
+ ORB_ID(sensor_pm25),
+ ORB_ID(sensor_ppgd),
+ ORB_ID(sensor_ppgq),
+ ORB_ID(sensor_prox),
+ ORB_ID(sensor_rgb),
+ ORB_ID(sensor_temp),
+ ORB_ID(sensor_tvoc),
+ ORB_ID(sensor_uv),
+ ORB_ID(sensor_wake_gesture),
+ ORB_ID(sensor_wake_gesture_uncal),
+ NULL,
+};
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+FAR const struct orb_metadata *orb_get_meta(FAR const char *name)
+{
+ struct sensor_state_s state;
+ char path[PATH_MAX];
+ int idx = -1;
+ int ret;
+ int fd;
+ int i;
+
+ /* Fisrt search built-in topics */
+
+ for (i = 0; g_sensor_list[i]; i++)
+ {
+ if (!strncmp(g_sensor_list[i]->o_name, name,
+ strlen(g_sensor_list[i]->o_name)))
+ {
+ if (idx == -1 || strlen(g_sensor_list[idx]->o_name) <
+ strlen(g_sensor_list[i]->o_name))
+ {
+ idx = i;
+ }
+ }
+ }
+
+ if (idx != -1)
+ {
+ return g_sensor_list[idx];
+ }
+
+ /* Then open node to get meta */
+
+ snprintf(path, PATH_MAX, ORB_SENSOR_PATH"%s", name);
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ {
+ snprintf(path, PATH_MAX, ORB_SENSOR_PATH"%s%d", name, 0);
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ {
+ return NULL;
+ }
+ }
+
+ ret = ioctl(fd, SNIOC_GET_STATE, (unsigned long)(uintptr_t)&state);
+ close(fd);
+ if (ret < 0)
+ {
+ return NULL;
+ }
+
+ return state.priv;
+}
diff --git a/system/uorb/uORB/uORB.h b/system/uorb/uORB/uORB.h
index f31ba7063..77e424319 100644
--- a/system/uorb/uORB/uORB.h
+++ b/system/uorb/uORB/uORB.h
@@ -638,6 +638,21 @@ int orb_exists(FAR const struct orb_metadata *meta, int instance);
int orb_group_count(FAR const struct orb_metadata *meta);
+/****************************************************************************
+ * Name: orb_get_meta
+ *
+ * Description:
+ * Get the metadata of topic object by name string.
+ *
+ * Input Parameters:
+ * name The name of topic, ex: sensor_accel, sensor_accel0.
+ *
+ * Returned Value:
+ * The metadata on success. NULL on failure.
+ ****************************************************************************/
+
+FAR const struct orb_metadata *orb_get_meta(FAR const char *name);
+
#ifdef __cplusplus
}
#endif