|
54 | 54 | */ |
55 | 55 | static uint8_t mfg_data[BT_MFG_DATA_LEN] = { 0xFF, 0xFF, }; |
56 | 56 |
|
57 | | -static const struct bt_data ad[] = { |
| 57 | +static const struct bt_data ad_long[] = { |
58 | 58 | BT_DATA(BT_DATA_MANUFACTURER_DATA, mfg_data, sizeof(mfg_data)), |
59 | 59 | #if CONFIG_BT_CTLR_ADV_DATA_LEN_MAX > 255 |
60 | 60 | BT_DATA(BT_DATA_MANUFACTURER_DATA, mfg_data, sizeof(mfg_data)), |
61 | 61 | #endif |
62 | 62 | BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME, sizeof(CONFIG_BT_DEVICE_NAME) - 1), |
63 | 63 | }; |
64 | 64 |
|
| 65 | +static const struct bt_data ad_short[] = { |
| 66 | + BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME, sizeof(CONFIG_BT_DEVICE_NAME) - 1), |
| 67 | +}; |
| 68 | + |
65 | 69 | static struct bt_le_ext_adv *adv[CONFIG_BT_EXT_ADV_MAX_ADV_SET]; |
66 | 70 |
|
67 | 71 | int broadcaster_multiple(void) |
68 | 72 | { |
69 | | - struct bt_le_adv_param adv_param = { |
70 | | - .id = BT_ID_DEFAULT, |
71 | | - .sid = 0U, /* Supply unique SID when creating advertising set */ |
72 | | - .secondary_max_skip = 0U, |
73 | | - .options = BT_LE_ADV_OPT_EXT_ADV, |
74 | | - .interval_min = BT_GAP_ADV_FAST_INT_MIN_2, |
75 | | - .interval_max = BT_GAP_ADV_FAST_INT_MAX_2, |
76 | | - .peer = NULL, |
77 | | - }; |
78 | 73 | int err; |
79 | 74 |
|
| 75 | + /* Create and start Advertising Sets */ |
80 | 76 | for (int index = 0; index < CONFIG_BT_EXT_ADV_MAX_ADV_SET; index++) { |
| 77 | + struct bt_le_adv_param adv_param = { |
| 78 | + .id = BT_ID_DEFAULT, |
| 79 | + .sid = 0U, /* Supply unique SID when creating advertising set */ |
| 80 | + .secondary_max_skip = 0U, |
| 81 | + .options = BT_LE_ADV_OPT_EXT_ADV, |
| 82 | + .interval_min = BT_GAP_ADV_FAST_INT_MIN_2, |
| 83 | + .interval_max = BT_GAP_ADV_FAST_INT_MAX_2, |
| 84 | + .peer = NULL, |
| 85 | + }; |
| 86 | + const struct adv_param_config { |
| 87 | + uint32_t options; |
| 88 | + const struct bt_data *ad; |
| 89 | + size_t ad_size; |
| 90 | + } param_config[] = { |
| 91 | + { /* Use 1M legacy PDU */ |
| 92 | + .options = BT_LE_ADV_OPT_NONE, |
| 93 | + .ad = ad_short, |
| 94 | + .ad_size = ARRAY_SIZE(ad_short), |
| 95 | + }, |
| 96 | + { /* Use 2M auxiliary PDU */ |
| 97 | + .options = BT_LE_ADV_OPT_EXT_ADV, |
| 98 | + .ad = ad_long, |
| 99 | + .ad_size = ARRAY_SIZE(ad_long), |
| 100 | + }, |
| 101 | + { /* Use 1M auxiliary PDU */ |
| 102 | + .options = BT_LE_ADV_OPT_EXT_ADV | BT_LE_ADV_OPT_NO_2M, |
| 103 | + .ad = ad_long, |
| 104 | + .ad_size = ARRAY_SIZE(ad_long), |
| 105 | + }, |
| 106 | + { /* Use Coded PHY */ |
| 107 | + .options = BT_LE_ADV_OPT_EXT_ADV | BT_LE_ADV_OPT_CODED, |
| 108 | + .ad = ad_long, |
| 109 | + .ad_size = ARRAY_SIZE(ad_long), |
| 110 | + }}; |
| 111 | + const struct bt_data *ad; |
| 112 | + size_t ad_size; |
| 113 | + |
81 | 114 | /* Use advertising set instance index as SID */ |
82 | 115 | adv_param.sid = index; |
83 | 116 |
|
| 117 | + /* Advertising set options, AD and AD array size */ |
| 118 | + const struct adv_param_config *config = |
| 119 | + ¶m_config[index % ARRAY_SIZE(param_config)]; |
| 120 | + |
| 121 | + adv_param.options = config->options; |
| 122 | + ad = config->ad; |
| 123 | + ad_size = config->ad_size; |
| 124 | + |
| 125 | +ext_adv_create_retry: |
84 | 126 | /* Create a non-connectable advertising set */ |
85 | 127 | err = bt_le_ext_adv_create(&adv_param, NULL, &adv[index]); |
86 | 128 | if (err) { |
87 | | - printk("Failed to create advertising set %d (err %d)\n", |
88 | | - index, err); |
| 129 | + /* Failed creating Coded PHY advertising set? */ |
| 130 | + if ((adv_param.options & BT_LE_ADV_OPT_CODED) != 0U) { |
| 131 | + printk("Failed to create advertising set %d with Coded PHY " |
| 132 | + "(err %d), retry without...\n", index, err); |
| 133 | + |
| 134 | + /* Retry with non-Coded PHY advertising set */ |
| 135 | + adv_param.options &= ~BT_LE_ADV_OPT_CODED; |
| 136 | + |
| 137 | + goto ext_adv_create_retry; |
| 138 | + } |
| 139 | + |
| 140 | + printk("Failed to create advertising set %d (err %d)\n", index, err); |
89 | 141 | return err; |
90 | 142 | } |
91 | 143 |
|
92 | 144 | /* Set extended advertising data */ |
93 | | - err = bt_le_ext_adv_set_data(adv[index], ad, ARRAY_SIZE(ad), |
94 | | - NULL, 0); |
| 145 | + err = bt_le_ext_adv_set_data(adv[index], ad, ad_size, NULL, 0); |
95 | 146 | if (err) { |
96 | 147 | printk("Failed to set advertising data for set %d " |
97 | 148 | "(err %d)\n", index, err); |
|
0 commit comments