Pi-Kodi CallerID - VoIP provider with supported VOIP Adapters
#1
Goal: Utilize VOIP adapter's logging feature to display incoming call callerid to all Raspberry PIs running Kodi (Milhouse Build).


I missed the TV callerID alert feature so bad after dropping the cable TV service, i started poking around the internet for a solution. Then I come across this post which resulted in a working callerID solution for me. Even though there are so many variables to get this right and the process/script looks bit clunky [my apology in advance], i thought i share the project as-is here for other's benefit. I am not good at instructions, hope this make sense to others.


Disclaimer: Credit goes to 'DmCK' from this thread. I simply modify his script to adopt to my environment.
If you are using Sipura or Linksys or Cisco (excluding Cisco ATA 18X) Phone adapters, the script should work as-is. The OBixx phone adapter works well as well but needs a slight adjustment in the script below. Similarly, this can be adjusted to make YAC Listner work to send callerID to MAC/PCs.


My Environment:
1. Ubuntu server/PI (openelec) - rsyslog server
2. 2 x PI2 Running Openelec Milhouse Build
3. 1 x Cisco SPA 122 Phone Adapter.
4. A BYOD VOIP Provider

I. Prepare Linksys/Cisco Phone Adapter
1. Login into Phone Adapter with admin privilege
2. Navigate to Voice - > System -> Enter syslog server IP address. Click "Save".
3. Navigate to Line 1 -> SIP Debug Option: -> select "full excl. OPT|NTFY|REG" from the drop down list. Click "Save"
4. Repeat step 3 if you are using Line 2 (Optional)


II. Prepare rSyslog Server (Ubuntu)
Note: Raspberry PI's rsyslog service is suffice but I am using separate Ubuntu as example here.

1. Install rsyslog and configure if it is not already.
2. Configure rsyslog to listen to udp 514. un-comment following lines.
nano /etc/rsyslog.conf
Code:
# provides UDP syslog reception
        $ModLoad imudp
        $UDPServerRun 514
3. Allow syslog from phone adapter. Add following to rsyslog.conf. xxx = your phone adapter IP address
Code:
$AllowedSender UDP, 127.0.0.1, 192.168.1.xxx
4. Save rsyslog.conf
5. Restart rsyslog
Code:
service rsyslog restart

III. Rsyslog Rules
1. Don't log local0 and user stuff to /var/log/messages since the Linksys/Cisco SPA uses those facility categories
Code:
nano /etc/rsyslog.d/50-default.conf
2. Add following to skip local0 and redirect the log to a separate file

Code:
#Don't log local0 and user stuff to /var/log/messages since the SPA uses those facility categories
        *.info;user.none;local0.none;syslog.none;mail.none;authpriv.none;cron.none /var/log/messages
        
        #SPA is logging at user and local0 facility categories.
        #user.debug;local0.debug;    /var/log/voip/voip.log
        if ($syslogfacility-text == 'user' or $syslogfacility-text == 'local0') and ($msg contains "Ringing") then /var/log/voip/voip.log

3. save "50-default.conf" and restart rsyslog service


IV. Rsyslog default behavior
1. Add '-r' switch default/rsyslog

Code:
nano /etc/default/rsyslog
        RSYSLOGD_OPTIONS="-r"
2. Save rsyslog and restart rsyslog service

V. Install Incron
(Note: Still available but sadly, i heard, the incron development incron has stopped.)
1. Install incron
Code:
apt-get install incron
2. Add user(s) to allow list.
Code:
nano /etc/incron.allow
        root, yourusername
3. Save "incron.allow" and exit


VI. User crontab
1.
Code:
sudo incrontab -e
2. add log file defined in Step III and 2 here
Code:
"/var/log/voip/voip.log IN_MODIFY /bin/bash /home/yourusername/callerid.sh"
3. save and exit

VII. Kodi Setting
1.Enable the webserver
Code:
Settings>Services>Webserver>Allow control of kodi via HTTP
in kodi
2. Note username and password if any and adjust callerid.sh setting acordingly


callerid.sh
Note: Make you script (callerid.sh) executable

Code:
chmod 777 ~/callerid.sh

Code:
#!/bin/bash
##############
## lockfile ##
##############
exec 9>/tmp/callerid-lockfile
if ! flock -n 9  ; then
   exit 1
fi
# this script now runs under the lock until 9 is closed. it will be closed automatically when the script ends.

####################
##  Configuration ##
####################

LOGLOCATION="/var/log/voip/voip.log"    # the location of your syslog log

kODIipBegin="192.168.1."   # only the first 3 (octates) values and dots for your LAN IP Address
kODIipEnd=(109 108)    # the last value(s) of the ip address(es) of your kodi devices(s)
kODIPort="80"        # default port for kodi webserver
kODIPOPUPDURATION="20000"     # in msecs (i.e., 20000 = an kodi popup for 20 seconds)
NotifiLogo="/storage/pictures/call.png" # Notification Logo
kODIUserName="" # Kodi username
kODIPassword="" # Kodi Password



#Check to see if username is being used
if [ -z "$kODIUserName" ]; then
       kODIUserPass=""
   else
       kODIUserPass="$kODIUserName:$[email protected]"
fi



BEGIN="--data-binary"
END="-H content-type:application/json;"


####################################
##   Extract string from the log  ##
####################################

sleep 0.1        # allow a bit of time for log messages to complete.
STRINGFROMLOG=$(tac $LOGLOCATION | grep -B 0 -m1 'SIP/2.0 180 Ringing#015#012To:.*' | tac)        
NAMEPHONE=$(grep -o -P ".{0,0}From.{0,50}" <<< "$STRINGFROMLOG")

NAME="CALLING..:"$(grep -oP '"\K[^"\047]+(?=["\047])' <<< "$NAMEPHONE") #Extract name btwn ""
NAME=${NAME//[[:space:]]/,} # Remove all spaces from the name
NAMEPHONE=${NAMEPHONE//:/} #remote : from the

PHONE=$(grep -oP '"\K[^"\072]+(?=["\100])' <<< "$NAMEPHONE")       # gets all characters after the last space (i.e., the number)
PHONE=$(grep -Eo '[0-9]{1,10}' <<< "$PHONE")

for i in ${kODIipEnd[@]}; do
curl $BEGIN '{"jsonrpc": "2.0", "params": {"message": "-----------:'$PHONE'", "image": "'$NotifiLogo'", "displaytime": '$kODIPOPUPDURATION', "title": "'$NAME'"}, "method": "GUI.ShowNotification", "id": "libNotification"}' $END 'http://'$kODIUserPass$kODIipBegin${i}':'$kODIPort'/jsonrpc'
#echo $BEGIN '{"jsonrpc": "2.0", "params": {"message": "-----------:'$PHONE'", "image": "'$NotifiLogo'", "displaytime": '$kODIPOPUPDURATION', "title": "'$NAME'"}, "method": "GUI.ShowNotification", "id": "libNotification"}' $END 'http://'$kODIUserPass$kODIipBegin${i}':'$kODIPort'/jsonrpc'
done

exit 0

Edit: Sorry - Could not upload images here. Call.png and some screenshots are missing at this point.
Reply
#2
Rather than use incrontab it would be easier to run callerid.sh as a "daemon" and just tail syslog with the output feeding a read loop, this would also eliminate the need for the flock. For example:

Code:
#!/bin/bash

  tail -qF -n0 /var/log/syslog | \
    while read -r line; do
      if [[ ${line} =~ callerid-pattern ]]; then
#do something callerid related
      fi
    done

You might even be able to use netcat/nc in place of syslog and tail.
Texture Cache Maintenance Utility: Preload your texture cache for optimal UI performance. Remotely manage media libraries. Purge unused artwork to free up space. Find missing media. Configurable QA check to highlight metadata issues. Aid in diagnosis of library and cache related problems.
Reply
#3
You're welcome for the clunky script! :-)

I'm not a programmer at all and pieced that ugly bit of code together from other stuff I saw on the internet. The flock stuff never worked correctly for me and I had actually removed it from my script. I've since moved on from a Ubuntu server and now use unRaid. I haven't looked at getting the script working again because most of my calls are now on my mobile and my voip phone is hardly used. Good luck with improving the script though!

Edit:
This should probably be in the general part of the forum as it's not specific to the RPi.
Reply
#4
cool stuff, will try on my SPA2102 once I have switched VOIP providers. I assume the script will work the same for my 2102.
Reply
#5
Thank you @Milhouse for the suggestions. I am getting good result from the tail and gawk. I will revamp the script and the installation steps, and report here again once i test them.

Code:
tail -qF -n0 /var/log/syslog -f | gawk -v FS=';' '{match($3, /From: (.*)@/, m); print m[1]}'

@doug thank you for the script. It works wonder for me.

@jebise - yes linksys spa2102 will work with this.

thanks
NPuser
Reply
#6
any updates on the script? I configured my syslog on a separate Pi but do not see anything in voip.log, my other devices are logging just fine. Does the debug level need to be updated under Voice > System on the ATA? mine is set to 0
Reply
#7
need some help, I have followed everything exactly as OP but voip.log stays empty
Reply
#8
taking this thread as a great place to start, i began work on this today using an Obi202 unit that i got configured for Google voice calls yesterday.

because the logging works slightly differently in the obi units i was looking for a way around the callerid.sh script. I decided to use perl for this and to keep the idea of sending a curl to a kodi unit.

I've got everything running right up to the point of sending the curl to kodi and am stuck there.

so how do i send this curl command to kodi with a variable in it?

Code:
curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"id":1,"jsonrpc":"2.0","method":"GUI.ShowNotification","params":{"title":"Incoming Phone Call","message":"$callerid"}}' http://192.168.1.100:8080/jsonrpc

where $callerid is the number i want to present on the popup in kodi.

any help appreciated Smile
Reply



Logout Mark Read Team Forum Stats Members Help
Pi-Kodi CallerID - VoIP provider with supported VOIP Adapters0
This forum uses Lukasz Tkacz MyBB addons.