Task issue fixed. On to the server!

This commit is contained in:
Scott Duensing 2021-11-29 17:42:28 -06:00
parent a4a8534aa6
commit 89a266165f
12 changed files with 89 additions and 86 deletions

View file

@ -167,14 +167,27 @@ static void comModem(uint8_t c) {
// Find port, if any. // Find port, if any.
_address.port = 23; _address.port = 23;
if ((p = strstr(&command[x], ":"))) { if ((p = strstr(&command[x], ":"))) {
p = 0; *p = 0;
p++; p++;
_address.port = atoi(p); _address.port = atoi(p);
} }
enet_address_set_host(&_address, &command[x]); if (enet_address_set_host(&_address, &command[x]) < 0) {
_peer = enet_host_connect(_host, &_address, 1, 0); result = MODEM_RESULT_ERROR;
if (!_peer) result = MODEM_RESULT_ERROR; } else {
result = MODEM_RESULT_OK; _host = enet_host_create(NULL, 1, 1, 0, 0);
if (!_host) {
result = MODEM_RESULT_ERROR;
} else {
_peer = enet_host_connect(_host, &_address, 1, 0);
if (!_peer) {
enet_host_destroy(_host);
_host = NULL;
result = MODEM_RESULT_ERROR;
} else {
result = MODEM_RESULT_OK;
}
}
}
_modemCommandMode = 0; _modemCommandMode = 0;
break; break;
@ -219,6 +232,9 @@ int comOpen(int com, long bps, int dataBits, char parity, int stopBits, int hand
_bufferHead = 0; _bufferHead = 0;
_bufferTail = 0; _bufferTail = 0;
// We really shouldn't do this, but enet is being stupid about not sending a DISCONNECT_TIMEOUT when failing to connect.
_modemCommandMode = 1;
return SER_SUCCESS; return SER_SUCCESS;
} }
@ -436,6 +452,7 @@ static void processNetworkEvent(void) {
break; break;
case ENET_EVENT_TYPE_RECEIVE: case ENET_EVENT_TYPE_RECEIVE:
logWrite("Packet in: %d\n", event->packet->dataLength);
comAddToBuffer((char *)event->packet->data, event->packet->dataLength); comAddToBuffer((char *)event->packet->data, event->packet->dataLength);
break; break;

View file

@ -126,7 +126,7 @@ void taskSettings(void *data) {
if (rc == SER_SUCCESS) { if (rc == SER_SUCCESS) {
snprintf(buffer, 1023, "%s%c", "AT+SOCK1", 13); snprintf(buffer, 1023, "%s%c", "AT+SOCK1", 13);
comWrite(x, buffer, strlen(buffer)); comWrite(x, buffer, strlen(buffer));
// timerQuarterSecondsWait(4); timerQuarterSecondsWait(4);
len = comRead(x, buffer, 1023); len = comRead(x, buffer, 1023);
buffer[len] = 0; buffer[len] = 0;
if (strstr(buffer, "OK")) { if (strstr(buffer, "OK")) {

View file

@ -42,14 +42,16 @@ int comWaitWithTimeout(int com, char *buffer, int len, int quarterSeconds, char
int bufferIndex = 0; int bufferIndex = 0;
char data[2]; char data[2];
// Returns number of bytes read into buffer.
// Value is positive if "expect" was found.
// Value is negative if not found.
while (quarterTicks <= quarterSeconds) { while (quarterTicks <= quarterSeconds) {
r = comRead(com, data, 1); r = comRead(com, data, 1);
if (r == 1) { if (r == 1) {
logWrite("Byte\n");
buffer[bufferIndex++] = data[0]; buffer[bufferIndex++] = data[0];
buffer[bufferIndex] = 0; buffer[bufferIndex] = 0;
if (data[0] == expecting[count]) { if (data[0] == expecting[count]) {
logWrite("Expect\n");
count++; count++;
if (count == (int)strlen(expecting)) { if (count == (int)strlen(expecting)) {
// Found our expect. // Found our expect.
@ -62,12 +64,9 @@ int comWaitWithTimeout(int com, char *buffer, int len, int quarterSeconds, char
// Out of buffer. // Out of buffer.
break; break;
} }
} else {
sprintf(data, "%c", 0);
} }
if (_timerQuarterSecondTick) { if (_timerQuarterSecondTick) {
quarterTicks++; quarterTicks++;
logWrite("Tick\n");
} }
taskYield(); taskYield();
} }

View file

@ -68,6 +68,12 @@ static void taskHandle(mco_coro* coroutine) {
} }
void taskProxy(WidgetT *widget) {
taskFunction task = (taskFunction)guiUserDataGet(widget);
taskCreate(task, NULL);
}
void taskRun(void) { void taskRun(void) {
uint16_t taskIndex = 0; uint16_t taskIndex = 0;

View file

@ -23,9 +23,14 @@
#include "os.h" #include "os.h"
#include "widget.h"
typedef void (*taskFunction)(void *data);
uint8_t taskCreate(void (*function)(void *), void *data); uint8_t taskCreate(void (*function)(void *), void *data);
void taskProxy(WidgetT *widget);
void taskRun(void); void taskRun(void);
void taskShutdown(void); void taskShutdown(void);
void taskStartup(void); void taskStartup(void);

View file

@ -28,18 +28,18 @@
#define TICKS_PER_DAY (SECONDS_IN_DAY * TICKS_PER_SECOND) #define TICKS_PER_DAY (SECONDS_IN_DAY * TICKS_PER_SECOND)
volatile uint8_t _timerQuarterSecondTick = 0; uint8_t _timerQuarterSecondTick = 0;
volatile uint8_t _timerHalfSecondTick = 0; uint8_t _timerHalfSecondTick = 0;
volatile uint8_t _timerSecondTick = 0; uint8_t _timerSecondTick = 0;
volatile uint8_t _timerQuarterSecondOn = 0; uint8_t _timerQuarterSecondOn = 0;
volatile uint8_t _timerHalfSecondOn = 0; uint8_t _timerHalfSecondOn = 0;
volatile uint8_t _timerSecondOn = 0; uint8_t _timerSecondOn = 0;
static volatile long timerLast = 0; static long timerLast = 0;
static volatile uint8_t timerHalfSecond = 2; static uint8_t timerHalfSecond = 2;
static volatile uint8_t timerSecond = 2; static uint8_t timerSecond = 2;
void timerShutdown(void) { void timerShutdown(void) {
@ -98,16 +98,13 @@ void timerUpdate(void) {
} }
/*
void timerQuarterSecondsWait(u_int8_t quarterSeconds) { void timerQuarterSecondsWait(u_int8_t quarterSeconds) {
uint8_t counter = 0; uint8_t counter = 0;
while (counter <= quarterSeconds && !guiHasStopped()) { while (counter <= quarterSeconds && !guiHasStopped()) {
if (_timerQuarterSecondTick) { if (_timerQuarterSecondTick) {
counter++; counter++;
} }
logWrite("Waiting %d of %d\n", counter, quarterSeconds);
taskYield(); taskYield();
} }
} }
*/

View file

@ -25,19 +25,19 @@
#include "os.h" #include "os.h"
extern volatile uint8_t _timerQuarterSecondTick; extern uint8_t _timerQuarterSecondTick;
extern volatile uint8_t _timerHalfSecondTick; extern uint8_t _timerHalfSecondTick;
extern volatile uint8_t _timerSecondTick; extern uint8_t _timerSecondTick;
extern volatile uint8_t _timerQuarterSecondOn; extern uint8_t _timerQuarterSecondOn;
extern volatile uint8_t _timerHalfSecondOn; extern uint8_t _timerHalfSecondOn;
extern volatile uint8_t _timerSecondOn; extern uint8_t _timerSecondOn;
void timerShutdown(void); void timerShutdown(void);
void timerStartup(void); void timerStartup(void);
void timerUpdate(void); void timerUpdate(void);
//void timerQuarterSecondsWait(u_int8_t quarterSeconds); void timerQuarterSecondsWait(u_int8_t quarterSeconds);
#endif // TIMER_H #endif // TIMER_H

View file

@ -43,7 +43,7 @@ static ButtonT *btnSettings = NULL;
static ButtonT *btnConnect = NULL; static ButtonT *btnConnect = NULL;
static void btnConnectClick(WidgetT *widget); static void taskConnectClick(void *data);
static void btnMsgBox(MsgBoxButtonT button); static void btnMsgBox(MsgBoxButtonT button);
static void btnMsgBoxQuit(MsgBoxButtonT button); static void btnMsgBoxQuit(MsgBoxButtonT button);
static void btnQuitClick(WidgetT *widget); static void btnQuitClick(WidgetT *widget);
@ -75,16 +75,11 @@ static void btnMsgBoxQuit(MsgBoxButtonT button) {
} }
static void btnConnectClick(WidgetT *widget) { static void taskConnectClick(void *data) {
uint32_t r; int32_t r;
uint32_t len;
char buffer[1024]; char buffer[1024];
int8_t timeout = 7 * 4; // Seven seconds (of quarter seconds) to connect to server.
uint16_t count = 0;
uint8_t connected = 0;
char *banner = "KPMPGSMKII";
(void)widget; (void)data;
// Ghost all buttons. // Ghost all buttons.
widgetEnableSet(W(btnConnect), 0); widgetEnableSet(W(btnConnect), 0);
@ -102,62 +97,33 @@ static void btnConnectClick(WidgetT *widget) {
msgBoxOne("COM Problem", MSGBOX_ICON_ERROR, "Unable to open COM port!\nPlease check settings.", "Okay", btnMsgBox); msgBoxOne("COM Problem", MSGBOX_ICON_ERROR, "Unable to open COM port!\nPlease check settings.", "Okay", btnMsgBox);
return; return;
} }
logWrite("COM open\n");
// Send a CR to clear anything in the modem. // Send a CR to clear anything in the modem.
snprintf(buffer, 1023, "%c", 13); snprintf(buffer, 1023, "%c", 13);
comWrite(_configData.serialCom - 1, buffer, strlen(buffer)); comWrite(_configData.serialCom - 1, buffer, strlen(buffer));
logWrite("CR sent\n"); // Wait roughly a second for anything.
// Wait roughly a second.
comWaitWithTimeout(_configData.serialCom - 1, buffer, 1023, 4, "weExpectNothing"); comWaitWithTimeout(_configData.serialCom - 1, buffer, 1023, 4, "weExpectNothing");
//uint32_t dumbThing = timerQuarterSecondsWait(4);
logWrite("Wait over\n");
// Flush COM port.
comReceiveBufferFlush(_configData.serialCom - 1);
logWrite("Port flushed\n");
// Send actual init // Send actual init
snprintf(buffer, 1023, "%s%c", "AT+SOCK1", 13); snprintf(buffer, 1023, "%s%c", "AT+SOCK1", 13);
comWrite(_configData.serialCom - 1, buffer, strlen(buffer)); comWrite(_configData.serialCom - 1, buffer, strlen(buffer));
// Wait roughly a second. // Wait roughly a second for "OK".
// timerQuarterSecondsWait(4); r = comWaitWithTimeout(_configData.serialCom - 1, buffer, 1023, 4, "\rOK\r");
// Read COM port & check for OK. if (r <= 0) {
len = comRead(_configData.serialCom - 1, buffer, 1023);
buffer[len] = 0;
if (!strstr(buffer, "OK")) {
comClose(_configData.serialCom - 1); comClose(_configData.serialCom - 1);
msgBoxOne("Modem Problem", MSGBOX_ICON_ERROR, "Modem does not support ENET!\nPlease check settings.", "Okay", btnMsgBox); msgBoxOne("Modem Problem", MSGBOX_ICON_ERROR, "Modem does not support ENET!\nPlease check settings.", "Okay", btnMsgBox);
return; return;
} }
// Flush COM port.
timerQuarterSecondsWait(4);
comReceiveBufferFlush(_configData.serialCom - 1);
// Show dialing, dial service. // Show dialing, dial service.
widgetVisibleSet(W(picDialing), 1); widgetVisibleSet(W(picDialing), 1);
taskYield(); taskYield();
snprintf(buffer, 1023, "ATDT%s:%d%c", _configData.serverHost, _configData.serverPort, 13); snprintf(buffer, 1023, "ATDT%s:%d%c", _configData.serverHost, _configData.serverPort, 13);
comWrite(_configData.serialCom - 1, buffer, strlen(buffer)); comWrite(_configData.serialCom - 1, buffer, strlen(buffer));
while (timeout > 0 && connected == 0) { // Wait 7 seconds for welcome banner.
len = comRead(_configData.serialCom - 1, buffer, 1); r = comWaitWithTimeout(_configData.serialCom - 1, buffer, 1023, 4 * 7, "KPMPGSMKII\r");
// Did we get a character? if (r <= 0) {
if (len == 1) {
// Is it the next we expect in the server banner?
if (buffer[0] == banner[count]) {
count++;
} else {
count = 0;
}
// Are we connected?
if (count == strlen(banner)) {
connected = 1;
}
}
// Tick timeout.
if (_timerQuarterSecondTick) timeout--;
taskYield();
}
// timerQuarterSecondsWait(4);
comReceiveBufferFlush(_configData.serialCom - 1);
// Did we connect?
if (!connected) {
comClose(_configData.serialCom - 1); comClose(_configData.serialCom - 1);
msgBoxOne("No Connection", MSGBOX_ICON_INFORMATION, "Unable to connect to server!\nPlease check settings or try later.", "Okay", btnMsgBox); msgBoxOne("No Connection", MSGBOX_ICON_INFORMATION, "Unable to connect to server!\nPlease check settings or try later.", "Okay", btnMsgBox);
return; return;
@ -242,7 +208,8 @@ void taskWelcome(void *data) {
T_BUTTON, O(btnConnect), T_BUTTON, O(btnConnect),
T_TITLE, P("Connect"), T_TITLE, P("Connect"),
T_X, 379, T_Y, 157, T_X, 379, T_Y, 157,
T_CLICK, P(btnConnectClick), T_CLICK, P(taskProxy),
T_USER_DATA, P(taskConnectClick),
T_ENABLED, (_configData.serialCom > 0 && strlen(_configData.serverHost) > 2) ? T_TRUE : T_FALSE, T_ENABLED, (_configData.serialCom > 0 && strlen(_configData.serverHost) > 2) ? T_TRUE : T_FALSE,
T_BUTTON, T_DONE, T_BUTTON, T_DONE,

View file

@ -20,10 +20,7 @@ TEMPLATE = subdirs
CONFIG *= ORDERED CONFIG *= ORDERED
SUBDIRS = \ SUBDIRS = \
# primes \ client \
server
# font \ # font \
client # primes
# server
HEADERS += \
network.h

View file

@ -22,11 +22,20 @@
#include "client.h" #include "client.h"
#include "network.h" #include "network.h"
#include "console.h"
void *clientThread(void *data) { void *clientThread(void *data) {
ClientThreadT *client = (ClientThreadT *)data; ClientThreadT *client = (ClientThreadT *)data;
ENetPacket *packet = NULL;
int32_t r;
// Send service banner.
packet = enet_packet_create("KPMPGSMKII\r", 11, ENET_PACKET_FLAG_RELIABLE);
r = enet_peer_send(client->peer, 0, packet);
consoleMessageQueue("Packet: %p %d\n", packet, r);
pthread_exit(NULL); pthread_exit(NULL);
} }

View file

@ -53,6 +53,8 @@ void consoleMessageQueue(const char *message, ...) {
pthread_mutex_lock(&_messageQueueMutex); pthread_mutex_lock(&_messageQueueMutex);
arrput(_consoleMessageQueue, newMessage); arrput(_consoleMessageQueue, newMessage);
pthread_mutex_unlock(&_messageQueueMutex); pthread_mutex_unlock(&_messageQueueMutex);
free(newMessage);
} }

View file

@ -149,6 +149,8 @@ static void *serverThread(void *data) {
// Tell the console. // Tell the console.
enet_address_get_host_ip(&event.peer->address, buffer, 2047); enet_address_get_host_ip(&event.peer->address, buffer, 2047);
consoleMessageQueue("%ld: %s disconnected.\n", clientList[i]->threadIndex, buffer); consoleMessageQueue("%ld: %s disconnected.\n", clientList[i]->threadIndex, buffer);
// Delete client.
DEL(clientList[i]);
// Take it out of the client list. // Take it out of the client list.
arrdel(clientList, i); arrdel(clientList, i);
break; break;
@ -166,6 +168,8 @@ static void *serverThread(void *data) {
// Stop client processing. // Stop client processing.
clientList[i]->running = 0; clientList[i]->running = 0;
pthread_join(clientList[i]->threadHandle, &status); pthread_join(clientList[i]->threadHandle, &status);
// Delete client.
DEL(clientList[i]);
} }
arrfree(clientList); arrfree(clientList);