Operating System

Dependencies:   UnitTest mDNS

This is an operating system for K64F. It includes ssh server, POST, a web-server and mDNS.

Revision:
3:351ee68a721d
Parent:
2:453e73d66757
--- a/main.cpp	Thu Jun 13 14:20:16 2019 +0000
+++ b/main.cpp	Wed Sep 11 10:41:02 2019 +0000
@@ -1,21 +1,22 @@
 #include "mbed.h"
 #include <EthernetInterface.h>
-#include <FreescaleIAP.h>
 #include <drivers/vga.h>
 #include <gui/desktop.h>
+#include "mDNSResponder.h"
+#include "UnitTest.h"
 
 //#define DEBUG
 //#define GRAPHICSMODE
 
 const int PORT = 8000;
-
 static const char* SERVER_IP = "172.26.10.207"; //IP of server board
 char screen[3840];
 
 DigitalOut red(LED_RED);
 DigitalOut blue(LED_BLUE);
 DigitalOut green(LED_GREEN);
-        
+FlashIAP flash;
+
 class PrintfTCPHandler
 {
 public:
@@ -34,7 +35,11 @@
             && data[9] == 'P'
         )
         {
-            socket->send((uint8_t*)"HTTP/1.1 200 OK\r\nServer: MyOS\r\nContent-Type: text/html\r\n\r\n<html><head><title>My Operating System</title></head><body><b>My Operating System</b> http://www.AlgorithMan.de</body></html>\r\n", 184);
+            unsigned int address = flash.get_flash_size() - 4096; //Write in last sector
+            char *data = (char*)address;                       //Read
+            socket->send("HTTP/1.1 200 OK\r\nServer: MyOS\r\nContent-Type: text/html\r\n\r\n<html><head><title>My Operating System</title></head><body><b>My Operating System</b> http://www.AlgorithMan.de</body></html>\r\n", 184);
+            socket->close();
+        } else {
             socket->close();
         }
 
@@ -43,38 +48,80 @@
 };
 
 void write(unsigned int n, unsigned int addressOfX, char X){
-    unsigned int address = n * SECTOR_SIZE; //Write in last sector     
+    unsigned int address = n * 4096;            //Write in n sector     
     char *data   = (char*)address;                     //Read     
-    // This works in C and C++
-    char b[SECTOR_SIZE];
+    char b[4096];
     b[addressOfX] = X;
-    memcpy(b, data, SECTOR_SIZE * sizeof(char));       // int is a POD                           
-    erase_sector(address);                
-    program_flash(address, b, SECTOR_SIZE);            //an integers has four bytes each.
+    memcpy(b, data, 4096 * sizeof(char));       //int is a POD                           
+    flash.erase(address, flash.get_sector_size(address));                
+    flash.program(b, address, 4096);
+}
+
+void write(char *data, unsigned int size){
+    unsigned int address = flash.get_flash_size() - 4096; //Write in last sector                     
+    flash.erase(address, flash.get_sector_size(address));                
+    flash.program(data, address, size);
 }
 
 char read(unsigned int address){
-    char *data = (char*)address;       //Read
+    char *data = (char*)address; //Read
     return data[0];
 }
 
+EthernetInterface interface;
+
+void http() {
+    PrintfTCPHandler tcphandler;
+    TCPSocket server;
+    server.open(&interface);
+    server.bind(80);
+    server.listen(1);
+    TCPSocket *client;
+    int bufferSize = 512;
+    char buffer[bufferSize];
+
+    while (true) {
+        client = server.accept();
+        int len = client->recv(&buffer, bufferSize);
+          
+#ifdef DEBUG
+      printf("%d: ", len);
+      for (int i = 0; i < len; i++)
+          printf("%d ", buffer[i]);
+      printf("\r\n");
+#endif
+
+        tcphandler.HandleTransmissionControlProtocolMessage(client, (char*)buffer, len);
+        wait(0.03333333);
+    }
+}
+
+/*void mDNS() {
+    mDNSResponder mdns(interface);
+    mdns.announce(interface.get_ip_address());
+    while (true) {
+        mdns.MDNS_process();
+        wait(0.03333333);
+    }
+}*/
+
 int main() {
     red = 1;
     blue = 0;
     green = 1;
     
+    UnitTest unitest;
+    
+    unitest.assertOn(red, 1);
+    
     printf("Hello World! --- http://www.AlgorithMan.de\n");
-
-    //TaskManager taskManager;
-    /*
-    Task task1(&gdt, taskA);
-    Task task2(&gdt, taskB);
-    taskManager.AddTask(&task1);
-    taskManager.AddTask(&task2);
-    */
+    
+    //write("HTTP/1.1 200 OK\r\nServer: MyOS\r\nContent-Type: text/html\r\n\r\n<html><head><title>My Operating System</title></head><body><b>My Operating System</b> http://www.AlgorithMan.de</body></html>\r\n", 184);
+    
+    Thread task1;
+    Thread task2;
 
     //InterruptManager interrupts(0x20, &gdt, &taskManager);
-    //SyscallHandler syscalls(&interrupts, 0x80);
 
     printf("Initializing Hardware, Stage 1\n");
     
@@ -100,9 +147,6 @@
             //MouseDriver mouse(&interrupts, &mousehandler);
         #endif
         //drvManager.AddDriver(&mouse);
-
-        //PeripheralComponentInterconnectController PCIController;
-        //PCIController.SelectDrivers(&drvManager, &interrupts);
         
         #ifdef GRAPHICSMODE
             VideoGraphicsArray vga;
@@ -111,7 +155,7 @@
     printf("Initializing Hardware, Stage 2\n");
         //drvManager.ActivateAll();
 
-    printf("Initializing Hardware, Stage 3\n");
+    printf("Initializing Hardware, Stage 3\n\r");
 
     #ifdef GRAPHICSMODE
         vga.SetMode(320, 200, 8);
@@ -121,59 +165,34 @@
         desktop.AddChild(&win2);
     #endif
 
-    EthernetInterface interface;
-    interface.disconnect();
     interface.connect();
-       
+
     // Show the network address
     const char *ip = interface.get_ip_address();
-    printf("IP address is: %s\n", ip ? ip : "No IP");
-    
-    //InternetControlMessageProtocol icmp(&ipv4);
+    printf("IP address is: %s\n\r", ip ? ip : "No IP");
+    mDNSResponder mdns(interface);
 
     //interrupts.Activate();
 
     printf("\n\n\n\n");
 
-    PrintfTCPHandler tcphandler;
-    TCPServer server;
-    UDPSocket sockUDP(&interface);                 //creat Ethernet socket
-    SocketAddress sockAddr(SERVER_IP, PORT);
-
-    server.open(&interface);
+    UDPSocket sockUDP;
+    sockUDP.open(&interface);
     
-    server.bind(interface.get_ip_address(), 80);
-    server.listen();
-    server.set_blocking(false);
-    TCPSocket client;            //!< client class
-    SocketAddress clt_addr;
-    int bufferSize = 512;
-    char buffer[bufferSize];
+    mdns.announce(ip);
     
     //TransmissionControlProtocolSocket* tcpsocket = tcp.Connect(ip2_be, 1234);
     //tcpsocket->Send((uint8_t*)"Hello TCP!", 10);
 
-    //icmp.RequestEchoReply(gip_be);
-
     //PrintfUDPHandler udphandler;
 
     //UserDatagramProtocolSocket* udpsocket = udp.Listen(1234);
     //udp.Bind(udpsocket, &udphandler);
-  
+    //task1.start(mDNS);
+    task1.start(http);
     while (true){
-          //printf("\nCLIENT - Sending '%i' to server %s\r\n", screen[0], SERVER_IP);       //print message to send
-          sockUDP.sendto(SERVER_IP, PORT, screen, sizeof(screen));
-          server.accept(&client, &clt_addr);
-          int len = client.recv(&buffer, bufferSize);
-          
-#ifdef DEBUG
-      printf("%d: ", len);
-      for (int i = 0; i < len; i++)
-          printf("%d ", buffer[i]);
-      printf("\r\n");
-#endif
-
-          tcphandler.HandleTransmissionControlProtocolMessage(&client, (char*)buffer, len);
-          wait(0.03333333);
-       }
+        mdns.MDNS_process();
+        //sockUDP.sendto(SERVER_IP, PORT, screen, sizeof(screen));
+        wait(0.03333333);
+    }
 }
\ No newline at end of file