From 152271e6d31b9aeddffb1d86b4f3a355d15b5a4d Mon Sep 17 00:00:00 2001 From: Ferràn Quer i Guerrero Date: Thu, 20 Mar 2014 10:31:08 +0000 Subject: [PATCH] --- Project/applications/smartcities/example-code/httpClient/tcp-client.c | 34 ++++++++++++++++++++++++++++++++++ Project/applications/smartcities/httpClient.c | 56 ++++++++++++++++++++++++++++---------------------------- Project/applications/smartcities/include/httpClient.h | 6 ++++++ 3 files changed, 68 insertions(+), 28 deletions(-) create mode 100644 Project/applications/smartcities/example-code/httpClient/tcp-client.c diff --git a/Project/applications/smartcities/example-code/httpClient/tcp-client.c b/Project/applications/smartcities/example-code/httpClient/tcp-client.c new file mode 100644 index 0000000..1a976e5 --- /dev/null +++ b/Project/applications/smartcities/example-code/httpClient/tcp-client.c @@ -0,0 +1,34 @@ +/* +err_t netconn_connect ( struct netconn * aNetConn, ip_addr_t * aAddr, u16_t aPport ); + + in aNetConn : netconn structure received by netconn_new or netconn_accept. + in aAddr : the IP address of the remote server to connect to + in aPort : the port of the remote server to connect to +*/ +struct netconn *xNetConn = NULL; + +struct ip_addr local_ip; +struct ip_addr remote_ip; +int rc1, rc2; + +xNetConn = netconn_new ( NETCONN_TCP ); + +if ( xNetConn == NULL ) { + + /* No memory for new connection? */ + continue; +} + +local_ip.addr = + +rc1 = netconn_bind ( xNetConn, &local_ip, 0 ); + +remote_ip.addr = xRemoteIp; // static or by netconn_gethostbyname () +rc2 = netconn_connect ( xNetConn, &remote_ip, cClientPort ); + +if ( rc1 != ERR_OK || rc2 != ERR_OK ) +{ + + netconn_delete ( xNetConn ); + continue; +} diff --git a/Project/applications/smartcities/httpClient.c b/Project/applications/smartcities/httpClient.c index c5f24f7..b495936 100644 --- a/Project/applications/smartcities/httpClient.c +++ b/Project/applications/smartcities/httpClient.c @@ -45,37 +45,36 @@ Host: www.host1.com:80 [Content] ----------- -POST:4 GET:3 PUT:3 DELETE:6 -espai +1 -255.255.255.255:15 o bé sizeof(*uri) -rn +2 -HTTP/1.1:8 -+2 -Host: :6 + sizeof(host) */ //const static char httpHeaders[] = "HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n"; struct netconn* neocon; struct ip_addr local_ip; struct ip_addr remote_ip; -char* httpHeaders; -char* httpContent; +char* head; +char* content; int httpRequest(struct httpHeaders head_in, struct ip_addr remote_ip, u16_t remote_port, char* content, int content_size) { // Check or default params + if (head_in.uri == NULL || content == NULL) return 1; + if (head_in.host == NULL) host = DEFAULT_REMOTE_IP; if (remote_ip == NULL || remote_ip == 0) remote_ip = DEFAULT_REMOTE_IP; - if (host == NULL) host = DEFAULT_REMOTE_IP; if (remote_port == NULL || remote_port == 0) remote_port = DEFAULT_REMOTE_PORT; - if (uri == NULL || content == NULL) return 1; // Calculate header size - int header_size = sizeof(reqMethod2text(method)); - header_size += sizeof " "; - header_size += strlen(uri); - header_size += sizeof(\r\n); - header_size += sizeof("Host: "); - header_size += + int head_size = strlen(reqMethod2text(method)); + head_size += sizeof " " + head_in.uri_size + sizeof "ENDL"; + head_size += sizeof "Host: " + head_in.host_size + sizeof "ENDL"; + head_size += sizeof CONTENT_TYPE_HEADER; + head_size += sizeof "Content-Length: " + numberofdigits(content_size); + + // Build header + head = (char *) malloc(head_size); + strcpy(head, reqMethod2text(method)); + strcat(head, " "); + strcat(head, head_in.uri); + /* ... */ // Set connection neocon = netconn_new(NETCONN_TCP); @@ -87,7 +86,7 @@ int httpRequest(struct httpHeaders head_in, struct ip_addr remote_ip, u16_t remo } -*char reqMethod2text(enum reqMethod method) +char[] reqMethod2text(enum reqMethod method) { switch(method) { @@ -103,6 +102,17 @@ int httpRequest(struct httpHeaders head_in, struct ip_addr remote_ip, u16_t remo return NULL; } } + +int numberofdigits(int number) // 0 has one digit. +{ + int digits = 0; + while (number) + { + number /= 10; + digits++; + } + return (digits == 0 ?) 1 : digits; +} /* struct netconn* httpServerConnection; httpServerConnection = netconn_new(NETCONN_TCP); @@ -111,13 +121,3 @@ int httpRequest(struct httpHeaders head_in, struct ip_addr remote_ip, u16_t remo libwismart_GetCurrentIP(libwismart_ip_addr_t *adress, *netmask, *gateway); */ - - - - - - - - - - diff --git a/Project/applications/smartcities/include/httpClient.h b/Project/applications/smartcities/include/httpClient.h index 5da7dda..cdc5fa7 100644 --- a/Project/applications/smartcities/include/httpClient.h +++ b/Project/applications/smartcities/include/httpClient.h @@ -17,6 +17,9 @@ #define DBG_WARNING(fmt,...) if(1){printf("[SRV_WARNING] "fmt"\r\n", ##__VA_ARGS__);}else{({});} #define DEFAULT_REMOTE_IP 192.168.1.1 +#define DEFAULT_REMOTE_PORT 80 +#define ENDL "\r\n" +#define CONTENT_TYPE_HEADER "Content-Type: application/json; charset=UTF-8" /*void httpServer_init(void); void httpServer_start(void); @@ -26,6 +29,9 @@ void getLocalTime(uint32_t* hours, uint32_t* minutes, uint32_t* seconds); */ int httpRequest(struct httpHeaders head_in, struct ip_addr remote_ip, u16_t remote_port, char* content, int content_size); +char[] reqMethod2text(enum reqMethod method); +int numberofdigits(int number); + typedef enum reqMethod { -- libgit2 0.22.2