aboutsummaryrefslogtreecommitdiffstats
path: root/dynamic-layers/openembedded-layer/recipes-navigation/gpsd/gpsd/0001-Introduce-Qualcomm-PDS-service-support.patch
blob: 9709932a026563fb3afbce8968e4d4e317dfc77d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
From 3f46e63ff08afba0ad532d4cac1957499769dc69 Mon Sep 17 00:00:00 2001
From: Bjorn Andersson <bjorn.andersson@linaro.org>
Date: Wed, 4 Apr 2018 04:29:09 +0000
Subject: [PATCH] Introduce Qualcomm PDS service support
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The Qualcomm PDS service provides location data on a wide range of
Qualcomm platforms. It used QMI encoded messages sent over a shared
memory link, implemented in Linux as AF_QIPCRTR.

A special service is available on port -2 on the local node in the
network, which provides functionality to the node address and port of
registered services by id. As the driver is opened this mechanism is
used to search for a registered PDS service in the system.

As the PDS driver is activated two messages are sent to the PDS service,
the first one configures which events the service will send to the
client (in our case NMEA reports) and the second starts the transmission
of these packets. Similarly when the driver is deactivated a stop
request is sent to the service.

Between the start and stop request the PDS service will send NMEA
messages to the PDS client at a rate of 1 Hz, the NMEA string is
extracted from the QMI encoded message and handed to the nmea_parse()
function.

The PDS driver is selected by the url pds://<host>, where host is either
a numerical identifier of the node in the AF_QIPCRTR network or the
string "any".

Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
Signed-off-by: Aníbal Limón <anibal.limon@linaro.org>
---
 SConstruct     |  10 ++
 driver_pds.c   | 325 +++++++++++++++++++++++++++++++++++++++++++++++++
 driver_pds.h   |  20 +++
 drivers.c      |   5 +
 gpsd.h         |   2 +
 libgpsd_core.c |  15 ++-
 6 files changed, 376 insertions(+), 1 deletion(-)
 create mode 100644 driver_pds.c
 create mode 100644 driver_pds.h

diff --git a/SConstruct b/SConstruct
index 33e0ff326..d4ae81979 100644
--- a/SConstruct
+++ b/SConstruct
@@ -192,6 +192,7 @@ boolopts = (
     ("tripmate",      True,  "DeLorme TripMate support"),
     ("tsip",          True,  "Trimble TSIP support"),
     ("ublox",         True,  "u-blox Protocol support"),
+    ("pds",           True,  "Qualcomm PDS support"),
     # Non-GPS protocols
     ("aivdm",         True,  "AIVDM support"),
     ("gpsclock",      True,  "GPSClock support"),
@@ -977,6 +978,14 @@ else:
         announce("You do not have kernel CANbus available.")
         config.env["nmea2000"] = False
 
+    if config.CheckHeader(["bits/sockaddr.h", "linux/qrtr.h"]):
+        confdefs.append("#define HAVE_LINUX_QRTR_H 1\n")
+        announce("You have kernel QRTR available.")
+    else:
+        confdefs.append("/* #undef HAVE_LINUX_QRTR_H */\n")
+        announce("You do not have kernel QRTR available.")
+        env["pds"] = False
+
     # check for C11 or better, and __STDC__NO_ATOMICS__ is not defined
     # before looking for stdatomic.h
     if ((config.CheckC11() and
@@ -1407,6 +1416,7 @@ libgpsd_sources = [
     "driver_nmea0183.c",
     "driver_nmea2000.c",
     "driver_oncore.c",
+    "driver_pds.c",
     "driver_rtcm2.c",
     "driver_rtcm3.c",
     "drivers.c",
diff --git a/driver_pds.c b/driver_pds.c
new file mode 100644
index 000000000..734b40f83
--- /dev/null
+++ b/driver_pds.c
@@ -0,0 +1,325 @@
+#include <sys/socket.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include "gpsd.h"
+#include "libgps.h"
+
+#if defined(PDS_ENABLE)
+#include "driver_pds.h"
+
+#include <linux/qrtr.h>
+
+#define QMI_PDS_SERVICE_ID	0x10
+#define QMI_PDS_VERSION		0x2
+
+struct qmi_header {
+	uint8_t type;
+	uint16_t txn;
+	uint16_t msg;
+	uint16_t len;
+} __attribute__((__packed__));
+
+struct qmi_tlv {
+	uint8_t key;
+	uint16_t len;
+	uint8_t value[];
+} __attribute__((__packed__));
+
+#define QMI_REQUEST			0
+#define QMI_INDICATION			4
+
+#define QMI_LOC_REG_EVENTS		0x21
+#define QMI_TLV_EVENT_MASK		1
+#define QMI_EVENT_MASK_NMEA		4
+
+#define QMI_LOC_START			0x22
+#define QMI_LOC_STOP			0x23
+#define QMI_TLV_SESSION_ID		1
+
+#define QMI_LOC_EVENT_NMEA		0x26
+#define QMI_TLV_NMEA			1
+
+static ssize_t qmi_pds_get(struct gps_device_t *session)
+{
+	struct sockaddr_qrtr sq;
+	socklen_t sl = sizeof(sq);
+	struct qmi_header *hdr;
+	struct qmi_tlv *tlv;
+	size_t buflen = sizeof(session->lexer.inbuffer);
+	size_t offset;
+	void *buf = session->lexer.inbuffer;
+	int ret;
+
+	ret = recvfrom(session->gpsdata.gps_fd, buf, buflen, 0,
+		       (struct sockaddr *)&sq, &sl);
+	if (ret < 0 && errno == EAGAIN) {
+		session->lexer.outbuflen = 0;
+		return 1;
+	} else if (ret < 0) {
+		GPSD_LOG(LOG_ERROR, &session->context->errout,
+			 "QRTR get: Unable to receive packet.\n");
+		return -1;
+	}
+
+	/* TODO: Validate sq to be our peer */
+
+	session->lexer.type = QMI_PDS_PACKET;
+
+	hdr = buf;
+	if (hdr->type != QMI_INDICATION ||
+	    hdr->msg != QMI_LOC_EVENT_NMEA) {
+		session->lexer.outbuflen = 0;
+		return ret;
+	}
+
+	offset = sizeof(*hdr);
+	while (offset < (size_t)ret) {
+		tlv = (struct qmi_tlv *)((char*)buf + offset);
+
+		if (offset + sizeof(*tlv) + tlv->len > (size_t)ret)
+			break;
+
+		if (tlv->key == QMI_TLV_NMEA) {
+			memcpy(session->lexer.outbuffer, tlv->value, tlv->len);
+			session->lexer.outbuffer[tlv->len] = 0;
+			session->lexer.outbuflen = tlv->len;
+			break;
+		}
+
+		offset += tlv->len;
+	}
+
+	return ret;
+}
+
+static gps_mask_t qmi_pds_parse_input(struct gps_device_t *session)
+{
+	return nmea_parse((char *)session->lexer.outbuffer, session);
+}
+
+static void qmi_pds_event_hook(struct gps_device_t *session, event_t event)
+{
+	struct qmi_header *hdr;
+	struct qmi_tlv *tlv;
+	static int txn_id;
+	char buf[128];
+	char *ptr;
+	int sock = session->gpsdata.gps_fd;
+	int ret;
+
+	switch (event) {
+	case event_deactivate:
+		ptr = buf;
+		hdr = (struct qmi_header *)ptr;
+		hdr->type = QMI_REQUEST;
+		hdr->txn = txn_id++;
+		hdr->msg = QMI_LOC_STOP;
+		hdr->len = sizeof(*tlv) + sizeof(uint8_t);
+		ptr += sizeof(*hdr);
+
+		tlv = (struct qmi_tlv *)ptr;
+		tlv->key = QMI_TLV_SESSION_ID;
+		tlv->len = sizeof(uint8_t);
+		*(uint8_t*)tlv->value = 1;
+		ptr += sizeof(*tlv) + sizeof(uint8_t);
+
+		ret = send(sock, buf, ptr - buf, 0);
+		if (ret < 0) {
+			GPSD_LOG(LOG_ERROR, &session->context->errout,
+				 "QRTR event_hook: failed to send STOP request.\n");
+			return;
+		}
+		break;
+	case event_reactivate:
+		ptr = buf;
+		hdr = (struct qmi_header *)ptr;
+		hdr->type = QMI_REQUEST;
+		hdr->txn = txn_id++;
+		hdr->msg = QMI_LOC_REG_EVENTS;
+		hdr->len = sizeof(*tlv) + sizeof(uint64_t);
+		ptr += sizeof(*hdr);
+
+		tlv = (struct qmi_tlv *)ptr;
+		tlv->key = QMI_TLV_EVENT_MASK;
+		tlv->len = sizeof(uint64_t);
+		*(uint64_t*)tlv->value = QMI_EVENT_MASK_NMEA;
+		ptr += sizeof(*tlv) + sizeof(uint64_t);
+
+		ret = send(sock, buf, ptr - buf, 0);
+		if (ret < 0) {
+			GPSD_LOG(LOG_ERROR, &session->context->errout,
+				 "QRTR event_hook: failed to send REG_EVENTS request.\n");
+			return;
+		}
+
+		ptr = buf;
+		hdr = (struct qmi_header *)ptr;
+		hdr->type = QMI_REQUEST;
+		hdr->txn = txn_id++;
+		hdr->msg = QMI_LOC_START;
+		hdr->len = sizeof(*tlv) + sizeof(uint8_t);
+		ptr += sizeof(*hdr);
+
+		tlv = (struct qmi_tlv *)(buf + sizeof(*hdr));
+		tlv->key = QMI_TLV_SESSION_ID;
+		tlv->len = sizeof(uint8_t);
+		*(uint8_t*)tlv->value = 1;
+		ptr += sizeof(*tlv) + sizeof(uint8_t);
+
+		ret = send(sock, buf, ptr - buf, 0);
+		if (ret < 0) {
+			GPSD_LOG(LOG_ERROR, &session->context->errout,
+				 "QRTR event_hook: failed to send START request.\n");
+			return;
+		}
+		break;
+	default:
+		break;
+	}
+}
+
+int qmi_pds_open(struct gps_device_t *session)
+{
+	struct sockaddr_qrtr sq_ctrl;
+	struct qrtr_ctrl_pkt pkt;
+	struct sockaddr_qrtr sq;
+	unsigned int pds_node = 0;
+	unsigned int pds_port = 0;
+	socklen_t sl = sizeof(sq_ctrl);
+	char *hostname;
+	char *endptr;
+	int hostid;
+	int flags;
+	int sock;
+	int ret;
+
+	hostname = session->gpsdata.dev.path + 6;
+	if (!strcmp(hostname, "any")) {
+		hostid = -1;
+	} else {
+		hostid = (int)strtol(hostname, &endptr, 10);
+		if (endptr == hostname) {
+			GPSD_LOG(LOG_ERROR, &session->context->errout,
+				 "QRTR open: Invalid node id.\n");
+			return -1;
+		}
+	}
+
+
+	sock = socket(AF_QIPCRTR, SOCK_DGRAM, 0);
+	if (sock < 0) {
+	        GPSD_LOG(LOG_ERROR, &session->context->errout,
+			 "QRTR open: Unable to get QRTR socket.\n");
+		return -1;
+	}
+
+	ret = getsockname(sock, (struct sockaddr *)&sq_ctrl, &sl);
+	if (ret < 0 || sq_ctrl.sq_family != AF_QIPCRTR || sl != sizeof(sq_ctrl)) {
+	        GPSD_LOG(LOG_ERROR, &session->context->errout,
+			 "QRTR open: Unable to acquire local address.\n");
+		close(sock);
+		return -1;
+	}
+
+	memset(&pkt, 0, sizeof(pkt));
+	pkt.cmd = QRTR_TYPE_NEW_LOOKUP;
+	pkt.server.service = QMI_PDS_SERVICE_ID;
+	pkt.server.instance = QMI_PDS_VERSION;
+
+	sq_ctrl.sq_port = QRTR_PORT_CTRL;
+	ret = sendto(sock, &pkt, sizeof(pkt), 0, (struct sockaddr *)&sq_ctrl, sizeof(sq_ctrl));
+	if (ret < 0) {
+	        GPSD_LOG(LOG_ERROR, &session->context->errout,
+			 "QRTR open: Unable to send lookup request.\n");
+		close(sock);
+		return -1;
+	}
+
+	for (;;) {
+		sl = sizeof(sq);
+
+		ret = recvfrom(sock, &pkt, sizeof(pkt), 0, (struct sockaddr *)&sq, &sl);
+		if (ret < 0) {
+			GPSD_LOG(LOG_ERROR, &session->context->errout,
+				 "QRTR open: Unable to receive lookup request.\n");
+			close(sock);
+			return -1;
+		}
+
+		if (sl != sizeof(sq) || sq.sq_node != sq_ctrl.sq_node ||
+		    sq.sq_port != sq_ctrl.sq_port) {
+			GPSD_LOG(LOG_ERROR, &session->context->errout,
+				 "QRTR open: Received message is not ctrl message, ignoring.\n");
+			continue;
+		}
+
+		if (pkt.cmd != QRTR_TYPE_NEW_SERVER)
+			continue;
+
+		/* All fields zero indicates end of lookup response */
+		if (!pkt.server.service && !pkt.server.instance &&
+		    !pkt.server.node && !pkt.server.port)
+			break;
+
+		/* Filter results based on specified node */
+		if (hostid != -1 && hostid != (int)pkt.server.node)
+			continue;
+
+		pds_node = pkt.server.node;
+		pds_port = pkt.server.port;
+	}
+
+	if (!pds_node && !pds_port) {
+		GPSD_LOG(LOG_ERROR, &session->context->errout,
+			 "QRTR open: No PDS service found.\n");
+		close(sock);
+		return -1;
+	}
+
+	flags = fcntl(sock, F_GETFL, 0);
+	flags |= O_NONBLOCK;
+	fcntl(sock, F_SETFL, flags);
+
+	GPSD_LOG(LOG_INF, &session->context->errout,
+		 "QRTR open: Found PDS at %d %d.\n", pds_node, pds_port);
+
+	sq.sq_family = AF_QIPCRTR;
+	sq.sq_node = pds_node;
+	sq.sq_port = pds_port;
+	ret = connect(sock, (struct sockaddr *)&sq, sizeof(sq));
+	if (ret < 0) {
+		GPSD_LOG(LOG_ERROR, &session->context->errout,
+			 "QRTR open: Failed to connect socket.\n");
+		close(sock);
+		return -1;
+	}
+
+	gpsd_switch_driver(session, "Qualcomm PDS");
+	session->gpsdata.gps_fd = sock;
+	session->sourcetype = source_qrtr;
+	session->servicetype = service_sensor;
+
+	return session->gpsdata.gps_fd;
+}
+
+void qmi_pds_close(struct gps_device_t *session)
+{
+	if (!BAD_SOCKET(session->gpsdata.gps_fd)) {
+		close(session->gpsdata.gps_fd);
+		INVALIDATE_SOCKET(session->gpsdata.gps_fd);
+	}
+}
+
+const struct gps_type_t driver_pds = {
+    .type_name      = "Qualcomm PDS",       /* full name of type */
+    .packet_type    = QMI_PDS_PACKET,	/* associated lexer packet type */
+    .flags	    = DRIVER_STICKY,	/* remember this */
+    .channels       = 12,		/* not an actual GPS at all */
+    .get_packet     = qmi_pds_get,	/* how to get a packet */
+    .parse_packet   = qmi_pds_parse_input,	/* how to interpret a packet */
+    .event_hook	    = qmi_pds_event_hook,
+};
+
+#endif /* of defined(PDS_ENABLE) */
diff --git a/driver_pds.h b/driver_pds.h
new file mode 100644
index 000000000..3b373743d
--- /dev/null
+++ b/driver_pds.h
@@ -0,0 +1,20 @@
+/*
+ * PDS on QRTR.
+ *
+ * The entry points for driver_pds
+ *
+ * This file is Copyright (c) 2018 by the GPSD project
+ * SPDX-License-Identifier: BSD-2-clause
+ */
+
+#ifndef _DRIVER_PDS_H_
+#define _DRIVER_PDS_H_
+
+#if defined(PDS_ENABLE)
+
+int qmi_pds_open(struct gps_device_t *session);
+
+void qmi_pds_close(struct gps_device_t *session);
+
+#endif /* of defined(PDS_ENABLE) */
+#endif /* of ifndef _DRIVER_PDS_H_ */
diff --git a/drivers.c b/drivers.c
index 39e3d0a4c..7d788e1ab 100644
--- a/drivers.c
+++ b/drivers.c
@@ -1747,6 +1747,7 @@ extern const struct gps_type_t driver_greis;
 extern const struct gps_type_t driver_italk;
 extern const struct gps_type_t driver_navcom;
 extern const struct gps_type_t driver_nmea2000;
+extern const struct gps_type_t driver_pds;
 extern const struct gps_type_t driver_oncore;
 extern const struct gps_type_t driver_sirf;
 extern const struct gps_type_t driver_skytraq;
@@ -1844,6 +1845,10 @@ static const struct gps_type_t *gpsd_driver_array[] = {
     &driver_nmea2000,
 #endif /* NMEA2000_ENABLE */
 
+#ifdef PDS_ENABLE
+    &driver_pds,
+#endif /* PDS_ENABLE */
+
 #ifdef RTCM104V2_ENABLE
     &driver_rtcm104v2,
 #endif /* RTCM104V2_ENABLE */
diff --git a/gpsd.h b/gpsd.h
index 57e4b0553..e9f61cc71 100644
--- a/gpsd.h
+++ b/gpsd.h
@@ -209,6 +209,7 @@ struct gps_lexer_t {
 #define GEOSTAR_PACKET   	14
 #define NMEA2000_PACKET 	15
 #define GREIS_PACKET		16
+#define QMI_PDS_PACKET		16
 #define MAX_GPSPACKET_TYPE	16	/* increment this as necessary */
 #define RTCM2_PACKET    	17
 #define RTCM3_PACKET    	18
@@ -447,6 +448,7 @@ typedef enum {source_unknown,
 	      source_usb,	/* potential GPS source, discoverable */
 	      source_bluetooth,	/* potential GPS source, discoverable */
 	      source_can,	/* potential GPS source, fixed CAN format */
+	      source_qrtr,	/* potential GPS source, discoverable */
 	      source_pty,	/* PTY: we don't require exclusive access */
 	      source_tcp,	/* TCP/IP stream: case detected but not used */
 	      source_udp,	/* UDP stream: case detected but not used */
diff --git a/libgpsd_core.c b/libgpsd_core.c
index 52bf8e5ae..a8a2ec0d3 100644
--- a/libgpsd_core.c
+++ b/libgpsd_core.c
@@ -39,6 +39,9 @@
 #if defined(NMEA2000_ENABLE)
 #include "driver_nmea2000.h"
 #endif /* defined(NMEA2000_ENABLE) */
+#if defined(PDS_ENABLE)
+#include "driver_pds.h"
+#endif /* defined(PDS_ENABLE) */
 
 ssize_t gpsd_write(struct gps_device_t *session,
 		   const char *buf,
@@ -351,6 +354,11 @@ void gpsd_deactivate(struct gps_device_t *session)
         (void)nmea2000_close(session);
     else
 #endif /* of defined(NMEA2000_ENABLE) */
+#if defined(PDS_ENABLE)
+    if (session->sourcetype == source_qrtr)
+	(void)qmi_pds_close(session);
+    else
+#endif /* of defined(PDS_ENABLE) */
         (void)gpsd_close(session);
     if (session->mode == O_OPTIMIZE)
 	gpsd_run_device_hook(&session->context->errout,
@@ -553,6 +561,11 @@ int gpsd_open(struct gps_device_t *session)
         return nmea2000_open(session);
     }
 #endif /* defined(NMEA2000_ENABLE) */
+#if defined(PDS_ENABLE)
+    if (str_starts_with(session->gpsdata.dev.path, "pds://")) {
+	    return qmi_pds_open(session);
+    }
+#endif /* defined(PDS_ENABLE) */
     /* fall through to plain serial open */
     /* could be a naked /dev/ppsX */
     return gpsd_serial_open(session);
@@ -581,7 +594,7 @@ int gpsd_activate(struct gps_device_t *session, const int mode)
 #ifdef NON_NMEA0183_ENABLE
     /* if it's a sensor, it must be probed */
     if ((session->servicetype == service_sensor) &&
-	(session->sourcetype != source_can)) {
+	(session->sourcetype != source_can && session->sourcetype != source_qrtr)) {
 	const struct gps_type_t **dp;
 
 	for (dp = gpsd_drivers; *dp; dp++) {
-- 
2.27.0.rc0