Operating System

Dependencies:   UnitTest wolfssh mDNS wolfcrypt wolfSSL

This is an embedded operating system for K64F. It includes a ssh server, a web-server and mDNS server. It has POST on boot. The main purpose of the OS is a router for the thing network.

Committer:
sPymbed
Date:
Wed Nov 20 13:28:13 2019 +0000
Revision:
6:c400a6461dd6
Parent:
4:eed09833d4bf
Child:
8:270a2c86866e
improved: code

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 4:eed09833d4bf 8
sPymbed 4:eed09833d4bf 9 #define MAXDATASIZE (1024*4)
sPymbed 0:97ba3e2cd071 10
sPymbed 0:97ba3e2cd071 11 //#define DEBUG
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 4:eed09833d4bf 116 c = pc.getc();
sPymbed 6:c400a6461dd6 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 0:97ba3e2cd071 122 red = 1;
sPymbed 0:97ba3e2cd071 123 blue = 0;
sPymbed 0:97ba3e2cd071 124 green = 1;
sPymbed 0:97ba3e2cd071 125
sPymbed 3:351ee68a721d 126 UnitTest unitest;
sPymbed 3:351ee68a721d 127
sPymbed 3:351ee68a721d 128 unitest.assertOn(red, 1);
sPymbed 3:351ee68a721d 129
sPymbed 6:c400a6461dd6 130 printf("Hello World! --- http://www.AlgorithMan.de\n\r");
sPymbed 3:351ee68a721d 131
sPymbed 3:351ee68a721d 132 //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 133
sPymbed 3:351ee68a721d 134 Thread task1;
sPymbed 3:351ee68a721d 135 Thread task2;
sPymbed 0:97ba3e2cd071 136
sPymbed 0:97ba3e2cd071 137 //InterruptManager interrupts(0x20, &gdt, &taskManager);
sPymbed 0:97ba3e2cd071 138
sPymbed 6:c400a6461dd6 139 printf("Initializing Hardware, Stage 1\n\r");
sPymbed 0:97ba3e2cd071 140
sPymbed 0:97ba3e2cd071 141 #ifdef GRAPHICSMODE
sPymbed 0:97ba3e2cd071 142 Desktop desktop(320, 200, 0x00, 0x00, 0xA8);
sPymbed 0:97ba3e2cd071 143 #endif
sPymbed 0:97ba3e2cd071 144
sPymbed 0:97ba3e2cd071 145
sPymbed 0:97ba3e2cd071 146 #ifdef GRAPHICSMODE
sPymbed 0:97ba3e2cd071 147 //KeyboardDriver keyboard(&interrupts, &desktop);
sPymbed 0:97ba3e2cd071 148 #else
sPymbed 0:97ba3e2cd071 149 //PrintfKeyboardEventHandler kbhandler;
sPymbed 0:97ba3e2cd071 150 //KeyboardDriver keyboard(&interrupts, &kbhandler);
sPymbed 0:97ba3e2cd071 151 #endif
sPymbed 0:97ba3e2cd071 152
sPymbed 0:97ba3e2cd071 153
sPymbed 0:97ba3e2cd071 154 #ifdef GRAPHICSMODE
sPymbed 0:97ba3e2cd071 155 //MouseDriver mouse(&interrupts, &desktop);
sPymbed 0:97ba3e2cd071 156 #else
sPymbed 0:97ba3e2cd071 157 //MouseToConsole mousehandler;
sPymbed 0:97ba3e2cd071 158 //MouseDriver mouse(&interrupts, &mousehandler);
sPymbed 0:97ba3e2cd071 159 #endif
sPymbed 0:97ba3e2cd071 160
sPymbed 0:97ba3e2cd071 161 #ifdef GRAPHICSMODE
sPymbed 0:97ba3e2cd071 162 VideoGraphicsArray vga;
sPymbed 0:97ba3e2cd071 163 #endif
sPymbed 0:97ba3e2cd071 164
sPymbed 6:c400a6461dd6 165 printf("Initializing Hardware, Stage 2\n\r");
sPymbed 0:97ba3e2cd071 166
sPymbed 3:351ee68a721d 167 printf("Initializing Hardware, Stage 3\n\r");
sPymbed 0:97ba3e2cd071 168
sPymbed 0:97ba3e2cd071 169 #ifdef GRAPHICSMODE
sPymbed 0:97ba3e2cd071 170 vga.SetMode(320, 200, 8);
sPymbed 0:97ba3e2cd071 171 Window win1(&desktop, 10, 10, 20, 20, 0xA8, 0x00, 0x00);
sPymbed 0:97ba3e2cd071 172 desktop.AddChild(&win1);
sPymbed 0:97ba3e2cd071 173 Window win2(&desktop, 40, 15, 30, 30, 0x00, 0xA8, 0x00);
sPymbed 0:97ba3e2cd071 174 desktop.AddChild(&win2);
sPymbed 0:97ba3e2cd071 175 #endif
sPymbed 0:97ba3e2cd071 176
sPymbed 0:97ba3e2cd071 177 interface.connect();
sPymbed 3:351ee68a721d 178
sPymbed 0:97ba3e2cd071 179 // Show the network address
sPymbed 0:97ba3e2cd071 180 const char *ip = interface.get_ip_address();
sPymbed 3:351ee68a721d 181 printf("IP address is: %s\n\r", ip ? ip : "No IP");
sPymbed 0:97ba3e2cd071 182
sPymbed 0:97ba3e2cd071 183 //interrupts.Activate();
sPymbed 0:97ba3e2cd071 184
sPymbed 3:351ee68a721d 185 UDPSocket sockUDP;
sPymbed 3:351ee68a721d 186 sockUDP.open(&interface);
sPymbed 0:97ba3e2cd071 187
sPymbed 0:97ba3e2cd071 188 //TransmissionControlProtocolSocket* tcpsocket = tcp.Connect(ip2_be, 1234);
sPymbed 0:97ba3e2cd071 189 //tcpsocket->Send((uint8_t*)"Hello TCP!", 10);
sPymbed 0:97ba3e2cd071 190
sPymbed 0:97ba3e2cd071 191 //PrintfUDPHandler udphandler;
sPymbed 0:97ba3e2cd071 192
sPymbed 0:97ba3e2cd071 193 //UserDatagramProtocolSocket* udpsocket = udp.Listen(1234);
sPymbed 0:97ba3e2cd071 194 //udp.Bind(udpsocket, &udphandler);
sPymbed 6:c400a6461dd6 195 task1.start(mDNS);
sPymbed 6:c400a6461dd6 196 task2.start(http);
sPymbed 0:97ba3e2cd071 197 while (true){
sPymbed 3:351ee68a721d 198 //sockUDP.sendto(SERVER_IP, PORT, screen, sizeof(screen));
sPymbed 6:c400a6461dd6 199 //pc.putc(pc.getc());
sPymbed 3:351ee68a721d 200 wait(0.03333333);
sPymbed 3:351ee68a721d 201 }
sPymbed 0:97ba3e2cd071 202 }