Firmware Code Book Blue - FujiNetWIFI/fujinet-firmware GitHub Wiki
FujiNet Firmware Diving
We will start at the beginning with
main.cpp
Here's a summary of its key components:
- Platform-specific conditional compilation:
- Differentiates between ESP platform (likely ESP32) and other platforms (Windows, Linux, macOS).
- Includes various headers for different functionalities:
- Debug, bus, device, WiFi, filesystem, HTTP service, Bluetooth (if supported), and more.
- Main setup function:
- Initializes hardware, loads configuration, sets up file systems.
- Creates and configures various devices based on the target platform (e.g., printers, modems).
- Sets up different bus systems (SIO, IEC, ComLynx, RS232, etc.) depending on the build target.
- Main service loop:
- Handles continuous operations like servicing the active bus system.
- Manages WiFi or Bluetooth connections.
- On non-ESP platforms, it also handles HTTP service and task management.
- Platform-specific main entry points:
- For ESP platform:
app_main()
function that creates a high-priority task for the main loop. - For other platforms: Standard
main()
function that calls setup and enters the service loop.
- For ESP platform:
- Shutdown and reboot handling:
- Includes functions to handle graceful shutdown and reboot scenarios.
The code is designed to be highly modular and adaptable to different hardware platforms and configurations, with extensive use of conditional compilation to include or exclude features based on the target system.
Main Loop
The service loop is the heart of the FujiNet system, continuously running to handle various tasks. Let's break down its key components and actions:
- WiFi/Bluetooth Initialization:
- If Bluetooth is enabled in the configuration, it starts the Bluetooth manager.
- If WiFi is enabled, it initiates the WiFi connection.
- Main Loop - The loop runs continuously, performing these tasks:
- Bluetooth Service (if active):
- If Bluetooth is active, it calls
fnBtManager.service()
to handle Bluetooth operations.
- If Bluetooth is active, it calls
- Bus Service:
- Calls
SYSTEM_BUS.service()
to handle operations on the main system bus. - This likely involves processing commands and data transfers for connected devices.
- Calls
- Platform-specific Operations:
- On ESP platforms:
- Yields to other tasks using
taskYIELD()
to allow multitasking.
- Yields to other tasks using
- On non-ESP platforms:
- Services the HTTP server (
fnHTTPD.service()
). - Manages tasks with
taskMgr.service()
. - Checks for deferred reboot requests.
- Services the HTTP server (
- On ESP platforms:
- Reboot Handling (non-ESP platforms):
- If a reboot is requested:
- Stops the web server.
- Initiates a system reboot with a special exit code.
- If a reboot is requested:
- Bluetooth Service (if active):
- Loop Control:
- On ESP platforms, the loop runs indefinitely.
- On other platforms, it continues until a shutdown is requested.
- Resource Management:
- Includes debug prints for heap memory usage on ESP platforms.
- Multitasking (ESP platform):
- The service loop runs as a high-priority task on a specific CPU core.
- This allows for efficient multitasking, especially important for real-time operations.
The service loop essentially acts as the central coordinator, ensuring that all components of the FujiNet system (communication buses, network services, device management) are continuously serviced. It's designed to be responsive to various events and requests while maintaining the overall system's functionality across different hardware platforms.