2424#include "memfault/metrics/platform/timer.h"
2525#include "memfault/ports/zephyr/version.h"
2626
27+ #define BT_LE_INTERVAL_UNIT_US (1250U)
28+
2729static struct bt_conn * s_mflt_bt_current_conn = NULL ;
28- static uint16_t s_mflt_bt_connection_interval = 0 ;
30+ static uint32_t s_mflt_bt_connection_interval_us = 0 ;
2931struct k_work_delayable s_mflt_bt_delayed_metrics_work ;
3032
3133static void prv_record_gatt_mtu (struct bt_conn * conn ) {
3234 uint16_t mtu = bt_gatt_get_mtu (conn );
3335 MEMFAULT_METRIC_SET_UNSIGNED (bt_gatt_mtu_size , mtu );
3436}
3537
36- static void prv_count_connection_events (uint16_t interval , bool reset_time ) {
38+ static void prv_count_connection_events (uint32_t interval_us , bool reset_time ) {
3739 // to accumulate data correctly on:
3840 // - connection interval change
3941 // - connection up/down
@@ -47,10 +49,9 @@ static void prv_count_connection_events(uint16_t interval, bool reset_time) {
4749 return ;
4850 }
4951
50- if (interval ) {
51- // connection interval is in units of 1.25ms
52- // calculate events per second: 1000ms / (interval * 1.25ms)
53- int32_t events_per_second = 800 / interval ; // 1000 / 1.25 = 800
52+ if (interval_us ) {
53+ // calculate events per second: 1000000us / interval_us
54+ int32_t events_per_second = 1000000 / interval_us ;
5455
5556 // compute connection events accumulated
5657 const uint64_t current_time_ms = memfault_platform_get_time_since_boot_ms ();
@@ -138,13 +139,17 @@ static void prv_delayed_metrics_work_handler(struct k_work *work) {
138139static void prv_record_connection_params (struct bt_conn * conn ) {
139140 struct bt_conn_info info ;
140141 if (bt_conn_get_info (conn , & info ) == 0 ) {
141- MEMFAULT_METRIC_SET_UNSIGNED (bt_connection_interval , info .le .interval );
142+ MEMFAULT_METRIC_SET_UNSIGNED (bt_connection_interval_us , info .le .interval_us );
142143 MEMFAULT_METRIC_SET_UNSIGNED (bt_connection_latency , info .le .latency );
143144 MEMFAULT_METRIC_SET_UNSIGNED (bt_connection_timeout , info .le .timeout );
144- s_mflt_bt_connection_interval = info .le .interval ;
145+ #if MEMFAULT_ZEPHYR_VERSION_GT (4 , 3 ) || MEMFAULT_NCS_VERSION_GT (4 , 2 )
146+ s_mflt_bt_connection_interval_us = info .le .interval_us ;
147+ #else
148+ s_mflt_bt_connection_interval_us = info .le .interval * BT_LE_INTERVAL_UNIT_US ;
149+ #endif
145150 } else {
146151 MEMFAULT_LOG_ERROR ("Failed to get connection info" );
147- s_mflt_bt_connection_interval = 0 ;
152+ s_mflt_bt_connection_interval_us = 0 ;
148153 }
149154}
150155
@@ -172,8 +177,8 @@ static void prv_bt_connected_cb(struct bt_conn *conn, uint8_t err) {
172177
173178static void prv_bt_disconnected_cb (struct bt_conn * conn , uint8_t reason ) {
174179 // tally connection events
175- prv_count_connection_events (s_mflt_bt_connection_interval , false);
176- s_mflt_bt_connection_interval = 0 ;
180+ prv_count_connection_events (s_mflt_bt_connection_interval_us , false);
181+ s_mflt_bt_connection_interval_us = 0 ;
177182
178183 if (s_mflt_bt_current_conn == conn ) {
179184 bt_conn_unref (s_mflt_bt_current_conn );
@@ -203,16 +208,18 @@ static void prv_record_remote_info_cb(struct bt_conn *conn,
203208
204209static void prv_bt_le_param_updated_cb (struct bt_conn * conn , uint16_t interval , uint16_t latency ,
205210 uint16_t timeout ) {
211+ uint32_t interval_us = interval * BT_LE_INTERVAL_UNIT_US ;
212+
206213 // Record LE connection parameters
207- MEMFAULT_METRIC_SET_UNSIGNED (bt_connection_interval , interval );
214+ MEMFAULT_METRIC_SET_UNSIGNED (bt_connection_interval_us , interval_us );
208215 MEMFAULT_METRIC_SET_UNSIGNED (bt_connection_latency , latency );
209216 MEMFAULT_METRIC_SET_UNSIGNED (bt_connection_timeout , timeout );
210217
211218 // Tally connection event counts received with the previous interval setting
212- prv_count_connection_events (s_mflt_bt_connection_interval , false);
219+ prv_count_connection_events (s_mflt_bt_connection_interval_us , false);
213220
214221 // Update connection interval for computing connection event count
215- s_mflt_bt_connection_interval = interval ;
222+ s_mflt_bt_connection_interval_us = interval_us ;
216223}
217224
218225BT_CONN_CB_DEFINE (bt_metrics_conn_callbacks ) = {
@@ -238,7 +245,7 @@ static struct bt_gatt_cb prv_gatt_callbacks = {
238245void memfault_bluetooth_metrics_heartbeat_update (void ) {
239246 if (s_mflt_bt_current_conn != NULL ) {
240247 // Update connection event count estimate
241- prv_count_connection_events (s_mflt_bt_connection_interval , false);
248+ prv_count_connection_events (s_mflt_bt_connection_interval_us , false);
242249
243250 // Record current RSSI (if available)
244251 prv_record_connection_rssi (s_mflt_bt_current_conn );
0 commit comments