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:
Mon Nov 25 14:24:15 2019 +0000
Revision:
8:270a2c86866e
Parent:
6:c400a6461dd6
Child:
10:38b716b7534f
added: ssh

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