96_SIP.pm 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408
  1. ###############################################################################
  2. #
  3. # $Id: 96_SIP.pm 17070 2018-07-31 19:02:39Z Wzut $
  4. # 96_SIP.pm
  5. # Based on FB_SIP from werner.meines@web.de
  6. #
  7. # Forum : https://forum.fhem.de/index.php/topic,67443.0.html
  8. #
  9. ###############################################################################
  10. #
  11. # (c) 2017 Copyright: Wzut & plin
  12. # All rights reserved
  13. #
  14. # This script is free software; you can redistribute it and/or modify
  15. # it under the terms of the GNU General Public License as published by
  16. # the Free Software Foundation; either version 2 of the License, or
  17. # (at your option) any later version.
  18. #
  19. # The GNU General Public License can be found at
  20. # http://www.gnu.org/copyleft/gpl.html.
  21. # A copy is found in the textfile GPL.txt and imPORTant notices to the license
  22. # from the author is found in LICENSE.txt distributed with these scripts.
  23. #
  24. # This script is distributed in the hope that it will be useful,
  25. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. # GNU General Public License for more details.
  28. #
  29. ##################################################################################
  30. #######################################################################
  31. # need: Net::SIP (cpan install Net::SIP)
  32. #
  33. #
  34. # convert audio to PCM 8000 :
  35. # sox <file>.wav -t raw -r 8000 -c 1 -e a-law <file>.alaw
  36. # oder
  37. # sox <file> -r 8000 -c 1 -e a-law <file>.wav
  38. #
  39. ########################################################################
  40. package main;
  41. use strict;
  42. use warnings;
  43. use POSIX qw( strftime );
  44. use Net::SIP qw//;
  45. use Net::SIP::Packet;
  46. use IO::Socket;
  47. use Socket;
  48. use Net::Domain qw(hostname hostfqdn);
  49. use Blocking; # http://www.fhemwiki.de/wiki/Blocking_Call
  50. #use Data::Dumper;
  51. my $sip_version ="V1.91 / 31.07.18";
  52. my $ua; # SIP user agent
  53. my @fifo;
  54. my %sets = (
  55. "call" => "",
  56. "listen:noArg" => "",
  57. "reject:noArg" => "",
  58. "reset:noArg" => "",
  59. "fetch:noArg" => "",
  60. "password" => ""
  61. );
  62. #my %gets = (
  63. #"search_phonebook" => "",
  64. #"show_phonebook" => ""
  65. # );
  66. sub SIP_Initialize($$)
  67. {
  68. my ($hash) = @_;
  69. $hash->{DefFn} = "SIP_Define";
  70. $hash->{UndefFn} = "SIP_Undef";
  71. $hash->{ShutdownFn} = "SIP_Undef";
  72. $hash->{SetFn} = "SIP_Set";
  73. #$hash->{GetFn} = "SIP_Get";
  74. $hash->{NotifyFn} = "SIP_Notify";
  75. $hash->{AttrFn} = "SIP_Attr";
  76. $hash->{AttrList} = "sip_watch_listen ". #
  77. "sip_ringtime ". #
  78. "sip_waittime ". #
  79. "sip_ip ". #
  80. "sip_port ". #
  81. "sip_user ". #
  82. "sip_registrar ". #
  83. "sip_from ". #
  84. "sip_call_audio_delay:0,0.25,0.5,0.75,1,1.25,1.5,1.75,2,2.25,2.5,2.75,3 ". #
  85. "sip_audiofile_call ". #
  86. "sip_audiofile_dtmf ". #
  87. "sip_audiofile_ok ". #
  88. "sip_audiofile_wfp ". # CE
  89. "sip_dtmf_size:1,2,3,4 ". #
  90. "sip_dtmf_send:audio,rfc2833 ". #
  91. "sip_dtmf_loop:once,loop ". #
  92. "sip_listen:none,dtmf,wfp,echo ". #
  93. "sip_filter ". #
  94. "sip_blocking ". #
  95. "sip_elbc:yes,no ". #
  96. "sip_force_interval ". #
  97. "sip_force_max ". #
  98. "T2S_Device ". #
  99. "T2S_Timeout ". #
  100. "audio_converter:sox,ffmpeg ". #
  101. "history_file ".
  102. "history_size ".
  103. "phonebook ".
  104. "disabled:0,1 ".$readingFnAttributes;
  105. }
  106. sub SIP_Define($$)
  107. {
  108. my ($hash, $def) = @_;
  109. my @a = split("[ \t][ \t]*", $def);
  110. my $name = shift @a;
  111. my $addr = "0.0.0.0";
  112. $hash->{STATE} = "defined";
  113. $hash->{VERSION} = $sip_version;
  114. $hash->{".reset"} = 0;
  115. $attr{$name}{sip_ringtime} = '3' unless (exists($attr{$name}{sip_ringtime}));
  116. $attr{$name}{sip_user} = '620' unless (exists($attr{$name}{sip_user}));
  117. $attr{$name}{sip_registrar} = 'fritz.box' unless (exists($attr{$name}{sip_registrar}));
  118. $attr{$name}{sip_listen} = 'none' unless (exists($attr{$name}{sip_listen}));
  119. $attr{$name}{sip_dtmf_size} = '2' unless (exists($attr{$name}{sip_dtmf_size}));
  120. $attr{$name}{sip_dtmf_loop} = 'once' unless (exists($attr{$name}{sip_dtmf_loop}));
  121. $attr{$name}{sip_dtmf_send} = 'audio' unless (exists($attr{$name}{sip_dtmf_send}));
  122. $attr{$name}{sip_elbc} = 'yes' unless (exists($attr{$name}{sip_elbc}));
  123. $attr{$name}{sip_from} = 'sip:'.$attr{$name}{sip_user}.'@'.$attr{$name}{sip_registrar} unless (exists($attr{$name}{sip_from}));
  124. $attr{$name}{history_size} = 0 unless (exists($attr{$name}{history_size}));
  125. $attr{$name}{history_file} = "./log/$name.sip" unless (exists($attr{$name}{history_file}));
  126. unless (exists($attr{$name}{sip_ip}))
  127. {
  128. eval { $addr = inet_ntoa(scalar(gethostbyname(hostfqdn()))); };
  129. if ($@)
  130. {
  131. Log3 $name,2,"$name, please check your FQDN hostname -> $@";
  132. eval { $addr = inet_ntoa(scalar(gethostbyname(hostname()))); };
  133. Log3 $name,2,"$name, please check your hostname -> ".$@ if ($@);
  134. }
  135. $attr{$name}{sip_ip} = $addr;
  136. }
  137. RemoveInternalTimer($hash);
  138. InternalTimer(gettimeofday()+5, "SIP_updateConfig", $hash);
  139. notifyRegexpChanged($hash,"global");
  140. return undef;
  141. }
  142. sub SIP_Notify($$)
  143. {
  144. # $hash is my hash, $dev_hash is the hash of the changed device
  145. my ($hash, $dev_hash) = @_;
  146. my $events = deviceEvents($dev_hash,0);
  147. return undef if ($dev_hash->{NAME} ne AttrVal($hash->{NAME},"T2S_Device",""));
  148. my $val = ReadingsVal($dev_hash->{NAME},"lastFilename","");
  149. return undef if ((!$val) || (index(@{$events}[0],"lastFilename") == -1));
  150. if (defined($hash->{audio1}) ||
  151. defined($hash->{audio2}) ||
  152. defined($hash->{audio3}) ||
  153. (defined($hash->{callnr}) && defined($hash->{ringtime})))
  154. { SIP_wait_for_t2s($hash);}
  155. return undef;
  156. }
  157. sub SIP_Attr (@)
  158. {
  159. my ($cmd, $name, $attrName, $attrVal) = @_;
  160. my $hash = $defs{$name};
  161. if ($cmd eq "set")
  162. {
  163. if (substr($attrName ,0,4) eq "sip_")
  164. {
  165. $_[3] = $attrVal;
  166. $hash->{".reset"} = 1 if defined($hash->{LPID});
  167. }
  168. elsif (($attrName eq "disable") && ($attrVal == 1))
  169. {
  170. readingsSingleUpdate($hash,"state","disabled",1);
  171. $_[3] = $attrVal;
  172. $hash->{".reset"} = 1 if defined($hash->{LPID});
  173. }
  174. elsif ($attrName eq "audio_converter")
  175. {
  176. my $res = qx(which $attrVal);
  177. $res =~ s/\n//;
  178. $hash->{AC} = ($res) ? $res : undef;
  179. }
  180. elsif ($attrName eq "T2S_Device")
  181. {
  182. $_[3] = $attrVal;
  183. #$hash->{NOTIFYDEV} = $attrVal;
  184. notifyRegexpChanged($hash,$attrVal.":lastFilename");
  185. }
  186. }
  187. elsif ($cmd eq "del")
  188. {
  189. if (substr($attrName,0,4) eq "sip_")
  190. {
  191. $_[3] = $attrVal;
  192. $hash->{".reset"} = 1 if defined($hash->{LPID});
  193. }
  194. elsif ($attrName eq "audio_converter")
  195. {
  196. $_[3] = $attrVal;
  197. delete $hash->{AC};
  198. }
  199. elsif ($attrName eq "T2S_Device")
  200. {
  201. $_[3] = $attrVal;
  202. #delete $hash->{NOTIFYDEV};
  203. notifyRegexpChanged($hash,"global");
  204. }
  205. }
  206. if ($hash->{".reset"})
  207. {
  208. Log3 $name,5,"$name , SIP_Attr : reset";
  209. InternalTimer(gettimeofday()+1,"SIP_updateConfig",$hash);
  210. }
  211. return undef;
  212. }
  213. sub SIP_updateConfig($)
  214. {
  215. # this routine is called 5 sec after the last define of a restart
  216. # this gives FHEM sufficient time to fill in attributes
  217. my ($hash) = @_;
  218. my $name = $hash->{NAME};
  219. my $error;
  220. if (!$init_done)
  221. {
  222. RemoveInternalTimer($hash);
  223. InternalTimer(gettimeofday()+5,"SIP_updateConfig", $hash);
  224. return;
  225. }
  226. ## kommen wir via reset Kommando ?
  227. if ($hash->{".reset"})
  228. {
  229. $hash->{".reset"} = 0;
  230. RemoveInternalTimer($hash);
  231. if(defined($hash->{LPID}))
  232. {
  233. Log3 $name,4, "$name, Listen Kill PID : ".$hash->{LPID};
  234. BlockingKill($hash->{helper}{LISTEN_PID});
  235. delete $hash->{helper}{LISTEN_PID};
  236. delete $hash->{LPID};
  237. readingsSingleUpdate($hash,"listen_alive","no",1);
  238. Log3 $name,4,"$name, Reset Listen done";
  239. }
  240. if(defined($hash->{CPID}))
  241. {
  242. Log3 $name,4, "$name, CALL Kill PID : ".$hash->{CPID};
  243. BlockingKill($hash->{helper}{CALL_PID});
  244. delete $hash->{helper}{CALL_PID};
  245. delete $hash->{CPID};
  246. Log3 $name,4,"$name, Reset Call done";
  247. }
  248. }
  249. if (IsDisabled($name))
  250. {
  251. readingsSingleUpdate($hash,"state","disabled",1);
  252. return undef;
  253. }
  254. my $t2s = AttrVal($name,"T2S_Device",undef);
  255. #$hash->{NOTIFYDEV} = $t2s if defined($t2s);
  256. notifyRegexpChanged($hash, $t2s.":lastFilename") if defined($t2s);
  257. if (AttrVal($name,"audio_converter","") && defined($t2s))
  258. {
  259. my $converter = AttrVal($name,"audio_converter","");
  260. my $res = qx(which $converter);
  261. $res =~ s/\n//;
  262. $hash->{AC} = ($res) ? $res : undef;
  263. }
  264. if (AttrVal($name,"sip_listen", "none") ne "none")
  265. {
  266. $error = SIP_try_listen($hash);
  267. if ($error)
  268. {
  269. Log3 $name, 1, $name.", listen -> $error";
  270. readingsBeginUpdate($hash);
  271. readingsBulkUpdate($hash,"state","error");
  272. readingsBulkUpdate($hash,"last_error",$error);
  273. readingsBulkUpdate($hash,"listen_alive","no");
  274. readingsEndUpdate($hash, 1 );
  275. return undef;
  276. }
  277. }
  278. else
  279. {
  280. readingsBeginUpdate($hash);
  281. readingsBulkUpdate($hash,"state","initialized");
  282. readingsBulkUpdate($hash,"listen_alive","no");
  283. readingsEndUpdate($hash, 1 );
  284. }
  285. return undef;
  286. }
  287. sub SIP_Register($$)
  288. {
  289. my ($hash,$type) = @_;
  290. my $name = $hash->{NAME};
  291. $hash->{LPID} = $$;
  292. my $logname = $name."[".$hash->{LPID}."]";
  293. my $ip = AttrVal($name,"sip_ip","");
  294. my $port = int(AttrVal($name,"sip_port",0));
  295. my $leg;
  296. return "missing attribute sip_ip" if (!$ip);
  297. return "this is the IP address of your registrar , not your FHEM !" if ($ip eq AttrVal($name,"sip_registrar",""));
  298. return "invalid IP address $ip" if (($ip eq "0.0.0.0") || ($ip eq "127.0.0.1"));
  299. if ($port)
  300. {
  301. $port +=10 if ($type eq "calling");
  302. Log3 $name,4,"$logname, trying to use port $port";
  303. $leg = IO::Socket::INET->new(Proto => 'udp', LocalHost => $ip, LocalPort => $port);
  304. # if port is already used try another one
  305. if (!$leg)
  306. {
  307. Log3 $name,1,"$logname, cannot open port $port at $ip : ".$!;
  308. $port += 10;
  309. $leg = IO::Socket::INET->new(Proto => 'udp', LocalHost => $ip, LocalPort => $port) || return "can't open port $port at $ip : ".$!;
  310. Log3 $name,2,"$logname, using secundary port $port with IP $ip";
  311. }
  312. close($leg);
  313. $leg = $ip.":".$port;
  314. }
  315. else
  316. {
  317. $leg = $ip;
  318. Log3 $name,4,"$logname, using Leg.pm to find a free port";
  319. }
  320. my $registrar = AttrVal($name,"sip_registrar","fritz.box");
  321. my $user = AttrVal($name,"sip_user","620");
  322. my $from = AttrVal($name,"sip_from","sip:".$user."@".$registrar);
  323. # create new agent
  324. $ua = Net::SIP::Simple->new(
  325. registrar => $registrar,
  326. domain => $registrar,
  327. leg => $leg,
  328. from => $from,
  329. auth => [ $user , SIP_readPassword($name) ]);
  330. # Register agent
  331. # optional registration
  332. my $sub_register;
  333. $sub_register = sub
  334. {
  335. my $expire = $ua->register(registrar => $registrar ) || return "registration failed: ".$ua->error;
  336. my $cmd = "ps -e | grep '".$hash->{parent}." '";
  337. my $result = qx($cmd);
  338. if (index($result,"perl") == -1)
  339. {
  340. Log3 $name,1,"$logname, can´t find my parent ".$hash->{parent}." in process list !";
  341. die;
  342. }
  343. Log3 $name,4,"$logname, register new expire : ".FmtDateTime(time()+$expire);
  344. if (AttrVal($name,"sip_listen","none") ne "none")
  345. {
  346. BlockingInformParent("SIP_rBU", [$name,"state;$type|listen_alive;".$hash->{LPID}."|expire;$expire"],0);
  347. }
  348. else
  349. {
  350. BlockingInformParent("SIP_rSU", [$name, "state;$type"], 0);
  351. }
  352. # need to refresh registration periodically
  353. $ua->add_timer( $expire/2, $sub_register );
  354. };
  355. $sub_register->();
  356. if($ua->register) # returned expires time or undef if failed
  357. {
  358. #Log3 $name,4,Dumper($ua);
  359. return 0;
  360. }
  361. my $ret = ($ua->error) ? $ua->error : "registration error";
  362. return $ret;
  363. }
  364. sub SIP_CALLStart($)
  365. {
  366. my ($arg) = @_;
  367. return unless(defined($arg));
  368. my ($name,$nr,$ringtime,$msg,$repeat) = split("\\|",$arg);
  369. my $hash = $defs{$name};
  370. my $logname = $name."[".$$."]";
  371. $ua = undef;
  372. my $rtp_done = 0;
  373. my $dtmf = 'ABCD*#123--4567890';
  374. my $delay = AttrVal($name,"sip_call_audio_delay",0); # Verzoegerung in 1/4 Sekunden Schritten
  375. my $w = ($delay) ? -1 : 0;
  376. my $fi = 0;
  377. #$repeat = 0 if (!$repeat);
  378. my $packets = int($delay*50);
  379. my $timeout = 0;
  380. my $final;
  381. my $peer_hangup;
  382. my $peer_hangup2;
  383. my $stopvar;
  384. my $state;
  385. my $no_answer;
  386. my $call;
  387. my $codec;
  388. my $call_established = 0;
  389. my $calltime =0;
  390. my @files;
  391. my $anz;
  392. my $stat;
  393. my $ph_ok = 0;
  394. my $sound_of_silence = sub
  395. {
  396. return unless $packets-- > 0;
  397. return chr(0) x 160; # 160 bytes for PCMU/8000 = 1/50 Sekunde Sound
  398. };
  399. $hash->{parent} = getppid();
  400. Log3 $name,4,"$logname, my parent is ".$hash->{parent};
  401. my $error = SIP_Register($hash,"calling");
  402. return $name."|0|CallRegister: $error|0" if ($error);
  403. $hash->{helper}{LALL_EST} = 0;
  404. if ((substr($msg,0,1) ne "-") && $msg)
  405. {
  406. $codec = "PCMA/8000" if ($msg =~ /\.al(.+)$/);
  407. $codec = "PCMU/8000" if ($msg =~ /\.ul(.+)$/);
  408. return $name."|0|CallStart: please use filetype .alaw (for a-law) or .ulaw (for u-law)|0" if !defined($codec);
  409. push @files,$sound_of_silence if ($delay);
  410. if ($repeat < 0) { $repeat = $repeat * -1; $ph_ok= 1; }
  411. for(my $i=0; $i<=$repeat; $i++) { push @files,$msg; }
  412. $anz = @files;
  413. Log3 $name,4,"$logname, CallStart with $anz files - first file : $files[0] - $codec , repeat $repeat";
  414. $call = $ua->invite( $nr,
  415. init_media => $ua->rtp('send_recv', $files[0]),
  416. cb_rtp_done => \$rtp_done,
  417. cb_final => sub { my ($status,$self,%info) = @_;
  418. $final = $info{code};
  419. $stat = $status;
  420. Log3 $name,4,"$logname, cb_final - status : $status" if (!defined($final));
  421. Log3 $name,4,"$logname, cb_final - status : $status - final : $final" if (defined($final));
  422. if (($status eq "FAIL") && defined($final))
  423. {
  424. if (int($final) == 481) { BlockingInformParent("SIP_rSU", [$name, "call_state;ringing"], 0);} # bis Net::SIP 0.808
  425. elsif (int($final) == 486) { $fi=1; } # canceled
  426. elsif (int($final) == 603) { $fi=1; } # declined - ab Net::SIP 0.812
  427. }
  428. elsif (($status eq "OK") && !defined($final) && !$call_established) # der Angrufene hat abgenommen
  429. {
  430. Log3 $name,4, $logname.", call established";
  431. $hash->{helper}{CALL_EST} = time();
  432. BlockingInformParent("SIP_rSU", [$name, "call_state;established"], 0);
  433. $call_established++; # nur 1x , bei mehr als einem File kommen wir ofters hier vorbei
  434. }
  435. },
  436. recv_bye => \$peer_hangup,
  437. #ring_time => 5,
  438. #cb_noanswer => \$no_answer, klappt hier nicht wir gehen ueber add_timer
  439. rtp_param => [8, 160, 160/8000, $codec]) || return $name."|0|invite failed: ".$ua->error;
  440. }
  441. else
  442. {
  443. $dtmf = (substr($msg,0,1) eq "-") ? substr($msg,1) : $dtmf;
  444. Log3 $name,4,"$logname, CallStart DTMF : $dtmf";
  445. $delay = 0; # wenn delay sein muss dann ueber DTMF ----
  446. $repeat = 0; # keine Wiederholungen
  447. $call = $ua->invite($nr,
  448. init_media => $ua->rtp( 'recv_echo',undef,0 ),
  449. rtp_param => [0, 160, 160/8000, 'PCMU/8000'],
  450. cb_final => sub { my ($status,$self,%info) = @_;
  451. $final = $info{code};
  452. Log3 $name,4,"$logname, cb_final - Status : $status" if (!defined($final));
  453. Log3 $name,4,"$logname, cb_final - status : $status - final : $final" if (defined($final));
  454. if (($status eq "FAIL") && defined($final))
  455. {
  456. if (int($final) == 481) { BlockingInformParent("SIP_rSU", [$name, "call_state;ringing"], 0); } # bis Net::SIP 0.808
  457. elsif (int($final) == 486) { $fi=1; } # canceled
  458. elsif (int($final) == 603) { $fi=1; } # declined - ab Net::SIP 0.812
  459. }
  460. elsif (($status eq "OK") && !defined($final)) # der Angrufene hat abgenommen
  461. {
  462. Log3 $name,4, $logname.", call established";
  463. $hash->{helper}{CALL_EST} = time();
  464. BlockingInformParent("SIP_rSU", [$name, "call_state;established"], 0);
  465. $call_established = 1; # setzen für die spätere Entscheidung bye oder cancel
  466. }
  467. },
  468. #cb_noanswer => \$no_answer,
  469. #ring_time => 5, siehe oben -> add_timer
  470. cb_cleanup => sub {0},
  471. recv_bye => \$peer_hangup) || return $name."|0|invite failed ".$ua->error."|0";
  472. if (AttrVal($name,"sip_dtmf_send","audio") eq "audio")
  473. { $call->dtmf( $dtmf, methods => 'audio', duration => 500, cb_final => \$rtp_done); }
  474. else { $call->dtmf( $dtmf, cb_final => \$rtp_done); }
  475. }
  476. return "$name|0|invite call failed |0".$call->error if ($call->error);
  477. Log3 $name,4,"$logname, calling : $nr";
  478. BlockingInformParent("SIP_rSU", [$name, "call_state;calling $nr"], 0);
  479. #return "$name|1|no answer" if ($no_answer);
  480. $ua->add_timer($ringtime,\$stopvar);
  481. $ua->loop( \$stopvar,\$peer_hangup,\$rtp_done,\$fi );
  482. $timeout = 1 if defined($stopvar); # hat der bereits zugeschlagen ?
  483. Log3 $name,5,"$logname, 0. Ende des ersten Loops";
  484. Log3 $name,5,"$logname, 1. rtp_done : $rtp_done" if defined($rtp_done);
  485. Log3 $name,5,"$logname, 2. fi : $fi" if defined($fi);
  486. Log3 $name,5,"$logname, 3. Final : $final" if defined($final);
  487. Log3 $name,5,"$logname, 4. timeout : ".$timeout;
  488. Log3 $name,5,"$logname, 5. peer_hangup : $peer_hangup" if defined($peer_hangup);
  489. Log3 $name,5,"$logname, 6. call_established : ".$call_established;
  490. Log3 $name,5,"$logname, 7. no_answer : $no_answer" if defined($no_answer);
  491. # Lebt der Call noch und gibt es ueberhaupt etwas zum wiederholen ?
  492. while ( !$peer_hangup && !$peer_hangup2 && !$fi && !$stopvar && $msg && ($anz > 1))
  493. {
  494. shift(@files); # done with file
  495. @files || last; # raus hier sobald kein File mehr da ist
  496. Log3 $name,4,"$logname, next file : $files[0]" if defined($files[0]);
  497. Log3 $name,3,"$logname, opps no file" if !defined($files[0]);
  498. # re-invite on current call for next file
  499. $rtp_done = undef; # wichtig ! u.U. haengen wir hier fest wenn der Anrufer jetzt auflegt
  500. select(undef, undef, undef, 0.1); # minimale pause
  501. $call->reinvite(
  502. init_media => $ua->rtp('send_recv', $files[0]),
  503. #rtp_param => [0, 160, 160/8000, 'PCMU/8000'], unbedingt weglassen ! fuehrt zu Verzerrungen bei der Wiedergabe
  504. cb_rtp_done => \$rtp_done,
  505. recv_bye => \$peer_hangup2, # FIXME: do we need to repeat this? Wzut : I think so ...
  506. ) || return $name."|0|reinvite failed: ".$ua->error."|0";
  507. $ua->loop( \$rtp_done,\$peer_hangup2,\$peer_hangup,\$stopvar );
  508. Log3 $name,4,"$logname, loop rtp_done : $rtp_done" if defined($rtp_done);
  509. $w++;
  510. }
  511. $timeout = 1 if defined($stopvar); # nach eventuellen reinvte nochmal testen
  512. # timeout or dtmf done, hang up
  513. if ( $timeout || $rtp_done)
  514. {
  515. $stopvar = undef;
  516. if ($timeout && !$call_established)
  517. {
  518. $hash->{helper}{CALL_STATUS} = "cancel";
  519. Log3 $name,5,"$logname, call->cancel";
  520. $call->cancel( cb_final => \$stopvar );
  521. }
  522. else
  523. {
  524. $hash->{helper}{CALL_STATUS} = "bye";
  525. Log3 $name,5,"$logname, call->bye";
  526. $call->bye( cb_final => \$stopvar );
  527. }
  528. $ua->loop( \$stopvar );
  529. }
  530. $calltime = ($hash->{helper}{CALL_EST}) ? int(time()-$hash->{helper}{CALL_EST}) : 0;
  531. Log3 $name,5,"$logname, RTP done : $rtp_done" if defined($rtp_done);
  532. Log3 $name,5,"$logname, Hangup : $peer_hangup" if defined($peer_hangup);
  533. Log3 $name,5,"$logname, Hangup2 : $peer_hangup2" if defined($peer_hangup2);
  534. Log3 $name,5,"$logname, Timeout : $timeout";
  535. Log3 $name,5,"$logname, Final : $final" if defined($final);
  536. Log3 $name,5,"$logname, while : $w" if defined($w);
  537. Log3 $name,5,"$logname, Status : $stat" if defined($stat);
  538. Log3 $name,4,"$logname, Calltime : $calltime" if defined($calltime);
  539. if (defined($rtp_done))
  540. {
  541. if ($rtp_done eq "OK") {return $name."|1|ok|$calltime";} # kein Audio
  542. else
  543. {
  544. if (defined($final))
  545. {
  546. my $txt;
  547. $txt = "canceled" if (int($final) == 486);
  548. $txt = "no answer" if (int($final) == 487);
  549. $txt = "declined" if (int($final) == 603);
  550. return $name."|1|$txt|$calltime" if ($txt);
  551. }
  552. else {return $name."|1|ok|$calltime" if ($rtp_done !=0);}
  553. }
  554. }
  555. # immer noch kein richtiger Text zur Rueckgabe ?
  556. $final = "unknown" if (!defined($final) && !$timeout);
  557. $final = "timeout" if (!defined($final) && $timeout);
  558. $final = "peer hangup" if defined($peer_hangup);
  559. $final = "peer_hangup" if defined($peer_hangup2); # ts,ts hat der doch glatt im reinvite noch abgebochen
  560. # geben wir doch ok zurueck wenn er sich die Nachricht min 1x angehört hat
  561. return $name."|1|ok peer hangup|$calltime" if ($ph_ok && defined($peer_hangup2) && ($stat eq "OK") && ($w>0)); # bei delay 1x mehr !
  562. return $name."|1|$final|$calltime";
  563. }
  564. sub SIP_CALLDone($)
  565. {
  566. my ($string) = @_;
  567. return unless(defined($string));
  568. my @r = split("\\|",$string);
  569. my $hash = $defs{$r[0]};
  570. my $error = (defined($r[1])) ? $r[1] : "0";
  571. my $final = (defined($r[2])) ? $r[2] : "???";
  572. my $calltime = (defined($r[3])) ? $r[3] : 0;
  573. my $name = $hash->{NAME};
  574. my $success = (substr($final,0,2) eq "ok") ? 1 : 0;
  575. my @a;
  576. my @fo = (300,0,0);
  577. my (undef,$nr,$ringtime,$msg,$repeat,$force) = split("\\|",$hash->{helper}{CALL}); # zerlegen wir den Original Call
  578. Log3 $name, 4,"$name, CALLDone -> $string";
  579. $hash->{helper}{CALL_TIME} = $calltime;
  580. $hash->{helper}{CALL_BYE} = $final;
  581. $hash->{helper}{CALL_ERROR} = $error;
  582. $hash->{helper}{CALL_NAME} = SIP_search_phonebook($hash,$name,$nr);
  583. SIP_write_history($hash,$name);
  584. delete($hash->{helper}{CALL_PID}) if (defined($hash->{helper}{CALL_PID}));
  585. delete($hash->{CPID}) if (defined($hash->{CPID}));
  586. delete $hash->{lastnr} if (defined($hash->{lastnr}));
  587. if ($force)
  588. {
  589. $force =~ s/^\&//;
  590. @fo = split(",",$force);
  591. $fo[2]++ if(!$success); # Anzahl bisheriger Durchläufe
  592. }
  593. if ($error ne "1")
  594. {
  595. readingsBeginUpdate($hash);
  596. readingsBulkUpdate($hash, "call","done");
  597. readingsBulkUpdate($hash, "call_time",int($calltime)) if defined($calltime);
  598. readingsBulkUpdate($hash, "last_error",$final);
  599. readingsBulkUpdate($hash, "call_state","fail");
  600. readingsBulkUpdate($hash, "call_success","0");
  601. readingsBulkUpdate($hash, "call_attempt",$fo[2]) if ($force);
  602. readingsBulkUpdate($hash, "call_attempt","0") if (!$force);
  603. readingsBulkUpdate($hash, "state",$hash->{'.oldstate'}) if defined($hash->{'.oldstate'});
  604. readingsEndUpdate($hash, 1);
  605. }
  606. else
  607. {
  608. readingsBeginUpdate($hash);
  609. readingsBulkUpdate($hash, "call","done");
  610. readingsBulkUpdate($hash, "call_state",lc($final));
  611. readingsBulkUpdate($hash, "call_success",$success);
  612. readingsBulkUpdate($hash, "call_time",int($calltime)) if defined($calltime);
  613. readingsBulkUpdate($hash, "state",$hash->{'.oldstate'}) if defined($hash->{'.oldstate'});
  614. readingsBulkUpdate($hash, "call_attempt",$fo[2]) if ($force);
  615. readingsBulkUpdate($hash, "call_attempt","0") if (!$force);
  616. readingsEndUpdate($hash, 1);
  617. }
  618. if ($force && !$success)
  619. {
  620. $repeat++; $repeat--;
  621. my $nr2 = $nr;
  622. $nr2 =~ tr/0-9//cd;
  623. $nr2 .= "_";
  624. $nr2 .= unpack ("%16C*",$msg);
  625. if ($fo[2] < $fo[1]) # bisherige Anzahl kleiner max Wiederholungen ?
  626. {
  627. $force = "&".join(",", @fo);
  628. my $time_s = strftime("\%H:\%M:\%S", gmtime($fo[0]));
  629. $error = CommandDefine(undef, "at_forcecall_".$nr2." at +".$time_s." set $name call $nr $ringtime $msg *".$repeat." ".$force);
  630. if (!$error) { $attr{"at_forcecall_".$nr2}{room} = AttrVal($name,"room","Unsorted"); }
  631. else { Log3 $name,2,"$name, $error"; }
  632. Log3 $name,4,"$name, at_forcecall_".$nr2." at +".$time_s." set $name call $nr $ringtime $msg *".$repeat." ".$force;
  633. }
  634. else
  635. {
  636. Log3 $name,3,"$name, at_forcecall_".$nr2." max count $fo[1] reached giving up !";
  637. }
  638. } ### end force and !$success
  639. my $nextcall = shift @fifo; # sind da noch Calls in der Queue ?
  640. if ($nextcall)
  641. {
  642. @a = split(" ",$nextcall);
  643. $error = SIP_Set($hash,@a);
  644. Log3 $name,3,"$name, error setting nextcall $nextcall -> $error" if ($error);
  645. return undef;
  646. } else { Log3 $name,5,"$name, fifo is empty"; }
  647. if (exists($hash->{'.elbc'}))
  648. {
  649. @a = (undef,"listen");
  650. Log3 $name,4,"$name, try restarting listen process after call ends";
  651. $error = SIP_Set($hash,@a);
  652. Log3 $name,3,"$name, error restarting listen -> $error" if ($error);
  653. delete $hash->{'.elbc'};
  654. } else { Log3 $name,5,"$name, no elbc"; }
  655. delete $hash->{helper}{CALL};
  656. return undef;
  657. }
  658. #####################################
  659. sub SIP_Set($@)
  660. {
  661. my ($hash, @a) = @_;
  662. my $name = $hash->{NAME};
  663. my $cmd = (defined($a[1])) ? $a[1] : "?";
  664. my $subcmd;
  665. my $error;
  666. return join(" ", sort keys %sets) if ($cmd eq "?");
  667. if (($cmd eq "call") || ($cmd eq "listen"))
  668. {
  669. my $pwd = SIP_readPassword($name);
  670. unless (defined $pwd)
  671. {
  672. $error = "Error: no SIP user password set. Please define it with 'set $name password Your_SIP_User_Password'";
  673. Log3 $name,2,"$name, $error";
  674. return $error;
  675. }
  676. }
  677. if ($cmd eq "call")
  678. {
  679. my $nr = (defined($a[2])) ? $a[2] : "";
  680. my $ringtime = (defined($a[3])) ? $a[3] : 30;
  681. my $msg = (defined($a[4])) ? $a[4] : AttrVal($name, "sip_audiofile_call", "");
  682. return "missing target call number" if (!$nr);
  683. return "invalid max time : $ringtime" unless $ringtime =~ m/^\d+$/;
  684. if (exists($hash->{CPID}))
  685. {
  686. return "there is already a call activ for target $nr" if (defined($hash->{lastnr}) && ($hash->{lastnr} eq $nr));
  687. my $call = join(" ",@a);
  688. push (@fifo,$call);
  689. Log3 $name ,4,"$name, add call $call to fifo so we can do it later !";
  690. return undef;
  691. }
  692. my $anz = @a;
  693. $anz--; # letztes Element
  694. my $force = (substr($a[$anz],0,1) eq "&") ? $a[$anz] : 0;
  695. if ($force)
  696. {
  697. Log3 $name,3,"$name, force call $force";
  698. $force =~ s/^\&//;
  699. my @fo = split(",", $force);
  700. $fo[0] = int(AttrVal($name,"sip_force_interval",300)) if (!$fo[0]);
  701. $fo[1] = int(AttrVal($name,"sip_force_max",99)) if (!$fo[1]);
  702. $fo[2] = 0 if (!$fo[2]);
  703. $force = "&".join("," , @fo);
  704. $anz--; # checken wir dann noch auf repeat
  705. }
  706. my $repeat = 0;
  707. if ((substr($a[$anz],0,1) eq "*") && ($anz > 3))
  708. {
  709. $repeat = $a[$anz];
  710. $repeat =~ s/^\*//;
  711. $repeat ++; $repeat --;
  712. } # * weg , Rest als Int
  713. Log3 $name,4,"$name, msg will be repeat $repeat times" if ($repeat);
  714. if (exists($hash->{LPID}) && (AttrVal($name,"sip_elbc","no") eq "yes"))
  715. {
  716. Log3 $name,4,"$name, listen process ".$hash->{LPID}." must be killed befor we start a new call !";
  717. BlockingKill($hash->{helper}{LISTEN_PID});
  718. delete $hash->{helper}{LISTEN_PID};
  719. delete $hash->{LPID};
  720. readingsSingleUpdate($hash,"listen_alive","no",1);
  721. $hash->{'.elbc'} = 1; # haben wir gerade einen listen Prozess abgeschossen ?
  722. }
  723. if ($msg)
  724. {
  725. if (substr($msg,0,1) eq "-")
  726. {
  727. Log3 $name, 4, $name.", message DTMF = $msg";
  728. }
  729. elsif (substr($msg,0,1) eq "!") # Text2Speech Text ?
  730. {
  731. if ($msg eq AttrVal($name,"sip_audiofile_call", ""))
  732. {
  733. @a = split(" ",AttrVal($name,"sip_audiofile_call", ""));
  734. unshift (@a, ('t2s_name','tts')); # zwei Platzhalter einfügen , Text beginnt jetzt in $a[2]
  735. }
  736. else
  737. {
  738. shift @a;
  739. shift @a;
  740. pop @a if ($force); # das & muss ggf. auch noch weg
  741. pop @a if ($repeat); # das * muss ggf. auch weg
  742. $a[0] = "t2s_name";
  743. $a[1] = "tts"; # Kommando des Set Befehls
  744. }
  745. $a[2] =~ s/^\!//; # das ! muss weg
  746. if (!$a[2]) # ist denn jetzt noch etwas übrig geblieben ?
  747. {
  748. Log3 $name,4,"name, no valid text found in message : $msg";
  749. return "No message text after [!] found";
  750. }
  751. # gibt es denn Text schon als mp3 ?
  752. my $filename = SIP_check_T2S_File($hash,@a);
  753. if($filename)
  754. {
  755. $cmd = "$name call $nr $ringtime $filename";
  756. $cmd .= " *".$repeat if ($repeat);
  757. $cmd .= " ".$force if ($force);
  758. Log3 $name,5,"$name, set call new -> $cmd";
  759. return CommandSet(undef,$cmd);
  760. }
  761. # die nächsten vier brauchen wir unbedingt fuer T2S
  762. $hash->{callnr} = $nr;
  763. $hash->{ringtime} = $ringtime;
  764. $hash->{forcecall} = $force;
  765. $hash->{repeat} = $repeat;
  766. $error = SIP_create_T2S_File($hash,@a); # na dann lege schon mal los
  767. return $error if defined($error); # Das ging leider schief
  768. readingsSingleUpdate($hash,"call_state","waiting T2S",1);
  769. RemoveInternalTimer($hash);
  770. # geben wir T2S mal ein paar Sekunden
  771. InternalTimer(gettimeofday()+int(AttrVal($name,"T2S_Timeout",5)), "SIP_wait_for_t2s", $hash);
  772. return undef;
  773. }
  774. elsif (-e $msg)
  775. {
  776. Log3 $name, 4, $name.", audio file $msg found";
  777. $error = SIP_MP3_conv($hash,$msg,$name) if ($msg =~/\.mp3$/);
  778. if (!$error)
  779. {
  780. $msg =~ s/mp3/alaw/;
  781. $error = "unknown audio type, please use only .alaw , .ulaw or .mp3" if (($msg !~ /\.al(.+)$/) && ($msg !~ /\.ul(.+)$/));
  782. $error = "audio file $msg not found" if(!-e $msg);
  783. }
  784. }
  785. else
  786. {
  787. $error = "audio file $msg not found";
  788. }
  789. if ($error)
  790. {
  791. readingsSingleUpdate($hash, "last_error",$error,1);
  792. Log3 $name, 3, "$name, $error !";
  793. $hash->{repeat} = 0;
  794. $hash->{forcecall} = 0;
  795. return $error;
  796. }
  797. }
  798. else { Log3 $name, 4, $name.", calling $nr, ringtime: $ringtime , no message"; }
  799. $hash->{lastnr} = $nr;
  800. my $arg = "$name|$nr|$ringtime|$msg|$repeat"; # da muss force nicht mit
  801. Log3 $name, 4, "$name, $arg";
  802. #BlockingCall($blockingFn, $arg, $finishFn, $timeout, $abortFn, $abortArg);
  803. $hash->{helper}{CALL_PID} = BlockingCall("SIP_CALLStart",$arg, "SIP_CALLDone") unless(exists($hash->{helper}{CALL_PID}));
  804. if($hash->{helper}{CALL_PID})
  805. {
  806. $hash->{CPID} = $hash->{helper}{CALL_PID}{pid};
  807. $hash->{helper}{CALL} = $arg."|$force"; # hier retten wir aber force
  808. Log3 $name, 4, "$name, call -> ".$hash->{helper}{CALL};
  809. Log3 $name, 5, "$name, call has pid ".$hash->{CPID};
  810. $hash->{helper}{CALL_START} = time();
  811. $hash->{helper}{CALL_TYPE} = "out";
  812. $hash->{helper}{CALL_NR} = $nr;
  813. readingsBeginUpdate($hash);
  814. readingsBulkUpdate($hash, "call_state","invite");
  815. readingsBulkUpdate($hash, "call",$nr);
  816. readingsEndUpdate($hash, 1);
  817. $hash->{'.oldstate'} = ReadingsVal($name,"state",undef);
  818. return undef;
  819. }
  820. else
  821. { # das war wohl nix :(
  822. Log3 $name, 3, "$name, CALL process start failed, arg : $arg";
  823. $error = "can't execute call number $nr as NonBlockingCall";
  824. readingsBeginUpdate($hash);
  825. readingsBulkUpdate($hash, "last_error",$error);
  826. readingsBulkUpdate($hash, "call_state","fail");
  827. readingsEndUpdate($hash, 1);
  828. delete $hash->{lastnr} if (defined($hash->{lastnr}));
  829. return $error;
  830. }
  831. }
  832. elsif ($cmd eq "listen")
  833. {
  834. my $type = AttrVal($name,"sip_listen","none");
  835. return "there is already a listen process running with pid ".$hash->{LPID} if exists($hash->{LPID});
  836. return "please set attr sip_listen to dtmf or wfp or echo first" if (AttrVal($name,"sip_listen","none") eq "none");
  837. $error = SIP_try_listen($hash);
  838. if ($error)
  839. {
  840. Log3 $name, 1, $name.", listen -> $error";
  841. readingsBeginUpdate($hash);
  842. readingsBulkUpdate($hash,"state","error");
  843. readingsBulkUpdate($hash,"last_error",$error);
  844. readingsBulkUpdate($hash,"listen_alive","no");
  845. readingsEndUpdate($hash, 1 );
  846. return $error;
  847. }
  848. return undef;
  849. }
  850. elsif (($cmd eq "dtmf_event") && defined($a[2]))
  851. {
  852. readingsSingleUpdate($hash, "dtmf",$a[2],1);
  853. return undef;
  854. }
  855. elsif ($cmd eq "fetch")
  856. {
  857. readingsSingleUpdate($hash, "caller","fetch",1);
  858. return undef;
  859. }
  860. elsif ($cmd eq "reject")
  861. {
  862. readingsSingleUpdate($hash, "caller","reject",1);
  863. return undef;
  864. }
  865. elsif ($cmd eq "reset")
  866. {
  867. $hash->{".reset"} = 1;
  868. SIP_updateConfig($hash);
  869. return undef;
  870. }
  871. # die ersten beiden brauchen wir nicht mehr
  872. shift @a;
  873. shift @a;
  874. # den Rest als ein String
  875. $subcmd = join(" ",@a);
  876. if ($cmd eq "password")
  877. {
  878. return SIP_storePassword($name,$subcmd);
  879. }
  880. return "Unknown argument: $cmd, choose one of ".join(" ", sort keys %sets);
  881. }
  882. sub SIP_Get($@)
  883. {
  884. my ($hash, @a) = @_;
  885. my $name = $hash->{NAME};
  886. my $cmd = $a[1];
  887. return "get $name needs at least one argument" if(int(@a) < 2);
  888. shift @a;
  889. shift @a;
  890. # den Rest als ein String
  891. my $subcmd = join(" ",@a);
  892. if ($cmd eq "search_phonebook")
  893. {
  894. return SIP_search_phonebook($hash,$name,$subcmd);
  895. }
  896. # return "Unknown argument $cmd, choose one of " . join(" ", sort keys %gets);
  897. return undef;
  898. }
  899. sub SIP_Undef($$)
  900. {
  901. my ($hash, $name) = @_;
  902. $ua->cleanup if (defined($ua));
  903. BlockingKill($hash->{helper}{LISTEN_PID}) if (defined($hash->{helper}{LISTEN_PID}));
  904. #RemoveInternalTimer($hash);
  905. #RemoveInternalTimer($name);
  906. return undef;
  907. }
  908. sub SIP_ListenStart($)
  909. {
  910. my ($name) = @_;
  911. return unless(defined($name));
  912. my $logname = $name."[".$$."]";
  913. my $hash = $defs{$name}; # $hash / $name gueltig in diesem Block
  914. $hash->{parent} = getppid();
  915. Log3 $name,4,"$logname, my parent is ".$hash->{parent};
  916. my $dtmfloop; # Ende-Flag für die DTMF-Schleife
  917. my $okloop; # Ende-Flag für die OK-Ansage
  918. my $okloopbye = 0; # Ende-Flag für recv_bye währne der OK-Ansage
  919. my $byebye = 0; # Anrufer hat aufgelegt
  920. my $packets = 50;
  921. my $block_it;
  922. my $calltime = 0;
  923. my $sub_create;
  924. my $sub_invite_wfp;
  925. my $sub_filter;
  926. my $sub_bye;
  927. my $sub_dtmf;
  928. my $send_something;
  929. $hash->{helper}{CALL_EST} = 0;
  930. $ua = undef;
  931. my $error = SIP_Register($hash,"listen_".AttrVal($name,"sip_listen",""));
  932. return $name."|ListenRegister: $error" if ($error);
  933. my $msg1 = AttrVal($name, "sip_audiofile_dtmf", "");
  934. my $msg2 = AttrVal($name, "sip_audiofile_ok", "");
  935. my $msg3 = AttrVal($name, "sip_audiofile_wfp", "");
  936. $msg1 = SIP_check_file($hash,$hash->{audio1}) if (defined($hash->{audio1}));
  937. $msg1 = SIP_check_file($hash,$msg1) if (!defined($hash->{audio1}) && $msg1);
  938. $msg2 = SIP_check_file($hash,$hash->{audio2}) if (defined($hash->{audio2}));
  939. $msg2 = SIP_check_file($hash,$msg2) if (!defined($hash->{audio2}) && $msg2);
  940. $msg3 = SIP_check_file($hash,$hash->{audio3}) if (defined($hash->{audio3}));
  941. $msg3 = SIP_check_file($hash,$msg3) if (!defined($hash->{audio3}) && $msg3);
  942. Log3 $name,4,"$logname, using $msg1 for audio_dtmf" if ($msg1);
  943. Log3 $name,4,"$logname, using $msg2 for audio_ok" if ($msg2);
  944. Log3 $name,4,"$logname, using $msg3 for audio_wfp" if ($msg3);
  945. $hash->{dtmf} = 0;
  946. $hash->{dtmf_event} = "";
  947. $hash->{old} ="-";
  948. $send_something = sub
  949. {
  950. return unless $packets-- > 0;
  951. my $buf = sprintf "%010d",$packets;
  952. $buf .= "1234567890" x 15;
  953. return $buf; # 160 bytes for PCMU/8000
  954. };
  955. $sub_dtmf = sub
  956. {
  957. my ($event,$dur) = @_;
  958. Log3 $name,5,"$logname, DTMF Event: $event - $dur ms";
  959. return if (int($dur) < 90);
  960. if (($event eq "#") || ($event eq "*"))
  961. {
  962. $hash->{dtmf} = 1;
  963. $hash->{dtmf_event} = "";
  964. $hash->{old} = $event;
  965. return;
  966. }
  967. if (($event ne $hash->{old}) && $hash->{dtmf})
  968. {
  969. $hash->{dtmf} ++;
  970. $hash->{old} = $event;
  971. $hash->{dtmf_event} .= $event;
  972. Log3 $name,5,"$logname, DTMF: ".$hash->{dtmf_event}." , Anz: ".$hash->{dtmf};
  973. if ($hash->{dtmf} > int(AttrVal($name,"sip_dtmf_size",2)))
  974. {
  975. BlockingInformParent("SIP_rSU", [$name, "dtmf_event;".$hash->{dtmf_event}], 0);
  976. $hash->{dtmf} = 0;
  977. $hash->{dtmf_event} = "";
  978. $hash->{old} = "-";
  979. $dtmfloop = 1;
  980. }
  981. }
  982. return;
  983. };
  984. $sub_create = sub
  985. {
  986. my ($call,$request,$leg,$from) = @_;
  987. $hash->{helper}{call} = $call;
  988. $hash->{request} = $request;
  989. $hash->{leg} = $leg;
  990. $hash->{from} = $from;
  991. Log3 $name,4,"$logname, cb_create : ".$request->method;
  992. my $response = ($block_it) ? $request->create_response('487','Request Terminated') : $request->create_response('180','Ringing');
  993. $call->{endpoint}->new_response( $call->{ctx},$response,$leg,$from );
  994. 1;
  995. };
  996. $sub_invite_wfp = sub
  997. {
  998. my ($a,$b,$c,$d) = @_;
  999. my $waittime = int(AttrVal($name, "sip_waittime", 10));
  1000. my $i;
  1001. $packets = 50;
  1002. $hash->{helper}{CALL_EST} = 0;
  1003. Log3 $name, 5,"$logname, cb_invite_wfp";
  1004. for($i=1; $i<=$waittime; $i++)
  1005. {
  1006. if ($block_it) #und gleich wieder weg
  1007. {
  1008. sleep int(AttrVal($name, "sip_waittime", 2)); # kleine Pause
  1009. last;
  1010. }
  1011. Log3 $name, 4,"$logname, SIP_invite -> ringing $i";
  1012. select(undef, undef, undef, 1); # 1 Sekunde Pause
  1013. my $action = BlockingInformParent("SIP_rSU", [$name, "caller_state;ringing $i"], 1);
  1014. if(defined($action))
  1015. {
  1016. Log3 $name, 5,"$logname, cb_invite_wfp action $action";
  1017. if ( $action eq "fetch" )
  1018. {
  1019. $hash->{helper}{CALL_EST} = time();
  1020. $hash->{helper}{CALL_BYE} = "fetch";
  1021. Log3 $name, 4,"$logname, cb_invite_wfp fetch";
  1022. BlockingInformParent("SIP_rSU", [$name, "caller_state;fetching"], 0);
  1023. last;
  1024. }
  1025. elsif ( $action eq "reject" )
  1026. {
  1027. Log3 $name, 4,"$logname, cp_invite_wfp reject";
  1028. BlockingInformParent("SIP_rSU", [$name, "caller_state;rejected"], 0);
  1029. my $call = $hash->{helper}{call};
  1030. my $request = $hash->{request};
  1031. my $leg = $hash->{leg};
  1032. my $from = $hash->{from};
  1033. my $response = $request->create_response('603','Declined');
  1034. $call->{endpoint}->new_response( $call->{ctx},$response,$leg,$from );
  1035. $hash->{helper}{CALL_BYE} ="reject";
  1036. $hash->{helper}{CALL_TIME}= 0;
  1037. SIP_write_history($hash,$logname); # wir kommen nicht zu Bye!
  1038. BlockingInformParent("SIP_rBU", [$name, "caller;none|caller_state;waiting|caller_nr;---|caller_time;0|caller_name;---"], 0);
  1039. last;
  1040. }
  1041. }
  1042. }
  1043. if (($i>$waittime) || $block_it)
  1044. {
  1045. $calltime = ($hash->{helper}{CALL_EST}) ? int(time()-$hash->{helper}{CALL_EST}) : 0;
  1046. BlockingInformParent("SIP_rBU", [$name, "caller;none|caller_state;waiting|caller_nr;---|caller_time;$calltime|caller_name;---"], 0);
  1047. }
  1048. return 0;
  1049. };
  1050. $sub_filter = sub
  1051. {
  1052. my ($a,undef) = @_;
  1053. Log3 $name, 5, "$logname, SIP_filter : $a";
  1054. $block_it = 0;
  1055. $hash->{helper}{CALL_START} = time(); # nochmal prüfen !
  1056. my ($caller,undef) = split("\;", $a);
  1057. my @callers;
  1058. my $caller_nr;
  1059. my $caller_name;
  1060. $caller =~ s/\"|\>|\<|^\s+|\s+$//g; # fhem mag keine <> in ReadingsVal :(
  1061. ($caller_nr,undef) = split("\@", $caller);
  1062. ($caller_name,$caller_nr) = split("\:", $caller_nr);
  1063. $caller_name =~ s/sip//g;
  1064. $caller_name =~ s/\s+$//g; # Leerzeichen am Anfang und Ende nochmal entfernen
  1065. $caller_nr = "0000" if(!$caller_nr);
  1066. Log3 $name, 4, "$logname, SIP_filter: caller $caller, caller_nr $caller_nr, caller_name $caller_name";
  1067. $caller_name = SIP_search_phonebook($hash,$logname,$caller_nr) if (!$caller_name || ($caller_name eq $caller_nr));
  1068. BlockingInformParent("SIP_rBU", [$name, "caller;$caller|caller_nr;$caller_nr|caller_name;$caller_name|caller_time;0|caller_state;calling"], 0);
  1069. $hash->{helper}{CALL_NAME} = $caller_name;
  1070. $hash->{helper}{CALL_NR} = $caller_nr;
  1071. my $block = AttrVal($name,"sip_blocking",undef);
  1072. if (defined($block))
  1073. {
  1074. my @blockers = split (/,/,$block);
  1075. foreach (@blockers)
  1076. {
  1077. if ((index($caller_nr, $_) > -1) || ($_ eq ".*"))
  1078. {
  1079. $hash->{helper}{CALL_BYE} = "block";
  1080. $hash->{helper}{CALL_TIME} = 0;
  1081. SIP_write_history($hash,$logname); # wir kommen nicht zu Bye!
  1082. BlockingInformParent("SIP_rSU", [$name, "caller_state;blocking"], 0);
  1083. Log3 $name, 4, "$logname, blocking $caller_nr found on $block";
  1084. $block_it = 1;
  1085. #$byebye = 1;
  1086. return 1;
  1087. }
  1088. }
  1089. }
  1090. my $filter = AttrVal($name,"sip_filter",undef);
  1091. if (defined($filter))
  1092. {
  1093. @callers = split (/,/,$filter);
  1094. foreach (@callers) { return 1 if (index($caller_nr, $_) > -1); }
  1095. $hash->{helper}{CALL_BYE} = "ignore";
  1096. $hash->{helper}{CALL_TIME} = 0;
  1097. SIP_write_history($hash,$logname); # wir kommen nicht zu Bye!
  1098. BlockingInformParent("SIP_rSU", [$name, "caller_state;ignoring"], 0);
  1099. Log3 $name, 4, "$logname, ignoring $caller_nr number not found in $filter";
  1100. return 0;
  1101. }
  1102. return 1;
  1103. };
  1104. $sub_bye = sub
  1105. {
  1106. my ($event) = @_;
  1107. Log3 $name, 5, "$logname, SIP_bye : $event";
  1108. $calltime = ($hash->{helper}{CALL_EST}) ? int(time()-$hash->{helper}{CALL_EST}) : 0;
  1109. BlockingInformParent("SIP_rBU", [$name, "caller;none|caller_state;hangup|caller_time;$calltime|caller_nr;---|caller_name;---"], 0);
  1110. $hash->{helper}{CALL_BYE}="ok" if (!defined($hash->{helper}{CALL_BYE})); #wfp oder filter kann es schon vorbesetzt haben !
  1111. $hash->{helper}{CALL_TIME}= $calltime;
  1112. SIP_write_history($hash,$logname);
  1113. $hash->{helper}{CALL_EST} = 0;
  1114. $byebye = 1;
  1115. return 1;
  1116. };
  1117. ################
  1118. if (AttrVal($name,"sip_listen", "none") eq "dtmf")
  1119. {
  1120. $dtmfloop = 0; # Ende-Flag für die DTMF-Schleife
  1121. $okloop = 0; # Ende-Flag für die OK-Ansage
  1122. $okloopbye = 0; # Ende-Flag für recv_bye während der OK-Ansage
  1123. # ToDo : was kann davon noch nach while(1) ?
  1124. #$byebye = 0; # Anrufer hat aufgelegt . sthet nun in while(1)
  1125. #BlockingInformParent("SIP_rBU", [$name, "caller;none|caller_state;waiting"], 0);
  1126. while(1)
  1127. {
  1128. my $call;
  1129. $byebye = 0; # eingefügt mit V1.82 Fehler gefunden von tmp88 ,Forum : https://forum.fhem.de/index.php/topic,67443.msg819729.html#msg819729
  1130. $hash->{dtmf} = 0;
  1131. $hash->{dtmf_event} = "";
  1132. $hash->{old} ="-";
  1133. $hash->{helper}{CALL_TYPE} = "dtmf";
  1134. $ua->listen (cb_create => \&$sub_create,
  1135. cb_invite => sub {
  1136. Log3 $name, 5, "$logname, cb_invite_dtmf";
  1137. $hash->{helper}{CALL_EST} = 0;
  1138. if (!$block_it)
  1139. {
  1140. BlockingInformParent("SIP_rSU", [$name,"caller_state;ringing"],0);
  1141. sleep int(AttrVal($name, "sip_ringtime", 3)); #Anrufer hört das typische Klingeln wenn die Gegenseite nicht abnimmt
  1142. }
  1143. },
  1144. filter => \&$sub_filter,
  1145. cb_established => sub {
  1146. (my $status,$call) = @_;
  1147. Log3 $name, 5, "$logname, cb_est_dtmf";
  1148. if (!$block_it)
  1149. {
  1150. $hash->{helper}{CALL_EST} = time();
  1151. BlockingInformParent("SIP_rSU", [$name,"caller_state;established"],0);
  1152. }
  1153. else {
  1154. sleep 1;
  1155. return 0;
  1156. }
  1157. } # sobald invite verlassen wird, wird in cb_established verzweigt
  1158. );
  1159. $ua->loop(\$call);
  1160. # Der SIP-Client ist jetzt im echo-Modus und zwar so lange, bis der Anrufer auflegt,
  1161. # das bekommen wir durch recv_bye mit
  1162. my $dtmf_loop = 1; # für jeden Anruf neu setzen
  1163. while ($dtmf_loop) # Schleife für Code-Ansage, DTMF-Erkennung, okay-Ansage
  1164. {
  1165. $dtmfloop = 0;
  1166. $okloop = 0;
  1167. $okloopbye = 0;
  1168. Log3 $name, 5, "$logname, while dtmf_loop : start reinvite1";
  1169. $call->reinvite(
  1170. init_media => $ua->rtp('send_recv',($msg1) ? $msg1 : $send_something),
  1171. rtp_param => [8, 160, 160/8000, 'PCMA/8000'],
  1172. cb_rtp_done => sub { $packets = 25; },
  1173. cb_dtmf => \&$sub_dtmf,
  1174. recv_bye => \&$sub_bye);
  1175. $ua->loop(\$dtmfloop, \$byebye);
  1176. Log3 $name, 5, "$logname, while dtmf_loop : dtmfloop : $dtmfloop , byebye : $byebye";
  1177. if (!$byebye)
  1178. { # Anrufer hat nicht aufgelegt
  1179. Log3 $name, 5, "$logname, while dtmf_loop : reinvite2";
  1180. $call->reinvite(
  1181. init_media => $ua->rtp('send_recv',($msg2) ? $msg2 : $send_something),
  1182. rtp_param => [8, 160, 160/8000, 'PCMA/8000'],
  1183. cb_rtp_done => sub { select(undef, undef, undef, 0.1); $okloop = 1; $packets = 50;},
  1184. recv_bye => sub { $okloopbye = 1; },
  1185. cb_cleanup => sub {0},
  1186. );
  1187. Log3 $name, 5, "$logname, while dtmf_loop : after reinvite2 $okloop , $okloopbye";
  1188. $ua->loop(\$okloop,\$okloopbye); # ohne diese loop endet der Anruf sofort
  1189. }
  1190. else { $dtmf_loop = 0; $byebye = 1; Log3 $name, 5, "$logname, aufgelegt";} # Schleife beenden, Anrufer hat aufgelegt
  1191. Log3 $name, 5, "$logname, while dtmf_loop, okloopbye : $okloopbye , byebye : $byebye";
  1192. if ( $okloopbye || $byebye )
  1193. {
  1194. # wenn jemand mitten im "okay" auflegt
  1195. $dtmf_loop = 0; # beende die innere Loop
  1196. $byebye = 1;
  1197. }
  1198. else {
  1199. $dtmf_loop = ((AttrVal($name,"sip_dtmf_loop","once") eq 'once')) ? 0 : 1;
  1200. $calltime = (defined($hash->{helper}{CALL_EST})) ? int(time()-$hash->{helper}{CALL_EST}) : 0;
  1201. if(!$dtmf_loop)
  1202. {
  1203. BlockingInformParent("SIP_rBU", [$name, "caller;none|caller_state;hangup|caller_time;$calltime|caller_nr;---|caller_name;---"], 0);
  1204. $hash->{helper}{CALL_BYE} = "ok";
  1205. $hash->{helper}{CALL_TIME} = $calltime;
  1206. SIP_write_history($hash,$logname);
  1207. }
  1208. } # führt ggf. zum Schleifenende
  1209. } # end inner loop
  1210. Log3 $name, 5, "$logname, end while dtmf_loop, byebye : $byebye";
  1211. if (!$byebye)
  1212. { # Anrufer hat nicht aufgelegt und nur ein DTMF angefordert
  1213. my $hanguploop;
  1214. $call->bye( cb_final => \$hanguploop );
  1215. $ua->loop( \$hanguploop );
  1216. }
  1217. Log3 $name, 5, "$logname, while(1)";
  1218. } # while(1)
  1219. }
  1220. elsif (AttrVal($name,"sip_listen", "none") eq "wfp")
  1221. {
  1222. $hash->{helper}{CALL_TYPE} = "wfp";
  1223. $ua->listen(
  1224. cb_create => \&$sub_create,
  1225. cb_invite => \&$sub_invite_wfp,
  1226. cb_established => sub { $hash->{helper}{CALL_EST} = time(); Log3 $name, 5, "$logname, cb_est_wfp";},
  1227. filter => \&$sub_filter,
  1228. recv_bye => \&$sub_bye,
  1229. init_media => $ua->rtp('send_recv',($msg3) ? $msg3 : $send_something),
  1230. #cb_rtp_done => sub {Log3 $name, 5, "$logname, wfp cb_rtp_done";}, legt nicht mehr auf wenn aktiv !
  1231. rtp_param => [8, 160, 160/8000, 'PCMA/8000']
  1232. ); # options are invite and hangup
  1233. }
  1234. elsif (AttrVal($name,"sip_listen", "none") eq "echo")
  1235. {
  1236. $hash->{helper}{CALL_TYPE} = "echo";
  1237. $ua->listen(
  1238. filter => \&$sub_filter,
  1239. cb_create => \&$sub_create,
  1240. cb_invite => sub {
  1241. Log3 $name, 5, "$logname, cb_invite_echo";
  1242. $hash->{helper}{CALL_EST} = 0;
  1243. if (!$block_it)
  1244. {
  1245. Log3 $name, 5, "$logname, cb_invite";
  1246. BlockingInformParent("SIP_rSU", [$name,"caller_state;ringing"],0);
  1247. sleep int(AttrVal($name, "sip_ringtime", 3)); #Anrufer hört das typische Klingeln wenn die Gegenseite nicht abnimmt
  1248. }
  1249. },
  1250. cb_established => sub {
  1251. Log3 $name, 5, "$logname, cb_estab_echo";
  1252. if (!$block_it)
  1253. {
  1254. Log3 $name, 5, "$logname, cb_est";
  1255. $hash->{helper}{CALL_EST} = time();
  1256. BlockingInformParent("SIP_rSU", [$name,"caller_state;established"],0);
  1257. }
  1258. else
  1259. {
  1260. sleep 1;
  1261. return 0;
  1262. }
  1263. },
  1264. init_media => $ua->rtp( 'recv_echo',undef,0 ),
  1265. rtp_param => [8, 160, 160/8000, 'PCMA/8000'],
  1266. recv_bye => \&$sub_bye,
  1267. );
  1268. }
  1269. else { return $name."|end"; }
  1270. $ua->loop;
  1271. return $name."|end"; # hier sollten wir eigentlich nie himkommen !
  1272. }
  1273. sub SIP_ListenDone($)
  1274. {
  1275. my ($string) = @_;
  1276. return unless(defined($string));
  1277. my @r = split("\\|",$string);
  1278. my $hash = $defs{$r[0]};
  1279. my $ret = (defined($r[1])) ? $r[1] : "unknown error";
  1280. my $name = $hash->{NAME};
  1281. Log3 $name, 5,"$name, ListenDone -> $string";
  1282. delete($hash->{helper}{LISTEN_PID});
  1283. delete $hash->{LPID};
  1284. RemoveInternalTimer($name);
  1285. if ($ret ne "end")
  1286. {
  1287. readingsBeginUpdate($hash);
  1288. readingsBulkUpdate($hash,"state","error");
  1289. readingsBulkUpdate($hash,"last_error",$ret);
  1290. readingsBulkUpdate($hash,"listen_alive","no");
  1291. readingsEndUpdate($hash, 1 );
  1292. Log3 $name, 3 , "$name, listen error -> $ret";
  1293. return if(IsDisabled($name));
  1294. InternalTimer(gettimeofday()+AttrVal($name, "sip_watch_listen", 60), "SIP_try_listen", $hash);
  1295. }
  1296. else
  1297. {
  1298. readingsBeginUpdate($hash);
  1299. readingsBulkUpdate($hash,"state","ListenDone");
  1300. readingsBulkUpdate($hash,"listen_alive","no");
  1301. readingsEndUpdate($hash, 1 );
  1302. return if(IsDisabled($name));
  1303. return if(!AttrVal($name, "sip_dtmf", 0));
  1304. SIP_try_listen($hash);
  1305. }
  1306. return;
  1307. }
  1308. sub SIP_try_listen($)
  1309. {
  1310. my ($hash) = @_;
  1311. my $name = $hash->{NAME};
  1312. my $waits = AttrVal($name, "sip_watch_listen", 60);
  1313. my $audio1 = AttrVal($name, "sip_audiofile_dtmf","-");
  1314. my $audio2 = AttrVal($name, "sip_audiofile_ok", "-");
  1315. my $audio3 = AttrVal($name, "sip_audiofile_wfp", "-");
  1316. my @a = ("tts","tts", "-");;
  1317. if (AttrVal($name,"sip_listen","none") eq "dtmf")
  1318. {
  1319. if ((substr($audio1,0,1) eq "!") && !defined($hash->{audio1})) # muss erst T2S gefragt werden ?
  1320. {
  1321. $audio1 =~ s/^\!//;
  1322. $hash->{audio1} = $audio1;
  1323. $a[2] = $audio1;
  1324. Log3 $name ,4,"$name, hole $audio1";
  1325. }
  1326. if ((substr($audio2,0,1) eq "!") && !defined($hash->{audio2})) # muss erst T2S gefragt werden ?
  1327. {
  1328. $audio2 =~ s/^\!//;
  1329. $hash->{audio2} = $audio2;
  1330. $a[2] = $audio2;
  1331. Log3 $name ,4,"$name, hole $audio2";
  1332. }
  1333. }
  1334. elsif ((substr($audio3,0,1) eq "!") && !defined($hash->{audio3}) && (AttrVal($name,"sip_listen","none") eq "wfp")) # muss erst T2S gefragt werden ?
  1335. {
  1336. $audio3 =~ s/^\!//;
  1337. $hash->{audio3} = $audio3;
  1338. $a[2] = $audio3;
  1339. Log3 $name ,4,"$name, hole $audio3";
  1340. }
  1341. if ($a[2] ne "-")
  1342. {
  1343. # prüfen ob es schon eine passende mp3 Datei gibt
  1344. my $filename = SIP_check_T2S_File($hash,@a);
  1345. if (!$filename)
  1346. {
  1347. my $ret = SIP_create_T2S_File($hash,@a);
  1348. if ($ret)
  1349. {
  1350. delete $hash->{audio1} if defined($hash->{audio1});
  1351. delete $hash->{audio2} if defined($hash->{audio2});
  1352. delete $hash->{audio3} if defined($hash->{audio3});
  1353. return $ret;
  1354. }
  1355. #starte die Überwachung von T2S
  1356. RemoveInternalTimer($hash);
  1357. InternalTimer(gettimeofday()+int(AttrVal($name,"T2S_Timeout",5)), "SIP_watchdog_T2S", $hash);
  1358. return undef;
  1359. }
  1360. else
  1361. {
  1362. Log3 $name, 4 , "$name, T2S not used $filename exits";
  1363. $hash->{audio1} = $filename if defined($hash->{audio1});
  1364. $hash->{audio2} = $filename if defined($hash->{audio2});
  1365. $hash->{audio3} = $filename if defined($hash->{audio3});
  1366. }
  1367. }
  1368. $hash->{helper}{LISTEN_PID} = BlockingCall("SIP_ListenStart",$name, "SIP_ListenDone") unless(exists($hash->{helper}{LISTEN_PID}));
  1369. if ($hash->{helper}{LISTEN_PID})
  1370. {
  1371. $hash->{LPID} = $hash->{helper}{LISTEN_PID}{pid};
  1372. Log3 $name, 4 , $name.", Listen new PID : ".$hash->{LPID};
  1373. RemoveInternalTimer($name);
  1374. InternalTimer(gettimeofday()+$waits, "SIP_watch_listen", $name); # starte die Überwachung
  1375. delete $hash->{audio1};
  1376. delete $hash->{audio2};
  1377. delete $hash->{audio3};
  1378. return 0;
  1379. }
  1380. else
  1381. {
  1382. Log3 $name, 2 , $name.", Listen Start failed, waiting $waits seconds for next try";
  1383. RemoveInternalTimer($hash);
  1384. InternalTimer(gettimeofday()+$waits, "SIP_try_listen", $hash);
  1385. return "Listen Start failed";
  1386. }
  1387. }
  1388. sub SIP_watch_listen($)
  1389. {
  1390. # Lebt denn der Listen Prozess überhaupt noch ?
  1391. my ($name) = @_;
  1392. my $hash = $defs{$name};
  1393. my $listen_dead = 0;
  1394. RemoveInternalTimer($name);
  1395. return if (IsDisabled($name));
  1396. return if (!defined($hash->{LPID}));
  1397. my $cmd = "ps -e | grep '".$hash->{LPID}." '";
  1398. my $result = qx($cmd);
  1399. my $age = int(ReadingsAge($name, "expire", 0));
  1400. my $maxage = int(ReadingsNum($name,"expire",300)*0.7);
  1401. my $alive = ReadingsVal($name,"listen_alive","no");
  1402. my $waits = AttrVal($name, "sip_watch_listen", 60);
  1403. if (($age > $maxage) && ($alive ne "no")) # nach expire/2 Sekunden sollte sich der listen Prozess erneut melden
  1404. {
  1405. Log3 $name, 2 , "$name, expire timestamp is $age seconds old, restarting listen process";
  1406. readingsSingleUpdate($hash,"listen_alive","no",1);
  1407. $alive = "no";
  1408. }
  1409. elsif (index($result,"perl") == -1)
  1410. {
  1411. Log3 $name, 2 , $name.", cant find listen process ".$hash->{LPID}." in process list !";
  1412. $alive = "no";;
  1413. }
  1414. else { Log3 $name, 5 , $name.", listen process ".$hash->{LPID}." found"; }
  1415. if ($alive eq "no")
  1416. {
  1417. BlockingKill($hash->{helper}{LISTEN_PID});
  1418. delete $hash->{helper}{LISTEN_PID};
  1419. delete $hash->{LPID};
  1420. InternalTimer(gettimeofday()+2, "SIP_try_listen", $hash, 0);
  1421. }
  1422. InternalTimer(gettimeofday()+$waits, "SIP_watch_listen", $name, 0);
  1423. return;
  1424. }
  1425. sub SIP_wait_for_t2s($)
  1426. {
  1427. my ($hash) = @_;
  1428. my $name = $hash->{NAME};
  1429. RemoveInternalTimer($hash);
  1430. my $t2s_name = AttrVal($name,"T2S_Device",undef);
  1431. my $file = ReadingsVal($t2s_name,"lastFilename","");
  1432. my $msg = "";
  1433. Log3 $name,4,"$name, wait_for_t2s file : $file";
  1434. if (-e $file)
  1435. {
  1436. Log3 $name,4,"$name, new T2S file $file";
  1437. my $out = $file;
  1438. $out =~ s/mp3/alaw/;
  1439. my $error = SIP_MP3_conv($hash,$file,$name);
  1440. $msg = $out if (!$error && (-e $out));
  1441. }
  1442. else
  1443. {
  1444. Log3 $name,3,"$name, timeout waiting for T2S";
  1445. if ($hash->{callnr})
  1446. {
  1447. readingsSingleUpdate($hash,"call_state","T2S timeout",1);
  1448. return undef;
  1449. }
  1450. }
  1451. if (!$hash->{callnr})
  1452. {
  1453. if (defined($hash->{audio3}))
  1454. {
  1455. $hash->{audio3} = $msg;
  1456. SIP_try_listen($hash);
  1457. return undef;
  1458. }
  1459. elsif (defined($hash->{audio2}))
  1460. {
  1461. $hash->{audio2} = $msg;
  1462. SIP_try_listen($hash);
  1463. return undef;
  1464. }
  1465. elsif (defined($hash->{audio1}))
  1466. {
  1467. $hash->{audio1} = $msg;
  1468. SIP_try_listen($hash);
  1469. return undef;
  1470. }
  1471. }
  1472. # nun aber calling
  1473. my $repeat = "*".$hash->{repeat};
  1474. my @a;
  1475. if ($hash->{forcecall})
  1476. { @a = ($name,"call",$hash->{callnr}, $hash->{ringtime},$msg,$repeat,$hash->{forcecall}) ; }
  1477. else
  1478. { @a = ($name,"call",$hash->{callnr}, $hash->{ringtime},$msg,$repeat) ; }
  1479. delete($hash->{callnr});
  1480. delete($hash->{ringtime});
  1481. delete($hash->{forcecall});
  1482. delete($hash->{repeat});
  1483. my $ret = SIP_Set($hash , @a);
  1484. Log3 $name,3,"$name, error T2S Call : $ret" if defined($ret);
  1485. return undef;
  1486. }
  1487. ######################################################
  1488. # storePW & readPW Code geklaut aus 72_FRITZBOX.pm :)
  1489. ######################################################
  1490. sub SIP_storePassword($$)
  1491. {
  1492. my ($name, $password) = @_;
  1493. my $index = "SIP_".$name."_passwd";
  1494. my $key = getUniqueId().$index;
  1495. my $e_pwd = "";
  1496. if (eval "use Digest::MD5;1")
  1497. {
  1498. $key = Digest::MD5::md5_hex(unpack "H*", $key);
  1499. $key .= Digest::MD5::md5_hex($key);
  1500. }
  1501. for my $char (split //, $password)
  1502. {
  1503. my $encode=chop($key);
  1504. $e_pwd.=sprintf("%.2x",ord($char)^ord($encode));
  1505. $key=$encode.$key;
  1506. }
  1507. my $error = setKeyValue($index, $e_pwd);
  1508. return "error while saving SIP user password : $error" if(defined($error));
  1509. return "SIP user password successfully saved in FhemUtils/uniqueID Key $index";
  1510. }
  1511. sub SIP_readPassword($)
  1512. {
  1513. my ($name) = @_;
  1514. my $index = "SIP_".$name."_passwd";
  1515. my $key = getUniqueId().$index;
  1516. my ($password, $error);
  1517. #Log3 $name,5,"$name, read SIP user password from FhemUtils/uniqueID Key $key";
  1518. ($error, $password) = getKeyValue($index);
  1519. if ( defined($error) )
  1520. {
  1521. Log3 $name,3, "$name, cant't read SIP user password from FhemUtils/uniqueID: $error";
  1522. return undef;
  1523. }
  1524. if ( defined($password) )
  1525. {
  1526. if (eval "use Digest::MD5;1")
  1527. {
  1528. $key = Digest::MD5::md5_hex(unpack "H*", $key);
  1529. $key .= Digest::MD5::md5_hex($key);
  1530. }
  1531. my $dec_pwd = '';
  1532. for my $char (map { pack('C', hex($_)) } ($password =~ /(..)/g))
  1533. {
  1534. my $decode=chop($key);
  1535. $dec_pwd.=chr(ord($char)^ord($decode));
  1536. $key=$decode.$key;
  1537. }
  1538. return $dec_pwd;
  1539. }
  1540. else
  1541. {
  1542. Log3 $name,3,"$name, no SIP user password found in FhemUtils/uniqueID";
  1543. return undef;
  1544. }
  1545. }
  1546. #####################################
  1547. sub SIP_check_file($$)
  1548. {
  1549. my ($hash,$file) = @_;
  1550. my $name = $hash->{NAME};
  1551. my $logname = $name."[".$$."]";
  1552. if (substr($file,0,1) eq "!")
  1553. {
  1554. Log3 $name,3,"$logname, Text : $file found, ignoring it";
  1555. return "";
  1556. }
  1557. if ($file =~/\.mp3$/)
  1558. {
  1559. my $ret = SIP_MP3_conv($hash,$file,$logname);
  1560. if ($ret)
  1561. {
  1562. Log3 $name,3,"$logname, $ret";
  1563. return "";
  1564. }
  1565. $file =~ s/mp3/alaw/;
  1566. }
  1567. if (($file !~ /\.al(.+)$/) && ($file !~ /\.ul(.+)$/))
  1568. {
  1569. Log3 $name,3,"$logname, audio file $file not type .alaw or .ulaw, ignoring it";
  1570. return "";
  1571. }
  1572. if (!-e $file)
  1573. {
  1574. Log3 $name,3,"$logname, audio file $file not found, ignoring it";
  1575. return "";
  1576. }
  1577. Log3 $name,5,"$logname, audio file $file found";
  1578. return $file;
  1579. }
  1580. sub SIP_create_T2S_File($@)
  1581. {
  1582. my ($hash,@a) = @_;
  1583. my $name = $hash->{NAME};
  1584. my $t2s_name = AttrVal($name,"T2S_Device",undef);
  1585. return "attr T2S_Device not set !" if !defined($t2s_name);
  1586. my $t2s_hash = (defined($defs{$t2s_name})) ? $defs{$t2s_name} : undef;
  1587. return "T2S_Device $t2s_name not found" if !defined($t2s_hash);
  1588. return "attr audio_converter not set" if !AttrVal($name,"audio_converter","");
  1589. return "external sox or ffmpeg programm not found, please install sox or ffmpeg first and set attr audio_converter" if !defined($hash->{AC});
  1590. my $t2s_file = ReadingsVal($t2s_name,"lastFilename",undef);
  1591. Log3 $name,3,"$name, Reading lastFilename not found at device $t2s_name, are you using a old version ?" if !defined($t2s_file);
  1592. readingsSingleUpdate($t2s_hash,"lastFilename","",0);
  1593. my $ret = Text2Speech_Set($t2s_hash, @a); # na dann lege schon mal los
  1594. if (defined($ret))
  1595. {
  1596. Log3 $name,3,"$name, T2S error : $ret";
  1597. readingsSingleUpdate($hash,"last_error",$ret,0);
  1598. return $ret; # Das ging leider schief
  1599. }
  1600. return undef; # alles klar
  1601. }
  1602. sub SIP_check_T2S_File($@)
  1603. {
  1604. my ($hash,@a) = @_;
  1605. my $name = $hash->{NAME};
  1606. my $t2s_name = AttrVal($hash->{NAME},"T2S_Device","");
  1607. return 0 if (!$t2s_name);
  1608. shift @a;
  1609. shift @a;
  1610. my $txt = join(" ",@a);
  1611. my $filename = (eval "use Digest::MD5;1") ? md5_hex("de|".$txt).".mp3" : "";
  1612. if ($filename)
  1613. {
  1614. my $file = AttrVal($t2s_name,"TTS_CacheFileDir", "cache"). "/".$filename;
  1615. Log3 $name,5,"$name, MD5: $txt -> $filename";
  1616. return $file if (-e $file);
  1617. }
  1618. Log3 $name,5,"$name, mp3 File file not found in cache";
  1619. return 0;
  1620. }
  1621. sub SIP_watchdog_T2S($)
  1622. {
  1623. my ($hash) = @_;
  1624. my $name = $hash->{NAME};
  1625. Log3 $name,3,"$name, Timeout waiting for T2S";
  1626. if (defined($hash->{audio1}))
  1627. {
  1628. $hash->{audio1}="!T2S Timeout";
  1629. SIP_try_listen($hash);
  1630. return undef;
  1631. }
  1632. if (defined($hash->{audio2}))
  1633. {
  1634. $hash->{audio2}="!T2S Timeout";
  1635. SIP_try_listen($hash);
  1636. return undef;
  1637. }
  1638. if (defined($hash->{audio3}))
  1639. {
  1640. $hash->{audio3}="!T2S Timeout";
  1641. SIP_try_listen($hash);
  1642. return undef;
  1643. }
  1644. }
  1645. #####################################
  1646. # Benutzt um Infos aus dem Blockingprozess in die Readings zu schreiben
  1647. #####################################
  1648. sub SIP_rSU($$) {
  1649. my ($name, $line) = @_;
  1650. my $hash = $defs{$name};
  1651. my ($reading,$val) = split("\;",$line);
  1652. Log3 $hash, 5, "$name, readingS:$reading Val:$val";
  1653. readingsSingleUpdate($hash, $reading, $val, 1);
  1654. # Sonderfall bei wfp , Abfrage des Readings caller auf fetch oder reject
  1655. #my $action = ReadingsVal($name,"caller","");
  1656. #return $action if (($reading eq "caller_state") && (substr($val,0,7) eq "ringing") && (($action eq "fetch") || ($action eq "reject")));
  1657. return ReadingsVal($name,"caller","") if (($reading eq "caller_state") && (substr($val,0,7) eq "ringing"));
  1658. return undef;
  1659. }
  1660. sub SIP_rBU($$) {
  1661. my ($name, $line) = @_;
  1662. my $hash = $defs{$name};
  1663. readingsBeginUpdate($hash);
  1664. my @pair = split("\\|",$line);
  1665. foreach (@pair)
  1666. {
  1667. my ($reading,$val) = split("\;",$_);
  1668. {
  1669. Log3 $hash, 5, "$name, readingB:$reading Val:$val";
  1670. readingsBulkUpdate($hash, $reading, $val);
  1671. }
  1672. }
  1673. readingsEndUpdate($hash, 1 );
  1674. return undef;
  1675. }
  1676. sub SIP_MP3_conv($$$)
  1677. {
  1678. my ($hash,$file,$logname) = @_;
  1679. my $name = $hash->{NAME};
  1680. my $ret;
  1681. my $status;
  1682. my $cmd;
  1683. my $out = $file;
  1684. $out =~ s/mp3/alaw/;
  1685. if (-e $out)
  1686. {
  1687. Log3 $name,5,"$logname, not converted - using $out from cache";
  1688. return undef;
  1689. }
  1690. else
  1691. {
  1692. return "external sox or ffmpeg programm not found, please install sox or ffmpeg first and set attr audio_converter" if (!defined($hash->{AC}));
  1693. my $converter = AttrVal($name,"audio_converter","");
  1694. return "attr audio_converter not set" if(!$converter);
  1695. if ($converter eq "sox")
  1696. {
  1697. $cmd = $hash->{AC}." ".$file." -t raw -r 8000 -c 1 -e a-law ".$out." 2>&1";
  1698. Log3 $name,5,"$logname, $cmd";
  1699. $ret = qx($cmd);
  1700. if ($ret)
  1701. {
  1702. unlink $out;
  1703. $ret =~ s/\n//g;
  1704. Log3 $name,5,"$logname, sox output : $ret";
  1705. }
  1706. }
  1707. elsif ($converter eq "ffmpeg")
  1708. {
  1709. $cmd = $hash->{AC}." -v quiet -y -i ".$file." -f alaw -ar 8000 ".$out;
  1710. Log3 $name,5,"$logname, $cmd";
  1711. $ret = qx($cmd);
  1712. }
  1713. else { return "unknow audio_converter"; }
  1714. return "$converter : $ret" if ($ret);
  1715. return "converted file $out not found" if (!-e $out);
  1716. return undef;
  1717. }
  1718. }
  1719. sub SIP_search_phonebook($$$)
  1720. {
  1721. my ($hash,$logname,$number) = @_;
  1722. my $name = $hash->{NAME};
  1723. my $file = AttrVal($name,"phonebook",undef);
  1724. return "unknown" if (!$file);
  1725. Log3 $name,5,"$logname, Phonebook: $file, $number, ".int(AttrVal($name,"history_size",0));
  1726. my ($error, @lines) = FileRead($file);
  1727. if ($error)
  1728. {
  1729. Log3 $name,2,"$logname, phonebook : $error";
  1730. return "error";
  1731. }
  1732. my $i = @lines;
  1733. if (!@lines)
  1734. {
  1735. Log3 $name,2,"$logname, phonebook is empty";
  1736. return "empty";
  1737. }
  1738. Log3 $name,5,"$logname, read $i lines from phonebook";
  1739. foreach(@lines)
  1740. {
  1741. my ($nr,$na) = split(",",$_);
  1742. ($nr,$na) = split("\\|",$_) if (!$na); # Liste vllt doch durch | getrennt ?
  1743. if ($na && ($nr eq $number))
  1744. {
  1745. Log3 $name,4,"$logname, found $na for number $number in phonebook";
  1746. return $na;
  1747. }
  1748. }
  1749. Log3 $name,3,"$logname, no entry found in phonebook for number $number";
  1750. return $number;
  1751. }
  1752. sub SIP_write_history($$)
  1753. {
  1754. my ($hash,$logname) = @_;
  1755. my $name = $hash->{NAME};
  1756. return if (!int(AttrVal($name,"history_size",0)));
  1757. $hash->{helper}{CALL_START} = time() if (!$hash->{helper}{CALL_START});
  1758. $hash->{helper}{CALL_TYPE} = "out" if (!$hash->{helper}{CALL_TYPE});
  1759. $hash->{helper}{CALL_TIME} = 0 if (!$hash->{helper}{CALL_TIME});
  1760. $hash->{helper}{CALL_NAME} = "????" if (!$hash->{helper}{CALL_NAME});
  1761. $hash->{helper}{CALL_NR} = "0000" if (!$hash->{helper}{CALL_NR});
  1762. $hash->{helper}{CALL_BYE} = "-" if (!$hash->{helper}{CALL_BYE});
  1763. my $file = AttrVal($name,"history_file","./log/$name.sip");
  1764. my ($error, @lines) = FileRead($file);
  1765. my $anz = @lines;
  1766. if ($error)
  1767. {
  1768. Log3 $name,2,"$logname, history file $file, $error";
  1769. return undef;
  1770. }
  1771. Log3 $name,4,"$logname, read $anz lines from history file $file";
  1772. while ($anz >= int(AttrVal($name,"history_size", 10))) { shift @lines; $anz--;}
  1773. my $line = FmtDateTime(int($hash->{helper}{CALL_START}))."|";
  1774. $line .= $hash->{helper}{CALL_TYPE}."|";
  1775. $line .= $hash->{helper}{CALL_NAME}."|";
  1776. $line .= $hash->{helper}{CALL_NR}."|";
  1777. $line .= $hash->{helper}{CALL_BYE}."|";
  1778. $line .= int($hash->{helper}{CALL_TIME})."|";
  1779. push @lines,$line;
  1780. $error = FileWrite($file, @lines);
  1781. if ($error)
  1782. {
  1783. Log3 $name,2,"$logname, history file $file, $error";
  1784. return undef;
  1785. }
  1786. $anz++;
  1787. delete $hash->{helper}{CALL_TIME};
  1788. delete($hash->{helper}{CALL_START});
  1789. delete($hash->{helper}{CALL_BYE});
  1790. delete($hash->{helper}{CALL_NAME});
  1791. delete($hash->{helper}{CALL_NR});
  1792. $hash->{helper}{CALL_EST} = 0;
  1793. if ($name ne $logname)
  1794. { BlockingInformParent("SIP_rSU", [$name,"history_lines;$anz"],0); }
  1795. else
  1796. { readingsSingleUpdate($hash,"history_lines",$anz,0);}
  1797. return undef;
  1798. }
  1799. sub SIP_html($;$)
  1800. {
  1801. my ($name,$header) = @_;
  1802. $name = "<none>" if(!$name);
  1803. $header = "SIP Call List" if(!$header);
  1804. my $error = (!$defs{$name} || $defs{$name}{TYPE} ne "SIP") ? "$name is not a SIP device" : "";
  1805. my $html = '<table class="roomoverview"><tr class="devTypeTr"><td><div class="devType">'.$header.'</div></td></tr><tr><td>';
  1806. $html .='<table class="block fbcalllist">
  1807. <tr align="center" class="fbcalllist header">
  1808. <td>State</td>
  1809. <td>Time</td>
  1810. <td>Name</td>
  1811. <td>Number</td>
  1812. <td>IO</td>
  1813. <td>Duration</td>
  1814. </tr>'."\n";
  1815. my $ehtml = "<tr align='center' number='1' class='odd'><td colspan='6'>";
  1816. my $end = "</table></td></tr></table>";
  1817. return $html.$ehtml.$error."</td></tr>".$end if($error);
  1818. my $hash = $defs{$name};
  1819. my @lines;
  1820. if (!int(AttrVal($name,"history_size",0)))
  1821. {
  1822. $html .= $ehtml."please set attr $name history_size first</td></tr>".$end;
  1823. return $html;
  1824. }
  1825. my $file = AttrVal($name,"history_file","./log/$name.sip");
  1826. ($error, @lines) = FileRead($file);
  1827. my $anz = @lines;
  1828. if ($error)
  1829. {
  1830. $html .= $ehtml."error reading $file, $error</td></tr>".$end;
  1831. return $html;
  1832. }
  1833. if (!$anz)
  1834. {
  1835. $html .= $ehtml."file $file is empty !</td></tr>".$end;
  1836. return $html;
  1837. }
  1838. my $i = 1;
  1839. my $style = "style='padding-left:5px;padding-right:5px;'";
  1840. foreach(@lines)
  1841. {
  1842. my $oe = ($i %2) ? 'odd' : 'even';
  1843. my @a = split("\\|",$_);
  1844. my($d,$h,$m,$s,$dur,$sec);
  1845. if (int($a[5])>0)
  1846. {
  1847. $sec = $a[5];
  1848. $d=int($sec/(24*60*60));
  1849. $h=($sec/(60*60))%24;
  1850. $m=($sec/60)%60;
  1851. $s=$sec%60;
  1852. $dur = sprintf("%02s:%02s:%02s", $h, $m, $s);
  1853. } else { $dur = "-"; }
  1854. $html .= "<tr align='center' number='$i' class='$oe'>";
  1855. $html .= "<td $style>".$a[4]."</td>";
  1856. $html .= "<td $style>".$a[0]."</td>";
  1857. $html .= "<td $style>".$a[2]."</td>";
  1858. $html .= "<td $style>".$a[3]."</td>";
  1859. $html .= "<td $style>".$a[1]."</td>";
  1860. $html .= "<td $style>".$dur."</td></tr>\n";
  1861. $i++;
  1862. }
  1863. $html .= $end;
  1864. return $html;
  1865. }
  1866. 1;
  1867. =pod
  1868. =item helper
  1869. =item summary SIP device
  1870. =item summary_DE SIP Ger&auml;t
  1871. =begin html
  1872. <a name="SIP"></a>
  1873. <h3>SIP</h3>
  1874. <ul>
  1875. Define a SIP-Client device.<br>
  1876. Wiki : <a href="https://wiki.fhem.de/wiki/SIP-Client">https://wiki.fhem.de/wiki/SIP-Client</a>
  1877. <br>
  1878. Forum : <a href="https://forum.fhem.de/index.php/topic,67443.0.html">https://forum.fhem.de/index.php/topic,67443.0.html</a>
  1879. <br><br>
  1880. <a name="SIPdefine"></a>
  1881. <b>Define</b>
  1882. <ul>
  1883. <code>define &lt;name&gt; SIP</code>
  1884. <br><br>
  1885. Example:
  1886. <ul>
  1887. <code>define MySipClient SIP</code><br>
  1888. </ul>
  1889. </ul>
  1890. <br>
  1891. <a name="SIPset"></a>
  1892. <b>Set</b>
  1893. <ul>
  1894. <li>
  1895. <code>set &lt;name&gt; &lt;SIP password&gt;</code><br>
  1896. Stores the password for the SIP users. Without stored password the functions set call and set listen are blocked !<br>
  1897. IMPORTANT : if you rename the fhem Device you must set the password again!
  1898. </li>
  1899. <li>
  1900. <code>set &lt;name&gt; reset</code><br>
  1901. Stop any listen process and initialize device.<br>
  1902. </li>
  1903. <li>
  1904. <code>set &lt;name&gt; call &lt;number&gt [&lt;maxtime&gt;] [&lt;message&gt;]</code><br>
  1905. Start a call to the given number.<br>
  1906. Optionally you can supply a max time. Default is 30.
  1907. Optionally you can supply a message which is either a full path to an audio file or a relativ path starting from the home directory of the fhem.pl.
  1908. </li>
  1909. <li>
  1910. <code>set &lt;name&gt; listen</code><br>
  1911. attr sip_listen = dtmf :<br>
  1912. Start a listening process that receives calls. The device goes into an echo mode when a call comes in. If you press # on the keypad followed by 2 numbers and hang up the reading <b>dtmf</b> will reflect that number.<br>
  1913. attr sip_listen = wfp :<br>
  1914. Start a listening process that waits for incoming calls. If a call comes in for the SIP-Client the state will change to <b>ringing</b>. If you manually set the state to <b>fetch</b> the call will be picked up and the sound file given in attribute sip_audiofile will be played to the caller. After that the devive will go gack into state <b>listenwfp</b>.<br>
  1915. </li>
  1916. </ul>
  1917. <br>
  1918. <a name="SIPattr"></a>
  1919. <b>Attributes</b>
  1920. <ul>
  1921. <li>sip_audiofile_wfp<br>
  1922. Audio file that will be played after <b>fetch</b> command. The audio file has to be generated via <br>
  1923. sox &lt;file&gt;.wav -t raw -r 8000 -c 1 -e a-law &lt;file&gt;.al<br>
  1924. since only raw audio format is supported.
  1925. </li>
  1926. <li>sip_audiofile_call</li>
  1927. <li>sip_audiofile_dtmf</li>
  1928. <li>sip_audiofile_ok</li>
  1929. <li>sip_listen (none , dtmf , wfp)</li>
  1930. <li>sip_from<br>
  1931. My sip client info, defaults to sip:620@fritz.box
  1932. </li>
  1933. <li>sip_ip<br>
  1934. external IP address of the FHEM server.
  1935. </li>
  1936. <li>sip_port<br>
  1937. Optionally portnumber used for sip client<br>
  1938. If attribute is not set a random port number between 44000 and 45000 will be used
  1939. </li>
  1940. <li>sip_registrar<br>
  1941. Hostname or IP address of the SIP server you are connecting to, defaults to fritz.box.
  1942. </li>
  1943. <li>sip_ringtime<br>
  1944. Ringtime for incomming calls (dtmf &wfp)
  1945. </li>
  1946. <li>sip_user<br>
  1947. User name of the SIP client, defaults to 620.
  1948. </li>
  1949. <li>sip_waittime<br>
  1950. Maximum waiting time in state listen_for_wfp it will wait to pick up the call.
  1951. </li>
  1952. <li>sip_dtmf_size 1 to 4 , default is 2</li>
  1953. <li>sip_dtmf_loop once or loop , default once</li>
  1954. <li>sip_force_interval default 300</li>
  1955. <li>sip_force_max default 99</li>
  1956. <li>phonebook default none , filename of own phonebook. each row : number,name</li>
  1957. <li>history_size default 0 , max rows in history list</li>
  1958. <li>history_file default none, filename of history list</li>
  1959. </ul>
  1960. <br>
  1961. </ul>
  1962. =end html
  1963. =begin html_DE
  1964. <a name="SIP"></a>
  1965. <h3>SIP</h3>
  1966. <ul>
  1967. Definiert ein SIP-Client Device.<br>
  1968. Wiki : <a href="https://wiki.fhem.de/wiki/SIP-Client">https://wiki.fhem.de/wiki/SIP-Client</a>
  1969. <br>
  1970. Forum : <a href="https://forum.fhem.de/index.php/topic,67443.0.html">https://forum.fhem.de/index.php/topic,67443.0.html</a>
  1971. <br><br>
  1972. <a name="SIPdefine"></a>
  1973. <b>Define</b>
  1974. <ul>
  1975. <code>define &lt;name&gt; SIP</code>
  1976. <br><br>
  1977. Beispiel:
  1978. <ul>
  1979. <code>define MySipClient SIP</code><br>
  1980. </ul>
  1981. </ul>
  1982. <br>
  1983. <a name="SIPset"></a>
  1984. <b>Set</b>
  1985. <ul>
  1986. <li>
  1987. <code>set &lt;name&gt; &lt;SIP Passwort&gt;</code><br>
  1988. Speichert das Passwort des SIP Users. Ohne gespeichertes Passwort sind die set call und set listen Funktionen gesperrt !<br>
  1989. WICHTIG : wird das SIP Device umbenannt muss dieser Befehl unbedingt wiederholt werden !
  1990. </li>
  1991. <li>
  1992. <code>set &lt;name&gt; reset</code><br>
  1993. Stoppt laufende listen-Prozess und initalisiert das Device.<br>
  1994. </li>
  1995. <li>
  1996. <code>set &lt;name&gt; call &lt;nummer&gt; [&lt;maxtime&gt;] [&lt;nachricht&gt;]</code><br>
  1997. Startet einen Anruf an die angegebene Nummer.<br>
  1998. Optional kann die maximale Zeit angegeben werden. Default ist 30.<br>
  1999. Optional kann eine Nachricht in Form eines Audiofiles angegeben werden . Das File ist mit dem vollen Pfad oder dem relativen ab dem Verzeichnis mit fhem.pl anzugeben..
  2000. </li>
  2001. <li>
  2002. <code>set &lt;name&gt; listen</code><br>
  2003. Attribut sip_listen = dtmf :
  2004. Der SIP-Client wird in einen Status versetzt in dem er Anrufe annimmt. Der Ton wird als Echo zurückgespielt. Über die Eingabe von # gefolgt von 2 unterschiedlichen Zahlen und anschließendem Auflegen kann eine Zahl in das Reading <b>dtmf</b> übergeben werden.<br>
  2005. Attribut sip_listen = wfp :
  2006. Der SIP-Client wird in einen Status versetzt in dem er auf Anrufe wartet. Erfolgt an Anruf an den Client, wechselt der Status zu <b>ringing</b>. Nun kann das Gespräch via set-Command <b>fetch</b> angenommen werden. Das als sip_audiofile angegebene File wird abgespielt. Anschließend wechselt der Status wieder zu <b>listenwfp</b>.<br>
  2007. </li>
  2008. </ul>
  2009. <br>
  2010. <a name="SIPattr"></a>
  2011. <b>Attributes</b>
  2012. <ul>
  2013. <li>sip_user<br>
  2014. User Name des SIP-Clients. Default ist 620 (Fritzbox erstes SIP Telefon)
  2015. </li>
  2016. <li>sip_registrar<br>
  2017. Hostname oder IP-Addresse des SIP-Servers mit dem sich das Modul verbinden soll. (Default fritz.box)
  2018. </li>
  2019. <li>sip_from<br>
  2020. SIP-Client-Info. Syntax : sip:sip_user@sip_registrar Default ist sip:620@fritz.box
  2021. </li>
  2022. <li>sip_ip<br>
  2023. Die IP-Addresse von FHEM im Heimnetz. Solange das Attribut nicht gesetzt ist versucht das Modul diese beim Start zu ermitteln.
  2024. </li>
  2025. <li>sip_port<br>
  2026. Optinale Portnummer die vom Modul benutzt wird.<br>
  2027. Wenn dem Attribut kein Wert zugewiesen wurde verwendet das Modul eine zuf&auml;llige Portnummer zwichen 44000 und 45000
  2028. </li>
  2029. <li><b>Audiofiles</b>
  2030. Audiofiles k&ouml;nnen einfach mit dem externen Programm sox erzeugt werden :<br>
  2031. sox &lt;file&gt;.wav -t raw -r 8000 -c 1 -e a-law &lt;file&gt;.al<br>
  2032. Unterst&uuml;tzt werden nur die beiden RAW Audio Formate a-law und u-law !<br>
  2033. Statt eines echten Audiofiles kann auch eine Text2Speech Nachricht eingetragen werden.<br>
  2034. Bsp : attr mySIP sip_audiofile_call !Hier ist dein FHEM Server
  2035. </li>
  2036. <li>sip_audiofile_wfp<br>
  2037. Audiofile das nach dem Command <b>fetch</b> abgespielt wird.
  2038. </li>
  2039. <li>sip_audiofile_call</br>
  2040. Audiofile das dem Angerufenen bei set call vorgespielt wird.
  2041. </li>
  2042. <li>sip_audiofile_dtmf<br>
  2043. Audiofile das dem Anrufer bei listen_for_dtmf abgespielt wird.
  2044. </li>
  2045. <li>sip_audiofile_ok<br>
  2046. Audiofile das bei erkannter DTMF Sequenz abgespielt wird.
  2047. </li>
  2048. <li>sip_listen (none , dtmf, wfp)</li>
  2049. <li>sip_ringtime<br>
  2050. Klingelzeit für eingehende Anrufe bei listen_for_dtmf
  2051. </li>
  2052. <li>sip_dtmf_size</a><br>
  2053. 1 bis 4 , default 2 Legt die L&auml;ge des erwartenden DTMF Events fest.
  2054. </li>
  2055. <li>sip_dtmf_loop<br> once oder loop , default once</li>
  2056. <li>sip_waittime<br>
  2057. Maximale Wartezeit im Status listen_for_wfp bis das Gespr&auml;ch automatisch angenommen wird.
  2058. </li>
  2059. <li>T2S_Device<br>
  2060. Name des Text2Speech Devices (Wird nur ben&ouml;tigt wenn Sprachnachrichten statt Audiofiles verwendet werden)
  2061. </li>
  2062. <li>T2S_Timeout<br>
  2063. Wartezeit in Sekunden wie lange maximal auf Text2Speech gewartet wird.
  2064. </li>
  2065. <li>audo_converter<br>sox oder ffmpeg, default sox<br>
  2066. Ist f&uml;r Text2Speech unbedingt erforderlich um die mp3 Dateien in Raw Audio umzuwandeln.<br>
  2067. Installation z.B. mit sudo apt-get install sox und noch die mp3 Unterst&uuml;tzung mit sudo apt-get install libsox-fmt-mp3
  2068. </li>
  2069. <li>sip_force_interval default 300 </li>
  2070. <li>sip_force_max default 99</li>
  2071. <li>phonebook default none , Dateiname des eigenen Telefonbuchs. Inhalt: zeilenweise Nr,Name</li>
  2072. <li>history_size default 0 , max Anzahl von Zeilen in der Ruf/Anrufer Liste</li>
  2073. <li>history_file default none, Dateiname der Ruf/Anrufer Liste</li>
  2074. </ul>
  2075. <br>
  2076. </ul>
  2077. =end html_DE
  2078. =cut