PingPong
Dependencies: EthernetInterface_pm mbed-rtos mbed
Codes/Http_post.cpp
- Committer:
- rebonatto
- Date:
- 2014-03-27
- Revision:
- 1:b343d9cd769a
- Parent:
- 0:c8f056a185d0
File content as of revision 1:b343d9cd769a:
/* * Http_post.cpp * * Created on: 10/09/2013 * Author: Marcelo Trindade Rebonatto */ #include "Configuracoes.h" #include "Http_post.h" static int countCnt = 0; static int countSnd = 0; int HttpPost::getCountCnt() { return countCnt; }; int HttpPost::getCountSnd() { return countSnd; }; void HttpPost::HttpPost_Thread(void const *arg) { printf("HTTP POST Thread starting...\r\n"); /* //inicializar socket // TCPSocketConnection sock; CaptureMailbox& mbox = EventDetector::GetMailbox(); osEvent evt; //printf("aqui\n"); while(1) { TCPSocketConnection sock; //printf("Esperando Evento\n"); evt = mbox.get(); if(evt.status == osEventMail) { //printf("Recebido osEventMail...\n"); CaptureEvent* cap = (CaptureEvent*)evt.value.p; DoPost(sock,Settings::get_ServerUrl(),cap); mbox.free(cap); //sock.reset_address(); } } */ } void HttpPost::DoPost(TCPSocketConnection sock, char *host){ char http_cmd[500]; int escritos, r, i; //Timer t; //t.start(); //printf("HTTP Socket %s:%d\n", host, sock.get_port()); //printf("Antes Connect\n"); //printf("%d\n", rs); //printf("Depois Connect "); //t.stop(); //printf("HHTP: The time taken in connection was %d useconds\n", t.read_us()); //http_cmd = (char *) malloc(500); memset(http_cmd, 0, 500); PreparePost( http_cmd ); //printf("%d\n", strlen(http_cmd)); //http_cmd = prepare_POST( dados ); //printf("Tamanho comando %d\n", strlen(http_cmd)); //Timer t1; //t1.start(); //printf("Antes Send\n"); //printf("Apos Send\n"); //sock.send_all(http_cmd, strlen(http_cmd)); //t1.stop(); //printf("HHTP: The time taken in send was %d useconds\n", t1.read_us()); for(i=0; i < MAXTRIES; i++){ r = sock.connect(host, 80); if (r < 0) { if (i == 0) countCnt++; printf("Error %d Unable to connect to (%s) on port (%d) Try %d\n", ++countCnt, host, 80, i); //wait(1); } else break; } if (r == 0){ for(i=0; i < MAXTRIES; i++){ escritos = sock.send_all(http_cmd, strlen(http_cmd)); if(escritos != strlen(http_cmd)){ if (i == 0) countSnd++ ; printf("Erro %d ao gravar no socket HTTP!! Escritos %d\t Tam %d Try %d\n", countSnd, escritos, strlen(http_cmd), i); wait_ms(DELAYTRY); } else break; } wait_ms(DELAY); //Codigo para buscar o retorno do servidor HTTP /* char buffer[300]; int ret; while (true) { ret = sock.receive(buffer, sizeof(buffer)-1); if (ret <= 0) break; buffer[ret] = '\0'; printf("Received %d chars from server:\n%s\n", ret, buffer); } */ } sock.close(); } void HttpPost::PreparePost(char *strfinal){ char str[400]; char aux[12]; int i; const char *header1 = "POST /capture.php HTTP/1.1\r\n"; //"Host: 192.168.1.26\r\n" //"Content-Length: " const char *header2 = "\r\n" "Content-Type: application/x-www-form-urlencoded\r\n" "\r\n"; //str = (char *) malloc(450); //strfinal = (char *) malloc(450); memset(str,0,400); memset(strfinal,0,500); strcat(strfinal, header1); strcat(strfinal, "Host: "); strcat(strfinal, "192.168.1.26" ); strcat(strfinal, "\r\n"); i = 1; sprintf(aux,"TYPE=0%d",i); strcat(str, aux); i++; sprintf(aux,"&OUTLET=%02d",i); strcat(str, aux); sprintf(aux,"&RFID=%s", "ABCDEFGH"); strcat(str,aux); i++; sprintf(aux,"&OFFSET=%04d",i); strcat(str,aux); float f = i++; sprintf(aux,"&GAIN=%08X", *(unsigned int*)&f); strcat(str,aux); f = i++; sprintf(aux,"&RMS=%08X",*(unsigned int*)&f); strcat(str,aux); f = i++; sprintf(aux,"&MV=%08X",*(unsigned int*)&f); strcat(str,aux); strcat(str,"&SIN="); for(i=0;i<12;i++) { char s[10]; //According to RFC1738,RFC3986 the semicolon is a reserved character and must be encoded f += i; sprintf(s,"%08X",*(unsigned int*)&f); strcat(str,s); if (i < (12 - 1)) strcat(str, "%3B"); } strcat(str,"&COS="); for(i=0;i<12;i++) { char c[10]; //According to RFC1738,RFC3986 the semicolon is a reserved character and must be encoded f += i; sprintf(c,"%08X",*(unsigned int*)&f); strcat(str,c); if (i < (12-1)) strcat(str, "%3B"); } strcat(str,"\r\n"); char len[5]; sprintf(len,"%d",strlen(str)); strcat(strfinal, "Content-Length: "); strcat(strfinal, len); strcat(strfinal, header2); strcat(strfinal, str); strcat(strfinal, "\r\n"); //printf("Request=[%s]\n",strfinal); //printf("Tamanho STR %d\n", strlen(str)); //printf("Tamanho STRFINAL %d\n", strlen(strfinal)); }