blob: 4a219743ddc45bb446c6f0c3ad0a8d8ad0ef8cba [file] [log] [blame]
/*
* Copyright (c) 2012-2014 Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <assert.h>
#include <zephyr.h>
#include <string.h>
#include <stdlib.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/conn.h>
#include <bluetooth/gatt.h>
#include <stats/stats.h>
#include <mgmt/smp_bt.h>
#include <mgmt/buf.h>
#ifdef CONFIG_MCUMGR_CMD_FS_MGMT
#include <device.h>
#include <fs/fs.h>
#include <nffs/nffs.h>
#include "fs_mgmt/fs_mgmt.h"
#endif
#ifdef CONFIG_MCUMGR_CMD_OS_MGMT
#include "os_mgmt/os_mgmt.h"
#endif
#ifdef CONFIG_MCUMGR_CMD_IMG_MGMT
#include "img_mgmt/img_mgmt.h"
#endif
#ifdef CONFIG_MCUMGR_CMD_STAT_MGMT
#include "stat_mgmt/stat_mgmt.h"
#endif
#define DEVICE_NAME CONFIG_BT_DEVICE_NAME
#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)
/* Define an example stats group; approximates seconds since boot. */
STATS_SECT_START(smp_svr_stats)
STATS_SECT_ENTRY(ticks)
STATS_SECT_END;
/* Assign a name to the `ticks` stat. */
STATS_NAME_START(smp_svr_stats)
STATS_NAME(smp_svr_stats, ticks)
STATS_NAME_END(smp_svr_stats);
/* Define an instance of the stats group. */
STATS_SECT_DECL(smp_svr_stats) smp_svr_stats;
#ifdef CONFIG_MCUMGR_CMD_FS_MGMT
static struct nffs_flash_desc flash_desc;
static struct fs_mount_t nffs_mnt = {
.type = FS_NFFS,
.mnt_point = "/nffs",
.fs_data = &flash_desc,
};
#endif
static const struct bt_data ad[] = {
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
BT_DATA_BYTES(BT_DATA_UUID128_ALL,
0x84, 0xaa, 0x60, 0x74, 0x52, 0x8a, 0x8b, 0x86,
0xd3, 0x4c, 0xb7, 0x1d, 0x1d, 0xdc, 0x53, 0x8d),
};
static const struct bt_data sd[] = {
BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
};
static void advertise(void)
{
int rc;
bt_le_adv_stop();
rc = bt_le_adv_start(BT_LE_ADV_CONN, ad, ARRAY_SIZE(ad),
sd, ARRAY_SIZE(sd));
if (rc) {
printk("Advertising failed to start (rc %d)\n", rc);
return;
}
printk("Advertising successfully started\n");
}
static void connected(struct bt_conn *conn, u8_t err)
{
if (err) {
printk("Connection failed (err %u)\n", err);
} else {
printk("Connected\n");
}
}
static void disconnected(struct bt_conn *conn, u8_t reason)
{
printk("Disconnected (reason %u)\n", reason);
advertise();
}
static struct bt_conn_cb conn_callbacks = {
.connected = connected,
.disconnected = disconnected,
};
static void bt_ready(int err)
{
if (err) {
printk("Bluetooth init failed (err %d)\n", err);
return;
}
printk("Bluetooth initialized\n");
advertise();
}
void main(void)
{
struct device *flash_dev;
int rc;
rc = STATS_INIT_AND_REG(smp_svr_stats, STATS_SIZE_32, "smp_svr_stats");
assert(rc == 0);
/* Register the built-in mcumgr command handlers. */
#ifdef CONFIG_MCUMGR_CMD_FS_MGMT
flash_dev = device_get_binding(CONFIG_FS_NFFS_FLASH_DEV_NAME);
if (!flash_dev) {
printk("Error getting NFFS flash device binding\n");
} else {
/* set backend storage dev */
nffs_mnt.storage_dev = flash_dev;
rc = fs_mount(&nffs_mnt);
if (rc < 0) {
printk("Error mounting nffs [%d]\n", rc);
}
}
fs_mgmt_register_group();
#endif
#ifdef CONFIG_MCUMGR_CMD_OS_MGMT
os_mgmt_register_group();
#endif
#ifdef CONFIG_MCUMGR_CMD_IMG_MGMT
img_mgmt_register_group();
#endif
#ifdef CONFIG_MCUMGR_CMD_STAT_MGMT
stat_mgmt_register_group();
#endif
/* Enable Bluetooth. */
rc = bt_enable(bt_ready);
if (rc != 0) {
printk("Bluetooth init failed (err %d)\n", rc);
return;
}
bt_conn_cb_register(&conn_callbacks);
/* Initialize the Bluetooth mcumgr transport. */
smp_bt_register();
/* The system work queue handles all incoming mcumgr requests. Let the
* main thread idle while the mcumgr server runs.
*/
while (1) {
k_sleep(1000);
STATS_INC(smp_svr_stats, ticks);
}
}