Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mount and ESP32 System.IO.Filesystem support #2096

Merged
merged 17 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMake/Modules/FindSystem.IO.FileSystem.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ set(System.IO.FileSystem_SRCS

nf_sys_io_filesystem.cpp


nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard.cpp
nf_sys_io_filesystem_System_IO_Directory.cpp
nf_sys_io_filesystem_System_IO_File.cpp
nf_sys_io_filesystem_System_IO_FileStream.cpp
Expand Down
42 changes: 41 additions & 1 deletion src/System.IO.FileSystem/nf_sys_io_filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,46 @@

static const CLR_RT_MethodHandler method_lookup[] =
{
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard::InitNative___VOID,
Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard::NativeDispose___VOID,
Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard::MountNative___VOID,
Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard::UnmountNative___VOID,
Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard::PollCardDetectNative___BOOLEAN,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
Expand Down Expand Up @@ -107,7 +147,7 @@ static const CLR_RT_MethodHandler method_lookup[] =
const CLR_RT_NativeAssemblyData g_CLR_AssemblyNative_System_IO_FileSystem =
{
"System.IO.FileSystem",
0x3112D24C,
0x210D05B1,
method_lookup,
{ 1, 0, 0, 0 }
};
Expand Down
99 changes: 95 additions & 4 deletions src/System.IO.FileSystem/nf_sys_io_filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,33 @@
#include <nanoCLR_Runtime.h>
#include <nanoPackStruct.h>
#include <corlib_native.h>
#include "target_platform.h"

typedef enum __nfpack RemovableDeviceEventArgs_RemovableDeviceEvent
{
RemovableDeviceEventArgs_RemovableDeviceEvent_Inserted = 0,
RemovableDeviceEventArgs_RemovableDeviceEvent_Removed = 1,
} RemovableDeviceEventArgs_RemovableDeviceEvent;

typedef enum __nfpack SDCard_SDDataWidth
{
SDCard_SDDataWidth__1_bit = 1,
SDCard_SDDataWidth__4_bit = 2,
} SDCard_SDDataWidth;

typedef enum __nfpack SDCard_SDInterfaceType
{
SDCard_SDInterfaceType_System = 0,
SDCard_SDInterfaceType_Mmc = 1,
SDCard_SDInterfaceType_Spi = 2,
} SDCard_SDInterfaceType;

typedef enum __nfpack StorageEventManager_StorageEventType
{
StorageEventManager_StorageEventType_Invalid = 0,
StorageEventManager_StorageEventType_RemovableDeviceInsertion = 1,
StorageEventManager_StorageEventType_RemovableDeviceRemoval = 2,
} StorageEventManager_StorageEventType;

typedef enum __nfpack FileAccess
{
Expand Down Expand Up @@ -45,6 +72,70 @@ typedef enum __nfpack FileShare
FileShare_ReadWrite = 3,
} FileShare;

struct Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_RemovableDeviceEventArgs
{
static const int FIELD___path = 1;
static const int FIELD___event = 2;

//--//
};

struct Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard
{
static const int FIELD___mounted = 1;
static const int FIELD___disposed = 2;
static const int FIELD___sdCardType = 3;
static const int FIELD___enableCardDetectPin = 4;
static const int FIELD___cardDetectPin = 5;
static const int FIELD___dataWidth = 6;
static const int FIELD___spiBus = 7;
static const int FIELD___chipSelectPin = 8;

NANOCLR_NATIVE_DECLARE(InitNative___VOID);
NANOCLR_NATIVE_DECLARE(NativeDispose___VOID);
NANOCLR_NATIVE_DECLARE(MountNative___VOID);
NANOCLR_NATIVE_DECLARE(UnmountNative___VOID);
NANOCLR_NATIVE_DECLARE(PollCardDetectNative___BOOLEAN);

//--//
};

struct Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard__SDCardMmcParameters
{
static const int FIELD__dataWidth = 1;
static const int FIELD__enableCardDetectPin = 2;
static const int FIELD__cardDetectPin = 3;

//--//
};

struct Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard__SDCardSpiParameters
{
static const int FIELD__spiBus = 1;
static const int FIELD__chipSelectPin = 2;
static const int FIELD__enableCardDetectPin = 3;
static const int FIELD__cardDetectPin = 4;

//--//
};

struct Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_StorageEventManager
{
static const int FIELD_STATIC__RemovableDeviceInserted = 0;
static const int FIELD_STATIC__RemovableDeviceRemoved = 1;

//--//
};

struct Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_StorageEventManager__StorageEvent
{
static const int FIELD__EventType = 3;
static const int FIELD__DriveIndex = 4;
static const int FIELD__Time = 5;

//--//
};

struct Library_nf_sys_io_filesystem_System_IO_Directory
{
NANOCLR_NATIVE_DECLARE(ExistsNative___STATIC__BOOLEAN__STRING);
Expand Down Expand Up @@ -104,13 +195,13 @@ struct Library_nf_sys_io_filesystem_System_IO_MemoryStream

struct Library_nf_sys_io_filesystem_System_IO_Path
{
static const int FIELD_STATIC__DirectorySeparatorChar = 0;
static const int FIELD_STATIC__InvalidPathChars = 1;
static const int FIELD_STATIC__m_illegalCharacters = 2;
static const int FIELD_STATIC__DirectorySeparatorChar = 2;
static const int FIELD_STATIC__InvalidPathChars = 3;
static const int FIELD_STATIC__m_illegalCharacters = 4;

//--//
};

extern const CLR_RT_NativeAssemblyData g_CLR_AssemblyNative_System_IO_FileSystem;

#endif //NF_SYS_IO_FILESYSTEM_H
#endif // NF_SYS_IO_FILESYSTEM_H
Original file line number Diff line number Diff line change
Expand Up @@ -377,32 +377,67 @@ HRESULT Library_nf_sys_io_filesystem_System_IO_Directory::GetDirectoriesNative__
NANOCLR_CLEANUP_END();
}

// Enumerate drives in system
// if array == null then only count drives
// Return number of drives
HRESULT EnumerateDrives(CLR_RT_HeapBlock *array, int &count)
{
NANOCLR_HEADER();
{
CLR_RT_HeapBlock *storageFolder = NULL;
FRESULT operationResult;
DIR currentDirectory;
char workingDrive[] = INDEX0_DRIVE_PATH;

if (array)
{
// get a pointer to the first object in the array (which is of type <String>)
storageFolder = (CLR_RT_HeapBlock *)array->DereferenceArray()->GetFirstElement();
}

count = 0;
for (char drive = INDEX0_DRIVE_LETTER[0]; drive <= INTERNAL_DRIVE0_LETTER[0]; drive++)
{
workingDrive[0] = drive;

operationResult = f_opendir(&currentDirectory, workingDrive);
if (operationResult == FR_OK)
{
count++;
f_closedir(&currentDirectory);

if (array)
{
// Add entry to array
// set the drive letter in string array
NANOCLR_CHECK_HRESULT(CLR_RT_HeapBlock_String::CreateInstance(*storageFolder, workingDrive));

// Next element in array
storageFolder++;
}
}
}
}
NANOCLR_NOCLEANUP();
}

HRESULT Library_nf_sys_io_filesystem_System_IO_Directory::GetLogicalDrivesNative___STATIC__SZARRAY_STRING(
CLR_RT_StackFrame &stack)
{
NANOCLR_HEADER();
{
// TODO: implement retrieving more than one hardcoded drive
// multiple different boards can have different implementations
// for now just return "D:\\" as an only available drive
CLR_RT_HeapBlock *storageFolder;
CLR_RT_HeapBlock &top = stack.PushValueAndClear();
char workingDrive[sizeof(DRIVE_PATH_LENGTH)];

// create an array of files paths <String>
NANOCLR_CHECK_HRESULT(CLR_RT_HeapBlock_Array::CreateInstance(top, 1, g_CLR_RT_WellKnownTypes.m_String));
int count = 0;

// get a pointer to the first object in the array (which is of type <String>)
storageFolder = (CLR_RT_HeapBlock *)top.DereferenceArray()->GetFirstElement();
// 1st pass find number of drives
NANOCLR_CHECK_HRESULT(EnumerateDrives(NULL, count));

// as of now we only have one drive,
// in the future if more comes, we might want to iterate over all drives
memcpy(workingDrive, INDEX0_DRIVE_PATH, DRIVE_PATH_LENGTH);
// create an array of files paths <String> for count drives
NANOCLR_CHECK_HRESULT(CLR_RT_HeapBlock_Array::CreateInstance(top, count, g_CLR_RT_WellKnownTypes.m_String));

// set the drive letter in string array
NANOCLR_CHECK_HRESULT(CLR_RT_HeapBlock_String::CreateInstance(*storageFolder, workingDrive));
// 2nd pass fill array with drives
NANOCLR_CHECK_HRESULT(EnumerateDrives(&top, count));
}

NANOCLR_NOCLEANUP();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ HRESULT Library_nf_sys_io_filesystem_System_IO_File::GetAttributesNative___STATI
else if (operationResult == FR_NO_FILE)
{
// File/Directory not found
NANOCLR_SET_AND_LEAVE(CLR_E_FILE_NOT_FOUND);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//

#include "nf_sys_io_filesystem.h"

__nfweak HRESULT
Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard::InitNative___VOID(CLR_RT_StackFrame &stack)
{
NANOCLR_HEADER();

NANOCLR_SET_AND_LEAVE(stack.NotImplementedStub());

NANOCLR_NOCLEANUP();
}

__nfweak HRESULT
Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard::MountNative___VOID(CLR_RT_StackFrame &stack)
{
NANOCLR_HEADER();

NANOCLR_SET_AND_LEAVE(stack.NotImplementedStub());

NANOCLR_NOCLEANUP();
}

__nfweak HRESULT Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard::UnmountNative___VOID(
CLR_RT_StackFrame &stack)
{
NANOCLR_HEADER();

NANOCLR_SET_AND_LEAVE(stack.NotImplementedStub());

NANOCLR_NOCLEANUP();
}

__nfweak HRESULT Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard::PollCardDetectNative___BOOLEAN(
CLR_RT_StackFrame &stack)
{
NANOCLR_HEADER();

NANOCLR_SET_AND_LEAVE(stack.NotImplementedStub());

NANOCLR_NOCLEANUP();
}

__nfweak HRESULT Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard::NativeDispose___VOID(
CLR_RT_StackFrame &stack)
{
NANOCLR_HEADER();

NANOCLR_SET_AND_LEAVE(stack.NotImplementedStub());

NANOCLR_NOCLEANUP();
}
Loading