From 766ee385865ce6a14b9795064896a59a49f552d8 Mon Sep 17 00:00:00 2001 From: Nick Van Doorn Date: Tue, 5 Dec 2017 00:37:32 -0800 Subject: Fix I/O callbacks & isActive function I/O callbacks previously required an un-used formatter argument. isActive function previously returned before writing the value --- util.c | 21 +++++++++++---------- util.h | 6 +++--- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/util.c b/util.c index 8bb9ba1..3b4b404 100644 --- a/util.c +++ b/util.c @@ -2,29 +2,28 @@ #include "util.h" le_result_t readFromFile (const char *path, void *value, - int (*scanCallback) (FILE *f, const char *formatter, void *val)) { + int (*scanCallback) (FILE *f, void *val)) { FILE *f = fopen(path, "r"); if (f == NULL) { LE_WARN("Couldn't open '%s' - %m", path); return LE_IO_ERROR; } - int numScanned = scanCallback(f, "%lf", value); + int numScanned = scanCallback(f, value); if (numScanned != 1) return LE_FORMAT_ERROR; fclose(f); return LE_OK; } -int scanIntCallback (FILE *f, const char *formatter, void *value) { +int scanIntCallback (FILE *f, void *value) { int *val = value; return fscanf(f, "%d", val); } -int scanDoubleCallback (FILE *f, const char *formatter, void *value) { +int scanDoubleCallback (FILE *f, void *value) { double *val = value; return fscanf(f, "%lf", val); } -// TODO camel case these methods le_result_t ioutil_readIntFromFile (const char *path, int *value) { return readFromFile(path, value, scanIntCallback); } @@ -40,12 +39,13 @@ le_result_t ioutil_writeToFile (const char *path, void *value, size_t size, size LE_WARN("Failed to open %s for writing", path); return LE_IO_ERROR; } - size_t nWritten = fwrite(value, size, count, f); + ssize_t nWritten = fwrite(value, size, count, f); LE_INFO("Wrote %d bytes", nWritten); fclose(f); return LE_OK; } +// TODO verify this is working le_result_t gpio_exportPin (int pin) { return ioutil_writeToFile ("/sys/class/gpio/export", &pin, sizeof(int), 1); } @@ -91,9 +91,10 @@ le_result_t gpio_isActive (int pin, bool *isActive) { char path[50]; getGpioPath(path, pin, "value"); int val; - return ioutil_readIntFromFile(path, &val); - LE_FATAL_IF(val != 0 || val != 1, "value is not boolean"); - *isActive = (bool)val; + le_result_t readRes = ioutil_readIntFromFile(path, &val); + LE_INFO("Raw value (from %s): %d", path, val); + *isActive = val; + return readRes; } le_result_t gpio_setValue (int pin, bool state) { @@ -123,4 +124,4 @@ long getTimeMicrosecs () { struct timeval currentTime; gettimeofday(¤tTime, NULL); return currentTime.tv_sec * (int)1e6 + currentTime.tv_usec; -} \ No newline at end of file +} diff --git a/util.h b/util.h index 6881eda..175543b 100644 --- a/util.h +++ b/util.h @@ -8,9 +8,9 @@ // ioutil le_result_t readFromFile (const char *path, void *value, - int (*scanCallback) (FILE *f, const char *formatter, void *val)); -int scanIntCallback (FILE *f, const char *formatter, void *value); -int scanDoubleCallback (FILE *f, const char *formatter, void *value); + int (*scanCallback) (FILE *f, void *val)); +int scanIntCallback (FILE *f, void *value); +int scanDoubleCallback (FILE *f, void *value); le_result_t ioutil_readIntFromFile (const char *path, int *value); le_result_t ioutil_readDoubleFromFile (const char *filePath, double *value); le_result_t ioutil_writeToFile (const char *path, void *value, size_t size, size_t count); -- cgit v1.2.3 From 8ffa245080e38509783169e7f7446ebcbecea9d7 Mon Sep 17 00:00:00 2001 From: Nick Van Doorn Date: Tue, 5 Dec 2017 00:47:09 -0800 Subject: Add intial readme --- README.md | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..2326cbe --- /dev/null +++ b/README.md @@ -0,0 +1,50 @@ +# legato-util + +Small collection of helper functions required by some of our open source code + +## API + +### File I/O + +Functions to handle boilerplate when reading and writing from files. + +```C +le_result_t readFromFile (const char *path, void *value, + int (*scanCallback) (FILE *f, void *val)); +int scanIntCallback (FILE *f, void *value); +int scanDoubleCallback (FILE *f, void *value); +le_result_t ioutil_readIntFromFile (const char *path, int *value); +le_result_t ioutil_readDoubleFromFile (const char *filePath, double *value); +le_result_t ioutil_writeToFile (const char *path, void *value, size_t size, size_t count); + +``` + +### GPIO + +Functions to manipulate sysfs GPIO. + +```C +void getGpioPath (char *outputStr, int pin, char *subDir); +le_result_t gpio_exportPin (int pin); +le_result_t gpio_unexportPin (int pin); +le_result_t gpio_setDirection (int pin, char *direction); +le_result_t gpio_setInput (int pin); +le_result_t gpio_setOutput (int pin); +le_result_t gpio_setActiveState (int pin, bool isActiveLow); +le_result_t gpio_isActive (int pin, bool *isActive); +le_result_t gpio_setValue (int pin, bool state); +le_result_t gpio_setLow (int pin); +le_result_t gpio_setHigh (int pin); + +``` + +### Timing + +Functions to help with sample timing. Could potentially be replaced with system calls + +```C +// TODO give these a quasi "namespace" +void delayMicro (unsigned long microsecs); +void delayMilli (unsigned long millisecs); +long getTimeMicrosecs (); +``` -- cgit v1.2.3 From 61b60adb4d4ac5b3df7b5eb561ab648b2862805b Mon Sep 17 00:00:00 2001 From: Nick Van Doorn Date: Tue, 5 Dec 2017 00:47:19 -0800 Subject: Add todo --- util.h | 1 + 1 file changed, 1 insertion(+) diff --git a/util.h b/util.h index 175543b..668e37d 100644 --- a/util.h +++ b/util.h @@ -29,6 +29,7 @@ le_result_t gpio_setLow (int pin); le_result_t gpio_setHigh (int pin); // time util +// TODO give these a quasi "namespace" void delayMicro (unsigned long microsecs); void delayMilli (unsigned long millisecs); long getTimeMicrosecs (); -- cgit v1.2.3 From 18b311979ae71499074ab9da204fb9275047339a Mon Sep 17 00:00:00 2001 From: Nick Van Doorn Date: Tue, 5 Dec 2017 03:27:04 -0800 Subject: Fix spacing --- util.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/util.c b/util.c index 3b4b404..8d1830f 100644 --- a/util.c +++ b/util.c @@ -47,11 +47,11 @@ le_result_t ioutil_writeToFile (const char *path, void *value, size_t size, size // TODO verify this is working le_result_t gpio_exportPin (int pin) { - return ioutil_writeToFile ("/sys/class/gpio/export", &pin, sizeof(int), 1); + return ioutil_writeToFile("/sys/class/gpio/export", &pin, sizeof(int), 1); } le_result_t gpio_unexportPin (int pin) { - return ioutil_writeToFile ("/sys/class/gpio/unexport", &pin, sizeof(int), 1); + return ioutil_writeToFile("/sys/class/gpio/unexport", &pin, sizeof(int), 1); } void getGpioPath (char *outputStr, int pin, char *subDir) { -- cgit v1.2.3 From 49d6a7a827baa6ff7250371021693c5b6e7b987b Mon Sep 17 00:00:00 2001 From: Nick Van Doorn Date: Mon, 18 Dec 2017 09:16:40 -0800 Subject: Implement helper to flatten res array --- util.c | 9 +++++++++ util.h | 2 ++ 2 files changed, 11 insertions(+) diff --git a/util.c b/util.c index 8d1830f..66e717c 100644 --- a/util.c +++ b/util.c @@ -111,6 +111,15 @@ le_result_t gpio_setHigh (int pin) { return gpio_setValue(pin, HIGH); } +le_result_t util_flattenRes (le_result_t *res, int nRes) { + for (int i = 0; i < nRes; i++) { + le_result_t *resPtr = res + i; + if (*resPtr != LE_OK) return *resPtr; + } + return LE_OK; +} + +// Functions below are deprecated void delayMicro (unsigned long microsecs) { unsigned long retTime = getTimeMicrosecs() + microsecs; while (getTimeMicrosecs() < retTime); diff --git a/util.h b/util.h index 668e37d..93d0e12 100644 --- a/util.h +++ b/util.h @@ -34,4 +34,6 @@ void delayMicro (unsigned long microsecs); void delayMilli (unsigned long millisecs); long getTimeMicrosecs (); +le_result_t util_flattenRes (le_result_t *res, int nRes); + #endif -- cgit v1.2.3 From 9ae96091978fe5f2fcd64a9b7103ba61b9ed6f18 Mon Sep 17 00:00:00 2001 From: Nick Van Doorn Date: Fri, 26 Jan 2018 13:47:53 -0800 Subject: Add some time helpers --- util.c | 19 +++++++++++++++++++ util.h | 2 ++ 2 files changed, 21 insertions(+) diff --git a/util.c b/util.c index 66e717c..6d79f5d 100644 --- a/util.c +++ b/util.c @@ -119,6 +119,25 @@ le_result_t util_flattenRes (le_result_t *res, int nRes) { return LE_OK; } +int util_getUnixDatetime () { + struct timeval tv; + gettimeofday(&tv, NULL); + return tv.tv_sec; +} + +/** + * Convenience function to get current time as uint64_t. + * + * @return + * Current time as a uint64_t + */ +uint64_t GetCurrentTimestamp(void) { + struct timeval tv; + gettimeofday(&tv, NULL); + uint64_t utcMilliSec = (uint64_t)(tv.tv_sec) * 1000 + (uint64_t)(tv.tv_usec) / 1000; + return utcMilliSec; +} + // Functions below are deprecated void delayMicro (unsigned long microsecs) { unsigned long retTime = getTimeMicrosecs() + microsecs; diff --git a/util.h b/util.h index 93d0e12..a706588 100644 --- a/util.h +++ b/util.h @@ -33,7 +33,9 @@ le_result_t gpio_setHigh (int pin); void delayMicro (unsigned long microsecs); void delayMilli (unsigned long millisecs); long getTimeMicrosecs (); +uint64_t GetCurrentTimestamp (void); +int util_getUnixDatetime (); le_result_t util_flattenRes (le_result_t *res, int nRes); #endif -- cgit v1.2.3 From 1e37dad1fc37f7626fa0c83441940bab673ef4c5 Mon Sep 17 00:00:00 2001 From: Nick Van Doorn Date: Tue, 30 Jan 2018 09:27:08 -0800 Subject: Add helper to get mtime of file --- util.c | 10 ++++++++++ util.h | 1 + 2 files changed, 11 insertions(+) diff --git a/util.c b/util.c index 6d79f5d..d1858e0 100644 --- a/util.c +++ b/util.c @@ -125,6 +125,16 @@ int util_getUnixDatetime () { return tv.tv_sec; } +/** + * Get the last modified datetime + * of the file at path + */ +time_t util_getMTime (char *path) { + struct stat st; + if (stat(path, &st) != 0) return -1; + else return st.st_mtime; +} + /** * Convenience function to get current time as uint64_t. * diff --git a/util.h b/util.h index a706588..12cf5eb 100644 --- a/util.h +++ b/util.h @@ -35,6 +35,7 @@ void delayMilli (unsigned long millisecs); long getTimeMicrosecs (); uint64_t GetCurrentTimestamp (void); +time_t util_getMTime (char *path); int util_getUnixDatetime (); le_result_t util_flattenRes (le_result_t *res, int nRes); -- cgit v1.2.3 From cdf4df771d223c27ea9b8b08a449c23d020dde6b Mon Sep 17 00:00:00 2001 From: Nick Van Doorn Date: Tue, 6 Mar 2018 10:41:44 -0800 Subject: Cleanout un-used time and GPIO helpers --- README.md | 33 +++++------------------ util.c | 90 +-------------------------------------------------------------- util.h | 19 -------------- 3 files changed, 8 insertions(+), 134 deletions(-) diff --git a/README.md b/README.md index 2326cbe..bdb186e 100644 --- a/README.md +++ b/README.md @@ -16,35 +16,16 @@ int scanDoubleCallback (FILE *f, void *value); le_result_t ioutil_readIntFromFile (const char *path, int *value); le_result_t ioutil_readDoubleFromFile (const char *filePath, double *value); le_result_t ioutil_writeToFile (const char *path, void *value, size_t size, size_t count); - ``` -### GPIO - -Functions to manipulate sysfs GPIO. - +### Time ```C -void getGpioPath (char *outputStr, int pin, char *subDir); -le_result_t gpio_exportPin (int pin); -le_result_t gpio_unexportPin (int pin); -le_result_t gpio_setDirection (int pin, char *direction); -le_result_t gpio_setInput (int pin); -le_result_t gpio_setOutput (int pin); -le_result_t gpio_setActiveState (int pin, bool isActiveLow); -le_result_t gpio_isActive (int pin, bool *isActive); -le_result_t gpio_setValue (int pin, bool state); -le_result_t gpio_setLow (int pin); -le_result_t gpio_setHigh (int pin); - +int util_getUnixDatetime (); +time_t util_getMTime (char *path); +uint64_t GetCurrentTimestamp (void); ``` -### Timing - -Functions to help with sample timing. Could potentially be replaced with system calls - +### Other ```C -// TODO give these a quasi "namespace" -void delayMicro (unsigned long microsecs); -void delayMilli (unsigned long millisecs); -long getTimeMicrosecs (); -``` +le_result_t util_flattenRes (le_result_t *res, int nRes); +``` \ No newline at end of file diff --git a/util.c b/util.c index d1858e0..1c30ca8 100644 --- a/util.c +++ b/util.c @@ -45,72 +45,6 @@ le_result_t ioutil_writeToFile (const char *path, void *value, size_t size, size return LE_OK; } -// TODO verify this is working -le_result_t gpio_exportPin (int pin) { - return ioutil_writeToFile("/sys/class/gpio/export", &pin, sizeof(int), 1); -} - -le_result_t gpio_unexportPin (int pin) { - return ioutil_writeToFile("/sys/class/gpio/unexport", &pin, sizeof(int), 1); -} - -void getGpioPath (char *outputStr, int pin, char *subDir) { - sprintf(outputStr, "/sys/class/gpio/gpio%d/%s", pin, subDir); -} - -le_result_t gpio_setDirection (int pin, char *direction) { - char path[50]; - getGpioPath(path, pin, "direction"); - return ioutil_writeToFile(path, direction, sizeof(char), strlen(direction)); -} - -le_result_t gpio_setInput (int pin) { - return gpio_setDirection(pin, "in"); -} - -le_result_t gpio_setOutput (int pin) { - return gpio_setDirection(pin, "out"); -} - -le_result_t gpio_setActiveState (int pin, bool isActiveLow) { - // Any non zero value toggles the existing value - // so we must read the existing value first - char path[50]; - int isActiveLowSet; - getGpioPath(path, pin, "active_low"); - le_result_t readRes = ioutil_readIntFromFile(path, &isActiveLowSet); - le_result_t writeRes = LE_OK; - if (isActiveLow != isActiveLowSet) { - writeRes = ioutil_writeToFile(path, "1", sizeof(char), 1); - } - return readRes == LE_OK && writeRes == LE_OK ? - LE_OK : LE_FAULT; -} - -le_result_t gpio_isActive (int pin, bool *isActive) { - char path[50]; - getGpioPath(path, pin, "value"); - int val; - le_result_t readRes = ioutil_readIntFromFile(path, &val); - LE_INFO("Raw value (from %s): %d", path, val); - *isActive = val; - return readRes; -} - -le_result_t gpio_setValue (int pin, bool state) { - char path[50]; - getGpioPath(path, pin, "value"); - return ioutil_writeToFile(path, &state, sizeof(bool), 1); -} - -le_result_t gpio_setLow (int pin) { - return gpio_setValue(pin, LOW); -} - -le_result_t gpio_setHigh (int pin) { - return gpio_setValue(pin, HIGH); -} - le_result_t util_flattenRes (le_result_t *res, int nRes) { for (int i = 0; i < nRes; i++) { le_result_t *resPtr = res + i; @@ -135,31 +69,9 @@ time_t util_getMTime (char *path) { else return st.st_mtime; } -/** - * Convenience function to get current time as uint64_t. - * - * @return - * Current time as a uint64_t - */ uint64_t GetCurrentTimestamp(void) { struct timeval tv; gettimeofday(&tv, NULL); uint64_t utcMilliSec = (uint64_t)(tv.tv_sec) * 1000 + (uint64_t)(tv.tv_usec) / 1000; return utcMilliSec; -} - -// Functions below are deprecated -void delayMicro (unsigned long microsecs) { - unsigned long retTime = getTimeMicrosecs() + microsecs; - while (getTimeMicrosecs() < retTime); -} - -void delayMilli (unsigned long millisecs) { - delayMicro(millisecs * 1000); -} - -long getTimeMicrosecs () { - struct timeval currentTime; - gettimeofday(¤tTime, NULL); - return currentTime.tv_sec * (int)1e6 + currentTime.tv_usec; -} +} \ No newline at end of file diff --git a/util.h b/util.h index 12cf5eb..7e90d36 100644 --- a/util.h +++ b/util.h @@ -15,26 +15,7 @@ le_result_t ioutil_readIntFromFile (const char *path, int *value); le_result_t ioutil_readDoubleFromFile (const char *filePath, double *value); le_result_t ioutil_writeToFile (const char *path, void *value, size_t size, size_t count); -// gpio -void getGpioPath (char *outputStr, int pin, char *subDir); -le_result_t gpio_exportPin (int pin); -le_result_t gpio_unexportPin (int pin); -le_result_t gpio_setDirection (int pin, char *direction); -le_result_t gpio_setInput (int pin); -le_result_t gpio_setOutput (int pin); -le_result_t gpio_setActiveState (int pin, bool isActiveLow); -le_result_t gpio_isActive (int pin, bool *isActive); -le_result_t gpio_setValue (int pin, bool state); -le_result_t gpio_setLow (int pin); -le_result_t gpio_setHigh (int pin); - -// time util -// TODO give these a quasi "namespace" -void delayMicro (unsigned long microsecs); -void delayMilli (unsigned long millisecs); -long getTimeMicrosecs (); uint64_t GetCurrentTimestamp (void); - time_t util_getMTime (char *path); int util_getUnixDatetime (); le_result_t util_flattenRes (le_result_t *res, int nRes); -- cgit v1.2.3