From dbc3b8fb0cd052308cfa478e009048d3fe008fb8 Mon Sep 17 00:00:00 2001
From: Jeff Gough <j@jeffreygough.com>
Date: Thu, 22 Aug 2019 22:50:31 +0200
Subject: [PATCH] Add timer to appDb persistant storage to group write
 operations together

---
 epicardium/ble/app/common/app_db.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/epicardium/ble/app/common/app_db.c b/epicardium/ble/app/common/app_db.c
index 285570f7..46c30755 100644
--- a/epicardium/ble/app/common/app_db.c
+++ b/epicardium/ble/app/common/app_db.c
@@ -33,6 +33,11 @@
 #include "app_cfg.h"
 
 #include "epicardium.h"
+#include "modules/log.h"
+#include "FreeRTOS.h"
+#include "timers.h"
+#include "mxc_sys.h"
+#include "wdt.h"
 #include <string.h>
 #include <stdio.h>
 
@@ -90,6 +95,12 @@ static appDb_t appDb;
 /*! When all records are allocated use this index to determine which to overwrite */
 static appDbRec_t *pAppDbNewRec = appDb.rec;
 
+/* Timer to delay writing to persistent storage until a burst of store() calls has finished */
+static TimerHandle_t store_timer;
+static StaticTimer_t store_timer_buffer;
+static void store_callback();
+#define STORE_DELAY pdMS_TO_TICKS(1000)
+
 /*************************************************************************************************/
 /*!
  *  \brief  Initialize the device database.
@@ -107,13 +118,32 @@ void AppDbInit(void)
     }
     epic_file_close(fd);
   }
+
+	store_timer = xTimerCreateStatic(
+                                     "appdb_store_timer",
+                                     STORE_DELAY,
+                                     pdFALSE,
+                                     NULL,
+                                     store_callback,
+                                     &store_timer_buffer
+                                     );
 }
 
 static void store(void)
+{
+  LOG_DEBUG("appDb", "store() called, resetting timer");
+  if (xTimerReset(store_timer, 10) != pdPASS) {     /* (Re)start the timer */
+    /* Store timer could not be reset, write to persistent storage anyway */
+    store_callback();
+  }
+}
+
+static void store_callback()
 {
   int fd = epic_file_open("pairings.bin", "w");
   if(fd >= 0) {
     if(epic_file_write(fd, &appDb, sizeof(appDb)) != sizeof(appDb)) {
+      LOG_DEBUG("appDb", "STORE_DELAY passed, writing to persistent storage");
     }
     epic_file_close(fd);
   }
-- 
GitLab