Hi!
I don't have a problem, I just have a doubt about the "on_connect" function. I opened the UART example from:
nRF5_SDK_17.1.0_ddde560\examples\ble_peripheral\ble_app_uart\pca10040\s132\ses
In this project, it's used the "on_connect" function from the "ble_conn_params.c", which is a common code found here:
nRF5_SDK_17.1.0_ddde560\components\ble\common
Here is the code:
static void on_connect(ble_evt_t const * p_ble_evt) { uint8_t role = p_ble_evt->evt.gap_evt.params.connected.role; uint16_t conn_handle = p_ble_evt->evt.gap_evt.conn_handle; if (role != BLE_GAP_ROLE_PERIPH) { return; } ble_conn_params_instance_t * p_instance = instance_get(BLE_CONN_HANDLE_INVALID); if (p_instance == NULL) { send_error_evt(NRF_ERROR_NO_MEM); return; } instance_claim(p_instance, conn_handle); p_instance->params_ok = is_conn_params_ok(&p_instance->preferred_conn_params, &p_ble_evt->evt.gap_evt.params.connected.conn_params, NRF_BLE_CONN_PARAMS_MAX_SLAVE_LATENCY_DEVIATION, NRF_BLE_CONN_PARAMS_MAX_SUPERVISION_TIMEOUT_DEVIATION); // Check if we shall handle negotiation on connect if (m_conn_params_config.start_on_notify_cccd_handle == BLE_GATT_HANDLE_INVALID) { conn_params_negotiation(conn_handle, p_instance); } }
The problem I see is simplified here:
uint16_t conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
conn_params_negotiation(conn_handle, p_instance);
And finally, inside the "conn_params_negotiation":
err_code = app_timer_start(p_instance->timer_id, timeout_ticks, (void *)(uint32_t)conn_handle);
The first thing I don't understand is why "conn_handle" is treated as an address, since I would expect something like "(void *)(uint32_t)(&conn_handle))". But even if the address was passed as a pointer for the p_context of the timer, the variable will disappear from the stack once the function is exited.
What it would be logical to me is that the address of the "p_ble_evt->evt.gap_evt.conn_handle" is passed to the app_timer_start as the p_context so that it can be recovered back in the timer callback handler.
The code works, but I don't know if it works because the variable is not overwritten in the stack due to a good luck, or if the code is correctly coded and I am missing something...
Thanks!