src/drivers: Ensure existing dir and params file

Otherwise, if no drivers are generated by GfDrivers::ensure_min(), the
driver type (e.g.: dandroid) would not be available from the driver
selection menu because its parameter file (e.g.: dandroid.xml) would not
be available.
This commit is contained in:
Xavier Del Campo Romero 2025-01-30 23:34:06 +01:00
parent c650fa8faa
commit 2f72b8b9e8
Signed by: xavi
GPG key ID: 84FF3612A9BF43F2
4 changed files with 126 additions and 50 deletions

View file

@ -82,11 +82,22 @@ extern "C" int moduleWelcome(const tModWelcomeIn * welcomeIn,
// Save module name and loadDir, and determine module XML file pathname.
setRobotName(welcomeIn->name);
// Filehandle for robot's xml-file
void* pRobotSettings = GfParmReadFileLocal(pathBuffer, GFPARM_RMODE_STD);
PLogDANDROID = GfLogger::instance("DANDROID");
std::string dirstr = std::string(GfLocalDir()) + "drivers/dandroid";
const char *dir = dirstr.c_str();
if (GfDirCreate(dir) != GF_DIR_CREATED)
{
PLogDANDROID->error("GfDirCreate %s failed\n", dir);
return -1;
}
int ret = 0;
// Filehandle for robot's xml-file
void* pRobotSettings = GfParmReadFileLocal(pathBuffer,
GFPARM_RMODE_STD | GFPARM_RMODE_CREAT);
// Loop over all possible drivers, clear all buffers,
// save defined driver names and descriptions.
driver.clear();
@ -94,22 +105,15 @@ extern "C" int moduleWelcome(const tModWelcomeIn * welcomeIn,
if (pRobotSettings) // robot settings XML could be read
{
char SectionBuffer[BUFSIZE];
snprintf(SectionBuffer, BUFSIZE, "%s/%s/%d", ROB_SECT_ROBOTS, ROB_LIST_INDEX, 0);
// Try to get first driver from index 0
string sDriverName = GfParmGetStrNC(pRobotSettings,
SectionBuffer,
ROB_ATTR_NAME,
const_cast<char*>(sUndefined));
int n = GfParmGetEltNb(pRobotSettings, ROB_SECT_ROBOTS "/" ROB_LIST_INDEX);
for (int i = 0; i < n; i++)
{
char SectionBuffer[BUFSIZE];
snprintf(SectionBuffer, BUFSIZE, "%s/%s/%d", ROB_SECT_ROBOTS, ROB_LIST_INDEX, i);
sDriverName = GfParmGetStr(pRobotSettings, SectionBuffer,
std::string sDriverName = GfParmGetStr(pRobotSettings, SectionBuffer,
ROB_ATTR_NAME, sUndefined);
string sDriverDesc = GfParmGetStr(pRobotSettings, SectionBuffer,
@ -118,6 +122,12 @@ extern "C" int moduleWelcome(const tModWelcomeIn * welcomeIn,
driver.push_back(TDriver(i));
} // for i
if (GfParmWriteFile(nullptr, pRobotSettings, "dandroid"))
{
PLogDANDROID->error("GfParmWriteFile failed\n");
ret = -1;
}
GfParmReleaseHandle(pRobotSettings);
}
else
@ -128,7 +138,7 @@ extern "C" int moduleWelcome(const tModWelcomeIn * welcomeIn,
// Set max nb of interfaces to return.
welcomeOut->maxNbItf = driver.size();
return 0;
return ret;
}
// Module entry point (new fixed name scheme).
@ -167,18 +177,24 @@ extern "C" int moduleTerminate()
// Module entry point
extern "C" int dandroid(tModInfo * modInfo)
{
std::string dirstr = std::string(GfLocalDir()) + "drivers/dandroid";
const char *dir = dirstr.c_str();
if (GfDirCreate(dir) != GF_DIR_CREATED)
return -1;
driver.clear();
Drivers.clear();
nameBuffer = "dandroid";
// Filehandle for robot's xml-file
void* pRobotSettings = GfParmReadFileLocal("drivers/dandroid/dandroid.xml",
GFPARM_RMODE_STD);
GFPARM_RMODE_STD | GFPARM_RMODE_CREAT);
if (pRobotSettings)
{ // Let's look what we have to provide here
char SectionBuffer[BUFSIZE];
int n = GfParmGetEltNb(pRobotSettings, ROB_SECT_ROBOTS);
int ret = 0, n = GfParmGetEltNb(pRobotSettings, ROB_SECT_ROBOTS);
for (int i = 0; i < n; i++)
{
@ -188,7 +204,14 @@ extern "C" int dandroid(tModInfo * modInfo)
Drivers.push_back(make_pair(sDriverName, sDriverDesc));
driver.push_back(TDriver(i));
}
if (GfParmWriteFile(nullptr, pRobotSettings, "usr"))
ret = -1;
GfParmReleaseHandle(pRobotSettings);
if (ret)
return ret;
}
return moduleInitialize(modInfo);

View file

@ -122,23 +122,24 @@ extern "C" int moduleWelcome(const tModWelcomeIn* welcomeIn, tModWelcomeOut* wel
{
// Save module name and loadDir, and determine module XML file pathname.
setRobotName(welcomeIn->name);
// Filehandle for robot's xml-file
void *pRobotSettings = GfParmReadFileLocal(pathBuffer, GFPARM_RMODE_STD);
PLogSHADOW = GfLogger::instance("SHADOW");
std::string dirstr = std::string(GfLocalDir()) + "drivers/shadow";
const char *dir = dirstr.c_str();
if (GfDirCreate(dir) != GF_DIR_CREATED)
{
PLogSHADOW->error("GfDirCreate %s failed\n", dir);
return -1;
}
int ret = 0;
// Filehandle for robot's xml-file
void *pRobotSettings = GfParmReadFileLocal(pathBuffer,
GFPARM_RMODE_STD | GFPARM_RMODE_CREAT);
if (pRobotSettings) // robot settings XML could be read
{
char SectionBuffer[BUFSIZE];
snprintf(SectionBuffer, BUFSIZE, "%s/%s/%d", ROB_SECT_ROBOTS, ROB_LIST_INDEX, 0);
// Try to get first driver from index 0
string sDriverName = GfParmGetStrNC(pRobotSettings,
SectionBuffer,
ROB_ATTR_NAME,
const_cast<char*>(sUndefined));
// Loop over all possible drivers, clear all buffers,
// save defined driver names and descriptions.
Drivers.clear();
@ -147,9 +148,11 @@ extern "C" int moduleWelcome(const tModWelcomeIn* welcomeIn, tModWelcomeOut* wel
for (int i = 0; i < n; i++)
{
char SectionBuffer[BUFSIZE];
snprintf(SectionBuffer, BUFSIZE, "%s/%s/%d", ROB_SECT_ROBOTS, ROB_LIST_INDEX, i);
sDriverName = GfParmGetStr(pRobotSettings, SectionBuffer,
std::string sDriverName = GfParmGetStr(pRobotSettings, SectionBuffer,
ROB_ATTR_NAME, sUndefined);
// This driver is defined in robot's xml-file
@ -158,6 +161,12 @@ extern "C" int moduleWelcome(const tModWelcomeIn* welcomeIn, tModWelcomeOut* wel
Drivers.push_back(make_pair(sDriverName, sDriverDesc));
} // for i
if (GfParmWriteFile(nullptr, pRobotSettings, "shadow"))
{
PLogSHADOW->error("GfParmWriteFile failed\n");
ret = -1;
}
GfParmReleaseHandle(pRobotSettings);
}
else
@ -169,7 +178,7 @@ extern "C" int moduleWelcome(const tModWelcomeIn* welcomeIn, tModWelcomeOut* wel
// Set max nb of interfaces to return.
welcomeOut->maxNbItf = Drivers.size();
return 0;
return ret;
}
// Module entry point (new fixed name scheme).
@ -210,12 +219,13 @@ extern "C" int shadow(tModInfo *modInfo)
nameBuffer = "shadow";
// Filehandle for robot's xml-file
void *pRobotSettings = GfParmReadFileLocal(pathBuffer, GFPARM_RMODE_STD);
void *pRobotSettings = GfParmReadFileLocal(pathBuffer,
GFPARM_RMODE_STD | GFPARM_RMODE_CREAT);
if (pRobotSettings)
{ // Let's look what we have to provide here
char SectionBuffer[BUFSIZE];
int n = GfParmGetEltNb(pRobotSettings, ROB_SECT_ROBOTS);
int ret = 0, n = GfParmGetEltNb(pRobotSettings, ROB_SECT_ROBOTS);
for (int i = 0; i < n; i++)
{
@ -224,7 +234,14 @@ extern "C" int shadow(tModInfo *modInfo)
std::string sDriverDesc = GfParmGetStr(pRobotSettings, SectionBuffer, ROB_ATTR_DESC, "");
Drivers.push_back(make_pair(sDriverName, sDriverDesc));
}
if (GfParmWriteFile(nullptr, pRobotSettings, "shadow"))
ret = -1;
GfParmReleaseHandle(pRobotSettings);
if (ret)
return ret;
}
return moduleInitialize(modInfo);

View file

@ -263,13 +263,19 @@ void* GetFileHandle(const char* RobotName)
snprintf(BufPathXMLRel, BUFSIZE, // Robot's xml-filename
"drivers/%s/%s.xml",RobotName,RobotName);// relative to installation
std::string dirstr = std::string(GfLocalDir()) + "drivers" + RobotName;
const char *dir = dirstr.c_str();
if (GfDirCreate(dir) != GF_DIR_CREATED)
return NULL;
// Test local installation path
snprintf(BufPathXML, BUFSIZE, "%s%s",
GetLocalDir(), RobPathXMLRel);
snprintf(BufPathDir, BUFSIZE, "%s%s",
GetLocalDir(), RobPathDirRel);
RobotSettings = GfParmReadFile
(RobPathXML, GFPARM_RMODE_STD );
(RobPathXML, GFPARM_RMODE_STD | GFPARM_RMODE_CREAT);
if (!RobotSettings)
{
@ -281,6 +287,13 @@ void* GetFileHandle(const char* RobotName)
RobotSettings = GfParmReadFile
(RobPathXML, GFPARM_RMODE_STD );
}
if (GfParmWriteFile(nullptr, RobotSettings, "simplix"))
{
GfParmReleaseHandle(RobotSettings);
return NULL;
}
return RobotSettings;
}
//==========================================================================*

View file

@ -93,22 +93,24 @@ extern "C" int moduleWelcome(const tModWelcomeIn* welcomeIn,
// Save module name and loadDir, and determine module XML file pathname.
setRobotName(welcomeIn->name);
// Filehandle for robot's xml-file
void *pRobotSettings = GfParmReadFileLocal(pathBuffer, GFPARM_RMODE_STD);
PLogUSR = GfLogger::instance("USR");
std::string dirstr = std::string(GfLocalDir()) + "drivers/usr";
const char *dir = dirstr.c_str();
if (GfDirCreate(dir) != GF_DIR_CREATED)
{
PLogUSR->error("GfDirCreate %s failed\n", dir);
return -1;
}
// Filehandle for robot's xml-file
int ret = 0;
void *pRobotSettings = GfParmReadFileLocal(pathBuffer,
GFPARM_RMODE_STD | GFPARM_RMODE_CREAT);
if (pRobotSettings) // robot settings XML could be read
{
char SectionBuffer[BUFSIZE];
snprintf(SectionBuffer, BUFSIZE, "%s/%s/%d", ROB_SECT_ROBOTS, ROB_LIST_INDEX, 0);
// Try to get first driver from index 0
string sDriverName = GfParmGetStrNC(pRobotSettings,
SectionBuffer,
ROB_ATTR_NAME,
const_cast<char*>(sUndefined));
// Loop over all possible drivers, clear all buffers,
// save defined driver names and descriptions.
Drivers.clear();
@ -117,9 +119,11 @@ extern "C" int moduleWelcome(const tModWelcomeIn* welcomeIn,
for (int i = 0; i < n; i++)
{
char SectionBuffer[BUFSIZE];
snprintf(SectionBuffer, BUFSIZE, "%s/%s/%d", ROB_SECT_ROBOTS, ROB_LIST_INDEX, i);
sDriverName = GfParmGetStr(pRobotSettings, SectionBuffer,
std::string sDriverName = GfParmGetStr(pRobotSettings, SectionBuffer,
ROB_ATTR_NAME, sUndefined);
// This driver is defined in robot's xml-file
@ -128,6 +132,12 @@ extern "C" int moduleWelcome(const tModWelcomeIn* welcomeIn,
Drivers.push_back(make_pair(sDriverName, sDriverDesc));
} // for i
if (GfParmWriteFile(nullptr, pRobotSettings, "usr"))
{
PLogUSR->error("GfDirCreate %s failed\n", dir);
ret = -1;
}
GfParmReleaseHandle(pRobotSettings);
}
else // if robot settings XML could not be read
@ -139,7 +149,7 @@ extern "C" int moduleWelcome(const tModWelcomeIn* welcomeIn,
// Set max nb of interfaces to return.
welcomeOut->maxNbItf = Drivers.size();
return 0;
return ret;
}
// Module entry point (new fixed name scheme).
@ -175,17 +185,24 @@ extern "C" int moduleTerminate()
// Module entry point
extern "C" int usr(tModInfo *modInfo)
{
std::string dirstr = std::string(GfLocalDir()) + "drivers/usr";
const char *dir = dirstr.c_str();
if (GfDirCreate(dir) != GF_DIR_CREATED)
return -1;
Drivers.clear();
pathBuffer = "drivers/usr/usr.xml";
nameBuffer = "usr";
// Filehandle for robot's xml-file
void *pRobotSettings = GfParmReadFileLocal(pathBuffer, GFPARM_RMODE_STD);
void *pRobotSettings = GfParmReadFileLocal(pathBuffer,
GFPARM_RMODE_STD | GFPARM_RMODE_CREAT);
if (pRobotSettings) // Let's look what we have to provide here
{
char SectionBuffer[BUFSIZE];
int n = GfParmGetEltNb(pRobotSettings, ROB_SECT_ROBOTS);
int ret = 0, n = GfParmGetEltNb(pRobotSettings, ROB_SECT_ROBOTS);
for (int i = 0; i < n; i++)
{
@ -195,7 +212,13 @@ extern "C" int usr(tModInfo *modInfo)
Drivers.push_back(make_pair(sDriverName, sDriverDesc));
}
if (GfParmWriteFile(nullptr, pRobotSettings, "usr"))
ret = -1;
GfParmReleaseHandle(pRobotSettings);
if (ret)
return ret;
}
return moduleInitialize(modInfo);