WWW serwer SKM
Dependencies: SDFileSystem mbed
Fork of SKM_SERWER_WWW by
Diff: main.cpp
- Revision:
- 2:8d5a84fcf90c
- Parent:
- 1:623f51ea713b
- Child:
- 3:c05bbec8d96b
--- a/main.cpp Mon Feb 23 15:57:49 2015 +0000 +++ b/main.cpp Mon Nov 13 17:06:43 2017 +0000 @@ -1,44 +1,112 @@ #include "mbed.h" -//Simple program allowing user to send messages from mbed COM out through UART to another UART COM. -//Andrea Corrado -//Updated 2015/02/23 Eric Gowland - Process on line termination. +#include "EthernetInterface.h" +#include "SDFileSystem.h" +#include <string.h> +#include <stdio.h> +#define PORT 80 -Serial pc(USBTX, USBRX); // tx, rx -Serial uart (PTC17, PTC16); +const char *ip = "192.168.3.2"; +const char *mask = "255.255.255.0"; +const char *gateway = "192.168.3.2"; -char* PARSE_TRIGGERS = "\r\n"; -int PARSE_TRIGGERS_LENGTH = 2; -int MAX_BUFFER_SIZE = 128; +Serial pc(USBTX, USBRX); +EthernetInterface eth; //uchwyt do Ethernet +SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); //inicjalizacja karty SD (MOSI,MISO,SCK,CS) +TCPSocketConnection client; +TCPSocketServer svr; -bool isTriggerChar(char c); +bool server_is_listened = false; +bool client_is_connected = false; -int main() { - char buffer[MAX_BUFFER_SIZE]; - int pos = 0; - char thisChar = 0; - while(1) { - if (pc.readable()) { - thisChar = pc.getc(); - //Echo - pc.putc(thisChar); - //Buffer - buffer[pos++] = thisChar; - //If trigger or buffer overflow, output and reset buffer... - if(pos >= MAX_BUFFER_SIZE || isTriggerChar(thisChar)) { - pc.printf("\r\n"); - uart.printf("%.*s\r\n", pos, buffer); - pos = 0; +void Send_Data_From_SD() +{ + int i = 0; + const char *index = "/sd/www/index.html"; + //const char *style = "/sd/www/style.css"; + //const char *alien = "/sd/www/img/alien.jpg"; + //const char *forrest = "/sd/www/img/forrest.jpg"; + char send_char[1]; + FILE *fp = fopen(index, "r"); + while (1) + { + if (fgets(send_char,1, fp) == NULL) break; + client.send(send_char, 1); //przerobic na get line + i++; + } + fclose(fp); + /* + fp = fopen(style, "r"); + while (fread(&send_char, sizeof(char), 1, fp) == 1) { + client.send(&send_char, 1); + } + fclose(fp); + fp = fopen(alien, "r"); + while (fread(&send_char, sizeof(char), 1, fp) == 1) { + client.send(&send_char, 1); + } + fclose(fp); + fp = fopen(forrest, "r"); + while (fread(&send_char, sizeof(char), 1, fp) == 1) { + client.send(&send_char, 1); + } + fclose(fp); + */ + client_is_connected = false; + +} +void Initialize_Server(void) +{ + eth.init(ip,mask,gateway); //inicjalizacja ethernet, statyczny adres ip + eth.connect(); //wlaczenie interfejsu + printf("Zainicjowano Ethernet! \n"); + printf("Adres IP: %s\n",eth.getIPAddress()); + printf("Maska: %s\n",eth.getNetworkMask()); + printf("Brama: %s\n",eth.getGateway()); + printf("Adres MAC urzadzenia: %s\n",eth.getMACAddress()); + if (svr.bind(PORT) < 0) + { + printf("Nie udalo sie powiazac serwera TCP z portem 80 (HTTP)! \n"); + return; + } + else + { + printf("Powiazano serwer TCP z portem 80 (HTTP). \n"); + server_is_listened = true; + } + + if (svr.listen(1) < 0) + { + printf("Nie udalo sie rozpoczac nasluchiwania polaczen przychodzacych serwera TCP! \n"); + return; + } + else + { + printf("Serwer TCP rozpoczal nasluchiwanie polaczen przychodzacych. \n"); + } +} + +int main() +{ + pc.baud(9600); + printf("Zainicjowano UART! \n"); + printf("Predkosc bitowa transmisji - 9600b/s.\n"); + Initialize_Server(); + while (server_is_listened) { + if (svr.accept(client)) + { + printf("Nie udalo sie zaakceptowac polaczenia z klientem! \n"); + } + else + { + printf("Polaczono z klientem. IP klienta: %s\n", client.get_address()); + client_is_connected = true; + while (client_is_connected) + { + Send_Data_From_SD(); } + client.close(); + printf("Wyslano dane do klienta i rozlaczono sesje. \n"); } } } - -bool isTriggerChar(char c) { - for (int i = 0; i < PARSE_TRIGGERS_LENGTH; i++) { - if(c == PARSE_TRIGGERS[i]) { - return true; - } - } - return false; -}