-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdmdevfs.c
More file actions
1273 lines (1108 loc) · 36.2 KB
/
dmdevfs.c
File metadata and controls
1273 lines (1108 loc) · 36.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file dmdevfs.c
* @brief DMOD Driver File System - Implementation
* @author Patryk Kubiak
*
* This is a driver-based file system that provides an interface to access
* files through hardware drivers or external storage.
*/
#define DMOD_ENABLE_REGISTRATION ON
#define ENABLE_DIF_REGISTRATIONS ON
#include "dmod.h"
#include "dmdevfs.h"
#include "dmfsi.h"
#include "dmlist.h"
#include "dmini.h"
#include "dmdrvi.h"
#include <string.h>
/**
* @brief Magic number for DMDEVFS context validation
*/
#define DMDEVFS_CONTEXT_MAGIC 0x444D4456 // 'DMDV'
#define ROOT_DIRECTORY_NAME "/"
#define MAX_PATH_LENGTH (DMOD_MAX_MODULE_NAME_LENGTH + 20)
/**
* @brief Type definition for path strings
*/
typedef char path_t[MAX_PATH_LENGTH];
typedef struct
{
dmdrvi_context_t driver_context; // Driver-specific context
Dmod_Context_t* driver; // Driver module context
dmdrvi_dev_num_t dev_num; // Device number assigned to the driver
bool was_loaded; // Indicates if the driver was loaded by dmdevfs
bool was_enabled; // Indicates if the driver was enabled by dmdevfs
path_t path; // Path associated with the driver
} driver_node_t;
typedef struct
{
driver_node_t* driver; // Last driver
char* directory_path; // Directory path
} directory_node_t;
/**
* @brief File handle structure for file operations
*/
typedef struct
{
driver_node_t* driver; // Driver associated with this file
void* driver_handle; // Driver device handle
const char* path; // File path
int mode; // File open mode
int attr; // File attributes
} file_handle_t;
/**
* @brief File system context structure
*/
struct dmfsi_context
{
uint32_t magic;
char* config_path; // Path with the configuration files
dmlist_context_t* drivers; // List of loaded drivers
};
// ============================================================================
// Local prototypes
// ============================================================================
static int configure_drivers(dmfsi_context_t ctx, const char* driver_name, const char* config_path);
static driver_node_t* configure_driver(const char* driver_name, dmini_context_t config_ctx);
static int unconfigure_drivers(dmfsi_context_t ctx);
static bool is_file(const char* path);
static void read_base_name(const char* path, char* base_name, size_t name_size);
static dmini_context_t read_driver_for_config(const char* config_path, char* driver_name, size_t name_size, const char* default_driver);
static Dmod_Context_t* prepare_driver_module(const char* driver_name, bool* was_loaded, bool* was_enabled);
static void cleanup_driver_module(const char* driver_name, bool was_loaded, bool was_enabled);
static int read_driver_parent_directory( const driver_node_t* node, char* path_buffer, size_t buffer_size );
static int read_driver_node_path( const driver_node_t* node, char* path_buffer, size_t buffer_size );
static int compare_driver_directory( const void* data, const void* user_data );
static int compare_driver_node_path( const void* data, const void* user_data );
static int compare_driver(const void* data, const void* user_data );
static bool is_directory( dmfsi_context_t ctx, const char* path );
static driver_node_t* get_next_driver_node( dmfsi_context_t ctx, driver_node_t* current, const char* dir_path );
static driver_node_t* find_driver_node( dmfsi_context_t ctx, const char* path );
static int driver_stat( driver_node_t* context, const char* path, dmdrvi_stat_t* stat );
// ============================================================================
// Module Interface Implementation
// ============================================================================
/**
* @brief Module pre-initialization (optional)
*/
void dmod_preinit(void)
{
// Nothing to do
}
/**
* @brief Module initialization
*/
int dmod_init(const Dmod_Config_t *Config)
{
// Nothing to do
return 0;
}
/**
* @brief Module deinitialization
*/
int dmod_deinit(void)
{
// Nothing to do
return 0;
}
// ============================================================================
// DMFSI Interface Implementation
// ============================================================================
/**
* @brief Initialize the file system
*/
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, dmfsi_context_t, _init, (const char* config) )
{
if(config == NULL)
{
DMOD_LOG_ERROR("Config path is NULL\n");
return NULL;
}
if(strlen(config) == 0)
{
DMOD_LOG_ERROR("Config path is empty\n");
return NULL;
}
dmfsi_context_t ctx = Dmod_Malloc(sizeof(struct dmfsi_context));
if (ctx == NULL)
{
DMOD_LOG_ERROR("Failed to allocate memory for context\n");
return NULL;
}
ctx->magic = DMDEVFS_CONTEXT_MAGIC;
ctx->config_path = Dmod_StrDup(config);
ctx->drivers = dmlist_create(DMOD_MODULE_NAME);
int res = configure_drivers(ctx, NULL, ctx->config_path);
if (res != DMFSI_OK)
{
DMOD_LOG_ERROR("Failed to configure drivers\n");
unconfigure_drivers(ctx);
dmlist_destroy(ctx->drivers);
Dmod_Free(ctx->config_path);
Dmod_Free(ctx);
return NULL;
}
return ctx;
}
/**
* @brief Validate the file system context
*/
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _context_is_valid, (dmfsi_context_t ctx) )
{
return (ctx && ctx->magic == DMDEVFS_CONTEXT_MAGIC) ? 1 : 0;
}
/**
* @brief Deinitialize the file system
*/
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _deinit, (dmfsi_context_t ctx) )
{
if (!dmfsi_dmdevfs_context_is_valid(ctx))
{
DMOD_LOG_ERROR("Invalid context in deinit\n");
return DMFSI_ERR_INVALID;
}
unconfigure_drivers(ctx);
dmlist_destroy(ctx->drivers);
Dmod_Free(ctx->config_path);
Dmod_Free(ctx);
return DMFSI_OK;
}
/**
* @brief Open a file
*/
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _fopen, (dmfsi_context_t ctx, void** fp, const char* path, int mode, int attr) )
{
if(dmfsi_dmdevfs_context_is_valid(ctx) == 0)
{
DMOD_LOG_ERROR("Invalid context in fopen\n");
return DMFSI_ERR_INVALID;
}
if(fp == NULL || path == NULL)
{
DMOD_LOG_ERROR("NULL pointer in fopen\n");
return DMFSI_ERR_INVALID;
}
// Find the driver node for this file
driver_node_t* driver_node = find_driver_node(ctx, path);
if(driver_node == NULL)
{
DMOD_LOG_ERROR("File not found: %s\n", path);
return DMFSI_ERR_NOT_FOUND;
}
// Get the dmdrvi_open function
dmod_dmdrvi_open_t dmdrvi_open = Dmod_GetDifFunction(driver_node->driver, dmod_dmdrvi_open_sig);
if(dmdrvi_open == NULL)
{
DMOD_LOG_ERROR("Driver does not implement dmdrvi_open\n");
return DMFSI_ERR_NOT_FOUND;
}
// Create file handle
file_handle_t* handle = Dmod_Malloc(sizeof(file_handle_t));
if(handle == NULL)
{
DMOD_LOG_ERROR("Failed to allocate memory for file handle\n");
return DMFSI_ERR_GENERAL;
}
// Open the device through the driver
// Note: dmdrvi_open only takes context and flags, returns device handle
handle->driver_handle = dmdrvi_open(driver_node->driver_context, mode);
if(handle->driver_handle == NULL)
{
DMOD_LOG_ERROR("Driver failed to open device: %s\n", path);
Dmod_Free(handle);
return DMFSI_ERR_GENERAL;
}
handle->driver = driver_node;
handle->path = Dmod_StrDup(path);
handle->mode = mode;
handle->attr = attr;
*fp = handle;
return DMFSI_OK;
}
/**
* @brief Close a file
*/
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _fclose, (dmfsi_context_t ctx, void* fp) )
{
if(dmfsi_dmdevfs_context_is_valid(ctx) == 0)
{
DMOD_LOG_ERROR("Invalid context in fclose\n");
return DMFSI_ERR_INVALID;
}
if(fp == NULL)
{
DMOD_LOG_ERROR("NULL file pointer in fclose\n");
return DMFSI_ERR_INVALID;
}
file_handle_t* handle = (file_handle_t*)fp;
// Get the dmdrvi_close function
dmod_dmdrvi_close_t dmdrvi_close = Dmod_GetDifFunction(handle->driver->driver, dmod_dmdrvi_close_sig);
if(dmdrvi_close != NULL)
{
dmdrvi_close(handle->driver->driver_context, handle->driver_handle);
}
// Free the path string that was duplicated in fopen
if(handle->path)
{
Dmod_Free((void*)handle->path);
}
Dmod_Free(handle);
return DMFSI_OK;
}
/**
* @brief Read from a file
*/
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _fread, (dmfsi_context_t ctx, void* fp, void* buffer, size_t size, size_t* read) )
{
if(dmfsi_dmdevfs_context_is_valid(ctx) == 0)
{
DMOD_LOG_ERROR("Invalid context in fread\n");
if(read) *read = 0;
return DMFSI_ERR_INVALID;
}
if(fp == NULL || buffer == NULL)
{
if(read) *read = 0;
return DMFSI_ERR_INVALID;
}
file_handle_t* handle = (file_handle_t*)fp;
// Get the dmdrvi_read function
dmod_dmdrvi_read_t dmdrvi_read = Dmod_GetDifFunction(handle->driver->driver, dmod_dmdrvi_read_sig);
if(dmdrvi_read == NULL)
{
DMOD_LOG_ERROR("Driver does not implement dmdrvi_read\n");
if(read) *read = 0;
return DMFSI_ERR_NOT_FOUND;
}
// dmdrvi_read returns size_t (bytes read), not error code
size_t bytes_read = dmdrvi_read(handle->driver->driver_context, handle->driver_handle, buffer, size);
if(read) *read = bytes_read;
return DMFSI_OK;
}
/**
* @brief Write to a file
*/
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _fwrite, (dmfsi_context_t ctx, void* fp, const void* buffer, size_t size, size_t* written) )
{
if(dmfsi_dmdevfs_context_is_valid(ctx) == 0)
{
DMOD_LOG_ERROR("Invalid context in fwrite\n");
if(written) *written = 0;
return DMFSI_ERR_INVALID;
}
if(fp == NULL || buffer == NULL)
{
if(written) *written = 0;
return DMFSI_ERR_INVALID;
}
file_handle_t* handle = (file_handle_t*)fp;
// Get the dmdrvi_write function
dmod_dmdrvi_write_t dmdrvi_write = Dmod_GetDifFunction(handle->driver->driver, dmod_dmdrvi_write_sig);
if(dmdrvi_write == NULL)
{
DMOD_LOG_ERROR("Driver does not implement dmdrvi_write\n");
if(written) *written = 0;
return DMFSI_ERR_NOT_FOUND;
}
// dmdrvi_write returns size_t (bytes written), not error code
size_t bytes_written = dmdrvi_write(handle->driver->driver_context, handle->driver_handle, buffer, size);
if(written) *written = bytes_written;
return DMFSI_OK;
}
/**
* @brief Seek to a position in a file
* @note Not supported for device drivers - devices are typically non-seekable
*/
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _lseek, (dmfsi_context_t ctx, void* fp, long offset, int whence) )
{
if(dmfsi_dmdevfs_context_is_valid(ctx) == 0)
{
DMOD_LOG_ERROR("Invalid context in lseek\n");
return DMFSI_ERR_INVALID;
}
if(fp == NULL)
{
return DMFSI_ERR_INVALID;
}
// Device drivers typically don't support seek operations
// Return error to indicate operation not supported
DMOD_LOG_ERROR("lseek not supported for device drivers\n");
return DMFSI_ERR_GENERAL;
}
/**
* @brief Get current position in a file
* @note Not supported for device drivers - devices are typically non-seekable
*/
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, long, _tell, (dmfsi_context_t ctx, void* fp) )
{
if(dmfsi_dmdevfs_context_is_valid(ctx) == 0)
{
DMOD_LOG_ERROR("Invalid context in tell\n");
return -1;
}
if(fp == NULL)
{
return -1;
}
// Device drivers typically don't support tell operations
DMOD_LOG_ERROR("tell not supported for device drivers\n");
return -1;
}
/**
* @brief Check if at end of file
* @note Device drivers typically operate in streaming mode - always return 0 (not at EOF)
*/
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _eof, (dmfsi_context_t ctx, void* fp) )
{
if(dmfsi_dmdevfs_context_is_valid(ctx) == 0)
{
DMOD_LOG_ERROR("Invalid context in eof\n");
return 1;
}
if(fp == NULL)
{
return 1;
}
// Device drivers typically don't have EOF concept
// Return 0 (not at EOF) as devices can always potentially provide more data
return 0;
}
/**
* @brief Get file size
* @note Device drivers represent devices, not files with fixed sizes. Use stat for size info.
*/
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, long, _size, (dmfsi_context_t ctx, void* fp) )
{
if(dmfsi_dmdevfs_context_is_valid(ctx) == 0)
{
DMOD_LOG_ERROR("Invalid context in size\n");
return -1;
}
if(fp == NULL)
{
return -1;
}
file_handle_t* handle = (file_handle_t*)fp;
// Try to get size from stat if available
dmdrvi_stat_t stat = {0};
int result = driver_stat(handle->driver, handle->path, &stat);
if(result == 0)
{
return (long)stat.size;
}
// Size not available for this device
return -1;
}
/**
* @brief Read a single character
*/
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _getc, (dmfsi_context_t ctx, void* fp) )
{
if(dmfsi_dmdevfs_context_is_valid(ctx) == 0)
{
DMOD_LOG_ERROR("Invalid context in getc\n");
return -1;
}
if(fp == NULL)
{
return -1;
}
unsigned char ch;
size_t bytes_read = 0;
int result = dmfsi_dmdevfs_fread(ctx, fp, &ch, 1, &bytes_read);
if(result != DMFSI_OK || bytes_read != 1)
{
return -1;
}
return (int)ch;
}
/**
* @brief Write a single character
*/
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _putc, (dmfsi_context_t ctx, void* fp, char c) )
{
if(dmfsi_dmdevfs_context_is_valid(ctx) == 0)
{
DMOD_LOG_ERROR("Invalid context in putc\n");
return -1;
}
if(fp == NULL)
{
return -1;
}
unsigned char ch = (unsigned char)c;
size_t bytes_written = 0;
int result = dmfsi_dmdevfs_fwrite(ctx, fp, &ch, 1, &bytes_written);
if(result != DMFSI_OK || bytes_written != 1)
{
return -1;
}
return (int)ch;
}
/**
* @brief Flush file buffers
*/
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _fflush, (dmfsi_context_t ctx, void* fp) )
{
if(dmfsi_dmdevfs_context_is_valid(ctx) == 0)
{
DMOD_LOG_ERROR("Invalid context in fflush\n");
return DMFSI_ERR_INVALID;
}
if(fp == NULL)
{
return DMFSI_ERR_INVALID;
}
file_handle_t* handle = (file_handle_t*)fp;
// Get the dmdrvi_flush function
dmod_dmdrvi_flush_t dmdrvi_flush = Dmod_GetDifFunction(handle->driver->driver, dmod_dmdrvi_flush_sig);
if(dmdrvi_flush == NULL)
{
// Flush not supported by driver, return OK
return DMFSI_OK;
}
int result = dmdrvi_flush(handle->driver->driver_context, handle->driver_handle);
if(result != 0)
{
return DMFSI_ERR_GENERAL;
}
return DMFSI_OK;
}
/**
* @brief Sync file to storage
* @note For device drivers, sync/flush are equivalent operations
*/
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _sync, (dmfsi_context_t ctx, void* fp) )
{
if(dmfsi_dmdevfs_context_is_valid(ctx) == 0)
{
DMOD_LOG_ERROR("Invalid context in sync\n");
return DMFSI_ERR_INVALID;
}
if(fp == NULL)
{
return DMFSI_ERR_INVALID;
}
file_handle_t* handle = (file_handle_t*)fp;
// Get the dmdrvi_flush function (sync and flush are equivalent for devices)
dmod_dmdrvi_flush_t dmdrvi_flush = Dmod_GetDifFunction(handle->driver->driver, dmod_dmdrvi_flush_sig);
if(dmdrvi_flush == NULL)
{
// Sync not supported by driver, return OK
return DMFSI_OK;
}
int result = dmdrvi_flush(handle->driver->driver_context, handle->driver_handle);
if(result != 0)
{
return DMFSI_ERR_GENERAL;
}
return DMFSI_OK;
}
/**
* @brief Open a directory
*/
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _opendir, (dmfsi_context_t ctx, void** dp, const char* path) )
{
if(dmfsi_dmdevfs_context_is_valid(ctx) == 0)
{
DMOD_LOG_ERROR("Invalid context in opendir\n");
return DMFSI_ERR_INVALID;
}
if (!is_directory(ctx, path))
{
DMOD_LOG_ERROR("Directory not found: %s\n", path);
return DMFSI_ERR_NOT_FOUND;
}
directory_node_t* dir_node = Dmod_Malloc(sizeof(directory_node_t));
if (dir_node == NULL)
{
DMOD_LOG_ERROR("Failed to allocate memory for directory node\n");
return DMFSI_ERR_GENERAL;
}
dir_node->driver = get_next_driver_node(ctx, NULL, path);
dir_node->directory_path = Dmod_StrDup(path);
*dp = dir_node;
return DMFSI_OK;
}
/**
* @brief Read directory entry
*/
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _readdir, (dmfsi_context_t ctx, void* dp, dmfsi_dir_entry_t* entry) )
{
if(dmfsi_dmdevfs_context_is_valid(ctx) == 0)
{
DMOD_LOG_ERROR("Invalid context in readdir\n");
return DMFSI_ERR_INVALID;
}
directory_node_t* dir_node = (directory_node_t*)dp;
if (dir_node->driver == NULL)
{
return DMFSI_ERR_NOT_FOUND; // No more entries
}
driver_node_t* driver = dir_node->driver;
path_t parent_dir;
if (read_driver_parent_directory(dir_node->driver, parent_dir, sizeof(parent_dir)) != 0)
{
DMOD_LOG_ERROR("Failed to read parent directory for driver\n");
return DMFSI_ERR_GENERAL;
}
bool file_should_be_listed = strcmp(dir_node->directory_path, parent_dir) == 0;
if(file_should_be_listed)
{
strncpy(entry->name, driver->path, sizeof(entry->name));
dmdrvi_stat_t stat;
int res = driver_stat(driver, driver->path, &stat);
if (res != 0)
{
DMOD_LOG_ERROR("Failed to get file stats for: %s\n", driver->path);
return DMFSI_ERR_GENERAL;
}
entry->size = stat.size;
entry->attr = stat.mode;
}
else
{
strncpy(entry->name, parent_dir, sizeof(entry->name));
entry->size = 0;
entry->attr = DMFSI_ATTR_DIRECTORY;
}
// Move to next driver for subsequent call
dir_node->driver = get_next_driver_node(ctx, driver, dir_node->directory_path);
return DMFSI_OK;
}
/**
* @brief Close a directory
*/
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _closedir, (dmfsi_context_t ctx, void* dp) )
{
if(dmfsi_dmdevfs_context_is_valid(ctx) == 0)
{
DMOD_LOG_ERROR("Invalid context in closedir\n");
return DMFSI_ERR_INVALID;
}
directory_node_t* dir_node = (directory_node_t*)dp;
Dmod_Free(dir_node->directory_path);
Dmod_Free(dir_node);
return DMFSI_OK;
}
/**
* @brief Create a directory
*/
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _mkdir, (dmfsi_context_t ctx, const char* path) )
{
return DMFSI_ERR_INVALID; // Not supported
}
/**
* @brief Check if directory exists
*/
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _direxists, (dmfsi_context_t ctx, const char* path) )
{
if(dmfsi_dmdevfs_context_is_valid(ctx) == 0)
{
DMOD_LOG_ERROR("Invalid context in direxists\n");
return 0;
}
return is_directory(ctx, path) ? 1 : 0;
}
/**
* @brief Get file/directory statistics
*/
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _stat, (dmfsi_context_t ctx, const char* path, dmfsi_stat_t* stat) )
{
if(dmfsi_dmdevfs_context_is_valid(ctx) == 0)
{
DMOD_LOG_ERROR("Invalid context in stat\n");
return DMFSI_ERR_INVALID;
}
driver_node_t* driver_node = find_driver_node(ctx, path);
if (driver_node == NULL)
{
DMOD_LOG_ERROR("File not found in stat: %s\n", path);
return DMFSI_ERR_NOT_FOUND;
}
dmdrvi_stat_t driver_stat_buf;
int res = driver_stat(driver_node, path, &driver_stat_buf);
if (res != 0)
{
DMOD_LOG_ERROR("Failed to get file stats for: %s\n", path);
return DMFSI_ERR_GENERAL;
}
stat->size = driver_stat_buf.size;
stat->attr = driver_stat_buf.mode;
return DMFSI_OK;
}
/**
* @brief Delete a file
* @note Not supported for device drivers - devices cannot be deleted through filesystem operations
*/
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _unlink, (dmfsi_context_t ctx, const char* path) )
{
if(dmfsi_dmdevfs_context_is_valid(ctx) == 0)
{
DMOD_LOG_ERROR("Invalid context in unlink\n");
return DMFSI_ERR_INVALID;
}
if(path == NULL)
{
DMOD_LOG_ERROR("NULL path in unlink\n");
return DMFSI_ERR_INVALID;
}
// Device files cannot be deleted through filesystem operations
DMOD_LOG_ERROR("unlink not supported for device drivers\n");
return DMFSI_ERR_GENERAL;
}
/**
* @brief Rename a file
* @note Not supported for device drivers - devices cannot be renamed through filesystem operations
*/
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _rename, (dmfsi_context_t ctx, const char* oldpath, const char* newpath) )
{
if(dmfsi_dmdevfs_context_is_valid(ctx) == 0)
{
DMOD_LOG_ERROR("Invalid context in rename\n");
return DMFSI_ERR_INVALID;
}
if(oldpath == NULL || newpath == NULL)
{
DMOD_LOG_ERROR("NULL path in rename\n");
return DMFSI_ERR_INVALID;
}
// Device files cannot be renamed through filesystem operations
DMOD_LOG_ERROR("rename not supported for device drivers\n");
return DMFSI_ERR_GENERAL;
}
// ============================================================================
// Local functions
// ============================================================================
/**
* @brief Configure drivers based on the configuration file
*/
static int configure_drivers(dmfsi_context_t ctx, const char* driver_name, const char* config_path)
{
void* dir = Dmod_OpenDir(config_path);
if (dir == NULL)
{
DMOD_LOG_ERROR("Failed to open config directory: %s\n", config_path);
return DMFSI_ERR_NOT_FOUND;
}
const char* entry;
while ((entry = Dmod_ReadDir(dir)) != NULL)
{
// Construct full path for the entry
char full_path[MAX_PATH_LENGTH];
size_t config_path_len = strlen(config_path);
size_t entry_len = strlen(entry);
// Check if we need a separator
bool needs_separator = (config_path_len > 0 && config_path[config_path_len - 1] != '/');
size_t required_len = config_path_len + (needs_separator ? 1 : 0) + entry_len + 1;
if (required_len > MAX_PATH_LENGTH)
{
DMOD_LOG_ERROR("Path too long: %s/%s\n", config_path, entry);
continue;
}
Dmod_SnPrintf(full_path, sizeof(full_path), "%s%s%s",
config_path,
needs_separator ? "/" : "",
entry);
if (is_file(full_path))
{
char module_name[DMOD_MAX_MODULE_NAME_LENGTH];
dmini_context_t config_ctx = read_driver_for_config(full_path, module_name, sizeof(module_name), driver_name);
if (config_ctx == NULL)
{
DMOD_LOG_ERROR("Failed to read driver for config: %s\n", full_path);
continue;
}
driver_node_t* driver_node = configure_driver(module_name, config_ctx);
// Third priority: if driver with filename-based name failed and we have a parent directory name,
// try using the parent directory name as fallback
if (driver_node == NULL && driver_name != NULL && module_name[0] != '\0' &&
strcmp(module_name, driver_name) != 0)
{
DMOD_LOG_INFO("Driver '%s' not found, trying fallback to parent directory name '%s'\n",
module_name, driver_name);
driver_node = configure_driver(driver_name, config_ctx);
}
dmini_destroy(config_ctx);
if (driver_node == NULL)
{
DMOD_LOG_ERROR("Failed to configure driver: %s\n", module_name);
continue;
}
if(dmlist_push_back(ctx->drivers, driver_node) != 0)
{
DMOD_LOG_ERROR("Failed to add driver to list: %s\n", module_name);
Dmod_Free(driver_node);
continue;
}
}
else
{
// read driver name from directory name
char module_name[DMOD_MAX_MODULE_NAME_LENGTH];
read_base_name(entry, module_name, sizeof(module_name));
int res = configure_drivers(ctx, driver_name, full_path);
if (res != DMFSI_OK)
{
DMOD_LOG_ERROR("Failed to configure drivers in directory: %s\n", full_path);
}
}
}
Dmod_CloseDir(dir);
return DMFSI_OK;
}
/**
* @brief Configure a single driver based on its name and configuration file
*/
static driver_node_t* configure_driver(const char* driver_name, dmini_context_t config_ctx)
{
DMOD_LOG_VERBOSE("Configuring driver: %s\n", driver_name);
bool was_loaded = false;
bool was_enabled = false;
Dmod_Context_t* driver = prepare_driver_module(driver_name, &was_loaded, &was_enabled);
if (driver == NULL)
{
return NULL;
}
dmod_dmdrvi_create_t dmdrvi_create = Dmod_GetDifFunction(driver, dmod_dmdrvi_create_sig);
if (dmdrvi_create == NULL)
{
DMOD_LOG_ERROR("Driver module does not implement dmdrvi_create: %s\n", driver_name);
cleanup_driver_module(driver_name, was_loaded, was_enabled);
return NULL;
}
driver_node_t* driver_node = Dmod_Malloc(sizeof(driver_node_t));
if (driver_node == NULL)
{
DMOD_LOG_ERROR("Failed to allocate memory for driver node: %s\n", driver_name);
cleanup_driver_module(driver_name, was_loaded, was_enabled);
return NULL;
}
driver_node->was_loaded = was_loaded;
driver_node->was_enabled = was_enabled;
driver_node->driver = driver;
driver_node->driver_context = dmdrvi_create(config_ctx, &driver_node->dev_num);
if (driver_node->driver_context == NULL)
{
DMOD_LOG_ERROR("Failed to create driver context: %s\n", driver_name);
cleanup_driver_module(driver_name, was_loaded, was_enabled);
Dmod_Free(driver_node);
return NULL;
}
if(read_driver_node_path( driver_node, driver_node->path, sizeof(driver_node->path) ) != 0)
{
DMOD_LOG_ERROR("Failed to read driver node path: %s\n", driver_name);
dmod_dmdrvi_free_t dmdrvi_free = Dmod_GetDifFunction(driver, dmod_dmdrvi_free_sig);
if (dmdrvi_free != NULL)
{
dmdrvi_free(driver_node->driver_context);
}
cleanup_driver_module(driver_name, was_loaded, was_enabled);
Dmod_Free(driver_node);
return NULL;
}
DMOD_LOG_INFO("Configured driver: %s (path: %s)\n", driver_name, driver_node->path);
return driver_node;
}
/**
* @brief Unconfigure and unload all drivers
*/
static int unconfigure_drivers(dmfsi_context_t ctx)
{
if (ctx == NULL || ctx->drivers == NULL)
{
return DMFSI_ERR_INVALID;
}
size_t list_size = dmlist_size(ctx->drivers);
for (size_t i = 0; i < list_size; i++)
{
driver_node_t* driver_node = (driver_node_t*)dmlist_get(ctx->drivers, i);
if (driver_node != NULL)
{
dmod_dmdrvi_free_t dmdrvi_free = Dmod_GetDifFunction(driver_node->driver, dmod_dmdrvi_free_sig);
if (dmdrvi_free != NULL)
{
dmdrvi_free(driver_node->driver_context);
DMOD_LOG_INFO("Freed driver context for: %s\n", Dmod_GetName(driver_node->driver));
}
cleanup_driver_module(Dmod_GetName(driver_node->driver), driver_node->was_loaded, driver_node->was_enabled);
Dmod_Free(driver_node);
}
}
dmlist_clear(ctx->drivers);
DMOD_LOG_INFO("Unconfigured all drivers\n");
return DMFSI_OK;
}
/**
* @brief Check if a path is a file
*/
static bool is_file(const char* path)
{
// Check if path exists
if (Dmod_Access(path, DMOD_F_OK) != 0)
{
return false;
}
// Try to open as directory - if it succeeds, it's a directory, not a file
void* dir_handle = Dmod_OpenDir(path);
if (dir_handle != NULL)
{
Dmod_CloseDir(dir_handle);
return false; // It's a directory
}
return true; // It's a file
}
/**
* @brief Extract base name from a path
*/
static void read_base_name(const char* path, char* base_name, size_t name_size)
{
const char* last_slash = strrchr(path, '/');