Commit a83c0d534bf6fa9fa0ba29c516ae6af4a3ef0c6a

Authored by Imanol-Mikel Barba Sabariego
1 parent 73758932

--no commit message

Project/applications/smartcities/httpClient.c
1   -#include "libwismart.h"
2 1 #include "httpClient.h"
  2 +
3 3 /*
4 4 ------------
5 5 PUT /path/file.html HTTP/1.1
... ... @@ -10,37 +10,40 @@ Content-Type: application/json; charset=UTF-8
10 10 -----------
11 11 */
12 12  
13   -struct netconn* neocon;
14   -struct ip_addr local_ip;
15   -struct ip_addr remote_ip;//{128,30,52,37};
16   -struct netbuf* netBufs;
  13 +int httpRequest(struct httpHeaders head, char* content, int content_size)
  14 +{
  15 + struct netconn *neocon = NULL;
  16 + struct ip_addr remote_ip;
  17 + struct netbuf *netBufs;
17 18  
18   -char* request;
19   -char* response;
  19 + char* request;
  20 + char* response;
20 21  
21   -err_t err;
  22 + //LE PARCHE
  23 + initialize_IP(&remote_ip,SERVER_IP);
22 24  
23   -int httpRequest(struct httpHeaders head, char* content, int content_size)
24   -{
25 25 // Check or default params
26 26 printf("httpRequest: Checking params\r\n");
27   - if (head.uri == NULL) return 1;
28   - if (head.host == NULL); //head.host = DEFAULT_REMOTE_PORT;
  27 + if (head.uri == NULL || head.host == NULL)
  28 + {
  29 + return 1;
  30 + }
29 31  
30 32 // Calculate header size
31 33 printf("httpRequest: Calculating header size\r\n");
32 34 int head_size = strlen(reqMethod2text(head.method));
33   - head_size += sizeof " " + head.uri_size + sizeof " HTTP/1.1" + sizeof ENDL;
34   - head_size += sizeof "Host: " + head.host_size + sizeof ENDL;
  35 + head_size += strlen(" ") + head.uri_size + strlen(" HTTP/1.1") + strlen(ENDL);
  36 + head_size += strlen("Host: ") + head.host_size + strlen(ENDL);
35 37 //if (remote_port != '0') head_size += sizeof ":" + 2*sizeof(char);
36   - if(content_size > 0)head_size += sizeof CONTENT_TYPE_HEADER + sizeof ENDL;
37   - head_size += sizeof "Content-Length: " + numberofdigits(content_size) + 2*(sizeof ENDL) + sizeof '\0';
38   -
  38 + if(content_size > 0)
  39 + {
  40 + head_size += strlen(CONTENT_TYPE_HEADER) + strlen(ENDL);
  41 + head_size += strlen("Content-Length: ") + numberofdigits(content_size) + 2*(strlen(ENDL));
  42 + }
39 43 // Build request head
40 44 printf("httpRequest: Building request head\r\n");
41   - int request_size = head_size + content_size + 2*(sizeof ENDL) + sizeof '\0';
  45 + int request_size = head_size + content_size + 2*(strlen(ENDL));
42 46 request = (char *) chHeapAlloc(NULL,request_size);
43   -
44 47 strcpy(request, reqMethod2text(head.method));
45 48 strcat(request, " ");
46 49 strcat(request, head.uri);
... ... @@ -49,9 +52,15 @@ int httpRequest(struct httpHeaders head, char* content, int content_size)
49 52 strcat(request, head.host);
50 53 //if (remote_port != '0'){ strcat(request, ":"); strcat(request, remote_port); }
51 54 strcat(request, ENDL);
52   - if(content_size > 0){ strcat(request, CONTENT_TYPE_HEADER); strcat(request, ENDL); }
  55 + if(content_size > 0)
  56 + {
  57 + strcat(request, CONTENT_TYPE_HEADER);
  58 + strcat(request, ENDL);
  59 + }
53 60 strcat(request, "Content-Length: ");
54   - strcat(request, int2string(content_size));
  61 + char *str_content_size = int2string(content_size);
  62 + strcat(request, str_content_size);
  63 + chHeapFree(str_content_size);
55 64  
56 65 // Interlude
57 66 strcat(request, ENDL);
... ... @@ -62,36 +71,22 @@ int httpRequest(struct httpHeaders head, char* content, int content_size)
62 71 {
63 72 printf("httpRequest: Adding content to request string\r\n");
64 73 strcat(request, content);
65   - }else
  74 + }
  75 + else
  76 + {
66 77 printf("httpRequest: Skipping void content\r\n");
67   -
  78 + }
  79 + printf("packet: %s\r\n",request);
68 80 // Set connection
69 81 printf("httpRequest: Setting connection\r\n");
70   - remote_ip.addr = ipaddr_addr(SERVER_IP);
71   -
72   - while(libwismart_IsConnected() != WISMART_CONNECTED){
73   - DBG("Waiting WiFi Connection..");
74   - chThdSleepMilliseconds(2000);
75   - }
76   -
77 82 neocon = netconn_new(NETCONN_TCP);
78   - while(neocon == NULL){
79   - printf("`->netconn_new() Failed!\r\n");
80   - chThdSleepMilliseconds(2000);
81   - neocon = netconn_new(NETCONN_TCP);
82   - }
83   - printf("`->netconn_new ok\r\n");
84   -
85   - local_ip.addr = 0;//getip
86   - netconn_bind(neocon, IP_ADDR_ANY, 0); //0 means any port
87   - err = netconn_connect(neocon, &remote_ip, DEFAULT_REMOTE_PORT);
88   - while (ERR_OK != err)
  83 + if(neocon == NULL)
89 84 {
90   - printf("`->netconn_connect ko\r\n");
91   - chThdSleepMilliseconds(2000);
92   - err = netconn_connect(neocon, &remote_ip, DEFAULT_REMOTE_PORT);
  85 + printf("Socket creation FAILED\n\r");
93 86 }
94   - printf("`->netconn_connect ok\r\n");
  87 + /*local_ip.addr = 0;//getip
  88 + netconn_bind(neocon, IP_ADDR_ANY, LOCAL_PORT); //88 is provisional local port.*/
  89 + netconn_connect(neocon, &remote_ip, DEFAULT_REMOTE_PORT);
95 90  
96 91 // Send Request
97 92 printf("httpRequest: Sending request\r\n");
... ... @@ -100,21 +95,68 @@ int httpRequest(struct httpHeaders head, char* content, int content_size)
100 95  
101 96 // Wait for Response
102 97 printf("httpRequest: Waiting for response\r\n");
103   - neocon->recv_timeout = 5000; // for 5s
104   - err = netconn_recv(neocon, &netBufs);
105   - if (err !=ERR_OK)
106   - printf("`->netconn_recv error\r\n");
107   - else
108   - printf("`->netconn_recv ok\r\n");
  98 + //neocon->recv_timeout = 5000; // for 5s
  99 + netconn_recv(neocon, &netBufs);
109 100  
110 101 // Manage Response
111 102 printf("httpRequest: Response received. Let's parse the information\r\n");
112 103 response = (char*)chHeapAlloc(NULL,4*sizeof(char));
113   - netbuf_copy_partial(netBufs,response,3,9); // read 3 Bytes starting from 9th -> read response code. "HTTP/1.1 301 Moved Permanently"
114   - printf("httpRequest: Unbelievablelybilbiyblyib successful! :D\r\nResponse code: %s",response);
115   - return response2int(response);
  104 + netbuf_copy_partial(netBufs,response,3,9); // read 3B starting from 9th -> read response code. "HTTP/1.1 301 Moved Permanently"
  105 + printf("httpRequest: Unbelievablelybilbiyblyib successful! :D\r\nResponse code: %s\r\n",response);
  106 + int http_response = response2int(response);
  107 + chHeapFree(response);
  108 + return http_response;
  109 +}
  110 +
  111 +void initialize_IP(struct ip_addr* addr,char* IP)
  112 +{
  113 + uint8_t b[4],i,j = 0,k = 0,l;
  114 + char byte[4];
  115 + for(i = 0; i < (strlen(IP)+1); i++)
  116 + {
  117 + if(IP[i] == '.' || IP[i] == '\0')
  118 + {
  119 + for(l = 0; l < (3-j); l++)
  120 + {
  121 + byte[2] = byte[1];
  122 + byte[1] = byte[0];
  123 + byte[0] = '0';
  124 + }
  125 + b[k++] = response2int(byte);
  126 + j = 0;
  127 + continue;
  128 + }
  129 + byte[j++] = IP[i];
  130 + }
  131 + IP4_ADDR(addr,b[0],b[1],b[2],b[3]);
116 132 }
117 133  
  134 +/*
  135 + err_t netconn_write ( struct netconn * aNetConn, const void * aData, size_t aSize, u8_t aApiFlags );
  136 +
  137 + aNetConn : the netconn object the data is written to
  138 + aData : address of beginning of the data to write
  139 + aSize : the length of the data in bytes
  140 + aApiFlags : either of
  141 + NETCONN_NOCOPY if the data is stable for the time of the transmission (static data or heap)
  142 + NETCONN_COPY if the data is not stable for the time of the transmission (stack)
  143 +
  144 + With the netconn API, netconn_recv() returns a netbuf which may contain a chain of pbufs. You have no control over how much data will be returned in a single request, which may be more or less than you want to deal with. You can use various netbuf functions to copy data out of the buffer. If you need more data, you must call netconn_recv() again to get the next netbuf. If you received more data than you wanted, you will have to manage the extra data yourself.
  145 +
  146 +
  147 + u16_t netbuf_copy_partial ( struct netbuf * aNetBuf, void * aData, u16_t aLen, u16_t aOffset );
  148 +
  149 + Copy at most "aLen" bytes from the netbuf object to the destination location. The second alternative can start at an offset instead of 0.
  150 +
  151 + in aNetBuf : netbuf object the data is copied from
  152 + out aData : address of the memory where the data is copied to
  153 + in aLen : the size of the buffer, or the length of data to copy
  154 + in aOffset : an offset to start the with copy operation. In "netbuf_copy" this value is 0
  155 + return : total number of copied bytes, might be 0
  156 +
  157 + Contray to using "netbuf_data" you don't have to fumble around with chained buffers. You get all the data out of the buffer. As a drawback you have an extra overhead of memory usage.
  158 +*/
  159 +
118 160 const char* reqMethod2text(enum reqMethod method)
119 161 {
120 162 switch(method)
... ...
Project/applications/smartcities/include/httpClient.h
... ... @@ -14,9 +14,6 @@
14 14 #include "ch.h"
15 15 #include "globals.h"
16 16  
17   -#define DBG(fmt,...) if(1){printf("[SRV] "fmt"\r\n", ##__VA_ARGS__);}else{({});}
18   -#define DBG_WARNING(fmt,...) if(1){printf("[SRV_WARNING] "fmt"\r\n", ##__VA_ARGS__);}else{({});}
19   -
20 17 /*
21 18 * No fem servir DEFAULT_REMOTE_IP. Fem servir en canvi SERVER_IP, definida a globals.h
22 19 */
... ... @@ -50,6 +47,7 @@ const char* reqMethod2text(enum reqMethod method);
50 47 char* int2string(int num);
51 48 int numberofdigits(int number);
52 49 int response2int(char* chars);
  50 +void initialize_IP(struct ip_addr* addr,char* IP);
53 51  
54 52 #endif
55 53  
... ...