Developing lightweight computation at the DSG edge

qmp_common.sh 10.2 KB
Newer Older
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
#!/bin/sh
SOURCE_COMMON=1
#DEBUG="/tmp/qmp_common.debug"

#######################
# UCI related commands
#######################

qmp_uci_get() {
	u="$(uci -q get qmp.$1)"
	r=$?
	echo "$u"
	[ $r -ne 0 ] && logger -t qMp "UCI returned an error (uci get qmp.$1)"
	qmp_debug "qmp_uci_get: uci -q get qmp.$1"
	return $r
}

qmp_uci_get_raw() {
	u="$(uci -q get $@)"
	r=$?
	echo "$u"
	[ $r -ne 0 ] && logger -t qMp "UCI returned an error (uci get $@)"
	qmp_debug "qmp_uci_get_raw: uci -q get $@"
	return $r
}

qmp_uci_set() {
28
29
30
	section="$1"
	shift
	uci -q set qmp.$section="$@" > /dev/null
31
32
33
34
35
36
37
38
39
	r=$?
	uci commit
	r=$(( $r + $? ))
	[ $r -ne 0 ] && logger -t qMp "UCI returned an error (uci set qmp.$1=$2)"
	qmp_debug "qmp_uci_set: uci -q set qmp.$1=$2"
	return $r
}

qmp_uci_set_raw() {
p4u's avatar
p4u committed
40
41
42
	local a=$(echo "$@" | cut -d= -f1)
	local b=$(echo "$@" | cut -d= -f2)
	[ -n "$b" ] && uci -q set $a="$b" || uci -q set $@ 2>/dev/null
43
44
45
46
47
	r=$?
	uci commit
	r=$(( $r + $? ))
	[ $r -ne 0 ] && logger -t qMp "UCI returned an error (uci set $@)"
	qmp_debug "qmp_uci_set_raw: uci -q set $@"
p4u's avatar
p4u committed
48
	return $r
49
50
51
}

qmp_uci_del() {
p4u's avatar
p4u committed
52
	uci -q delete qmp.$1
53
54
55
56
57
58
59
60
61
	r=$?
	uci commit
	r=$(( $r + $? ))
	[ $r -ne 0 ] && logger -t qMp "UCI returned an error (uci del qmp.$1)"
	qmp_debug "qmp_uci_del: uci -q del qmp.$1"
	return $r
}

qmp_uci_del_raw() {
p4u's avatar
p4u committed
62
        uci -q delete $@
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
	r=$?
	uci commit
	r=$(( $r + $? ))
	[ $r -ne 0 ] && logger -t qMp "UCI returned an error (uci del $@)"
	qmp_debug "qmp_uci_del_raw uci -q del $@"
	return $r
}

qmp_uci_add() {
	uci -q add qmp $1 > /dev/null
	r=$?
	uci commit
	r=$(( $r + $? ))
	[ $r -ne 0 ] && logger -t qMp "UCI returned an error (uci add qmp $1)"
	qmp_debug "qmp_uci_add: uci -q add qmp $1"
	return $r
}

qmp_uci_add_raw_get_cfg() {
	cfg=$(uci -q add $@)
	r=$?
	[ $r -ne 0 ] && logger -t qMp "UCI returned an error (uci add $@)"
	echo "$cfg"
	qmp_debug "qmp_uci_add_raw_get_cfg: uci -q add $@"
	return $r
}

qmp_uci_set_cfg() {
	uci -q set $@ >/dev/null
	qmp_debug "qmp_uci_set_cfg: uci -q set $@"
	return $?
}

qmp_uci_commit() {
	uci commit $1
	r=$(( $r + $? ))
	[ $r -ne 0 ] && logger -t qMp "UCI returned an error (uci commit $1)"
	qmp_debug "qmp_uci_commit: uci commit $1"
	return $r
}

qmp_uci_add_raw() {
	uci -q add $@ > /dev/null
	r=$?
	uci commit
	r=$(( $r + $? ))
	[ $r -ne 0 ] && logger -t qMp "UCI returned an error (uci add $@)"
	qmp_debug "qmp_uci_add_raw: uci -q add $@"
	return $r
}

qmp_uci_add_list_raw() {
	uci -q add_list $@ > /dev/null
	r=$?
	uci commit
	r=$(( $r + $? ))
	[ $r -ne 0 ] && logger -t qMp "UCI returned an error (uci add_list $@)"
	qmp_debug "qmp_uci_add_list_raw: uci -q add_list $@"
	return $r
}

qmp_uci_import() {
	cat "$1" | while read v; do
	[ ! -z "$v" ] && { uci set $v; qmp_debug "qmp_uci_import: uci set $v"; }
	done
	uci commit
	return $?
}

qmp_uci_test() {
p4u's avatar
p4u committed
133
	u="$(uci -q get $@ 2>&1)"
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
	r=$?
	return $r
}

##################################
# Log and errors related commnads
##################################

# Exit from execution and shows an error
# qmp_error The device is burning
qmp_error() {
	logger -s -t qMp "ERROR: $@"
	exit 1
}

# Send info to system log
# qmp_log qMp is the best
qmp_log() {
	logger -s -t qMp "$@"
}

qmp_debug() {
	[ ! -z "$DEBUG" ] &&  echo "$@" >> $DEBUG
}

#######################################
# Networking and Wifi related commands
#######################################

163
# Returns the names of the wifi devices in the system, one per line
164
qmp_get_wifi_devices() {
165

166
167
168
169
	# Legacy code, sometimes not reporting all the interfaces as they take some time to appear there
	local proc_net_wireless=$(awk 'NR>2 { gsub(/:$/,"",$1); print $1 }' /proc/net/wireless | grep -v -e "wlan[0-9]-[0-9]" | sort -u)
	# Initial fix for #481, but some devices would still fail. Gather devices from /sys/class/net
	local sys_class_net
170
171
172
	for i in $(ls /sys/class/net/); do
			[ -e /sys/class/net/${i}/phy80211 ] && sys_class_net="${sys_class_net} ${i}"
	done
173
174
175
176
	# Actual fix for #481, using ubus
	local ubus_network_wireless=$(ubus call network.wireless status | jsonfilter -e '@.*.interfaces.*.ifname')
	# Last, fetch those devices in /etc/config/wireless
	local uci_show_wireless=$(uci show wireless | grep wireless\..*\.ifname | cut -d '=' -f 2 | tr -d "'")
177

178
179
180
181
182
	# Print the devices gotten from /proc/net/wireless
	for i in $proc_net_wireless; do
		echo $i
	done
	# Print the remaining devices gotten from /sys/class/net
183
	for i in $sys_class_net; do
184
		! qmp_is_in $i $proc_net_wireless && echo "$i"
185
	done
186
	# Print the remaining devices gotten from ubus
187
	for i in $ubus_network_wireless; do
188
189
190
191
192
		! qmp_is_in $i $proc_net_wireless && ! qmp_is_in $i $sys_class_net && echo "$i"
	done
	# Print the remaining devices gotten from /etc/configc/wireless
	for i in $uci_show_wireless; do
		! qmp_is_in $i $proc_net_wireless && ! qmp_is_in $i $sys_class_net && ! qmp_is_in $i $ubus_network_wireless && echo "$i"
193
	done
194
195
196
197
198
199
200
201


	# Actual fix for #481, using ubus
	ubus_network_wireless = $(ubus call network.wireless status | jsonfilter -e '@.*.interfaces.*.section')

	for i in $ubus_network_wireless; do
		! qmp_is_in $i $proc_net_wireless && ! qmp_is_in $i $sys_class_net && echo $i
	done
202
203
}

204

205
# Returns the MAC address of the wifi devices
206
# (get MAC addres of the physical wifi device, if exists)
207
qmp_get_wifi_mac_devices() {
208
209
210
211
212
213
214
215
216
	for device in $(qmp_get_wifi_devices)
	do
		if [ -s "/sys/class/net/$device/phy80211/macaddress" ]
		then
			cat "/sys/class/net/$device/phy80211/macaddress"
		else
			qmp_get_mac_for_dev $device
		fi
	done
217
218
219
220
221
}

# Returns the device name that corresponds to the MAC address
# qmp_get_dev_from_mac 00:22:11:33:44:55
qmp_get_dev_from_mac() {
222
	echo "$(ip link | grep $1 -i -B1 | grep -v \@ | egrep -v "ether|mon" | grep mtu | awk '{print $2}' | tr -d : | awk NR==1)"
223
224
225
226
227
}

# Returns the mac address of the device
# qmp_get_mac_for_dev eth0
qmp_get_mac_for_dev() {
228
	mac="$(cat /sys/class/net/$1/address)"
229
230
231
232
	[ -z "$mac" ] && mac="00:00:00:00:00:00"
	echo "$mac"
}

233
234
235
# Returns the mac addres for specific device,, only wifi devs are allowed. Useful when eth and wlan have same MAC
# qmp_get_dev_from_wifi_mac 00:22:11:33:44:55
qmp_get_dev_from_wifi_mac() {
p4u's avatar
p4u committed
236
	for device in $(qmp_get_wifi_devices | sort)
237
238
239
240
241
242
243
	do
		if grep -q -i "^$1$" "/sys/class/net/$device/phy80211/macaddress" "/sys/class/net/$device/address" 2> /dev/null
		then
			echo $device
			return
		fi
	done
244
245
}

p4u's avatar
p4u committed
246
247
248
249
250
251
#########################
# Hooks related functions
#########################

qmp_hooks_exec() {
    local stage="$1"
252
	local device="none"
253

254
255
	[ -e /tmp/sysinfo/board_name ] && device="$(cat /tmp/sysinfo/board_name 2>/dev/null)" \
		|| device=$(cat /proc/cpuinfo | grep vendor_id | cut -d: -f2 | tr -d ' ')
256

p4u's avatar
p4u committed
257
258
259
260
261
262
263
264
265
266
267
    [ -z "$stage" -o -z "$device" ] && return 1
    local hooksdir="/etc/qmp/hooks/$device"

    [ -d "$hooksdir" ] && {
    for h in $hooksdir/*; do
        echo "Executing hook $h in stage $stage"
        sh $h $stage
    done
    }
}

268
#########################
269
# ID/IP commands
270
271
#########################

272
# Returns the crc16 from the mac of the primary mac device
p4u's avatar
p4u committed
273
274
275
276
277
278
279
280
281
282
# If no parameter it returns the entire hash
# If parameter = 1 or 2, returns the first/second 8bit module 256
qmp_get_crc16() {
	local mac="$(qmp_get_mac_for_dev $(qmp_uci_get node.primary_device))"
	local crc16="$(lua /usr/bin/crc16 $mac)"
	[ "$1" == "1" ] && echo -e "$crc16" | awk NR==2 && return
	[ "$1" == "2" ] && echo -e "$crc16" | awk NR==3 && return
	echo -e "$crc16" | awk NR==1
}

283
# qmp_get_id [8bit]
284
qmp_get_id() {
285
286
287
288
289
  local device_id="$(qmp_uci_get node.device_id)"
	device_id="$(echo -n $device_id | tr -cd 'ABCDEFabcdef0123456789' | tail -c 4)"
  [ -z "$device_id" ] && \
    device_id="$(qmp_get_crc16)"
  [ "$1" == "8bit" ] && echo "$(( 0x$device_id % 0x100 ))" || echo "$device_id"
290
291
292
293
294
295
296
297
}

# qmp_get_id_ip <1,2>
qmp_get_id_ip() {
  [ "$1" == "1" ] && echo "$(qmp_get_crc16 1)"
  [ "$1" == "2" ] && echo "$(qmp_get_crc16 2)"
}

298
299
300
301
302
303
# qmp_get_id_hostname
# returns the two last bytes of the primary MAC address
qmp_get_id_hostname() {
	local mac="$(qmp_get_mac_for_dev $(qmp_uci_get node.primary_device))"
	echo "$mac" | awk -F: '{print tolower($5)tolower($6)}'
}
304
305
306
307
308

#########################
# Other kind of commands
#########################

309
310
311
312
313
314
315
316
317
318
319
# Print the content of the parameters in reverse order (separed by spaces)
qmp_reverse_order() {
	echo "$@" | awk '{for (i=NF; i>0; i--) printf("%s ",$i);print ""}'
}

# Print the output of the command parameter in reverse order (separed by lines)
qmp_tac() {
	$@ | awk '{a[NR]=$0} END {for(i=NR;i>0;i--)print a[i]}'
}

qmp_get_dec_node_id() {
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
	qmp_log "Getting decimal node id"

	local DEVICE_ID=""
	local PRIMARY_DEVICE=""

	if qmp_uci_test qmp.node.device_id; then
		qmp_log "Getting device_id from qMp config"
		DEVICE_ID="$(uci get qmp.node.device_id)"
		qmp_log "Device id: ${DEVICE_ID}"
	fi

	if [ -z "$DEVICE_ID" ] && qmp_uci_test qmp.node.primary_device; then
		qmp_log "Getting primary_device from qMp config"
		local QMPCONFIG_PRIMARY_DEVICE="$(uci get qmp.node.primary_device)"
		if [ -e "/sys/class/net/$QMPCONFIG_PRIMARY_DEVICE" ]; then
			PRIMARY_DEVICE=$QMPCONFIG_PRIMARY_DEVICE
			qmp_log "Primary device found: ${PRIMARY_DEVICE}"
		else
			qmp_log "Primary device not found: ${QMPCONFIG_PRIMARY_DEVICE}"
		fi
	fi
	if [ -z "$DEVICE_ID" ] && [ -z "$PRIMARY_DEVICE" ] && \
	qmp_uci_test qmp.interfaces.mesh_devices; then
		qmp_log "Getting first mesh device from qMp config"
		qmp_log "Found mesh devices: $(uci get qmp.interfaces.mesh_devices)"
		local PRIMARY_DEVICE="$(uci get qmp.interfaces.mesh_devices | awk '{print $1}')"
		qmp_log "Mesh primary device: $PRIMARY_DEVICE"
	fi

	if [ -n "$PRIMARY_DEVICE" ]; then
		qmp_log "Getting MAC for device $PRIMARY_DEVICE"
		LSB_PRIM_MAC="$( qmp_get_mac_for_dev $PRIMARY_DEVICE | awk -F':' '{print $6}' )"
		qmp_log "Primary device LSBs: $LSB_PRIM_MAC"
		if [ -n $LSB_PRIM_MAC ]; then
			DEVICE_ID=${LSB_PRIM_MAC}
		fi
	fi

  if [ -n "$DEVICE_ID" ]; then
		qmp_log "Decimal node id: $(printf %d 0x$DEVICE_ID)"
		echo "$(printf %d 0x$DEVICE_ID)"
	else
		qmp_log "Decimal node id not found, returning 0xABCD."
			echo "$(printf %d 0xABCD)"
	fi
365
366
367
368
369
370
371
372
373
374
375
376
}

# Returns the prefix /XX from netmask
qmp_get_prefix_from_netmask() {
 echo "$(ipcalc.sh 1.1.1.1 $1| grep PREFIX | cut -d= -f2)"
}

# Returns the netid from IP NETMASK
qmp_get_netid_from_network() {
 echo "$(ipcalc.sh $1 $2 | grep NETWORK | cut -d= -f2)"
}

p4u's avatar
p4u committed
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# TO-EXPLAIN
qmp_is_in()
{
	local search="$1"
	shift
	local item
	for item in $@
	do
		if [ "$search" == "$item" ]
		then
			return 0
		fi
	done
	return 1
}