collapse

* Posts Recentes

Arame de Estendal por almamater
[Ontem às 16:16]


O que é isto ? por SerraCabo
[12 de Abril de 2024, 14:20]


Amplificador - Rockboard HA 1 In-Ear por almamater
[11 de Abril de 2024, 20:46]


Emulador NES em ESP32 por dropes
[10 de Abril de 2024, 15:30]


Meu novo robô por josecarlos
[29 de Março de 2024, 18:30]


Bateria - Portátil por almamater
[25 de Março de 2024, 22:14]


Escolher Osciloscópio por jm_araujo
[06 de Fevereiro de 2024, 23:07]


TP4056 - Dúvida por dropes
[31 de Janeiro de 2024, 14:13]


Leitura de dados por Porta Serie por jm_araujo
[22 de Janeiro de 2024, 14:00]


Distancia Cabo por jm_araujo
[08 de Janeiro de 2024, 16:30]

Autor Tópico: alguem sabe explica o codigo abaixo ethernet  (Lida 2925 vezes)

0 Membros e 1 Visitante estão a ver este tópico.

Offline clauiocbamt

  • Mini Robot
  • *
  • Mensagens: 18
alguem sabe explica o codigo abaixo ethernet
« em: 08 de Julho de 2009, 17:19 »
olá pessoal

pessoal é o seguinte, estava tentando entender o funcionamneto do codigo abaixo porem sou sincero em dizer pra voces que não consequi entender totalmente esse codigo , tem alguem aqui no forum que possa me explica esse codigo de maneira detalhada ou seja linha a linha ......... apartir da função void loop




Código: [Seleccione]
#include "Ethernet.h"

// change MAC and IP address if necessary
static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24};
static uint8_t myip[4] = {192,168,0,15};
static char baseurl[]="http://192.168.0.15/";
static uint16_t mywwwport =80;

#define BUFFER_SIZE 500
static uint8_t buf[BUFFER_SIZE+1];
#define STR_BUFFER_SIZE 22
static char strbuf[STR_BUFFER_SIZE+1];

Ethernet es = Ethernet();

// prepare the webpage by writing the data to the tcp send buffer
uint16_t print_webpage(uint8_t *buf);
int8_t analyse_cmd(char *str);
void setup(){
  
    /*initialize enc28j60*/
    es.E_enc28j60Init(mymac);
    es.E_enc28j60clkout(2); // change clkout from 6.25MHz to 12.5MHz
    delay(10);
        
    /* Magjack leds configuration, see enc28j60 datasheet, page 11 */
    // LEDA=greed LEDB=yellow

    // 0x880 is PHLCON LEDB=on, LEDA=on
    // enc28j60PhyWrite(PHLCON,0b0000 1000 1000 00 00);
    es.E_enc28j60PhyWrite(PHLCON,0x880);
    delay(500);
    
    // 0x990 is PHLCON LEDB=off, LEDA=off
    // enc28j60PhyWrite(PHLCON,0b0000 1001 1001 00 00);
    es.E_enc28j60PhyWrite(PHLCON,0x990);
    delay(500);

    // 0x880 is PHLCON LEDB=on, LEDA=on
    // enc28j60PhyWrite(PHLCON,0b0000 1000 1000 00 00);
    es.E_enc28j60PhyWrite(PHLCON,0x880);
    delay(500);

    // 0x990 is PHLCON LEDB=off, LEDA=off
    // enc28j60PhyWrite(PHLCON,0b0000 1001 1001 00 00);
    es.E_enc28j60PhyWrite(PHLCON,0x990);
    delay(500);

    // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit
    // enc28j60PhyWrite(PHLCON,0b0000 0100 0111 01 10);
    es.E_enc28j60PhyWrite(PHLCON,0x476);
    delay(100);
        
    //init the ethernet/ip layer:
    es.E_init_ip_arp_udp_tcp(mymac,myip,80);
}

void loop(){
    uint16_t plen, dat_p;
    int8_t cmd;
    
    plen = es.E_enc28j60PacketReceive(BUFFER_SIZE, buf);
    // plen will ne unequal to zero if there is a valid packet
    if(plen!=0){
// arp is broadcast if unknown but a host may also verify
// the mac address by sending it to a unicast address.
if(es.E_eth_type_is_arp_and_my_ip(buf,plen)){
   es.E_make_arp_answer_from_request(buf);
   return;
}

// check if ip packets are for us:
if(es.E_eth_type_is_ip_and_my_ip(buf,plen)==0){
   return;
}
    
if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V
  && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){
   es.E_make_echo_reply_from_request(buf,plen);
   return;
}
    
// tcp port www start, compare only the lower byte
if (buf[IP_PROTO_P]==IP_PROTO_TCP_V
   && buf[TCP_DST_PORT_H_P]== 0
   && buf[TCP_DST_PORT_L_P]== mywwwport){
   if (buf[TCP_FLAGS_P] & TCP_FLAGS_SYN_V){
// make_tcp_synack_from_syn does already send the syn,ack
es.E_make_tcp_synack_from_syn(buf);
return;    
   }
   if (buf[TCP_FLAGS_P] & TCP_FLAGS_ACK_V){
es.E_init_len_info(buf); // init some data structures
dat_p=es.E_get_tcp_data_pointer();
if (dat_p==0){ // we can possibly have no data, just ack:
   if (buf[TCP_FLAGS_P] & TCP_FLAGS_FIN_V){
es.E_make_tcp_ack_from_any(buf);
   }
   return;
}
if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0){
   // head, post and other methods for possible status codes
   // [url=http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html]http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html[/url]
   plen=es.E_fill_tcp_data_p(buf, 0, PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<h1>200 OK</h1>"));
   goto SENDTCP;
}
if (strncmp("/ ",(char *)&(buf[dat_p+4]),2)==0){
   plen=print_webpage(buf);
   goto SENDTCP;
}
cmd=analyse_cmd((char *)&(buf[dat_p+5]));
if (cmd==1){
   plen=print_webpage(buf);
}
// send ack for http get
SENDTCP:  es.E_make_tcp_ack_from_any(buf);
// send data
es.E_make_tcp_ack_with_data(buf,plen);
   }
}
    }
}

// The returned value is stored in the global var strbuf
uint8_t find_key_val(char *str,char *key)
{
    uint8_t found=0;
    uint8_t i=0;
    char *kp;
    kp=key;
    while(*str &&  *str!=' ' && found==0){
if (*str == *kp){
   kp++;
   if (*kp == '\0'){
str++;
kp=key;
if (*str == '='){
   found=1;
}
   }
}else{
   kp=key;
}
str++;
    }
    if (found==1){
// copy the value to a buffer and terminate it with '\0'
while(*str &&  *str!=' ' && *str!='&' && i<STR_BUFFER_SIZE){
   strbuf[i]=*str;
   i++;
   str++;
}
strbuf[i]='\0';
    }
    return(found);
}

int8_t analyse_cmd(char *str)
{
    int8_t r=-1;
    
    if (find_key_val(str,"cmd")){
if (*strbuf < 0x3a && *strbuf > 0x2f){
   // is a ASCII number, return it
   r=(*strbuf-0x30);
}
    }
    return r;
}


uint16_t print_webpage(uint8_t *buf)
{
    uint16_t plen;
    plen=es.E_fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"));
    plen=es.E_fill_tcp_data_p(buf,plen,PSTR("<center><p><h1>Welcome to Arduino Ethernet Shield V1.0</h1></p> "));
    plen=es.E_fill_tcp_data_p(buf,plen,PSTR("<hr><br> <h2><font color=\"blue\">-- Your Arduino is online now  -- "));
    plen=es.E_fill_tcp_data_p(buf,plen,PSTR("<br> Control digital outputs "));
    plen=es.E_fill_tcp_data_p(buf,plen,PSTR("<br> Read digital analog inputs HERE "));
    plen=es.E_fill_tcp_data_p(buf,plen,PSTR("  <br></font></h2> ") );
    plen=es.E_fill_tcp_data_p(buf,plen,PSTR("</center><hr> <a href=\"http://www.flamingoeda.com\">[url=http://www.flamingoeda.com]www.flamingoeda.com[/url]<a>"));
    
    return(plen);
 }

« Última modificação: 08 de Julho de 2009, 19:53 por TigPT »

Offline TigPT

  • Administrator
  • Mini Robot
  • *****
  • Mensagens: 5.372
    • Tiago Rodrigues
Re:alguem sabe explica o codigo abaixo ethernet
« Responder #1 em: 08 de Julho de 2009, 19:55 »
Esse código cria um servidor web para no arduino e mostra páginas quando se entra em contacto com ele... no código está discreto o código HTML que ele vai apresentar, e tem muita coisa que faz parte da inicialização dos ics no shield ethernet, assim como de protocolo tcp.

Não é algo que se explique exaustivamente linha a linha, mas se disseres o que estás a tentar fazer e qual a tua dúvida, talvez possamos ajudar!

Offline clauiocbamt

  • Mini Robot
  • *
  • Mensagens: 18
Re:alguem sabe explica o codigo abaixo ethernet
« Responder #2 em: 08 de Julho de 2009, 21:45 »
quanto a isso TigPT eu até coloquei o exemplo para funciona so que não entendi os detalhes de funcionamento .....o que mais fiquei perdido foi com aquela função procura string e cmd....sendo que o cmd entendi não muito claro como funciona mais a função uint8_t find_key_val(char *str,char *key) me deixou totalmente sem noção...tem como explica essas 2 funçoes ai pra mim,
por exemplo essa parte aqui de onde saiu essas numeros  em hexa .......ou codigo ASCII

Código: [Seleccione]
if (find_key_val(str,"cmd")){
if (*strbuf < 0x3a && *strbuf > 0x2f){
  // is a ASCII number, return it
  r=(*strbuf-0x30);
}





uint8_t find_key_val(char *str,char *key)
{
    uint8_t found=0;
    uint8_t i=0;
    char *kp;
    kp=key;
    while(*str &&  *str!=' ' && found==0){
if (*str == *kp){
  kp++;
  if (*kp == '\0'){
str++;
kp=key;
if (*str == '='){
  found=1;
}
  }
}else{
  kp=key;
}
str++;
    }
    if (found==1){
// copy the value to a buffer and terminate it with '\0'
while(*str &&  *str!=' ' && *str!='&' && i<STR_BUFFER_SIZE){
  strbuf[i]=*str;
  i++;
  str++;
}
strbuf[i]='\0';
    }
    return(found);
}

int8_t analyse_cmd(char *str)
{
    int8_t r=-1;
    
    if (find_key_val(str,"cmd")){
if (*strbuf < 0x3a && *strbuf > 0x2f){
  // is a ASCII number, return it
  r=(*strbuf-0x30);
}
    }
    return r;
}

« Última modificação: 08 de Julho de 2009, 21:52 por TigPT »

Offline TigPT

  • Administrator
  • Mini Robot
  • *****
  • Mensagens: 5.372
    • Tiago Rodrigues
Re:alguem sabe explica o codigo abaixo ethernet
« Responder #3 em: 09 de Julho de 2009, 12:18 »
Ao que me parece a função procura se existe o texto "cmd" no url que foi chamado ao server e se for um número ele retorna esse int directamente. Para ver se ele é um int, ele testa se está no intervalo dos caracteres de números na tabela de ASCII e dai os hexas,

0x2f = '/'
0x3a = ':'

no meio destes caracteres estão os: 0x30 que é o '0' até ao 0x39 que é o '9'.