Marcelo Rebonatto / Mbed 2 deprecated PM_COPIA

Dependencies:   EthernetInterface mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Http_post.cpp Source File

Http_post.cpp

00001 /*
00002  * Http_post.cpp
00003  *
00004  *  Created on: 
00005  *      Author: 
00006  */
00007 #include "Http_post.h"
00008 
00009 extern "C" void mbed_reset();
00010 
00011 void HttpPost::HttpPost_Thread(void const *arg)
00012 {
00013     printf("HTTP POST Thread starting...\r\n");    
00014     
00015     //inicializar socket
00016    // TCPSocketConnection sock;
00017     
00018     CaptureMailbox& mbox = EventDetector::GetMailbox();
00019     
00020     osEvent evt;
00021     //printf("aqui\n");
00022     while(1)
00023     {
00024         TCPSocketConnection sock;
00025         //printf("Esperando Evento\n");
00026         evt = mbox.get();
00027 
00028         if(evt.status == osEventMail)
00029         {
00030             //printf("Recebido osEventMail...\n ");
00031             CaptureEvent* cap = (CaptureEvent*)evt.value.p;
00032             DoPost(sock,Settings::get_ServerUrl(),cap);    
00033             mbox.free(cap);
00034             //printf("Enviado e Liberado Mbox ...\n ");
00035             //sock.reset_address();
00036         }
00037         //Thread::yield();
00038     }
00039 }
00040 
00041 
00042 
00043 void HttpPost::DoPost(TCPSocketConnection sock, char *host, CaptureEvent* dados){
00044     char http_cmd[500]; 
00045     int escritos, r=-1, i;
00046     FILE *f;
00047     
00048     //Timer t;
00049     //t.start();
00050     
00051     //printf("HTTP Socket %s:%d\n", host, sock.get_port());
00052     //printf("Antes Connect\n");
00053     //rs= sock.connect(host, 80);   
00054     //printf("%d\n", rs);
00055     //printf("Depois Connect "); 
00056     
00057     //t.stop();
00058     //printf("HHTP: The time taken in connection was %d useconds\n", t.read_us());
00059            
00060     //http_cmd = (char *) malloc(500);
00061     memset(http_cmd, 0, 500);
00062     PreparePost( dados,http_cmd );
00063     //printf("Fuga\n");
00064     //printf("%d\n", strlen(http_cmd));
00065     
00066     //http_cmd = prepare_POST( dados );
00067     
00068     //printf("Tamanho comando %d\n", strlen(http_cmd));     
00069     //printf("Request %d\n [%s]\n", Settings::get_MaxTries(), http_cmd);     
00070 
00071     
00072     for(i=0; i < Settings::get_MaxTries(); i++){
00073         r = sock.connect(host, 80);        
00074         if (r < 0) {
00075             printf("Error: Unable to connect to (%s) on port (%d) Try %d\n", host, 80, i);
00076             Thread::wait(Settings::get_DelayTry());
00077         }
00078         else
00079             break;            
00080     }     
00081     if (r == 0){
00082         for(i=0; i < Settings::get_MaxTries(); i++){
00083             escritos = sock.send_all(http_cmd, strlen(http_cmd));        
00084             if(escritos != strlen(http_cmd)){
00085                 printf("Erro ao gravar no socket HTTP!! Escritos %d\t Tam %d Try %d\n", escritos, strlen(http_cmd), i);            
00086                 Thread::wait(Settings::get_DelayTry());
00087             }
00088             else 
00089                 break;
00090         }
00091         if (i != Settings::get_MaxTries() )
00092             Thread::wait(Settings::get_DelaySend());
00093         else{
00094             //printf("Reset\n");
00095             f = fopen(FILENAMERESET, "a");
00096             if (f == NULL)            
00097                 f = fopen(FILENAMERESET, "w");
00098             fprintf(f, "Reset - Connect\n");
00099             fclose(f);
00100                 
00101             mbed_reset();
00102         }            
00103             
00104     //Codigo para buscar o retorno do servidor HTTP
00105     /*
00106     printf("Vai pegar retorno\n");
00107     
00108     char buffer[300];
00109     int ret;
00110     while (true) {
00111         ret = sock.receive(buffer, sizeof(buffer)-1);
00112         if (ret <= 0)
00113             break;
00114         buffer[ret] = '\0';
00115         printf("Received %d chars from server:\n%s\n", ret, buffer);
00116     }
00117     */
00118     }
00119     else{
00120         printf("Reset\n");
00121         f = fopen(FILENAMERESET, "a");
00122         if (f == NULL)            
00123             f = fopen(FILENAMERESET, "w");
00124         fprintf(f, "Reset - Send\n");
00125         fclose(f);
00126             
00127         mbed_reset();
00128     }
00129     
00130     sock.close();  
00131 }
00132 
00133 void HttpPost::PreparePost(CaptureEvent* dados,char *strfinal){
00134     char str[400];
00135     char aux[NUMBER_OF_HARMONICS];
00136     int i;
00137 
00138     const char *header1 = "POST /capture.php HTTP/1.1\r\n";
00139                 //"Host: 192.168.1.26\r\n"
00140                 //"Content-Length: "
00141                 
00142     const char *header2 = "\r\n"
00143                 "Content-Type: application/x-www-form-urlencoded\r\n"
00144                 "\r\n";
00145     
00146     //str = (char *) malloc(450);
00147     //strfinal = (char *) malloc(450);
00148     memset(str,0,400);
00149     memset(strfinal,0,500);
00150             
00151     strcat(strfinal, header1);    
00152     strcat(strfinal, "Host: ");
00153     strcat(strfinal, Settings::get_ServerUrl() );
00154     strcat(strfinal, "\r\n");
00155     
00156     sprintf(aux,"TYPE=0%d",dados->get_Type());
00157     strcat(str, aux);
00158 
00159     sprintf(aux,"&OUTLET=%02d",dados->get_OutletNumber());
00160     strcat(str, aux);
00161     
00162     sprintf(aux,"&RFID=%s", dados->get_RFID());
00163     strcat(str,aux);
00164 
00165     sprintf(aux,"&OFFSET=%04d",dados->get_Offset());
00166     strcat(str,aux);
00167     
00168     float f = dados->get_Gain();
00169     sprintf(aux,"&GAIN=%08X", *(unsigned int*)&f);
00170     strcat(str,aux);
00171     
00172     f = dados->get_RMSValue();
00173     sprintf(aux,"&RMS=%08X",*(unsigned int*)&f);
00174     strcat(str,aux);
00175 
00176     f = dados->get_MeanValue();
00177     sprintf(aux,"&MV=%08X",*(unsigned int*)&f);
00178     strcat(str,aux);        
00179 
00180     strcat(str,"&SIN=");
00181     for(i=0;i<Settings::get_MaxHarmonics();i++)
00182     {
00183         char s[10];
00184         //According to RFC1738,RFC3986 the semicolon is a reserved character and must be encoded
00185         f = dados->get_SineValue(i);
00186         sprintf(s,"%08X",*(unsigned int*)&f);
00187         strcat(str,s);
00188         if (i < (Settings::get_MaxHarmonics() - 1))
00189             strcat(str, "%3B");
00190     }
00191 
00192     strcat(str,"&COS=");
00193     for(i=0;i<Settings::get_MaxHarmonics();i++)
00194     {
00195         char c[10];
00196         //According to RFC1738,RFC3986 the semicolon is a reserved character and must be encoded
00197         f = dados->get_CossineValue(i);
00198         sprintf(c,"%08X",*(unsigned int*)&f);
00199         strcat(str,c);
00200         if (i < (Settings::get_MaxHarmonics()-1))
00201             strcat(str, "%3B");
00202     }
00203     strcat(str,"\r\n");
00204     
00205     char len[5];
00206     sprintf(len,"%d",strlen(str));        
00207     
00208     strcat(strfinal, "Content-Length: ");
00209     strcat(strfinal, len);
00210     strcat(strfinal, header2);
00211     strcat(strfinal, str);    
00212     strcat(strfinal, "\r\n");
00213     
00214     //printf("Request=[%s]\n",strfinal);
00215     //printf("Tamanho STR %d\n", strlen(str));
00216     //printf("Tamanho STRFINAL %d\n", strlen(strfinal));       
00217  }