|
4 | 4 | * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
5 | 5 | */ |
6 | 6 |
|
| 7 | +#include <zephyr/kernel.h> |
7 | 8 | #include <zephyr/logging/log.h> |
8 | 9 |
|
9 | 10 | #include "protocol/protocol.h" |
|
12 | 13 |
|
13 | 14 | LOG_MODULE_REGISTER(data_forwarder); |
14 | 15 |
|
| 16 | +/** |
| 17 | + * @brief Stop sensor sampling and log a warning if stop fails. |
| 18 | + */ |
| 19 | +static void sensor_stop(void) |
| 20 | +{ |
| 21 | + int err = data_fwd_sensor_stop(); |
| 22 | + |
| 23 | + if (err) { |
| 24 | + LOG_WRN("Sensor stop failed (err %d)", err); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * @brief Start sensors and a protocol session, retrying on failure. |
| 30 | + * |
| 31 | + * Retries while the transport link remains up. Returns when both starts |
| 32 | + * succeed or the link drops. |
| 33 | + * |
| 34 | + * @param session Session configuration passed to @ref proto_start_session(). |
| 35 | + */ |
| 36 | +static void session_start_retry(const struct proto_session_config *session) |
| 37 | +{ |
| 38 | + int err; |
| 39 | + |
| 40 | + while (transport_is_connected()) { |
| 41 | + err = data_fwd_sensor_start(); |
| 42 | + if (err) { |
| 43 | + LOG_WRN("Sensor start failed (err %d), retrying in %d ms", err, |
| 44 | + CONFIG_DATA_FWD_START_RETRY_MS); |
| 45 | + k_sleep(K_MSEC(CONFIG_DATA_FWD_START_RETRY_MS)); |
| 46 | + continue; |
| 47 | + } |
| 48 | + |
| 49 | + err = proto_start_session(session); |
| 50 | + if (err) { |
| 51 | + LOG_WRN("Failed to start session (err %d), retrying in %d ms", err, |
| 52 | + CONFIG_DATA_FWD_START_RETRY_MS); |
| 53 | + sensor_stop(); |
| 54 | + k_sleep(K_MSEC(CONFIG_DATA_FWD_START_RETRY_MS)); |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + LOG_INF("Sampling session started (sid %u)", proto_get_session_id()); |
| 59 | + break; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * @brief Fetch sensor samples and send them until the transport disconnects. |
| 65 | + */ |
| 66 | +static void stream_samples(void) |
| 67 | +{ |
| 68 | + int err; |
| 69 | + |
| 70 | + while (transport_is_connected()) { |
| 71 | + proto_value_t values[CONFIG_DATA_FWD_PROTO_MAX_CHANNELS]; |
| 72 | + size_t count; |
| 73 | + |
| 74 | + err = data_fwd_sensor_fetch(values, ARRAY_SIZE(values), &count); |
| 75 | + if (err) { |
| 76 | + LOG_WRN("Sample fetch failed (err %d)", err); |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + err = proto_send_samples(values, count); |
| 81 | + if (err) { |
| 82 | + LOG_WRN("Sample send failed (err %d)", err); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * @brief Stop the protocol session and sensor sampling after a connection ends. |
| 89 | + */ |
| 90 | +static void session_stop(void) |
| 91 | +{ |
| 92 | + proto_stop_session(); |
| 93 | + sensor_stop(); |
| 94 | + LOG_INF("Connection terminated"); |
| 95 | +} |
| 96 | + |
15 | 97 | int main(void) |
16 | 98 | { |
17 | 99 | int err; |
@@ -47,28 +129,13 @@ int main(void) |
47 | 129 | return err; |
48 | 130 | } |
49 | 131 |
|
50 | | - err = proto_start_session(&session); |
51 | | - if (err) { |
52 | | - LOG_ERR("Failed to start session (err %d)", err); |
53 | | - return err; |
54 | | - } |
55 | | - |
56 | | - LOG_INF("Data forwarder started (sid %u)", proto_get_session_id()); |
| 132 | + LOG_INF("Data forwarder started"); |
57 | 133 |
|
58 | 134 | while (1) { |
59 | | - proto_value_t values[CONFIG_DATA_FWD_PROTO_MAX_CHANNELS]; |
60 | | - size_t count; |
61 | | - |
62 | | - err = data_fwd_sensor_fetch(values, ARRAY_SIZE(values), &count); |
63 | | - if (err) { |
64 | | - LOG_WRN("Sample fetch failed (err %d)", err); |
65 | | - continue; |
66 | | - } |
67 | | - |
68 | | - err = proto_send_samples(values, count); |
69 | | - if (err) { |
70 | | - LOG_WRN("Sample send failed (err %d)", err); |
71 | | - } |
| 135 | + transport_wait_connected(); |
| 136 | + session_start_retry(&session); |
| 137 | + stream_samples(); |
| 138 | + session_stop(); |
72 | 139 | } |
73 | 140 |
|
74 | 141 | return 0; |
|
0 commit comments