13  Review questions and further exercises

13.1 Review questions

Q1

When you tried to put “Flags + a 128-bit service UUID + a 20-character device name” in the advertisement, ble_gap_adv_set_fields() returned an error. Give the reason and two fixes.

Legacy advertising data is at most 31 bytes, and Flags (3) + 128-bit UUID (2 + 16 = 18) + name (2 + 20 = 22) = 43 bytes, which exceeds it.

Fixes: (1) move the name to the scan response (the approach from Step 3 on); (2) use a shortened name (0x08 Shortened Local Name). Another option is BLE 5 extended advertising (up to 1650 bytes), but older phones may not be able to receive it. (Chapters 2 and 5)

Q2

What must the client be doing for Notify to be sent? And with which event can you learn of it in NimBLE?

The client must have subscribed by writing 01 00 to that characteristic’s CCCD (0x2902). In NimBLE you learn of it from cur_notify of BLE_GAP_EVENT_SUBSCRIBE. (Chapters 3 and 6)

Q3

What can happen if you call vTaskDelay(pdMS_TO_TICKS(2000)) inside gap_event_cb()?

GAP callbacks are called inside NimBLE’s host task, so all host processing stops for 2 seconds. During that time, incoming ATT requests cannot be answered, pairing exchanges do not progress, and pending packets are not processed, which can cause timeouts on the peer (30 s for ATT, also 30 s for pairing) and delayed app responses. (Chapters 4 and 10)

Q4

With the ESP32-C3’s IO Capability as DisplayOnly and the phone as KeyboardDisplay, what is the pairing method? What happens if you change the ESP32-C3 to NoInputNoOutput? In that case, can characteristics with the *_AUTHEN flags be accessed?

DisplayOnly × KeyboardDisplay is Passkey Entry (the ESP32-C3 displays, the phone enters). With NoInputNoOutput it becomes Just Works and the key is “unauthenticated”, so access to characteristics with *_AUTHEN is rejected with Insufficient Authentication. (Chapters 8 and 9)

Q5

Explain the difference in resistance to eavesdropping between Passkey Entry in LE Legacy Pairing and Passkey Entry in LE Secure Connections.

In LE Legacy, a temporary key (TK) is made from the passkey and the keys are derived from it, so if the pairing exchange is sniffed, the passkey (at most 1 million possibilities) can be brute-forced to recover the key. In LE Secure Connections, keys are shared via ECDH (elliptic-curve Diffie–Hellman), so sniffing the exchange alone does not reveal the key. The passkey is used to authenticate that the ECDH public keys have not been swapped by a man in the middle. (Chapter 8)

Q6

What problem occurs if you remove BLE_GATT_CHR_F_NOTIFY_INDICATE_ENC / _AUTHEN from Step 5’s characteristics?

Writes to the CCCD are allowed without encryption, so even an unpaired peer can subscribe to Notify and receive the counter and command responses. This is because READ_ENC and the like only apply to the value attribute, and the CCCD is a separate attribute. (Chapters 8 and 9)

Q7

In Step 6, the Write callback (gatt_access_cb()) does not send a response but only posts an event. Give two advantages of this design.

  1. The host task returns immediately, so BLE processing doesn’t stop even if building the response takes time
  2. The meaning of a command (what to do) can be decided by the state transition table, so the same command can behave differently depending on the state, and you can check the overview in a table

“The state is changed only in one place, the event loop, so there are no races from several tasks rewriting shared variables” is also a correct answer. (Chapter 10)

13.2 Further exercises

13.2.1 Exercise A: changing state with a button

Connect a tactile switch to a GPIO and create a driver (app_button.c) that posts EVT_BUTTON_PRESSED from the button interrupt.

  • Use gpio_install_isr_service() and gpio_isr_handler_add(). Inside the interrupt handler, only call enqueue() (the FromISR version is used via xPortInIsrContext())
  • When the button is pressed while connected, Notify 10 01 on the Command characteristic
  • When the button is pressed while not connected, do nothing (set the table cell to NULL)
  • To handle contact bounce, ignore interrupts within 50 ms of the previous one

13.2.2 Exercise B: reliable delivery with Indicate

Button events should not be missed, so add a characteristic that sends via Indicate.

  • Add BLE_GATT_CHR_F_INDICATE to the flags and send with ble_gatts_indicate_custom()
  • With Indicate, you cannot send the next one until the confirmation returns. Check the result via indication and status of BLE_GAP_EVENT_NOTIFY_TX, and build a mechanism that sends the next one only after the confirmation returns, using an event (EVT_BLE_INDICATE_DONE) and states

13.2.3 Exercise C: random passkey and LCD display

  • Generate a 6-digit passkey with esp_random() for every pairing
  • Display the passkey on a small I²C OLED (SSD1306 etc.). Do the display in a handler (the driver only posts EVT_PAIRING_PASSKEY)
  • Enable CONFIG_BT_NIMBLE_SM_SC_ONLY to reject pairing via LE Legacy

13.2.4 Exercise D: power saving

  • If 5 minutes pass while not connected, lengthen the advertising interval to 1 second (add an IDLE_SLOW state)
  • If another 30 minutes pass, stop advertising and enter Deep Sleep, waking with the button
  • Measure how much the current consumption while connected drops with CONFIG_PM_ENABLE and the BLE controller’s modem sleep (on the ESP32-C3, CONFIG_BT_CTRL_MODEM_SLEEP etc.; see sdkconfig.defaults.esp32c3 of ESP-IDF’s power_save example)