Commit 0a650948cf6f245c28ce6be38a15e3e90e04d0ca
1 parent
323b3949
--no commit message
Showing
4 changed files
with
138 additions
and
2 deletions
Project/applications/smartcities/example-code/httpClient/JSON_POST.pcapng
0 → 100644
No preview for this file type
Project/applications/smartcities/example-code/httpClient/post http.txt
0 → 100644
1 | +Hola ferran | |
2 | +t'explico lo del HTTP | |
3 | +básicament, es fer un POST a un servidor HTTP normal i corrent | |
4 | +la funcion a fer hauria d'acceptar com a arguments lo següent: | |
5 | + | |
6 | + char* data (string a posar a pincho al camp de dades, aqui va el JSON) | |
7 | + uint32_t length (la longitud del camp de dades) | |
8 | + char* URL (aqui va la URL a la que fer la petició POST) | |
9 | + uint32_t URL_length (longitud de la URL) | |
10 | + | |
11 | + retorna un uint8_t | |
12 | + | |
13 | +que fos el codi HTTP de resposta (200, 401 etc) | |
14 | +recorda que es un POST normal, excepte que la capçalera ha de tenir el | |
15 | + | |
16 | + Content-type a application/json | |
17 | + | |
18 | +et passo una captura del wireshark d'exemple | |
19 | +un sec que la generi | |
20 | +en la captura veuras que el servidor respon amb dades | |
21 | +es un script de prova que vai fer jo | |
22 | +Aqui estan les respostes del servidor | |
23 | +realment sols es necessari el codi | |
24 | +http://www.sentilo.io/xwiki/bin/view/APIDocs/Overview#HReply | |
25 | +http://imanolbarba.myftp.biz/upload/JSON_POST.pcapng | |
26 | +els 3 ultims paquets son caca | |
27 | +quan abans ho tinguis millor, recorda que lo altre que feu tu i la maria ha d'estar per l'1 d'abril | |
28 | +si aneu molt de cul avisam i ho faig jo, pero amb temps si pot ser xd | |
29 | +qualsevol cosa em dius | |
30 | +ah se me olvidava | |
31 | +si la connexio amb el servidor no ha estat possible | |
32 | +fes que retorni un numero especial i el fots en un #define | |
33 | +per saber com va el tema sockets en la placa, a la documentació t'explica com obrir sockets BSD amb el lwip | |
34 | +o mira una de les aplicacions d'exemple | |
35 | +en l'exemple fan servir la API del Netconn crec, no sockets BSD | |
36 | +fes servir allò que t'agradi més | |
37 | +amb els sockets BSD t'hi sentiràs més familiar per que son els sockets normals de linux que es veuen també a ARISO 2, pero si veus més clar l'altra manera o t'agrada més endevant | |
38 | +hi ha mes info al Developer manual a la documentació | |
39 | +Developer guide perdó | |
0 | 40 | \ No newline at end of file | ... | ... |
Project/applications/smartcities/httpClient.c
... | ... | @@ -44,7 +44,80 @@ GET /path/file.html HTTP/1.1 |
44 | 44 | Host: www.host1.com:80 |
45 | 45 | |
46 | 46 | [Content] |
47 | +----------- | |
48 | +POST:4 GET:3 PUT:3 DELETE:6 | |
49 | +espai +1 | |
50 | +255.255.255.255:15 o bé sizeof(*uri) | |
51 | +rn +2 | |
52 | +HTTP/1.1:8 | |
53 | ++2 | |
54 | +Host: :6 + sizeof(host) | |
47 | 55 | */ |
48 | 56 | |
49 | 57 | //const static char httpHeaders[] = "HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n"; |
50 | -const static char httpHeaders[] = "HTTP/1.1 OK\r\nContent-type: text/html\r\n\r\n"; | |
58 | +struct netconn* neocon; | |
59 | +struct ip_addr local_ip; | |
60 | +struct ip_addr remote_ip; | |
61 | +char* httpHeaders; | |
62 | +char* httpContent; | |
63 | + | |
64 | +int httpRequest(struct httpHeaders head_in, struct ip_addr remote_ip, u16_t remote_port, char* content, int content_size) | |
65 | +{ | |
66 | + // Check or default params | |
67 | + if (remote_ip == NULL || remote_ip == 0) remote_ip = DEFAULT_REMOTE_IP; | |
68 | + if (host == NULL) host = DEFAULT_REMOTE_IP; | |
69 | + if (remote_port == NULL || remote_port == 0) remote_port = DEFAULT_REMOTE_PORT; | |
70 | + if (uri == NULL || content == NULL) return 1; | |
71 | + | |
72 | + // Calculate header size | |
73 | + int header_size = sizeof(reqMethod2text(method)); | |
74 | + header_size += sizeof " "; | |
75 | + header_size += strlen(uri); | |
76 | + header_size += sizeof(\r\n); | |
77 | + header_size += sizeof("Host: "); | |
78 | + header_size += | |
79 | + | |
80 | + // Set connection | |
81 | + neocon = netconn_new(NETCONN_TCP); | |
82 | + local_ip.addr = 0;//getip | |
83 | + netconn_bind(neocon, IP_ADDR_ANY, 88); //88 is provisional local port. | |
84 | + netconn_connect(neocon, &remote_ip, remote_port); | |
85 | + | |
86 | + | |
87 | + | |
88 | +} | |
89 | + | |
90 | +*char reqMethod2text(enum reqMethod method) | |
91 | +{ | |
92 | + switch(method) | |
93 | + { | |
94 | + case post: | |
95 | + return "POST"; | |
96 | + case put: | |
97 | + return "PUT"; | |
98 | + case get: | |
99 | + return "GET"; | |
100 | + case delete: | |
101 | + return "DELETE"; | |
102 | + default: | |
103 | + return NULL; | |
104 | + } | |
105 | +} | |
106 | +/* | |
107 | + struct netconn* httpServerConnection; | |
108 | + httpServerConnection = netconn_new(NETCONN_TCP); | |
109 | + netconn_bind(httpServerConnection, IP_ADDR_ANY, cTcpPort); // bind, to a local IP/port | |
110 | + netconn_connect ( xNetConn, &remote_ip, cClientPort ); // connect, to a remote IP/port | |
111 | + | |
112 | + libwismart_GetCurrentIP(libwismart_ip_addr_t *adress, *netmask, *gateway); | |
113 | +*/ | |
114 | + | |
115 | + | |
116 | + | |
117 | + | |
118 | + | |
119 | + | |
120 | + | |
121 | + | |
122 | + | |
123 | + | ... | ... |
Project/applications/smartcities/include/httpClient.h
... | ... | @@ -16,10 +16,34 @@ |
16 | 16 | #define DBG(fmt,...) if(1){printf("[SRV] "fmt"\r\n", ##__VA_ARGS__);}else{({});} |
17 | 17 | #define DBG_WARNING(fmt,...) if(1){printf("[SRV_WARNING] "fmt"\r\n", ##__VA_ARGS__);}else{({});} |
18 | 18 | |
19 | -void httpServer_init(void); | |
19 | +#define DEFAULT_REMOTE_IP 192.168.1.1 | |
20 | + | |
21 | +/*void httpServer_init(void); | |
20 | 22 | void httpServer_start(void); |
21 | 23 | msg_t httpServer_threadFunc(void *arg); |
22 | 24 | void httpServer_serveClient(struct netconn* httpClientConnection); |
23 | 25 | void getLocalTime(uint32_t* hours, uint32_t* minutes, uint32_t* seconds); |
26 | +*/ | |
27 | + | |
28 | +int httpRequest(struct httpHeaders head_in, struct ip_addr remote_ip, u16_t remote_port, char* content, int content_size); | |
29 | + | |
30 | +typedef enum reqMethod | |
31 | +{ | |
32 | + post, | |
33 | + get, | |
34 | + put, | |
35 | + del //delete may be a special word. | |
36 | + | |
37 | +}reqMethod; | |
38 | + | |
39 | +typedef struct httpHeader | |
40 | +{ | |
41 | + enum reqMethod method, | |
42 | + char* uri; | |
43 | + int uri_size; | |
44 | + char* host; | |
45 | + int host_size; | |
46 | + | |
47 | +}httpHeader; | |
24 | 48 | |
25 | 49 | #endif |
26 | 50 | \ No newline at end of file | ... | ... |