Added DNS resolver to [19_m1284p_WIZNET_blynk],
slightly optimized code. Tested on M644p - OK.
This commit is contained in:
@@ -18,10 +18,11 @@
|
|||||||
|
|
||||||
#include "blynk.h"
|
#include "blynk.h"
|
||||||
#include "blynkDependency.h"
|
#include "blynkDependency.h"
|
||||||
|
#include "../globals.h"
|
||||||
|
|
||||||
//#include "common.h" // When the project has no "common.h" file, this line have to commented out.
|
//#include "common.h" // When the project has no "common.h" file, this line have to commented out.
|
||||||
#ifndef DATA_BUF_SIZE
|
#ifndef BLYNK_DATA_BUF_SIZE
|
||||||
#define DATA_BUF_SIZE 2048
|
#define BLYNK_DATA_BUF_SIZE 2048
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
uint8_t blynk_connect(void);
|
uint8_t blynk_connect(void);
|
||||||
@@ -106,7 +107,7 @@ void blynk_run(void)
|
|||||||
#ifdef BLYNK_DEBUG
|
#ifdef BLYNK_DEBUG
|
||||||
getSn_DIPR(s, destip);
|
getSn_DIPR(s, destip);
|
||||||
destport = getSn_DPORT(s);
|
destport = getSn_DPORT(s);
|
||||||
printf("Blynk[%d] : Connected - %d.%d.%d.%d:%d\r\n",s, destip[0], destip[1], destip[2], destip[3], destport);
|
PRINTF("Blynk[%d] : Connected - %d.%d.%d.%d:%d\r\n",s, destip[0], destip[1], destip[2], destip[3], destport);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,7 +118,7 @@ void blynk_run(void)
|
|||||||
{
|
{
|
||||||
blynk_connected = true;
|
blynk_connected = true;
|
||||||
#ifdef BLYNK_DEBUG
|
#ifdef BLYNK_DEBUG
|
||||||
printf("Blynk[%d] : Auth connection complete\r\n", s);
|
PRINTF("Blynk[%d] : Auth connection complete\r\n", s);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -128,9 +129,9 @@ void blynk_run(void)
|
|||||||
|
|
||||||
if (t - lastActivityIn > (1000UL * BLYNK_HEARTBEAT + BLYNK_TIMEOUT_MS*3)) {
|
if (t - lastActivityIn > (1000UL * BLYNK_HEARTBEAT + BLYNK_TIMEOUT_MS*3)) {
|
||||||
#ifdef BLYNK_DEBUG
|
#ifdef BLYNK_DEBUG
|
||||||
printf("Heartbeat timeout (last in: %lu)\r\n", lastActivityIn);
|
PRINTF("Heartbeat timeout (last in: %lu)\r\n", lastActivityIn);
|
||||||
#else
|
#else
|
||||||
printf("Heartbeat timeout\r\n");
|
PRINTF("Heartbeat timeout\r\n");
|
||||||
#endif
|
#endif
|
||||||
blynk_connected = false;
|
blynk_connected = false;
|
||||||
blynk_connection_available = false;
|
blynk_connection_available = false;
|
||||||
@@ -142,7 +143,7 @@ void blynk_run(void)
|
|||||||
{
|
{
|
||||||
// Send ping if we didn't both send and receive something for BLYNK_HEARTBEAT seconds
|
// Send ping if we didn't both send and receive something for BLYNK_HEARTBEAT seconds
|
||||||
#ifdef BLYNK_DEBUG
|
#ifdef BLYNK_DEBUG
|
||||||
printf("Heartbeat\r\n");
|
PRINTF("Heartbeat\r\n");
|
||||||
#endif
|
#endif
|
||||||
sendCmd(BLYNK_CMD_PING, 0, NULL, 0, NULL, 0);
|
sendCmd(BLYNK_CMD_PING, 0, NULL, 0, NULL, 0);
|
||||||
lastHeartbeat = t;
|
lastHeartbeat = t;
|
||||||
@@ -151,14 +152,14 @@ void blynk_run(void)
|
|||||||
|
|
||||||
case SOCK_CLOSE_WAIT:
|
case SOCK_CLOSE_WAIT:
|
||||||
#ifdef BLYNK_DEBUG
|
#ifdef BLYNK_DEBUG
|
||||||
printf("Blynk[%d] : ClOSE WAIT\r\n", s); // if a peer requests to close the current connection
|
PRINTF("Blynk[%d] : ClOSE WAIT\r\n", s); // if a peer requests to close the current connection
|
||||||
#endif
|
#endif
|
||||||
disconnect(s);
|
disconnect(s);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SOCK_CLOSED:
|
case SOCK_CLOSED:
|
||||||
#ifdef BLYNK_DEBUG
|
#ifdef BLYNK_DEBUG
|
||||||
//printf("> Blynk[%d] : CLOSED\r\n", s);
|
//PRINTF("> Blynk[%d] : CLOSED\r\n", s);
|
||||||
#endif
|
#endif
|
||||||
blynk_connected = false;
|
blynk_connected = false;
|
||||||
blynk_connection_available = false;
|
blynk_connection_available = false;
|
||||||
@@ -166,15 +167,15 @@ void blynk_run(void)
|
|||||||
if(socket(s, Sn_MR_TCP, blynkclient_port++, 0x00) == s) /* Reinitialize the socket */
|
if(socket(s, Sn_MR_TCP, blynkclient_port++, 0x00) == s) /* Reinitialize the socket */
|
||||||
{
|
{
|
||||||
#ifdef BLYNK_DEBUG
|
#ifdef BLYNK_DEBUG
|
||||||
printf("Blynk[%d] : SOCKET OPEN\r\n", s);
|
PRINTF("Blynk[%d] : SOCKET OPEN\r\n", s);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SOCK_INIT:
|
case SOCK_INIT:
|
||||||
#ifdef BLYNK_DEBUG
|
#ifdef BLYNK_DEBUG
|
||||||
printf("Blynk[%d] : Connecting to ", s);
|
PRINTF("Blynk[%d] : Connecting to ", s);
|
||||||
printf("%d.%d.%d.%d:%d\r\n", server_ip[0], server_ip[1], server_ip[2], server_ip[3], server_port);
|
PRINTF("%d.%d.%d.%d:%d\r\n", server_ip[0], server_ip[1], server_ip[2], server_ip[3], server_port);
|
||||||
#endif
|
#endif
|
||||||
connect(s, server_ip, server_port);
|
connect(s, server_ip, server_port);
|
||||||
break;
|
break;
|
||||||
@@ -233,15 +234,15 @@ uint8_t blynk_connect(void)
|
|||||||
{
|
{
|
||||||
if (BLYNK_TIMEOUT == hdr.length)
|
if (BLYNK_TIMEOUT == hdr.length)
|
||||||
{
|
{
|
||||||
printf("Timeout\r\n");
|
PRINTF("Timeout\r\n");
|
||||||
}
|
}
|
||||||
else if (BLYNK_INVALID_TOKEN == hdr.length)
|
else if (BLYNK_INVALID_TOKEN == hdr.length)
|
||||||
{
|
{
|
||||||
printf("Invalid auth token\r\n");
|
PRINTF("Invalid auth token\r\n");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("Connect failed (code: %d)\r\n", hdr.length);
|
PRINTF("Connect failed (code: %d)\r\n", hdr.length);
|
||||||
|
|
||||||
// Send some invalid headers to server for disconnection
|
// Send some invalid headers to server for disconnection
|
||||||
hdr.type = 255;
|
hdr.type = 255;
|
||||||
@@ -272,9 +273,9 @@ uint8_t blynk_connect(void)
|
|||||||
deltaCmd = 1000;
|
deltaCmd = 1000;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
printf("Ready!\r\n");
|
PRINTF("Ready!\r\n");
|
||||||
#ifdef BLYNK_DEBUG
|
#ifdef BLYNK_DEBUG
|
||||||
printf("Roundtrip: %ldms\r\n", lastActivityIn-t);
|
PRINTF("Roundtrip: %ldms\r\n", lastActivityIn-t);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -311,27 +312,27 @@ void processInput(void)
|
|||||||
{
|
{
|
||||||
if (hdr.length > BLYNK_MAX_READBYTES)
|
if (hdr.length > BLYNK_MAX_READBYTES)
|
||||||
{
|
{
|
||||||
printf("Packet size (%u) > max allowed (%u)\r\n", hdr.length, BLYNK_MAX_READBYTES);
|
PRINTF("Packet size (%u) > max allowed (%u)\r\n", hdr.length, BLYNK_MAX_READBYTES);
|
||||||
disconnect(s);
|
disconnect(s);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//printf("hdr.length = %d\r\n", hdr.length);
|
//PRINTF("hdr.length = %d\r\n", hdr.length);
|
||||||
if (hdr.length != recv(s, msgbuf, hdr.length))
|
if (hdr.length != recv(s, msgbuf, hdr.length))
|
||||||
{
|
{
|
||||||
printf("Can't read body\r\n");
|
PRINTF("Can't read body\r\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
msgbuf[hdr.length] = '\0'; // Add 1 to zero-terminate
|
msgbuf[hdr.length] = '\0'; // Add 1 to zero-terminate
|
||||||
|
|
||||||
#ifdef BLYNK_DEBUG
|
#ifdef BLYNK_DEBUG
|
||||||
printf(">");
|
PRINTF(">");
|
||||||
for(i = 0; i < hdr.length; i++)
|
for(i = 0; i < hdr.length; i++)
|
||||||
{
|
{
|
||||||
if(msgbuf[i] != '\0') printf("%c", msgbuf[i]);
|
if(msgbuf[i] != '\0') PRINTF("%c", msgbuf[i]);
|
||||||
else printf(" ");
|
else PRINTF(" ");
|
||||||
}
|
}
|
||||||
printf("\r\n");
|
PRINTF("\r\n");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
currentMsgId = hdr.msg_id;
|
currentMsgId = hdr.msg_id;
|
||||||
@@ -340,7 +341,7 @@ void processInput(void)
|
|||||||
} break;
|
} break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
printf("Invalid header type: %d\r\n", hdr.type);
|
PRINTF("Invalid header type: %d\r\n", hdr.type);
|
||||||
disconnect(s);
|
disconnect(s);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -423,22 +424,22 @@ void processCmd(uint8_t * buff, size_t len)
|
|||||||
|
|
||||||
if(!strcmp(cmd, "dr")) // digital pin read
|
if(!strcmp(cmd, "dr")) // digital pin read
|
||||||
{
|
{
|
||||||
rsp_len = sprintf((char *)rsp_mem, "dw %d %d ", pin, digitalRead(pin));
|
rsp_len = SPRINTF((char *)rsp_mem, "dw %d %d ", pin, digitalRead(pin));
|
||||||
replacetonull(rsp_mem, ' ');
|
replacetonull(rsp_mem, ' ');
|
||||||
sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len, NULL, 0);
|
sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len, NULL, 0);
|
||||||
}
|
}
|
||||||
else if(!strcmp(cmd, "ar")) // analog pin read
|
else if(!strcmp(cmd, "ar")) // analog pin read
|
||||||
{
|
{
|
||||||
rsp_len = sprintf((char *)rsp_mem, "aw %d %d ", pin, analogRead(pin));
|
rsp_len = SPRINTF((char *)rsp_mem, "aw %d %d ", pin, analogRead(pin));
|
||||||
replacetonull(rsp_mem, ' ');
|
replacetonull(rsp_mem, ' ');
|
||||||
sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len, NULL, 0);
|
sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len, NULL, 0);
|
||||||
}
|
}
|
||||||
else if(!strcmp(cmd, "vr")) // virtual pin read
|
else if(!strcmp(cmd, "vr")) // virtual pin read
|
||||||
{
|
{
|
||||||
#ifdef BLYNK_DEBUG
|
#ifdef BLYNK_DEBUG
|
||||||
printf("vr command: Not fully supported yet\r\n");
|
PRINTF("vr command: Not fully supported yet\r\n");
|
||||||
#endif
|
#endif
|
||||||
rsp_len = sprintf((char *)rsp_mem, "vr %d %d ", pin, virtualRead(pin));
|
rsp_len = SPRINTF((char *)rsp_mem, "vr %d %d ", pin, virtualRead(pin));
|
||||||
replacetonull(rsp_mem, ' ');
|
replacetonull(rsp_mem, ' ');
|
||||||
sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len, NULL, 0);
|
sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len, NULL, 0);
|
||||||
|
|
||||||
@@ -457,14 +458,14 @@ void processCmd(uint8_t * buff, size_t len)
|
|||||||
if(!strcmp(cmd, "vw")) // virtual pin write
|
if(!strcmp(cmd, "vw")) // virtual pin write
|
||||||
{
|
{
|
||||||
#ifdef BLYNK_DEBUG
|
#ifdef BLYNK_DEBUG
|
||||||
printf("vw command: Not fully supported yet\r\n");
|
PRINTF("vw command: Not fully supported yet\r\n");
|
||||||
#endif
|
#endif
|
||||||
nexttok = blynkparam_get();
|
nexttok = blynkparam_get();
|
||||||
w_param = ATOI((uint8_t *)nexttok, 10);
|
w_param = ATOI((uint8_t *)nexttok, 10);
|
||||||
virtualWrite(pin, w_param);
|
virtualWrite(pin, w_param);
|
||||||
|
|
||||||
// update widget state
|
// update widget state
|
||||||
//rsp_len = sprintf((char *)rsp_mem, "vw %d %d ", pin, w_param);
|
//rsp_len = SPRINTF((char *)rsp_mem, "vw %d %d ", pin, w_param);
|
||||||
//replacetonull(rsp_mem, ' ');
|
//replacetonull(rsp_mem, ' ');
|
||||||
//sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len, NULL, 0);
|
//sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len, NULL, 0);
|
||||||
|
|
||||||
@@ -499,7 +500,7 @@ void processCmd(uint8_t * buff, size_t len)
|
|||||||
pinMode(pin, INPUT_PULLUP);
|
pinMode(pin, INPUT_PULLUP);
|
||||||
} else {
|
} else {
|
||||||
#ifdef BLYNK_DEBUG
|
#ifdef BLYNK_DEBUG
|
||||||
printf("Invalid pinMode %u -> %s\r\n", pin, nexttok);
|
PRINTF("Invalid pinMode %u -> %s\r\n", pin, nexttok);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
nexttok = blynkparam_get();
|
nexttok = blynkparam_get();
|
||||||
@@ -521,7 +522,7 @@ void processCmd(uint8_t * buff, size_t len)
|
|||||||
digitalWrite(pin, w_param ? HIGH : LOW);
|
digitalWrite(pin, w_param ? HIGH : LOW);
|
||||||
|
|
||||||
// update widget state
|
// update widget state
|
||||||
//rsp_len = sprintf((char *)rsp_mem, "dw %d %d ", pin, digitalRead(pin));
|
//rsp_len = SPRINTF((char *)rsp_mem, "dw %d %d ", pin, digitalRead(pin));
|
||||||
//replacetonull(rsp_mem, ' ');
|
//replacetonull(rsp_mem, ' ');
|
||||||
//sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len, NULL, 0);
|
//sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len, NULL, 0);
|
||||||
}
|
}
|
||||||
@@ -532,13 +533,13 @@ void processCmd(uint8_t * buff, size_t len)
|
|||||||
analogWrite(pin, w_param);
|
analogWrite(pin, w_param);
|
||||||
|
|
||||||
// update widget state
|
// update widget state
|
||||||
//rsp_len = sprintf((char *)rsp_mem, "aw %d %d ", pin, digitalRead(pin));
|
//rsp_len = SPRINTF((char *)rsp_mem, "aw %d %d ", pin, digitalRead(pin));
|
||||||
//replacetonull(rsp_mem, ' ');
|
//replacetonull(rsp_mem, ' ');
|
||||||
//sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len, NULL, 0);
|
//sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len, NULL, 0);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("Invalid HW cmd: %s\r\n", cmd);
|
PRINTF("Invalid HW cmd: %s\r\n", cmd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -551,7 +552,7 @@ uint8_t readHeader(BlynkHeader * hdr)
|
|||||||
if((len = getSn_RX_RSR(s)) > 0)
|
if((len = getSn_RX_RSR(s)) > 0)
|
||||||
{
|
{
|
||||||
#ifdef BLYNK_DEBUG
|
#ifdef BLYNK_DEBUG
|
||||||
//printf("recv header len = %d\r\n", len);
|
//PRINTF("recv header len = %d\r\n", len);
|
||||||
#endif
|
#endif
|
||||||
if(BLINK_HEADER_SIZE != recv(s, msgbuf, BLINK_HEADER_SIZE))
|
if(BLINK_HEADER_SIZE != recv(s, msgbuf, BLINK_HEADER_SIZE))
|
||||||
{
|
{
|
||||||
@@ -568,7 +569,7 @@ uint8_t readHeader(BlynkHeader * hdr)
|
|||||||
hdr->msg_id = ntohs(hdr->msg_id);
|
hdr->msg_id = ntohs(hdr->msg_id);
|
||||||
hdr->length = ntohs(hdr->length);
|
hdr->length = ntohs(hdr->length);
|
||||||
#ifdef BLYNK_DEBUG
|
#ifdef BLYNK_DEBUG
|
||||||
printf(">msg %d,%u,%u\r\n", hdr->type, hdr->msg_id, hdr->length);
|
PRINTF(">msg %d,%u,%u\r\n", hdr->type, hdr->msg_id, hdr->length);
|
||||||
#endif
|
#endif
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -591,7 +592,7 @@ void sendCmd(uint8_t cmd, uint16_t id, uint8_t * data, size_t length, uint8_t *
|
|||||||
if(getSn_SR(s) != SOCK_ESTABLISHED)
|
if(getSn_SR(s) != SOCK_ESTABLISHED)
|
||||||
{
|
{
|
||||||
#ifdef BLYNK_DEBUG
|
#ifdef BLYNK_DEBUG
|
||||||
printf("Cmd not sent\r\n");
|
PRINTF("Cmd not sent\r\n");
|
||||||
#endif
|
#endif
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -612,7 +613,7 @@ void sendCmd(uint8_t cmd, uint16_t id, uint8_t * data, size_t length, uint8_t *
|
|||||||
msgbuf[hsize++] = (uint8_t)((0xff00 & hdr.length) >> 8);
|
msgbuf[hsize++] = (uint8_t)((0xff00 & hdr.length) >> 8);
|
||||||
|
|
||||||
#ifdef BLYNK_DEBUG
|
#ifdef BLYNK_DEBUG
|
||||||
printf("<msg %d,%u,%u\r\n", cmd, id, length+length2);
|
PRINTF("<msg %d,%u,%u\r\n", cmd, id, length+length2);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
wlen += (size_t)send(s, msgbuf, hsize);
|
wlen += (size_t)send(s, msgbuf, hsize);
|
||||||
@@ -620,30 +621,30 @@ void sendCmd(uint8_t cmd, uint16_t id, uint8_t * data, size_t length, uint8_t *
|
|||||||
if (cmd != BLYNK_CMD_RESPONSE) {
|
if (cmd != BLYNK_CMD_RESPONSE) {
|
||||||
if (length) {
|
if (length) {
|
||||||
#ifdef BLYNK_DEBUG
|
#ifdef BLYNK_DEBUG
|
||||||
printf("<");
|
PRINTF("<");
|
||||||
for(i = 0; i < length; i++)
|
for(i = 0; i < length; i++)
|
||||||
{
|
{
|
||||||
if(data[i] != '\0') printf("%c", data[i]);
|
if(data[i] != '\0') PRINTF("%c", data[i]);
|
||||||
else printf(" ");
|
else PRINTF(" ");
|
||||||
}
|
}
|
||||||
printf("\r\n");
|
PRINTF("\r\n");
|
||||||
#endif
|
#endif
|
||||||
wlen += (size_t)send(s, (uint8_t *)data, length);
|
wlen += (size_t)send(s, (uint8_t *)data, length);
|
||||||
}
|
}
|
||||||
if (length2) {
|
if (length2) {
|
||||||
#ifdef BLYNK_DEBUG
|
#ifdef BLYNK_DEBUG
|
||||||
printf("<");
|
PRINTF("<");
|
||||||
for(i = 0; i < length2; i++)
|
for(i = 0; i < length2; i++)
|
||||||
{
|
{
|
||||||
if(data2[i] != '\0') printf("%c", data2[i]);
|
if(data2[i] != '\0') PRINTF("%c", data2[i]);
|
||||||
else printf(" ");
|
else PRINTF(" ");
|
||||||
}
|
}
|
||||||
printf("\r\n");
|
PRINTF("\r\n");
|
||||||
#endif
|
#endif
|
||||||
wlen += (size_t)send(s, (uint8_t *)data2, length2);
|
wlen += (size_t)send(s, (uint8_t *)data2, length2);
|
||||||
}
|
}
|
||||||
if (wlen != hsize+length+length2) {
|
if (wlen != hsize+length+length2) {
|
||||||
printf("Sent %u/%u\r\n", wlen, hsize+length+length2);
|
PRINTF("Sent %u/%u\r\n", wlen, hsize+length+length2);
|
||||||
disconnect(s);
|
disconnect(s);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -655,7 +656,7 @@ void sendCmd(uint8_t cmd, uint16_t id, uint8_t * data, size_t length, uint8_t *
|
|||||||
lastActivityOut = ts;
|
lastActivityOut = ts;
|
||||||
if (deltaCmd < (1000/BLYNK_MSG_LIMIT)) {
|
if (deltaCmd < (1000/BLYNK_MSG_LIMIT)) {
|
||||||
//::delay(5000);
|
//::delay(5000);
|
||||||
printf("Flood\r\n");
|
PRINTF("Flood\r\n");
|
||||||
disconnect(s);
|
disconnect(s);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
|
|||||||
@@ -33,9 +33,18 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef BLYNK_INFO_CPU
|
#ifndef BLYNK_INFO_CPU
|
||||||
|
/*
|
||||||
|
#if defined (__AVR_ATmega644P__)
|
||||||
|
#define BLYNK_INFO_CPU "ATmega644"
|
||||||
|
#elif defined (__AVR_ATmega1284P__)
|
||||||
|
#define BLYNK_INFO_CPU "ATmega1284"
|
||||||
|
#else
|
||||||
#define BLYNK_INFO_CPU "ATmega2560"
|
#define BLYNK_INFO_CPU "ATmega2560"
|
||||||
//#define BLYNK_INFO_CPU "ST103FRB"
|
//#define BLYNK_INFO_CPU "ST103FRB"
|
||||||
#endif
|
#endif
|
||||||
|
*/
|
||||||
|
#define BLYNK_INFO_CPU "ATmega2560"
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef BLYNK_INFO_CONNECTION
|
#ifndef BLYNK_INFO_CONNECTION
|
||||||
#define BLYNK_INFO_CONNECTION "W5000"
|
#define BLYNK_INFO_CONNECTION "W5000"
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ uint8_t digitalRead(uint8_t pin)
|
|||||||
#elif defined WIZNET_W5500_EVB
|
#elif defined WIZNET_W5500_EVB
|
||||||
val = Chip_GPIO_GetPinState(LPC_GPIO, dio_ports[pin], dio_pins[pin]);
|
val = Chip_GPIO_GetPinState(LPC_GPIO, dio_ports[pin], dio_pins[pin]);
|
||||||
#else
|
#else
|
||||||
printf("digital pin %d read\r\n", pin);
|
PRINTF("digital pin %d read\r\n", pin);
|
||||||
#endif
|
#endif
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
@@ -35,7 +35,7 @@ void digitalWrite(uint8_t pin, uint8_t val)
|
|||||||
if(val == HIGH) Chip_GPIO_SetPinState(LPC_GPIO, dio_ports[pin], dio_pins[pin], true); // High
|
if(val == HIGH) Chip_GPIO_SetPinState(LPC_GPIO, dio_ports[pin], dio_pins[pin], true); // High
|
||||||
else if(val == LOW) Chip_GPIO_SetPinState(LPC_GPIO, dio_ports[pin], dio_pins[pin], false); // Low
|
else if(val == LOW) Chip_GPIO_SetPinState(LPC_GPIO, dio_ports[pin], dio_pins[pin], false); // Low
|
||||||
#else
|
#else
|
||||||
printf("digital pin %d write val %d\r\n", pin, val);
|
PRINTF("digital pin %d write val %d\r\n", pin, val);
|
||||||
if(pin == 13)
|
if(pin == 13)
|
||||||
{
|
{
|
||||||
if(val == 0)
|
if(val == 0)
|
||||||
@@ -56,15 +56,15 @@ uint16_t analogRead(uint8_t pin)
|
|||||||
uint16_t val = 0;
|
uint16_t val = 0;
|
||||||
if(pin > 14) analog_pin = pin - 14;
|
if(pin > 14) analog_pin = pin - 14;
|
||||||
#ifdef WIZNET_WIZ550WEB
|
#ifdef WIZNET_WIZ550WEB
|
||||||
//printf("analog_pin = %d\r\n", analog_pin);
|
//PRINTF("analog_pin = %d\r\n", analog_pin);
|
||||||
val = get_ADC_val(analog_pin);
|
val = get_ADC_val(analog_pin);
|
||||||
#elif defined WIZNET_W5500_EVB
|
#elif defined WIZNET_W5500_EVB
|
||||||
printf("analog_pin = %d\r\n", analog_pin);
|
PRINTF("analog_pin = %d\r\n", analog_pin);
|
||||||
if(analog_pin == A0) analog_pin = AIN;
|
if(analog_pin == A0) analog_pin = AIN;
|
||||||
printf("changed analog_pin = %d\r\n", analog_pin);
|
PRINTF("changed analog_pin = %d\r\n", analog_pin);
|
||||||
val = get_ADC_val(analog_pin);
|
val = get_ADC_val(analog_pin);
|
||||||
#else
|
#else
|
||||||
printf("analog pin %d read\r\n", analog_pin);
|
PRINTF("analog pin %d read\r\n", analog_pin);
|
||||||
#endif
|
#endif
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
@@ -72,11 +72,11 @@ uint16_t analogRead(uint8_t pin)
|
|||||||
void analogWrite(uint8_t pin, uint8_t val)
|
void analogWrite(uint8_t pin, uint8_t val)
|
||||||
{
|
{
|
||||||
#ifdef WIZNET_WIZ550WEB
|
#ifdef WIZNET_WIZ550WEB
|
||||||
printf("Analog Write: Not supported yet. pin %d, val %d", pin, val);
|
PRINTF("Analog Write: Not supported yet. pin %d, val %d", pin, val);
|
||||||
#elif defined WIZNET_W5500_EVB
|
#elif defined WIZNET_W5500_EVB
|
||||||
printf("Analog Write: Not supported yet. pin %d, val %d", pin, val);
|
PRINTF("Analog Write: Not supported yet. pin %d, val %d", pin, val);
|
||||||
#else
|
#else
|
||||||
printf("analog pin %d write val %d\r\n", pin, val);
|
PRINTF("analog pin %d write val %d\r\n", pin, val);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,7 +97,7 @@ void pinMode(uint8_t pin, pinmode_dir dir)
|
|||||||
else if(dir == INPUT_PULLUP) Chip_GPIO_SetPinDIRInput(LPC_GPIO, dio_ports[pin], dio_pins[pin]); // Input
|
else if(dir == INPUT_PULLUP) Chip_GPIO_SetPinDIRInput(LPC_GPIO, dio_ports[pin], dio_pins[pin]); // Input
|
||||||
else if(dir == OUTPUT) Chip_GPIO_SetPinDIROutput(LPC_GPIO, dio_ports[pin], dio_pins[pin]); // Output
|
else if(dir == OUTPUT) Chip_GPIO_SetPinDIROutput(LPC_GPIO, dio_ports[pin], dio_pins[pin]); // Output
|
||||||
#else
|
#else
|
||||||
printf("pinmode setting: pin %d dir %d\r\n", pin, dir);
|
PRINTF("pinmode setting: pin %d dir %d\r\n", pin, dir);
|
||||||
if((pin == 13)&&(dir ==1))
|
if((pin == 13)&&(dir ==1))
|
||||||
{
|
{
|
||||||
//m1284p LED1 pin to out
|
//m1284p LED1 pin to out
|
||||||
@@ -109,11 +109,11 @@ void pinMode(uint8_t pin, pinmode_dir dir)
|
|||||||
// Virtual Pin Read / Write functions; Not fully supported yet
|
// Virtual Pin Read / Write functions; Not fully supported yet
|
||||||
uint16_t virtualRead(uint8_t pin)
|
uint16_t virtualRead(uint8_t pin)
|
||||||
{
|
{
|
||||||
printf("virtual pin %d read\r\n", pin);
|
PRINTF("virtual pin %d read\r\n", pin);
|
||||||
return pin;
|
return pin;
|
||||||
}
|
}
|
||||||
|
|
||||||
void virtualWrite(uint8_t pin, uint16_t val)
|
void virtualWrite(uint8_t pin, uint16_t val)
|
||||||
{
|
{
|
||||||
printf("virtual pin %d write val %d\r\n", pin, val);
|
PRINTF("virtual pin %d write val %d\r\n", pin, val);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,225 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include "loopback.h"
|
|
||||||
#include "socket.h"
|
|
||||||
#include "wizchip_conf.h"
|
|
||||||
|
|
||||||
#if LOOPBACK_MODE == LOOPBACK_MAIN_NOBLCOK
|
|
||||||
|
|
||||||
int32_t loopback_tcps(uint8_t sn, uint8_t* buf, uint16_t port)
|
|
||||||
{
|
|
||||||
int32_t ret;
|
|
||||||
uint16_t size = 0, sentsize=0;
|
|
||||||
|
|
||||||
#ifdef _LOOPBACK_DEBUG_
|
|
||||||
uint8_t destip[4];
|
|
||||||
uint16_t destport;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
switch(getSn_SR(sn))
|
|
||||||
{
|
|
||||||
case SOCK_ESTABLISHED :
|
|
||||||
if(getSn_IR(sn) & Sn_IR_CON)
|
|
||||||
{
|
|
||||||
#ifdef _LOOPBACK_DEBUG_
|
|
||||||
getSn_DIPR(sn, destip);
|
|
||||||
destport = getSn_DPORT(sn);
|
|
||||||
|
|
||||||
printf("%d:Connected - %d.%d.%d.%d : %d\r\n",sn, destip[0], destip[1], destip[2], destip[3], destport);
|
|
||||||
#endif
|
|
||||||
setSn_IR(sn,Sn_IR_CON);
|
|
||||||
}
|
|
||||||
if((size = getSn_RX_RSR(sn)) > 0) // Don't need to check SOCKERR_BUSY because it doesn't not occur.
|
|
||||||
{
|
|
||||||
if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE;
|
|
||||||
ret = recv(sn, buf, size);
|
|
||||||
|
|
||||||
if(ret <= 0) return ret; // check SOCKERR_BUSY & SOCKERR_XXX. For showing the occurrence of SOCKERR_BUSY.
|
|
||||||
size = (uint16_t) ret;
|
|
||||||
sentsize = 0;
|
|
||||||
|
|
||||||
while(size != sentsize)
|
|
||||||
{
|
|
||||||
ret = send(sn, buf+sentsize, size-sentsize);
|
|
||||||
if(ret < 0)
|
|
||||||
{
|
|
||||||
close(sn);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case SOCK_CLOSE_WAIT :
|
|
||||||
#ifdef _LOOPBACK_DEBUG_
|
|
||||||
//printf("%d:CloseWait\r\n",sn);
|
|
||||||
#endif
|
|
||||||
if((ret = disconnect(sn)) != SOCK_OK) return ret;
|
|
||||||
#ifdef _LOOPBACK_DEBUG_
|
|
||||||
printf("%d:Socket Closed\r\n", sn);
|
|
||||||
#endif
|
|
||||||
break;
|
|
||||||
case SOCK_INIT :
|
|
||||||
#ifdef _LOOPBACK_DEBUG_
|
|
||||||
printf("%d:Listen, TCP server loopback, port [%d]\r\n", sn, port);
|
|
||||||
#endif
|
|
||||||
if( (ret = listen(sn)) != SOCK_OK) return ret;
|
|
||||||
break;
|
|
||||||
case SOCK_CLOSED:
|
|
||||||
#ifdef _LOOPBACK_DEBUG_
|
|
||||||
//printf("%d:TCP server loopback start\r\n",sn);
|
|
||||||
#endif
|
|
||||||
if((ret = socket(sn, Sn_MR_TCP, port, 0x00)) != sn) return ret;
|
|
||||||
#ifdef _LOOPBACK_DEBUG_
|
|
||||||
//printf("%d:Socket opened\r\n",sn);
|
|
||||||
#endif
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int32_t loopback_tcpc(uint8_t sn, uint8_t* buf, uint8_t* destip, uint16_t destport)
|
|
||||||
{
|
|
||||||
int32_t ret; // return value for SOCK_ERRORs
|
|
||||||
uint16_t size = 0, sentsize=0;
|
|
||||||
|
|
||||||
// Destination (TCP Server) IP info (will be connected)
|
|
||||||
// >> loopback_tcpc() function parameter
|
|
||||||
// >> Ex)
|
|
||||||
// uint8_t destip[4] = {192, 168, 0, 214};
|
|
||||||
// uint16_t destport = 5000;
|
|
||||||
|
|
||||||
// Port number for TCP client (will be increased)
|
|
||||||
static uint16_t any_port = 50000;
|
|
||||||
|
|
||||||
// Socket Status Transitions
|
|
||||||
// Check the W5500 Socket n status register (Sn_SR, The 'Sn_SR' controlled by Sn_CR command or Packet send/recv status)
|
|
||||||
switch(getSn_SR(sn))
|
|
||||||
{
|
|
||||||
case SOCK_ESTABLISHED :
|
|
||||||
if(getSn_IR(sn) & Sn_IR_CON) // Socket n interrupt register mask; TCP CON interrupt = connection with peer is successful
|
|
||||||
{
|
|
||||||
#ifdef _LOOPBACK_DEBUG_
|
|
||||||
printf("%d:Connected to - %d.%d.%d.%d : %d\r\n",sn, destip[0], destip[1], destip[2], destip[3], destport);
|
|
||||||
#endif
|
|
||||||
setSn_IR(sn, Sn_IR_CON); // this interrupt should be write the bit cleared to '1'
|
|
||||||
}
|
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Data Transaction Parts; Handle the [data receive and send] process
|
|
||||||
//////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
if((size = getSn_RX_RSR(sn)) > 0) // Sn_RX_RSR: Socket n Received Size Register, Receiving data length
|
|
||||||
{
|
|
||||||
if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE; // DATA_BUF_SIZE means user defined buffer size (array)
|
|
||||||
ret = recv(sn, buf, size); // Data Receive process (H/W Rx socket buffer -> User's buffer)
|
|
||||||
|
|
||||||
if(ret <= 0) return ret; // If the received data length <= 0, receive failed and process end
|
|
||||||
size = (uint16_t) ret;
|
|
||||||
sentsize = 0;
|
|
||||||
|
|
||||||
// Data sentsize control
|
|
||||||
while(size != sentsize)
|
|
||||||
{
|
|
||||||
ret = send(sn, buf+sentsize, size-sentsize); // Data send process (User's buffer -> Destination through H/W Tx socket buffer)
|
|
||||||
if(ret < 0) // Send Error occurred (sent data length < 0)
|
|
||||||
{
|
|
||||||
close(sn); // socket close
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SOCK_CLOSE_WAIT :
|
|
||||||
#ifdef _LOOPBACK_DEBUG_
|
|
||||||
//printf("%d:CloseWait\r\n",sn);
|
|
||||||
#endif
|
|
||||||
if((ret=disconnect(sn)) != SOCK_OK) return ret;
|
|
||||||
#ifdef _LOOPBACK_DEBUG_
|
|
||||||
printf("%d:Socket Closed\r\n", sn);
|
|
||||||
#endif
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SOCK_INIT :
|
|
||||||
#ifdef _LOOPBACK_DEBUG_
|
|
||||||
printf("%d:Try to connect to the %d.%d.%d.%d : %d\r\n", sn, destip[0], destip[1], destip[2], destip[3], destport);
|
|
||||||
#endif
|
|
||||||
if( (ret = connect(sn, destip, destport)) != SOCK_OK) return ret; // Try to TCP connect to the TCP server (destination)
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SOCK_CLOSED:
|
|
||||||
close(sn);
|
|
||||||
if((ret=socket(sn, Sn_MR_TCP, any_port++, 0x00)) != sn){
|
|
||||||
if(any_port == 0xffff) any_port = 50000;
|
|
||||||
return ret; // TCP socket open with 'any_port' port number
|
|
||||||
}
|
|
||||||
#ifdef _LOOPBACK_DEBUG_
|
|
||||||
//printf("%d:TCP client loopback start\r\n",sn);
|
|
||||||
//printf("%d:Socket opened\r\n",sn);
|
|
||||||
#endif
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int32_t loopback_udps(uint8_t sn, uint8_t* buf, uint16_t port)
|
|
||||||
{
|
|
||||||
int32_t ret;
|
|
||||||
uint16_t size, sentsize;
|
|
||||||
uint8_t destip[4];
|
|
||||||
uint16_t destport;
|
|
||||||
|
|
||||||
switch(getSn_SR(sn))
|
|
||||||
{
|
|
||||||
case SOCK_UDP :
|
|
||||||
if((size = getSn_RX_RSR(sn)) > 0)
|
|
||||||
{
|
|
||||||
if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE;
|
|
||||||
ret = recvfrom(sn, buf, size, destip, (uint16_t*)&destport);
|
|
||||||
if(ret <= 0)
|
|
||||||
{
|
|
||||||
#ifdef _LOOPBACK_DEBUG_
|
|
||||||
printf("%d: recvfrom error. %ld\r\n",sn,ret);
|
|
||||||
#endif
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
size = (uint16_t) ret;
|
|
||||||
sentsize = 0;
|
|
||||||
while(sentsize != size)
|
|
||||||
{
|
|
||||||
ret = sendto(sn, buf+sentsize, size-sentsize, destip, destport);
|
|
||||||
if(ret < 0)
|
|
||||||
{
|
|
||||||
#ifdef _LOOPBACK_DEBUG_
|
|
||||||
printf("%d: sendto error. %ld\r\n",sn,ret);
|
|
||||||
#endif
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case SOCK_CLOSED:
|
|
||||||
#ifdef _LOOPBACK_DEBUG_
|
|
||||||
//printf("%d:UDP loopback start\r\n",sn);
|
|
||||||
#endif
|
|
||||||
if((ret = socket(sn, Sn_MR_UDP, port, 0x00)) != sn)
|
|
||||||
return ret;
|
|
||||||
#ifdef _LOOPBACK_DEBUG_
|
|
||||||
printf("%d:Opened, UDP loopback, port [%d]\r\n", sn, port);
|
|
||||||
#endif
|
|
||||||
break;
|
|
||||||
default :
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
#ifndef _LOOPBACK_H_
|
|
||||||
#define _LOOPBACK_H_
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
/* Loopback test debug message printout enable */
|
|
||||||
#define _LOOPBACK_DEBUG_
|
|
||||||
|
|
||||||
/* DATA_BUF_SIZE define for Loopback example */
|
|
||||||
#ifndef DATA_BUF_SIZE
|
|
||||||
#define DATA_BUF_SIZE 2048
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/************************/
|
|
||||||
/* Select LOOPBACK_MODE */
|
|
||||||
/************************/
|
|
||||||
#define LOOPBACK_MAIN_NOBLOCK 0
|
|
||||||
#define LOOPBACK_MODE LOOPBACK_MAIN_NOBLOCK
|
|
||||||
|
|
||||||
|
|
||||||
/* TCP server Loopback test example */
|
|
||||||
int32_t loopback_tcps(uint8_t sn, uint8_t* buf, uint16_t port);
|
|
||||||
|
|
||||||
/* TCP client Loopback test example */
|
|
||||||
int32_t loopback_tcpc(uint8_t sn, uint8_t* buf, uint8_t* destip, uint16_t destport);
|
|
||||||
|
|
||||||
/* UDP Loopback test example */
|
|
||||||
int32_t loopback_udps(uint8_t sn, uint8_t* buf, uint16_t port);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
|
||||||
566
19_m1284p_WIZNET_blynk/Internet/DNS/dns.c
Normal file
566
19_m1284p_WIZNET_blynk/Internet/DNS/dns.c
Normal file
@@ -0,0 +1,566 @@
|
|||||||
|
//*****************************************************************************
|
||||||
|
//
|
||||||
|
//! \file dns.c
|
||||||
|
//! \brief DNS APIs Implement file.
|
||||||
|
//! \details Send DNS query & Receive DNS reponse. \n
|
||||||
|
//! It depends on stdlib.h & string.h in ansi-c library
|
||||||
|
//! \version 1.1.0
|
||||||
|
//! \date 2013/11/18
|
||||||
|
//! \par Revision history
|
||||||
|
//! <2013/10/21> 1st Release
|
||||||
|
//! <2013/12/20> V1.1.0
|
||||||
|
//! 1. Remove secondary DNS server in DNS_run
|
||||||
|
//! If 1st DNS_run failed, call DNS_run with 2nd DNS again
|
||||||
|
//! 2. DNS_timerHandler -> DNS_time_handler
|
||||||
|
//! 3. Remove the unused define
|
||||||
|
//! 4. Integrated dns.h dns.c & dns_parse.h dns_parse.c into dns.h & dns.c
|
||||||
|
//! <2013/12/20> V1.1.0
|
||||||
|
//!
|
||||||
|
//! \author Eric Jung & MidnightCow
|
||||||
|
//! \copyright
|
||||||
|
//!
|
||||||
|
//! Copyright (c) 2013, WIZnet Co., LTD.
|
||||||
|
//! All rights reserved.
|
||||||
|
//!
|
||||||
|
//! Redistribution and use in source and binary forms, with or without
|
||||||
|
//! modification, are permitted provided that the following conditions
|
||||||
|
//! are met:
|
||||||
|
//!
|
||||||
|
//! * Redistributions of source code must retain the above copyright
|
||||||
|
//! notice, this list of conditions and the following disclaimer.
|
||||||
|
//! * Redistributions in binary form must reproduce the above copyright
|
||||||
|
//! notice, this list of conditions and the following disclaimer in the
|
||||||
|
//! documentation and/or other materials provided with the distribution.
|
||||||
|
//! * Neither the name of the <ORGANIZATION> nor the names of its
|
||||||
|
//! contributors may be used to endorse or promote products derived
|
||||||
|
//! from this software without specific prior written permission.
|
||||||
|
//!
|
||||||
|
//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
//! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
//! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
//! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||||
|
//! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
//! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
//! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
//! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
//! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
//! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
||||||
|
//! THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
//
|
||||||
|
//*****************************************************************************
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "socket.h"
|
||||||
|
#include "dns.h"
|
||||||
|
|
||||||
|
#ifdef _DNS_DEBUG_
|
||||||
|
#include <stdio.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define INITRTT 2000L /* Initial smoothed response time */
|
||||||
|
#define MAXCNAME (MAX_DOMAIN_NAME + (MAX_DOMAIN_NAME>>1)) /* Maximum amount of cname recursion */
|
||||||
|
|
||||||
|
#define TYPE_A 1 /* Host address */
|
||||||
|
#define TYPE_NS 2 /* Name server */
|
||||||
|
#define TYPE_MD 3 /* Mail destination (obsolete) */
|
||||||
|
#define TYPE_MF 4 /* Mail forwarder (obsolete) */
|
||||||
|
#define TYPE_CNAME 5 /* Canonical name */
|
||||||
|
#define TYPE_SOA 6 /* Start of Authority */
|
||||||
|
#define TYPE_MB 7 /* Mailbox name (experimental) */
|
||||||
|
#define TYPE_MG 8 /* Mail group member (experimental) */
|
||||||
|
#define TYPE_MR 9 /* Mail rename name (experimental) */
|
||||||
|
#define TYPE_NULL 10 /* Null (experimental) */
|
||||||
|
#define TYPE_WKS 11 /* Well-known sockets */
|
||||||
|
#define TYPE_PTR 12 /* Pointer record */
|
||||||
|
#define TYPE_HINFO 13 /* Host information */
|
||||||
|
#define TYPE_MINFO 14 /* Mailbox information (experimental)*/
|
||||||
|
#define TYPE_MX 15 /* Mail exchanger */
|
||||||
|
#define TYPE_TXT 16 /* Text strings */
|
||||||
|
#define TYPE_ANY 255 /* Matches any type */
|
||||||
|
|
||||||
|
#define CLASS_IN 1 /* The ARPA Internet */
|
||||||
|
|
||||||
|
/* Round trip timing parameters */
|
||||||
|
#define AGAIN 8 /* Average RTT gain = 1/8 */
|
||||||
|
#define LAGAIN 3 /* Log2(AGAIN) */
|
||||||
|
#define DGAIN 4 /* Mean deviation gain = 1/4 */
|
||||||
|
#define LDGAIN 2 /* log2(DGAIN) */
|
||||||
|
|
||||||
|
/* Header for all domain messages */
|
||||||
|
struct dhdr
|
||||||
|
{
|
||||||
|
uint16_t id; /* Identification */
|
||||||
|
uint8_t qr; /* Query/Response */
|
||||||
|
#define QUERY 0
|
||||||
|
#define RESPONSE 1
|
||||||
|
uint8_t opcode;
|
||||||
|
#define IQUERY 1
|
||||||
|
uint8_t aa; /* Authoratative answer */
|
||||||
|
uint8_t tc; /* Truncation */
|
||||||
|
uint8_t rd; /* Recursion desired */
|
||||||
|
uint8_t ra; /* Recursion available */
|
||||||
|
uint8_t rcode; /* Response code */
|
||||||
|
#define NO_ERROR 0
|
||||||
|
#define FORMAT_ERROR 1
|
||||||
|
#define SERVER_FAIL 2
|
||||||
|
#define NAME_ERROR 3
|
||||||
|
#define NOT_IMPL 4
|
||||||
|
#define REFUSED 5
|
||||||
|
uint16_t qdcount; /* Question count */
|
||||||
|
uint16_t ancount; /* Answer count */
|
||||||
|
uint16_t nscount; /* Authority (name server) count */
|
||||||
|
uint16_t arcount; /* Additional record count */
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
uint8_t* pDNSMSG; // DNS message buffer
|
||||||
|
uint8_t DNS_SOCKET; // SOCKET number for DNS
|
||||||
|
uint16_t DNS_MSGID; // DNS message ID
|
||||||
|
|
||||||
|
uint32_t dns_1s_tick; // for timout of DNS processing
|
||||||
|
static uint8_t retry_count;
|
||||||
|
|
||||||
|
/* converts uint16_t from network buffer to a host byte order integer. */
|
||||||
|
uint16_t get16(uint8_t * s)
|
||||||
|
{
|
||||||
|
uint16_t i;
|
||||||
|
i = *s++ << 8;
|
||||||
|
i = i + *s;
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* copies uint16_t to the network buffer with network byte order. */
|
||||||
|
uint8_t * put16(uint8_t * s, uint16_t i)
|
||||||
|
{
|
||||||
|
*s++ = i >> 8;
|
||||||
|
*s++ = i;
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* CONVERT A DOMAIN NAME TO THE HUMAN-READABLE FORM
|
||||||
|
*
|
||||||
|
* Description : This function converts a compressed domain name to the human-readable form
|
||||||
|
* Arguments : msg - is a pointer to the reply message
|
||||||
|
* compressed - is a pointer to the domain name in reply message.
|
||||||
|
* buf - is a pointer to the buffer for the human-readable form name.
|
||||||
|
* len - is the MAX. size of buffer.
|
||||||
|
* Returns : the length of compressed message
|
||||||
|
*/
|
||||||
|
int parse_name(uint8_t * msg, uint8_t * compressed, char * buf, int16_t len)
|
||||||
|
{
|
||||||
|
uint16_t slen; /* Length of current segment */
|
||||||
|
uint8_t * cp;
|
||||||
|
int clen = 0; /* Total length of compressed name */
|
||||||
|
int indirect = 0; /* Set if indirection encountered */
|
||||||
|
int nseg = 0; /* Total number of segments in name */
|
||||||
|
|
||||||
|
cp = compressed;
|
||||||
|
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
slen = *cp++; /* Length of this segment */
|
||||||
|
|
||||||
|
if (!indirect) clen++;
|
||||||
|
|
||||||
|
if ((slen & 0xc0) == 0xc0)
|
||||||
|
{
|
||||||
|
if (!indirect)
|
||||||
|
clen++;
|
||||||
|
indirect = 1;
|
||||||
|
/* Follow indirection */
|
||||||
|
cp = &msg[((slen & 0x3f)<<8) + *cp];
|
||||||
|
slen = *cp++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (slen == 0) /* zero length == all done */
|
||||||
|
break;
|
||||||
|
|
||||||
|
len -= slen + 1;
|
||||||
|
|
||||||
|
if (len < 0) return -1;
|
||||||
|
|
||||||
|
if (!indirect) clen += slen;
|
||||||
|
|
||||||
|
while (slen-- != 0) *buf++ = (char)*cp++;
|
||||||
|
*buf++ = '.';
|
||||||
|
nseg++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nseg == 0)
|
||||||
|
{
|
||||||
|
/* Root name; represent as single dot */
|
||||||
|
*buf++ = '.';
|
||||||
|
len--;
|
||||||
|
}
|
||||||
|
|
||||||
|
*buf++ = '\0';
|
||||||
|
len--;
|
||||||
|
|
||||||
|
return clen; /* Length of compressed message */
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* PARSE QUESTION SECTION
|
||||||
|
*
|
||||||
|
* Description : This function parses the qeustion record of the reply message.
|
||||||
|
* Arguments : msg - is a pointer to the reply message
|
||||||
|
* cp - is a pointer to the qeustion record.
|
||||||
|
* Returns : a pointer the to next record.
|
||||||
|
*/
|
||||||
|
uint8_t * dns_question(uint8_t * msg, uint8_t * cp)
|
||||||
|
{
|
||||||
|
int len;
|
||||||
|
char name[MAXCNAME];
|
||||||
|
|
||||||
|
len = parse_name(msg, cp, name, MAXCNAME);
|
||||||
|
|
||||||
|
|
||||||
|
if (len == -1) return 0;
|
||||||
|
|
||||||
|
cp += len;
|
||||||
|
cp += 2; /* type */
|
||||||
|
cp += 2; /* class */
|
||||||
|
|
||||||
|
return cp;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* PARSE ANSER SECTION
|
||||||
|
*
|
||||||
|
* Description : This function parses the answer record of the reply message.
|
||||||
|
* Arguments : msg - is a pointer to the reply message
|
||||||
|
* cp - is a pointer to the answer record.
|
||||||
|
* Returns : a pointer the to next record.
|
||||||
|
*/
|
||||||
|
uint8_t * dns_answer(uint8_t * msg, uint8_t * cp, uint8_t * ip_from_dns)
|
||||||
|
{
|
||||||
|
int len, type;
|
||||||
|
char name[MAXCNAME];
|
||||||
|
|
||||||
|
len = parse_name(msg, cp, name, MAXCNAME);
|
||||||
|
|
||||||
|
if (len == -1) return 0;
|
||||||
|
|
||||||
|
cp += len;
|
||||||
|
type = get16(cp);
|
||||||
|
cp += 2; /* type */
|
||||||
|
cp += 2; /* class */
|
||||||
|
cp += 4; /* ttl */
|
||||||
|
cp += 2; /* len */
|
||||||
|
|
||||||
|
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
case TYPE_A:
|
||||||
|
/* Just read the address directly into the structure */
|
||||||
|
ip_from_dns[0] = *cp++;
|
||||||
|
ip_from_dns[1] = *cp++;
|
||||||
|
ip_from_dns[2] = *cp++;
|
||||||
|
ip_from_dns[3] = *cp++;
|
||||||
|
break;
|
||||||
|
case TYPE_CNAME:
|
||||||
|
case TYPE_MB:
|
||||||
|
case TYPE_MG:
|
||||||
|
case TYPE_MR:
|
||||||
|
case TYPE_NS:
|
||||||
|
case TYPE_PTR:
|
||||||
|
/* These types all consist of a single domain name */
|
||||||
|
/* convert it to ascii format */
|
||||||
|
len = parse_name(msg, cp, name, MAXCNAME);
|
||||||
|
if (len == -1) return 0;
|
||||||
|
|
||||||
|
cp += len;
|
||||||
|
break;
|
||||||
|
case TYPE_HINFO:
|
||||||
|
len = *cp++;
|
||||||
|
cp += len;
|
||||||
|
|
||||||
|
len = *cp++;
|
||||||
|
cp += len;
|
||||||
|
break;
|
||||||
|
case TYPE_MX:
|
||||||
|
cp += 2;
|
||||||
|
/* Get domain name of exchanger */
|
||||||
|
len = parse_name(msg, cp, name, MAXCNAME);
|
||||||
|
if (len == -1) return 0;
|
||||||
|
|
||||||
|
cp += len;
|
||||||
|
break;
|
||||||
|
case TYPE_SOA:
|
||||||
|
/* Get domain name of name server */
|
||||||
|
len = parse_name(msg, cp, name, MAXCNAME);
|
||||||
|
if (len == -1) return 0;
|
||||||
|
|
||||||
|
cp += len;
|
||||||
|
|
||||||
|
/* Get domain name of responsible person */
|
||||||
|
len = parse_name(msg, cp, name, MAXCNAME);
|
||||||
|
if (len == -1) return 0;
|
||||||
|
|
||||||
|
cp += len;
|
||||||
|
|
||||||
|
cp += 4;
|
||||||
|
cp += 4;
|
||||||
|
cp += 4;
|
||||||
|
cp += 4;
|
||||||
|
cp += 4;
|
||||||
|
break;
|
||||||
|
case TYPE_TXT:
|
||||||
|
/* Just stash */
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
/* Ignore */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return cp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* PARSE THE DNS REPLY
|
||||||
|
*
|
||||||
|
* Description : This function parses the reply message from DNS server.
|
||||||
|
* Arguments : dhdr - is a pointer to the header for DNS message
|
||||||
|
* buf - is a pointer to the reply message.
|
||||||
|
* len - is the size of reply message.
|
||||||
|
* Returns : -1 - Domain name lenght is too big
|
||||||
|
* 0 - Fail (Timout or parse error)
|
||||||
|
* 1 - Success,
|
||||||
|
*/
|
||||||
|
int8_t parseDNSMSG(struct dhdr * pdhdr, uint8_t * pbuf, uint8_t * ip_from_dns)
|
||||||
|
{
|
||||||
|
uint16_t tmp;
|
||||||
|
uint16_t i;
|
||||||
|
uint8_t * msg;
|
||||||
|
uint8_t * cp;
|
||||||
|
|
||||||
|
msg = pbuf;
|
||||||
|
memset(pdhdr, 0, sizeof(*pdhdr));
|
||||||
|
|
||||||
|
pdhdr->id = get16(&msg[0]);
|
||||||
|
tmp = get16(&msg[2]);
|
||||||
|
if (tmp & 0x8000) pdhdr->qr = 1;
|
||||||
|
|
||||||
|
pdhdr->opcode = (tmp >> 11) & 0xf;
|
||||||
|
|
||||||
|
if (tmp & 0x0400) pdhdr->aa = 1;
|
||||||
|
if (tmp & 0x0200) pdhdr->tc = 1;
|
||||||
|
if (tmp & 0x0100) pdhdr->rd = 1;
|
||||||
|
if (tmp & 0x0080) pdhdr->ra = 1;
|
||||||
|
|
||||||
|
pdhdr->rcode = tmp & 0xf;
|
||||||
|
pdhdr->qdcount = get16(&msg[4]);
|
||||||
|
pdhdr->ancount = get16(&msg[6]);
|
||||||
|
pdhdr->nscount = get16(&msg[8]);
|
||||||
|
pdhdr->arcount = get16(&msg[10]);
|
||||||
|
|
||||||
|
|
||||||
|
/* Now parse the variable length sections */
|
||||||
|
cp = &msg[12];
|
||||||
|
|
||||||
|
/* Question section */
|
||||||
|
for (i = 0; i < pdhdr->qdcount; i++)
|
||||||
|
{
|
||||||
|
cp = dns_question(msg, cp);
|
||||||
|
#ifdef _DNS_DEUBG_
|
||||||
|
printf("MAX_DOMAIN_NAME is too small, it should be redfine in dns.h"
|
||||||
|
#endif
|
||||||
|
if(!cp) return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Answer section */
|
||||||
|
for (i = 0; i < pdhdr->ancount; i++)
|
||||||
|
{
|
||||||
|
cp = dns_answer(msg, cp, ip_from_dns);
|
||||||
|
#ifdef _DNS_DEUBG_
|
||||||
|
printf("MAX_DOMAIN_NAME is too small, it should be redfine in dns.h"
|
||||||
|
#endif
|
||||||
|
if(!cp) return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Name server (authority) section */
|
||||||
|
for (i = 0; i < pdhdr->nscount; i++)
|
||||||
|
{
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Additional section */
|
||||||
|
for (i = 0; i < pdhdr->arcount; i++)
|
||||||
|
{
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(pdhdr->rcode == 0) return 1; // No error
|
||||||
|
else return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* MAKE DNS QUERY MESSAGE
|
||||||
|
*
|
||||||
|
* Description : This function makes DNS query message.
|
||||||
|
* Arguments : op - Recursion desired
|
||||||
|
* name - is a pointer to the domain name.
|
||||||
|
* buf - is a pointer to the buffer for DNS message.
|
||||||
|
* len - is the MAX. size of buffer.
|
||||||
|
* Returns : the pointer to the DNS message.
|
||||||
|
*/
|
||||||
|
int16_t dns_makequery(uint16_t op, char * name, uint8_t * buf, uint16_t len)
|
||||||
|
{
|
||||||
|
uint8_t *cp;
|
||||||
|
char *cp1;
|
||||||
|
char sname[MAXCNAME];
|
||||||
|
char *dname;
|
||||||
|
uint16_t p;
|
||||||
|
uint16_t dlen;
|
||||||
|
|
||||||
|
cp = buf;
|
||||||
|
|
||||||
|
DNS_MSGID++;
|
||||||
|
cp = put16(cp, DNS_MSGID);
|
||||||
|
p = (op << 11) | 0x0100; /* Recursion desired */
|
||||||
|
cp = put16(cp, p);
|
||||||
|
cp = put16(cp, 1);
|
||||||
|
cp = put16(cp, 0);
|
||||||
|
cp = put16(cp, 0);
|
||||||
|
cp = put16(cp, 0);
|
||||||
|
|
||||||
|
strcpy(sname, name);
|
||||||
|
dname = sname;
|
||||||
|
dlen = strlen(dname);
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
/* Look for next dot */
|
||||||
|
cp1 = strchr(dname, '.');
|
||||||
|
|
||||||
|
if (cp1 != NULL) len = cp1 - dname; /* More to come */
|
||||||
|
else len = dlen; /* Last component */
|
||||||
|
|
||||||
|
*cp++ = len; /* Write length of component */
|
||||||
|
if (len == 0) break;
|
||||||
|
|
||||||
|
/* Copy component up to (but not including) dot */
|
||||||
|
strncpy((char *)cp, dname, len);
|
||||||
|
cp += len;
|
||||||
|
if (cp1 == NULL)
|
||||||
|
{
|
||||||
|
*cp++ = 0; /* Last one; write null and finish */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
dname += len+1;
|
||||||
|
dlen -= len+1;
|
||||||
|
}
|
||||||
|
|
||||||
|
cp = put16(cp, 0x0001); /* type */
|
||||||
|
cp = put16(cp, 0x0001); /* class */
|
||||||
|
|
||||||
|
return ((int16_t)((uint32_t)(cp) - (uint32_t)(buf)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* CHECK DNS TIMEOUT
|
||||||
|
*
|
||||||
|
* Description : This function check the DNS timeout
|
||||||
|
* Arguments : None.
|
||||||
|
* Returns : -1 - timeout occurred, 0 - timer over, but no timeout, 1 - no timer over, no timeout occur
|
||||||
|
* Note : timeout : retry count and timer both over.
|
||||||
|
*/
|
||||||
|
|
||||||
|
int8_t check_DNS_timeout(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
if(dns_1s_tick >= DNS_WAIT_TIME)
|
||||||
|
{
|
||||||
|
dns_1s_tick = 0;
|
||||||
|
if(retry_count >= MAX_DNS_RETRY) {
|
||||||
|
retry_count = 0;
|
||||||
|
return -1; // timeout occurred
|
||||||
|
}
|
||||||
|
retry_count++;
|
||||||
|
return 0; // timer over, but no timeout
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1; // no timer over, no timeout occur
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* DNS CLIENT INIT */
|
||||||
|
void DNS_init(uint8_t s, uint8_t * buf)
|
||||||
|
{
|
||||||
|
DNS_SOCKET = s; // SOCK_DNS
|
||||||
|
pDNSMSG = buf; // User's shared buffer
|
||||||
|
DNS_MSGID = DNS_MSG_ID;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* DNS CLIENT RUN */
|
||||||
|
int8_t DNS_run(uint8_t * dns_ip, uint8_t * name, uint8_t * ip_from_dns)
|
||||||
|
{
|
||||||
|
int8_t ret;
|
||||||
|
struct dhdr dhp;
|
||||||
|
uint8_t ip[4];
|
||||||
|
uint16_t len, port;
|
||||||
|
int8_t ret_check_timeout;
|
||||||
|
|
||||||
|
retry_count = 0;
|
||||||
|
dns_1s_tick = 0;
|
||||||
|
|
||||||
|
// Socket open
|
||||||
|
socket(DNS_SOCKET, Sn_MR_UDP, 0, 0);
|
||||||
|
|
||||||
|
#ifdef _DNS_DEBUG_
|
||||||
|
printf("> DNS Query to DNS Server : %d.%d.%d.%d\r\n", dns_ip[0], dns_ip[1], dns_ip[2], dns_ip[3]);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
len = dns_makequery(0, (char *)name, pDNSMSG, MAX_DNS_BUF_SIZE);
|
||||||
|
sendto(DNS_SOCKET, pDNSMSG, len, dns_ip, IPPORT_DOMAIN);
|
||||||
|
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
if ((len = getSn_RX_RSR(DNS_SOCKET)) > 0)
|
||||||
|
{
|
||||||
|
if (len > MAX_DNS_BUF_SIZE) len = MAX_DNS_BUF_SIZE;
|
||||||
|
len = recvfrom(DNS_SOCKET, pDNSMSG, len, ip, &port);
|
||||||
|
#ifdef _DNS_DEBUG_
|
||||||
|
printf("> Receive DNS message from %d.%d.%d.%d(%d). len = %d\r\n", ip[0], ip[1], ip[2], ip[3],port,len);
|
||||||
|
#endif
|
||||||
|
ret = parseDNSMSG(&dhp, pDNSMSG, ip_from_dns);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// Check Timeout
|
||||||
|
ret_check_timeout = check_DNS_timeout();
|
||||||
|
if (ret_check_timeout < 0) {
|
||||||
|
|
||||||
|
#ifdef _DNS_DEBUG_
|
||||||
|
printf("> DNS Server is not responding : %d.%d.%d.%d\r\n", dns_ip[0], dns_ip[1], dns_ip[2], dns_ip[3]);
|
||||||
|
#endif
|
||||||
|
return 0; // timeout occurred
|
||||||
|
}
|
||||||
|
else if (ret_check_timeout == 0) {
|
||||||
|
|
||||||
|
#ifdef _DNS_DEBUG_
|
||||||
|
printf("> DNS Timeout\r\n");
|
||||||
|
#endif
|
||||||
|
sendto(DNS_SOCKET, pDNSMSG, len, dns_ip, IPPORT_DOMAIN);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
close(DNS_SOCKET);
|
||||||
|
// Return value
|
||||||
|
// 0 > : failed / 1 - success
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* DNS TIMER HANDLER */
|
||||||
|
void DNS_time_handler(void)
|
||||||
|
{
|
||||||
|
dns_1s_tick++;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
109
19_m1284p_WIZNET_blynk/Internet/DNS/dns.h
Normal file
109
19_m1284p_WIZNET_blynk/Internet/DNS/dns.h
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
//*****************************************************************************
|
||||||
|
//
|
||||||
|
//! \file dns.h
|
||||||
|
//! \brief DNS APIs Header file.
|
||||||
|
//! \details Send DNS query & Receive DNS reponse.
|
||||||
|
//! \version 1.1.0
|
||||||
|
//! \date 2013/11/18
|
||||||
|
//! \par Revision history
|
||||||
|
//! <2013/10/21> 1st Release
|
||||||
|
//! <2013/12/20> V1.1.0
|
||||||
|
//! 1. Remove secondary DNS server in DNS_run
|
||||||
|
//! If 1st DNS_run failed, call DNS_run with 2nd DNS again
|
||||||
|
//! 2. DNS_timerHandler -> DNS_time_handler
|
||||||
|
//! 3. Move the no reference define to dns.c
|
||||||
|
//! 4. Integrated dns.h dns.c & dns_parse.h dns_parse.c into dns.h & dns.c
|
||||||
|
//! <2013/12/20> V1.1.0
|
||||||
|
//!
|
||||||
|
//! \author Eric Jung & MidnightCow
|
||||||
|
//! \copyright
|
||||||
|
//!
|
||||||
|
//! Copyright (c) 2013, WIZnet Co., LTD.
|
||||||
|
//! All rights reserved.
|
||||||
|
//!
|
||||||
|
//! Redistribution and use in source and binary forms, with or without
|
||||||
|
//! modification, are permitted provided that the following conditions
|
||||||
|
//! are met:
|
||||||
|
//!
|
||||||
|
//! * Redistributions of source code must retain the above copyright
|
||||||
|
//! notice, this list of conditions and the following disclaimer.
|
||||||
|
//! * Redistributions in binary form must reproduce the above copyright
|
||||||
|
//! notice, this list of conditions and the following disclaimer in the
|
||||||
|
//! documentation and/or other materials provided with the distribution.
|
||||||
|
//! * Neither the name of the <ORGANIZATION> nor the names of its
|
||||||
|
//! contributors may be used to endorse or promote products derived
|
||||||
|
//! from this software without specific prior written permission.
|
||||||
|
//!
|
||||||
|
//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
//! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
//! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
//! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||||
|
//! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
//! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
//! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
//! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
//! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
//! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
||||||
|
//! THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
//
|
||||||
|
//*****************************************************************************
|
||||||
|
|
||||||
|
#ifndef _DNS_H_
|
||||||
|
#define _DNS_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
/*
|
||||||
|
* @brief Define it for Debug & Monitor DNS processing.
|
||||||
|
* @note If defined, it dependens on <stdio.h>
|
||||||
|
*/
|
||||||
|
//#define _DNS_DEBUG_
|
||||||
|
|
||||||
|
#define MAX_DNS_BUF_SIZE 256 ///< maximum size of DNS buffer. */
|
||||||
|
/*
|
||||||
|
* @brief Maxium length of your queried Domain name
|
||||||
|
* @todo SHOULD BE defined it equal as or greater than your Domain name lenght + null character(1)
|
||||||
|
* @note SHOULD BE careful to stack overflow because it is allocated 1.5 times as MAX_DOMAIN_NAME in stack.
|
||||||
|
*/
|
||||||
|
#define MAX_DOMAIN_NAME 16 // for example "www.google.com"
|
||||||
|
|
||||||
|
#define MAX_DNS_RETRY 2 ///< Requery Count
|
||||||
|
#define DNS_WAIT_TIME 3 ///< Wait response time. unit 1s.
|
||||||
|
|
||||||
|
#define IPPORT_DOMAIN 53 ///< DNS server port number
|
||||||
|
|
||||||
|
#define DNS_MSG_ID 0x1122 ///< ID for DNS message. You can be modifyed it any number
|
||||||
|
/*
|
||||||
|
* @brief DNS process initialize
|
||||||
|
* @param s : Socket number for DNS
|
||||||
|
* @param buf : Buffer for DNS message
|
||||||
|
*/
|
||||||
|
void DNS_init(uint8_t s, uint8_t * buf);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @brief DNS process
|
||||||
|
* @details Send DNS query and receive DNS response
|
||||||
|
* @param dns_ip : DNS server ip
|
||||||
|
* @param name : Domain name to be queryed
|
||||||
|
* @param ip_from_dns : IP address from DNS server
|
||||||
|
* @return -1 : failed. @ref MAX_DOMIN_NAME is too small \n
|
||||||
|
* 0 : failed (Timeout or Parse error)\n
|
||||||
|
* 1 : success
|
||||||
|
* @note This funtion blocks until success or fail. max time = @ref MAX_DNS_RETRY * @ref DNS_WAIT_TIME
|
||||||
|
*/
|
||||||
|
int8_t DNS_run(uint8_t * dns_ip, uint8_t * name, uint8_t * ip_from_dns);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @brief DNS 1s Tick Timer handler
|
||||||
|
* @note SHOULD BE register to your system 1s Tick timer handler
|
||||||
|
*/
|
||||||
|
void DNS_time_handler(void);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* _DNS_H_ */
|
||||||
@@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
#ifdef IP_WORK
|
#ifdef IP_WORK
|
||||||
//NIC metrics for WORK PC
|
//NIC metrics for WORK PC
|
||||||
|
uint8_t DNS_2nd[4] = {192, 168, 0, 1}; // Secondary DNS server IP
|
||||||
wiz_NetInfo netInfo = { .mac = {0x00, 0x08, 0xdc, 0xab, 0xcd, 0xef}, // Mac address
|
wiz_NetInfo netInfo = { .mac = {0x00, 0x08, 0xdc, 0xab, 0xcd, 0xef}, // Mac address
|
||||||
.ip = {192, 168, 0, 199}, // IP address
|
.ip = {192, 168, 0, 199}, // IP address
|
||||||
.sn = {255, 255, 255, 0}, // Subnet mask
|
.sn = {255, 255, 255, 0}, // Subnet mask
|
||||||
@@ -16,6 +17,7 @@ wiz_NetInfo netInfo = { .mac = {0x00, 0x08, 0xdc, 0xab, 0xcd, 0xef}, // Mac add
|
|||||||
.dhcp = NETINFO_STATIC}; //Static IP configuration
|
.dhcp = NETINFO_STATIC}; //Static IP configuration
|
||||||
#else
|
#else
|
||||||
//NIC metrics for another PC (second IP configuration)
|
//NIC metrics for another PC (second IP configuration)
|
||||||
|
uint8_t DNS_2nd[4] = {192, 168, 1, 1}; // Secondary DNS server IP
|
||||||
wiz_NetInfo netInfo = { .mac = {0x00, 0x08, 0xdc, 0xab, 0xcd, 0xef}, // Mac address
|
wiz_NetInfo netInfo = { .mac = {0x00, 0x08, 0xdc, 0xab, 0xcd, 0xef}, // Mac address
|
||||||
.ip = {192, 168, 1, 199}, // IP address
|
.ip = {192, 168, 1, 199}, // IP address
|
||||||
.sn = {255, 255, 255, 0}, // Subnet mask
|
.sn = {255, 255, 255, 0}, // Subnet mask
|
||||||
|
|||||||
@@ -29,9 +29,6 @@ static FATFS Fatfs; //File system object for each logical drive. >= 2
|
|||||||
//******************************* Fat FS declare related: END
|
//******************************* Fat FS declare related: END
|
||||||
|
|
||||||
|
|
||||||
#define HTTPD_MAX_BUF_SIZE 2048 //For Mega1284p(16kb RAM)/Mega2560(8kb RAM)
|
|
||||||
//#define HTTPD_MAX_BUF_SIZE MAX_URI_SIZE+10 //For Mega644p(4kb RAM)/Mega128(4kb RAM) (ie. 512+10=522 bytes look at httpParser.h <_st_http_request> definition)
|
|
||||||
|
|
||||||
#define PRINTF_EN 1
|
#define PRINTF_EN 1
|
||||||
#if PRINTF_EN
|
#if PRINTF_EN
|
||||||
#define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
|
#define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
|
||||||
@@ -39,7 +36,9 @@ static FATFS Fatfs; //File system object for each logical drive. >= 2
|
|||||||
#define PRINTF(...)
|
#define PRINTF(...)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define IP_WORK
|
#define SPRINTF(__S, FORMAT, args...) sprintf_P(__S, PSTR(FORMAT),##args)
|
||||||
|
|
||||||
|
//#define IP_WORK
|
||||||
|
|
||||||
extern unsigned long millis(void);
|
extern unsigned long millis(void);
|
||||||
extern int freeRam (void);
|
extern int freeRam (void);
|
||||||
@@ -61,5 +60,8 @@ extern const char compile_time[] PROGMEM;
|
|||||||
extern const char str_prog_name[] PROGMEM;
|
extern const char str_prog_name[] PROGMEM;
|
||||||
|
|
||||||
extern wiz_NetInfo netInfo;
|
extern wiz_NetInfo netInfo;
|
||||||
|
extern uint8_t DNS_2nd[4];
|
||||||
|
|
||||||
|
#define BLYNK_DATA_BUF_SIZE 1024
|
||||||
|
|
||||||
#endif /* GLOBALS_H_ */
|
#endif /* GLOBALS_H_ */
|
||||||
|
|||||||
@@ -19,8 +19,8 @@
|
|||||||
#include "stdbool.h"
|
#include "stdbool.h"
|
||||||
#include "Ethernet/socket.h"
|
#include "Ethernet/socket.h"
|
||||||
#include "Ethernet/wizchip_conf.h"
|
#include "Ethernet/wizchip_conf.h"
|
||||||
#include "Application/loopback/loopback.h"
|
|
||||||
#include "Application/Blynk/blynk.h"
|
#include "Application/Blynk/blynk.h"
|
||||||
|
#include "Internet/DNS/dns.h"
|
||||||
|
|
||||||
#define _MAIN_DEBUG_
|
#define _MAIN_DEBUG_
|
||||||
|
|
||||||
@@ -29,16 +29,33 @@
|
|||||||
//My auth token for my android test application UNO+USB:
|
//My auth token for my android test application UNO+USB:
|
||||||
uint8_t auth[] = "c113f724351444fc872ae586d70b18cd"; // You should get Auth Token in the Blynk App
|
uint8_t auth[] = "c113f724351444fc872ae586d70b18cd"; // You should get Auth Token in the Blynk App
|
||||||
// IP: 139.59.206.133 for <blynk-cloud.com> via WIN7 nslookup - actually need to use DNS resolving
|
// IP: 139.59.206.133 for <blynk-cloud.com> via WIN7 nslookup - actually need to use DNS resolving
|
||||||
uint8_t blynk_server_ip[4] = {139, 59, 206, 133}; // Blynk cloud server IP (cloud.blynk.cc, 8422)
|
//Resolve here via DNS query see below Domain_IP[4]
|
||||||
uint8_t BLYNK_RX_BUF[DATA_BUF_SIZE];
|
//uint8_t blynk_server_ip[4] = {139, 59, 206, 133}; // Blynk cloud server IP (cloud.blynk.cc, 8422)
|
||||||
uint8_t BLYNK_TX_BUF[DATA_BUF_SIZE];
|
//uint8_t BLYNK_RX_BUF[DATA_BUF_SIZE];
|
||||||
|
uint8_t BLYNK_TX_BUF[BLYNK_DATA_BUF_SIZE];
|
||||||
|
|
||||||
//***********BLYNK related: END
|
//***********BLYNK related: END
|
||||||
|
|
||||||
|
//***************** DNS: BEGIN
|
||||||
|
//////////////////////////////////////////////////
|
||||||
|
// Socket & Port number definition for Examples //
|
||||||
|
//////////////////////////////////////////////////
|
||||||
|
#define SOCK_DNS 5
|
||||||
|
|
||||||
|
unsigned char gDATABUF_DNS[512];
|
||||||
|
//#define IP_WORK
|
||||||
|
|
||||||
|
////////////////
|
||||||
|
// DNS client //
|
||||||
|
////////////////
|
||||||
|
uint8_t Domain_name[] = BLYNK_DEFAULT_DOMAIN; // Public russian ntp server - works good via GSM Modem
|
||||||
|
uint8_t Domain_IP[4] = {0, }; // Translated IP address by DNS Server
|
||||||
|
//***************** DNS: END
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* (19)OK (v1.0) Trying port to WIZNET5500 BLYNK IOT app (look: https://blynk.io/)
|
* (19)OK (v1.0) Trying port to WIZNET5500 BLYNK IOT app (look: https://blynk.io/)
|
||||||
* TODO:
|
* TODO:
|
||||||
* Add DNS resolve before BLYNK app running to <blynk-cloud.com>
|
* OK (v1.2) Add DNS resolve before BLYNK app running to <blynk-cloud.com>
|
||||||
* OK (v1.1) Add LED_ON/LED_OFF handle on LED D13 BLYNK Android application
|
* OK (v1.1) Add LED_ON/LED_OFF handle on LED D13 BLYNK Android application
|
||||||
* GPIO OUT - works OK (look ./Application/Blynk/blynkDependency.c digitalWrite(..) && pinMode(..))!
|
* GPIO OUT - works OK (look ./Application/Blynk/blynkDependency.c digitalWrite(..) && pinMode(..))!
|
||||||
* OK Add printout <blynk> server metrics on start-up
|
* OK Add printout <blynk> server metrics on start-up
|
||||||
@@ -83,7 +100,7 @@ volatile unsigned long _millis; // for millis tick !! Overflow every ~49.7 days
|
|||||||
//*********Program metrics
|
//*********Program metrics
|
||||||
const char compile_date[] PROGMEM = __DATE__; // Mmm dd yyyy - Дата компиляции
|
const char compile_date[] PROGMEM = __DATE__; // Mmm dd yyyy - Дата компиляции
|
||||||
const char compile_time[] PROGMEM = __TIME__; // hh:mm:ss - Время компиляции
|
const char compile_time[] PROGMEM = __TIME__; // hh:mm:ss - Время компиляции
|
||||||
const char str_prog_name[] PROGMEM = "\r\nAtMega1284p v1.1 Static IP BLYNK WIZNET_5500 ETHERNET 12/03/2019\r\n"; // Program name
|
const char str_prog_name[] PROGMEM = "\r\nAtMega1284p v1.2 Static IP BLYNK WIZNET_5500 ETHERNET 12/03/2019\r\n"; // Program name
|
||||||
|
|
||||||
#if defined(__AVR_ATmega128__)
|
#if defined(__AVR_ATmega128__)
|
||||||
const char PROGMEM str_mcu[] = "ATmega128"; //CPU is m128
|
const char PROGMEM str_mcu[] = "ATmega128"; //CPU is m128
|
||||||
@@ -231,16 +248,18 @@ uint16_t adc_read(uint8_t channel)
|
|||||||
|
|
||||||
|
|
||||||
//***************** WIZCHIP INIT: BEGIN
|
//***************** WIZCHIP INIT: BEGIN
|
||||||
|
//Shouldn't used here
|
||||||
|
/*
|
||||||
#define SOCK_TCPS 0
|
#define SOCK_TCPS 0
|
||||||
#define SOCK_UDPS 1
|
#define SOCK_UDPS 1
|
||||||
#define PORT_TCPS 5000
|
#define PORT_TCPS 5000
|
||||||
#define PORT_UDPS 3000
|
#define PORT_UDPS 3000
|
||||||
|
|
||||||
#define ETH_MAX_BUF_SIZE 2048
|
#define ETH_MAX_BUF_SIZE 512
|
||||||
|
|
||||||
unsigned char ethBuf0[ETH_MAX_BUF_SIZE];
|
|
||||||
unsigned char ethBuf1[ETH_MAX_BUF_SIZE];
|
|
||||||
|
|
||||||
|
//unsigned char ethBuf0[ETH_MAX_BUF_SIZE];
|
||||||
|
//unsigned char ethBuf1[ETH_MAX_BUF_SIZE];
|
||||||
|
*/
|
||||||
void cs_sel() {
|
void cs_sel() {
|
||||||
SPI_WIZNET_ENABLE();
|
SPI_WIZNET_ENABLE();
|
||||||
}
|
}
|
||||||
@@ -339,10 +358,56 @@ int main()
|
|||||||
IO_LIBRARY_Init(); //After that ping must working
|
IO_LIBRARY_Init(); //After that ping must working
|
||||||
print_network_information();
|
print_network_information();
|
||||||
|
|
||||||
//IOT BLYK app init:
|
|
||||||
/* Blynk client Initialization */
|
/* DNS client Initialization */
|
||||||
PRINTF("Try connect to BLYNK SERVER [%s]: %d.%d.%d.%d:%d..\n\r",BLYNK_DEFAULT_DOMAIN,blynk_server_ip[0],blynk_server_ip[1],blynk_server_ip[2],blynk_server_ip[3],BLYNK_DEFAULT_PORT);
|
PRINTF("> [BLYNK] Target Domain Name : %s\r\n", Domain_name);
|
||||||
blynk_begin(auth, blynk_server_ip, BLYNK_DEFAULT_PORT, BLYNK_TX_BUF, SOCK_BLYNK_CLIENT);
|
DNS_init(SOCK_DNS, gDATABUF_DNS);
|
||||||
|
|
||||||
|
/* DNS processing */
|
||||||
|
int32_t ret;
|
||||||
|
if ((ret = DNS_run(netInfo.dns, Domain_name, Domain_IP)) > 0) // try to 1st DNS
|
||||||
|
{
|
||||||
|
#ifdef _MAIN_DEBUG_
|
||||||
|
PRINTF("> 1st DNS Respond\r\n");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
else if ((ret != -1) && ((ret = DNS_run(DNS_2nd, Domain_name, Domain_IP))>0)) // retry to 2nd DNS
|
||||||
|
{
|
||||||
|
#ifdef _MAIN_DEBUG_
|
||||||
|
PRINTF("> 2nd DNS Respond\r\n");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
else if(ret == -1)
|
||||||
|
{
|
||||||
|
#ifdef _MAIN_DEBUG_
|
||||||
|
PRINTF("> MAX_DOMAIN_NAME is too small. Should be redefined it.\r\n");
|
||||||
|
#endif
|
||||||
|
;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
#ifdef _MAIN_DEBUG_
|
||||||
|
PRINTF("> DNS Failed\r\n");
|
||||||
|
#endif
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(ret > 0)
|
||||||
|
{
|
||||||
|
#ifdef _MAIN_DEBUG_
|
||||||
|
printf("> Translated %s to [%d.%d.%d.%d]\r\n\r\n",Domain_name,Domain_IP[0],Domain_IP[1],Domain_IP[2],Domain_IP[3]);
|
||||||
|
#endif
|
||||||
|
//IOT BLYK app init:
|
||||||
|
/* Blynk client Initialization */
|
||||||
|
PRINTF("Try connect to BLYNK SERVER [%s]: %d.%d.%d.%d:%d..\n\r",Domain_name,Domain_IP[0],Domain_IP[1],Domain_IP[2],Domain_IP[3],BLYNK_DEFAULT_PORT);
|
||||||
|
blynk_begin(auth, Domain_IP, BLYNK_DEFAULT_PORT, BLYNK_TX_BUF, SOCK_BLYNK_CLIENT);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
PRINTF("> [BLYNK] Target Domain Name : %s resolve ERROR\r\nReboot board..\r\n", Domain_name);
|
||||||
|
while(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//Short Blink LED 3 times on startup
|
//Short Blink LED 3 times on startup
|
||||||
@@ -364,12 +429,14 @@ int main()
|
|||||||
{
|
{
|
||||||
//Here at least every 1sec
|
//Here at least every 1sec
|
||||||
wdt_reset(); // WDT reset at least every sec
|
wdt_reset(); // WDT reset at least every sec
|
||||||
|
// Blynk process handler
|
||||||
|
blynk_run();
|
||||||
|
|
||||||
//Use Hercules Terminal to check loopback tcp:5000 and udp:3000
|
//Use Hercules Terminal to check loopback tcp:5000 and udp:3000
|
||||||
/*
|
/*
|
||||||
* https://www.hw-group.com/software/hercules-setup-utility
|
* https://www.hw-group.com/software/hercules-setup-utility
|
||||||
* */
|
* */
|
||||||
loopback_tcps(SOCK_TCPS,ethBuf0,PORT_TCPS);
|
//loopback_tcps(SOCK_TCPS,ethBuf0,PORT_TCPS);
|
||||||
//loopback_udps(SOCK_UDPS,ethBuf0,PORT_UDPS);
|
//loopback_udps(SOCK_UDPS,ethBuf0,PORT_UDPS);
|
||||||
|
|
||||||
//loopback_ret = loopback_tcpc(SOCK_TCPS, gDATABUF, destip, destport);
|
//loopback_ret = loopback_tcpc(SOCK_TCPS, gDATABUF, destip, destport);
|
||||||
@@ -391,8 +458,6 @@ int main()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
// Blynk process handler
|
|
||||||
blynk_run();
|
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user