Skip to content

feat: implement periodic RFID cache synchronization with server - #48

Open
PBrunot wants to merge 3 commits into
mainfrom
feature/cache-sync
Open

PBrunot wants to merge 3 commits into
mainfrom
feature/cache-sync

Conversation

@PBrunot

@PBrunot PBrunot commented Jun 28, 2025

Copy link
Copy Markdown
Contributor

Summary

Implements automatic RFID cache synchronization between ESP32 boards and the backend server to ensure offline authentication remains current with server-side changes.

Key Features

  • Periodic sync: Cache synchronizes every 30 minutes when online
  • Server protocol: New synccache MQTT action for requesting authorized card list
  • Scalable: Supports up to 200 authorized cards (configurable)
  • Backwards compatible: Existing circular cache continues to work
  • Memory efficient: Only +112 bytes RAM, +2.4KB Flash usage

Technical Implementation

MQTT Protocol Extension

  • Request: {"action": "synccache"}
  • Response: {"request_ok": true, "cards": [{"uid": "1234ABCD", "level": 1}, ...]}

New Components

  1. SyncCacheQuery/SyncCacheResponse classes in MQTTtypes.hpp
  2. FabBackend::syncCache() method for server communication
  3. AuthProvider::syncCacheFromServer() for cache replacement
  4. Periodic task running every 30 minutes (configurable)

Configuration

// conf/conf.hpp
inline constexpr uint16_t SYNC_CACHE_MAX_SIZE{200};
inline constexpr auto SYNC_INTERVAL{30min};

Memory Analysis

  • Current RAM usage: 15.0% (49,292 / 327,680 bytes)
  • Current Flash usage: 74.3% (973,244 / 1,310,720 bytes)
  • Memory increase: +112 bytes RAM, +2.4KB Flash
  • Remaining capacity: 99.9% RAM, 25.7% Flash available

Behavior

  1. Every 30 minutes, taskSyncCache() executes
  2. If server is online, requests complete authorized card list
  3. Replaces local cache with server response (up to 200 cards)
  4. Falls back to existing circular cache during network outages
  5. Maintains offline authentication resilience

Backend Server Requirements

⚠️ Server implementation needed - see implementation instructions below.

Test Results

  • ✅ Builds successfully on hardware-rev0-en_US
  • ✅ Memory usage within acceptable limits
  • ✅ No breaking changes to existing functionality

Files Changed

  • conf/conf.hpp: Added sync configuration constants
  • include/MQTTtypes.hpp: Added SyncCacheQuery/Response classes
  • src/MQTTtypes.cpp: Implemented payload generation and JSON parsing
  • include/FabBackend.hpp: Added syncCache() method declaration
  • src/FabBackend.cpp: Implemented syncCache() method
  • include/AuthProvider.hpp: Added cache sync method declarations
  • src/AuthProvider.cpp: Implemented syncCacheFromServer() and clearCache()
  • include/BoardLogic.hpp: Added syncRfidCache() method
  • src/BoardLogic.cpp: Implemented syncRfidCache() method
  • src/main.cpp: Added periodic sync task

🤖 Generated with Claude Code

- Add SyncCacheQuery/SyncCacheResponse MQTT protocol for cache sync
- Implement FabBackend::syncCache() method for server communication
- Add AuthProvider::syncCacheFromServer() and clearCache() methods
- Create periodic task for automatic cache synchronization (30min interval)
- Support up to 200 authorized cards in synchronized cache
- Maintain backwards compatibility with existing circular cache
- Memory usage: +112 bytes RAM, +2.4KB Flash (well within limits)

The cache now synchronizes with the server every 30 minutes when online,
ensuring offline authentication remains current with server-side changes.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

Firmware size changes for esp32-devboard

Commit CURRENT e6d782e vs REFERENCE 1ed3348

<CURRENT> MAP file: ../firmware.map.latest
<REFERENCE> MAP file: ../firmware.map.previous
Difference is counted as <CURRENT> - <REFERENCE>,           i.e. a positive number means that <CURRENT> is larger.
Total sizes of <CURRENT>:                                       <REFERENCE>     Difference
Used static DRAM:   55104 bytes (  69476 remain, 44.2% used)          54984           +120 (   -120 remain,      +0 total)
      .data size:   21728 bytes                                       21728
      .bss  size:   33376 bytes                                       33256           +120
Used static IRAM:   72374 bytes (  99658 remain, 42.1% used)          72374                (     +0 remain,      +0 total)
      .text size:   71347 bytes                                       71347
   .vectors size:    1027 bytes                                        1027
Used Flash size :  870114 bytes                                      867298          +2816
           .text:  798506 bytes                                      796218          +2288
         .rodata:   71352 bytes                                       70824           +528
Total image size:  964216 bytes (.bin may be padded larger)          961400          +2816

@github-actions

Copy link
Copy Markdown

Firmware size changes for hardware-rev0-it_IT

Commit CURRENT e6d782e vs REFERENCE 1ed3348

<CURRENT> MAP file: ../firmware.map.latest
<REFERENCE> MAP file: ../firmware.map.previous
Difference is counted as <CURRENT> - <REFERENCE>,           i.e. a positive number means that <CURRENT> is larger.
Total sizes of <CURRENT>:                                       <REFERENCE>     Difference
Used static IRAM:   16383 bytes (      1 remain, 100.0% used)          16383                (     +0 remain,      +0 total)
      .text size:   15356 bytes                                       15356
   .vectors size:    1027 bytes                                        1027
Used stat D/IRAM:   88347 bytes ( 253413 remain, 25.9% used)          88235           +112 (   -112 remain,      +0 total)
      .data size:   17164 bytes                                       17164
      .bss  size:   32128 bytes                                       32016           +112
      .text size:   39055 bytes                                       39055
Used Flash size :  900870 bytes                                      898426          +2444
           .text:  824686 bytes                                      822802          +1884
         .rodata:   75928 bytes                                       75368           +560
Total image size:  973472 bytes (.bin may be padded larger)          971028          +2444

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant