Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

gdp / demo / 201501 / pi-reader.c @ master

History | View | Annotate | Download (4.18 KB)

1 d1e8474e Eric Allman
#include <gdp/gdp.h>
2
#include <ep/ep_app.h>
3
#include <ep/ep_dbg.h>
4
#include <wiringPi.h>
5
#include <jansson.h>
6
7
#include <errno.h>
8
#include <stdio.h>
9
#include <string.h>
10
#include <sysexits.h>
11
#include <sys/time.h>
12
13
/*
14
**  PI-READER --- reads a GCL and triggers a LED
15
**
16
**        Reads a particular log for 0/1 input and tracks that to
17
**        a visual indicator.
18
**
19
**        Assumes a LED attached to GPIO 17 (Pin P1-11 on Pi-B)
20
**        through a 220 ohm resistor to ground.
21
*/
22
23
#define LOG_NAME        "demo/201501/pi"
24
#define LEDPIN                17
25
26
FILE        *LogFile;
27
int        LedPin = LEDPIN;
28
29 07d4e42e Eric Allman
void
30
usage(void)
31
{
32
        fprintf(stderr,
33
                "Usage: %s [-D dbgspec] [-g gclname] [-p ledpin]\n",
34
                ep_app_getprogname());
35
        exit(EX_USAGE);
36
}
37
38 d1e8474e Eric Allman
int
39
main(int argc, char **argv)
40
{
41
        gdp_gcl_t *gcl;
42
        EP_STAT estat;
43
        gdp_name_t gclname;
44
        char *gclpname = LOG_NAME;
45
        bool show_usage = false;
46
        char *log_file_name = NULL;
47
        int opt;
48
49
        while ((opt = getopt(argc, argv, "D:g:L:p:")) > 0)
50
        {
51
                switch (opt)
52
                {
53
                  case 'D':
54
                        ep_dbg_set(optarg);
55
                        break;
56
57
                  case 'g':
58
                        gclpname = optarg;
59
                        break;
60
61
                  case 'L':
62
                        log_file_name = optarg;
63
                        break;
64
65
                  case 'p':
66
                        LedPin = atoi(optarg);
67
                        break;
68
69
                  default:
70
                        show_usage = true;
71
                        break;
72
                }
73
        }
74
        argc -= optind;
75
        argv += optind;
76
77
        if (show_usage || argc > 0)
78 07d4e42e Eric Allman
                usage();
79 d1e8474e Eric Allman
80
        // initialize wiringPi library (must be root!)
81
        printf("Initializing wiringPi library:\n");
82
        wiringPiSetupGpio();
83
        pinMode(LedPin, OUTPUT);
84
85
        //XXX should probably give up root privileges here
86
87
        if (log_file_name != NULL)
88
        {
89
                // open a log file (for timing measurements)
90
                printf("Opening log file:\n");
91
                LogFile = fopen(log_file_name, "a");
92
                if (LogFile == NULL)
93
                        printf("Cannot open log file: %s\n", strerror(errno));
94
                setlinebuf(LogFile);
95
        }
96
97
        // initialize the GDP library
98
        printf("Initializing GDP library:\n");
99
        estat = gdp_init(NULL);
100
        if (!EP_STAT_ISOK(estat))
101
        {
102
                ep_app_error("GDP Initialization failed");
103
                exit(EX_UNAVAILABLE);
104
        }
105
106
        // convert the name to internal form
107
        printf("Converting name %s to internal form:\n", gclpname);
108
        estat = gdp_parse_name(gclpname, gclname);
109
        if (!EP_STAT_ISOK(estat))
110
        {
111
                ep_app_error("Illegal GCL name syntax:\n\t%s",
112
                        gclpname);
113
                exit(EX_NOINPUT);
114
        }
115
116
        // open the GCL for reading
117
        printf("Opening GCL for reading:\n");
118
        estat = gdp_gcl_open(gclname, GDP_MODE_RO, &gcl);
119
        if (!EP_STAT_ISOK(estat))
120
        {
121
                char sbuf[100];
122
123
                ep_app_error("Cannot open GCL:\n    %s",
124
                        ep_stat_tostr(estat, sbuf, sizeof sbuf));
125
                exit(EX_NOINPUT);
126
        }
127
128
        // start a subscription
129
        printf("Subscribing to GCL:\n");
130
        estat = gdp_gcl_subscribe(gcl, -1, 0, NULL, NULL, NULL);
131
        if (!EP_STAT_ISOK(estat))
132
        {
133
                char sbuf[100];
134
135
                ep_app_error("Cannot subscribe:\n\t%s",
136
                        ep_stat_tostr(estat, sbuf, sizeof sbuf));
137
                exit(EX_SOFTWARE);
138
        }
139
140
        // start reading events
141
        printf("Starting to read data:\n");
142
        for (;;)
143
        {
144
                gdp_event_t *gev = gdp_event_next(NULL);
145
                gdp_buf_t *b;
146
                json_t *json;
147
                size_t l;
148
149
                printf("Got datum:\n");
150
151
                // decode the event
152
                switch (gdp_event_gettype(gev))
153
                {
154
                  case GDP_EVENT_DATA:
155
                          // track the input
156
                        if (LogFile != NULL)
157
                        {
158
                                struct timeval tv;
159
                                gettimeofday(&tv, NULL);
160
                                fprintf(LogFile, "R %d.%06d\n",
161
                                                tv.tv_sec, tv.tv_usec);
162
                        }
163
164
                        b = gdp_datum_getbuf(gdp_event_getdatum(gev));
165
                        l = gdp_buf_getlength(b);
166
                        printf("  ... recno %" PRIgdp_recno ", %.*s\n",
167
                                gdp_datum_getrecno(gdp_event_getdatum(gev)),
168
                                (int) l, gdp_buf_getptr(b, l));
169
                        json = json_loadb((const char *)gdp_buf_getptr(b, l),
170
                                        l, 0, NULL);
171
                        if (json == NULL || !json_is_object(json))
172
                        {
173
                                ep_app_error("Cannot load JSON data");
174
                                break;
175
                        }
176
177
                        json_t *jval = json_object_get(json, "value");
178
                        if (jval == NULL || !json_is_boolean(jval))
179
                        {
180
                                ep_app_error("Invalid JSON data");
181
                                break;
182
                        }
183
184
                        // write the output pin
185
                        printf("Value = %d\n", json_is_true(jval));
186
                        digitalWrite(LedPin, json_is_true(jval));
187
188
                        json_decref(jval);
189
                        json_decref(json);
190
                        break;
191
192
                  case GDP_EVENT_EOS:
193
                          // end of subscription (shouldn't happen!)
194
                        break;
195
                        ep_app_error("End of subscription");
196
                        exit(EX_SOFTWARE);
197
198
                  default:
199
                        // unknown events are ignored
200
                        printf("Unknown event type %d\n",
201
                                        gdp_event_gettype(gev));
202
                        break;
203
                }
204
205
                gdp_event_free(gev);
206
        }
207
208
        // should never get here
209
        ep_app_abort("Impossible exit");
210
}