/* Copyright (C) Shenzhen Minew Technologies Co., Ltd 
   All rights reserved. */

#include <zephyr.h>
#include <zephyr/types.h>
#include <stddef.h>
#include <sys/util.h>

#include <bluetooth/hci.h>
#include <bluetooth/hci_vs.h>
#include <bluetooth/conn.h>
#include <bluetooth/uuid.h>
#include <bluetooth/gatt.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/controller.h>

#include <logging/log.h>
LOG_MODULE_REGISTER(mt_advertising, 3);

static struct bt_le_ext_adv *adv_set;
static struct bt_le_ext_adv *adv_set_1;

/**
 * @brief 广播数据.
 */
static const struct bt_data ad[] = {
	BT_DATA_BYTES(BT_DATA_FLAGS, BT_DATA_UUID128_SOME),
	BT_DATA_BYTES(BT_DATA_MANUFACTURER_DATA,
		      	0x4C, 0x00, 			/* Apple. */
		      	0x02, 0x15, 			/* iBeacon. */
		      	0x01, 0x12, 0x23, 0x34,	/* UUID-1. */
				0x45, 0x56, 0x67, 0x78,	/* UUID-2. */
				0x89, 0x9A, 0xAB, 0xBC,	/* UUID-3. */
				0xCD, 0xDE, 0xEF, 0xF0,	/* UUID-4. */
		      	0x66,0x66, 				/* Major. */
		      	0x66,0x66, 				/* Minor. */
		      	0xC5),					/* Calibrated RSSI @ 1m. */
	BT_DATA_BYTES(BT_DATA_MANUFACTURER_DATA,
				0x39, 0x06,
				0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00,
				0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00,
				0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00,
				0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00,
				0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00,
				0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00,
				0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00,
				0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00,
				0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00,
				0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00,
				0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00,
				0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00,
				0xFF),
    BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME, (sizeof(CONFIG_BT_DEVICE_NAME) - 1))
};

/**
 * @brief 广播类型等参数.
 */
const static struct bt_le_adv_param param =
		BT_LE_ADV_PARAM_INIT(
                     BT_LE_ADV_OPT_EXT_ADV | BT_LE_ADV_OPT_CODED | BT_LE_ADV_OPT_CONNECTABLE | BT_LE_ADV_OPT_USE_TX_POWER,
				     BT_GAP_ADV_FAST_INT_MIN_2,
				     BT_GAP_ADV_FAST_INT_MIN_2,
				     NULL);

/**
 * @brief 主函数入口.
 */
void main(void)
{
    int err = bt_enable(NULL);
    if (err) {
        LOG_ERR("[%04d] Bluetooth init failed (err %d).\n", __LINE__, err);
        return;
    }
    LOG_INF("[%04d] Bluetooth initialized.", __LINE__);

    err = bt_le_ext_adv_create(&param, NULL, &adv_set);
    if (err) {
        LOG_ERR("[%04d] Create extended advertising set_id failed (err %d).\n", __LINE__, err);
        return;
    }

    err = bt_le_ext_adv_create(&param, NULL, &adv_set_1);
    if (err) {
        LOG_ERR("[%04d] Create extended advertising set_id_1 failed (err %d).\n", __LINE__, err);
        return;
    }

    err = bt_le_ext_adv_set_data(adv_set, ad, ARRAY_SIZE(ad), NULL, 0);
    if (err) {
        LOG_ERR("[%04d] Failed to set advertising data (%d).\n", __LINE__, err);
        return;
    }

    err = bt_le_ext_adv_set_data(adv_set_1, ad, ARRAY_SIZE(ad), NULL, 0);
    if (err) {
        LOG_ERR("[%04d] Failed to set advertising data (%d).\n", __LINE__, err);
        return;
    }

    err = bt_le_ext_adv_start(adv_set, NULL);
    if (err) {
        LOG_ERR("[%04d] Extended advertising failed to start (err %d).\n", __LINE__, err);
        return;
    }

    err = bt_le_ext_adv_start(adv_set_1, NULL);
    if (err) {
        LOG_ERR("[%04d] Extended advertising failed to start (err %d).\n", __LINE__, err);
        return;
    }

    LOG_INF("[%04d] Extended advertising enable...", __LINE__);
}