Commit 106ecd02516f7877349cc5e90319087876c0e239

Authored by Ferràn Quer i Guerrero
1 parent 5fc47ae3

--no commit message

Project/applications/smartcities/Makefile
@@ -10,7 +10,7 @@ @@ -10,7 +10,7 @@
10 SDK_ROOT = ../../ 10 SDK_ROOT = ../../
11 PROJECT_OUT = smartcities 11 PROJECT_OUT = smartcities
12 12
13 -USER_SRC = main.c 13 +USER_SRC = main.c
14 USER_INCDIR = include/ 14 USER_INCDIR = include/
15 15
16 # if you need to add build Defines options add to USER_DEFS define 16 # if you need to add build Defines options add to USER_DEFS define
Project/applications/smartcities/httpClient.c
@@ -21,12 +21,14 @@ char* response; @@ -21,12 +21,14 @@ char* response;
21 int httpRequest(struct httpHeaders head, struct ip_addr remote_ip, u16_t remote_port, char* content, int content_size) 21 int httpRequest(struct httpHeaders head, struct ip_addr remote_ip, u16_t remote_port, char* content, int content_size)
22 { 22 {
23 // Check or default params 23 // Check or default params
  24 + printf("httpRequest: Checking params\r\n");
24 if (head.uri == NULL || content == NULL) return 1; 25 if (head.uri == NULL || content == NULL) return 1;
25 if (head.host == NULL) continue; //head.host = DEFAULT_REMOTE_PORT; 26 if (head.host == NULL) continue; //head.host = DEFAULT_REMOTE_PORT;
26 if (remote_ip == NULL || remote_ip == 0) remote_ip = DEFAULT_REMOTE_IP; 27 if (remote_ip == NULL || remote_ip == 0) remote_ip = DEFAULT_REMOTE_IP;
27 if (remote_port == NULL || remote_port == 0) remote_port = DEFAULT_REMOTE_PORT; 28 if (remote_port == NULL || remote_port == 0) remote_port = DEFAULT_REMOTE_PORT;
28 29
29 // Calculate header size 30 // Calculate header size
  31 + printf("httpRequest: Calculating header size\r\n");
30 int head_size = strlen(reqMethod2text(head.method)); 32 int head_size = strlen(reqMethod2text(head.method));
31 head_size += sizeof " " + head.uri_size + sizeof " HTTP/1.1" + sizeof ENDL; 33 head_size += sizeof " " + head.uri_size + sizeof " HTTP/1.1" + sizeof ENDL;
32 head_size += sizeof "Host: " + head.host_size + sizeof ENDL; 34 head_size += sizeof "Host: " + head.host_size + sizeof ENDL;
@@ -35,6 +37,7 @@ int httpRequest(struct httpHeaders head, struct ip_addr remote_ip, u16_t remote_ @@ -35,6 +37,7 @@ int httpRequest(struct httpHeaders head, struct ip_addr remote_ip, u16_t remote_
35 head_size += sizeof "Content-Length: " + numberofdigits(content_size) + 2*(sizeof ENDL) + sizeof '\0'; 37 head_size += sizeof "Content-Length: " + numberofdigits(content_size) + 2*(sizeof ENDL) + sizeof '\0';
36 38
37 // Build request head 39 // Build request head
  40 + printf("httpRequest: Building request head\r\n");
38 int request_size = head_size + content_size + 2*(sizeof ENDL) + sizeof '\0'; 41 int request_size = head_size + content_size + 2*(sizeof ENDL) + sizeof '\0';
39 request = (char *) malloc(request_size); 42 request = (char *) malloc(request_size);
40 strcpy(request, reqMethod2text(method)); 43 strcpy(request, reqMethod2text(method));
@@ -54,24 +57,30 @@ int httpRequest(struct httpHeaders head, struct ip_addr remote_ip, u16_t remote_ @@ -54,24 +57,30 @@ int httpRequest(struct httpHeaders head, struct ip_addr remote_ip, u16_t remote_
54 strcat(request, ENDL); 57 strcat(request, ENDL);
55 58
56 // Build request with content 59 // Build request with content
  60 + printf("httpRequest: Adding content to request string\r\n");
57 strcat(request, content); 61 strcat(request, content);
58 62
59 // Set connection 63 // Set connection
  64 + printf("httpRequest: Setting connection\r\n");
60 neocon = netconn_new(NETCONN_TCP); 65 neocon = netconn_new(NETCONN_TCP);
61 /*local_ip.addr = 0;//getip 66 /*local_ip.addr = 0;//getip
62 netconn_bind(neocon, IP_ADDR_ANY, LOCAL_PORT); //88 is provisional local port.*/ 67 netconn_bind(neocon, IP_ADDR_ANY, LOCAL_PORT); //88 is provisional local port.*/
63 netconn_connect(neocon, &remote_ip, remote_port); 68 netconn_connect(neocon, &remote_ip, remote_port);
64 69
65 // Send Request 70 // Send Request
  71 + printf("httpRequest: Sending request\r\n");
66 netconn_write(neocon, request, request_size, NETCONN_NOCOPY); 72 netconn_write(neocon, request, request_size, NETCONN_NOCOPY);
67 73
68 // Wait for Response 74 // Wait for Response
  75 + printf("httpRequest: Waiting for response\r\n");
69 neocon->recv_timeout = 5000; // for 5s 76 neocon->recv_timeout = 5000; // for 5s
70 netconn_recv(neocon, netBufs); 77 netconn_recv(neocon, netBufs);
71 78
72 // Manage Response 79 // Manage Response
  80 + printf("httpRequest: Response received. Let's parse the information\r\n");
73 response = (char*)malloc(4*sizeof char); 81 response = (char*)malloc(4*sizeof char);
74 netbuf_copy(netBuf,response,3,9); // read 3B starting from 9th -> read response code. "HTTP/1.1 301 Moved Permanently" 82 netbuf_copy(netBuf,response,3,9); // read 3B starting from 9th -> read response code. "HTTP/1.1 301 Moved Permanently"
  83 + printf("httpRequest: Unbelievablelybilbiyblyib successful! :D\r\n");
75 return response2int(response); 84 return response2int(response);
76 } 85 }
77 86