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