Ver Fonte

iperf: add output format change function for iperf

Xu Si Yu há 3 anos atrás
pai
commit
fcafd8dc27

+ 5 - 0
examples/common_components/iperf/include/iperf.h

@@ -52,6 +52,10 @@ extern "C" {
 #define IPERF_SOCKET_RX_TIMEOUT 10
 #define IPERF_SOCKET_ACCEPT_TIMEOUT 5
 
+typedef enum {
+    MBITS_PER_SEC, KBITS_PER_SEC, BITS_PER_SEC
+} iperf_output_format;
+
 typedef struct {
     uint32_t flag;
     union {
@@ -69,6 +73,7 @@ typedef struct {
     uint32_t time;
     uint16_t len_send_buf;
     int32_t bw_lim;
+    iperf_output_format format;
 } iperf_cfg_t;
 
 esp_err_t iperf_start(iperf_cfg_t *cfg);

+ 8 - 5
examples/common_components/iperf/iperf.c

@@ -76,20 +76,23 @@ static void iperf_report_task(void *arg)
     double average = 0;
     double actual_bandwidth = 0;
     int k = 1;
+    const double coefficient[3] = {1048576.0, 1024.0, 1.0};
+    const char unit[3] = {'M', 'K', '\0'};
+    iperf_output_format format = s_iperf_ctrl.cfg.format;
 
     printf("\n%16s %s\n", "Interval", "Bandwidth");
     while (!s_iperf_ctrl.finish) {
         vTaskDelay(delay_interval);
-        actual_bandwidth = (s_iperf_ctrl.actual_len / 1e6 * 8) / interval;
-        printf("%4d-%4d sec       %.2f Mbits/sec\n", cur, cur + interval,
-            actual_bandwidth);
+        actual_bandwidth = (s_iperf_ctrl.actual_len / coefficient[format] * 8) / interval;
+        printf("%4d-%4d sec       %.2f %cbits/sec\n", cur, cur + interval,
+            actual_bandwidth, unit[format]);
         cur += interval;
         average = ((average * (k - 1) / k) + (actual_bandwidth / k));
         k++;
         s_iperf_ctrl.actual_len = 0;
         if (cur >= time) {
-            printf("%4d-%4d sec       %.2f Mbits/sec\n", 0, time,
-                average);
+            printf("%4d-%4d sec       %.2f %cbits/sec\n", 0, time,
+                average, unit[format]);
             break;
         }
     }