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,18 @@ 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 (defined(MEMFAULT_ZEPHYR_VERSION_GT ) && MEMFAULT_ZEPHYR_VERSION_GT (4 , 3 )) || \
146+ (defined(MEMFAULT_NCS_VERSION_GT ) && MEMFAULT_NCS_VERSION_GT (3 , 1 ))
147+ s_mflt_bt_connection_interval_us = info .le .interval_us ;
148+ #else
149+ s_mflt_bt_connection_interval_us = info .le .interval * BT_LE_INTERVAL_UNIT_US ;
150+ #endif
145151 } else {
146152 MEMFAULT_LOG_ERROR ("Failed to get connection info" );
147- s_mflt_bt_connection_interval = 0 ;
153+ s_mflt_bt_connection_interval_us = 0 ;
148154 }
149155}
150156
@@ -172,8 +178,8 @@ static void prv_bt_connected_cb(struct bt_conn *conn, uint8_t err) {
172178
173179static void prv_bt_disconnected_cb (struct bt_conn * conn , uint8_t reason ) {
174180 // tally connection events
175- prv_count_connection_events (s_mflt_bt_connection_interval , false);
176- s_mflt_bt_connection_interval = 0 ;
181+ prv_count_connection_events (s_mflt_bt_connection_interval_us , false);
182+ s_mflt_bt_connection_interval_us = 0 ;
177183
178184 if (s_mflt_bt_current_conn == conn ) {
179185 bt_conn_unref (s_mflt_bt_current_conn );
@@ -203,16 +209,18 @@ static void prv_record_remote_info_cb(struct bt_conn *conn,
203209
204210static void prv_bt_le_param_updated_cb (struct bt_conn * conn , uint16_t interval , uint16_t latency ,
205211 uint16_t timeout ) {
212+ uint32_t interval_us = interval * BT_LE_INTERVAL_UNIT_US ;
213+
206214 // Record LE connection parameters
207- MEMFAULT_METRIC_SET_UNSIGNED (bt_connection_interval , interval );
215+ MEMFAULT_METRIC_SET_UNSIGNED (bt_connection_interval_us , interval_us );
208216 MEMFAULT_METRIC_SET_UNSIGNED (bt_connection_latency , latency );
209217 MEMFAULT_METRIC_SET_UNSIGNED (bt_connection_timeout , timeout );
210218
211219 // Tally connection event counts received with the previous interval setting
212- prv_count_connection_events (s_mflt_bt_connection_interval , false);
220+ prv_count_connection_events (s_mflt_bt_connection_interval_us , false);
213221
214222 // Update connection interval for computing connection event count
215- s_mflt_bt_connection_interval = interval ;
223+ s_mflt_bt_connection_interval_us = interval_us ;
216224}
217225
218226BT_CONN_CB_DEFINE (bt_metrics_conn_callbacks ) = {
@@ -238,7 +246,7 @@ static struct bt_gatt_cb prv_gatt_callbacks = {
238246void memfault_bluetooth_metrics_heartbeat_update (void ) {
239247 if (s_mflt_bt_current_conn != NULL ) {
240248 // Update connection event count estimate
241- prv_count_connection_events (s_mflt_bt_connection_interval , false);
249+ prv_count_connection_events (s_mflt_bt_connection_interval_us , false);
242250
243251 // Record current RSSI (if available)
244252 prv_record_connection_rssi (s_mflt_bt_current_conn );
0 commit comments