diff --git a/client/src/linux/linux.c b/client/src/linux/linux.c index a56bb36..4cedea6 100644 --- a/client/src/linux/linux.c +++ b/client/src/linux/linux.c @@ -167,14 +167,27 @@ static void comModem(uint8_t c) { // Find port, if any. _address.port = 23; if ((p = strstr(&command[x], ":"))) { - p = 0; + *p = 0; p++; _address.port = atoi(p); } - enet_address_set_host(&_address, &command[x]); - _peer = enet_host_connect(_host, &_address, 1, 0); - if (!_peer) result = MODEM_RESULT_ERROR; - result = MODEM_RESULT_OK; + if (enet_address_set_host(&_address, &command[x]) < 0) { + result = MODEM_RESULT_ERROR; + } else { + _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; break; @@ -219,6 +232,9 @@ int comOpen(int com, long bps, int dataBits, char parity, int stopBits, int hand _bufferHead = 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; } @@ -436,6 +452,7 @@ static void processNetworkEvent(void) { break; case ENET_EVENT_TYPE_RECEIVE: + logWrite("Packet in: %d\n", event->packet->dataLength); comAddToBuffer((char *)event->packet->data, event->packet->dataLength); break; diff --git a/client/src/settings.c b/client/src/settings.c index a253849..526bc10 100644 --- a/client/src/settings.c +++ b/client/src/settings.c @@ -126,7 +126,7 @@ void taskSettings(void *data) { if (rc == SER_SUCCESS) { snprintf(buffer, 1023, "%s%c", "AT+SOCK1", 13); comWrite(x, buffer, strlen(buffer)); -// timerQuarterSecondsWait(4); + timerQuarterSecondsWait(4); len = comRead(x, buffer, 1023); buffer[len] = 0; if (strstr(buffer, "OK")) { diff --git a/client/src/system/comport.c b/client/src/system/comport.c index 58de140..1376ab9 100644 --- a/client/src/system/comport.c +++ b/client/src/system/comport.c @@ -42,14 +42,16 @@ int comWaitWithTimeout(int com, char *buffer, int len, int quarterSeconds, char int bufferIndex = 0; 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) { r = comRead(com, data, 1); if (r == 1) { - logWrite("Byte\n"); buffer[bufferIndex++] = data[0]; buffer[bufferIndex] = 0; if (data[0] == expecting[count]) { - logWrite("Expect\n"); count++; if (count == (int)strlen(expecting)) { // Found our expect. @@ -62,12 +64,9 @@ int comWaitWithTimeout(int com, char *buffer, int len, int quarterSeconds, char // Out of buffer. break; } - } else { - sprintf(data, "%c", 0); } if (_timerQuarterSecondTick) { quarterTicks++; - logWrite("Tick\n"); } taskYield(); } diff --git a/client/src/system/task.c b/client/src/system/task.c index 76f7cc5..5011d2f 100644 --- a/client/src/system/task.c +++ b/client/src/system/task.c @@ -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) { uint16_t taskIndex = 0; diff --git a/client/src/system/task.h b/client/src/system/task.h index 2b9c776..fbe64fc 100644 --- a/client/src/system/task.h +++ b/client/src/system/task.h @@ -23,9 +23,14 @@ #include "os.h" +#include "widget.h" + + +typedef void (*taskFunction)(void *data); uint8_t taskCreate(void (*function)(void *), void *data); +void taskProxy(WidgetT *widget); void taskRun(void); void taskShutdown(void); void taskStartup(void); diff --git a/client/src/system/timer.c b/client/src/system/timer.c index 00f3c40..f589257 100644 --- a/client/src/system/timer.c +++ b/client/src/system/timer.c @@ -28,18 +28,18 @@ #define TICKS_PER_DAY (SECONDS_IN_DAY * TICKS_PER_SECOND) -volatile uint8_t _timerQuarterSecondTick = 0; -volatile uint8_t _timerHalfSecondTick = 0; -volatile uint8_t _timerSecondTick = 0; +uint8_t _timerQuarterSecondTick = 0; +uint8_t _timerHalfSecondTick = 0; +uint8_t _timerSecondTick = 0; -volatile uint8_t _timerQuarterSecondOn = 0; -volatile uint8_t _timerHalfSecondOn = 0; -volatile uint8_t _timerSecondOn = 0; +uint8_t _timerQuarterSecondOn = 0; +uint8_t _timerHalfSecondOn = 0; +uint8_t _timerSecondOn = 0; -static volatile long timerLast = 0; -static volatile uint8_t timerHalfSecond = 2; -static volatile uint8_t timerSecond = 2; +static long timerLast = 0; +static uint8_t timerHalfSecond = 2; +static uint8_t timerSecond = 2; void timerShutdown(void) { @@ -98,16 +98,13 @@ void timerUpdate(void) { } -/* void timerQuarterSecondsWait(u_int8_t quarterSeconds) { - uint8_t counter = 0; + uint8_t counter = 0; while (counter <= quarterSeconds && !guiHasStopped()) { if (_timerQuarterSecondTick) { counter++; } - logWrite("Waiting %d of %d\n", counter, quarterSeconds); taskYield(); } } -*/ diff --git a/client/src/system/timer.h b/client/src/system/timer.h index 0740077..de2ca49 100644 --- a/client/src/system/timer.h +++ b/client/src/system/timer.h @@ -25,19 +25,19 @@ #include "os.h" -extern volatile uint8_t _timerQuarterSecondTick; -extern volatile uint8_t _timerHalfSecondTick; -extern volatile uint8_t _timerSecondTick; +extern uint8_t _timerQuarterSecondTick; +extern uint8_t _timerHalfSecondTick; +extern uint8_t _timerSecondTick; -extern volatile uint8_t _timerQuarterSecondOn; -extern volatile uint8_t _timerHalfSecondOn; -extern volatile uint8_t _timerSecondOn; +extern uint8_t _timerQuarterSecondOn; +extern uint8_t _timerHalfSecondOn; +extern uint8_t _timerSecondOn; void timerShutdown(void); void timerStartup(void); void timerUpdate(void); -//void timerQuarterSecondsWait(u_int8_t quarterSeconds); +void timerQuarterSecondsWait(u_int8_t quarterSeconds); #endif // TIMER_H diff --git a/client/src/welcome.c b/client/src/welcome.c index 352be0d..e001e2b 100644 --- a/client/src/welcome.c +++ b/client/src/welcome.c @@ -43,7 +43,7 @@ static ButtonT *btnSettings = NULL; static ButtonT *btnConnect = NULL; -static void btnConnectClick(WidgetT *widget); +static void taskConnectClick(void *data); static void btnMsgBox(MsgBoxButtonT button); static void btnMsgBoxQuit(MsgBoxButtonT button); static void btnQuitClick(WidgetT *widget); @@ -75,16 +75,11 @@ static void btnMsgBoxQuit(MsgBoxButtonT button) { } -static void btnConnectClick(WidgetT *widget) { - uint32_t r; - uint32_t len; +static void taskConnectClick(void *data) { + int32_t r; 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. 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); return; } - logWrite("COM open\n"); - // Send a CR to clear anything in the modem. snprintf(buffer, 1023, "%c", 13); comWrite(_configData.serialCom - 1, buffer, strlen(buffer)); - logWrite("CR sent\n"); - // Wait roughly a second. + // Wait roughly a second for anything. 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 snprintf(buffer, 1023, "%s%c", "AT+SOCK1", 13); comWrite(_configData.serialCom - 1, buffer, strlen(buffer)); - // Wait roughly a second. -// timerQuarterSecondsWait(4); - // Read COM port & check for OK. - len = comRead(_configData.serialCom - 1, buffer, 1023); - buffer[len] = 0; - if (!strstr(buffer, "OK")) { + // Wait roughly a second for "OK". + r = comWaitWithTimeout(_configData.serialCom - 1, buffer, 1023, 4, "\rOK\r"); + if (r <= 0) { comClose(_configData.serialCom - 1); msgBoxOne("Modem Problem", MSGBOX_ICON_ERROR, "Modem does not support ENET!\nPlease check settings.", "Okay", btnMsgBox); return; } + // Flush COM port. + timerQuarterSecondsWait(4); + comReceiveBufferFlush(_configData.serialCom - 1); // Show dialing, dial service. widgetVisibleSet(W(picDialing), 1); taskYield(); snprintf(buffer, 1023, "ATDT%s:%d%c", _configData.serverHost, _configData.serverPort, 13); comWrite(_configData.serialCom - 1, buffer, strlen(buffer)); - while (timeout > 0 && connected == 0) { - len = comRead(_configData.serialCom - 1, buffer, 1); - // Did we get a character? - 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) { + // Wait 7 seconds for welcome banner. + r = comWaitWithTimeout(_configData.serialCom - 1, buffer, 1023, 4 * 7, "KPMPGSMKII\r"); + if (r <= 0) { comClose(_configData.serialCom - 1); msgBoxOne("No Connection", MSGBOX_ICON_INFORMATION, "Unable to connect to server!\nPlease check settings or try later.", "Okay", btnMsgBox); return; @@ -242,7 +208,8 @@ void taskWelcome(void *data) { T_BUTTON, O(btnConnect), T_TITLE, P("Connect"), 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_BUTTON, T_DONE, diff --git a/kpmpgsmkii.pro b/kpmpgsmkii.pro index f79fd07..00ff9fd 100644 --- a/kpmpgsmkii.pro +++ b/kpmpgsmkii.pro @@ -20,10 +20,7 @@ TEMPLATE = subdirs CONFIG *= ORDERED SUBDIRS = \ -# primes \ + client \ + server # font \ - client -# server - -HEADERS += \ - network.h +# primes diff --git a/server/src/client.c b/server/src/client.c index 1c63b26..a21ad02 100644 --- a/server/src/client.c +++ b/server/src/client.c @@ -22,11 +22,20 @@ #include "client.h" #include "network.h" +#include "console.h" void *clientThread(void *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); } diff --git a/server/src/console.c b/server/src/console.c index 3e7a4f8..d852c5b 100644 --- a/server/src/console.c +++ b/server/src/console.c @@ -53,6 +53,8 @@ void consoleMessageQueue(const char *message, ...) { pthread_mutex_lock(&_messageQueueMutex); arrput(_consoleMessageQueue, newMessage); pthread_mutex_unlock(&_messageQueueMutex); + + free(newMessage); } diff --git a/server/src/main.c b/server/src/main.c index 97d1d8b..24f99ba 100644 --- a/server/src/main.c +++ b/server/src/main.c @@ -149,6 +149,8 @@ static void *serverThread(void *data) { // Tell the console. enet_address_get_host_ip(&event.peer->address, buffer, 2047); consoleMessageQueue("%ld: %s disconnected.\n", clientList[i]->threadIndex, buffer); + // Delete client. + DEL(clientList[i]); // Take it out of the client list. arrdel(clientList, i); break; @@ -166,6 +168,8 @@ static void *serverThread(void *data) { // Stop client processing. clientList[i]->running = 0; pthread_join(clientList[i]->threadHandle, &status); + // Delete client. + DEL(clientList[i]); } arrfree(clientList);