chan_unistim.c 170 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * UNISTIM channel driver for asterisk
  5. *
  6. * Copyright (C) 2005 - 2007, Cedric Hans
  7. *
  8. * Cedric Hans <cedric.hans@mlkj.net>
  9. *
  10. * Asterisk 1.4 patch by Peter Be
  11. *
  12. * See http://www.asterisk.org for more information about
  13. * the Asterisk project. Please do not directly contact
  14. * any of the maintainers of this project for assistance;
  15. * the project provides a web site, mailing lists and IRC
  16. * channels for your use.
  17. *
  18. * This program is free software, distributed under the terms of
  19. * the GNU General Public License Version 2. See the LICENSE file
  20. * at the top of the source tree.
  21. */
  22. /*!
  23. * \file
  24. *
  25. * \brief chan_unistim channel driver for Asterisk
  26. * \author Cedric Hans <cedric.hans@mlkj.net>
  27. *
  28. * Unistim (Unified Networks IP Stimulus) channel driver
  29. * for Nortel i2002, i2004 and i2050
  30. *
  31. * \ingroup channel_drivers
  32. */
  33. /*** MODULEINFO
  34. <support_level>extended</support_level>
  35. <defaultenabled>no</defaultenabled>
  36. ***/
  37. #include "asterisk.h"
  38. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  39. #include <sys/stat.h>
  40. #include <signal.h>
  41. #if defined(__CYGWIN__)
  42. /*
  43. * cygwin headers are partly inconsistent. struct iovec is defined in sys/uio.h
  44. * which is not included by default by sys/socket.h - in_pktinfo is defined in
  45. * w32api/ws2tcpip.h but this probably has compatibility problems with sys/socket.h
  46. * So for the time being we simply disable HAVE_PKTINFO when building under cygwin.
  47. * This should be done in some common header, but for now this is the only file
  48. * using iovec and in_pktinfo so it suffices to apply the fix here.
  49. */
  50. #ifdef HAVE_PKTINFO
  51. #undef HAVE_PKTINFO
  52. #endif
  53. #endif /* __CYGWIN__ */
  54. #include "asterisk/paths.h" /* ast_config_AST_LOG_DIR used in (too ?) many places */
  55. #include "asterisk/network.h"
  56. #include "asterisk/channel.h"
  57. #include "asterisk/config.h"
  58. #include "asterisk/module.h"
  59. #include "asterisk/pbx.h"
  60. #include "asterisk/event.h"
  61. #include "asterisk/rtp_engine.h"
  62. #include "asterisk/netsock.h"
  63. #include "asterisk/acl.h"
  64. #include "asterisk/callerid.h"
  65. #include "asterisk/cli.h"
  66. #include "asterisk/app.h"
  67. #include "asterisk/musiconhold.h"
  68. #include "asterisk/causes.h"
  69. #include "asterisk/indications.h"
  70. /*! Beware, G729 and G723 are not supported by asterisk, except with the proper licence */
  71. #define CAPABILITY AST_FORMAT_ALAW | AST_FORMAT_ULAW /* | AST_FORMAT_G729A | AST_FORMAT_G723_1 */
  72. #define DEFAULTCONTEXT "default"
  73. #define DEFAULTCALLERID "Unknown"
  74. #define DEFAULTCALLERNAME " "
  75. #define DEFAULTHEIGHT 3
  76. #define USTM_LOG_DIR "unistimHistory"
  77. /*! Size of the transmit buffer */
  78. #define MAX_BUF_SIZE 64
  79. /*! Number of slots for the transmit queue */
  80. #define MAX_BUF_NUMBER 50
  81. /*! Try x times before removing the phone */
  82. #define NB_MAX_RETRANSMIT 8
  83. /*! Nb of milliseconds waited when no events are scheduled */
  84. #define IDLE_WAIT 1000
  85. /*! Wait x milliseconds before resending a packet */
  86. #define RETRANSMIT_TIMER 2000
  87. /*! How often the mailbox is checked for new messages */
  88. #define TIMER_MWI 10000
  89. /*! Not used */
  90. #define DEFAULT_CODEC 0x00
  91. #define SIZE_PAGE 4096
  92. #define DEVICE_NAME_LEN 16
  93. #define AST_CONFIG_MAX_PATH 255
  94. #define MAX_ENTRY_LOG 30
  95. #define SUB_REAL 0
  96. #define SUB_THREEWAY 1
  97. #define MAX_SUBS 2
  98. enum autoprovision {
  99. AUTOPROVISIONING_NO = 0,
  100. AUTOPROVISIONING_YES,
  101. AUTOPROVISIONING_DB,
  102. AUTOPROVISIONING_TN
  103. };
  104. enum autoprov_extn {
  105. /*! Do not create an extension into the default dialplan */
  106. EXTENSION_NONE = 0,
  107. /*! Prompt user for an extension number and register it */
  108. EXTENSION_ASK,
  109. /*! Register an extension with the line=> value */
  110. EXTENSION_LINE,
  111. /*! Used with AUTOPROVISIONING_TN */
  112. EXTENSION_TN
  113. };
  114. #define OUTPUT_HANDSET 0xC0
  115. #define OUTPUT_HEADPHONE 0xC1
  116. #define OUTPUT_SPEAKER 0xC2
  117. #define VOLUME_LOW 0x01
  118. #define VOLUME_LOW_SPEAKER 0x03
  119. #define VOLUME_NORMAL 0x02
  120. #define VOLUME_INSANELY_LOUD 0x07
  121. #define MUTE_OFF 0x00
  122. #define MUTE_ON 0xFF
  123. #define MUTE_ON_DISCRET 0xCE
  124. #define SIZE_HEADER 6
  125. #define SIZE_MAC_ADDR 17
  126. #define TEXT_LENGTH_MAX 24
  127. #define TEXT_LINE0 0x00
  128. #define TEXT_LINE1 0x20
  129. #define TEXT_LINE2 0x40
  130. #define TEXT_NORMAL 0x05
  131. #define TEXT_INVERSE 0x25
  132. #define STATUS_LENGTH_MAX 28
  133. #define FAV_ICON_NONE 0x00
  134. #define FAV_ICON_ONHOOK_BLACK 0x20
  135. #define FAV_ICON_ONHOOK_WHITE 0x21
  136. #define FAV_ICON_SPEAKER_ONHOOK_BLACK 0x22
  137. #define FAV_ICON_SPEAKER_ONHOOK_WHITE 0x23
  138. #define FAV_ICON_OFFHOOK_BLACK 0x24
  139. #define FAV_ICON_OFFHOOK_WHITE 0x25
  140. #define FAV_ICON_ONHOLD_BLACK 0x26
  141. #define FAV_ICON_ONHOLD_WHITE 0x27
  142. #define FAV_ICON_SPEAKER_OFFHOOK_BLACK 0x28
  143. #define FAV_ICON_SPEAKER_OFFHOOK_WHITE 0x29
  144. #define FAV_ICON_PHONE_BLACK 0x2A
  145. #define FAV_ICON_PHONE_WHITE 0x2B
  146. #define FAV_ICON_SPEAKER_ONHOLD_BLACK 0x2C
  147. #define FAV_ICON_SPEAKER_ONHOLD_WHITE 0x2D
  148. #define FAV_ICON_HEADPHONES 0x2E
  149. #define FAV_ICON_HEADPHONES_ONHOLD 0x2F
  150. #define FAV_ICON_HOME 0x30
  151. #define FAV_ICON_CITY 0x31
  152. #define FAV_ICON_SHARP 0x32
  153. #define FAV_ICON_PAGER 0x33
  154. #define FAV_ICON_CALL_CENTER 0x34
  155. #define FAV_ICON_FAX 0x35
  156. #define FAV_ICON_MAILBOX 0x36
  157. #define FAV_ICON_REFLECT 0x37
  158. #define FAV_ICON_COMPUTER 0x38
  159. #define FAV_ICON_FORWARD 0x39
  160. #define FAV_ICON_LOCKED 0x3A
  161. #define FAV_ICON_TRASH 0x3B
  162. #define FAV_ICON_INBOX 0x3C
  163. #define FAV_ICON_OUTBOX 0x3D
  164. #define FAV_ICON_MEETING 0x3E
  165. #define FAV_ICON_BOX 0x3F
  166. #define FAV_BLINK_FAST 0x20
  167. #define FAV_BLINK_SLOW 0x40
  168. #define FAV_MAX_LENGTH 0x0A
  169. static void dummy(char *unused, ...)
  170. {
  171. return;
  172. }
  173. /*! \brief Global jitterbuffer configuration - by default, jb is disabled
  174. * \note Values shown here match the defaults shown in unistim.conf.sample */
  175. static struct ast_jb_conf default_jbconf =
  176. {
  177. .flags = 0,
  178. .max_size = 200,
  179. .resync_threshold = 1000,
  180. .impl = "fixed",
  181. .target_extra = 40,
  182. };
  183. static struct ast_jb_conf global_jbconf;
  184. /* #define DUMP_PACKET 1 */
  185. /* #define DEBUG_TIMER ast_verbose */
  186. #define DEBUG_TIMER dummy
  187. /*! Enable verbose output. can also be set with the CLI */
  188. static int unistimdebug = 0;
  189. static int unistim_port;
  190. static enum autoprovision autoprovisioning = AUTOPROVISIONING_NO;
  191. static int unistim_keepalive;
  192. static int unistimsock = -1;
  193. static struct {
  194. unsigned int tos;
  195. unsigned int tos_audio;
  196. unsigned int cos;
  197. unsigned int cos_audio;
  198. } qos = { 0, 0, 0, 0 };
  199. static struct io_context *io;
  200. static struct sched_context *sched;
  201. static struct sockaddr_in public_ip = { 0, };
  202. /*! give the IP address for the last packet received */
  203. static struct sockaddr_in address_from;
  204. /*! size of the sockaddr_in (in WSARecvFrom) */
  205. static unsigned int size_addr_from = sizeof(address_from);
  206. /*! Receive buffer address */
  207. static unsigned char *buff;
  208. static int unistim_reloading = 0;
  209. AST_MUTEX_DEFINE_STATIC(unistim_reload_lock);
  210. AST_MUTEX_DEFINE_STATIC(usecnt_lock);
  211. static int usecnt = 0;
  212. /* extern char ast_config_AST_LOG_DIR[AST_CONFIG_MAX_PATH]; */
  213. /*! This is the thread for the monitor which checks for input on the channels
  214. * which are not currently in use. */
  215. static pthread_t monitor_thread = AST_PTHREADT_NULL;
  216. /*! Protect the monitoring thread, so only one process can kill or start it, and not
  217. * when it's doing something critical. */
  218. AST_MUTEX_DEFINE_STATIC(monlock);
  219. /*! Protect the session list */
  220. AST_MUTEX_DEFINE_STATIC(sessionlock);
  221. /*! Protect the device list */
  222. AST_MUTEX_DEFINE_STATIC(devicelock);
  223. enum phone_state {
  224. STATE_INIT,
  225. STATE_AUTHDENY,
  226. STATE_MAINPAGE,
  227. STATE_EXTENSION,
  228. STATE_DIALPAGE,
  229. STATE_RINGING,
  230. STATE_CALL,
  231. STATE_SELECTCODEC,
  232. STATE_CLEANING,
  233. STATE_HISTORY
  234. };
  235. enum handset_state {
  236. STATE_ONHOOK,
  237. STATE_OFFHOOK,
  238. };
  239. enum phone_key {
  240. KEY_0 = 0x40,
  241. KEY_1 = 0x41,
  242. KEY_2 = 0x42,
  243. KEY_3 = 0x43,
  244. KEY_4 = 0x44,
  245. KEY_5 = 0x45,
  246. KEY_6 = 0x46,
  247. KEY_7 = 0x47,
  248. KEY_8 = 0x48,
  249. KEY_9 = 0x49,
  250. KEY_STAR = 0x4a,
  251. KEY_SHARP = 0x4b,
  252. KEY_UP = 0x4c,
  253. KEY_DOWN = 0x4d,
  254. KEY_RIGHT = 0x4e,
  255. KEY_LEFT = 0x4f,
  256. KEY_QUIT = 0x50,
  257. KEY_COPY = 0x51,
  258. KEY_FUNC1 = 0x54,
  259. KEY_FUNC2 = 0x55,
  260. KEY_FUNC3 = 0x56,
  261. KEY_FUNC4 = 0x57,
  262. KEY_ONHOLD = 0x5b,
  263. KEY_HANGUP = 0x5c,
  264. KEY_MUTE = 0x5d,
  265. KEY_HEADPHN = 0x5e,
  266. KEY_LOUDSPK = 0x5f,
  267. KEY_FAV0 = 0x60,
  268. KEY_FAV1 = 0x61,
  269. KEY_FAV2 = 0x62,
  270. KEY_FAV3 = 0x63,
  271. KEY_FAV4 = 0x64,
  272. KEY_FAV5 = 0x65,
  273. KEY_COMPUTR = 0x7b,
  274. KEY_CONF = 0x7c,
  275. KEY_SNDHIST = 0x7d,
  276. KEY_RCVHIST = 0x7e,
  277. KEY_INDEX = 0x7f
  278. };
  279. struct tone_zone_unistim {
  280. char country[3];
  281. int freq1;
  282. int freq2;
  283. };
  284. static const struct tone_zone_unistim frequency[] = {
  285. {"us", 350, 440},
  286. {"fr", 440, 0},
  287. {"au", 413, 438},
  288. {"nl", 425, 0},
  289. {"uk", 350, 440},
  290. {"fi", 425, 0},
  291. {"es", 425, 0},
  292. {"jp", 400, 0},
  293. {"no", 425, 0},
  294. {"at", 420, 0},
  295. {"nz", 400, 0},
  296. {"tw", 350, 440},
  297. {"cl", 400, 0},
  298. {"se", 425, 0},
  299. {"be", 425, 0},
  300. {"sg", 425, 0},
  301. {"il", 414, 0},
  302. {"br", 425, 0},
  303. {"hu", 425, 0},
  304. {"lt", 425, 0},
  305. {"pl", 425, 0},
  306. {"za", 400, 0},
  307. {"pt", 425, 0},
  308. {"ee", 425, 0},
  309. {"mx", 425, 0},
  310. {"in", 400, 0},
  311. {"de", 425, 0},
  312. {"ch", 425, 0},
  313. {"dk", 425, 0},
  314. {"cn", 450, 0},
  315. {"--", 0, 0}
  316. };
  317. struct wsabuf {
  318. u_long len;
  319. unsigned char *buf;
  320. };
  321. struct systemtime {
  322. unsigned short w_year;
  323. unsigned short w_month;
  324. unsigned short w_day_of_week;
  325. unsigned short w_day;
  326. unsigned short w_hour;
  327. unsigned short w_minute;
  328. unsigned short w_second;
  329. unsigned short w_milliseconds;
  330. };
  331. struct unistim_subchannel {
  332. ast_mutex_t lock;
  333. /*! SUBS_REAL or SUBS_THREEWAY */
  334. unsigned int subtype;
  335. /*! Asterisk channel used by the subchannel */
  336. struct ast_channel *owner;
  337. /*! Unistim line */
  338. struct unistim_line *parent;
  339. /*! RTP handle */
  340. struct ast_rtp_instance *rtp;
  341. int alreadygone;
  342. char ringvolume;
  343. char ringstyle;
  344. };
  345. /*!
  346. * \todo Convert to stringfields
  347. */
  348. struct unistim_line {
  349. ast_mutex_t lock;
  350. /*! Like 200 */
  351. char name[80];
  352. /*! Like USTM/200\@black */
  353. char fullname[80];
  354. /*! pointer to our current connection, channel... */
  355. struct unistim_subchannel *subs[MAX_SUBS];
  356. /*! Extension where to start */
  357. char exten[AST_MAX_EXTENSION];
  358. /*! Context to start in */
  359. char context[AST_MAX_EXTENSION];
  360. /*! Language for asterisk sounds */
  361. char language[MAX_LANGUAGE];
  362. /*! CallerID Number */
  363. char cid_num[AST_MAX_EXTENSION];
  364. /*! Mailbox for MWI */
  365. char mailbox[AST_MAX_EXTENSION];
  366. /*! Used by MWI */
  367. int lastmsgssent;
  368. /*! Used by MWI */
  369. time_t nextmsgcheck;
  370. /*! MusicOnHold class */
  371. char musicclass[MAX_MUSICCLASS];
  372. /*! Call group */
  373. unsigned int callgroup;
  374. /*! Pickup group */
  375. unsigned int pickupgroup;
  376. /*! Account code (for billing) */
  377. char accountcode[80];
  378. /*! AMA flags (for billing) */
  379. int amaflags;
  380. /*! Codec supported */
  381. format_t capability;
  382. /*! Parkinglot */
  383. char parkinglot[AST_MAX_CONTEXT];
  384. struct unistim_line *next;
  385. struct unistim_device *parent;
  386. };
  387. /*!
  388. * \brief A device containing one or more lines
  389. */
  390. static struct unistim_device {
  391. int receiver_state; /*!< state of the receiver (see ReceiverState) */
  392. int size_phone_number; /*!< size of the phone number */
  393. char phone_number[16]; /*!< the phone number entered by the user */
  394. char redial_number[16]; /*!< the last phone number entered by the user */
  395. int phone_current; /*!< Number of the current phone */
  396. int pos_fav; /*!< Position of the displayed favorites (used for scrolling) */
  397. char id[18]; /*!< mac address of the current phone in ascii */
  398. char name[DEVICE_NAME_LEN]; /*!< name of the device */
  399. int softkeylinepos; /*!< position of the line softkey (default 0) */
  400. char softkeylabel[6][11]; /*!< soft key label */
  401. char softkeynumber[6][16]; /*!< number dialed when the soft key is pressed */
  402. char softkeyicon[6]; /*!< icon number */
  403. char softkeydevice[6][16]; /*!< name of the device monitored */
  404. struct unistim_device *sp[6]; /*!< pointer to the device monitored by this soft key */
  405. int height; /*!< The number of lines the phone can display */
  406. char maintext0[25]; /*!< when the phone is idle, display this string on line 0 */
  407. char maintext1[25]; /*!< when the phone is idle, display this string on line 1 */
  408. char maintext2[25]; /*!< when the phone is idle, display this string on line 2 */
  409. char titledefault[13]; /*!< title (text before date/time) */
  410. char datetimeformat; /*!< format used for displaying time/date */
  411. char contrast; /*!< contrast */
  412. char country[3]; /*!< country used for dial tone frequency */
  413. struct ast_tone_zone *tz; /*!< Tone zone for res_indications (ring, busy, congestion) */
  414. char ringvolume; /*!< Ring volume */
  415. char ringstyle; /*!< Ring melody */
  416. int rtp_port; /*!< RTP port used by the phone */
  417. int rtp_method; /*!< Select the unistim data used to establish a RTP session */
  418. int status_method; /*!< Select the unistim packet used for sending status text */
  419. char codec_number; /*!< The current codec used to make calls */
  420. int missed_call; /*!< Number of call unanswered */
  421. int callhistory; /*!< Allowed to record call history */
  422. char lst_cid[TEXT_LENGTH_MAX]; /*!< Last callerID received */
  423. char lst_cnm[TEXT_LENGTH_MAX]; /*!< Last callername recevied */
  424. char call_forward[AST_MAX_EXTENSION]; /*!< Forward number */
  425. int output; /*!< Handset, headphone or speaker */
  426. int previous_output; /*!< Previous output */
  427. int volume; /*!< Default volume */
  428. int mute; /*!< Mute mode */
  429. int moh; /*!< Music on hold in progress */
  430. int nat; /*!< Used by the obscure ast_rtp_setnat */
  431. enum autoprov_extn extension; /*!< See ifdef EXTENSION for valid values */
  432. char extension_number[11]; /*!< Extension number entered by the user */
  433. char to_delete; /*!< Used in reload */
  434. time_t start_call_timestamp; /*!< timestamp for the length calculation of the call */
  435. struct ast_silence_generator *silence_generator;
  436. struct unistim_line *lines;
  437. struct ast_ha *ha;
  438. struct unistimsession *session;
  439. struct unistim_device *next;
  440. } *devices = NULL;
  441. static struct unistimsession {
  442. ast_mutex_t lock;
  443. struct sockaddr_in sin; /*!< IP address of the phone */
  444. struct sockaddr_in sout; /*!< IP address of server */
  445. int timeout; /*!< time-out in ticks : resend packet if no ack was received before the timeout occured */
  446. unsigned short seq_phone; /*!< sequence number for the next packet (when we receive a request) */
  447. unsigned short seq_server; /*!< sequence number for the next packet (when we send a request) */
  448. unsigned short last_seq_ack; /*!< sequence number of the last ACK received */
  449. unsigned long tick_next_ping; /*!< time for the next ping */
  450. int last_buf_available; /*!< number of a free slot */
  451. int nb_retransmit; /*!< number of retransmition */
  452. int state; /*!< state of the phone (see phone_state) */
  453. int size_buff_entry; /*!< size of the buffer used to enter datas */
  454. char buff_entry[16]; /*!< Buffer for temporary datas */
  455. char macaddr[18]; /*!< mac adress of the phone (not always available) */
  456. struct wsabuf wsabufsend[MAX_BUF_NUMBER]; /*!< Size of each paquet stored in the buffer array & pointer to this buffer */
  457. unsigned char buf[MAX_BUF_NUMBER][MAX_BUF_SIZE]; /*!< Buffer array used to keep the lastest non-acked paquets */
  458. struct unistim_device *device;
  459. struct unistimsession *next;
  460. } *sessions = NULL;
  461. /*!
  462. * \page Unistim datagram formats
  463. *
  464. * Format of datagrams :
  465. * bytes 0 & 1 : ffff for discovery packet, 0000 for everything else
  466. * byte 2 : sequence number (high part)
  467. * byte 3 : sequence number (low part)
  468. * byte 4 : 2 = ask question or send info, 1 = answer or ACK, 0 = retransmit request
  469. * byte 5 : direction, 1 = server to phone, 2 = phone to server arguments
  470. */
  471. static const unsigned char packet_rcv_discovery[] =
  472. { 0xff, 0xff, 0xff, 0xff, 0x02, 0x02, 0xff, 0xff, 0xff, 0xff, 0x9e, 0x03, 0x08 };
  473. static const unsigned char packet_send_discovery_ack[] =
  474. { 0x00, 0x00, /*Initial Seq (2 bytes) */ 0x00, 0x00, 0x00, 0x01 };
  475. static const unsigned char packet_recv_firm_version[] =
  476. { 0x00, 0x00, 0x00, 0x13, 0x9a, 0x0a, 0x02 };
  477. static const unsigned char packet_recv_pressed_key[] =
  478. { 0x00, 0x00, 0x00, 0x13, 0x99, 0x04, 0x00 };
  479. static const unsigned char packet_recv_pick_up[] =
  480. { 0x00, 0x00, 0x00, 0x13, 0x99, 0x03, 0x04 };
  481. static const unsigned char packet_recv_hangup[] =
  482. { 0x00, 0x00, 0x00, 0x13, 0x99, 0x03, 0x03 };
  483. static const unsigned char packet_recv_r2[] = { 0x00, 0x00, 0x00, 0x13, 0x96, 0x03, 0x03 };
  484. /*! TransportAdapter */
  485. static const unsigned char packet_recv_resume_connection_with_server[] =
  486. { 0xff, 0xff, 0xff, 0xff, 0x9e, 0x03, 0x08 };
  487. static const unsigned char packet_recv_mac_addr[] =
  488. { 0xff, 0xff, 0xff, 0xff, 0x9a, 0x0d, 0x07 /*MacAddr */ };
  489. static const unsigned char packet_send_date_time3[] =
  490. { 0x11, 0x09, 0x02, 0x02, /*Month */ 0x05, /*Day */ 0x06, /*Hour */ 0x07,
  491. /*Minutes */ 0x08, 0x32
  492. };
  493. static const unsigned char packet_send_date_time[] =
  494. { 0x11, 0x09, 0x02, 0x0a, /*Month */ 0x05, /*Day */ 0x06, /*Hour */ 0x07, /*Minutes */
  495. 0x08, 0x32, 0x17, 0x04, 0x24, 0x07, 0x19,
  496. 0x04, 0x07, 0x00, 0x19, 0x05, 0x09, 0x3e, 0x0f, 0x16, 0x05, 0x00, 0x80, 0x00, 0x1e,
  497. 0x05, 0x12, 0x00, 0x78
  498. };
  499. static const unsigned char packet_send_no_ring[] =
  500. { 0x16, 0x04, 0x1a, 0x00, 0x16, 0x04, 0x11, 0x00 };
  501. static const unsigned char packet_send_s4[] =
  502. { 0x16, 0x04, 0x1a, 0x00, 0x16, 0x04, 0x11, 0x00, 0x16, 0x06, 0x32, 0xdf, 0x00, 0xff,
  503. 0x16, 0x05, 0x1c, 0x00, 0x00, 0x17, 0x05,
  504. 0x0b, 0x00, 0x00, 0x19, 0x04, 0x00, 0x00, 0x19, 0x04, 0x00, 0x08, 0x19, 0x04, 0x00,
  505. 0x10, 0x19, 0x04, 0x00, 0x18, 0x16, 0x05,
  506. 0x31, 0x00, 0x00, 0x16, 0x05, 0x04, 0x00, 0x00
  507. };
  508. static const unsigned char packet_send_call[] =
  509. { 0x16, 0x04, 0x1a, 0x00, 0x16, 0x04, 0x11, 0x00, 0x16, 0x06, 0x32, 0xdf,
  510. 0x00, 0xff, 0x16, 0x05, 0x1c, 0x00, 0x00, 0x16, 0x0a, 0x38, 0x00, 0x12, 0xca, 0x03,
  511. 0xc0, 0xc3, 0xc5, 0x16, 0x16, 0x30, 0x00,
  512. 0x00, /*codec */ 0x12, 0x12, /* frames per packet */ 0x01, 0x5c, 0x00, /*port RTP */
  513. 0x0f, 0xa0, /* port RTCP */ 0x9c, 0x41,
  514. /*port RTP */ 0x0f, 0xa0, /* port RTCP */ 0x9c, 0x41, /* IP Address */ 0x0a, 0x01,
  515. 0x16, 0x66
  516. };
  517. static const unsigned char packet_send_stream_based_tone_off[] =
  518. { 0x16, 0x05, 0x1c, 0x00, 0x00 };
  519. /* static const unsigned char packet_send_Mute[] = { 0x16, 0x05, 0x04, 0x00, 0x00 };
  520. static const unsigned char packet_send_CloseAudioStreamRX[] = { 0x16, 0x05, 0x31, 0x00, 0xff };
  521. static const unsigned char packet_send_CloseAudioStreamTX[] = { 0x16, 0x05, 0x31, 0xff, 0x00 };*/
  522. static const unsigned char packet_send_stream_based_tone_on[] =
  523. { 0x16, 0x06, 0x1b, 0x00, 0x00, 0x05 };
  524. static const unsigned char packet_send_stream_based_tone_single_freq[] =
  525. { 0x16, 0x06, 0x1d, 0x00, 0x01, 0xb8 };
  526. static const unsigned char packet_send_stream_based_tone_dial_freq[] =
  527. { 0x16, 0x08, 0x1d, 0x00, 0x01, 0xb8, 0x01, 0x5e };
  528. static const unsigned char packet_send_select_output[] =
  529. { 0x16, 0x06, 0x32, 0xc0, 0x01, 0x00 };
  530. static const unsigned char packet_send_ring[] =
  531. { 0x16, 0x06, 0x32, 0xdf, 0x00, 0xff, 0x16, 0x05, 0x1c, 0x00, 0x00, 0x16,
  532. 0x04, 0x1a, 0x01, 0x16, 0x05, 0x12, 0x13 /* Ring type 10 to 17 */ , 0x18, 0x16, 0x04, 0x18, /* volume 00, 10, 20... */
  533. 0x20, 0x16, 0x04, 0x10, 0x00
  534. };
  535. static const unsigned char packet_send_end_call[] =
  536. { 0x16, 0x06, 0x32, 0xdf, 0x00, 0xff, 0x16, 0x05, 0x31, 0x00, 0x00, 0x19, 0x04, 0x00,
  537. 0x10, 0x19, 0x04, 0x00, 0x18, 0x16, 0x05,
  538. 0x04, 0x00, 0x00, 0x16, 0x04, 0x37, 0x10
  539. };
  540. static const unsigned char packet_send_s9[] =
  541. { 0x16, 0x06, 0x32, 0xdf, 0x00, 0xff, 0x19, 0x04, 0x00, 0x10, 0x16, 0x05, 0x1c, 0x00,
  542. 0x00 };
  543. static const unsigned char packet_send_rtp_packet_size[] =
  544. { 0x16, 0x08, 0x38, 0x00, 0x00, 0xe0, 0x00, 0xa0 };
  545. static const unsigned char packet_send_jitter_buffer_conf[] =
  546. { 0x16, 0x0e, 0x3a, 0x00, /* jitter */ 0x02, /* high water mark */ 0x04, 0x00, 0x00,
  547. /* early packet resync 2 bytes */ 0x3e, 0x80,
  548. 0x00, 0x00, /* late packet resync 2 bytes */ 0x3e, 0x80
  549. };
  550. /* Duration in ms div 2 (0x20 = 64ms, 0x08 = 16ms)
  551. static unsigned char packet_send_StreamBasedToneCad[] =
  552. { 0x16, 0x0a, 0x1e, 0x00, duration on 0x0a, duration off 0x0d, duration on 0x0a, duration off 0x0d, duration on 0x0a, duration off 0x2b }; */
  553. static const unsigned char packet_send_open_audio_stream_rx[] =
  554. { 0x16, 0x1a, 0x30, 0x00, 0xff, /* Codec */ 0x00, 0x00, 0x01, 0x00, 0xb8, 0xb8, 0x0e,
  555. 0x0e, 0x01, /* Port */ 0x14, 0x50, 0x00,
  556. 0x00, /* Port */ 0x14, 0x50, 0x00, 0x00, /* Dest IP */ 0x0a, 0x93, 0x69, 0x05
  557. };
  558. static const unsigned char packet_send_open_audio_stream_tx[] =
  559. { 0x16, 0x1a, 0x30, 0xff, 0x00, 0x00, /* Codec */ 0x00, 0x01, 0x00, 0xb8, 0xb8, 0x0e,
  560. 0x0e, 0x01, /* Local port */ 0x14, 0x50,
  561. 0x00, 0x00, /* Rmt Port */ 0x14, 0x50, 0x00, 0x00, /* Dest IP */ 0x0a, 0x93, 0x69, 0x05
  562. };
  563. static const unsigned char packet_send_open_audio_stream_rx3[] =
  564. { 0x16, 0x1a, 0x30, 0x00, 0xff, /* Codec */ 0x00, 0x00, 0x02, 0x01, 0xb8, 0xb8, 0x06,
  565. 0x06, 0x81, /* RTP Port */ 0x14, 0x50,
  566. /* RTCP Port */ 0x14,
  567. 0x51, /* RTP Port */ 0x14, 0x50, /* RTCP Port */ 0x00, 0x00, /* Dest IP */ 0x0a, 0x93,
  568. 0x69, 0x05
  569. };
  570. static const unsigned char packet_send_open_audio_stream_tx3[] =
  571. { 0x16, 0x1a, 0x30, 0xff, 0x00, 0x00, /* Codec */ 0x00, 0x02, 0x01, 0xb8, 0xb8, 0x06,
  572. 0x06, 0x81, /* RTP Local port */ 0x14, 0x50,
  573. /* RTCP Port */ 0x00, 0x00, /* RTP Rmt Port */ 0x14, 0x50, /* RTCP Port */ 0x00, 0x00,
  574. /* Dest IP */ 0x0a, 0x93, 0x69, 0x05
  575. };
  576. static const unsigned char packet_send_arrow[] = { 0x17, 0x04, 0x04, 0x00 };
  577. static const unsigned char packet_send_blink_cursor[] = { 0x17, 0x04, 0x10, 0x86 };
  578. static const unsigned char packet_send_date_time2[] = { 0x17, 0x04, 0x17, 0x3d, 0x11, 0x09, 0x02, 0x0a, /*Month */ 0x05, /*Day */
  579. 0x06, /*Hour */ 0x07, /*Minutes */ 0x08, 0x32
  580. };
  581. static const unsigned char packet_send_Contrast[] =
  582. { 0x17, 0x04, 0x24, /*Contrast */ 0x08 };
  583. static const unsigned char packet_send_StartTimer[] =
  584. { 0x17, 0x05, 0x0b, 0x05, 0x00, 0x17, 0x08, 0x16, /* Text */ 0x44, 0x75, 0x72, 0xe9,
  585. 0x65 };
  586. static const unsigned char packet_send_stop_timer[] = { 0x17, 0x05, 0x0b, 0x02, 0x00 };
  587. static const unsigned char packet_send_icon[] = { 0x17, 0x05, 0x14, /*pos */ 0x00, /*icon */ 0x25 }; /* display an icon in front of the text zone */
  588. static const unsigned char packet_send_S7[] = { 0x17, 0x06, 0x0f, 0x30, 0x07, 0x07 };
  589. static const unsigned char packet_send_set_pos_cursor[] =
  590. { 0x17, 0x06, 0x10, 0x81, 0x04, /*pos */ 0x20 };
  591. /*static unsigned char packet_send_MonthLabelsDownload[] =
  592. { 0x17, 0x0a, 0x15, Month (3 char) 0x46, 0x65, 0x62, 0x4d, 0xe4, 0x72, 0x20 }; */
  593. static const unsigned char packet_send_favorite[] =
  594. { 0x17, 0x0f, 0x19, 0x10, /*pos */ 0x01, /*name */ 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  595. 0x20, 0x20, 0x20, 0x20, /*end_name */ 0x19,
  596. 0x05, 0x0f, /*pos */ 0x01, /*icone */ 0x00
  597. };
  598. static const unsigned char packet_send_title[] =
  599. { 0x17, 0x10, 0x19, 0x02, /*text */ 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  600. 0x20, 0x20, 0x20, 0x20 /*end_text */ };
  601. static const unsigned char packet_send_text[] =
  602. { 0x17, 0x1e, 0x1b, 0x04, /*pos */ 0x00, /*inverse */ 0x25, /*text */ 0x20, 0x20,
  603. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  604. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  605. /*end_text */ 0x17, 0x04, 0x10, 0x87
  606. };
  607. static const unsigned char packet_send_status[] =
  608. { 0x17, 0x20, 0x19, 0x08, /*text */ 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  609. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  610. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 /*end_text */
  611. };
  612. static const unsigned char packet_send_status2[] =
  613. { 0x17, 0x0b, 0x19, /* pos [08|28|48|68] */ 0x00, /* text */ 0x20, 0x20, 0x20, 0x20,
  614. 0x20, 0x20, 0x20 /* end_text */ };
  615. static const unsigned char packet_send_led_update[] = { 0x19, 0x04, 0x00, 0x00 };
  616. static const unsigned char packet_send_query_basic_manager_04[] = { 0x1a, 0x04, 0x01, 0x04 };
  617. static const unsigned char packet_send_query_mac_address[] = { 0x1a, 0x04, 0x01, 0x08 };
  618. static const unsigned char packet_send_query_basic_manager_10[] = { 0x1a, 0x04, 0x01, 0x10 };
  619. static const unsigned char packet_send_S1[] = { 0x1a, 0x07, 0x07, 0x00, 0x00, 0x00, 0x13 };
  620. static unsigned char packet_send_ping[] =
  621. { 0x1e, 0x05, 0x12, 0x00, /*Watchdog timer */ 0x78 };
  622. #define BUFFSEND unsigned char buffsend[64] = { 0x00, 0x00, 0xaa, 0xbb, 0x02, 0x01 }
  623. static const char tdesc[] = "UNISTIM Channel Driver";
  624. static const char channel_type[] = "USTM";
  625. /*! Protos */
  626. static struct ast_channel *unistim_new(struct unistim_subchannel *sub, int state, const char *linkedid);
  627. static int load_module(void);
  628. static int reload(void);
  629. static int unload_module(void);
  630. static int reload_config(void);
  631. static void show_main_page(struct unistimsession *pte);
  632. static struct ast_channel *unistim_request(const char *type, format_t format, const struct ast_channel *requestor,
  633. void *data, int *cause);
  634. static int unistim_call(struct ast_channel *ast, char *dest, int timeout);
  635. static int unistim_hangup(struct ast_channel *ast);
  636. static int unistim_answer(struct ast_channel *ast);
  637. static struct ast_frame *unistim_read(struct ast_channel *ast);
  638. static int unistim_write(struct ast_channel *ast, struct ast_frame *frame);
  639. static int unistim_indicate(struct ast_channel *ast, int ind, const void *data,
  640. size_t datalen);
  641. static int unistim_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
  642. static int unistim_senddigit_begin(struct ast_channel *ast, char digit);
  643. static int unistim_senddigit_end(struct ast_channel *ast, char digit,
  644. unsigned int duration);
  645. static int unistim_sendtext(struct ast_channel *ast, const char *text);
  646. static int write_entry_history(struct unistimsession *pte, FILE * f, char c,
  647. char *line1);
  648. static void change_callerid(struct unistimsession *pte, int type, char *callerid);
  649. static const struct ast_channel_tech unistim_tech = {
  650. .type = channel_type,
  651. .description = tdesc,
  652. .capabilities = CAPABILITY,
  653. .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
  654. .requester = unistim_request,
  655. .call = unistim_call,
  656. .hangup = unistim_hangup,
  657. .answer = unistim_answer,
  658. .read = unistim_read,
  659. .write = unistim_write,
  660. .indicate = unistim_indicate,
  661. .fixup = unistim_fixup,
  662. .send_digit_begin = unistim_senddigit_begin,
  663. .send_digit_end = unistim_senddigit_end,
  664. .send_text = unistim_sendtext,
  665. .bridge = ast_rtp_instance_bridge,
  666. };
  667. static void display_last_error(const char *sz_msg)
  668. {
  669. time_t cur_time;
  670. time(&cur_time);
  671. /* Display the error message */
  672. ast_log(LOG_WARNING, "%s %s : (%u) %s\n", ctime(&cur_time), sz_msg, errno,
  673. strerror(errno));
  674. }
  675. static unsigned int get_tick_count(void)
  676. {
  677. struct timeval now = ast_tvnow();
  678. return (now.tv_sec * 1000) + (now.tv_usec / 1000);
  679. }
  680. /* Send data to a phone without retransmit nor buffering */
  681. static void send_raw_client(int size, const unsigned char *data, struct sockaddr_in *addr_to,
  682. const struct sockaddr_in *addr_ourip)
  683. {
  684. #ifdef HAVE_PKTINFO
  685. struct iovec msg_iov;
  686. struct msghdr msg;
  687. char buffer[CMSG_SPACE(sizeof(struct in_pktinfo))];
  688. struct cmsghdr *ip_msg = (struct cmsghdr *) buffer;
  689. struct in_pktinfo *pki = (struct in_pktinfo *) CMSG_DATA(ip_msg);
  690. /* cast this to a non-const pointer, since the sendmsg() API
  691. * does not provide read-only and write-only flavors of the
  692. * structures used for its arguments, but in this case we know
  693. * the data will not be modified
  694. */
  695. msg_iov.iov_base = (char *) data;
  696. msg_iov.iov_len = size;
  697. msg.msg_name = addr_to; /* optional address */
  698. msg.msg_namelen = sizeof(struct sockaddr_in); /* size of address */
  699. msg.msg_iov = &msg_iov; /* scatter/gather array */
  700. msg.msg_iovlen = 1; /* # elements in msg_iov */
  701. msg.msg_control = ip_msg; /* ancillary data */
  702. msg.msg_controllen = sizeof(buffer); /* ancillary data buffer len */
  703. msg.msg_flags = 0; /* flags on received message */
  704. ip_msg->cmsg_len = CMSG_LEN(sizeof(*pki));
  705. ip_msg->cmsg_level = IPPROTO_IP;
  706. ip_msg->cmsg_type = IP_PKTINFO;
  707. pki->ipi_ifindex = 0; /* Interface index, 0 = use interface specified in routing table */
  708. pki->ipi_spec_dst.s_addr = addr_ourip->sin_addr.s_addr; /* Local address */
  709. /* pki->ipi_addr = ; Header Destination address - ignored by kernel */
  710. #ifdef DUMP_PACKET
  711. if (unistimdebug) {
  712. int tmp;
  713. char iabuf[INET_ADDRSTRLEN];
  714. char iabuf2[INET_ADDRSTRLEN];
  715. ast_verb(0, "\n**> From %s sending %d bytes to %s ***\n",
  716. ast_inet_ntoa(addr_ourip->sin_addr), (int) size,
  717. ast_inet_ntoa(addr_to->sin_addr));
  718. for (tmp = 0; tmp < size; tmp++)
  719. ast_verb(0, "%.2x ", (unsigned char) data[tmp]);
  720. ast_verb(0, "\n******************************************\n");
  721. }
  722. #endif
  723. if (sendmsg(unistimsock, &msg, 0) == -1)
  724. display_last_error("Error sending datas");
  725. #else
  726. if (sendto(unistimsock, data, size, 0, (struct sockaddr *) addr_to, sizeof(*addr_to))
  727. == -1)
  728. display_last_error("Error sending datas");
  729. #endif
  730. }
  731. static void send_client(int size, const unsigned char *data, struct unistimsession *pte)
  732. {
  733. unsigned int tick;
  734. int buf_pos;
  735. unsigned short seq = ntohs(++pte->seq_server);
  736. ast_mutex_lock(&pte->lock);
  737. buf_pos = pte->last_buf_available;
  738. if (buf_pos >= MAX_BUF_NUMBER) {
  739. ast_log(LOG_WARNING, "Error : send queue overflow\n");
  740. ast_mutex_unlock(&pte->lock);
  741. return;
  742. }
  743. memcpy((void *)data + sizeof(unsigned short), (void *)&seq, sizeof(unsigned short));
  744. pte->wsabufsend[buf_pos].len = size;
  745. memcpy(pte->wsabufsend[buf_pos].buf, data, size);
  746. tick = get_tick_count();
  747. pte->timeout = tick + RETRANSMIT_TIMER;
  748. /*#ifdef DUMP_PACKET */
  749. if (unistimdebug)
  750. ast_verb(6, "Sending datas with seq #0x%.4x Using slot #%d :\n", pte->seq_server, buf_pos);
  751. /*#endif */
  752. send_raw_client(pte->wsabufsend[buf_pos].len, pte->wsabufsend[buf_pos].buf, &(pte->sin),
  753. &(pte->sout));
  754. pte->last_buf_available++;
  755. ast_mutex_unlock(&pte->lock);
  756. }
  757. static void send_ping(struct unistimsession *pte)
  758. {
  759. BUFFSEND;
  760. if (unistimdebug)
  761. ast_verb(6, "Sending ping\n");
  762. pte->tick_next_ping = get_tick_count() + unistim_keepalive;
  763. memcpy(buffsend + SIZE_HEADER, packet_send_ping, sizeof(packet_send_ping));
  764. send_client(SIZE_HEADER + sizeof(packet_send_ping), buffsend, pte);
  765. }
  766. static int get_to_address(int fd, struct sockaddr_in *toAddr)
  767. {
  768. #ifdef HAVE_PKTINFO
  769. int err;
  770. struct msghdr msg;
  771. struct {
  772. struct cmsghdr cm;
  773. int len;
  774. struct in_addr address;
  775. } ip_msg;
  776. /* Zero out the structures before we use them */
  777. /* This sets several key values to NULL */
  778. memset(&msg, 0, sizeof(msg));
  779. memset(&ip_msg, 0, sizeof(ip_msg));
  780. /* Initialize the message structure */
  781. msg.msg_control = &ip_msg;
  782. msg.msg_controllen = sizeof(ip_msg);
  783. /* Get info about the incoming packet */
  784. err = recvmsg(fd, &msg, MSG_PEEK);
  785. if (err == -1)
  786. ast_log(LOG_WARNING, "recvmsg returned an error: %s\n", strerror(errno));
  787. memcpy(&toAddr->sin_addr, &ip_msg.address, sizeof(struct in_addr));
  788. return err;
  789. #else
  790. memcpy(&toAddr, &public_ip, sizeof(&toAddr));
  791. return 0;
  792. #endif
  793. }
  794. /* Allocate memory & initialize structures for a new phone */
  795. /* addr_from : ip address of the phone */
  796. static struct unistimsession *create_client(const struct sockaddr_in *addr_from)
  797. {
  798. int tmp;
  799. struct unistimsession *s;
  800. if (!(s = ast_calloc(1, sizeof(*s))))
  801. return NULL;
  802. memcpy(&s->sin, addr_from, sizeof(struct sockaddr_in));
  803. get_to_address(unistimsock, &s->sout);
  804. s->sout.sin_family = AF_INET;
  805. if (unistimdebug) {
  806. ast_verb(0, "Creating a new entry for the phone from %s received via server ip %s\n",
  807. ast_inet_ntoa(addr_from->sin_addr), ast_inet_ntoa(s->sout.sin_addr));
  808. }
  809. ast_mutex_init(&s->lock);
  810. ast_mutex_lock(&sessionlock);
  811. s->next = sessions;
  812. sessions = s;
  813. s->timeout = get_tick_count() + RETRANSMIT_TIMER;
  814. s->seq_phone = (short) 0x0000;
  815. s->seq_server = (short) 0x0000;
  816. s->last_seq_ack = (short) 0x000;
  817. s->last_buf_available = 0;
  818. s->nb_retransmit = 0;
  819. s->state = STATE_INIT;
  820. s->tick_next_ping = get_tick_count() + unistim_keepalive;
  821. /* Initialize struct wsabuf */
  822. for (tmp = 0; tmp < MAX_BUF_NUMBER; tmp++) {
  823. s->wsabufsend[tmp].buf = s->buf[tmp];
  824. }
  825. ast_mutex_unlock(&sessionlock);
  826. return s;
  827. }
  828. static void send_end_call(struct unistimsession *pte)
  829. {
  830. BUFFSEND;
  831. if (unistimdebug)
  832. ast_verb(0, "Sending end call\n");
  833. memcpy(buffsend + SIZE_HEADER, packet_send_end_call, sizeof(packet_send_end_call));
  834. send_client(SIZE_HEADER + sizeof(packet_send_end_call), buffsend, pte);
  835. }
  836. static void set_ping_timer(struct unistimsession *pte)
  837. {
  838. unsigned int tick = 0; /* XXX what is this for, anyways */
  839. pte->timeout = pte->tick_next_ping;
  840. DEBUG_TIMER("tick = %u next ping at %u tick\n", tick, pte->timeout);
  841. return;
  842. }
  843. /* Checking if our send queue is empty,
  844. * if true, setting up a timer for keepalive */
  845. static void check_send_queue(struct unistimsession *pte)
  846. {
  847. /* Check if our send queue contained only one element */
  848. if (pte->last_buf_available == 1) {
  849. if (unistimdebug)
  850. ast_verb(6, "Our single packet was ACKed.\n");
  851. pte->last_buf_available--;
  852. set_ping_timer(pte);
  853. return;
  854. }
  855. /* Check if this ACK catch up our latest packet */
  856. else if (pte->last_seq_ack + 1 == pte->seq_server + 1) {
  857. if (unistimdebug)
  858. ast_verb(6, "Our send queue is completely ACKed.\n");
  859. pte->last_buf_available = 0; /* Purge the send queue */
  860. set_ping_timer(pte);
  861. return;
  862. }
  863. if (unistimdebug)
  864. ast_verb(6, "We still have packets in our send queue\n");
  865. return;
  866. }
  867. static void send_start_timer(struct unistimsession *pte)
  868. {
  869. BUFFSEND;
  870. if (unistimdebug)
  871. ast_verb(0, "Sending start timer\n");
  872. memcpy(buffsend + SIZE_HEADER, packet_send_StartTimer, sizeof(packet_send_StartTimer));
  873. send_client(SIZE_HEADER + sizeof(packet_send_StartTimer), buffsend, pte);
  874. }
  875. static void send_stop_timer(struct unistimsession *pte)
  876. {
  877. BUFFSEND;
  878. if (unistimdebug)
  879. ast_verb(0, "Sending stop timer\n");
  880. memcpy(buffsend + SIZE_HEADER, packet_send_stop_timer, sizeof(packet_send_stop_timer));
  881. send_client(SIZE_HEADER + sizeof(packet_send_stop_timer), buffsend, pte);
  882. }
  883. static void Sendicon(unsigned char pos, unsigned char status, struct unistimsession *pte)
  884. {
  885. BUFFSEND;
  886. if (unistimdebug)
  887. ast_verb(0, "Sending icon pos %d with status 0x%.2x\n", pos, status);
  888. memcpy(buffsend + SIZE_HEADER, packet_send_icon, sizeof(packet_send_icon));
  889. buffsend[9] = pos;
  890. buffsend[10] = status;
  891. send_client(SIZE_HEADER + sizeof(packet_send_icon), buffsend, pte);
  892. }
  893. static void send_tone(struct unistimsession *pte, uint16_t tone1, uint16_t tone2)
  894. {
  895. BUFFSEND;
  896. if (!tone1) {
  897. if (unistimdebug)
  898. ast_verb(0, "Sending Stream Based Tone Off\n");
  899. memcpy(buffsend + SIZE_HEADER, packet_send_stream_based_tone_off,
  900. sizeof(packet_send_stream_based_tone_off));
  901. send_client(SIZE_HEADER + sizeof(packet_send_stream_based_tone_off), buffsend, pte);
  902. return;
  903. }
  904. /* Since most of the world use a continuous tone, it's useless
  905. if (unistimdebug)
  906. ast_verb(0, "Sending Stream Based Tone Cadence Download\n");
  907. memcpy (buffsend + SIZE_HEADER, packet_send_StreamBasedToneCad, sizeof (packet_send_StreamBasedToneCad));
  908. send_client (SIZE_HEADER + sizeof (packet_send_StreamBasedToneCad), buffsend, pte); */
  909. if (unistimdebug)
  910. ast_verb(0, "Sending Stream Based Tone Frequency Component List Download %d %d\n", tone1, tone2);
  911. tone1 *= 8;
  912. if (!tone2) {
  913. memcpy(buffsend + SIZE_HEADER, packet_send_stream_based_tone_single_freq,
  914. sizeof(packet_send_stream_based_tone_single_freq));
  915. buffsend[10] = (tone1 & 0xff00) >> 8;
  916. buffsend[11] = (tone1 & 0x00ff);
  917. send_client(SIZE_HEADER + sizeof(packet_send_stream_based_tone_single_freq), buffsend,
  918. pte);
  919. } else {
  920. tone2 *= 8;
  921. memcpy(buffsend + SIZE_HEADER, packet_send_stream_based_tone_dial_freq,
  922. sizeof(packet_send_stream_based_tone_dial_freq));
  923. buffsend[10] = (tone1 & 0xff00) >> 8;
  924. buffsend[11] = (tone1 & 0x00ff);
  925. buffsend[12] = (tone2 & 0xff00) >> 8;
  926. buffsend[13] = (tone2 & 0x00ff);
  927. send_client(SIZE_HEADER + sizeof(packet_send_stream_based_tone_dial_freq), buffsend,
  928. pte);
  929. }
  930. if (unistimdebug)
  931. ast_verb(0, "Sending Stream Based Tone On\n");
  932. memcpy(buffsend + SIZE_HEADER, packet_send_stream_based_tone_on,
  933. sizeof(packet_send_stream_based_tone_on));
  934. send_client(SIZE_HEADER + sizeof(packet_send_stream_based_tone_on), buffsend, pte);
  935. }
  936. /* Positions for favorites
  937. |--------------------|
  938. | 5 2 |
  939. | 4 1 |
  940. | 3 0 |
  941. */
  942. /* status (icons) : 00 = nothing, 2x/3x = see parser.h, 4x/5x = blink fast, 6x/7x = blink slow */
  943. static void
  944. send_favorite(unsigned char pos, unsigned char status, struct unistimsession *pte,
  945. const char *text)
  946. {
  947. BUFFSEND;
  948. int i;
  949. if (unistimdebug)
  950. ast_verb(0, "Sending favorite pos %d with status 0x%.2x\n", pos, status);
  951. memcpy(buffsend + SIZE_HEADER, packet_send_favorite, sizeof(packet_send_favorite));
  952. buffsend[10] = pos;
  953. buffsend[24] = pos;
  954. buffsend[25] = status;
  955. i = strlen(text);
  956. if (i > FAV_MAX_LENGTH)
  957. i = FAV_MAX_LENGTH;
  958. memcpy(buffsend + FAV_MAX_LENGTH + 1, text, i);
  959. send_client(SIZE_HEADER + sizeof(packet_send_favorite), buffsend, pte);
  960. }
  961. static void refresh_all_favorite(struct unistimsession *pte)
  962. {
  963. int i = 0;
  964. if (unistimdebug)
  965. ast_verb(0, "Refreshing all favorite\n");
  966. for (i = 0; i < 6; i++) {
  967. if ((pte->device->softkeyicon[i] <= FAV_ICON_HEADPHONES_ONHOLD) &&
  968. (pte->device->softkeylinepos != i))
  969. send_favorite((unsigned char) i, pte->device->softkeyicon[i] + 1, pte,
  970. pte->device->softkeylabel[i]);
  971. else
  972. send_favorite((unsigned char) i, pte->device->softkeyicon[i], pte,
  973. pte->device->softkeylabel[i]);
  974. }
  975. }
  976. /* Change the status for this phone (pte) and update for each phones where pte is bookmarked
  977. * use FAV_ICON_*_BLACK constant in status parameters */
  978. static void change_favorite_icon(struct unistimsession *pte, unsigned char status)
  979. {
  980. struct unistim_device *d = devices;
  981. int i;
  982. /* Update the current phone */
  983. if (pte->state != STATE_CLEANING)
  984. send_favorite(pte->device->softkeylinepos, status, pte,
  985. pte->device->softkeylabel[pte->device->softkeylinepos]);
  986. /* Notify other phones if we're in their bookmark */
  987. while (d) {
  988. for (i = 0; i < 6; i++) {
  989. if (d->sp[i] == pte->device) { /* It's us ? */
  990. if (d->softkeyicon[i] != status) { /* Avoid resending the same icon */
  991. d->softkeyicon[i] = status;
  992. if (d->session)
  993. send_favorite(i, status + 1, d->session, d->softkeylabel[i]);
  994. }
  995. }
  996. }
  997. d = d->next;
  998. }
  999. }
  1000. static int RegisterExtension(const struct unistimsession *pte)
  1001. {
  1002. if (unistimdebug)
  1003. ast_verb(0, "Trying to register extension '%s' into context '%s' to %s\n",
  1004. pte->device->extension_number, pte->device->lines->context,
  1005. pte->device->lines->fullname);
  1006. return ast_add_extension(pte->device->lines->context, 0,
  1007. pte->device->extension_number, 1, NULL, NULL, "Dial",
  1008. pte->device->lines->fullname, 0, "Unistim");
  1009. }
  1010. static int UnregisterExtension(const struct unistimsession *pte)
  1011. {
  1012. if (unistimdebug)
  1013. ast_verb(0, "Trying to unregister extension '%s' context '%s'\n",
  1014. pte->device->extension_number, pte->device->lines->context);
  1015. return ast_context_remove_extension(pte->device->lines->context,
  1016. pte->device->extension_number, 1, "Unistim");
  1017. }
  1018. /* Free memory allocated for a phone */
  1019. static void close_client(struct unistimsession *s)
  1020. {
  1021. struct unistim_subchannel *sub;
  1022. struct unistimsession *cur, *prev = NULL;
  1023. ast_mutex_lock(&sessionlock);
  1024. cur = sessions;
  1025. /* Looking for the session in the linked chain */
  1026. while (cur) {
  1027. if (cur == s)
  1028. break;
  1029. prev = cur;
  1030. cur = cur->next;
  1031. }
  1032. if (cur) { /* Session found ? */
  1033. if (cur->device) { /* This session was registered ? */
  1034. s->state = STATE_CLEANING;
  1035. if (unistimdebug)
  1036. ast_verb(0, "close_client session %p device %p lines %p sub %p\n",
  1037. s, s->device, s->device->lines,
  1038. s->device->lines->subs[SUB_REAL]);
  1039. change_favorite_icon(s, FAV_ICON_NONE);
  1040. sub = s->device->lines->subs[SUB_REAL];
  1041. if (sub) {
  1042. if (sub->owner) { /* Call in progress ? */
  1043. if (unistimdebug)
  1044. ast_verb(0, "Aborting call\n");
  1045. ast_queue_hangup_with_cause(sub->owner, AST_CAUSE_NETWORK_OUT_OF_ORDER);
  1046. }
  1047. } else
  1048. ast_log(LOG_WARNING, "Freeing a client with no subchannel !\n");
  1049. if (!ast_strlen_zero(s->device->extension_number))
  1050. UnregisterExtension(s);
  1051. cur->device->session = NULL;
  1052. } else {
  1053. if (unistimdebug)
  1054. ast_verb(0, "Freeing an unregistered client\n");
  1055. }
  1056. if (prev)
  1057. prev->next = cur->next;
  1058. else
  1059. sessions = cur->next;
  1060. ast_mutex_destroy(&s->lock);
  1061. ast_free(s);
  1062. } else
  1063. ast_log(LOG_WARNING, "Trying to delete non-existent session %p?\n", s);
  1064. ast_mutex_unlock(&sessionlock);
  1065. return;
  1066. }
  1067. /* Return 1 if the session chained link was modified */
  1068. static int send_retransmit(struct unistimsession *pte)
  1069. {
  1070. int i;
  1071. ast_mutex_lock(&pte->lock);
  1072. if (++pte->nb_retransmit >= NB_MAX_RETRANSMIT) {
  1073. if (unistimdebug)
  1074. ast_verb(0, "Too many retransmit - freeing client\n");
  1075. ast_mutex_unlock(&pte->lock);
  1076. close_client(pte);
  1077. return 1;
  1078. }
  1079. pte->timeout = get_tick_count() + RETRANSMIT_TIMER;
  1080. for (i = pte->last_buf_available - (pte->seq_server - pte->last_seq_ack);
  1081. i < pte->last_buf_available; i++) {
  1082. if (i < 0) {
  1083. ast_log(LOG_WARNING,
  1084. "Asked to retransmit an ACKed slot ! last_buf_available=%d, seq_server = #0x%.4x last_seq_ack = #0x%.4x\n",
  1085. pte->last_buf_available, pte->seq_server, pte->last_seq_ack);
  1086. continue;
  1087. }
  1088. if (unistimdebug) {
  1089. unsigned short *sbuf = (unsigned short *) pte->wsabufsend[i].buf;
  1090. unsigned short seq;
  1091. seq = ntohs(sbuf[1]);
  1092. ast_verb(0, "Retransmit slot #%d (seq=#0x%.4x), last ack was #0x%.4x\n", i,
  1093. seq, pte->last_seq_ack);
  1094. }
  1095. send_raw_client(pte->wsabufsend[i].len, pte->wsabufsend[i].buf, &pte->sin,
  1096. &pte->sout);
  1097. }
  1098. ast_mutex_unlock(&pte->lock);
  1099. return 0;
  1100. }
  1101. /* inverse : TEXT_INVERSE : yes, TEXT_NORMAL : no */
  1102. static void
  1103. send_text(unsigned char pos, unsigned char inverse, struct unistimsession *pte,
  1104. const char *text)
  1105. {
  1106. int i;
  1107. BUFFSEND;
  1108. if (unistimdebug)
  1109. ast_verb(0, "Sending text at pos %d, inverse flag %d\n", pos, inverse);
  1110. memcpy(buffsend + SIZE_HEADER, packet_send_text, sizeof(packet_send_text));
  1111. buffsend[10] = pos;
  1112. buffsend[11] = inverse;
  1113. i = strlen(text);
  1114. if (i > TEXT_LENGTH_MAX)
  1115. i = TEXT_LENGTH_MAX;
  1116. memcpy(buffsend + 12, text, i);
  1117. send_client(SIZE_HEADER + sizeof(packet_send_text), buffsend, pte);
  1118. }
  1119. static void send_text_status(struct unistimsession *pte, const char *text)
  1120. {
  1121. BUFFSEND;
  1122. int i;
  1123. if (unistimdebug)
  1124. ast_verb(0, "Sending status text\n");
  1125. if (pte->device) {
  1126. if (pte->device->status_method == 1) { /* For new firmware and i2050 soft phone */
  1127. int n = strlen(text);
  1128. /* Must send individual button separately */
  1129. int j;
  1130. for (i = 0, j = 0; i < 4; i++, j += 7) {
  1131. int pos = 0x08 + (i * 0x20);
  1132. memcpy(buffsend + SIZE_HEADER, packet_send_status2,
  1133. sizeof(packet_send_status2));
  1134. buffsend[9] = pos;
  1135. memcpy(buffsend + 10, (j < n) ? (text + j) : " ", 7);
  1136. send_client(SIZE_HEADER + sizeof(packet_send_status2), buffsend, pte);
  1137. }
  1138. return;
  1139. }
  1140. }
  1141. memcpy(buffsend + SIZE_HEADER, packet_send_status, sizeof(packet_send_status));
  1142. i = strlen(text);
  1143. if (i > STATUS_LENGTH_MAX)
  1144. i = STATUS_LENGTH_MAX;
  1145. memcpy(buffsend + 10, text, i);
  1146. send_client(SIZE_HEADER + sizeof(packet_send_status), buffsend, pte);
  1147. }
  1148. /* led values in hexa : 0 = bar off, 1 = bar on, 2 = bar 1s on/1s off, 3 = bar 2.5s on/0.5s off
  1149. * 4 = bar 0.6s on/0.3s off, 5 = bar 0.5s on/0.5s off, 6 = bar 2s on/0.5s off
  1150. * 7 = bar off, 8 = speaker off, 9 = speaker on, 10 = headphone off, 11 = headphone on
  1151. * 18 = mute off, 19 mute on */
  1152. static void send_led_update(struct unistimsession *pte, unsigned char led)
  1153. {
  1154. BUFFSEND;
  1155. if (unistimdebug)
  1156. ast_verb(0, "Sending led_update (%x)\n", led);
  1157. memcpy(buffsend + SIZE_HEADER, packet_send_led_update, sizeof(packet_send_led_update));
  1158. buffsend[9] = led;
  1159. send_client(SIZE_HEADER + sizeof(packet_send_led_update), buffsend, pte);
  1160. }
  1161. /* output = OUTPUT_HANDSET, OUTPUT_HEADPHONE or OUTPUT_SPEAKER
  1162. * volume = VOLUME_LOW, VOLUME_NORMAL, VOLUME_INSANELY_LOUD
  1163. * mute = MUTE_OFF, MUTE_ON */
  1164. static void
  1165. send_select_output(struct unistimsession *pte, unsigned char output, unsigned char volume,
  1166. unsigned char mute)
  1167. {
  1168. BUFFSEND;
  1169. if (unistimdebug)
  1170. ast_verb(0, "Sending select output packet output=%x volume=%x mute=%x\n", output,
  1171. volume, mute);
  1172. memcpy(buffsend + SIZE_HEADER, packet_send_select_output,
  1173. sizeof(packet_send_select_output));
  1174. buffsend[9] = output;
  1175. if (output == OUTPUT_SPEAKER)
  1176. volume = VOLUME_LOW_SPEAKER;
  1177. else
  1178. volume = VOLUME_LOW;
  1179. buffsend[10] = volume;
  1180. if (mute == MUTE_ON_DISCRET)
  1181. buffsend[11] = MUTE_ON;
  1182. else
  1183. buffsend[11] = mute;
  1184. send_client(SIZE_HEADER + sizeof(packet_send_select_output), buffsend, pte);
  1185. if (mute == MUTE_OFF)
  1186. send_led_update(pte, 0x18);
  1187. else if (mute == MUTE_ON)
  1188. send_led_update(pte, 0x19);
  1189. pte->device->mute = mute;
  1190. if (output == OUTPUT_HANDSET) {
  1191. if (mute == MUTE_ON)
  1192. change_favorite_icon(pte, FAV_ICON_ONHOLD_BLACK);
  1193. else
  1194. change_favorite_icon(pte, FAV_ICON_OFFHOOK_BLACK);
  1195. send_led_update(pte, 0x08);
  1196. send_led_update(pte, 0x10);
  1197. } else if (output == OUTPUT_HEADPHONE) {
  1198. if (mute == MUTE_ON)
  1199. change_favorite_icon(pte, FAV_ICON_HEADPHONES_ONHOLD);
  1200. else
  1201. change_favorite_icon(pte, FAV_ICON_HEADPHONES);
  1202. send_led_update(pte, 0x08);
  1203. send_led_update(pte, 0x11);
  1204. } else if (output == OUTPUT_SPEAKER) {
  1205. send_led_update(pte, 0x10);
  1206. send_led_update(pte, 0x09);
  1207. if (pte->device->receiver_state == STATE_OFFHOOK) {
  1208. if (mute == MUTE_ON)
  1209. change_favorite_icon(pte, FAV_ICON_SPEAKER_ONHOLD_BLACK);
  1210. else
  1211. change_favorite_icon(pte, FAV_ICON_SPEAKER_ONHOOK_BLACK);
  1212. } else {
  1213. if (mute == MUTE_ON)
  1214. change_favorite_icon(pte, FAV_ICON_SPEAKER_ONHOLD_BLACK);
  1215. else
  1216. change_favorite_icon(pte, FAV_ICON_SPEAKER_OFFHOOK_BLACK);
  1217. }
  1218. } else
  1219. ast_log(LOG_WARNING, "Invalid output (%d)\n", output);
  1220. if (output != pte->device->output)
  1221. pte->device->previous_output = pte->device->output;
  1222. pte->device->output = output;
  1223. }
  1224. static void send_ring(struct unistimsession *pte, char volume, char style)
  1225. {
  1226. BUFFSEND;
  1227. if (unistimdebug)
  1228. ast_verb(0, "Sending ring packet\n");
  1229. memcpy(buffsend + SIZE_HEADER, packet_send_ring, sizeof(packet_send_ring));
  1230. buffsend[24] = style + 0x10;
  1231. buffsend[29] = volume * 0x10;
  1232. send_client(SIZE_HEADER + sizeof(packet_send_ring), buffsend, pte);
  1233. }
  1234. static void send_no_ring(struct unistimsession *pte)
  1235. {
  1236. BUFFSEND;
  1237. if (unistimdebug)
  1238. ast_verb(0, "Sending no ring packet\n");
  1239. memcpy(buffsend + SIZE_HEADER, packet_send_no_ring, sizeof(packet_send_no_ring));
  1240. send_client(SIZE_HEADER + sizeof(packet_send_no_ring), buffsend, pte);
  1241. }
  1242. static void send_texttitle(struct unistimsession *pte, const char *text)
  1243. {
  1244. BUFFSEND;
  1245. int i;
  1246. if (unistimdebug)
  1247. ast_verb(0, "Sending title text\n");
  1248. memcpy(buffsend + SIZE_HEADER, packet_send_title, sizeof(packet_send_title));
  1249. i = strlen(text);
  1250. if (i > 12)
  1251. i = 12;
  1252. memcpy(buffsend + 10, text, i);
  1253. send_client(SIZE_HEADER + sizeof(packet_send_title), buffsend, pte);
  1254. }
  1255. static void send_date_time(struct unistimsession *pte)
  1256. {
  1257. BUFFSEND;
  1258. struct timeval now = ast_tvnow();
  1259. struct ast_tm atm = { 0, };
  1260. if (unistimdebug)
  1261. ast_verb(0, "Sending Time & Date\n");
  1262. memcpy(buffsend + SIZE_HEADER, packet_send_date_time, sizeof(packet_send_date_time));
  1263. ast_localtime(&now, &atm, NULL);
  1264. buffsend[10] = (unsigned char) atm.tm_mon + 1;
  1265. buffsend[11] = (unsigned char) atm.tm_mday;
  1266. buffsend[12] = (unsigned char) atm.tm_hour;
  1267. buffsend[13] = (unsigned char) atm.tm_min;
  1268. send_client(SIZE_HEADER + sizeof(packet_send_date_time), buffsend, pte);
  1269. }
  1270. static void send_date_time2(struct unistimsession *pte)
  1271. {
  1272. BUFFSEND;
  1273. struct timeval now = ast_tvnow();
  1274. struct ast_tm atm = { 0, };
  1275. if (unistimdebug)
  1276. ast_verb(0, "Sending Time & Date #2\n");
  1277. memcpy(buffsend + SIZE_HEADER, packet_send_date_time2, sizeof(packet_send_date_time2));
  1278. ast_localtime(&now, &atm, NULL);
  1279. if (pte->device)
  1280. buffsend[9] = pte->device->datetimeformat;
  1281. else
  1282. buffsend[9] = 61;
  1283. buffsend[14] = (unsigned char) atm.tm_mon + 1;
  1284. buffsend[15] = (unsigned char) atm.tm_mday;
  1285. buffsend[16] = (unsigned char) atm.tm_hour;
  1286. buffsend[17] = (unsigned char) atm.tm_min;
  1287. send_client(SIZE_HEADER + sizeof(packet_send_date_time2), buffsend, pte);
  1288. }
  1289. static void send_date_time3(struct unistimsession *pte)
  1290. {
  1291. BUFFSEND;
  1292. struct timeval now = ast_tvnow();
  1293. struct ast_tm atm = { 0, };
  1294. if (unistimdebug)
  1295. ast_verb(0, "Sending Time & Date #3\n");
  1296. memcpy(buffsend + SIZE_HEADER, packet_send_date_time3, sizeof(packet_send_date_time3));
  1297. ast_localtime(&now, &atm, NULL);
  1298. buffsend[10] = (unsigned char) atm.tm_mon + 1;
  1299. buffsend[11] = (unsigned char) atm.tm_mday;
  1300. buffsend[12] = (unsigned char) atm.tm_hour;
  1301. buffsend[13] = (unsigned char) atm.tm_min;
  1302. send_client(SIZE_HEADER + sizeof(packet_send_date_time3), buffsend, pte);
  1303. }
  1304. static void send_blink_cursor(struct unistimsession *pte)
  1305. {
  1306. BUFFSEND;
  1307. if (unistimdebug)
  1308. ast_verb(0, "Sending set blink\n");
  1309. memcpy(buffsend + SIZE_HEADER, packet_send_blink_cursor, sizeof(packet_send_blink_cursor));
  1310. send_client(SIZE_HEADER + sizeof(packet_send_blink_cursor), buffsend, pte);
  1311. return;
  1312. }
  1313. /* pos : 0xab (a=0/2/4 = line ; b = row) */
  1314. static void send_cursor_pos(struct unistimsession *pte, unsigned char pos)
  1315. {
  1316. BUFFSEND;
  1317. if (unistimdebug)
  1318. ast_verb(0, "Sending set cursor position\n");
  1319. memcpy(buffsend + SIZE_HEADER, packet_send_set_pos_cursor,
  1320. sizeof(packet_send_set_pos_cursor));
  1321. buffsend[11] = pos;
  1322. send_client(SIZE_HEADER + sizeof(packet_send_set_pos_cursor), buffsend, pte);
  1323. return;
  1324. }
  1325. static void rcv_resume_connection_with_server(struct unistimsession *pte)
  1326. {
  1327. BUFFSEND;
  1328. if (unistimdebug) {
  1329. ast_verb(0, "ResumeConnectionWithServer received\n");
  1330. ast_verb(0, "Sending packet_send_query_mac_address\n");
  1331. }
  1332. memcpy(buffsend + SIZE_HEADER, packet_send_query_mac_address,
  1333. sizeof(packet_send_query_mac_address));
  1334. send_client(SIZE_HEADER + sizeof(packet_send_query_mac_address), buffsend, pte);
  1335. return;
  1336. }
  1337. static int unistim_register(struct unistimsession *s)
  1338. {
  1339. struct unistim_device *d;
  1340. ast_mutex_lock(&devicelock);
  1341. d = devices;
  1342. while (d) {
  1343. if (!strcasecmp(s->macaddr, d->id)) {
  1344. /* XXX Deal with IP authentication */
  1345. s->device = d;
  1346. d->session = s;
  1347. d->codec_number = DEFAULT_CODEC;
  1348. d->pos_fav = 0;
  1349. d->missed_call = 0;
  1350. d->receiver_state = STATE_ONHOOK;
  1351. break;
  1352. }
  1353. d = d->next;
  1354. }
  1355. ast_mutex_unlock(&devicelock);
  1356. if (!d)
  1357. return 0;
  1358. return 1;
  1359. }
  1360. static int alloc_sub(struct unistim_line *l, int x)
  1361. {
  1362. struct unistim_subchannel *sub;
  1363. if (!(sub = ast_calloc(1, sizeof(*sub))))
  1364. return 0;
  1365. if (unistimdebug)
  1366. ast_verb(3, "Allocating UNISTIM subchannel #%d on %s@%s ptr=%p\n", x, l->name, l->parent->name, sub);
  1367. sub->parent = l;
  1368. sub->subtype = x;
  1369. l->subs[x] = sub;
  1370. ast_mutex_init(&sub->lock);
  1371. return 1;
  1372. }
  1373. static int unalloc_sub(struct unistim_line *p, int x)
  1374. {
  1375. if (!x) {
  1376. ast_log(LOG_WARNING, "Trying to unalloc the real channel %s@%s?!?\n", p->name,
  1377. p->parent->name);
  1378. return -1;
  1379. }
  1380. if (unistimdebug)
  1381. ast_debug(1, "Released sub %d of channel %s@%s\n", x, p->name,
  1382. p->parent->name);
  1383. ast_mutex_destroy(&p->lock);
  1384. ast_free(p->subs[x]);
  1385. p->subs[x] = 0;
  1386. return 0;
  1387. }
  1388. static void rcv_mac_addr(struct unistimsession *pte, const unsigned char *buf)
  1389. {
  1390. BUFFSEND;
  1391. int tmp, i = 0;
  1392. char addrmac[19];
  1393. int res = 0;
  1394. if (unistimdebug)
  1395. ast_verb(0, "Mac Address received : ");
  1396. for (tmp = 15; tmp < 15 + SIZE_HEADER; tmp++) {
  1397. sprintf(&addrmac[i], "%.2x", (unsigned char) buf[tmp]);
  1398. i += 2;
  1399. }
  1400. if (unistimdebug)
  1401. ast_verb(0, "%s\n", addrmac);
  1402. strcpy(pte->macaddr, addrmac);
  1403. res = unistim_register(pte);
  1404. if (!res) {
  1405. switch (autoprovisioning) {
  1406. case AUTOPROVISIONING_NO:
  1407. ast_log(LOG_WARNING, "No entry found for this phone : %s\n", addrmac);
  1408. pte->state = STATE_AUTHDENY;
  1409. break;
  1410. case AUTOPROVISIONING_YES:
  1411. {
  1412. struct unistim_device *d, *newd;
  1413. struct unistim_line *newl;
  1414. if (unistimdebug)
  1415. ast_verb(0, "New phone, autoprovisioning on\n");
  1416. /* First : locate the [template] section */
  1417. ast_mutex_lock(&devicelock);
  1418. d = devices;
  1419. while (d) {
  1420. if (!strcasecmp(d->name, "template")) {
  1421. /* Found, cloning this entry */
  1422. if (!(newd = ast_malloc(sizeof(*newd)))) {
  1423. ast_mutex_unlock(&devicelock);
  1424. return;
  1425. }
  1426. memcpy(newd, d, sizeof(*newd));
  1427. if (!(newl = ast_malloc(sizeof(*newl)))) {
  1428. ast_free(newd);
  1429. ast_mutex_unlock(&devicelock);
  1430. return;
  1431. }
  1432. memcpy(newl, d->lines, sizeof(*newl));
  1433. if (!alloc_sub(newl, SUB_REAL)) {
  1434. ast_free(newd);
  1435. ast_free(newl);
  1436. ast_mutex_unlock(&devicelock);
  1437. return;
  1438. }
  1439. /* Ok, now updating some fields */
  1440. ast_copy_string(newd->id, addrmac, sizeof(newd->id));
  1441. ast_copy_string(newd->name, addrmac, sizeof(newd->name));
  1442. if (newd->extension == EXTENSION_NONE)
  1443. newd->extension = EXTENSION_ASK;
  1444. newd->lines = newl;
  1445. newd->receiver_state = STATE_ONHOOK;
  1446. newd->session = pte;
  1447. newd->to_delete = -1;
  1448. pte->device = newd;
  1449. newd->next = NULL;
  1450. newl->parent = newd;
  1451. strcpy(newl->name, d->lines->name);
  1452. snprintf(d->lines->name, sizeof(d->lines->name), "%d",
  1453. atoi(d->lines->name) + 1);
  1454. snprintf(newl->fullname, sizeof(newl->fullname), "USTM/%s@%s",
  1455. newl->name, newd->name);
  1456. /* Go to the end of the linked chain */
  1457. while (d->next) {
  1458. d = d->next;
  1459. }
  1460. d->next = newd;
  1461. d = newd;
  1462. break;
  1463. }
  1464. d = d->next;
  1465. }
  1466. ast_mutex_unlock(&devicelock);
  1467. if (!d) {
  1468. ast_log(LOG_WARNING, "No entry [template] found in unistim.conf\n");
  1469. pte->state = STATE_AUTHDENY;
  1470. }
  1471. }
  1472. break;
  1473. case AUTOPROVISIONING_TN:
  1474. pte->state = STATE_AUTHDENY;
  1475. break;
  1476. case AUTOPROVISIONING_DB:
  1477. ast_log(LOG_WARNING,
  1478. "Autoprovisioning with database is not yet functional\n");
  1479. break;
  1480. default:
  1481. ast_log(LOG_WARNING, "Internal error : unknown autoprovisioning value = %d\n",
  1482. autoprovisioning);
  1483. }
  1484. }
  1485. if (pte->state != STATE_AUTHDENY) {
  1486. ast_verb(3, "Device '%s' successfuly registered\n", pte->device->name);
  1487. switch (pte->device->extension) {
  1488. case EXTENSION_NONE:
  1489. pte->state = STATE_MAINPAGE;
  1490. break;
  1491. case EXTENSION_ASK:
  1492. /* Checking if we already have an extension number */
  1493. if (ast_strlen_zero(pte->device->extension_number))
  1494. pte->state = STATE_EXTENSION;
  1495. else {
  1496. /* Yes, because of a phone reboot. We don't ask again for the TN */
  1497. if (RegisterExtension(pte))
  1498. pte->state = STATE_EXTENSION;
  1499. else
  1500. pte->state = STATE_MAINPAGE;
  1501. }
  1502. break;
  1503. case EXTENSION_LINE:
  1504. ast_copy_string(pte->device->extension_number, pte->device->lines->name,
  1505. sizeof(pte->device->extension_number));
  1506. if (RegisterExtension(pte))
  1507. pte->state = STATE_EXTENSION;
  1508. else
  1509. pte->state = STATE_MAINPAGE;
  1510. break;
  1511. case EXTENSION_TN:
  1512. /* If we are here, it's because of a phone reboot */
  1513. pte->state = STATE_MAINPAGE;
  1514. break;
  1515. default:
  1516. ast_log(LOG_WARNING, "Internal error, extension value unknown : %d\n",
  1517. pte->device->extension);
  1518. pte->state = STATE_AUTHDENY;
  1519. break;
  1520. }
  1521. }
  1522. if (pte->state == STATE_EXTENSION) {
  1523. if (pte->device->extension != EXTENSION_TN)
  1524. pte->device->extension = EXTENSION_ASK;
  1525. pte->device->extension_number[0] = '\0';
  1526. }
  1527. if (unistimdebug)
  1528. ast_verb(0, "\nSending S1\n");
  1529. memcpy(buffsend + SIZE_HEADER, packet_send_S1, sizeof(packet_send_S1));
  1530. send_client(SIZE_HEADER + sizeof(packet_send_S1), buffsend, pte);
  1531. if (unistimdebug)
  1532. ast_verb(0, "Sending query_basic_manager_04\n");
  1533. memcpy(buffsend + SIZE_HEADER, packet_send_query_basic_manager_04,
  1534. sizeof(packet_send_query_basic_manager_04));
  1535. send_client(SIZE_HEADER + sizeof(packet_send_query_basic_manager_04), buffsend, pte);
  1536. if (unistimdebug)
  1537. ast_verb(0, "Sending query_basic_manager_10\n");
  1538. memcpy(buffsend + SIZE_HEADER, packet_send_query_basic_manager_10,
  1539. sizeof(packet_send_query_basic_manager_10));
  1540. send_client(SIZE_HEADER + sizeof(packet_send_query_basic_manager_10), buffsend, pte);
  1541. send_date_time(pte);
  1542. return;
  1543. }
  1544. static int write_entry_history(struct unistimsession *pte, FILE * f, char c, char *line1)
  1545. {
  1546. if (fwrite(&c, 1, 1, f) != 1) {
  1547. display_last_error("Unable to write history log header.");
  1548. return -1;
  1549. }
  1550. if (fwrite(line1, TEXT_LENGTH_MAX, 1, f) != 1) {
  1551. display_last_error("Unable to write history entry - date.");
  1552. return -1;
  1553. }
  1554. if (fwrite(pte->device->lst_cid, TEXT_LENGTH_MAX, 1, f) != 1) {
  1555. display_last_error("Unable to write history entry - callerid.");
  1556. return -1;
  1557. }
  1558. if (fwrite(pte->device->lst_cnm, TEXT_LENGTH_MAX, 1, f) != 1) {
  1559. display_last_error("Unable to write history entry - callername.");
  1560. return -1;
  1561. }
  1562. return 0;
  1563. }
  1564. static int write_history(struct unistimsession *pte, char way, char ismissed)
  1565. {
  1566. char tmp[AST_CONFIG_MAX_PATH], tmp2[AST_CONFIG_MAX_PATH];
  1567. char line1[TEXT_LENGTH_MAX + 1];
  1568. char count = 0, *histbuf;
  1569. int size;
  1570. FILE *f, *f2;
  1571. struct timeval now = ast_tvnow();
  1572. struct ast_tm atm = { 0, };
  1573. if (!pte->device)
  1574. return -1;
  1575. if (!pte->device->callhistory)
  1576. return 0;
  1577. if (strchr(pte->device->name, '/') || (pte->device->name[0] == '.')) {
  1578. ast_log(LOG_WARNING, "Account code '%s' insecure for writing file\n",
  1579. pte->device->name);
  1580. return -1;
  1581. }
  1582. snprintf(tmp, sizeof(tmp), "%s/%s", ast_config_AST_LOG_DIR, USTM_LOG_DIR);
  1583. if (ast_mkdir(tmp, 0770)) {
  1584. if (errno != EEXIST) {
  1585. display_last_error("Unable to create directory for history");
  1586. return -1;
  1587. }
  1588. }
  1589. ast_localtime(&now, &atm, NULL);
  1590. if (ismissed) {
  1591. if (way == 'i')
  1592. strcpy(tmp2, "Miss");
  1593. else
  1594. strcpy(tmp2, "Fail");
  1595. } else
  1596. strcpy(tmp2, "Answ");
  1597. snprintf(line1, sizeof(line1), "%04d/%02d/%02d %02d:%02d:%02d %s",
  1598. atm.tm_year + 1900, atm.tm_mon + 1, atm.tm_mday, atm.tm_hour,
  1599. atm.tm_min, atm.tm_sec, tmp2);
  1600. snprintf(tmp, sizeof(tmp), "%s/%s/%s-%c.csv", ast_config_AST_LOG_DIR,
  1601. USTM_LOG_DIR, pte->device->name, way);
  1602. if ((f = fopen(tmp, "r"))) {
  1603. struct stat bufstat;
  1604. if (stat(tmp, &bufstat)) {
  1605. display_last_error("Unable to stat history log.");
  1606. fclose(f);
  1607. return -1;
  1608. }
  1609. size = 1 + (MAX_ENTRY_LOG * TEXT_LENGTH_MAX * 3);
  1610. if (bufstat.st_size != size) {
  1611. ast_log(LOG_WARNING,
  1612. "History file %s has an incorrect size (%d instead of %d). It will be replaced by a new one.",
  1613. tmp, (int) bufstat.st_size, size);
  1614. fclose(f);
  1615. f = NULL;
  1616. count = 1;
  1617. }
  1618. }
  1619. /* If we can't open the log file, we create a brand new one */
  1620. if (!f) {
  1621. char c = 1;
  1622. int i;
  1623. if ((errno != ENOENT) && (count == 0)) {
  1624. display_last_error("Unable to open history log.");
  1625. return -1;
  1626. }
  1627. f = fopen(tmp, "w");
  1628. if (!f) {
  1629. display_last_error("Unable to create history log.");
  1630. return -1;
  1631. }
  1632. if (write_entry_history(pte, f, c, line1)) {
  1633. fclose(f);
  1634. return -1;
  1635. }
  1636. memset(line1, ' ', TEXT_LENGTH_MAX);
  1637. for (i = 3; i < MAX_ENTRY_LOG * 3; i++) {
  1638. if (fwrite(line1, TEXT_LENGTH_MAX, 1, f) != 1) {
  1639. display_last_error("Unable to write history entry - stuffing.");
  1640. fclose(f);
  1641. return -1;
  1642. }
  1643. }
  1644. if (fclose(f))
  1645. display_last_error("Unable to close history - creation.");
  1646. return 0;
  1647. }
  1648. /* We can open the log file, we create a temporary one, we add our entry and copy the rest */
  1649. if (fread(&count, 1, 1, f) != 1) {
  1650. display_last_error("Unable to read history header.");
  1651. fclose(f);
  1652. return -1;
  1653. }
  1654. if (count > MAX_ENTRY_LOG) {
  1655. ast_log(LOG_WARNING, "Invalid count in history header of %s (%d max %d)\n", tmp,
  1656. count, MAX_ENTRY_LOG);
  1657. fclose(f);
  1658. return -1;
  1659. }
  1660. snprintf(tmp2, sizeof(tmp2), "%s/%s/%s-%c.csv.tmp", ast_config_AST_LOG_DIR,
  1661. USTM_LOG_DIR, pte->device->name, way);
  1662. if (!(f2 = fopen(tmp2, "w"))) {
  1663. display_last_error("Unable to create temporary history log.");
  1664. fclose(f);
  1665. return -1;
  1666. }
  1667. if (++count > MAX_ENTRY_LOG)
  1668. count = MAX_ENTRY_LOG;
  1669. if (write_entry_history(pte, f2, count, line1)) {
  1670. fclose(f);
  1671. fclose(f2);
  1672. return -1;
  1673. }
  1674. size = (MAX_ENTRY_LOG - 1) * TEXT_LENGTH_MAX * 3;
  1675. if (!(histbuf = ast_malloc(size))) {
  1676. fclose(f);
  1677. fclose(f2);
  1678. return -1;
  1679. }
  1680. if (fread(histbuf, size, 1, f) != 1) {
  1681. ast_free(histbuf);
  1682. fclose(f);
  1683. fclose(f2);
  1684. display_last_error("Unable to read previous history entries.");
  1685. return -1;
  1686. }
  1687. if (fwrite(histbuf, size, 1, f2) != 1) {
  1688. ast_free(histbuf);
  1689. fclose(f);
  1690. fclose(f2);
  1691. display_last_error("Unable to write previous history entries.");
  1692. return -1;
  1693. }
  1694. ast_free(histbuf);
  1695. if (fclose(f))
  1696. display_last_error("Unable to close history log.");
  1697. if (fclose(f2))
  1698. display_last_error("Unable to close temporary history log.");
  1699. if (unlink(tmp))
  1700. display_last_error("Unable to remove old history log.");
  1701. if (rename(tmp2, tmp))
  1702. display_last_error("Unable to rename new history log.");
  1703. return 0;
  1704. }
  1705. static void cancel_dial(struct unistimsession *pte)
  1706. {
  1707. send_no_ring(pte);
  1708. pte->device->missed_call++;
  1709. write_history(pte, 'i', 1);
  1710. show_main_page(pte);
  1711. return;
  1712. }
  1713. static void swap_subs(struct unistim_line *p, int a, int b)
  1714. {
  1715. /* struct ast_channel *towner; */
  1716. struct ast_rtp_instance *rtp;
  1717. int fds;
  1718. if (unistimdebug)
  1719. ast_verb(0, "Swapping %d and %d\n", a, b);
  1720. if ((!p->subs[a]->owner) || (!p->subs[b]->owner)) {
  1721. ast_log(LOG_WARNING,
  1722. "Attempted to swap subchannels with a null owner : sub #%d=%p sub #%d=%p\n",
  1723. a, p->subs[a]->owner, b, p->subs[b]->owner);
  1724. return;
  1725. }
  1726. rtp = p->subs[a]->rtp;
  1727. p->subs[a]->rtp = p->subs[b]->rtp;
  1728. p->subs[b]->rtp = rtp;
  1729. fds = p->subs[a]->owner->fds[0];
  1730. p->subs[a]->owner->fds[0] = p->subs[b]->owner->fds[0];
  1731. p->subs[b]->owner->fds[0] = fds;
  1732. fds = p->subs[a]->owner->fds[1];
  1733. p->subs[a]->owner->fds[1] = p->subs[b]->owner->fds[1];
  1734. p->subs[b]->owner->fds[1] = fds;
  1735. }
  1736. static int attempt_transfer(struct unistim_subchannel *p1, struct unistim_subchannel *p2)
  1737. {
  1738. int res = 0;
  1739. struct ast_channel
  1740. *chana = NULL, *chanb = NULL, *bridgea = NULL, *bridgeb = NULL, *peera =
  1741. NULL, *peerb = NULL, *peerc = NULL;
  1742. if (!p1->owner || !p2->owner) {
  1743. ast_log(LOG_WARNING, "Transfer attempted without dual ownership?\n");
  1744. return -1;
  1745. }
  1746. chana = p1->owner;
  1747. chanb = p2->owner;
  1748. bridgea = ast_bridged_channel(chana);
  1749. bridgeb = ast_bridged_channel(chanb);
  1750. if (bridgea) {
  1751. peera = chana;
  1752. peerb = chanb;
  1753. peerc = bridgea;
  1754. } else if (bridgeb) {
  1755. peera = chanb;
  1756. peerb = chana;
  1757. peerc = bridgeb;
  1758. }
  1759. if (peera && peerb && peerc && (peerb != peerc)) {
  1760. /*ast_quiet_chan(peera);
  1761. ast_quiet_chan(peerb);
  1762. ast_quiet_chan(peerc);
  1763. ast_quiet_chan(peerd); */
  1764. if (peera->cdr && peerb->cdr) {
  1765. peerb->cdr = ast_cdr_append(peerb->cdr, peera->cdr);
  1766. } else if (peera->cdr) {
  1767. peerb->cdr = peera->cdr;
  1768. }
  1769. peera->cdr = NULL;
  1770. if (peerb->cdr && peerc->cdr) {
  1771. peerb->cdr = ast_cdr_append(peerb->cdr, peerc->cdr);
  1772. } else if (peerc->cdr) {
  1773. peerb->cdr = peerc->cdr;
  1774. }
  1775. peerc->cdr = NULL;
  1776. if (ast_channel_masquerade(peerb, peerc)) {
  1777. ast_log(LOG_WARNING, "Failed to masquerade %s into %s\n", peerb->name,
  1778. peerc->name);
  1779. res = -1;
  1780. }
  1781. return res;
  1782. } else {
  1783. ast_log(LOG_NOTICE,
  1784. "Transfer attempted with no appropriate bridged calls to transfer\n");
  1785. if (chana)
  1786. ast_softhangup_nolock(chana, AST_SOFTHANGUP_DEV);
  1787. if (chanb)
  1788. ast_softhangup_nolock(chanb, AST_SOFTHANGUP_DEV);
  1789. return -1;
  1790. }
  1791. return 0;
  1792. }
  1793. void change_callerid(struct unistimsession *pte, int type, char *callerid)
  1794. {
  1795. char *data;
  1796. int size;
  1797. if (type)
  1798. data = pte->device->lst_cnm;
  1799. else
  1800. data = pte->device->lst_cid;
  1801. /* This is very nearly strncpy(), except that the remaining buffer
  1802. * is padded with ' ', instead of '\0' */
  1803. memset(data, ' ', TEXT_LENGTH_MAX);
  1804. size = strlen(callerid);
  1805. if (size > TEXT_LENGTH_MAX)
  1806. size = TEXT_LENGTH_MAX;
  1807. memcpy(data, callerid, size);
  1808. }
  1809. static void close_call(struct unistimsession *pte)
  1810. {
  1811. struct unistim_subchannel *sub;
  1812. struct unistim_line *l = pte->device->lines;
  1813. sub = pte->device->lines->subs[SUB_REAL];
  1814. send_stop_timer(pte);
  1815. if (sub->owner) {
  1816. sub->alreadygone = 1;
  1817. if (l->subs[SUB_THREEWAY]) {
  1818. l->subs[SUB_THREEWAY]->alreadygone = 1;
  1819. if (attempt_transfer(sub, l->subs[SUB_THREEWAY]) < 0)
  1820. ast_verb(0, "attempt_transfer failed.\n");
  1821. } else
  1822. ast_queue_hangup(sub->owner);
  1823. } else {
  1824. if (l->subs[SUB_THREEWAY]) {
  1825. if (l->subs[SUB_THREEWAY]->owner)
  1826. ast_queue_hangup_with_cause(l->subs[SUB_THREEWAY]->owner, AST_CAUSE_NORMAL_CLEARING);
  1827. else
  1828. ast_log(LOG_WARNING, "threeway sub without owner\n");
  1829. } else
  1830. ast_verb(0, "USTM(%s@%s-%d) channel already destroyed\n", sub->parent->name,
  1831. sub->parent->parent->name, sub->subtype);
  1832. }
  1833. change_callerid(pte, 0, pte->device->redial_number);
  1834. change_callerid(pte, 1, "");
  1835. write_history(pte, 'o', pte->device->missed_call);
  1836. pte->device->missed_call = 0;
  1837. show_main_page(pte);
  1838. return;
  1839. }
  1840. static void IgnoreCall(struct unistimsession *pte)
  1841. {
  1842. send_no_ring(pte);
  1843. return;
  1844. }
  1845. static void *unistim_ss(void *data)
  1846. {
  1847. struct ast_channel *chan = data;
  1848. struct unistim_subchannel *sub = chan->tech_pvt;
  1849. struct unistim_line *l = sub->parent;
  1850. struct unistimsession *s = l->parent->session;
  1851. int res;
  1852. ast_verb(3, "Starting switch on '%s@%s-%d' to %s\n", l->name, l->parent->name, sub->subtype, s->device->phone_number);
  1853. ast_copy_string(chan->exten, s->device->phone_number, sizeof(chan->exten));
  1854. ast_copy_string(s->device->redial_number, s->device->phone_number,
  1855. sizeof(s->device->redial_number));
  1856. ast_setstate(chan, AST_STATE_RING);
  1857. res = ast_pbx_run(chan);
  1858. if (res) {
  1859. ast_log(LOG_WARNING, "PBX exited non-zero\n");
  1860. send_tone(s, 1000, 0);;
  1861. }
  1862. return NULL;
  1863. }
  1864. static void start_rtp(struct unistim_subchannel *sub)
  1865. {
  1866. BUFFSEND;
  1867. struct sockaddr_in us = { 0, };
  1868. struct sockaddr_in public = { 0, };
  1869. struct sockaddr_in sin = { 0, };
  1870. format_t codec;
  1871. struct sockaddr_in sout = { 0, };
  1872. struct ast_sockaddr us_tmp;
  1873. struct ast_sockaddr sin_tmp;
  1874. struct ast_sockaddr sout_tmp;
  1875. /* Sanity checks */
  1876. if (!sub) {
  1877. ast_log(LOG_WARNING, "start_rtp with a null subchannel !\n");
  1878. return;
  1879. }
  1880. if (!sub->parent) {
  1881. ast_log(LOG_WARNING, "start_rtp with a null line !\n");
  1882. return;
  1883. }
  1884. if (!sub->parent->parent) {
  1885. ast_log(LOG_WARNING, "start_rtp with a null device !\n");
  1886. return;
  1887. }
  1888. if (!sub->parent->parent->session) {
  1889. ast_log(LOG_WARNING, "start_rtp with a null session !\n");
  1890. return;
  1891. }
  1892. sout = sub->parent->parent->session->sout;
  1893. ast_mutex_lock(&sub->lock);
  1894. /* Allocate the RTP */
  1895. if (unistimdebug)
  1896. ast_verb(0, "Starting RTP. Bind on %s\n", ast_inet_ntoa(sout.sin_addr));
  1897. ast_sockaddr_from_sin(&sout_tmp, &sout);
  1898. sub->rtp = ast_rtp_instance_new("asterisk", sched, &sout_tmp, NULL);
  1899. if (!sub->rtp) {
  1900. ast_log(LOG_WARNING, "Unable to create RTP session: %s binaddr=%s\n",
  1901. strerror(errno), ast_inet_ntoa(sout.sin_addr));
  1902. ast_mutex_unlock(&sub->lock);
  1903. return;
  1904. }
  1905. ast_rtp_instance_set_prop(sub->rtp, AST_RTP_PROPERTY_RTCP, 1);
  1906. if (sub->owner) {
  1907. sub->owner->fds[0] = ast_rtp_instance_fd(sub->rtp, 0);
  1908. sub->owner->fds[1] = ast_rtp_instance_fd(sub->rtp, 1);
  1909. }
  1910. ast_rtp_instance_set_qos(sub->rtp, qos.tos_audio, qos.cos_audio, "UNISTIM RTP");
  1911. ast_rtp_instance_set_prop(sub->rtp, AST_RTP_PROPERTY_NAT, sub->parent->parent->nat);
  1912. /* Create the RTP connection */
  1913. ast_rtp_instance_get_local_address(sub->rtp, &us_tmp);
  1914. ast_sockaddr_to_sin(&us_tmp, &us);
  1915. sin.sin_family = AF_INET;
  1916. /* Setting up RTP for our side */
  1917. memcpy(&sin.sin_addr, &sub->parent->parent->session->sin.sin_addr,
  1918. sizeof(sin.sin_addr));
  1919. sin.sin_port = htons(sub->parent->parent->rtp_port);
  1920. ast_sockaddr_from_sin(&sin_tmp, &sin);
  1921. ast_rtp_instance_set_remote_address(sub->rtp, &sin_tmp);
  1922. if (!(sub->owner->nativeformats & sub->owner->readformat)) {
  1923. format_t fmt;
  1924. char tmp[256];
  1925. fmt = ast_best_codec(sub->owner->nativeformats);
  1926. ast_log(LOG_WARNING,
  1927. "Our read/writeformat has been changed to something incompatible: %s, using %s best codec from %s\n",
  1928. ast_getformatname(sub->owner->readformat),
  1929. ast_getformatname(fmt),
  1930. ast_getformatname_multiple(tmp, sizeof(tmp), sub->owner->nativeformats));
  1931. sub->owner->readformat = fmt;
  1932. sub->owner->writeformat = fmt;
  1933. }
  1934. codec = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(sub->rtp), 1, sub->owner->readformat);
  1935. /* Setting up RTP of the phone */
  1936. if (public_ip.sin_family == 0) /* NAT IP override ? */
  1937. memcpy(&public, &us, sizeof(public)); /* No defined, using IP from recvmsg */
  1938. else
  1939. memcpy(&public, &public_ip, sizeof(public)); /* override */
  1940. if (unistimdebug) {
  1941. ast_verb(0, "RTP started : Our IP/port is : %s:%hd with codec %s\n",
  1942. ast_inet_ntoa(us.sin_addr),
  1943. htons(us.sin_port), ast_getformatname(sub->owner->readformat));
  1944. ast_verb(0, "Starting phone RTP stack. Our public IP is %s\n",
  1945. ast_inet_ntoa(public.sin_addr));
  1946. }
  1947. if ((sub->owner->readformat == AST_FORMAT_ULAW) ||
  1948. (sub->owner->readformat == AST_FORMAT_ALAW)) {
  1949. if (unistimdebug)
  1950. ast_verb(0, "Sending packet_send_rtp_packet_size for codec %s\n", ast_getformatname(codec));
  1951. memcpy(buffsend + SIZE_HEADER, packet_send_rtp_packet_size,
  1952. sizeof(packet_send_rtp_packet_size));
  1953. buffsend[10] = (int) codec & 0xffffffffLL;
  1954. send_client(SIZE_HEADER + sizeof(packet_send_rtp_packet_size), buffsend,
  1955. sub->parent->parent->session);
  1956. }
  1957. if (unistimdebug)
  1958. ast_verb(0, "Sending Jitter Buffer Parameters Configuration\n");
  1959. memcpy(buffsend + SIZE_HEADER, packet_send_jitter_buffer_conf,
  1960. sizeof(packet_send_jitter_buffer_conf));
  1961. send_client(SIZE_HEADER + sizeof(packet_send_jitter_buffer_conf), buffsend,
  1962. sub->parent->parent->session);
  1963. if (sub->parent->parent->rtp_method != 0) {
  1964. uint16_t rtcpsin_port = htons(us.sin_port) + 1; /* RTCP port is RTP + 1 */
  1965. if (unistimdebug)
  1966. ast_verb(0, "Sending OpenAudioStreamTX using method #%d\n",
  1967. sub->parent->parent->rtp_method);
  1968. if (sub->parent->parent->rtp_method == 3)
  1969. memcpy(buffsend + SIZE_HEADER, packet_send_open_audio_stream_tx3,
  1970. sizeof(packet_send_open_audio_stream_tx3));
  1971. else
  1972. memcpy(buffsend + SIZE_HEADER, packet_send_open_audio_stream_tx,
  1973. sizeof(packet_send_open_audio_stream_tx));
  1974. if (sub->parent->parent->rtp_method != 2) {
  1975. memcpy(buffsend + 28, &public.sin_addr, sizeof(public.sin_addr));
  1976. buffsend[20] = (htons(sin.sin_port) & 0xff00) >> 8;
  1977. buffsend[21] = (htons(sin.sin_port) & 0x00ff);
  1978. buffsend[23] = (rtcpsin_port & 0x00ff);
  1979. buffsend[22] = (rtcpsin_port & 0xff00) >> 8;
  1980. buffsend[25] = (us.sin_port & 0xff00) >> 8;
  1981. buffsend[24] = (us.sin_port & 0x00ff);
  1982. buffsend[27] = (rtcpsin_port & 0x00ff);
  1983. buffsend[26] = (rtcpsin_port & 0xff00) >> 8;
  1984. } else {
  1985. memcpy(buffsend + 23, &public.sin_addr, sizeof(public.sin_addr));
  1986. buffsend[15] = (htons(sin.sin_port) & 0xff00) >> 8;
  1987. buffsend[16] = (htons(sin.sin_port) & 0x00ff);
  1988. buffsend[20] = (us.sin_port & 0xff00) >> 8;
  1989. buffsend[19] = (us.sin_port & 0x00ff);
  1990. buffsend[11] = codec;
  1991. }
  1992. buffsend[12] = codec;
  1993. send_client(SIZE_HEADER + sizeof(packet_send_open_audio_stream_tx), buffsend,
  1994. sub->parent->parent->session);
  1995. if (unistimdebug)
  1996. ast_verb(0, "Sending OpenAudioStreamRX\n");
  1997. if (sub->parent->parent->rtp_method == 3)
  1998. memcpy(buffsend + SIZE_HEADER, packet_send_open_audio_stream_rx3,
  1999. sizeof(packet_send_open_audio_stream_rx3));
  2000. else
  2001. memcpy(buffsend + SIZE_HEADER, packet_send_open_audio_stream_rx,
  2002. sizeof(packet_send_open_audio_stream_rx));
  2003. if (sub->parent->parent->rtp_method != 2) {
  2004. memcpy(buffsend + 28, &public.sin_addr, sizeof(public.sin_addr));
  2005. buffsend[20] = (htons(sin.sin_port) & 0xff00) >> 8;
  2006. buffsend[21] = (htons(sin.sin_port) & 0x00ff);
  2007. buffsend[23] = (rtcpsin_port & 0x00ff);
  2008. buffsend[22] = (rtcpsin_port & 0xff00) >> 8;
  2009. buffsend[25] = (us.sin_port & 0xff00) >> 8;
  2010. buffsend[24] = (us.sin_port & 0x00ff);
  2011. buffsend[27] = (rtcpsin_port & 0x00ff);
  2012. buffsend[26] = (rtcpsin_port & 0xff00) >> 8;
  2013. } else {
  2014. memcpy(buffsend + 23, &public.sin_addr, sizeof(public.sin_addr));
  2015. buffsend[15] = (htons(sin.sin_port) & 0xff00) >> 8;
  2016. buffsend[16] = (htons(sin.sin_port) & 0x00ff);
  2017. buffsend[20] = (us.sin_port & 0xff00) >> 8;
  2018. buffsend[19] = (us.sin_port & 0x00ff);
  2019. buffsend[12] = codec;
  2020. }
  2021. buffsend[11] = codec;
  2022. send_client(SIZE_HEADER + sizeof(packet_send_open_audio_stream_rx), buffsend,
  2023. sub->parent->parent->session);
  2024. } else {
  2025. uint16_t rtcpsin_port = htons(us.sin_port) + 1; /* RTCP port is RTP + 1 */
  2026. if (unistimdebug)
  2027. ast_verb(0, "Sending packet_send_call default method\n");
  2028. memcpy(buffsend + SIZE_HEADER, packet_send_call, sizeof(packet_send_call));
  2029. memcpy(buffsend + 53, &public.sin_addr, sizeof(public.sin_addr));
  2030. /* Destination port when sending RTP */
  2031. buffsend[49] = (us.sin_port & 0x00ff);
  2032. buffsend[50] = (us.sin_port & 0xff00) >> 8;
  2033. /* Destination port when sending RTCP */
  2034. buffsend[52] = (rtcpsin_port & 0x00ff);
  2035. buffsend[51] = (rtcpsin_port & 0xff00) >> 8;
  2036. /* Codec */
  2037. buffsend[40] = codec;
  2038. buffsend[41] = codec;
  2039. if (sub->owner->readformat == AST_FORMAT_ULAW)
  2040. buffsend[42] = 1; /* 1 = 20ms (160 bytes), 2 = 40ms (320 bytes) */
  2041. else if (sub->owner->readformat == AST_FORMAT_ALAW)
  2042. buffsend[42] = 1; /* 1 = 20ms (160 bytes), 2 = 40ms (320 bytes) */
  2043. else if (sub->owner->readformat == AST_FORMAT_G723_1)
  2044. buffsend[42] = 2; /* 1 = 30ms (24 bytes), 2 = 60 ms (48 bytes) */
  2045. else if (sub->owner->readformat == AST_FORMAT_G729A)
  2046. buffsend[42] = 2; /* 1 = 10ms (10 bytes), 2 = 20ms (20 bytes) */
  2047. else
  2048. ast_log(LOG_WARNING, "Unsupported codec %s!\n",
  2049. ast_getformatname(sub->owner->readformat));
  2050. /* Source port for transmit RTP and Destination port for receiving RTP */
  2051. buffsend[45] = (htons(sin.sin_port) & 0xff00) >> 8;
  2052. buffsend[46] = (htons(sin.sin_port) & 0x00ff);
  2053. buffsend[47] = (rtcpsin_port & 0xff00) >> 8;
  2054. buffsend[48] = (rtcpsin_port & 0x00ff);
  2055. send_client(SIZE_HEADER + sizeof(packet_send_call), buffsend,
  2056. sub->parent->parent->session);
  2057. }
  2058. ast_mutex_unlock(&sub->lock);
  2059. }
  2060. static void SendDialTone(struct unistimsession *pte)
  2061. {
  2062. int i;
  2063. /* No country defined ? Using US tone */
  2064. if (ast_strlen_zero(pte->device->country)) {
  2065. if (unistimdebug)
  2066. ast_verb(0, "No country defined, using US tone\n");
  2067. send_tone(pte, 350, 440);
  2068. return;
  2069. }
  2070. if (strlen(pte->device->country) != 2) {
  2071. if (unistimdebug)
  2072. ast_verb(0, "Country code != 2 char, using US tone\n");
  2073. send_tone(pte, 350, 440);
  2074. return;
  2075. }
  2076. i = 0;
  2077. while (frequency[i].freq1) {
  2078. if ((frequency[i].country[0] == pte->device->country[0]) &&
  2079. (frequency[i].country[1] == pte->device->country[1])) {
  2080. if (unistimdebug)
  2081. ast_verb(0, "Country code found (%s), freq1=%d freq2=%d\n",
  2082. frequency[i].country, frequency[i].freq1, frequency[i].freq2);
  2083. send_tone(pte, frequency[i].freq1, frequency[i].freq2);
  2084. }
  2085. i++;
  2086. }
  2087. }
  2088. static void handle_dial_page(struct unistimsession *pte)
  2089. {
  2090. pte->state = STATE_DIALPAGE;
  2091. if (pte->device->call_forward[0] == -1) {
  2092. send_text(TEXT_LINE0, TEXT_NORMAL, pte, "");
  2093. send_text(TEXT_LINE1, TEXT_NORMAL, pte, "Enter forward");
  2094. send_text_status(pte, "ForwardCancel BackSpcErase");
  2095. if (pte->device->call_forward[1] != 0) {
  2096. char tmp[TEXT_LENGTH_MAX + 1];
  2097. ast_copy_string(pte->device->phone_number, pte->device->call_forward + 1,
  2098. sizeof(pte->device->phone_number));
  2099. pte->device->size_phone_number = strlen(pte->device->phone_number);
  2100. if (pte->device->size_phone_number > 15)
  2101. pte->device->size_phone_number = 15;
  2102. strcpy(tmp, "Number : ...............");
  2103. memcpy(tmp + 9, pte->device->phone_number, pte->device->size_phone_number);
  2104. if (pte->device->height == 1) {
  2105. send_text(TEXT_LINE0, TEXT_NORMAL, pte, tmp);
  2106. send_blink_cursor(pte);
  2107. send_cursor_pos(pte,
  2108. (unsigned char) (TEXT_LINE0 + 0x09 +
  2109. pte->device->size_phone_number));
  2110. } else {
  2111. send_text(TEXT_LINE2, TEXT_NORMAL, pte, tmp);
  2112. send_blink_cursor(pte);
  2113. send_cursor_pos(pte,
  2114. (unsigned char) (TEXT_LINE2 + 0x09 +
  2115. pte->device->size_phone_number));
  2116. }
  2117. send_led_update(pte, 0);
  2118. return;
  2119. }
  2120. } else {
  2121. if ((pte->device->output == OUTPUT_HANDSET) &&
  2122. (pte->device->receiver_state == STATE_ONHOOK))
  2123. send_select_output(pte, OUTPUT_SPEAKER, pte->device->volume, MUTE_OFF);
  2124. else
  2125. send_select_output(pte, pte->device->output, pte->device->volume, MUTE_OFF);
  2126. SendDialTone(pte);
  2127. if (pte->device->height > 1) {
  2128. send_text(TEXT_LINE0, TEXT_NORMAL, pte, "Enter the number to dial");
  2129. send_text(TEXT_LINE1, TEXT_NORMAL, pte, "and press Call");
  2130. }
  2131. send_text_status(pte, "Call Redial BackSpcErase");
  2132. }
  2133. if (pte->device->height == 1) {
  2134. send_text(TEXT_LINE0, TEXT_NORMAL, pte, "Number : ...............");
  2135. send_blink_cursor(pte);
  2136. send_cursor_pos(pte, TEXT_LINE0 + 0x09);
  2137. } else {
  2138. send_text(TEXT_LINE2, TEXT_NORMAL, pte, "Number : ...............");
  2139. send_blink_cursor(pte);
  2140. send_cursor_pos(pte, TEXT_LINE2 + 0x09);
  2141. }
  2142. pte->device->size_phone_number = 0;
  2143. pte->device->phone_number[0] = 0;
  2144. change_favorite_icon(pte, FAV_ICON_PHONE_BLACK);
  2145. Sendicon(TEXT_LINE0, FAV_ICON_NONE, pte);
  2146. pte->device->missed_call = 0;
  2147. send_led_update(pte, 0);
  2148. return;
  2149. }
  2150. /* Step 1 : Music On Hold for peer, Dialing screen for us */
  2151. static void TransferCallStep1(struct unistimsession *pte)
  2152. {
  2153. struct unistim_subchannel *sub;
  2154. struct unistim_line *p = pte->device->lines;
  2155. sub = p->subs[SUB_REAL];
  2156. if (!sub->owner) {
  2157. ast_log(LOG_WARNING, "Unable to find subchannel for music on hold\n");
  2158. return;
  2159. }
  2160. if (p->subs[SUB_THREEWAY]) {
  2161. if (unistimdebug)
  2162. ast_verb(0, "Transfer canceled, hangup our threeway channel\n");
  2163. if (p->subs[SUB_THREEWAY]->owner)
  2164. ast_queue_hangup_with_cause(p->subs[SUB_THREEWAY]->owner, AST_CAUSE_NORMAL_CLEARING);
  2165. else
  2166. ast_log(LOG_WARNING, "Canceling a threeway channel without owner\n");
  2167. return;
  2168. }
  2169. /* Start music on hold if appropriate */
  2170. if (pte->device->moh)
  2171. ast_log(LOG_WARNING, "Transfer with peer already listening music on hold\n");
  2172. else {
  2173. if (ast_bridged_channel(p->subs[SUB_REAL]->owner)) {
  2174. ast_moh_start(ast_bridged_channel(p->subs[SUB_REAL]->owner),
  2175. pte->device->lines->musicclass, NULL);
  2176. pte->device->moh = 1;
  2177. } else {
  2178. ast_log(LOG_WARNING, "Unable to find peer subchannel for music on hold\n");
  2179. return;
  2180. }
  2181. }
  2182. /* Silence our channel */
  2183. if (!pte->device->silence_generator) {
  2184. pte->device->silence_generator =
  2185. ast_channel_start_silence_generator(p->subs[SUB_REAL]->owner);
  2186. if (pte->device->silence_generator == NULL)
  2187. ast_log(LOG_WARNING, "Unable to start a silence generator.\n");
  2188. else if (unistimdebug)
  2189. ast_verb(0, "Starting silence generator\n");
  2190. }
  2191. handle_dial_page(pte);
  2192. }
  2193. /* From phone to PBX */
  2194. static void HandleCallOutgoing(struct unistimsession *s)
  2195. {
  2196. struct ast_channel *c;
  2197. struct unistim_subchannel *sub;
  2198. pthread_t t;
  2199. s->state = STATE_CALL;
  2200. sub = s->device->lines->subs[SUB_REAL];
  2201. if (!sub) {
  2202. ast_log(LOG_NOTICE, "No available lines on: %s\n", s->device->name);
  2203. return;
  2204. }
  2205. if (!sub->owner) { /* A call is already in progress ? */
  2206. c = unistim_new(sub, AST_STATE_DOWN, NULL); /* No, starting a new one */
  2207. if (c) {
  2208. /* Need to start RTP before calling ast_pbx_run */
  2209. if (!sub->rtp)
  2210. start_rtp(sub);
  2211. send_select_output(s, s->device->output, s->device->volume, MUTE_OFF);
  2212. if (s->device->height == 1) {
  2213. send_text(TEXT_LINE0, TEXT_NORMAL, s, s->device->phone_number);
  2214. } else {
  2215. send_text(TEXT_LINE0, TEXT_NORMAL, s, "Calling :");
  2216. send_text(TEXT_LINE1, TEXT_NORMAL, s, s->device->phone_number);
  2217. send_text(TEXT_LINE2, TEXT_NORMAL, s, "Dialing...");
  2218. }
  2219. send_text_status(s, "Hangup");
  2220. /* start switch */
  2221. if (ast_pthread_create(&t, NULL, unistim_ss, c)) {
  2222. display_last_error("Unable to create switch thread");
  2223. ast_queue_hangup_with_cause(c, AST_CAUSE_SWITCH_CONGESTION);
  2224. }
  2225. } else
  2226. ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n",
  2227. sub->parent->name, s->device->name);
  2228. } else { /* We already have a call, so we switch in a threeway call */
  2229. if (s->device->moh) {
  2230. struct unistim_subchannel *subchannel;
  2231. struct unistim_line *p = s->device->lines;
  2232. subchannel = p->subs[SUB_REAL];
  2233. if (!subchannel->owner) {
  2234. ast_log(LOG_WARNING, "Unable to find subchannel for music on hold\n");
  2235. return;
  2236. }
  2237. if (p->subs[SUB_THREEWAY]) {
  2238. ast_log(LOG_WARNING,
  2239. "Can't transfer while an another transfer is taking place\n");
  2240. return;
  2241. }
  2242. if (!alloc_sub(p, SUB_THREEWAY)) {
  2243. ast_log(LOG_WARNING, "Unable to allocate three-way subchannel\n");
  2244. return;
  2245. }
  2246. /* Stop the silence generator */
  2247. if (s->device->silence_generator) {
  2248. if (unistimdebug)
  2249. ast_verb(0, "Stopping silence generator\n");
  2250. ast_channel_stop_silence_generator(subchannel->owner,
  2251. s->device->silence_generator);
  2252. s->device->silence_generator = NULL;
  2253. }
  2254. send_tone(s, 0, 0);
  2255. /* Make new channel */
  2256. c = unistim_new(p->subs[SUB_THREEWAY], AST_STATE_DOWN, NULL);
  2257. if (!c) {
  2258. ast_log(LOG_WARNING, "Cannot allocate new structure on channel %p\n", p);
  2259. return;
  2260. }
  2261. /* Swap things around between the three-way and real call */
  2262. swap_subs(p, SUB_THREEWAY, SUB_REAL);
  2263. send_select_output(s, s->device->output, s->device->volume, MUTE_OFF);
  2264. if (s->device->height == 1) {
  2265. send_text(TEXT_LINE0, TEXT_NORMAL, s, s->device->phone_number);
  2266. } else {
  2267. send_text(TEXT_LINE0, TEXT_NORMAL, s, "Calling (pre-transfer)");
  2268. send_text(TEXT_LINE1, TEXT_NORMAL, s, s->device->phone_number);
  2269. send_text(TEXT_LINE2, TEXT_NORMAL, s, "Dialing...");
  2270. }
  2271. send_text_status(s, "TransfrCancel");
  2272. if (ast_pthread_create(&t, NULL, unistim_ss, p->subs[SUB_THREEWAY]->owner)) {
  2273. ast_log(LOG_WARNING, "Unable to start simple switch on channel %p\n", p);
  2274. ast_hangup(c);
  2275. return;
  2276. }
  2277. if (unistimdebug)
  2278. ast_verb(0, "Started three way call on channel %p (%s) subchan %d\n",
  2279. p->subs[SUB_THREEWAY]->owner, p->subs[SUB_THREEWAY]->owner->name,
  2280. p->subs[SUB_THREEWAY]->subtype);
  2281. } else
  2282. ast_debug(1, "Current sub [%s] already has owner\n", sub->owner->name);
  2283. }
  2284. return;
  2285. }
  2286. /* From PBX to phone */
  2287. static void HandleCallIncoming(struct unistimsession *s)
  2288. {
  2289. struct unistim_subchannel *sub;
  2290. s->state = STATE_CALL;
  2291. s->device->missed_call = 0;
  2292. send_no_ring(s);
  2293. sub = s->device->lines->subs[SUB_REAL];
  2294. if (!sub) {
  2295. ast_log(LOG_NOTICE, "No available lines on: %s\n", s->device->name);
  2296. return;
  2297. } else if (unistimdebug)
  2298. ast_verb(0, "Handle Call Incoming for %s@%s\n", sub->parent->name,
  2299. s->device->name);
  2300. start_rtp(sub);
  2301. if (!sub->rtp)
  2302. ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", sub->parent->name,
  2303. s->device->name);
  2304. ast_queue_control(sub->owner, AST_CONTROL_ANSWER);
  2305. send_text(TEXT_LINE2, TEXT_NORMAL, s, "is on-line");
  2306. send_text_status(s, "Hangup Transf");
  2307. send_start_timer(s);
  2308. if ((s->device->output == OUTPUT_HANDSET) &&
  2309. (s->device->receiver_state == STATE_ONHOOK))
  2310. send_select_output(s, OUTPUT_SPEAKER, s->device->volume, MUTE_OFF);
  2311. else
  2312. send_select_output(s, s->device->output, s->device->volume, MUTE_OFF);
  2313. s->device->start_call_timestamp = time(0);
  2314. write_history(s, 'i', 0);
  2315. return;
  2316. }
  2317. static int unistim_do_senddigit(struct unistimsession *pte, char digit)
  2318. {
  2319. struct ast_frame f = { .frametype = AST_FRAME_DTMF, .subclass.integer = digit, .src = "unistim" };
  2320. struct unistim_subchannel *sub;
  2321. sub = pte->device->lines->subs[SUB_REAL];
  2322. if (!sub->owner || sub->alreadygone) {
  2323. ast_log(LOG_WARNING, "Unable to find subchannel in dtmf senddigit\n");
  2324. return -1;
  2325. }
  2326. /* Send DTMF indication _before_ playing sounds */
  2327. ast_queue_frame(sub->owner, &f);
  2328. if (unistimdebug)
  2329. ast_verb(0, "Send Digit %c\n", digit);
  2330. switch (digit) {
  2331. case '0':
  2332. send_tone(pte, 941, 1336);
  2333. break;
  2334. case '1':
  2335. send_tone(pte, 697, 1209);
  2336. break;
  2337. case '2':
  2338. send_tone(pte, 697, 1336);
  2339. break;
  2340. case '3':
  2341. send_tone(pte, 697, 1477);
  2342. break;
  2343. case '4':
  2344. send_tone(pte, 770, 1209);
  2345. break;
  2346. case '5':
  2347. send_tone(pte, 770, 1336);
  2348. break;
  2349. case '6':
  2350. send_tone(pte, 770, 1477);
  2351. break;
  2352. case '7':
  2353. send_tone(pte, 852, 1209);
  2354. break;
  2355. case '8':
  2356. send_tone(pte, 852, 1336);
  2357. break;
  2358. case '9':
  2359. send_tone(pte, 852, 1477);
  2360. break;
  2361. case 'A':
  2362. send_tone(pte, 697, 1633);
  2363. break;
  2364. case 'B':
  2365. send_tone(pte, 770, 1633);
  2366. break;
  2367. case 'C':
  2368. send_tone(pte, 852, 1633);
  2369. break;
  2370. case 'D':
  2371. send_tone(pte, 941, 1633);
  2372. break;
  2373. case '*':
  2374. send_tone(pte, 941, 1209);
  2375. break;
  2376. case '#':
  2377. send_tone(pte, 941, 1477);
  2378. break;
  2379. default:
  2380. send_tone(pte, 500, 2000);
  2381. }
  2382. usleep(150000); /* XXX Less than perfect, blocking an important thread is not a good idea */
  2383. send_tone(pte, 0, 0);
  2384. return 0;
  2385. }
  2386. static void key_call(struct unistimsession *pte, char keycode)
  2387. {
  2388. if ((keycode >= KEY_0) && (keycode <= KEY_SHARP)) {
  2389. if (keycode == KEY_SHARP)
  2390. keycode = '#';
  2391. else if (keycode == KEY_STAR)
  2392. keycode = '*';
  2393. else
  2394. keycode -= 0x10;
  2395. unistim_do_senddigit(pte, keycode);
  2396. return;
  2397. }
  2398. switch (keycode) {
  2399. case KEY_HANGUP:
  2400. case KEY_FUNC1:
  2401. close_call(pte);
  2402. break;
  2403. case KEY_FUNC2:
  2404. TransferCallStep1(pte);
  2405. break;
  2406. case KEY_HEADPHN:
  2407. if (pte->device->output == OUTPUT_HEADPHONE)
  2408. send_select_output(pte, OUTPUT_HANDSET, pte->device->volume, MUTE_OFF);
  2409. else
  2410. send_select_output(pte, OUTPUT_HEADPHONE, pte->device->volume, MUTE_OFF);
  2411. break;
  2412. case KEY_LOUDSPK:
  2413. if (pte->device->output != OUTPUT_SPEAKER)
  2414. send_select_output(pte, OUTPUT_SPEAKER, pte->device->volume, MUTE_OFF);
  2415. else
  2416. send_select_output(pte, pte->device->previous_output, pte->device->volume,
  2417. MUTE_OFF);
  2418. break;
  2419. case KEY_MUTE:
  2420. if (!pte->device->moh) {
  2421. if (pte->device->mute == MUTE_ON)
  2422. send_select_output(pte, pte->device->output, pte->device->volume, MUTE_OFF);
  2423. else
  2424. send_select_output(pte, pte->device->output, pte->device->volume, MUTE_ON);
  2425. break;
  2426. }
  2427. case KEY_ONHOLD:
  2428. {
  2429. struct unistim_subchannel *sub;
  2430. struct ast_channel *bridgepeer = NULL;
  2431. sub = pte->device->lines->subs[SUB_REAL];
  2432. if (!sub->owner) {
  2433. ast_log(LOG_WARNING, "Unable to find subchannel for music on hold\n");
  2434. return;
  2435. }
  2436. if ((bridgepeer = ast_bridged_channel(sub->owner))) {
  2437. if (pte->device->moh) {
  2438. ast_moh_stop(bridgepeer);
  2439. pte->device->moh = 0;
  2440. send_select_output(pte, pte->device->output, pte->device->volume,
  2441. MUTE_OFF);
  2442. } else {
  2443. ast_moh_start(bridgepeer, pte->device->lines->musicclass, NULL);
  2444. pte->device->moh = 1;
  2445. send_select_output(pte, pte->device->output, pte->device->volume,
  2446. MUTE_ON);
  2447. }
  2448. } else
  2449. ast_log(LOG_WARNING,
  2450. "Unable to find peer subchannel for music on hold\n");
  2451. break;
  2452. }
  2453. }
  2454. return;
  2455. }
  2456. static void key_ringing(struct unistimsession *pte, char keycode)
  2457. {
  2458. if (keycode == KEY_FAV0 + pte->device->softkeylinepos) {
  2459. HandleCallIncoming(pte);
  2460. return;
  2461. }
  2462. switch (keycode) {
  2463. case KEY_HANGUP:
  2464. case KEY_FUNC4:
  2465. IgnoreCall(pte);
  2466. break;
  2467. case KEY_FUNC1:
  2468. HandleCallIncoming(pte);
  2469. break;
  2470. }
  2471. return;
  2472. }
  2473. static void Keyfavorite(struct unistimsession *pte, char keycode)
  2474. {
  2475. int fav;
  2476. if ((keycode < KEY_FAV1) && (keycode > KEY_FAV5)) {
  2477. ast_log(LOG_WARNING, "It's not a favorite key\n");
  2478. return;
  2479. }
  2480. if (keycode == KEY_FAV0)
  2481. return;
  2482. fav = keycode - KEY_FAV0;
  2483. if (pte->device->softkeyicon[fav] == 0)
  2484. return;
  2485. ast_copy_string(pte->device->phone_number, pte->device->softkeynumber[fav],
  2486. sizeof(pte->device->phone_number));
  2487. HandleCallOutgoing(pte);
  2488. return;
  2489. }
  2490. static void key_dial_page(struct unistimsession *pte, char keycode)
  2491. {
  2492. if (keycode == KEY_FUNC3) {
  2493. if (pte->device->size_phone_number <= 1)
  2494. keycode = KEY_FUNC4;
  2495. else {
  2496. pte->device->size_phone_number -= 2;
  2497. keycode = pte->device->phone_number[pte->device->size_phone_number] + 0x10;
  2498. }
  2499. }
  2500. if ((keycode >= KEY_0) && (keycode <= KEY_SHARP)) {
  2501. char tmpbuf[] = "Number : ...............";
  2502. int i = 0;
  2503. if (pte->device->size_phone_number >= 15)
  2504. return;
  2505. if (pte->device->size_phone_number == 0)
  2506. send_tone(pte, 0, 0);
  2507. while (i < pte->device->size_phone_number) {
  2508. tmpbuf[i + 9] = pte->device->phone_number[i];
  2509. i++;
  2510. }
  2511. if (keycode == KEY_SHARP)
  2512. keycode = '#';
  2513. else if (keycode == KEY_STAR)
  2514. keycode = '*';
  2515. else
  2516. keycode -= 0x10;
  2517. tmpbuf[i + 9] = keycode;
  2518. pte->device->phone_number[i] = keycode;
  2519. pte->device->size_phone_number++;
  2520. pte->device->phone_number[i + 1] = 0;
  2521. if (pte->device->height == 1) {
  2522. send_text(TEXT_LINE0, TEXT_NORMAL, pte, tmpbuf);
  2523. } else {
  2524. send_text(TEXT_LINE2, TEXT_NORMAL, pte, tmpbuf);
  2525. }
  2526. send_blink_cursor(pte);
  2527. send_cursor_pos(pte, (unsigned char) (TEXT_LINE2 + 0x0a + i));
  2528. return;
  2529. }
  2530. if (keycode == KEY_FUNC4) {
  2531. pte->device->size_phone_number = 0;
  2532. if (pte->device->height == 1) {
  2533. send_text(TEXT_LINE0, TEXT_NORMAL, pte, "Number : ...............");
  2534. send_blink_cursor(pte);
  2535. send_cursor_pos(pte, TEXT_LINE0 + 0x09);
  2536. } else {
  2537. send_text(TEXT_LINE2, TEXT_NORMAL, pte, "Number : ...............");
  2538. send_blink_cursor(pte);
  2539. send_cursor_pos(pte, TEXT_LINE2 + 0x09);
  2540. }
  2541. return;
  2542. }
  2543. if (pte->device->call_forward[0] == -1) {
  2544. if (keycode == KEY_FUNC1) {
  2545. ast_copy_string(pte->device->call_forward, pte->device->phone_number,
  2546. sizeof(pte->device->call_forward));
  2547. show_main_page(pte);
  2548. } else if ((keycode == KEY_FUNC2) || (keycode == KEY_HANGUP)) {
  2549. pte->device->call_forward[0] = '\0';
  2550. show_main_page(pte);
  2551. }
  2552. return;
  2553. }
  2554. switch (keycode) {
  2555. case KEY_FUNC2:
  2556. if (ast_strlen_zero(pte->device->redial_number))
  2557. break;
  2558. ast_copy_string(pte->device->phone_number, pte->device->redial_number,
  2559. sizeof(pte->device->phone_number));
  2560. case KEY_FUNC1:
  2561. HandleCallOutgoing(pte);
  2562. break;
  2563. case KEY_HANGUP:
  2564. if (pte->device->lines->subs[SUB_REAL]->owner) {
  2565. /* Stop the silence generator */
  2566. if (pte->device->silence_generator) {
  2567. if (unistimdebug)
  2568. ast_verb(0, "Stopping silence generator\n");
  2569. ast_channel_stop_silence_generator(pte->device->lines->subs[SUB_REAL]->
  2570. owner, pte->device->silence_generator);
  2571. pte->device->silence_generator = NULL;
  2572. }
  2573. send_tone(pte, 0, 0);
  2574. ast_moh_stop(ast_bridged_channel(pte->device->lines->subs[SUB_REAL]->owner));
  2575. pte->device->moh = 0;
  2576. pte->state = STATE_CALL;
  2577. if (pte->device->height == 1) {
  2578. send_text(TEXT_LINE0, TEXT_NORMAL, pte, "Dial Cancel,back to priv. call.");
  2579. } else {
  2580. send_text(TEXT_LINE0, TEXT_NORMAL, pte, "Dialing canceled,");
  2581. send_text(TEXT_LINE1, TEXT_NORMAL, pte, "switching back to");
  2582. send_text(TEXT_LINE2, TEXT_NORMAL, pte, "previous call.");
  2583. }
  2584. send_text_status(pte, "Hangup Transf");
  2585. } else
  2586. show_main_page(pte);
  2587. break;
  2588. case KEY_FAV1:
  2589. case KEY_FAV2:
  2590. case KEY_FAV3:
  2591. case KEY_FAV4:
  2592. case KEY_FAV5:
  2593. Keyfavorite(pte, keycode);
  2594. break;
  2595. case KEY_LOUDSPK:
  2596. if (pte->device->output == OUTPUT_SPEAKER) {
  2597. if (pte->device->receiver_state == STATE_OFFHOOK)
  2598. send_select_output(pte, pte->device->previous_output, pte->device->volume,
  2599. MUTE_OFF);
  2600. else
  2601. show_main_page(pte);
  2602. } else
  2603. send_select_output(pte, OUTPUT_SPEAKER, pte->device->volume, MUTE_OFF);
  2604. break;
  2605. case KEY_HEADPHN:
  2606. if (pte->device->output == OUTPUT_HEADPHONE) {
  2607. if (pte->device->receiver_state == STATE_OFFHOOK)
  2608. send_select_output(pte, OUTPUT_HANDSET, pte->device->volume, MUTE_OFF);
  2609. else
  2610. show_main_page(pte);
  2611. } else
  2612. send_select_output(pte, OUTPUT_HEADPHONE, pte->device->volume, MUTE_OFF);
  2613. break;
  2614. }
  2615. return;
  2616. }
  2617. #define SELECTCODEC_START_ENTRY_POS 15
  2618. #define SELECTCODEC_MAX_LENGTH 2
  2619. #define SELECTCODEC_MSG "Codec number : .."
  2620. static void HandleSelectCodec(struct unistimsession *pte)
  2621. {
  2622. char buf[30], buf2[5];
  2623. pte->state = STATE_SELECTCODEC;
  2624. strcpy(buf, "Using codec ");
  2625. sprintf(buf2, "%d", pte->device->codec_number);
  2626. strcat(buf, buf2);
  2627. strcat(buf, " (G711u=0,");
  2628. send_text(TEXT_LINE0, TEXT_NORMAL, pte, buf);
  2629. send_text(TEXT_LINE1, TEXT_NORMAL, pte, "G723=4,G711a=8,G729A=18)");
  2630. send_text(TEXT_LINE2, TEXT_INVERSE, pte, SELECTCODEC_MSG);
  2631. send_blink_cursor(pte);
  2632. send_cursor_pos(pte, TEXT_LINE2 + SELECTCODEC_START_ENTRY_POS);
  2633. pte->size_buff_entry = 0;
  2634. send_text_status(pte, "Select BackSpcErase Cancel");
  2635. return;
  2636. }
  2637. static void key_select_codec(struct unistimsession *pte, char keycode)
  2638. {
  2639. if (keycode == KEY_FUNC2) {
  2640. if (pte->size_buff_entry <= 1)
  2641. keycode = KEY_FUNC3;
  2642. else {
  2643. pte->size_buff_entry -= 2;
  2644. keycode = pte->buff_entry[pte->size_buff_entry] + 0x10;
  2645. }
  2646. }
  2647. if ((keycode >= KEY_0) && (keycode <= KEY_9)) {
  2648. char tmpbuf[] = SELECTCODEC_MSG;
  2649. int i = 0;
  2650. if (pte->size_buff_entry >= SELECTCODEC_MAX_LENGTH)
  2651. return;
  2652. while (i < pte->size_buff_entry) {
  2653. tmpbuf[i + SELECTCODEC_START_ENTRY_POS] = pte->buff_entry[i];
  2654. i++;
  2655. }
  2656. tmpbuf[i + SELECTCODEC_START_ENTRY_POS] = keycode - 0x10;
  2657. pte->buff_entry[i] = keycode - 0x10;
  2658. pte->size_buff_entry++;
  2659. send_text(TEXT_LINE2, TEXT_INVERSE, pte, tmpbuf);
  2660. send_blink_cursor(pte);
  2661. send_cursor_pos(pte,
  2662. (unsigned char) (TEXT_LINE2 + SELECTCODEC_START_ENTRY_POS + 1 + i));
  2663. return;
  2664. }
  2665. switch (keycode) {
  2666. case KEY_FUNC1:
  2667. if (pte->size_buff_entry == 1)
  2668. pte->device->codec_number = pte->buff_entry[0] - 48;
  2669. else if (pte->size_buff_entry == 2)
  2670. pte->device->codec_number =
  2671. ((pte->buff_entry[0] - 48) * 10) + (pte->buff_entry[1] - 48);
  2672. show_main_page(pte);
  2673. break;
  2674. case KEY_FUNC3:
  2675. pte->size_buff_entry = 0;
  2676. send_text(TEXT_LINE2, TEXT_INVERSE, pte, SELECTCODEC_MSG);
  2677. send_blink_cursor(pte);
  2678. send_cursor_pos(pte, TEXT_LINE2 + SELECTCODEC_START_ENTRY_POS);
  2679. break;
  2680. case KEY_HANGUP:
  2681. case KEY_FUNC4:
  2682. show_main_page(pte);
  2683. break;
  2684. }
  2685. return;
  2686. }
  2687. #define SELECTEXTENSION_START_ENTRY_POS 0
  2688. #define SELECTEXTENSION_MAX_LENGTH 10
  2689. #define SELECTEXTENSION_MSG ".........."
  2690. static void ShowExtensionPage(struct unistimsession *pte)
  2691. {
  2692. pte->state = STATE_EXTENSION;
  2693. send_text(TEXT_LINE0, TEXT_NORMAL, pte, "Please enter a Terminal");
  2694. send_text(TEXT_LINE1, TEXT_NORMAL, pte, "Number (TN) :");
  2695. send_text(TEXT_LINE2, TEXT_NORMAL, pte, SELECTEXTENSION_MSG);
  2696. send_blink_cursor(pte);
  2697. send_cursor_pos(pte, TEXT_LINE2 + SELECTEXTENSION_START_ENTRY_POS);
  2698. send_text_status(pte, "Enter BackSpcErase");
  2699. pte->size_buff_entry = 0;
  2700. return;
  2701. }
  2702. static void key_select_extension(struct unistimsession *pte, char keycode)
  2703. {
  2704. if (keycode == KEY_FUNC2) {
  2705. if (pte->size_buff_entry <= 1)
  2706. keycode = KEY_FUNC3;
  2707. else {
  2708. pte->size_buff_entry -= 2;
  2709. keycode = pte->buff_entry[pte->size_buff_entry] + 0x10;
  2710. }
  2711. }
  2712. if ((keycode >= KEY_0) && (keycode <= KEY_9)) {
  2713. char tmpbuf[] = SELECTEXTENSION_MSG;
  2714. int i = 0;
  2715. if (pte->size_buff_entry >= SELECTEXTENSION_MAX_LENGTH)
  2716. return;
  2717. while (i < pte->size_buff_entry) {
  2718. tmpbuf[i + SELECTEXTENSION_START_ENTRY_POS] = pte->buff_entry[i];
  2719. i++;
  2720. }
  2721. tmpbuf[i + SELECTEXTENSION_START_ENTRY_POS] = keycode - 0x10;
  2722. pte->buff_entry[i] = keycode - 0x10;
  2723. pte->size_buff_entry++;
  2724. send_text(TEXT_LINE2, TEXT_NORMAL, pte, tmpbuf);
  2725. send_blink_cursor(pte);
  2726. send_cursor_pos(pte,
  2727. (unsigned char) (TEXT_LINE2 + SELECTEXTENSION_START_ENTRY_POS + 1 +
  2728. i));
  2729. return;
  2730. }
  2731. switch (keycode) {
  2732. case KEY_FUNC1:
  2733. if (pte->size_buff_entry < 1)
  2734. return;
  2735. if (autoprovisioning == AUTOPROVISIONING_TN) {
  2736. struct unistim_device *d;
  2737. /* First step : looking for this TN in our device list */
  2738. ast_mutex_lock(&devicelock);
  2739. d = devices;
  2740. pte->buff_entry[pte->size_buff_entry] = '\0';
  2741. while (d) {
  2742. if (d->id[0] == 'T') { /* It's a TN device ? */
  2743. /* It's the TN we're looking for ? */
  2744. if (!strcmp((d->id) + 1, pte->buff_entry)) {
  2745. pte->device = d;
  2746. d->session = pte;
  2747. d->codec_number = DEFAULT_CODEC;
  2748. d->pos_fav = 0;
  2749. d->missed_call = 0;
  2750. d->receiver_state = STATE_ONHOOK;
  2751. strcpy(d->id, pte->macaddr);
  2752. pte->device->extension_number[0] = 'T';
  2753. pte->device->extension = EXTENSION_TN;
  2754. ast_copy_string((pte->device->extension_number) + 1,
  2755. pte->buff_entry, pte->size_buff_entry + 1);
  2756. ast_mutex_unlock(&devicelock);
  2757. show_main_page(pte);
  2758. refresh_all_favorite(pte);
  2759. return;
  2760. }
  2761. }
  2762. d = d->next;
  2763. }
  2764. ast_mutex_unlock(&devicelock);
  2765. send_text(TEXT_LINE0, TEXT_NORMAL, pte, "Invalid Terminal Number.");
  2766. send_text(TEXT_LINE1, TEXT_NORMAL, pte, "Please try again :");
  2767. send_cursor_pos(pte,
  2768. (unsigned char) (TEXT_LINE2 + SELECTEXTENSION_START_ENTRY_POS +
  2769. pte->size_buff_entry));
  2770. send_blink_cursor(pte);
  2771. } else {
  2772. ast_copy_string(pte->device->extension_number, pte->buff_entry,
  2773. pte->size_buff_entry + 1);
  2774. if (RegisterExtension(pte)) {
  2775. send_text(TEXT_LINE0, TEXT_NORMAL, pte, "Invalid extension.");
  2776. send_text(TEXT_LINE1, TEXT_NORMAL, pte, "Please try again :");
  2777. send_cursor_pos(pte,
  2778. (unsigned char) (TEXT_LINE2 +
  2779. SELECTEXTENSION_START_ENTRY_POS +
  2780. pte->size_buff_entry));
  2781. send_blink_cursor(pte);
  2782. } else
  2783. show_main_page(pte);
  2784. }
  2785. break;
  2786. case KEY_FUNC3:
  2787. pte->size_buff_entry = 0;
  2788. send_text(TEXT_LINE2, TEXT_NORMAL, pte, SELECTEXTENSION_MSG);
  2789. send_blink_cursor(pte);
  2790. send_cursor_pos(pte, TEXT_LINE2 + SELECTEXTENSION_START_ENTRY_POS);
  2791. break;
  2792. }
  2793. return;
  2794. }
  2795. static int ReformatNumber(char *number)
  2796. {
  2797. int pos = 0, i = 0, size = strlen(number);
  2798. for (; i < size; i++) {
  2799. if ((number[i] >= '0') && (number[i] <= '9')) {
  2800. if (i == pos) {
  2801. pos++;
  2802. continue;
  2803. }
  2804. number[pos] = number[i];
  2805. pos++;
  2806. }
  2807. }
  2808. number[pos] = 0;
  2809. return pos;
  2810. }
  2811. static void show_entry_history(struct unistimsession *pte, FILE ** f)
  2812. {
  2813. char line[TEXT_LENGTH_MAX + 1], status[STATUS_LENGTH_MAX + 1], func1[10], func2[10],
  2814. func3[10];
  2815. if (fread(line, TEXT_LENGTH_MAX, 1, *f) != 1) {
  2816. display_last_error("Can't read history date entry");
  2817. fclose(*f);
  2818. return;
  2819. }
  2820. line[sizeof(line) - 1] = '\0';
  2821. send_text(TEXT_LINE0, TEXT_NORMAL, pte, line);
  2822. if (fread(line, TEXT_LENGTH_MAX, 1, *f) != 1) {
  2823. display_last_error("Can't read callerid entry");
  2824. fclose(*f);
  2825. return;
  2826. }
  2827. line[sizeof(line) - 1] = '\0';
  2828. ast_copy_string(pte->device->lst_cid, line, sizeof(pte->device->lst_cid));
  2829. send_text(TEXT_LINE1, TEXT_NORMAL, pte, line);
  2830. if (fread(line, TEXT_LENGTH_MAX, 1, *f) != 1) {
  2831. display_last_error("Can't read callername entry");
  2832. fclose(*f);
  2833. return;
  2834. }
  2835. line[sizeof(line) - 1] = '\0';
  2836. send_text(TEXT_LINE2, TEXT_NORMAL, pte, line);
  2837. fclose(*f);
  2838. snprintf(line, sizeof(line), "Call %03d/%03d", pte->buff_entry[2],
  2839. pte->buff_entry[1]);
  2840. send_texttitle(pte, line);
  2841. if (pte->buff_entry[2] == 1)
  2842. strcpy(func1, " ");
  2843. else
  2844. strcpy(func1, "Prvious");
  2845. if (pte->buff_entry[2] >= pte->buff_entry[1])
  2846. strcpy(func2, " ");
  2847. else
  2848. strcpy(func2, "Next ");
  2849. if (ReformatNumber(pte->device->lst_cid))
  2850. strcpy(func3, "Redial ");
  2851. else
  2852. strcpy(func3, " ");
  2853. snprintf(status, sizeof(status), "%s%s%sCancel", func1, func2, func3);
  2854. send_text_status(pte, status);
  2855. }
  2856. static char OpenHistory(struct unistimsession *pte, char way, FILE ** f)
  2857. {
  2858. char tmp[AST_CONFIG_MAX_PATH];
  2859. char count;
  2860. snprintf(tmp, sizeof(tmp), "%s/%s/%s-%c.csv", ast_config_AST_LOG_DIR,
  2861. USTM_LOG_DIR, pte->device->name, way);
  2862. *f = fopen(tmp, "r");
  2863. if (!*f) {
  2864. display_last_error("Unable to open history file");
  2865. return 0;
  2866. }
  2867. if (fread(&count, 1, 1, *f) != 1) {
  2868. display_last_error("Unable to read history header - display.");
  2869. fclose(*f);
  2870. return 0;
  2871. }
  2872. if (count > MAX_ENTRY_LOG) {
  2873. ast_log(LOG_WARNING, "Invalid count in history header of %s (%d max %d)\n", tmp,
  2874. count, MAX_ENTRY_LOG);
  2875. fclose(*f);
  2876. return 0;
  2877. }
  2878. return count;
  2879. }
  2880. static void show_history(struct unistimsession *pte, char way)
  2881. {
  2882. FILE *f;
  2883. char count;
  2884. if (!pte->device)
  2885. return;
  2886. if (!pte->device->callhistory)
  2887. return;
  2888. count = OpenHistory(pte, way, &f);
  2889. if (!count)
  2890. return;
  2891. pte->buff_entry[0] = way;
  2892. pte->buff_entry[1] = count;
  2893. pte->buff_entry[2] = 1;
  2894. show_entry_history(pte, &f);
  2895. pte->state = STATE_HISTORY;
  2896. }
  2897. static void show_main_page(struct unistimsession *pte)
  2898. {
  2899. char tmpbuf[TEXT_LENGTH_MAX + 1];
  2900. if ((pte->device->extension == EXTENSION_ASK) &&
  2901. (ast_strlen_zero(pte->device->extension_number))) {
  2902. ShowExtensionPage(pte);
  2903. return;
  2904. }
  2905. pte->state = STATE_MAINPAGE;
  2906. send_tone(pte, 0, 0);
  2907. send_select_output(pte, pte->device->output, pte->device->volume, MUTE_ON_DISCRET);
  2908. pte->device->lines->lastmsgssent = 0;
  2909. send_favorite(pte->device->softkeylinepos, FAV_ICON_ONHOOK_BLACK, pte,
  2910. pte->device->softkeylabel[pte->device->softkeylinepos]);
  2911. if (!ast_strlen_zero(pte->device->call_forward)) {
  2912. if (pte->device->height == 1) {
  2913. send_text(TEXT_LINE0, TEXT_NORMAL, pte, "Forwarding ON");
  2914. } else {
  2915. send_text(TEXT_LINE0, TEXT_NORMAL, pte, "Call forwarded to :");
  2916. send_text(TEXT_LINE1, TEXT_NORMAL, pte, pte->device->call_forward);
  2917. }
  2918. Sendicon(TEXT_LINE0, FAV_ICON_REFLECT + FAV_BLINK_SLOW, pte);
  2919. send_text_status(pte, "Dial Redial NoForwd");
  2920. } else {
  2921. if ((pte->device->extension == EXTENSION_ASK) ||
  2922. (pte->device->extension == EXTENSION_TN))
  2923. send_text_status(pte, "Dial Redial ForwardUnregis");
  2924. else
  2925. send_text_status(pte, "Dial Redial Forward");
  2926. send_text(TEXT_LINE1, TEXT_NORMAL, pte, pte->device->maintext1);
  2927. if (pte->device->missed_call == 0)
  2928. send_text(TEXT_LINE0, TEXT_NORMAL, pte, pte->device->maintext0);
  2929. else {
  2930. sprintf(tmpbuf, "%d unanswered call(s)", pte->device->missed_call);
  2931. send_text(TEXT_LINE0, TEXT_NORMAL, pte, tmpbuf);
  2932. Sendicon(TEXT_LINE0, FAV_ICON_CALL_CENTER + FAV_BLINK_SLOW, pte);
  2933. }
  2934. }
  2935. if (ast_strlen_zero(pte->device->maintext2)) {
  2936. strcpy(tmpbuf, "IP : ");
  2937. strcat(tmpbuf, ast_inet_ntoa(pte->sin.sin_addr));
  2938. send_text(TEXT_LINE2, TEXT_NORMAL, pte, tmpbuf);
  2939. } else
  2940. send_text(TEXT_LINE2, TEXT_NORMAL, pte, pte->device->maintext2);
  2941. send_texttitle(pte, pte->device->titledefault);
  2942. change_favorite_icon(pte, FAV_ICON_ONHOOK_BLACK);
  2943. }
  2944. static void key_main_page(struct unistimsession *pte, char keycode)
  2945. {
  2946. if (pte->device->missed_call) {
  2947. Sendicon(TEXT_LINE0, FAV_ICON_NONE, pte);
  2948. pte->device->missed_call = 0;
  2949. }
  2950. if ((keycode >= KEY_0) && (keycode <= KEY_SHARP)) {
  2951. handle_dial_page(pte);
  2952. key_dial_page(pte, keycode);
  2953. return;
  2954. }
  2955. switch (keycode) {
  2956. case KEY_FUNC1:
  2957. handle_dial_page(pte);
  2958. break;
  2959. case KEY_FUNC2:
  2960. if (ast_strlen_zero(pte->device->redial_number))
  2961. break;
  2962. if ((pte->device->output == OUTPUT_HANDSET) &&
  2963. (pte->device->receiver_state == STATE_ONHOOK))
  2964. send_select_output(pte, OUTPUT_SPEAKER, pte->device->volume, MUTE_OFF);
  2965. else
  2966. send_select_output(pte, pte->device->output, pte->device->volume, MUTE_OFF);
  2967. ast_copy_string(pte->device->phone_number, pte->device->redial_number,
  2968. sizeof(pte->device->phone_number));
  2969. HandleCallOutgoing(pte);
  2970. break;
  2971. case KEY_FUNC3:
  2972. if (!ast_strlen_zero(pte->device->call_forward)) {
  2973. /* Cancel call forwarding */
  2974. memmove(pte->device->call_forward + 1, pte->device->call_forward,
  2975. sizeof(pte->device->call_forward));
  2976. pte->device->call_forward[0] = '\0';
  2977. Sendicon(TEXT_LINE0, FAV_ICON_NONE, pte);
  2978. pte->device->output = OUTPUT_HANDSET; /* Seems to be reseted somewhere */
  2979. show_main_page(pte);
  2980. break;
  2981. }
  2982. pte->device->call_forward[0] = -1;
  2983. handle_dial_page(pte);
  2984. break;
  2985. case KEY_FUNC4:
  2986. if (pte->device->extension == EXTENSION_ASK) {
  2987. UnregisterExtension(pte);
  2988. pte->device->extension_number[0] = '\0';
  2989. ShowExtensionPage(pte);
  2990. } else if (pte->device->extension == EXTENSION_TN) {
  2991. ast_mutex_lock(&devicelock);
  2992. strcpy(pte->device->id, pte->device->extension_number);
  2993. pte->buff_entry[0] = '\0';
  2994. pte->size_buff_entry = 0;
  2995. pte->device->session = NULL;
  2996. pte->device = NULL;
  2997. ast_mutex_unlock(&devicelock);
  2998. ShowExtensionPage(pte);
  2999. }
  3000. break;
  3001. case KEY_FAV0:
  3002. handle_dial_page(pte);
  3003. break;
  3004. case KEY_FAV1:
  3005. case KEY_FAV2:
  3006. case KEY_FAV3:
  3007. case KEY_FAV4:
  3008. case KEY_FAV5:
  3009. if ((pte->device->output == OUTPUT_HANDSET) &&
  3010. (pte->device->receiver_state == STATE_ONHOOK))
  3011. send_select_output(pte, OUTPUT_SPEAKER, pte->device->volume, MUTE_OFF);
  3012. else
  3013. send_select_output(pte, pte->device->output, pte->device->volume, MUTE_OFF);
  3014. Keyfavorite(pte, keycode);
  3015. break;
  3016. case KEY_CONF:
  3017. HandleSelectCodec(pte);
  3018. break;
  3019. case KEY_LOUDSPK:
  3020. send_select_output(pte, OUTPUT_SPEAKER, pte->device->volume, MUTE_OFF);
  3021. handle_dial_page(pte);
  3022. break;
  3023. case KEY_HEADPHN:
  3024. send_select_output(pte, OUTPUT_HEADPHONE, pte->device->volume, MUTE_OFF);
  3025. handle_dial_page(pte);
  3026. break;
  3027. case KEY_SNDHIST:
  3028. show_history(pte, 'o');
  3029. break;
  3030. case KEY_RCVHIST:
  3031. show_history(pte, 'i');
  3032. break;
  3033. }
  3034. return;
  3035. }
  3036. static void key_history(struct unistimsession *pte, char keycode)
  3037. {
  3038. FILE *f;
  3039. char count;
  3040. long offset;
  3041. switch (keycode) {
  3042. case KEY_UP:
  3043. case KEY_LEFT:
  3044. case KEY_FUNC1:
  3045. if (pte->buff_entry[2] <= 1)
  3046. return;
  3047. pte->buff_entry[2]--;
  3048. count = OpenHistory(pte, pte->buff_entry[0], &f);
  3049. if (!count)
  3050. return;
  3051. offset = ((pte->buff_entry[2] - 1) * TEXT_LENGTH_MAX * 3);
  3052. if (fseek(f, offset, SEEK_CUR)) {
  3053. display_last_error("Unable to seek history entry.");
  3054. fclose(f);
  3055. return;
  3056. }
  3057. show_entry_history(pte, &f);
  3058. break;
  3059. case KEY_DOWN:
  3060. case KEY_RIGHT:
  3061. case KEY_FUNC2:
  3062. if (pte->buff_entry[2] >= pte->buff_entry[1])
  3063. return;
  3064. pte->buff_entry[2]++;
  3065. count = OpenHistory(pte, pte->buff_entry[0], &f);
  3066. if (!count)
  3067. return;
  3068. offset = ((pte->buff_entry[2] - 1) * TEXT_LENGTH_MAX * 3);
  3069. if (fseek(f, offset, SEEK_CUR)) {
  3070. display_last_error("Unable to seek history entry.");
  3071. fclose(f);
  3072. return;
  3073. }
  3074. show_entry_history(pte, &f);
  3075. break;
  3076. case KEY_FUNC3:
  3077. if (!ReformatNumber(pte->device->lst_cid))
  3078. break;
  3079. ast_copy_string(pte->device->redial_number, pte->device->lst_cid,
  3080. sizeof(pte->device->redial_number));
  3081. key_main_page(pte, KEY_FUNC2);
  3082. break;
  3083. case KEY_FUNC4:
  3084. case KEY_HANGUP:
  3085. show_main_page(pte);
  3086. break;
  3087. case KEY_SNDHIST:
  3088. if (pte->buff_entry[0] == 'i')
  3089. show_history(pte, 'o');
  3090. else
  3091. show_main_page(pte);
  3092. break;
  3093. case KEY_RCVHIST:
  3094. if (pte->buff_entry[0] == 'i')
  3095. show_main_page(pte);
  3096. else
  3097. show_history(pte, 'i');
  3098. break;
  3099. }
  3100. return;
  3101. }
  3102. static void init_phone_step2(struct unistimsession *pte)
  3103. {
  3104. BUFFSEND;
  3105. if (unistimdebug)
  3106. ast_verb(0, "Sending S4\n");
  3107. memcpy(buffsend + SIZE_HEADER, packet_send_s4, sizeof(packet_send_s4));
  3108. send_client(SIZE_HEADER + sizeof(packet_send_s4), buffsend, pte);
  3109. send_date_time2(pte);
  3110. send_date_time3(pte);
  3111. if (unistimdebug)
  3112. ast_verb(0, "Sending S7\n");
  3113. memcpy(buffsend + SIZE_HEADER, packet_send_S7, sizeof(packet_send_S7));
  3114. send_client(SIZE_HEADER + sizeof(packet_send_S7), buffsend, pte);
  3115. if (unistimdebug)
  3116. ast_verb(0, "Sending Contrast\n");
  3117. memcpy(buffsend + SIZE_HEADER, packet_send_Contrast, sizeof(packet_send_Contrast));
  3118. if (pte->device != NULL)
  3119. buffsend[9] = pte->device->contrast;
  3120. send_client(SIZE_HEADER + sizeof(packet_send_Contrast), buffsend, pte);
  3121. if (unistimdebug)
  3122. ast_verb(0, "Sending S9\n");
  3123. memcpy(buffsend + SIZE_HEADER, packet_send_s9, sizeof(packet_send_s9));
  3124. send_client(SIZE_HEADER + sizeof(packet_send_s9), buffsend, pte);
  3125. send_no_ring(pte);
  3126. if (unistimdebug)
  3127. ast_verb(0, "Sending S7\n");
  3128. memcpy(buffsend + SIZE_HEADER, packet_send_S7, sizeof(packet_send_S7));
  3129. send_client(SIZE_HEADER + sizeof(packet_send_S7), buffsend, pte);
  3130. send_led_update(pte, 0);
  3131. send_ping(pte);
  3132. if (pte->state < STATE_MAINPAGE) {
  3133. if (autoprovisioning == AUTOPROVISIONING_TN) {
  3134. ShowExtensionPage(pte);
  3135. return;
  3136. } else {
  3137. int i;
  3138. char tmp[30];
  3139. for (i = 1; i < 6; i++)
  3140. send_favorite(i, 0, pte, "");
  3141. send_text(TEXT_LINE0, TEXT_NORMAL, pte, "Sorry, this phone is not");
  3142. send_text(TEXT_LINE1, TEXT_NORMAL, pte, "registered in unistim.cfg");
  3143. strcpy(tmp, "MAC = ");
  3144. strcat(tmp, pte->macaddr);
  3145. send_text(TEXT_LINE2, TEXT_NORMAL, pte, tmp);
  3146. send_text_status(pte, "");
  3147. send_texttitle(pte, "UNISTIM for*");
  3148. return;
  3149. }
  3150. }
  3151. show_main_page(pte);
  3152. refresh_all_favorite(pte);
  3153. if (unistimdebug)
  3154. ast_verb(0, "Sending arrow\n");
  3155. memcpy(buffsend + SIZE_HEADER, packet_send_arrow, sizeof(packet_send_arrow));
  3156. send_client(SIZE_HEADER + sizeof(packet_send_arrow), buffsend, pte);
  3157. return;
  3158. }
  3159. static void process_request(int size, unsigned char *buf, struct unistimsession *pte)
  3160. {
  3161. char tmpbuf[255];
  3162. if (memcmp
  3163. (buf + SIZE_HEADER, packet_recv_resume_connection_with_server,
  3164. sizeof(packet_recv_resume_connection_with_server)) == 0) {
  3165. rcv_resume_connection_with_server(pte);
  3166. return;
  3167. }
  3168. if (memcmp(buf + SIZE_HEADER, packet_recv_firm_version, sizeof(packet_recv_firm_version)) ==
  3169. 0) {
  3170. buf[size] = 0;
  3171. if (unistimdebug)
  3172. ast_verb(0, "Got the firmware version : '%s'\n", buf + 13);
  3173. init_phone_step2(pte);
  3174. return;
  3175. }
  3176. if (memcmp(buf + SIZE_HEADER, packet_recv_mac_addr, sizeof(packet_recv_mac_addr)) == 0) {
  3177. rcv_mac_addr(pte, buf);
  3178. return;
  3179. }
  3180. if (memcmp(buf + SIZE_HEADER, packet_recv_r2, sizeof(packet_recv_r2)) == 0) {
  3181. if (unistimdebug)
  3182. ast_verb(0, "R2 received\n");
  3183. return;
  3184. }
  3185. if (pte->state < STATE_MAINPAGE) {
  3186. if (unistimdebug)
  3187. ast_verb(0, "Request not authorized in this state\n");
  3188. return;
  3189. }
  3190. if (!memcmp(buf + SIZE_HEADER, packet_recv_pressed_key, sizeof(packet_recv_pressed_key))) {
  3191. char keycode = buf[13];
  3192. if (unistimdebug)
  3193. ast_verb(0, "Key pressed : keycode = 0x%.2x - current state : %d\n", keycode,
  3194. pte->state);
  3195. switch (pte->state) {
  3196. case STATE_INIT:
  3197. if (unistimdebug)
  3198. ast_verb(0, "No keys allowed in the init state\n");
  3199. break;
  3200. case STATE_AUTHDENY:
  3201. if (unistimdebug)
  3202. ast_verb(0, "No keys allowed in authdeny state\n");
  3203. break;
  3204. case STATE_MAINPAGE:
  3205. key_main_page(pte, keycode);
  3206. break;
  3207. case STATE_DIALPAGE:
  3208. key_dial_page(pte, keycode);
  3209. break;
  3210. case STATE_RINGING:
  3211. key_ringing(pte, keycode);
  3212. break;
  3213. case STATE_CALL:
  3214. key_call(pte, keycode);
  3215. break;
  3216. case STATE_EXTENSION:
  3217. key_select_extension(pte, keycode);
  3218. break;
  3219. case STATE_SELECTCODEC:
  3220. key_select_codec(pte, keycode);
  3221. break;
  3222. case STATE_HISTORY:
  3223. key_history(pte, keycode);
  3224. break;
  3225. default:
  3226. ast_log(LOG_WARNING, "Key : Unknown state\n");
  3227. }
  3228. return;
  3229. }
  3230. if (memcmp(buf + SIZE_HEADER, packet_recv_pick_up, sizeof(packet_recv_pick_up)) == 0) {
  3231. if (unistimdebug)
  3232. ast_verb(0, "Handset off hook\n");
  3233. if (!pte->device) /* We are not yet registered (asking for a TN in AUTOPROVISIONING_TN) */
  3234. return;
  3235. pte->device->receiver_state = STATE_OFFHOOK;
  3236. if (pte->device->output == OUTPUT_HEADPHONE)
  3237. send_select_output(pte, OUTPUT_HEADPHONE, pte->device->volume, MUTE_OFF);
  3238. else
  3239. send_select_output(pte, OUTPUT_HANDSET, pte->device->volume, MUTE_OFF);
  3240. if (pte->state == STATE_RINGING)
  3241. HandleCallIncoming(pte);
  3242. else if ((pte->state == STATE_DIALPAGE) || (pte->state == STATE_CALL))
  3243. send_select_output(pte, OUTPUT_HANDSET, pte->device->volume, MUTE_OFF);
  3244. else if (pte->state == STATE_EXTENSION) /* We must have a TN before calling */
  3245. return;
  3246. else {
  3247. send_select_output(pte, OUTPUT_HANDSET, pte->device->volume, MUTE_OFF);
  3248. handle_dial_page(pte);
  3249. }
  3250. return;
  3251. }
  3252. if (memcmp(buf + SIZE_HEADER, packet_recv_hangup, sizeof(packet_recv_hangup)) == 0) {
  3253. if (unistimdebug)
  3254. ast_verb(0, "Handset on hook\n");
  3255. if (!pte->device)
  3256. return;
  3257. pte->device->receiver_state = STATE_ONHOOK;
  3258. if (pte->state == STATE_CALL)
  3259. close_call(pte);
  3260. else if (pte->device->lines->subs[SUB_REAL]->owner)
  3261. close_call(pte);
  3262. else if (pte->state == STATE_EXTENSION)
  3263. return;
  3264. else
  3265. show_main_page(pte);
  3266. return;
  3267. }
  3268. strcpy(tmpbuf, ast_inet_ntoa(pte->sin.sin_addr));
  3269. strcat(tmpbuf, " Unknown request packet\n");
  3270. if (unistimdebug)
  3271. ast_debug(1, "%s", tmpbuf);
  3272. return;
  3273. }
  3274. static void parsing(int size, unsigned char *buf, struct unistimsession *pte,
  3275. struct sockaddr_in *addr_from)
  3276. {
  3277. unsigned short *sbuf = (unsigned short *) buf;
  3278. unsigned short seq;
  3279. char tmpbuf[255];
  3280. strcpy(tmpbuf, ast_inet_ntoa(addr_from->sin_addr));
  3281. if (size < 10) {
  3282. if (size == 0) {
  3283. ast_log(LOG_WARNING, "%s Read error\n", tmpbuf);
  3284. } else {
  3285. ast_log(LOG_NOTICE, "%s Packet too short - ignoring\n", tmpbuf);
  3286. }
  3287. return;
  3288. }
  3289. if (sbuf[0] == 0xffff) { /* Starting with 0xffff ? *//* Yes, discovery packet ? */
  3290. if (size != sizeof(packet_rcv_discovery)) {
  3291. ast_log(LOG_NOTICE, "%s Invalid size of a discovery packet\n", tmpbuf);
  3292. } else {
  3293. if (memcmp(buf, packet_rcv_discovery, sizeof(packet_rcv_discovery)) == 0) {
  3294. if (unistimdebug)
  3295. ast_verb(0, "Discovery packet received - Sending Discovery ACK\n");
  3296. if (pte) { /* A session was already active for this IP ? */
  3297. if (pte->state == STATE_INIT) { /* Yes, but it's a dupe */
  3298. if (unistimdebug)
  3299. ast_verb(1, "Duplicated Discovery packet\n");
  3300. send_raw_client(sizeof(packet_send_discovery_ack),
  3301. packet_send_discovery_ack, addr_from, &pte->sout);
  3302. pte->seq_phone = (short) 0x0000; /* reset sequence number */
  3303. } else { /* No, probably a reboot, phone side */
  3304. close_client(pte); /* Cleanup the previous session */
  3305. if (create_client(addr_from))
  3306. send_raw_client(sizeof(packet_send_discovery_ack),
  3307. packet_send_discovery_ack, addr_from, &pte->sout);
  3308. }
  3309. } else {
  3310. /* Creating new entry in our phone list */
  3311. if ((pte = create_client(addr_from)))
  3312. send_raw_client(sizeof(packet_send_discovery_ack),
  3313. packet_send_discovery_ack, addr_from, &pte->sout);
  3314. }
  3315. return;
  3316. }
  3317. ast_log(LOG_NOTICE, "%s Invalid discovery packet\n", tmpbuf);
  3318. }
  3319. return;
  3320. }
  3321. if (!pte) {
  3322. if (unistimdebug)
  3323. ast_verb(0, "%s Not a discovery packet from an unknown source : ignoring\n",
  3324. tmpbuf);
  3325. return;
  3326. }
  3327. if (sbuf[0] != 0) { /* Starting with something else than 0x0000 ? */
  3328. ast_log(LOG_NOTICE, "Unknown packet received - ignoring\n");
  3329. return;
  3330. }
  3331. if (buf[5] != 2) {
  3332. ast_log(LOG_NOTICE, "%s Wrong direction : got 0x%.2x expected 0x02\n", tmpbuf,
  3333. buf[5]);
  3334. return;
  3335. }
  3336. seq = ntohs(sbuf[1]);
  3337. if (buf[4] == 1) {
  3338. ast_mutex_lock(&pte->lock);
  3339. if (unistimdebug)
  3340. ast_verb(6, "ACK received for packet #0x%.4x\n", seq);
  3341. pte->nb_retransmit = 0;
  3342. if ((pte->last_seq_ack) + 1 == seq) {
  3343. pte->last_seq_ack++;
  3344. check_send_queue(pte);
  3345. ast_mutex_unlock(&pte->lock);
  3346. return;
  3347. }
  3348. if (pte->last_seq_ack > seq) {
  3349. if (pte->last_seq_ack == 0xffff) {
  3350. ast_verb(0, "ACK at 0xffff, restarting counter.\n");
  3351. pte->last_seq_ack = 0;
  3352. } else
  3353. ast_log(LOG_NOTICE,
  3354. "%s Warning : ACK received for an already ACKed packet : #0x%.4x we are at #0x%.4x\n",
  3355. tmpbuf, seq, pte->last_seq_ack);
  3356. ast_mutex_unlock(&pte->lock);
  3357. return;
  3358. }
  3359. if (pte->seq_server < seq) {
  3360. ast_log(LOG_NOTICE,
  3361. "%s Error : ACK received for a non-existent packet : #0x%.4x\n",
  3362. tmpbuf, pte->seq_server);
  3363. ast_mutex_unlock(&pte->lock);
  3364. return;
  3365. }
  3366. if (unistimdebug)
  3367. ast_verb(0, "%s ACK gap : Received ACK #0x%.4x, previous was #0x%.4x\n",
  3368. tmpbuf, seq, pte->last_seq_ack);
  3369. pte->last_seq_ack = seq;
  3370. check_send_queue(pte);
  3371. ast_mutex_unlock(&pte->lock);
  3372. return;
  3373. }
  3374. if (buf[4] == 2) {
  3375. if (unistimdebug)
  3376. ast_verb(0, "Request received\n");
  3377. if (pte->seq_phone == seq) {
  3378. /* Send ACK */
  3379. buf[4] = 1;
  3380. buf[5] = 1;
  3381. send_raw_client(SIZE_HEADER, buf, addr_from, &pte->sout);
  3382. pte->seq_phone++;
  3383. process_request(size, buf, pte);
  3384. return;
  3385. }
  3386. if (pte->seq_phone > seq) {
  3387. ast_log(LOG_NOTICE,
  3388. "%s Warning : received a retransmitted packet : #0x%.4x (we are at #0x%.4x)\n",
  3389. tmpbuf, seq, pte->seq_phone);
  3390. /* BUG ? pte->device->seq_phone = seq; */
  3391. /* Send ACK */
  3392. buf[4] = 1;
  3393. buf[5] = 1;
  3394. send_raw_client(SIZE_HEADER, buf, addr_from, &pte->sout);
  3395. return;
  3396. }
  3397. ast_log(LOG_NOTICE,
  3398. "%s Warning : we lost a packet : received #0x%.4x (we are at #0x%.4x)\n",
  3399. tmpbuf, seq, pte->seq_phone);
  3400. return;
  3401. }
  3402. if (buf[4] == 0) {
  3403. ast_log(LOG_NOTICE, "%s Retransmit request for packet #0x%.4x\n", tmpbuf, seq);
  3404. if (pte->last_seq_ack > seq) {
  3405. ast_log(LOG_NOTICE,
  3406. "%s Error : received a request for an already ACKed packet : #0x%.4x\n",
  3407. tmpbuf, pte->last_seq_ack);
  3408. return;
  3409. }
  3410. if (pte->seq_server < seq) {
  3411. ast_log(LOG_NOTICE,
  3412. "%s Error : received a request for a non-existent packet : #0x%.4x\n",
  3413. tmpbuf, pte->seq_server);
  3414. return;
  3415. }
  3416. send_retransmit(pte);
  3417. return;
  3418. }
  3419. ast_log(LOG_NOTICE, "%s Unknown request : got 0x%.2x expected 0x00,0x01 or 0x02\n",
  3420. tmpbuf, buf[4]);
  3421. return;
  3422. }
  3423. static struct unistimsession *channel_to_session(struct ast_channel *ast)
  3424. {
  3425. struct unistim_subchannel *sub;
  3426. if (!ast) {
  3427. ast_log(LOG_WARNING, "Unistim callback function called with a null channel\n");
  3428. return NULL;
  3429. }
  3430. if (!ast->tech_pvt) {
  3431. ast_log(LOG_WARNING, "Unistim callback function called without a tech_pvt\n");
  3432. return NULL;
  3433. }
  3434. sub = ast->tech_pvt;
  3435. if (!sub->parent) {
  3436. ast_log(LOG_WARNING, "Unistim callback function called without a line\n");
  3437. return NULL;
  3438. }
  3439. if (!sub->parent->parent) {
  3440. ast_log(LOG_WARNING, "Unistim callback function called without a device\n");
  3441. return NULL;
  3442. }
  3443. if (!sub->parent->parent->session) {
  3444. ast_log(LOG_WARNING, "Unistim callback function called without a session\n");
  3445. return NULL;
  3446. }
  3447. return sub->parent->parent->session;
  3448. }
  3449. /*--- unistim_call: Initiate UNISTIM call from PBX ---*/
  3450. /* used from the dial() application */
  3451. static int unistim_call(struct ast_channel *ast, char *dest, int timeout)
  3452. {
  3453. int res = 0;
  3454. struct unistim_subchannel *sub;
  3455. struct unistimsession *session;
  3456. session = channel_to_session(ast);
  3457. if (!session) {
  3458. ast_log(LOG_ERROR, "Device not registered, cannot call %s\n", dest);
  3459. return -1;
  3460. }
  3461. sub = ast->tech_pvt;
  3462. if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
  3463. ast_log(LOG_WARNING, "unistim_call called on %s, neither down nor reserved\n",
  3464. ast->name);
  3465. return -1;
  3466. }
  3467. if (unistimdebug)
  3468. ast_verb(3, "unistim_call(%s)\n", ast->name);
  3469. session->state = STATE_RINGING;
  3470. Sendicon(TEXT_LINE0, FAV_ICON_NONE, session);
  3471. if (sub->owner) {
  3472. if (sub->owner->connected.id.number.valid
  3473. && sub->owner->connected.id.number.str) {
  3474. if (session->device->height == 1) {
  3475. send_text(TEXT_LINE0, TEXT_NORMAL, session, sub->owner->connected.id.number.str);
  3476. } else {
  3477. send_text(TEXT_LINE1, TEXT_NORMAL, session, sub->owner->connected.id.number.str);
  3478. }
  3479. change_callerid(session, 0, sub->owner->connected.id.number.str);
  3480. } else {
  3481. if (session->device->height == 1) {
  3482. send_text(TEXT_LINE0, TEXT_NORMAL, session, DEFAULTCALLERID);
  3483. } else {
  3484. send_text(TEXT_LINE1, TEXT_NORMAL, session, DEFAULTCALLERID);
  3485. }
  3486. change_callerid(session, 0, DEFAULTCALLERID);
  3487. }
  3488. if (sub->owner->connected.id.name.valid
  3489. && sub->owner->connected.id.name.str) {
  3490. send_text(TEXT_LINE0, TEXT_NORMAL, session, sub->owner->connected.id.name.str);
  3491. change_callerid(session, 1, sub->owner->connected.id.name.str);
  3492. } else {
  3493. send_text(TEXT_LINE0, TEXT_NORMAL, session, DEFAULTCALLERNAME);
  3494. change_callerid(session, 1, DEFAULTCALLERNAME);
  3495. }
  3496. }
  3497. send_text(TEXT_LINE2, TEXT_NORMAL, session, "is calling you.");
  3498. send_text_status(session, "Accept Ignore");
  3499. if (sub->ringstyle == -1)
  3500. send_ring(session, session->device->ringvolume, session->device->ringstyle);
  3501. else {
  3502. if (sub->ringvolume == -1)
  3503. send_ring(session, session->device->ringvolume, sub->ringstyle);
  3504. else
  3505. send_ring(session, sub->ringvolume, sub->ringstyle);
  3506. }
  3507. change_favorite_icon(session, FAV_ICON_SPEAKER_ONHOOK_BLACK + FAV_BLINK_FAST);
  3508. ast_setstate(ast, AST_STATE_RINGING);
  3509. ast_queue_control(ast, AST_CONTROL_RINGING);
  3510. return res;
  3511. }
  3512. /*--- unistim_hangup: Hangup UNISTIM call */
  3513. static int unistim_hangup(struct ast_channel *ast)
  3514. {
  3515. struct unistim_subchannel *sub;
  3516. struct unistim_line *l;
  3517. struct unistimsession *s;
  3518. s = channel_to_session(ast);
  3519. sub = ast->tech_pvt;
  3520. if (!s) {
  3521. ast_debug(1, "Asked to hangup channel not connected\n");
  3522. ast_mutex_lock(&sub->lock);
  3523. sub->owner = NULL;
  3524. ast->tech_pvt = NULL;
  3525. sub->alreadygone = 0;
  3526. ast_mutex_unlock(&sub->lock);
  3527. if (sub->rtp) {
  3528. if (unistimdebug)
  3529. ast_verb(0, "Destroying RTP session\n");
  3530. ast_rtp_instance_destroy(sub->rtp);
  3531. sub->rtp = NULL;
  3532. }
  3533. return 0;
  3534. }
  3535. l = sub->parent;
  3536. if (unistimdebug)
  3537. ast_verb(0, "unistim_hangup(%s) on %s@%s\n", ast->name, l->name, l->parent->name);
  3538. if ((l->subs[SUB_THREEWAY]) && (sub->subtype == SUB_REAL)) {
  3539. if (unistimdebug)
  3540. ast_verb(0, "Real call disconnected while talking to threeway\n");
  3541. sub->owner = NULL;
  3542. ast->tech_pvt = NULL;
  3543. return 0;
  3544. }
  3545. if ((l->subs[SUB_REAL]->owner) && (sub->subtype == SUB_THREEWAY) &&
  3546. (sub->alreadygone == 0)) {
  3547. if (unistimdebug)
  3548. ast_verb(0, "threeway call disconnected, switching to real call\n");
  3549. send_text(TEXT_LINE0, TEXT_NORMAL, s, "Three way call canceled,");
  3550. send_text(TEXT_LINE1, TEXT_NORMAL, s, "switching back to");
  3551. send_text(TEXT_LINE2, TEXT_NORMAL, s, "previous call.");
  3552. send_text_status(s, "Hangup Transf");
  3553. ast_moh_stop(ast_bridged_channel(l->subs[SUB_REAL]->owner));
  3554. swap_subs(l, SUB_THREEWAY, SUB_REAL);
  3555. l->parent->moh = 0;
  3556. ast_mutex_lock(&sub->lock);
  3557. sub->owner = NULL;
  3558. ast->tech_pvt = NULL;
  3559. ast_mutex_unlock(&sub->lock);
  3560. unalloc_sub(l, SUB_THREEWAY);
  3561. return 0;
  3562. }
  3563. ast_mutex_lock(&sub->lock);
  3564. sub->owner = NULL;
  3565. ast->tech_pvt = NULL;
  3566. sub->alreadygone = 0;
  3567. ast_mutex_unlock(&sub->lock);
  3568. if (!s) {
  3569. if (unistimdebug)
  3570. ast_verb(0, "Asked to hangup channel not connected (no session)\n");
  3571. if (sub->rtp) {
  3572. if (unistimdebug)
  3573. ast_verb(0, "Destroying RTP session\n");
  3574. ast_rtp_instance_destroy(sub->rtp);
  3575. sub->rtp = NULL;
  3576. }
  3577. return 0;
  3578. }
  3579. if (sub->subtype == SUB_REAL) {
  3580. /* Stop the silence generator */
  3581. if (s->device->silence_generator) {
  3582. if (unistimdebug)
  3583. ast_verb(0, "Stopping silence generator\n");
  3584. if (sub->owner)
  3585. ast_channel_stop_silence_generator(sub->owner,
  3586. s->device->silence_generator);
  3587. else
  3588. ast_log(LOG_WARNING,
  3589. "Trying to stop silence generator on a null channel !\n");
  3590. s->device->silence_generator = NULL;
  3591. }
  3592. }
  3593. l->parent->moh = 0;
  3594. send_no_ring(s);
  3595. send_end_call(s);
  3596. if (sub->rtp) {
  3597. if (unistimdebug)
  3598. ast_verb(0, "Destroying RTP session\n");
  3599. ast_rtp_instance_destroy(sub->rtp);
  3600. sub->rtp = NULL;
  3601. } else if (unistimdebug)
  3602. ast_verb(0, "No RTP session to destroy\n");
  3603. if (l->subs[SUB_THREEWAY]) {
  3604. if (unistimdebug)
  3605. ast_verb(0, "Cleaning other subchannels\n");
  3606. unalloc_sub(l, SUB_THREEWAY);
  3607. }
  3608. if (s->state == STATE_RINGING)
  3609. cancel_dial(s);
  3610. else if (s->state == STATE_CALL)
  3611. close_call(s);
  3612. return 0;
  3613. }
  3614. /*--- unistim_answer: Answer UNISTIM call */
  3615. static int unistim_answer(struct ast_channel *ast)
  3616. {
  3617. int res = 0;
  3618. struct unistim_subchannel *sub;
  3619. struct unistim_line *l;
  3620. struct unistimsession *s;
  3621. s = channel_to_session(ast);
  3622. if (!s) {
  3623. ast_log(LOG_WARNING, "unistim_answer on a disconnected device ?\n");
  3624. return -1;
  3625. }
  3626. sub = ast->tech_pvt;
  3627. l = sub->parent;
  3628. if ((!sub->rtp) && (!l->subs[SUB_THREEWAY]))
  3629. start_rtp(sub);
  3630. if (unistimdebug)
  3631. ast_verb(0, "unistim_answer(%s) on %s@%s-%d\n", ast->name, l->name,
  3632. l->parent->name, sub->subtype);
  3633. send_text(TEXT_LINE2, TEXT_NORMAL, l->parent->session, "is now on-line");
  3634. if (l->subs[SUB_THREEWAY])
  3635. send_text_status(l->parent->session, "Transf Cancel");
  3636. else
  3637. send_text_status(l->parent->session, "Hangup Transf");
  3638. send_start_timer(l->parent->session);
  3639. if (ast->_state != AST_STATE_UP)
  3640. ast_setstate(ast, AST_STATE_UP);
  3641. return res;
  3642. }
  3643. /*--- unistimsock_read: Read data from UNISTIM socket ---*/
  3644. /* Successful messages is connected to UNISTIM call and forwarded to parsing() */
  3645. static int unistimsock_read(int *id, int fd, short events, void *ignore)
  3646. {
  3647. struct sockaddr_in addr_from = { 0, };
  3648. struct unistimsession *cur = NULL;
  3649. int found = 0;
  3650. int tmp = 0;
  3651. int dw_num_bytes_rcvd;
  3652. #ifdef DUMP_PACKET
  3653. int dw_num_bytes_rcvdd;
  3654. char iabuf[INET_ADDRSTRLEN];
  3655. #endif
  3656. dw_num_bytes_rcvd =
  3657. recvfrom(unistimsock, buff, SIZE_PAGE, 0, (struct sockaddr *) &addr_from,
  3658. &size_addr_from);
  3659. if (dw_num_bytes_rcvd == -1) {
  3660. if (errno == EAGAIN)
  3661. ast_log(LOG_NOTICE, "UNISTIM: Received packet with bad UDP checksum\n");
  3662. else if (errno != ECONNREFUSED)
  3663. ast_log(LOG_WARNING, "Recv error %d (%s)\n", errno, strerror(errno));
  3664. return 1;
  3665. }
  3666. /* Looking in the phone list if we already have a registration for him */
  3667. ast_mutex_lock(&sessionlock);
  3668. cur = sessions;
  3669. while (cur) {
  3670. if (cur->sin.sin_addr.s_addr == addr_from.sin_addr.s_addr) {
  3671. found = 1;
  3672. break;
  3673. }
  3674. tmp++;
  3675. cur = cur->next;
  3676. }
  3677. ast_mutex_unlock(&sessionlock);
  3678. #ifdef DUMP_PACKET
  3679. if (unistimdebug)
  3680. ast_verb(0, "\n*** Dump %d bytes from %s - phone_table[%d] ***\n",
  3681. dw_num_bytes_rcvd, ast_inet_ntoa(addr_from.sin_addr), tmp);
  3682. for (dw_num_bytes_rcvdd = 0; dw_num_bytes_rcvdd < dw_num_bytes_rcvd;
  3683. dw_num_bytes_rcvdd++)
  3684. ast_verb(0, "%.2x ", (unsigned char) buff[dw_num_bytes_rcvdd]);
  3685. ast_verb(0, "\n******************************************\n");
  3686. #endif
  3687. if (!found) {
  3688. if (unistimdebug)
  3689. ast_verb(0, "Received a packet from an unknown source\n");
  3690. parsing(dw_num_bytes_rcvd, buff, NULL, (struct sockaddr_in *) &addr_from);
  3691. } else
  3692. parsing(dw_num_bytes_rcvd, buff, cur, (struct sockaddr_in *) &addr_from);
  3693. return 1;
  3694. }
  3695. static struct ast_frame *unistim_rtp_read(const struct ast_channel *ast,
  3696. const struct unistim_subchannel *sub)
  3697. {
  3698. /* Retrieve audio/etc from channel. Assumes sub->lock is already held. */
  3699. struct ast_frame *f;
  3700. if (!ast) {
  3701. ast_log(LOG_WARNING, "Channel NULL while reading\n");
  3702. return &ast_null_frame;
  3703. }
  3704. if (!sub->rtp) {
  3705. ast_log(LOG_WARNING, "RTP handle NULL while reading on subchannel %d\n",
  3706. sub->subtype);
  3707. return &ast_null_frame;
  3708. }
  3709. switch (ast->fdno) {
  3710. case 0:
  3711. f = ast_rtp_instance_read(sub->rtp, 0); /* RTP Audio */
  3712. break;
  3713. case 1:
  3714. f = ast_rtp_instance_read(sub->rtp, 1); /* RTCP Control Channel */
  3715. break;
  3716. default:
  3717. f = &ast_null_frame;
  3718. }
  3719. if (sub->owner) {
  3720. /* We already hold the channel lock */
  3721. if (f->frametype == AST_FRAME_VOICE) {
  3722. if (f->subclass.codec != sub->owner->nativeformats) {
  3723. ast_debug(1,
  3724. "Oooh, format changed from %s to %s\n",
  3725. ast_getformatname(sub->owner->nativeformats),
  3726. ast_getformatname(f->subclass.codec));
  3727. sub->owner->nativeformats = f->subclass.codec;
  3728. ast_set_read_format(sub->owner, sub->owner->readformat);
  3729. ast_set_write_format(sub->owner, sub->owner->writeformat);
  3730. }
  3731. }
  3732. }
  3733. return f;
  3734. }
  3735. static struct ast_frame *unistim_read(struct ast_channel *ast)
  3736. {
  3737. struct ast_frame *fr;
  3738. struct unistim_subchannel *sub = ast->tech_pvt;
  3739. ast_mutex_lock(&sub->lock);
  3740. fr = unistim_rtp_read(ast, sub);
  3741. ast_mutex_unlock(&sub->lock);
  3742. return fr;
  3743. }
  3744. static int unistim_write(struct ast_channel *ast, struct ast_frame *frame)
  3745. {
  3746. struct unistim_subchannel *sub = ast->tech_pvt;
  3747. int res = 0;
  3748. if (frame->frametype != AST_FRAME_VOICE) {
  3749. if (frame->frametype == AST_FRAME_IMAGE)
  3750. return 0;
  3751. else {
  3752. ast_log(LOG_WARNING, "Can't send %d type frames with unistim_write\n",
  3753. frame->frametype);
  3754. return 0;
  3755. }
  3756. } else {
  3757. if (!(frame->subclass.codec & ast->nativeformats)) {
  3758. char tmp[256];
  3759. ast_log(LOG_WARNING,
  3760. "Asked to transmit frame type %s, while native formats is %s (read/write = (%s/%s)\n",
  3761. ast_getformatname(frame->subclass.codec),
  3762. ast_getformatname_multiple(tmp, sizeof(tmp), ast->nativeformats),
  3763. ast_getformatname(ast->readformat),
  3764. ast_getformatname(ast->writeformat));
  3765. return -1;
  3766. }
  3767. }
  3768. if (sub) {
  3769. ast_mutex_lock(&sub->lock);
  3770. if (sub->rtp) {
  3771. res = ast_rtp_instance_write(sub->rtp, frame);
  3772. }
  3773. ast_mutex_unlock(&sub->lock);
  3774. }
  3775. return res;
  3776. }
  3777. static int unistim_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  3778. {
  3779. struct unistim_subchannel *p = newchan->tech_pvt;
  3780. struct unistim_line *l = p->parent;
  3781. ast_mutex_lock(&p->lock);
  3782. ast_debug(1, "New owner for channel USTM/%s@%s-%d is %s\n", l->name,
  3783. l->parent->name, p->subtype, newchan->name);
  3784. if (p->owner != oldchan) {
  3785. ast_log(LOG_WARNING, "old channel wasn't %s (%p) but was %s (%p)\n",
  3786. oldchan->name, oldchan, p->owner->name, p->owner);
  3787. return -1;
  3788. }
  3789. p->owner = newchan;
  3790. ast_mutex_unlock(&p->lock);
  3791. return 0;
  3792. }
  3793. static char *control2str(int ind)
  3794. {
  3795. switch (ind) {
  3796. case AST_CONTROL_HANGUP:
  3797. return "Other end has hungup";
  3798. case AST_CONTROL_RING:
  3799. return "Local ring";
  3800. case AST_CONTROL_RINGING:
  3801. return "Remote end is ringing";
  3802. case AST_CONTROL_ANSWER:
  3803. return "Remote end has answered";
  3804. case AST_CONTROL_BUSY:
  3805. return "Remote end is busy";
  3806. case AST_CONTROL_TAKEOFFHOOK:
  3807. return "Make it go off hook";
  3808. case AST_CONTROL_OFFHOOK:
  3809. return "Line is off hook";
  3810. case AST_CONTROL_CONGESTION:
  3811. return "Congestion (circuits busy)";
  3812. case AST_CONTROL_FLASH:
  3813. return "Flash hook";
  3814. case AST_CONTROL_WINK:
  3815. return "Wink";
  3816. case AST_CONTROL_OPTION:
  3817. return "Set a low-level option";
  3818. case AST_CONTROL_RADIO_KEY:
  3819. return "Key Radio";
  3820. case AST_CONTROL_RADIO_UNKEY:
  3821. return "Un-Key Radio";
  3822. case -1:
  3823. return "Stop tone";
  3824. }
  3825. return "UNKNOWN";
  3826. }
  3827. static void in_band_indication(struct ast_channel *ast, const struct ast_tone_zone *tz,
  3828. const char *indication)
  3829. {
  3830. struct ast_tone_zone_sound *ts = NULL;
  3831. if ((ts = ast_get_indication_tone(tz, indication))) {
  3832. ast_playtones_start(ast, 0, ts->data, 1);
  3833. ts = ast_tone_zone_sound_unref(ts);
  3834. } else {
  3835. ast_log(LOG_WARNING, "Unable to get indication tone for %s\n", indication);
  3836. }
  3837. }
  3838. static int unistim_indicate(struct ast_channel *ast, int ind, const void *data,
  3839. size_t datalen)
  3840. {
  3841. struct unistim_subchannel *sub;
  3842. struct unistim_line *l;
  3843. struct unistimsession *s;
  3844. if (unistimdebug) {
  3845. ast_verb(3, "Asked to indicate '%s' condition on channel %s\n",
  3846. control2str(ind), ast->name);
  3847. }
  3848. s = channel_to_session(ast);
  3849. if (!s)
  3850. return -1;
  3851. sub = ast->tech_pvt;
  3852. l = sub->parent;
  3853. switch (ind) {
  3854. case AST_CONTROL_RINGING:
  3855. if (ast->_state != AST_STATE_UP) {
  3856. send_text(TEXT_LINE2, TEXT_NORMAL, s, "Ringing...");
  3857. in_band_indication(ast, l->parent->tz, "ring");
  3858. s->device->missed_call = -1;
  3859. break;
  3860. }
  3861. return -1;
  3862. case AST_CONTROL_BUSY:
  3863. if (ast->_state != AST_STATE_UP) {
  3864. sub->alreadygone = 1;
  3865. send_text(TEXT_LINE2, TEXT_NORMAL, s, "Busy");
  3866. in_band_indication(ast, l->parent->tz, "busy");
  3867. s->device->missed_call = -1;
  3868. break;
  3869. }
  3870. return -1;
  3871. case AST_CONTROL_INCOMPLETE:
  3872. /* Overlapped dialing is not currently supported for UNIStim. Treat an indication
  3873. * of incomplete as congestion
  3874. */
  3875. case AST_CONTROL_CONGESTION:
  3876. if (ast->_state != AST_STATE_UP) {
  3877. sub->alreadygone = 1;
  3878. send_text(TEXT_LINE2, TEXT_NORMAL, s, "Congestion");
  3879. in_band_indication(ast, l->parent->tz, "congestion");
  3880. s->device->missed_call = -1;
  3881. break;
  3882. }
  3883. return -1;
  3884. case AST_CONTROL_HOLD:
  3885. ast_moh_start(ast, data, NULL);
  3886. break;
  3887. case AST_CONTROL_UNHOLD:
  3888. ast_moh_stop(ast);
  3889. break;
  3890. case AST_CONTROL_PROGRESS:
  3891. case AST_CONTROL_SRCUPDATE:
  3892. break;
  3893. case -1:
  3894. ast_playtones_stop(ast);
  3895. s->device->missed_call = 0;
  3896. break;
  3897. case AST_CONTROL_PROCEEDING:
  3898. break;
  3899. default:
  3900. ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", ind);
  3901. return -1;
  3902. }
  3903. return 0;
  3904. }
  3905. static struct unistim_subchannel *find_subchannel_by_name(const char *dest)
  3906. {
  3907. struct unistim_line *l;
  3908. struct unistim_device *d;
  3909. char line[256];
  3910. char *at;
  3911. char *device;
  3912. ast_copy_string(line, dest, sizeof(line));
  3913. at = strchr(line, '@');
  3914. if (!at) {
  3915. ast_log(LOG_NOTICE, "Device '%s' has no @ (at) sign!\n", dest);
  3916. return NULL;
  3917. }
  3918. *at = '\0';
  3919. at++;
  3920. device = at;
  3921. ast_mutex_lock(&devicelock);
  3922. d = devices;
  3923. at = strchr(device, '/'); /* Extra options ? */
  3924. if (at)
  3925. *at = '\0';
  3926. while (d) {
  3927. if (!strcasecmp(d->name, device)) {
  3928. if (unistimdebug)
  3929. ast_verb(0, "Found device: %s\n", d->name);
  3930. /* Found the device */
  3931. l = d->lines;
  3932. while (l) {
  3933. /* Search for the right line */
  3934. if (!strcasecmp(l->name, line)) {
  3935. l->subs[SUB_REAL]->ringvolume = -1;
  3936. l->subs[SUB_REAL]->ringstyle = -1;
  3937. if (at) { /* Other options ? */
  3938. at++; /* Skip slash */
  3939. if (*at == 'r') { /* distinctive ring */
  3940. at++;
  3941. if ((*at < '0') || (*at > '7')) /* ring style */
  3942. ast_log(LOG_WARNING, "Invalid ring selection (%s)", at);
  3943. else {
  3944. char ring_volume = -1;
  3945. char ring_style = *at - '0';
  3946. at++;
  3947. if ((*at >= '0') && (*at <= '3')) /* ring volume */
  3948. ring_volume = *at - '0';
  3949. if (unistimdebug)
  3950. ast_verb(0, "Distinctive ring : style #%d volume %d\n",
  3951. ring_style, ring_volume);
  3952. l->subs[SUB_REAL]->ringvolume = ring_volume;
  3953. l->subs[SUB_REAL]->ringstyle = ring_style;
  3954. }
  3955. }
  3956. }
  3957. ast_mutex_unlock(&devicelock);
  3958. return l->subs[SUB_REAL];
  3959. }
  3960. l = l->next;
  3961. }
  3962. }
  3963. d = d->next;
  3964. }
  3965. /* Device not found */
  3966. ast_mutex_unlock(&devicelock);
  3967. return NULL;
  3968. }
  3969. static int unistim_senddigit_begin(struct ast_channel *ast, char digit)
  3970. {
  3971. struct unistimsession *pte = channel_to_session(ast);
  3972. if (!pte)
  3973. return -1;
  3974. return unistim_do_senddigit(pte, digit);
  3975. }
  3976. static int unistim_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration)
  3977. {
  3978. struct unistimsession *pte = channel_to_session(ast);
  3979. struct ast_frame f = { 0, };
  3980. struct unistim_subchannel *sub;
  3981. sub = pte->device->lines->subs[SUB_REAL];
  3982. if (!sub->owner || sub->alreadygone) {
  3983. ast_log(LOG_WARNING, "Unable to find subchannel in dtmf senddigit_end\n");
  3984. return -1;
  3985. }
  3986. if (unistimdebug)
  3987. ast_verb(0, "Send Digit off %c\n", digit);
  3988. if (!pte)
  3989. return -1;
  3990. send_tone(pte, 0, 0);
  3991. f.frametype = AST_FRAME_DTMF;
  3992. f.subclass.integer = digit;
  3993. f.src = "unistim";
  3994. ast_queue_frame(sub->owner, &f);
  3995. return 0;
  3996. }
  3997. /*--- unistim_sendtext: Display a text on the phone screen ---*/
  3998. /* Called from PBX core text message functions */
  3999. static int unistim_sendtext(struct ast_channel *ast, const char *text)
  4000. {
  4001. struct unistimsession *pte = channel_to_session(ast);
  4002. int size;
  4003. char tmp[TEXT_LENGTH_MAX + 1];
  4004. if (unistimdebug)
  4005. ast_verb(0, "unistim_sendtext called\n");
  4006. if (!text) {
  4007. ast_log(LOG_WARNING, "unistim_sendtext called with a null text\n");
  4008. return 1;
  4009. }
  4010. size = strlen(text);
  4011. if (text[0] == '@') {
  4012. int pos = 0, i = 1, tok = 0, sz = 0;
  4013. char label[11];
  4014. char number[16];
  4015. char icon = '\0';
  4016. char cur = '\0';
  4017. memset(label, 0, 11);
  4018. memset(number, 0, 16);
  4019. while (text[i]) {
  4020. cur = text[i++];
  4021. switch (tok) {
  4022. case 0:
  4023. if ((cur < '0') && (cur > '5')) {
  4024. ast_log(LOG_WARNING,
  4025. "sendtext failed : position must be a number beetween 0 and 5\n");
  4026. return 1;
  4027. }
  4028. pos = cur - '0';
  4029. tok = 1;
  4030. continue;
  4031. case 1:
  4032. if (cur != '@') {
  4033. ast_log(LOG_WARNING, "sendtext failed : invalid position\n");
  4034. return 1;
  4035. }
  4036. tok = 2;
  4037. continue;
  4038. case 2:
  4039. if ((cur < '3') && (cur > '6')) {
  4040. ast_log(LOG_WARNING,
  4041. "sendtext failed : icon must be a number beetween 32 and 63 (first digit invalid)\n");
  4042. return 1;
  4043. }
  4044. icon = (cur - '0') * 10;
  4045. tok = 3;
  4046. continue;
  4047. case 3:
  4048. if ((cur < '0') && (cur > '9')) {
  4049. ast_log(LOG_WARNING,
  4050. "sendtext failed : icon must be a number beetween 32 and 63 (second digit invalid)\n");
  4051. return 1;
  4052. }
  4053. icon += (cur - '0');
  4054. tok = 4;
  4055. continue;
  4056. case 4:
  4057. if (cur != '@') {
  4058. ast_log(LOG_WARNING,
  4059. "sendtext failed : icon must be a number beetween 32 and 63 (too many digits)\n");
  4060. return 1;
  4061. }
  4062. tok = 5;
  4063. continue;
  4064. case 5:
  4065. if (cur == '@') {
  4066. tok = 6;
  4067. sz = 0;
  4068. continue;
  4069. }
  4070. if (sz > 10)
  4071. continue;
  4072. label[sz] = cur;
  4073. sz++;
  4074. continue;
  4075. case 6:
  4076. if (sz > 15) {
  4077. ast_log(LOG_WARNING,
  4078. "sendtext failed : extension too long = %d (15 car max)\n",
  4079. sz);
  4080. return 1;
  4081. }
  4082. number[sz] = cur;
  4083. sz++;
  4084. continue;
  4085. }
  4086. }
  4087. if (tok != 6) {
  4088. ast_log(LOG_WARNING, "sendtext failed : incomplet command\n");
  4089. return 1;
  4090. }
  4091. if (!pte->device) {
  4092. ast_log(LOG_WARNING, "sendtext failed : no device ?\n");
  4093. return 1;
  4094. }
  4095. strcpy(pte->device->softkeylabel[pos], label);
  4096. strcpy(pte->device->softkeynumber[pos], number);
  4097. pte->device->softkeyicon[pos] = icon;
  4098. send_favorite(pos, icon, pte, label);
  4099. return 0;
  4100. }
  4101. if (size <= TEXT_LENGTH_MAX * 2) {
  4102. if (pte->device->height == 1) {
  4103. send_text(TEXT_LINE0, TEXT_NORMAL, pte, text);
  4104. } else {
  4105. send_text(TEXT_LINE0, TEXT_NORMAL, pte, "Message :");
  4106. send_text(TEXT_LINE1, TEXT_NORMAL, pte, text);
  4107. }
  4108. if (size <= TEXT_LENGTH_MAX) {
  4109. send_text(TEXT_LINE2, TEXT_NORMAL, pte, "");
  4110. return 0;
  4111. }
  4112. memcpy(tmp, text + TEXT_LENGTH_MAX, TEXT_LENGTH_MAX);
  4113. tmp[sizeof(tmp) - 1] = '\0';
  4114. send_text(TEXT_LINE2, TEXT_NORMAL, pte, tmp);
  4115. return 0;
  4116. }
  4117. send_text(TEXT_LINE0, TEXT_NORMAL, pte, text);
  4118. memcpy(tmp, text + TEXT_LENGTH_MAX, TEXT_LENGTH_MAX);
  4119. tmp[sizeof(tmp) - 1] = '\0';
  4120. send_text(TEXT_LINE1, TEXT_NORMAL, pte, tmp);
  4121. memcpy(tmp, text + TEXT_LENGTH_MAX * 2, TEXT_LENGTH_MAX);
  4122. tmp[sizeof(tmp) - 1] = '\0';
  4123. send_text(TEXT_LINE2, TEXT_NORMAL, pte, tmp);
  4124. return 0;
  4125. }
  4126. /*--- unistim_send_mwi_to_peer: Send message waiting indication ---*/
  4127. static int unistim_send_mwi_to_peer(struct unistimsession *s, unsigned int tick)
  4128. {
  4129. struct ast_event *event;
  4130. int new;
  4131. char *mailbox, *context;
  4132. struct unistim_line *peer = s->device->lines;
  4133. context = mailbox = ast_strdupa(peer->mailbox);
  4134. strsep(&context, "@");
  4135. if (ast_strlen_zero(context))
  4136. context = "default";
  4137. event = ast_event_get_cached(AST_EVENT_MWI,
  4138. AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
  4139. AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
  4140. AST_EVENT_IE_END);
  4141. if (event) {
  4142. new = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
  4143. ast_event_destroy(event);
  4144. } else { /* Fall back on checking the mailbox directly */
  4145. new = ast_app_has_voicemail(peer->mailbox, "INBOX");
  4146. }
  4147. peer->nextmsgcheck = tick + TIMER_MWI;
  4148. /* Return now if it's the same thing we told them last time */
  4149. if (new == peer->lastmsgssent) {
  4150. return 0;
  4151. }
  4152. peer->lastmsgssent = new;
  4153. if (new == 0) {
  4154. send_led_update(s, 0);
  4155. } else {
  4156. send_led_update(s, 1);
  4157. }
  4158. return 0;
  4159. }
  4160. /*--- unistim_new: Initiate a call in the UNISTIM channel */
  4161. /* called from unistim_request (calls from the pbx ) */
  4162. static struct ast_channel *unistim_new(struct unistim_subchannel *sub, int state, const char *linkedid)
  4163. {
  4164. struct ast_channel *tmp;
  4165. struct unistim_line *l;
  4166. int fmt;
  4167. if (!sub) {
  4168. ast_log(LOG_WARNING, "subchannel null in unistim_new\n");
  4169. return NULL;
  4170. }
  4171. if (!sub->parent) {
  4172. ast_log(LOG_WARNING, "no line for subchannel %p\n", sub);
  4173. return NULL;
  4174. }
  4175. l = sub->parent;
  4176. tmp = ast_channel_alloc(1, state, l->cid_num, NULL, l->accountcode, l->exten,
  4177. l->context, linkedid, l->amaflags, "%s@%s-%d", l->name, l->parent->name, sub->subtype);
  4178. if (unistimdebug)
  4179. ast_verb(0, "unistim_new sub=%d (%p) chan=%p\n", sub->subtype, sub, tmp);
  4180. if (!tmp) {
  4181. ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
  4182. return NULL;
  4183. }
  4184. tmp->nativeformats = l->capability;
  4185. if (!tmp->nativeformats)
  4186. tmp->nativeformats = CAPABILITY;
  4187. fmt = ast_best_codec(tmp->nativeformats);
  4188. if (unistimdebug) {
  4189. char tmp1[256], tmp2[256], tmp3[256];
  4190. ast_verb(0, "Best codec = %s from nativeformats %s (line cap=%s global=%s)\n",
  4191. ast_getformatname(fmt),
  4192. ast_getformatname_multiple(tmp1, sizeof(tmp1), tmp->nativeformats),
  4193. ast_getformatname_multiple(tmp2, sizeof(tmp2), l->capability),
  4194. ast_getformatname_multiple(tmp3, sizeof(tmp3), CAPABILITY));
  4195. }
  4196. if ((sub->rtp) && (sub->subtype == 0)) {
  4197. if (unistimdebug)
  4198. ast_verb(0, "New unistim channel with a previous rtp handle ?\n");
  4199. tmp->fds[0] = ast_rtp_instance_fd(sub->rtp, 0);
  4200. tmp->fds[1] = ast_rtp_instance_fd(sub->rtp, 1);
  4201. }
  4202. if (sub->rtp)
  4203. ast_jb_configure(tmp, &global_jbconf);
  4204. /* tmp->type = type; */
  4205. ast_setstate(tmp, state);
  4206. if (state == AST_STATE_RING)
  4207. tmp->rings = 1;
  4208. tmp->adsicpe = AST_ADSI_UNAVAILABLE;
  4209. tmp->writeformat = fmt;
  4210. tmp->rawwriteformat = fmt;
  4211. tmp->readformat = fmt;
  4212. tmp->rawreadformat = fmt;
  4213. tmp->tech_pvt = sub;
  4214. tmp->tech = &unistim_tech;
  4215. if (!ast_strlen_zero(l->language))
  4216. ast_string_field_set(tmp, language, l->language);
  4217. sub->owner = tmp;
  4218. ast_mutex_lock(&usecnt_lock);
  4219. usecnt++;
  4220. ast_mutex_unlock(&usecnt_lock);
  4221. ast_update_use_count();
  4222. tmp->callgroup = l->callgroup;
  4223. tmp->pickupgroup = l->pickupgroup;
  4224. ast_string_field_set(tmp, call_forward, l->parent->call_forward);
  4225. if (!ast_strlen_zero(l->cid_num)) {
  4226. char *name, *loc, *instr;
  4227. instr = ast_strdup(l->cid_num);
  4228. if (instr) {
  4229. ast_callerid_parse(instr, &name, &loc);
  4230. tmp->caller.id.number.valid = 1;
  4231. ast_free(tmp->caller.id.number.str);
  4232. tmp->caller.id.number.str = ast_strdup(loc);
  4233. tmp->caller.id.name.valid = 1;
  4234. ast_free(tmp->caller.id.name.str);
  4235. tmp->caller.id.name.str = ast_strdup(name);
  4236. ast_free(instr);
  4237. }
  4238. }
  4239. tmp->priority = 1;
  4240. if (state != AST_STATE_DOWN) {
  4241. if (unistimdebug)
  4242. ast_verb(0, "Starting pbx in unistim_new\n");
  4243. if (ast_pbx_start(tmp)) {
  4244. ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
  4245. ast_hangup(tmp);
  4246. tmp = NULL;
  4247. }
  4248. }
  4249. return tmp;
  4250. }
  4251. static void *do_monitor(void *data)
  4252. {
  4253. struct unistimsession *cur = NULL;
  4254. unsigned int dw_timeout = 0;
  4255. unsigned int tick;
  4256. int res;
  4257. int reloading;
  4258. /* Add an I/O event to our UDP socket */
  4259. if (unistimsock > -1)
  4260. ast_io_add(io, unistimsock, unistimsock_read, AST_IO_IN, NULL);
  4261. /* This thread monitors our UDP socket and timers */
  4262. for (;;) {
  4263. /* This loop is executed at least every IDLE_WAITus (1s) or every time a packet is received */
  4264. /* Looking for the smallest time-out value */
  4265. tick = get_tick_count();
  4266. dw_timeout = UINT_MAX;
  4267. ast_mutex_lock(&sessionlock);
  4268. cur = sessions;
  4269. DEBUG_TIMER("checking timeout for session %p with tick = %u\n", cur, tick);
  4270. while (cur) {
  4271. DEBUG_TIMER("checking timeout for session %p timeout = %u\n", cur,
  4272. cur->timeout);
  4273. /* Check if we have miss something */
  4274. if (cur->timeout <= tick) {
  4275. DEBUG_TIMER("Event for session %p\n", cur);
  4276. /* If the queue is empty, send a ping */
  4277. if (cur->last_buf_available == 0)
  4278. send_ping(cur);
  4279. else {
  4280. if (send_retransmit(cur)) {
  4281. DEBUG_TIMER("The chained link was modified, restarting...\n");
  4282. cur = sessions;
  4283. dw_timeout = UINT_MAX;
  4284. continue;
  4285. }
  4286. }
  4287. }
  4288. if (dw_timeout > cur->timeout - tick)
  4289. dw_timeout = cur->timeout - tick;
  4290. /* Checking if the phone is logged on for a new MWI */
  4291. if (cur->device) {
  4292. if ((!ast_strlen_zero(cur->device->lines->mailbox)) &&
  4293. ((tick >= cur->device->lines->nextmsgcheck))) {
  4294. DEBUG_TIMER("Checking mailbox for MWI\n");
  4295. unistim_send_mwi_to_peer(cur, tick);
  4296. break;
  4297. }
  4298. }
  4299. cur = cur->next;
  4300. }
  4301. ast_mutex_unlock(&sessionlock);
  4302. DEBUG_TIMER("Waiting for %dus\n", dw_timeout);
  4303. res = dw_timeout;
  4304. /* We should not wait more than IDLE_WAIT */
  4305. if ((res < 0) || (res > IDLE_WAIT))
  4306. res = IDLE_WAIT;
  4307. /* Wait for UDP messages for a maximum of res us */
  4308. res = ast_io_wait(io, res); /* This function will call unistimsock_read if a packet is received */
  4309. /* Check for a reload request */
  4310. ast_mutex_lock(&unistim_reload_lock);
  4311. reloading = unistim_reloading;
  4312. unistim_reloading = 0;
  4313. ast_mutex_unlock(&unistim_reload_lock);
  4314. if (reloading) {
  4315. ast_verb(1, "Reloading unistim.conf...\n");
  4316. reload_config();
  4317. }
  4318. pthread_testcancel();
  4319. }
  4320. /* Never reached */
  4321. return NULL;
  4322. }
  4323. /*--- restart_monitor: Start the channel monitor thread ---*/
  4324. static int restart_monitor(void)
  4325. {
  4326. pthread_attr_t attr;
  4327. /* If we're supposed to be stopped -- stay stopped */
  4328. if (monitor_thread == AST_PTHREADT_STOP)
  4329. return 0;
  4330. if (ast_mutex_lock(&monlock)) {
  4331. ast_log(LOG_WARNING, "Unable to lock monitor\n");
  4332. return -1;
  4333. }
  4334. if (monitor_thread == pthread_self()) {
  4335. ast_mutex_unlock(&monlock);
  4336. ast_log(LOG_WARNING, "Cannot kill myself\n");
  4337. return -1;
  4338. }
  4339. if (monitor_thread != AST_PTHREADT_NULL) {
  4340. /* Wake up the thread */
  4341. pthread_kill(monitor_thread, SIGURG);
  4342. } else {
  4343. pthread_attr_init(&attr);
  4344. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  4345. /* Start a new monitor */
  4346. if (ast_pthread_create(&monitor_thread, &attr, do_monitor, NULL) < 0) {
  4347. ast_mutex_unlock(&monlock);
  4348. ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
  4349. return -1;
  4350. }
  4351. }
  4352. ast_mutex_unlock(&monlock);
  4353. return 0;
  4354. }
  4355. /*--- unistim_request: PBX interface function ---*/
  4356. /* UNISTIM calls initiated by the PBX arrive here */
  4357. static struct ast_channel *unistim_request(const char *type, format_t format, const struct ast_channel *requestor, void *data,
  4358. int *cause)
  4359. {
  4360. format_t oldformat;
  4361. struct unistim_subchannel *sub;
  4362. struct ast_channel *tmpc = NULL;
  4363. char tmp[256];
  4364. char *dest = data;
  4365. oldformat = format;
  4366. format &= CAPABILITY;
  4367. ast_log(LOG_NOTICE,
  4368. "Asked to get a channel of format %s while capability is %s result : %s\n",
  4369. ast_getformatname(oldformat),
  4370. ast_getformatname_multiple(tmp, sizeof(tmp), CAPABILITY),
  4371. ast_getformatname(format));
  4372. if (!format) {
  4373. ast_log(LOG_NOTICE,
  4374. "Asked to get a channel of unsupported format %s while capability is %s\n",
  4375. ast_getformatname(oldformat), ast_getformatname_multiple(tmp, sizeof(tmp), CAPABILITY));
  4376. return NULL;
  4377. }
  4378. ast_copy_string(tmp, dest, sizeof(tmp));
  4379. if (ast_strlen_zero(tmp)) {
  4380. ast_log(LOG_NOTICE, "Unistim channels require a device\n");
  4381. return NULL;
  4382. }
  4383. sub = find_subchannel_by_name(tmp);
  4384. if (!sub) {
  4385. ast_log(LOG_NOTICE, "No available lines on: %s\n", dest);
  4386. *cause = AST_CAUSE_CONGESTION;
  4387. return NULL;
  4388. }
  4389. ast_verb(3, "unistim_request(%s)\n", tmp);
  4390. /* Busy ? */
  4391. if (sub->owner) {
  4392. if (unistimdebug)
  4393. ast_verb(0, "Can't create channel : Busy !\n");
  4394. *cause = AST_CAUSE_BUSY;
  4395. return NULL;
  4396. }
  4397. sub->parent->capability = format;
  4398. tmpc = unistim_new(sub, AST_STATE_DOWN, requestor ? requestor->linkedid : NULL);
  4399. if (!tmpc)
  4400. ast_log(LOG_WARNING, "Unable to make channel for '%s'\n", tmp);
  4401. if (unistimdebug)
  4402. ast_verb(0, "unistim_request owner = %p\n", sub->owner);
  4403. restart_monitor();
  4404. /* and finish */
  4405. return tmpc;
  4406. }
  4407. static char *unistim_info(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4408. {
  4409. struct unistim_device *device = devices;
  4410. struct unistim_line *line;
  4411. struct unistim_subchannel *sub;
  4412. struct unistimsession *s;
  4413. int i;
  4414. struct ast_channel *tmp;
  4415. switch (cmd) {
  4416. case CLI_INIT:
  4417. e->command = "unistim show info";
  4418. e->usage =
  4419. "Usage: unistim show info\n"
  4420. " Dump internal structures.\n";
  4421. return NULL;
  4422. case CLI_GENERATE:
  4423. return NULL; /* no completion */
  4424. }
  4425. if (a->argc != e->args)
  4426. return CLI_SHOWUSAGE;
  4427. ast_cli(a->fd, "Dumping internal structures :\ndevice\n->line\n-->sub\n");
  4428. while (device) {
  4429. ast_cli(a->fd, "\nname=%s id=%s line=%p ha=%p sess=%p device=%p\n",
  4430. device->name, device->id, device->lines, device->ha, device->session,
  4431. device);
  4432. line = device->lines;
  4433. while (line) {
  4434. ast_cli(a->fd,
  4435. "->name=%s fullname=%s exten=%s callid=%s cap=%" PRId64 " device=%p line=%p\n",
  4436. line->name, line->fullname, line->exten, line->cid_num,
  4437. line->capability, line->parent, line);
  4438. for (i = 0; i < MAX_SUBS; i++) {
  4439. sub = line->subs[i];
  4440. if (!sub)
  4441. continue;
  4442. if (!sub->owner)
  4443. tmp = (void *) -42;
  4444. else
  4445. tmp = sub->owner->_bridge;
  4446. if (sub->subtype != i)
  4447. ast_cli(a->fd, "Warning ! subchannel->subs[%d] have a subtype=%d\n", i,
  4448. sub->subtype);
  4449. ast_cli(a->fd,
  4450. "-->subtype=%d chan=%p rtp=%p bridge=%p line=%p alreadygone=%d\n",
  4451. sub->subtype, sub->owner, sub->rtp, tmp, sub->parent,
  4452. sub->alreadygone);
  4453. }
  4454. line = line->next;
  4455. }
  4456. device = device->next;
  4457. }
  4458. ast_cli(a->fd, "\nSessions:\n");
  4459. ast_mutex_lock(&sessionlock);
  4460. s = sessions;
  4461. while (s) {
  4462. ast_cli(a->fd,
  4463. "sin=%s timeout=%u state=%d macaddr=%s device=%p session=%p\n",
  4464. ast_inet_ntoa(s->sin.sin_addr), s->timeout, s->state, s->macaddr,
  4465. s->device, s);
  4466. s = s->next;
  4467. }
  4468. ast_mutex_unlock(&sessionlock);
  4469. return CLI_SUCCESS;
  4470. }
  4471. static char *unistim_sp(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4472. {
  4473. BUFFSEND;
  4474. struct unistim_subchannel *sub;
  4475. int i, j = 0, len;
  4476. unsigned char c, cc;
  4477. char tmp[256];
  4478. switch (cmd) {
  4479. case CLI_INIT:
  4480. e->command = "unistim send packet";
  4481. e->usage =
  4482. "Usage: unistim send packet USTM/line@name hexa\n"
  4483. " unistim send packet USTM/1000@hans 19040004\n";
  4484. return NULL;
  4485. case CLI_GENERATE:
  4486. return NULL; /* no completion */
  4487. }
  4488. if (a->argc < 5)
  4489. return CLI_SHOWUSAGE;
  4490. if (strlen(a->argv[3]) < 9)
  4491. return CLI_SHOWUSAGE;
  4492. len = strlen(a->argv[4]);
  4493. if (len % 2)
  4494. return CLI_SHOWUSAGE;
  4495. ast_copy_string(tmp, a->argv[3] + 5, sizeof(tmp));
  4496. sub = find_subchannel_by_name(tmp);
  4497. if (!sub) {
  4498. ast_cli(a->fd, "Can't find '%s'\n", tmp);
  4499. return CLI_SUCCESS;
  4500. }
  4501. if (!sub->parent->parent->session) {
  4502. ast_cli(a->fd, "'%s' is not connected\n", tmp);
  4503. return CLI_SUCCESS;
  4504. }
  4505. ast_cli(a->fd, "Sending '%s' to %s (%p)\n", a->argv[4], tmp, sub->parent->parent->session);
  4506. for (i = 0; i < len; i++) {
  4507. c = a->argv[4][i];
  4508. if (c >= 'a')
  4509. c -= 'a' - 10;
  4510. else
  4511. c -= '0';
  4512. i++;
  4513. cc = a->argv[4][i];
  4514. if (cc >= 'a')
  4515. cc -= 'a' - 10;
  4516. else
  4517. cc -= '0';
  4518. tmp[j++] = (c << 4) | cc;
  4519. }
  4520. memcpy(buffsend + SIZE_HEADER, tmp, j);
  4521. send_client(SIZE_HEADER + j, buffsend, sub->parent->parent->session);
  4522. return CLI_SUCCESS;
  4523. }
  4524. static char *unistim_do_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4525. {
  4526. switch (cmd) {
  4527. case CLI_INIT:
  4528. e->command = "unistim set debug {on|off}";
  4529. e->usage =
  4530. "Usage: unistim set debug\n"
  4531. " Display debug messages.\n";
  4532. return NULL;
  4533. case CLI_GENERATE:
  4534. return NULL; /* no completion */
  4535. }
  4536. if (a->argc != e->args)
  4537. return CLI_SHOWUSAGE;
  4538. if (!strcasecmp(a->argv[3], "on")) {
  4539. unistimdebug = 1;
  4540. ast_cli(a->fd, "UNISTIM Debugging Enabled\n");
  4541. } else if (!strcasecmp(a->argv[3], "off")) {
  4542. unistimdebug = 0;
  4543. ast_cli(a->fd, "UNISTIM Debugging Disabled\n");
  4544. } else
  4545. return CLI_SHOWUSAGE;
  4546. return CLI_SUCCESS;
  4547. }
  4548. /*! \brief --- unistim_reload: Force reload of module from cli ---
  4549. * Runs in the asterisk main thread, so don't do anything useful
  4550. * but setting a flag and waiting for do_monitor to do the job
  4551. * in our thread */
  4552. static char *unistim_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4553. {
  4554. switch (cmd) {
  4555. case CLI_INIT:
  4556. e->command = "unistim reload";
  4557. e->usage =
  4558. "Usage: unistim reload\n"
  4559. " Reloads UNISTIM configuration from unistim.conf\n";
  4560. return NULL;
  4561. case CLI_GENERATE:
  4562. return NULL; /* no completion */
  4563. }
  4564. if (e && a && a->argc != e->args)
  4565. return CLI_SHOWUSAGE;
  4566. if (unistimdebug)
  4567. ast_verb(0, "reload unistim\n");
  4568. ast_mutex_lock(&unistim_reload_lock);
  4569. if (!unistim_reloading)
  4570. unistim_reloading = 1;
  4571. ast_mutex_unlock(&unistim_reload_lock);
  4572. restart_monitor();
  4573. return CLI_SUCCESS;
  4574. }
  4575. static struct ast_cli_entry unistim_cli[] = {
  4576. AST_CLI_DEFINE(unistim_reload, "Reload UNISTIM configuration"),
  4577. AST_CLI_DEFINE(unistim_info, "Show UNISTIM info"),
  4578. AST_CLI_DEFINE(unistim_sp, "Send packet (for reverse engineering)"),
  4579. AST_CLI_DEFINE(unistim_do_debug, "Toggle UNITSTIM debugging"),
  4580. };
  4581. static void unquote(char *out, const char *src, int maxlen)
  4582. {
  4583. int len = strlen(src);
  4584. if (!len)
  4585. return;
  4586. if ((len > 1) && src[0] == '\"') {
  4587. /* This is a quoted string */
  4588. src++;
  4589. /* Don't take more than what's there */
  4590. len--;
  4591. if (maxlen > len - 1)
  4592. maxlen = len - 1;
  4593. memcpy(out, src, maxlen);
  4594. ((char *) out)[maxlen] = '\0';
  4595. } else
  4596. memcpy(out, src, maxlen);
  4597. return;
  4598. }
  4599. static int ParseBookmark(const char *text, struct unistim_device *d)
  4600. {
  4601. char line[256];
  4602. char *at;
  4603. char *number;
  4604. char *icon;
  4605. int p;
  4606. int len = strlen(text);
  4607. ast_copy_string(line, text, sizeof(line));
  4608. /* Position specified ? */
  4609. if ((len > 2) && (line[1] == '@')) {
  4610. p = line[0];
  4611. if ((p >= '0') && (p <= '5'))
  4612. p -= '0';
  4613. else {
  4614. ast_log(LOG_WARNING,
  4615. "Invalid position for bookmark : must be between 0 and 5\n");
  4616. return 0;
  4617. }
  4618. if (d->softkeyicon[p] != 0) {
  4619. ast_log(LOG_WARNING, "Invalid position %d for bookmark : already used\n:", p);
  4620. return 0;
  4621. }
  4622. memmove(line, line + 2, sizeof(line));
  4623. } else {
  4624. /* No position specified, looking for a free slot */
  4625. for (p = 0; p <= 5; p++) {
  4626. if (!d->softkeyicon[p])
  4627. break;
  4628. }
  4629. if (p > 5) {
  4630. ast_log(LOG_WARNING, "No more free bookmark position\n");
  4631. return 0;
  4632. }
  4633. }
  4634. at = strchr(line, '@');
  4635. if (!at) {
  4636. ast_log(LOG_NOTICE, "Bookmark entry '%s' has no @ (at) sign!\n", text);
  4637. return 0;
  4638. }
  4639. *at = '\0';
  4640. at++;
  4641. number = at;
  4642. at = strchr(at, '@');
  4643. if (ast_strlen_zero(number)) {
  4644. ast_log(LOG_NOTICE, "Bookmark entry '%s' has no number\n", text);
  4645. return 0;
  4646. }
  4647. if (ast_strlen_zero(line)) {
  4648. ast_log(LOG_NOTICE, "Bookmark entry '%s' has no description\n", text);
  4649. return 0;
  4650. }
  4651. at = strchr(number, '@');
  4652. if (!at)
  4653. d->softkeyicon[p] = FAV_ICON_SHARP; /* default icon */
  4654. else {
  4655. *at = '\0';
  4656. at++;
  4657. icon = at;
  4658. if (ast_strlen_zero(icon)) {
  4659. ast_log(LOG_NOTICE, "Bookmark entry '%s' has no icon value\n", text);
  4660. return 0;
  4661. }
  4662. if (strncmp(icon, "USTM/", 5))
  4663. d->softkeyicon[p] = atoi(icon);
  4664. else {
  4665. d->softkeyicon[p] = 1;
  4666. ast_copy_string(d->softkeydevice[p], icon + 5, sizeof(d->softkeydevice[p]));
  4667. }
  4668. }
  4669. ast_copy_string(d->softkeylabel[p], line, sizeof(d->softkeylabel[p]));
  4670. ast_copy_string(d->softkeynumber[p], number, sizeof(d->softkeynumber[p]));
  4671. if (unistimdebug)
  4672. ast_verb(0, "New bookmark at pos %d label='%s' number='%s' icon=%x\n",
  4673. p, d->softkeylabel[p], d->softkeynumber[p], d->softkeyicon[p]);
  4674. return 1;
  4675. }
  4676. /* Looking for dynamic icons entries in bookmarks */
  4677. static void finish_bookmark(void)
  4678. {
  4679. struct unistim_device *d = devices;
  4680. int i;
  4681. while (d) {
  4682. for (i = 0; i < 6; i++) {
  4683. if (d->softkeyicon[i] == 1) { /* Something for us */
  4684. struct unistim_device *d2 = devices;
  4685. while (d2) {
  4686. if (!strcmp(d->softkeydevice[i], d2->name)) {
  4687. d->sp[i] = d2;
  4688. d->softkeyicon[i] = 0;
  4689. break;
  4690. }
  4691. d2 = d2->next;
  4692. }
  4693. if (d->sp[i] == NULL)
  4694. ast_log(LOG_NOTICE, "Bookmark entry with device %s not found\n",
  4695. d->softkeydevice[i]);
  4696. }
  4697. }
  4698. d = d->next;
  4699. }
  4700. }
  4701. static struct unistim_device *build_device(const char *cat, const struct ast_variable *v)
  4702. {
  4703. struct unistim_device *d;
  4704. struct unistim_line *l = NULL;
  4705. int create = 1;
  4706. int nbsoftkey, dateformat, timeformat, callhistory;
  4707. char linelabel[AST_MAX_EXTENSION];
  4708. char context[AST_MAX_EXTENSION];
  4709. char ringvolume, ringstyle;
  4710. /* First, we need to know if we already have this name in our list */
  4711. /* Get a lock for the device chained list */
  4712. ast_mutex_lock(&devicelock);
  4713. d = devices;
  4714. while (d) {
  4715. if (!strcmp(d->name, cat)) {
  4716. /* Yep, we alreay have this one */
  4717. if (unistimsock < 0) {
  4718. /* It's a dupe */
  4719. ast_log(LOG_WARNING, "Duplicate entry found (%s), ignoring.\n", cat);
  4720. ast_mutex_unlock(&devicelock);
  4721. return NULL;
  4722. }
  4723. /* we're reloading right now */
  4724. create = 0;
  4725. l = d->lines;
  4726. break;
  4727. }
  4728. d = d->next;
  4729. }
  4730. ast_mutex_unlock(&devicelock);
  4731. if (create) {
  4732. if (!(d = ast_calloc(1, sizeof(*d))))
  4733. return NULL;
  4734. if (!(l = ast_calloc(1, sizeof(*l)))) {
  4735. ast_free(d);
  4736. return NULL;
  4737. }
  4738. ast_copy_string(d->name, cat, sizeof(d->name));
  4739. }
  4740. ast_copy_string(context, DEFAULTCONTEXT, sizeof(context));
  4741. d->contrast = -1;
  4742. d->output = OUTPUT_HANDSET;
  4743. d->previous_output = OUTPUT_HANDSET;
  4744. d->volume = VOLUME_LOW;
  4745. d->mute = MUTE_OFF;
  4746. d->height = DEFAULTHEIGHT;
  4747. linelabel[0] = '\0';
  4748. dateformat = 1;
  4749. timeformat = 1;
  4750. ringvolume = 2;
  4751. callhistory = 1;
  4752. ringstyle = 3;
  4753. nbsoftkey = 0;
  4754. while (v) {
  4755. if (!strcasecmp(v->name, "rtp_port"))
  4756. d->rtp_port = atoi(v->value);
  4757. else if (!strcasecmp(v->name, "rtp_method"))
  4758. d->rtp_method = atoi(v->value);
  4759. else if (!strcasecmp(v->name, "status_method"))
  4760. d->status_method = atoi(v->value);
  4761. else if (!strcasecmp(v->name, "device"))
  4762. ast_copy_string(d->id, v->value, sizeof(d->id));
  4763. else if (!strcasecmp(v->name, "tn"))
  4764. ast_copy_string(d->extension_number, v->value, sizeof(d->extension_number));
  4765. else if (!strcasecmp(v->name, "permit") || !strcasecmp(v->name, "deny"))
  4766. d->ha = ast_append_ha(v->name, v->value, d->ha, NULL);
  4767. else if (!strcasecmp(v->name, "context"))
  4768. ast_copy_string(context, v->value, sizeof(context));
  4769. else if (!strcasecmp(v->name, "maintext0"))
  4770. unquote(d->maintext0, v->value, sizeof(d->maintext0) - 1);
  4771. else if (!strcasecmp(v->name, "maintext1"))
  4772. unquote(d->maintext1, v->value, sizeof(d->maintext1) - 1);
  4773. else if (!strcasecmp(v->name, "maintext2"))
  4774. unquote(d->maintext2, v->value, sizeof(d->maintext2) - 1);
  4775. else if (!strcasecmp(v->name, "titledefault"))
  4776. unquote(d->titledefault, v->value, sizeof(d->titledefault) - 1);
  4777. else if (!strcasecmp(v->name, "dateformat"))
  4778. dateformat = atoi(v->value);
  4779. else if (!strcasecmp(v->name, "timeformat"))
  4780. timeformat = atoi(v->value);
  4781. else if (!strcasecmp(v->name, "contrast")) {
  4782. d->contrast = atoi(v->value);
  4783. if ((d->contrast < 0) || (d->contrast > 15)) {
  4784. ast_log(LOG_WARNING, "constrast must be beetween 0 and 15");
  4785. d->contrast = 8;
  4786. }
  4787. } else if (!strcasecmp(v->name, "nat"))
  4788. d->nat = ast_true(v->value);
  4789. else if (!strcasecmp(v->name, "ringvolume"))
  4790. ringvolume = atoi(v->value);
  4791. else if (!strcasecmp(v->name, "ringstyle"))
  4792. ringstyle = atoi(v->value);
  4793. else if (!strcasecmp(v->name, "callhistory"))
  4794. callhistory = atoi(v->value);
  4795. else if (!strcasecmp(v->name, "callerid")) {
  4796. if (!strcasecmp(v->value, "asreceived"))
  4797. l->cid_num[0] = '\0';
  4798. else
  4799. ast_copy_string(l->cid_num, v->value, sizeof(l->cid_num));
  4800. } else if (!strcasecmp(v->name, "language"))
  4801. ast_copy_string(l->language, v->value, sizeof(l->language));
  4802. else if (!strcasecmp(v->name, "country"))
  4803. ast_copy_string(d->country, v->value, sizeof(d->country));
  4804. else if (!strcasecmp(v->name, "accountcode"))
  4805. ast_copy_string(l->accountcode, v->value, sizeof(l->accountcode));
  4806. else if (!strcasecmp(v->name, "amaflags")) {
  4807. int y;
  4808. y = ast_cdr_amaflags2int(v->value);
  4809. if (y < 0)
  4810. ast_log(LOG_WARNING, "Invalid AMA flags: %s at line %d\n", v->value,
  4811. v->lineno);
  4812. else
  4813. l->amaflags = y;
  4814. } else if (!strcasecmp(v->name, "musiconhold"))
  4815. ast_copy_string(l->musicclass, v->value, sizeof(l->musicclass));
  4816. else if (!strcasecmp(v->name, "callgroup"))
  4817. l->callgroup = ast_get_group(v->value);
  4818. else if (!strcasecmp(v->name, "pickupgroup"))
  4819. l->pickupgroup = ast_get_group(v->value);
  4820. else if (!strcasecmp(v->name, "mailbox"))
  4821. ast_copy_string(l->mailbox, v->value, sizeof(l->mailbox));
  4822. else if (!strcasecmp(v->name, "parkinglot"))
  4823. ast_copy_string(l->parkinglot, v->value, sizeof(l->parkinglot));
  4824. else if (!strcasecmp(v->name, "linelabel"))
  4825. unquote(linelabel, v->value, sizeof(linelabel) - 1);
  4826. else if (!strcasecmp(v->name, "extension")) {
  4827. if (!strcasecmp(v->value, "none"))
  4828. d->extension = EXTENSION_NONE;
  4829. else if (!strcasecmp(v->value, "ask"))
  4830. d->extension = EXTENSION_ASK;
  4831. else if (!strcasecmp(v->value, "line"))
  4832. d->extension = EXTENSION_LINE;
  4833. else
  4834. ast_log(LOG_WARNING, "Unknown extension option.\n");
  4835. } else if (!strcasecmp(v->name, "bookmark")) {
  4836. if (nbsoftkey > 5)
  4837. ast_log(LOG_WARNING,
  4838. "More than 6 softkeys defined. Ignoring new entries.\n");
  4839. else {
  4840. if (ParseBookmark(v->value, d))
  4841. nbsoftkey++;
  4842. }
  4843. } else if (!strcasecmp(v->name, "line")) {
  4844. int len = strlen(linelabel);
  4845. if (nbsoftkey) {
  4846. ast_log(LOG_WARNING,
  4847. "You must use bookmark AFTER line=>. Only one line is supported in this version\n");
  4848. if (create) {
  4849. ast_free(d);
  4850. ast_free(l);
  4851. }
  4852. return NULL;
  4853. }
  4854. if (create) {
  4855. ast_mutex_init(&l->lock);
  4856. } else {
  4857. d->to_delete = 0;
  4858. /* reset bookmarks */
  4859. memset(d->softkeylabel, 0, sizeof(d->softkeylabel));
  4860. memset(d->softkeynumber, 0, sizeof(d->softkeynumber));
  4861. memset(d->softkeyicon, 0, sizeof(d->softkeyicon));
  4862. memset(d->softkeydevice, 0, sizeof(d->softkeydevice));
  4863. memset(d->sp, 0, sizeof(d->sp));
  4864. }
  4865. ast_copy_string(l->name, v->value, sizeof(l->name));
  4866. snprintf(l->fullname, sizeof(l->fullname), "USTM/%s@%s", l->name, d->name);
  4867. d->softkeyicon[0] = FAV_ICON_ONHOOK_BLACK;
  4868. if (!len) /* label is undefined ? */
  4869. ast_copy_string(d->softkeylabel[0], v->value, sizeof(d->softkeylabel[0]));
  4870. else {
  4871. if ((len > 2) && (linelabel[1] == '@')) {
  4872. d->softkeylinepos = linelabel[0];
  4873. if ((d->softkeylinepos >= '0') && (d->softkeylinepos <= '5')) {
  4874. d->softkeylinepos -= '0';
  4875. d->softkeyicon[0] = 0;
  4876. } else {
  4877. ast_log(LOG_WARNING,
  4878. "Invalid position for linelabel : must be between 0 and 5\n");
  4879. d->softkeylinepos = 0;
  4880. }
  4881. ast_copy_string(d->softkeylabel[d->softkeylinepos], linelabel + 2,
  4882. sizeof(d->softkeylabel[d->softkeylinepos]));
  4883. d->softkeyicon[d->softkeylinepos] = FAV_ICON_ONHOOK_BLACK;
  4884. } else
  4885. ast_copy_string(d->softkeylabel[0], linelabel,
  4886. sizeof(d->softkeylabel[0]));
  4887. }
  4888. nbsoftkey++;
  4889. ast_copy_string(l->context, context, sizeof(l->context));
  4890. if (!ast_strlen_zero(l->mailbox)) {
  4891. if (unistimdebug)
  4892. ast_verb(3, "Setting mailbox '%s' on %s@%s\n", l->mailbox, d->name, l->name);
  4893. }
  4894. l->capability = CAPABILITY;
  4895. l->parent = d;
  4896. if (create) {
  4897. if (!alloc_sub(l, SUB_REAL)) {
  4898. ast_mutex_destroy(&l->lock);
  4899. ast_free(l);
  4900. ast_free(d);
  4901. return NULL;
  4902. }
  4903. l->next = d->lines;
  4904. d->lines = l;
  4905. }
  4906. } else if (!strcasecmp(v->name, "height")) {
  4907. /* Allow the user to lower the expected display lines on the phone
  4908. * For example the Nortal I2001 and I2002 only have one ! */
  4909. d->height = atoi(v->value);
  4910. } else
  4911. ast_log(LOG_WARNING, "Don't know keyword '%s' at line %d\n", v->name,
  4912. v->lineno);
  4913. v = v->next;
  4914. }
  4915. d->ringvolume = ringvolume;
  4916. d->ringstyle = ringstyle;
  4917. d->callhistory = callhistory;
  4918. d->tz = ast_get_indication_zone(d->country);
  4919. if ((d->tz == NULL) && !ast_strlen_zero(d->country))
  4920. ast_log(LOG_WARNING, "Country '%s' was not found in indications.conf\n",
  4921. d->country);
  4922. d->datetimeformat = 56 + (dateformat * 4);
  4923. d->datetimeformat += timeformat;
  4924. if (!d->lines) {
  4925. ast_log(LOG_ERROR, "An Unistim device must have at least one line!\n");
  4926. ast_mutex_destroy(&l->lock);
  4927. ast_free(l);
  4928. if (d->tz) {
  4929. d->tz = ast_tone_zone_unref(d->tz);
  4930. }
  4931. ast_free(d);
  4932. return NULL;
  4933. }
  4934. if ((autoprovisioning == AUTOPROVISIONING_TN) &&
  4935. (!ast_strlen_zero(d->extension_number))) {
  4936. d->extension = EXTENSION_TN;
  4937. if (!ast_strlen_zero(d->id))
  4938. ast_log(LOG_WARNING,
  4939. "tn= and device= can't be used together. Ignoring device= entry\n");
  4940. d->id[0] = 'T'; /* magic : this is a tn entry */
  4941. ast_copy_string((d->id) + 1, d->extension_number, sizeof(d->id) - 1);
  4942. d->extension_number[0] = '\0';
  4943. } else if (ast_strlen_zero(d->id)) {
  4944. if (strcmp(d->name, "template")) {
  4945. ast_log(LOG_ERROR, "You must specify the mac address with device=\n");
  4946. ast_mutex_destroy(&l->lock);
  4947. ast_free(l);
  4948. if (d->tz) {
  4949. d->tz = ast_tone_zone_unref(d->tz);
  4950. }
  4951. ast_free(d);
  4952. return NULL;
  4953. } else
  4954. strcpy(d->id, "000000000000");
  4955. }
  4956. if (!d->rtp_port)
  4957. d->rtp_port = 10000;
  4958. if (d->contrast == -1)
  4959. d->contrast = 8;
  4960. if (ast_strlen_zero(d->maintext0))
  4961. strcpy(d->maintext0, "Welcome");
  4962. if (ast_strlen_zero(d->maintext1))
  4963. strcpy(d->maintext1, d->name);
  4964. if (ast_strlen_zero(d->titledefault)) {
  4965. struct ast_tm tm = { 0, };
  4966. struct timeval cur_time = ast_tvnow();
  4967. if ((ast_localtime(&cur_time, &tm, 0)) == 0 || ast_strlen_zero(tm.tm_zone)) {
  4968. display_last_error("Error in ast_localtime()");
  4969. ast_copy_string(d->titledefault, "UNISTIM for*", 12);
  4970. } else {
  4971. if (strlen(tm.tm_zone) < 4) {
  4972. strcpy(d->titledefault, "TimeZone ");
  4973. strcat(d->titledefault, tm.tm_zone);
  4974. } else if (strlen(tm.tm_zone) < 9) {
  4975. strcpy(d->titledefault, "TZ ");
  4976. strcat(d->titledefault, tm.tm_zone);
  4977. } else
  4978. ast_copy_string(d->titledefault, tm.tm_zone, 12);
  4979. }
  4980. }
  4981. /* Update the chained link if it's a new device */
  4982. if (create) {
  4983. ast_mutex_lock(&devicelock);
  4984. d->next = devices;
  4985. devices = d;
  4986. ast_mutex_unlock(&devicelock);
  4987. ast_verb(3, "Added device '%s'\n", d->name);
  4988. } else {
  4989. ast_verb(3, "Device '%s' reloaded\n", d->name);
  4990. }
  4991. return d;
  4992. }
  4993. /*--- reload_config: Re-read unistim.conf config file ---*/
  4994. static int reload_config(void)
  4995. {
  4996. struct ast_config *cfg;
  4997. struct ast_variable *v;
  4998. struct ast_hostent ahp;
  4999. struct hostent *hp;
  5000. struct sockaddr_in bindaddr = { 0, };
  5001. char *config = "unistim.conf";
  5002. char *cat;
  5003. struct unistim_device *d;
  5004. const int reuseFlag = 1;
  5005. struct unistimsession *s;
  5006. struct ast_flags config_flags = { 0, };
  5007. cfg = ast_config_load(config, config_flags);
  5008. /* We *must* have a config file otherwise stop immediately */
  5009. if (!cfg) {
  5010. ast_log(LOG_ERROR, "Unable to load config %s\n", config);
  5011. return -1;
  5012. } else if (cfg == CONFIG_STATUS_FILEINVALID) {
  5013. ast_log(LOG_ERROR, "Config file %s is in an invalid format. Aborting.\n", config);
  5014. return -1;
  5015. }
  5016. /* Copy the default jb config over global_jbconf */
  5017. memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
  5018. unistim_keepalive = 120;
  5019. unistim_port = 0;
  5020. v = ast_variable_browse(cfg, "general");
  5021. while (v) {
  5022. /* handle jb conf */
  5023. if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
  5024. continue;
  5025. if (!strcasecmp(v->name, "keepalive"))
  5026. unistim_keepalive = atoi(v->value);
  5027. else if (!strcasecmp(v->name, "port"))
  5028. unistim_port = atoi(v->value);
  5029. else if (!strcasecmp(v->name, "tos")) {
  5030. if (ast_str2tos(v->value, &qos.tos))
  5031. ast_log(LOG_WARNING, "Invalid tos value at line %d, refer to QoS documentation\n", v->lineno);
  5032. } else if (!strcasecmp(v->name, "tos_audio")) {
  5033. if (ast_str2tos(v->value, &qos.tos_audio))
  5034. ast_log(LOG_WARNING, "Invalid tos_audio value at line %d, refer to QoS documentation\n", v->lineno);
  5035. } else if (!strcasecmp(v->name, "cos")) {
  5036. if (ast_str2cos(v->value, &qos.cos))
  5037. ast_log(LOG_WARNING, "Invalid cos value at line %d, refer to QoS documentation\n", v->lineno);
  5038. } else if (!strcasecmp(v->name, "cos_audio")) {
  5039. if (ast_str2cos(v->value, &qos.cos_audio))
  5040. ast_log(LOG_WARNING, "Invalid cos_audio value at line %d, refer to QoS documentation\n", v->lineno);
  5041. } else if (!strcasecmp(v->name, "autoprovisioning")) {
  5042. if (!strcasecmp(v->value, "no"))
  5043. autoprovisioning = AUTOPROVISIONING_NO;
  5044. else if (!strcasecmp(v->value, "yes"))
  5045. autoprovisioning = AUTOPROVISIONING_YES;
  5046. else if (!strcasecmp(v->value, "db"))
  5047. autoprovisioning = AUTOPROVISIONING_DB;
  5048. else if (!strcasecmp(v->value, "tn"))
  5049. autoprovisioning = AUTOPROVISIONING_TN;
  5050. else
  5051. ast_log(LOG_WARNING, "Unknown autoprovisioning option.\n");
  5052. } else if (!strcasecmp(v->name, "public_ip")) {
  5053. if (!ast_strlen_zero(v->value)) {
  5054. if (!(hp = ast_gethostbyname(v->value, &ahp)))
  5055. ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
  5056. else {
  5057. memcpy(&public_ip.sin_addr, hp->h_addr, sizeof(public_ip.sin_addr));
  5058. public_ip.sin_family = AF_INET;
  5059. }
  5060. }
  5061. }
  5062. v = v->next;
  5063. }
  5064. if ((unistim_keepalive < 10) ||
  5065. (unistim_keepalive >
  5066. 255 - (((NB_MAX_RETRANSMIT + 1) * RETRANSMIT_TIMER) / 1000))) {
  5067. ast_log(LOG_ERROR, "keepalive is invalid in %s\n", config);
  5068. ast_config_destroy(cfg);
  5069. return -1;
  5070. }
  5071. packet_send_ping[4] =
  5072. unistim_keepalive + (((NB_MAX_RETRANSMIT + 1) * RETRANSMIT_TIMER) / 1000);
  5073. if ((unistim_port < 1) || (unistim_port > 65535)) {
  5074. ast_log(LOG_ERROR, "port is not set or invalid in %s\n", config);
  5075. ast_config_destroy(cfg);
  5076. return -1;
  5077. }
  5078. unistim_keepalive *= 1000;
  5079. ast_mutex_lock(&devicelock);
  5080. d = devices;
  5081. while (d) {
  5082. if (d->to_delete >= 0)
  5083. d->to_delete = 1;
  5084. d = d->next;
  5085. }
  5086. ast_mutex_unlock(&devicelock);
  5087. /* load the device sections */
  5088. cat = ast_category_browse(cfg, NULL);
  5089. while (cat) {
  5090. if (strcasecmp(cat, "general")) {
  5091. d = build_device(cat, ast_variable_browse(cfg, cat));
  5092. }
  5093. cat = ast_category_browse(cfg, cat);
  5094. }
  5095. ast_mutex_lock(&devicelock);
  5096. d = devices;
  5097. while (d) {
  5098. if (d->to_delete) {
  5099. int i;
  5100. if (unistimdebug)
  5101. ast_verb(0, "Removing device '%s'\n", d->name);
  5102. if (!d->lines) {
  5103. ast_log(LOG_ERROR, "Device '%s' without a line !, aborting\n", d->name);
  5104. ast_config_destroy(cfg);
  5105. return 0;
  5106. }
  5107. if (!d->lines->subs[0]) {
  5108. ast_log(LOG_ERROR, "Device '%s' without a subchannel !, aborting\n",
  5109. d->name);
  5110. ast_config_destroy(cfg);
  5111. return 0;
  5112. }
  5113. if (d->lines->subs[0]->owner) {
  5114. ast_log(LOG_WARNING,
  5115. "Device '%s' was not deleted : a call is in progress. Try again later.\n",
  5116. d->name);
  5117. d = d->next;
  5118. continue;
  5119. }
  5120. ast_mutex_destroy(&d->lines->subs[0]->lock);
  5121. ast_free(d->lines->subs[0]);
  5122. for (i = 1; i < MAX_SUBS; i++) {
  5123. if (d->lines->subs[i]) {
  5124. ast_log(LOG_WARNING,
  5125. "Device '%s' with threeway call subchannels allocated, aborting.\n",
  5126. d->name);
  5127. break;
  5128. }
  5129. }
  5130. if (i < MAX_SUBS) {
  5131. d = d->next;
  5132. continue;
  5133. }
  5134. ast_mutex_destroy(&d->lines->lock);
  5135. ast_free(d->lines);
  5136. if (d->session) {
  5137. if (sessions == d->session)
  5138. sessions = d->session->next;
  5139. else {
  5140. s = sessions;
  5141. while (s) {
  5142. if (s->next == d->session) {
  5143. s->next = d->session->next;
  5144. break;
  5145. }
  5146. s = s->next;
  5147. }
  5148. }
  5149. ast_mutex_destroy(&d->session->lock);
  5150. ast_free(d->session);
  5151. }
  5152. if (devices == d)
  5153. devices = d->next;
  5154. else {
  5155. struct unistim_device *d2 = devices;
  5156. while (d2) {
  5157. if (d2->next == d) {
  5158. d2->next = d->next;
  5159. break;
  5160. }
  5161. d2 = d2->next;
  5162. }
  5163. }
  5164. if (d->tz) {
  5165. d->tz = ast_tone_zone_unref(d->tz);
  5166. }
  5167. ast_free(d);
  5168. d = devices;
  5169. continue;
  5170. }
  5171. d = d->next;
  5172. }
  5173. finish_bookmark();
  5174. ast_mutex_unlock(&devicelock);
  5175. ast_config_destroy(cfg);
  5176. ast_mutex_lock(&sessionlock);
  5177. s = sessions;
  5178. while (s) {
  5179. if (s->device)
  5180. refresh_all_favorite(s);
  5181. s = s->next;
  5182. }
  5183. ast_mutex_unlock(&sessionlock);
  5184. /* We don't recreate a socket when reloading (locks would be necessary). */
  5185. if (unistimsock > -1)
  5186. return 0;
  5187. bindaddr.sin_addr.s_addr = INADDR_ANY;
  5188. bindaddr.sin_port = htons(unistim_port);
  5189. bindaddr.sin_family = AF_INET;
  5190. unistimsock = socket(AF_INET, SOCK_DGRAM, 0);
  5191. if (unistimsock < 0) {
  5192. ast_log(LOG_WARNING, "Unable to create UNISTIM socket: %s\n", strerror(errno));
  5193. return -1;
  5194. }
  5195. #ifdef HAVE_PKTINFO
  5196. {
  5197. const int pktinfoFlag = 1;
  5198. setsockopt(unistimsock, IPPROTO_IP, IP_PKTINFO, &pktinfoFlag,
  5199. sizeof(pktinfoFlag));
  5200. }
  5201. #else
  5202. if (public_ip.sin_family == 0) {
  5203. ast_log(LOG_WARNING,
  5204. "Your OS does not support IP_PKTINFO, you must set public_ip.\n");
  5205. unistimsock = -1;
  5206. return -1;
  5207. }
  5208. #endif
  5209. setsockopt(unistimsock, SOL_SOCKET, SO_REUSEADDR, (const char *) &reuseFlag,
  5210. sizeof(reuseFlag));
  5211. if (bind(unistimsock, (struct sockaddr *) &bindaddr, sizeof(bindaddr)) < 0) {
  5212. ast_log(LOG_WARNING, "Failed to bind to %s:%d: %s\n",
  5213. ast_inet_ntoa(bindaddr.sin_addr), htons(bindaddr.sin_port),
  5214. strerror(errno));
  5215. close(unistimsock);
  5216. unistimsock = -1;
  5217. } else {
  5218. ast_verb(2, "UNISTIM Listening on %s:%d\n", ast_inet_ntoa(bindaddr.sin_addr), htons(bindaddr.sin_port));
  5219. ast_netsock_set_qos(unistimsock, qos.tos, qos.cos, "UNISTIM");
  5220. }
  5221. return 0;
  5222. }
  5223. static enum ast_rtp_glue_result unistim_get_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
  5224. {
  5225. struct unistim_subchannel *sub = chan->tech_pvt;
  5226. ao2_ref(sub->rtp, +1);
  5227. *instance = sub->rtp;
  5228. return AST_RTP_GLUE_RESULT_LOCAL;
  5229. }
  5230. static struct ast_rtp_glue unistim_rtp_glue = {
  5231. .type = channel_type,
  5232. .get_rtp_info = unistim_get_rtp_peer,
  5233. };
  5234. /*--- load_module: PBX load module - initialization ---*/
  5235. int load_module(void)
  5236. {
  5237. int res;
  5238. if (!(buff = ast_malloc(SIZE_PAGE)))
  5239. goto buff_failed;
  5240. io = io_context_create();
  5241. if (!io) {
  5242. ast_log(LOG_ERROR, "Failed to allocate IO context\n");
  5243. goto io_failed;
  5244. }
  5245. sched = sched_context_create();
  5246. if (!sched) {
  5247. ast_log(LOG_ERROR, "Failed to allocate scheduler context\n");
  5248. goto sched_failed;
  5249. }
  5250. res = reload_config();
  5251. if (res)
  5252. return AST_MODULE_LOAD_DECLINE;
  5253. /* Make sure we can register our unistim channel type */
  5254. if (ast_channel_register(&unistim_tech)) {
  5255. ast_log(LOG_ERROR, "Unable to register channel type '%s'\n", channel_type);
  5256. goto chanreg_failed;
  5257. }
  5258. ast_rtp_glue_register(&unistim_rtp_glue);
  5259. ast_cli_register_multiple(unistim_cli, ARRAY_LEN(unistim_cli));
  5260. restart_monitor();
  5261. return AST_MODULE_LOAD_SUCCESS;
  5262. chanreg_failed:
  5263. /*! XXX \todo Leaking anything allocated by reload_config() ... */
  5264. sched_context_destroy(sched);
  5265. sched = NULL;
  5266. sched_failed:
  5267. io_context_destroy(io);
  5268. io = NULL;
  5269. io_failed:
  5270. ast_free(buff);
  5271. buff = NULL;
  5272. buff_failed:
  5273. return AST_MODULE_LOAD_FAILURE;
  5274. }
  5275. static int unload_module(void)
  5276. {
  5277. /* First, take us out of the channel loop */
  5278. if (sched)
  5279. sched_context_destroy(sched);
  5280. ast_cli_unregister_multiple(unistim_cli, ARRAY_LEN(unistim_cli));
  5281. ast_channel_unregister(&unistim_tech);
  5282. ast_rtp_glue_unregister(&unistim_rtp_glue);
  5283. ast_mutex_lock(&monlock);
  5284. if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
  5285. pthread_cancel(monitor_thread);
  5286. pthread_kill(monitor_thread, SIGURG);
  5287. pthread_join(monitor_thread, NULL);
  5288. }
  5289. monitor_thread = AST_PTHREADT_STOP;
  5290. ast_mutex_unlock(&monlock);
  5291. if (buff)
  5292. ast_free(buff);
  5293. if (unistimsock > -1)
  5294. close(unistimsock);
  5295. return 0;
  5296. }
  5297. /*! reload: Part of Asterisk module interface ---*/
  5298. int reload(void)
  5299. {
  5300. unistim_reload(NULL, 0, NULL);
  5301. return 0;
  5302. }
  5303. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "UNISTIM Protocol (USTM)",
  5304. .load = load_module,
  5305. .unload = unload_module,
  5306. .reload = reload,
  5307. );