Add radio tuning test app
[rdstmc.git] / decoder / rds_test.c
1 /*
2  * Copyright (C) 2009, 2010 by Nils Faerber <nils.faerber@kernelconcepts.de>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version
7  * 2 of the License, or (at your option) any later version.
8  *
9  */
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <time.h>
15 #include <unistd.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <limits.h>
19
20 #include <sqlite3.h>
21
22 #include "rds.h"
23 #include "tmc.h"
24
25 sqlite3 *lcl_db;
26 int OutputFlags;
27
28 void test_rds_PI_cb(unsigned short PI, unsigned char ccode, unsigned char ptype, unsigned char pref, void *udata)
29 {
30                 printf("New PI=%d ccode=%X ptype=%X '%s' '%s' pref=%d\n", PI, ccode, ptype, ptype_stext[ptype], ptype_ltext[ptype], pref);
31 }
32
33 void test_rds_radiotext_cb(char *radio_text, void *udata)
34 {
35         printf("New RT: '%s'\n", radio_text);
36 }
37
38 void test_rds_date_time_cb(struct tm *rds_time, void *udata)
39 {
40         printf("RDS date/time: %s", asctime(rds_time));
41 }
42
43 void test_rds_sname_cb(char *sname, void *udata)
44 {
45         printf("RDS sname='%s'\n", sname);
46 }
47
48 void test_tmc_msg_cb(char *msg, void *user_data)
49 {
50         printf("\nTMC msg:\n%s", msg);
51 }
52
53 int open_radio(const char *name)
54 {
55 int fd;
56
57         fd = open(name, O_RDONLY);
58         if (fd > 0)
59                 return fd;
60         else
61                 return 0;
62 }
63
64 int main(int argc, char **argv)
65 {
66 char rdevname[PATH_MAX] = "/dev/radio0";
67 unsigned short rdsgroup[4];
68 int rds_fd, i;
69
70         rds_init();
71         tmc_init();
72
73         i = 1;
74         while (i < argc) {
75                 if (strncmp("-all", argv[i], 4) == 0)
76                         OutputFlags |= ~0;
77                 if (strncmp("-di", argv[i], 3) == 0)
78                         OutputFlags |= RDS_OUTPUT_RDSINFO;
79                 if (strncmp("-rd", argv[i], 3) == 0)
80                         if (argc > i)
81                                 strcpy(rdevname, argv[++i]);
82                 if (strncmp("-rt", argv[i], 3) == 0) {
83                         // OutputFlags |= RDS_OUTPUT_RADIO_TEXT;
84                         rds_set_radiotext_cb(test_rds_radiotext_cb, NULL);
85                 }
86                 if (strncmp("-ri", argv[i], 3) == 0) {
87                         // OutputFlags |= RDS_OUTPUT_STATION_ID;
88                         rds_set_sname_cb(test_rds_sname_cb, NULL);
89                 }
90                 if (strncmp("-tmc", argv[i], 4) == 0) {
91                         // OutputFlags |= RDS_OUTPUT_TMC;
92                         tmc_set_msg_cb(test_tmc_msg_cb, NULL);
93                 }
94                 if (strncmp("-eon", argv[i], 4) == 0)
95                         OutputFlags |= RDS_OUTPUT_EON;
96                 if (strncmp("-dt", argv[i], 3) == 0) {
97                         // OutputFlags |= RDS_OUTPUT_DATETIME;
98                         rds_set_date_time_cb(test_rds_date_time_cb, NULL);
99                 }
100                 if (strncmp("-ug", argv[i], 3) == 0)
101                         OutputFlags |= RDS_OUTPUT_UNKNGRP;
102                 if (strncmp("-pi", argv[i], 3) == 0) {
103                         // OutputFlags |= RDS_RECEIVE_INDICATOR;
104                         rds_set_PI_cb(test_rds_PI_cb, NULL);
105                 }
106                 i++;
107         }
108
109         if (!(rds_fd = open_radio(rdevname))) {
110                 perror("open radio:");
111                 return -1;
112         }
113
114         if (sqlite3_open("lcl.db", &lcl_db) != SQLITE_OK) {
115                 perror("open LCL database:");
116                 close(rds_fd);
117                 return -1;
118         }
119
120         while (1) {
121                 if (rds_receive_group(rds_fd, rdsgroup)) {
122                         /* group complete, start decode */
123                         rds_decode_group(rdsgroup);
124                 }
125         }
126         
127 return 0;
128 }
129