Commit 152271e6d31b9aeddffb1d86b4f3a355d15b5a4d
1 parent
0a650948
--no commit message
Showing
3 changed files
with
68 additions
and
28 deletions
Project/applications/smartcities/example-code/httpClient/tcp-client.c
0 → 100644
1 | +/* | |
2 | +err_t netconn_connect ( struct netconn * aNetConn, ip_addr_t * aAddr, u16_t aPport ); | |
3 | + | |
4 | + in aNetConn : netconn structure received by netconn_new or netconn_accept. | |
5 | + in aAddr : the IP address of the remote server to connect to | |
6 | + in aPort : the port of the remote server to connect to | |
7 | +*/ | |
8 | +struct netconn *xNetConn = NULL; | |
9 | + | |
10 | +struct ip_addr local_ip; | |
11 | +struct ip_addr remote_ip; | |
12 | +int rc1, rc2; | |
13 | + | |
14 | +xNetConn = netconn_new ( NETCONN_TCP ); | |
15 | + | |
16 | +if ( xNetConn == NULL ) { | |
17 | + | |
18 | + /* No memory for new connection? */ | |
19 | + continue; | |
20 | +} | |
21 | + | |
22 | +local_ip.addr = <get IP of this device> | |
23 | + | |
24 | +rc1 = netconn_bind ( xNetConn, &local_ip, 0 ); | |
25 | + | |
26 | +remote_ip.addr = xRemoteIp; // static or by netconn_gethostbyname () | |
27 | +rc2 = netconn_connect ( xNetConn, &remote_ip, cClientPort ); | |
28 | + | |
29 | +if ( rc1 != ERR_OK || rc2 != ERR_OK ) | |
30 | +{ | |
31 | + | |
32 | + netconn_delete ( xNetConn ); | |
33 | + continue; | |
34 | +} | ... | ... |
Project/applications/smartcities/httpClient.c
... | ... | @@ -45,37 +45,36 @@ Host: www.host1.com:80 |
45 | 45 | |
46 | 46 | [Content] |
47 | 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) | |
55 | 48 | */ |
56 | 49 | |
57 | 50 | //const static char httpHeaders[] = "HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n"; |
58 | 51 | struct netconn* neocon; |
59 | 52 | struct ip_addr local_ip; |
60 | 53 | struct ip_addr remote_ip; |
61 | -char* httpHeaders; | |
62 | -char* httpContent; | |
54 | +char* head; | |
55 | +char* content; | |
63 | 56 | |
64 | 57 | int httpRequest(struct httpHeaders head_in, struct ip_addr remote_ip, u16_t remote_port, char* content, int content_size) |
65 | 58 | { |
66 | 59 | // Check or default params |
60 | + if (head_in.uri == NULL || content == NULL) return 1; | |
61 | + if (head_in.host == NULL) host = DEFAULT_REMOTE_IP; | |
67 | 62 | if (remote_ip == NULL || remote_ip == 0) remote_ip = DEFAULT_REMOTE_IP; |
68 | - if (host == NULL) host = DEFAULT_REMOTE_IP; | |
69 | 63 | if (remote_port == NULL || remote_port == 0) remote_port = DEFAULT_REMOTE_PORT; |
70 | - if (uri == NULL || content == NULL) return 1; | |
71 | 64 | |
72 | 65 | // 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 += | |
66 | + int head_size = strlen(reqMethod2text(method)); | |
67 | + head_size += sizeof " " + head_in.uri_size + sizeof "ENDL"; | |
68 | + head_size += sizeof "Host: " + head_in.host_size + sizeof "ENDL"; | |
69 | + head_size += sizeof CONTENT_TYPE_HEADER; | |
70 | + head_size += sizeof "Content-Length: " + numberofdigits(content_size); | |
71 | + | |
72 | + // Build header | |
73 | + head = (char *) malloc(head_size); | |
74 | + strcpy(head, reqMethod2text(method)); | |
75 | + strcat(head, " "); | |
76 | + strcat(head, head_in.uri); | |
77 | + /* ... */ | |
79 | 78 | |
80 | 79 | // Set connection |
81 | 80 | neocon = netconn_new(NETCONN_TCP); |
... | ... | @@ -87,7 +86,7 @@ int httpRequest(struct httpHeaders head_in, struct ip_addr remote_ip, u16_t remo |
87 | 86 | |
88 | 87 | } |
89 | 88 | |
90 | -*char reqMethod2text(enum reqMethod method) | |
89 | +char[] reqMethod2text(enum reqMethod method) | |
91 | 90 | { |
92 | 91 | switch(method) |
93 | 92 | { |
... | ... | @@ -103,6 +102,17 @@ int httpRequest(struct httpHeaders head_in, struct ip_addr remote_ip, u16_t remo |
103 | 102 | return NULL; |
104 | 103 | } |
105 | 104 | } |
105 | + | |
106 | +int numberofdigits(int number) // 0 has one digit. | |
107 | +{ | |
108 | + int digits = 0; | |
109 | + while (number) | |
110 | + { | |
111 | + number /= 10; | |
112 | + digits++; | |
113 | + } | |
114 | + return (digits == 0 ?) 1 : digits; | |
115 | +} | |
106 | 116 | /* |
107 | 117 | struct netconn* httpServerConnection; |
108 | 118 | httpServerConnection = netconn_new(NETCONN_TCP); |
... | ... | @@ -111,13 +121,3 @@ int httpRequest(struct httpHeaders head_in, struct ip_addr remote_ip, u16_t remo |
111 | 121 | |
112 | 122 | libwismart_GetCurrentIP(libwismart_ip_addr_t *adress, *netmask, *gateway); |
113 | 123 | */ |
114 | - | |
115 | - | |
116 | - | |
117 | - | |
118 | - | |
119 | - | |
120 | - | |
121 | - | |
122 | - | |
123 | - | ... | ... |
Project/applications/smartcities/include/httpClient.h
... | ... | @@ -17,6 +17,9 @@ |
17 | 17 | #define DBG_WARNING(fmt,...) if(1){printf("[SRV_WARNING] "fmt"\r\n", ##__VA_ARGS__);}else{({});} |
18 | 18 | |
19 | 19 | #define DEFAULT_REMOTE_IP 192.168.1.1 |
20 | +#define DEFAULT_REMOTE_PORT 80 | |
21 | +#define ENDL "\r\n" | |
22 | +#define CONTENT_TYPE_HEADER "Content-Type: application/json; charset=UTF-8" | |
20 | 23 | |
21 | 24 | /*void httpServer_init(void); |
22 | 25 | void httpServer_start(void); |
... | ... | @@ -26,6 +29,9 @@ void getLocalTime(uint32_t* hours, uint32_t* minutes, uint32_t* seconds); |
26 | 29 | */ |
27 | 30 | |
28 | 31 | int httpRequest(struct httpHeaders head_in, struct ip_addr remote_ip, u16_t remote_port, char* content, int content_size); |
32 | +char[] reqMethod2text(enum reqMethod method); | |
33 | +int numberofdigits(int number); | |
34 | + | |
29 | 35 | |
30 | 36 | typedef enum reqMethod |
31 | 37 | { | ... | ... |