90 likes | 315 Views
CS423 Dick Steflik. Alternate User Interfaces. User Interfaces. Traditional User Interfaces for Devices Allow the user control of/over the internal state of the device Buttons/Keys Lamps, LEDs, LCD panels Knobs, sliders
E N D
CS423 Dick Steflik Alternate User Interfaces
User Interfaces • Traditional User Interfaces for Devices • Allow the user control of/over the internal state of the device • Buttons/Keys • Lamps, LEDs, LCD panels • Knobs, sliders • On devices with embedded microcontrollers this can also be done via a web based user Interface (web page)
Common Gateway Interface • A specification that describes how a web server exchanges data with a web program • How data from a web form is sent to a web server • What the web server does with the data • How the web server activates a specific program • How the program gets the user data and how it responds • http://hoohoo.ncsa.uiuc.edu/cgi/
CGI • Standard HTTP used • can use GET or POST methods to send data • GET sends data (as name/value pairs) attached to URL • POST send data (as name/value pairs) in the HTTP body • Server will receive the request packet and place the data in an environment variable called QUERYSTRING and place it in SYSIN so that it can be retrieved by the processing program • SYSOUT is connected to the outgoing socket of the web server so that whatever is sent to sysout will go back as the body of the response packet (this could be a subsequent web page or just a positive response)
Pictorially Browser Request Packet SYSIN Web Server C Program SYSOUT Response Packet
cgipio.c /* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. */ #include <stdio.h> #include <errno.h> #include <fcntl.h> #include <string.h> #include <unistd.h> #define BUFFERSIZE 257 #define LED_OFFSET 19 int main(int argc, char *argv[]) { int i = 0; int pio = 0; int retval = 0; int toggle = 0; int led_on = -1; int read_bytes = 0; unsigned int pio_buffer; char buffer[BUFFERSIZE]; /* Read in stuff from stdinn */ while ((i = read(0, buffer, BUFFERSIZE-1)) != 0) { read_bytes += i; } buffer[read_bytes] = '\0'; if(strstr(buffer, "toggle=1") != 0) { toggle = 1; }
(cont.) /* Print basic headers */ printf("HTTP/1.0 200 OK\n"); printf("Content-type: text/html\n\n"); printf( "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n" "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" "<link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" href=\"/webif.css\" />" "<?xml-stylesheet href=\"/include/style.css\" type=\"text/css\" media=\"screen\" ?>" "<head>\n" "<title>AVR32 NGW pin toggler</title>\n" "</head>\n" "<body style=\"margin: 30px; color: #C3C4D2; background-color: #213242;\">\n" ); printf("<h1>AVR32 NGW pin toggler</h1>\n"); printf("<p><a style=\"color: inherit;\" href=\"/\">Back to home...</a></p>\n"); printf("<br />"); printf("<p>");
(cont.) /* Get status on LED */ pio = open("/dev/gpio1", O_RDWR|O_NONBLOCK); get_status: if (pio < 0) { printf("Error: I could not open the GPIO device.</p>\n"); goto error; } else { retval = read(pio, &pio_buffer, 4); if (retval == 4) { led_on = pio_buffer & (1<<LED_OFFSET) ? 0 : 1; } else { if (retval >= 0) { printf("Error: I could only read %d bytes from the GPIO device.</p>\n", retval); } else { if (errno != EAGAIN) { printf("Error: I could not read from the GPIO device (%d).</p>\n", errno); } } } }
if (led_on) { printf("LED is on => "); } else { printf("LED is off => "); } if (toggle == 1 && led_on == 1) { unsigned int off = 1<<LED_OFFSET; write(pio, &off, 4); printf("turning LED off => "); toggle = 0; goto get_status; } else if (toggle == 1 && led_on == 0) { unsigned int on = 0; write(pio, &on, 4); printf("turning LED on => "); toggle = 0; goto get_status; } else if (toggle == 1) { printf( "LED was ordered to be toggled, but I could " "not find out if I was supposed to turn it " "off or on, I am very sorry :'(" ); } else if (led_on) { printf("do you want to turn it off?"); } else { printf("do you want to turn it on?"); } printf("</p>\n"); printf("<br />\n"); printf( "<div><form method=\"post\" action=\"cgipio.cgi\" " "name=\"cgipio\">\n" "<input type=\"hidden\" name=\"toggle\" value=\"1\" />\n" "<input type=\"submit\" value=\"Change LED\" />\n" "</form></div>\n" ); close(pio); error: printf("</body></html>\n"); return 0; } (cont.)