diff options
author | Nick Van Doorn <vandoorn.nick@gmail.com> | 2017-12-05 00:37:32 -0800 |
---|---|---|
committer | Nick Van Doorn <vandoorn.nick@gmail.com> | 2017-12-05 00:37:32 -0800 |
commit | 766ee385865ce6a14b9795064896a59a49f552d8 (patch) | |
tree | 01fc47a4fa5e102f4f7270a5c97185649df91210 | |
parent | e49a483788f4293a1223518537531e4fc31913f4 (diff) |
Fix I/O callbacks & isActive function
I/O callbacks previously required an un-used formatter argument.
isActive function previously returned before writing the value
-rw-r--r-- | util.c | 21 | ||||
-rw-r--r-- | util.h | 6 |
2 files changed, 14 insertions, 13 deletions
@@ -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 +} @@ -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); |