From fbbf1c65d053195339b828b76443856eb66c908e Mon Sep 17 00:00:00 2001 From: Shawn Date: Sun, 7 Sep 2025 16:00:24 -0700 Subject: [PATCH 01/12] Lynx WIP code --- lib/bus/comlynx/comlynx.cpp | 81 ++++++- lib/bus/comlynx/comlynx.h | 9 +- lib/device/comlynx/fuji.cpp | 70 +++--- lib/device/comlynx/fuji.h | 5 +- lib/device/comlynx/network.cpp | 1 + lib/device/comlynx/udpstream.cpp | 234 +++++++++++++++++++-- lib/device/comlynx/udpstream.h | 14 ++ lib/hardware/fnWiFi.cpp | 1 + platformio-ini-files/platformio.common.ini | 6 +- 9 files changed, 370 insertions(+), 51 deletions(-) diff --git a/lib/bus/comlynx/comlynx.cpp b/lib/bus/comlynx/comlynx.cpp index 7ddf9473e..7c89d443c 100755 --- a/lib/bus/comlynx/comlynx.cpp +++ b/lib/bus/comlynx/comlynx.cpp @@ -78,8 +78,13 @@ uint8_t comlynx_checksum(uint8_t *buf, unsigned short len) void virtualDevice::comlynx_send(uint8_t b) { + Debug_printf("comlynx_send_buffer: %X\n", b); + + // Wait for idle only when in UDPStream mode + if (ComLynx._udpDev->udpstreamActive) + ComLynx.wait_for_idle(); + // Write the byte - ComLynx.wait_for_idle(); fnUartBUS.write(b); fnUartBUS.flush(); fnUartBUS.read(); @@ -87,7 +92,13 @@ void virtualDevice::comlynx_send(uint8_t b) void virtualDevice::comlynx_send_buffer(uint8_t *buf, unsigned short len) { - ComLynx.wait_for_idle(); + //Debug_printf("comlynx_send_buffer: %d %s\n", len, buf); + Debug_printf("comlynx_send_buffer: len:%d %0X %0X %0X %0X %0X %0X\n", len, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]); + + // Wait for idle only when in UDPStream mode + if (ComLynx._udpDev->udpstreamActive) + ComLynx.wait_for_idle(); + fnUartBUS.write(buf,len); fnUartBUS.readBytes(buf,len); } @@ -101,6 +112,8 @@ uint8_t virtualDevice::comlynx_recv() b = fnUartBUS.read(); + //Debug_printf("comlynx_recv: %x\n", b); + return b; } @@ -141,7 +154,10 @@ uint16_t virtualDevice::comlynx_recv_length() void virtualDevice::comlynx_send_length(uint16_t l) { - ComLynx.wait_for_idle(); + // Wait for idle only when in UDPStream mode + if (ComLynx._udpDev->udpstreamActive) + ComLynx.wait_for_idle(); + comlynx_send(l >> 8); comlynx_send(l & 0xFF); } @@ -197,6 +213,7 @@ void systemBus::wait_for_idle() if (trashCount > 0) Debug_printf("wait_for_idle() dropped %d bytes\n", trashCount); + start = current = esp_timer_get_time(); @@ -204,7 +221,12 @@ void systemBus::wait_for_idle() { current = esp_timer_get_time(); dur = current - start; - if (dur > IDLE_TIME) + //#ifdef DEBUG + // Debug_printf("wait_for_idle() - not idle, dur:%d\n", dur); + //#endif + + //if (dur > IDLE_TIME) + if (dur > ComLynx.comlynx_idle_time) isIdle = true; } } while (isIdle == false); @@ -266,6 +288,8 @@ void systemBus::_comlynx_process_cmd() uint8_t d = b & 0x0F; + Debug_printf("comlynx_process_cmd: dev:%d\n", d); + // Find device ID and pass control to it if (_daisyChain.count(d) < 1) { @@ -453,11 +477,56 @@ void systemBus::setUDPHost(const char *hostname, int port) } // Restart UDP Stream mode if needed - if (_udpDev->udpstreamActive) + if (_udpDev->udpstreamActive) { _udpDev->comlynx_disable_udpstream(); - if (_udpDev->udpstream_host_ip != IPADDR_NONE) + _udpDev->comlynx_disable_redeye(); + } + if (_udpDev->udpstream_host_ip != IPADDR_NONE) { _udpDev->comlynx_enable_udpstream(); + if (_udpDev->redeye_mode) + _udpDev->comlynx_enable_redeye(); + } +} + + +void systemBus::setRedeyeMode(bool enable) +{ + Debug_printf("setRedeyeMode, %d\n", enable); + _udpDev->redeye_mode = enable; + _udpDev->redeye_logon = true; } + +void systemBus::setRedeyeGameRemap(uint32_t remap) +{ + Debug_printf("setRedeyeGameRemap, %d\n", remap); + + // handle pure updstream games + if ((remap >> 8) == 0xE1) { + _udpDev->redeye_mode = false; // turn off redeye + _udpDev->redeye_logon = true; // reset logon phase toggle + _udpDev->redeye_game = remap; // set game, since we can't detect it + } + + // handle redeye game that need remapping + if (remap != 0xFFFF) { + _udpDev->remap_game_id = true; + _udpDev->new_game_id = remap; + } + else { + _udpDev->remap_game_id = false; + _udpDev->new_game_id = 0xFFFF; + } +} + + +void systemBus::setComlynxIdleTime(uint64_t idle_time) +{ + Debug_printf("setComlynxIdleTime, %d\n", idle_time); + + ComLynx.comlynx_idle_time = idle_time; +} + + systemBus ComLynx; #endif /* BUILD_LYNX */ \ No newline at end of file diff --git a/lib/bus/comlynx/comlynx.h b/lib/bus/comlynx/comlynx.h index 259b1d944..8ed148618 100755 --- a/lib/bus/comlynx/comlynx.h +++ b/lib/bus/comlynx/comlynx.h @@ -235,12 +235,14 @@ class systemBus virtualDevice *_activeDev = nullptr; lynxFuji *_fujiDev = nullptr; lynxPrinter *_printerDev = nullptr; - lynxUDPStream *_udpDev = nullptr; + void _comlynx_process_cmd(); void _comlynx_process_queue(); public: + lynxUDPStream *_udpDev = nullptr; + void setup(); void service(); void shutdown(); @@ -255,6 +257,7 @@ class systemBus * stopwatch */ int64_t start_time; + int64_t comlynx_idle_time = 3000; int numDevices(); void addDevice(virtualDevice *pDevice, uint8_t device_id); @@ -269,6 +272,10 @@ class systemBus QueueHandle_t qComlynxMessages = nullptr; void setUDPHost(const char *newhost, int port); // Set new host/ip & port for UDP Stream + void setRedeyeMode(bool enable); + void setRedeyeGameRemap(uint32_t remap); + void setComlynxIdleTime(uint64_t idle_time); + bool shuttingDown = false; // TRUE if we are in shutdown process bool getShuttingDown() { return shuttingDown; }; }; diff --git a/lib/device/comlynx/fuji.cpp b/lib/device/comlynx/fuji.cpp index 33db887b0..fa42cae63 100755 --- a/lib/device/comlynx/fuji.cpp +++ b/lib/device/comlynx/fuji.cpp @@ -98,20 +98,23 @@ void lynxFuji::comlynx_net_scan_networks() comlynx_recv(); // get ck - isReady = false; + //isReady = false; + //scanStarted = false; // testing -SJ - if (scanStarted == false) - { + //if (scanStarted == false) + //{ _countScannedSSIDs = fnWiFi.scan_networks(); - scanStarted = true; - setSSIDStarted = false; - } + // scanStarted = true; + // setSSIDStarted = false; + //} - isReady = true; + //isReady = true; response[0] = _countScannedSSIDs; response_len = 1; + Debug_printf("comlynx_net_scan_networks, _countScannedSSIDs %d\n", _countScannedSSIDs); + comlynx_response_ack(); } @@ -185,7 +188,8 @@ void lynxFuji::comlynx_net_get_ssid() // Set SSID void lynxFuji::comlynx_net_set_ssid(uint16_t s) { - if (!fnWiFi.connected() && setSSIDStarted == false) + //if (!fnWiFi.connected() && setSSIDStarted == false) + if (setSSIDStarted == false) { Debug_println("Fuji cmd: SET SSID"); @@ -223,6 +227,7 @@ void lynxFuji::comlynx_net_set_ssid(uint16_t s) comlynx_response_ack(); } + // Get WiFi Status void lynxFuji::comlynx_net_get_wifi_status() { @@ -406,12 +411,17 @@ void lynxFuji::mount_all() { bool nodisks = true; // Check at the end if no disks are in a slot and disable config + + Debug_println("fujinet_mount_all()"); + for (int i = 0; i < MAX_DISK_DEVICES; i++) { fujiDisk &disk = _fnDisks[i]; fujiHost &host = _fnHosts[disk.host_slot]; char flag[3] = {'r', 0, 0}; + Debug_printf("moun_all %d '%s' from host #%u as %s on D%u:\n", i, disk.filename, disk.host_slot, flag, i + 1); + if (disk.access_mode == DISK_ACCESS_MODE_WRITE) flag[1] = '+'; @@ -536,7 +546,7 @@ void lynxFuji::comlynx_read_app_key() response_len = 1; // if no file found set return length to 1 or lynx hangs waiting for response return; } - + response_len = fread(response, sizeof(char), 64, fp); fclose(fp); @@ -609,10 +619,12 @@ void lynxFuji::comlynx_open_directory(uint16_t s) uint8_t hostSlot = comlynx_recv(); s--; - s--; + s--; comlynx_recv_buffer((uint8_t *)&dirpath, s); + Debug_printf("comlynx_open_directory: dirpath: %s\n", dirpath); + comlynx_recv(); // Grab checksum ComLynx.start_time = esp_timer_get_time(); @@ -621,7 +633,7 @@ void lynxFuji::comlynx_open_directory(uint16_t s) { // See if there's a search pattern after the directory path const char *pattern = nullptr; - int pathlen = strnlen(dirpath, sizeof(dirpath)); + /*int pathlen = strnlen(dirpath, sizeof(dirpath)); if (pathlen < sizeof(dirpath) - 3) // Allow for two NULLs and a 1-char pattern { pattern = dirpath + pathlen + 1; @@ -633,7 +645,7 @@ void lynxFuji::comlynx_open_directory(uint16_t s) // Remove trailing slash if (pathlen > 1 && dirpath[pathlen - 1] == '/') dirpath[pathlen - 1] = '\0'; - + */ Debug_printf("Opening directory: \"%s\", pattern: \"%s\"\n", dirpath, pattern ? pattern : ""); if (_fnHosts[hostSlot].dir_open(dirpath, pattern, 0)) @@ -694,7 +706,7 @@ void _set_additional_direntry_details(fsdir_entry_t *f, uint8_t *dest, uint8_t m void lynxFuji::comlynx_read_directory_entry() { - Debug_printf("READ DIR ENTRY"); + Debug_printf("READ DIR ENTRY\n"); uint8_t maxlen = comlynx_recv(); uint8_t addtl = comlynx_recv(); @@ -948,14 +960,6 @@ void lynxFuji::comlynx_get_host_prefix() { } -// Public method to update host in specific slot -fujiHost *lynxFuji::set_slot_hostname(int host_slot, char *hostname) -{ - _fnHosts[host_slot].set_hostname(hostname); - _populate_config_from_slots(); - return &_fnHosts[host_slot]; -} - // Send device slot data to computer void lynxFuji::comlynx_read_device_slots() { @@ -1007,12 +1011,15 @@ void lynxFuji::comlynx_write_device_slots() comlynx_recv(); // ck + //Debug_printf("comlnyx_write_device_slots, hs:%d m:%d %s\n", diskSlots[0].hostSlot, diskSlots[0].mode, diskSlots[0].filename); + // Load the data into our current device array for (int i = 0; i < MAX_DISK_DEVICES; i++) _fnDisks[i].reset(diskSlots[i].filename, diskSlots[i].hostSlot, diskSlots[i].mode); // Save the data to disk _populate_config_from_slots(); + Config.mark_dirty(); // not sure why, but I have to mark as dirty Config.save(); comlynx_response_ack(); @@ -1145,7 +1152,7 @@ void lynxFuji::comlynx_enable_device() Debug_printf("FUJI ENABLE DEVICE %02x\n",d); - comlynx_recv(); + comlynx_recv(); // checksum switch(d) { @@ -1201,7 +1208,7 @@ void lynxFuji::comlynx_disable_device() } Config.save(); - + ComLynx.disableDevice(d); comlynx_response_ack(); @@ -1234,7 +1241,7 @@ void lynxFuji::comlynx_random_number() response_len = sizeof(int); *p = rand(); - + comlynx_response_ack(); } @@ -1287,6 +1294,17 @@ lynxDisk *lynxFuji::bootdisk() return _bootDisk; } + +fujiHost *lynxFuji::set_slot_hostname(int host_slot, char *hostname) +{ + + _fnHosts[host_slot].set_hostname(hostname); + _populate_config_from_slots(); + + return &_fnHosts[host_slot]; +} + + void lynxFuji::comlynx_hello() { const char resp[] = "HI FROM FUJINET!\n"; @@ -1459,7 +1477,7 @@ void lynxFuji::comlynx_control_clr() comlynx_send_length(response_len); comlynx_send_buffer(response, response_len); comlynx_send(comlynx_checksum(response, response_len)); - comlynx_recv(); // get the ack. + comlynx_recv(); // get the ack. We should probably be checking if we get an ACK or NACK -SJ memset(response, 0, sizeof(response)); response_len = 0; } @@ -1468,7 +1486,7 @@ void lynxFuji::comlynx_process(uint8_t b) { unsigned char c = b >> 4; Debug_printf("%02x \n",c); - + switch (c) { case MN_STATUS: diff --git a/lib/device/comlynx/fuji.h b/lib/device/comlynx/fuji.h index ae7f30d69..a4079175d 100755 --- a/lib/device/comlynx/fuji.h +++ b/lib/device/comlynx/fuji.h @@ -123,6 +123,7 @@ class lynxFuji : public virtualDevice void comlynx_device_enable_status(); // 0xD1 void comlynx_get_copy_status(); // 0xD0 + void comlynx_hello(); // test void comlynx_test_command(); @@ -137,9 +138,9 @@ class lynxFuji : public virtualDevice public: bool boot_config = true; - + bool status_wait_enabled = true; - + lynxDisk *bootdisk(); lynxNetwork *network(); diff --git a/lib/device/comlynx/network.cpp b/lib/device/comlynx/network.cpp index 446177953..3e724b676 100755 --- a/lib/device/comlynx/network.cpp +++ b/lib/device/comlynx/network.cpp @@ -226,6 +226,7 @@ void lynxNetwork::write(uint16_t num_bytes) err = comlynx_write_channel(num_bytes); } + /** * Perform the correct write based on value of channelMode * @param num_bytes Number of bytes to write. diff --git a/lib/device/comlynx/udpstream.cpp b/lib/device/comlynx/udpstream.cpp index b88959e9d..f9b85777c 100755 --- a/lib/device/comlynx/udpstream.cpp +++ b/lib/device/comlynx/udpstream.cpp @@ -8,6 +8,8 @@ #include "fnSystem.h" #include "utils.h" +//#define DEBUG_UDPSTREAM + void lynxUDPStream::comlynx_enable_udpstream() { // Open the UDP connection @@ -28,24 +30,77 @@ void lynxUDPStream::comlynx_disable_udpstream() #endif } + +void lynxUDPStream::comlynx_enable_redeye() // also can be used to reset redeye mode +{ + redeye_mode = true; + redeye_logon = true; + redeye_game = 0; + redeye_players = 0; + +#ifdef DEBUG + Debug_println("UDPSTREAM redeye mode ENABLED"); +#endif +} + + +void lynxUDPStream::comlynx_disable_redeye() +{ + redeye_mode = false; + redeye_logon = true; + +#ifdef DEBUG + Debug_println("UDPSTREAM redeye mode DISABLED"); +#endif +} + + void lynxUDPStream::comlynx_handle_udpstream() { + bool good_packet = true; + // if there’s data available, read a packet int packetSize = udpStream.parsePacket(); if (packetSize > 0) { udpStream.read(buf_net, UDPSTREAM_BUFFER_SIZE); - // Send to Lynx UART - _comlynx_bus->wait_for_idle(); - fnUartBUS.write(buf_net, packetSize); -#ifdef DEBUG_UDPSTREAM - Debug_print("UDP-IN: "); - util_dump_bytes(buf_net, packetSize); -#endif - fnUartBUS.readBytes(buf_net, packetSize); // Trash what we just sent over serial + + // Lynx Redeye protocol handling + if (redeye_mode) { + if (packetSize < 3) { // check that we have a packet at least 3 bytes + good_packet = false; + #ifdef DEBUG + Debug_println("UDPStream Redeye IN - bad packet size < 3"); + #endif + } + else { + if (!comlynx_redeye_checksum(buf_net)) { // check the checksum + good_packet = false; + #ifdef DEBUG + Debug_println("UDPStream Redeye IN - checksum failed"); + util_dump_bytes(buf_net, packetSize); + #endif + } + } + } + + if (good_packet) { + // Send to Lynx UART + _comlynx_bus->wait_for_idle(); + fnUartBUS.write(buf_net, packetSize); + #ifdef DEBUG_UDPSTREAM + Debug_print("UDP-IN: "); + util_dump_bytes(buf_net, packetSize); + #endif + fnUartBUS.readBytes(buf_net, packetSize); // Trash what we just sent over serial + } } // Read the data until there's a pause in the incoming stream + + fnUartBUS.flush(); + + buf_stream_index = 0; if (fnUartBUS.available() > 0) { while (true) @@ -54,6 +109,11 @@ void lynxUDPStream::comlynx_handle_udpstream() { // Collect bytes read in our buffer buf_stream[buf_stream_index] = (char)fnUartBUS.read(); + if (redeye_mode && (buf_stream_index == 0)) { // Check first byte + if ((buf_stream[0] < 1) || (buf_stream[0] > 6)) // discard bad size byte (must be between 1 and 6) + continue; + } + if (buf_stream_index < UDPSTREAM_BUFFER_SIZE - 1) buf_stream_index++; } @@ -65,19 +125,167 @@ void lynxUDPStream::comlynx_handle_udpstream() } } + // Did we get any data? + if (buf_stream_index == 0) + return; + + // Lynx Redeye protocol handling + if (redeye_mode) { + if (buf_stream_index < 3) { // packets have to be at least three bytes + #ifdef DEBUG + Debug_println("UDPStream Redeye OUT - bad packet size < 3"); + util_dump_bytes(buf_stream, buf_stream_index); + #endif + return; // bail out + } + + if (comlynx_redeye_checksum(buf_stream)) { + if (redeye_logon) { // Are we in logon phase? + if (buf_stream[0] == 5) { // and this is a logon packet (size = 5) + redeye_game = (buf_stream[4]+(buf_stream[5]<<8)); // collect redye game ID + + if (remap_game_id) { // need to remap the game id? + redeye_remap_game_id(); + redeye_recalculate_checksum(); // recalculate the checksum + } + + redeye_players = 0; // extract number of players in game + for(int i = 0; i < 8; i++) + redeye_players += (buf_stream[3] >> i) & 0x01; + + if (buf_stream[1] == 2) { // redeye logon phase ending? + redeye_logon = false; // stop handling logon phase packets + #ifdef DEBUG + Debug_println("UDPSTREAM redeye logon phase ending"); + Debug_printf("UDPSTREAM redeye game: %d, redeye_players: %d\n", redeye_game, redeye_players); + #endif + } + + } + } + + #ifdef DEBUG + //Debug_printf("UDPSTREAM redeye_game: %d, redeye_players: %d\n", redeye_game, redeye_players); + #endif + + // Send what we've collected over WiFi + //udpStream.beginPacket(udpstream_host_ip, udpstream_port); // remote IP and port + //udpStream.write(buf_stream, buf_stream_index); + //udpStream.endPacket(); + } + else { + #ifdef DEBUG + Debug_println("UDPSTREAM Redeye OUT - checksum failed"); + util_dump_bytes(buf_stream, buf_stream_index); + #endif + return; + } + } + // Send what we've collected over WiFi udpStream.beginPacket(udpstream_host_ip, udpstream_port); // remote IP and port udpStream.write(buf_stream, buf_stream_index); udpStream.endPacket(); -#ifdef DEBUG_UDPSTREAM - Debug_print("UDP-OUT: "); - util_dump_bytes(buf_stream, buf_stream_index); -#endif - buf_stream_index = 0; + #ifdef DEBUG_UDPSTREAM + Debug_print("UDP-OUT: "); + util_dump_bytes(buf_stream, buf_stream_index); + #endif + } +} + + + /* Calculate the checksum of incoming from the lynx redeye packets + Return true if ok, false if not + + typical message: + 05 00 00 01 FF FF F8 + + Checksum is calculated on size, plus message bytes. + */ + bool lynxUDPStream::comlynx_redeye_checksum(uint8_t *buf) + { + uint16_t ck; + uint8_t i; + uint8_t size; + + + size = buf[0]; // get message size + if ((size == 0) || (size > 6)) { // check packets are in range + //Debug_printf("checksum size %d %d\n", size, buf[0]); + return false; + } + + // checksum caculation is 255 - size - message bytes + ck = 255; + for (i=0; i < size+1; i++) { + ck -= buf[i]; } + + if ((ck & 0xFF) == buf[size+1]) + return true; + else + return false; + + } + + + /* Recalculate the checksum of the lynx redeye packet. + + Checksum is calculated on size, plus message bytes. + */ + void lynxUDPStream::redeye_recalculate_checksum() + { + uint16_t ck; + uint8_t i; + uint8_t size; + + + size = buf_stream[0]; // get message size + + // checksum caculation is 255 - size - message bytes + ck = 255; + for (i=0; i < size+1; i++) { + ck -= buf_stream[i]; + } + + // set new checksum on packet + buf_stream[size+1] = (ck & 0xFF); + return; + } + + +/* redeye_remap_game_id + * + * Remap certain game IDs (based on GUI setting) so that we + * have a unique game id for each game. + * + * 0xFFFF 0xE001 Relief Pitcher + * 0xFFFF 0xE002 Pit Fighter + * 0xFFFF 0xE003 Double Dragon + * 0xFFFF 0xE004 European Soccer + * 0xFFFF 0xE005 Lynx Casino + * 0xFFFF 0xE006 Super Off-Road + * + * redeye_game = (buf_stream[4]+(buf_stream[5]<<8)); + */ +void lynxUDPStream::redeye_remap_game_id() +{ + uint16_t gid; + + + // Double check that game id even needs to be remapped + gid = (buf_stream[4]+(buf_stream[5]<<8)); + if (gid != 0xFFFF) + return; + + // Set new game ID + buf_stream[4] = new_game_id & 0xFF; + buf_stream[5] = (new_game_id >> 8) & 0xFF; + return; } + void lynxUDPStream::comlynx_process(uint8_t b) { // Nothing to do here diff --git a/lib/device/comlynx/udpstream.h b/lib/device/comlynx/udpstream.h index 87670ef73..7ae06a485 100755 --- a/lib/device/comlynx/udpstream.h +++ b/lib/device/comlynx/udpstream.h @@ -26,15 +26,29 @@ class lynxUDPStream : public virtualDevice uint8_t buf_stream_index=0; void comlynx_process(uint8_t b) override; + bool comlynx_redeye_checksum(uint8_t *buf); // check the redeye checksum + void redeye_recalculate_checksum(); // recalculate redeye packet checksum (for remapped game_id) + void redeye_remap_game_id(); // remap game_id to provide a unique game_id public: bool udpstreamActive = false; // If we are in udpstream mode or not in_addr_t udpstream_host_ip = IPADDR_NONE; int udpstream_port; + bool redeye_mode = true; // redeye UDP stream mode + bool redeye_logon = true; // in redeye logon phase + uint16_t redeye_game = 0; // redeye game ID + uint8_t redeye_players = 0; // redeye number of players - not sure we need this + bool remap_game_id = false; // should we be remapping the game id? + uint16_t new_game_id = 0xFFFF; // the new game ID to remap, set from Web GUI + void comlynx_enable_udpstream(); // setup udpstream void comlynx_disable_udpstream(); // stop udpstream void comlynx_handle_udpstream(); // Handle incoming & outgoing data for udpstream + + void comlynx_enable_redeye(); // setup redeye mode overtop udpstream + void comlynx_disable_redeye(); // disable redeye mode + }; #endif \ No newline at end of file diff --git a/lib/hardware/fnWiFi.cpp b/lib/hardware/fnWiFi.cpp index febd14e51..c2709d471 100755 --- a/lib/hardware/fnWiFi.cpp +++ b/lib/hardware/fnWiFi.cpp @@ -326,6 +326,7 @@ uint8_t WiFiManager::scan_networks(uint8_t maxresults) temporary_disconnect = true; _disconnecting = true; esp_wifi_disconnect(); + sleep(2); _disconnecting = false; } diff --git a/platformio-ini-files/platformio.common.ini b/platformio-ini-files/platformio.common.ini index 1e2b29dd5..f34eb3274 100644 --- a/platformio-ini-files/platformio.common.ini +++ b/platformio-ini-files/platformio.common.ini @@ -83,10 +83,10 @@ build_flags = ;build_board = fujinet-iec-lolin-d32 ; Commodore IEC using Lolin D32 Pro ;build_board = fujinet-iec-nugget ; Commodore IEC using Lolin D32 Pro "Nugget" -;build_platform = BUILD_LYNX -;build_bus = comlynx +build_platform = BUILD_LYNX +build_bus = comlynx ;build_board = fujinet-lynx-prototype ; Lynx Prototype PCB -;build_board = fujinet-lynx-devkitc ; Lynx with basic DEVKITC +build_board = fujinet-lynx-devkitc ; Lynx with basic DEVKITC ;build_platform = BUILD_S100 ;build_bus = s100Bus From a461a749047b7ef8535ec5ca0ccd2eb5622df095 Mon Sep 17 00:00:00 2001 From: Shawn Date: Sun, 7 Sep 2025 19:09:09 -0700 Subject: [PATCH 02/12] Added checksum checking to Fujinet commands --- lib/bus/comlynx/comlynx.cpp | 42 +++++- lib/bus/comlynx/comlynx.h | 12 ++ lib/device/comlynx/fuji.cpp | 292 +++++++++++++++++++++++++++--------- 3 files changed, 270 insertions(+), 76 deletions(-) diff --git a/lib/bus/comlynx/comlynx.cpp b/lib/bus/comlynx/comlynx.cpp index 7c89d443c..9333ac63e 100755 --- a/lib/bus/comlynx/comlynx.cpp +++ b/lib/bus/comlynx/comlynx.cpp @@ -103,6 +103,30 @@ void virtualDevice::comlynx_send_buffer(uint8_t *buf, unsigned short len) fnUartBUS.readBytes(buf,len); } +bool virtualDevice::comlynx_recv_ck() +{ + uint8_t recv_ck, ck; + + + while (fnUartBUS.available() <= 0) + fnSystem.yield(); + + // get checksum + recv_ck = fnUartBUS.read(); + + ck = comlynx_checksum(recvbuffer, recvbuffer_len); + + Debug_printf("comlynx_recv_ck, recv:%02X calc:%02X\n", recv_ck, ck); + + // reset receive buffer + recvbuffer_len = 0; + + if (recv_ck == ck) + return true; + else + return false; +} + uint8_t virtualDevice::comlynx_recv() { uint8_t b; @@ -112,8 +136,11 @@ uint8_t virtualDevice::comlynx_recv() b = fnUartBUS.read(); - //Debug_printf("comlynx_recv: %x\n", b); + // Add to receive buffer + recvbuffer[recvbuffer_len] = b; + recvbuffer_len++; + //Debug_printf("comlynx_recv: %x\n", b); return b; } @@ -149,6 +176,9 @@ uint16_t virtualDevice::comlynx_recv_length() s = comlynx_recv() << 8; s |= comlynx_recv(); + // Reset recv buffer, but maybe we want checksum over the length too? -SJ + recvbuffer_len = 0; + return s; } @@ -164,7 +194,15 @@ void virtualDevice::comlynx_send_length(uint16_t l) unsigned short virtualDevice::comlynx_recv_buffer(uint8_t *buf, unsigned short len) { - return fnUartBUS.readBytes(buf, len); + unsigned short b; + + b = fnUartBUS.readBytes(buf, len); + + // Add to receive buffer + memcpy(&recvbuffer[recvbuffer_len], buf, len); + recvbuffer_len += len; + + return(b); } uint32_t virtualDevice::comlynx_recv_blockno() diff --git a/lib/bus/comlynx/comlynx.h b/lib/bus/comlynx/comlynx.h index 8ed148618..98022223f 100755 --- a/lib/bus/comlynx/comlynx.h +++ b/lib/bus/comlynx/comlynx.h @@ -92,6 +92,12 @@ class virtualDevice */ void comlynx_send_buffer(uint8_t *buf, unsigned short len); + /** + * @brief Receive checksum byte and compare with checksum calculation + * @return true or false + */ + bool comlynx_recv_ck(); + /** * @brief Receive byte from Comlynx * @return byte received @@ -204,6 +210,12 @@ class virtualDevice */ uint16_t response_len; + /** + * Receive buffer and length + */ + uint8_t recvbuffer[1024]; + uint16_t recvbuffer_len = 0; + public: /** diff --git a/lib/device/comlynx/fuji.cpp b/lib/device/comlynx/fuji.cpp index fa42cae63..f81827c67 100755 --- a/lib/device/comlynx/fuji.cpp +++ b/lib/device/comlynx/fuji.cpp @@ -85,8 +85,14 @@ void lynxFuji::comlynx_control_status() // Reset FujiNet void lynxFuji::comlynx_reset_fujinet() { - comlynx_recv(); // get ck Debug_println("COMLYNX RESET FUJINET"); + + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } + comlynx_response_ack(); fnSystem.reboot(); } @@ -96,19 +102,13 @@ void lynxFuji::comlynx_net_scan_networks() { Debug_println("Fuji cmd: SCAN NETWORKS"); - comlynx_recv(); // get ck - - //isReady = false; - //scanStarted = false; // testing -SJ - - //if (scanStarted == false) - //{ - _countScannedSSIDs = fnWiFi.scan_networks(); - // scanStarted = true; - // setSSIDStarted = false; - //} + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } - //isReady = true; + _countScannedSSIDs = fnWiFi.scan_networks(); response[0] = _countScannedSSIDs; response_len = 1; @@ -126,7 +126,11 @@ void lynxFuji::comlynx_net_scan_result() uint8_t n = comlynx_recv(); - comlynx_recv(); // get CK + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } // Response to FUJICMD_GET_SCAN_RESULT struct @@ -154,7 +158,11 @@ void lynxFuji::comlynx_net_get_ssid() { Debug_println("Fuji cmd: GET SSID"); - comlynx_recv(); // get CK + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } // Response to FUJICMD_GET_SSID struct @@ -188,24 +196,27 @@ void lynxFuji::comlynx_net_get_ssid() // Set SSID void lynxFuji::comlynx_net_set_ssid(uint16_t s) { - //if (!fnWiFi.connected() && setSSIDStarted == false) - if (setSSIDStarted == false) - { - Debug_println("Fuji cmd: SET SSID"); + Debug_println("Fuji cmd: SET SSID"); - s--; + s--; - // Data for FUJICMD_SET_SSID - struct - { - char ssid[MAX_SSID_LEN+1]; - char password[MAX_WIFI_PASS_LEN]; - } cfg; + // Data for FUJICMD_SET_SSID + struct + { + char ssid[MAX_SSID_LEN+1]; + char password[MAX_WIFI_PASS_LEN]; + } cfg; - comlynx_recv_buffer((uint8_t *)&cfg, s); + comlynx_recv_buffer((uint8_t *)&cfg, s); - comlynx_recv(); // CK + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } + if (setSSIDStarted == false) // Do we need this? Check Atari implementation -SJ + { bool save = true; // URL Decode SSID/PASSWORD to handle special chars (FIXME) @@ -216,6 +227,7 @@ void lynxFuji::comlynx_net_set_ssid(uint16_t s) fnWiFi.connect(cfg.ssid, cfg.password); setSSIDStarted = true; + // Only save these if we're asked to, otherwise assume it was a test for connectivity if (save) { @@ -231,8 +243,14 @@ void lynxFuji::comlynx_net_set_ssid(uint16_t s) // Get WiFi Status void lynxFuji::comlynx_net_get_wifi_status() { - comlynx_recv(); // Get CK Debug_println("Fuji cmd: GET WIFI STATUS"); + + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } + // WL_CONNECTED = 3, WL_DISCONNECTED = 6 uint8_t wifiStatus = fnWiFi.connected() ? 3 : 6; response[0] = wifiStatus; @@ -248,7 +266,11 @@ void lynxFuji::comlynx_mount_host() unsigned char hostSlot = comlynx_recv(); - comlynx_recv(); // Get CK + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } if (hostMounted[hostSlot] == false) { @@ -266,7 +288,11 @@ void lynxFuji::comlynx_unmount_host() unsigned char hostSlot = comlynx_recv(); - comlynx_recv(); // Get CK + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } if (hostMounted[hostSlot] == false) { @@ -285,7 +311,11 @@ void lynxFuji::comlynx_disk_image_mount() uint8_t deviceSlot = comlynx_recv(); uint8_t options = comlynx_recv(); // DISK_ACCESS_MODE - comlynx_recv(); // CK + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } // TODO: Implement FETCH? char flag[3] = {'r', 0, 0}; @@ -319,10 +349,15 @@ void lynxFuji::comlynx_disk_image_mount() // Toggle boot config on/off, aux1=0 is disabled, aux1=1 is enabled void lynxFuji::comlynx_set_boot_config() { + Debug_printf("Boot config is now %d",boot_config); + boot_config = comlynx_recv(); - comlynx_recv(); - Debug_printf("Boot config is now %d",boot_config); + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } if (_fnDisks[0].disk_dev.is_config_device) { @@ -357,7 +392,12 @@ void lynxFuji::comlynx_copy_file() sourceSlot = comlynx_recv(); destSlot = comlynx_recv(); comlynx_recv_buffer(csBuf,sizeof(csBuf)); - ck = comlynx_recv(); + + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } dataBuf = (char *)malloc(COPY_SIZE); @@ -411,16 +451,21 @@ void lynxFuji::mount_all() { bool nodisks = true; // Check at the end if no disks are in a slot and disable config - Debug_println("fujinet_mount_all()"); + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } + for (int i = 0; i < MAX_DISK_DEVICES; i++) { fujiDisk &disk = _fnDisks[i]; fujiHost &host = _fnHosts[disk.host_slot]; char flag[3] = {'r', 0, 0}; - Debug_printf("moun_all %d '%s' from host #%u as %s on D%u:\n", i, disk.filename, disk.host_slot, flag, i + 1); + Debug_printf("mount_all %d '%s' from host #%u as %s on D%u:\n", i, disk.filename, disk.host_slot, flag, i + 1); if (disk.access_mode == DISK_ACCESS_MODE_WRITE) flag[1] = '+'; @@ -471,7 +516,12 @@ void lynxFuji::mount_all() void lynxFuji::comlynx_set_boot_mode() { uint8_t bm = comlynx_recv(); - comlynx_recv(); // CK + + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } insert_boot_device(bm); boot_config = true; @@ -499,15 +549,19 @@ void lynxFuji::comlynx_write_app_key() char appkeyfilename[30]; FILE *fp; + Debug_printf("Fuji Cmd: WRITE APPKEY %s\n", appkeyfilename); + snprintf(appkeyfilename, sizeof(appkeyfilename), "/FujiNet/%04hx%02hhx%02hhx.key", creator, app, key); comlynx_recv_buffer(data, 64); - comlynx_recv(); // CK - Debug_printf("Fuji Cmd: WRITE APPKEY %s\n", appkeyfilename); + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } fp = fnSDFAT.file_open(appkeyfilename, "w"); - if (fp == nullptr) { Debug_printf("Could not open.\n"); @@ -529,7 +583,11 @@ void lynxFuji::comlynx_read_app_key() uint8_t app = comlynx_recv(); uint8_t key = comlynx_recv(); - comlynx_recv(); // CK + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } char appkeyfilename[30]; FILE *fp; @@ -562,7 +620,12 @@ void lynxFuji::debug_tape() void lynxFuji::comlynx_disk_image_umount() { unsigned char ds = comlynx_recv(); - comlynx_recv(); + + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } _fnDisks[ds].disk_dev.unmount(); _fnDisks[ds].reset(); @@ -625,7 +688,11 @@ void lynxFuji::comlynx_open_directory(uint16_t s) Debug_printf("comlynx_open_directory: dirpath: %s\n", dirpath); - comlynx_recv(); // Grab checksum + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } ComLynx.start_time = esp_timer_get_time(); @@ -710,7 +777,11 @@ void lynxFuji::comlynx_read_directory_entry() uint8_t maxlen = comlynx_recv(); uint8_t addtl = comlynx_recv(); - comlynx_recv(); // Checksum + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } if (response[0] == 0x00) { @@ -801,7 +872,11 @@ void lynxFuji::comlynx_get_directory_position() uint16_t pos = _fnHosts[_current_open_directory_slot].dir_tell(); - comlynx_recv(); // ck + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } response_len = sizeof(pos); memcpy(response, &pos, sizeof(pos)); @@ -814,13 +889,18 @@ void lynxFuji::comlynx_set_directory_position() // DAUX1 and DAUX2 hold the position to seek to in low/high order uint16_t pos = 0; + Debug_println("Fuji cmd: SET DIRECTORY POSITION"); + comlynx_recv_buffer((uint8_t *)&pos, sizeof(uint16_t)); - comlynx_recv(); // ck + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } _fnHosts[_current_open_directory_slot].dir_seek(pos); comlynx_response_ack(); - Debug_println("Fuji cmd: SET DIRECTORY POSITION"); Debug_printf("pos is now %u", pos); } @@ -828,7 +908,11 @@ void lynxFuji::comlynx_close_directory() { Debug_println("Fuji cmd: CLOSE DIRECTORY"); - comlynx_recv(); // ck + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } if (_current_open_directory_slot != -1) _fnHosts[_current_open_directory_slot].dir_close(); @@ -843,7 +927,11 @@ void lynxFuji::comlynx_get_adapter_config() { Debug_println("Fuji cmd: GET ADAPTER CONFIG"); - comlynx_recv(); // ck + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } // Response to FUJICMD_GET_ADAPTERCONFIG AdapterConfig cfg; @@ -885,7 +973,11 @@ void lynxFuji::comlynx_new_disk() comlynx_recv_buffer(c, sizeof(uint32_t)); comlynx_recv_buffer(p, 256); - comlynx_recv(); // CK + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } fujiDisk &disk = _fnDisks[ds]; fujiHost &host = _fnHosts[hs]; @@ -915,7 +1007,11 @@ void lynxFuji::comlynx_read_host_slots() { Debug_println("Fuji cmd: READ HOST SLOTS"); - comlynx_recv(); // ck + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } char hostSlots[MAX_HOSTS][MAX_HOSTNAME_LEN]; memset(hostSlots, 0, sizeof(hostSlots)); @@ -937,7 +1033,11 @@ void lynxFuji::comlynx_write_host_slots() char hostSlots[MAX_HOSTS][MAX_HOSTNAME_LEN]; comlynx_recv_buffer((uint8_t *)hostSlots, sizeof(hostSlots)); - comlynx_recv(); // ck + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } for (int i = 0; i < MAX_HOSTS; i++) { @@ -965,6 +1065,12 @@ void lynxFuji::comlynx_read_device_slots() { Debug_println("Fuji cmd: READ DEVICE SLOTS"); + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } + struct disk_slot { uint8_t hostSlot; @@ -987,8 +1093,6 @@ void lynxFuji::comlynx_read_device_slots() returnsize = sizeof(disk_slot) * MAX_DISK_DEVICES; - comlynx_recv(); // ck - memcpy(response, &diskSlots, returnsize); response_len = returnsize; @@ -1009,7 +1113,11 @@ void lynxFuji::comlynx_write_device_slots() comlynx_recv_buffer((uint8_t *)&diskSlots, sizeof(diskSlots)); - comlynx_recv(); // ck + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } //Debug_printf("comlnyx_write_device_slots, hs:%d m:%d %s\n", diskSlots[0].hostSlot, diskSlots[0].mode, diskSlots[0].filename); @@ -1100,7 +1208,11 @@ void lynxFuji::comlynx_set_device_filename(uint16_t s) Debug_printf("filename: %s\n", f); - comlynx_recv(); // CK + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } memcpy(_fnDisks[ds].filename, f, MAX_FILENAME_LEN); _populate_config_from_slots(); @@ -1113,7 +1225,11 @@ void lynxFuji::comlynx_get_device_filename() { unsigned char ds = comlynx_recv(); - comlynx_recv(); + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } memcpy(response, _fnDisks[ds].filename, 256); response_len = 256; @@ -1149,10 +1265,13 @@ void lynxFuji::insert_boot_device(uint8_t d) void lynxFuji::comlynx_enable_device() { unsigned char d = comlynx_recv(); - Debug_printf("FUJI ENABLE DEVICE %02x\n",d); - - comlynx_recv(); // checksum + + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } switch(d) { @@ -1174,19 +1293,20 @@ void lynxFuji::comlynx_enable_device() } Config.save(); - ComLynx.enableDevice(d); - comlynx_response_ack(); } void lynxFuji::comlynx_disable_device() { unsigned char d = comlynx_recv(); - Debug_printf("FUJI DISABLE DEVICE %02x\n",d); - comlynx_recv(); + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } switch(d) { @@ -1207,10 +1327,8 @@ void lynxFuji::comlynx_disable_device() break; } - Config.save(); - + Config.save(); ComLynx.disableDevice(d); - comlynx_response_ack(); } @@ -1237,7 +1355,11 @@ void lynxFuji::comlynx_random_number() { int *p = (int *)&response[0]; - comlynx_recv(); // CK + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } response_len = sizeof(int); *p = rand(); @@ -1248,7 +1370,12 @@ void lynxFuji::comlynx_random_number() void lynxFuji::comlynx_get_time() { Debug_println("FUJI GET TIME"); - comlynx_recv(); // CK + + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } time_t tt = time(nullptr); @@ -1276,7 +1403,12 @@ void lynxFuji::comlynx_get_time() void lynxFuji::comlynx_device_enable_status() { uint8_t d = comlynx_recv(); - comlynx_recv(); // CK + + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } ComLynx.start_time = esp_timer_get_time(); @@ -1307,6 +1439,12 @@ fujiHost *lynxFuji::set_slot_hostname(int host_slot, char *hostname) void lynxFuji::comlynx_hello() { + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } + const char resp[] = "HI FROM FUJINET!\n"; response_len = strlen(resp); memcpy(response,resp,response_len); @@ -1322,7 +1460,7 @@ void lynxFuji::comlynx_hello() void lynxFuji::comlynx_enable_udpstream(uint16_t s) { char host[128]; - + s--; // Receive port # @@ -1338,8 +1476,11 @@ void lynxFuji::comlynx_enable_udpstream(uint16_t s) // Receive host comlynx_recv_buffer((uint8_t *)host,s); - // Receive Checksum. - comlynx_recv(); + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } // Acknowledge comlynx_response_ack(); @@ -1355,6 +1496,9 @@ void lynxFuji::comlynx_enable_udpstream(uint16_t s) void lynxFuji::comlynx_control_send() { + // Reset the recvbuffer + recvbuffer_len = 0; // happens in recv_length, but may remove from there -SJ + uint16_t s = comlynx_recv_length(); uint8_t c = comlynx_recv(); From a3358662e3c414ce52f2bff92e464b6b87dc4752 Mon Sep 17 00:00:00 2001 From: Shawn Date: Sun, 7 Sep 2025 20:19:41 -0700 Subject: [PATCH 03/12] Further cleanup and small fixes --- lib/bus/comlynx/comlynx.cpp | 3 ++ lib/device/comlynx/fuji.cpp | 69 +++++++++++++++++++++++-------------- 2 files changed, 46 insertions(+), 26 deletions(-) diff --git a/lib/bus/comlynx/comlynx.cpp b/lib/bus/comlynx/comlynx.cpp index 9333ac63e..24a4f168b 100755 --- a/lib/bus/comlynx/comlynx.cpp +++ b/lib/bus/comlynx/comlynx.cpp @@ -176,6 +176,9 @@ uint16_t virtualDevice::comlynx_recv_length() s = comlynx_recv() << 8; s |= comlynx_recv(); + if (s > 1024) + s = 1024; + // Reset recv buffer, but maybe we want checksum over the length too? -SJ recvbuffer_len = 0; diff --git a/lib/device/comlynx/fuji.cpp b/lib/device/comlynx/fuji.cpp index f81827c67..5a8eeba41 100755 --- a/lib/device/comlynx/fuji.cpp +++ b/lib/device/comlynx/fuji.cpp @@ -122,7 +122,7 @@ void lynxFuji::comlynx_net_scan_networks() void lynxFuji::comlynx_net_scan_result() { Debug_println("Fuji cmd: GET SCAN RESULT"); - scanStarted = false; + //scanStarted = false; uint8_t n = comlynx_recv(); @@ -174,7 +174,7 @@ void lynxFuji::comlynx_net_get_ssid() memset(&cfg, 0, sizeof(cfg)); /* - We memcpy instead of strcpy because technically the SSID and phasephras aren't strings and aren't null terminated, + We memcpy instead of strcpy because technically the SSID and phasephrase aren't strings and aren't null terminated, they're arrays of bytes officially and can contain any byte value - including a zero - at any point in the array. However, we're not consistent about how we treat this in the different parts of the code. */ @@ -196,9 +196,11 @@ void lynxFuji::comlynx_net_get_ssid() // Set SSID void lynxFuji::comlynx_net_set_ssid(uint16_t s) { + uint8_t save; + Debug_println("Fuji cmd: SET SSID"); - s--; + save = comlynx_recv(); // Data for FUJICMD_SET_SSID struct @@ -207,6 +209,8 @@ void lynxFuji::comlynx_net_set_ssid(uint16_t s) char password[MAX_WIFI_PASS_LEN]; } cfg; + s--; + s--; comlynx_recv_buffer((uint8_t *)&cfg, s); // Get packet checksum @@ -215,26 +219,17 @@ void lynxFuji::comlynx_net_set_ssid(uint16_t s) return; } - if (setSSIDStarted == false) // Do we need this? Check Atari implementation -SJ - { - bool save = true; - - // URL Decode SSID/PASSWORD to handle special chars (FIXME) - //mstr::urlDecode(cfg.ssid, sizeof(cfg.ssid)); - //mstr::urlDecode(cfg.password, sizeof(cfg.password)); + Debug_printf("Connecting to net: %s password: %s\n", cfg.ssid, cfg.password); - Debug_printf("Connecting to net: %s password: %s\n", cfg.ssid, cfg.password); + fnWiFi.connect(cfg.ssid, cfg.password); + setSSIDStarted = true; - fnWiFi.connect(cfg.ssid, cfg.password); - setSSIDStarted = true; - - // Only save these if we're asked to, otherwise assume it was a test for connectivity - if (save) - { - Config.store_wifi_ssid(cfg.ssid, sizeof(cfg.ssid)); - Config.store_wifi_passphrase(cfg.password, sizeof(cfg.password)); - Config.save(); - } + // Only save these if we're asked to, otherwise assume it was a test for connectivity + if (save) + { + Config.store_wifi_ssid(cfg.ssid, sizeof(cfg.ssid)); + Config.store_wifi_passphrase(cfg.password, sizeof(cfg.password)); + Config.save(); } comlynx_response_ack(); @@ -349,6 +344,9 @@ void lynxFuji::comlynx_disk_image_mount() // Toggle boot config on/off, aux1=0 is disabled, aux1=1 is enabled void lynxFuji::comlynx_set_boot_config() { + // does nothing on Lynx -SJ + + /* Debug_printf("Boot config is now %d",boot_config); boot_config = comlynx_recv(); @@ -367,7 +365,7 @@ void lynxFuji::comlynx_set_boot_config() Debug_printf("Boot config unmounted slot 0"); } - comlynx_response_ack(); + comlynx_response_ack(); */ } // Do SIO copy @@ -515,6 +513,9 @@ void lynxFuji::mount_all() // Set boot mode void lynxFuji::comlynx_set_boot_mode() { + // does nothing on Lynx -SJ + + /* uint8_t bm = comlynx_recv(); // Get packet checksum @@ -527,6 +528,7 @@ void lynxFuji::comlynx_set_boot_mode() boot_config = true; comlynx_response_ack(); + */ } char *_generate_appkey_filename(appkey *info) @@ -642,6 +644,8 @@ void lynxFuji::image_rotate() { Debug_println("Fuji cmd: IMAGE ROTATE"); + // probably won't be needed on Lynx -SJ + int count = 0; // Find the first empty slot while (_fnDisks[count].fileh != nullptr) @@ -835,6 +839,8 @@ void lynxFuji::comlynx_read_directory_entry() } // Hack-o-rama to add file type character to beginning of path. + // I don't think we need any of this -SJ + /* if (maxlen == 38) { memmove(&dirpath[2], dirpath, 254); @@ -851,7 +857,7 @@ void lynxFuji::comlynx_read_directory_entry() } else dirpath[0] = dirpath[1] = 0x20; - } + }*/ memset(response, 0, sizeof(response)); memcpy(response, dirpath, maxlen); @@ -886,7 +892,6 @@ void lynxFuji::comlynx_get_directory_position() void lynxFuji::comlynx_set_directory_position() { - // DAUX1 and DAUX2 hold the position to seek to in low/high order uint16_t pos = 0; Debug_println("Fuji cmd: SET DIRECTORY POSITION"); @@ -1240,6 +1245,9 @@ void lynxFuji::comlynx_get_device_filename() // Mounts the desired boot disk number void lynxFuji::insert_boot_device(uint8_t d) { + // This isn't needed on Lynx -SJ + + /* // TODO: Change this when CONFIG is ready. const char *config_atr = "/autorun.ddp"; const char *mount_all_atr = "/mount-and-boot.ddp"; @@ -1260,6 +1268,7 @@ void lynxFuji::insert_boot_device(uint8_t d) _fnDisks[0].disk_dev.is_config_device = true; _fnDisks[0].disk_dev.device_active = true; + */ } void lynxFuji::comlynx_enable_device() @@ -1617,11 +1626,19 @@ void lynxFuji::comlynx_control_send() void lynxFuji::comlynx_control_clr() { + uint8_t b; + comlynx_send(0xBF); comlynx_send_length(response_len); comlynx_send_buffer(response, response_len); comlynx_send(comlynx_checksum(response, response_len)); - comlynx_recv(); // get the ack. We should probably be checking if we get an ACK or NACK -SJ + b = comlynx_recv(); // get the ack or nack + // ignore response from Lynx, if they didn't receive the data properly + // they should resend the entire command -SJ + + Debug_printf("comlynx_control_clr: %02X\n", b); + + // Reset response buffer memset(response, 0, sizeof(response)); response_len = 0; } @@ -1629,7 +1646,7 @@ void lynxFuji::comlynx_control_clr() void lynxFuji::comlynx_process(uint8_t b) { unsigned char c = b >> 4; - Debug_printf("%02x \n",c); + //Debug_printf("%02x \n",c); switch (c) { From 045aab14d7fe73ce1ce6856ab717cb92fedf5287 Mon Sep 17 00:00:00 2001 From: Shawn Date: Thu, 11 Sep 2025 21:04:41 -0700 Subject: [PATCH 04/12] Added checksum to fuji, disk and network. General cleanup --- lib/bus/comlynx/comlynx.cpp | 37 +++++---- lib/bus/comlynx/comlynx.h | 4 +- lib/device/comlynx/disk.cpp | 37 ++++++--- lib/device/comlynx/fuji.cpp | 8 +- lib/device/comlynx/network.cpp | 141 ++++++++++++++++++++++++--------- 5 files changed, 159 insertions(+), 68 deletions(-) diff --git a/lib/bus/comlynx/comlynx.cpp b/lib/bus/comlynx/comlynx.cpp index 24a4f168b..f774230ec 100755 --- a/lib/bus/comlynx/comlynx.cpp +++ b/lib/bus/comlynx/comlynx.cpp @@ -13,7 +13,7 @@ #include "led.h" #include -#define IDLE_TIME 1000 // Idle tolerance in microseconds +#define IDLE_TIME 500 // Idle tolerance in microseconds (roughly three characters at 62500 baud) static QueueHandle_t reset_evt_queue = NULL; @@ -187,10 +187,6 @@ uint16_t virtualDevice::comlynx_recv_length() void virtualDevice::comlynx_send_length(uint16_t l) { - // Wait for idle only when in UDPStream mode - if (ComLynx._udpDev->udpstreamActive) - ComLynx.wait_for_idle(); - comlynx_send(l >> 8); comlynx_send(l & 0xFF); } @@ -241,10 +237,17 @@ void systemBus::wait_for_idle() { bool isIdle = false; int64_t start, current, dur; - int trashCount = 0; + //int trashCount = 0; - do - { + // SJ notes: we really don't need to do this unless we are in UDPStream mode + // Likely we want to just wait until the bus is "idle" for about 3 character times + // which is about 0.5 ms at 62500 baud 8N1 + // + // Check that the bus is truly idle for the whole duration, and then we can start sending? + + //do + //{ + /* // Wait for serial line to quiet down. while (fnUartBUS.available() > 0) { @@ -254,10 +257,14 @@ void systemBus::wait_for_idle() if (trashCount > 0) Debug_printf("wait_for_idle() dropped %d bytes\n", trashCount); - + */ + + // Wait for serial line to quiet down. + while (fnUartBUS.available() > 0); start = current = esp_timer_get_time(); + // Wait for IDLE_TIME duration before calling it idle while ((fnUartBUS.available() <= 0) && (isIdle == false)) { current = esp_timer_get_time(); @@ -266,11 +273,11 @@ void systemBus::wait_for_idle() // Debug_printf("wait_for_idle() - not idle, dur:%d\n", dur); //#endif - //if (dur > IDLE_TIME) - if (dur > ComLynx.comlynx_idle_time) + if (dur > IDLE_TIME) + //if (dur > ComLynx.comlynx_idle_time) isIdle = true; } - } while (isIdle == false); + //} while (isIdle == false); fnSystem.yield(); } @@ -281,7 +288,7 @@ void virtualDevice::comlynx_process(uint8_t b) void virtualDevice::comlynx_control_status() { - ComLynx.start_time = esp_timer_get_time(); + //ComLynx.start_time = esp_timer_get_time(); comlynx_response_status(); } @@ -561,12 +568,12 @@ void systemBus::setRedeyeGameRemap(uint32_t remap) } -void systemBus::setComlynxIdleTime(uint64_t idle_time) +/*void systemBus::setComlynxIdleTime(uint64_t idle_time) { Debug_printf("setComlynxIdleTime, %d\n", idle_time); ComLynx.comlynx_idle_time = idle_time; -} +}*/ systemBus ComLynx; diff --git a/lib/bus/comlynx/comlynx.h b/lib/bus/comlynx/comlynx.h index 98022223f..06a588827 100755 --- a/lib/bus/comlynx/comlynx.h +++ b/lib/bus/comlynx/comlynx.h @@ -269,7 +269,7 @@ class systemBus * stopwatch */ int64_t start_time; - int64_t comlynx_idle_time = 3000; + //int64_t comlynx_idle_time = 1000; int numDevices(); void addDevice(virtualDevice *pDevice, uint8_t device_id); @@ -286,7 +286,7 @@ class systemBus void setRedeyeMode(bool enable); void setRedeyeGameRemap(uint32_t remap); - void setComlynxIdleTime(uint64_t idle_time); + //void setComlynxIdleTime(uint64_t idle_time); bool shuttingDown = false; // TRUE if we are in shutdown process bool getShuttingDown() { return shuttingDown; }; diff --git a/lib/device/comlynx/disk.cpp b/lib/device/comlynx/disk.cpp index c0da7d43a..e54b1ba02 100755 --- a/lib/device/comlynx/disk.cpp +++ b/lib/device/comlynx/disk.cpp @@ -102,16 +102,18 @@ bool lynxDisk::write_blank(FILE *fileh, uint32_t numBlocks) void lynxDisk::comlynx_control_clr() { - int64_t t = esp_timer_get_time() - ComLynx.start_time; + //int64_t t = esp_timer_get_time() - ComLynx.start_time; - if (t < 1500) - { + //if (t < 1500) + //{ comlynx_response_send(); - } + //} } void lynxDisk::comlynx_control_receive() { + Debug_println("comlynx_control_receive()\n"); + if (_media == nullptr) return; @@ -123,12 +125,18 @@ void lynxDisk::comlynx_control_receive() void lynxDisk::comlynx_control_send_block_num() { - uint8_t x[8]; + uint8_t x[5]; // 32-bit block num, plus 1 byte reserved Debug_printf("comlynx_control_send_block_num()\n"); for (uint16_t i = 0; i < 5; i++) x[i] = comlynx_recv(); + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } + blockNum = x[3] << 24 | x[2] << 16 | x[1] << 8 | x[0]; if (_media->num_blocks() < 0x10000UL) // Smaller than 64MB? @@ -141,7 +149,7 @@ void lynxDisk::comlynx_control_send_block_num() _media->format(NULL); } - ComLynx.start_time=esp_timer_get_time(); + //ComLynx.start_time=esp_timer_get_time(); comlynx_response_ack(); @@ -154,7 +162,14 @@ void lynxDisk::comlynx_control_send_block_data() return; comlynx_recv_buffer(_media->_media_blockbuff, 256); - ComLynx.start_time = esp_timer_get_time(); + + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } + + //ComLynx.start_time = esp_timer_get_time(); comlynx_response_ack(); Debug_printf("Block Data Write\n"); @@ -182,12 +197,12 @@ void lynxDisk::comlynx_response_status() else status_response[4] = 0x40 | _media->_media_controller_status; - int64_t t = esp_timer_get_time() - ComLynx.start_time; + //int64_t t = esp_timer_get_time() - ComLynx.start_time; - if (t < 300) - { + //if (t < 300) + //{ virtualDevice::comlynx_response_status(); - } + //} } void lynxDisk::comlynx_response_send() diff --git a/lib/device/comlynx/fuji.cpp b/lib/device/comlynx/fuji.cpp index 5a8eeba41..230decf4c 100755 --- a/lib/device/comlynx/fuji.cpp +++ b/lib/device/comlynx/fuji.cpp @@ -698,7 +698,7 @@ void lynxFuji::comlynx_open_directory(uint16_t s) return; } - ComLynx.start_time = esp_timer_get_time(); + //ComLynx.start_time = esp_timer_get_time(); if (_current_open_directory_slot == -1) { @@ -867,7 +867,7 @@ void lynxFuji::comlynx_read_directory_entry() else { Debug_printf("Already filled. response is %s\n",response); - ComLynx.start_time = esp_timer_get_time(); + //ComLynx.start_time = esp_timer_get_time(); comlynx_response_ack(); } } @@ -989,7 +989,7 @@ void lynxFuji::comlynx_new_disk() if (host.file_exists((const char *)p)) { - ComLynx.start_time = esp_timer_get_time(); + //ComLynx.start_time = esp_timer_get_time(); comlynx_response_ack(); return; } @@ -1419,7 +1419,7 @@ void lynxFuji::comlynx_device_enable_status() return; } - ComLynx.start_time = esp_timer_get_time(); + //ComLynx.start_time = esp_timer_get_time(); if (ComLynx.deviceExists(d)) comlynx_response_ack(); diff --git a/lib/device/comlynx/network.cpp b/lib/device/comlynx/network.cpp index 3e724b676..c18efb43d 100755 --- a/lib/device/comlynx/network.cpp +++ b/lib/device/comlynx/network.cpp @@ -88,9 +88,14 @@ void lynxNetwork::open(unsigned short s) memset(response, 0, sizeof(response)); comlynx_recv_buffer(response, s); - comlynx_recv(); // checksum + + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } - ComLynx.start_time = esp_timer_get_time(); + //ComLynx.start_time = esp_timer_get_time(); comlynx_response_ack(); channelMode = PROTOCOL; @@ -157,9 +162,13 @@ void lynxNetwork::close() { Debug_printf("lynxNetwork::close()\n"); - comlynx_recv(); // CK + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } - ComLynx.start_time = esp_timer_get_time(); + //ComLynx.start_time = esp_timer_get_time(); comlynx_response_ack(); statusByte.byte = 0x00; @@ -217,9 +226,14 @@ void lynxNetwork::write(uint16_t num_bytes) memset(response, 0, sizeof(response)); comlynx_recv_buffer(response, num_bytes); - comlynx_recv(); // CK + + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } - ComLynx.start_time = esp_timer_get_time(); + //ComLynx.start_time = esp_timer_get_time(); comlynx_response_ack(); *transmitBuffer += string((char *)response, num_bytes); @@ -257,8 +271,14 @@ bool lynxNetwork::comlynx_write_channel(unsigned short num_bytes) void lynxNetwork::status() { NetworkStatus s; - comlynx_recv(); // CK - ComLynx.start_time = esp_timer_get_time(); + + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } + + //ComLynx.start_time = esp_timer_get_time(); comlynx_response_ack(); switch (channelMode) @@ -290,9 +310,13 @@ void lynxNetwork::status() */ void lynxNetwork::get_prefix() { - comlynx_recv(); // CK + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } - ComLynx.start_time = esp_timer_get_time(); + //ComLynx.start_time = esp_timer_get_time(); comlynx_response_ack(); Debug_printf("lynxNetwork::comlynx_getprefix(%s)\n", prefix.c_str()); @@ -311,9 +335,14 @@ void lynxNetwork::set_prefix(unsigned short s) memset(prefixSpec, 0, sizeof(prefixSpec)); comlynx_recv_buffer(prefixSpec, s); - comlynx_recv(); // CK + + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } - ComLynx.start_time = esp_timer_get_time(); + //ComLynx.start_time = esp_timer_get_time(); comlynx_response_ack(); prefixSpec_str = string((const char *)prefixSpec); @@ -370,9 +399,14 @@ void lynxNetwork::set_login(uint16_t s) memset(loginspec, 0, sizeof(loginspec)); comlynx_recv_buffer(loginspec, s); - comlynx_recv(); // ck + + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } - ComLynx.start_time = esp_timer_get_time(); + //ComLynx.start_time = esp_timer_get_time(); comlynx_response_ack(); login = string((char *)loginspec, s); @@ -388,9 +422,14 @@ void lynxNetwork::set_password(uint16_t s) memset(passwordspec, 0, sizeof(passwordspec)); comlynx_recv_buffer(passwordspec, s); - comlynx_recv(); // ck + + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } - ComLynx.start_time = esp_timer_get_time(); + //ComLynx.start_time = esp_timer_get_time(); comlynx_response_ack(); password = string((char *)passwordspec, s); @@ -402,9 +441,14 @@ void lynxNetwork::del(uint16_t s) memset(response, 0, sizeof(response)); comlynx_recv_buffer(response, s); - comlynx_recv(); // CK + + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } - ComLynx.start_time = esp_timer_get_time(); + //ComLynx.start_time = esp_timer_get_time(); comlynx_response_ack(); d = string((char *)response, s); @@ -428,9 +472,14 @@ void lynxNetwork::rename(uint16_t s) memset(response, 0, sizeof(response)); comlynx_recv_buffer(response, s); - comlynx_recv(); // CK + + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } - ComLynx.start_time = esp_timer_get_time(); + //ComLynx.start_time = esp_timer_get_time(); comlynx_response_ack(); d = string((char *)response, s); @@ -451,9 +500,14 @@ void lynxNetwork::mkdir(uint16_t s) memset(response, 0, sizeof(response)); comlynx_recv_buffer(response, s); - comlynx_recv(); // CK + + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } - ComLynx.start_time = esp_timer_get_time(); + //ComLynx.start_time = esp_timer_get_time(); comlynx_response_ack(); d = string((char *)response, s); @@ -471,28 +525,33 @@ void lynxNetwork::mkdir(uint16_t s) void lynxNetwork::channel_mode() { unsigned char m = comlynx_recv(); - comlynx_recv(); // CK + + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } switch (m) { case 0: channelMode = PROTOCOL; - ComLynx.start_time = esp_timer_get_time(); + //ComLynx.start_time = esp_timer_get_time(); comlynx_response_ack(); break; case 1: channelMode = JSON; - ComLynx.start_time = esp_timer_get_time(); + //ComLynx.start_time = esp_timer_get_time(); comlynx_response_ack(); break; default: - ComLynx.start_time = esp_timer_get_time(); + //ComLynx.start_time = esp_timer_get_time(); comlynx_response_nack(); break; } Debug_printf("lynxNetwork::channel_mode(%u)\n", m); - ComLynx.start_time = esp_timer_get_time(); + //ComLynx.start_time = esp_timer_get_time(); comlynx_response_ack(); } @@ -501,9 +560,14 @@ void lynxNetwork::json_query(unsigned short s) uint8_t *c = (uint8_t *)malloc(s); comlynx_recv_buffer(c, s); - comlynx_recv(); // CK + + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } - ComLynx.start_time = esp_timer_get_time(); + //ComLynx.start_time = esp_timer_get_time(); comlynx_response_ack(); json.setReadQuery(std::string((char *)c, s),cmdFrame.aux2); @@ -515,8 +579,13 @@ void lynxNetwork::json_query(unsigned short s) void lynxNetwork::json_parse() { - comlynx_recv(); // CK - ComLynx.start_time = esp_timer_get_time(); + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } + + //ComLynx.start_time = esp_timer_get_time(); comlynx_response_ack(); json.parse(); } @@ -592,7 +661,7 @@ void lynxNetwork::comlynx_special_00(unsigned short s) cmdFrame.aux1 = comlynx_recv(); cmdFrame.aux2 = comlynx_recv(); - ComLynx.start_time = esp_timer_get_time(); + //ComLynx.start_time = esp_timer_get_time(); if (protocol->special_00(&cmdFrame) == false) comlynx_response_ack(); @@ -659,9 +728,9 @@ void lynxNetwork::comlynx_response_status() status_response[4] = statusByte.byte; - int64_t t = esp_timer_get_time() - ComLynx.start_time; + //int64_t t = esp_timer_get_time() - ComLynx.start_time; - if (t < 300) + //if (t < 300) virtualDevice::comlynx_response_status(); } @@ -783,7 +852,7 @@ void lynxNetwork::comlynx_control_receive_channel_json() } else { - ComLynx.start_time = esp_timer_get_time(); + //ComLynx.start_time = esp_timer_get_time(); if (response_len > 0) comlynx_response_ack(); else @@ -830,7 +899,7 @@ void lynxNetwork::comlynx_control_receive_channel_protocol() void lynxNetwork::comlynx_control_receive() { - ComLynx.start_time = esp_timer_get_time(); + //ComLynx.start_time = esp_timer_get_time(); // Data is waiting, go ahead and send it off. if (response_len > 0) From 5d2f95eef3c9794657f300210f7ca0e67504faef Mon Sep 17 00:00:00 2001 From: Shawn Date: Fri, 12 Sep 2025 15:04:14 -0700 Subject: [PATCH 05/12] lynx disk changes - partial blocks accounted for - checksum, ack/nack receive in disk block send media type changes - basically removed media type detection, treat all files the same misc cleanup --- lib/bus/comlynx/comlynx.cpp | 50 ++++++++++----------------------- lib/bus/comlynx/comlynx.h | 4 +-- lib/device/comlynx/disk.cpp | 29 +++++++------------ lib/device/comlynx/disk.h | 3 ++ lib/device/comlynx/network.cpp | 4 +-- lib/media/lynx/mediaType.cpp | 11 ++++++-- lib/media/lynx/mediaTypeROM.cpp | 6 ++-- 7 files changed, 44 insertions(+), 63 deletions(-) diff --git a/lib/bus/comlynx/comlynx.cpp b/lib/bus/comlynx/comlynx.cpp index f774230ec..4b2f1c7b5 100755 --- a/lib/bus/comlynx/comlynx.cpp +++ b/lib/bus/comlynx/comlynx.cpp @@ -233,11 +233,9 @@ void virtualDevice::comlynx_control_ready() comlynx_response_ack(); } -void systemBus::wait_for_idle() +bool systemBus::wait_for_idle() { - bool isIdle = false; int64_t start, current, dur; - //int trashCount = 0; // SJ notes: we really don't need to do this unless we are in UDPStream mode // Likely we want to just wait until the bus is "idle" for about 3 character times @@ -245,40 +243,22 @@ void systemBus::wait_for_idle() // // Check that the bus is truly idle for the whole duration, and then we can start sending? - //do - //{ - /* - // Wait for serial line to quiet down. - while (fnUartBUS.available() > 0) - { - fnUartBUS.read(); - trashCount++; - } + start = esp_timer_get_time(); + + do { + current = esp_timer_get_time(); + dur = current - start; - if (trashCount > 0) - Debug_printf("wait_for_idle() dropped %d bytes\n", trashCount); - */ + // Did we get any data in the FIFO while waiting? + if (fnUartBUS.available() > 0) + return false; - // Wait for serial line to quiet down. - while (fnUartBUS.available() > 0); + } while (dur < IDLE_TIME); - start = current = esp_timer_get_time(); + // Must have been idle at least IDLE_TIME to get here + return true; - // Wait for IDLE_TIME duration before calling it idle - while ((fnUartBUS.available() <= 0) && (isIdle == false)) - { - current = esp_timer_get_time(); - dur = current - start; - //#ifdef DEBUG - // Debug_printf("wait_for_idle() - not idle, dur:%d\n", dur); - //#endif - - if (dur > IDLE_TIME) - //if (dur > ComLynx.comlynx_idle_time) - isIdle = true; - } - //} while (isIdle == false); - fnSystem.yield(); + //fnSystem.yield(); // not sure if we need to do this, from old function - SJ } void virtualDevice::comlynx_process(uint8_t b) @@ -332,11 +312,11 @@ void systemBus::_comlynx_process_cmd() uint8_t b; b = fnUartBUS.read(); - start_time = esp_timer_get_time(); + //start_time = esp_timer_get_time(); uint8_t d = b & 0x0F; - Debug_printf("comlynx_process_cmd: dev:%d\n", d); + Debug_printf("comlynx_process_cmd: dev:%X cmd:%X\n", d, (b & 0xF0)); // Find device ID and pass control to it if (_daisyChain.count(d) < 1) diff --git a/lib/bus/comlynx/comlynx.h b/lib/bus/comlynx/comlynx.h index 06a588827..c2cd65b8d 100755 --- a/lib/bus/comlynx/comlynx.h +++ b/lib/bus/comlynx/comlynx.h @@ -261,9 +261,9 @@ class systemBus void reset(); /** - * @brief Wait for Comlynx bus to become idle. + * @brief Wait to see if Comlynx bus is idle. */ - void wait_for_idle(); + bool wait_for_idle(); /** * stopwatch diff --git a/lib/device/comlynx/disk.cpp b/lib/device/comlynx/disk.cpp index e54b1ba02..7a42d8986 100755 --- a/lib/device/comlynx/disk.cpp +++ b/lib/device/comlynx/disk.cpp @@ -87,7 +87,7 @@ bool lynxDisk::write_blank(FILE *fileh, uint32_t numBlocks) { uint8_t buf[256]; - memset(buf, 0xE5, 256); + memset(buf, 0xE5, 256); // On the lynx, maybe set to 0x00 or 0xFF -SJ for (uint32_t b = 0; b < numBlocks; b++) { @@ -102,17 +102,12 @@ bool lynxDisk::write_blank(FILE *fileh, uint32_t numBlocks) void lynxDisk::comlynx_control_clr() { - //int64_t t = esp_timer_get_time() - ComLynx.start_time; - - //if (t < 1500) - //{ - comlynx_response_send(); - //} + comlynx_response_send(); } void lynxDisk::comlynx_control_receive() { - Debug_println("comlynx_control_receive()\n"); + Debug_println("comlynx_control_receive() - Disk block read started\n"); if (_media == nullptr) return; @@ -127,7 +122,7 @@ void lynxDisk::comlynx_control_send_block_num() { uint8_t x[5]; // 32-bit block num, plus 1 byte reserved - Debug_printf("comlynx_control_send_block_num()\n"); + Debug_printf("comlynx_control_send_block_num\n"); for (uint16_t i = 0; i < 5; i++) x[i] = comlynx_recv(); @@ -148,8 +143,6 @@ void lynxDisk::comlynx_control_send_block_num() { _media->format(NULL); } - - //ComLynx.start_time=esp_timer_get_time(); comlynx_response_ack(); @@ -169,7 +162,6 @@ void lynxDisk::comlynx_control_send_block_data() return; } - //ComLynx.start_time = esp_timer_get_time(); comlynx_response_ack(); Debug_printf("Block Data Write\n"); @@ -183,7 +175,7 @@ void lynxDisk::comlynx_control_send() { uint16_t s = comlynx_recv_length(); - Debug_printf("S is %u\n",s); + Debug_printf("disk_control_send, S is %u\n",s); if (s == 5) comlynx_control_send_block_num(); else if (s == 256) @@ -197,12 +189,7 @@ void lynxDisk::comlynx_response_status() else status_response[4] = 0x40 | _media->_media_controller_status; - //int64_t t = esp_timer_get_time() - ComLynx.start_time; - - //if (t < 300) - //{ - virtualDevice::comlynx_response_status(); - //} + virtualDevice::comlynx_response_status(); } void lynxDisk::comlynx_response_send() @@ -220,6 +207,10 @@ void lynxDisk::comlynx_response_send() b[2] = 0x00; b[259] = c; comlynx_send_buffer(b, sizeof(b)); + + // get ACK or NACK from lynx (but don't care, they can request again if needed) + c = comlynx_recv(); + Debug_printf("comlynx_disk_send NAK/ACK: %02X\n", c); } void lynxDisk::comlynx_process(uint8_t b) diff --git a/lib/device/comlynx/disk.h b/lib/device/comlynx/disk.h index 730702f0d..44fd173ab 100755 --- a/lib/device/comlynx/disk.h +++ b/lib/device/comlynx/disk.h @@ -10,6 +10,9 @@ #define STATUS_NO_MEDIA 3 #define STATUS_NO_DRIVE 4 +// disk block size, on lynx doesn't need to remain 256 +#define BLOCK_SIZE 256 + class lynxDisk : public virtualDevice { private: diff --git a/lib/device/comlynx/network.cpp b/lib/device/comlynx/network.cpp index c18efb43d..c2afc1d5e 100755 --- a/lib/device/comlynx/network.cpp +++ b/lib/device/comlynx/network.cpp @@ -899,9 +899,7 @@ void lynxNetwork::comlynx_control_receive_channel_protocol() void lynxNetwork::comlynx_control_receive() { - //ComLynx.start_time = esp_timer_get_time(); - - // Data is waiting, go ahead and send it off. + // Data is waiting, go ahead and send it off. if (response_len > 0) { comlynx_response_ack(); diff --git a/lib/media/lynx/mediaType.cpp b/lib/media/lynx/mediaType.cpp index 2bf12e9d2..b05c5341d 100644 --- a/lib/media/lynx/mediaType.cpp +++ b/lib/media/lynx/mediaType.cpp @@ -37,17 +37,24 @@ void MediaType::unmount() mediatype_t MediaType::discover_mediatype(const char *filename) { + // On the Lynx, we can just treat every file like a ROM file + // We aren't doing anything special with the files at this point + // but might need to change in future. + + /* int l = strlen(filename); if (l > 4 && filename[l - 4] == '.') { // Check the last 3 characters of the string const char *ext = filename + l - 3; - if (strcasecmp(ext, "LNX") == 0) + if ((strcasecmp(ext, "LNX") == 0) || (strcasecmp(ext, "LYX") = 0)) { return MEDIATYPE_ROM; } } - return MEDIATYPE_UNKNOWN; + return MEDIATYPE_UNKNOWN;*/ + + return MEDIATYPE_ROM; } #endif /* BUILD_LYNX */ \ No newline at end of file diff --git a/lib/media/lynx/mediaTypeROM.cpp b/lib/media/lynx/mediaTypeROM.cpp index aa1ffdd9e..06aa7b668 100644 --- a/lib/media/lynx/mediaTypeROM.cpp +++ b/lib/media/lynx/mediaTypeROM.cpp @@ -42,7 +42,7 @@ bool MediaTypeROM::read(uint32_t blockNum, uint16_t *readcount) } if (err == false) - err = fread(_media_blockbuff, 1, 256, _media_fileh) != 256; + err = fread(_media_blockbuff, 1, 256, _media_fileh) == 0; // handle potential last block partial read if (err == false) { @@ -83,7 +83,9 @@ mediatype_t MediaTypeROM::mount(FILE *f, uint32_t disksize) _media_fileh = f; _mediatype = MEDIATYPE_ROM; - _media_num_blocks = disksize / 256; + _media_num_blocks = disksize / MEDIA_BLOCK_SIZE; + if (_media_num_blocks % MEDIA_BLOCK_SIZE) // handle extra bytes + _media_num_blocks++; return _mediatype; } From 645b6964f47430b7b04bcca1c9f44aa9a4f51075 Mon Sep 17 00:00:00 2001 From: Shawn Date: Fri, 19 Sep 2025 21:49:03 -0700 Subject: [PATCH 06/12] Use MEDIA_BLOCK_SIZE in all disk routines, general cleanup --- lib/bus/comlynx/comlynx.cpp | 6 +++--- lib/device/comlynx/disk.cpp | 37 +++++++++++++++++++-------------- lib/device/comlynx/disk.h | 2 -- lib/device/comlynx/fuji.cpp | 5 +---- lib/media/lynx/mediaType.h | 26 +---------------------- lib/media/lynx/mediaTypeROM.cpp | 8 +++---- 6 files changed, 30 insertions(+), 54 deletions(-) diff --git a/lib/bus/comlynx/comlynx.cpp b/lib/bus/comlynx/comlynx.cpp index 4b2f1c7b5..c0c8238a8 100755 --- a/lib/bus/comlynx/comlynx.cpp +++ b/lib/bus/comlynx/comlynx.cpp @@ -93,14 +93,14 @@ void virtualDevice::comlynx_send(uint8_t b) void virtualDevice::comlynx_send_buffer(uint8_t *buf, unsigned short len) { //Debug_printf("comlynx_send_buffer: %d %s\n", len, buf); - Debug_printf("comlynx_send_buffer: len:%d %0X %0X %0X %0X %0X %0X\n", len, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]); + Debug_printf("comlynx_send_buffer: len:%d %0X %0X %0X %0X %0X %0X\n", len, buf[0], buf[1], buf[2], buf[3], buf[4], buf[len-1]); // Wait for idle only when in UDPStream mode if (ComLynx._udpDev->udpstreamActive) ComLynx.wait_for_idle(); - fnUartBUS.write(buf,len); - fnUartBUS.readBytes(buf,len); + fnUartBUS.write(buf, len); + fnUartBUS.readBytes(buf, len); } bool virtualDevice::comlynx_recv_ck() diff --git a/lib/device/comlynx/disk.cpp b/lib/device/comlynx/disk.cpp index 7a42d8986..6ff2e842f 100755 --- a/lib/device/comlynx/disk.cpp +++ b/lib/device/comlynx/disk.cpp @@ -14,8 +14,11 @@ lynxDisk::lynxDisk() { device_active = false; blockNum = 0; - status_response[1] = 0x00; - status_response[2] = 0x01; // 256 bytes + //status_response[1] = 0x00; + //status_response[2] = 0x01; // 256 bytes + + status_response[1] = MEDIA_BLOCK_SIZE % 256; + status_response[2] = MEDIA_BLOCK_SIZE / 256; status_response[3] = 0x01; // Block device } @@ -85,16 +88,16 @@ void lynxDisk::unmount() bool lynxDisk::write_blank(FILE *fileh, uint32_t numBlocks) { - uint8_t buf[256]; + uint8_t buf[MEDIA_BLOCK_SIZE]; - memset(buf, 0xE5, 256); // On the lynx, maybe set to 0x00 or 0xFF -SJ + memset(buf, 0x00, MEDIA_BLOCK_SIZE); for (uint32_t b = 0; b < numBlocks; b++) { - fwrite(buf, 1, 256, fileh); - fwrite(buf, 1, 256, fileh); - fwrite(buf, 1, 256, fileh); - fwrite(buf, 1, 256, fileh); + fwrite(buf, 1, MEDIA_BLOCK_SIZE, fileh); // I don't understand why we do this four times? -SJ + fwrite(buf, 1, MEDIA_BLOCK_SIZE, fileh); + fwrite(buf, 1, MEDIA_BLOCK_SIZE, fileh); + fwrite(buf, 1, MEDIA_BLOCK_SIZE, fileh); } return false; @@ -154,7 +157,7 @@ void lynxDisk::comlynx_control_send_block_data() if (_media == nullptr) return; - comlynx_recv_buffer(_media->_media_blockbuff, 256); + comlynx_recv_buffer(_media->_media_blockbuff, MEDIA_BLOCK_SIZE); // Get packet checksum if (!comlynx_recv_ck()) { @@ -178,7 +181,7 @@ void lynxDisk::comlynx_control_send() Debug_printf("disk_control_send, S is %u\n",s); if (s == 5) comlynx_control_send_block_num(); - else if (s == 256) + else if (s == MEDIA_BLOCK_SIZE) comlynx_control_send_block_data(); } @@ -197,17 +200,19 @@ void lynxDisk::comlynx_response_send() if (_media == nullptr) return; - uint8_t c = comlynx_checksum(_media->_media_blockbuff, 256); - uint8_t b[260]; + uint8_t c = comlynx_checksum(_media->_media_blockbuff, MEDIA_BLOCK_SIZE); + uint8_t b[MEDIA_BLOCK_SIZE+4]; - memcpy(&b[3], _media->_media_blockbuff, 256); + memcpy(&b[3], _media->_media_blockbuff, MEDIA_BLOCK_SIZE); b[0] = 0xB0 | _devnum; - b[1] = 0x01; - b[2] = 0x00; - b[259] = c; + b[1] = MEDIA_BLOCK_SIZE / 256; // block length + b[2] = MEDIA_BLOCK_SIZE % 256; + b[MEDIA_BLOCK_SIZE+3] = c; comlynx_send_buffer(b, sizeof(b)); + Debug_println("comlynx_send_buffer from disk block send done"); + // get ACK or NACK from lynx (but don't care, they can request again if needed) c = comlynx_recv(); Debug_printf("comlynx_disk_send NAK/ACK: %02X\n", c); diff --git a/lib/device/comlynx/disk.h b/lib/device/comlynx/disk.h index 44fd173ab..05f17f5ff 100755 --- a/lib/device/comlynx/disk.h +++ b/lib/device/comlynx/disk.h @@ -10,8 +10,6 @@ #define STATUS_NO_MEDIA 3 #define STATUS_NO_DRIVE 4 -// disk block size, on lynx doesn't need to remain 256 -#define BLOCK_SIZE 256 class lynxDisk : public virtualDevice { diff --git a/lib/device/comlynx/fuji.cpp b/lib/device/comlynx/fuji.cpp index 230decf4c..b6210c3d1 100755 --- a/lib/device/comlynx/fuji.cpp +++ b/lib/device/comlynx/fuji.cpp @@ -122,8 +122,7 @@ void lynxFuji::comlynx_net_scan_networks() void lynxFuji::comlynx_net_scan_result() { Debug_println("Fuji cmd: GET SCAN RESULT"); - //scanStarted = false; - + uint8_t n = comlynx_recv(); // Get packet checksum @@ -1419,8 +1418,6 @@ void lynxFuji::comlynx_device_enable_status() return; } - //ComLynx.start_time = esp_timer_get_time(); - if (ComLynx.deviceExists(d)) comlynx_response_ack(); else diff --git a/lib/media/lynx/mediaType.h b/lib/media/lynx/mediaType.h index c9782a697..8917f39dc 100755 --- a/lib/media/lynx/mediaType.h +++ b/lib/media/lynx/mediaType.h @@ -7,10 +7,6 @@ #define MEDIA_BLOCK_SIZE 256 -#define DISK_BYTES_PER_SECTOR_SINGLE 128 -#define DISK_BYTES_PER_SECTOR_DOUBLE 256 -#define DISK_BYTES_PER_SECTOR_DOUBLE_DOUBLE 512 - #define DISK_CTRL_STATUS_CLEAR 0x00 enum mediatype_t @@ -28,31 +24,14 @@ class MediaType FILE *_media_fileh = nullptr; uint32_t _media_image_size = 0; uint32_t _media_num_blocks = 256; - uint16_t _media_sector_size = DISK_BYTES_PER_SECTOR_SINGLE; + //uint16_t _media_sector_size = DISK_BYTES_PER_SECTOR_SINGLE; public: - struct - { - uint8_t num_tracks; - uint8_t step_rate; - uint8_t sectors_per_trackH; - uint8_t sectors_per_trackL; - uint8_t num_sides; - uint8_t density; - uint8_t sector_sizeH; - uint8_t sector_sizeL; - uint8_t drive_present; - uint8_t reserved1; - uint8_t reserved2; - uint8_t reserved3; - } _percomBlock; - uint8_t _media_blockbuff[MEDIA_BLOCK_SIZE]; uint32_t _media_last_block = INVALID_SECTOR_VALUE-1; uint8_t _media_controller_status = DISK_CTRL_STATUS_CLEAR; mediatype_t _mediatype = MEDIATYPE_UNKNOWN; - bool _allow_hsio = true; virtual mediatype_t mount(FILE *f, uint32_t disksize) = 0; virtual void unmount(); @@ -69,9 +48,6 @@ class MediaType static mediatype_t discover_mediatype(const char *filename); - void dump_percom_block(); - void derive_percom_block(uint16_t numSectors); - uint32_t num_blocks() { return _media_num_blocks; } virtual ~MediaType(); diff --git a/lib/media/lynx/mediaTypeROM.cpp b/lib/media/lynx/mediaTypeROM.cpp index 06aa7b668..babfdbc0d 100644 --- a/lib/media/lynx/mediaTypeROM.cpp +++ b/lib/media/lynx/mediaTypeROM.cpp @@ -11,7 +11,7 @@ // Returns byte offset of given sector number uint32_t MediaTypeROM::_block_to_offset(uint32_t blockNum) { - return blockNum * 256; + return blockNum * MEDIA_BLOCK_SIZE; } // Returns TRUE if an error condition occurred @@ -20,7 +20,7 @@ bool MediaTypeROM::read(uint32_t blockNum, uint16_t *readcount) if (blockNum == _media_last_block) return false; // We already have block. - Debug_print("LNX READ\r\n"); + Debug_print("**DISK READ START**\r\n"); // Return an error if we're trying to read beyond the end of the disk if (blockNum > _media_num_blocks-1) @@ -42,7 +42,7 @@ bool MediaTypeROM::read(uint32_t blockNum, uint16_t *readcount) } if (err == false) - err = fread(_media_blockbuff, 1, 256, _media_fileh) == 0; // handle potential last block partial read + err = fread(_media_blockbuff, 1, MEDIA_BLOCK_SIZE, _media_fileh) == 0; // handle potential last block partial read if (err == false) { @@ -79,7 +79,7 @@ bool MediaTypeROM::format(uint16_t *responsesize) mediatype_t MediaTypeROM::mount(FILE *f, uint32_t disksize) { - Debug_print("ROM MOUNT\r\n"); + Debug_print("**FILE MOUNT**\r\n"); _media_fileh = f; _mediatype = MEDIATYPE_ROM; From 32bde9d2591d869245c10af5334c2a7a638feef5 Mon Sep 17 00:00:00 2001 From: Shawn Date: Mon, 22 Sep 2025 14:14:59 -0700 Subject: [PATCH 07/12] Fixed bug in blocksize calculation General cleanup, fixes --- lib/bus/comlynx/comlynx.cpp | 2 +- lib/media/lynx/mediaTypeROM.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/bus/comlynx/comlynx.cpp b/lib/bus/comlynx/comlynx.cpp index c0c8238a8..a737b76d8 100755 --- a/lib/bus/comlynx/comlynx.cpp +++ b/lib/bus/comlynx/comlynx.cpp @@ -316,7 +316,7 @@ void systemBus::_comlynx_process_cmd() uint8_t d = b & 0x0F; - Debug_printf("comlynx_process_cmd: dev:%X cmd:%X\n", d, (b & 0xF0)); + Debug_printf("comlynx_process_cmd: dev:%X cmd:%X\n", d, (b & 0xF0)>>4); // Find device ID and pass control to it if (_daisyChain.count(d) < 1) diff --git a/lib/media/lynx/mediaTypeROM.cpp b/lib/media/lynx/mediaTypeROM.cpp index babfdbc0d..10152127b 100644 --- a/lib/media/lynx/mediaTypeROM.cpp +++ b/lib/media/lynx/mediaTypeROM.cpp @@ -84,7 +84,7 @@ mediatype_t MediaTypeROM::mount(FILE *f, uint32_t disksize) _media_fileh = f; _mediatype = MEDIATYPE_ROM; _media_num_blocks = disksize / MEDIA_BLOCK_SIZE; - if (_media_num_blocks % MEDIA_BLOCK_SIZE) // handle extra bytes + if (disksize % MEDIA_BLOCK_SIZE) // handle extra bytes _media_num_blocks++; return _mediatype; From cb851961276334291fbd7a48251e358f0b64c7da Mon Sep 17 00:00:00 2001 From: Shawn Date: Thu, 9 Oct 2025 20:42:01 -0700 Subject: [PATCH 08/12] mostly debugging message cleanup (lynx serial packets start with ---) some fixes in json parse function, took code from sio implementation --- lib/bus/comlynx/comlynx.cpp | 17 +++++-- lib/device/comlynx/network.cpp | 87 ++++++++++++++++++++++++++++------ lib/device/comlynx/network.h | 5 ++ 3 files changed, 90 insertions(+), 19 deletions(-) diff --git a/lib/bus/comlynx/comlynx.cpp b/lib/bus/comlynx/comlynx.cpp index a737b76d8..06f6d0957 100755 --- a/lib/bus/comlynx/comlynx.cpp +++ b/lib/bus/comlynx/comlynx.cpp @@ -92,8 +92,9 @@ void virtualDevice::comlynx_send(uint8_t b) void virtualDevice::comlynx_send_buffer(uint8_t *buf, unsigned short len) { - //Debug_printf("comlynx_send_buffer: %d %s\n", len, buf); - Debug_printf("comlynx_send_buffer: len:%d %0X %0X %0X %0X %0X %0X\n", len, buf[0], buf[1], buf[2], buf[3], buf[4], buf[len-1]); + buf[len] = '\0'; + Debug_printf("comlynx_send_buffer: %d %s\n", len, buf); + //Debug_printf("comlynx_send_buffer: len:%d %0X %0X %0X %0X %0X %0X\n", len, buf[0], buf[1], buf[2], buf[3], buf[4], buf[len-1]); // Wait for idle only when in UDPStream mode if (ComLynx._udpDev->udpstreamActive) @@ -116,7 +117,8 @@ bool virtualDevice::comlynx_recv_ck() ck = comlynx_checksum(recvbuffer, recvbuffer_len); - Debug_printf("comlynx_recv_ck, recv:%02X calc:%02X\n", recv_ck, ck); + // debugging checksum values + //Debug_printf("comlynx_recv_ck, recv:%02X calc:%02X\n", recv_ck, ck); // reset receive buffer recvbuffer_len = 0; @@ -316,7 +318,14 @@ void systemBus::_comlynx_process_cmd() uint8_t d = b & 0x0F; - Debug_printf("comlynx_process_cmd: dev:%X cmd:%X\n", d, (b & 0xF0)>>4); + #ifdef DEBUG + if ((b & 0xF0) == (MN_ACK<<4)) + Debug_println("Lynx sent ACK"); + else { + Debug_println("---"); + Debug_printf("comlynx_process_cmd: dev:%X cmd:%X\n", d, (b & 0xF0)>>4); + } + #endif // Find device ID and pass control to it if (_daisyChain.count(d) < 1) diff --git a/lib/device/comlynx/network.cpp b/lib/device/comlynx/network.cpp index c2afc1d5e..e11fe49b5 100755 --- a/lib/device/comlynx/network.cpp +++ b/lib/device/comlynx/network.cpp @@ -33,8 +33,12 @@ */ lynxNetwork::lynxNetwork() { - status_response[1] = 0x00; - status_response[2] = 0x04; // 1024 bytes + //status_response[1] = 0x00; + //status_response[2] = 0x04; // 1024 bytes + + status_response[1] = SERIAL_PACKET_SIZE % 256; + status_response[2] = SERIAL_PACKET_SIZE / 256; + status_response[3] = 0x00; // Character device receiveBuffer = new string(); @@ -168,7 +172,6 @@ void lynxNetwork::close() return; } - //ComLynx.start_time = esp_timer_get_time(); comlynx_response_ack(); statusByte.byte = 0x00; @@ -222,7 +225,7 @@ bool lynxNetwork::read_channel(unsigned short num_bytes) */ void lynxNetwork::write(uint16_t num_bytes) { - Debug_printf("!!! WRITE\n"); + Debug_printf("lynxNetwork::write\n"); memset(response, 0, sizeof(response)); comlynx_recv_buffer(response, num_bytes); @@ -557,7 +560,8 @@ void lynxNetwork::channel_mode() void lynxNetwork::json_query(unsigned short s) { - uint8_t *c = (uint8_t *)malloc(s); + /* + uint8_t *c = (uint8_t *) malloc(s+1); comlynx_recv_buffer(c, s); @@ -570,11 +574,60 @@ void lynxNetwork::json_query(unsigned short s) //ComLynx.start_time = esp_timer_get_time(); comlynx_response_ack(); - json.setReadQuery(std::string((char *)c, s),cmdFrame.aux2); + json.setReadQuery(std::string((char *)c, s), cmdFrame.aux2); Debug_printf("lynxNetwork::json_query(%s)\n", c); free(c); + */ + + uint8_t in[256]; + + memset(in, 0, sizeof(in)); + comlynx_recv_buffer(in, s); + + // Get packet checksum + if (!comlynx_recv_ck()) { + comlynx_response_nack(); + return; + } + + comlynx_response_ack(); + + // strip away line endings from input spec. + for (int i = 0; i < s; i++) + { + if (in[i] == 0x0A || in[i] == 0x0D || in[i] == 0x9b) + in[i] = 0x00; + } + + std::string in_string(reinterpret_cast(in)); + size_t last_colon_pos = in_string.rfind(':'); + + std::string inp_string; + if (last_colon_pos != std::string::npos) { + // Skip the device spec. There was a debug message here, + // but it was removed, because there are cases where + // removing the devicespec isn't possible, e.g. accessing + // via CIO (as an XIO). -thom + inp_string = in_string.substr(last_colon_pos + 1); + } else { + inp_string = in_string; + } + + json.setReadQuery(inp_string, cmdFrame.aux2); + + /*json_bytes_remaining = json->json_bytes_remaining; + + std::vector tmp(json_bytes_remaining); + json->readValue(tmp.data(), json_bytes_remaining); + + // don't copy past first nul char in tmp + auto null_pos = std::find(tmp.begin(), tmp.end(), 0); + *receiveBuffer += std::string(tmp.begin(), null_pos);*/ + + Debug_printf("lynxNetwork::json_query(%s)\n", inp_string.c_str()); + } void lynxNetwork::json_parse() @@ -723,15 +776,13 @@ void lynxNetwork::comlynx_response_status() statusByte.bits.client_data_available = s.rxBytesWaiting > 0; statusByte.bits.client_error = s.error > 1; - status_response[1] = 2; // max packet size 1026 bytes, maybe larger? - status_response[2] = 4; - + //status_response[1] = 2; // max packet size 1026 bytes, maybe larger? + //status_response[2] = 4; + status_response[1] = (SERIAL_PACKET_SIZE % 256) + 2; // why +2? -SJ + status_response[2] = SERIAL_PACKET_SIZE / 256; status_response[4] = statusByte.byte; - //int64_t t = esp_timer_get_time() - ComLynx.start_time; - - //if (t < 300) - virtualDevice::comlynx_response_status(); + virtualDevice::comlynx_response_status(); } void lynxNetwork::comlynx_control_ack() @@ -879,7 +930,8 @@ void lynxNetwork::comlynx_control_receive_channel_protocol() } // Truncate bytes waiting to response size - ns.rxBytesWaiting = (ns.rxBytesWaiting > 1024) ? 1024 : ns.rxBytesWaiting; + //ns.rxBytesWaiting = (ns.rxBytesWaiting > 1024) ? 1024 : ns.rxBytesWaiting; + ns.rxBytesWaiting = (ns.rxBytesWaiting > SERIAL_PACKET_SIZE) ? SERIAL_PACKET_SIZE: ns.rxBytesWaiting; response_len = ns.rxBytesWaiting; if (protocol->read(response_len)) // protocol adapter returned error @@ -920,12 +972,17 @@ void lynxNetwork::comlynx_response_send() { uint8_t c = comlynx_checksum(response, response_len); - comlynx_send(0xB0 | _devnum); + //comlynx_send(0xB0 | _devnum); + comlynx_send((NM_SEND << 4) | _devnum); comlynx_send_length(response_len); comlynx_send_buffer(response, response_len); comlynx_send(c); + // print response we're sending + response[response_len] = '\0'; Debug_printf("comlynx_response_send: %s\n",response); + + // clear response for next time memset(response, 0, response_len); response_len = 0; } diff --git a/lib/device/comlynx/network.h b/lib/device/comlynx/network.h index 2b821565a..acb0352ea 100755 --- a/lib/device/comlynx/network.h +++ b/lib/device/comlynx/network.h @@ -20,6 +20,11 @@ */ #define NUM_DEVICES 8 +/* + * Size of the serial packet we can send to Lynx (determined mainly by the Lynx RX buffer) + */ +#define SERIAL_PACKET_SIZE 256 + /** * The size of rx and tx buffers */ From 6e1983634a6947e2f261f45e5c75bd43e26c9588 Mon Sep 17 00:00:00 2001 From: Shawn Date: Sat, 18 Oct 2025 21:46:58 -0700 Subject: [PATCH 09/12] Fixes differences from upstream --- lib/hardware/fnWiFi.cpp | 1 - platformio-ini-files/platformio.common.ini | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/hardware/fnWiFi.cpp b/lib/hardware/fnWiFi.cpp index 335b9900e..aaea82f28 100755 --- a/lib/hardware/fnWiFi.cpp +++ b/lib/hardware/fnWiFi.cpp @@ -328,7 +328,6 @@ uint8_t WiFiManager::scan_networks(uint8_t maxresults) temporary_disconnect = true; _disconnecting = true; esp_wifi_disconnect(); - sleep(2); _disconnecting = false; } diff --git a/platformio-ini-files/platformio.common.ini b/platformio-ini-files/platformio.common.ini index f34eb3274..1e2b29dd5 100644 --- a/platformio-ini-files/platformio.common.ini +++ b/platformio-ini-files/platformio.common.ini @@ -83,10 +83,10 @@ build_flags = ;build_board = fujinet-iec-lolin-d32 ; Commodore IEC using Lolin D32 Pro ;build_board = fujinet-iec-nugget ; Commodore IEC using Lolin D32 Pro "Nugget" -build_platform = BUILD_LYNX -build_bus = comlynx +;build_platform = BUILD_LYNX +;build_bus = comlynx ;build_board = fujinet-lynx-prototype ; Lynx Prototype PCB -build_board = fujinet-lynx-devkitc ; Lynx with basic DEVKITC +;build_board = fujinet-lynx-devkitc ; Lynx with basic DEVKITC ;build_platform = BUILD_S100 ;build_bus = s100Bus From 219835db6264eec5a1d6689ffa52778b9b90f000 Mon Sep 17 00:00:00 2001 From: Shawn Date: Sun, 19 Oct 2025 15:29:52 -0700 Subject: [PATCH 10/12] fixed out of bounds write to array in comlynx_send_buffer debugging statement --- lib/bus/comlynx/comlynx.cpp | 9 ++++++--- lib/device/comlynx/disk.cpp | 3 ++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/bus/comlynx/comlynx.cpp b/lib/bus/comlynx/comlynx.cpp index 925ff9d6a..a97008996 100755 --- a/lib/bus/comlynx/comlynx.cpp +++ b/lib/bus/comlynx/comlynx.cpp @@ -94,10 +94,13 @@ void virtualDevice::comlynx_send(uint8_t b) void virtualDevice::comlynx_send_buffer(uint8_t *buf, unsigned short len) { - buf[len] = '\0'; - Debug_printf("comlynx_send_buffer: %d %s\n", len, buf); - //Debug_printf("comlynx_send_buffer: len:%d %0X %0X %0X %0X %0X %0X\n", len, buf[0], buf[1], buf[2], buf[3], buf[4], buf[len-1]); + //buf[len] = '\0'; + //Debug_printf("comlynx_send_buffer: %d %s\n", len, buf); // causes out of bounds write in disk routines + + //Debug_printf("comlynx_send_buffer: len:%d %0X %0X %0X %0X %0X %0X\n", len, buf[0], buf[1], buf[2], buf[3], buf[4], buf[len-1]); + Debug_printf("comlynx_send_buffer: len:%d\n", len); + // Wait for idle only when in UDPStream mode if (SYSTEM_BUS._udpDev->udpstreamActive) SYSTEM_BUS.wait_for_idle(); diff --git a/lib/device/comlynx/disk.cpp b/lib/device/comlynx/disk.cpp index 6ff2e842f..e88a6e03e 100755 --- a/lib/device/comlynx/disk.cpp +++ b/lib/device/comlynx/disk.cpp @@ -211,9 +211,10 @@ void lynxDisk::comlynx_response_send() b[MEDIA_BLOCK_SIZE+3] = c; comlynx_send_buffer(b, sizeof(b)); - Debug_println("comlynx_send_buffer from disk block send done"); + Debug_println("comlynx_send_buffer - disk block sent to Lynx"); // get ACK or NACK from lynx (but don't care, they can request again if needed) + recvbuffer_len = 0; // reset recvbuffer to grab ACK/NACK from Lynx c = comlynx_recv(); Debug_printf("comlynx_disk_send NAK/ACK: %02X\n", c); } From e6f04713f40799ef05dbdcb845b46e1de499417b Mon Sep 17 00:00:00 2001 From: Shawn Date: Sun, 2 Nov 2025 09:37:49 -0800 Subject: [PATCH 11/12] debug output changes --- lib/device/comlynx/fuji.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/device/comlynx/fuji.cpp b/lib/device/comlynx/fuji.cpp index e90653052..2316c8f2a 100755 --- a/lib/device/comlynx/fuji.cpp +++ b/lib/device/comlynx/fuji.cpp @@ -143,7 +143,7 @@ void lynxFuji::comlynx_net_scan_result() if (n < _countScannedSSIDs) fnWiFi.get_scan_result(n, detail.ssid, &detail.rssi); - Debug_printf("SSID: %s - RSSI: %u\n", detail.ssid, detail.rssi); + Debug_printf("SSID: %s - RSSI: %d\n", detail.ssid, detail.rssi); memset(response, 0, sizeof(response)); memcpy(response, &detail, sizeof(detail)); From bea6937c8fd1223eebfeaf9009ba3db0925eceb2 Mon Sep 17 00:00:00 2001 From: Shawn Date: Thu, 13 Nov 2025 20:38:15 -0800 Subject: [PATCH 12/12] Getting lynx to compile again after refactoring work by Fozztexx --- lib/bus/comlynx/comlynx.cpp | 69 ++++++++++++++-------------- lib/bus/comlynx/comlynx.h | 15 ++++++- lib/device/comlynx/modem.cpp | 77 ++++++++++++++++---------------- lib/device/comlynx/network.cpp | 17 ++----- lib/device/comlynx/udpstream.cpp | 14 +++--- 5 files changed, 98 insertions(+), 94 deletions(-) diff --git a/lib/bus/comlynx/comlynx.cpp b/lib/bus/comlynx/comlynx.cpp index a97008996..7dc80a920 100755 --- a/lib/bus/comlynx/comlynx.cpp +++ b/lib/bus/comlynx/comlynx.cpp @@ -87,9 +87,9 @@ void virtualDevice::comlynx_send(uint8_t b) SYSTEM_BUS.wait_for_idle(); // Write the byte - fnUartBUS.write(b); - fnUartBUS.flush(); - fnUartBUS.read(); + SYSTEM_BUS.write(b); + SYSTEM_BUS.flush(); + SYSTEM_BUS.read(); } void virtualDevice::comlynx_send_buffer(uint8_t *buf, unsigned short len) @@ -105,8 +105,8 @@ void virtualDevice::comlynx_send_buffer(uint8_t *buf, unsigned short len) if (SYSTEM_BUS._udpDev->udpstreamActive) SYSTEM_BUS.wait_for_idle(); - fnUartBUS.write(buf, len); - fnUartBUS.readBytes(buf, len); + SYSTEM_BUS.write(buf, len); + SYSTEM_BUS.read(buf, len); } bool virtualDevice::comlynx_recv_ck() @@ -114,11 +114,11 @@ bool virtualDevice::comlynx_recv_ck() uint8_t recv_ck, ck; - while (fnUartBUS.available() <= 0) + while (SYSTEM_BUS.available() <= 0) fnSystem.yield(); // get checksum - recv_ck = fnUartBUS.read(); + recv_ck = SYSTEM_BUS.read(); ck = comlynx_checksum(recvbuffer, recvbuffer_len); @@ -138,10 +138,10 @@ uint8_t virtualDevice::comlynx_recv() { uint8_t b; - while (fnUartBUS.available() <= 0) + while (SYSTEM_BUS.available() <= 0) fnSystem.yield(); - b = fnUartBUS.read(); + b = SYSTEM_BUS.read(); // Add to receive buffer recvbuffer[recvbuffer_len] = b; @@ -159,7 +159,7 @@ bool virtualDevice::comlynx_recv_timeout(uint8_t *b, uint64_t dur) start = current = esp_timer_get_time(); elapsed = 0; - while (fnUartBUS.available() <= 0) + while (SYSTEM_BUS.available() <= 0) { current = esp_timer_get_time(); elapsed = current - start; @@ -167,9 +167,9 @@ bool virtualDevice::comlynx_recv_timeout(uint8_t *b, uint64_t dur) break; } - if (fnUartBUS.available() > 0) + if (SYSTEM_BUS.available() > 0) { - *b = (uint8_t)fnUartBUS.read(); + *b = (uint8_t)SYSTEM_BUS.read(); timeout = false; } // else // Debug_printf("duration: %llu\n", elapsed); @@ -202,7 +202,7 @@ unsigned short virtualDevice::comlynx_recv_buffer(uint8_t *buf, unsigned short l { unsigned short b; - b = fnUartBUS.readBytes(buf, len); + b = SYSTEM_BUS.read(buf, len); // Add to receive buffer memcpy(&recvbuffer[recvbuffer_len], buf, len); @@ -257,7 +257,7 @@ bool systemBus::wait_for_idle() dur = current - start; // Did we get any data in the FIFO while waiting? - if (fnUartBUS.available() > 0) + if (SYSTEM_BUS.available() > 0) return false; } while (dur < IDLE_TIME); @@ -270,7 +270,7 @@ bool systemBus::wait_for_idle() void virtualDevice::comlynx_process(uint8_t b) { - fnUartDebug.printf("comlynx_process() not implemented yet for this device. Cmd received: %02x\n", b); + fnDebugConsole.printf("comlynx_process() not implemented yet for this device. Cmd received: %02x\n", b); } void virtualDevice::comlynx_control_status() @@ -316,28 +316,27 @@ void virtualDevice::comlynx_idle() void systemBus::_comlynx_process_cmd() { - uint8_t b; - - b = fnUartBUS.read(); - //start_time = esp_timer_get_time(); + uint8_t d, b; - uint8_t d = b & 0x0F; - - #ifdef DEBUG - if ((b & 0xF0) == (MN_ACK<<4)) - Debug_println("Lynx sent ACK"); - else { - Debug_println("---"); - Debug_printf("comlynx_process_cmd: dev:%X cmd:%X\n", d, (b & 0xF0)>>4); - } - #endif + b = SYSTEM_BUS.read(); + d = b & 0x0F; + // Find device ID and pass control to it if (_daisyChain.count(d) < 1) { } else if (_daisyChain[d]->device_active == true) { + #ifdef DEBUG + if ((b & 0xF0) == (MN_ACK<<4)) + Debug_println("Lynx sent ACK"); + else { + Debug_println("---"); + Debug_printf("comlynx_process_cmd: dev:%X cmd:%X\n", d, (b & 0xF0)>>4); + } + #endif + // turn on Comlynx Indicator LED fnLedManager.set(eLed::LED_BUS, true); _daisyChain[d]->comlynx_process(b); @@ -345,8 +344,8 @@ void systemBus::_comlynx_process_cmd() fnLedManager.set(eLed::LED_BUS, false); } - //wait_for_idle(); // to avoid failing edge case where device is connected but disabled. - fnUartBUS.flush_input(); + //SYSTEM_BUS.flush_input(); + SYSTEM_BUS.flush(); } void systemBus::_comlynx_process_queue() @@ -359,7 +358,7 @@ void systemBus::service() if (_udpDev != nullptr && _udpDev->udpstreamActive) _udpDev->comlynx_handle_udpstream(); // Process anything waiting - else if (fnUartBUS.available() > 0) + else if (SYSTEM_BUS.available() > 0) _comlynx_process_cmd(); } @@ -380,7 +379,11 @@ void systemBus::setup() _udpDev = new lynxUDPStream(); // Set up UART - fnUartBUS.begin(COMLYNX_BAUDRATE); + _port.begin(ChannelConfig() + .deviceID(FN_UART_BUS) + .baud(COMLYNX_BAUDRATE) + .parity(UART_PARITY_ODD) + ); } void systemBus::shutdown() diff --git a/lib/bus/comlynx/comlynx.h b/lib/bus/comlynx/comlynx.h index 2b17155cb..4b47fba40 100755 --- a/lib/bus/comlynx/comlynx.h +++ b/lib/bus/comlynx/comlynx.h @@ -5,6 +5,7 @@ * Comlynx Routines */ +#include "UARTChannel.h" #include #include @@ -248,6 +249,7 @@ class systemBus lynxFuji *_fujiDev = nullptr; lynxPrinter *_printerDev = nullptr; + UARTChannel _port; void _comlynx_process_cmd(); void _comlynx_process_queue(); @@ -289,9 +291,20 @@ class systemBus bool shuttingDown = false; // TRUE if we are in shutdown process bool getShuttingDown() { return shuttingDown; }; + + // Everybody thinks "oh I know how a serial port works, I'll just + // access it directly and bypass the bus!" ಠ_ಠ + size_t read(void *buffer, size_t length) { return _port.read(buffer, length); } + size_t read() { return _port.read(); } + size_t write(const void *buffer, size_t length) { return _port.write(buffer, length); } + size_t write(int n) { return _port.write(n); } + size_t available() { return _port.available(); } + void flush() { _port.flushOutput(); } + size_t print(int n, int base = 10) { return _port.print(n, base); } + size_t print(const char *str) { return _port.print(str); } + size_t print(const std::string &str) { return _port.print(str); } }; extern systemBus SYSTEM_BUS; -//extern systemBus ComLynx; #endif /* COMLYNX_H */ diff --git a/lib/device/comlynx/modem.cpp b/lib/device/comlynx/modem.cpp index 49c74b973..4d8830114 100755 --- a/lib/device/comlynx/modem.cpp +++ b/lib/device/comlynx/modem.cpp @@ -7,7 +7,6 @@ #include "fnSystem.h" #include "fnConfig.h" -#include "fnUART.h" #include "fnWiFi.h" #include "utils.h" @@ -41,7 +40,7 @@ static void _telnet_event_handler(telnet_t *telnet, telnet_event_t *ev, void *us switch (ev->type) { case TELNET_EV_DATA: - if (ev->data.size && fnUartBUS.write((uint8_t *)ev->data.buffer, ev->data.size) != ev->data.size) + if (ev->data.size && SYSTEM_BUS.write((uint8_t *)ev->data.buffer, ev->data.size) != ev->data.size) Debug_printf("_telnet_event_handler(%d) - Could not write complete buffer to SIO.\n", ev->type); break; case TELNET_EV_SEND: @@ -128,8 +127,8 @@ void lynxModem::at_connect_resultCode(int modemBaud) resultCode = 1; break; } - fnUartBUS.print(resultCode); - fnUartBUS.write(ASCII_CR); + SYSTEM_BUS.print(resultCode); + SYSTEM_BUS.write(ASCII_CR); } /** @@ -138,9 +137,9 @@ void lynxModem::at_connect_resultCode(int modemBaud) */ void lynxModem::at_cmd_resultCode(int resultCode) { - fnUartBUS.print(resultCode); - fnUartBUS.write(ASCII_CR); - fnUartBUS.write(ASCII_LF); + SYSTEM_BUS.print(resultCode); + SYSTEM_BUS.write(ASCII_CR); + SYSTEM_BUS.write(ASCII_LF); } /** @@ -153,14 +152,14 @@ void lynxModem::at_cmd_println() if (cmdAtascii == true) { - fnUartBUS.write(ATASCII_EOL); + SYSTEM_BUS.write(ATASCII_EOL); } else { - fnUartBUS.write(ASCII_CR); - fnUartBUS.write(ASCII_LF); + SYSTEM_BUS.write(ASCII_CR); + SYSTEM_BUS.write(ASCII_LF); } - fnUartBUS.flush(); + SYSTEM_BUS.flush(); } void lynxModem::at_cmd_println(const char *s, bool addEol) @@ -168,20 +167,20 @@ void lynxModem::at_cmd_println(const char *s, bool addEol) if (cmdOutput == false) return; - fnUartBUS.print(s); + SYSTEM_BUS.print(s); if (addEol) { if (cmdAtascii == true) { - fnUartBUS.write(ATASCII_EOL); + SYSTEM_BUS.write(ATASCII_EOL); } else { - fnUartBUS.write(ASCII_CR); - fnUartBUS.write(ASCII_LF); + SYSTEM_BUS.write(ASCII_CR); + SYSTEM_BUS.write(ASCII_LF); } } - fnUartBUS.flush(); + SYSTEM_BUS.flush(); } void lynxModem::at_cmd_println(int i, bool addEol) @@ -189,20 +188,20 @@ void lynxModem::at_cmd_println(int i, bool addEol) if (cmdOutput == false) return; - fnUartBUS.print(i); + SYSTEM_BUS.print(i); if (addEol) { if (cmdAtascii == true) { - fnUartBUS.write(ATASCII_EOL); + SYSTEM_BUS.write(ATASCII_EOL); } else { - fnUartBUS.write(ASCII_CR); - fnUartBUS.write(ASCII_LF); + SYSTEM_BUS.write(ASCII_CR); + SYSTEM_BUS.write(ASCII_LF); } } - fnUartBUS.flush(); + SYSTEM_BUS.flush(); } void lynxModem::at_cmd_println(std::string s, bool addEol) @@ -210,20 +209,20 @@ void lynxModem::at_cmd_println(std::string s, bool addEol) if (cmdOutput == false) return; - fnUartBUS.print(s); + SYSTEM_BUS.print(s); if (addEol) { if (cmdAtascii == true) { - fnUartBUS.write(ATASCII_EOL); + SYSTEM_BUS.write(ATASCII_EOL); } else { - fnUartBUS.write(ASCII_CR); - fnUartBUS.write(ASCII_LF); + SYSTEM_BUS.write(ASCII_CR); + SYSTEM_BUS.write(ASCII_LF); } } - fnUartBUS.flush(); + SYSTEM_BUS.flush(); } void lynxModem::at_handle_wificonnect() @@ -489,7 +488,7 @@ void lynxModem::at_handle_answer() CRX = true; cmdMode = false; - fnUartBUS.flush(); + SYSTEM_BUS.flush(); answerHack = false; } } @@ -1030,11 +1029,11 @@ void lynxModem::sio_handle_modem() // In command mode - don't exchange with TCP but gather characters to a string //if (SIO_UART.available() /*|| blockWritePending == true */ ) - if (fnUartBUS.available() > 0) + if (SYSTEM_BUS.available() > 0) { // get char from Atari SIO //char chr = SIO_UART.read(); - char chr = fnUartBUS.read(); + char chr = SYSTEM_BUS.read(); // Return, enter, new line, carriage return.. anything goes to end the command if ((chr == ASCII_LF) || (chr == ASCII_CR) || (chr == ATASCII_EOL)) @@ -1059,9 +1058,9 @@ void lynxModem::sio_handle_modem() // Clear with a space if (commandEcho == true) { - fnUartBUS.write(ASCII_BACKSPACE); - fnUartBUS.write(' '); - fnUartBUS.write(ASCII_BACKSPACE); + SYSTEM_BUS.write(ASCII_BACKSPACE); + SYSTEM_BUS.write(' '); + SYSTEM_BUS.write(ASCII_BACKSPACE); } } } @@ -1074,7 +1073,7 @@ void lynxModem::sio_handle_modem() { cmd.erase(len - 1); if (commandEcho == true) - fnUartBUS.write(ATASCII_BACKSPACE); + SYSTEM_BUS.write(ATASCII_BACKSPACE); } } // Take into account arrow key movement and clear screen @@ -1082,7 +1081,7 @@ void lynxModem::sio_handle_modem() ((chr >= ATASCII_CURSOR_UP) && (chr <= ATASCII_CURSOR_RIGHT))) { if (commandEcho == true) - fnUartBUS.write(chr); + SYSTEM_BUS.write(chr); } else { @@ -1092,7 +1091,7 @@ void lynxModem::sio_handle_modem() cmd += chr; } if (commandEcho == true) - fnUartBUS.write(chr); + SYSTEM_BUS.write(chr); } } } @@ -1124,7 +1123,7 @@ void lynxModem::sio_handle_modem() } //int sioBytesAvail = SIO_UART.available(); - int sioBytesAvail = std::min(0, fnUartBUS.available()); + int sioBytesAvail = std::min((size_t) 0, SYSTEM_BUS.available()); // send from Atari to Fujinet if (sioBytesAvail && tcpClient.connected()) @@ -1139,7 +1138,7 @@ void lynxModem::sio_handle_modem() // Read from serial, the amount available up to // maximum size of the buffer - int sioBytesRead = fnUartBUS.readBytes(&txBuf[0], //SIO_UART.readBytes(&txBuf[0], + int sioBytesRead = SYSTEM_BUS.read(&txBuf[0], //SIO_UART.read(&txBuf[0], (sioBytesAvail > TX_BUF_SIZE) ? TX_BUF_SIZE : sioBytesAvail); // Disconnect if going to AT mode with "+++" sequence @@ -1189,8 +1188,8 @@ void lynxModem::sio_handle_modem() } else { - fnUartBUS.write(buf, bytesRead); - fnUartBUS.flush(); + SYSTEM_BUS.write(buf, bytesRead); + SYSTEM_BUS.flush(); } // And dump to sniffer, if enabled. diff --git a/lib/device/comlynx/network.cpp b/lib/device/comlynx/network.cpp index e11fe49b5..c51a271c1 100755 --- a/lib/device/comlynx/network.cpp +++ b/lib/device/comlynx/network.cpp @@ -616,18 +616,8 @@ void lynxNetwork::json_query(unsigned short s) } json.setReadQuery(inp_string, cmdFrame.aux2); - - /*json_bytes_remaining = json->json_bytes_remaining; - - std::vector tmp(json_bytes_remaining); - json->readValue(tmp.data(), json_bytes_remaining); - - // don't copy past first nul char in tmp - auto null_pos = std::find(tmp.begin(), tmp.end(), 0); - *receiveBuffer += std::string(tmp.begin(), null_pos);*/ - + Debug_printf("lynxNetwork::json_query(%s)\n", inp_string.c_str()); - } void lynxNetwork::json_parse() @@ -897,13 +887,12 @@ void lynxNetwork::comlynx_control_receive_channel_json() if (jsonRecvd == false) { response_len = json.readValueLen(); - json.readValue(response,response_len); - jsonRecvd=true; + json.readValue(response, response_len); + jsonRecvd = true; comlynx_response_ack(); } else { - //ComLynx.start_time = esp_timer_get_time(); if (response_len > 0) comlynx_response_ack(); else diff --git a/lib/device/comlynx/udpstream.cpp b/lib/device/comlynx/udpstream.cpp index f9b85777c..a5a213009 100755 --- a/lib/device/comlynx/udpstream.cpp +++ b/lib/device/comlynx/udpstream.cpp @@ -87,28 +87,28 @@ void lynxUDPStream::comlynx_handle_udpstream() if (good_packet) { // Send to Lynx UART _comlynx_bus->wait_for_idle(); - fnUartBUS.write(buf_net, packetSize); + SYSTEM_BUS.write(buf_net, packetSize); #ifdef DEBUG_UDPSTREAM Debug_print("UDP-IN: "); util_dump_bytes(buf_net, packetSize); #endif - fnUartBUS.readBytes(buf_net, packetSize); // Trash what we just sent over serial + SYSTEM_BUS.read(buf_net, packetSize); // Trash what we just sent over serial } } // Read the data until there's a pause in the incoming stream - fnUartBUS.flush(); + SYSTEM_BUS.flush(); buf_stream_index = 0; - if (fnUartBUS.available() > 0) + if (SYSTEM_BUS.available() > 0) { while (true) { - if (fnUartBUS.available() > 0) + if (SYSTEM_BUS.available() > 0) { // Collect bytes read in our buffer - buf_stream[buf_stream_index] = (char)fnUartBUS.read(); + buf_stream[buf_stream_index] = (char)SYSTEM_BUS.read(); if (redeye_mode && (buf_stream_index == 0)) { // Check first byte if ((buf_stream[0] < 1) || (buf_stream[0] > 6)) // discard bad size byte (must be between 1 and 6) continue; @@ -120,7 +120,7 @@ void lynxUDPStream::comlynx_handle_udpstream() else { fnSystem.delay_microseconds(UDPSTREAM_PACKET_TIMEOUT); - if (fnUartBUS.available() <= 0) + if (SYSTEM_BUS.available() <= 0) break; } }