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@2:453e73d66757, 2019-06-13 (annotated)
- Committer:
- sPymbed
- Date:
- Thu Jun 13 14:20:16 2019 +0000
- Revision:
- 2:453e73d66757
- Parent:
- 1:4059ac7ebc65
- Child:
- 3:351ee68a721d
added: write and read function
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 <FreescaleIAP.h> |
sPymbed | 0:97ba3e2cd071 | 4 | #include <drivers/vga.h> |
sPymbed | 0:97ba3e2cd071 | 5 | #include <gui/desktop.h> |
sPymbed | 0:97ba3e2cd071 | 6 | |
sPymbed | 0:97ba3e2cd071 | 7 | //#define DEBUG |
sPymbed | 0:97ba3e2cd071 | 8 | //#define GRAPHICSMODE |
sPymbed | 0:97ba3e2cd071 | 9 | |
sPymbed | 0:97ba3e2cd071 | 10 | const int PORT = 8000; |
sPymbed | 0:97ba3e2cd071 | 11 | |
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 | 0:97ba3e2cd071 | 18 | |
sPymbed | 1:4059ac7ebc65 | 19 | class PrintfTCPHandler |
sPymbed | 0:97ba3e2cd071 | 20 | { |
sPymbed | 1:4059ac7ebc65 | 21 | public: |
sPymbed | 0:97ba3e2cd071 | 22 | bool HandleTransmissionControlProtocolMessage(TCPSocket* socket, char* data, uint16_t size) |
sPymbed | 0:97ba3e2cd071 | 23 | { |
sPymbed | 0:97ba3e2cd071 | 24 | if(size > 9 |
sPymbed | 0:97ba3e2cd071 | 25 | && data[0] == 'G' |
sPymbed | 0:97ba3e2cd071 | 26 | && data[1] == 'E' |
sPymbed | 0:97ba3e2cd071 | 27 | && data[2] == 'T' |
sPymbed | 0:97ba3e2cd071 | 28 | && data[3] == ' ' |
sPymbed | 0:97ba3e2cd071 | 29 | && data[4] == '/' |
sPymbed | 0:97ba3e2cd071 | 30 | && data[5] == ' ' |
sPymbed | 0:97ba3e2cd071 | 31 | && data[6] == 'H' |
sPymbed | 0:97ba3e2cd071 | 32 | && data[7] == 'T' |
sPymbed | 0:97ba3e2cd071 | 33 | && data[8] == 'T' |
sPymbed | 0:97ba3e2cd071 | 34 | && data[9] == 'P' |
sPymbed | 0:97ba3e2cd071 | 35 | ) |
sPymbed | 0:97ba3e2cd071 | 36 | { |
sPymbed | 0:97ba3e2cd071 | 37 | 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); |
sPymbed | 0:97ba3e2cd071 | 38 | socket->close(); |
sPymbed | 0:97ba3e2cd071 | 39 | } |
sPymbed | 0:97ba3e2cd071 | 40 | |
sPymbed | 0:97ba3e2cd071 | 41 | return true; |
sPymbed | 0:97ba3e2cd071 | 42 | } |
sPymbed | 1:4059ac7ebc65 | 43 | }; |
sPymbed | 0:97ba3e2cd071 | 44 | |
sPymbed | 2:453e73d66757 | 45 | void write(unsigned int n, unsigned int addressOfX, char X){ |
sPymbed | 2:453e73d66757 | 46 | unsigned int address = n * SECTOR_SIZE; //Write in last sector |
sPymbed | 2:453e73d66757 | 47 | char *data = (char*)address; //Read |
sPymbed | 2:453e73d66757 | 48 | // This works in C and C++ |
sPymbed | 2:453e73d66757 | 49 | char b[SECTOR_SIZE]; |
sPymbed | 2:453e73d66757 | 50 | b[addressOfX] = X; |
sPymbed | 2:453e73d66757 | 51 | memcpy(b, data, SECTOR_SIZE * sizeof(char)); // int is a POD |
sPymbed | 2:453e73d66757 | 52 | erase_sector(address); |
sPymbed | 2:453e73d66757 | 53 | program_flash(address, b, SECTOR_SIZE); //an integers has four bytes each. |
sPymbed | 2:453e73d66757 | 54 | } |
sPymbed | 2:453e73d66757 | 55 | |
sPymbed | 2:453e73d66757 | 56 | char read(unsigned int address){ |
sPymbed | 2:453e73d66757 | 57 | char *data = (char*)address; //Read |
sPymbed | 2:453e73d66757 | 58 | return data[0]; |
sPymbed | 2:453e73d66757 | 59 | } |
sPymbed | 2:453e73d66757 | 60 | |
sPymbed | 0:97ba3e2cd071 | 61 | int main() { |
sPymbed | 0:97ba3e2cd071 | 62 | red = 1; |
sPymbed | 0:97ba3e2cd071 | 63 | blue = 0; |
sPymbed | 0:97ba3e2cd071 | 64 | green = 1; |
sPymbed | 0:97ba3e2cd071 | 65 | |
sPymbed | 0:97ba3e2cd071 | 66 | printf("Hello World! --- http://www.AlgorithMan.de\n"); |
sPymbed | 0:97ba3e2cd071 | 67 | |
sPymbed | 0:97ba3e2cd071 | 68 | //TaskManager taskManager; |
sPymbed | 0:97ba3e2cd071 | 69 | /* |
sPymbed | 0:97ba3e2cd071 | 70 | Task task1(&gdt, taskA); |
sPymbed | 0:97ba3e2cd071 | 71 | Task task2(&gdt, taskB); |
sPymbed | 0:97ba3e2cd071 | 72 | taskManager.AddTask(&task1); |
sPymbed | 0:97ba3e2cd071 | 73 | taskManager.AddTask(&task2); |
sPymbed | 0:97ba3e2cd071 | 74 | */ |
sPymbed | 0:97ba3e2cd071 | 75 | |
sPymbed | 0:97ba3e2cd071 | 76 | //InterruptManager interrupts(0x20, &gdt, &taskManager); |
sPymbed | 0:97ba3e2cd071 | 77 | //SyscallHandler syscalls(&interrupts, 0x80); |
sPymbed | 0:97ba3e2cd071 | 78 | |
sPymbed | 0:97ba3e2cd071 | 79 | printf("Initializing Hardware, Stage 1\n"); |
sPymbed | 0:97ba3e2cd071 | 80 | |
sPymbed | 0:97ba3e2cd071 | 81 | #ifdef GRAPHICSMODE |
sPymbed | 0:97ba3e2cd071 | 82 | Desktop desktop(320, 200, 0x00, 0x00, 0xA8); |
sPymbed | 0:97ba3e2cd071 | 83 | #endif |
sPymbed | 0:97ba3e2cd071 | 84 | |
sPymbed | 0:97ba3e2cd071 | 85 | //DriverManager drvManager; |
sPymbed | 0:97ba3e2cd071 | 86 | |
sPymbed | 0:97ba3e2cd071 | 87 | #ifdef GRAPHICSMODE |
sPymbed | 0:97ba3e2cd071 | 88 | //KeyboardDriver keyboard(&interrupts, &desktop); |
sPymbed | 0:97ba3e2cd071 | 89 | #else |
sPymbed | 0:97ba3e2cd071 | 90 | //PrintfKeyboardEventHandler kbhandler; |
sPymbed | 0:97ba3e2cd071 | 91 | //KeyboardDriver keyboard(&interrupts, &kbhandler); |
sPymbed | 0:97ba3e2cd071 | 92 | #endif |
sPymbed | 0:97ba3e2cd071 | 93 | //drvManager.AddDriver(&keyboard); |
sPymbed | 0:97ba3e2cd071 | 94 | |
sPymbed | 0:97ba3e2cd071 | 95 | |
sPymbed | 0:97ba3e2cd071 | 96 | #ifdef GRAPHICSMODE |
sPymbed | 0:97ba3e2cd071 | 97 | //MouseDriver mouse(&interrupts, &desktop); |
sPymbed | 0:97ba3e2cd071 | 98 | #else |
sPymbed | 0:97ba3e2cd071 | 99 | //MouseToConsole mousehandler; |
sPymbed | 0:97ba3e2cd071 | 100 | //MouseDriver mouse(&interrupts, &mousehandler); |
sPymbed | 0:97ba3e2cd071 | 101 | #endif |
sPymbed | 0:97ba3e2cd071 | 102 | //drvManager.AddDriver(&mouse); |
sPymbed | 0:97ba3e2cd071 | 103 | |
sPymbed | 0:97ba3e2cd071 | 104 | //PeripheralComponentInterconnectController PCIController; |
sPymbed | 0:97ba3e2cd071 | 105 | //PCIController.SelectDrivers(&drvManager, &interrupts); |
sPymbed | 0:97ba3e2cd071 | 106 | |
sPymbed | 0:97ba3e2cd071 | 107 | #ifdef GRAPHICSMODE |
sPymbed | 0:97ba3e2cd071 | 108 | VideoGraphicsArray vga; |
sPymbed | 0:97ba3e2cd071 | 109 | #endif |
sPymbed | 0:97ba3e2cd071 | 110 | |
sPymbed | 0:97ba3e2cd071 | 111 | printf("Initializing Hardware, Stage 2\n"); |
sPymbed | 0:97ba3e2cd071 | 112 | //drvManager.ActivateAll(); |
sPymbed | 0:97ba3e2cd071 | 113 | |
sPymbed | 0:97ba3e2cd071 | 114 | printf("Initializing Hardware, Stage 3\n"); |
sPymbed | 0:97ba3e2cd071 | 115 | |
sPymbed | 0:97ba3e2cd071 | 116 | #ifdef GRAPHICSMODE |
sPymbed | 0:97ba3e2cd071 | 117 | vga.SetMode(320, 200, 8); |
sPymbed | 0:97ba3e2cd071 | 118 | Window win1(&desktop, 10, 10, 20, 20, 0xA8, 0x00, 0x00); |
sPymbed | 0:97ba3e2cd071 | 119 | desktop.AddChild(&win1); |
sPymbed | 0:97ba3e2cd071 | 120 | Window win2(&desktop, 40, 15, 30, 30, 0x00, 0xA8, 0x00); |
sPymbed | 0:97ba3e2cd071 | 121 | desktop.AddChild(&win2); |
sPymbed | 0:97ba3e2cd071 | 122 | #endif |
sPymbed | 0:97ba3e2cd071 | 123 | |
sPymbed | 0:97ba3e2cd071 | 124 | EthernetInterface interface; |
sPymbed | 0:97ba3e2cd071 | 125 | interface.disconnect(); |
sPymbed | 0:97ba3e2cd071 | 126 | interface.connect(); |
sPymbed | 0:97ba3e2cd071 | 127 | |
sPymbed | 0:97ba3e2cd071 | 128 | // Show the network address |
sPymbed | 0:97ba3e2cd071 | 129 | const char *ip = interface.get_ip_address(); |
sPymbed | 0:97ba3e2cd071 | 130 | printf("IP address is: %s\n", ip ? ip : "No IP"); |
sPymbed | 0:97ba3e2cd071 | 131 | |
sPymbed | 0:97ba3e2cd071 | 132 | //InternetControlMessageProtocol icmp(&ipv4); |
sPymbed | 0:97ba3e2cd071 | 133 | |
sPymbed | 0:97ba3e2cd071 | 134 | //interrupts.Activate(); |
sPymbed | 0:97ba3e2cd071 | 135 | |
sPymbed | 0:97ba3e2cd071 | 136 | printf("\n\n\n\n"); |
sPymbed | 0:97ba3e2cd071 | 137 | |
sPymbed | 1:4059ac7ebc65 | 138 | PrintfTCPHandler tcphandler; |
sPymbed | 0:97ba3e2cd071 | 139 | TCPServer server; |
sPymbed | 0:97ba3e2cd071 | 140 | UDPSocket sockUDP(&interface); //creat Ethernet socket |
sPymbed | 0:97ba3e2cd071 | 141 | SocketAddress sockAddr(SERVER_IP, PORT); |
sPymbed | 0:97ba3e2cd071 | 142 | |
sPymbed | 0:97ba3e2cd071 | 143 | server.open(&interface); |
sPymbed | 0:97ba3e2cd071 | 144 | |
sPymbed | 0:97ba3e2cd071 | 145 | server.bind(interface.get_ip_address(), 80); |
sPymbed | 0:97ba3e2cd071 | 146 | server.listen(); |
sPymbed | 0:97ba3e2cd071 | 147 | server.set_blocking(false); |
sPymbed | 0:97ba3e2cd071 | 148 | TCPSocket client; //!< client class |
sPymbed | 0:97ba3e2cd071 | 149 | SocketAddress clt_addr; |
sPymbed | 0:97ba3e2cd071 | 150 | int bufferSize = 512; |
sPymbed | 0:97ba3e2cd071 | 151 | char buffer[bufferSize]; |
sPymbed | 0:97ba3e2cd071 | 152 | |
sPymbed | 0:97ba3e2cd071 | 153 | //TransmissionControlProtocolSocket* tcpsocket = tcp.Connect(ip2_be, 1234); |
sPymbed | 0:97ba3e2cd071 | 154 | //tcpsocket->Send((uint8_t*)"Hello TCP!", 10); |
sPymbed | 0:97ba3e2cd071 | 155 | |
sPymbed | 0:97ba3e2cd071 | 156 | //icmp.RequestEchoReply(gip_be); |
sPymbed | 0:97ba3e2cd071 | 157 | |
sPymbed | 0:97ba3e2cd071 | 158 | //PrintfUDPHandler udphandler; |
sPymbed | 0:97ba3e2cd071 | 159 | |
sPymbed | 0:97ba3e2cd071 | 160 | //UserDatagramProtocolSocket* udpsocket = udp.Listen(1234); |
sPymbed | 0:97ba3e2cd071 | 161 | //udp.Bind(udpsocket, &udphandler); |
sPymbed | 2:453e73d66757 | 162 | |
sPymbed | 0:97ba3e2cd071 | 163 | while (true){ |
sPymbed | 0:97ba3e2cd071 | 164 | //printf("\nCLIENT - Sending '%i' to server %s\r\n", screen[0], SERVER_IP); //print message to send |
sPymbed | 0:97ba3e2cd071 | 165 | sockUDP.sendto(SERVER_IP, PORT, screen, sizeof(screen)); |
sPymbed | 0:97ba3e2cd071 | 166 | server.accept(&client, &clt_addr); |
sPymbed | 0:97ba3e2cd071 | 167 | int len = client.recv(&buffer, bufferSize); |
sPymbed | 0:97ba3e2cd071 | 168 | |
sPymbed | 0:97ba3e2cd071 | 169 | #ifdef DEBUG |
sPymbed | 0:97ba3e2cd071 | 170 | printf("%d: ", len); |
sPymbed | 0:97ba3e2cd071 | 171 | for (int i = 0; i < len; i++) |
sPymbed | 0:97ba3e2cd071 | 172 | printf("%d ", buffer[i]); |
sPymbed | 0:97ba3e2cd071 | 173 | printf("\r\n"); |
sPymbed | 0:97ba3e2cd071 | 174 | #endif |
sPymbed | 0:97ba3e2cd071 | 175 | |
sPymbed | 1:4059ac7ebc65 | 176 | tcphandler.HandleTransmissionControlProtocolMessage(&client, (char*)buffer, len); |
sPymbed | 0:97ba3e2cd071 | 177 | wait(0.03333333); |
sPymbed | 0:97ba3e2cd071 | 178 | } |
sPymbed | 0:97ba3e2cd071 | 179 | } |