tile_bdaddr.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * NOTICE
  3. *
  4. * Copyright 2020 Tile Inc. All Rights Reserved.
  5. * All code or other information included in the accompanying files ("Tile Source Material")
  6. * is PROPRIETARY information of Tile Inc. ("Tile") and access and use of the Tile Source Material
  7. * is subject to these terms. The Tile Source Material may only be used for demonstration purposes,
  8. * and may not be otherwise distributed or made available to others, including for commercial purposes.
  9. * Without limiting the foregoing , you understand and agree that no production use
  10. * of the Tile Source Material is allowed without a Tile ID properly obtained under a separate
  11. * agreement with Tile.
  12. * You also understand and agree that Tile may terminate the limited rights granted under these terms
  13. * at any time in its discretion.
  14. * All Tile Source Material is provided AS-IS without warranty of any kind.
  15. * Tile does not warrant that the Tile Source Material will be error-free or fit for your purposes.
  16. * Tile will not be liable for any damages resulting from your use of or inability to use
  17. * the Tile Source Material.
  18. *
  19. * Support: firmware_support@tile.com
  20. */
  21. /**
  22. * @file tile_bdaddr.c
  23. ** @brief
  24. ** Use part of TileId, modified to make it Public Random Mac Addr compatible, is used as BdAddr
  25. ** FYI on BLE Mac Address Types : https://devzone.nordicsemi.com/f/nordic-q-a/27012/how-to-distinguish-between-random-and-public-gap-addresses
  26. */
  27. /*******************************************************************************
  28. * Includes
  29. ******************************************************************************/
  30. #include "sdk_common.h"
  31. #if NRF_MODULE_ENABLED(TILE_SUPPORT)
  32. #include "tile_bdaddr.h"
  33. #include "tile_assert/tile_assert.h"
  34. #include "tile_storage/tile_storage.h"
  35. #include "tile_config.h"
  36. #include "tile_tmd_module.h"
  37. #include "tile_toa_module.h"
  38. #include "nrf_log.h"
  39. #include "ble_gap.h"
  40. /*******************************************************************************
  41. * Macro Definitions
  42. ******************************************************************************/
  43. /*******************************************************************************
  44. * Global variables
  45. ******************************************************************************/
  46. /* @brief Should contain currently used MacAddr value
  47. * Used to assign to member of tdi_module
  48. */
  49. uint8_t bdaddr[BLE_GAP_ADDR_LEN];
  50. /* @brief Used when switching from Act->Manu or Shipping->Manu before a reboot
  51. * Contains default MacAddr value provided by Nordic FICR register
  52. * No need to save in Flash
  53. */
  54. uint8_t default_bdaddr[BLE_GAP_ADDR_LEN];
  55. /***********************************************************************
  56. * Local functions
  57. ***********************************************************************/
  58. /***********************************************************************
  59. * Global functions
  60. ***********************************************************************/
  61. /**
  62. * @brief Update destination bdaddr from source bdaddr array
  63. */
  64. void update_default_bdaddr(uint8_t* dest_bdaddr, uint8_t* source_bdaddr)
  65. {
  66. /* Select new Addr Value */
  67. for(uint8_t i=0; i<BLE_GAP_ADDR_LEN; i++)
  68. {
  69. dest_bdaddr[i] = source_bdaddr[BLE_GAP_ADDR_LEN-1-i];
  70. }
  71. }
  72. /**
  73. * @brief Update MacAddr value from first 6 bytes of TileId
  74. * We need to use Nordic Set API to update the value used in advertising
  75. */
  76. void set_tileid_macAddr(void)
  77. {
  78. NRF_LOG_INFO("set_tileid_macAddr\n");
  79. /********** Update MacAddr used while advertising using Nordic API ************/
  80. ble_gap_addr_t addr;
  81. /* Select new Addr Type */
  82. addr.addr_type = BLE_GAP_ADDR_TYPE_RANDOM_STATIC;
  83. /* Select new Addr Value from Tile Id */
  84. update_default_bdaddr(addr.addr, tile_checked->tile_id);
  85. /* Make it RANDOM STATIC to match addr_type, by setting first 2 bits high, else set function will return error */
  86. addr.addr[BLE_GAP_ADDR_LEN-1] |= 0xC0;
  87. /* Need to set the updated Mac Addr using API, so that ble_advdata_encode() can use the updated value
  88. * sd_ble_gap_addr_get() will now start returning updated value, till a power cycle
  89. * At boot, sd_ble_gap_addr_get() will return default value again
  90. */
  91. (void) sd_ble_gap_addr_set(&addr);
  92. /************ Update Internal bdaddr value, used by tdi module *************/
  93. update_default_bdaddr(bdaddr, addr.addr);
  94. /****************************************************************************/
  95. }
  96. /**
  97. * @brief Obtain default Mac Address from FICR register
  98. */
  99. void get_default_macAddr(void)
  100. {
  101. /* Obtain Default MacAddr from FICR register */
  102. ble_gap_addr_t addr;
  103. (void) sd_ble_gap_addr_get(&addr);
  104. /* Store value in global variable */
  105. update_default_bdaddr(default_bdaddr, addr.addr);
  106. /* Update Internal bdaddr value, used by tdi module */
  107. for(uint8_t i=0; i<BLE_GAP_ADDR_LEN; i++)
  108. {
  109. bdaddr[i] = default_bdaddr[i];
  110. }
  111. }
  112. /**
  113. * @brief Set the Mac Address to be used internally by tdi module and to be used for Advertising
  114. * Select this based on the MacAddress Mechanism configured for that Product, and based on the Tile mode
  115. */
  116. void set_new_macAddr(void)
  117. {
  118. set_tileid_macAddr();
  119. }
  120. #endif // NRF_MODULE_ENABLED(TILE_SUPPORT)