Repository: incubator-mynewt-larva Updated Branches: refs/heads/master 0c7295431 -> 85c4011ae
Fix scan response reception and modify code to add a packet number to advertisements (for debugging purposes). Use console for advertising report display 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/85c4011a Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/85c4011a Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/85c4011a Branch: refs/heads/master Commit: 85c4011ae0a53435e2f47bd609cbccf11133720e Parents: 0c72954 Author: Willam San Filippo <w...@micosa.io> Authored: Fri Oct 30 13:05:05 2015 -0700 Committer: Willam San Filippo <w...@micosa.io> Committed: Fri Oct 30 13:05:28 2015 -0700 ---------------------------------------------------------------------- .gitignore | 1 + net/nimble/controller/include/controller/ll.h | 5 +- .../controller/include/controller/ll_adv.h | 5 +- net/nimble/controller/src/ble_ll_scan.c | 93 +++++++++---------- net/nimble/controller/src/ll.c | 70 ++++++++++----- net/nimble/controller/src/ll_adv.c | 61 ++++++------- net/nimble/controller/src/ll_hci.c | 2 +- net/nimble/controller/src/phy.c | 14 ++- net/nimble/host/src/host_dbg.c | 95 ++++++++++++++++++++ net/nimble/host/src/host_dbg.h | 22 +++++ net/nimble/host/src/host_hci.c | 9 +- project/bletest/src/main.c | 63 ++++++++++--- 12 files changed, 307 insertions(+), 133 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/85c4011a/.gitignore ---------------------------------------------------------------------- diff --git a/.gitignore b/.gitignore index c8dec68..30e9109 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ tags .gdb_history *~ .nest +.DS_Store http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/85c4011a/net/nimble/controller/include/controller/ll.h ---------------------------------------------------------------------- diff --git a/net/nimble/controller/include/controller/ll.h b/net/nimble/controller/include/controller/ll.h index aa23582..7fbf8db 100644 --- a/net/nimble/controller/include/controller/ll.h +++ b/net/nimble/controller/include/controller/ll.h @@ -403,7 +403,7 @@ enum ll_init_filt_policy int ll_init(void); /* 'Boolean' function returning true if address is a valid random address */ -int ll_is_valid_rand_addr(uint8_t *addr); +int ble_ll_is_valid_random_addr(uint8_t *addr); /* Calculate the amount of time a pdu of 'len' bytes will take to transmit */ uint16_t ll_pdu_tx_time_get(uint16_t len); @@ -431,4 +431,7 @@ void ble_ll_state_set(int ll_state); /* Send an event to LL task */ void ble_ll_event_send(struct os_event *ev); +/* Set random address */ +int ble_ll_set_random_addr(uint8_t *addr); + #endif /* H_LL_ */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/85c4011a/net/nimble/controller/include/controller/ll_adv.h ---------------------------------------------------------------------- diff --git a/net/nimble/controller/include/controller/ll_adv.h b/net/nimble/controller/include/controller/ll_adv.h index a302f20..0f0a8b7 100644 --- a/net/nimble/controller/include/controller/ll_adv.h +++ b/net/nimble/controller/include/controller/ll_adv.h @@ -134,9 +134,6 @@ int ll_adv_set_adv_data(uint8_t *cmd, uint8_t len); /* Set scan response data */ int ll_adv_set_scan_rsp_data(uint8_t *cmd, uint8_t len); -/* Set random address */ -int ll_adv_set_rand_addr(uint8_t *addr); - /* Set advertising parameters */ int ll_adv_set_adv_params(uint8_t *cmd); @@ -151,6 +148,6 @@ void ll_adv_tx_done_proc(void *arg); void ll_adv_init(void); /* Called when a scan request has been received. */ -int ll_adv_rx_scan_req(uint8_t *rxbuf); +int ble_ll_adv_rx_scan_req(uint8_t *rxbuf); #endif /* H_LL_ADV_ */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/85c4011a/net/nimble/controller/src/ble_ll_scan.c ---------------------------------------------------------------------- diff --git a/net/nimble/controller/src/ble_ll_scan.c b/net/nimble/controller/src/ble_ll_scan.c index 29e42c6..554ebe4 100644 --- a/net/nimble/controller/src/ble_ll_scan.c +++ b/net/nimble/controller/src/ble_ll_scan.c @@ -414,7 +414,7 @@ ble_ll_hci_send_adv_report(uint8_t pdu_type, uint8_t addr_type, uint8_t *rxbuf, subev = BLE_HCI_LE_SUBEV_ADV_RPT; adv_data_len = rxbuf[1] & BLE_ADV_PDU_HDR_LEN_MASK; - adv_data_len -= (BLE_LL_PDU_HDR_LEN + BLE_DEV_ADDR_LEN); + adv_data_len -= BLE_DEV_ADDR_LEN; } rc = BLE_ERR_MEM_CAPACITY; @@ -433,18 +433,19 @@ ble_ll_hci_send_adv_report(uint8_t pdu_type, uint8_t addr_type, uint8_t *rxbuf, } else { evbuf[5] = BLE_HCI_ADV_OWN_ADDR_PUBLIC; } - memcpy(evbuf + 6, rxbuf + BLE_LL_PDU_HDR_LEN, BLE_DEV_ADDR_LEN); + rxbuf += BLE_LL_PDU_HDR_LEN; + memcpy(evbuf + 6, rxbuf, BLE_DEV_ADDR_LEN); evbuf[12] = adv_data_len; - memcpy(evbuf + 13, rxbuf + BLE_LL_PDU_HDR_LEN + BLE_DEV_ADDR_LEN, + memcpy(evbuf + 13, rxbuf + BLE_DEV_ADDR_LEN, adv_data_len); evbuf[13 + adv_data_len] = rssi; - } - rc = ble_ll_hci_event_send(evbuf); - if (!rc) { - /* If we are filtering, add it to list of duplicate addresses */ - if (g_ble_ll_scan_sm.scan_filt_dups) { - ble_ll_scan_add_dup_adv(rxbuf + BLE_LL_PDU_HDR_LEN, addr_type); + rc = ble_ll_hci_event_send(evbuf); + if (!rc) { + /* If filtering, add it to list of duplicate addresses */ + if (g_ble_ll_scan_sm.scan_filt_dups) { + ble_ll_scan_add_dup_adv(rxbuf, addr_type); + } } } } @@ -629,7 +630,7 @@ ble_ll_scan_sm_start(struct ble_ll_scan_sm *scansm) * parameter (which in this case is just enable or disable). */ if (scansm->own_addr_type != BLE_HCI_ADV_OWN_ADDR_PUBLIC) { - if (!ll_is_valid_rand_addr(g_random_addr)) { + if (!ble_ll_is_valid_random_addr(g_random_addr)) { return BLE_ERR_CMD_DISALLOWED; } @@ -799,49 +800,42 @@ ble_ll_scan_rx_pdu_end(uint8_t *rxbuf) /* We only care about indirect advertisements or scan indirect */ if ((pdu_type == BLE_ADV_PDU_TYPE_ADV_IND) || (pdu_type == BLE_ADV_PDU_TYPE_ADV_SCAN_IND)) { - /* See if we should check the whitelist */ - adv_addr = rxbuf + BLE_LL_PDU_HDR_LEN; - addr_type = rxbuf[0] & BLE_ADV_PDU_HDR_TXADD_MASK; - if ((scansm->scan_filt_policy == BLE_HCI_SCAN_FILT_USE_WL) || - (scansm->scan_filt_policy == BLE_HCI_SCAN_FILT_USE_WL_INITA)) { - /* Check if device is on whitelist. If not, leave */ - if (!ble_ll_is_on_whitelist(adv_addr, addr_type)) { - return -1; - } - } - - /* - * Check to see if we have received a scan response from this - * advertisor. If so, no need to send scan request. - */ - if (ble_ll_scan_have_rxd_scan_rsp(adv_addr, addr_type)) { + /* See if we should check the whitelist */ + adv_addr = rxbuf + BLE_LL_PDU_HDR_LEN; + addr_type = rxbuf[0] & BLE_ADV_PDU_HDR_TXADD_MASK; + if ((scansm->scan_filt_policy == BLE_HCI_SCAN_FILT_USE_WL) || + (scansm->scan_filt_policy == BLE_HCI_SCAN_FILT_USE_WL_INITA)) { + /* Check if device is on whitelist. If not, leave */ + if (!ble_ll_is_on_whitelist(adv_addr, addr_type)) { return -1; } + } - /* Better not be a scan response pending */ - assert(scansm->scan_rsp_pending == 0); - - /* We want to send a request. See if backoff allows us */ - --scansm->backoff_count; - if (scansm->backoff_count == 0) { - /* Setup to transmit the scan request */ - ble_ll_scan_req_pdu_make(scansm, adv_addr, addr_type); - rc = ble_phy_tx(scansm->scan_req_pdu, BLE_PHY_TRANSITION_RX_TX, - BLE_PHY_TRANSITION_TX_RX); - - /* XXX: I still may want to post an event to the LL task - * instead of setting the scan response flag here. For now, - just do it here. */ - /* Set "waiting for scan response" flag */ - scansm->scan_rsp_pending = 1; - } + /* + * Check to see if we have received a scan response from this + * advertisor. If so, no need to send scan request. + */ + if (ble_ll_scan_have_rxd_scan_rsp(adv_addr, addr_type)) { + return -1; + } - /* - * XXX: how do we go back into receive when in scanning mode? - * For example, if we disable the PHY, we need to put the device - * back into receive mode. How does that happen???? Does the rx end - * do that or should the LL task? - */ + /* Better not be a scan response pending */ + assert(scansm->scan_rsp_pending == 0); + + /* We want to send a request. See if backoff allows us */ + --scansm->backoff_count; + if (scansm->backoff_count == 0) { + /* Setup to transmit the scan request */ + ble_ll_scan_req_pdu_make(scansm, adv_addr, addr_type); + rc = ble_phy_tx(scansm->scan_req_pdu, BLE_PHY_TRANSITION_RX_TX, + BLE_PHY_TRANSITION_TX_RX); + + /* XXX: I still may want to post an event to the LL task + * instead of setting the scan response flag here. For now, + just do it here. */ + /* Set "waiting for scan response" flag */ + scansm->scan_rsp_pending = 1; + } } return rc; @@ -924,6 +918,7 @@ ble_ll_scan_rx_pdu_proc(uint8_t pdu_type, uint8_t *rxbuf, int8_t rssi) } } + /* XXX: what do I do with return code here? Anything? */ /* Send the advertising report */ ble_ll_hci_send_adv_report(pdu_type, addr_type, rxbuf, rssi); } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/85c4011a/net/nimble/controller/src/ll.c ---------------------------------------------------------------------- diff --git a/net/nimble/controller/src/ll.c b/net/nimble/controller/src/ll.c index baa895d..ea25f6d 100644 --- a/net/nimble/controller/src/ll.c +++ b/net/nimble/controller/src/ll.c @@ -187,30 +187,9 @@ ble_ll_is_resolvable_priv_addr(uint8_t *addr) return 0; } -int -ble_ll_is_our_devaddr(uint8_t *addr, int addr_type) -{ - int rc; - uint8_t *our_addr; - - rc = 0; - if (addr_type) { - our_addr = g_dev_addr; - } else { - our_addr = g_random_addr; - } - - rc = 0; - if (!memcmp(our_addr, g_random_addr, BLE_DEV_ADDR_LEN)) { - rc = 1; - } - - return rc; -} - /* Checks to see that the device is a valid random address */ int -ll_is_valid_rand_addr(uint8_t *addr) +ble_ll_is_valid_random_addr(uint8_t *addr) { int i; int rc; @@ -253,6 +232,51 @@ ll_is_valid_rand_addr(uint8_t *addr) } /** + * Called from the HCI command parser when the set random address command + * is received. + * + * Context: Link Layer task (HCI command parser) + * + * @param addr Pointer to address + * + * @return int 0: success + */ +int +ble_ll_set_random_addr(uint8_t *addr) +{ + int rc; + + rc = BLE_ERR_INV_HCI_CMD_PARMS; + if (ble_ll_is_valid_random_addr(addr)) { + memcpy(g_random_addr, addr, BLE_DEV_ADDR_LEN); + rc = BLE_ERR_SUCCESS; + } + + return rc; +} + +int +ble_ll_is_our_devaddr(uint8_t *addr, int addr_type) +{ + int rc; + uint8_t *our_addr; + + rc = 0; + if (addr_type) { + our_addr = g_dev_addr; + } else { + our_addr = g_random_addr; + } + + rc = 0; + if (!memcmp(our_addr, g_random_addr, BLE_DEV_ADDR_LEN)) { + rc = 1; + } + + return rc; +} + +/** * ll pdu tx time get * * Returns the number of usecs it will take to transmit a PDU of length 'len' @@ -494,7 +518,7 @@ ll_rx_end(struct os_mbuf *rxpdu, uint8_t crcok) if (pdu_type == BLE_ADV_PDU_TYPE_SCAN_REQ) { /* Just bail if CRC is not good */ if (crcok) { - rc = ll_adv_rx_scan_req(rxbuf); + rc = ble_ll_adv_rx_scan_req(rxbuf); if (rc) { /* XXX: One thing left to reconcile here. We have * the advertisement schedule element still running. http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/85c4011a/net/nimble/controller/src/ll_adv.c ---------------------------------------------------------------------- diff --git a/net/nimble/controller/src/ll_adv.c b/net/nimble/controller/src/ll_adv.c index 5bdfed2..497699a 100644 --- a/net/nimble/controller/src/ll_adv.c +++ b/net/nimble/controller/src/ll_adv.c @@ -17,6 +17,7 @@ #include <string.h> #include <assert.h> #include "os/os.h" +#include "bsp/bsp.h" #include "nimble/ble.h" #include "nimble/hci_common.h" #include "controller/phy.h" @@ -25,6 +26,7 @@ #include "controller/ll_sched.h" #include "controller/ll_scan.h" #include "hal/hal_cputime.h" +#include "hal/hal_gpio.h" /* * Advertising configuration parameters. These are parameters that I have @@ -98,7 +100,8 @@ struct ll_adv_stats { uint32_t late_tx_done; uint32_t cant_set_sched; - /* XXX: add to these */ + uint32_t scan_rsp_txg; + uint32_t adv_txg; }; struct ll_adv_stats g_ll_adv_stats; @@ -115,9 +118,10 @@ struct ll_adv_stats g_ll_adv_stats; */ #define BLE_LL_ADV_SCHED_MAX_USECS (852) +/* For debug purposes */ +extern void bletest_inc_adv_pkt_num(void); + /** - * ble ll adv first chan - * * Calculate the first channel that we should advertise upon when we start * an advertising event. * @@ -345,6 +349,9 @@ ll_adv_tx_start_cb(struct ll_sched_item *sch) /* Get the state machine for the event */ advsm = (struct ll_adv_sm *)sch->cb_arg; + /* Toggle the LED */ + gpio_toggle(LED_BLINK_PIN); + /* Set channel */ rc = ble_phy_setchan(advsm->adv_chan); assert(rc == 0); @@ -365,6 +372,9 @@ ll_adv_tx_start_cb(struct ll_sched_item *sch) /* Set link layer state to advertising */ ble_ll_state_set(BLE_LL_STATE_ADV); + /* Count # of adv. sent */ + ++g_ll_adv_stats.adv_txg; + /* Set schedule item next wakeup time */ if (advsm->adv_type == BLE_HCI_ADV_TYPE_ADV_NONCONN_IND) { sch->next_wakeup = sch->end_time; @@ -389,7 +399,6 @@ ll_adv_tx_start_cb(struct ll_sched_item *sch) return rc; } - static struct ll_sched_item * ll_adv_sched_set(struct ll_adv_sm *advsm) { @@ -564,7 +573,7 @@ ll_adv_sm_start(struct ll_adv_sm *advsm) * parameter (which in this case is just enable or disable). */ if (advsm->own_addr_type != BLE_HCI_ADV_OWN_ADDR_PUBLIC) { - if (!ll_is_valid_rand_addr(g_random_addr)) { + if (!ble_ll_is_valid_random_addr(g_random_addr)) { return BLE_ERR_CMD_DISALLOWED; } @@ -730,39 +739,10 @@ ll_adv_set_adv_data(uint8_t *cmd, uint8_t len) return 0; } -/* XXX: this might be used for both scanning and advertising. */ /** - * ble ll adv set rand addr - * - * Called from the HCI command parser when the set random address command - * is received. - * - * Context: Link Layer task (HCI command parser) - * - * @param addr Pointer to address - * - * @return int 0: success - */ -int -ll_adv_set_rand_addr(uint8_t *addr) -{ - int rc; - - rc = BLE_ERR_INV_HCI_CMD_PARMS; - if (ll_is_valid_rand_addr(addr)) { - memcpy(g_random_addr, addr, BLE_DEV_ADDR_LEN); - rc = BLE_ERR_SUCCESS; - } - - return rc; -} - -/** - * ll adv rx scan req - * * Called when the LL receives a scan request. * - * NOTE: Called from interrupt context. + * Context: Called from interrupt context. * * @param rxbuf * @@ -771,7 +751,7 @@ ll_adv_set_rand_addr(uint8_t *addr) * > 0: PHY error attempting to go from rx to tx. */ int -ll_adv_rx_scan_req(uint8_t *rxbuf) +ble_ll_adv_rx_scan_req(uint8_t *rxbuf) { int rc; uint8_t rxaddr_type; @@ -792,6 +772,9 @@ ll_adv_rx_scan_req(uint8_t *rxbuf) /* Setup to transmit the scan response */ rc = ble_phy_tx(g_ll_adv_sm.scan_rsp_pdu, BLE_PHY_TRANSITION_RX_TX, BLE_PHY_TRANSITION_NONE); + if (!rc) { + ++g_ll_adv_stats.scan_rsp_txg; + } } return rc; @@ -819,6 +802,9 @@ ll_adv_tx_done_proc(void *arg) advsm = (struct ll_adv_sm *)arg; ble_ll_state_set(BLE_LL_STATE_STANDBY); + /* For debug purposes */ + bletest_inc_adv_pkt_num(); + /* * Check if we have ended our advertising event. If our last advertising * packet was sent on the last channel, it means we are done with this @@ -841,6 +827,9 @@ ll_adv_tx_done_proc(void *arg) itvl += rand() % (BLE_LL_ADV_DELAY_MS_MAX * 1000); advsm->adv_event_start_time += cputime_usecs_to_ticks(itvl); advsm->adv_pdu_start_time = advsm->adv_event_start_time; + + /* Toggle the LED */ + gpio_toggle(LED_BLINK_PIN); } else { /* * Move to next advertising channel. If not in the mask, just http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/85c4011a/net/nimble/controller/src/ll_hci.c ---------------------------------------------------------------------- diff --git a/net/nimble/controller/src/ll_hci.c b/net/nimble/controller/src/ll_hci.c index e717f75..8807a5b 100644 --- a/net/nimble/controller/src/ll_hci.c +++ b/net/nimble/controller/src/ll_hci.c @@ -175,7 +175,7 @@ ble_ll_hci_le_cmd_proc(uint8_t *cmdbuf, uint16_t ocf, uint8_t *rsplen) case BLE_HCI_OCF_LE_SET_RAND_ADDR: if (len == BLE_DEV_ADDR_LEN) { - rc = ll_adv_set_rand_addr(cmdbuf); + rc = ble_ll_set_random_addr(cmdbuf); } break; case BLE_HCI_OCF_LE_SET_ADV_PARAMS: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/85c4011a/net/nimble/controller/src/phy.c ---------------------------------------------------------------------- diff --git a/net/nimble/controller/src/phy.c b/net/nimble/controller/src/phy.c index ef698b6..83050a3 100644 --- a/net/nimble/controller/src/phy.c +++ b/net/nimble/controller/src/phy.c @@ -128,6 +128,7 @@ ble_phy_isr(void) uint32_t irq_en; uint32_t state; uint32_t shortcuts; + struct os_mbuf *rxpdu; struct ble_mbuf_hdr *ble_hdr; /* Check for disabled event. This only happens for transmits now */ @@ -167,6 +168,10 @@ ble_phy_isr(void) NRF_RADIO->INTENSET = RADIO_INTENSET_ADDRESS_Msk; g_ble_phy_data.phy_state = BLE_PHY_STATE_RX; + } else { + /* Disable the phy */ + /* XXX: count no bufs? */ + ble_phy_disable(); } } else { /* Better not be going from rx to tx! */ @@ -180,6 +185,8 @@ ble_phy_isr(void) NRF_RADIO->EVENTS_ADDRESS = 0; NRF_RADIO->INTENCLR = RADIO_INTENCLR_ADDRESS_Msk; + assert(g_ble_phy_data.rxpdu != NULL); + /* Wait to get 1st byte of frame */ while (1) { state = NRF_RADIO->STATE; @@ -245,14 +252,13 @@ ble_phy_isr(void) } /* Call Link Layer receive payload function */ - rc = ll_rx_end(g_ble_phy_data.rxpdu, ble_hdr->crcok); + rxpdu = g_ble_phy_data.rxpdu; + g_ble_phy_data.rxpdu = NULL; + rc = ll_rx_end(rxpdu, ble_hdr->crcok); if (rc < 0) { /* Disable the PHY. */ ble_phy_disable(); } - - /* The receive PDU has been handed to the Link Layer */ - g_ble_phy_data.rxpdu = NULL; } phy_isr_exit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/85c4011a/net/nimble/host/src/host_dbg.c ---------------------------------------------------------------------- diff --git a/net/nimble/host/src/host_dbg.c b/net/nimble/host/src/host_dbg.c new file mode 100644 index 0000000..ebd164e --- /dev/null +++ b/net/nimble/host/src/host_dbg.c @@ -0,0 +1,95 @@ +/** + * Copyright (c) 2015 Runtime Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include <stdint.h> +#include <stdio.h> +#include <assert.h> +#include <string.h> +#include "os/os.h" +#include "console/console.h" +#include "nimble/hci_common.h" +#include "nimble/hci_transport.h" + +void +host_hci_dbg_le_event_disp(uint8_t subev, uint8_t len, uint8_t *evdata) +{ + int8_t rssi; + uint8_t advlen; + int i; + int imax; + uint8_t *dptr; + char *adv_ptr; + char adv_data_buf[32]; + + switch (subev) { + case 0x02: + advlen = evdata[9]; + rssi = evdata[10 + advlen]; + console_printf("LE advertising report. len=%u num=%u evtype=%u " + "addrtype=%u addr=%x.%x.%x.%x.%x.%x advlen=%u " + "rssi=%d", len, evdata[0], evdata[1], evdata[2], + evdata[8], evdata[7], evdata[6], evdata[5], + evdata[4], evdata[3], advlen, rssi); + if (advlen) { + dptr = &evdata[10]; + while (advlen > 0) { + memset(adv_data_buf, 0, 32); + imax = advlen; + if (imax > 8) { + imax = 8; + } + adv_ptr = &adv_data_buf[0]; + for (i = 0; i < imax; ++i) { + snprintf(adv_ptr, 4, "%02x ", *dptr); + adv_ptr += 3; + ++dptr; + } + advlen -= imax; + console_printf("%s", adv_data_buf); + } + } + break; + default: + console_printf("\tUnknown LE event"); + break; + } +} + +void +host_hci_dbg_event_disp(uint8_t *evbuf) +{ + uint8_t *evdata; + uint8_t evcode; + uint8_t len; + uint16_t opcode; + + evcode = evbuf[0]; + len = evbuf[1]; + evdata = evbuf + 2; + + switch (evcode) { + case BLE_HCI_EVCODE_COMMAND_COMPLETE: + opcode = le16toh(evdata +1); + console_printf("Command Complete: cmd_pkts=%u ocf=0x%x ogf=0x%x", + evdata[0], opcode & 0x3FF, opcode >> 10); + break; + case BLE_HCI_EVCODE_LE_META: + host_hci_dbg_le_event_disp(evdata[0], len, evdata + 1); + break; + default: + console_printf("Unknown event 0x%x len=%u", evcode, len); + break; + } +} http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/85c4011a/net/nimble/host/src/host_dbg.h ---------------------------------------------------------------------- diff --git a/net/nimble/host/src/host_dbg.h b/net/nimble/host/src/host_dbg.h new file mode 100644 index 0000000..8d28e72 --- /dev/null +++ b/net/nimble/host/src/host_dbg.h @@ -0,0 +1,22 @@ +/** + * Copyright (c) 2015 Stack Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef H_HOST_DBG_ +#define H_HOST_DBG_ + +void host_hci_dbg_event_disp(uint8_t *evbuf); + +#endif /* H_HOST_DBG_ */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/85c4011a/net/nimble/host/src/host_hci.c ---------------------------------------------------------------------- diff --git a/net/nimble/host/src/host_hci.c b/net/nimble/host/src/host_hci.c index b658f99..837d2e8 100644 --- a/net/nimble/host/src/host_hci.c +++ b/net/nimble/host/src/host_hci.c @@ -20,8 +20,9 @@ #include "console/console.h" #include "nimble/hci_common.h" #include "nimble/hci_transport.h" +#include "host_dbg.h" -#define HCI_CMD_BUFS (4) +#define HCI_CMD_BUFS (8) #define HCI_CMD_BUF_SIZE (260) /* XXX: temporary, Fix later */ struct os_mempool g_hci_cmd_pool; os_membuf_t g_hci_cmd_buf[OS_MEMPOOL_SIZE(HCI_CMD_BUFS, HCI_CMD_BUF_SIZE)]; @@ -280,15 +281,13 @@ host_hci_cmd_le_set_scan_enable(uint8_t enable, uint8_t filter_dups) void host_hci_event_proc(struct os_event *ev) { - uint8_t *evbuf; os_error_t err; /* Count events received */ ++g_host_hci_stats.events_rxd; /* Display to console */ - evbuf = (uint8_t *)ev->ev_arg; - console_printf("Host received event %u", evbuf[0]); + host_hci_dbg_event_disp((uint8_t *)ev->ev_arg); /* XXX: Process the event */ @@ -308,6 +307,8 @@ ble_hci_transport_ctlr_event_send(uint8_t *hci_ev) os_error_t err; struct os_event *ev; + assert(hci_ev != NULL); + /* Get an event structure off the queue */ ev = (struct os_event *)os_memblock_get(&g_hci_os_event_pool); if (!ev) { http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/85c4011a/project/bletest/src/main.c ---------------------------------------------------------------------- diff --git a/project/bletest/src/main.c b/project/bletest/src/main.c index 34bc283..b2cc313 100755 --- a/project/bletest/src/main.c +++ b/project/bletest/src/main.c @@ -47,6 +47,7 @@ uint8_t g_random_addr[BLE_DEV_ADDR_LEN]; /* A buffer for host advertising data */ uint8_t g_host_adv_data[BLE_HCI_MAX_ADV_DATA_LEN]; +uint8_t g_host_adv_len; /* Create a mbuf pool of BLE mbufs */ #define MBUF_NUM_MBUFS (16) @@ -64,14 +65,40 @@ os_membuf_t g_mbuf_buffer[MBUF_MEMPOOL_SIZE]; /* Some application configurations */ #define BLETEST_ROLE_ADVERTISER (0) #define BLETEST_ROLE_SCANNER (1) +#define BLETEST_CFG_FILT_DUP_ADV (0) #define BLETEST_CFG_ADV_ITVL (500000 / BLE_HCI_ADV_ITVL) #define BLETEST_CFG_ADV_TYPE BLE_HCI_ADV_TYPE_ADV_SCAN_IND -#define BLETEST_CFG_SCAN_ITVL (500000 / BLE_HCI_SCAN_ITVL) -#define BLETEST_CFG_SCAN_WINDOW (400000 / BLE_HCI_SCAN_ITVL) +#define BLETEST_CFG_SCAN_ITVL (700000 / BLE_HCI_SCAN_ITVL) +#define BLETEST_CFG_SCAN_WINDOW (650000 / BLE_HCI_SCAN_ITVL) #define BLETEST_CFG_ROLE (BLETEST_ROLE_SCANNER) uint32_t g_next_os_time; int bletest_state; +void +bletest_inc_adv_pkt_num(void) +{ + int rc; + uint8_t *dptr; + uint8_t digit; + + dptr = &g_host_adv_data[18]; + while (dptr >= &g_host_adv_data[13]) { + digit = *dptr; + ++digit; + if (digit == 58) { + digit = 48; + *dptr = digit; + --dptr; + } else { + *dptr = digit; + break; + } + } + + rc = host_hci_cmd_le_set_adv_data(g_host_adv_data, g_host_adv_len); + assert(rc == 0); +} + uint8_t bletest_create_adv_pdu(uint8_t *dptr) { @@ -95,12 +122,12 @@ bletest_create_adv_pdu(uint8_t *dptr) dptr[7] = 'm'; dptr[8] = 'e'; dptr[9] = '-'; - dptr[10] = 'm'; - dptr[11] = 'y'; - dptr[12] = 'n'; - dptr[13] = 'e'; - dptr[14] = 'w'; - dptr[15] = 't'; + dptr[10] = '0'; + dptr[11] = '0'; + dptr[12] = '0'; + dptr[13] = '0'; + dptr[14] = '0'; + dptr[15] = '0'; dptr += 16; len += 16; @@ -111,6 +138,8 @@ bletest_create_adv_pdu(uint8_t *dptr) memcpy(dptr + 3, g_dev_addr, BLE_DEV_ADDR_LEN); len += 9; + g_host_adv_len = len; + return len; } @@ -140,6 +169,9 @@ bletest_init_advertising(void) /* Set advertising data */ rc = host_hci_cmd_le_set_adv_data(&g_host_adv_data[0], adv_len); assert(rc == 0); + + /* Set scan response data */ + rc = host_hci_cmd_le_set_scan_rsp_data(&g_host_adv_data[0], adv_len); } void @@ -148,7 +180,7 @@ bletest_init_scanner(void) int rc; /* Set scanning parameters */ - rc = host_hci_cmd_le_set_scan_params(BLE_HCI_SCAN_TYPE_ACTIVE, + rc = host_hci_cmd_le_set_scan_params(BLE_HCI_SCAN_TYPE_PASSIVE, BLETEST_CFG_SCAN_ITVL, BLETEST_CFG_SCAN_WINDOW, BLE_HCI_ADV_OWN_ADDR_PUBLIC, @@ -216,11 +248,11 @@ bletest_execute(void) /* Enable scanning */ if ((int32_t)(os_time_get() - g_next_os_time) >= 0) { if (bletest_state) { - rc = host_hci_cmd_le_set_scan_enable(0, 1); + rc = host_hci_cmd_le_set_scan_enable(0, BLETEST_CFG_FILT_DUP_ADV); assert(rc == 0); bletest_state = 0; } else { - rc = host_hci_cmd_le_set_scan_enable(1, 1); + rc = host_hci_cmd_le_set_scan_enable(1, BLETEST_CFG_FILT_DUP_ADV); assert(rc == 0); bletest_state = 1; } @@ -280,12 +312,21 @@ main(void) assert(rc == 0); /* Dummy device address */ +#if BLETEST_CFG_ROLE == BLETEST_ROLE_ADVERTISER g_dev_addr[0] = 0x00; g_dev_addr[1] = 0x00; g_dev_addr[2] = 0x00; g_dev_addr[3] = 0x88; g_dev_addr[4] = 0x88; g_dev_addr[5] = 0x08; +#else + g_dev_addr[0] = 0x00; + g_dev_addr[1] = 0x00; + g_dev_addr[2] = 0x00; + g_dev_addr[3] = 0x99; + g_dev_addr[4] = 0x99; + g_dev_addr[5] = 0x09; +#endif /* * Seed random number generator with least significant bytes of device