|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | + zbnt/python-client |
| 4 | + Copyright (C) 2021 Oscar R. |
| 5 | +
|
| 6 | + This program is free software: you can redistribute it and/or modify |
| 7 | + it under the terms of the GNU General Public License as published by |
| 8 | + the Free Software Foundation, either version 3 of the License, or |
| 9 | + (at your option) any later version. |
| 10 | +
|
| 11 | + This program is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + GNU General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU General Public License |
| 17 | + along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | +""" |
| 19 | + |
| 20 | +import asyncio |
| 21 | +import os |
| 22 | +from zbnt import ZbntClient, StatsCollector, LatencyMeasurer, TrafficGenerator, Devices, Properties, discover_devices |
| 23 | + |
| 24 | +if not os.path.exists("results"): |
| 25 | + os.mkdir("results") |
| 26 | + |
| 27 | +files = [ |
| 28 | + open(f"results/{f}.csv", "w") for f in [ |
| 29 | + "latency", |
| 30 | + "traffic_p0", |
| 31 | + "traffic_p1", |
| 32 | + "traffic_p2", |
| 33 | + "traffic_p3" |
| 34 | + ] |
| 35 | +] |
| 36 | + |
| 37 | +def handle_measurement(device, measurement): |
| 38 | + measurement_csv = ",".join(map(str, measurement.__dict__.values())) |
| 39 | + |
| 40 | + if isinstance(device, LatencyMeasurer): |
| 41 | + files[0].write(measurement_csv) |
| 42 | + files[0].write("\n") |
| 43 | + elif isinstance(device, StatsCollector): |
| 44 | + files[device.ports[0] + 1].write(measurement_csv) |
| 45 | + files[device.ports[0] + 1].write("\n") |
| 46 | + |
| 47 | +async def main(): |
| 48 | + devices = await discover_devices(2) |
| 49 | + |
| 50 | + if len(devices) == 0: |
| 51 | + print("Error: No devices found") |
| 52 | + exit(1) |
| 53 | + |
| 54 | + if len(devices) > 1: |
| 55 | + print("Available devices:\n") |
| 56 | + |
| 57 | + for i in range(len(devices)): |
| 58 | + print("{0}) {1} ({2})".format(i + 1, devices[i]["name"], devices[i]["address"])) |
| 59 | + |
| 60 | + req_dev = int(input("\nSelect a device [1-{0}]: ".format(len(devices)))) - 1 |
| 61 | + |
| 62 | + if req_dev < 0 or req_dev >= len(devices): |
| 63 | + print("Error: Invalid device selected") |
| 64 | + exit(1) |
| 65 | + |
| 66 | + print("") |
| 67 | + else: |
| 68 | + req_dev = 0 |
| 69 | + |
| 70 | + req_dev = devices[req_dev] |
| 71 | + |
| 72 | + if not req_dev["local"]: |
| 73 | + print("- Connecting to {0} ({1})".format(req_dev["name"], req_dev["address"])) |
| 74 | + else: |
| 75 | + print("- Connecting to {0} (PID {1})".format(req_dev["name"], req_dev["pid"])) |
| 76 | + |
| 77 | + client = await ZbntClient.connect(req_dev) |
| 78 | + |
| 79 | + if client == None: |
| 80 | + print("Error: Failed to connect to device") |
| 81 | + exit(1) |
| 82 | + |
| 83 | + print("- Loading bitstream") |
| 84 | + await client.load_bitstream("dual_tgen_latency") |
| 85 | + |
| 86 | + # Get handle to the timer, eth0/1 traffic generators and latency measurer |
| 87 | + |
| 88 | + timer = client.get_device(Devices.DEV_SIMPLE_TIMER) |
| 89 | + tgen0 = client.get_device(Devices.DEV_TRAFFIC_GENERATOR, {0}) |
| 90 | + tgen1 = client.get_device(Devices.DEV_TRAFFIC_GENERATOR, {1}) |
| 91 | + lm = client.get_device(Devices.DEV_LATENCY_MEASURER, {2, 3}) |
| 92 | + sc0 = client.get_device(Devices.DEV_STATS_COLLECTOR, {0}) |
| 93 | + sc1 = client.get_device(Devices.DEV_STATS_COLLECTOR, {1}) |
| 94 | + sc2 = client.get_device(Devices.DEV_STATS_COLLECTOR, {2}) |
| 95 | + sc3 = client.get_device(Devices.DEV_STATS_COLLECTOR, {3}) |
| 96 | + |
| 97 | + # Tell the server to start the run, this will initialize the DMA core |
| 98 | + |
| 99 | + await client.start_run() |
| 100 | + await asyncio.sleep(4) |
| 101 | + |
| 102 | + # Configure devices |
| 103 | + |
| 104 | + tg0_template, tg0_source = TrafficGenerator.load_frame_template("udp_broadcast.hex") |
| 105 | + tg1_template, tg1_source = TrafficGenerator.load_frame_template("udp_unicast.hex") |
| 106 | + |
| 107 | + await lm.set_property(Properties.PROP_MAC_ADDR, "9A:B1:2B:CF:93:02", {"index": 0}) |
| 108 | + await lm.set_property(Properties.PROP_MAC_ADDR, "9A:B1:2B:CF:93:03", {"index": 1}) |
| 109 | + await lm.set_property(Properties.PROP_IP_ADDR, "192.168.111.102", {"index": 0}) |
| 110 | + await lm.set_property(Properties.PROP_IP_ADDR, "192.168.111.103", {"index": 1}) |
| 111 | + await lm.set_property(Properties.PROP_FRAME_PADDING, 82) |
| 112 | + await lm.set_property(Properties.PROP_FRAME_GAP, 6250000 - 82) |
| 113 | + await lm.set_property(Properties.PROP_ENABLE_LOG, True) |
| 114 | + await lm.set_property(Properties.PROP_ENABLE, True) |
| 115 | + |
| 116 | + await tgen0.set_property(Properties.PROP_FRAME_TEMPLATE, tg0_template) |
| 117 | + await tgen0.set_property(Properties.PROP_FRAME_SOURCE, tg0_source) |
| 118 | + await tgen0.set_property(Properties.PROP_FRAME_SIZE, 1500) |
| 119 | + await tgen0.set_property(Properties.PROP_FRAME_GAP, 1200000) |
| 120 | + await tgen0.set_property(Properties.PROP_ENABLE, True) |
| 121 | + |
| 122 | + await tgen1.set_property(Properties.PROP_FRAME_TEMPLATE, tg1_template) |
| 123 | + await tgen1.set_property(Properties.PROP_FRAME_SOURCE, tg1_source) |
| 124 | + await tgen1.set_property(Properties.PROP_FRAME_SIZE, 1500) |
| 125 | + await tgen1.set_property(Properties.PROP_FRAME_GAP, 1200000) |
| 126 | + await tgen1.set_property(Properties.PROP_ENABLE, True) |
| 127 | + |
| 128 | + for d in [sc0, sc1, sc2, sc3]: |
| 129 | + await d.set_property(Properties.PROP_SAMPLE_PERIOD, timer.freq // 10) |
| 130 | + await d.set_property(Properties.PROP_ENABLE_LOG, True) |
| 131 | + await d.set_property(Properties.PROP_ENABLE, True) |
| 132 | + |
| 133 | + await timer.set_property(Properties.PROP_TIMER_LIMIT, 10 * timer.freq) |
| 134 | + |
| 135 | + # Register measurement handler |
| 136 | + |
| 137 | + client.set_callback(handle_measurement) |
| 138 | + |
| 139 | + # Start the timer |
| 140 | + |
| 141 | + await timer.set_property(Properties.PROP_ENABLE, True) |
| 142 | + |
| 143 | + # Increase data rate every 2 seconds |
| 144 | + |
| 145 | + frame_gap = 1200000 |
| 146 | + |
| 147 | + for i in range(4): |
| 148 | + await asyncio.sleep(2) |
| 149 | + |
| 150 | + frame_gap //= 10 |
| 151 | + |
| 152 | + await tgen0.set_property(Properties.PROP_FRAME_GAP, frame_gap) |
| 153 | + |
| 154 | + # Wait until all data has been sent and the server stops the run |
| 155 | + |
| 156 | + await client.wait_for_run_end() |
| 157 | + |
| 158 | + # Close output files |
| 159 | + |
| 160 | + for f in files: |
| 161 | + f.close() |
| 162 | + |
| 163 | +asyncio.run(main()) |
0 commit comments