/
gnss_pvtlog_main.c
796 lines (666 loc) · 18.4 KB
/
gnss_pvtlog_main.c
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
/****************************************************************************
* gnss_pvtlog/gnss_pvtlog_main.c
*
* Copyright 2018 Sony Semiconductor Solutions Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of Sony Semiconductor Solutions Corporation nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <sdk/config.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <arch/chip/gnss.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define MY_GNSS_SIG0 18
#define MY_GNSS_SIG1 19
#define TEST_LOOP_TIME 600
#define TEST_RECORDING_CYCLE 1
#define TEST_NOTIFY_THRESHOLD CXD56_GNSS_PVTLOG_THRESHOLD_HALF
#define FILE_NAME_LEN 256
#if(TEST_NOTIFY_THRESHOLD == CXD56_GNSS_PVTLOG_THRESHOLD_HALF)
# define PVTLOG_UNITNUM (CXD56_GNSS_PVTLOG_MAXNUM/2)
#else
# define PVTLOG_UNITNUM (CXD56_GNSS_PVTLOG_MAXNUM)
#endif
#define TEST_FILE_COUNT (1 + (int)(TEST_LOOP_TIME / PVTLOG_UNITNUM))
/****************************************************************************
* Private Types
****************************************************************************/
struct cxd56_gnss_dms_s
{
int8_t sign;
uint8_t degree;
uint8_t minute;
uint32_t frac;
};
/****************************************************************************
* Private Data
****************************************************************************/
static struct cxd56_gnss_positiondata_s posdat;
static struct cxd56_pvtlog_s pvtlogdat;
/****************************************************************************
* Name: double_to_dmf()
*
* Description:
* Convert from double format to degree-minute-frac format.
*
* Input Parameters:
* x - double value.
* dmf - Address to store the conversion result.
*
* Returned Value:
* none.
*
* Assumptions/Limitations:
* none.
*
****************************************************************************/
static void double_to_dmf(double x, struct cxd56_gnss_dms_s * dmf)
{
int b;
int d;
int m;
double f;
double t;
if (x < 0)
{
b = 1;
x = -x;
}
else
{
b = 0;
}
d = (int)x; /* = floor(x), x is always positive */
t = (x - d) * 60;
m = (int)t; /* = floor(t), t is always positive */
f = (t - m) * 10000;
dmf->sign = b;
dmf->degree = d;
dmf->minute = m;
dmf->frac = f;
}
/****************************************************************************
* Name: read_and_print()
*
* Description:
* Read and print POS and PVTLOG data.
*
* Input Parameters:
* fd - File descriptor.
*
* Returned Value:
* Zero (OK) on success; Negative value on error.
*
* Assumptions/Limitations:
* none.
*
****************************************************************************/
static int read_and_print(int fd)
{
int ret;
struct cxd56_gnss_dms_s dmf;
struct cxd56_pvtlog_status_s pvtlog_status;
/* Seek data */
ret = lseek(fd, CXD56_GNSS_READ_OFFSET_LAST_GNSS, SEEK_SET);
if (ret < 0)
{
ret = errno;
printf("lseek error %d\n", ret);
goto _err1;
}
/* Read POS data */
ret = read(fd, &posdat, sizeof(posdat));
if (ret < 0)
{
ret = errno;
printf("read error %d\n", ret);
goto _err1;
}
else if (ret < sizeof(posdat))
{
ret = ERROR;
printf("read size error %d\n", ret);
goto _err1;
}
else
{
ret = OK;
}
if (posdat.receiver.pos_dataexist &&
(posdat.receiver.pos_fixmode != CXD56_GNSS_PVT_POSFIX_INVALID))
{
/* Print POS data */
printf(" Y=%d, M=%2d, d=%2d", posdat.receiver.date.year,
posdat.receiver.date.month, posdat.receiver.date.day);
printf(" h=%2d, m=%2d, s=%2d m=%3d", posdat.receiver.time.hour,
posdat.receiver.time.minute, posdat.receiver.time.sec,
(int)(posdat.receiver.time.usec/1000));
double_to_dmf(posdat.receiver.latitude, &dmf);
printf(", Lat %d:%d:%d",
dmf.degree, dmf.minute, dmf.frac);
double_to_dmf(posdat.receiver.longitude, &dmf);
printf(" , Lon %d:%d:%d",
dmf.degree, dmf.minute, dmf.frac);
/* Get Log status */
ret = ioctl(fd, CXD56_GNSS_IOCTL_PVTLOG_GET_STATUS,
(unsigned long)&pvtlog_status);
printf(", Log No:%d \n", pvtlog_status.status.log_count);
}
else
{
printf("No Positioning...\n");
}
_err1:
return ret;
}
/****************************************************************************
* Name: get_pvtlog()
*
* Description:
* Read PVTLOG data.
*
* Input Parameters:
* fd - File descriptor.
*
* Returned Value:
* Zero (OK) on success; Negative value on error.
*
* Assumptions/Limitations:
* none.
*
****************************************************************************/
static int get_pvtlog(int fd)
{
int ret;
/* Seek. */
ret = lseek(fd, CXD56_GNSS_READ_OFFSET_PVTLOG, SEEK_SET);
if (ret < 0)
{
ret = errno;
printf("lseek error %d\n", ret);
goto _err1;
}
/* Read PVTLOG. */
ret = read(fd, &pvtlogdat, sizeof(pvtlogdat));
if (ret < 0)
{
ret = errno;
printf("read error %d\n", ret);
goto _err1;
}
else if (ret == 0)
{
printf("read data size is 0 \n");
goto _err1;
}
else
{
ret = OK;
}
_err1:
return ret;
}
/****************************************************************************
* Name: set_signal()
*
* Description:
* Call CXD56_GNSS_IOCTL_SIGNAL_SET.
*
* Input Parameters:
* fd - File descriptor.
* signo - signal number
* gnsssig - GNSS ID
* enable - set enable or disable
* mask - signal mask
*
* Returned Value:
* Zero (OK) on success; Negative value on error.
*
* Assumptions/Limitations:
* none.
*
****************************************************************************/
static int set_signal(int fd, int signo, uint8_t gnsssig, int enable,
sigset_t * mask)
{
int ret;
struct cxd56_gnss_signal_setting_s setting;
sigaddset(mask, signo);
ret = sigprocmask(SIG_BLOCK, mask, NULL);
if (ret != OK)
{
printf("sigprocmask failed. %d\n", ret);
goto _err1;
}
setting.fd = fd;
setting.enable = enable;
setting.gnsssig = gnsssig;
setting.signo = signo;
setting.data = NULL;
ret = ioctl(fd, CXD56_GNSS_IOCTL_SIGNAL_SET, (unsigned long)&setting);
_err1:
return ret;
}
/****************************************************************************
* Name: writefile()
*
* Description:
* Write PVTLOG data.
*
* Input Parameters:
* file_count - Write file count.
*
* Returned Value:
* Zero (OK) on success; Negative value on error.
*
* Assumptions/Limitations:
* Write buffer refers to global variable "pvtlogdat".
*
****************************************************************************/
static int writefile(uint32_t file_count)
{
int fd_write;
int ret = OK;
char filename[FILE_NAME_LEN];
/* Make file name */
snprintf(filename, FILE_NAME_LEN, "%s%d.dat",
CONFIG_EXAMPLES_PVTLOG_FILEPATH, file_count);
/* Open file */
fd_write = open(filename, O_WRONLY | O_CREAT | O_BINARY);
if (fd_write < 0)
{
printf("%s open error:%d\n", filename, errno);
ret = ERROR;
}
else
{
if (write(fd_write, &pvtlogdat, sizeof(struct cxd56_pvtlog_s)) !=
sizeof(struct cxd56_pvtlog_s))
{
printf("%s write error:%d\n", filename, errno);
ret = ERROR;
}
else
{
printf("%s write OK\n", filename);
}
close(fd_write);
}
fd_write = 0;
return ret;
}
/****************************************************************************
* Name: gnss_pvtlog_write()
*
* Description:
* Enable PVTLOG and run GNSS for 10 minutes.
* When notification of PVTLOG comes, save PVTLOG in a file.
*
* Input Parameters:
* argc - Does not use.
* argv - Does not use.
*
* Returned Value:
* Zero (OK) on success; Negative value on error.
*
* Assumptions/Limitations:
* none.
*
****************************************************************************/
int gnss_pvtlog_write(int argc, char *argv[])
{
int fd;
int ret;
int timecount = 0;
int sig_id = -1;
uint32_t file_count = 1;
sigset_t mask;
struct cxd56_pvtlog_setting_s pvtlog_setting;
/* Program start */
printf("%s() in\n", __func__);
/* Get file descriptor to control GNSS. */
fd = open("/dev/gps", O_RDONLY);
if (fd < 0)
{
printf("open error:%d,%d\n", fd, errno);
return -ENODEV;
}
sigemptyset(&mask);
/* Init positioning signal */
printf("Set GNSS signal \n");
ret = set_signal(fd, MY_GNSS_SIG0, CXD56_GNSS_SIG_GNSS, TRUE, &mask);
if (ret < 0)
{
printf("GNSS signal set error\n");
goto _err3;
}
/* Init PVTLOG signal */
printf("Set PVTLOG signal \n");
ret = set_signal(fd, MY_GNSS_SIG1, CXD56_GNSS_SIG_PVTLOG, TRUE, &mask);
if (ret < 0)
{
printf("PVTLOG signal set error\n");
goto _err2;
}
/* Delete Log data */
printf("Delete Log \n");
ret = ioctl(fd, CXD56_GNSS_IOCTL_PVTLOG_DELETE_LOG, 0);
if (ret < 0)
{
printf("Delete log error\n");
goto _err1;
}
/* Start Log */
printf("Start Log \n");
pvtlog_setting.cycle = TEST_RECORDING_CYCLE;
pvtlog_setting.threshold = TEST_NOTIFY_THRESHOLD;
ret = ioctl(fd, CXD56_GNSS_IOCTL_PVTLOG_START,
(unsigned long)&pvtlog_setting);
if (ret < 0)
{
printf("Start pvtlog error\n");
goto _err1;
}
/* Start GNSS. */
printf("Start Positioning \n");
ret = ioctl(fd, CXD56_GNSS_IOCTL_START, CXD56_GNSS_STMOD_HOT);
if (ret < 0)
{
printf("start GNSS ERROR %d\n", errno);
goto _err0;
}
else
{
printf("start GNSS OK\n");
}
do
{
/* Wait signal */
sig_id = sigwaitinfo(&mask, NULL);
switch (sig_id)
{
case MY_GNSS_SIG0:
/* Read and print POS data. */
read_and_print(fd);
timecount++;
break;
case MY_GNSS_SIG1:
/* Receive pvtlog signal */
get_pvtlog(fd);
writefile(file_count);
file_count++;
break;
default:
/* Invalid case */
printf("ret %d\n", ret);
break;
}
}
while (timecount < TEST_LOOP_TIME);
/* Stop Log */
printf("Stop Log \n");
ioctl(fd, CXD56_GNSS_IOCTL_PVTLOG_STOP, 0);
/* If the last signal is positioning, there is a unsaved PVTLOG. */
if (sig_id == MY_GNSS_SIG0)
{
/* Write unsaved logs */
get_pvtlog(fd);
writefile(file_count);
}
_err0:
/* Stop GNSS. */
ret = ioctl(fd, CXD56_GNSS_IOCTL_STOP, 0);
if (ret < 0)
{
printf("stop GNSS ERROR\n");
}
else
{
printf("stop GNSS OK\n");
}
_err1:
/* TBD */
set_signal(fd, MY_GNSS_SIG0, CXD56_GNSS_SIG_GNSS, FALSE, &mask);
_err2:
/* TBD */
set_signal(fd, MY_GNSS_SIG1, CXD56_GNSS_SIG_PVTLOG, FALSE, &mask);
_err3:
/* Release GNSS file descriptor. */
ret = close(fd);
if (ret < 0)
{
printf("close error\n");
}
printf("%s() out %d\n", __func__, ret);
return ret;
}
/****************************************************************************
* Name: gnss_pvtlog_read()
*
* Description:
* Read and dump pvtlog file.
* Make sure that the dump logs of gnss_pvtlog_write() and
* gnss_pvtlog_read() match.
*
* Input Parameters:
* argc - Does not use.
* argv - Does not use.
*
* Returned Value:
* Zero (OK) on success; Negative value on error.
*
* Assumptions/Limitations:
* none.
*
****************************************************************************/
int gnss_pvtlog_read(int argc, char *argv[])
{
int ret = OK;
int fd_read;
uint32_t file_count;
uint32_t log_count;
uint32_t log_max;
char filename[FILE_NAME_LEN];
struct cxd56_pvtlog_data_s *log;
/* Program start */
printf("%s() in\n", __func__);
for (file_count = 1; file_count <= TEST_FILE_COUNT; file_count++)
{
/* Make file name */
snprintf(filename, FILE_NAME_LEN, "%s%d.dat",
CONFIG_EXAMPLES_PVTLOG_FILEPATH, file_count);
/* Open file */
fd_read = open(filename, O_RDONLY | O_BINARY);
if (fd_read < 0)
{
/* Continue */
}
else
{
/* Read file */
ret = read(fd_read, &pvtlogdat, sizeof(pvtlogdat));
if (ret < 0)
{
ret = errno;
printf("%s read error:%d\n", filename, errno);
}
else if (ret != sizeof(pvtlogdat))
{
ret = ERROR;
printf("%s read error:%d\n", filename, errno);
}
else
{
ret = OK;
log_max = pvtlogdat.log_count;
printf("%s read OK(%d line)\n", filename, log_max);
/* Printf pvtlog file */
for (log_count = 0; log_count < log_max; log_count++)
{
/* Printf record */
log = &pvtlogdat.log_data[log_count];
printf(" Y=20%2d, M=%2d, d=%2d",
log->date.year, log->date.month, log->date.day);
printf(" h=%2d, m=%2d, s=%2d m=%3d", log->time.hour,
log->time.minute, log->time.sec, log->time.msec);
printf(", Lat %d:%d:%d",log->latitude.degree,
log->latitude.minute, log->latitude.frac);
printf(" , Lon %d:%d:%d", log->longitude.degree,
log->longitude.minute, log->longitude.frac);
printf(", Log No:%d \n", (log_count + 1));
}
}
}
/* Close file */
close(fd_read);
fd_read = 0;
}
printf("%s() out %d\n", __func__, ret);
return ret;
}
/****************************************************************************
* Name: gnss_pvtlog_delete()
*
* Description:
* Delete pvtlog files.
*
* Input Parameters:
* argc - Does not use.
* argv - Does not use.
*
* Returned Value:
* Zero (OK) on success; Negative value on error.
*
* Assumptions/Limitations:
* none.
*
****************************************************************************/
int gnss_pvtlog_delete(int argc, char *argv[])
{
int ret = OK;
uint32_t file_count;
char filename[FILE_NAME_LEN];
/* Program start */
printf("%s() in\n", __func__);
for (file_count = 1; file_count <= TEST_FILE_COUNT; file_count++)
{
/* Make file name */
snprintf(filename, FILE_NAME_LEN, "%s%d.dat",
CONFIG_EXAMPLES_PVTLOG_FILEPATH, file_count);
/* Delete file */
if (unlink(filename) == OK)
{
printf("%s delete ok\n", filename);
}
}
printf("%s() out %d\n", __func__, ret);
return ret;
}
/****************************************************************************
* Name: gnss_pvtlog_main()
*
* Description:
* Run positioning and write / read PVTLOG file.
*
* Input Parameters:
* argv[1] - Specify the operation mode of PVTLOG.
* "W": positioning and write pvtlog file.
* Including the case of not specifying.
* "R": read and dump pvtlog file.
* "D": delete pvtlog file.
* "A": run W and R and D.
*
* Returned Value:
* Zero (OK) on success; Negative value on error.
*
* Assumptions/Limitations:
* none.
*
****************************************************************************/
#ifdef CONFIG_BUILD_KERNEL
int main(int argc, FAR char *argv[])
#else
int gnss_pvtlog_main(int argc, char *argv[])
#endif
{
int ret = OK;
char *argmode = "w"; /* Default:write */
/* Program start */
printf("Hello, PVTLOG SAMPLE!!\n");
/* Argument check */
if (argc >= 2)
{
argmode = argv[1];
}
switch (argmode[0])
{
case 'r':
case 'R':
/* Read and dump pvtlog file */
ret = gnss_pvtlog_read(argc, argv);
break;
case 'd':
case 'D':
/* Delete pvtlog file */
ret = gnss_pvtlog_delete(argc, argv);
break;
case 'a':
case 'A':
/* Run W and R and D */
if (gnss_pvtlog_write(argc, argv) != OK)
{
ret = ERROR;
}
if (gnss_pvtlog_read(argc, argv) != OK)
{
ret = ERROR;
}
if (gnss_pvtlog_delete(argc, argv) != OK)
{
ret = ERROR;
}
break;
case 'w':
case 'W':
default:
/* Positioning and write pvtlog file */
ret = gnss_pvtlog_write(argc, argv);
break;
}
printf("End of PVTLOG Sample:%d\n", ret);
return ret;
}