obuspa,gateway-info: Support GatewayInfo USP params

This commit is contained in:
Suvendhu Hansa 2025-03-07 15:51:56 +05:30
parent db72ee376e
commit 2cb0f6e159
4 changed files with 258 additions and 7 deletions

View file

@ -28,7 +28,7 @@ include ../bbfdm/bbfdm.mk
define Package/gateway-info define Package/gateway-info
CATEGORY:=Utilities CATEGORY:=Utilities
TITLE:=GatewayInfo Data Model Support TITLE:=GatewayInfo Data Model Support
DEPENDS:=+libuci +libubox +libubus +libblobmsg-json DEPENDS:=+libuci +libubox +libubus +libblobmsg-json +umdns
DEPENDS+=+libbbfdm-api +libbbfdm-ubus +dm-service DEPENDS+=+libbbfdm-api +libbbfdm-ubus +dm-service
endef endef

View file

@ -1,10 +1,13 @@
#!/bin/sh #!/bin/sh
. /lib/functions.sh . /lib/functions.sh
. /usr/share/libubox/jshn.sh
CLASS="" CLASS=""
OUI="" OUI=""
SERIAL="" SERIAL=""
GW_DISCOVERED=0
_json_no_warning=1
get_vivsoi() { get_vivsoi() {
# opt125 environment variable has data in below format # opt125 environment variable has data in below format
@ -86,11 +89,17 @@ get_vivsoi() {
# assign the value found in sub option # assign the value found in sub option
case "${sub_opt_id}" in case "${sub_opt_id}" in
"4") OUI=$(echo -n $sub_opt_val | sed 's/\([0-9A-F]\{2\}\)/\\\\\\x\1/gI' | xargs printf && echo '') "4")
OUI=$(echo -n $sub_opt_val | sed 's/\([0-9A-F]\{2\}\)/\\\\\\x\1/gI' | xargs printf && echo '')
GW_DISCOVERED=1
;; ;;
"5") SERIAL=$(echo -n $sub_opt_val | sed 's/\([0-9A-F]\{2\}\)/\\\\\\x\1/gI' | xargs printf && echo '') "5")
SERIAL=$(echo -n $sub_opt_val | sed 's/\([0-9A-F]\{2\}\)/\\\\\\x\1/gI' | xargs printf && echo '')
GW_DISCOVERED=1
;; ;;
"6") CLASS=$(echo -n $sub_opt_val | sed 's/\([0-9A-F]\{2\}\)/\\\\\\x\1/gI' | xargs printf && echo '') "6")
CLASS=$(echo -n $sub_opt_val | sed 's/\([0-9A-F]\{2\}\)/\\\\\\x\1/gI' | xargs printf && echo '')
GW_DISCOVERED=1
;; ;;
esac esac
@ -110,13 +119,153 @@ get_vivsoi() {
done done
} }
send_host_query() {
intf="${1}"
resp=1
loop=3
usp_serv_found=0
sent_host=" "
ubus call umdns update
while [ "${loop}" -ne 0 ]; do
sleep 5
json_load "$(ubus call umdns browse)"
json_select discovered_services
if [ "${?}" -ne 0 ]; then
json_cleanup
loop=$(( loop - 1 ))
continue
fi
json_select _usp-agt-mqtt._tcp
if [ "${?}" -ne 0 ]; then
json_cleanup
loop=$(( loop - 1 ))
continue
fi
usp_serv_found=1
break
done
if [ "${usp_serv_found}" -eq 0 ]; then
echo "${resp}"
return 0
fi
json_get_keys keys
for key in $keys; do
json_select $key
json_get_var _host host ""
if [ -z "${_host}" ] || [[ "${sent_host}" =~ " ${_host}" ]]; then
json_select ..
continue
fi
sent_host="${sent_host} ${_host}"
cmd="ubus call umdns query '{\"question\":\"$_host\",\"interface\":\"$intf\"}'"
eval $cmd
resp=0
json_select ..
done
json_cleanup
echo "${resp}"
}
get_usp_agent_id() {
dhcp_ip="${1}"
family="ipv4"
ID=""
if [[ "${dhcp_ip}" =~ ":" ]]; then
family="ipv6"
fi
json_load "$(ubus call umdns browse)"
json_select discovered_services
if [ "${?}" -ne 0 ]; then
json_cleanup
echo ${ID}
return 0
fi
json_select _usp-agt-mqtt._tcp
if [ "${?}" -ne 0 ]; then
json_cleanup
echo ${ID}
return 0
fi
json_get_keys keys
for key in $keys; do
json_select $key
json_select $family
if [ "${?}" -ne 0 ]; then
json_select ..
continue
fi
json_get_keys ips
for ip in $ips; do
json_get_var ip_val $ip
if [ "${ip_val}" != "${dhcp_ip}" ]; then
continue
fi
json_select ..
json_select txt
json_get_keys txts
for _txt in $txts; do
json_get_var text_val $_txt
if [[ "${text_val:0:3}" == "ID=" ]]; then
ID="${text_val:3}"
break
fi
done
break
done
json_select ..
json_select ..
if [ -n "${ID}" ]; then
break
fi
done
json_cleanup
echo ${ID}
}
get_mac_address() {
ip="${1}"
mac="$(cat /proc/net/arp | grep $ip | awk '{print $4}')"
echo "${mac}"
}
config_load gateway config_load gateway
config_get wan_intf global wan_interface "wan" config_get wan_intf global wan_interface "wan"
if [ "${wan_intf}" == "${INTERFACE}" ]; then if [ "${wan_intf}" == "${INTERFACE}" ]; then
if [ -n "$opt125" ]; then if [ "${1}" == "deconfig" ]; then
len=$(printf "$opt125"|wc -c) rm -rf /var/state/gwinfo
get_vivsoi "$opt125" "$len" return 0
fi
if [ -z "$opt125" ]; then
return 0
fi
len=$(printf "$opt125"|wc -c)
get_vivsoi "$opt125" "$len"
if [ "${GW_DISCOVERED}" -eq 0 ]; then
return 0
fi fi
mkdir -p /var/state mkdir -p /var/state
@ -127,8 +276,44 @@ if [ "${wan_intf}" == "${INTERFACE}" ]; then
uci -q -c /var/state rename gwinfo."${sec}"="gatewayinfo" uci -q -c /var/state rename gwinfo."${sec}"="gatewayinfo"
fi fi
uci -q -c /var/state set gwinfo.gatewayinfo.hwaddr=""
uci -q -c /var/state set gwinfo.gatewayinfo.endpoint=""
uci -q -c /var/state set gwinfo.gatewayinfo.class="$CLASS" uci -q -c /var/state set gwinfo.gatewayinfo.class="$CLASS"
uci -q -c /var/state set gwinfo.gatewayinfo.oui="$OUI" uci -q -c /var/state set gwinfo.gatewayinfo.oui="$OUI"
uci -q -c /var/state set gwinfo.gatewayinfo.serial="$SERIAL" uci -q -c /var/state set gwinfo.gatewayinfo.serial="$SERIAL"
uci -q -c /var/state set gwinfo.gatewayinfo.proto="CWMP"
uci -q -c /var/state commit gwinfo
# Check for USP parameters
ubus -t 15 wait_for umdns
if [ "${?}" -ne 0 ]; then
return 0
fi
json_load "$(ifstatus ${INTERFACE})"
json_get_var dev_name device ""
json_select data
json_get_var dhcp_ip dhcpserver ""
json_cleanup
if [ -z "${dhcp_ip}" ] || [ -z "${dev_name}" ]; then
return 0
fi
resp=$(send_host_query $dev_name)
if [ "${resp}" -ne 0 ]; then
return 0
fi
ID="$(get_usp_agent_id $dhcp_ip)"
if [ -z "${ID}" ]; then
return 0
fi
MAC="$(get_mac_address $dhcp_ip)"
uci -q -c /var/state set gwinfo.gatewayinfo.hwaddr="$MAC"
uci -q -c /var/state set gwinfo.gatewayinfo.endpoint="$ID"
uci -q -c /var/state set gwinfo.gatewayinfo.proto="USP"
uci -q -c /var/state commit gwinfo uci -q -c /var/state commit gwinfo
fi fi

View file

@ -132,6 +132,7 @@ define Package/obuspa/install
$(INSTALL_BIN) ./files/etc/uci-defaults/60-generate-ctrust-defaults $(1)/etc/uci-defaults/ $(INSTALL_BIN) ./files/etc/uci-defaults/60-generate-ctrust-defaults $(1)/etc/uci-defaults/
$(INSTALL_BIN) ./files/etc/uci-defaults/obuspa-set-dhcp-option $(1)/etc/uci-defaults/ $(INSTALL_BIN) ./files/etc/uci-defaults/obuspa-set-dhcp-option $(1)/etc/uci-defaults/
$(INSTALL_BIN) ./files/etc/uci-defaults/92-obuspa_firewall $(1)/etc/uci-defaults/ $(INSTALL_BIN) ./files/etc/uci-defaults/92-obuspa_firewall $(1)/etc/uci-defaults/
$(INSTALL_BIN) ./files/etc/uci-defaults/93-obuspa_mdns_adv $(1)/etc/uci-defaults/
$(INSTALL_BIN) ./files/etc/firewall.usp $(1)/etc/ $(INSTALL_BIN) ./files/etc/firewall.usp $(1)/etc/
$(INSTALL_BIN) ./files/etc/udhcpc.user.d/udhcpc_obuspa_opt125.user $(1)/etc/udhcpc.user.d/udhcpc_obuspa_opt125.user $(INSTALL_BIN) ./files/etc/udhcpc.user.d/udhcpc_obuspa_opt125.user $(1)/etc/udhcpc.user.d/udhcpc_obuspa_opt125.user
ifeq ($(CONFIG_OBUSPA_CWMP_DATAMODEL_SUPPORT),y) ifeq ($(CONFIG_OBUSPA_CWMP_DATAMODEL_SUPPORT),y)

View file

@ -0,0 +1,65 @@
#!/bin/sh
. /usr/share/libubox/jshn.sh
. /lib/functions.sh
get_oui_from_db() {
db -q get device.deviceinfo.ManufacturerOUI
}
get_serial_from_db() {
db -q get device.deviceinfo.SerialNumber
}
get_endpoint_id() {
AgentEndpointID="$(uci -q get obuspa.localagent.EndpointID)"
if [ -z "${AgentEndpointID}" ]; then
serial=$(get_serial_from_db)
oui=$(get_oui_from_db)
AgentEndpointID=$(echo "os::${oui}-${serial//+/%2B}")
fi
echo "${AgentEndpointID}"
}
get_device_role()
{
local mode lan_proto
lan_proto="$(uci -q get network.lan.proto)"
if [ "${lan_proto}" == "dhcp" ]; then
mode="extender"
else
mode="gateway"
fi
echo "$mode"
}
add_mdns_advertise() {
mkdir -p /etc/umdns
usp_id="$(get_endpoint_id)"
json_init
json_add_object "usp_mdns"
json_add_string "service" "_usp-agt-mqtt._tcp.local"
json_add_int "port" 0
json_add_array "txt"
json_add_string "" "ID=$usp_id"
json_close_array
json_close_object
json_dump > /etc/umdns/obuspa_mdns.json
}
config_load obuspa
config_get_bool enable_obuspa global enabled 1
if [ "${enable_obuspa}" -eq 1 ]; then
role="$(get_device_role)"
if [ "${role}" == "gateway" ]; then
add_mdns_advertise
fi
fi