Added DNS resolver to [19_m1284p_WIZNET_blynk],

slightly optimized code. Tested on M644p - OK.
This commit is contained in:
maxxir
2019-03-12 22:12:23 +04:00
parent 271dfb17ce
commit ddef579714
10 changed files with 836 additions and 345 deletions

View File

@@ -18,10 +18,11 @@
#include "blynk.h"
#include "blynkDependency.h"
#include "../globals.h"
//#include "common.h" // When the project has no "common.h" file, this line have to commented out.
#ifndef DATA_BUF_SIZE
#define DATA_BUF_SIZE 2048
#ifndef BLYNK_DATA_BUF_SIZE
#define BLYNK_DATA_BUF_SIZE 2048
#endif
uint8_t blynk_connect(void);
@@ -106,7 +107,7 @@ void blynk_run(void)
#ifdef BLYNK_DEBUG
getSn_DIPR(s, destip);
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
}
@@ -117,7 +118,7 @@ void blynk_run(void)
{
blynk_connected = true;
#ifdef BLYNK_DEBUG
printf("Blynk[%d] : Auth connection complete\r\n", s);
PRINTF("Blynk[%d] : Auth connection complete\r\n", s);
#endif
}
}
@@ -128,9 +129,9 @@ void blynk_run(void)
if (t - lastActivityIn > (1000UL * BLYNK_HEARTBEAT + BLYNK_TIMEOUT_MS*3)) {
#ifdef BLYNK_DEBUG
printf("Heartbeat timeout (last in: %lu)\r\n", lastActivityIn);
PRINTF("Heartbeat timeout (last in: %lu)\r\n", lastActivityIn);
#else
printf("Heartbeat timeout\r\n");
PRINTF("Heartbeat timeout\r\n");
#endif
blynk_connected = 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
#ifdef BLYNK_DEBUG
printf("Heartbeat\r\n");
PRINTF("Heartbeat\r\n");
#endif
sendCmd(BLYNK_CMD_PING, 0, NULL, 0, NULL, 0);
lastHeartbeat = t;
@@ -151,14 +152,14 @@ void blynk_run(void)
case SOCK_CLOSE_WAIT:
#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
disconnect(s);
break;
case SOCK_CLOSED:
#ifdef BLYNK_DEBUG
//printf("> Blynk[%d] : CLOSED\r\n", s);
//PRINTF("> Blynk[%d] : CLOSED\r\n", s);
#endif
blynk_connected = 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 */
{
#ifdef BLYNK_DEBUG
printf("Blynk[%d] : SOCKET OPEN\r\n", s);
PRINTF("Blynk[%d] : SOCKET OPEN\r\n", s);
#endif
}
break;
case SOCK_INIT:
#ifdef BLYNK_DEBUG
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("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);
#endif
connect(s, server_ip, server_port);
break;
@@ -233,15 +234,15 @@ uint8_t blynk_connect(void)
{
if (BLYNK_TIMEOUT == hdr.length)
{
printf("Timeout\r\n");
PRINTF("Timeout\r\n");
}
else if (BLYNK_INVALID_TOKEN == hdr.length)
{
printf("Invalid auth token\r\n");
PRINTF("Invalid auth token\r\n");
}
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
hdr.type = 255;
@@ -272,9 +273,9 @@ uint8_t blynk_connect(void)
deltaCmd = 1000;
#endif
printf("Ready!\r\n");
PRINTF("Ready!\r\n");
#ifdef BLYNK_DEBUG
printf("Roundtrip: %ldms\r\n", lastActivityIn-t);
PRINTF("Roundtrip: %ldms\r\n", lastActivityIn-t);
#endif
return true;
@@ -311,27 +312,27 @@ void processInput(void)
{
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);
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))
{
printf("Can't read body\r\n");
PRINTF("Can't read body\r\n");
return;
}
msgbuf[hdr.length] = '\0'; // Add 1 to zero-terminate
#ifdef BLYNK_DEBUG
printf(">");
PRINTF(">");
for(i = 0; i < hdr.length; i++)
{
if(msgbuf[i] != '\0') printf("%c", msgbuf[i]);
else printf(" ");
if(msgbuf[i] != '\0') PRINTF("%c", msgbuf[i]);
else PRINTF(" ");
}
printf("\r\n");
PRINTF("\r\n");
#endif
currentMsgId = hdr.msg_id;
@@ -340,7 +341,7 @@ void processInput(void)
} break;
default:
printf("Invalid header type: %d\r\n", hdr.type);
PRINTF("Invalid header type: %d\r\n", hdr.type);
disconnect(s);
return;
}
@@ -423,22 +424,22 @@ void processCmd(uint8_t * buff, size_t len)
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, ' ');
sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len, NULL, 0);
}
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, ' ');
sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len, NULL, 0);
}
else if(!strcmp(cmd, "vr")) // virtual pin read
{
#ifdef BLYNK_DEBUG
printf("vr command: Not fully supported yet\r\n");
PRINTF("vr command: Not fully supported yet\r\n");
#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, ' ');
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
{
#ifdef BLYNK_DEBUG
printf("vw command: Not fully supported yet\r\n");
PRINTF("vw command: Not fully supported yet\r\n");
#endif
nexttok = blynkparam_get();
w_param = ATOI((uint8_t *)nexttok, 10);
virtualWrite(pin, w_param);
// 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, ' ');
//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);
} else {
#ifdef BLYNK_DEBUG
printf("Invalid pinMode %u -> %s\r\n", pin, nexttok);
PRINTF("Invalid pinMode %u -> %s\r\n", pin, nexttok);
#endif
}
nexttok = blynkparam_get();
@@ -521,7 +522,7 @@ void processCmd(uint8_t * buff, size_t len)
digitalWrite(pin, w_param ? HIGH : LOW);
// 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, ' ');
//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);
// 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, ' ');
//sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len, NULL, 0);
}
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)
{
#ifdef BLYNK_DEBUG
//printf("recv header len = %d\r\n", len);
//PRINTF("recv header len = %d\r\n", len);
#endif
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->length = ntohs(hdr->length);
#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
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)
{
#ifdef BLYNK_DEBUG
printf("Cmd not sent\r\n");
PRINTF("Cmd not sent\r\n");
#endif
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);
#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
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 (length) {
#ifdef BLYNK_DEBUG
printf("<");
PRINTF("<");
for(i = 0; i < length; i++)
{
if(data[i] != '\0') printf("%c", data[i]);
else printf(" ");
if(data[i] != '\0') PRINTF("%c", data[i]);
else PRINTF(" ");
}
printf("\r\n");
PRINTF("\r\n");
#endif
wlen += (size_t)send(s, (uint8_t *)data, length);
}
if (length2) {
#ifdef BLYNK_DEBUG
printf("<");
PRINTF("<");
for(i = 0; i < length2; i++)
{
if(data2[i] != '\0') printf("%c", data2[i]);
else printf(" ");
if(data2[i] != '\0') PRINTF("%c", data2[i]);
else PRINTF(" ");
}
printf("\r\n");
PRINTF("\r\n");
#endif
wlen += (size_t)send(s, (uint8_t *)data2, 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);
return;
}
@@ -655,7 +656,7 @@ void sendCmd(uint8_t cmd, uint16_t id, uint8_t * data, size_t length, uint8_t *
lastActivityOut = ts;
if (deltaCmd < (1000/BLYNK_MSG_LIMIT)) {
//::delay(5000);
printf("Flood\r\n");
PRINTF("Flood\r\n");
disconnect(s);
}
#else

View File

@@ -33,9 +33,18 @@
#endif
#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 "ST103FRB"
#endif
*/
#define BLYNK_INFO_CPU "ATmega2560"
#endif
#ifndef BLYNK_INFO_CONNECTION
#define BLYNK_INFO_CONNECTION "W5000"

View File

@@ -19,7 +19,7 @@ uint8_t digitalRead(uint8_t pin)
#elif defined WIZNET_W5500_EVB
val = Chip_GPIO_GetPinState(LPC_GPIO, dio_ports[pin], dio_pins[pin]);
#else
printf("digital pin %d read\r\n", pin);
PRINTF("digital pin %d read\r\n", pin);
#endif
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
else if(val == LOW) Chip_GPIO_SetPinState(LPC_GPIO, dio_ports[pin], dio_pins[pin], false); // Low
#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(val == 0)
@@ -56,15 +56,15 @@ uint16_t analogRead(uint8_t pin)
uint16_t val = 0;
if(pin > 14) analog_pin = pin - 14;
#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);
#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;
printf("changed analog_pin = %d\r\n", analog_pin);
PRINTF("changed analog_pin = %d\r\n", analog_pin);
val = get_ADC_val(analog_pin);
#else
printf("analog pin %d read\r\n", analog_pin);
PRINTF("analog pin %d read\r\n", analog_pin);
#endif
return val;
}
@@ -72,11 +72,11 @@ uint16_t analogRead(uint8_t pin)
void analogWrite(uint8_t pin, uint8_t val)
{
#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
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
printf("analog pin %d write val %d\r\n", pin, val);
PRINTF("analog pin %d write val %d\r\n", pin, val);
#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 == OUTPUT) Chip_GPIO_SetPinDIROutput(LPC_GPIO, dio_ports[pin], dio_pins[pin]); // Output
#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))
{
//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
uint16_t virtualRead(uint8_t pin)
{
printf("virtual pin %d read\r\n", pin);
PRINTF("virtual pin %d read\r\n", pin);
return pin;
}
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);
}

View File

@@ -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

View File

@@ -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