Skip to content

Commit

Permalink
Add function to get current switch label from config
Browse files Browse the repository at this point in the history
  • Loading branch information
knro committed Jun 23, 2021
1 parent d7af9d6 commit a761417
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
60 changes: 60 additions & 0 deletions indidriver.c
Original file line number Diff line number Diff line change
Expand Up @@ -1326,6 +1326,66 @@ int IUGetConfigOnSwitchIndex(const char *dev, const char *property, int *index)
return (*index >= 0 ? 0 : -1);
}

int IUGetConfigOnSwitchLabel(const char *dev, const char *property, char *label, size_t size)
{
char *rname, *rdev;
XMLEle *root = NULL, *fproot = NULL;
char errmsg[MAXRBUF];
LilXML *lp = newLilXML();
int found = -1;

FILE *fp = IUGetConfigFP(NULL, dev, "r", errmsg);

if (fp == NULL)
return -1;

fproot = readXMLFile(fp, lp, errmsg);

if (fproot == NULL)
{
fclose(fp);
return -1;
}

for (root = nextXMLEle(fproot, 1); root != NULL; root = nextXMLEle(fproot, 0))
{
/* pull out device and name */
if (crackDN(root, &rdev, &rname, errmsg) < 0)
{
fclose(fp);
delXMLEle(fproot);
return -1;
}

// It doesn't belong to our device??
if (strcmp(dev, rdev))
continue;

if ((property && !strcmp(property, rname)) || property == NULL)
{
XMLEle *oneSwitch = NULL;
int currentIndex = 0;
for (oneSwitch = nextXMLEle(root, 1); oneSwitch != NULL; oneSwitch = nextXMLEle(root, 0), currentIndex++)
{
ISState s = ISS_OFF;
if (crackISState(pcdataXMLEle(oneSwitch), &s) == 0 && s == ISS_ON)
{
found = 0;
strncpy(label, findXMLAttValu(oneSwitch, "name"), size);
break;
}
}
break;
}
}

fclose(fp);
delXMLEle(fproot);
delLilXML(lp);

return found;
}

int IUGetConfigNumber(const char *dev, const char *property, const char *member, double *value)
{
char *rname, *rdev;
Expand Down
11 changes: 11 additions & 0 deletions indidriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,17 @@ extern int IUGetConfigSwitch(const char *dev, const char *property, const char *
*/
extern int IUGetConfigOnSwitchIndex(const char *dev, const char *property, int *index);


/**
* @brief IUGetConfigOnSwitchLabel Opens configuration file and reads single switch property to find ON switch index, if any.
* @param dev name of device
* @param property name of vector property
* @param label of the ON switch index if any.
* @param size size of label in bytes.
* @return 0 on success, -1 if not found.
*/
extern int IUGetConfigOnSwitchLabel(const char *dev, const char *property, char *label, size_t size);

/**
* @brief IUGetConfigOnSwitch Opens configuration file and reads a single switch vector property to find the index
* of the first ON switch element.
Expand Down

0 comments on commit a761417

Please sign in to comment.