obuspc: added dhcp opt125 for agent on-boarding

This commit is contained in:
Suvendhu Hansa 2023-08-09 05:40:35 +00:00 committed by Vivek Kumar Dutta
parent 8651b12aa6
commit 406c8ef73c
4 changed files with 280 additions and 5 deletions

View file

@ -5,13 +5,13 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=obuspc
PKG_VERSION:=1.0.1.1
PKG_VERSION:=1.0.1.2
LOCAL_DEV:=0
ifneq ($(LOCAL_DEV),1)
PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://dev.iopsys.eu/bbf/obuspa-test-controller.git
PKG_SOURCE_VERSION:=f1f721bc1a4feaf63c7f7837eb7b0c86111e2f71
PKG_SOURCE_VERSION:=2ae29195817e75ada7c030a03662e72dbaae8e77
PKG_MAINTAINER:=Vivek Dutta <vivek.dutta@iopsys.eu>
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)-$(PKG_SOURCE_VERSION).tar.gz
PKG_MIRROR_HASH:=skip
@ -66,6 +66,7 @@ define Package/obuspc/install
$(INSTALL_BIN) $(PKG_BUILD_DIR)/obuspc $(1)/usr/sbin/
$(INSTALL_BIN) ./files/etc/init.d/obuspc $(1)/etc/init.d/
$(INSTALL_BIN) ./files/etc/uci-defaults/99-fix-agent-endpoint $(1)/etc/uci-defaults/
$(INSTALL_BIN) ./files/etc/uci-defaults/100-add-mosquitto-listener $(1)/etc/uci-defaults/
$(INSTALL_DATA) ./files/etc/config/obuspc $(1)/etc/config/
endef

View file

@ -6,13 +6,13 @@ config obuspc 'global'
config mqtt 'mqtt'
option BrokerAddress '127.0.0.1'
option BrokerPort '1883'
option BrokerPort '9006'
option TransportProtocol 'TCP/IP'
config controller 'controller'
option EndpointID 'proto::interop-usp-controller'
option EndpointID 'proto::discovery-usp-controller'
option Protocol 'MQTT'
option ResponseTopicConfigured '/usp/controller'
option ResponseTopicConfigured '/usp/discovery/controller'
option mqtt 'mqtt'
config agent 'agent'

View file

@ -14,6 +14,7 @@ BASEPATH=""
INSTANCE_COUNT=0
. /usr/share/libubox/jshn.sh
. /lib/functions.sh
global_init()
{
@ -78,6 +79,261 @@ get_refrence_path()
echo "${path}"
}
convert_to_hex() {
local val=""
local optval="${1}"
OPTIND=1
while getopts ":" opt "-$optval"
do
temp=$(printf "%02X" "'${OPTARG:-:}")
val="${val}:${temp}"
done
echo "${val}"
}
check_for_suboptions() {
new_opt=""
# Check if option 25 and 29 present inside enterprise id 3561
data=$(echo "${1}" | sed 's/://g')
len=$(printf "${data}"|wc -c)
rem_len="${len}"
while [ $rem_len -gt 8 ]; do
subopt_present=0
ent_id="${data:0:8}"
ent_id=$(printf "%d\n" "0x$ent_id")
if [ $ent_id -ne 3561 ]; then
len_val=${data:8:2}
data_len=$(printf "%d\n" "0x$len_val")
# add 4 byte for ent_id and 1 byte for len
data_len=$(( data_len * 2 + 10 ))
opt=${data:0:"${data_len}"}
if [ -n "${new_opt}" ]; then
new_opt="${new_opt}:$(echo ${opt} | sed 's/../&:/g;s/:$//')"
else
new_opt="$(echo ${opt} | sed 's/../&:/g;s/:$//')"
fi
# move ahead data to next enterprise id
data=${data:"${data_len}":"${rem_len}"}
rem_len=$(( rem_len - data_len ))
continue
fi
# read the length of enterprise data
len_val=${data:8:2}
data_len=$(printf "%d\n" "0x$len_val")
# add 4 byte for ent_id and 1 byte for len
data_len=$(( data_len * 2 + 10 ))
len_val=${data:8:2}
opt_len=$(printf "%d\n" "0x$len_val")
if [ $opt_len -eq 0 ]; then
echo "${new_opt}"
return 0
fi
# populate the option data of enterprise id
sub_data_len=$(( opt_len * 2))
# starting 10 means ahead of length field
sub_data=${data:10:"${sub_data_len}"}
# parsing of suboption of option 125
while [ $sub_data_len -gt 0 ]; do
# get the suboption id
sub_opt_id=${sub_data:0:2}
sub_opt_id=$(printf "%d\n" "0x$sub_opt_id")
case "${sub_opt_id}" in
"25") subopt_present=1
;;
"26") subopt_present=1
;;
"27") subopt_present=1
;;
"28") subopt_present=1
;;
"29") subopt_present=1
;;
esac
if [ $subopt_present -eq 1 ]; then
break;
fi
# get the length of suboption
sub_opt_len=${sub_data:2:2}
sub_opt_len=$(printf "%d\n" "0x$sub_opt_len")
sub_opt_len=$(( sub_opt_len * 2 ))
# add 2 bytes for sub_opt id and sub_opt len field
sub_opt_end=$(( sub_opt_len + 4 ))
# update the remaining sub option hex string length
sub_data_len=$((sub_data_len - sub_opt_end))
# fetch next sub option hex string
sub_data=${sub_data:${sub_opt_end}:${sub_data_len}}
done
if [ $subopt_present -eq 1 ]; then
# move ahead data to next enterprise id
rem_len=$(( rem_len - $data_len ))
data=${data:"${data_len}":"${rem_len}"}
else
opt=${data:0:"${data_len}"}
if [ -n "${new_opt}" ]; then
new_opt="${new_opt}:$(echo ${opt} | sed 's/../&:/g;s/:$//')"
else
new_opt="$(echo ${opt} | sed 's/../&:/g;s/:$//')"
fi
# move ahead data to next enterprise id
rem_len=$(( rem_len - $data_len ))
data=${data:"${data_len}":"${rem_len}"}
fi
done
echo "${new_opt}"
}
configure_dnsmasq_op125() {
intf="${1}"
endpoint=""
proto=""
address=""
port=""
topic=""
prov_code="obusp-client"
interval="5"
multiplier="2000"
config_load ${CONFIGURATION}
config_get endpoint controller EndpointID
config_get proto controller Protocol
if [ -z "${endpoint}" ] || [ -z "${proto}" ]; then
return 0
fi
if [ "${proto}" = "MQTT" ]; then
config_get port mqtt BrokerPort "1883"
config_get topic controller ResponseTopicConfigured
proto="mqtt://"
else
return 0
fi
address=$(ifstatus "${intf}" | jsonfilter -q -e '@["ipv4-address"][0].address')
if [ -z "${address}" ] || [ -z "${topic}" ]; then
return 0
fi
subop_present=0
opt125="125,"
base_opt=""
service="$(uci -q get dhcp."${intf}".dhcpv4)"
if [ "${service}" = "server" ]; then
opt_list="$(uci -q get dhcp."${intf}".dhcp_option)"
for sopt in $opt_list; do
if [[ "$sopt" == "$opt125"* ]]; then
base_opt=$(check_for_suboptions "${sopt:4}")
uci -q del_list dhcp."${intf}".dhcp_option="$sopt"
uci -q commit dhcp
break
fi
done
else
return 0
fi
if [ -z "${base_opt}" ]; then
opt125="125,00:00:0D:E9"
else
opt125="125,${base_opt}:00:00:0D:E9"
fi
url="${proto}${address}:${port}${topic}"
url_len=$(echo -n "${url}" | wc -m)
prov_code_len=$(echo -n "${prov_code}" | wc -m)
endpoint_len=$(echo -n "${endpoint}" | wc -m)
interval_len=$(echo -n "${interval}" | wc -m)
multiplier_len=$(echo -n "${multiplier}" | wc -m)
([ ${url_len} -gt 255 ] || [ ${prov_code_len} -gt 255 ] || [ ${endpoint_len} -gt 255 ]) && return 0
([ ${interval_len} -gt 255 ] || [ ${multiplier_len} -gt 255 ]) && return 0
opt125_len=$((url_len + prov_code_len + endpoint_len + interval_len + multiplier_len))
opt125_len=$((opt125_len + 10))
[ $opt125_len -gt 255 ] && return 0
hex_opt125_len=$(printf "%02X" "${opt125_len}")
opt125="${opt125}:${hex_opt125_len}"
hex_url=$(convert_to_hex "${url}")
if [ -z "${hex_url}" ]; then
return 0
fi
hex_url_len=$(printf "%02X" "${url_len}")
opt125="${opt125}:19:${hex_url_len}${hex_url}"
hex_prov_code=$(convert_to_hex "${prov_code}")
if [ -z "${hex_prov_code}" ]; then
return 0
fi
hex_prov_len=$(printf "%02X" "${prov_code_len}")
opt125="${opt125}:1A:${hex_prov_len}${hex_prov_code}"
hex_interval=$(convert_to_hex "${interval}")
if [ -z "${hex_interval}" ]; then
return 0
fi
hex_interval_len=$(printf "%02X" "${interval_len}")
opt125="${opt125}:1B:${hex_interval_len}${hex_interval}"
hex_multiplier=$(convert_to_hex "${multiplier}")
if [ -z "${hex_multiplier}" ]; then
return 0
fi
hex_multiplier_len=$(printf "%02X" "${multiplier_len}")
opt125="${opt125}:1C:${hex_multiplier_len}${hex_multiplier}"
hex_endpoint=$(convert_to_hex "${endpoint}")
if [ -z "${hex_endpoint}" ]; then
return 0
fi
hex_endpoint_len=$(printf "%02X" "${endpoint_len}")
opt125="${opt125}:1D:${hex_endpoint_len}${hex_endpoint}"
uci -q add_list dhcp."${intf}".dhcp_option="$opt125"
ubus call uci commit '{"config":"dhcp"}'
}
boot() {
local enabled
local interface
config_load ${CONFIGURATION}
config_get_bool enabled global enabled 0
config_get interface global interface "lan"
if [ "${enabled}" -eq 0 ]; then
return 0;
fi
configure_dnsmasq_op125 "${interface}"
start
}
validate_global_section()
{
uci_validate_section ${CONFIGURATION} obuspc "${1}" \
@ -344,6 +600,11 @@ stop_service() {
${PROG} -c stop >/dev/null 2>&1
}
reload_service() {
stop
start
}
service_triggers() {
procd_add_reload_trigger "obuspc"
}

View file

@ -0,0 +1,13 @@
#!/bin/sh
section_exist=$(uci -q get mosquitto.obuspc)
if [ -z "${section_exist}" ]; then
section=$(uci -q add mosquitto listener)
uci -q rename mosquitto.$section="obuspc"
fi
port=$(uci -q get obuspc.mqtt.BrokerPort)
uci -q set mosquitto.obuspc.enabled="1"
uci -q set mosquitto.obuspc.port=$port
uci -q set mosquitto.obuspc.allow_anonymous="1"
uci commit mosquitto