Fx0 hackathon team4

Dependencies:   NySNICInterface mbed-rtos mbed

Fork of RESTServerSample by KDDI Fx0 hackathon

Committer:
yi
Date:
Sun Feb 15 13:18:24 2015 +0000
Revision:
9:01aa69185ed8
Parent:
8:babc71c8c498
fixed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
komoritan 0:998e2e00df0c 1 #include "HTTPServer.h"
komoritan 0:998e2e00df0c 2 #include "mbed.h"
komoritan 0:998e2e00df0c 3
yi 2:2f187e09bdb0 4 #include "control_motors.h"
yi 1:e821c773d5f0 5
komoritan 0:998e2e00df0c 6
komoritan 0:998e2e00df0c 7 bool cmp(char* a, char* b)
komoritan 0:998e2e00df0c 8 {
komoritan 0:998e2e00df0c 9 return strcmp(a,b) < 0;
komoritan 0:998e2e00df0c 10 }
komoritan 0:998e2e00df0c 11
komoritan 0:998e2e00df0c 12
komoritan 0:998e2e00df0c 13 HTTPServer::HTTPServer():
komoritan 0:998e2e00df0c 14 reply()
komoritan 0:998e2e00df0c 15 {
komoritan 0:998e2e00df0c 16 }
komoritan 0:998e2e00df0c 17
komoritan 0:998e2e00df0c 18
komoritan 0:998e2e00df0c 19 HTTPServer::~HTTPServer()
komoritan 0:998e2e00df0c 20 {
komoritan 0:998e2e00df0c 21 }
komoritan 0:998e2e00df0c 22
komoritan 0:998e2e00df0c 23
komoritan 0:998e2e00df0c 24 bool HTTPServer::init(int port)
komoritan 0:998e2e00df0c 25 {
komoritan 0:998e2e00df0c 26 DigitalOut led4(LED4);
komoritan 0:998e2e00df0c 27
komoritan 0:998e2e00df0c 28 socketserver.set_blocking(true);
komoritan 0:998e2e00df0c 29 if(socketserver.bind(port))
komoritan 0:998e2e00df0c 30 {
komoritan 0:998e2e00df0c 31 printf("Could not bind on port %d.\n", port);
komoritan 0:998e2e00df0c 32 return false;
komoritan 0:998e2e00df0c 33 }
komoritan 0:998e2e00df0c 34
komoritan 0:998e2e00df0c 35 if(socketserver.listen())
komoritan 0:998e2e00df0c 36 {
komoritan 0:998e2e00df0c 37 printf("Could not listen %d.\n", port);
komoritan 0:998e2e00df0c 38 return false;
komoritan 0:998e2e00df0c 39 }
komoritan 0:998e2e00df0c 40
komoritan 0:998e2e00df0c 41 led4 = 1; // server is ready
komoritan 0:998e2e00df0c 42
komoritan 0:998e2e00df0c 43 return true;
komoritan 0:998e2e00df0c 44 }
komoritan 0:998e2e00df0c 45
komoritan 0:998e2e00df0c 46
komoritan 0:998e2e00df0c 47 void HTTPServer::run()
komoritan 0:998e2e00df0c 48 {
komoritan 0:998e2e00df0c 49 char buffer[1024];
komoritan 0:998e2e00df0c 50 TCPSocketConnection c;
komoritan 0:998e2e00df0c 51
komoritan 0:998e2e00df0c 52 while(true)
komoritan 0:998e2e00df0c 53 {
komoritan 0:998e2e00df0c 54 while(socketserver.accept(&c));
komoritan 0:998e2e00df0c 55 c.set_blocking(false, 1000);
yi 1:e821c773d5f0 56
komoritan 0:998e2e00df0c 57 while(c.is_connected())
komoritan 0:998e2e00df0c 58 {
komoritan 0:998e2e00df0c 59 int n = c.receive(buffer, sizeof(buffer)-1);
yi 1:e821c773d5f0 60
komoritan 0:998e2e00df0c 61 if(n == 0)
komoritan 0:998e2e00df0c 62 {
komoritan 0:998e2e00df0c 63 c.close();
komoritan 0:998e2e00df0c 64 break;
komoritan 0:998e2e00df0c 65 }
komoritan 0:998e2e00df0c 66 else if(n != -1)
komoritan 0:998e2e00df0c 67 {
komoritan 0:998e2e00df0c 68 buffer[n] = '\0';
yi 8:babc71c8c498 69 //printf("Received data -- %s --. \r\n", buffer);
komoritan 0:998e2e00df0c 70 handle_request(buffer);
komoritan 0:998e2e00df0c 71 create_response(buffer);
yi 8:babc71c8c498 72 //printf("Sending data -- %s --. \r\n", buffer);
komoritan 0:998e2e00df0c 73 c.send_all(buffer, strlen(buffer));
yi 8:babc71c8c498 74 //printf("done. \r\n");
komoritan 0:998e2e00df0c 75 c.close();
komoritan 0:998e2e00df0c 76 break;
komoritan 0:998e2e00df0c 77 }
komoritan 0:998e2e00df0c 78 else {
komoritan 0:998e2e00df0c 79 printf("Error while receiving data. \r\n");
komoritan 0:998e2e00df0c 80 c.close();
komoritan 0:998e2e00df0c 81 break;
komoritan 0:998e2e00df0c 82 }
komoritan 0:998e2e00df0c 83 }
komoritan 0:998e2e00df0c 84 }
komoritan 0:998e2e00df0c 85 }
komoritan 0:998e2e00df0c 86
komoritan 0:998e2e00df0c 87
komoritan 0:998e2e00df0c 88 void HTTPServer::handle_request(char *buffer)
komoritan 0:998e2e00df0c 89 {
komoritan 0:998e2e00df0c 90 char* request_type = strtok(buffer, " ");
komoritan 0:998e2e00df0c 91 char* request = strtok(NULL, " ");
komoritan 0:998e2e00df0c 92
yi 5:70c9f6045f2d 93 // リクエストされたURLをパースし、モーターを制御
yi 5:70c9f6045f2d 94 bool ret = parse_request(request);
yi 5:70c9f6045f2d 95 if(ret){
yi 5:70c9f6045f2d 96 response_code = HTTP_200_OK;
yi 5:70c9f6045f2d 97 }else{
yi 5:70c9f6045f2d 98 response_code = HTTP_404_NOTFOUND;
komoritan 0:998e2e00df0c 99 }
komoritan 0:998e2e00df0c 100
yi 5:70c9f6045f2d 101 reply[0] = '\0';
komoritan 0:998e2e00df0c 102 }
komoritan 0:998e2e00df0c 103
komoritan 0:998e2e00df0c 104 void HTTPServer::create_response(char *buffer)
komoritan 0:998e2e00df0c 105 {
komoritan 0:998e2e00df0c 106 char content_length[30] = "";
komoritan 0:998e2e00df0c 107 buffer[0] = '\0';
komoritan 0:998e2e00df0c 108
komoritan 0:998e2e00df0c 109 /* HTTP Status Code */
komoritan 0:998e2e00df0c 110 strcat(buffer, "HTTP/1.1 ");
komoritan 0:998e2e00df0c 111 switch(response_code){
komoritan 0:998e2e00df0c 112 case HTTP_200_OK:
komoritan 0:998e2e00df0c 113 strcat(buffer, "200 OK\r\n");
komoritan 0:998e2e00df0c 114 break;
komoritan 0:998e2e00df0c 115 case HTTP_404_NOTFOUND:
komoritan 0:998e2e00df0c 116 strcat(buffer, "404 Not Found\r\n");
komoritan 0:998e2e00df0c 117 break;
komoritan 0:998e2e00df0c 118 default:
komoritan 0:998e2e00df0c 119 strcat(buffer, "500 Internal Server Error\r\n");
komoritan 0:998e2e00df0c 120 break;
komoritan 0:998e2e00df0c 121 }
komoritan 0:998e2e00df0c 122
komoritan 0:998e2e00df0c 123 /* add header */
komoritan 0:998e2e00df0c 124 strcat(buffer, "Access-Control-Allow-Origin: *\r\n");
komoritan 0:998e2e00df0c 125 sprintf(content_length, "Content-Length: %d\r\n", strlen(reply));
komoritan 0:998e2e00df0c 126 strncat(buffer, content_length, strlen(content_length));
komoritan 0:998e2e00df0c 127 strcat(buffer, "Content-Type: text/plain\r\n\r\n");
komoritan 0:998e2e00df0c 128
komoritan 0:998e2e00df0c 129 /* add content */
komoritan 0:998e2e00df0c 130 strcat(buffer, reply);
komoritan 0:998e2e00df0c 131 }