|
1 |
| -# Main module |
| 1 | +# Main Module |
2 | 2 |
|
3 |
| -What does this module do |
| 3 | +The Main module serves as the central control unit of the Asset Tracker Template application. It implements a hierarchical state machine that coordinates the activities of all other modules through [zbus](https://docs.nordicsemi.com/bundle/ncs-latest/page/zephyr/services/zbus/index.html) messages. |
| 4 | +This module handles the application's business logic, including cloud connectivity, data sampling, firmware updates, configuration updates, and user interactions. |
4 | 5 |
|
5 | 6 | ## Messages
|
6 | 7 |
|
| 8 | +The main module does not implement messages available to other modules. Instead, it processes messages from other modules to control the application's behavior. |
| 9 | +It subscribes to messages on the following zbus channels: |
| 10 | + |
| 11 | +The Main module uses the following zbus channels, both for subscribing to incoming data and publishing outbound requests or status updates: |
| 12 | + |
| 13 | +* **BUTTON_CHAN** |
| 14 | + - Processes user button presses for manually triggering data samples. |
| 15 | + |
| 16 | +* **CLOUD_CHAN** |
| 17 | + - Receive connectivity status (connected, disconnected) and cloud response data. |
| 18 | + - Trigger device shadow polling to retrieve configuration updates. |
| 19 | + |
| 20 | +* **ENVIRONMENTAL_CHAN** |
| 21 | + - Request sensor data from the environmental module. |
| 22 | + |
| 23 | +* **FOTA_CHAN** |
| 24 | + - Poll for FOTA updates and manage the FOTA process. |
| 25 | + - Apply FOTA updates to install the new firmware image. |
| 26 | + |
| 27 | +* **LED_CHAN** |
| 28 | + - Update LED pattern to indicate system state. |
| 29 | + |
| 30 | +* **LOCATION_CHAN** |
| 31 | + - Requests new location data when a sample is due. |
| 32 | + |
| 33 | +* **NETWORK_CHAN** |
| 34 | + - Control LTE network connection. |
| 35 | + - Track cellular connectivity events. |
| 36 | + - Request network quality samples. |
| 37 | + |
| 38 | +* **POWER_CHAN** |
| 39 | + - Request battery status. |
| 40 | + - Initiate low-power mode. |
| 41 | + |
| 42 | +* **TIMER_CHAN** |
| 43 | + - Handle timer events for sampling. |
| 44 | + |
| 45 | + |
7 | 46 | ## Configurations
|
8 | 47 |
|
9 |
| -Kconfig and device tree |
| 48 | +The Main module can be configured using the following Kconfig options: |
| 49 | + |
| 50 | +* **CONFIG_APP_LOG_LEVEL** |
| 51 | + Controls logging level for the main module. |
| 52 | + |
| 53 | +* **CONFIG_APP_MODULE_TRIGGER_TIMEOUT_SECONDS** |
| 54 | + Default data sampling interval. |
| 55 | + |
| 56 | +* **CONFIG_APP_REQUEST_NETWORK_QUALITY** |
| 57 | + When enabled, requests network quality metrics during regular sampling. |
| 58 | + |
| 59 | +* **CONFIG_APP_MSG_PROCESSING_TIMEOUT_SECONDS** |
| 60 | + Maximum time allowed for processing a single message. |
| 61 | + |
| 62 | +* **CONFIG_APP_WATCHDOG_TIMEOUT_SECONDS** |
| 63 | + Defines the watchdog timeout for the main module. |
| 64 | + |
| 65 | + |
| 66 | +## State Diagram |
| 67 | + |
| 68 | +The Main module implements a hierarchical state machine with the following states: |
| 69 | + |
| 70 | +```mermaid |
| 71 | +stateDiagram-v2 |
| 72 | + [*] --> STATE_RUNNING |
| 73 | +
|
| 74 | + state STATE_RUNNING { |
| 75 | + [*] --> STATE_IDLE |
| 76 | +
|
| 77 | + STATE_IDLE --> STATE_TRIGGERING : CLOUD_CONNECTED_READY_TO_SEND |
| 78 | + STATE_TRIGGERING --> STATE_IDLE : CLOUD_DISCONNECTED/CLOUD_CONNECTED_PAUSED |
| 79 | +
|
| 80 | + state STATE_TRIGGERING { |
| 81 | + [*] --> STATE_SAMPLE_DATA |
| 82 | + STATE_WAIT_FOR_TRIGGER --> STATE_SAMPLE_DATA : timer_trigger/button_press |
| 83 | +
|
| 84 | + STATE_SAMPLE_DATA --> STATE_WAIT_FOR_TRIGGER : LOCATION_SEARCH_DONE |
| 85 | + } |
| 86 | + } |
| 87 | +
|
| 88 | + STATE_RUNNING --> STATE_FOTA : FOTA_DOWNLOADING_UPDATE |
| 89 | +
|
| 90 | + state STATE_FOTA { |
| 91 | + [*] --> STATE_FOTA_DOWNLOADING |
| 92 | +
|
| 93 | + STATE_FOTA_DOWNLOADING --> STATE_FOTA_WAITING_FOR_NETWORK_DISCONNECT : FOTA_SUCCESS_REBOOT_NEEDED |
| 94 | + STATE_FOTA_DOWNLOADING --> STATE_FOTA_WAITING_FOR_NETWORK_DISCONNECT_TO_APPLY_IMAGE : FOTA_IMAGE_APPLY_NEEDED |
| 95 | +
|
| 96 | + STATE_FOTA_WAITING_FOR_NETWORK_DISCONNECT --> STATE_FOTA_REBOOTING : NETWORK_DISCONNECTED |
| 97 | +
|
| 98 | + STATE_FOTA_WAITING_FOR_NETWORK_DISCONNECT_TO_APPLY_IMAGE --> STATE_FOTA_APPLYING_IMAGE : NETWORK_DISCONNECTED |
| 99 | + STATE_FOTA_APPLYING_IMAGE --> STATE_FOTA_REBOOTING : FOTA_SUCCESS_REBOOT_NEEDED |
| 100 | +
|
| 101 | + STATE_FOTA_REBOOTING --> [*] : sys_reboot() |
| 102 | + } |
10 | 103 |
|
11 |
| -## State diagram |
| 104 | + STATE_FOTA --> STATE_RUNNING : FOTA_DOWNLOAD_CANCELED/TIMED_OUT/FAILED |
| 105 | +``` |
0 commit comments