BigW Consortium Gitlab

Commit 2f05e797 by David Frey

simplify implementation of YellowOnBoardActuators

This new version uses substantially less code to do nearly the same thing. Data hub inputs are no longer created because they were not used and I believe that clients should be observing the things that control the LEDs rather than the LEDs themselves.
parent 8077ab3d
......@@ -4,19 +4,14 @@ version: 0.1
executables:
{
actuatorYellow = (
components/led/generic
components/led/triRed
components/led/triGreen
components/led/triBlue
)
actuatorYellow = ( components/yellowActuators )
}
processes:
{
run:
{
( actuatorYellow )
( actuatorYellow )
}
envVars:
......@@ -26,11 +21,7 @@ processes:
faultAction: stopApp
}
bindings:
{
actuatorYellow.generic.dhubIO -> dataHub.io
actuatorYellow.triRed.dhubIO -> dataHub.io
actuatorYellow.triGreen.dhubIO -> dataHub.io
actuatorYellow.triBlue.dhubIO -> dataHub.io
actuatorYellow.outputActuator.dhub -> dataHub.io
}
/**
*
* This file provides the implementation of @ref c_led
*
*
* <hr>
*
* Copyright (C) Sierra Wireless Inc.
*/
#include "legato.h"
#include "interfaces.h"
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(array) ((unsigned long) (sizeof (array) / sizeof ((array)[0])))
#endif
#define RES_PATH_GENERIC_LED "generic_led"
#define RES_PATH_GENERIC_VALUE "generic_led/value"
static const char GenericLedPath[] = "/sys/devices/platform/expander.0/generic_led";
typedef struct
{
const char *path;
dhubIO_DataType_t dataType;
const char *units;
dhubIO_BooleanPushHandlerRef_t ref;
}
actuator_DhOut_t;
static actuator_DhOut_t dhubResources[] = {
{RES_PATH_GENERIC_LED, DHUBIO_DATA_TYPE_BOOLEAN, "", NULL },
};
static void actuator_DhOutputHandler
(
double timestamp,
bool value,
void *context
)
{
const char *writeData = value ? "1" : "0";
FILE *ledFile = fopen(GenericLedPath, "r+");
if (ledFile == NULL)
{
LE_ERROR("Open LED device file('%s') failed(%d)", GenericLedPath, errno);
return;
}
LE_DEBUG("Turn %s LED", value ? "on" : "off");
if (fwrite(writeData, sizeof(writeData), 1, ledFile) != 1)
{
LE_ERROR("Write LED device file('%s') failed", GenericLedPath);
}
dhubIO_PushBoolean(RES_PATH_GENERIC_VALUE, DHUBIO_NOW, value);
dhubIO_MarkOptional(RES_PATH_GENERIC_LED);
fclose(ledFile);
}
static le_result_t actuator_DhRegister
(
void
)
{
le_result_t result = LE_OK;
for (int i = 0; i < ARRAY_SIZE(dhubResources); i++)
{
result = dhubIO_CreateOutput(dhubResources[i].path,
dhubResources[i].dataType,
dhubResources[i].units);
if (LE_OK != result)
{
LE_ERROR("Failed to create output resource %s", dhubResources[i].path);
break;
}
LE_ASSERT(LE_OK == dhubIO_CreateInput(RES_PATH_GENERIC_VALUE, DHUBIO_DATA_TYPE_BOOLEAN, ""));
dhubResources[i].ref = dhubIO_AddBooleanPushHandler(dhubResources[i].path, actuator_DhOutputHandler,
NULL );
if (NULL == dhubResources[i].ref)
{
LE_ERROR("Failed to add handler for output resource %s", dhubResources[i].path);
result = LE_FAULT;
break;
}
}
return result;
}
//--------------------------------------------------------------------------------------------------
/**
* SIGTERM handler to cleanly shutdown
*/
//--------------------------------------------------------------------------------------------------
static void actuator_SigTermHandler
(
int pSigNum
)
{
(void)pSigNum;
LE_INFO("Remove LED resource");
dhubIO_DeleteResource(GenericLedPath);
}
//--------------------------------------------------------------------------------------------------
/**
* Main program
*/
//--------------------------------------------------------------------------------------------------
COMPONENT_INIT
{
///< Catch application termination and shutdown cleanly
le_sig_Block(SIGTERM);
le_sig_SetEventHandler(SIGTERM, actuator_SigTermHandler);
LE_ASSERT(LE_OK == actuator_DhRegister());
}
/**
*
* This file provides the implementation of @ref c_led
*
*
* <hr>
*
* Copyright (C) Sierra Wireless Inc.
*/
#include "legato.h"
#include "interfaces.h"
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(array) ((unsigned long) (sizeof (array) / sizeof ((array)[0])))
#endif
#define RES_PATH_TRI_BLUE_LED "tri_led_blue"
#define RES_PATH_BLUE_VALUE "tri_led_blue/value"
static const char TriBlueLedPath[] = "/sys/devices/platform/expander.0/tri_led_blu";
typedef struct
{
const char *path;
dhubIO_DataType_t dataType;
const char *units;
dhubIO_BooleanPushHandlerRef_t ref;
}
actuator_DhOut_t;
static actuator_DhOut_t dhubResources[] = {
{RES_PATH_TRI_BLUE_LED, DHUBIO_DATA_TYPE_BOOLEAN, "", NULL },
};
static void actuator_DhOutputHandler
(
double timestamp,
bool value,
void *context
)
{
const char *writeData = value ? "1" : "0";
FILE *ledFile = fopen(TriBlueLedPath, "r+");
if (ledFile == NULL)
{
LE_ERROR("Open LED device file('%s') failed(%d)", TriBlueLedPath, errno);
return;
}
LE_DEBUG("Turn %s LED", value ? "on" : "off");
if (fwrite(writeData, sizeof(writeData), 1, ledFile) != 1)
{
LE_ERROR("Write LED device file('%s') failed", TriBlueLedPath);
}
dhubIO_PushBoolean(RES_PATH_BLUE_VALUE, DHUBIO_NOW, value);
dhubIO_MarkOptional(RES_PATH_TRI_BLUE_LED);
dhubIO_SetBooleanDefault(RES_PATH_TRI_BLUE_LED,false);
fclose(ledFile);
}
static le_result_t actuator_DhRegister
(
void
)
{
le_result_t result = LE_OK;
for (int i = 0; i < ARRAY_SIZE(dhubResources); i++)
{
result = dhubIO_CreateOutput(dhubResources[i].path,
dhubResources[i].dataType,
dhubResources[i].units);
if (LE_OK != result)
{
LE_ERROR("Failed to create output resource %s", dhubResources[i].path);
break;
}
/* LE_ASSERT(LE_OK == dhubIO_CreateInput(dhubResources[i].value, DHUBIO_DATA_TYPE_BOOLEAN, ""));
dhubResources[i].ref = dhubIO_AddBooleanPushHandler(dhubResources[i].path,
actuator_DhOutputHandler,
(void *) &dhubResources[i].gpio_path);
*/
LE_ASSERT(LE_OK == dhubIO_CreateInput(RES_PATH_BLUE_VALUE, DHUBIO_DATA_TYPE_BOOLEAN, ""));
dhubResources[i].ref = dhubIO_AddBooleanPushHandler(dhubResources[i].path, actuator_DhOutputHandler,
NULL );
if (NULL == dhubResources[i].ref)
{
LE_ERROR("Failed to add handler for output resource %s", dhubResources[i].path);
result = LE_FAULT;
break;
}
}
return result;
}
//--------------------------------------------------------------------------------------------------
/**
* SIGTERM handler to cleanly shutdown
*/
//--------------------------------------------------------------------------------------------------
static void actuator_SigTermHandler
(
int pSigNum
)
{
(void)pSigNum;
LE_INFO("Remove LED resource");
dhubIO_DeleteResource(TriBlueLedPath);
}
//--------------------------------------------------------------------------------------------------
/**
* Main program
*/
//--------------------------------------------------------------------------------------------------
COMPONENT_INIT
{
///< Catch application termination and shutdown cleanly
le_sig_Block(SIGTERM);
le_sig_SetEventHandler(SIGTERM, actuator_SigTermHandler);
LE_ASSERT(LE_OK == actuator_DhRegister());
}
//--------------------------------------------------------------------------------------------------
/**
* Component definition file for the mangOH Yellow on board actuator component.
*/
//--------------------------------------------------------------------------------------------------
cflags:
{
-std=c99
-DMANGOH_BOARD=YELLOW
}
requires:
{
api:
{
dhubIO = io.api
}
file:
{
/sys/devices/platform/expander.0/tri_led_grn /sys/devices/platform/expander.0/tri_led_grn
}
}
sources:
{
tri_led_green.c
}
/**
*
* This file provides the implementation of @ref c_led
*
*
* <hr>
*
* Copyright (C) Sierra Wireless Inc.
*/
#include "legato.h"
#include "interfaces.h"
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(array) ((unsigned long) (sizeof (array) / sizeof ((array)[0])))
#endif
#define RES_PATH_TRI_GREEN_LED "tri_led_green"
#define RES_PATH_GREEN_VALUE "tri_led_green/value"
static const char TriGreenLedPath[] = "/sys/devices/platform/expander.0/tri_led_grn";
typedef struct
{
const char *path;
dhubIO_DataType_t dataType;
const char *units;
dhubIO_BooleanPushHandlerRef_t ref;
}
actuator_DhOut_t;
static actuator_DhOut_t dhubResources[] = {
{RES_PATH_TRI_GREEN_LED, DHUBIO_DATA_TYPE_BOOLEAN, "", NULL },
};
static void actuator_DhOutputHandler
(
double timestamp,
bool value,
void *context
)
{
const char *writeData = value ? "1" : "0";
FILE *ledFile = fopen(TriGreenLedPath, "r+");
if (ledFile == NULL)
{
LE_ERROR("Open LED device file('%s') failed(%d)", TriGreenLedPath, errno);
return;
}
LE_DEBUG("Turn %s LED", value ? "on" : "off");
if (fwrite(writeData, sizeof(writeData), 1, ledFile) != 1)
{
LE_ERROR("Write LED device file('%s') failed", TriGreenLedPath);
}
dhubIO_PushBoolean(RES_PATH_GREEN_VALUE, DHUBIO_NOW, value);
dhubIO_MarkOptional(RES_PATH_TRI_GREEN_LED);
dhubIO_SetBooleanDefault(RES_PATH_TRI_GREEN_LED,false);
fclose(ledFile);
}
static le_result_t actuator_DhRegister
(
void
)
{
le_result_t result = LE_OK;
for (int i = 0; i < ARRAY_SIZE(dhubResources); i++)
{
result = dhubIO_CreateOutput(dhubResources[i].path,
dhubResources[i].dataType,
dhubResources[i].units);
if (LE_OK != result)
{
LE_ERROR("Failed to create output resource %s", dhubResources[i].path);
break;
}
/* LE_ASSERT(LE_OK == dhubIO_CreateInput(dhubResources[i].value, DHUBIO_DATA_TYPE_BOOLEAN, ""));
dhubResources[i].ref = dhubIO_AddBooleanPushHandler(dhubResources[i].path,
actuator_DhOutputHandler,
(void *) &dhubResources[i].gpio_path);
*/
LE_ASSERT(LE_OK == dhubIO_CreateInput(RES_PATH_GREEN_VALUE, DHUBIO_DATA_TYPE_BOOLEAN, ""));
dhubResources[i].ref = dhubIO_AddBooleanPushHandler(dhubResources[i].path, actuator_DhOutputHandler,
NULL );
if (NULL == dhubResources[i].ref)
{
LE_ERROR("Failed to add handler for output resource %s", dhubResources[i].path);
result = LE_FAULT;
break;
}
}
return result;
}
//--------------------------------------------------------------------------------------------------
/**
* SIGTERM handler to cleanly shutdown
*/
//--------------------------------------------------------------------------------------------------
static void actuator_SigTermHandler
(
int pSigNum
)
{
(void)pSigNum;
LE_INFO("Remove LED resource");
dhubIO_DeleteResource(TriGreenLedPath);
}
//--------------------------------------------------------------------------------------------------
/**
* Main program
*/
//--------------------------------------------------------------------------------------------------
COMPONENT_INIT
{
///< Catch application termination and shutdown cleanly
le_sig_Block(SIGTERM);
le_sig_SetEventHandler(SIGTERM, actuator_SigTermHandler);
LE_ASSERT(LE_OK == actuator_DhRegister());
}
//--------------------------------------------------------------------------------------------------
/**
* Component definition file for the mangOH Yellow on board actuator component.
*/
//--------------------------------------------------------------------------------------------------
cflags:
{
-std=c99
-DMANGOH_BOARD=YELLOW
}
requires:
{
api:
{
dhubIO = io.api
}
file:
{
/sys/devices/platform/expander.0/tri_led_red /sys/devices/platform/expander.0/tri_led_red
}
}
sources:
{
tri_led_red.c
}
/**
*
* This file provides the implementation of @ref c_led
*
*
* <hr>
*
* Copyright (C) Sierra Wireless Inc.
*/
#include "legato.h"
#include "interfaces.h"
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(array) ((unsigned long) (sizeof (array) / sizeof ((array)[0])))
#endif
#define RES_PATH_TRI_RED_LED "tri_led_red"
#define RES_PATH_RED_VALUE "tri_led_red/value"
static const char TriRedLedPath[] = "/sys/devices/platform/expander.0/tri_led_red";
typedef struct
{
const char *path;
dhubIO_DataType_t dataType;
const char *units;
dhubIO_BooleanPushHandlerRef_t ref;
}
actuator_DhOut_t;
static actuator_DhOut_t dhubResources[] = {
{RES_PATH_TRI_RED_LED, DHUBIO_DATA_TYPE_BOOLEAN, "", NULL },
};
static void actuator_DhOutputHandler
(
double timestamp,
bool value,
void *context
)
{
const char *writeData = value ? "1" : "0";
FILE *ledFile = fopen(TriRedLedPath, "r+");
if (ledFile == NULL)
{
LE_ERROR("Open LED device file('%s') failed(%d)", TriRedLedPath, errno);
return;
}
LE_DEBUG("Turn %s LED", value ? "on" : "off");
if (fwrite(writeData, sizeof(writeData), 1, ledFile) != 1)
{
LE_ERROR("Write LED device file('%s') failed", TriRedLedPath);
}
dhubIO_PushBoolean(RES_PATH_RED_VALUE, DHUBIO_NOW, value);
dhubIO_MarkOptional(RES_PATH_TRI_RED_LED);
dhubIO_SetBooleanDefault(RES_PATH_TRI_RED_LED,true);
fclose(ledFile);
}
static le_result_t actuator_DhRegister
(
void
)
{
le_result_t result = LE_OK;
for (int i = 0; i < ARRAY_SIZE(dhubResources); i++)
{
result = dhubIO_CreateOutput(dhubResources[i].path,
dhubResources[i].dataType,
dhubResources[i].units);
if (LE_OK != result)
{
LE_ERROR("Failed to create output resource %s", dhubResources[i].path);
break;
}
/* LE_ASSERT(LE_OK == dhubIO_CreateInput(dhubResources[i].value, DHUBIO_DATA_TYPE_BOOLEAN, ""));
dhubResources[i].ref = dhubIO_AddBooleanPushHandler(dhubResources[i].path,
actuator_DhOutputHandler,
(void *) &dhubResources[i].gpio_path);
*/
LE_ASSERT(LE_OK == dhubIO_CreateInput(RES_PATH_RED_VALUE, DHUBIO_DATA_TYPE_BOOLEAN, ""));
dhubResources[i].ref = dhubIO_AddBooleanPushHandler(dhubResources[i].path, actuator_DhOutputHandler,
NULL );
if (NULL == dhubResources[i].ref)
{
LE_ERROR("Failed to add handler for output resource %s", dhubResources[i].path);
result = LE_FAULT;
break;
}
}
return result;
}
//--------------------------------------------------------------------------------------------------
/**
* SIGTERM handler to cleanly shutdown
*/
//--------------------------------------------------------------------------------------------------
static void actuator_SigTermHandler
(
int pSigNum
)
{
(void)pSigNum;
LE_INFO("Remove LED resource");
dhubIO_DeleteResource(TriRedLedPath);
}
//--------------------------------------------------------------------------------------------------
/**
* Main program
*/
//--------------------------------------------------------------------------------------------------
COMPONENT_INIT
{
///< Catch application termination and shutdown cleanly
le_sig_Block(SIGTERM);
le_sig_SetEventHandler(SIGTERM, actuator_SigTermHandler);
LE_ASSERT(LE_OK == actuator_DhRegister());
}
//--------------------------------------------------------------------------------------------------
/**
* Component definition file for the mangOH Yellow on board actuator component.
*/
//--------------------------------------------------------------------------------------------------
cflags:
{
-std=c99
-DMANGOH_BOARD=YELLOW
-std=c99
}
requires:
{
api:
{
dhubIO = io.api
dhub = io.api
}
}
file:
{
/sys/devices/platform/expander.0/generic_led /sys/devices/platform/expander.0/generic_led
}
provides:
{
headerDir:
{
${CURDIR}
}
}
sources:
{
generic_led.c
outputActuator.c
}
#include "legato.h"
#include "interfaces.h"
#include "outputActuator.h"
#define DHUB_PATH_LENGTH 256
#define SYSFS_PATH_LENGTH 256
struct OutputActuator
{
char dhubOutputPath[DHUB_PATH_LENGTH];
char sysfsPath[SYSFS_PATH_LENGTH];
dhub_BooleanPushHandlerRef_t pushHandlerRef;
};
static le_result_t WriteStringToFile(const char *path, const char *s)
{
le_result_t res = LE_OK;
const size_t sLen = strlen(s);
FILE *f = fopen(path, "r+");
if (!f)
{
LE_ERROR("Couldn't open %s: %s", path, strerror(errno));
res = LE_FAULT;
goto done;
}
if (fwrite(s, 1, sLen, f) != sLen)
{
LE_ERROR("Write to %s failed", path);
res = LE_IO_ERROR;
goto cleanup;
}
cleanup:
fclose(f);
done:
return res;
}
static void ActuatorPushHandler
(
double timestamp,
bool value,
void *context
)
{
struct OutputActuator *oa = context;
le_result_t res = WriteStringToFile(oa->sysfsPath, value ? "1" : "0");
if (res != LE_OK)
{
return;
}
}
le_result_t RegisterOutputActuator(const char *dhubBasePath, const char *sysfsPath)
{
/*
* TODO: The current design assumes that once an actuator has been registered, it can't be
* deregistered so I make no attempt to hang onto the pointer so that it can be cleaned up
* later.
*/
struct OutputActuator *oa = calloc(sizeof(*oa), 1);
LE_ASSERT(oa);
le_result_t res = LE_OK;
strncpy(oa->sysfsPath, sysfsPath, SYSFS_PATH_LENGTH);
LE_FATAL_IF(oa->sysfsPath[SYSFS_PATH_LENGTH - 1] != '\0', "path too long");
int snprintfRes = snprintf(oa->dhubOutputPath, sizeof(oa->dhubOutputPath), "%s/%s", dhubBasePath, "value");
if (snprintfRes < 0)
{
LE_ERROR("Failed to format output path");
res = LE_FAULT;
goto error_output;
}
else if (snprintfRes >= sizeof(oa->dhubOutputPath))
{
res = LE_OVERFLOW;
goto error_output;
}
const char *noUnits = "";
res = dhub_CreateOutput(oa->dhubOutputPath, DHUB_DATA_TYPE_BOOLEAN, noUnits);
if (res != LE_OK)
{
LE_ERROR("Failed to create DataHub output %s", oa->dhubOutputPath);
goto error_output;
}
dhub_SetBooleanDefault(oa->dhubOutputPath, false);
oa->pushHandlerRef = dhub_AddBooleanPushHandler(oa->dhubOutputPath, ActuatorPushHandler, oa);
return res;
error_output:
free(oa);
return res;
}
COMPONENT_INIT
{
}
#ifndef _OUTPUT_ACTUATOR_H_
#define _OUTPUT_ACTUATOR_H_
LE_SHARED le_result_t RegisterOutputActuator(const char *dhubBasePath, const char *sysfsPath);
#endif // _OUTPUT_ACTUATOR_H_
//--------------------------------------------------------------------------------------------------
/**
* Component definition file for the mangOH Yellow on board actuator component.
*/
//--------------------------------------------------------------------------------------------------
cflags:
{
-std=c99
-DMANGOH_BOARD=YELLOW
-std=c99
}
requires:
{
api:
component:
{
dhubIO = io.api
../outputActuator
}
file:
{
/sys/devices/platform/expander.0/tri_led_blu /sys/devices/platform/expander.0/tri_led_blu
}
{
/sys/devices/platform/expander.0/generic_led /sys/devices/platform/expander.0/
/sys/devices/platform/expander.0/tri_led_red /sys/devices/platform/expander.0/
/sys/devices/platform/expander.0/tri_led_grn /sys/devices/platform/expander.0/
/sys/devices/platform/expander.0/tri_led_blu /sys/devices/platform/expander.0/
}
}
sources:
{
tri_led_blu.c
yellowActuators.c
}
#include "legato.h"
#include "interfaces.h"
#include "outputActuator.h"
static const struct
{
const char *sysfsPath;
const char *dhubPath;
} OutputActuators[] =
{
{
.sysfsPath="/sys/devices/platform/expander.0/tri_led_red",
.dhubPath="tri_led_red",
},
{
.sysfsPath="/sys/devices/platform/expander.0/tri_led_grn",
.dhubPath="tri_led_green",
},
{
.sysfsPath="/sys/devices/platform/expander.0/tri_led_blu",
.dhubPath="tri_led_blue",
},
{
.sysfsPath="/sys/devices/platform/expander.0/generic_led",
.dhubPath="generic_led",
},
};
COMPONENT_INIT
{
for (int i = 0; i < NUM_ARRAY_MEMBERS(OutputActuators); i++)
{
le_result_t res = RegisterOutputActuator(
OutputActuators[i].dhubPath, OutputActuators[i].sysfsPath);
LE_FATAL_IF(
res != LE_OK,
"Output actuator registration failed for %s at %s",
OutputActuators[i].sysfsPath,
OutputActuators[i].dhubPath);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment