3 * Swiss-Army-Knife, Set-UID command-line interface to the Raspberry
5 * Copyright (c) 2012 Gordon Henderson
6 ***********************************************************************
7 * This file is part of wiringPi:
8 * https://projects.drogon.net/raspberry-pi/wiringpi/
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.
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.
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 ***********************************************************************
32 #include <sys/types.h>
36 #include <gertboard.h>
47 char *usage = "Usage: gpio -v\n"
49 " gpio [-g] <read/write/pwm/mode> ...\n"
50 " gpio [-p] <read/write/mode> ...\n"
51 " gpio export/edge/unexport/unexportall/exports ...\n"
52 " gpio drive <group> <value>\n"
53 " gpio pwm-bal/pwm-ms \n"
54 " gpio pwmr <range> \n"
55 " gpio load spi/i2c\n"
56 " gpio gbr <channel>\n"
57 " gpio gbw <channel> <value>\n" ;
62 * Change the ownership of the file to the real userId of the calling
63 * program so we can access it.
64 *********************************************************************************
67 static void changeOwner (char *cmd, char *file)
69 uid_t uid = getuid () ;
70 uid_t gid = getgid () ;
72 if (chown (file, uid, gid) != 0)
74 if (errno == ENOENT) // Warn that it's not there
75 fprintf (stderr, "%s: Warning: File not present: %s\n", cmd, file) ;
78 fprintf (stderr, "%s: Unable to change ownership of %s: %s\n", cmd, file, strerror (errno)) ;
87 * Return true/false if the supplied module is loaded
88 *********************************************************************************
91 static int moduleLoaded (char *modName)
93 int len = strlen (modName) ;
95 FILE *fd = fopen ("/proc/modules", "r") ;
100 fprintf (stderr, "gpio: Unable to check modules: %s\n", strerror (errno)) ;
104 while (fgets (line, 80, fd) != NULL)
106 if (strncmp (line, modName, len) != 0)
121 * Load either the spi or i2c modules and change device ownerships, etc.
122 *********************************************************************************
125 static void _doLoadUsage (char *argv [])
127 fprintf (stderr, "Usage: %s load <spi/i2c>\n", argv [0]) ;
131 static void doLoad (int argc, char *argv [])
135 char *file1, *file2 ;
138 _doLoadUsage (argv) ;
140 /**/ if (strcasecmp (argv [2], "spi") == 0)
142 module = "spi_bcm2708" ;
143 file1 = "/dev/spidev0.0" ;
144 file2 = "/dev/spidev0.1" ;
146 else if (strcasecmp (argv [2], "i2c") == 0)
148 module = "i2c_bcm2708" ;
149 file1 = "/dev/i2c-0" ;
150 file2 = "/dev/i2c-1" ;
153 _doLoadUsage (argv) ;
155 if (!moduleLoaded (module))
157 sprintf (cmd, "modprobe %s", module) ;
161 if (!moduleLoaded (module))
163 fprintf (stderr, "%s: Unable to load %s\n", argv [0], module) ;
167 sleep (1) ; // To let things get settled
169 changeOwner (argv [0], file1) ;
170 changeOwner (argv [0], file2) ;
177 * List all GPIO exports
178 *********************************************************************************
181 static void doExports (int argc, char *argv [])
188 // Rather crude, but who knows what others are up to...
190 for (first = 0, i = 0 ; i < 64 ; ++i)
193 // Try to read the direction
195 sprintf (fName, "/sys/class/gpio/gpio%d/direction", i) ;
196 if ((fd = open (fName, O_RDONLY)) == -1)
202 printf ("GPIO Pins exported:\n") ;
205 printf ("%4d: ", i) ;
207 if ((l = read (fd, buf, 16)) == 0)
208 sprintf (buf, "%s", "?") ;
211 if ((buf [strlen (buf) - 1]) == '\n')
212 buf [strlen (buf) - 1] = 0 ;
214 printf ("%-3s", buf) ;
218 // Try to Read the value
220 sprintf (fName, "/sys/class/gpio/gpio%d/value", i) ;
221 if ((fd = open (fName, O_RDONLY)) == -1)
223 printf ("No Value file (huh?)\n") ;
227 if ((l = read (fd, buf, 16)) == 0)
228 sprintf (buf, "%s", "?") ;
231 if ((buf [strlen (buf) - 1]) == '\n')
232 buf [strlen (buf) - 1] = 0 ;
234 printf (" %s", buf) ;
236 // Read any edge trigger file
238 sprintf (fName, "/sys/class/gpio/gpio%d/edge", i) ;
239 if ((fd = open (fName, O_RDONLY)) == -1)
245 if ((l = read (fd, buf, 16)) == 0)
246 sprintf (buf, "%s", "?") ;
249 if ((buf [strlen (buf) - 1]) == '\n')
250 buf [strlen (buf) - 1] = 0 ;
252 printf (" %-8s\n", buf) ;
261 * gpio export pin mode
262 * This uses the /sys/class/gpio device interface.
263 *********************************************************************************
266 void doExport (int argc, char *argv [])
275 fprintf (stderr, "Usage: %s export pin mode\n", argv [0]) ;
279 pin = atoi (argv [2]) ;
283 if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
285 fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
289 fprintf (fd, "%d\n", pin) ;
292 sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
293 if ((fd = fopen (fName, "w")) == NULL)
295 fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
299 /**/ if ((strcasecmp (mode, "in") == 0) || (strcasecmp (mode, "input") == 0))
300 fprintf (fd, "in\n") ;
301 else if ((strcasecmp (mode, "out") == 0) || (strcasecmp (mode, "output") == 0))
302 fprintf (fd, "out\n") ;
305 fprintf (stderr, "%s: Invalid mode: %s. Should be in or out\n", argv [1], mode) ;
311 // Change ownership so the current user can actually use it!
313 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
314 changeOwner (argv [0], fName) ;
316 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
317 changeOwner (argv [0], fName) ;
325 * Easy access to changing the edge trigger on a GPIO pin
326 * This uses the /sys/class/gpio device interface.
327 *********************************************************************************
330 void doEdge (int argc, char *argv [])
339 fprintf (stderr, "Usage: %s edge pin mode\n", argv [0]) ;
343 pin = atoi (argv [2]) ;
346 // Export the pin and set direction to input
348 if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
350 fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
354 fprintf (fd, "%d\n", pin) ;
357 sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
358 if ((fd = fopen (fName, "w")) == NULL)
360 fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
364 fprintf (fd, "in\n") ;
367 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
368 if ((fd = fopen (fName, "w")) == NULL)
370 fprintf (stderr, "%s: Unable to open GPIO edge interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
374 /**/ if (strcasecmp (mode, "none") == 0) fprintf (fd, "none\n") ;
375 else if (strcasecmp (mode, "rising") == 0) fprintf (fd, "rising\n") ;
376 else if (strcasecmp (mode, "falling") == 0) fprintf (fd, "falling\n") ;
377 else if (strcasecmp (mode, "both") == 0) fprintf (fd, "both\n") ;
380 fprintf (stderr, "%s: Invalid mode: %s. Should be none, rising, falling or both\n", argv [1], mode) ;
384 // Change ownership of the value and edge files, so the current user can actually use it!
386 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
387 changeOwner (argv [0], fName) ;
389 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
390 changeOwner (argv [0], fName) ;
399 * This uses the /sys/class/gpio device interface.
400 *********************************************************************************
403 void doUnexport (int argc, char *argv [])
410 fprintf (stderr, "Usage: %s unexport pin\n", argv [0]) ;
414 pin = atoi (argv [2]) ;
416 if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
418 fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ;
422 fprintf (fd, "%d\n", pin) ;
430 * Un-Export all the GPIO pins.
431 * This uses the /sys/class/gpio device interface.
432 *********************************************************************************
435 void doUnexportall (int argc, char *argv [])
440 for (pin = 0 ; pin < 63 ; ++pin)
442 if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
444 fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ;
447 fprintf (fd, "%d\n", pin) ;
455 * gpio mode pin mode ...
456 *********************************************************************************
459 void doMode (int argc, char *argv [])
466 fprintf (stderr, "Usage: %s mode pin mode\n", argv [0]) ;
470 pin = atoi (argv [2]) ;
472 if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
477 /**/ if (strcasecmp (mode, "in") == 0) pinMode (pin, INPUT) ;
478 else if (strcasecmp (mode, "out") == 0) pinMode (pin, OUTPUT) ;
479 else if (strcasecmp (mode, "pwm") == 0) pinMode (pin, PWM_OUTPUT) ;
480 else if (strcasecmp (mode, "up") == 0) pullUpDnControl (pin, PUD_UP) ;
481 else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ;
482 else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ;
485 fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/up/down/tri\n", argv [1], mode) ;
493 * gpio drive group value
494 *********************************************************************************
497 static void doPadDrive (int argc, char *argv [])
503 fprintf (stderr, "Usage: %s drive group value\n", argv [0]) ;
507 group = atoi (argv [2]) ;
508 val = atoi (argv [3]) ;
510 if ((group < 0) || (group > 2))
512 fprintf (stderr, "%s: drive group not 0, 1 or 2: %d\n", argv [0], group) ;
516 if ((val < 0) || (val > 7))
518 fprintf (stderr, "%s: drive value not 0-7: %d\n", argv [0], val) ;
522 setPadDrive (group, val) ;
528 * gpio gbw channel value
529 *********************************************************************************
532 static void doGbw (int argc, char *argv [])
538 fprintf (stderr, "Usage: %s gbr <channel> <value>\n", argv [0]) ;
542 channel = atoi (argv [2]) ;
543 value = atoi (argv [3]) ;
545 if ((channel < 0) || (channel > 1))
547 fprintf (stderr, "%s: channel must be 0 or 1\n", argv [0]) ;
551 if ((value < 0) || (value > 1023))
553 fprintf (stderr, "%s: value must be from 0 to 255\n", argv [0]) ;
557 if (gertboardSPISetup () == -1)
559 fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
563 gertboardAnalogWrite (channel, value) ;
570 *********************************************************************************
573 static void doGbr (int argc, char *argv [])
579 fprintf (stderr, "Usage: %s gbr <channel>\n", argv [0]) ;
583 channel = atoi (argv [2]) ;
585 if ((channel < 0) || (channel > 1))
587 fprintf (stderr, "%s: channel must be 0 or 1\n", argv [0]) ;
591 if (gertboardSPISetup () == -1)
593 fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
597 printf ("%d\n",gertboardAnalogRead (channel)) ;
604 * gpio write pin value
605 *********************************************************************************
608 static void doWrite (int argc, char *argv [])
614 fprintf (stderr, "Usage: %s write pin value\n", argv [0]) ;
618 pin = atoi (argv [2]) ;
620 if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
623 val = atoi (argv [3]) ;
626 digitalWrite (pin, LOW) ;
628 digitalWrite (pin, HIGH) ;
634 * Read a pin and return the value
635 *********************************************************************************
638 void doRead (int argc, char *argv [])
644 fprintf (stderr, "Usage: %s read pin\n", argv [0]) ;
648 pin = atoi (argv [2]) ;
650 if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
656 val = digitalRead (pin) ;
658 printf ("%s\n", val == 0 ? "0" : "1") ;
664 * Output a PWM value on a pin
665 *********************************************************************************
668 void doPwm (int argc, char *argv [])
674 fprintf (stderr, "Usage: %s pwm <pin> <value>\n", argv [0]) ;
678 pin = atoi (argv [2]) ;
680 if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
683 val = atoi (argv [3]) ;
685 pwmWrite (pin, val) ;
690 * doPwmMode: doPwmRange:
691 * Change the PWM mode and Range values
692 *********************************************************************************
695 static void doPwmMode (int mode)
700 static void doPwmRange (int argc, char *argv [])
706 fprintf (stderr, "Usage: %s pwmr <range>\n", argv [0]) ;
710 range = (unsigned int)strtoul (argv [2], NULL, 10) ;
714 fprintf (stderr, "%s: range must be > 0\n", argv [0]) ;
718 pwmSetRange (range) ;
725 *********************************************************************************
728 int main (int argc, char *argv [])
734 fprintf (stderr, "%s: %s\n", argv [0], usage) ;
738 if (strcasecmp (argv [1], "-h") == 0)
740 printf ("%s: %s\n", argv [0], usage) ;
744 if (strcasecmp (argv [1], "-v") == 0)
746 printf ("gpio version: %s\n", VERSION) ;
747 printf ("Copyright (c) 2012 Gordon Henderson\n") ;
748 printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ;
749 printf ("For details type: %s -warranty\n", argv [0]) ;
753 if (strcasecmp (argv [1], "-warranty") == 0)
755 printf ("gpio version: %s\n", VERSION) ;
756 printf ("Copyright (c) 2012 Gordon Henderson\n") ;
758 printf (" This program is free software; you can redistribute it and/or modify\n") ;
759 printf (" it under the terms of the GNU Leser General Public License as published\n") ;
760 printf (" by the Free Software Foundation, either version 3 of the License, or\n") ;
761 printf (" (at your option) any later version.\n") ;
763 printf (" This program is distributed in the hope that it will be useful,\n") ;
764 printf (" but WITHOUT ANY WARRANTY; without even the implied warranty of\n") ;
765 printf (" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n") ;
766 printf (" GNU Lesser General Public License for more details.\n") ;
768 printf (" You should have received a copy of the GNU Lesser General Public License\n") ;
769 printf (" along with this program. If not, see <http://www.gnu.org/licenses/>.\n") ;
776 fprintf (stderr, "%s: Must be root to run. Program should be suid root. This is an error.\n", argv [0]) ;
780 // Initial test for /sys/class/gpio operations:
782 /**/ if (strcasecmp (argv [1], "exports" ) == 0) { doExports (argc, argv) ; return 0 ; }
783 else if (strcasecmp (argv [1], "export" ) == 0) { doExport (argc, argv) ; return 0 ; }
784 else if (strcasecmp (argv [1], "edge" ) == 0) { doEdge (argc, argv) ; return 0 ; }
785 else if (strcasecmp (argv [1], "unexportall") == 0) { doUnexportall (argc, argv) ; return 0 ; }
786 else if (strcasecmp (argv [1], "unexport" ) == 0) { doUnexport (argc, argv) ; return 0 ; }
788 // Check for drive or load commands:
790 if (strcasecmp (argv [1], "drive") == 0) { doPadDrive (argc, argv) ; return 0 ; }
791 if (strcasecmp (argv [1], "load" ) == 0) { doLoad (argc, argv) ; return 0 ; }
793 // Gertboard commands
795 if (strcasecmp (argv [1], "gbr" ) == 0) { doGbr (argc, argv) ; return 0 ; }
796 if (strcasecmp (argv [1], "gbw" ) == 0) { doGbw (argc, argv) ; return 0 ; }
798 // Check for -g argument
800 if (strcasecmp (argv [1], "-g") == 0)
802 if (wiringPiSetupGpio () == -1)
804 fprintf (stderr, "%s: Unable to initialise GPIO mode.\n", argv [0]) ;
808 for (i = 2 ; i < argc ; ++i)
809 argv [i - 1] = argv [i] ;
811 wpMode = WPI_MODE_GPIO ;
814 // Check for -p argument for PiFace
816 else if (strcasecmp (argv [1], "-p") == 0)
818 if (wiringPiSetupPiFaceForGpioProg () == -1)
820 fprintf (stderr, "%s: Unable to initialise PiFace.\n", argv [0]) ;
824 for (i = 2 ; i < argc ; ++i)
825 argv [i - 1] = argv [i] ;
827 wpMode = WPI_MODE_PIFACE ;
830 // Default to wiringPi mode
834 if (wiringPiSetup () == -1)
836 fprintf (stderr, "%s: Unable to initialise wiringPi mode\n", argv [0]) ;
839 wpMode = WPI_MODE_PINS ;
842 // Check for PWM operations
844 if (wpMode != WPI_MODE_PIFACE)
846 if (strcasecmp (argv [1], "pwm-bal") == 0) { doPwmMode (PWM_MODE_BAL) ; return 0 ; }
847 if (strcasecmp (argv [1], "pwm-ms") == 0) { doPwmMode (PWM_MODE_MS) ; return 0 ; }
848 if (strcasecmp (argv [1], "pwmr") == 0) { doPwmRange (argc, argv) ; return 0 ; }
851 // Check for wiring commands
853 /**/ if (strcasecmp (argv [1], "read" ) == 0) doRead (argc, argv) ;
854 else if (strcasecmp (argv [1], "write") == 0) doWrite (argc, argv) ;
855 else if (strcasecmp (argv [1], "pwm" ) == 0) doPwm (argc, argv) ;
856 else if (strcasecmp (argv [1], "mode" ) == 0) doMode (argc, argv) ;
859 fprintf (stderr, "%s: Unknown command: %s.\n", argv [0], argv [1]) ;