Hardware Reference
In-Depth Information
Except for the use of pthreads and ØMQ, not much is new in the source code.
Consequently, I'll just provide some highlights.
The
sensor.c
main program gets everything started, by opening the GPIO files (input
and output), opening the ØMQ sockets, and creating two threads. The main thread is
contained within the main program, within the
for
loop starting at line 298. The loop simply
pulls console commands from the ØMQ socket
console
at line 299 and then acts upon them.
There are only two supported console commands:
led:%d
: Change LED status
stop:
: Shut down the
./sensor
program
Line 286 of the main program creates the
SW1_monitor_thread
. This thread is
located in lines 211 to 223. It uses the
poll(2)
system call in the routine
gpio_poll()
,
to determine when the switch setting changes. This GPIO input is coming from
Q
1
of the
flip-flop, which is connected to either a switch or a microswitch push-button.
Program execution blocks at line 216, until the switch changes state. Then the status
of the switch is captured in
rc
and relayed to all interested consoles by calling the routine
publish_SW1()
.
The remaining thread is launched in the main program from line 289. It runs in
lines 228 through 237. It is a very small loop, which simply updates the consoles every 3
seconds, with the current status of the LED and SW
1
. This is necessary so that consoles
that are restarted can eventually know the current state of these items.
The
mutex_lock()
and
mutex_unlock()
routines are designed to guard against
two threads using the same ØMQ resources at the same time. Doing so would cause
program aborts.
The ØMQ library supports a routine named
zmq_poll()
, which would have
simplified things if it could have been used. Unfortunately, it supports only
ZMQ_POLLIN
for input. Our switch change driver requires the use of
poll(2)
's
POLLPRI
event, so
zmq_poll()
will not support us there.
1 /
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
2
sensor.c − Sense SW1, send to console (and take LED cmd from console)
∗
3
/
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <string.h>
9 #include <fcntl.h>
10 #include <assert.h>
11 #include <poll.h>
12 #include <pthread.h>
13
14 #include <zmq.h>
15
16 static const char
service_sensor_pub = "tcp ://
:9999";
∗
∗
17 static const char
service_sensor_pull = "tcp ://
:9998";
∗
∗
18