Y SI / Mbed OS lib_Transmission_Serial_example

Dependencies:   lib_Transmission

Revision:
4:0e42ea16a9cb
Parent:
3:63567efc8429
Child:
5:f74f0e2f5106
diff -r 63567efc8429 -r 0e42ea16a9cb main.cpp
--- a/main.cpp	Fri Feb 12 15:39:24 2021 +0000
+++ b/main.cpp	Wed Mar 03 13:03:12 2021 +0000
@@ -2,176 +2,49 @@
 
 int main(void)
 {
-    setup();
+    Led = Transmission::RED_DISCONNECTED;
+    processing("ETH:IP 192.168.1.25:80");
     while(1) Led = transmission.recv();
 }
 
-void setup(void)
-{
-    Led = Transmission::RED_DISCONNECTED;
-    uc.mac = transmission.ip(true);//, "192.168.1.25", 80);
-    for(string name : { "22:42:9d:a3:be:be ELINS-UC-A K64F YANNIC.SIMON@UNIVERSITE-PARIS-SACLAY.FR",
-                        "00:02:f7:f3:01:b8 ELINS-UC-B LPC1768 YANNIC.SIMON@UNIVERSITE-PARIS-SACLAY.FR",
-                        "00:80:e1:44:00:3a ELINS-UC-C.visiteur H743ZI2 YANNIC.SIMON@UNIVERSITE-PARIS-SACLAY.FR"})
-    {
-        stringstream streamnames(name);
-        for(int line = 0; getline(streamnames, name, ' '); line += ((name == uc.mac) || (line > 0))?1:0)
-        {
-            switch(line)
-            {
-                case 1: uc.name = name;                                 break;
-                case 2: uc.spec = name;                                 break;
-                case 3: case 4: case 5: case 6: uc.mail[line-3] = name; break;
-                default:                                                break;
-            }
-        }
-    }
-    if(uc.name.empty()) uc.name = uc.mac;
-    else if(uc.name.find(".") != string::npos)
-    {
-        uc.vlan = uc.name.substr(uc.name.find("."));
-        uc.name = uc.name.substr(0, uc.name.find("."));
-    }
-}
-
-void init(void)
-{
-    set_time(transmission.ntp());
-    notify("CONNECTION");
-}
-
-string csv_time(const bool& second)
-{
-    time_t TimeStamp = time(NULL);
-    if(TimeStamp < 1600000000) set_time(TimeStamp = transmission.ntp("ntp.u-psud.fr"));
-    struct tm * tmTimeStamp = localtime(&TimeStamp);
-    char timeLogs[22] = {0};
-    strftime(timeLogs, 21, second?"%F;%T;":"%F;%R;", tmTimeStamp);
-    return timeLogs;
-}
-
-string html(enumHTML CONTENT)
-{
-    map<string, string> BgColor {{"OK", "lime"}, {"WARNING", "orange"}, {"ERROR", "red"}, {"MISSING", "white"}, {"LOST", "black"}};
-    stringstream buffer;
-    buffer.precision(3);
-    switch(CONTENT)
-    {
-        case HEAD:  buffer << "Content-Type: text/html; charset=utf-8\r\nAccess-Control-Allow-Origin: *\r\n\r\n"; break;
-        case OPEN:  buffer << "<!DOCTYPE html>\r\n<html>\r\n\t<head>\r\n\t\t<title>" << MBED_PROJECT << " : " << uc.name << "</title>\r\n\t</head>\r\n\t<body style=background-color:dimgray>\r\n\t\t<center>"; break;
-        case CLOSE: buffer << "\r\n\t\t</center>\r\n\t</body>\r\n</html>"; break;
-        case BODY:
-            buffer << "<!DOCTYPE html>\r\n<html>\r\n\t<head>\r\n\t\t<title>" << MBED_PROJECT << " : " << uc.name << "</title>\r\n\t\t<meta http-equiv=refresh content=1>\r\n\t</head>";
-            buffer << "\r\n\t<body style=background-color:dimgray>\r\n\t\t<center>\r\n\t\t\t<h1>" << MBED_PROJECT << " : " << uc.name << "</h1>";
-            buffer << "\r\n\t\t\t<h3>" << csv_time() << "</h3>\r\n\t\t\t<a href=request><button type=button>Request</button></a>\r\n\t\t</center>\r\n\t</body>\r\n</html>";
-        break;
-        case SMTP:
-            buffer << "MIME-Version: 1.0\r\nMessage-Id: <Mbed." << uc.name << "." << time(NULL) << "@UNIVERSITE-PARIS-SACLAY.FR>\r\nContent-Type: multipart/mixed; boundary=\"boundaryUPT0\"\r\n";
-            buffer << "\r\n--boundaryUPT0\r\nContent-Type: multipart/alternative; boundary=boundaryUPT1\r\n\r\n";
-            buffer << "\r\n--boundaryUPT1\r\nContent-Type: text/plain; charset=utf-8\r\n\r\n";
-            buffer << MBED_PROJECT << " : " << uc.name;
-            buffer << "\r\n\r\nReal-time monitoring display (from the local network) : http://" << uc.name << uc.vlan << ".lps.u-psud.fr/";
-            buffer << "\r\n--boundaryUPT1\r\nContent-Type: text/html; charset=utf-8\r\n\r\n";
-            buffer << "<html>\r\n\t<body>\r\n\t\t<center>\r\n\t\t\t<h1>" << MBED_PROJECT << " : " << uc.name << "</h1>\r\n\t\t\t<table style=color:white;border-collapse:collapse>";
-            buffer << "\r\n\t\t\t</table>\r\n\t\t</center>\r\n\t\t<footer><p>Real-time monitoring display (from the local network) : http://" << uc.name << uc.vlan << ".lps.u-psud.fr/</p></footer>\r\n\t</body>\r\n</html>";
-            buffer << "\r\n--boundaryUPT1\r\n\r\n--boundaryUPT0\r\nContent-Type: text/csv\r\nContent-disposition: attachment; filename=";
-            buffer << "time.csv;\r\n\r\n" << csv_time() << "\r\n--boundaryUPT0\r\n";
-        break;
-    }
-    return buffer.str();
-}
-
-string notify(const string& EVENT, const string& STATUS)
-{
-    string ACK, PROJECT(MBED_PROJECT), HTML(html(SMTP)), SUBJECT("[Alert]" + PROJECT + " " + uc.spec + " : " + EVENT + (STATUS.empty()?"":" - ") + STATUS);
-    for(const string &MAIL : uc.mail) if(!MAIL.empty()) if(!transmission.smtp(MAIL.c_str(), uc.name.c_str(), SUBJECT.c_str(), HTML.c_str())) ACK += " ERROR[ " + MAIL + " ]";
-    return ACK.empty()?"OK":ACK;
-}
-
-string str_precision(float value)
+string processing(string cmd)
 {
-    ostringstream buffer;
-    buffer.precision(3);
-    buffer << value;
-    return buffer.str();
-}
-
-void processing(string cmd, Transmission::enum_trans_to to)
-{
-    for (unsigned int i = 0; i < cmd.size(); i++) if ((cmd[i] >= 'a') && (cmd[i] <= 'z')) cmd[i] += 'A' - 'a';
-    stringstream srecv(cmd), ssend;
-    ssend.precision(3);
-    while(getline(srecv, cmd, ';'))
+    ostringstream ssend;
+    ssend << fixed;
+    ssend.precision(2);
+    if(cmd.empty());
+    else if(cmd.find("GET /*IDN? HTTP") != string::npos)
+        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__;
+    else if(cmd.find("GET /FAVICON.ICO HTTP") != string::npos)
+        ssend << transmission.http.RETURN_NO_CONTENT;
+    else if(cmd.find("GET /") != string::npos)
+        ssend << transmission.http.RETURN_SEE_OTHER << "Location: /*idn?\r\n\r\n";
+    else if(cmd.find("HEAD /") != string::npos)
+        ssend << transmission.http.RETURN_OK << "Content-Type: text/html; charset=utf-8\r\nAccess-Control-Allow-Origin: *\r\n\r\n";
+    else if(cmd.find("HOST: ") != string::npos)
+        transmission.delivery(Transmission::HTTP);
+    else if(cmd == "*IDN?")
+        ssend << MBED_PROJECT << ", Mbed OS " << MBED_VERSION << ", Version dated, " << __DATE__ << ", " << __TIME__;
+    else if(cmd == "IP?")
+        ssend << "IP(" << transmission.ip() << ")";
+    else if(cmd == "MAC?")
+        ssend << "MAC(" << (eth.get_mac_address()?eth.get_mac_address():"00:00:00:00:00:00") << ")";
+    else if(cmd == "ETH?")
+        ssend << "ETH:" << ((eth.get_connection_status() == NSAPI_STATUS_GLOBAL_UP)? "UP":"DOWN");
+    else if(cmd[cmd.size()-1] == '?')
+        ssend << "incorrect requeste [" << cmd << "]";
+    else if(cmd == "ETH:DHCP")
+        transmission.ip(true);
+    else if(cmd == "ETH:OFF")
+        transmission.ip(false);
+    else if(cmd.find("ETH:IP ") != string::npos)
     {
-        if(ssend.str().size() > 0) ssend << ' ';
-        if(cmd.empty());
-        else if(cmd == "*IDN?")
-        {
-            ssend << MBED_PROJECT << ", Mbed OS " << MBED_VERSION << ", Version dated, " << __DATE__ << ", " << __TIME__;
-        }
-        else if(cmd.find("GET /*IDN? HTTP") != string::npos)
-        {
-            ssend << HTTP_RETURN_OK << html(HEAD) << MBED_PROJECT << ", Mbed OS " << MBED_VERSION << ", Version dated, " << __DATE__ << ", " << __TIME__;
-        }
-        else if(cmd.find("GET /REQUEST HTTP") != string::npos)
-        {
-            ssend << HTTP_RETURN_OK << html(HEAD) << html(OPEN) << "\r\n\t\t\t<p>EMAIL REQUEST " << notify("REQUESTE") << "</p>\r\n\t\t\t<a href=/><button type=button>Return</button></a>" << html(CLOSE);
-        }
-        else if(cmd.find("GET / HTTP") != string::npos)
-        {
-            ssend << HTTP_RETURN_OK << html(HEAD) << html(BODY);
-        }
-        else if(cmd.find("GET /FAVICON.ICO HTTP") != string::npos)
-        {
-            ssend << HTTP_RETURN_OK << "Content-type: image/x-icon\r\n\r\n";
-            for(char c : HTTP_FAVICON) ssend << c;
-        }
-        else if(cmd.find("GET /") != string::npos)
-        {
-            ssend << HTTP_RETURN_NO_CONTENT << html(HEAD);
-        }
-        else if(cmd.find("HEAD /") != string::npos)
-        {
-            ssend << HTTP_RETURN_OK << html(HEAD);
-        }
-        else if(cmd.find("HOST: ") != string::npos)
-        {
-            if(to == Transmission::TCP) to = Transmission::HTTP;
-        }
-        else if(cmd == "MAC?")
-        {
-            ssend << "MAC[" << uc.mac << "], NAME[" << uc.name << "]";
-        }
-        else if(cmd == "IP?")
-        {
-            ssend << "IP[" << transmission.ip() << "]";
-        }
-        else if(cmd == "ETH?")
-        {
-            ssend << "ETH:" << ((eth.get_connection_status() == NSAPI_STATUS_GLOBAL_UP)?"UP":"DOWN");
-        }
-        else if(cmd.find("ETH:IP ") != string::npos)
-        {
-            string param(cmd.substr(cmd.find(' ')+1));
-            string IP(param.substr(0, param.find(':')));
-            stringstream istr(param.substr(param.find(':')+1));
-            uint16_t PORT;
-            istr >> PORT;
-            transmission.ip(true, IP.c_str(), PORT);
-        }
-        else if(cmd == "ETH:DHCP")
-        {
-            transmission.ip(true);
-        }
-        else if(cmd == "ETH:OFF")
-        {
-            transmission.ip(false);
-        }
-        else if(cmd == "EMAIL?")
-        {
-            ssend << "EMAIL REQUEST " << notify("REQUESTE");
-        }
-        else if(cmd.find('?') != string::npos) ssend << "incorrect requeste [" << cmd << "]";
+        string param(cmd.substr(cmd.find_first_of(" ")+1));
+        string IP(param.substr(0, param.find_first_of(":")));
+        istringstream istr(param.substr(param.find_first_of(":")+1));
+        uint16_t PORT;
+        istr >> PORT;
+        transmission.ip(true, IP.c_str(), PORT);
     }
-    transmission.send(ssend.str(), to);
+    return ssend.str();
 }
\ No newline at end of file