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:
Thu Jun 13 13:08:37 2019 +0000
Revision:
0:97ba3e2cd071
Child:
1:4059ac7ebc65
version 1.0

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 <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 0:97ba3e2cd071 19 /*class TransmissionControlProtocolHandler
sPymbed 0:97ba3e2cd071 20 {
sPymbed 0:97ba3e2cd071 21 public:
sPymbed 0:97ba3e2cd071 22 TransmissionControlProtocolHandler();
sPymbed 0:97ba3e2cd071 23 ~TransmissionControlProtocolHandler();
sPymbed 0:97ba3e2cd071 24 virtual bool HandleTransmissionControlProtocolMessage(TCPSocket* socket, char* data, uint16_t size);
sPymbed 0:97ba3e2cd071 25 };
sPymbed 0:97ba3e2cd071 26
sPymbed 0:97ba3e2cd071 27 class PrintfTCPHandler : public TransmissionControlProtocolHandler
sPymbed 0:97ba3e2cd071 28 {
sPymbed 0:97ba3e2cd071 29 public:*/
sPymbed 0:97ba3e2cd071 30 bool HandleTransmissionControlProtocolMessage(TCPSocket* socket, char* data, uint16_t size)
sPymbed 0:97ba3e2cd071 31 {
sPymbed 0:97ba3e2cd071 32 if(size > 9
sPymbed 0:97ba3e2cd071 33 && data[0] == 'G'
sPymbed 0:97ba3e2cd071 34 && data[1] == 'E'
sPymbed 0:97ba3e2cd071 35 && data[2] == 'T'
sPymbed 0:97ba3e2cd071 36 && data[3] == ' '
sPymbed 0:97ba3e2cd071 37 && data[4] == '/'
sPymbed 0:97ba3e2cd071 38 && data[5] == ' '
sPymbed 0:97ba3e2cd071 39 && data[6] == 'H'
sPymbed 0:97ba3e2cd071 40 && data[7] == 'T'
sPymbed 0:97ba3e2cd071 41 && data[8] == 'T'
sPymbed 0:97ba3e2cd071 42 && data[9] == 'P'
sPymbed 0:97ba3e2cd071 43 )
sPymbed 0:97ba3e2cd071 44 {
sPymbed 0:97ba3e2cd071 45 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 46 socket->close();
sPymbed 0:97ba3e2cd071 47 }
sPymbed 0:97ba3e2cd071 48
sPymbed 0:97ba3e2cd071 49 return true;
sPymbed 0:97ba3e2cd071 50 }
sPymbed 0:97ba3e2cd071 51 //};
sPymbed 0:97ba3e2cd071 52
sPymbed 0:97ba3e2cd071 53 int main() {
sPymbed 0:97ba3e2cd071 54 red = 1;
sPymbed 0:97ba3e2cd071 55 blue = 0;
sPymbed 0:97ba3e2cd071 56 green = 1;
sPymbed 0:97ba3e2cd071 57
sPymbed 0:97ba3e2cd071 58 printf("Hello World! --- http://www.AlgorithMan.de\n");
sPymbed 0:97ba3e2cd071 59
sPymbed 0:97ba3e2cd071 60 //TaskManager taskManager;
sPymbed 0:97ba3e2cd071 61 /*
sPymbed 0:97ba3e2cd071 62 Task task1(&gdt, taskA);
sPymbed 0:97ba3e2cd071 63 Task task2(&gdt, taskB);
sPymbed 0:97ba3e2cd071 64 taskManager.AddTask(&task1);
sPymbed 0:97ba3e2cd071 65 taskManager.AddTask(&task2);
sPymbed 0:97ba3e2cd071 66 */
sPymbed 0:97ba3e2cd071 67
sPymbed 0:97ba3e2cd071 68 //InterruptManager interrupts(0x20, &gdt, &taskManager);
sPymbed 0:97ba3e2cd071 69 //SyscallHandler syscalls(&interrupts, 0x80);
sPymbed 0:97ba3e2cd071 70
sPymbed 0:97ba3e2cd071 71 printf("Initializing Hardware, Stage 1\n");
sPymbed 0:97ba3e2cd071 72
sPymbed 0:97ba3e2cd071 73 #ifdef GRAPHICSMODE
sPymbed 0:97ba3e2cd071 74 Desktop desktop(320, 200, 0x00, 0x00, 0xA8);
sPymbed 0:97ba3e2cd071 75 #endif
sPymbed 0:97ba3e2cd071 76
sPymbed 0:97ba3e2cd071 77 //DriverManager drvManager;
sPymbed 0:97ba3e2cd071 78
sPymbed 0:97ba3e2cd071 79 #ifdef GRAPHICSMODE
sPymbed 0:97ba3e2cd071 80 //KeyboardDriver keyboard(&interrupts, &desktop);
sPymbed 0:97ba3e2cd071 81 #else
sPymbed 0:97ba3e2cd071 82 //PrintfKeyboardEventHandler kbhandler;
sPymbed 0:97ba3e2cd071 83 //KeyboardDriver keyboard(&interrupts, &kbhandler);
sPymbed 0:97ba3e2cd071 84 #endif
sPymbed 0:97ba3e2cd071 85 //drvManager.AddDriver(&keyboard);
sPymbed 0:97ba3e2cd071 86
sPymbed 0:97ba3e2cd071 87
sPymbed 0:97ba3e2cd071 88 #ifdef GRAPHICSMODE
sPymbed 0:97ba3e2cd071 89 //MouseDriver mouse(&interrupts, &desktop);
sPymbed 0:97ba3e2cd071 90 #else
sPymbed 0:97ba3e2cd071 91 //MouseToConsole mousehandler;
sPymbed 0:97ba3e2cd071 92 //MouseDriver mouse(&interrupts, &mousehandler);
sPymbed 0:97ba3e2cd071 93 #endif
sPymbed 0:97ba3e2cd071 94 //drvManager.AddDriver(&mouse);
sPymbed 0:97ba3e2cd071 95
sPymbed 0:97ba3e2cd071 96 //PeripheralComponentInterconnectController PCIController;
sPymbed 0:97ba3e2cd071 97 //PCIController.SelectDrivers(&drvManager, &interrupts);
sPymbed 0:97ba3e2cd071 98
sPymbed 0:97ba3e2cd071 99 #ifdef GRAPHICSMODE
sPymbed 0:97ba3e2cd071 100 VideoGraphicsArray vga;
sPymbed 0:97ba3e2cd071 101 #endif
sPymbed 0:97ba3e2cd071 102
sPymbed 0:97ba3e2cd071 103 printf("Initializing Hardware, Stage 2\n");
sPymbed 0:97ba3e2cd071 104 //drvManager.ActivateAll();
sPymbed 0:97ba3e2cd071 105
sPymbed 0:97ba3e2cd071 106 printf("Initializing Hardware, Stage 3\n");
sPymbed 0:97ba3e2cd071 107
sPymbed 0:97ba3e2cd071 108 #ifdef GRAPHICSMODE
sPymbed 0:97ba3e2cd071 109 vga.SetMode(320, 200, 8);
sPymbed 0:97ba3e2cd071 110 Window win1(&desktop, 10, 10, 20, 20, 0xA8, 0x00, 0x00);
sPymbed 0:97ba3e2cd071 111 desktop.AddChild(&win1);
sPymbed 0:97ba3e2cd071 112 Window win2(&desktop, 40, 15, 30, 30, 0x00, 0xA8, 0x00);
sPymbed 0:97ba3e2cd071 113 desktop.AddChild(&win2);
sPymbed 0:97ba3e2cd071 114 #endif
sPymbed 0:97ba3e2cd071 115
sPymbed 0:97ba3e2cd071 116
sPymbed 0:97ba3e2cd071 117 /*
sPymbed 0:97ba3e2cd071 118 printf("\nS-ATA primary master: ");
sPymbed 0:97ba3e2cd071 119 AdvancedTechnologyAttachment ata0m(true, 0x1F0);
sPymbed 0:97ba3e2cd071 120 ata0m.Identify();
sPymbed 0:97ba3e2cd071 121
sPymbed 0:97ba3e2cd071 122 printf("\nS-ATA primary slave: ");
sPymbed 0:97ba3e2cd071 123 AdvancedTechnologyAttachment ata0s(false, 0x1F0);
sPymbed 0:97ba3e2cd071 124 ata0s.Identify();
sPymbed 0:97ba3e2cd071 125 ata0s.Write28(0, (uint8_t*)"http://www.AlgorithMan.de", 25);
sPymbed 0:97ba3e2cd071 126 ata0s.Flush();
sPymbed 0:97ba3e2cd071 127 ata0s.Read28(0, 25);
sPymbed 0:97ba3e2cd071 128
sPymbed 0:97ba3e2cd071 129 printf("\nS-ATA secondary master: ");
sPymbed 0:97ba3e2cd071 130 AdvancedTechnologyAttachment ata1m(true, 0x170);
sPymbed 0:97ba3e2cd071 131 ata1m.Identify();
sPymbed 0:97ba3e2cd071 132
sPymbed 0:97ba3e2cd071 133 printf("\nS-ATA secondary slave: ");
sPymbed 0:97ba3e2cd071 134 AdvancedTechnologyAttachment ata1s(false, 0x170);
sPymbed 0:97ba3e2cd071 135 ata1s.Identify();
sPymbed 0:97ba3e2cd071 136 // third: 0x1E8
sPymbed 0:97ba3e2cd071 137 // fourth: 0x168
sPymbed 0:97ba3e2cd071 138 */
sPymbed 0:97ba3e2cd071 139 EthernetInterface interface;
sPymbed 0:97ba3e2cd071 140 interface.disconnect();
sPymbed 0:97ba3e2cd071 141 interface.connect();
sPymbed 0:97ba3e2cd071 142
sPymbed 0:97ba3e2cd071 143 // Show the network address
sPymbed 0:97ba3e2cd071 144 const char *ip = interface.get_ip_address();
sPymbed 0:97ba3e2cd071 145 printf("IP address is: %s\n", ip ? ip : "No IP");
sPymbed 0:97ba3e2cd071 146
sPymbed 0:97ba3e2cd071 147 //AddressResolutionProtocol arp(&etherframe);
sPymbed 0:97ba3e2cd071 148
sPymbed 0:97ba3e2cd071 149 //InternetProtocolProvider ipv4(&etherframe, &arp, gip_be, subnet_be);
sPymbed 0:97ba3e2cd071 150 //InternetControlMessageProtocol icmp(&ipv4);
sPymbed 0:97ba3e2cd071 151 //UserDatagramProtocolProvider udp(&ipv4);
sPymbed 0:97ba3e2cd071 152 //TransmissionControlProtocolProvider tcp(&ipv4);
sPymbed 0:97ba3e2cd071 153
sPymbed 0:97ba3e2cd071 154
sPymbed 0:97ba3e2cd071 155 //interrupts.Activate();
sPymbed 0:97ba3e2cd071 156
sPymbed 0:97ba3e2cd071 157 printf("\n\n\n\n");
sPymbed 0:97ba3e2cd071 158
sPymbed 0:97ba3e2cd071 159 //PrintfTCPHandler tcphandler;
sPymbed 0:97ba3e2cd071 160 TCPServer server;
sPymbed 0:97ba3e2cd071 161 UDPSocket sockUDP(&interface); //creat Ethernet socket
sPymbed 0:97ba3e2cd071 162 SocketAddress sockAddr(SERVER_IP, PORT);
sPymbed 0:97ba3e2cd071 163
sPymbed 0:97ba3e2cd071 164 server.open(&interface);
sPymbed 0:97ba3e2cd071 165
sPymbed 0:97ba3e2cd071 166 server.bind(interface.get_ip_address(), 80);
sPymbed 0:97ba3e2cd071 167 server.listen();
sPymbed 0:97ba3e2cd071 168 server.set_blocking(false);
sPymbed 0:97ba3e2cd071 169 TCPSocket client; //!< client class
sPymbed 0:97ba3e2cd071 170 SocketAddress clt_addr;
sPymbed 0:97ba3e2cd071 171 int bufferSize = 512;
sPymbed 0:97ba3e2cd071 172 char buffer[bufferSize];
sPymbed 0:97ba3e2cd071 173
sPymbed 0:97ba3e2cd071 174 //TransmissionControlProtocolSocket* tcpsocket = tcp.Connect(ip2_be, 1234);
sPymbed 0:97ba3e2cd071 175 //tcpsocket->Send((uint8_t*)"Hello TCP!", 10);
sPymbed 0:97ba3e2cd071 176
sPymbed 0:97ba3e2cd071 177 //icmp.RequestEchoReply(gip_be);
sPymbed 0:97ba3e2cd071 178
sPymbed 0:97ba3e2cd071 179 //PrintfUDPHandler udphandler;
sPymbed 0:97ba3e2cd071 180 //UserDatagramProtocolSocket* udpsocket = udp.Connect(ip2_be, 1234);
sPymbed 0:97ba3e2cd071 181 //udp.Bind(udpsocket, &udphandler);
sPymbed 0:97ba3e2cd071 182 //udpsocket->Send((uint8_t*)"Hello UDP!", 10);
sPymbed 0:97ba3e2cd071 183
sPymbed 0:97ba3e2cd071 184 //UserDatagramProtocolSocket* udpsocket = udp.Listen(1234);
sPymbed 0:97ba3e2cd071 185 //udp.Bind(udpsocket, &udphandler);
sPymbed 0:97ba3e2cd071 186
sPymbed 0:97ba3e2cd071 187 while (true){
sPymbed 0:97ba3e2cd071 188 //printf("\nCLIENT - Sending '%i' to server %s\r\n", screen[0], SERVER_IP); //print message to send
sPymbed 0:97ba3e2cd071 189 sockUDP.sendto(SERVER_IP, PORT, screen, sizeof(screen));
sPymbed 0:97ba3e2cd071 190 server.accept(&client, &clt_addr);
sPymbed 0:97ba3e2cd071 191 int len = client.recv(&buffer, bufferSize);
sPymbed 0:97ba3e2cd071 192
sPymbed 0:97ba3e2cd071 193 #ifdef DEBUG
sPymbed 0:97ba3e2cd071 194 printf("%d: ", len);
sPymbed 0:97ba3e2cd071 195 for (int i = 0; i < len; i++)
sPymbed 0:97ba3e2cd071 196 printf("%d ", buffer[i]);
sPymbed 0:97ba3e2cd071 197 printf("\r\n");
sPymbed 0:97ba3e2cd071 198 #endif
sPymbed 0:97ba3e2cd071 199
sPymbed 0:97ba3e2cd071 200 HandleTransmissionControlProtocolMessage(&client, (char*)buffer, len);
sPymbed 0:97ba3e2cd071 201 wait(0.03333333);
sPymbed 0:97ba3e2cd071 202 }
sPymbed 0:97ba3e2cd071 203 }