Skip to content

Commit 0601708

Browse files
CopilotJohnAmadis
andcommitted
Refactor to iterator-based API pattern and add documentation
- Changed from Dmod_ReadModules to OpenModules/ReadModule/CloseModules pattern - Added Doxygen documentation for Dmod_ModuleState_t enum - Added Doxygen documentation for Dmod_ModuleInfo_t struct - Added Dmod_ModulesIterator_t opaque handle type - Updated example application to use new API - Updated unit tests (8 tests passing) - Memory efficient: no large array allocation required Co-authored-by: JohnAmadis <17320783+JohnAmadis@users.noreply.github.com>
1 parent a71af90 commit 0601708

File tree

5 files changed

+364
-186
lines changed

5 files changed

+364
-186
lines changed

examples/system/list_modules/main.c

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/**
66
* @brief Simple test application to list all modules
77
*
8-
* This application demonstrates the usage of Dmod_ReadModules API
8+
* This application demonstrates the usage of Dmod_OpenModules/ReadModule/CloseModules API
99
*/
1010
int main( int argc, char *argv[] )
1111
{
@@ -36,53 +36,63 @@ int main( int argc, char *argv[] )
3636
printf("\n");
3737
}
3838

39-
// Allocate array for module info
40-
Dmod_ModuleInfo_t modules[100];
41-
size_t count = Dmod_ReadModules( modules, 100 );
39+
// Open modules iterator
40+
Dmod_ModulesIterator_t iterator = Dmod_OpenModules();
41+
if( iterator == NULL )
42+
{
43+
printf("Error: Failed to open modules iterator\n");
44+
return -1;
45+
}
4246

43-
printf("Found %zu modules:\n\n", count);
47+
printf("Listing modules:\n\n");
48+
printf("%-30s %-15s %-15s\n", "Module Name", "Version", "State");
49+
printf("%-30s %-15s %-15s\n", "--------------------------------", "---------------", "---------------");
4450

45-
if( count > 0 )
51+
// Iterate through modules
52+
size_t count = 0;
53+
const Dmod_ModuleInfo_t* module;
54+
while( (module = Dmod_ReadModule( iterator )) != NULL )
4655
{
47-
// Print header
48-
printf("%-30s %-15s %-15s\n", "Module Name", "Version", "State");
49-
printf("%-30s %-15s %-15s\n", "--------------------------------", "---------------", "---------------");
50-
51-
// Print each module
52-
for( size_t i = 0; i < count; i++ )
56+
const char* stateStr = "Unknown";
57+
switch( module->State )
5358
{
54-
const char* stateStr = "Unknown";
55-
switch( modules[i].State )
56-
{
57-
case Dmod_ModuleState_Available:
58-
stateStr = "Available";
59-
break;
60-
case Dmod_ModuleState_Loaded:
61-
stateStr = "Loaded";
62-
break;
63-
case Dmod_ModuleState_Enabled:
64-
stateStr = "Enabled";
65-
break;
66-
case Dmod_ModuleState_Running:
67-
stateStr = "Running";
68-
break;
69-
default:
70-
stateStr = "Unknown";
71-
break;
72-
}
73-
74-
printf("%-30s %-15s %-15s\n",
75-
modules[i].ModuleName,
76-
modules[i].Version[0] != '\0' ? modules[i].Version : "N/A",
77-
stateStr);
59+
case Dmod_ModuleState_Available:
60+
stateStr = "Available";
61+
break;
62+
case Dmod_ModuleState_Loaded:
63+
stateStr = "Loaded";
64+
break;
65+
case Dmod_ModuleState_Enabled:
66+
stateStr = "Enabled";
67+
break;
68+
case Dmod_ModuleState_Running:
69+
stateStr = "Running";
70+
break;
71+
default:
72+
stateStr = "Unknown";
73+
break;
7874
}
75+
76+
printf("%-30s %-15s %-15s\n",
77+
module->ModuleName,
78+
module->Version[0] != '\0' ? module->Version : "N/A",
79+
stateStr);
80+
count++;
7981
}
80-
else
82+
83+
// Close iterator
84+
Dmod_CloseModules( iterator );
85+
86+
if( count == 0 )
8187
{
82-
printf("No modules found. Try:\n");
88+
printf("\nNo modules found. Try:\n");
8389
printf(" - Setting DMOD_REPO_PATHS environment variable to a directory with .dmf/.dmfc files\n");
8490
printf(" - Running with module name arguments: %s <module_name>\n", argv[0]);
8591
}
92+
else
93+
{
94+
printf("\nTotal modules found: %zu\n", count);
95+
}
8696

8797
printf("\n");
8898
return 0;

inc/dmod.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ DMOD_BUILTIN_API( Dmod, 1.0, size_t , _GetNumberOfPackages, ( void ) );
126126
DMOD_BUILTIN_API( Dmod, 1.0, bool , _GetPackageInfo, ( uint32_t PackageIndex, char* outName, size_t NameMaxLength, size_t* outSize ) );
127127
DMOD_BUILTIN_API( Dmod, 1.0, uint32_t , _GetMainIndexFromPackage, ( const char* PackageName ) );
128128

129-
DMOD_BUILTIN_API( Dmod, 1.0, size_t , _ReadModules, ( Dmod_ModuleInfo_t* outModules, size_t Max ) );
129+
DMOD_BUILTIN_API( Dmod, 1.0, Dmod_ModulesIterator_t, _OpenModules, ( void ) );
130+
DMOD_BUILTIN_API( Dmod, 1.0, const Dmod_ModuleInfo_t*, _ReadModule, ( Dmod_ModulesIterator_t Iterator ) );
131+
DMOD_BUILTIN_API( Dmod, 1.0, void , _CloseModules, ( Dmod_ModulesIterator_t Iterator ) );
130132

131133
//! @}
132134

inc/dmod_types.h

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ typedef enum
2727
Dmod_ModuleType_Count,
2828
} Dmod_ModuleType_t;
2929

30+
/**
31+
* @brief Module state enumeration
32+
*
33+
* Describes the current state of a module in the system.
34+
*/
3035
typedef enum
3136
{
3237
Dmod_ModuleState_Available, //!< Module is available but not loaded
@@ -37,13 +42,25 @@ typedef enum
3742
Dmod_ModuleState_Count,
3843
} Dmod_ModuleState_t;
3944

45+
/**
46+
* @brief Module information structure
47+
*
48+
* Contains information about a module including its name, version, and current state.
49+
*/
4050
typedef struct
4151
{
42-
char ModuleName[DMOD_MAX_MODULE_NAME_LENGTH];
43-
char Version[DMOD_MAX_VERSION_LENGTH];
44-
Dmod_ModuleState_t State;
52+
char ModuleName[DMOD_MAX_MODULE_NAME_LENGTH]; //!< Name of the module
53+
char Version[DMOD_MAX_VERSION_LENGTH]; //!< Version string of the module
54+
Dmod_ModuleState_t State; //!< Current state of the module
4555
} Dmod_ModuleInfo_t;
4656

57+
/**
58+
* @brief Modules iterator handle
59+
*
60+
* @note Opaque handle for iterating through modules. Use Dmod_OpenModules, Dmod_ReadModule, and Dmod_CloseModules.
61+
*/
62+
typedef void* Dmod_ModulesIterator_t;
63+
4764
typedef struct
4865
{
4966
uint32_t Size;

0 commit comments

Comments
 (0)