WWW serwer SKM
Dependencies: SDFileSystem mbed
Fork of SKM_SERWER_WWW by
main.cpp@2:8d5a84fcf90c, 2017-11-13 (annotated)
- Committer:
- barti19941
- Date:
- Mon Nov 13 17:06:43 2017 +0000
- Revision:
- 2:8d5a84fcf90c
- Parent:
- 1:623f51ea713b
- Child:
- 3:c05bbec8d96b
v1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andcor02 | 0:f682dcf80f00 | 1 | #include "mbed.h" |
barti19941 | 2:8d5a84fcf90c | 2 | #include "EthernetInterface.h" |
barti19941 | 2:8d5a84fcf90c | 3 | #include "SDFileSystem.h" |
barti19941 | 2:8d5a84fcf90c | 4 | #include <string.h> |
barti19941 | 2:8d5a84fcf90c | 5 | #include <stdio.h> |
erigow01 | 1:623f51ea713b | 6 | |
barti19941 | 2:8d5a84fcf90c | 7 | #define PORT 80 |
erigow01 | 1:623f51ea713b | 8 | |
barti19941 | 2:8d5a84fcf90c | 9 | const char *ip = "192.168.3.2"; |
barti19941 | 2:8d5a84fcf90c | 10 | const char *mask = "255.255.255.0"; |
barti19941 | 2:8d5a84fcf90c | 11 | const char *gateway = "192.168.3.2"; |
andcor02 | 0:f682dcf80f00 | 12 | |
barti19941 | 2:8d5a84fcf90c | 13 | Serial pc(USBTX, USBRX); |
barti19941 | 2:8d5a84fcf90c | 14 | EthernetInterface eth; //uchwyt do Ethernet |
barti19941 | 2:8d5a84fcf90c | 15 | SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); //inicjalizacja karty SD (MOSI,MISO,SCK,CS) |
barti19941 | 2:8d5a84fcf90c | 16 | TCPSocketConnection client; |
barti19941 | 2:8d5a84fcf90c | 17 | TCPSocketServer svr; |
andcor02 | 0:f682dcf80f00 | 18 | |
barti19941 | 2:8d5a84fcf90c | 19 | bool server_is_listened = false; |
barti19941 | 2:8d5a84fcf90c | 20 | bool client_is_connected = false; |
erigow01 | 1:623f51ea713b | 21 | |
barti19941 | 2:8d5a84fcf90c | 22 | void Send_Data_From_SD() |
barti19941 | 2:8d5a84fcf90c | 23 | { |
barti19941 | 2:8d5a84fcf90c | 24 | int i = 0; |
barti19941 | 2:8d5a84fcf90c | 25 | const char *index = "/sd/www/index.html"; |
barti19941 | 2:8d5a84fcf90c | 26 | //const char *style = "/sd/www/style.css"; |
barti19941 | 2:8d5a84fcf90c | 27 | //const char *alien = "/sd/www/img/alien.jpg"; |
barti19941 | 2:8d5a84fcf90c | 28 | //const char *forrest = "/sd/www/img/forrest.jpg"; |
barti19941 | 2:8d5a84fcf90c | 29 | char send_char[1]; |
barti19941 | 2:8d5a84fcf90c | 30 | FILE *fp = fopen(index, "r"); |
barti19941 | 2:8d5a84fcf90c | 31 | while (1) |
barti19941 | 2:8d5a84fcf90c | 32 | { |
barti19941 | 2:8d5a84fcf90c | 33 | if (fgets(send_char,1, fp) == NULL) break; |
barti19941 | 2:8d5a84fcf90c | 34 | client.send(send_char, 1); //przerobic na get line |
barti19941 | 2:8d5a84fcf90c | 35 | i++; |
barti19941 | 2:8d5a84fcf90c | 36 | } |
barti19941 | 2:8d5a84fcf90c | 37 | fclose(fp); |
barti19941 | 2:8d5a84fcf90c | 38 | /* |
barti19941 | 2:8d5a84fcf90c | 39 | fp = fopen(style, "r"); |
barti19941 | 2:8d5a84fcf90c | 40 | while (fread(&send_char, sizeof(char), 1, fp) == 1) { |
barti19941 | 2:8d5a84fcf90c | 41 | client.send(&send_char, 1); |
barti19941 | 2:8d5a84fcf90c | 42 | } |
barti19941 | 2:8d5a84fcf90c | 43 | fclose(fp); |
barti19941 | 2:8d5a84fcf90c | 44 | fp = fopen(alien, "r"); |
barti19941 | 2:8d5a84fcf90c | 45 | while (fread(&send_char, sizeof(char), 1, fp) == 1) { |
barti19941 | 2:8d5a84fcf90c | 46 | client.send(&send_char, 1); |
barti19941 | 2:8d5a84fcf90c | 47 | } |
barti19941 | 2:8d5a84fcf90c | 48 | fclose(fp); |
barti19941 | 2:8d5a84fcf90c | 49 | fp = fopen(forrest, "r"); |
barti19941 | 2:8d5a84fcf90c | 50 | while (fread(&send_char, sizeof(char), 1, fp) == 1) { |
barti19941 | 2:8d5a84fcf90c | 51 | client.send(&send_char, 1); |
barti19941 | 2:8d5a84fcf90c | 52 | } |
barti19941 | 2:8d5a84fcf90c | 53 | fclose(fp); |
barti19941 | 2:8d5a84fcf90c | 54 | */ |
barti19941 | 2:8d5a84fcf90c | 55 | client_is_connected = false; |
barti19941 | 2:8d5a84fcf90c | 56 | |
barti19941 | 2:8d5a84fcf90c | 57 | } |
barti19941 | 2:8d5a84fcf90c | 58 | void Initialize_Server(void) |
barti19941 | 2:8d5a84fcf90c | 59 | { |
barti19941 | 2:8d5a84fcf90c | 60 | eth.init(ip,mask,gateway); //inicjalizacja ethernet, statyczny adres ip |
barti19941 | 2:8d5a84fcf90c | 61 | eth.connect(); //wlaczenie interfejsu |
barti19941 | 2:8d5a84fcf90c | 62 | printf("Zainicjowano Ethernet! \n"); |
barti19941 | 2:8d5a84fcf90c | 63 | printf("Adres IP: %s\n",eth.getIPAddress()); |
barti19941 | 2:8d5a84fcf90c | 64 | printf("Maska: %s\n",eth.getNetworkMask()); |
barti19941 | 2:8d5a84fcf90c | 65 | printf("Brama: %s\n",eth.getGateway()); |
barti19941 | 2:8d5a84fcf90c | 66 | printf("Adres MAC urzadzenia: %s\n",eth.getMACAddress()); |
barti19941 | 2:8d5a84fcf90c | 67 | if (svr.bind(PORT) < 0) |
barti19941 | 2:8d5a84fcf90c | 68 | { |
barti19941 | 2:8d5a84fcf90c | 69 | printf("Nie udalo sie powiazac serwera TCP z portem 80 (HTTP)! \n"); |
barti19941 | 2:8d5a84fcf90c | 70 | return; |
barti19941 | 2:8d5a84fcf90c | 71 | } |
barti19941 | 2:8d5a84fcf90c | 72 | else |
barti19941 | 2:8d5a84fcf90c | 73 | { |
barti19941 | 2:8d5a84fcf90c | 74 | printf("Powiazano serwer TCP z portem 80 (HTTP). \n"); |
barti19941 | 2:8d5a84fcf90c | 75 | server_is_listened = true; |
barti19941 | 2:8d5a84fcf90c | 76 | } |
barti19941 | 2:8d5a84fcf90c | 77 | |
barti19941 | 2:8d5a84fcf90c | 78 | if (svr.listen(1) < 0) |
barti19941 | 2:8d5a84fcf90c | 79 | { |
barti19941 | 2:8d5a84fcf90c | 80 | printf("Nie udalo sie rozpoczac nasluchiwania polaczen przychodzacych serwera TCP! \n"); |
barti19941 | 2:8d5a84fcf90c | 81 | return; |
barti19941 | 2:8d5a84fcf90c | 82 | } |
barti19941 | 2:8d5a84fcf90c | 83 | else |
barti19941 | 2:8d5a84fcf90c | 84 | { |
barti19941 | 2:8d5a84fcf90c | 85 | printf("Serwer TCP rozpoczal nasluchiwanie polaczen przychodzacych. \n"); |
barti19941 | 2:8d5a84fcf90c | 86 | } |
barti19941 | 2:8d5a84fcf90c | 87 | } |
barti19941 | 2:8d5a84fcf90c | 88 | |
barti19941 | 2:8d5a84fcf90c | 89 | int main() |
barti19941 | 2:8d5a84fcf90c | 90 | { |
barti19941 | 2:8d5a84fcf90c | 91 | pc.baud(9600); |
barti19941 | 2:8d5a84fcf90c | 92 | printf("Zainicjowano UART! \n"); |
barti19941 | 2:8d5a84fcf90c | 93 | printf("Predkosc bitowa transmisji - 9600b/s.\n"); |
barti19941 | 2:8d5a84fcf90c | 94 | Initialize_Server(); |
barti19941 | 2:8d5a84fcf90c | 95 | while (server_is_listened) { |
barti19941 | 2:8d5a84fcf90c | 96 | if (svr.accept(client)) |
barti19941 | 2:8d5a84fcf90c | 97 | { |
barti19941 | 2:8d5a84fcf90c | 98 | printf("Nie udalo sie zaakceptowac polaczenia z klientem! \n"); |
barti19941 | 2:8d5a84fcf90c | 99 | } |
barti19941 | 2:8d5a84fcf90c | 100 | else |
barti19941 | 2:8d5a84fcf90c | 101 | { |
barti19941 | 2:8d5a84fcf90c | 102 | printf("Polaczono z klientem. IP klienta: %s\n", client.get_address()); |
barti19941 | 2:8d5a84fcf90c | 103 | client_is_connected = true; |
barti19941 | 2:8d5a84fcf90c | 104 | while (client_is_connected) |
barti19941 | 2:8d5a84fcf90c | 105 | { |
barti19941 | 2:8d5a84fcf90c | 106 | Send_Data_From_SD(); |
erigow01 | 1:623f51ea713b | 107 | } |
barti19941 | 2:8d5a84fcf90c | 108 | client.close(); |
barti19941 | 2:8d5a84fcf90c | 109 | printf("Wyslano dane do klienta i rozlaczono sesje. \n"); |
andcor02 | 0:f682dcf80f00 | 110 | } |
andcor02 | 0:f682dcf80f00 | 111 | } |
andcor02 | 0:f682dcf80f00 | 112 | } |