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:54:10 2019 +0000
Revision:
1:4059ac7ebc65
Parent:
0:97ba3e2cd071
Child:
2:453e73d66757
improved

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