-
Notifications
You must be signed in to change notification settings - Fork 3
/
angle-measure.sh
258 lines (217 loc) · 6.73 KB
/
angle-measure.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/bin/bash
#
### angle-measure.sh
#
# Usage:
#
# angle-measure.sh init-sender channel bandwidth
# e.g. channel=64
# e.g. bandwidth=HT20 (for 20MHz)
#
# angle-measure.sh init-receiver channel bandwidth
# ditto
#
# angle-measure.sh run-sender packets size period
#
# angle-measure.sh run-receiver packets size period
set -x
############################## UTILITIES for managing drivers and interfaces
# helper functions
# mostly so that
# (*) we figure the name of the intel wireless interfaces wlan<n>
# (*) we can wait for that device to be up
# (*) we can gather a complete status of the setup for the record
# http://unix.stackexchange.com/questions/41817/linux-how-to-find-the-device-driver-used-for-a-device/225496#225496?newreg=c865c93607124e70b9f530f2733aba05
function list-interfaces () {
set +x
for f in /sys/class/net/*; do
dev=$(basename $f)
driver=$(readlink $f/device/driver/module)
[ -n "$driver" ] && driver=$(basename $driver)
addr=$(cat $f/address)
operstate=$(cat $f/operstate)
flags=$(cat $f/flags)
printf "%10s [%s]: %10s flags=%6s (%s)\n" "$dev" "$addr" "$driver" "$flags" "$operstate"
done
}
function details-on-interface () {
dev=$1; shift
echo ==================== ip address show $dev
ip addr sh $dev
echo ==================== ip link show $dev
ip link sh $dev
echo ==================== iwconfig $dev
iwconfig $dev
echo ==================== iw dev $dev info
iw dev $dev info
}
# actually returns first interface using a given driver
# prints interface name on stdout
function find-interface-by-driver () {
set +x
search_driver=$1; shift
for f in /sys/class/net/*; do
_if=$(basename $f)
driver=$(readlink $f/device/driver/module)
[ -n "$driver" ] && driver=$(basename $driver)
if [ "$driver" == "$search_driver" ]; then
echo $_if
return
fi
done
}
# wait for one interface to show up using this driver
# prints interface name on stdout
function wait-for-interface-on-driver() {
driver=$1; shift
while true; do
# use the first device that runs on iwlwifi
_found=$(find-interface-by-driver $driver)
if [ -n "$_found" ]; then
>&2 echo Using wlan device $_found
echo $_found
return
else
>&2 echo "Waiting for some interface to run on driver $driver"; sleep 1
fi
done
}
# wait for device dev to be in state wait_state
function wait-for-device () {
set +x
dev=$1; shift
wait_state="$1"; shift
while true; do
f=/sys/class/net/$dev
operstate=$(cat $f/operstate 2> /dev/null)
if [ "$operstate" == "$wait_state" ]; then
2>& echo Device $dev is $wait_state
break
else
>&2 echo "Device $dev is $operstate - waiting 1s"; sleep 1
fi
done
}
############################## successive steps of the experiment
function init-sender() {
### 2 arguments are required
channel=$1; shift # e.g. 64
bandwidth=$1; shift # e.g. HT20
# unload any wireless driver
# useful when the experiment is restarted
modprobe -r iwlwifi mac80211 cfg80211
# load our driver
modprobe iwlwifi debug=0x40000
# list-interfaces
wlan=$(wait-for-interface-on-driver iwlwifi)
# create the monitor interface
iw dev $wlan interface add mon0 type monitor
# bring it up
ip link set dev mon0 up
# init monitor interface
iw mon0 set channel $channel $bandwidth
### define the number of Space time streams
# and the number of Antenna for transmission
txs=$(find /sys -name monitor_tx_rate)
for tx in $txs; do
echo tweaking $tx
echo 0x4101 > $tx
done
details-on-interface $wlan
details-on-interface mon0
return 0
}
function init-receiver() {
### 2 arguments are required
channel=$1; shift # e.g. 64
bandwidth=$1; shift # e.g. HT20
modprobe -r iwlwifi mac80211 cfg80211
modprobe iwlwifi connector_log=0x1
wlan=$(wait-for-interface-on-driver iwlwifi)
while true; do
iwconfig $wlan mode monitor && break # >&/dev/null
sleep 1
done
# turn on
ip link set $wlan up
# set on same channel
iw $wlan set channel $channel $bandwidth
details-on-interface $wlan
return 0
}
# the image that we use contains the csitool packages installed here
# root@fit04:~# ls -l /root
# total 8
# drwxr-xr-x 24 root root 4096 Jan 8 11:55 linux-80211n-csitool
# drwxr-xr-x 9 root root 4096 Jan 25 21:29 linux-80211n-csitool-supplementary
function run-sender () {
# 2 arguments are required
packets=$1; shift
size=$1; shift
period=$1; shift
echo "Sending $packets packets $size-long with period $period us"
echo $(date) - begin traffic
set -x
# random_packets uses the mon0 interface which is hard-wired in the binary
/root/linux-80211n-csitool-supplementary/injection/random_packets $packets $size 1 $period
# which means:
# * send $packets packets
# * of $size bytes each
# * 1: on the injection MAC
# * each $period microseconds
set +x
echo $(date) - end traffic
# unload driver to be 100% sure we will be silent
echo unloading intel drivers
modprobe -r iwlwifi mac80211 cfg80211
return 0
}
# echo everything on stderr so we can just redirect stdout
# so stdout receives the output of log_to_file
function run-receiver () {
# 3 arguments are required
packets=$1; shift
size=$1; shift
period=$1; shift
### estimate experiment duration
# in theory overall duration is $packets * $period
# but in fact the time for sending a packet must be included as well
# and with small periods this is substantially different
# we work around this problem by using a minimal period of 1ms per packet
minimum=1000
# compute min of period and minimum
actual_period=$(( $period <= $minimum ? $minimum : $period))
duration=$(( $packets * $actual_period / 1000000))
safety=10
# then add another extra delay for safety
total=$(( $duration + $safety))
echo "Recording CSI data for $total = $duration + $safety seconds"
# for forensics
echo $(date) - begin
# start a job that logs indefinitely the csi data into file rawdata
/root/linux-80211n-csitool-supplementary/netlink/log_to_file rawdata &
# record its pid
pid=$!
# wait for the estimated duration
sleep $duration
echo "Waiting extra safety ${safety}s"
sleep $safety
# kill the recording job
echo "it's time to leave after ${total}s"
kill $pid
# for forensics
echo $(date) - end
md5sum rawdata
return 0
}
########################################
# just a wrapper around the individual functions
function main() {
command=$1; shift
case $command in
init-sender|init-receiver|run-sender|run-receiver)
$command "$@" ;;
*) echo unknown command \"$command\" ;;
esac
}
main "$@"