From 23d564116cb6e0d57fc6db209fcd5cb0098b41b8 Mon Sep 17 00:00:00 2001 From: vdutta Date: Tue, 4 Oct 2022 17:00:07 +0530 Subject: [PATCH] obuspa: 6.0.0.2 --- obuspa/Config.in | 2 +- obuspa/Makefile | 4 +- obuspa/files/etc/init.d/obuspa | 333 ++++++++++++++++++++++++++++----- 3 files changed, 289 insertions(+), 50 deletions(-) diff --git a/obuspa/Config.in b/obuspa/Config.in index 9b569f05e..639f4c319 100644 --- a/obuspa/Config.in +++ b/obuspa/Config.in @@ -20,7 +20,7 @@ config OBUSPA_MTP_ENABLE_STOMP config OBUSPA_MTP_ENABLE_COAP bool "Enable CoAP as mtp protocol" - default n + default y config OBUSPA_CONTROLLER_MTP_VERIFY bool "Enable verification of MQTT response topic before processing the message" diff --git a/obuspa/Makefile b/obuspa/Makefile index faa6c2c1c..d58487378 100644 --- a/obuspa/Makefile +++ b/obuspa/Makefile @@ -5,13 +5,13 @@ include $(TOPDIR)/rules.mk PKG_NAME:=obuspa -PKG_VERSION:=6.0.0.1 +PKG_VERSION:=6.0.0.2 LOCAL_DEV:=0 ifneq ($(LOCAL_DEV),1) PKG_SOURCE_PROTO:=git PKG_SOURCE_URL:=https://dev.iopsys.eu/fork/obuspa.git -PKG_SOURCE_VERSION:=6372841ada18a4f577799fedb19f489ec1f28255 +PKG_SOURCE_VERSION:=44118c2d59328b90591634bf74159329a98c17bc PKG_MAINTAINER:=Vivek Dutta PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)-$(PKG_SOURCE_VERSION).tar.gz PKG_MIRROR_HASH:=skip diff --git a/obuspa/files/etc/init.d/obuspa b/obuspa/files/etc/init.d/obuspa index f361372d6..ff7ba2385 100755 --- a/obuspa/files/etc/init.d/obuspa +++ b/obuspa/files/etc/init.d/obuspa @@ -1,14 +1,14 @@ #!/bin/sh /etc/rc.common START=99 -STOP=2 +STOP=02 USE_PROCD=1 PROG=/usr/sbin/obuspa CONFIGURATION=obuspa ENV_PROFILE="/root/.profile" -KEEP_FILES="/lib/upgrade/keep.d/obuspa" +KEEP_FILE="/lib/upgrade/keep.d/obuspa" RESET_FILE="/tmp/obuspa/obuspa_param_reset.txt" SQL_DB_FILE="/tmp/obuspa/usp.db" @@ -112,8 +112,8 @@ get_refrence_path() value="${2}" path="" - if [ -f "${SQL_DB_FILE}" ]; then - path=$(${PROG} -f ${SQL_DB_FILE} -c show database |grep "${dmref}\d.Alias "|grep -w "${value}") + if [ -f "${DB_DUMP}" ]; then + path=$(grep "${dmref}\d.Alias " ${DB_DUMP}|grep -w "${value}") elif [ -f "${RESET_FILE}" ]; then path=$(grep "${dmref}\d.Alias " ${RESET_FILE}|grep -w "${value}") fi @@ -123,14 +123,18 @@ get_refrence_path() update_keep() { - keep_file=${1} + file=${1} - if [ -z "${keep_file}" ]; then + if [ -z "${file}" ]; then return; fi - if ! grep -q "${keep_file}" ${KEEP_FILES}; then - echo "${keep_file}" >> ${KEEP_FILES} + if [ ! -f "${KEEP_FILE}" ]; then + touch "${KEEP_FILE}" + fi + + if ! grep -q "${file}" ${KEEP_FILE}; then + echo "${file}" >> ${KEEP_FILE} fi } @@ -200,13 +204,15 @@ validate_controller_section() 'Enable:bool:1' \ 'EndpointID:string' \ 'assigned_role_name:string' \ + 'AssignedRole:string' \ 'Protocol:or("STOMP","CoAP","MQTT", "WebSocket")' \ 'Destination:string' \ 'Topic:string' \ 'ParameterName:list(string)' \ + 'Reference:string' \ 'mqtt:string' \ 'stomp:string' \ - 'Host:host' \ + 'Host:string' \ 'Port:port' \ 'Path:string' \ 'EnableEncryption:bool' @@ -218,7 +224,7 @@ validate_subscription_section() 'Enable:bool:1' \ 'NotifType:or("ValueChange","ObjectCreation","ObjectDeletion","OperationComplete","Event")' \ 'ReferenceList:list(string)' \ - 'Persistent:bool:1' \ + 'Recipient:string' \ 'controller:string' } @@ -233,6 +239,7 @@ validate_mtp_section() 'Path:string' \ 'mqtt:string' \ 'stomp:string' \ + 'Reference:string' \ 'EnableEncryption:bool' } @@ -240,7 +247,7 @@ validate_stomp_connection_section() { uci_validate_section ${CONFIGURATION} stomp "${1}" \ 'Enable:bool:1' \ - 'Host:host' \ + 'Host:string' \ 'Port:port:61613' \ 'Username:string' \ 'Password:string' \ @@ -253,7 +260,7 @@ validate_mqtt_client_section() { uci_validate_section ${CONFIGURATION} mqtt "${1}" \ 'Enable:bool:1' \ - 'BrokerAddress:host' \ + 'BrokerAddress:string' \ 'BrokerPort:port:1883' \ 'Username:string' \ 'Password:string' \ @@ -350,16 +357,18 @@ configure_controller() { local EndpointID Enable local Protocol Destination - local Topic mqtt stomp assigned_role_name ParameterName - local Host Port Path EnableEncryption - local dm_ref + local Topic mqtt stomp assigned_role_name AssignedRole ParameterName + local Host Port Path EnableEncryption Reference + local dm_ref sec + sec="${1}" validate_controller_section "${1}" || { log "Validation of controller section failed" return 1; } - get_base_path "Device.LocalAgent.Controller." "uci-${1}" + sec="${sec/controller_/cpe-}" + get_base_path "Device.LocalAgent.Controller." "${sec}" if [ -z "${BASEPATH}" ]; then log "Failed to get path [$BASEPATH]" return 1; @@ -371,21 +380,30 @@ configure_controller() fi dm_ref="" - if [ "${Protocol}" = "STOMP" ]; then - dm_ref=$(get_refrence_path "Device.STOMP.Connection." "uci-${stomp}") - elif [ "${Protocol}" = "MQTT" ]; then - dm_ref=$(get_refrence_path "Device.MQTT.Client." "uci-${mqtt}") + if [ -z "${Reference}" ]; then + if [ "${Protocol}" = "STOMP" ]; then + stomp="${stomp/stomp_/cpe-}" + dm_ref=$(get_refrence_path "Device.STOMP.Connection." "${stomp}") + elif [ "${Protocol}" = "MQTT" ]; then + mqtt="${mqtt/mqtt_/cpe-}" + dm_ref=$(get_refrence_path "Device.MQTT.Client." "${mqtt}") + fi + else + dm_ref="${Reference}" fi - db_set "${BASEPATH}.Alias" "uci-${1}" + db_set "${BASEPATH}.Alias" "${sec}" db_set "${BASEPATH}.Enable" "${Enable}" db_set "${BASEPATH}.EndpointID" "${EndpointID}" if [ -n "${assigned_role_name}" ]; then AssignedRole=$(get_role_index "${assigned_role_name}") + fi + + if [ -n "${AssignedRole}" ]; then db_set "${BASEPATH}.AssignedRole" "${AssignedRole}" fi - db_set "${BASEPATH}.MTP.1.Alias" "uci-${1}" + db_set "${BASEPATH}.MTP.1.Alias" "${sec}" db_set "${BASEPATH}.MTP.1.Enable" "${Enable}" db_set "${BASEPATH}.MTP.1.Protocol" "${Protocol}" # only support configuration of one mtp path per controller using uci @@ -412,7 +430,7 @@ configure_controller() for param in ${ParameterName} do - db_set "${BASEPATH}.BootParameter.${_pnum}.Alias" "uci-${1}-${_pnum}" + db_set "${BASEPATH}.BootParameter.${_pnum}.Alias" "${sec}_boot_${_pnum}" db_set "${BASEPATH}.BootParameter.${_pnum}.Enable" "${Enable}" db_set "${BASEPATH}.BootParameter.${_pnum}.ParameterName" "${param}" _pnum=$(( _pnum + 1 )) @@ -423,25 +441,29 @@ configure_controller() configure_subscription() { - local Enable NotifType ReferenceList Persistent controller + local Enable NotifType ReferenceList controller local dm_ref + local sec + sec="${1}" validate_subscription_section "${1}" || { log "Validation of subscription section failed" return 1; } - get_base_path "Device.LocalAgent.Subscription." "uci-${1}" + sec="${sec/sub_/cpe-}" + get_base_path "Device.LocalAgent.Subscription." "sub_${1}" if [ -z "${BASEPATH}" ]; then log "Failed to get path [$BASEPATH]" return 1; fi if [ -n "${controller}" ]; then - dm_ref=$(get_refrence_path "Device.LocalAgent.Controller." "uci-${controller}") + controller="${controller/controller_/cpe-}" + dm_ref=$(get_refrence_path "Device.LocalAgent.Controller." "${controller}") fi - db_set "${BASEPATH}.Alias" "uci-${1}" + db_set "${BASEPATH}.Alias" "${sec}" db_set "${BASEPATH}.ID" "uci-${1}" db_set "${BASEPATH}.CreationDate" "$(date -I'seconds')" db_set "${BASEPATH}.Enable" "${Enable}" @@ -450,7 +472,7 @@ configure_subscription() ReferenceList=${ReferenceList// /,} db_set "${BASEPATH}.ReferenceList" "${ReferenceList}" fi - db_set "${BASEPATH}.Persistent" "${Persistent}" + db_set "${BASEPATH}.Persistent" "1" db_set "${BASEPATH}.Recipient" "${dm_ref}" } @@ -488,8 +510,7 @@ get_role_index() *) local ji=0 - check_json_load - if [ "$?" -eq "0" ]; then + if check_json_load; then ji=$(get_role_index_from_json "${role_name}" "${role_def_file}") fi idx=$(( ji + 2 )) # Add predefined roles @@ -501,13 +522,16 @@ get_role_index() configure_challenges() { local Enable Description role_name Role Value Retries LockoutPeriod + local sec + sec="${1}" validate_challenge_section "${1}" || { log "Validation of challenge section failed" exit 1; } - get_base_path "Device.LocalAgent.ControllerTrust.Challenge." "uci-${1}" + sec="${sec/challenge_/cpe-}" + get_base_path "Device.LocalAgent.ControllerTrust.Challenge." "${sec}" if [ -z "${BASEPATH}" ]; then log "Failed to get path [$BASEPATH]" return 1; @@ -518,7 +542,7 @@ configure_challenges() return 1; fi - db_set "${BASEPATH}.Alias" "uci-${1}" + db_set "${BASEPATH}.Alias" "${sec}" db_set "${BASEPATH}.Enable" "${Enable}" db_set "${BASEPATH}.Description" "${Description}" @@ -535,14 +559,16 @@ configure_challenges() configure_mtp() { local Enable Protocol ResponseTopicConfigured local Path Port EnableEncryption - local stomp mqtt dm_ref + local stomp mqtt dm_ref sec + sec="${1}" validate_mtp_section "${1}" || { log "Validation of mtp section failed" return 1; } - get_base_path "Device.LocalAgent.MTP." "uci-${1}" + sec="${sec/mtp_/cpe-}" + get_base_path "Device.LocalAgent.MTP." "${sec}" if [ -z "${BASEPATH}" ]; then log "Failed to get path [$BASEPATH]" return 1; @@ -554,13 +580,19 @@ configure_mtp() { fi dm_ref="" - if [ "${Protocol}" = "STOMP" ]; then - dm_ref=$(get_refrence_path "Device.STOMP.Connection." "uci-${stomp}") - elif [ "${Protocol}" = "MQTT" ]; then - dm_ref=$(get_refrence_path "Device.MQTT.Client." "uci-${mqtt}") + if [ -z "${Reference}" ]; then + if [ "${Protocol}" = "STOMP" ]; then + stomp="${stomp/stomp_/cpe-}" + dm_ref=$(get_refrence_path "Device.STOMP.Connection." "${stomp}") + elif [ "${Protocol}" = "MQTT" ]; then + mqtt="${mqtt/mqtt_/cpe-}" + dm_ref=$(get_refrence_path "Device.MQTT.Client." "${mqtt}") + fi + else + dm_ref="${Reference}" fi - db_set "${BASEPATH}.Alias" "uci-${1}" + db_set "${BASEPATH}.Alias" "${sec}" db_set "${BASEPATH}.Enable" "${Enable}" db_set "${BASEPATH}.Protocol" "${Protocol}" if [ "${Protocol}" = "MQTT" ]; then @@ -583,19 +615,22 @@ configure_mtp() { configure_stomp_connection() { local Host Username Password Enable Port VirtualHost local EnableEncryption EnableHeartbeats + local sec + sec="${1}" validate_stomp_connection_section "${1}" || { log "Validation of stomp section failed" return 1; } - get_base_path "Device.STOMP.Connection." "uci-${1}" + sec="${sec/stomp_/cpe-}" + get_base_path "Device.STOMP.Connection." "${sec}" if [ -z "${BASEPATH}" ]; then log "Failed to get path [$BASEPATH]" return 1; fi - db_set "${BASEPATH}.Alias" "uci-${1}" + db_set "${BASEPATH}.Alias" "${sec}" db_set "${BASEPATH}.Host" "${Host}" db_set "${BASEPATH}.Username" "${Username}" db_set "${BASEPATH}.Password" "${Password}" @@ -611,19 +646,22 @@ configure_stomp_connection() { configure_mqtt_client(){ local BrokerAddress BrokerPort Enable Username Password ProtocolVersion local TransportProtocol ClientID + local sec + sec="${1}" validate_mqtt_client_section "${1}" || { log "Validation of mqtt section failed" return 1; } - get_base_path "Device.MQTT.Client." "uci-${1}" + sec="${sec/mqtt_/cpe-}" + get_base_path "Device.MQTT.Client." "${sec}" if [ -z "${BASEPATH}" ]; then log "Failed to get path [$BASEPATH]" return 1; fi - db_set "${BASEPATH}.Alias" "uci-${1}" + db_set "${BASEPATH}.Alias" "${sec}" db_set "${BASEPATH}.Enable" "${Enable}" db_set "${BASEPATH}.BrokerAddress" "${BrokerAddress}" db_set "${BASEPATH}.BrokerPort" "${BrokerPort}" @@ -705,21 +743,222 @@ configure_obuspa() { fi } +get_instances_from_db_dump() +{ + local obj inst + + obj="${1}\d" + if [ ! -f "${DB_DUMP}" ]; then + echo "" + return 0; + fi + + inst="$(grep -oe "${obj}" "${DB_DUMP}"|uniq)" + echo "$inst" +} + +get_param_value_from_dump() +{ + local param value + + param="${1}" + + if [ -z "${param}" ] || [ ! -f "${DB_DUMP}" ]; then + log "error getting param" + echo "" + return 0 + fi + + value="$(grep "^${param} " ${DB_DUMP}|awk '{print $3}')" + + echo "$value" +} + +update_uci_sec() +{ + local sec tmp + + sec="${1}" + stype="${2}" + if [ -z "$sec" ] || [ -z "$stype" ]; then + log "No section name, error" + return 0 + fi + + tmp="$(uci_get obuspa "${sec}")" + if [ "$tmp" != "$stype" ]; then + uci_add obuspa "${stype}" "${sec}" + fi +} + +sync_db_controller() +{ + local cntrs copts sec pvalue protocol + + copts="Enable EndpointID AssignedRole" + popts="Destination Topic Reference Host Port Path EnableEncryption" + ropts="assigned_role_name mqtt stomp" + + cntrs="$(get_instances_from_db_dump Device.LocalAgent.Controller.)" + for cntr in $cntrs; do + sec="$(get_param_value_from_dump "${cntr}".Alias)" + sec="${sec/cpe-/controller_}" + sec="${sec/-/_}" + + update_uci_sec "${sec}" controller + for param in ${copts}; do + pvalue="$(get_param_value_from_dump "${cntr}"."${param}")" + uci_set obuspa "${sec}" "${param}" "${pvalue}" + done + uci_set obuspa "${sec}" "_sync" "1" + + protocol="$(get_param_value_from_dump "${cntr}".MTP.1.Protocol)" + if [ -z "${protocol}" ]; then + break; + fi + uci_set obuspa "${sec}" "Protocol" "${protocol}" + for param in ${popts}; do + pvalue="$(get_param_value_from_dump "${cntr}".MTP.1."${protocol}"."${param}")" + uci_set obuspa "${sec}" "${param}" "${pvalue}" + done + for param in ${ropts}; do + uci_set obuspa "${sec}" "${param}" "" + done + done +} + +sync_db_localagent_mtp() +{ + local mtps opts popts sec pvalue protocol + + opts="Enable" + popts="ResponseTopicConfigured Destination Port Path Reference EnableEncryption" + ropts="mqtt stomp" + + mtps="$(get_instances_from_db_dump Device.LocalAgent.MTP.)" + for inst in $mtps; do + sec="$(get_param_value_from_dump "${inst}".Alias)" + sec="${sec/cpe-/mtp_}" + sec="${sec/-/_}" + update_uci_sec "${sec}" mtp + for param in ${opts}; do + pvalue="$(get_param_value_from_dump "${inst}"."${param}")" + uci_set obuspa "${sec}" "${param}" "${pvalue}" + done + uci_set obuspa "${sec}" "_sync" "1" + + protocol="$(get_param_value_from_dump "${inst}".Protocol)" + if [ -z "${protocol}" ]; then + break; + fi + uci_set obuspa "${sec}" "Protocol" "${protocol}" + for param in ${popts}; do + pvalue="$(get_param_value_from_dump "${inst}"."${protocol}"."${param}")" + uci_set obuspa "${sec}" "${param}" "${pvalue}" + done + for param in ${ropts}; do + uci_set obuspa "${sec}" "${param}" "" + done + done +} + + +sync_db_mqtt_client() +{ + local mtps copts sec pvalue protocol + + opts="Enable BrokerAddress BrokerPort Username ProtocolVersion TransportProtocol ClientID" + + mtps="$(get_instances_from_db_dump Device.MQTT.Client.)" + for inst in $mtps; do + sec="$(get_param_value_from_dump "${inst}".Alias)" + sec="${sec/cpe-/mqtt_}" + sec="${sec/-/_}" + update_uci_sec "${sec}" mqtt + for param in ${opts}; do + pvalue="$(get_param_value_from_dump "${inst}"."${param}")" + uci_set obuspa "${sec}" "${param}" "${pvalue}" + done + uci_set obuspa "${sec}" "_sync" "1" + done +} + +sync_db_stomp_connection() +{ + local mtps copts sec pvalue protocol + + opts="Enable Host Port Username EnableEncryption EnableHeartbeats VirtualHost" + + mtps="$(get_instances_from_db_dump Device.STOMP.Connection.)" + for inst in $mtps; do + sec="$(get_param_value_from_dump "${inst}".Alias)" + sec="${sec/cpe-/stomp_}" + sec="${sec/-/_}" + update_uci_sec "${sec}" stomp + for param in ${opts}; do + pvalue="$(get_param_value_from_dump "${inst}"."${param}")" + uci_set obuspa "${sec}" "${param}" "${pvalue}" + done + uci_set obuspa "${sec}" "_sync" "1" + done +} + +sync_update_sec() +{ + local _sync + config_get _sync "${1}" _sync "" + if [ -z "${_sync}" ]; then + uci_remove obuspa "${1}" + else + uci_remove obuspa "${1}" _sync + fi +} + +sync_db_with_uci() +{ + if [ ! -f "${DB_DUMP}" ]; then + return 0; + fi + + config_load obuspa + sync_db_controller + sync_db_localagent_mtp + sync_db_mqtt_client + sync_db_stomp_connection + uci_commit obuspa + + config_load obuspa + config_foreach sync_update_sec controller + config_foreach sync_update_sec mtp + config_foreach sync_update_sec mqtt + config_foreach sync_update_sec stomp + uci_commit obuspa +} + # Create factory reset file db_init() { + local reason + + reason="${1}" # Load configuration config_load $CONFIGURATION config_get SQL_DB_FILE global db_file "/tmp/obuspa/usp.db" # Dump datamodel parameters from DB if [ -f "${SQL_DB_FILE}" ]; then - ${PROG} -f ${SQL_DB_FILE} -c show database > ${DB_DUMP} + ${PROG} -f ${SQL_DB_FILE} -c show database |sort > ${DB_DUMP} + fi + + # Only sync uci with db in case of non service triggers + if [ -f "${DB_DUMP}" ] && [ "${reason}" != "update" ]; then + sync_db_with_uci fi # Remove reset file if present [ -f "${RESET_FILE}" ] && rm -f ${RESET_FILE} + config_load $CONFIGURATION global_init config_foreach configure_localagent localagent global_init @@ -771,7 +1010,7 @@ start_service() { fi mkdir -p /tmp/obuspa/ - db_init + db_init "${1}" register_service } @@ -781,7 +1020,7 @@ stop_service() { reload_service() { stop - start + start update } service_triggers() { @@ -801,7 +1040,7 @@ service_triggers() { json_add_array json_add_string "" "run_script" json_add_string "" "/etc/init.d/obuspa" - json_add_string "" "reload" + json_add_string "" "restart" json_close_array json_close_array json_add_int "" "2000"