#!/bin/sh . /usr/share/libubox/jshn.sh . /lib/voice/voicelib.sh case "$1" in list) echo '{ "status" : {}, "lines" : {}, "codecs" : {}, "call_log" : {}, "platform" : {} }' ;; call) case "$2" in status) [ -e /var/run/asterisk/asterisk.ctl ] || { echo "{}" return } voiceports=$(db -q get hw.board.VoicePorts) json_load "$(ubus call endpt count)" json_get_var num_endpoints num_endpoints #json_get_var num_dect_endpoints num_dect_endpoints json_cleanup pndiff=$((voiceports - num_endpoints)) json_init json_add_object "sip" for peer in $(uci show voice_client | grep sip_service_provider | awk -F[.,=] '{print$2}'); do asterisk -x 'sip show registry' | grep $peer: > /tmp/sip_reg.status asterisk -x "sip show peer $peer" > /tmp/sip_peer.status json_add_object "$peer" json_add_boolean registered $(cat /tmp/sip_reg.status | grep -q Registered && echo 1 || echo 0) json_add_boolean registry_request_sent 0 addrip="$(cat /tmp/sip_peer.status | grep 'Addr->IP' | awk '{print$NF}')" json_add_string ip "$(echo $addrip | cut -d':' -f1)" json_add_string port "$(echo $addrip | cut -d':' -f2)" json_add_string username "$(cat /tmp/sip_peer.status | grep Username | awk '{print$NF}')" json_add_string domain "$(cat /tmp/sip_peer.status | grep FromDomain | awk '{print$3}')" json_add_string domain_port "$(cat /tmp/sip_peer.status | grep FromDomain | awk '{print$5}')" json_add_string refresh_interval "$(cat /tmp/sip_reg.status | awk '{print$4}')" json_add_string state "$(cat /tmp/sip_reg.status | grep -q 'Auth' && cat /tmp/sip_reg.status | awk '{print$5" "$6}' || cat /tmp/sip_reg.status | awk '{print$5}')" regtime="$(cat /tmp/sip_reg.status | grep -q 'Auth' || cat /tmp/sip_reg.status | awk '{print$6" "$7" "$8" "$9" "$NF}')" json_add_string registration_time "$regtime" json_add_string last_successful_registration "$regtime" rm -f /tmp/sip_peer.status rm -f /tmp/sip_reg.status json_select .. done json_select .. json_add_object "$(getLineName)" for line in $(uci show voice_client | grep $(getLineName)_line | awk -F[.,=] '{print$2}'); do json_add_object "$line" linestate="ONHOOK" linenum=${line:4} linenum=$((linenum - pndiff)) [ $linenum -ge 0 ] && ubus call endpt status "{'line':$linenum}" | grep offhook | grep -q 1 && linestate="OFFHOOK" json_add_string sub_0_state "$linestate" json_add_string sub_1_state "$linestate" json_select .. done json_select .. json_close_object json_dump ;; lines) subchannels=$(asterisk -x "$(getLineName) show status" 2>/dev/null | grep Subchannel | sort -u | wc -l) [ $subchannels -eq 0 ] && subchannels=2 json_init json_add_int num_subchannels $subchannels json_add_int num_lines $(db -q get hw.board.VoicePorts) json_dump ;; codecs) json_init for codec in $(uci show voice_codecs | grep supported_codec | awk -F[.,=] '{print$2}'); do json_add_object "$codec" json_add_string name "$(uci -q get voice_codecs.$codec.name)" bitrate=$(uci -q get voice_codecs.$codec.bitrate) [ -n "$bitrate" ] && json_add_int bitrate $bitrate json_select .. done json_dump ;; call_log) json_init json_add_array "call_log" [ -f /var/log/asterisk/cdr-csv/Master.csv ] || { json_dump return } while read -r line do line="$(echo $line | tr -d '\"')" uniqueid="$(echo $line | awk -F',' '{print $(NF-1)}')" echo $uniqueid | grep "[A-Z,a-z]" && continue from="$(echo $line | cut -d',' -f2)" to="$(echo $line | cut -d',' -f3)" callok=0 uci show voice_client | grep user | grep -wq "$from" && callok=1 uci show voice_client | grep user | grep -wq "$to" && callok=1 [ $callok -eq 0 ] && continue account="$(echo $line | cut -d',' -f4)" uci show voice_client | grep sip_service_provider | grep -wq "$account" || continue timestart="$(echo $line | awk -F',' '{print $(NF-8)}')" timend="$(echo $line | awk -F',' '{print $(NF-6)}')" startdate=$(date -u -d "$timestart" +"%s") enddate=$(date -u -d "$timend" +"%s") uci show voice_client | grep user | grep -wq "$from" && direction="OUTGOING" || direction="INCOMING" json_add_object "" json_add_string uniqueid "$uniqueid" json_add_string time "$timestart" json_add_int duration $((enddate - startdate)) json_add_string disposition "$(echo $line | awk -F',' '{print $(NF-3)}')" json_add_string direction "$direction" json_add_string from "$from" json_add_string to "$to" json_select .. done < /var/log/asterisk/cdr-csv/Master.csv json_dump ;; platform) json_init json_add_string platform "$(getLineName)" case $(getLineName) in "brcm") json_add_int lineoffset 0 json_add_int chanoffset 0 ;; "tapi") json_add_int lineoffset 1 json_add_int chanoffset -1 ;; *) # Error, unknown platform ;; esac json_dump ;; esac ;; esac