aboutsummaryrefslogtreecommitdiffstats
path: root/package
diff options
context:
space:
mode:
authorSungbo Eo <mans0n@gorani.run>2020-01-25 23:06:07 +0900
committerPetr Štetiar <ynezz@true.cz>2020-07-08 23:22:30 +0200
commitdc61e3b7ffdc2e3873af238a9100d44ee685578c (patch)
tree6934345a5301be10b013361bf967eb7fd7a2f7fb /package
parentaf9932c9b7b94417d46ba59439793514e93ba8a6 (diff)
downloadupstream-dc61e3b7ffdc2e3873af238a9100d44ee685578c.tar.gz
upstream-dc61e3b7ffdc2e3873af238a9100d44ee685578c.tar.bz2
upstream-dc61e3b7ffdc2e3873af238a9100d44ee685578c.zip
base-files: add functions to set or clear bit in MAC address
Some devices (e.g. Arduino Yun) need bitwise operations during MAC address setup. This commit adds generalized versions of macaddr_setbit_la(), which are helpful when manipulating a single bit in a MAC address. Signed-off-by: Sungbo Eo <mans0n@gorani.run>
Diffstat (limited to 'package')
-rw-r--r--package/base-files/files/lib/functions/system.sh20
1 files changed, 18 insertions, 2 deletions
diff --git a/package/base-files/files/lib/functions/system.sh b/package/base-files/files/lib/functions/system.sh
index c7aa7bf75c..f3c901ff79 100644
--- a/package/base-files/files/lib/functions/system.sh
+++ b/package/base-files/files/lib/functions/system.sh
@@ -152,10 +152,26 @@ macaddr_geteui() {
echo ${mac:9:2}$sep${mac:12:2}$sep${mac:15:2}
}
-macaddr_setbit_la() {
+macaddr_setbit() {
+ local mac=$1
+ local bit=${2:-0}
+
+ [ $bit -gt 0 -a $bit -le 48 ] || return
+
+ printf "%012x" $(( 0x${mac//:/} | 2**(48-bit) )) | sed -e 's/\(.\{2\}\)/\1:/g' -e 's/:$//'
+}
+
+macaddr_unsetbit() {
local mac=$1
+ local bit=${2:-0}
+
+ [ $bit -gt 0 -a $bit -le 48 ] || return
- printf "%02x:%s" $((0x${mac%%:*} | 0x02)) ${mac#*:}
+ printf "%012x" $(( 0x${mac//:/} & ~(2**(48-bit)) )) | sed -e 's/\(.\{2\}\)/\1:/g' -e 's/:$//'
+}
+
+macaddr_setbit_la() {
+ macaddr_setbit $1 7
}
macaddr_2bin() {