From 543cbeebc9ec5fd5b7bbbbd9e76ae22562395c1c Mon Sep 17 00:00:00 2001 From: Andrew Lehmer Date: Wed, 24 May 2017 09:52:04 -0700 Subject: [PATCH] clover: folio_daemon: retry slowly on failure Bug: 38001818 Test: loaded on taimen Change-Id: I3f5a8cbf0faca3b5d027dcd74f1b16de80fdbee2 Signed-off-by: pix106 --- folio_daemon/main.cpp | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/folio_daemon/main.cpp b/folio_daemon/main.cpp index 12f350f..992b4a5 100644 --- a/folio_daemon/main.cpp +++ b/folio_daemon/main.cpp @@ -15,6 +15,7 @@ */ #include +#include #include #include #include @@ -25,8 +26,9 @@ // Hall-effect sensor type #define SENSOR_TYPE 33171016 -// Warn if the polling loop yields zero events at most once every five seconds. -#define WARN_PERIOD (time_t)5 +#define RETRY_LIMIT 120 +#define RETRY_PERIOD 30 // 30 seconds +#define WARN_PERIOD (time_t)300 // 5 minutes /* * This simple daemon listens for events from the Hall-effect sensor and writes @@ -42,6 +44,7 @@ int main(void) { ALooper *looper; ASensorEventQueue *eventQueue = nullptr; time_t lastWarn = 0; + int attemptCount = 0; ALOGI("Started"); @@ -82,10 +85,29 @@ int main(void) { // Get Hall-effect sensor events from the NDK sensorManager = ASensorManager_getInstanceForPackage(nullptr); - hallSensor = ASensorManager_getDefaultSensor(sensorManager, SENSOR_TYPE); - if (hallSensor == nullptr) { - ALOGE("Unable to get Hall-effect sensor"); - goto out; + /* + * As long as we are unable to get the sensor handle, periodically retry + * and emit an error message at a low frequency to prevent high CPU usage + * and log spam. If we simply exited with an error here, we would be + * immediately restarted and fail in the same way indefinitely. + */ + while (true) { + time_t now = time(NULL); + hallSensor = ASensorManager_getDefaultSensor(sensorManager, + SENSOR_TYPE); + if (hallSensor != nullptr) { + break; + } + + if (++attemptCount >= RETRY_LIMIT) { + ALOGE("Retries exhausted; exiting"); + goto out; + } else if (now > lastWarn + WARN_PERIOD) { + ALOGE("Unable to get Hall-effect sensor"); + lastWarn = now; + } + + sleep(RETRY_PERIOD); } looper = ALooper_forThread();