// Some common utilities needed for IP and web applications // Author: Guido Socher // Copyright: GPL V2 // // 2010-05-20 #include "EtherCard.h" void EtherCard::copyIp (uint8_t *dst, const uint8_t *src) { memcpy(dst, src, IP_LEN); } void EtherCard::copyMac (uint8_t *dst, const uint8_t *src) { memcpy(dst, src, ETH_LEN); } void EtherCard::printIp (const char* msg, const uint8_t *buf) { Serial.print(msg); EtherCard::printIp(buf); Serial.println(); } void EtherCard::printIp (const __FlashStringHelper *ifsh, const uint8_t *buf) { Serial.print(ifsh); EtherCard::printIp(buf); Serial.println(); } void EtherCard::printIp (const uint8_t *buf) { for (uint8_t i = 0; i < IP_LEN; ++i) { Serial.print( buf[i], DEC ); if (i < 3) Serial.print('.'); } } // search for a string of the form key=value in // a string that looks like q?xyz=abc&uvw=defgh HTTP/1.1\r\n // // The returned value is stored in strbuf. You must allocate // enough storage for strbuf, maxlen is the size of strbuf. // I.e the value it is declated with: strbuf[5]-> maxlen=5 uint8_t EtherCard::findKeyVal (const char *str,char *strbuf, uint8_t maxlen,const char *key) { byte found = false; uint8_t i=0; const char *kp; kp=key; while(*str && *str!=' ' && *str!='\n' && !found) { if (*str == *kp) { kp++; if (*kp == '\0') { str++; kp=key; if (*str == '=') { found = true; } } } else { kp=key; } str++; } if (found) { // copy the value to a buffer and terminate it with '\0' while(*str && *str!=' ' && *str!='\n' && *str!='&' && i9) { hstr[1]=(c & 0xf) - 10 + 'a'; } c=(c>>4)&0xf; hstr[0]=c+'0'; if (c > 9) { hstr[0]=c - 10 + 'a'; } hstr[2]='\0'; } // there must be enough space in urlbuf. In the worst case that is // 3 times the length of str void EtherCard::urlEncode (char *str,char *urlbuf) { char c; while ((c = *str) != 0) { if (c == ' '||isalnum(c)) { if (c == ' ') { c = '+'; } *urlbuf=c; str++; urlbuf++; continue; } *urlbuf='%'; urlbuf++; int2h(c,urlbuf); urlbuf++; urlbuf++; str++; } *urlbuf='\0'; } // parse a string and extract the IP to bytestr uint8_t EtherCard::parseIp (uint8_t *bytestr,char *str) { char *sptr; uint8_t i=0; sptr=NULL; while(i