Dawn Framework 1.0
Universal data acquisition framework for embedded systems
gnss.cxx
1// dawn/src/io/gnss.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/io/gnss.hxx"
7
8#include <cstdint>
9#include <errno.h>
10
11#include <nuttx/uorb.h>
12
13#include "dawn/debug.hxx"
14#include "dawn/porting/sensors.hxx"
15
16using namespace dawn;
17
18int CIOSensorGnss::validateDtype()
19{
20 // GNSS variants are float (position/info), uint64 (time) or uint32
21 // (satellites). Accept those and record the element size.
22
23 switch (getDtype())
24 {
29 return OK;
30 default:
31 DAWNERR("GNSS IO unsupported dtype %d\n", getDtype());
32 return -EINVAL;
33 }
34}
35
37{
38 struct sensor_gnss gnss;
39 int ret;
40
41 // No batch supported
42
43 if (len != 1)
44 {
45 return -ENOTSUP;
46 }
47
48 // Read the full GNSS event from the device
49
50 ret = sensor_read(fd, &gnss, sizeof(gnss));
51 if (ret < 0)
52 {
53 DAWNERR("sensor_read failed %d\n", ret);
54 return ret;
55 }
56
57 // Assemble the fields for this variant (non-adjacent struct members, with
58 // type conversion for time/satellites).
59
60 switch (getCls())
61 {
63 {
64 // Position + velocity
65 float *o = static_cast<float *>(data.getDataPtr());
66 o[0] = gnss.latitude;
67 o[1] = gnss.longitude;
68 o[2] = gnss.altitude;
69 o[3] = gnss.ground_speed;
70 o[4] = gnss.course;
71 }
72 break;
73
75 // time_utc is already UTC seconds since the epoch (the nRF91 driver
76 // converts the GNSS calendar time with timegm()); LwM2M Time wants
77 // seconds, so pass it through.
78 *static_cast<uint64_t *>(data.getDataPtr()) = gnss.time_utc;
79 break;
80
82 {
83 // Accuracy + dilution of precision
84 float *o = static_cast<float *>(data.getDataPtr());
85 o[0] = gnss.eph;
86 o[1] = gnss.epv;
87 o[2] = gnss.hdop;
88 o[3] = gnss.pdop;
89 o[4] = gnss.vdop;
90 }
91 break;
92
94 *static_cast<uint32_t *>(data.getDataPtr()) = gnss.satellites_used;
95 break;
96
97 default:
98 return -EINVAL;
99 }
100
101#ifdef CONFIG_DAWN_IO_TIMESTAMP
102 if (isTimestamp())
103 {
104 data.getTs() = static_cast<io_ts_t>(gnss.timestamp);
105 }
106#endif
107
108 return OK;
109}
bool isTimestamp() const
Check if I/O supports timestamp.
Definition common.cxx:189
@ IO_CLASS_SENSOR_GNSS_TIME
GNSS UTC time (seconds since epoch)
Definition common.hxx:148
@ IO_CLASS_SENSOR_GNSS_INFO
GNSS accuracy + DOP (eph/epv/hdop/pdop/vdop)
Definition common.hxx:149
@ IO_CLASS_SENSOR_GNSS
GNSS position+velocity (lat/lon/alt/speed/course)
Definition common.hxx:147
@ IO_CLASS_SENSOR_GNSS_SATELLITES
GNSS satellites used in fix.
Definition common.hxx:150
int getDataImpl(IODataCmn &data, size_t len)
Get data implementation (override in derived classes).
Definition gnss.cxx:36
int fd
File descriptor for sensor device.
Definition sensor.hxx:265
size_t dsize
Sensor data size in bytes.
Definition sensor.hxx:261
uint16_t getCls() const
Get object class field.
Definition object.cxx:170
uint8_t getDtype() const
Get data type field.
Definition object.cxx:175
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
uint64_t io_ts_t
Timestamp data type (uint64_t, typically microseconds since boot).
Definition idata.hxx:16
Base interface for I/O data buffers (static and dynamic).
Definition idata.hxx:21
virtual void * getDataPtr(size_t batch=0)=0
Get pointer to data only (skips timestamp if present).
virtual uint64_t & getTs(size_t batch=0)=0
Get timestamp reference for batch.
EObjectDataType
Data types supported by Dawn framework.
Definition objectid.hxx:61
@ DTYPE_FLOAT
IEEE 754 single-precision floating point (32-bit).
Definition objectid.hxx:112
@ DTYPE_UINT64
Unsigned 64-bit integer.
Definition objectid.hxx:104
@ DTYPE_UINT32
Unsigned 32-bit integer (0 to 4294967295).
Definition objectid.hxx:96
static int getDtypeSize_(const EObjectDataType dtype)
Get byte size for a specific data type.
Definition objectid.cxx:12