@@ -404,9 +404,60 @@ static int cmd_write(const struct shell *shell, size_t argc, char *argv[])
404
404
return err ;
405
405
}
406
406
407
+ static struct write_stats {
408
+ uint32_t count ;
409
+ uint32_t len ;
410
+ uint32_t total ;
411
+ uint32_t rate ;
412
+ } write_stats ;
413
+
414
+ static void update_write_stats (uint16_t len )
415
+ {
416
+ static uint32_t cycle_stamp ;
417
+ uint32_t delta ;
418
+
419
+ delta = k_cycle_get_32 () - cycle_stamp ;
420
+ delta = (uint32_t )k_cyc_to_ns_floor64 (delta );
421
+
422
+ if (!delta ) {
423
+ delta = 1 ;
424
+ }
425
+
426
+ write_stats .count ++ ;
427
+ write_stats .total += len ;
428
+
429
+ /* if last data rx-ed was greater than 1 second in the past,
430
+ * reset the metrics.
431
+ */
432
+ if (delta > 1000000000 ) {
433
+ write_stats .len = 0U ;
434
+ write_stats .rate = 0U ;
435
+ cycle_stamp = k_cycle_get_32 ();
436
+ } else {
437
+ write_stats .len += len ;
438
+ write_stats .rate = ((uint64_t )write_stats .len << 3 ) *
439
+ 1000000000U / delta ;
440
+ }
441
+ }
442
+
443
+ static void reset_write_stats (void )
444
+ {
445
+ memset (& write_stats , 0 , sizeof (write_stats ));
446
+ }
447
+
448
+ static void print_write_stats (void )
449
+ {
450
+ shell_print (ctx_shell , "Write #%u: %u bytes (%u bps)" ,
451
+ write_stats .count , write_stats .total , write_stats .rate );
452
+ }
453
+
407
454
static void write_without_rsp_cb (struct bt_conn * conn , void * user_data )
408
455
{
409
- shell_print (ctx_shell , "Write transmission complete" );
456
+ uint16_t len = POINTER_TO_UINT (user_data );
457
+
458
+ update_write_stats (len );
459
+
460
+ print_write_stats ();
410
461
}
411
462
412
463
static int cmd_write_without_rsp (const struct shell * shell ,
@@ -428,6 +479,7 @@ static int cmd_write_without_rsp(const struct shell *shell,
428
479
if (!sign ) {
429
480
if (!strcmp (argv [0 ], "write-without-response-cb" )) {
430
481
func = write_without_rsp_cb ;
482
+ reset_write_stats ();
431
483
}
432
484
}
433
485
@@ -458,10 +510,14 @@ static int cmd_write_without_rsp(const struct shell *shell,
458
510
while (repeat -- ) {
459
511
err = bt_gatt_write_without_response_cb (default_conn , handle ,
460
512
gatt_write_buf , len ,
461
- sign , func , NULL );
513
+ sign , func ,
514
+ UINT_TO_POINTER (len ));
462
515
if (err ) {
463
516
break ;
464
517
}
518
+
519
+ k_yield ();
520
+
465
521
}
466
522
467
523
shell_print (shell , "Write Complete (err %d)" , err );
@@ -844,40 +900,19 @@ static ssize_t read_met(struct bt_conn *conn, const struct bt_gatt_attr *attr,
844
900
value_len );
845
901
}
846
902
847
- static u32_t write_count ;
848
- static u32_t write_len ;
849
- static u32_t write_rate ;
850
-
851
903
static ssize_t write_met (struct bt_conn * conn , const struct bt_gatt_attr * attr ,
852
904
const void * buf , u16_t len , u16_t offset ,
853
905
u8_t flags )
854
906
{
855
907
u8_t * value = attr -> user_data ;
856
- static u32_t cycle_stamp ;
857
- u32_t delta ;
858
908
859
909
if (offset + len > sizeof (met_char_value )) {
860
910
return BT_GATT_ERR (BT_ATT_ERR_INVALID_OFFSET );
861
911
}
862
912
863
913
memcpy (value + offset , buf , len );
864
914
865
- delta = k_cycle_get_32 () - cycle_stamp ;
866
- delta = (u32_t )k_cyc_to_ns_floor64 (delta );
867
-
868
- /* if last data rx-ed was greater than 1 second in the past,
869
- * reset the metrics.
870
- */
871
- if (delta > 1000000000 ) {
872
- write_count = 0U ;
873
- write_len = 0U ;
874
- write_rate = 0U ;
875
- cycle_stamp = k_cycle_get_32 ();
876
- } else {
877
- write_count ++ ;
878
- write_len += len ;
879
- write_rate = ((u64_t )write_len << 3 ) * 1000000000U / delta ;
880
- }
915
+ update_write_stats (len );
881
916
882
917
return len ;
883
918
}
@@ -898,10 +933,8 @@ static int cmd_metrics(const struct shell *shell, size_t argc, char *argv[])
898
933
int err = 0 ;
899
934
900
935
if (argc < 2 ) {
901
- shell_print (shell , "Write: count= %u, len= %u, rate= %u bps." ,
902
- write_count , write_len , write_rate );
903
-
904
- return - ENOEXEC ;
936
+ print_write_stats ();
937
+ return 0 ;
905
938
}
906
939
907
940
if (!strcmp (argv [1 ], "on" )) {
@@ -1063,9 +1096,7 @@ SHELL_STATIC_SUBCMD_SET_CREATE(gatt_cmds,
1063
1096
SHELL_CMD_ARG (set , NULL , "<handle> [data...]" , cmd_set , 2 , 255 ),
1064
1097
SHELL_CMD_ARG (show - db , NULL , "[uuid] [num_matches]" , cmd_show_db , 1 , 2 ),
1065
1098
#if defined(CONFIG_BT_GATT_DYNAMIC_DB )
1066
- SHELL_CMD_ARG (metrics , NULL ,
1067
- "register vendr char and measure rx <value: on, off>" ,
1068
- cmd_metrics , 2 , 0 ),
1099
+ SHELL_CMD_ARG (metrics , NULL , "[value: on, off]" , cmd_metrics , 1 , 1 ),
1069
1100
SHELL_CMD_ARG (register , NULL ,
1070
1101
"register pre-predefined test service" ,
1071
1102
cmd_register_test_svc , 1 , 0 ),
0 commit comments