1 /*
2 * Copyright (c) 2012-2014 Wind River Systems, Inc.
3 * Copyright (c) 2020 Prevas A/S
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include <zephyr/kernel.h>
9 #include <zephyr/stats/stats.h>
10 #include <zephyr/usb/usb_device.h>
11
12 #ifdef CONFIG_MCUMGR_GRP_FS
13 #include <zephyr/device.h>
14 #include <zephyr/fs/fs.h>
15 #include <zephyr/fs/littlefs.h>
16 #endif
17 #ifdef CONFIG_MCUMGR_GRP_STAT
18 #include <zephyr/mgmt/mcumgr/grp/stat_mgmt/stat_mgmt.h>
19 #endif
20
21 #define LOG_LEVEL LOG_LEVEL_DBG
22 #include <zephyr/logging/log.h>
23 LOG_MODULE_REGISTER(smp_sample);
24
25 #include "common.h"
26
27 #define STORAGE_PARTITION_LABEL storage_partition
28 #define STORAGE_PARTITION_ID FIXED_PARTITION_ID(STORAGE_PARTITION_LABEL)
29
30 /* Define an example stats group; approximates seconds since boot. */
31 STATS_SECT_START(smp_svr_stats)
32 STATS_SECT_ENTRY(ticks)
33 STATS_SECT_END;
34
35 /* Assign a name to the `ticks` stat. */
36 STATS_NAME_START(smp_svr_stats)
37 STATS_NAME(smp_svr_stats, ticks)
38 STATS_NAME_END(smp_svr_stats);
39
40 /* Define an instance of the stats group. */
41 STATS_SECT_DECL(smp_svr_stats) smp_svr_stats;
42
43 #ifdef CONFIG_MCUMGR_GRP_FS
44 FS_LITTLEFS_DECLARE_DEFAULT_CONFIG(cstorage);
45 static struct fs_mount_t littlefs_mnt = {
46 .type = FS_LITTLEFS,
47 .fs_data = &cstorage,
48 .storage_dev = (void *)STORAGE_PARTITION_ID,
49 .mnt_point = "/lfs1"
50 };
51 #endif
52
main(void)53 int main(void)
54 {
55 int rc = STATS_INIT_AND_REG(smp_svr_stats, STATS_SIZE_32,
56 "smp_svr_stats");
57
58 if (rc < 0) {
59 LOG_ERR("Error initializing stats system [%d]", rc);
60 }
61
62 /* Register the built-in mcumgr command handlers. */
63 #ifdef CONFIG_MCUMGR_GRP_FS
64 rc = fs_mount(&littlefs_mnt);
65 if (rc < 0) {
66 LOG_ERR("Error mounting littlefs [%d]", rc);
67 }
68 #endif
69
70 #ifdef CONFIG_MCUMGR_TRANSPORT_BT
71 start_smp_bluetooth_adverts();
72 #endif
73
74 if (IS_ENABLED(CONFIG_USB_DEVICE_STACK)) {
75 rc = usb_enable(NULL);
76
77 /* Ignore EALREADY error as USB CDC is likely already initialised */
78 if (rc != 0 && rc != -EALREADY) {
79 LOG_ERR("Failed to enable USB");
80 return 0;
81 }
82 }
83 /* using __TIME__ ensure that a new binary will be built on every
84 * compile which is convenient when testing firmware upgrade.
85 */
86 LOG_INF("build time: " __DATE__ " " __TIME__);
87
88 /* The system work queue handles all incoming mcumgr requests. Let the
89 * main thread idle while the mcumgr server runs.
90 */
91 while (1) {
92 k_sleep(K_MSEC(1000));
93 STATS_INC(smp_svr_stats, ticks);
94 }
95 return 0;
96 }
97