wiringpi-ruby 2.0.0 → 2.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ext/wiringpi/WiringPi/devLib/ds1302.c +240 -0
- data/ext/wiringpi/WiringPi/devLib/ds1302.h +44 -0
- data/ext/wiringpi/WiringPi/devLib/font.h +2577 -0
- data/ext/wiringpi/WiringPi/devLib/gertboard.c +164 -0
- data/ext/wiringpi/WiringPi/devLib/gertboard.h +45 -0
- data/ext/wiringpi/WiringPi/devLib/lcd.c +495 -0
- data/ext/wiringpi/WiringPi/devLib/lcd.h +52 -0
- data/ext/wiringpi/WiringPi/devLib/lcd128x64.c +673 -0
- data/ext/wiringpi/WiringPi/devLib/lcd128x64.h +39 -0
- data/ext/wiringpi/WiringPi/devLib/maxdetect.c +238 -0
- data/ext/wiringpi/WiringPi/devLib/maxdetect.h +40 -0
- data/ext/wiringpi/WiringPi/devLib/piFace.c +112 -0
- data/ext/wiringpi/WiringPi/devLib/piFace.h +32 -0
- data/ext/wiringpi/WiringPi/devLib/piFaceOld.c +177 -0
- data/ext/wiringpi/WiringPi/devLib/piGlow.c +118 -0
- data/ext/wiringpi/WiringPi/devLib/piGlow.h +45 -0
- data/ext/wiringpi/WiringPi/devLib/piNes.c +113 -0
- data/ext/wiringpi/WiringPi/devLib/piNes.h +45 -0
- data/ext/wiringpi/WiringPi/devLib/scrollPhat.c +430 -0
- data/ext/wiringpi/WiringPi/devLib/scrollPhat.h +39 -0
- data/ext/wiringpi/WiringPi/devLib/scrollPhatFont.h +544 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/7segments.c +221 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/buttons.c +83 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/gertboard.c +96 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/record.c +60 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/temperature.c +78 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/voltmeter.c +73 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/vumeter.c +152 -0
- data/ext/wiringpi/WiringPi/examples/PiFace/blink.c +59 -0
- data/ext/wiringpi/WiringPi/examples/PiFace/buttons.c +103 -0
- data/ext/wiringpi/WiringPi/examples/PiFace/ladder.c +337 -0
- data/ext/wiringpi/WiringPi/examples/PiFace/metro.c +111 -0
- data/ext/wiringpi/WiringPi/examples/PiFace/motor.c +120 -0
- data/ext/wiringpi/WiringPi/examples/PiFace/reaction.c +194 -0
- data/ext/wiringpi/WiringPi/examples/PiGlow/piGlow0.c +51 -0
- data/ext/wiringpi/WiringPi/examples/PiGlow/piGlow1.c +258 -0
- data/ext/wiringpi/WiringPi/examples/PiGlow/piglow.c +176 -0
- data/ext/wiringpi/WiringPi/examples/blink-thread.c +61 -0
- data/ext/wiringpi/WiringPi/examples/blink.c +48 -0
- data/ext/wiringpi/WiringPi/examples/blink12.c +111 -0
- data/ext/wiringpi/WiringPi/examples/blink12drcs.c +125 -0
- data/ext/wiringpi/WiringPi/examples/blink6drcs.c +115 -0
- data/ext/wiringpi/WiringPi/examples/blink8-drcn.c +61 -0
- data/ext/wiringpi/WiringPi/examples/blink8.c +57 -0
- data/ext/wiringpi/WiringPi/examples/clock.c +201 -0
- data/ext/wiringpi/WiringPi/examples/delayTest.c +102 -0
- data/ext/wiringpi/WiringPi/examples/ds1302.c +238 -0
- data/ext/wiringpi/WiringPi/examples/header.h +23 -0
- data/ext/wiringpi/WiringPi/examples/isr-osc.c +118 -0
- data/ext/wiringpi/WiringPi/examples/isr.c +110 -0
- data/ext/wiringpi/WiringPi/examples/lcd-adafruit.c +347 -0
- data/ext/wiringpi/WiringPi/examples/lcd.c +286 -0
- data/ext/wiringpi/WiringPi/examples/lowPower.c +68 -0
- data/ext/wiringpi/WiringPi/examples/max31855.c +60 -0
- data/ext/wiringpi/WiringPi/examples/nes.c +67 -0
- data/ext/wiringpi/WiringPi/examples/okLed.c +82 -0
- data/ext/wiringpi/WiringPi/examples/pwm.c +58 -0
- data/ext/wiringpi/WiringPi/examples/q2w/binary.c +79 -0
- data/ext/wiringpi/WiringPi/examples/q2w/blink-io.c +61 -0
- data/ext/wiringpi/WiringPi/examples/q2w/blink.c +50 -0
- data/ext/wiringpi/WiringPi/examples/q2w/bright.c +59 -0
- data/ext/wiringpi/WiringPi/examples/q2w/button.c +63 -0
- data/ext/wiringpi/WiringPi/examples/q2w/volts.c +62 -0
- data/ext/wiringpi/WiringPi/examples/rht03.c +86 -0
- data/ext/wiringpi/WiringPi/examples/scrollPhat/scphat.c +230 -0
- data/ext/wiringpi/WiringPi/examples/scrollPhat/test.c +115 -0
- data/ext/wiringpi/WiringPi/examples/serialRead.c +48 -0
- data/ext/wiringpi/WiringPi/examples/serialTest.c +75 -0
- data/ext/wiringpi/WiringPi/examples/servo.c +57 -0
- data/ext/wiringpi/WiringPi/examples/softPwm.c +89 -0
- data/ext/wiringpi/WiringPi/examples/softTone.c +54 -0
- data/ext/wiringpi/WiringPi/examples/speed.c +95 -0
- data/ext/wiringpi/WiringPi/examples/spiSpeed.c +118 -0
- data/ext/wiringpi/WiringPi/examples/wfi.c +161 -0
- data/ext/wiringpi/WiringPi/gpio/gpio.c +1538 -0
- data/ext/wiringpi/WiringPi/gpio/readall.c +403 -0
- data/ext/wiringpi/WiringPi/version.h +3 -0
- data/ext/wiringpi/WiringPi/wiringPi/ads1115.c +293 -0
- data/ext/wiringpi/WiringPi/wiringPi/ads1115.h +55 -0
- data/ext/wiringpi/WiringPi/wiringPi/bmp180.c +237 -0
- data/ext/wiringpi/WiringPi/wiringPi/bmp180.h +34 -0
- data/ext/wiringpi/WiringPi/wiringPi/drcNet.c +405 -0
- data/ext/wiringpi/WiringPi/wiringPi/drcNet.h +42 -0
- data/ext/wiringpi/WiringPi/wiringPi/drcSerial.c +196 -0
- data/ext/wiringpi/WiringPi/wiringPi/drcSerial.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/ds18b20.c +146 -0
- data/ext/wiringpi/WiringPi/wiringPi/ds18b20.h +34 -0
- data/ext/wiringpi/WiringPi/wiringPi/htu21d.c +150 -0
- data/ext/wiringpi/WiringPi/wiringPi/htu21d.h +34 -0
- data/ext/wiringpi/WiringPi/wiringPi/max31855.c +99 -0
- data/ext/wiringpi/WiringPi/wiringPi/max31855.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/max5322.c +84 -0
- data/ext/wiringpi/WiringPi/wiringPi/max5322.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23008.c +149 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23008.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23016.c +164 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23016.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23016reg.h +48 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23017.c +195 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23017.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23s08.c +188 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23s08.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23s17.c +235 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23s17.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23x08.h +73 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23x0817.h +87 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp3002.c +76 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp3002.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp3004.c +76 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp3004.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp3422.c +125 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp3422.h +43 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp4802.c +76 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp4802.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/pcf8574.c +126 -0
- data/ext/wiringpi/WiringPi/wiringPi/pcf8574.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/pcf8591.c +90 -0
- data/ext/wiringpi/WiringPi/wiringPi/pcf8591.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/piHiPri.c +51 -0
- data/ext/wiringpi/WiringPi/wiringPi/piThread.c +63 -0
- data/ext/wiringpi/WiringPi/wiringPi/pseudoPins.c +95 -0
- data/ext/wiringpi/WiringPi/wiringPi/pseudoPins.h +26 -0
- data/ext/wiringpi/WiringPi/wiringPi/rht03.c +252 -0
- data/ext/wiringpi/WiringPi/wiringPi/rht03.h +25 -0
- data/ext/wiringpi/WiringPi/wiringPi/sn3218.c +75 -0
- data/ext/wiringpi/WiringPi/wiringPi/sn3218.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/softPwm.c +186 -0
- data/ext/wiringpi/WiringPi/wiringPi/softPwm.h +35 -0
- data/ext/wiringpi/WiringPi/wiringPi/softServo.c +211 -0
- data/ext/wiringpi/WiringPi/wiringPi/softServo.h +35 -0
- data/ext/wiringpi/WiringPi/wiringPi/softTone.c +150 -0
- data/ext/wiringpi/WiringPi/wiringPi/softTone.h +39 -0
- data/ext/wiringpi/WiringPi/wiringPi/sr595.c +109 -0
- data/ext/wiringpi/WiringPi/wiringPi/sr595.h +34 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPi.c +2507 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPi.h +269 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPiI2C.c +233 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPiI2C.h +42 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPiSPI.c +144 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPiSPI.h +36 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringSerial.c +225 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringSerial.h +38 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringShift.c +83 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringShift.h +41 -0
- data/ext/wiringpi/WiringPi/wiringPi/wpiExtensions.c +928 -0
- data/ext/wiringpi/WiringPi/wiringPi/wpiExtensions.h +26 -0
- data/ext/wiringpi/WiringPi/wiringPiD/daemonise.c +82 -0
- data/ext/wiringpi/WiringPi/wiringPiD/daemonise.h +9 -0
- data/ext/wiringpi/WiringPi/wiringPiD/drcNetCmd.h +43 -0
- data/ext/wiringpi/WiringPi/wiringPiD/network.c +330 -0
- data/ext/wiringpi/WiringPi/wiringPiD/network.h +31 -0
- data/ext/wiringpi/WiringPi/wiringPiD/runRemote.c +126 -0
- data/ext/wiringpi/WiringPi/wiringPiD/runRemote.h +29 -0
- data/ext/wiringpi/WiringPi/wiringPiD/wiringpid.c +382 -0
- data/ext/wiringpi/wiringpi_wrap.c +636 -219
- data/lib/wiringpi.rb +25 -12
- data/lib/wiringpi/gpio.rb +90 -83
- data/lib/wiringpi/i2c.rb +29 -22
- data/lib/wiringpi/modules/mcp23017.rb +15 -0
- data/lib/wiringpi/modules/mcp23s17.rb +16 -0
- data/lib/wiringpi/scrollphat.rb +14 -14
- data/lib/wiringpi/serial.rb +8 -18
- data/lib/wiringpi/spi.rb +14 -11
- data/lib/wiringpi/watch.rb +24 -0
- metadata +160 -4
- data/lib/wiringpi/event.rb +0 -21
- data/lib/wiringpi/mcp23x17.rb +0 -31
@@ -0,0 +1,31 @@
|
|
1
|
+
/*
|
2
|
+
* network.h:
|
3
|
+
* Part of wiringPiD
|
4
|
+
* Copyright (c) 2012-2017 Gordon Henderson
|
5
|
+
***********************************************************************
|
6
|
+
* This file is part of wiringPi:
|
7
|
+
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
8
|
+
*
|
9
|
+
* wiringPi is free software: you can redistribute it and/or modify
|
10
|
+
* it under the terms of the GNU Lesser General Public License as published by
|
11
|
+
* the Free Software Foundation, either version 3 of the License, or
|
12
|
+
* (at your option) any later version.
|
13
|
+
*
|
14
|
+
* wiringPi is distributed in the hope that it will be useful,
|
15
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17
|
+
* GNU Lesser General Public License for more details.
|
18
|
+
*
|
19
|
+
* You should have received a copy of the GNU Lesser General Public License
|
20
|
+
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
|
21
|
+
***********************************************************************
|
22
|
+
*/
|
23
|
+
|
24
|
+
extern char *getClientIP (void) ;
|
25
|
+
extern int getResponce (int clientFd) ;
|
26
|
+
extern int setupServer (int serverPort) ;
|
27
|
+
extern int sendGreeting (int clientFd) ;
|
28
|
+
extern int sendChallenge (int clientFd) ;
|
29
|
+
extern int getResponse (int clientFd) ;
|
30
|
+
extern int passwordMatch (const char *password) ;
|
31
|
+
extern void closeServer (int clientFd) ;
|
@@ -0,0 +1,126 @@
|
|
1
|
+
/*
|
2
|
+
* runRemote.c:
|
3
|
+
* Run the remote commands passed over the network link.
|
4
|
+
*
|
5
|
+
* Copyright (c) 2012-2017 Gordon Henderson
|
6
|
+
***********************************************************************
|
7
|
+
* This file is part of wiringPi:
|
8
|
+
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
9
|
+
*
|
10
|
+
* wiringPi is free software: you can redistribute it and/or modify
|
11
|
+
* it under the terms of the GNU Lesser General Public License as published by
|
12
|
+
* the Free Software Foundation, either version 3 of the License, or
|
13
|
+
* (at your option) any later version.
|
14
|
+
*
|
15
|
+
* wiringPi is distributed in the hope that it will be useful,
|
16
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
+
* GNU Lesser General Public License for more details.
|
19
|
+
*
|
20
|
+
* You should have received a copy of the GNU Lesser General Public License
|
21
|
+
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
|
22
|
+
***********************************************************************
|
23
|
+
*/
|
24
|
+
|
25
|
+
#include <arpa/inet.h>
|
26
|
+
#include <stdio.h>
|
27
|
+
#include <stdlib.h>
|
28
|
+
#include <stdint.h>
|
29
|
+
#include <unistd.h>
|
30
|
+
#include <string.h>
|
31
|
+
#include <errno.h>
|
32
|
+
//#include <stdarg.h>
|
33
|
+
|
34
|
+
#include <wiringPi.h>
|
35
|
+
#include <wpiExtensions.h>
|
36
|
+
|
37
|
+
#include "drcNetCmd.h"
|
38
|
+
#include "network.h"
|
39
|
+
#include "runRemote.h"
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
int noLocalPins = FALSE ;
|
44
|
+
|
45
|
+
|
46
|
+
void runRemoteCommands (int fd)
|
47
|
+
{
|
48
|
+
register uint32_t pin ;
|
49
|
+
int len ;
|
50
|
+
struct drcNetComStruct cmd ;
|
51
|
+
|
52
|
+
len = sizeof (struct drcNetComStruct) ;
|
53
|
+
|
54
|
+
if (setsockopt (fd, SOL_SOCKET, SO_RCVLOWAT, (void *)&len, sizeof (len)) < 0)
|
55
|
+
return ;
|
56
|
+
|
57
|
+
for (;;)
|
58
|
+
{
|
59
|
+
if (recv (fd, &cmd, sizeof (cmd), 0) != sizeof (cmd)) // Probably remote hangup
|
60
|
+
return ;
|
61
|
+
|
62
|
+
pin = cmd.pin ;
|
63
|
+
if (noLocalPins && ((pin & PI_GPIO_MASK) == 0))
|
64
|
+
{
|
65
|
+
if (send (fd, &cmd, sizeof (cmd), 0) != sizeof (cmd))
|
66
|
+
return ;
|
67
|
+
continue ;
|
68
|
+
}
|
69
|
+
|
70
|
+
switch (cmd.cmd)
|
71
|
+
{
|
72
|
+
case DRCN_PIN_MODE:
|
73
|
+
pinMode (pin, cmd.data) ;
|
74
|
+
if (send (fd, &cmd, sizeof (cmd), 0) != sizeof (cmd))
|
75
|
+
return ;
|
76
|
+
break ;
|
77
|
+
|
78
|
+
case DRCN_PULL_UP_DN:
|
79
|
+
pullUpDnControl (pin, cmd.data) ;
|
80
|
+
break ;
|
81
|
+
|
82
|
+
case DRCN_PWM_WRITE:
|
83
|
+
pwmWrite (pin, cmd.data) ;
|
84
|
+
if (send (fd, &cmd, sizeof (cmd), 0) != sizeof (cmd))
|
85
|
+
return ;
|
86
|
+
break ;
|
87
|
+
|
88
|
+
case DRCN_DIGITAL_WRITE:
|
89
|
+
digitalWrite (pin, cmd.data) ;
|
90
|
+
if (send (fd, &cmd, sizeof (cmd), 0) != sizeof (cmd))
|
91
|
+
return ;
|
92
|
+
break ;
|
93
|
+
|
94
|
+
case DRCN_DIGITAL_WRITE8:
|
95
|
+
//digitalWrite8 (pin, cmd.data) ;
|
96
|
+
if (send (fd, &cmd, sizeof (cmd), 0) != sizeof (cmd))
|
97
|
+
return ;
|
98
|
+
break ;
|
99
|
+
|
100
|
+
case DRCN_DIGITAL_READ:
|
101
|
+
cmd.data = digitalRead (pin) ;
|
102
|
+
if (send (fd, &cmd, sizeof (cmd), 0) != sizeof (cmd))
|
103
|
+
return ;
|
104
|
+
break ;
|
105
|
+
|
106
|
+
case DRCN_DIGITAL_READ8:
|
107
|
+
//cmd.data = digitalRead8 (pin) ;
|
108
|
+
if (send (fd, &cmd, sizeof (cmd), 0) != sizeof (cmd))
|
109
|
+
return ;
|
110
|
+
break ;
|
111
|
+
|
112
|
+
case DRCN_ANALOG_WRITE:
|
113
|
+
analogWrite (pin, cmd.data) ;
|
114
|
+
if (send (fd, &cmd, sizeof (cmd), 0) != sizeof (cmd))
|
115
|
+
return ;
|
116
|
+
break ;
|
117
|
+
|
118
|
+
case DRCN_ANALOG_READ:
|
119
|
+
cmd.data = analogRead (pin) ;
|
120
|
+
if (send (fd, &cmd, sizeof (cmd), 0) != sizeof (cmd))
|
121
|
+
return ;
|
122
|
+
break ;
|
123
|
+
}
|
124
|
+
}
|
125
|
+
|
126
|
+
}
|
@@ -0,0 +1,29 @@
|
|
1
|
+
/*
|
2
|
+
* runRemote.h:
|
3
|
+
* Run the remote commands passed over the network link.
|
4
|
+
*
|
5
|
+
* Copyright (c) 2012-2017 Gordon Henderson
|
6
|
+
***********************************************************************
|
7
|
+
* This file is part of wiringPi:
|
8
|
+
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
9
|
+
*
|
10
|
+
* wiringPi is free software: you can redistribute it and/or modify
|
11
|
+
* it under the terms of the GNU Lesser General Public License as published by
|
12
|
+
* the Free Software Foundation, either version 3 of the License, or
|
13
|
+
* (at your option) any later version.
|
14
|
+
*
|
15
|
+
* wiringPi is distributed in the hope that it will be useful,
|
16
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
+
* GNU Lesser General Public License for more details.
|
19
|
+
*
|
20
|
+
* You should have received a copy of the GNU Lesser General Public License
|
21
|
+
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
|
22
|
+
***********************************************************************
|
23
|
+
*/
|
24
|
+
|
25
|
+
// Globals
|
26
|
+
|
27
|
+
extern int noLocalPins ;
|
28
|
+
|
29
|
+
extern void runRemoteCommands (int fd) ;
|
@@ -0,0 +1,382 @@
|
|
1
|
+
/*
|
2
|
+
* wiringPiD.c:
|
3
|
+
* Copyright (c) 2012-2017 Gordon Henderson
|
4
|
+
***********************************************************************
|
5
|
+
* This file is part of wiringPi:
|
6
|
+
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
7
|
+
*
|
8
|
+
* wiringPi is free software: you can redistribute it and/or modify
|
9
|
+
* it under the terms of the GNU Lesser General Public License as published by
|
10
|
+
* the Free Software Foundation, either version 3 of the License, or
|
11
|
+
* (at your option) any later version.
|
12
|
+
*
|
13
|
+
* wiringPi is distributed in the hope that it will be useful,
|
14
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16
|
+
* GNU Lesser General Public License for more details.
|
17
|
+
*
|
18
|
+
* You should have received a copy of the GNU Lesser General Public License
|
19
|
+
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
|
20
|
+
***********************************************************************
|
21
|
+
*/
|
22
|
+
|
23
|
+
#include <arpa/inet.h>
|
24
|
+
#include <stdio.h>
|
25
|
+
#include <stdlib.h>
|
26
|
+
#include <stdint.h>
|
27
|
+
#include <unistd.h>
|
28
|
+
#include <string.h>
|
29
|
+
#include <stdarg.h>
|
30
|
+
#include <syslog.h>
|
31
|
+
#include <signal.h>
|
32
|
+
#include <errno.h>
|
33
|
+
|
34
|
+
#include <wiringPi.h>
|
35
|
+
#include <wpiExtensions.h>
|
36
|
+
|
37
|
+
#include "drcNetCmd.h"
|
38
|
+
#include "network.h"
|
39
|
+
#include "runRemote.h"
|
40
|
+
#include "daemonise.h"
|
41
|
+
|
42
|
+
|
43
|
+
#define PIDFILE "/var/run/wiringPiD.pid"
|
44
|
+
|
45
|
+
|
46
|
+
// Globals
|
47
|
+
|
48
|
+
static const char *usage = "[-h] [-d] [-g | -1 | -z] [[-x extension:pin:params] ...] password" ;
|
49
|
+
static int doDaemon = FALSE ;
|
50
|
+
|
51
|
+
//
|
52
|
+
|
53
|
+
static void logMsg (const char *message, ...)
|
54
|
+
{
|
55
|
+
va_list argp ;
|
56
|
+
char buffer [1024] ;
|
57
|
+
|
58
|
+
va_start (argp, message) ;
|
59
|
+
vsnprintf (buffer, 1023, message, argp) ;
|
60
|
+
va_end (argp) ;
|
61
|
+
|
62
|
+
if (doDaemon)
|
63
|
+
syslog (LOG_DAEMON | LOG_INFO, "%s", buffer) ;
|
64
|
+
else
|
65
|
+
printf ("%s\n", buffer) ;
|
66
|
+
}
|
67
|
+
|
68
|
+
|
69
|
+
/*
|
70
|
+
* sigHandler:
|
71
|
+
* setupSigHandler:
|
72
|
+
* Somehing has happened that would normally terminate the program so try
|
73
|
+
* to close down nicely.
|
74
|
+
*********************************************************************************
|
75
|
+
*/
|
76
|
+
|
77
|
+
void sigHandler (int sig)
|
78
|
+
{
|
79
|
+
logMsg ("Exiting on signal %d: %s", sig, strsignal (sig)) ;
|
80
|
+
(void)unlink (PIDFILE) ;
|
81
|
+
exit (EXIT_FAILURE) ;
|
82
|
+
}
|
83
|
+
|
84
|
+
void setupSigHandler (void)
|
85
|
+
{
|
86
|
+
struct sigaction action ;
|
87
|
+
|
88
|
+
sigemptyset (&action.sa_mask) ;
|
89
|
+
action.sa_flags = 0 ;
|
90
|
+
|
91
|
+
// Ignore what we can
|
92
|
+
|
93
|
+
action.sa_handler = SIG_IGN ;
|
94
|
+
|
95
|
+
sigaction (SIGHUP, &action, NULL) ;
|
96
|
+
sigaction (SIGTTIN, &action, NULL) ;
|
97
|
+
sigaction (SIGTTOU, &action, NULL) ;
|
98
|
+
|
99
|
+
// Trap what we can to exit gracefully
|
100
|
+
|
101
|
+
action.sa_handler = sigHandler ;
|
102
|
+
|
103
|
+
sigaction (SIGINT, &action, NULL) ;
|
104
|
+
sigaction (SIGQUIT, &action, NULL) ;
|
105
|
+
sigaction (SIGILL, &action, NULL) ;
|
106
|
+
sigaction (SIGABRT, &action, NULL) ;
|
107
|
+
sigaction (SIGFPE, &action, NULL) ;
|
108
|
+
sigaction (SIGSEGV, &action, NULL) ;
|
109
|
+
sigaction (SIGPIPE, &action, NULL) ;
|
110
|
+
sigaction (SIGALRM, &action, NULL) ;
|
111
|
+
sigaction (SIGTERM, &action, NULL) ;
|
112
|
+
sigaction (SIGUSR1, &action, NULL) ;
|
113
|
+
sigaction (SIGUSR2, &action, NULL) ;
|
114
|
+
sigaction (SIGCHLD, &action, NULL) ;
|
115
|
+
sigaction (SIGTSTP, &action, NULL) ;
|
116
|
+
sigaction (SIGBUS, &action, NULL) ;
|
117
|
+
}
|
118
|
+
|
119
|
+
|
120
|
+
/*
|
121
|
+
* The works...
|
122
|
+
*********************************************************************************
|
123
|
+
*/
|
124
|
+
|
125
|
+
int main (int argc, char *argv [])
|
126
|
+
{
|
127
|
+
int clientFd ;
|
128
|
+
char *p, *password ;
|
129
|
+
int i ;
|
130
|
+
int port = DEFAULT_SERVER_PORT ;
|
131
|
+
int wpiSetup = 0 ;
|
132
|
+
|
133
|
+
if (argc < 2)
|
134
|
+
{
|
135
|
+
fprintf (stderr, "Usage: %s %s\n", argv [0], usage) ;
|
136
|
+
exit (EXIT_FAILURE) ;
|
137
|
+
}
|
138
|
+
|
139
|
+
// Help?
|
140
|
+
|
141
|
+
if (strcasecmp (argv [1], "-h") == 0)
|
142
|
+
{
|
143
|
+
printf ("Usage: %s %s\n", argv [0], usage) ;
|
144
|
+
return 0 ;
|
145
|
+
}
|
146
|
+
|
147
|
+
// Daemonize?
|
148
|
+
// Must come before the other args as e.g. some extensions
|
149
|
+
// open files which get closed on daemonise...
|
150
|
+
|
151
|
+
if (strcasecmp (argv [1], "-d") == 0)
|
152
|
+
{
|
153
|
+
if (geteuid () != 0)
|
154
|
+
{
|
155
|
+
fprintf (stderr, "%s: Must be root to run as a daemon.\n", argv [0]) ;
|
156
|
+
exit (EXIT_FAILURE) ;
|
157
|
+
}
|
158
|
+
|
159
|
+
doDaemon = TRUE ;
|
160
|
+
daemonise (PIDFILE) ;
|
161
|
+
|
162
|
+
for (i = 2 ; i < argc ; ++i)
|
163
|
+
argv [i - 1] = argv [i] ;
|
164
|
+
--argc ;
|
165
|
+
}
|
166
|
+
|
167
|
+
// Scan all other arguments
|
168
|
+
|
169
|
+
while (*argv [1] == '-')
|
170
|
+
{
|
171
|
+
|
172
|
+
// Look for wiringPi setup arguments:
|
173
|
+
// Same as the gpio command and rtb.
|
174
|
+
|
175
|
+
// -g - bcm_gpio
|
176
|
+
|
177
|
+
if (strcasecmp (argv [1], "-g") == 0)
|
178
|
+
{
|
179
|
+
if (wpiSetup == 0)
|
180
|
+
{
|
181
|
+
logMsg ("BCM_GPIO mode selected") ;
|
182
|
+
wiringPiSetupGpio () ;
|
183
|
+
}
|
184
|
+
|
185
|
+
for (i = 2 ; i < argc ; ++i)
|
186
|
+
argv [i - 1] = argv [i] ;
|
187
|
+
--argc ;
|
188
|
+
++wpiSetup ;
|
189
|
+
continue ;
|
190
|
+
}
|
191
|
+
|
192
|
+
// -1 - physical pins
|
193
|
+
|
194
|
+
if (strcasecmp (argv [1], "-1") == 0)
|
195
|
+
{
|
196
|
+
if (wpiSetup == 0)
|
197
|
+
{
|
198
|
+
logMsg ("GPIO-PHYS mode selected") ;
|
199
|
+
wiringPiSetupPhys () ;
|
200
|
+
}
|
201
|
+
|
202
|
+
for (i = 2 ; i < argc ; ++i)
|
203
|
+
argv [i - 1] = argv [i] ;
|
204
|
+
--argc ;
|
205
|
+
++wpiSetup ;
|
206
|
+
continue ;
|
207
|
+
}
|
208
|
+
|
209
|
+
// -z - no wiringPi - blocks remotes accessing local pins
|
210
|
+
|
211
|
+
if (strcasecmp (argv [1], "-z") == 0)
|
212
|
+
{
|
213
|
+
if (wpiSetup == 0)
|
214
|
+
logMsg ("No GPIO mode selected") ;
|
215
|
+
|
216
|
+
for (i = 2 ; i < argc ; ++i)
|
217
|
+
argv [i - 1] = argv [i] ;
|
218
|
+
--argc ;
|
219
|
+
noLocalPins = TRUE ;
|
220
|
+
++wpiSetup ;
|
221
|
+
continue ;
|
222
|
+
}
|
223
|
+
|
224
|
+
// -p to select the port
|
225
|
+
|
226
|
+
if (strcasecmp (argv [1], "-p") == 0)
|
227
|
+
{
|
228
|
+
if (argc < 3)
|
229
|
+
{
|
230
|
+
logMsg ("-p missing extension port") ;
|
231
|
+
exit (EXIT_FAILURE) ;
|
232
|
+
}
|
233
|
+
|
234
|
+
logMsg ("Setting port to: %s", argv [2]) ;
|
235
|
+
|
236
|
+
port = atoi (argv [2]) ;
|
237
|
+
if ((port < 1) || (port > 65535))
|
238
|
+
{
|
239
|
+
logMsg ("Invalid server port: %d", port) ;
|
240
|
+
exit (EXIT_FAILURE) ;
|
241
|
+
}
|
242
|
+
|
243
|
+
// Shift args down by 2
|
244
|
+
|
245
|
+
for (i = 3 ; i < argc ; ++i)
|
246
|
+
argv [i - 2] = argv [i] ;
|
247
|
+
argc -= 2 ;
|
248
|
+
|
249
|
+
continue ;
|
250
|
+
}
|
251
|
+
|
252
|
+
// Check for -x argument to load in a new extension
|
253
|
+
// -x extension:base:args
|
254
|
+
// Can load many modules to extend the daemon.
|
255
|
+
|
256
|
+
if (strcasecmp (argv [1], "-x") == 0)
|
257
|
+
{
|
258
|
+
if (argc < 3)
|
259
|
+
{
|
260
|
+
logMsg ("-x missing extension name:data:etc.") ;
|
261
|
+
exit (EXIT_FAILURE) ;
|
262
|
+
}
|
263
|
+
|
264
|
+
logMsg ("Loading extension: %s", argv [2]) ;
|
265
|
+
|
266
|
+
if (!loadWPiExtension (argv [0], argv [2], TRUE))
|
267
|
+
{
|
268
|
+
logMsg ("Extension load failed: %s", strerror (errno)) ;
|
269
|
+
exit (EXIT_FAILURE) ;
|
270
|
+
}
|
271
|
+
|
272
|
+
// Shift args down by 2
|
273
|
+
|
274
|
+
for (i = 3 ; i < argc ; ++i)
|
275
|
+
argv [i - 2] = argv [i] ;
|
276
|
+
argc -= 2 ;
|
277
|
+
|
278
|
+
continue ;
|
279
|
+
}
|
280
|
+
|
281
|
+
logMsg ("Invalid parameter: %s", argv [1]) ;
|
282
|
+
exit (EXIT_FAILURE) ;
|
283
|
+
}
|
284
|
+
|
285
|
+
// Default to wiringPi mode
|
286
|
+
|
287
|
+
if (wpiSetup == 0)
|
288
|
+
{
|
289
|
+
logMsg ("WiringPi GPIO mode selected") ;
|
290
|
+
wiringPiSetup () ;
|
291
|
+
}
|
292
|
+
|
293
|
+
// Finally, should just be one arg left - the password...
|
294
|
+
|
295
|
+
if (argc != 2)
|
296
|
+
{
|
297
|
+
logMsg ("No password supplied") ;
|
298
|
+
exit (EXIT_FAILURE) ;
|
299
|
+
}
|
300
|
+
|
301
|
+
if (strlen (argv [1]) < 6)
|
302
|
+
{
|
303
|
+
logMsg ("Password too short - at least 6 chars, not %d", strlen (argv [1])) ;
|
304
|
+
exit (EXIT_FAILURE) ;
|
305
|
+
}
|
306
|
+
|
307
|
+
if ((password = malloc (strlen (argv [1]) + 1)) == NULL)
|
308
|
+
{
|
309
|
+
logMsg ("Out of memory") ;
|
310
|
+
exit (EXIT_FAILURE) ;
|
311
|
+
}
|
312
|
+
strcpy (password, argv [1]) ;
|
313
|
+
|
314
|
+
// Wipe out the password on the command-line in a vague attempt to try to
|
315
|
+
// hide it from snoopers
|
316
|
+
|
317
|
+
for (p = argv [1] ; *p ; ++p)
|
318
|
+
*p = ' ' ;
|
319
|
+
|
320
|
+
setupSigHandler () ;
|
321
|
+
|
322
|
+
// Enter our big loop
|
323
|
+
|
324
|
+
for (;;)
|
325
|
+
{
|
326
|
+
|
327
|
+
if (!doDaemon)
|
328
|
+
printf ("-=-\nWaiting for a new connection...\n") ;
|
329
|
+
|
330
|
+
if ((clientFd = setupServer (port)) < 0)
|
331
|
+
{
|
332
|
+
logMsg ("Unable to setup server: %s", strerror (errno)) ;
|
333
|
+
exit (EXIT_FAILURE) ;
|
334
|
+
}
|
335
|
+
|
336
|
+
logMsg ("New connection from: %s.", getClientIP ()) ;
|
337
|
+
|
338
|
+
if (!doDaemon)
|
339
|
+
printf ("Sending Greeting.\n") ;
|
340
|
+
|
341
|
+
if (sendGreeting (clientFd) < 0)
|
342
|
+
{
|
343
|
+
logMsg ("Unable to send greeting message: %s", strerror (errno)) ;
|
344
|
+
closeServer (clientFd) ;
|
345
|
+
continue ;
|
346
|
+
}
|
347
|
+
|
348
|
+
if (!doDaemon)
|
349
|
+
printf ("Sending Challenge.\n") ;
|
350
|
+
|
351
|
+
if (sendChallenge (clientFd) < 0)
|
352
|
+
{
|
353
|
+
logMsg ("Unable to send challenge message: %s", strerror (errno)) ;
|
354
|
+
closeServer (clientFd) ;
|
355
|
+
continue ;
|
356
|
+
}
|
357
|
+
|
358
|
+
if (!doDaemon)
|
359
|
+
printf ("Waiting for response.\n") ;
|
360
|
+
|
361
|
+
if (getResponse (clientFd) < 0)
|
362
|
+
{
|
363
|
+
logMsg ("Connection closed waiting for response: %s", strerror (errno)) ;
|
364
|
+
closeServer (clientFd) ;
|
365
|
+
continue ;
|
366
|
+
}
|
367
|
+
|
368
|
+
if (!passwordMatch (password))
|
369
|
+
{
|
370
|
+
logMsg ("Password failure") ;
|
371
|
+
closeServer (clientFd) ;
|
372
|
+
continue ;
|
373
|
+
}
|
374
|
+
|
375
|
+
logMsg ("Password OK - Starting") ;
|
376
|
+
|
377
|
+
runRemoteCommands (clientFd) ;
|
378
|
+
closeServer (clientFd) ;
|
379
|
+
}
|
380
|
+
|
381
|
+
return 0 ;
|
382
|
+
}
|