![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
2018-12-21 3
Dependencies: DataStorage NetworkManager IR_Manager WheelManager RestAPI_Manager
main.md@0:8eda451f71fa, 2018-10-08 (annotated)
- Committer:
- asaewing
- Date:
- Mon Oct 08 00:50:33 2018 +0000
- Revision:
- 0:8eda451f71fa
First
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
asaewing | 0:8eda451f71fa | 1 | #if !FEATURE_LWIP |
asaewing | 0:8eda451f71fa | 2 | #error [NOT_SUPPORTED] LWIP not supported for this target |
asaewing | 0:8eda451f71fa | 3 | #endif |
asaewing | 0:8eda451f71fa | 4 | |
asaewing | 0:8eda451f71fa | 5 | #include "mbed.h" |
asaewing | 0:8eda451f71fa | 6 | #include "EthernetInterface.h" |
asaewing | 0:8eda451f71fa | 7 | #include "TCPServer.h" |
asaewing | 0:8eda451f71fa | 8 | #include "TCPSocket.h" |
asaewing | 0:8eda451f71fa | 9 | |
asaewing | 0:8eda451f71fa | 10 | #include <stdio.h> |
asaewing | 0:8eda451f71fa | 11 | #include <iostream> |
asaewing | 0:8eda451f71fa | 12 | #include <string.h> |
asaewing | 0:8eda451f71fa | 13 | |
asaewing | 0:8eda451f71fa | 14 | #define HTTP_STATUS_LINE "HTTP/1.0 200 OK" |
asaewing | 0:8eda451f71fa | 15 | #define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8" |
asaewing | 0:8eda451f71fa | 16 | #define HTTP_MESSAGE_BODY "" \ |
asaewing | 0:8eda451f71fa | 17 | "<html>" "\r\n" \ |
asaewing | 0:8eda451f71fa | 18 | " <body style=\"display:flex;text-align:center\">" "\r\n" \ |
asaewing | 0:8eda451f71fa | 19 | " <div style=\"margin:auto\">" "\r\n" \ |
asaewing | 0:8eda451f71fa | 20 | " <h1>Hello ! mbed</h1>" "\r\n" \ |
asaewing | 0:8eda451f71fa | 21 | " <p>It works ! from mbed 01</p>" "\r\n" \ |
asaewing | 0:8eda451f71fa | 22 | " </div>" "\r\n" \ |
asaewing | 0:8eda451f71fa | 23 | " </body>" "\r\n" \ |
asaewing | 0:8eda451f71fa | 24 | "</html>" |
asaewing | 0:8eda451f71fa | 25 | |
asaewing | 0:8eda451f71fa | 26 | #define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n" \ |
asaewing | 0:8eda451f71fa | 27 | HTTP_HEADER_FIELDS "\r\n" \ |
asaewing | 0:8eda451f71fa | 28 | "\r\n" \ |
asaewing | 0:8eda451f71fa | 29 | HTTP_MESSAGE_BODY "\r\n" |
asaewing | 0:8eda451f71fa | 30 | |
asaewing | 0:8eda451f71fa | 31 | #define PORT 8080 |
asaewing | 0:8eda451f71fa | 32 | |
asaewing | 0:8eda451f71fa | 33 | #define IP "192.168.0.101" |
asaewing | 0:8eda451f71fa | 34 | #define GATEWAY "192.168.0.1" |
asaewing | 0:8eda451f71fa | 35 | #define MASK "255.255.255.0" |
asaewing | 0:8eda451f71fa | 36 | |
asaewing | 0:8eda451f71fa | 37 | EthernetInterface eth; |
asaewing | 0:8eda451f71fa | 38 | TCPServer srv; |
asaewing | 0:8eda451f71fa | 39 | TCPSocket clt_sock; |
asaewing | 0:8eda451f71fa | 40 | SocketAddress clt_addr; |
asaewing | 0:8eda451f71fa | 41 | |
asaewing | 0:8eda451f71fa | 42 | const char *ip = 0; |
asaewing | 0:8eda451f71fa | 43 | const char *mac = 0; |
asaewing | 0:8eda451f71fa | 44 | |
asaewing | 0:8eda451f71fa | 45 | DigitalOut led1(LED1); |
asaewing | 0:8eda451f71fa | 46 | DigitalOut led2(LED2); |
asaewing | 0:8eda451f71fa | 47 | DigitalOut led3(LED3); |
asaewing | 0:8eda451f71fa | 48 | |
asaewing | 0:8eda451f71fa | 49 | void split(char **arr, char *str, const char *del) { |
asaewing | 0:8eda451f71fa | 50 | char *s = strtok(str, del); |
asaewing | 0:8eda451f71fa | 51 | while(s != NULL) { |
asaewing | 0:8eda451f71fa | 52 | *arr++ = s; |
asaewing | 0:8eda451f71fa | 53 | s = strtok(NULL, del); |
asaewing | 0:8eda451f71fa | 54 | } |
asaewing | 0:8eda451f71fa | 55 | } |
asaewing | 0:8eda451f71fa | 56 | |
asaewing | 0:8eda451f71fa | 57 | /* Define function */ |
asaewing | 0:8eda451f71fa | 58 | void ServerInit() { |
asaewing | 0:8eda451f71fa | 59 | eth.disconnect(); |
asaewing | 0:8eda451f71fa | 60 | eth.set_network(IP,MASK,GATEWAY); |
asaewing | 0:8eda451f71fa | 61 | eth.connect(); |
asaewing | 0:8eda451f71fa | 62 | |
asaewing | 0:8eda451f71fa | 63 | ip = eth.get_ip_address(); |
asaewing | 0:8eda451f71fa | 64 | mac = eth.get_mac_address(); |
asaewing | 0:8eda451f71fa | 65 | printf("mbed's IP address is: %s\n\r", ip ? ip : "No IP"); |
asaewing | 0:8eda451f71fa | 66 | printf("mbed's MAC address is: %s\n\r", mac ? mac : "No MAC"); |
asaewing | 0:8eda451f71fa | 67 | } |
asaewing | 0:8eda451f71fa | 68 | |
asaewing | 0:8eda451f71fa | 69 | bool OpenServer() { |
asaewing | 0:8eda451f71fa | 70 | /* Open the server on ethernet stack */ |
asaewing | 0:8eda451f71fa | 71 | int ii=srv.open(ð); |
asaewing | 0:8eda451f71fa | 72 | |
asaewing | 0:8eda451f71fa | 73 | if (ii<0) { |
asaewing | 0:8eda451f71fa | 74 | printf("open error: %i \r\n",ii); |
asaewing | 0:8eda451f71fa | 75 | return true; |
asaewing | 0:8eda451f71fa | 76 | } |
asaewing | 0:8eda451f71fa | 77 | printf("server open\r\n"); |
asaewing | 0:8eda451f71fa | 78 | return false; |
asaewing | 0:8eda451f71fa | 79 | } |
asaewing | 0:8eda451f71fa | 80 | |
asaewing | 0:8eda451f71fa | 81 | bool BindPort() { |
asaewing | 0:8eda451f71fa | 82 | /* Bind the HTTP port (TCP 80) to the server */ |
asaewing | 0:8eda451f71fa | 83 | int ii=srv.bind(ip, 80); |
asaewing | 0:8eda451f71fa | 84 | if (ii<0) { |
asaewing | 0:8eda451f71fa | 85 | printf("bind error: %i \r\n",ii); |
asaewing | 0:8eda451f71fa | 86 | return true; |
asaewing | 0:8eda451f71fa | 87 | } |
asaewing | 0:8eda451f71fa | 88 | printf("Bound\r\n"); |
asaewing | 0:8eda451f71fa | 89 | return false; |
asaewing | 0:8eda451f71fa | 90 | } |
asaewing | 0:8eda451f71fa | 91 | |
asaewing | 0:8eda451f71fa | 92 | bool ListenConnections() { |
asaewing | 0:8eda451f71fa | 93 | /* Can handle 5 simultaneous connections */ |
asaewing | 0:8eda451f71fa | 94 | srv.listen(5); |
asaewing | 0:8eda451f71fa | 95 | if (srv.listen()<0) { |
asaewing | 0:8eda451f71fa | 96 | printf("listen error\r\n"); |
asaewing | 0:8eda451f71fa | 97 | return true; |
asaewing | 0:8eda451f71fa | 98 | } |
asaewing | 0:8eda451f71fa | 99 | printf("Listening\r\n"); |
asaewing | 0:8eda451f71fa | 100 | return false; |
asaewing | 0:8eda451f71fa | 101 | } |
asaewing | 0:8eda451f71fa | 102 | |
asaewing | 0:8eda451f71fa | 103 | bool CheckAccept() { |
asaewing | 0:8eda451f71fa | 104 | int ii = srv.accept(&clt_sock); |
asaewing | 0:8eda451f71fa | 105 | if (ii<0) { |
asaewing | 0:8eda451f71fa | 106 | printf("accept error: %i \r\n",ii); |
asaewing | 0:8eda451f71fa | 107 | return true; |
asaewing | 0:8eda451f71fa | 108 | } |
asaewing | 0:8eda451f71fa | 109 | printf("Socket Accepted\r\n"); |
asaewing | 0:8eda451f71fa | 110 | return false; |
asaewing | 0:8eda451f71fa | 111 | } |
asaewing | 0:8eda451f71fa | 112 | |
asaewing | 0:8eda451f71fa | 113 | bool EventLoop() { |
asaewing | 0:8eda451f71fa | 114 | while (true) { |
asaewing | 0:8eda451f71fa | 115 | srv.accept(&clt_sock, &clt_addr); |
asaewing | 0:8eda451f71fa | 116 | |
asaewing | 0:8eda451f71fa | 117 | //printf("The target IP address is '%s'\n", eth.get_ip_address()); |
asaewing | 0:8eda451f71fa | 118 | //printf("The target MAC address is '%s'\n", eth.get_mac_address()); |
asaewing | 0:8eda451f71fa | 119 | printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port()); |
asaewing | 0:8eda451f71fa | 120 | |
asaewing | 0:8eda451f71fa | 121 | // Receive data |
asaewing | 0:8eda451f71fa | 122 | char buf[100]; |
asaewing | 0:8eda451f71fa | 123 | clt_sock.recv(buf, 100); |
asaewing | 0:8eda451f71fa | 124 | printf("recv: %s\n", buf); |
asaewing | 0:8eda451f71fa | 125 | |
asaewing | 0:8eda451f71fa | 126 | char *arr[10]; |
asaewing | 0:8eda451f71fa | 127 | split(arr, buf, " "); |
asaewing | 0:8eda451f71fa | 128 | |
asaewing | 0:8eda451f71fa | 129 | /*for(int jj=0;jj<10;jj++) { |
asaewing | 0:8eda451f71fa | 130 | printf("## %03d: %s\n", jj, arr[jj]); |
asaewing | 0:8eda451f71fa | 131 | }*/ |
asaewing | 0:8eda451f71fa | 132 | |
asaewing | 0:8eda451f71fa | 133 | if (strcmp(arr[0], "GET")==0) { |
asaewing | 0:8eda451f71fa | 134 | printf("## ##: GET\n"); |
asaewing | 0:8eda451f71fa | 135 | char response[] = HTTP_STATUS_LINE; |
asaewing | 0:8eda451f71fa | 136 | strcat(response, "\r\n"); |
asaewing | 0:8eda451f71fa | 137 | strcat(response, HTTP_HEADER_FIELDS); |
asaewing | 0:8eda451f71fa | 138 | strcat(response, "\r\n"); |
asaewing | 0:8eda451f71fa | 139 | strcat(response, "\r\n"); |
asaewing | 0:8eda451f71fa | 140 | strcat(response, "001 GET\r\n"); |
asaewing | 0:8eda451f71fa | 141 | clt_sock.send(response, strlen(response)); |
asaewing | 0:8eda451f71fa | 142 | } |
asaewing | 0:8eda451f71fa | 143 | |
asaewing | 0:8eda451f71fa | 144 | if (strcmp(arr[0], "POST")==0) { |
asaewing | 0:8eda451f71fa | 145 | printf("## ##: POST\n"); |
asaewing | 0:8eda451f71fa | 146 | char response[] = HTTP_STATUS_LINE; |
asaewing | 0:8eda451f71fa | 147 | strcat(response, "\r\n"); |
asaewing | 0:8eda451f71fa | 148 | strcat(response, HTTP_HEADER_FIELDS); |
asaewing | 0:8eda451f71fa | 149 | strcat(response, "\r\n"); |
asaewing | 0:8eda451f71fa | 150 | strcat(response, "\r\n"); |
asaewing | 0:8eda451f71fa | 151 | strcat(response, "002 POST\r\n"); |
asaewing | 0:8eda451f71fa | 152 | clt_sock.send(response, strlen(response)); |
asaewing | 0:8eda451f71fa | 153 | } |
asaewing | 0:8eda451f71fa | 154 | |
asaewing | 0:8eda451f71fa | 155 | // Send data |
asaewing | 0:8eda451f71fa | 156 | //clt_sock.send(HTTP_RESPONSE, strlen(HTTP_RESPONSE)); |
asaewing | 0:8eda451f71fa | 157 | |
asaewing | 0:8eda451f71fa | 158 | // Close the socket |
asaewing | 0:8eda451f71fa | 159 | clt_sock.close(); |
asaewing | 0:8eda451f71fa | 160 | |
asaewing | 0:8eda451f71fa | 161 | } |
asaewing | 0:8eda451f71fa | 162 | return false; |
asaewing | 0:8eda451f71fa | 163 | } |
asaewing | 0:8eda451f71fa | 164 | |
asaewing | 0:8eda451f71fa | 165 | /* Main function */ |
asaewing | 0:8eda451f71fa | 166 | int main(){ |
asaewing | 0:8eda451f71fa | 167 | printf("mbed HTTP server for REST API\n"); |
asaewing | 0:8eda451f71fa | 168 | |
asaewing | 0:8eda451f71fa | 169 | ServerInit(); |
asaewing | 0:8eda451f71fa | 170 | led1 = 1; |
asaewing | 0:8eda451f71fa | 171 | led2 = 0; |
asaewing | 0:8eda451f71fa | 172 | led3 = 0; |
asaewing | 0:8eda451f71fa | 173 | wait(0.1); |
asaewing | 0:8eda451f71fa | 174 | |
asaewing | 0:8eda451f71fa | 175 | if (OpenServer()) return 0; |
asaewing | 0:8eda451f71fa | 176 | |
asaewing | 0:8eda451f71fa | 177 | if (BindPort()) return 0; |
asaewing | 0:8eda451f71fa | 178 | |
asaewing | 0:8eda451f71fa | 179 | if (ListenConnections()) return 0; |
asaewing | 0:8eda451f71fa | 180 | |
asaewing | 0:8eda451f71fa | 181 | //if (CheckAccept()) return 0; |
asaewing | 0:8eda451f71fa | 182 | |
asaewing | 0:8eda451f71fa | 183 | led1 = 0; |
asaewing | 0:8eda451f71fa | 184 | led2 = 1; |
asaewing | 0:8eda451f71fa | 185 | led3 = 0; |
asaewing | 0:8eda451f71fa | 186 | wait(0.1); |
asaewing | 0:8eda451f71fa | 187 | |
asaewing | 0:8eda451f71fa | 188 | if (EventLoop()) return 0; |
asaewing | 0:8eda451f71fa | 189 | |
asaewing | 0:8eda451f71fa | 190 | } |