Skip to content

Commit bea970c

Browse files
modules: battery: channel definition into per-module header
Separating battery channel definition into dedicated per-module header. Signed-off-by: Giacomo Dematteis <giacomo.dematteis@nordicsemi.no>
1 parent c878c3b commit bea970c

File tree

17 files changed

+92
-34
lines changed

17 files changed

+92
-34
lines changed

app/src/common/message_channel.c

-8
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,6 @@ ZBUS_CHAN_DEFINE(NETWORK_CHAN,
4242
ZBUS_MSG_INIT(.type = NETWORK_DISCONNECTED)
4343
);
4444

45-
ZBUS_CHAN_DEFINE(BATTERY_CHAN,
46-
struct battery_msg,
47-
NULL,
48-
NULL,
49-
ZBUS_OBSERVERS_EMPTY,
50-
ZBUS_MSG_INIT(0)
51-
);
52-
5345
ZBUS_CHAN_DEFINE(ERROR_CHAN,
5446
enum error_type,
5547
NULL,

app/src/common/message_channel.h

-26
Original file line numberDiff line numberDiff line change
@@ -175,32 +175,7 @@ struct network_msg {
175175

176176
#define MSG_TO_NETWORK_MSG(_msg) (*(const struct network_msg *)_msg)
177177

178-
/** BATTERY MODULE */
179178

180-
enum battery_msg_type {
181-
/* Output message types */
182-
183-
/* Response message to a request for a battery percentage sample. The sample is found in the
184-
* .percentage field of the message.
185-
*/
186-
BATTERY_PERCENTAGE_SAMPLE_RESPONSE = 0x1,
187-
188-
/* Input message types */
189-
190-
/* Request to retrieve the current battery percentage. The response is sent as a
191-
* BATTERY_PERCENTAGE_SAMPLE_RESPONSE message.
192-
*/
193-
BATTERY_PERCENTAGE_SAMPLE_REQUEST,
194-
};
195-
196-
struct battery_msg {
197-
enum battery_msg_type type;
198-
199-
/** Contains the current charge of the battery in percentage. */
200-
double percentage;
201-
};
202-
203-
#define MSG_TO_BATTERY_MSG(_msg) (*(const struct battery_msg *)_msg)
204179

205180
enum trigger_type {
206181
TRIGGER_POLL_SHADOW = 0x1,
@@ -278,7 +253,6 @@ ZBUS_CHAN_DECLARE(
278253
FOTA_STATUS_CHAN,
279254
LED_CHAN,
280255
NETWORK_CHAN,
281-
BATTERY_CHAN,
282256
TIME_CHAN,
283257
TRIGGER_CHAN,
284258
TRIGGER_MODE_CHAN,

app/src/modules/app/app.c

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "modules_common.h"
1515
#include "message_channel.h"
16+
#include "battery.h"
1617

1718
/* Register log module */
1819
LOG_MODULE_REGISTER(app, CONFIG_APP_LOG_LEVEL);

app/src/modules/battery/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
#
66

77
target_sources(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/battery.c)
8+
target_include_directories(app PRIVATE .)

app/src/modules/battery/battery.c

+10
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,23 @@
1818
#include "lp803448_model.h"
1919
#include "message_channel.h"
2020
#include "modules_common.h"
21+
#include "battery.h"
2122

2223
/* Register log module */
2324
LOG_MODULE_REGISTER(battery, CONFIG_APP_BATTERY_LOG_LEVEL);
2425

2526
/* Register subscriber */
2627
ZBUS_MSG_SUBSCRIBER_DEFINE(battery);
2728

29+
/* Define channels provided by this module */
30+
ZBUS_CHAN_DEFINE(BATTERY_CHAN,
31+
struct battery_msg,
32+
NULL,
33+
NULL,
34+
ZBUS_OBSERVERS_EMPTY,
35+
ZBUS_MSG_INIT(0)
36+
);
37+
2838
/* Observe channels */
2939
ZBUS_CHAN_ADD_OBS(BATTERY_CHAN, battery, 0);
3040

app/src/modules/battery/battery.h

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
#ifndef _BATTERY_H_
8+
#define _BATTERY_H_
9+
10+
#include <zephyr/kernel.h>
11+
#include <zephyr/zbus/zbus.h>
12+
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
16+
17+
/* Channels provided by this module */
18+
ZBUS_CHAN_DECLARE(
19+
BATTERY_CHAN
20+
);
21+
22+
enum battery_msg_type {
23+
/* Output message types */
24+
25+
/* Response message to a request for a battery percentage sample. The sample is found in the
26+
* .percentage field of the message.
27+
*/
28+
BATTERY_PERCENTAGE_SAMPLE_RESPONSE = 0x1,
29+
30+
/* Input message types */
31+
32+
/* Request to retrieve the current battery percentage. The response is sent as a
33+
* BATTERY_PERCENTAGE_SAMPLE_RESPONSE message.
34+
*/
35+
BATTERY_PERCENTAGE_SAMPLE_REQUEST,
36+
};
37+
38+
struct battery_msg {
39+
enum battery_msg_type type;
40+
41+
/** Contains the current charge of the battery in percentage. */
42+
double percentage;
43+
};
44+
45+
#define MSG_TO_BATTERY_MSG(_msg) (*(const struct battery_msg *)_msg)
46+
47+
#ifdef __cplusplus
48+
}
49+
#endif
50+
51+
#endif /* _BATTERY_H_ */

app/src/modules/button/button.c

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <date_time.h>
1212

1313
#include "message_channel.h"
14+
#include "battery.h"
1415

1516
/* Register log module */
1617
LOG_MODULE_REGISTER(button, CONFIG_APP_BUTTON_LOG_LEVEL);

app/src/modules/cloud/cloud_module.c

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "modules_common.h"
2121
#include "message_channel.h"
22+
#include "battery.h"
2223

2324
/* Register log module */
2425
LOG_MODULE_REGISTER(cloud, CONFIG_APP_CLOUD_LOG_LEVEL);

tests/module/app/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ zephyr_include_directories(${ZEPHYR_BASE}/subsys/testsuite/include)
2727
zephyr_include_directories(../../../app/src/common)
2828
zephyr_include_directories(../../../app/src/modules/cloud)
2929
zephyr_include_directories(${NRF_DIR}/subsys/net/lib/nrf_cloud/include)
30+
zephyr_include_directories(../../../app/src/modules/battery)
3031
zephyr_include_directories(${NRF_DIR}/../modules/lib/cjson)
3132

3233
target_link_options(app PRIVATE --whole-archive)

tests/module/app/src/checks.c

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <zephyr/zbus/zbus.h>
88
#include <zephyr/logging/log.h>
99
#include "message_channel.h"
10+
#include "battery.h"
1011

1112
#include "checks.h"
1213

tests/module/app/src/main.c

+10
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,19 @@
1313

1414
#include "dk_buttons_and_leds.h"
1515
#include "message_channel.h"
16+
#include "battery.h"
1617

1718
#include "checks.h"
1819

20+
/* Define the channels for testing */
21+
ZBUS_CHAN_DEFINE(BATTERY_CHAN,
22+
struct battery_msg,
23+
NULL,
24+
NULL,
25+
ZBUS_OBSERVERS_EMPTY,
26+
ZBUS_MSG_INIT(0)
27+
);
28+
1929
DEFINE_FFF_GLOBALS;
2030

2131
#define HOUR_IN_SECONDS 3600

tests/module/battery/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ zephyr_include_directories(${ZEPHYR_NRFXLIB_MODULE_DIR}/nrf_fuel_gauge/include/)
2828
zephyr_include_directories(${ZEPHYR_BASE}/subsys/testsuite/include)
2929
zephyr_include_directories(${ASSET_TRACKER_TEMPLATE_DIR}/app/src/common)
3030
zephyr_include_directories(${ASSET_TRACKER_TEMPLATE_DIR}/app/src/modules/cloud)
31+
zephyr_include_directories(${ASSET_TRACKER_TEMPLATE_DIR}/app/src/modules/battery)
3132

3233
# Options that cannot be passed through Kconfig fragments
3334
target_compile_definitions(app PRIVATE

tests/module/battery/src/main.c

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <zephyr/task_wdt/task_wdt.h>
1111
#include <zephyr/logging/log.h>
1212
#include "message_channel.h"
13+
#include "battery.h"
1314

1415
DEFINE_FFF_GLOBALS;
1516

tests/module/button/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ zephyr_include_directories(${ZEPHYR_BASE}/include/zephyr/)
2222
zephyr_include_directories(${ZEPHYR_BASE}/subsys/testsuite/include)
2323
zephyr_include_directories(../../../app/src/common)
2424
zephyr_include_directories(../../../app/src/modules/cloud)
25+
zephyr_include_directories(../../../app/src/modules/battery)
2526

2627
target_link_options(app PRIVATE --whole-archive)
2728
# Options that cannot be passed through Kconfig fragments

tests/module/button/src/main.c

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <zephyr/logging/log.h>
1111

1212
#include "message_channel.h"
13+
#include "battery.h"
1314

1415
#include <dk_buttons_and_leds.h>
1516
#include <date_time.h>

tests/module/cloud/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ zephyr_include_directories(${ZEPHYR_BASE}/include/zephyr/)
2222
zephyr_include_directories(${ZEPHYR_BASE}/subsys/testsuite/include)
2323
zephyr_include_directories(../../../app/src/modules/cloud)
2424
zephyr_include_directories(../../../app/src/common)
25+
zephyr_include_directories(../../../app/src/modules/battery)
2526
zephyr_include_directories(${NRF_DIR}/subsys/net/lib/nrf_cloud/include)
2627
zephyr_include_directories(${NRF_DIR}/../modules/lib/cjson)
2728

tests/module/cloud/src/cloud_module_test.c

+10
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,20 @@
77

88
#include <zephyr/fff.h>
99
#include "message_channel.h"
10+
#include "battery.h"
1011
#include <zephyr/task_wdt/task_wdt.h>
1112

1213
DEFINE_FFF_GLOBALS;
1314

15+
/* Define the channels for testing */
16+
ZBUS_CHAN_DEFINE(BATTERY_CHAN,
17+
struct battery_msg,
18+
NULL,
19+
NULL,
20+
ZBUS_OBSERVERS_EMPTY,
21+
ZBUS_MSG_INIT(0)
22+
);
23+
1424
FAKE_VALUE_FUNC(int, task_wdt_feed, int);
1525
FAKE_VALUE_FUNC(int, task_wdt_add, uint32_t, task_wdt_callback_t, void *);
1626
FAKE_VALUE_FUNC(int, nrf_cloud_client_id_get, char *, size_t);

0 commit comments

Comments
 (0)