This application translates HTTP GET requests into the proper RS232 commands to control a Sharp Aquos TV
Dependencies: EthernetInterface mbed-rtos mbed
Revision 0:427a14ebab60, committed 2014-07-29
- Comitter:
- davisw00
- Date:
- Tue Jul 29 23:01:55 2014 +0000
- Child:
- 1:64bfc05e387d
- Commit message:
- Initial working copy of HTTP-to-RS232 server for a Sharp Aquos TV
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AquosHTTP.cpp Tue Jul 29 23:01:55 2014 +0000 @@ -0,0 +1,89 @@ + #include "AquosHTTP.h" + + +AquosHTTP::AquosHTTP(DebugPort *dbg) { + m_dbg = dbg; + init(); +} + +void AquosHTTP::arrayinit(char* ary) { + memset(ary,0,sizeof(char)*BUF_SIZE); +} +void AquosHTTP::init() { + arrayinit(m_buffer); + arrayinit(m_response); +} + +AquosHTTP::~AquosHTTP() { + m_eth.disconnect(); +} + +AquosHTTP& AquosHTTP::init(const char* ipaddr, const char* netmask, const char* gateway) { + int ret; + init(); + ret = m_eth.init(ipaddr,netmask,gateway); + if(ret<0) { + m_dbg->send("EthernetInterface failed initialization",1); + } else { + m_dbg->send("EthernetInterface initialized: "); + m_dbg->send(m_eth.getIPAddress()); + m_dbg->send("\n\r"); + } + + wait(0.1); + m_eth.connect(1000); //usually fails, so no throw + wait(0.1); + //m_server.set_blocking(true); + ret = m_server.bind(80); + if(ret<0) m_dbg->send("Server failed to bind",1); + ret = m_server.listen(1); + if(ret<0) m_dbg->send("Server failed to listen",1); + + return *this; +} + + +AquosHTTP& AquosHTTP::waitForRequest() { + int n; + int ret; + ret = m_server.accept(m_client); + if(ret<0) { + m_dbg->send("Error accepting client request"); + return *this; + } + n = m_client.receive(m_buffer,BUF_SIZE-1); + if(n>=BUF_SIZE-1) { + m_dbg->send("Request exceeds buffer ... ignoring"); + return *this; + } + m_buffer[BUF_SIZE-1] = '\0'; + m_buffer[n] = '\0'; + return *this; +} + +const char* AquosHTTP::getRequest() const {return m_buffer; } + +int AquosHTTP::setResponse(const char* msg) { + snprintf(m_response,BUF_SIZE-1,"<html><body>%s for:<br/><pre>%s</pre></body></html>\n",msg,m_buffer); + return strlen(m_response); +} + +AquosHTTP& AquosHTTP::returnSuccess() { + int msglen = setResponse("SUCCESS!"); + char* buf = new char[msglen + 256]; + snprintf(buf,msglen+255,"HTTP/1.1 200 OK\r\nContent-Length: %i\r\nConnection: close\r\nContent-type: text/html\r\n\r\n%s",msglen,m_response); + m_client.send(buf,strlen(buf)); + m_client.close(); + delete [] buf; + return *this; +} + +AquosHTTP& AquosHTTP::returnFailure() { + int msglen = setResponse("FAILED!"); + char* buf = new char[msglen + 256]; + snprintf(buf,msglen+255,"HTTP/1.1 501 Not Implemented\r\nContent-Length: %i\r\nConnection: close\r\nContent-type: text/html\r\n\r\n%s",msglen,m_response); + m_client.send(buf,strlen(buf)); + m_client.close(); + delete [] buf; + return *this; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AquosHTTP.h Tue Jul 29 23:01:55 2014 +0000 @@ -0,0 +1,41 @@ +#ifndef _AQUOSHTTP +#define _AQUOSHTTP + +#include "EthernetInterface.h" +#include "rtos.h" +#include <string.h> +#include "DebugPort.h" + +class AquosHTTP { + private: + const static unsigned int BUF_SIZE = 256; + DebugPort* m_dbg; + + protected: + EthernetInterface m_eth; + TCPSocketServer m_server; + TCPSocketConnection m_client; + + char m_buffer[BUF_SIZE]; + char m_response[BUF_SIZE]; + + void init(); + void arrayinit(char* ary); + + int setResponse(const char* msg); + + public: + AquosHTTP(DebugPort* dbg); + ~AquosHTTP(); + + AquosHTTP& init(const char *ipaddr, const char* netmask, const char* gateway); + + AquosHTTP& waitForRequest(); + const char* getRequest() const; + + AquosHTTP& returnSuccess(); + AquosHTTP& returnFailure(); +}; + + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AquosTV.cpp Tue Jul 29 23:01:55 2014 +0000 @@ -0,0 +1,83 @@ +#include "AquosTV.h" + +AquosTV::AquosTV(DebugPort* dbg) { + m_dbg = dbg; + init(); +} + +AquosTV::~AquosTV() { + delete m_tv; +} + +void AquosTV::init() { + m_tv = new Serial(PTC17,PTC16); + m_tv->baud(9600); + m_tv->format(8,SerialBase::None,1); + memset(httpcmd,0,sizeof(char)*BUF_SIZE); + memset(rs232cmd,0,sizeof(char)*BUF_SIZE); +} + +bool AquosTV::processCommand(const char* httpin) { + int i; + int j; + bool ret=false; + + char request[BUF_SIZE]; + strncpy(httpcmd,httpin,BUF_SIZE-1); + httpcmd[AQUOSBUF-1] = '\0'; + rs232cmd[0]='\0'; + if(httpcmd[0] == 'G' && httpcmd[1] == 'E' && httpcmd[2]=='T' && httpcmd[3]==' ' && httpcmd[4]=='/') { + for(i=5;httpcmd[i]!=' '&&httpcmd[i]!='\0'&&i<AQUOSBUF-1;++i) { + request[i-5]=(httpcmd[i] | 0x20); //make lower case + } + request[i-5]='\0'; + m_dbg->send("\n\rReceived Command Request: "); + m_dbg->send(request); + m_dbg->send("\n\r"); + + if(strncmp(request,"poweron",BUF_SIZE-1)==0) { + strncpy(rs232cmd,"POWR1 \r",BUF_SIZE-1); + ret = true; + } + else if(strncmp(request,"poweroff",BUF_SIZE-1)==0) { + strncpy(rs232cmd,"POWR0 \r",BUF_SIZE-1); + ret = true; + } + else if(strncmp(request,"selecttv",BUF_SIZE-1)==0) { + strncpy(rs232cmd,"ITVD0 \r",BUF_SIZE-1); + ret = true; + } + else if(strncmp(request,"input",5)==0) { + i=request[5]-'0'; + if(i>0 && i<10) { + snprintf(rs232cmd,BUF_SIZE-1,"IAVD%i \r",i); + ret = true; + } + } + else if(strncmp(request,"volume",6)==0) { + i=request[6]-'0'; + j=request[7]-'0'; + if(i>=0&&i<10) { + if(j>=0&&j<10) { + i = 10*i + j; + } + snprintf(rs232cmd,BUF_SIZE-1,"VOLM%02i \r",i); + ret = true; + } + } + } + + if(ret) { + m_dbg->send("\n\rCommand Interpreted: "); + m_dbg->send(rs232cmd); + m_dbg->send("\n"); + m_tv->printf(rs232cmd); + } + else { + m_dbg->send("Unknown Command Request\n\r"); + } + return ret; +} + +const char* AquosTV::tvcmd() { return rs232cmd; } +const char* AquosTV::http() { return httpcmd; } \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AquosTV.h Tue Jul 29 23:01:55 2014 +0000 @@ -0,0 +1,35 @@ +#ifndef _AQUOSTV +#define _AQUOSTV +#include "mbed.h" +#include <string.h> +#include <stdio.h> + +#include "DebugPort.h" + + + +#define AQUOSBUF 128 + +class AquosTV { + private: + const static unsigned int BUF_SIZE = 256; + DebugPort *m_dbg; + + protected: + Serial* m_tv; + char httpcmd[128]; + char rs232cmd[128]; + + void init(); + + public: + AquosTV(DebugPort *dbg); + virtual ~AquosTV(); + + bool processCommand(const char* httpin); + const char* http() ; + const char* tvcmd() ; +}; + + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DebugPort.h Tue Jul 29 23:01:55 2014 +0000 @@ -0,0 +1,52 @@ +#ifndef _DebugPort +#define _DebugPort +#include "mbed.h" + +class DebugPort { + protected: + //Serial m_pc(USBTX, USBRX); + Serial* m_pc; + DigitalOut* led_red; + DigitalOut* led_green; + DigitalOut* led_blue; + + public: + void setLED(bool r, bool g, bool b) { + *led_red = !r; + *led_green = !g; + *led_blue = !b; + } + + DebugPort() { + m_pc = new Serial(USBTX,USBRX); + led_red = new DigitalOut(LED_RED); + led_green = new DigitalOut(LED_GREEN); + led_blue = new DigitalOut(LED_BLUE); + + m_pc->baud(115200); + m_pc->format(8,SerialBase::None,1); + setbuf(*m_pc,NULL); // turn off buffering of printf output + setLED(0,0,0); + } + ~DebugPort() { + delete led_red; + delete led_green; + delete led_blue; + delete m_pc; + } + void send(const char* msg, int level=0) { + m_pc->printf(msg); + if(level>0) { // major fail. stop processing and toggle led + while(true) { + setLED(0,0,0); + wait(0.1); + setLED(1,0,0); + wait(0.1); + } + } + } + +}; + + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EthernetInterface.lib Tue Jul 29 23:01:55 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/EthernetInterface/#e6b79f0ccd95
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Jul 29 23:01:55 2014 +0000 @@ -0,0 +1,38 @@ +#include "mbed.h" +#include "EthernetInterface.h" +#include "rtos.h" +#include "DebugPort.h" +#include "AquosHTTP.h" +#include "AquosTV.h" +#include <string.h> + +#define DBG 1 + +const char ip[] = "192.168.16.36"; +const char mask[] = "255.255.255.0"; +const char gateway[] = "192.168.16.1"; + +int main() { + bool loop=true; + DebugPort dbg; + dbg.setLED(0,1,1); //yellow for init + wait(0.1); + + AquosHTTP server(&dbg); + server.init(ip,mask,gateway); + AquosTV tv(&dbg); + + + while(loop) { + dbg.setLED(0,0,0); //off while waiting + server.waitForRequest(); + dbg.setLED(0,0,1); //blue in progress + if( tv.processCommand( server.getRequest() )) { + dbg.setLED(0,1,0); + server.returnSuccess(); + } else { + dbg.setLED(1,0,0); + server.returnFailure(); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Tue Jul 29 23:01:55 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#ff95651f53c7
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Jul 29 23:01:55 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/6213f644d804 \ No newline at end of file