Skip to content

Commit

Permalink
added the CGI get example
Browse files Browse the repository at this point in the history
  • Loading branch information
Derek Molloy committed Mar 25, 2015
1 parent 34f6e51 commit 20bea25
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 1 deletion.
51 changes: 51 additions & 0 deletions chp10/cgicc/LED.cpp
@@ -0,0 +1,51 @@
#include "LED.h"

LED::LED(int number){
this->number = number;
// much easier with C++11 using to_string(number)
ostringstream s; // using a stream to contruct the path
s << LED_PATH << number; //append LED number to LED_PATH
path = string(s.str()); //convert back from stream to string
}

void LED::writeLED(string filename, string value){
ofstream fs;
fs.open((path + filename).c_str());
fs << value;
fs.close();
}

void LED::removeTrigger(){
writeLED("/trigger", "none");
}

void LED::turnOn(){
cout << "Turning LED" << number << " on." << endl;
removeTrigger();
writeLED("/brightness", "1");
}

void LED::turnOff(){
cout << "Turning LED" << number << " off." << endl;
removeTrigger();
writeLED("/brightness", "0");
}

void LED::flash(string delayms = "50"){
cout << "Making LED" << number << " flash." << endl;
writeLED("/trigger", "timer");
writeLED("/delay_on", delayms);
writeLED("/delay_off", delayms);
}

void LED::outputState(){
ifstream fs;
fs.open( (path + "/trigger").c_str());
string line;
while(getline(fs,line)) cout << line << endl;
fs.close();
}

LED::~LED(){
cout << "destroying the LED with path: " << path << endl;
}
36 changes: 36 additions & 0 deletions chp10/cgicc/LED.h
@@ -0,0 +1,36 @@
/** Simple On-board LED flashing program - written by Derek Molloy
* simple OOP struture for the Exploring BeagleBone book
*
* This program uses all four LEDS and can be executed in three ways:
* makeLEDs on
* makeLEDs off
* makeLEDs flash (flash at time delay intervals)
* makeLEDs status (get the trigger status)
*
* Written by Derek Molloy for the book "Exploring BeagleBone: Tools and
* Techniques for Building with Embedded Linux" by John Wiley & Sons, 2014
* ISBN 9781118935125. Please see the file README.md in the repository root
* directory for copyright and GNU GPLv3 license information. */

#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
using namespace std;

#define LED_PATH "/sys/class/leds/beaglebone:green:usr"

class LED{
private:
string path;
int number;
virtual void writeLED(string filename, string value);
virtual void removeTrigger();
public:
LED(int number);
virtual void turnOn();
virtual void turnOff();
virtual void flash(string delayms);
virtual void outputState();
virtual ~LED();
};
3 changes: 3 additions & 0 deletions chp10/cgicc/build
@@ -1,3 +1,6 @@
#!/bin/bash
echo "Building the hello.cgi C++ CGI Program"
g++ hello.cpp -o hello.cgi

echo "Building the getLED.cgi C++ CGI Program"
g++ getLED.cpp LED.cpp -o getLED.cgi -lcgicc
4 changes: 4 additions & 0 deletions chp10/cgicc/deploy
@@ -0,0 +1,4 @@
#!/bin/bash
echo "Copying the cgi files to the /usr/lib/cgi-bin/"
sudo cp *.cgi /usr/lib/cgi-bin/
sudo chmod +s /usr/lib/cgi-bin/getLED.cgi
Binary file added chp10/cgicc/getLED.cgi
Binary file not shown.
53 changes: 53 additions & 0 deletions chp10/cgicc/getLED.cpp
@@ -0,0 +1,53 @@
/* C++ CGI BeagleBone GET example -- Written by Derek Molloy (www.derekmolloy.ie)
You must set the sticky bit for this script in order that it can
access the on-board LED sysfs file system.
*/

#include <iostream> // for the input/output
#include <stdlib.h> // for the getenv call
#include <sys/sysinfo.h> // for the system uptime call

#include <cgicc/Cgicc.h> // the cgicc headers
#include <cgicc/CgiDefs.h>
#include <cgicc/HTTPHTMLHeader.h>
#include <cgicc/HTMLClasses.h>

#include "LED.h" // the LED class from Chapter 5 of the book

using namespace std;
using namespace cgicc;

int main(){
Cgicc form;
LED *led3 = new LED(3);

char *value = getenv("REMOTE_ADDR"); // The remote address CGI environment variable
cout << "Content-type:text/html\r\n\r\n"; // Generate the HTML output
cout << "<html><head>\n";
cout << "<title>EBB C++ GET Example</title>\n";
cout << "</head><body>\n";
cout << "<h1>BeagleBone GET Example</h1>\n";

form_iterator it = form.getElement("command");
string cmd(**it);
if (!it->isEmpty() && it!=(*form).end()) {
cout << "<div> The LED command is " << cmd << ".</div>";

/** This code sets the USR3 LED state using the LED class **/
if(cmd=="on") led3->turnOn();
else if(cmd=="off") led3->turnOff();
else if(cmd=="flash") led3->flash("100");
else if(cmd=="status"){
cout << "<div>";
led3->outputState();
cout << "</div>";
}
else cout << "<div> Invalid command! </div>";
}
else{
cout << "<div> The LED command is missing or invalid.</div>";
}
cout << "<div> The CGI REMOTE_ADDR environment variable is " << value << "</div>";
cout << "</body></html>\n";
return 0;
}
2 changes: 1 addition & 1 deletion chp10/cgicc/hello.cpp
Expand Up @@ -15,7 +15,7 @@ int main(){
cout << "</head><body>\n";
cout << "<h1>BeagleBone System Uptime</h1>\n";
int mins = info.uptime / 60; // the uptime comes from the sysinfo struct
int ram = info.freeram / 1024 / 1024; // the available memory in Mb
int ram = info.freeram / 1024 / 1024; // the available memory in Mb
cout << "<div> The BBB system uptime is " << mins << " minutes.\n";
cout << "There is " << ram << " Mb of memory available.</div>\n";
cout << "<div> The CGI REMOTE_ADDR environment variable is " << value << "</div>";
Expand Down

0 comments on commit 20bea25

Please sign in to comment.