lib_Transmission_Serial_example

Dependencies:   lib_Transmission

Committer:
YSI
Date:
Tue Mar 09 09:59:51 2021 +0000
Revision:
5:f74f0e2f5106
Parent:
4:0e42ea16a9cb
Child:
8:bea3564ffb56
simplify example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
YSI 0:5d2c38c99ce6 1 #include "main.h"
YSI 0:5d2c38c99ce6 2
YSI 0:5d2c38c99ce6 3 int main(void)
YSI 0:5d2c38c99ce6 4 {
YSI 4:0e42ea16a9cb 5 Led = Transmission::RED_DISCONNECTED;
YSI 4:0e42ea16a9cb 6 processing("ETH:IP 192.168.1.25:80");
YSI 0:5d2c38c99ce6 7 while(1) Led = transmission.recv();
YSI 0:5d2c38c99ce6 8 }
YSI 0:5d2c38c99ce6 9
YSI 4:0e42ea16a9cb 10 string processing(string cmd)
YSI 0:5d2c38c99ce6 11 {
YSI 4:0e42ea16a9cb 12 ostringstream ssend;
YSI 4:0e42ea16a9cb 13 ssend << fixed;
YSI 4:0e42ea16a9cb 14 ssend.precision(2);
YSI 4:0e42ea16a9cb 15 if(cmd.empty());
YSI 4:0e42ea16a9cb 16 else if(cmd == "*IDN?")
YSI 4:0e42ea16a9cb 17 ssend << MBED_PROJECT << ", Mbed OS " << MBED_VERSION << ", Version dated, " << __DATE__ << ", " << __TIME__;
YSI 4:0e42ea16a9cb 18 else if(cmd == "IP?")
YSI 4:0e42ea16a9cb 19 ssend << "IP(" << transmission.ip() << ")";
YSI 4:0e42ea16a9cb 20 else if(cmd == "MAC?")
YSI 4:0e42ea16a9cb 21 ssend << "MAC(" << (eth.get_mac_address()?eth.get_mac_address():"00:00:00:00:00:00") << ")";
YSI 4:0e42ea16a9cb 22 else if(cmd == "ETH?")
YSI 4:0e42ea16a9cb 23 ssend << "ETH:" << ((eth.get_connection_status() == NSAPI_STATUS_GLOBAL_UP)? "UP":"DOWN");
YSI 4:0e42ea16a9cb 24 else if(cmd[cmd.size()-1] == '?')
YSI 4:0e42ea16a9cb 25 ssend << "incorrect requeste [" << cmd << "]";
YSI 4:0e42ea16a9cb 26 else if(cmd == "ETH:DHCP")
YSI 4:0e42ea16a9cb 27 transmission.ip(true);
YSI 4:0e42ea16a9cb 28 else if(cmd == "ETH:OFF")
YSI 4:0e42ea16a9cb 29 transmission.ip(false);
YSI 4:0e42ea16a9cb 30 else if(cmd.find("ETH:IP ") != string::npos)
YSI 0:5d2c38c99ce6 31 {
YSI 4:0e42ea16a9cb 32 string param(cmd.substr(cmd.find_first_of(" ")+1));
YSI 4:0e42ea16a9cb 33 string IP(param.substr(0, param.find_first_of(":")));
YSI 4:0e42ea16a9cb 34 istringstream istr(param.substr(param.find_first_of(":")+1));
YSI 4:0e42ea16a9cb 35 uint16_t PORT;
YSI 4:0e42ea16a9cb 36 istr >> PORT;
YSI 4:0e42ea16a9cb 37 transmission.ip(true, IP.c_str(), PORT);
YSI 0:5d2c38c99ce6 38 }
YSI 5:f74f0e2f5106 39 else if(cmd.find("GET /*IDN? HTTP") != string::npos)
YSI 5:f74f0e2f5106 40 ssend << transmission.http.RETURN_OK << "Content-Type: text/html; charset=utf-8\r\nAccess-Control-Allow-Origin: *\r\n\r\n" << MBED_PROJECT << ", Mbed OS " << MBED_VERSION << ", Version dated, " << __DATE__ << ", " << __TIME__;
YSI 5:f74f0e2f5106 41 else if(cmd.find("GET /FAVICON.ICO HTTP") != string::npos)
YSI 5:f74f0e2f5106 42 ssend << transmission.http.RETURN_NO_CONTENT;
YSI 5:f74f0e2f5106 43 else if(cmd.find("GET /") != string::npos)
YSI 5:f74f0e2f5106 44 ssend << transmission.http.RETURN_SEE_OTHER << "Location: /*idn?\r\n\r\n";
YSI 4:0e42ea16a9cb 45 return ssend.str();
YSI 0:5d2c38c99ce6 46 }