Repository: incubator-mynewt-larva Updated Branches: refs/heads/master dc6289454 -> 56f1ce6f8
Set advertising data. Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/commit/56f1ce6f Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/56f1ce6f Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/56f1ce6f Branch: refs/heads/master Commit: 56f1ce6f8179c6c711d703de80e8a1c21d3499a5 Parents: dc62894 Author: Christopher Collins <[email protected]> Authored: Mon Dec 21 15:33:09 2015 -0800 Committer: Christopher Collins <[email protected]> Committed: Mon Dec 21 15:34:24 2015 -0800 ---------------------------------------------------------------------- net/nimble/host/include/host/ble_gap.h | 6 ++ net/nimble/host/src/ble_gap_conn.c | 123 +++++++++++++++++++++++++--- net/nimble/host/src/ble_gap_conn.h | 18 +++- project/hostctlrtest/src/main.c | 18 +++- 4 files changed, 146 insertions(+), 19 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/56f1ce6f/net/nimble/host/include/host/ble_gap.h ---------------------------------------------------------------------- diff --git a/net/nimble/host/include/host/ble_gap.h b/net/nimble/host/include/host/ble_gap.h index 8cb9d0f..f91c30e 100644 --- a/net/nimble/host/include/host/ble_gap.h +++ b/net/nimble/host/include/host/ble_gap.h @@ -68,9 +68,15 @@ typedef void ble_gap_connect_fn(struct ble_gap_conn_event *event, void *arg); #define BLE_GAP_DISC_MODE_LTD 2 #define BLE_GAP_DISC_MODE_GEN 3 +struct ble_gap_conn_adv_fields { + char *name; + unsigned name_is_complete:1; +}; + void ble_gap_conn_set_cb(ble_gap_connect_fn *cb, void *arg); int ble_gap_conn_advertise(uint8_t discoverable_mode, uint8_t connectable_mode, uint8_t *peer_addr, uint8_t peer_addr_type); +int ble_gap_conn_set_adv_fields(struct ble_gap_conn_adv_fields *adv_fields); int ble_gap_conn_gen_disc(uint32_t duration_ms); int ble_gap_conn_direct_connect(int addr_type, uint8_t *addr); int ble_gap_conn_terminate(uint16_t handle); http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/56f1ce6f/net/nimble/host/src/ble_gap_conn.c ---------------------------------------------------------------------- diff --git a/net/nimble/host/src/ble_gap_conn.c b/net/nimble/host/src/ble_gap_conn.c index 1e4db01..90db35b 100644 --- a/net/nimble/host/src/ble_gap_conn.c +++ b/net/nimble/host/src/ble_gap_conn.c @@ -82,18 +82,16 @@ /** 10.24 seconds. */ #define BLE_GAP_GEN_DISC_SCAN_MIN (10.24 * 1000) -#define BLE_GAP_CONN_S_MODE_NONE (-1) -#define BLE_GAP_CONN_S_MODE_NON_DISC 0 -#define BLE_GAP_CONN_S_MODE_LTD_DISC 1 -#define BLE_GAP_CONN_S_MODE_GEN_DISC 2 -#define BLE_GAP_CONN_S_MODE_NON_CONN 3 -#define BLE_GAP_CONN_S_MODE_DIR_CONN 4 -#define BLE_GAP_CONN_S_MODE_UND_CONN 5 -#define BLE_GAP_CONN_S_MODE_MAX 6 - #define BLE_GAP_CONN_MODE_MAX 4 #define BLE_GAP_DISC_MODE_MAX 4 +/** + * The maximum amount of user data that can be put into the advertising data. + * Six bytes are reserved at the end for the flags field and the transmit power + * field. + */ +#define BLE_GAP_CONN_ADV_DATA_LIMIT (BLE_HCI_MAX_ADV_DATA_LEN - 6) + static int ble_gap_conn_adv_params_tx(void *arg); static int ble_gap_conn_adv_power_tx(void *arg); static int ble_gap_conn_adv_data_tx(void *arg); @@ -126,6 +124,9 @@ static uint8_t ble_gap_conn_master_addr_type; static uint8_t ble_gap_conn_slave_addr_type; static uint8_t ble_gap_conn_master_addr[BLE_DEV_ADDR_LEN]; static uint8_t ble_gap_conn_slave_addr[BLE_DEV_ADDR_LEN]; +static uint8_t ble_gap_conn_adv_data_len; +static uint8_t ble_gap_conn_adv_data[BLE_HCI_MAX_ADV_DATA_LEN]; +static int8_t ble_gap_conn_tx_pwr_lvl; static struct os_callout_func ble_gap_conn_master_timer; static struct os_callout_func ble_gap_conn_slave_timer; @@ -572,11 +573,53 @@ ble_gap_conn_adv_rsp_data_tx(void *arg) static int ble_gap_conn_adv_data_tx(void *arg) { - uint8_t adv_data[BLE_HCI_MAX_ADV_DATA_LEN] = { 0 }; /* XXX */ + uint8_t flags; + int adv_data_len; int rc; + /* Calculate the value of the flags field from the discoverable mode. */ + flags = 0; + switch (ble_gap_conn_s_disc_mode) { + case BLE_GAP_DISC_MODE_NON: + break; + + case BLE_GAP_DISC_MODE_LTD: + flags |= BLE_GAP_CONN_AD_F_DISC_LTD; + break; + + case BLE_GAP_DISC_MODE_GEN: + flags |= BLE_GAP_CONN_AD_F_DISC_GEN; + break; + + default: + assert(0); + break; + } + + /* Encode the flags AD field if it is nonzero. */ + if (flags != 0) { + adv_data_len = ble_gap_conn_adv_data_len + 3; + assert(adv_data_len <= BLE_HCI_MAX_ADV_DATA_LEN); + + ble_gap_conn_adv_data[ble_gap_conn_adv_data_len] = 2; + ble_gap_conn_adv_data[ble_gap_conn_adv_data_len + 1] = + BLE_GAP_CONN_AD_TYPE_FLAGS; + ble_gap_conn_adv_data[ble_gap_conn_adv_data_len + 2] = flags; + } else { + adv_data_len = ble_gap_conn_adv_data_len; + } + + /* Encode the transmit power AD field. */ + adv_data_len = ble_gap_conn_adv_data_len + 3; + assert(adv_data_len <= BLE_HCI_MAX_ADV_DATA_LEN); + ble_gap_conn_adv_data[ble_gap_conn_adv_data_len] = 2; + ble_gap_conn_adv_data[ble_gap_conn_adv_data_len + 1] = + BLE_GAP_CONN_AD_TYPE_TX_PWR_LEVEL; + ble_gap_conn_adv_data[ble_gap_conn_adv_data_len + 2] = + ble_gap_conn_tx_pwr_lvl; + ble_hci_ack_set_callback(ble_gap_conn_adv_ack, NULL); - rc = host_hci_cmd_le_set_adv_data(adv_data, sizeof adv_data); + rc = host_hci_cmd_le_set_adv_data(ble_gap_conn_adv_data, adv_data_len); if (rc != 0) { ble_gap_conn_slave_failed(rc); return 1; @@ -611,7 +654,8 @@ ble_gap_conn_adv_power_ack(struct ble_hci_ack *ack, void *arg) return; } - /* XXX: Save power level value so it can be put in the adv. data. */ + /* Save power level value so it can be put in the advertising data. */ + ble_gap_conn_tx_pwr_lvl = power_level; ble_gap_conn_adv_next_state(); } @@ -765,6 +809,61 @@ ble_gap_conn_advertise(uint8_t discoverable_mode, uint8_t connectable_mode, return 0; } +static int +ble_gap_conn_set_adv_field(uint8_t type, uint8_t data_len, void *data) +{ + int new_len; + + new_len = ble_gap_conn_adv_data_len + 2 + data_len; + if (new_len > BLE_GAP_CONN_ADV_DATA_LIMIT) { + return BLE_HS_EMSGSIZE; + } + + ble_gap_conn_adv_data[ble_gap_conn_adv_data_len] = data_len + 1; + ble_gap_conn_adv_data[ble_gap_conn_adv_data_len + 1] = type; + memcpy(ble_gap_conn_adv_data + ble_gap_conn_adv_data_len + 2, + data, data_len); + + ble_gap_conn_adv_data_len = new_len; + + return 0; +} + +/** + * Sets the significant part of the data in outgoing advertisements. + * + * @return 0 on success; on failure. + */ +int +ble_gap_conn_set_adv_fields(struct ble_gap_conn_adv_fields *adv_fields) +{ + uint8_t type; + int name_len; + int rc; + + ble_gap_conn_adv_data_len = 0; + + if (adv_fields->name != NULL) { + name_len = strlen(adv_fields->name); + if (name_len > UINT8_MAX) { + return BLE_HS_EINVAL; + } + + if (adv_fields->name_is_complete) { + type = BLE_GAP_CONN_AD_TYPE_COMP_NAME; + } else { + type = BLE_GAP_CONN_AD_TYPE_INCOMP_NAME; + } + + rc = ble_gap_conn_set_adv_field(type, name_len, adv_fields->name); + if (rc != 0) { + return rc; + } + } + + return 0; +} + /***************************************************************************** * $general discovery procedure * *****************************************************************************/ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/56f1ce6f/net/nimble/host/src/ble_gap_conn.h ---------------------------------------------------------------------- diff --git a/net/nimble/host/src/ble_gap_conn.h b/net/nimble/host/src/ble_gap_conn.h index ea1430e..767cff5 100644 --- a/net/nimble/host/src/ble_gap_conn.h +++ b/net/nimble/host/src/ble_gap_conn.h @@ -23,9 +23,21 @@ struct hci_le_conn_complete; struct hci_disconn_complete; struct ble_hci_ack; -int ble_gap_conn_general_discovery(void); -int ble_gap_conn_direct_connect(int addr_type, uint8_t *addr); -int ble_gap_conn_direct_advertise(int addr_type, uint8_t *addr); +#define BLE_GAP_CONN_AD_TYPE_FLAGS 0x01 +#define BLE_GAP_CONN_AD_TYPE_INCOMP_16BIT_UUIDS 0x02 +#define BLE_GAP_CONN_AD_TYPE_COMP_16BIT_UUIDS 0x03 +#define BLE_GAP_CONN_AD_TYPE_INCOMP_32BIT_UUIDS 0x04 +#define BLE_GAP_CONN_AD_TYPE_COMP_32BIT_UUIDS 0x05 +#define BLE_GAP_CONN_AD_TYPE_INCOMP_128BIT_UUIDS 0x06 +#define BLE_GAP_CONN_AD_TYPE_COMP_128BIT_UUIDS 0x07 +#define BLE_GAP_CONN_AD_TYPE_INCOMP_NAME 0x08 +#define BLE_GAP_CONN_AD_TYPE_COMP_NAME 0x09 +#define BLE_GAP_CONN_AD_TYPE_TX_PWR_LEVEL 0x0a +#define BLE_GAP_CONN_AD_TYPE_DEVICE_CLASS 0x0b + +#define BLE_GAP_CONN_AD_F_DISC_LTD 0x01 +#define BLE_GAP_CONN_AD_F_DISC_GEN 0x02 + void ble_gap_conn_rx_adv_report(struct ble_gap_conn_adv_rpt *rpt); int ble_gap_conn_rx_conn_complete(struct hci_le_conn_complete *evt); void ble_gap_conn_rx_disconn_complete(struct hci_disconn_complete *evt); http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/56f1ce6f/project/hostctlrtest/src/main.c ---------------------------------------------------------------------- diff --git a/project/hostctlrtest/src/main.c b/project/hostctlrtest/src/main.c index 3c75de0..dc8c8e2 100755 --- a/project/hostctlrtest/src/main.c +++ b/project/hostctlrtest/src/main.c @@ -59,7 +59,9 @@ uint8_t g_host_adv_len; static uint8_t hostctlrtest_slv_addr[6] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06}; //static uint8_t hostctlrtest_slv_addr[6] = {0x82, 0x6a, 0xd0, 0x48, 0xb4, 0xb0}; +#if HOSTCTLRTEST_CFG_ROLE == HOSTCTLRTEST_ROLE_INITIATOR static uint8_t hostctlrtest_mst_addr[6] = {0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a}; +#endif /* Create a mbuf pool of BLE mbufs */ #define MBUF_NUM_MBUFS (8) @@ -331,12 +333,20 @@ hostctlrtest_task_handler(void *arg) ble_gap_conn_set_cb(hostctlrtest_on_connect, NULL); #if HOSTCTLRTEST_CFG_ROLE == HOSTCTLRTEST_ROLE_ADVERTISER + struct ble_gap_conn_adv_fields ad_fields; + hostctlrtest_register_attrs(); - console_printf("ble_gap_directed_connectable\n"); - rc = ble_gap_conn_direct_connectable(BLE_HCI_ADV_PEER_ADDR_PUBLIC, - hostctlrtest_mst_addr); + console_printf("ADVERTISER\n"); + + ad_fields.name = "blahblah"; + ad_fields.name_is_complete = 1; + rc = ble_gap_conn_set_adv_fields(&ad_fields); + assert(rc == 0); + + rc = ble_gap_conn_advertise(BLE_GAP_DISC_MODE_NON, BLE_GAP_CONN_MODE_UND, + NULL, 0); #else - console_printf("ble_gap_direct_connection_establishment\n"); + console_printf("INITIATOR\n"); rc = ble_gap_conn_direct_connect(BLE_HCI_ADV_PEER_ADDR_PUBLIC, hostctlrtest_slv_addr); #endif
