From 291266f50044381bbb40b9f37e00de3c4080ac6e Mon Sep 17 00:00:00 2001
From: Yalu Zhang <yalu.zhang@iopsys.eu>
Date: Fri, 7 Jun 2019 16:31:51 +0200
Subject: [PATCH] Re-fix Bug #705: Asterisk gives tone even if number is not
 configured

Set the parameter "context" in asterisk channel configuration file according to
voice_client.sipX.call_lines, i.e. "Incoming Phone Lines".

Also remove voice_client.sip0.call_lines in the default config. So no voice lines
are bound to "Incoming Phone Lines" of any SIP accounts by default.
---
 voice-client/files/etc/config/voice_client |  1 -
 voice-client/files/etc/init.d/voice_client | 58 +++++++++++++++++-----
 2 files changed, 46 insertions(+), 13 deletions(-)

diff --git a/voice-client/files/etc/config/voice_client b/voice-client/files/etc/config/voice_client
index dc5816eee..d60de1e08 100644
--- a/voice-client/files/etc/config/voice_client
+++ b/voice-client/files/etc/config/voice_client
@@ -174,5 +174,4 @@ config sip_service_provider 'sip0'
 	option redial '*66'
 	option is_fax '0'
 	option transport 'udp'
-	option call_lines '0'
 
diff --git a/voice-client/files/etc/init.d/voice_client b/voice-client/files/etc/init.d/voice_client
index 6b3238a65..4540e67e6 100755
--- a/voice-client/files/etc/init.d/voice_client
+++ b/voice-client/files/etc/init.d/voice_client
@@ -259,7 +259,7 @@ read_lines()
 	local loffset=$(ubus -t 1 call voice.asterisk platform | jsonfilter -e @.lineoffset)
 	loffset=${loffset:-0}
 
-	config_get call_lines $1 call_lines "0"
+	config_get call_lines $1 call_lines
 
 	for line in $call_lines ; do
 
@@ -1839,12 +1839,49 @@ configure_tel()
 	sed -i "s/|DTMFCOMPATIBILITY|/$dtmfcompatibility/"      $WORKDIR/$LINENAME.tmp
 }
 
+# Return all SIP accounts to which a voice line is bound as per "voice_client.sipX.call_lines",
+# i.e. Incoming Phone Lines
+get_sip_accounts_for_incoming_call()
+{
+	local tel_line="$1"
+	local sip_accounts=""
+	local line_name_len=${#LINENAME}
+	local lnum=${tel_line:${line_name_len}}
+	local section cfgtype call_lines
+
+	for section in ${CONFIG_SECTIONS}; do
+		config_get cfgtype "$section" TYPE
+		[ "$cfgtype" != "sip_service_provider" ] && continue
+
+		config_get call_lines "$section" call_lines
+		# Note: voice_client.sipX.call_lines contains voice line index which starts with 0
+		# for all platforms, e.g. "0", "1", or "0 1"
+		[ -n "$call_lines" ] && echo "$call_lines" | grep -wq "$lnum" && {
+			local user enabled
+
+			config_get user $section user
+			config_get enabled $section enabled
+
+			[ "$section" != "-" -a "$enabled" == "1" -a -n "$user" ] && {
+				# Append this sip_acount to the tel line
+				if [ -z "$sip_accounts" ]; then
+					sip_accounts="$section"
+				else
+					sip_accounts="$sip_accounts $section"
+				fi
+			}
+		}
+	done
+
+	echo "$sip_accounts"
+}
+
+
 #
 # Configure settings for individual line in $LINENAME.conf
 #
 configure_tel_line()
 {
-	echo "Configuring $CHANNELNAME line $1"
 	local extension
 	local sip_provider
 	local codecs
@@ -1852,13 +1889,10 @@ configure_tel_line()
 	local autodial
 	local autodial_timeout
 	local dialtone_timeout
-	local sip_provider_user
-	local sip_provider_enabled
 	local callwaiting
 	local clir
 
 	config_get extension $1 extension
-	config_get sip_provider $1 sip_account
 	config_get autodial $1 autodial
 	config_get autodial_timeout $1 autodial_timeout
 	config_get dialtone_timeout $1 dialtone_timeout
@@ -1866,9 +1900,8 @@ configure_tel_line()
 	config_get clir $1 clir
 
 	#If line is configured with no sip_provider, or sip_provider is disabled, use local_extensions context
-	config_get sip_provider_user $sip_provider user
-	config_get sip_provider_enabled $sip_provider enabled
-	if [ "$sip_provider" == "-" -o -z "$sip_provider_enabled" -o "$sip_provider_enabled" == "0" -o -z "$sip_provider_user" ] ; then
+	sip_provider=$(get_sip_accounts_for_incoming_call $1)
+	if [ -z "$sip_provider" ] ; then
 		sip_provider="local_extensions"
 	fi
 
@@ -2240,16 +2273,17 @@ stop_service() {
 
 reload_service() {
 	start
-	#stop
-	# turn off voice led; asterisk will turn it on
-	# if there is a registered account
+	# turn off voice led; asterisk will turn it on if there is a registered account
 	ubus call led.voice1 set '{"state":"off"}'
 
+	# FXS channel module must be reloaded before sip module. Otherwise some attributes like
+	# line's registration state which is updated by SIP module through callback might be
+	# reset.
+	asterisk -rx "$(getChipVendor) reload"
 	asterisk -rx "config reload $ASTERISKDIR/sip.conf"
 	sleep 1
 	asterisk -rx "core reload"
 	asterisk -rx "dialplan reload"
-	asterisk -rx "$(getChipVendor) reload"
 }
 
 service_triggers() {
-- 
GitLab