More TCP/IP related changes
This commit is contained in:
@@ -456,32 +456,35 @@ modem_enter_idle_state(modem_t* modem)
|
|||||||
modem->tcpIpConnInProgress = 0;
|
modem->tcpIpConnInProgress = 0;
|
||||||
modem->tcpIpConnCounter = 0;
|
modem->tcpIpConnCounter = 0;
|
||||||
|
|
||||||
if (modem->waitingclientsocket != -1)
|
if (modem->waitingclientsocket != (SOCKET)-1)
|
||||||
plat_netsocket_close(modem->waitingclientsocket);
|
plat_netsocket_close(modem->waitingclientsocket);
|
||||||
|
|
||||||
if (modem->clientsocket != -1)
|
if (modem->clientsocket != (SOCKET)-1)
|
||||||
plat_netsocket_close(modem->clientsocket);
|
plat_netsocket_close(modem->clientsocket);
|
||||||
|
|
||||||
modem->clientsocket = modem->waitingclientsocket = -1;
|
modem->clientsocket = modem->waitingclientsocket = (SOCKET)-1;
|
||||||
if (modem->serversocket != -1) {
|
if (modem->serversocket != (SOCKET)-1) {
|
||||||
modem->waitingclientsocket = plat_netsocket_accept(modem->serversocket);
|
modem->waitingclientsocket = plat_netsocket_accept(modem->serversocket);
|
||||||
while (modem->waitingclientsocket != -1) {
|
while (modem->waitingclientsocket != (SOCKET)-1) {
|
||||||
plat_netsocket_close(modem->waitingclientsocket);
|
plat_netsocket_close(modem->waitingclientsocket);
|
||||||
modem->waitingclientsocket = plat_netsocket_accept(modem->serversocket);
|
modem->waitingclientsocket = plat_netsocket_accept(modem->serversocket);
|
||||||
}
|
}
|
||||||
plat_netsocket_close(modem->serversocket);
|
plat_netsocket_close(modem->serversocket);
|
||||||
modem->serversocket = -1;
|
modem->serversocket = (SOCKET)-1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (modem->waitingclientsocket != -1)
|
if (modem->waitingclientsocket != (SOCKET)-1)
|
||||||
plat_netsocket_close(modem->waitingclientsocket);
|
plat_netsocket_close(modem->waitingclientsocket);
|
||||||
|
|
||||||
modem->waitingclientsocket = -1;
|
modem->waitingclientsocket = (SOCKET)-1;
|
||||||
modem->tcpIpMode = false;
|
modem->tcpIpMode = false;
|
||||||
modem->tcpIpConnInProgress = false;
|
modem->tcpIpConnInProgress = false;
|
||||||
|
|
||||||
if (modem->listen_port) {
|
if (modem->listen_port) {
|
||||||
modem->serversocket = plat_netsocket_create_server(NET_SOCKET_TCP, modem->listen_port);
|
modem->serversocket = plat_netsocket_create_server(NET_SOCKET_TCP, modem->listen_port);
|
||||||
|
if (modem->serversocket == (SOCKET)-1) {
|
||||||
|
pclog("Failed to set up server on port %d\n", modem->listen_port);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
serial_set_cts(modem->serial, 1);
|
serial_set_cts(modem->serial, 1);
|
||||||
@@ -560,12 +563,18 @@ modem_dial(modem_t* modem, const char* str)
|
|||||||
|
|
||||||
modem->clientsocket = plat_netsocket_create(NET_SOCKET_TCP);
|
modem->clientsocket = plat_netsocket_create(NET_SOCKET_TCP);
|
||||||
if (modem->clientsocket == -1) {
|
if (modem->clientsocket == -1) {
|
||||||
|
pclog("Failed to create client socket\n");
|
||||||
modem_send_res(modem, ResNOCARRIER);
|
modem_send_res(modem, ResNOCARRIER);
|
||||||
modem_enter_idle_state(modem);
|
modem_enter_idle_state(modem);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
plat_netsocket_connect(modem->clientsocket, buf, port);
|
if (-1 == plat_netsocket_connect(modem->clientsocket, buf, port)) {
|
||||||
|
pclog("Failed to connect to %s\n", buf);
|
||||||
|
modem_send_res(modem, ResNOCARRIER);
|
||||||
|
modem_enter_idle_state(modem);
|
||||||
|
return;
|
||||||
|
}
|
||||||
modem->tcpIpConnInProgress = 1;
|
modem->tcpIpConnInProgress = 1;
|
||||||
modem->tcpIpConnCounter = 0;
|
modem->tcpIpConnCounter = 0;
|
||||||
}
|
}
|
||||||
@@ -724,6 +733,7 @@ modem_do_command(modem_t* modem)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
modem_dial(modem, foundstr);
|
modem_dial(modem, foundstr);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
case 'I': // Some strings about firmware
|
case 'I': // Some strings about firmware
|
||||||
switch (modem_scan_number(&scanbuf)) {
|
switch (modem_scan_number(&scanbuf)) {
|
||||||
@@ -1023,120 +1033,125 @@ modem_accept_incoming_call(modem_t* modem)
|
|||||||
static void
|
static void
|
||||||
modem_cmdpause_timer_callback(void *priv)
|
modem_cmdpause_timer_callback(void *priv)
|
||||||
{
|
{
|
||||||
modem_t *dev = (modem_t *) priv;
|
modem_t *modem = (modem_t *) priv;
|
||||||
uint32_t guard_threashold = 0;
|
uint32_t guard_threashold = 0;
|
||||||
|
timer_on_auto(&modem->cmdpause_timer, 1000);
|
||||||
|
|
||||||
if (dev->tcpIpConnInProgress) {
|
if (modem->tcpIpConnInProgress) {
|
||||||
int status = plat_netsocket_connected(dev->clientsocket);
|
do {
|
||||||
|
int status = plat_netsocket_connected(modem->clientsocket);
|
||||||
|
|
||||||
if (status == -1) {
|
if (status == -1) {
|
||||||
plat_netsocket_close(dev->clientsocket);
|
plat_netsocket_close(modem->clientsocket);
|
||||||
dev->clientsocket = -1;
|
modem->clientsocket = -1;
|
||||||
modem_enter_idle_state(dev);
|
modem_enter_idle_state(modem);
|
||||||
modem_send_res(dev, ResNOCARRIER);
|
modem_send_res(modem, ResNOCARRIER);
|
||||||
dev->tcpIpConnInProgress = 0;
|
modem->tcpIpConnInProgress = 0;
|
||||||
} else if (status == 1) {
|
break;
|
||||||
modem_enter_connected_state(dev);
|
} else if (status == 1) {
|
||||||
dev->tcpIpConnInProgress = 0;
|
modem_enter_connected_state(modem);
|
||||||
}
|
modem->tcpIpConnInProgress = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
dev->tcpIpConnCounter++;
|
modem->tcpIpConnCounter++;
|
||||||
|
|
||||||
if (status <= 0 && dev->tcpIpConnCounter >= 5000) {
|
if (status < 0 || (status == 0 && modem->tcpIpConnCounter >= 5000)) {
|
||||||
plat_netsocket_close(dev->clientsocket);
|
plat_netsocket_close(modem->clientsocket);
|
||||||
dev->clientsocket = -1;
|
modem->clientsocket = -1;
|
||||||
modem_enter_idle_state(dev);
|
modem_enter_idle_state(modem);
|
||||||
modem_send_res(dev, ResNOANSWER);
|
modem_send_res(modem, ResNOANSWER);
|
||||||
dev->tcpIpConnInProgress = 0;
|
modem->tcpIpConnInProgress = 0;
|
||||||
dev->tcpIpMode = 0;
|
modem->tcpIpMode = 0;
|
||||||
}
|
break;
|
||||||
|
}
|
||||||
|
} while (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!dev->connected && dev->waitingclientsocket == -1 && dev->serversocket != -1) {
|
if (!modem->connected && modem->waitingclientsocket == -1 && modem->serversocket != -1) {
|
||||||
dev->waitingclientsocket = plat_netsocket_accept(dev->serversocket);
|
modem->waitingclientsocket = plat_netsocket_accept(modem->serversocket);
|
||||||
if (dev->waitingclientsocket != -1) {
|
if (modem->waitingclientsocket != -1) {
|
||||||
if (!(dev->serial->mctrl & 1) && dev->dtrmode != 0) {
|
if (!(modem->serial->mctrl & 1) && modem->dtrmode != 0) {
|
||||||
modem_enter_idle_state(dev);
|
modem_enter_idle_state(modem);
|
||||||
} else {
|
} else {
|
||||||
dev->ringing = true;
|
modem->ringing = true;
|
||||||
modem_send_res(dev, ResRING);
|
modem_send_res(modem, ResRING);
|
||||||
serial_set_ri(dev->serial, !serial_get_ri(dev->serial));
|
serial_set_ri(modem->serial, !serial_get_ri(modem->serial));
|
||||||
dev->ringtimer = 3000;
|
modem->ringtimer = 3000;
|
||||||
dev->reg[MREG_RING_COUNT] = 0;
|
modem->reg[MREG_RING_COUNT] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (dev->ringing) {
|
if (modem->ringing) {
|
||||||
if (dev->ringtimer <= 0) {
|
if (modem->ringtimer <= 0) {
|
||||||
dev->reg[MREG_RING_COUNT]++;
|
modem->reg[MREG_RING_COUNT]++;
|
||||||
if ((dev->reg[MREG_AUTOANSWER_COUNT] > 0) &&
|
if ((modem->reg[MREG_AUTOANSWER_COUNT] > 0) &&
|
||||||
(dev->reg[MREG_RING_COUNT] >= dev->reg[MREG_AUTOANSWER_COUNT])) {
|
(modem->reg[MREG_RING_COUNT] >= modem->reg[MREG_AUTOANSWER_COUNT])) {
|
||||||
modem_accept_incoming_call(dev);
|
modem_accept_incoming_call(modem);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
modem_send_res(dev, ResRING);
|
modem_send_res(modem, ResRING);
|
||||||
serial_set_ri(dev->serial, !serial_get_ri(dev->serial));
|
serial_set_ri(modem->serial, !serial_get_ri(modem->serial));
|
||||||
|
|
||||||
//MIXER_Enable(mhd.chan,true);
|
modem->ringtimer = 3000;
|
||||||
dev->ringtimer = 3000;
|
|
||||||
}
|
}
|
||||||
--dev->ringtimer;
|
--modem->ringtimer;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dev->in_warmup) {
|
if (modem->in_warmup) {
|
||||||
dev->in_warmup--;
|
modem->in_warmup--;
|
||||||
if (dev->in_warmup == 0) {
|
if (modem->in_warmup == 0) {
|
||||||
dev->tx_count = 0;
|
modem->tx_count = 0;
|
||||||
fifo8_reset(&dev->rx_data);
|
fifo8_reset(&modem->rx_data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (dev->connected && dev->tcpIpMode) {
|
else if (modem->connected && modem->tcpIpMode) {
|
||||||
if (dev->tx_count) {
|
if (modem->tx_count) {
|
||||||
int wouldblock = 0;
|
int wouldblock = 0;
|
||||||
int res = plat_netsocket_send(dev->clientsocket, dev->tx_pkt_ser_line, dev->tx_count, &wouldblock);
|
int res = plat_netsocket_send(modem->clientsocket, modem->tx_pkt_ser_line, modem->tx_count, &wouldblock);
|
||||||
|
|
||||||
if (res <= 0 && !wouldblock) {
|
if (res <= 0 && !wouldblock) {
|
||||||
/* No bytes sent or error. */
|
/* No bytes sent or error. */
|
||||||
dev->tx_count = 0;
|
modem->tx_count = 0;
|
||||||
modem_enter_idle_state(dev);
|
modem_enter_idle_state(modem);
|
||||||
modem_send_res(dev, ResNOCARRIER);
|
modem_send_res(modem, ResNOCARRIER);
|
||||||
} else if (res > 0) {
|
} else if (res > 0) {
|
||||||
if (res == dev->tx_count) {
|
if (res == modem->tx_count) {
|
||||||
dev->tx_count = 0;
|
modem->tx_count = 0;
|
||||||
} else {
|
} else {
|
||||||
memmove(dev->tx_pkt_ser_line, &dev->tx_pkt_ser_line[res], dev->tx_count - res);
|
memmove(modem->tx_pkt_ser_line, &modem->tx_pkt_ser_line[res], modem->tx_count - res);
|
||||||
dev->tx_count -= res;
|
modem->tx_count -= res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (dev->connected) {
|
if (modem->connected) {
|
||||||
uint8_t buffer[16];
|
uint8_t buffer[16];
|
||||||
int wouldblock = 0;
|
int wouldblock = 0;
|
||||||
int res = plat_netsocket_receive(dev->clientsocket, buffer, sizeof(buffer), &wouldblock);
|
int res = plat_netsocket_receive(modem->clientsocket, buffer, sizeof(buffer), &wouldblock);
|
||||||
|
|
||||||
if (res > 0) {
|
if (res > 0) {
|
||||||
fifo8_push_all(&dev->rx_data, buffer, res);
|
fifo8_push_all(&modem->rx_data, buffer, res);
|
||||||
} else if (res == 0) {
|
} else if (res == 0) {
|
||||||
dev->tx_count = 0;
|
modem->tx_count = 0;
|
||||||
modem_enter_idle_state(dev);
|
modem_enter_idle_state(modem);
|
||||||
modem_send_res(dev, ResNOCARRIER);
|
modem_send_res(modem, ResNOCARRIER);
|
||||||
} else if (!wouldblock) {
|
} else if (!wouldblock) {
|
||||||
dev->tx_count = 0;
|
modem->tx_count = 0;
|
||||||
modem_enter_idle_state(dev);
|
modem_enter_idle_state(modem);
|
||||||
modem_send_res(dev, ResNOCARRIER);
|
modem_send_res(modem, ResNOCARRIER);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dev->cmdpause++;
|
modem->cmdpause++;
|
||||||
guard_threashold = (uint32_t)(dev->reg[MREG_GUARD_TIME] * 20);
|
guard_threashold = (uint32_t)(modem->reg[MREG_GUARD_TIME] * 20);
|
||||||
if (dev->cmdpause > guard_threashold) {
|
if (modem->cmdpause > guard_threashold) {
|
||||||
if (dev->plusinc == 0) {
|
if (modem->plusinc == 0) {
|
||||||
dev->plusinc = 1;
|
modem->plusinc = 1;
|
||||||
} else if (dev->plusinc == 4) {
|
} else if (modem->plusinc == 4) {
|
||||||
dev->mode = MODEM_MODE_COMMAND;
|
modem->mode = MODEM_MODE_COMMAND;
|
||||||
modem_send_res(dev, ResOK);
|
modem_send_res(modem, ResOK);
|
||||||
dev->plusinc = 0;
|
modem->plusinc = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1232,12 +1247,13 @@ static const device_config_t modem_config[] = {
|
|||||||
{
|
{
|
||||||
.name = "listen_port",
|
.name = "listen_port",
|
||||||
.description = "TCP/IP listening port",
|
.description = "TCP/IP listening port",
|
||||||
.type = CONFIG_INT,
|
.type = CONFIG_SPINNER,
|
||||||
.default_string = "",
|
.spinner =
|
||||||
.default_int = 5000,
|
{
|
||||||
.file_filter = NULL,
|
.min = 0,
|
||||||
.spinner = { 0 },
|
.max = 32767
|
||||||
.selection = {}
|
},
|
||||||
|
.default_int = 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "phonebook_file",
|
.name = "phonebook_file",
|
||||||
|
@@ -155,7 +155,7 @@ int plat_netsocket_connect(SOCKET socket, const char* hostname, unsigned short p
|
|||||||
if (res == -1) {
|
if (res == -1) {
|
||||||
int error = errno;
|
int error = errno;
|
||||||
|
|
||||||
if (error == EISCONN)
|
if (error == EISCONN || error == EWOULDBLOCK || error == EAGAIN || error == EINPROGRESS)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
res = -1;
|
res = -1;
|
||||||
|
@@ -17,6 +17,7 @@
|
|||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include <ws2tcpip.h>
|
#include <ws2tcpip.h>
|
||||||
|
#include <winerror.h>
|
||||||
|
|
||||||
SOCKET plat_netsocket_create(int type)
|
SOCKET plat_netsocket_create(int type)
|
||||||
{
|
{
|
||||||
@@ -88,23 +89,31 @@ int plat_netsocket_connected(SOCKET socket)
|
|||||||
{
|
{
|
||||||
struct sockaddr addr;
|
struct sockaddr addr;
|
||||||
socklen_t len = sizeof(struct sockaddr);
|
socklen_t len = sizeof(struct sockaddr);
|
||||||
fd_set wrfds;
|
fd_set wrfds, exfds;
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
int res = SOCKET_ERROR;
|
int res = SOCKET_ERROR;
|
||||||
int status = 0;
|
int status = 0;
|
||||||
int optlen = 4;
|
int optlen = 4;
|
||||||
|
|
||||||
FD_ZERO(&wrfds);
|
FD_ZERO(&wrfds);
|
||||||
|
FD_ZERO(&exfds);
|
||||||
FD_SET(socket, &wrfds);
|
FD_SET(socket, &wrfds);
|
||||||
|
FD_SET(socket, &exfds);
|
||||||
|
|
||||||
tv.tv_sec = 0;
|
tv.tv_sec = 0;
|
||||||
tv.tv_usec = 0;
|
tv.tv_usec = 0;
|
||||||
|
|
||||||
res = select(1, NULL, &wrfds, NULL, &tv);
|
res = select(socket + 1, NULL, &wrfds, &exfds, &tv);
|
||||||
|
|
||||||
if (res == SOCKET_ERROR)
|
if (res == SOCKET_ERROR)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
if (res >= 1 && FD_ISSET(socket, &exfds)) {
|
||||||
|
res = getsockopt(socket, SOL_SOCKET, SO_ERROR, (char*)&status, &optlen);
|
||||||
|
pclog("Socket error %d\n", status);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
if (res == 0 || !(res >= 1 && FD_ISSET(socket, &wrfds)))
|
if (res == 0 || !(res >= 1 && FD_ISSET(socket, &wrfds)))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
@@ -147,7 +156,7 @@ int plat_netsocket_connect(SOCKET socket, const char* hostname, unsigned short p
|
|||||||
if (res == SOCKET_ERROR) {
|
if (res == SOCKET_ERROR) {
|
||||||
int error = WSAGetLastError();
|
int error = WSAGetLastError();
|
||||||
|
|
||||||
if (error == WSAEISCONN)
|
if (error == WSAEISCONN || error == WSAEWOULDBLOCK)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
res = -1;
|
res = -1;
|
||||||
|
Reference in New Issue
Block a user