Operating System

Dependencies:   UnitTest wolfssh mDNS wolfcrypt wolfSSL

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

Committer:
sPymbed
Date:
Tue Nov 26 11:18:09 2019 +0000
Revision:
10:38b716b7534f
Parent:
8:270a2c86866e
added: serial input

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sPymbed 0:97ba3e2cd071 1 #include "mbed.h"
sPymbed 0:97ba3e2cd071 2 #include <EthernetInterface.h>
sPymbed 0:97ba3e2cd071 3 #include <drivers/vga.h>
sPymbed 0:97ba3e2cd071 4 #include <gui/desktop.h>
sPymbed 3:351ee68a721d 5 #include "mDNSResponder.h"
sPymbed 3:351ee68a721d 6 #include "UnitTest.h"
sPymbed 4:eed09833d4bf 7 #include <user_settings.h>
sPymbed 10:38b716b7534f 8 #include <string>
sPymbed 10:38b716b7534f 9
sPymbed 4:eed09833d4bf 10 #define MAXDATASIZE (1024*4)
sPymbed 0:97ba3e2cd071 11
sPymbed 0:97ba3e2cd071 12 //#define GRAPHICSMODE
sPymbed 0:97ba3e2cd071 13
sPymbed 0:97ba3e2cd071 14 const int PORT = 8000;
sPymbed 0:97ba3e2cd071 15 static const char* SERVER_IP = "172.26.10.207"; //IP of server board
sPymbed 0:97ba3e2cd071 16 char screen[3840];
sPymbed 0:97ba3e2cd071 17
sPymbed 0:97ba3e2cd071 18 DigitalOut red(LED_RED);
sPymbed 0:97ba3e2cd071 19 DigitalOut blue(LED_BLUE);
sPymbed 0:97ba3e2cd071 20 DigitalOut green(LED_GREEN);
sPymbed 3:351ee68a721d 21 FlashIAP flash;
sPymbed 4:eed09833d4bf 22 Serial pc(USBTX, USBRX);
sPymbed 3:351ee68a721d 23
sPymbed 1:4059ac7ebc65 24 class PrintfTCPHandler
sPymbed 0:97ba3e2cd071 25 {
sPymbed 1:4059ac7ebc65 26 public:
sPymbed 0:97ba3e2cd071 27 bool HandleTransmissionControlProtocolMessage(TCPSocket* socket, char* data, uint16_t size)
sPymbed 0:97ba3e2cd071 28 {
sPymbed 0:97ba3e2cd071 29 if(size > 9
sPymbed 0:97ba3e2cd071 30 && data[0] == 'G'
sPymbed 0:97ba3e2cd071 31 && data[1] == 'E'
sPymbed 0:97ba3e2cd071 32 && data[2] == 'T'
sPymbed 0:97ba3e2cd071 33 && data[3] == ' '
sPymbed 0:97ba3e2cd071 34 && data[4] == '/'
sPymbed 0:97ba3e2cd071 35 && data[5] == ' '
sPymbed 0:97ba3e2cd071 36 && data[6] == 'H'
sPymbed 0:97ba3e2cd071 37 && data[7] == 'T'
sPymbed 0:97ba3e2cd071 38 && data[8] == 'T'
sPymbed 0:97ba3e2cd071 39 && data[9] == 'P'
sPymbed 0:97ba3e2cd071 40 )
sPymbed 0:97ba3e2cd071 41 {
sPymbed 3:351ee68a721d 42 unsigned int address = flash.get_flash_size() - 4096; //Write in last sector
sPymbed 4:eed09833d4bf 43 char *data = (char*)address; //Read
sPymbed 3:351ee68a721d 44 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);
sPymbed 3:351ee68a721d 45 socket->close();
sPymbed 3:351ee68a721d 46 } else {
sPymbed 0:97ba3e2cd071 47 socket->close();
sPymbed 0:97ba3e2cd071 48 }
sPymbed 0:97ba3e2cd071 49
sPymbed 0:97ba3e2cd071 50 return true;
sPymbed 0:97ba3e2cd071 51 }
sPymbed 1:4059ac7ebc65 52 };
sPymbed 0:97ba3e2cd071 53
sPymbed 2:453e73d66757 54 void write(unsigned int n, unsigned int addressOfX, char X){
sPymbed 3:351ee68a721d 55 unsigned int address = n * 4096; //Write in n sector
sPymbed 4:eed09833d4bf 56 char *data = (char*)address; //Read
sPymbed 3:351ee68a721d 57 char b[4096];
sPymbed 2:453e73d66757 58 b[addressOfX] = X;
sPymbed 3:351ee68a721d 59 memcpy(b, data, 4096 * sizeof(char)); //int is a POD
sPymbed 3:351ee68a721d 60 flash.erase(address, flash.get_sector_size(address));
sPymbed 3:351ee68a721d 61 flash.program(b, address, 4096);
sPymbed 3:351ee68a721d 62 }
sPymbed 3:351ee68a721d 63
sPymbed 3:351ee68a721d 64 void write(char *data, unsigned int size){
sPymbed 3:351ee68a721d 65 unsigned int address = flash.get_flash_size() - 4096; //Write in last sector
sPymbed 3:351ee68a721d 66 flash.erase(address, flash.get_sector_size(address));
sPymbed 3:351ee68a721d 67 flash.program(data, address, size);
sPymbed 2:453e73d66757 68 }
sPymbed 2:453e73d66757 69
sPymbed 2:453e73d66757 70 char read(unsigned int address){
sPymbed 3:351ee68a721d 71 char *data = (char*)address; //Read
sPymbed 2:453e73d66757 72 return data[0];
sPymbed 2:453e73d66757 73 }
sPymbed 2:453e73d66757 74
sPymbed 3:351ee68a721d 75 EthernetInterface interface;
sPymbed 3:351ee68a721d 76
sPymbed 3:351ee68a721d 77 void http() {
sPymbed 3:351ee68a721d 78 PrintfTCPHandler tcphandler;
sPymbed 3:351ee68a721d 79 TCPSocket server;
sPymbed 3:351ee68a721d 80 server.open(&interface);
sPymbed 3:351ee68a721d 81 server.bind(80);
sPymbed 3:351ee68a721d 82 server.listen(1);
sPymbed 3:351ee68a721d 83 TCPSocket *client;
sPymbed 3:351ee68a721d 84 int bufferSize = 512;
sPymbed 3:351ee68a721d 85 char buffer[bufferSize];
sPymbed 3:351ee68a721d 86
sPymbed 3:351ee68a721d 87 while (true) {
sPymbed 3:351ee68a721d 88 client = server.accept();
sPymbed 3:351ee68a721d 89 int len = client->recv(&buffer, bufferSize);
sPymbed 3:351ee68a721d 90
sPymbed 3:351ee68a721d 91 #ifdef DEBUG
sPymbed 3:351ee68a721d 92 printf("%d: ", len);
sPymbed 3:351ee68a721d 93 for (int i = 0; i < len; i++)
sPymbed 3:351ee68a721d 94 printf("%d ", buffer[i]);
sPymbed 3:351ee68a721d 95 printf("\r\n");
sPymbed 3:351ee68a721d 96 #endif
sPymbed 3:351ee68a721d 97
sPymbed 3:351ee68a721d 98 tcphandler.HandleTransmissionControlProtocolMessage(client, (char*)buffer, len);
sPymbed 3:351ee68a721d 99 wait(0.03333333);
sPymbed 3:351ee68a721d 100 }
sPymbed 3:351ee68a721d 101 }
sPymbed 3:351ee68a721d 102
sPymbed 6:c400a6461dd6 103 void mDNS() {
sPymbed 3:351ee68a721d 104 mDNSResponder mdns(interface);
sPymbed 3:351ee68a721d 105 mdns.announce(interface.get_ip_address());
sPymbed 3:351ee68a721d 106 while (true) {
sPymbed 3:351ee68a721d 107 mdns.MDNS_process();
sPymbed 3:351ee68a721d 108 wait(0.03333333);
sPymbed 3:351ee68a721d 109 }
sPymbed 6:c400a6461dd6 110 }
sPymbed 3:351ee68a721d 111
sPymbed 6:c400a6461dd6 112 volatile char c = '\0'; // Initialized to the NULL character
sPymbed 4:eed09833d4bf 113
sPymbed 4:eed09833d4bf 114 void onCharReceived()
sPymbed 4:eed09833d4bf 115 {
sPymbed 10:38b716b7534f 116 //c = pc.getc();
sPymbed 10:38b716b7534f 117 pc.putc(c);
sPymbed 4:eed09833d4bf 118 }
sPymbed 4:eed09833d4bf 119
sPymbed 0:97ba3e2cd071 120 int main() {
sPymbed 6:c400a6461dd6 121 //pc.attach(&onCharReceived);
sPymbed 10:38b716b7534f 122 pc.baud(115200);
sPymbed 0:97ba3e2cd071 123 red = 1;
sPymbed 0:97ba3e2cd071 124 blue = 0;
sPymbed 0:97ba3e2cd071 125 green = 1;
sPymbed 0:97ba3e2cd071 126
sPymbed 3:351ee68a721d 127 UnitTest unitest;
sPymbed 3:351ee68a721d 128
sPymbed 3:351ee68a721d 129 unitest.assertOn(red, 1);
sPymbed 3:351ee68a721d 130
sPymbed 10:38b716b7534f 131 pc.printf("Hello World! --- http://www.AlgorithMan.de\n\r");
sPymbed 3:351ee68a721d 132
sPymbed 3:351ee68a721d 133 //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);
sPymbed 3:351ee68a721d 134
sPymbed 3:351ee68a721d 135 Thread task1;
sPymbed 3:351ee68a721d 136 Thread task2;
sPymbed 0:97ba3e2cd071 137
sPymbed 0:97ba3e2cd071 138 //InterruptManager interrupts(0x20, &gdt, &taskManager);
sPymbed 0:97ba3e2cd071 139
sPymbed 10:38b716b7534f 140 pc.printf("Initializing Hardware, Stage 1\n\r");
sPymbed 0:97ba3e2cd071 141
sPymbed 0:97ba3e2cd071 142 #ifdef GRAPHICSMODE
sPymbed 0:97ba3e2cd071 143 Desktop desktop(320, 200, 0x00, 0x00, 0xA8);
sPymbed 0:97ba3e2cd071 144 #endif
sPymbed 0:97ba3e2cd071 145
sPymbed 0:97ba3e2cd071 146
sPymbed 0:97ba3e2cd071 147 #ifdef GRAPHICSMODE
sPymbed 0:97ba3e2cd071 148 //KeyboardDriver keyboard(&interrupts, &desktop);
sPymbed 0:97ba3e2cd071 149 #else
sPymbed 0:97ba3e2cd071 150 //PrintfKeyboardEventHandler kbhandler;
sPymbed 0:97ba3e2cd071 151 //KeyboardDriver keyboard(&interrupts, &kbhandler);
sPymbed 0:97ba3e2cd071 152 #endif
sPymbed 0:97ba3e2cd071 153
sPymbed 0:97ba3e2cd071 154
sPymbed 0:97ba3e2cd071 155 #ifdef GRAPHICSMODE
sPymbed 0:97ba3e2cd071 156 //MouseDriver mouse(&interrupts, &desktop);
sPymbed 0:97ba3e2cd071 157 #else
sPymbed 0:97ba3e2cd071 158 //MouseToConsole mousehandler;
sPymbed 0:97ba3e2cd071 159 //MouseDriver mouse(&interrupts, &mousehandler);
sPymbed 0:97ba3e2cd071 160 #endif
sPymbed 0:97ba3e2cd071 161
sPymbed 0:97ba3e2cd071 162 #ifdef GRAPHICSMODE
sPymbed 0:97ba3e2cd071 163 VideoGraphicsArray vga;
sPymbed 0:97ba3e2cd071 164 #endif
sPymbed 0:97ba3e2cd071 165
sPymbed 10:38b716b7534f 166 pc.printf("Initializing Hardware, Stage 2\n\r");
sPymbed 0:97ba3e2cd071 167
sPymbed 0:97ba3e2cd071 168 #ifdef GRAPHICSMODE
sPymbed 0:97ba3e2cd071 169 vga.SetMode(320, 200, 8);
sPymbed 0:97ba3e2cd071 170 Window win1(&desktop, 10, 10, 20, 20, 0xA8, 0x00, 0x00);
sPymbed 0:97ba3e2cd071 171 desktop.AddChild(&win1);
sPymbed 0:97ba3e2cd071 172 Window win2(&desktop, 40, 15, 30, 30, 0x00, 0xA8, 0x00);
sPymbed 0:97ba3e2cd071 173 desktop.AddChild(&win2);
sPymbed 0:97ba3e2cd071 174 #endif
sPymbed 0:97ba3e2cd071 175
sPymbed 0:97ba3e2cd071 176 interface.connect();
sPymbed 3:351ee68a721d 177
sPymbed 0:97ba3e2cd071 178 // Show the network address
sPymbed 0:97ba3e2cd071 179 const char *ip = interface.get_ip_address();
sPymbed 10:38b716b7534f 180 pc.printf("IP address is: %s\n\r", ip ? ip : "No IP");
sPymbed 0:97ba3e2cd071 181
sPymbed 0:97ba3e2cd071 182 //interrupts.Activate();
sPymbed 0:97ba3e2cd071 183
sPymbed 8:270a2c86866e 184 //UDPSocket sockUDP;
sPymbed 8:270a2c86866e 185 //sockUDP.open(&interface);
sPymbed 0:97ba3e2cd071 186
sPymbed 0:97ba3e2cd071 187 //TransmissionControlProtocolSocket* tcpsocket = tcp.Connect(ip2_be, 1234);
sPymbed 0:97ba3e2cd071 188 //tcpsocket->Send((uint8_t*)"Hello TCP!", 10);
sPymbed 0:97ba3e2cd071 189
sPymbed 0:97ba3e2cd071 190 //PrintfUDPHandler udphandler;
sPymbed 0:97ba3e2cd071 191
sPymbed 0:97ba3e2cd071 192 //UserDatagramProtocolSocket* udpsocket = udp.Listen(1234);
sPymbed 0:97ba3e2cd071 193 //udp.Bind(udpsocket, &udphandler);
sPymbed 6:c400a6461dd6 194 task1.start(mDNS);
sPymbed 6:c400a6461dd6 195 task2.start(http);
sPymbed 10:38b716b7534f 196 string input;
sPymbed 0:97ba3e2cd071 197 while (true){
sPymbed 3:351ee68a721d 198 //sockUDP.sendto(SERVER_IP, PORT, screen, sizeof(screen));
sPymbed 10:38b716b7534f 199 char c = pc.getc();
sPymbed 10:38b716b7534f 200 if (c == ';'){
sPymbed 10:38b716b7534f 201 pc.printf("%s\n\r", input.c_str());
sPymbed 10:38b716b7534f 202 input.clear();
sPymbed 10:38b716b7534f 203 } else {
sPymbed 10:38b716b7534f 204 input += c;
sPymbed 10:38b716b7534f 205 }
sPymbed 3:351ee68a721d 206 wait(0.03333333);
sPymbed 3:351ee68a721d 207 }
sPymbed 0:97ba3e2cd071 208 }