Skip to content

Commit

Permalink
[rs232] use fnio to access disk images (instead of stdio)
Browse files Browse the repository at this point in the history
  • Loading branch information
a8jan authored and tschak909 committed Mar 4, 2025
1 parent 4d01eb1 commit ddba4ca
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 36 deletions.
2 changes: 1 addition & 1 deletion lib/FileSystem/fnio.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <cstddef>

#if defined(BUILD_ATARI) || defined(BUILD_APPLE) || defined(BUILD_COCO)
#if defined(BUILD_ATARI) || defined(BUILD_APPLE) || defined(BUILD_COCO) || defined(BUILD_RS232)
// ATARI and APPLE was already ported to use fnio
// set FNIO_IS_STDIO to force stdio

Expand Down
4 changes: 2 additions & 2 deletions lib/device/rs232/disk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ void rs232Disk::rs232_write_percom_block()
then we assume it's MEDIATYPE_ATR.
Return value is MEDIATYPE_UNKNOWN in case of failure.
*/
mediatype_t rs232Disk::mount(FILE *f, const char *filename, uint32_t disksize, mediatype_t disk_type)
mediatype_t rs232Disk::mount(fnFile *f, const char *filename, uint32_t disksize, mediatype_t disk_type)
{
// TAPE or CASSETTE: use this function to send file info to cassette device
// MediaType::discover_disktype(filename) can detect CAS and WAV files
Expand Down Expand Up @@ -249,7 +249,7 @@ void rs232Disk::unmount()
}

// Create blank disk
bool rs232Disk::write_blank(FILE *f, uint16_t sectorSize, uint16_t numSectors)
bool rs232Disk::write_blank(fnFile *f, uint16_t sectorSize, uint16_t numSectors)
{
Debug_print("disk CREATE NEW IMAGE\n");

Expand Down
4 changes: 2 additions & 2 deletions lib/device/rs232/disk.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class rs232Disk : public virtualDevice
time_t mount_time = 0;

rs232Disk();
mediatype_t mount(FILE *f, const char *filename, uint32_t disksize, mediatype_t disk_type = MEDIATYPE_UNKNOWN);
mediatype_t mount(fnFile *f, const char *filename, uint32_t disksize, mediatype_t disk_type = MEDIATYPE_UNKNOWN);
void unmount();
bool write_blank(FILE *f, uint16_t sectorSize, uint16_t numSectors);
bool write_blank(fnFile *f, uint16_t sectorSize, uint16_t numSectors);

mediatype_t disktype() { return _disk == nullptr ? MEDIATYPE_UNKNOWN : _disk->_disktype; };

Expand Down
32 changes: 16 additions & 16 deletions lib/device/rs232/fuji.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ void rs232Fuji::rs232_disk_image_mount()
Debug_printf("Selecting '%s' from host #%u as %s on D%u:\n",
disk.filename, disk.host_slot, flag, deviceSlot + 1);

disk.fileh = host.file_open(disk.filename, disk.filename, sizeof(disk.filename), flag);
disk.fileh = host.fnfile_open(disk.filename, disk.filename, sizeof(disk.filename), flag);

if (disk.fileh == nullptr)
{
Expand Down Expand Up @@ -338,8 +338,8 @@ void rs232Fuji::rs232_copy_file()
std::string sourcePath;
std::string destPath;
uint8_t ck;
FILE *sourceFile;
FILE *destFile;
fnFile *sourceFile;
fnFile *destFile;
char *dataBuf;
unsigned char sourceSlot;
unsigned char destSlot;
Expand Down Expand Up @@ -411,7 +411,7 @@ void rs232Fuji::rs232_copy_file()
_fnHosts[destSlot].mount();

// Open files...
sourceFile = _fnHosts[sourceSlot].file_open(sourcePath.c_str(), (char *)sourcePath.c_str(), sourcePath.size() + 1, "r");
sourceFile = _fnHosts[sourceSlot].fnfile_open(sourcePath.c_str(), (char *)sourcePath.c_str(), sourcePath.size() + 1, "r");

if (sourceFile == nullptr)
{
Expand All @@ -420,28 +420,28 @@ void rs232Fuji::rs232_copy_file()
return;
}

destFile = _fnHosts[destSlot].file_open(destPath.c_str(), (char *)destPath.c_str(), destPath.size() + 1, "w");
destFile = _fnHosts[destSlot].fnfile_open(destPath.c_str(), (char *)destPath.c_str(), destPath.size() + 1, "w");

if (destFile == nullptr)
{
rs232_error();
fclose(sourceFile);
fnio::fclose(sourceFile);
free(dataBuf);
return;
}

size_t count = 0;
do
{
count = fread(dataBuf, 1, 532, sourceFile);
fwrite(dataBuf, 1, count, destFile);
count = fnio::fread(dataBuf, 1, 532, sourceFile);
fnio::fwrite(dataBuf, 1, count, destFile);
} while (count > 0);

rs232_complete();

// copyEnd:
fclose(sourceFile);
fclose(destFile);
fnio::fclose(sourceFile);
fnio::fclose(destFile);
free(dataBuf);
}

Expand Down Expand Up @@ -472,7 +472,7 @@ void rs232Fuji::mount_all()
Debug_printf("Selecting '%s' from host #%u as %s on D%u:\n",
disk.filename, disk.host_slot, flag, i + 1);

disk.fileh = host.file_open(disk.filename, disk.filename, sizeof(disk.filename), flag);
disk.fileh = host.fnfile_open(disk.filename, disk.filename, sizeof(disk.filename), flag);

if (disk.fileh == nullptr)
{
Expand Down Expand Up @@ -1054,7 +1054,7 @@ void rs232Fuji::rs232_new_disk()
return;
}

disk.fileh = host.file_open(disk.filename, disk.filename, sizeof(disk.filename), "w");
disk.fileh = host.fnfile_open(disk.filename, disk.filename, sizeof(disk.filename), "w");
if (disk.fileh == nullptr)
{
Debug_printf("rs232_new_disk Couldn't open file for writing: \"%s\"\n", disk.filename);
Expand All @@ -1063,7 +1063,7 @@ void rs232Fuji::rs232_new_disk()
}

bool ok = disk.disk_dev.write_blank(disk.fileh, newDisk.sectorSize, newDisk.numSectors);
fclose(disk.fileh);
fnio::fclose(disk.fileh);

if (ok == false)
{
Expand Down Expand Up @@ -1407,7 +1407,7 @@ void rs232Fuji::insert_boot_device(uint8_t d)
{
const char *config_atr = "/autorun.img";
const char *mount_all_atr = "/mount-and-boot.img";
FILE *fBoot;
fnFile *fBoot = nullptr;

Debug_printf("rs232Fuji::insert_boot_device(%u)\n",d);

Expand All @@ -1416,11 +1416,11 @@ void rs232Fuji::insert_boot_device(uint8_t d)
switch (d)
{
case 0:
fBoot = fsFlash.file_open(config_atr);
fBoot = fsFlash.fnfile_open(config_atr);
_bootDisk.mount(fBoot, config_atr, 368640);
break;
case 1:
fBoot = fsFlash.file_open(mount_all_atr);
fBoot = fsFlash.fnfile_open(mount_all_atr);
_bootDisk.mount(fBoot, mount_all_atr, 368640);
break;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/media/rs232/diskType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void MediaType::unmount()
{
if (_disk_fileh != nullptr)
{
fclose(_disk_fileh);
fnio::fclose(_disk_fileh);
_disk_fileh = nullptr;
}
}
Expand Down
7 changes: 4 additions & 3 deletions lib/media/rs232/diskType.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#ifndef _MEDIATYPE_RS232
#define _MEDIATYPE_RS232

#include <stdio.h>
#include <stdint.h>
#include "fnio.h"

#define INVALID_SECTOR_VALUE 65536

Expand Down Expand Up @@ -40,7 +41,7 @@ enum mediatype_t
class MediaType
{
protected:
FILE *_disk_fileh = nullptr;
fnFile *_disk_fileh = nullptr;
uint32_t _disk_image_size = 0;
uint32_t _disk_num_sectors = 0;
uint32_t _disk_sector_size = DISK_BYTES_PER_SECTOR_SINGLE;
Expand Down Expand Up @@ -68,7 +69,7 @@ class MediaType

mediatype_t _disktype = MEDIATYPE_UNKNOWN;

virtual mediatype_t mount(FILE *f, uint32_t disksize) = 0;
virtual mediatype_t mount(fnFile *f, uint32_t disksize) = 0;
virtual void unmount();

// Returns TRUE if an error condition occurred
Expand Down
17 changes: 8 additions & 9 deletions lib/media/rs232/diskTypeImg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ bool MediaTypeImg::read(uint32_t sectornum, uint32_t *readcount)
if (sectornum != _disk_last_sector + 1)
{
uint32_t offset = _sector_to_offset(sectornum);
err = fseek(_disk_fileh, offset, SEEK_SET) != 0;
err = fnio::fseek(_disk_fileh, offset, SEEK_SET) != 0;
}

if (err == false)
err = fread(_disk_sectorbuff, 1, sectorSize, _disk_fileh) != sectorSize;
err = fnio::fread(_disk_sectorbuff, 1, sectorSize, _disk_fileh) != sectorSize;

if (err == false)
_disk_last_sector = sectornum;
Expand Down Expand Up @@ -79,24 +79,23 @@ bool MediaTypeImg::write(uint32_t sectornum, bool verify)
int e;
if (sectornum != _disk_last_sector + 1)
{
e = fseek(_disk_fileh, offset, SEEK_SET);
e = fnio::fseek(_disk_fileh, offset, SEEK_SET);
if (e != 0)
{
Debug_printf("::write seek error %d\r\n", e);
return true;
}
}
// Write the data
e = fwrite(_disk_sectorbuff, 1, sectorSize, _disk_fileh);
e = fnio::fwrite(_disk_sectorbuff, 1, sectorSize, _disk_fileh);
if (e != sectorSize)
{
Debug_printf("::write error %d, %d\r\n", e, errno);
return true;
}

int ret = fflush(_disk_fileh); // This doesn't seem to be connected to anything in ESP-IDF VF, so it may not do anything
ret = fsync(fileno(_disk_fileh)); // Since we might get reset at any moment, go ahead and sync the file (not clear if fflush does this)
Debug_printf("IMG::write fsync:%d\r\n", ret);
int ret = fnio::fflush(_disk_fileh); // Since we might get reset at any moment, go ahead and sync the file
Debug_printf("IMG::write fflush:%d\r\n", ret);

_disk_last_sector = sectornum;

Expand Down Expand Up @@ -153,7 +152,7 @@ bool MediaTypeImg::format(uint32_t *responsesize)
07-0F have two possible interpretations but are no critical for our use
*/
mediatype_t MediaTypeImg::mount(FILE *f, uint32_t disksize)
mediatype_t MediaTypeImg::mount(fnFile *f, uint32_t disksize)
{
Debug_print("IMG MOUNT\r\n");

Expand All @@ -165,7 +164,7 @@ mediatype_t MediaTypeImg::mount(FILE *f, uint32_t disksize)
}

// Returns FALSE on error
bool MediaTypeImg::create(FILE *f, uint16_t sectorSize, uint32_t numSectors)
bool MediaTypeImg::create(fnFile *f, uint16_t sectorSize, uint32_t numSectors)
{
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/media/rs232/diskTypeImg.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ class MediaTypeImg : public MediaType

virtual bool format(uint32_t *responsesize) override;

virtual mediatype_t mount(FILE *f, uint32_t disksize) override;
virtual mediatype_t mount(fnFile *f, uint32_t disksize) override;

virtual void status(uint8_t statusbuff[4]) override;

static bool create(FILE *f, uint16_t sectorSize, uint32_t numSectors);
static bool create(fnFile *f, uint16_t sectorSize, uint32_t numSectors);
};


Expand Down

0 comments on commit ddba4ca

Please sign in to comment.