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>
This commit is contained in:
parent
af9932c9b7
commit
dc61e3b7ff
1 changed files with 18 additions and 2 deletions
|
@ -152,10 +152,26 @@ macaddr_geteui() {
|
||||||
echo ${mac:9:2}$sep${mac:12:2}$sep${mac:15:2}
|
echo ${mac:9:2}$sep${mac:12:2}$sep${mac:15:2}
|
||||||
}
|
}
|
||||||
|
|
||||||
macaddr_setbit_la() {
|
macaddr_setbit() {
|
||||||
local mac=$1
|
local mac=$1
|
||||||
|
local bit=${2:-0}
|
||||||
|
|
||||||
printf "%02x:%s" $((0x${mac%%:*} | 0x02)) ${mac#*:}
|
[ $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 "%012x" $(( 0x${mac//:/} & ~(2**(48-bit)) )) | sed -e 's/\(.\{2\}\)/\1:/g' -e 's/:$//'
|
||||||
|
}
|
||||||
|
|
||||||
|
macaddr_setbit_la() {
|
||||||
|
macaddr_setbit $1 7
|
||||||
}
|
}
|
||||||
|
|
||||||
macaddr_2bin() {
|
macaddr_2bin() {
|
||||||
|
|
Loading…
Reference in a new issue