use available constants instead of testing errors for length

This commit is contained in:
Neradoc 2022-07-02 22:34:42 +02:00
parent 50a9beda04
commit 5ca6d8953d
2 changed files with 7 additions and 10 deletions

View File

@ -106,6 +106,9 @@ void common_hal_bleio_adapter_set_enabled(bleio_adapter_obj_t *self, bool enable
char ble_name[32]; char ble_name[32];
name_len = dotenv_get_key("/.env", "CIRCUITPY_BLE_NAME", ble_name, sizeof(ble_name) - 1); name_len = dotenv_get_key("/.env", "CIRCUITPY_BLE_NAME", ble_name, sizeof(ble_name) - 1);
if (name_len > 0) { if (name_len > 0) {
if (name_len > MYNEWT_VAL_BLE_SVC_GAP_DEVICE_NAME_MAX_LENGTH) {
name_len = MYNEWT_VAL_BLE_SVC_GAP_DEVICE_NAME_MAX_LENGTH;
}
ble_name[name_len] = '\0'; ble_name[name_len] = '\0';
ble_svc_gap_device_name_set(ble_name); ble_svc_gap_device_name_set(ble_name);
} else { } else {

View File

@ -464,17 +464,11 @@ void common_hal_bleio_adapter_set_name(bleio_adapter_obj_t *self, const char *na
ble_gap_conn_sec_mode_t sec; ble_gap_conn_sec_mode_t sec;
sec.lv = 0; sec.lv = 0;
sec.sm = 0; sec.sm = 0;
int result; uint16_t len = strlen(name);
result = sd_ble_gap_device_name_set(&sec, (const uint8_t *)name, strlen(name)); if (len > BLE_GAP_DEVNAME_MAX_LEN) {
for (int name_len = strlen(name); name_len > 0; --name_len) { len = BLE_GAP_DEVNAME_MAX_LEN;
result = sd_ble_gap_device_name_set(&sec, (const uint8_t *)name, name_len);
// expecting NRF_ERROR_DATA_SIZE when name too long
if (result == NRF_SUCCESS) {
return;
}
} }
// default back to default if all fails sd_ble_gap_device_name_set(&sec, (const uint8_t *)name, len);
sd_ble_gap_device_name_set(&sec, (const uint8_t *)default_ble_name, sizeof(default_ble_name) - 1);
} }
STATIC uint32_t _update_identities(bool is_central) { STATIC uint32_t _update_identities(bool is_central) {