diff options
-rw-r--r-- | README.md | 31 | ||||
-rw-r--r-- | util.c | 103 | ||||
-rw-r--r-- | util.h | 27 |
3 files changed, 65 insertions, 96 deletions
diff --git a/README.md b/README.md new file mode 100644 index 0000000..bdb186e --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +# 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); +``` + +### Time +```C +int util_getUnixDatetime (); +time_t util_getMTime (char *path); +uint64_t GetCurrentTimestamp (void); +``` + +### Other +```C +le_result_t util_flattenRes (le_result_t *res, int nRes); +```
\ No newline at end of file @@ -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,87 +39,39 @@ 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; } -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); +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 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; - return ioutil_readIntFromFile(path, &val); - LE_FATAL_IF(val != 0 || val != 1, "value is not boolean"); - *isActive = (bool)val; -} - -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); + return LE_OK; } -void delayMicro (unsigned long microsecs) { - unsigned long retTime = getTimeMicrosecs() + microsecs; - while (getTimeMicrosecs() < retTime); +int util_getUnixDatetime () { + struct timeval tv; + gettimeofday(&tv, NULL); + return tv.tv_sec; } -void delayMilli (unsigned long millisecs) { - delayMicro(millisecs * 1000); +/** + * 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; } -long getTimeMicrosecs () { - struct timeval currentTime; - gettimeofday(¤tTime, NULL); - return currentTime.tv_sec * (int)1e6 + currentTime.tv_usec; +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; }
\ No newline at end of file @@ -8,29 +8,16 @@ // 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); -// 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 -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); #endif |