Skip to content

Commit

Permalink
feat: add customize ble address api
Browse files Browse the repository at this point in the history
  • Loading branch information
gl-chenxiaosheng committed Jun 19, 2024
1 parent cd5cdaf commit 3fe6ddc
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/bledriver/silabs/silabs_bleapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1223,5 +1223,37 @@ GL_RET silabs_ble_del_gattdb(void)
return GL_UNKNOW_ERR;
}

return GL_SUCCESS;
}

GL_RET silabs_ble_set_identity_address(BLE_MAC address, int address_type)
{
bd_addr addr;
memcpy(addr.addr, address, 6);
sl_status_t status = SL_STATUS_FAIL;

status = sl_bt_system_set_identity_address(addr, address_type);
if (status != SL_STATUS_OK)
{
return GL_UNKNOW_ERR;
}

return GL_SUCCESS;
}

GL_RET silabs_ble_get_identity_address(BLE_MAC address, int *address_type)
{
sl_status_t status = SL_STATUS_FAIL;
uint8_t type = 0;
bzero(address, sizeof(BLE_MAC));

status = sl_bt_system_get_identity_address((bd_addr *)address, &type);
if (status != SL_STATUS_OK)
{
return GL_UNKNOW_ERR;
}

*address_type = type;

return GL_SUCCESS;
}
4 changes: 4 additions & 0 deletions src/bledriver/silabs/silabs_bleapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,8 @@ GL_RET silabs_ble_set_gattdb(char *json_cfg_name);

GL_RET silabs_ble_del_gattdb(void);

GL_RET silabs_ble_set_identity_address(BLE_MAC address, int address_type);

GL_RET silabs_ble_get_identity_address(BLE_MAC address, int *address_type);

#endif
2 changes: 2 additions & 0 deletions src/bledriver/util/gl_methods.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
#define ble_set_notify silabs_ble_set_notify
#define ble_set_gattdb silabs_ble_set_gattdb
// #define ble_del_gattdb silabs_ble_del_gattdb
#define ble_set_identity_address silabs_ble_set_identity_address
#define ble_get_identity_address silabs_ble_get_identity_address

#endif

Expand Down
38 changes: 38 additions & 0 deletions src/example/bletool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,44 @@ bletool >> local_address
#### set_identity_address
```shell
bletool >> set_identity_address 0 94:de:b8:f1:35:10
{ "code": 0 }
```
**Description**:Set the device's Bluetooth identity address.
**Parameters**:
| Type | Name | Default Value | Description |
| ------- | -------- | ------------- | ------------------------------------------------------------ |
| int32_t | mac_type | - | **0:** Public device address <br />**1:** Static device address |
| string | mac | _ | The MAC address of the device |
**Note**: The new address will be effective in the next system reboot. Please use `enable 0` and `enable 1` to reboot the module. To 00:00:00:00:00:00 and ff:ff:ff:ff:ff:ff as invalid addresses, if the address is set to these two will use the default address in the next system reboot.
#### get_identity_address
```shell
bletool >> get_identity_address
{ "code": 0, "mac": "d4:de:b8:f1:2c:03", "mac_type": 1 }
```
**Description**:Get the Bluetooth identity address used by the device, which can be a public or random static device address.
**Parameters**:
| Type | Name | Default Value | Description |
| ------- | -------- | ------------- | ------------------------------------------------------------ |
| string | mac | _ | The MAC address of the device |
| int32_t | mac_type | - | **0:** Public device address <br />**1:** Static device address |
#### <span id="create_adv_handle">create_adv_handle</span>
```shell
Expand Down
62 changes: 62 additions & 0 deletions src/example/bletool/demo_bletool.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,66 @@ GL_RET cmd_local_address(int argc, char **argv)
return GL_SUCCESS;
}

GL_RET cmd_set_identity_address(int argc, char **argv)
{
char *addr = NULL;
int addr_type = -1;
if (argc != 3) {
printf(PARA_MISSING);
return GL_ERR_PARAM_MISSING;
}

addr_type = atoi(argv[1]);
addr = argv[2];

uint8_t addr_len = strlen(addr);
if (addr_len != (BLE_MAC_LEN - 1) || (addr_type != 0 && addr_type != 1))
{
printf(PARA_ERROR);
return GL_ERR_PARAM;
}

BLE_MAC address_u8;
str2addr(addr, address_u8);

GL_RET ret = gl_ble_set_identity_address(address_u8, addr_type);

json_object* o = NULL;
o = json_object_new_object();
json_object_object_add(o,"code",json_object_new_int(ret));
const char *temp = json_object_to_json_string(o);
printf("%s\n",temp);

json_object_put(o);

return GL_SUCCESS;
}

GL_RET cmd_get_identity_address(int argc, char **argv)
{
BLE_MAC address;
char str_addr[20] = {0};
int addr_type = -1;
GL_RET ret = gl_ble_get_identity_address(address, &addr_type);

json_object* o = NULL;
o = json_object_new_object();
json_object_object_add(o,"code",json_object_new_int(ret));
if(ret == GL_SUCCESS)
{
addr2str(address, str_addr);
json_object_object_add(o, "mac", json_object_new_string(str_addr));
json_object_object_add(o, "mac_type", json_object_new_int(addr_type));
}
const char *temp = json_object_to_json_string(o);
printf("%s\n",temp);


json_object_put(o);

return GL_SUCCESS;
}

GL_RET cmd_set_power(int argc, char **argv)
{
int power = 0;
Expand Down Expand Up @@ -1646,6 +1706,8 @@ command_t command_list[] = {
{"enable", cmd_enable, "Enable or disable the module"},
{"set_power", cmd_set_power, "Set the tx power level"},
{"local_address", cmd_local_address, "Get local Bluetooth module public address"},
{"set_identity_address", cmd_set_identity_address, "Set Bluetooth module identity address"},
{"get_identity_address", cmd_get_identity_address, "Get Bluetooth module identity address"},
/*BLE slave functions */
{"show_adv_handle_list", cmd_adv_handle_show, "Show adv handle list"},
{"create_adv_handle", cmd_adv_handle_create, "Create adv handle"},
Expand Down
20 changes: 19 additions & 1 deletion src/lib/gl_bleapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -549,4 +549,22 @@ GL_RET gl_ble_check_module(gl_ble_cbs *callback, bool force_dfu)
// GL_RET gl_ble_del_gattdb(void)
// {
// return ble_del_gattdb();
// }
// }

GL_RET gl_ble_set_identity_address(BLE_MAC address, int address_type)
{
GL_RET ret;
pthread_mutex_lock(&_ble_mutex);
ret = ble_set_identity_address(address, address_type);
pthread_mutex_unlock(&_ble_mutex);
return ret;
}

GL_RET gl_ble_get_identity_address(BLE_MAC address, int *address_type)
{
GL_RET ret;
pthread_mutex_lock(&_ble_mutex);
ret = ble_get_identity_address(address, address_type);
pthread_mutex_unlock(&_ble_mutex);
return ret;
}
34 changes: 34 additions & 0 deletions src/lib/include/gl_bleapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -501,4 +501,38 @@ GL_RET gl_ble_check_module(gl_ble_cbs *callback, bool force_dfu);
// */
// GL_RET gl_ble_del_gattdb(void);

/**
* @brief Set the device's Bluetooth identity address. The address can be a public device address or a static device address.
* A valid address set with this command will be written into persistent storage using NVM keys. The stack returns an
* error if the static device address does not conform to the Bluetooth specification.
* The new address will be effective in the next system reboot. The stack will use the address in the NVM keys when present.
* Otherwise, it uses the default Bluetooth public device address which is programmed at production.
* The stack treats 00:00:00:00:00:00 and ff:ff:ff:ff:ff:ff as invalid addresses. Therefore, passing one of them into this command will
* cause the stack to delete the NVM keys and use the default address in the next system reboot.
*
* @param address : Bluetooth identity address.
*
* @param address_type : Address type
* 0: Public device address
* 1: Static device address
*
* @return GL-RETURN-CODE
*
* @note Because the NVM keys are located in flash and flash wearing can occur, avoid calling this command regularly.
*/
GL_RET gl_ble_set_identity_address(BLE_MAC address, int address_type);

/**
* @brief Read the Bluetooth identity address used by the device, which can be a public or random static device address.
*
* @param address : Bluetooth public address.
*
* @param address_type : Address type
* 0: Public device address
* 1: Static device address
*
* @return GL-RETURN-CODE
*/
GL_RET gl_ble_get_identity_address(BLE_MAC address, int *address_type);

#endif

0 comments on commit 3fe6ddc

Please sign in to comment.