570 lines
17 KiB
C
570 lines
17 KiB
C
#include <stdio.h>
|
|
#include "loopback.h"
|
|
#include "socket.h"
|
|
#include "wizchip_conf.h"
|
|
#include "modbusSlave.h"
|
|
#include <string.h>
|
|
#include "slmpMaster.h"
|
|
|
|
#define DBID1_IDX 1000
|
|
#define START1_IDX 1001
|
|
#define SIZE1_IDX 1002
|
|
#define TOMOD_IDX 1003
|
|
#define DBID2_IDX 1005
|
|
#define START2_IDX 1006
|
|
#define SIZE2_IDX 1007
|
|
#define FROMMOD_IDX 1008
|
|
|
|
#if LOOPBACK_MODE == LOOPBACK_MAIN_NOBLCOK
|
|
|
|
int32_t loopback_tcps_modbus(uint8_t sn, uint8_t *buf, uint16_t port, uint16_t *comm_cnt, _Bool *comm) {
|
|
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:
|
|
*comm = 1;
|
|
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);
|
|
}
|
|
|
|
// Don't need to check SOCKERR_BUSY because it doesn't not occur.
|
|
if ((size = getSn_RX_RSR(sn)) > 0) {
|
|
*comm_cnt += 1;
|
|
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.
|
|
|
|
uint8_t sendbuf[1024];
|
|
uint16_t rxbuf_size = handleModbusTcp(buf, sendbuf);
|
|
|
|
if (rxbuf_size > 0) { //当判断到需要发送报文
|
|
// HAL_GPIO_TogglePin(comm_lamp_GPIO_Port, comm_lamp_Pin);
|
|
sentsize = 0;
|
|
|
|
while (rxbuf_size != sentsize) {
|
|
ret = send(sn, sendbuf + sentsize, rxbuf_size - sentsize);
|
|
if (ret < 0) {
|
|
close(sn);
|
|
return ret;
|
|
}
|
|
sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero.
|
|
}
|
|
} else {
|
|
}
|
|
|
|
}
|
|
|
|
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:
|
|
*comm = 0;
|
|
#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;
|
|
}
|
|
|
|
// ******************************* SLMP *******************************
|
|
uint8_t slmp_txbuf[1024], slmp_rxbuf[1024];
|
|
uint16_t read_address, read_size, write_address, write_size, read_idx_slmp = 0, write_idx_slmp = 0, curr_read_idx_slmp, curr_write_idx_slmp;
|
|
|
|
_Bool isReadRx(uint16_t clientrxsize) {
|
|
return clientrxsize == read_size * 2 + 11 && memcmp(slmp_rxbuf, readRespHeader, 7) == 0;
|
|
}
|
|
|
|
void mapReadRx() {
|
|
for (uint8_t i = 0; i < read_size; i++) {
|
|
uint16_t thisValue = slmp_rxbuf[i * 2 + 11] | (slmp_rxbuf[i * 2 + 12] << 8);
|
|
set_register(curr_read_idx_slmp * 20 + 1200 + i, thisValue);
|
|
}
|
|
}
|
|
|
|
_Bool update_read() {
|
|
curr_read_idx_slmp = read_idx_slmp;
|
|
uint16_t slave_id = get_register(writeST[read_idx_slmp].slave_id);
|
|
uint16_t plc_address = get_register(writeST[read_idx_slmp].plc_address);
|
|
uint16_t size = get_register(writeST[read_idx_slmp].size);
|
|
read_idx_slmp = (read_idx_slmp + 1) % 10;
|
|
if (slave_id < 0x01 || slave_id > 0xF7 || size < 0x01 || size > 20)
|
|
return 0;
|
|
read_address = plc_address;
|
|
read_size = size;
|
|
return 1;
|
|
}
|
|
|
|
_Bool update_write() {
|
|
curr_write_idx_slmp = write_idx_slmp;
|
|
uint16_t slave_id = get_register(readST[write_idx_slmp].slave_id);
|
|
uint16_t plc_address = get_register(readST[write_idx_slmp].plc_address);
|
|
uint16_t size = get_register(readST[write_idx_slmp].size);
|
|
write_idx_slmp = (write_idx_slmp + 1) % 10;
|
|
if (slave_id < 0x01 || slave_id > 0xF7 || size < 0x01 || size > 20)
|
|
return 0;
|
|
write_address = plc_address;
|
|
write_size = size;
|
|
return 1;
|
|
}
|
|
int32_t loopback_tcpc_slmp(uint8_t sn, uint8_t *destip, uint16_t destport, uint16_t *comm_cnt, _Bool *comm) {
|
|
int32_t ret = 0;
|
|
|
|
// 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)
|
|
uint8_t sr_status = getSn_SR(sn);
|
|
|
|
switch (sr_status) {
|
|
case SOCK_ESTABLISHED:
|
|
*comm = 1;
|
|
if (getSn_IR(sn) & Sn_IR_CON) // Socket n interrupt register mask; TCP CON interrupt = connection with peer is successful
|
|
{
|
|
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 (slmp_master_status == readySend) {
|
|
uint16_t clientrxsize = getSn_RX_RSR(sn);
|
|
|
|
if (clientrxsize > 0) {
|
|
*comm_cnt += 1;
|
|
ret = recv(sn, slmp_rxbuf, clientrxsize);
|
|
if (ret > 0) {
|
|
if (isReadRx(clientrxsize)) {
|
|
mapReadRx();
|
|
slmp_master_status = slmp_readRxDone;
|
|
} else {
|
|
slmp_master_status = slmp_writeRxDone;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 准备需要发送的报文
|
|
|
|
uint16_t sendbufsize = 0;
|
|
|
|
if (slmpTimeout > 1000 || slmp_master_status != readySend) {
|
|
slmpTimeout = 0;
|
|
switch (slmp_master_status) {
|
|
case slmp_writeRxDone:
|
|
if (update_read()) {
|
|
slmp_master_status = readySend;
|
|
|
|
if (read_size <= 20 && (read_address + read_size) < 8000) {
|
|
sendbufsize = handleSMLP_ReadTX(read_address, read_size);
|
|
memcpy(slmp_txbuf, frame.frameU8, 21);
|
|
}
|
|
}
|
|
|
|
break;
|
|
case slmp_readRxDone:
|
|
if (update_write()) {
|
|
slmp_master_status = readySend;
|
|
|
|
if (write_size <= 20 && (write_address + write_size) < 8000) {
|
|
sendbufsize = handleSLMP_WriteTX(write_address, write_size);
|
|
memcpy(slmp_txbuf, frame.frameU8, 21);
|
|
for (uint8_t i = 0; i < write_size; i++) {
|
|
uint16_t val = get_register(curr_write_idx_slmp * 20 + 1000 + i);
|
|
slmp_txbuf[i * 2 + 21] = val & 0xFF;
|
|
slmp_txbuf[i * 2 + 22] = (val >> 8) & 0xFF;
|
|
}
|
|
}
|
|
}
|
|
|
|
break;
|
|
default:
|
|
slmp_master_status = slmp_writeRxDone;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// 发送该报文
|
|
uint16_t sentsize = 0;
|
|
|
|
while (sendbufsize != sentsize) {
|
|
// Data send process (User's buffer -> Destination through H/W Tx socket buffer)
|
|
ret = send(sn, slmp_txbuf + sentsize, sendbufsize - sentsize);
|
|
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:
|
|
if ((ret = disconnect(sn)) != SOCK_OK)
|
|
return ret;
|
|
break;
|
|
|
|
case SOCK_INIT:
|
|
if ((ret = connect(sn, destip, destport)) != SOCK_OK) {
|
|
return ret; // Try to TCP connect to the TCP server (destination)
|
|
}
|
|
break;
|
|
|
|
case SOCK_CLOSED:
|
|
*comm = 0;
|
|
close(sn);
|
|
if ((ret = socket(sn, Sn_MR_TCP, any_port++, SF_IO_NONBLOCK)) != sn) {
|
|
if (any_port == 0xffff)
|
|
any_port = 50000;
|
|
return ret; // TCP socket open with 'any_port' port number
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
// ******************************** S7 ***********************************
|
|
//
|
|
//void to_big_endian_16(uint16_t value, uint8_t *dest) {
|
|
// dest[0] = (uint8_t) (value >> 8); // 高字节
|
|
// dest[1] = (uint8_t) (value & 0xFF); // 低字节
|
|
//}
|
|
//
|
|
//void to_big_endian_24(uint32_t value, uint8_t *dest) {
|
|
// dest[0] = (uint8_t) (value >> 16); // 最高字节
|
|
// dest[1] = (uint8_t) (value >> 8); // 次高字节
|
|
// dest[2] = (uint8_t) (value & 0xFF); // 最低字节
|
|
//}
|
|
//
|
|
//uint8_t s7_rx_done = 1;
|
|
//uint8_t comm_step = 0;
|
|
//uint8_t read_write_flag = 0;
|
|
//
|
|
//uint8_t sendbuf[1024];
|
|
//uint16_t sendbufsize;
|
|
//uint8_t comm1[] = { 0x03, 0x00, 0x00, 0x16, 0x11, 0xE0, 0x00, 0x00, 0x00, 0x01, 0x00, 0xC0, 0x01, 0x0A, 0xC1, 0x02, 0x01, 0x00, 0xC2, 0x02, 0x03, 0x02 };
|
|
//uint8_t comm2[] = { 0x03, 0x00, 0x00, 0x19, 0x02, 0xF0, 0x80, 0x32, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0xF0, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0xE0 };
|
|
//uint8_t read[] = { 0x03, 0x00, 0x00, 0x1f, 0x02, 0xf0, 0x80, 0x32, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x04, 0x01, 0x12, 0x0A, 0x10, 0x02, 0x00, 0x02, 0x00, 0x01, 0x84, 0x00, 0x00, 0x00 };
|
|
//uint8_t write[] = { 0x03, 0x00, 0x00, 0x29, 0x02, 0xf0, 0x80, 0x32, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x0e, 0x00, 0x0a, 0x05, 0x01, 0x12, 0x0a, 0x10, 0x02, 0x00, 0x06, 0x00, 0x01, 0x84, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x30, 0x00,
|
|
// 0xff, 0x00, 0xff, 0x00, 0xff };
|
|
//uint16_t comm1_size = sizeof(comm1);
|
|
//uint16_t comm2_size = sizeof(comm2);
|
|
//uint16_t read_size = sizeof(read);
|
|
//uint16_t write_size = sizeof(write);
|
|
//
|
|
//typedef union {
|
|
// uint8_t frame[31];
|
|
// struct {
|
|
// uint8_t padding1[23];
|
|
// uint8_t read_size[2];
|
|
// uint8_t db_id[2];
|
|
// uint8_t padding2[1];
|
|
// uint8_t start_address[3];
|
|
//
|
|
// };
|
|
//} s7ReadFrame;
|
|
//
|
|
//typedef union {
|
|
// uint8_t frame[1024];
|
|
// struct {
|
|
// uint8_t padding1[2];
|
|
// uint8_t frame_size[2];
|
|
// uint8_t padding2[11];
|
|
// uint8_t dataframe_size[2];
|
|
// uint8_t padding3[6];
|
|
// uint8_t data_size[2];
|
|
// uint8_t db_id[2];
|
|
// uint8_t padding4[1];
|
|
// uint8_t start_address[3];
|
|
// uint8_t padding5[2];
|
|
// uint8_t databyte_size_t8[2];
|
|
// uint8_t data[800];
|
|
// };
|
|
//} s7WriteFrame;
|
|
//
|
|
//s7ReadFrame readframe = { .frame = { 0x03, 0x00, 0x00, 0x1f, 0x02, 0xf0, 0x80, 0x32, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x04, 0x01, 0x12, 0x0A, 0x10, 0x02, 0x00, 0x02, 0x00, 0x01, 0x84, 0x00, 0x00, 0x00 } };
|
|
//
|
|
//s7WriteFrame writeframe = { .frame = { 0x03, 0x00, 0x00, 0x29, 0x02, 0xf0, 0x80, 0x32, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x0e, 0x00, 0x0a, 0x05, 0x01, 0x12, 0x0a, 0x10, 0x02, 0x00, 0x06, 0x00, 0x01, 0x84, 0x00, 0x00, 0x00, 0x00, 0x04,
|
|
// 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } };
|
|
//
|
|
//uint16_t prepare_write_frame(uint8_t *write, uint16_t writebyte_size) {
|
|
// if (writebyte_size == 0) {
|
|
// return 0;
|
|
// }
|
|
// uint16_t frame_size = 35 + writebyte_size;
|
|
// to_big_endian_16(frame_size, writeframe.frame_size);
|
|
// to_big_endian_16(4 + writebyte_size, writeframe.dataframe_size);
|
|
// to_big_endian_16(writebyte_size, writeframe.data_size);
|
|
// to_big_endian_16(get_register(DBID2_IDX), writeframe.db_id);
|
|
// to_big_endian_24(get_register(START2_IDX) * 8, writeframe.start_address);
|
|
// to_big_endian_16(writebyte_size * 8, writeframe.databyte_size_t8);
|
|
// memcpy(writeframe.data, write, writebyte_size);
|
|
// return frame_size;
|
|
//}
|
|
//
|
|
//void print_list(uint8_t *buf, uint16_t size) {
|
|
// for (uint16_t i = 0; i < size; i++) {
|
|
// printf("0x%x ", buf[i]);
|
|
// }
|
|
// printf("\r\n");
|
|
//}
|
|
//
|
|
//uint8_t write_to_s7[1024] = { 0 };
|
|
//
|
|
//int32_t loopback_tcpc_s7(uint8_t sn, uint8_t *recvbuf, uint8_t *destip, uint16_t destport) {
|
|
// int32_t ret; // return value for SOCK_ERRORs
|
|
// uint16_t sentsize = 0;
|
|
//
|
|
// // 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)
|
|
// uint8_t sr_status = getSn_SR(sn);
|
|
// switch (sr_status) {
|
|
// 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_S7_
|
|
// 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 (s7_rx_done == 0 && client_wait_timeout == 0) {
|
|
// close(sn);
|
|
// connect(sn, destip, destport);
|
|
// }
|
|
//
|
|
// // 原版 s7_rx_done == 1 || client_wait_timeout == 0
|
|
// if (s7_rx_done == 1) {
|
|
// sentsize = 0;
|
|
// if (comm_step == 0) {
|
|
// memcpy(sendbuf, comm1, comm1_size);
|
|
// sendbufsize = comm1_size;
|
|
// } else if (comm_step == 1) {
|
|
// memcpy(sendbuf, comm2, comm2_size);
|
|
// sendbufsize = comm2_size;
|
|
// } else {
|
|
// if (read_write_flag == 0) {
|
|
// read_write_flag = 1;
|
|
// // S7 > MODBUS
|
|
// if (get_register(SIZE1_IDX) == 0) {
|
|
// sendbufsize = 0;
|
|
// } else {
|
|
// to_big_endian_16(get_register(DBID1_IDX), readframe.db_id);
|
|
// to_big_endian_24(get_register(START1_IDX) * 8, readframe.start_address);
|
|
// to_big_endian_16(get_register(SIZE1_IDX) * 2, readframe.read_size);
|
|
// memcpy(sendbuf, readframe.frame, read_size);
|
|
// sendbufsize = read_size;
|
|
// }
|
|
//
|
|
// } else {
|
|
// read_write_flag = 0;
|
|
// // MODBUS > S7
|
|
// get_register_range_to_u8(get_register(FROMMOD_IDX), get_register(SIZE2_IDX), write_to_s7);
|
|
// uint16_t write_size = prepare_write_frame(write_to_s7, (uint16_t) get_register(SIZE2_IDX) * 2);
|
|
// memcpy(sendbuf, writeframe.frame, write_size);
|
|
// sendbufsize = write_size;
|
|
// }
|
|
// }
|
|
//
|
|
// if (sendbufsize != 0) {
|
|
// s7_rx_done = 0;
|
|
// client_wait_timeout = 2000;
|
|
//
|
|
// } else {
|
|
// s7_rx_done = 1;
|
|
// client_wait_timeout = 50;
|
|
// }
|
|
//
|
|
// while (sendbufsize != sentsize) {
|
|
//
|
|
// ret = send(sn, sendbuf + sentsize, sendbufsize - 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.
|
|
// }
|
|
//
|
|
// }
|
|
//
|
|
// if (s7_rx_done == 0) {
|
|
// uint16_t clientrxsize = 0;
|
|
// if ((clientrxsize = getSn_RX_RSR(sn)) > 0) {
|
|
// int16_t ret = recv(sn, recvbuf, clientrxsize);
|
|
// // printf("step %d -> ", comm_step);
|
|
// // print_list(recvbuf, clientrxsize);
|
|
// if (ret > 0) {
|
|
// if (comm_step < 2) {
|
|
// comm_step++;
|
|
// } else {
|
|
// // 处理接收到的数据
|
|
// if (read_write_flag == 1 && clientrxsize >= 27) {
|
|
// uint16_t idx_in_modbus = get_register(TOMOD_IDX);
|
|
// for (uint16_t idx = 25; idx < clientrxsize; idx += 2) {
|
|
// if (get_register(1004) == 10) {
|
|
// set_register(idx_in_modbus++, (recvbuf[idx + 1]) | (recvbuf[idx] << 8));
|
|
// } else {
|
|
// set_register(idx_in_modbus++, (recvbuf[idx]) | (recvbuf[idx + 1] << 8));
|
|
// }
|
|
//
|
|
// }
|
|
// }
|
|
// }
|
|
// s7_rx_done = 1;
|
|
// } else {
|
|
// // 处理错误或断开连接
|
|
// return ret;
|
|
// }
|
|
// }
|
|
// }
|
|
//
|
|
// //////////////////////////////////////////////////////////////////////////////////////////////
|
|
// break;
|
|
//
|
|
// case SOCK_CLOSE_WAIT:
|
|
//#ifdef _LOOPBACK_DEBUG_S7_
|
|
// //printf("%d:CloseWait\r\n",sn);
|
|
//#endif
|
|
// if ((ret = disconnect(sn)) != SOCK_OK)
|
|
// return ret;
|
|
//#ifdef _LOOPBACK_DEBUG_S7_
|
|
// printf("%d:Socket Closed\r\n", sn);
|
|
//#endif
|
|
// break;
|
|
//
|
|
// case SOCK_INIT:
|
|
//#ifdef _LOOPBACK_DEBUG_S7_
|
|
// 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:
|
|
// comm_step = 0;
|
|
// close(sn);
|
|
// if ((ret = socket(sn, Sn_MR_TCP, any_port++, SF_IO_NONBLOCK)) != sn) {
|
|
// if (any_port == 0xffff)
|
|
// any_port = 50000;
|
|
// return ret; // TCP socket open with 'any_port' port number
|
|
// }
|
|
//#ifdef _LOOPBACK_DEBUG_S7_
|
|
// //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
|