@@ -404,9 +404,60 @@ static int cmd_write(const struct shell *shell, size_t argc, char *argv[])
404404	return  err ;
405405}
406406
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+ 
407454static  void  write_without_rsp_cb (struct  bt_conn  * conn , void  * user_data )
408455{
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 ();
410461}
411462
412463static  int  cmd_write_without_rsp (const  struct  shell  * shell ,
@@ -428,6 +479,7 @@ static int cmd_write_without_rsp(const struct shell *shell,
428479	if  (!sign ) {
429480		if  (!strcmp (argv [0 ], "write-without-response-cb" )) {
430481			func  =  write_without_rsp_cb ;
482+ 			reset_write_stats ();
431483		}
432484	}
433485
@@ -458,10 +510,14 @@ static int cmd_write_without_rsp(const struct shell *shell,
458510	while  (repeat -- ) {
459511		err  =  bt_gatt_write_without_response_cb (default_conn , handle ,
460512							gatt_write_buf , len ,
461- 							sign , func , NULL );
513+ 							sign , func ,
514+ 							UINT_TO_POINTER (len ));
462515		if  (err ) {
463516			break ;
464517		}
518+ 
519+ 		k_yield ();
520+ 
465521	}
466522
467523	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,
844900				 value_len );
845901}
846902
847- static  uint32_t  write_count ;
848- static  uint32_t  write_len ;
849- static  uint32_t  write_rate ;
850- 
851903static  ssize_t  write_met (struct  bt_conn  * conn , const  struct  bt_gatt_attr  * attr ,
852904			 const  void  * buf , uint16_t  len , uint16_t  offset ,
853905			 uint8_t  flags )
854906{
855907	uint8_t  * value  =  attr -> user_data ;
856- 	static  uint32_t  cycle_stamp ;
857- 	uint32_t  delta ;
858908
859909	if  (offset  +  len  >  sizeof (met_char_value )) {
860910		return  BT_GATT_ERR (BT_ATT_ERR_INVALID_OFFSET );
861911	}
862912
863913	memcpy (value  +  offset , buf , len );
864914
865- 	delta  =  k_cycle_get_32 () -  cycle_stamp ;
866- 	delta  =  (uint32_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  =  ((uint64_t )write_len  << 3 ) *  1000000000U  / delta ;
880- 	}
915+ 	update_write_stats (len );
881916
882917	return  len ;
883918}
@@ -898,10 +933,8 @@ static int cmd_metrics(const struct shell *shell, size_t argc, char *argv[])
898933	int  err  =  0 ;
899934
900935	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 ;
905938	}
906939
907940	if  (!strcmp (argv [1 ], "on" )) {
@@ -1063,9 +1096,7 @@ SHELL_STATIC_SUBCMD_SET_CREATE(gatt_cmds,
10631096	SHELL_CMD_ARG (set , NULL , "<handle> [data...]" , cmd_set , 2 , 255 ),
10641097	SHELL_CMD_ARG (show - db , NULL , "[uuid] [num_matches]" , cmd_show_db , 1 , 2 ),
10651098#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 ),
10691100	SHELL_CMD_ARG (register , NULL ,
10701101		      "register pre-predefined test service" ,
10711102		      cmd_register_test_svc , 1 , 0 ),
0 commit comments