Repository: incubator-mynewt-core Updated Branches: refs/heads/bsnbranch 631cc3d93 -> ee0f350e0
MYNEWT-707: Add API to retrieve public and random static address Please read the Jira ticket for more information on how to use these API. The controller now calls ble_hw_get_public_addr() and will use that address for its public address. The global device address has not been hidden yet since some apps rely on the ability to modify it. Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/ee0f350e Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/ee0f350e Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/ee0f350e Branch: refs/heads/bsnbranch Commit: ee0f350e01b5239521f1d1338f062b8c62f3c9e0 Parents: 631cc3d Author: William San Filippo <[email protected]> Authored: Tue Apr 4 14:00:11 2017 -0700 Committer: Åukasz Rymanowski <[email protected]> Committed: Sat May 20 22:44:47 2017 +0200 ---------------------------------------------------------------------- apps/bleprph/src/main.c | 6 +-- hw/drivers/nimble/nrf51/src/ble_hw.c | 52 ++++++++++++++++++++ hw/drivers/nimble/nrf52/src/ble_hw.c | 52 ++++++++++++++++++++ .../controller/include/controller/ble_hw.h | 6 +++ net/nimble/controller/src/ble_ll.c | 13 +++++ net/nimble/controller/syscfg.yml | 9 ++++ 6 files changed, 135 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ee0f350e/apps/bleprph/src/main.c ---------------------------------------------------------------------- diff --git a/apps/bleprph/src/main.c b/apps/bleprph/src/main.c index 3bdbbcd..1f45fa9 100755 --- a/apps/bleprph/src/main.c +++ b/apps/bleprph/src/main.c @@ -255,12 +255,12 @@ main(void) { int rc; - /* Initialize OS */ - sysinit(); - /* Set initial BLE device address. */ memcpy(g_dev_addr, (uint8_t[6]){0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a}, 6); + /* Initialize OS */ + sysinit(); + /* Initialize the bleprph log. */ log_register("bleprph", &bleprph_log, &log_console_handler, NULL, LOG_SYSLEVEL); http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ee0f350e/hw/drivers/nimble/nrf51/src/ble_hw.c ---------------------------------------------------------------------- diff --git a/hw/drivers/nimble/nrf51/src/ble_hw.c b/hw/drivers/nimble/nrf51/src/ble_hw.c index 9f268cc..c9e6989 100644 --- a/hw/drivers/nimble/nrf51/src/ble_hw.c +++ b/hw/drivers/nimble/nrf51/src/ble_hw.c @@ -56,6 +56,58 @@ uint8_t g_nrf_num_irks; #endif +/* Returns public device address or -1 if not present */ +int +ble_hw_get_public_addr(ble_addr_t *addr) +{ + int rc; + uint32_t addr_high; + uint32_t addr_low; + + /* Does FICR have a public address */ + rc = -1; + if ((NRF_FICR->DEVICEADDRTYPE & 1) == 0) { + addr_low = NRF_FICR->DEVICEADDR[0]; + addr_high = NRF_FICR->DEVICEADDR[1]; + rc = 0; + } else { + /* See if programmed in UICR. Upper 16 bits must all be zero */ + addr_high = NRF_UICR->CUSTOMER[1]; + if (addr_high < 65536) { + addr_low = NRF_UICR->CUSTOMER[0]; + rc = 0; + } + } + + if (!rc) { + /* Copy into device address. We can do this because we know platform */ + memcpy(addr->val, &addr_low, 4); + memcpy(&addr->val[4], &addr_high, 2); + addr->type = BLE_ADDR_PUBLIC; + } + + return rc; +} + +/* Returns random static address or -1 if not present */ +int +ble_hw_get_static_addr(ble_addr_t *addr) +{ + int rc; + + if ((NRF_FICR->DEVICEADDRTYPE & 1) == 1) { + memcpy(addr->val, (void *)&NRF_FICR->DEVICEADDR[0], 4); + memcpy(&addr->val[4], (void *)&NRF_FICR->DEVICEADDR[1], 2); + addr->val[5] |= 0xc0; + addr->type = BLE_ADDR_RANDOM; + rc = 0; + } else { + rc = -1; + } + + return rc; +} + /** * Clear the whitelist * http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ee0f350e/hw/drivers/nimble/nrf52/src/ble_hw.c ---------------------------------------------------------------------- diff --git a/hw/drivers/nimble/nrf52/src/ble_hw.c b/hw/drivers/nimble/nrf52/src/ble_hw.c index ad5af49..dd1323a 100644 --- a/hw/drivers/nimble/nrf52/src/ble_hw.c +++ b/hw/drivers/nimble/nrf52/src/ble_hw.c @@ -58,6 +58,58 @@ uint8_t g_nrf_num_irks; #endif +/* Returns public device address or -1 if not present */ +int +ble_hw_get_public_addr(ble_addr_t *addr) +{ + int rc; + uint32_t addr_high; + uint32_t addr_low; + + /* Does FICR have a public address */ + rc = -1; + if ((NRF_FICR->DEVICEADDRTYPE & 1) == 0) { + addr_low = NRF_FICR->DEVICEADDR[0]; + addr_high = NRF_FICR->DEVICEADDR[1]; + rc = 0; + } else { + /* See if programmed in UICR. Upper 16 bits must all be zero */ + addr_high = NRF_UICR->CUSTOMER[1]; + if (addr_high < 65536) { + addr_low = NRF_UICR->CUSTOMER[0]; + rc = 0; + } + } + + if (!rc) { + /* Copy into device address. We can do this because we know platform */ + memcpy(addr->val, &addr_low, 4); + memcpy(&addr->val[4], &addr_high, 2); + addr->type = BLE_ADDR_PUBLIC; + } + + return rc; +} + +/* Returns random static address or -1 if not present */ +int +ble_hw_get_static_addr(ble_addr_t *addr) +{ + int rc; + + if ((NRF_FICR->DEVICEADDRTYPE & 1) == 1) { + memcpy(addr->val, (void *)&NRF_FICR->DEVICEADDR[0], 4); + memcpy(&addr->val[4], (void *)&NRF_FICR->DEVICEADDR[1], 2); + addr->val[5] |= 0xc0; + addr->type = BLE_ADDR_RANDOM; + rc = 0; + } else { + rc = -1; + } + + return rc; +} + /** * Clear the whitelist * http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ee0f350e/net/nimble/controller/include/controller/ble_hw.h ---------------------------------------------------------------------- diff --git a/net/nimble/controller/include/controller/ble_hw.h b/net/nimble/controller/include/controller/ble_hw.h index 022e2cb..a1941ac 100644 --- a/net/nimble/controller/include/controller/ble_hw.h +++ b/net/nimble/controller/include/controller/ble_hw.h @@ -101,6 +101,12 @@ void ble_hw_resolv_list_disable(void); /* Returns index of resolved address; -1 if not resolved */ int ble_hw_resolv_list_match(void); +/* Returns public device address or -1 if not present */ +int ble_hw_get_public_addr(ble_addr_t *addr); + +/* Returns random static address or -1 if not present */ +int ble_hw_get_static_addr(ble_addr_t *addr); + #ifdef __cplusplus } #endif http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ee0f350e/net/nimble/controller/src/ble_ll.c ---------------------------------------------------------------------- diff --git a/net/nimble/controller/src/ble_ll.c b/net/nimble/controller/src/ble_ll.c index 5db6ba2..6984d34 100644 --- a/net/nimble/controller/src/ble_ll.c +++ b/net/nimble/controller/src/ble_ll.c @@ -1214,11 +1214,24 @@ ble_ll_init(void) { int rc; uint8_t features; + ble_addr_t addr; struct ble_ll_obj *lldata; /* Ensure this function only gets called by sysinit. */ SYSINIT_ASSERT_ACTIVE(); + /* Retrieve the public device address if not set by syscfg */ + memcpy(&addr.val[0], MYNEWT_VAL_BLE_PUBLIC_DEV_ADDR, BLE_DEV_ADDR_LEN); + if (!memcmp(&addr.val[0], ((ble_addr_t *)BLE_ADDR_ANY)->val, + BLE_DEV_ADDR_LEN)) { + rc = ble_hw_get_public_addr(&addr); + if (!rc) { + memcpy(g_dev_addr, &addr.val[0], BLE_DEV_ADDR_LEN); + } + } else { + memcpy(g_dev_addr, &addr.val[0], BLE_DEV_ADDR_LEN); + } + /* Get pointer to global data object */ lldata = &g_ble_ll_data; http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ee0f350e/net/nimble/controller/syscfg.yml ---------------------------------------------------------------------- diff --git a/net/nimble/controller/syscfg.yml b/net/nimble/controller/syscfg.yml index 39c3b44..cfd64d2 100644 --- a/net/nimble/controller/syscfg.yml +++ b/net/nimble/controller/syscfg.yml @@ -216,3 +216,12 @@ syscfg.defs: policy feature. Currently, this feature is not supported by the nimble controller. value: '0' + + BLE_PUBLIC_DEV_ADDR: + description: > + Allows the target or app to override the public device address + used by the controller. If all zero, the controller will + attempt to retrieve the public device address from its + chip specific location. If non-zero, this address will + be used. + value: "(uint8_t[6]){0x00, 0x00, 0x00, 0x00, 0x00, 0x00}"
