chan_mobile.c 122 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2006, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*!
  19. * \file
  20. * \brief Bluetooth Mobile Device channel driver
  21. *
  22. * \author Dave Bowerman <david.bowerman@gmail.com>
  23. *
  24. * \ingroup channel_drivers
  25. */
  26. /*! \li \ref chan_mobile.c uses the configuration file \ref chan_mobile.conf
  27. * \addtogroup configuration_file Configuration Files
  28. */
  29. /*!
  30. * \page chan_mobile.conf chan_mobile.conf
  31. * \verbinclude chan_mobile.conf.sample
  32. */
  33. /*** MODULEINFO
  34. <depend>bluetooth</depend>
  35. <defaultenabled>no</defaultenabled>
  36. <support_level>extended</support_level>
  37. ***/
  38. #include "asterisk.h"
  39. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  40. #include <pthread.h>
  41. #include <signal.h>
  42. #include <bluetooth/bluetooth.h>
  43. #include <bluetooth/hci.h>
  44. #include <bluetooth/hci_lib.h>
  45. #include <bluetooth/sdp.h>
  46. #include <bluetooth/sdp_lib.h>
  47. #include <bluetooth/rfcomm.h>
  48. #include <bluetooth/sco.h>
  49. #include <bluetooth/l2cap.h>
  50. #include "asterisk/compat.h"
  51. #include "asterisk/lock.h"
  52. #include "asterisk/channel.h"
  53. #include "asterisk/config.h"
  54. #include "asterisk/logger.h"
  55. #include "asterisk/module.h"
  56. #include "asterisk/pbx.h"
  57. #include "asterisk/options.h"
  58. #include "asterisk/utils.h"
  59. #include "asterisk/linkedlists.h"
  60. #include "asterisk/cli.h"
  61. #include "asterisk/devicestate.h"
  62. #include "asterisk/causes.h"
  63. #include "asterisk/dsp.h"
  64. #include "asterisk/app.h"
  65. #include "asterisk/manager.h"
  66. #include "asterisk/io.h"
  67. #define MBL_CONFIG "chan_mobile.conf"
  68. #define MBL_CONFIG_OLD "mobile.conf"
  69. #define DEVICE_FRAME_SIZE 48
  70. #define DEVICE_FRAME_FORMAT AST_FORMAT_SLINEAR
  71. #define CHANNEL_FRAME_SIZE 320
  72. static struct ast_format prefformat;
  73. static int discovery_interval = 60; /* The device discovery interval, default 60 seconds. */
  74. static pthread_t discovery_thread = AST_PTHREADT_NULL; /* The discovery thread */
  75. static sdp_session_t *sdp_session;
  76. AST_MUTEX_DEFINE_STATIC(unload_mutex);
  77. static int unloading_flag = 0;
  78. static inline int check_unloading(void);
  79. static inline void set_unloading(void);
  80. enum mbl_type {
  81. MBL_TYPE_PHONE,
  82. MBL_TYPE_HEADSET
  83. };
  84. struct adapter_pvt {
  85. int dev_id; /* device id */
  86. int hci_socket; /* device descriptor */
  87. char id[31]; /* the 'name' from mobile.conf */
  88. bdaddr_t addr; /* adddress of adapter */
  89. unsigned int inuse:1; /* are we in use ? */
  90. unsigned int alignment_detection:1; /* do alignment detection on this adpater? */
  91. struct io_context *io; /*!< io context for audio connections */
  92. struct io_context *accept_io; /*!< io context for sco listener */
  93. int *sco_id; /*!< the io context id of the sco listener socket */
  94. int sco_socket; /*!< sco listener socket */
  95. pthread_t sco_listener_thread; /*!< sco listener thread */
  96. AST_LIST_ENTRY(adapter_pvt) entry;
  97. };
  98. static AST_RWLIST_HEAD_STATIC(adapters, adapter_pvt);
  99. struct msg_queue_entry;
  100. struct hfp_pvt;
  101. struct mbl_pvt {
  102. struct ast_channel *owner; /* Channel we belong to, possibly NULL */
  103. struct ast_frame fr; /* "null" frame */
  104. ast_mutex_t lock; /*!< pvt lock */
  105. /*! queue for messages we are expecting */
  106. AST_LIST_HEAD_NOLOCK(msg_queue, msg_queue_entry) msg_queue;
  107. enum mbl_type type; /* Phone or Headset */
  108. char id[31]; /* The id from mobile.conf */
  109. int group; /* group number for group dialling */
  110. bdaddr_t addr; /* address of device */
  111. struct adapter_pvt *adapter; /* the adapter we use */
  112. char context[AST_MAX_CONTEXT]; /* the context for incoming calls */
  113. struct hfp_pvt *hfp; /*!< hfp pvt */
  114. int rfcomm_port; /* rfcomm port number */
  115. int rfcomm_socket; /* rfcomm socket descriptor */
  116. char rfcomm_buf[256];
  117. char io_buf[CHANNEL_FRAME_SIZE + AST_FRIENDLY_OFFSET];
  118. struct ast_smoother *smoother; /* our smoother, for making 48 byte frames */
  119. int sco_socket; /* sco socket descriptor */
  120. pthread_t monitor_thread; /* monitor thread handle */
  121. int timeout; /*!< used to set the timeout for rfcomm data (may be used in the future) */
  122. unsigned int no_callsetup:1;
  123. unsigned int has_sms:1;
  124. unsigned int do_alignment_detection:1;
  125. unsigned int alignment_detection_triggered:1;
  126. unsigned int blackberry:1;
  127. short alignment_samples[4];
  128. int alignment_count;
  129. int ring_sched_id;
  130. struct ast_dsp *dsp;
  131. struct ast_sched_context *sched;
  132. int hangupcause;
  133. /* flags */
  134. unsigned int outgoing:1; /*!< outgoing call */
  135. unsigned int incoming:1; /*!< incoming call */
  136. unsigned int outgoing_sms:1; /*!< outgoing sms */
  137. unsigned int incoming_sms:1; /*!< outgoing sms */
  138. unsigned int needcallerid:1; /*!< we need callerid */
  139. unsigned int needchup:1; /*!< we need to send a chup */
  140. unsigned int needring:1; /*!< we need to send a RING */
  141. unsigned int answered:1; /*!< we sent/received an answer */
  142. unsigned int connected:1; /*!< do we have an rfcomm connection to a device */
  143. AST_LIST_ENTRY(mbl_pvt) entry;
  144. };
  145. static AST_RWLIST_HEAD_STATIC(devices, mbl_pvt);
  146. static int handle_response_ok(struct mbl_pvt *pvt, char *buf);
  147. static int handle_response_error(struct mbl_pvt *pvt, char *buf);
  148. static int handle_response_ciev(struct mbl_pvt *pvt, char *buf);
  149. static int handle_response_clip(struct mbl_pvt *pvt, char *buf);
  150. static int handle_response_ring(struct mbl_pvt *pvt, char *buf);
  151. static int handle_response_cmti(struct mbl_pvt *pvt, char *buf);
  152. static int handle_response_cmgr(struct mbl_pvt *pvt, char *buf);
  153. static int handle_response_cusd(struct mbl_pvt *pvt, char *buf);
  154. static int handle_response_busy(struct mbl_pvt *pvt);
  155. static int handle_response_no_dialtone(struct mbl_pvt *pvt, char *buf);
  156. static int handle_response_no_carrier(struct mbl_pvt *pvt, char *buf);
  157. static int handle_sms_prompt(struct mbl_pvt *pvt, char *buf);
  158. /* CLI stuff */
  159. static char *handle_cli_mobile_show_devices(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
  160. static char *handle_cli_mobile_search(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
  161. static char *handle_cli_mobile_rfcomm(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
  162. static char *handle_cli_mobile_cusd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
  163. static struct ast_cli_entry mbl_cli[] = {
  164. AST_CLI_DEFINE(handle_cli_mobile_show_devices, "Show Bluetooth Cell / Mobile devices"),
  165. AST_CLI_DEFINE(handle_cli_mobile_search, "Search for Bluetooth Cell / Mobile devices"),
  166. AST_CLI_DEFINE(handle_cli_mobile_rfcomm, "Send commands to the rfcomm port for debugging"),
  167. AST_CLI_DEFINE(handle_cli_mobile_cusd, "Send CUSD commands to the mobile"),
  168. };
  169. /* App stuff */
  170. static char *app_mblstatus = "MobileStatus";
  171. static char *mblstatus_synopsis = "MobileStatus(Device,Variable)";
  172. static char *mblstatus_desc =
  173. "MobileStatus(Device,Variable)\n"
  174. " Device - Id of mobile device from mobile.conf\n"
  175. " Variable - Variable to store status in will be 1-3.\n"
  176. " In order, Disconnected, Connected & Free, Connected & Busy.\n";
  177. static char *app_mblsendsms = "MobileSendSMS";
  178. static char *mblsendsms_synopsis = "MobileSendSMS(Device,Dest,Message)";
  179. static char *mblsendsms_desc =
  180. "MobileSendSms(Device,Dest,Message)\n"
  181. " Device - Id of device from mobile.conf\n"
  182. " Dest - destination\n"
  183. " Message - text of the message\n";
  184. static struct ast_channel *mbl_new(int state, struct mbl_pvt *pvt, char *cid_num,
  185. const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor);
  186. static struct ast_channel *mbl_request(const char *type, struct ast_format_cap *cap,
  187. const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause);
  188. static int mbl_call(struct ast_channel *ast, const char *dest, int timeout);
  189. static int mbl_hangup(struct ast_channel *ast);
  190. static int mbl_answer(struct ast_channel *ast);
  191. static int mbl_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
  192. static struct ast_frame *mbl_read(struct ast_channel *ast);
  193. static int mbl_write(struct ast_channel *ast, struct ast_frame *frame);
  194. static int mbl_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
  195. static int mbl_devicestate(const char *data);
  196. static void do_alignment_detection(struct mbl_pvt *pvt, char *buf, int buflen);
  197. static int mbl_queue_control(struct mbl_pvt *pvt, enum ast_control_frame_type control);
  198. static int mbl_queue_hangup(struct mbl_pvt *pvt);
  199. static int mbl_ast_hangup(struct mbl_pvt *pvt);
  200. static int mbl_has_service(struct mbl_pvt *pvt);
  201. static int rfcomm_connect(bdaddr_t src, bdaddr_t dst, int remote_channel);
  202. static int rfcomm_write(int rsock, char *buf);
  203. static int rfcomm_write_full(int rsock, char *buf, size_t count);
  204. static int rfcomm_wait(int rsock, int *ms);
  205. static ssize_t rfcomm_read(int rsock, char *buf, size_t count);
  206. static int sco_connect(bdaddr_t src, bdaddr_t dst);
  207. static int sco_write(int s, char *buf, int len);
  208. static int sco_accept(int *id, int fd, short events, void *data);
  209. static int sco_bind(struct adapter_pvt *adapter);
  210. static void *do_sco_listen(void *data);
  211. static int sdp_search(char *addr, int profile);
  212. static int headset_send_ring(const void *data);
  213. /*
  214. * bluetooth handsfree profile helpers
  215. */
  216. #define HFP_HF_ECNR (1 << 0)
  217. #define HFP_HF_CW (1 << 1)
  218. #define HFP_HF_CID (1 << 2)
  219. #define HFP_HF_VOICE (1 << 3)
  220. #define HFP_HF_VOLUME (1 << 4)
  221. #define HFP_HF_STATUS (1 << 5)
  222. #define HFP_HF_CONTROL (1 << 6)
  223. #define HFP_AG_CW (1 << 0)
  224. #define HFP_AG_ECNR (1 << 1)
  225. #define HFP_AG_VOICE (1 << 2)
  226. #define HFP_AG_RING (1 << 3)
  227. #define HFP_AG_TAG (1 << 4)
  228. #define HFP_AG_REJECT (1 << 5)
  229. #define HFP_AG_STATUS (1 << 6)
  230. #define HFP_AG_CONTROL (1 << 7)
  231. #define HFP_AG_ERRORS (1 << 8)
  232. #define HFP_CIND_UNKNOWN -1
  233. #define HFP_CIND_NONE 0
  234. #define HFP_CIND_SERVICE 1
  235. #define HFP_CIND_CALL 2
  236. #define HFP_CIND_CALLSETUP 3
  237. #define HFP_CIND_CALLHELD 4
  238. #define HFP_CIND_SIGNAL 5
  239. #define HFP_CIND_ROAM 6
  240. #define HFP_CIND_BATTCHG 7
  241. /* call indicator values */
  242. #define HFP_CIND_CALL_NONE 0
  243. #define HFP_CIND_CALL_ACTIVE 1
  244. /* callsetup indicator values */
  245. #define HFP_CIND_CALLSETUP_NONE 0
  246. #define HFP_CIND_CALLSETUP_INCOMING 1
  247. #define HFP_CIND_CALLSETUP_OUTGOING 2
  248. #define HFP_CIND_CALLSETUP_ALERTING 3
  249. /* service indicator values */
  250. #define HFP_CIND_SERVICE_NONE 0
  251. #define HFP_CIND_SERVICE_AVAILABLE 1
  252. /*!
  253. * \brief This struct holds HFP features that we support.
  254. */
  255. struct hfp_hf {
  256. int ecnr:1; /*!< echo-cancel/noise reduction */
  257. int cw:1; /*!< call waiting and three way calling */
  258. int cid:1; /*!< cli presentation (callier id) */
  259. int voice:1; /*!< voice recognition activation */
  260. int volume:1; /*!< remote volume control */
  261. int status:1; /*!< enhanced call status */
  262. int control:1; /*!< enhanced call control*/
  263. };
  264. /*!
  265. * \brief This struct holds HFP features the AG supports.
  266. */
  267. struct hfp_ag {
  268. int cw:1; /*!< three way calling */
  269. int ecnr:1; /*!< echo-cancel/noise reduction */
  270. int voice:1; /*!< voice recognition */
  271. int ring:1; /*!< in band ring tone capability */
  272. int tag:1; /*!< attach a number to a voice tag */
  273. int reject:1; /*!< ability to reject a call */
  274. int status:1; /*!< enhanced call status */
  275. int control:1; /*!< enhanced call control*/
  276. int errors:1; /*!< extended error result codes*/
  277. };
  278. /*!
  279. * \brief This struct holds mappings for indications.
  280. */
  281. struct hfp_cind {
  282. int service; /*!< whether we have service or not */
  283. int call; /*!< call state */
  284. int callsetup; /*!< bluetooth call setup indications */
  285. int callheld; /*!< bluetooth call hold indications */
  286. int signal; /*!< signal strength */
  287. int roam; /*!< roaming indicator */
  288. int battchg; /*!< battery charge indicator */
  289. };
  290. /*!
  291. * \brief This struct holds state information about the current hfp connection.
  292. */
  293. struct hfp_pvt {
  294. struct mbl_pvt *owner; /*!< the mbl_pvt struct that owns this struct */
  295. int initialized:1; /*!< whether a service level connection exists or not */
  296. int nocallsetup:1; /*!< whether we detected a callsetup indicator */
  297. struct hfp_ag brsf; /*!< the supported feature set of the AG */
  298. int cind_index[16]; /*!< the cind/ciev index to name mapping for this AG */
  299. int cind_state[16]; /*!< the cind/ciev state for this AG */
  300. struct hfp_cind cind_map; /*!< the cind name to index mapping for this AG */
  301. int rsock; /*!< our rfcomm socket */
  302. int rport; /*!< our rfcomm port */
  303. int sent_alerting; /*!< have we sent alerting? */
  304. };
  305. /* Our supported features.
  306. * we only support caller id
  307. */
  308. static struct hfp_hf hfp_our_brsf = {
  309. .ecnr = 0,
  310. .cw = 0,
  311. .cid = 1,
  312. .voice = 0,
  313. .volume = 0,
  314. .status = 0,
  315. .control = 0,
  316. };
  317. static int hfp_parse_ciev(struct hfp_pvt *hfp, char *buf, int *value);
  318. static char *hfp_parse_clip(struct hfp_pvt *hfp, char *buf);
  319. static int hfp_parse_cmti(struct hfp_pvt *hfp, char *buf);
  320. static int hfp_parse_cmgr(struct hfp_pvt *hfp, char *buf, char **from_number, char **text);
  321. static int hfp_parse_brsf(struct hfp_pvt *hfp, const char *buf);
  322. static int hfp_parse_cind(struct hfp_pvt *hfp, char *buf);
  323. static int hfp_parse_cind_test(struct hfp_pvt *hfp, char *buf);
  324. static char *hfp_parse_cusd(struct hfp_pvt *hfp, char *buf);
  325. static int hfp_brsf2int(struct hfp_hf *hf);
  326. static struct hfp_ag *hfp_int2brsf(int brsf, struct hfp_ag *ag);
  327. static int hfp_send_brsf(struct hfp_pvt *hfp, struct hfp_hf *brsf);
  328. static int hfp_send_cind(struct hfp_pvt *hfp);
  329. static int hfp_send_cind_test(struct hfp_pvt *hfp);
  330. static int hfp_send_cmer(struct hfp_pvt *hfp, int status);
  331. static int hfp_send_clip(struct hfp_pvt *hfp, int status);
  332. static int hfp_send_vgs(struct hfp_pvt *hfp, int value);
  333. #if 0
  334. static int hfp_send_vgm(struct hfp_pvt *hfp, int value);
  335. #endif
  336. static int hfp_send_dtmf(struct hfp_pvt *hfp, char digit);
  337. static int hfp_send_cmgf(struct hfp_pvt *hfp, int mode);
  338. static int hfp_send_cnmi(struct hfp_pvt *hfp);
  339. static int hfp_send_cmgr(struct hfp_pvt *hfp, int index);
  340. static int hfp_send_cmgs(struct hfp_pvt *hfp, const char *number);
  341. static int hfp_send_sms_text(struct hfp_pvt *hfp, const char *message);
  342. static int hfp_send_chup(struct hfp_pvt *hfp);
  343. static int hfp_send_atd(struct hfp_pvt *hfp, const char *number);
  344. static int hfp_send_ata(struct hfp_pvt *hfp);
  345. static int hfp_send_cusd(struct hfp_pvt *hfp, const char *code);
  346. /*
  347. * bluetooth headset profile helpers
  348. */
  349. static int hsp_send_ok(int rsock);
  350. static int hsp_send_error(int rsock);
  351. static int hsp_send_vgs(int rsock, int gain);
  352. static int hsp_send_vgm(int rsock, int gain);
  353. static int hsp_send_ring(int rsock);
  354. /*
  355. * Hayes AT command helpers
  356. */
  357. typedef enum {
  358. /* errors */
  359. AT_PARSE_ERROR = -2,
  360. AT_READ_ERROR = -1,
  361. AT_UNKNOWN = 0,
  362. /* at responses */
  363. AT_OK,
  364. AT_ERROR,
  365. AT_RING,
  366. AT_BRSF,
  367. AT_CIND,
  368. AT_CIEV,
  369. AT_CLIP,
  370. AT_CMTI,
  371. AT_CMGR,
  372. AT_SMS_PROMPT,
  373. AT_CMS_ERROR,
  374. /* at commands */
  375. AT_A,
  376. AT_D,
  377. AT_CHUP,
  378. AT_CKPD,
  379. AT_CMGS,
  380. AT_VGM,
  381. AT_VGS,
  382. AT_VTS,
  383. AT_CMGF,
  384. AT_CNMI,
  385. AT_CMER,
  386. AT_CIND_TEST,
  387. AT_CUSD,
  388. AT_BUSY,
  389. AT_NO_DIALTONE,
  390. AT_NO_CARRIER,
  391. AT_ECAM,
  392. } at_message_t;
  393. static int at_match_prefix(char *buf, char *prefix);
  394. static at_message_t at_read_full(int rsock, char *buf, size_t count);
  395. static inline const char *at_msg2str(at_message_t msg);
  396. struct msg_queue_entry {
  397. at_message_t expected;
  398. at_message_t response_to;
  399. void *data;
  400. AST_LIST_ENTRY(msg_queue_entry) entry;
  401. };
  402. static int msg_queue_push(struct mbl_pvt *pvt, at_message_t expect, at_message_t response_to);
  403. static int msg_queue_push_data(struct mbl_pvt *pvt, at_message_t expect, at_message_t response_to, void *data);
  404. static struct msg_queue_entry *msg_queue_pop(struct mbl_pvt *pvt);
  405. static void msg_queue_free_and_pop(struct mbl_pvt *pvt);
  406. static void msg_queue_flush(struct mbl_pvt *pvt);
  407. static struct msg_queue_entry *msg_queue_head(struct mbl_pvt *pvt);
  408. /*
  409. * channel stuff
  410. */
  411. static struct ast_channel_tech mbl_tech = {
  412. .type = "Mobile",
  413. .description = "Bluetooth Mobile Device Channel Driver",
  414. .requester = mbl_request,
  415. .call = mbl_call,
  416. .hangup = mbl_hangup,
  417. .answer = mbl_answer,
  418. .send_digit_end = mbl_digit_end,
  419. .read = mbl_read,
  420. .write = mbl_write,
  421. .fixup = mbl_fixup,
  422. .devicestate = mbl_devicestate
  423. };
  424. /* CLI Commands implementation */
  425. static char *handle_cli_mobile_show_devices(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  426. {
  427. struct mbl_pvt *pvt;
  428. char bdaddr[18];
  429. char group[6];
  430. #define FORMAT1 "%-15.15s %-17.17s %-5.5s %-15.15s %-9.9s %-10.10s %-3.3s\n"
  431. switch (cmd) {
  432. case CLI_INIT:
  433. e->command = "mobile show devices";
  434. e->usage =
  435. "Usage: mobile show devices\n"
  436. " Shows the state of Bluetooth Cell / Mobile devices.\n";
  437. return NULL;
  438. case CLI_GENERATE:
  439. return NULL;
  440. }
  441. if (a->argc != 3)
  442. return CLI_SHOWUSAGE;
  443. ast_cli(a->fd, FORMAT1, "ID", "Address", "Group", "Adapter", "Connected", "State", "SMS");
  444. AST_RWLIST_RDLOCK(&devices);
  445. AST_RWLIST_TRAVERSE(&devices, pvt, entry) {
  446. ast_mutex_lock(&pvt->lock);
  447. ba2str(&pvt->addr, bdaddr);
  448. snprintf(group, sizeof(group), "%d", pvt->group);
  449. ast_cli(a->fd, FORMAT1,
  450. pvt->id,
  451. bdaddr,
  452. group,
  453. pvt->adapter->id,
  454. pvt->connected ? "Yes" : "No",
  455. (!pvt->connected) ? "None" : (pvt->owner) ? "Busy" : (pvt->outgoing_sms || pvt->incoming_sms) ? "SMS" : (mbl_has_service(pvt)) ? "Free" : "No Service",
  456. (pvt->has_sms) ? "Yes" : "No"
  457. );
  458. ast_mutex_unlock(&pvt->lock);
  459. }
  460. AST_RWLIST_UNLOCK(&devices);
  461. #undef FORMAT1
  462. return CLI_SUCCESS;
  463. }
  464. static char *handle_cli_mobile_search(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  465. {
  466. struct adapter_pvt *adapter;
  467. inquiry_info *ii = NULL;
  468. int max_rsp, num_rsp;
  469. int len, flags;
  470. int i, phport, hsport;
  471. char addr[19] = {0};
  472. char name[31] = {0};
  473. #define FORMAT1 "%-17.17s %-30.30s %-6.6s %-7.7s %-4.4s\n"
  474. #define FORMAT2 "%-17.17s %-30.30s %-6.6s %-7.7s %d\n"
  475. switch (cmd) {
  476. case CLI_INIT:
  477. e->command = "mobile search";
  478. e->usage =
  479. "Usage: mobile search\n"
  480. " Searches for Bluetooth Cell / Mobile devices in range.\n";
  481. return NULL;
  482. case CLI_GENERATE:
  483. return NULL;
  484. }
  485. if (a->argc != 2)
  486. return CLI_SHOWUSAGE;
  487. /* find a free adapter */
  488. AST_RWLIST_RDLOCK(&adapters);
  489. AST_RWLIST_TRAVERSE(&adapters, adapter, entry) {
  490. if (!adapter->inuse)
  491. break;
  492. }
  493. AST_RWLIST_UNLOCK(&adapters);
  494. if (!adapter) {
  495. ast_cli(a->fd, "All Bluetooth adapters are in use at this time.\n");
  496. return CLI_SUCCESS;
  497. }
  498. len = 8;
  499. max_rsp = 255;
  500. flags = IREQ_CACHE_FLUSH;
  501. ii = ast_alloca(max_rsp * sizeof(inquiry_info));
  502. num_rsp = hci_inquiry(adapter->dev_id, len, max_rsp, NULL, &ii, flags);
  503. if (num_rsp > 0) {
  504. ast_cli(a->fd, FORMAT1, "Address", "Name", "Usable", "Type", "Port");
  505. for (i = 0; i < num_rsp; i++) {
  506. ba2str(&(ii + i)->bdaddr, addr);
  507. name[0] = 0x00;
  508. if (hci_read_remote_name(adapter->hci_socket, &(ii + i)->bdaddr, sizeof(name) - 1, name, 0) < 0)
  509. strcpy(name, "[unknown]");
  510. phport = sdp_search(addr, HANDSFREE_AGW_PROFILE_ID);
  511. if (!phport)
  512. hsport = sdp_search(addr, HEADSET_PROFILE_ID);
  513. else
  514. hsport = 0;
  515. ast_cli(a->fd, FORMAT2, addr, name, (phport > 0 || hsport > 0) ? "Yes" : "No",
  516. (phport > 0) ? "Phone" : "Headset", (phport > 0) ? phport : hsport);
  517. }
  518. } else
  519. ast_cli(a->fd, "No Bluetooth Cell / Mobile devices found.\n");
  520. #undef FORMAT1
  521. #undef FORMAT2
  522. return CLI_SUCCESS;
  523. }
  524. static char *handle_cli_mobile_rfcomm(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  525. {
  526. char buf[128];
  527. struct mbl_pvt *pvt = NULL;
  528. switch (cmd) {
  529. case CLI_INIT:
  530. e->command = "mobile rfcomm";
  531. e->usage =
  532. "Usage: mobile rfcomm <device ID> <command>\n"
  533. " Send <command> to the rfcomm port on the device\n"
  534. " with the specified <device ID>.\n";
  535. return NULL;
  536. case CLI_GENERATE:
  537. return NULL;
  538. }
  539. if (a->argc != 4)
  540. return CLI_SHOWUSAGE;
  541. AST_RWLIST_RDLOCK(&devices);
  542. AST_RWLIST_TRAVERSE(&devices, pvt, entry) {
  543. if (!strcmp(pvt->id, a->argv[2]))
  544. break;
  545. }
  546. AST_RWLIST_UNLOCK(&devices);
  547. if (!pvt) {
  548. ast_cli(a->fd, "Device %s not found.\n", a->argv[2]);
  549. goto e_return;
  550. }
  551. ast_mutex_lock(&pvt->lock);
  552. if (!pvt->connected) {
  553. ast_cli(a->fd, "Device %s not connected.\n", a->argv[2]);
  554. goto e_unlock_pvt;
  555. }
  556. snprintf(buf, sizeof(buf), "%s\r", a->argv[3]);
  557. rfcomm_write(pvt->rfcomm_socket, buf);
  558. msg_queue_push(pvt, AT_OK, AT_UNKNOWN);
  559. e_unlock_pvt:
  560. ast_mutex_unlock(&pvt->lock);
  561. e_return:
  562. return CLI_SUCCESS;
  563. }
  564. static char *handle_cli_mobile_cusd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  565. {
  566. char buf[128];
  567. struct mbl_pvt *pvt = NULL;
  568. switch (cmd) {
  569. case CLI_INIT:
  570. e->command = "mobile cusd";
  571. e->usage =
  572. "Usage: mobile cusd <device ID> <command>\n"
  573. " Send cusd <command> to the rfcomm port on the device\n"
  574. " with the specified <device ID>.\n";
  575. return NULL;
  576. case CLI_GENERATE:
  577. return NULL;
  578. }
  579. if (a->argc != 4)
  580. return CLI_SHOWUSAGE;
  581. AST_RWLIST_RDLOCK(&devices);
  582. AST_RWLIST_TRAVERSE(&devices, pvt, entry) {
  583. if (!strcmp(pvt->id, a->argv[2]))
  584. break;
  585. }
  586. AST_RWLIST_UNLOCK(&devices);
  587. if (!pvt) {
  588. ast_cli(a->fd, "Device %s not found.\n", a->argv[2]);
  589. goto e_return;
  590. }
  591. ast_mutex_lock(&pvt->lock);
  592. if (!pvt->connected) {
  593. ast_cli(a->fd, "Device %s not connected.\n", a->argv[2]);
  594. goto e_unlock_pvt;
  595. }
  596. snprintf(buf, sizeof(buf), "%s", a->argv[3]);
  597. if (hfp_send_cusd(pvt->hfp, buf) || msg_queue_push(pvt, AT_OK, AT_CUSD)) {
  598. ast_cli(a->fd, "[%s] error sending CUSD\n", pvt->id);
  599. goto e_unlock_pvt;
  600. }
  601. e_unlock_pvt:
  602. ast_mutex_unlock(&pvt->lock);
  603. e_return:
  604. return CLI_SUCCESS;
  605. }
  606. /*
  607. Dialplan applications implementation
  608. */
  609. static int mbl_status_exec(struct ast_channel *ast, const char *data)
  610. {
  611. struct mbl_pvt *pvt;
  612. char *parse;
  613. int stat;
  614. char status[2];
  615. AST_DECLARE_APP_ARGS(args,
  616. AST_APP_ARG(device);
  617. AST_APP_ARG(variable);
  618. );
  619. if (ast_strlen_zero(data))
  620. return -1;
  621. parse = ast_strdupa(data);
  622. AST_STANDARD_APP_ARGS(args, parse);
  623. if (ast_strlen_zero(args.device) || ast_strlen_zero(args.variable))
  624. return -1;
  625. stat = 1;
  626. AST_RWLIST_RDLOCK(&devices);
  627. AST_RWLIST_TRAVERSE(&devices, pvt, entry) {
  628. if (!strcmp(pvt->id, args.device))
  629. break;
  630. }
  631. AST_RWLIST_UNLOCK(&devices);
  632. if (pvt) {
  633. ast_mutex_lock(&pvt->lock);
  634. if (pvt->connected)
  635. stat = 2;
  636. if (pvt->owner)
  637. stat = 3;
  638. ast_mutex_unlock(&pvt->lock);
  639. }
  640. snprintf(status, sizeof(status), "%d", stat);
  641. pbx_builtin_setvar_helper(ast, args.variable, status);
  642. return 0;
  643. }
  644. static int mbl_sendsms_exec(struct ast_channel *ast, const char *data)
  645. {
  646. struct mbl_pvt *pvt;
  647. char *parse, *message;
  648. AST_DECLARE_APP_ARGS(args,
  649. AST_APP_ARG(device);
  650. AST_APP_ARG(dest);
  651. AST_APP_ARG(message);
  652. );
  653. if (ast_strlen_zero(data))
  654. return -1;
  655. parse = ast_strdupa(data);
  656. AST_STANDARD_APP_ARGS(args, parse);
  657. if (ast_strlen_zero(args.device)) {
  658. ast_log(LOG_ERROR,"NULL device for message -- SMS will not be sent.\n");
  659. return -1;
  660. }
  661. if (ast_strlen_zero(args.dest)) {
  662. ast_log(LOG_ERROR,"NULL destination for message -- SMS will not be sent.\n");
  663. return -1;
  664. }
  665. if (ast_strlen_zero(args.message)) {
  666. ast_log(LOG_ERROR,"NULL Message to be sent -- SMS will not be sent.\n");
  667. return -1;
  668. }
  669. AST_RWLIST_RDLOCK(&devices);
  670. AST_RWLIST_TRAVERSE(&devices, pvt, entry) {
  671. if (!strcmp(pvt->id, args.device))
  672. break;
  673. }
  674. AST_RWLIST_UNLOCK(&devices);
  675. if (!pvt) {
  676. ast_log(LOG_ERROR,"Bluetooth device %s wasn't found in the list -- SMS will not be sent.\n", args.device);
  677. goto e_return;
  678. }
  679. ast_mutex_lock(&pvt->lock);
  680. if (!pvt->connected) {
  681. ast_log(LOG_ERROR,"Bluetooth device %s wasn't connected -- SMS will not be sent.\n", args.device);
  682. goto e_unlock_pvt;
  683. }
  684. if (!pvt->has_sms) {
  685. ast_log(LOG_ERROR,"Bluetooth device %s doesn't handle SMS -- SMS will not be sent.\n", args.device);
  686. goto e_unlock_pvt;
  687. }
  688. message = ast_strdup(args.message);
  689. if (hfp_send_cmgs(pvt->hfp, args.dest)
  690. || msg_queue_push_data(pvt, AT_SMS_PROMPT, AT_CMGS, message)) {
  691. ast_log(LOG_ERROR, "[%s] problem sending SMS message\n", pvt->id);
  692. goto e_free_message;
  693. }
  694. ast_mutex_unlock(&pvt->lock);
  695. return 0;
  696. e_free_message:
  697. ast_free(message);
  698. e_unlock_pvt:
  699. ast_mutex_unlock(&pvt->lock);
  700. e_return:
  701. return -1;
  702. }
  703. /*
  704. Channel Driver callbacks
  705. */
  706. static struct ast_channel *mbl_new(int state, struct mbl_pvt *pvt, char *cid_num,
  707. const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor)
  708. {
  709. struct ast_channel *chn;
  710. pvt->answered = 0;
  711. pvt->alignment_count = 0;
  712. pvt->alignment_detection_triggered = 0;
  713. if (pvt->adapter->alignment_detection)
  714. pvt->do_alignment_detection = 1;
  715. else
  716. pvt->do_alignment_detection = 0;
  717. ast_smoother_reset(pvt->smoother, DEVICE_FRAME_SIZE);
  718. ast_dsp_digitreset(pvt->dsp);
  719. chn = ast_channel_alloc(1, state, cid_num, pvt->id, 0, 0, pvt->context,
  720. assignedids, requestor, 0,
  721. "Mobile/%s-%04lx", pvt->id, ast_random() & 0xffff);
  722. if (!chn) {
  723. goto e_return;
  724. }
  725. ast_channel_tech_set(chn, &mbl_tech);
  726. ast_format_cap_add(ast_channel_nativeformats(chn), &prefformat);
  727. ast_format_copy(ast_channel_rawreadformat(chn), &prefformat);
  728. ast_format_copy(ast_channel_rawwriteformat(chn), &prefformat);
  729. ast_format_copy(ast_channel_writeformat(chn), &prefformat);
  730. ast_format_copy(ast_channel_readformat(chn), &prefformat);
  731. ast_channel_tech_pvt_set(chn, pvt);
  732. if (state == AST_STATE_RING)
  733. ast_channel_rings_set(chn, 1);
  734. ast_channel_language_set(chn, "en");
  735. pvt->owner = chn;
  736. if (pvt->sco_socket != -1) {
  737. ast_channel_set_fd(chn, 0, pvt->sco_socket);
  738. }
  739. ast_channel_unlock(chn);
  740. return chn;
  741. e_return:
  742. return NULL;
  743. }
  744. static struct ast_channel *mbl_request(const char *type, struct ast_format_cap *cap,
  745. const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
  746. {
  747. struct ast_channel *chn = NULL;
  748. struct mbl_pvt *pvt;
  749. char *dest_dev = NULL;
  750. char *dest_num = NULL;
  751. int group = -1;
  752. if (!data) {
  753. ast_log(LOG_WARNING, "Channel requested with no data\n");
  754. *cause = AST_CAUSE_INCOMPATIBLE_DESTINATION;
  755. return NULL;
  756. }
  757. if (!(ast_format_cap_iscompatible(cap, &prefformat))) {
  758. char tmp[256];
  759. ast_log(LOG_WARNING, "Asked to get a channel of unsupported format '%s'\n", ast_getformatname_multiple(tmp, sizeof(tmp), cap));
  760. *cause = AST_CAUSE_FACILITY_NOT_IMPLEMENTED;
  761. return NULL;
  762. }
  763. dest_dev = ast_strdupa(data);
  764. dest_num = strchr(dest_dev, '/');
  765. if (dest_num)
  766. *dest_num++ = 0x00;
  767. if (((dest_dev[0] == 'g') || (dest_dev[0] == 'G')) && ((dest_dev[1] >= '0') && (dest_dev[1] <= '9'))) {
  768. group = atoi(&dest_dev[1]);
  769. }
  770. /* Find requested device and make sure it's connected. */
  771. AST_RWLIST_RDLOCK(&devices);
  772. AST_RWLIST_TRAVERSE(&devices, pvt, entry) {
  773. if (group > -1 && pvt->group == group && pvt->connected && !pvt->owner) {
  774. if (!mbl_has_service(pvt)) {
  775. continue;
  776. }
  777. break;
  778. } else if (!strcmp(pvt->id, dest_dev)) {
  779. break;
  780. }
  781. }
  782. AST_RWLIST_UNLOCK(&devices);
  783. if (!pvt || !pvt->connected || pvt->owner) {
  784. ast_log(LOG_WARNING, "Request to call on device %s which is not connected / already in use.\n", dest_dev);
  785. *cause = AST_CAUSE_REQUESTED_CHAN_UNAVAIL;
  786. return NULL;
  787. }
  788. if ((pvt->type == MBL_TYPE_PHONE) && !dest_num) {
  789. ast_log(LOG_WARNING, "Can't determine destination number.\n");
  790. *cause = AST_CAUSE_INCOMPATIBLE_DESTINATION;
  791. return NULL;
  792. }
  793. ast_mutex_lock(&pvt->lock);
  794. chn = mbl_new(AST_STATE_DOWN, pvt, NULL, assignedids, requestor);
  795. ast_mutex_unlock(&pvt->lock);
  796. if (!chn) {
  797. ast_log(LOG_WARNING, "Unable to allocate channel structure.\n");
  798. *cause = AST_CAUSE_REQUESTED_CHAN_UNAVAIL;
  799. return NULL;
  800. }
  801. return chn;
  802. }
  803. static int mbl_call(struct ast_channel *ast, const char *dest, int timeout)
  804. {
  805. struct mbl_pvt *pvt;
  806. char *dest_dev;
  807. char *dest_num = NULL;
  808. dest_dev = ast_strdupa(dest);
  809. pvt = ast_channel_tech_pvt(ast);
  810. if (pvt->type == MBL_TYPE_PHONE) {
  811. dest_num = strchr(dest_dev, '/');
  812. if (!dest_num) {
  813. ast_log(LOG_WARNING, "Cant determine destination number.\n");
  814. return -1;
  815. }
  816. *dest_num++ = 0x00;
  817. }
  818. if ((ast_channel_state(ast) != AST_STATE_DOWN) && (ast_channel_state(ast) != AST_STATE_RESERVED)) {
  819. ast_log(LOG_WARNING, "mbl_call called on %s, neither down nor reserved\n", ast_channel_name(ast));
  820. return -1;
  821. }
  822. ast_debug(1, "Calling %s on %s\n", dest, ast_channel_name(ast));
  823. ast_mutex_lock(&pvt->lock);
  824. if (pvt->type == MBL_TYPE_PHONE) {
  825. if (hfp_send_atd(pvt->hfp, dest_num)) {
  826. ast_mutex_unlock(&pvt->lock);
  827. ast_log(LOG_ERROR, "error sending ATD command on %s\n", pvt->id);
  828. return -1;
  829. }
  830. pvt->hangupcause = 0;
  831. pvt->needchup = 1;
  832. msg_queue_push(pvt, AT_OK, AT_D);
  833. } else {
  834. if (hsp_send_ring(pvt->rfcomm_socket)) {
  835. ast_log(LOG_ERROR, "[%s] error ringing device\n", pvt->id);
  836. ast_mutex_unlock(&pvt->lock);
  837. return -1;
  838. }
  839. if ((pvt->ring_sched_id = ast_sched_add(pvt->sched, 6000, headset_send_ring, pvt)) == -1) {
  840. ast_log(LOG_ERROR, "[%s] error ringing device\n", pvt->id);
  841. ast_mutex_unlock(&pvt->lock);
  842. return -1;
  843. }
  844. pvt->outgoing = 1;
  845. pvt->needring = 1;
  846. }
  847. ast_mutex_unlock(&pvt->lock);
  848. return 0;
  849. }
  850. static int mbl_hangup(struct ast_channel *ast)
  851. {
  852. struct mbl_pvt *pvt;
  853. if (!ast_channel_tech_pvt(ast)) {
  854. ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
  855. return 0;
  856. }
  857. pvt = ast_channel_tech_pvt(ast);
  858. ast_debug(1, "[%s] hanging up device\n", pvt->id);
  859. ast_mutex_lock(&pvt->lock);
  860. ast_channel_set_fd(ast, 0, -1);
  861. close(pvt->sco_socket);
  862. pvt->sco_socket = -1;
  863. if (pvt->needchup) {
  864. hfp_send_chup(pvt->hfp);
  865. msg_queue_push(pvt, AT_OK, AT_CHUP);
  866. pvt->needchup = 0;
  867. }
  868. pvt->outgoing = 0;
  869. pvt->incoming = 0;
  870. pvt->needring = 0;
  871. pvt->owner = NULL;
  872. ast_channel_tech_pvt_set(ast, NULL);
  873. ast_mutex_unlock(&pvt->lock);
  874. ast_setstate(ast, AST_STATE_DOWN);
  875. return 0;
  876. }
  877. static int mbl_answer(struct ast_channel *ast)
  878. {
  879. struct mbl_pvt *pvt;
  880. pvt = ast_channel_tech_pvt(ast);
  881. if (pvt->type == MBL_TYPE_HEADSET)
  882. return 0;
  883. ast_mutex_lock(&pvt->lock);
  884. if (pvt->incoming) {
  885. hfp_send_ata(pvt->hfp);
  886. msg_queue_push(pvt, AT_OK, AT_A);
  887. pvt->answered = 1;
  888. }
  889. ast_mutex_unlock(&pvt->lock);
  890. return 0;
  891. }
  892. static int mbl_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
  893. {
  894. struct mbl_pvt *pvt = ast_channel_tech_pvt(ast);
  895. if (pvt->type == MBL_TYPE_HEADSET)
  896. return 0;
  897. ast_mutex_lock(&pvt->lock);
  898. if (hfp_send_dtmf(pvt->hfp, digit)) {
  899. ast_mutex_unlock(&pvt->lock);
  900. ast_debug(1, "[%s] error sending digit %c\n", pvt->id, digit);
  901. return -1;
  902. }
  903. msg_queue_push(pvt, AT_OK, AT_VTS);
  904. ast_mutex_unlock(&pvt->lock);
  905. ast_debug(1, "[%s] dialed %c\n", pvt->id, digit);
  906. return 0;
  907. }
  908. static struct ast_frame *mbl_read(struct ast_channel *ast)
  909. {
  910. struct mbl_pvt *pvt = ast_channel_tech_pvt(ast);
  911. struct ast_frame *fr = &ast_null_frame;
  912. int r;
  913. ast_debug(3, "*** mbl_read()\n");
  914. while (ast_mutex_trylock(&pvt->lock)) {
  915. CHANNEL_DEADLOCK_AVOIDANCE(ast);
  916. }
  917. if (!pvt->owner || pvt->sco_socket == -1) {
  918. goto e_return;
  919. }
  920. memset(&pvt->fr, 0x00, sizeof(struct ast_frame));
  921. pvt->fr.frametype = AST_FRAME_VOICE;
  922. ast_format_set(&pvt->fr.subclass.format, DEVICE_FRAME_FORMAT, 0);
  923. pvt->fr.src = "Mobile";
  924. pvt->fr.offset = AST_FRIENDLY_OFFSET;
  925. pvt->fr.mallocd = 0;
  926. pvt->fr.delivery.tv_sec = 0;
  927. pvt->fr.delivery.tv_usec = 0;
  928. pvt->fr.data.ptr = pvt->io_buf + AST_FRIENDLY_OFFSET;
  929. if ((r = read(pvt->sco_socket, pvt->fr.data.ptr, DEVICE_FRAME_SIZE)) == -1) {
  930. if (errno != EAGAIN && errno != EINTR) {
  931. ast_debug(1, "[%s] read error %d, going to wait for new connection\n", pvt->id, errno);
  932. close(pvt->sco_socket);
  933. pvt->sco_socket = -1;
  934. ast_channel_set_fd(ast, 0, -1);
  935. }
  936. goto e_return;
  937. }
  938. pvt->fr.datalen = r;
  939. pvt->fr.samples = r / 2;
  940. if (pvt->do_alignment_detection)
  941. do_alignment_detection(pvt, pvt->fr.data.ptr, r);
  942. fr = ast_dsp_process(ast, pvt->dsp, &pvt->fr);
  943. ast_mutex_unlock(&pvt->lock);
  944. return fr;
  945. e_return:
  946. ast_mutex_unlock(&pvt->lock);
  947. return fr;
  948. }
  949. static int mbl_write(struct ast_channel *ast, struct ast_frame *frame)
  950. {
  951. struct mbl_pvt *pvt = ast_channel_tech_pvt(ast);
  952. struct ast_frame *f;
  953. ast_debug(3, "*** mbl_write\n");
  954. if (frame->frametype != AST_FRAME_VOICE) {
  955. return 0;
  956. }
  957. while (ast_mutex_trylock(&pvt->lock)) {
  958. CHANNEL_DEADLOCK_AVOIDANCE(ast);
  959. }
  960. ast_smoother_feed(pvt->smoother, frame);
  961. while ((f = ast_smoother_read(pvt->smoother))) {
  962. sco_write(pvt->sco_socket, f->data.ptr, f->datalen);
  963. }
  964. ast_mutex_unlock(&pvt->lock);
  965. return 0;
  966. }
  967. static int mbl_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  968. {
  969. struct mbl_pvt *pvt = ast_channel_tech_pvt(newchan);
  970. if (!pvt) {
  971. ast_debug(1, "fixup failed, no pvt on newchan\n");
  972. return -1;
  973. }
  974. ast_mutex_lock(&pvt->lock);
  975. if (pvt->owner == oldchan)
  976. pvt->owner = newchan;
  977. ast_mutex_unlock(&pvt->lock);
  978. return 0;
  979. }
  980. static int mbl_devicestate(const char *data)
  981. {
  982. char *device;
  983. int res = AST_DEVICE_INVALID;
  984. struct mbl_pvt *pvt;
  985. device = ast_strdupa(S_OR(data, ""));
  986. ast_debug(1, "Checking device state for device %s\n", device);
  987. AST_RWLIST_RDLOCK(&devices);
  988. AST_RWLIST_TRAVERSE(&devices, pvt, entry) {
  989. if (!strcmp(pvt->id, device))
  990. break;
  991. }
  992. AST_RWLIST_UNLOCK(&devices);
  993. if (!pvt)
  994. return res;
  995. ast_mutex_lock(&pvt->lock);
  996. if (pvt->connected) {
  997. if (pvt->owner)
  998. res = AST_DEVICE_INUSE;
  999. else
  1000. res = AST_DEVICE_NOT_INUSE;
  1001. if (!mbl_has_service(pvt))
  1002. res = AST_DEVICE_UNAVAILABLE;
  1003. }
  1004. ast_mutex_unlock(&pvt->lock);
  1005. return res;
  1006. }
  1007. /*
  1008. Callback helpers
  1009. */
  1010. /*
  1011. do_alignment_detection()
  1012. This routine attempts to detect where we get misaligned sco audio data from the bluetooth adaptor.
  1013. Its enabled by alignmentdetect=yes under the adapter entry in mobile.conf
  1014. Some adapters suffer a problem where occasionally they will byte shift the audio stream one byte to the right.
  1015. The result is static or white noise on the inbound (from the adapter) leg of the call.
  1016. This is characterised by a sudden jump in magnitude of the value of the 16 bit samples.
  1017. Here we look at the first 4 48 byte frames. We average the absolute values of each sample in the frame,
  1018. then average the sum of the averages of frames 1, 2, and 3.
  1019. Frame zero is usually zero.
  1020. If the end result > 100, and it usually is if we have the problem, set a flag and compensate by shifting the bytes
  1021. for each subsequent frame during the call.
  1022. If the result is <= 100 then clear the flag so we don't come back in here...
  1023. This seems to work OK....
  1024. */
  1025. static void do_alignment_detection(struct mbl_pvt *pvt, char *buf, int buflen)
  1026. {
  1027. int i;
  1028. short a, *s;
  1029. char *p;
  1030. if (pvt->alignment_detection_triggered) {
  1031. for (i=buflen, p=buf+buflen-1; i>0; i--, p--)
  1032. *p = *(p-1);
  1033. *(p+1) = 0;
  1034. return;
  1035. }
  1036. if (pvt->alignment_count < 4) {
  1037. s = (short *)buf;
  1038. for (i=0, a=0; i<buflen/2; i++) {
  1039. a += *s++;
  1040. a /= i+1;
  1041. }
  1042. pvt->alignment_samples[pvt->alignment_count++] = a;
  1043. return;
  1044. }
  1045. ast_debug(1, "Alignment Detection result is [%-d %-d %-d %-d]\n", pvt->alignment_samples[0], pvt->alignment_samples[1], pvt->alignment_samples[2], pvt->alignment_samples[3]);
  1046. a = abs(pvt->alignment_samples[1]) + abs(pvt->alignment_samples[2]) + abs(pvt->alignment_samples[3]);
  1047. a /= 3;
  1048. if (a > 100) {
  1049. pvt->alignment_detection_triggered = 1;
  1050. ast_debug(1, "Alignment Detection Triggered.\n");
  1051. } else
  1052. pvt->do_alignment_detection = 0;
  1053. }
  1054. static int mbl_queue_control(struct mbl_pvt *pvt, enum ast_control_frame_type control)
  1055. {
  1056. for (;;) {
  1057. if (pvt->owner) {
  1058. if (ast_channel_trylock(pvt->owner)) {
  1059. DEADLOCK_AVOIDANCE(&pvt->lock);
  1060. } else {
  1061. ast_queue_control(pvt->owner, control);
  1062. ast_channel_unlock(pvt->owner);
  1063. break;
  1064. }
  1065. } else
  1066. break;
  1067. }
  1068. return 0;
  1069. }
  1070. static int mbl_queue_hangup(struct mbl_pvt *pvt)
  1071. {
  1072. for (;;) {
  1073. if (pvt->owner) {
  1074. if (ast_channel_trylock(pvt->owner)) {
  1075. DEADLOCK_AVOIDANCE(&pvt->lock);
  1076. } else {
  1077. if (pvt->hangupcause != 0) {
  1078. ast_channel_hangupcause_set(pvt->owner, pvt->hangupcause);
  1079. }
  1080. ast_queue_hangup(pvt->owner);
  1081. ast_channel_unlock(pvt->owner);
  1082. break;
  1083. }
  1084. } else
  1085. break;
  1086. }
  1087. return 0;
  1088. }
  1089. static int mbl_ast_hangup(struct mbl_pvt *pvt)
  1090. {
  1091. ast_hangup(pvt->owner);
  1092. return 0;
  1093. }
  1094. /*!
  1095. * \brief Check if a mobile device has service.
  1096. * \param pvt a mbl_pvt struct
  1097. * \retval 1 this device has service
  1098. * \retval 0 no service
  1099. *
  1100. * \note This function will always indicate that service is available if the
  1101. * given device does not support service indication.
  1102. */
  1103. static int mbl_has_service(struct mbl_pvt *pvt)
  1104. {
  1105. if (pvt->type != MBL_TYPE_PHONE)
  1106. return 1;
  1107. if (!pvt->hfp->cind_map.service)
  1108. return 1;
  1109. if (pvt->hfp->cind_state[pvt->hfp->cind_map.service] == HFP_CIND_SERVICE_AVAILABLE)
  1110. return 1;
  1111. return 0;
  1112. }
  1113. /*
  1114. rfcomm helpers
  1115. */
  1116. static int rfcomm_connect(bdaddr_t src, bdaddr_t dst, int remote_channel)
  1117. {
  1118. struct sockaddr_rc addr;
  1119. int s;
  1120. if ((s = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) < 0) {
  1121. ast_debug(1, "socket() failed (%d).\n", errno);
  1122. return -1;
  1123. }
  1124. memset(&addr, 0, sizeof(addr));
  1125. addr.rc_family = AF_BLUETOOTH;
  1126. bacpy(&addr.rc_bdaddr, &src);
  1127. addr.rc_channel = (uint8_t) 0;
  1128. if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  1129. ast_debug(1, "bind() failed (%d).\n", errno);
  1130. close(s);
  1131. return -1;
  1132. }
  1133. memset(&addr, 0, sizeof(addr));
  1134. addr.rc_family = AF_BLUETOOTH;
  1135. bacpy(&addr.rc_bdaddr, &dst);
  1136. addr.rc_channel = remote_channel;
  1137. if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  1138. ast_debug(1, "connect() failed (%d).\n", errno);
  1139. close(s);
  1140. return -1;
  1141. }
  1142. return s;
  1143. }
  1144. /*!
  1145. * \brief Write to an rfcomm socket.
  1146. * \param rsock the socket to write to
  1147. * \param buf the null terminated buffer to write
  1148. *
  1149. * This function will write characters from buf. The buffer must be null
  1150. * terminated.
  1151. *
  1152. * \retval -1 error
  1153. * \retval 0 success
  1154. */
  1155. static int rfcomm_write(int rsock, char *buf)
  1156. {
  1157. return rfcomm_write_full(rsock, buf, strlen(buf));
  1158. }
  1159. /*!
  1160. * \brief Write to an rfcomm socket.
  1161. * \param rsock the socket to write to
  1162. * \param buf the buffer to write
  1163. * \param count the number of characters from the buffer to write
  1164. *
  1165. * This function will write count characters from buf. It will always write
  1166. * count chars unless it encounters an error.
  1167. *
  1168. * \retval -1 error
  1169. * \retval 0 success
  1170. */
  1171. static int rfcomm_write_full(int rsock, char *buf, size_t count)
  1172. {
  1173. char *p = buf;
  1174. ssize_t out_count;
  1175. ast_debug(1, "rfcomm_write() (%d) [%.*s]\n", rsock, (int) count, buf);
  1176. while (count > 0) {
  1177. if ((out_count = write(rsock, p, count)) == -1) {
  1178. ast_debug(1, "rfcomm_write() error [%d]\n", errno);
  1179. return -1;
  1180. }
  1181. count -= out_count;
  1182. p += out_count;
  1183. }
  1184. return 0;
  1185. }
  1186. /*!
  1187. * \brief Wait for activity on an rfcomm socket.
  1188. * \param rsock the socket to watch
  1189. * \param ms a pointer to an int containing a timeout in ms
  1190. * \return zero on timeout and the socket fd (non-zero) otherwise
  1191. * \retval 0 timeout
  1192. */
  1193. static int rfcomm_wait(int rsock, int *ms)
  1194. {
  1195. int exception, outfd;
  1196. outfd = ast_waitfor_n_fd(&rsock, 1, ms, &exception);
  1197. if (outfd < 0)
  1198. outfd = 0;
  1199. return outfd;
  1200. }
  1201. #ifdef RFCOMM_READ_DEBUG
  1202. #define rfcomm_read_debug(c) __rfcomm_read_debug(c)
  1203. static void __rfcomm_read_debug(char c)
  1204. {
  1205. if (c == '\r')
  1206. ast_debug(2, "rfcomm_read: \\r\n");
  1207. else if (c == '\n')
  1208. ast_debug(2, "rfcomm_read: \\n\n");
  1209. else
  1210. ast_debug(2, "rfcomm_read: %c\n", c);
  1211. }
  1212. #else
  1213. #define rfcomm_read_debug(c)
  1214. #endif
  1215. /*!
  1216. * \brief Append the given character to the given buffer and increase the
  1217. * in_count.
  1218. */
  1219. static void inline rfcomm_append_buf(char **buf, size_t count, size_t *in_count, char c)
  1220. {
  1221. if (*in_count < count) {
  1222. (*in_count)++;
  1223. *(*buf)++ = c;
  1224. }
  1225. }
  1226. /*!
  1227. * \brief Read a character from the given stream and check if it matches what
  1228. * we expected.
  1229. */
  1230. static int rfcomm_read_and_expect_char(int rsock, char *result, char expected)
  1231. {
  1232. int res;
  1233. char c;
  1234. if (!result)
  1235. result = &c;
  1236. if ((res = read(rsock, result, 1)) < 1) {
  1237. return res;
  1238. }
  1239. rfcomm_read_debug(*result);
  1240. if (*result != expected) {
  1241. return -2;
  1242. }
  1243. return 1;
  1244. }
  1245. /*!
  1246. * \brief Read a character from the given stream and append it to the given
  1247. * buffer if it matches the expected character.
  1248. */
  1249. static int rfcomm_read_and_append_char(int rsock, char **buf, size_t count, size_t *in_count, char *result, char expected)
  1250. {
  1251. int res;
  1252. char c;
  1253. if (!result)
  1254. result = &c;
  1255. if ((res = rfcomm_read_and_expect_char(rsock, result, expected)) < 1) {
  1256. return res;
  1257. }
  1258. rfcomm_append_buf(buf, count, in_count, *result);
  1259. return 1;
  1260. }
  1261. /*!
  1262. * \brief Read until \verbatim '\r\n'. \endverbatim
  1263. * This function consumes the \verbatim'\r\n'\endverbatim but does not add it to buf.
  1264. */
  1265. static int rfcomm_read_until_crlf(int rsock, char **buf, size_t count, size_t *in_count)
  1266. {
  1267. int res;
  1268. char c;
  1269. while ((res = read(rsock, &c, 1)) == 1) {
  1270. rfcomm_read_debug(c);
  1271. if (c == '\r') {
  1272. if ((res = rfcomm_read_and_expect_char(rsock, &c, '\n')) == 1) {
  1273. break;
  1274. } else if (res == -2) {
  1275. rfcomm_append_buf(buf, count, in_count, '\r');
  1276. } else {
  1277. rfcomm_append_buf(buf, count, in_count, '\r');
  1278. break;
  1279. }
  1280. }
  1281. rfcomm_append_buf(buf, count, in_count, c);
  1282. }
  1283. return res;
  1284. }
  1285. /*!
  1286. * \brief Read the remainder of an AT SMS prompt.
  1287. * \note the entire parsed string is \verbatim '\r\n> ' \endverbatim
  1288. *
  1289. * By the time this function is executed, only a ' ' is left to read.
  1290. */
  1291. static int rfcomm_read_sms_prompt(int rsock, char **buf, size_t count, size_t *in_count)
  1292. {
  1293. int res;
  1294. if ((res = rfcomm_read_and_append_char(rsock, buf, count, in_count, NULL, ' ')) < 1)
  1295. goto e_return;
  1296. return 1;
  1297. e_return:
  1298. ast_log(LOG_ERROR, "error parsing SMS prompt on rfcomm socket\n");
  1299. return res;
  1300. }
  1301. /*!
  1302. * \brief Read until a \verbatim \r\nOK\r\n \endverbatim message.
  1303. */
  1304. static int rfcomm_read_until_ok(int rsock, char **buf, size_t count, size_t *in_count)
  1305. {
  1306. int res;
  1307. char c;
  1308. /* here, we read until finding a \r\n, then we read one character at a
  1309. * time looking for the string '\r\nOK\r\n'. If we only find a partial
  1310. * match, we place that in the buffer and try again. */
  1311. for (;;) {
  1312. if ((res = rfcomm_read_until_crlf(rsock, buf, count, in_count)) != 1) {
  1313. break;
  1314. }
  1315. rfcomm_append_buf(buf, count, in_count, '\r');
  1316. rfcomm_append_buf(buf, count, in_count, '\n');
  1317. if ((res = rfcomm_read_and_expect_char(rsock, &c, '\r')) != 1) {
  1318. if (res != -2) {
  1319. break;
  1320. }
  1321. rfcomm_append_buf(buf, count, in_count, c);
  1322. continue;
  1323. }
  1324. if ((res = rfcomm_read_and_expect_char(rsock, &c, '\n')) != 1) {
  1325. if (res != -2) {
  1326. break;
  1327. }
  1328. rfcomm_append_buf(buf, count, in_count, '\r');
  1329. rfcomm_append_buf(buf, count, in_count, c);
  1330. continue;
  1331. }
  1332. if ((res = rfcomm_read_and_expect_char(rsock, &c, 'O')) != 1) {
  1333. if (res != -2) {
  1334. break;
  1335. }
  1336. rfcomm_append_buf(buf, count, in_count, '\r');
  1337. rfcomm_append_buf(buf, count, in_count, '\n');
  1338. rfcomm_append_buf(buf, count, in_count, c);
  1339. continue;
  1340. }
  1341. if ((res = rfcomm_read_and_expect_char(rsock, &c, 'K')) != 1) {
  1342. if (res != -2) {
  1343. break;
  1344. }
  1345. rfcomm_append_buf(buf, count, in_count, '\r');
  1346. rfcomm_append_buf(buf, count, in_count, '\n');
  1347. rfcomm_append_buf(buf, count, in_count, 'O');
  1348. rfcomm_append_buf(buf, count, in_count, c);
  1349. continue;
  1350. }
  1351. if ((res = rfcomm_read_and_expect_char(rsock, &c, '\r')) != 1) {
  1352. if (res != -2) {
  1353. break;
  1354. }
  1355. rfcomm_append_buf(buf, count, in_count, '\r');
  1356. rfcomm_append_buf(buf, count, in_count, '\n');
  1357. rfcomm_append_buf(buf, count, in_count, 'O');
  1358. rfcomm_append_buf(buf, count, in_count, 'K');
  1359. rfcomm_append_buf(buf, count, in_count, c);
  1360. continue;
  1361. }
  1362. if ((res = rfcomm_read_and_expect_char(rsock, &c, '\n')) != 1) {
  1363. if (res != -2) {
  1364. break;
  1365. }
  1366. rfcomm_append_buf(buf, count, in_count, '\r');
  1367. rfcomm_append_buf(buf, count, in_count, '\n');
  1368. rfcomm_append_buf(buf, count, in_count, 'O');
  1369. rfcomm_append_buf(buf, count, in_count, 'K');
  1370. rfcomm_append_buf(buf, count, in_count, '\r');
  1371. rfcomm_append_buf(buf, count, in_count, c);
  1372. continue;
  1373. }
  1374. /* we have successfully parsed a '\r\nOK\r\n' string */
  1375. return 1;
  1376. }
  1377. return res;
  1378. }
  1379. /*!
  1380. * \brief Read the remainder of a +CMGR message.
  1381. * \note the entire parsed string is \verbatim '+CMGR: ...\r\n...\r\n...\r\n...\r\nOK\r\n' \endverbatim
  1382. */
  1383. static int rfcomm_read_cmgr(int rsock, char **buf, size_t count, size_t *in_count)
  1384. {
  1385. int res;
  1386. /* append the \r\n that was stripped by the calling function */
  1387. rfcomm_append_buf(buf, count, in_count, '\r');
  1388. rfcomm_append_buf(buf, count, in_count, '\n');
  1389. if ((res = rfcomm_read_until_ok(rsock, buf, count, in_count)) != 1) {
  1390. ast_log(LOG_ERROR, "error reading +CMGR message on rfcomm socket\n");
  1391. }
  1392. return res;
  1393. }
  1394. /*!
  1395. * \brief Read and AT result code.
  1396. * \note the entire parsed string is \verbatim '\r\n<result code>\r\n' \endverbatim
  1397. */
  1398. static int rfcomm_read_result(int rsock, char **buf, size_t count, size_t *in_count)
  1399. {
  1400. int res;
  1401. char c;
  1402. if ((res = rfcomm_read_and_expect_char(rsock, &c, '\n')) < 1) {
  1403. goto e_return;
  1404. }
  1405. if ((res = rfcomm_read_and_append_char(rsock, buf, count, in_count, &c, '>')) == 1) {
  1406. return rfcomm_read_sms_prompt(rsock, buf, count, in_count);
  1407. } else if (res != -2) {
  1408. goto e_return;
  1409. }
  1410. rfcomm_append_buf(buf, count, in_count, c);
  1411. res = rfcomm_read_until_crlf(rsock, buf, count, in_count);
  1412. if (res != 1)
  1413. return res;
  1414. /* check for CMGR, which contains an embedded \r\n pairs terminated by
  1415. * an \r\nOK\r\n message */
  1416. if (*in_count >= 5 && !strncmp(*buf - *in_count, "+CMGR", 5)) {
  1417. return rfcomm_read_cmgr(rsock, buf, count, in_count);
  1418. }
  1419. return 1;
  1420. e_return:
  1421. ast_log(LOG_ERROR, "error parsing AT result on rfcomm socket\n");
  1422. return res;
  1423. }
  1424. /*!
  1425. * \brief Read the remainder of an AT command.
  1426. * \note the entire parsed string is \verbatim '<at command>\r' \endverbatim
  1427. */
  1428. static int rfcomm_read_command(int rsock, char **buf, size_t count, size_t *in_count)
  1429. {
  1430. int res;
  1431. char c;
  1432. while ((res = read(rsock, &c, 1)) == 1) {
  1433. rfcomm_read_debug(c);
  1434. /* stop when we get to '\r' */
  1435. if (c == '\r')
  1436. break;
  1437. rfcomm_append_buf(buf, count, in_count, c);
  1438. }
  1439. return res;
  1440. }
  1441. /*!
  1442. * \brief Read one Hayes AT message from an rfcomm socket.
  1443. * \param rsock the rfcomm socket to read from
  1444. * \param buf the buffer to store the result in
  1445. * \param count the size of the buffer or the maximum number of characters to read
  1446. *
  1447. * Here we need to read complete Hayes AT messages. The AT message formats we
  1448. * support are listed below.
  1449. *
  1450. * \verbatim
  1451. * \r\n<result code>\r\n
  1452. * <at command>\r
  1453. * \r\n>
  1454. * \endverbatim
  1455. *
  1456. * These formats correspond to AT result codes, AT commands, and the AT SMS
  1457. * prompt respectively. When messages are read the leading and trailing \verbatim '\r' \endverbatim
  1458. * and \verbatim '\n' \endverbatim characters are discarded. If the given buffer is not large enough
  1459. * to hold the response, what does not fit in the buffer will be dropped.
  1460. *
  1461. * \note The rfcomm connection to the device is asynchronous, so there is no
  1462. * guarantee that responses will be returned in a single read() call. We handle
  1463. * this by blocking until we can read an entire response.
  1464. *
  1465. * \retval 0 end of file
  1466. * \retval -1 read error
  1467. * \retval -2 parse error
  1468. * \retval other the number of characters added to buf
  1469. */
  1470. static ssize_t rfcomm_read(int rsock, char *buf, size_t count)
  1471. {
  1472. ssize_t res;
  1473. size_t in_count = 0;
  1474. char c;
  1475. if ((res = rfcomm_read_and_expect_char(rsock, &c, '\r')) == 1) {
  1476. res = rfcomm_read_result(rsock, &buf, count, &in_count);
  1477. } else if (res == -2) {
  1478. rfcomm_append_buf(&buf, count, &in_count, c);
  1479. res = rfcomm_read_command(rsock, &buf, count, &in_count);
  1480. }
  1481. if (res < 1)
  1482. return res;
  1483. else
  1484. return in_count;
  1485. }
  1486. /*
  1487. sco helpers and callbacks
  1488. */
  1489. static int sco_connect(bdaddr_t src, bdaddr_t dst)
  1490. {
  1491. struct sockaddr_sco addr;
  1492. int s;
  1493. if ((s = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO)) < 0) {
  1494. ast_debug(1, "socket() failed (%d).\n", errno);
  1495. return -1;
  1496. }
  1497. /* XXX this does not work with the do_sco_listen() thread (which also bind()s
  1498. * to this address). Also I am not sure if it is necessary. */
  1499. #if 0
  1500. memset(&addr, 0, sizeof(addr));
  1501. addr.sco_family = AF_BLUETOOTH;
  1502. bacpy(&addr.sco_bdaddr, &src);
  1503. if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  1504. ast_debug(1, "bind() failed (%d).\n", errno);
  1505. close(s);
  1506. return -1;
  1507. }
  1508. #endif
  1509. memset(&addr, 0, sizeof(addr));
  1510. addr.sco_family = AF_BLUETOOTH;
  1511. bacpy(&addr.sco_bdaddr, &dst);
  1512. if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  1513. ast_debug(1, "sco connect() failed (%d).\n", errno);
  1514. close(s);
  1515. return -1;
  1516. }
  1517. return s;
  1518. }
  1519. static int sco_write(int s, char *buf, int len)
  1520. {
  1521. int r;
  1522. if (s == -1) {
  1523. ast_debug(3, "sco_write() not ready\n");
  1524. return 0;
  1525. }
  1526. ast_debug(3, "sco_write()\n");
  1527. r = write(s, buf, len);
  1528. if (r == -1) {
  1529. ast_debug(3, "sco write error %d\n", errno);
  1530. return 0;
  1531. }
  1532. return 1;
  1533. }
  1534. /*!
  1535. * \brief Accept SCO connections.
  1536. * This function is an ast_io callback function used to accept incoming sco
  1537. * audio connections.
  1538. */
  1539. static int sco_accept(int *id, int fd, short events, void *data)
  1540. {
  1541. struct adapter_pvt *adapter = (struct adapter_pvt *) data;
  1542. struct sockaddr_sco addr;
  1543. socklen_t addrlen;
  1544. struct mbl_pvt *pvt;
  1545. socklen_t len;
  1546. char saddr[18];
  1547. struct sco_options so;
  1548. int sock;
  1549. addrlen = sizeof(struct sockaddr_sco);
  1550. if ((sock = accept(fd, (struct sockaddr *)&addr, &addrlen)) == -1) {
  1551. ast_log(LOG_ERROR, "error accepting audio connection on adapter %s\n", adapter->id);
  1552. return 0;
  1553. }
  1554. len = sizeof(so);
  1555. getsockopt(sock, SOL_SCO, SCO_OPTIONS, &so, &len);
  1556. ba2str(&addr.sco_bdaddr, saddr);
  1557. ast_debug(1, "Incoming Audio Connection from device %s MTU is %d\n", saddr, so.mtu);
  1558. /* figure out which device this sco connection belongs to */
  1559. pvt = NULL;
  1560. AST_RWLIST_RDLOCK(&devices);
  1561. AST_RWLIST_TRAVERSE(&devices, pvt, entry) {
  1562. if (!bacmp(&pvt->addr, &addr.sco_bdaddr))
  1563. break;
  1564. }
  1565. AST_RWLIST_UNLOCK(&devices);
  1566. if (!pvt) {
  1567. ast_log(LOG_WARNING, "could not find device for incoming audio connection\n");
  1568. close(sock);
  1569. return 1;
  1570. }
  1571. ast_mutex_lock(&pvt->lock);
  1572. if (pvt->sco_socket != -1) {
  1573. close(pvt->sco_socket);
  1574. pvt->sco_socket = -1;
  1575. }
  1576. pvt->sco_socket = sock;
  1577. if (pvt->owner) {
  1578. ast_channel_set_fd(pvt->owner, 0, sock);
  1579. } else {
  1580. ast_debug(1, "incoming audio connection for pvt without owner\n");
  1581. }
  1582. ast_mutex_unlock(&pvt->lock);
  1583. return 1;
  1584. }
  1585. /*!
  1586. * \brief Bind an SCO listener socket for the given adapter.
  1587. * \param adapter an adapter_pvt
  1588. * \return -1 on error, non zero on success
  1589. */
  1590. static int sco_bind(struct adapter_pvt *adapter)
  1591. {
  1592. struct sockaddr_sco addr;
  1593. int opt = 1;
  1594. if ((adapter->sco_socket = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO)) < 0) {
  1595. ast_log(LOG_ERROR, "Unable to create sco listener socket for adapter %s.\n", adapter->id);
  1596. goto e_return;
  1597. }
  1598. memset(&addr, 0, sizeof(addr));
  1599. addr.sco_family = AF_BLUETOOTH;
  1600. bacpy(&addr.sco_bdaddr, &adapter->addr);
  1601. if (bind(adapter->sco_socket, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  1602. ast_log(LOG_ERROR, "Unable to bind sco listener socket. (%d)\n", errno);
  1603. goto e_close_socket;
  1604. }
  1605. if (setsockopt(adapter->sco_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1) {
  1606. ast_log(LOG_ERROR, "Unable to setsockopt sco listener socket.\n");
  1607. goto e_close_socket;
  1608. }
  1609. if (listen(adapter->sco_socket, 5) < 0) {
  1610. ast_log(LOG_ERROR, "Unable to listen sco listener socket.\n");
  1611. goto e_close_socket;
  1612. }
  1613. return adapter->sco_socket;
  1614. e_close_socket:
  1615. close(adapter->sco_socket);
  1616. adapter->sco_socket = -1;
  1617. e_return:
  1618. return -1;
  1619. }
  1620. /*
  1621. * Hayes AT command helpers.
  1622. */
  1623. /*!
  1624. * \brief Match the given buffer with the given prefix.
  1625. * \param buf the buffer to match
  1626. * \param prefix the prefix to match
  1627. */
  1628. static int at_match_prefix(char *buf, char *prefix)
  1629. {
  1630. return !strncmp(buf, prefix, strlen(prefix));
  1631. }
  1632. /*!
  1633. * \brief Read an AT message and clasify it.
  1634. * \param rsock an rfcomm socket
  1635. * \param buf the buffer to store the result in
  1636. * \param count the size of the buffer or the maximum number of characters to read
  1637. * \return the type of message received, in addition buf will contain the
  1638. * message received and will be null terminated
  1639. * \see at_read()
  1640. */
  1641. static at_message_t at_read_full(int rsock, char *buf, size_t count)
  1642. {
  1643. ssize_t s;
  1644. if ((s = rfcomm_read(rsock, buf, count - 1)) < 1)
  1645. return s;
  1646. buf[s] = '\0';
  1647. if (!strcmp("OK", buf)) {
  1648. return AT_OK;
  1649. } else if (!strcmp("ERROR", buf)) {
  1650. return AT_ERROR;
  1651. } else if (!strcmp("RING", buf)) {
  1652. return AT_RING;
  1653. } else if (!strcmp("AT+CKPD=200", buf)) {
  1654. return AT_CKPD;
  1655. } else if (!strcmp("> ", buf)) {
  1656. return AT_SMS_PROMPT;
  1657. } else if (at_match_prefix(buf, "+CMTI:")) {
  1658. return AT_CMTI;
  1659. } else if (at_match_prefix(buf, "+CIEV:")) {
  1660. return AT_CIEV;
  1661. } else if (at_match_prefix(buf, "+BRSF:")) {
  1662. return AT_BRSF;
  1663. } else if (at_match_prefix(buf, "+CIND:")) {
  1664. return AT_CIND;
  1665. } else if (at_match_prefix(buf, "+CLIP:")) {
  1666. return AT_CLIP;
  1667. } else if (at_match_prefix(buf, "+CMGR:")) {
  1668. return AT_CMGR;
  1669. } else if (at_match_prefix(buf, "+VGM:")) {
  1670. return AT_VGM;
  1671. } else if (at_match_prefix(buf, "+VGS:")) {
  1672. return AT_VGS;
  1673. } else if (at_match_prefix(buf, "+CMS ERROR:")) {
  1674. return AT_CMS_ERROR;
  1675. } else if (at_match_prefix(buf, "AT+VGM=")) {
  1676. return AT_VGM;
  1677. } else if (at_match_prefix(buf, "AT+VGS=")) {
  1678. return AT_VGS;
  1679. } else if (at_match_prefix(buf, "+CUSD:")) {
  1680. return AT_CUSD;
  1681. } else if (at_match_prefix(buf, "BUSY")) {
  1682. return AT_BUSY;
  1683. } else if (at_match_prefix(buf, "NO DIALTONE")) {
  1684. return AT_NO_DIALTONE;
  1685. } else if (at_match_prefix(buf, "NO CARRIER")) {
  1686. return AT_NO_CARRIER;
  1687. } else if (at_match_prefix(buf, "*ECAV:")) {
  1688. return AT_ECAM;
  1689. } else {
  1690. return AT_UNKNOWN;
  1691. }
  1692. }
  1693. /*!
  1694. * \brief Get the string representation of the given AT message.
  1695. * \param msg the message to process
  1696. * \return a string describing the given message
  1697. */
  1698. static inline const char *at_msg2str(at_message_t msg)
  1699. {
  1700. switch (msg) {
  1701. /* errors */
  1702. case AT_PARSE_ERROR:
  1703. return "PARSE ERROR";
  1704. case AT_READ_ERROR:
  1705. return "READ ERROR";
  1706. default:
  1707. case AT_UNKNOWN:
  1708. return "UNKNOWN";
  1709. /* at responses */
  1710. case AT_OK:
  1711. return "OK";
  1712. case AT_ERROR:
  1713. return "ERROR";
  1714. case AT_RING:
  1715. return "RING";
  1716. case AT_BRSF:
  1717. return "AT+BRSF";
  1718. case AT_CIND:
  1719. return "AT+CIND";
  1720. case AT_CIEV:
  1721. return "AT+CIEV";
  1722. case AT_CLIP:
  1723. return "AT+CLIP";
  1724. case AT_CMTI:
  1725. return "AT+CMTI";
  1726. case AT_CMGR:
  1727. return "AT+CMGR";
  1728. case AT_SMS_PROMPT:
  1729. return "SMS PROMPT";
  1730. case AT_CMS_ERROR:
  1731. return "+CMS ERROR";
  1732. case AT_BUSY:
  1733. return "BUSY";
  1734. case AT_NO_DIALTONE:
  1735. return "NO DIALTONE";
  1736. case AT_NO_CARRIER:
  1737. return "NO CARRIER";
  1738. /* at commands */
  1739. case AT_A:
  1740. return "ATA";
  1741. case AT_D:
  1742. return "ATD";
  1743. case AT_CHUP:
  1744. return "AT+CHUP";
  1745. case AT_CKPD:
  1746. return "AT+CKPD";
  1747. case AT_CMGS:
  1748. return "AT+CMGS";
  1749. case AT_VGM:
  1750. return "AT+VGM";
  1751. case AT_VGS:
  1752. return "AT+VGS";
  1753. case AT_VTS:
  1754. return "AT+VTS";
  1755. case AT_CMGF:
  1756. return "AT+CMGF";
  1757. case AT_CNMI:
  1758. return "AT+CNMI";
  1759. case AT_CMER:
  1760. return "AT+CMER";
  1761. case AT_CIND_TEST:
  1762. return "AT+CIND=?";
  1763. case AT_CUSD:
  1764. return "AT+CUSD";
  1765. case AT_ECAM:
  1766. return "AT*ECAM";
  1767. }
  1768. }
  1769. /*
  1770. * bluetooth handsfree profile helpers
  1771. */
  1772. /*!
  1773. * \brief Parse a ECAV event.
  1774. * \param hfp an hfp_pvt struct
  1775. * \param buf the buffer to parse (null terminated)
  1776. * \return -1 on error (parse error) or a ECAM value on success
  1777. *
  1778. * Example string: *ECAV: <ccid>,<ccstatus>,<calltype>[,<processid>]
  1779. * [,exitcause][,<number>,<type>]
  1780. *
  1781. * Example indicating busy: *ECAV: 1,7,1
  1782. */
  1783. static int hfp_parse_ecav(struct hfp_pvt *hfp, char *buf)
  1784. {
  1785. int ccid = 0;
  1786. int ccstatus = 0;
  1787. int calltype = 0;
  1788. if (!sscanf(buf, "*ECAV: %2d,%2d,%2d", &ccid, &ccstatus, &calltype)) {
  1789. ast_debug(1, "[%s] error parsing ECAV event '%s'\n", hfp->owner->id, buf);
  1790. return -1;
  1791. }
  1792. return ccstatus;
  1793. }
  1794. /*!
  1795. * \brief Enable Sony Erricson extensions / indications.
  1796. * \param hfp an hfp_pvt struct
  1797. */
  1798. static int hfp_send_ecam(struct hfp_pvt *hfp)
  1799. {
  1800. return rfcomm_write(hfp->rsock, "AT*ECAM=1\r");
  1801. }
  1802. /*!
  1803. * \brief Parse a CIEV event.
  1804. * \param hfp an hfp_pvt struct
  1805. * \param buf the buffer to parse (null terminated)
  1806. * \param value a pointer to an int to store the event value in (can be NULL)
  1807. * \return 0 on error (parse error, or unknown event) or a HFP_CIND_* value on
  1808. * success
  1809. */
  1810. static int hfp_parse_ciev(struct hfp_pvt *hfp, char *buf, int *value)
  1811. {
  1812. int i, v;
  1813. if (!value)
  1814. value = &v;
  1815. if (!sscanf(buf, "+CIEV: %d,%d", &i, value)) {
  1816. ast_debug(2, "[%s] error parsing CIEV event '%s'\n", hfp->owner->id, buf);
  1817. return HFP_CIND_NONE;
  1818. }
  1819. if (i >= ARRAY_LEN(hfp->cind_state)) {
  1820. ast_debug(2, "[%s] CIEV event index too high (%s)\n", hfp->owner->id, buf);
  1821. return HFP_CIND_NONE;
  1822. }
  1823. hfp->cind_state[i] = *value;
  1824. return hfp->cind_index[i];
  1825. }
  1826. /*!
  1827. * \brief Parse a CLIP event.
  1828. * \param hfp an hfp_pvt struct
  1829. * \param buf the buffer to parse (null terminated)
  1830. * \note buf will be modified when the CID string is parsed
  1831. * \return NULL on error (parse error) or a pointer to the caller id
  1832. * information in buf
  1833. */
  1834. static char *hfp_parse_clip(struct hfp_pvt *hfp, char *buf)
  1835. {
  1836. int i, state;
  1837. char *clip = NULL;
  1838. size_t s;
  1839. /* parse clip info in the following format:
  1840. * +CLIP: "123456789",128,...
  1841. */
  1842. state = 0;
  1843. s = strlen(buf);
  1844. for (i = 0; i < s && state != 3; i++) {
  1845. switch (state) {
  1846. case 0: /* search for start of the number (") */
  1847. if (buf[i] == '"') {
  1848. state++;
  1849. }
  1850. break;
  1851. case 1: /* mark the number */
  1852. clip = &buf[i];
  1853. state++;
  1854. /* fall through */
  1855. case 2: /* search for the end of the number (") */
  1856. if (buf[i] == '"') {
  1857. buf[i] = '\0';
  1858. state++;
  1859. }
  1860. break;
  1861. }
  1862. }
  1863. if (state != 3) {
  1864. return NULL;
  1865. }
  1866. return clip;
  1867. }
  1868. /*!
  1869. * \brief Parse a CMTI notification.
  1870. * \param hfp an hfp_pvt struct
  1871. * \param buf the buffer to parse (null terminated)
  1872. * \note buf will be modified when the CMTI message is parsed
  1873. * \return -1 on error (parse error) or the index of the new sms message
  1874. */
  1875. static int hfp_parse_cmti(struct hfp_pvt *hfp, char *buf)
  1876. {
  1877. int index = -1;
  1878. /* parse cmti info in the following format:
  1879. * +CMTI: <mem>,<index>
  1880. */
  1881. if (!sscanf(buf, "+CMTI: %*[^,],%d", &index)) {
  1882. ast_debug(2, "[%s] error parsing CMTI event '%s'\n", hfp->owner->id, buf);
  1883. return -1;
  1884. }
  1885. return index;
  1886. }
  1887. /*!
  1888. * \brief Parse a CMGR message.
  1889. * \param hfp an hfp_pvt struct
  1890. * \param buf the buffer to parse (null terminated)
  1891. * \param from_number a pointer to a char pointer which will store the from
  1892. * number
  1893. * \param text a pointer to a char pointer which will store the message text
  1894. * \note buf will be modified when the CMGR message is parsed
  1895. * \retval -1 parse error
  1896. * \retval 0 success
  1897. */
  1898. static int hfp_parse_cmgr(struct hfp_pvt *hfp, char *buf, char **from_number, char **text)
  1899. {
  1900. int i, state;
  1901. size_t s;
  1902. /* parse cmgr info in the following format:
  1903. * +CMGR: <msg status>,"+123456789",...\r\n
  1904. * <message text>
  1905. */
  1906. state = 0;
  1907. s = strlen(buf);
  1908. for (i = 0; i < s && state != 6; i++) {
  1909. switch (state) {
  1910. case 0: /* search for start of the number section (,) */
  1911. if (buf[i] == ',') {
  1912. state++;
  1913. }
  1914. break;
  1915. case 1: /* find the opening quote (") */
  1916. if (buf[i] == '"') {
  1917. state++;
  1918. }
  1919. break;
  1920. case 2: /* mark the start of the number */
  1921. if (from_number) {
  1922. *from_number = &buf[i];
  1923. state++;
  1924. }
  1925. /* fall through */
  1926. case 3: /* search for the end of the number (") */
  1927. if (buf[i] == '"') {
  1928. buf[i] = '\0';
  1929. state++;
  1930. }
  1931. break;
  1932. case 4: /* search for the start of the message text (\n) */
  1933. if (buf[i] == '\n') {
  1934. state++;
  1935. }
  1936. break;
  1937. case 5: /* mark the start of the message text */
  1938. if (text) {
  1939. *text = &buf[i];
  1940. state++;
  1941. }
  1942. break;
  1943. }
  1944. }
  1945. if (state != 6) {
  1946. return -1;
  1947. }
  1948. return 0;
  1949. }
  1950. /*!
  1951. * \brief Parse a CUSD answer.
  1952. * \param hfp an hfp_pvt struct
  1953. * \param buf the buffer to parse (null terminated)
  1954. * \note buf will be modified when the CUSD string is parsed
  1955. * \return NULL on error (parse error) or a pointer to the cusd message
  1956. * information in buf
  1957. */
  1958. static char *hfp_parse_cusd(struct hfp_pvt *hfp, char *buf)
  1959. {
  1960. int i, message_start, message_end;
  1961. char *cusd;
  1962. size_t s;
  1963. /* parse cusd message in the following format:
  1964. * +CUSD: 0,"100,00 EURO, valid till 01.01.2010, you are using tariff "Mega Tariff". More informations *111#."
  1965. */
  1966. message_start = 0;
  1967. message_end = 0;
  1968. s = strlen(buf);
  1969. /* Find the start of the message (") */
  1970. for (i = 0; i < s; i++) {
  1971. if (buf[i] == '"') {
  1972. message_start = i + 1;
  1973. break;
  1974. }
  1975. }
  1976. if (message_start == 0 || message_start >= s) {
  1977. return NULL;
  1978. }
  1979. /* Find the end of the message (") */
  1980. for (i = s; i > 0; i--) {
  1981. if (buf[i] == '"') {
  1982. message_end = i;
  1983. break;
  1984. }
  1985. }
  1986. if (message_end == 0) {
  1987. return NULL;
  1988. }
  1989. if (message_start >= message_end) {
  1990. return NULL;
  1991. }
  1992. cusd = &buf[message_start];
  1993. buf[message_end] = '\0';
  1994. return cusd;
  1995. }
  1996. /*!
  1997. * \brief Convert a hfp_hf struct to a BRSF int.
  1998. * \param hf an hfp_hf brsf object
  1999. * \return an integer representing the given brsf struct
  2000. */
  2001. static int hfp_brsf2int(struct hfp_hf *hf)
  2002. {
  2003. int brsf = 0;
  2004. brsf |= hf->ecnr ? HFP_HF_ECNR : 0;
  2005. brsf |= hf->cw ? HFP_HF_CW : 0;
  2006. brsf |= hf->cid ? HFP_HF_CID : 0;
  2007. brsf |= hf->voice ? HFP_HF_VOICE : 0;
  2008. brsf |= hf->volume ? HFP_HF_VOLUME : 0;
  2009. brsf |= hf->status ? HFP_HF_STATUS : 0;
  2010. brsf |= hf->control ? HFP_HF_CONTROL : 0;
  2011. return brsf;
  2012. }
  2013. /*!
  2014. * \brief Convert a BRSF int to an hfp_ag struct.
  2015. * \param brsf a brsf integer
  2016. * \param ag a AG (hfp_ag) brsf object
  2017. * \return a pointer to the given hfp_ag object populated with the values from
  2018. * the given brsf integer
  2019. */
  2020. static struct hfp_ag *hfp_int2brsf(int brsf, struct hfp_ag *ag)
  2021. {
  2022. ag->cw = brsf & HFP_AG_CW ? 1 : 0;
  2023. ag->ecnr = brsf & HFP_AG_ECNR ? 1 : 0;
  2024. ag->voice = brsf & HFP_AG_VOICE ? 1 : 0;
  2025. ag->ring = brsf & HFP_AG_RING ? 1 : 0;
  2026. ag->tag = brsf & HFP_AG_TAG ? 1 : 0;
  2027. ag->reject = brsf & HFP_AG_REJECT ? 1 : 0;
  2028. ag->status = brsf & HFP_AG_STATUS ? 1 : 0;
  2029. ag->control = brsf & HFP_AG_CONTROL ? 1 : 0;
  2030. ag->errors = brsf & HFP_AG_ERRORS ? 1 : 0;
  2031. return ag;
  2032. }
  2033. /*!
  2034. * \brief Send a BRSF request.
  2035. * \param hfp an hfp_pvt struct
  2036. * \param brsf an hfp_hf brsf struct
  2037. *
  2038. * \retval 0 on success
  2039. * \retval -1 on error
  2040. */
  2041. static int hfp_send_brsf(struct hfp_pvt *hfp, struct hfp_hf *brsf)
  2042. {
  2043. char cmd[32];
  2044. snprintf(cmd, sizeof(cmd), "AT+BRSF=%d\r", hfp_brsf2int(brsf));
  2045. return rfcomm_write(hfp->rsock, cmd);
  2046. }
  2047. /*!
  2048. * \brief Send the CIND read command.
  2049. * \param hfp an hfp_pvt struct
  2050. */
  2051. static int hfp_send_cind(struct hfp_pvt *hfp)
  2052. {
  2053. return rfcomm_write(hfp->rsock, "AT+CIND?\r");
  2054. }
  2055. /*!
  2056. * \brief Send the CIND test command.
  2057. * \param hfp an hfp_pvt struct
  2058. */
  2059. static int hfp_send_cind_test(struct hfp_pvt *hfp)
  2060. {
  2061. return rfcomm_write(hfp->rsock, "AT+CIND=?\r");
  2062. }
  2063. /*!
  2064. * \brief Enable or disable indicator events reporting.
  2065. * \param hfp an hfp_pvt struct
  2066. * \param status enable or disable events reporting (should be 1 or 0)
  2067. */
  2068. static int hfp_send_cmer(struct hfp_pvt *hfp, int status)
  2069. {
  2070. char cmd[32];
  2071. snprintf(cmd, sizeof(cmd), "AT+CMER=3,0,0,%d\r", status ? 1 : 0);
  2072. return rfcomm_write(hfp->rsock, cmd);
  2073. }
  2074. /*!
  2075. * \brief Send the current speaker gain level.
  2076. * \param hfp an hfp_pvt struct
  2077. * \param value the value to send (must be between 0 and 15)
  2078. */
  2079. static int hfp_send_vgs(struct hfp_pvt *hfp, int value)
  2080. {
  2081. char cmd[32];
  2082. snprintf(cmd, sizeof(cmd), "AT+VGS=%d\r", value);
  2083. return rfcomm_write(hfp->rsock, cmd);
  2084. }
  2085. #if 0
  2086. /*!
  2087. * \brief Send the current microphone gain level.
  2088. * \param hfp an hfp_pvt struct
  2089. * \param value the value to send (must be between 0 and 15)
  2090. */
  2091. static int hfp_send_vgm(struct hfp_pvt *hfp, int value)
  2092. {
  2093. char cmd[32];
  2094. snprintf(cmd, sizeof(cmd), "AT+VGM=%d\r", value);
  2095. return rfcomm_write(hfp->rsock, cmd);
  2096. }
  2097. #endif
  2098. /*!
  2099. * \brief Enable or disable calling line identification.
  2100. * \param hfp an hfp_pvt struct
  2101. * \param status enable or disable calling line identification (should be 1 or
  2102. * 0)
  2103. */
  2104. static int hfp_send_clip(struct hfp_pvt *hfp, int status)
  2105. {
  2106. char cmd[32];
  2107. snprintf(cmd, sizeof(cmd), "AT+CLIP=%d\r", status ? 1 : 0);
  2108. return rfcomm_write(hfp->rsock, cmd);
  2109. }
  2110. /*!
  2111. * \brief Send a DTMF command.
  2112. * \param hfp an hfp_pvt struct
  2113. * \param digit the dtmf digit to send
  2114. * \return the result of rfcomm_write() or -1 on an invalid digit being sent
  2115. */
  2116. static int hfp_send_dtmf(struct hfp_pvt *hfp, char digit)
  2117. {
  2118. char cmd[10];
  2119. switch(digit) {
  2120. case '0':
  2121. case '1':
  2122. case '2':
  2123. case '3':
  2124. case '4':
  2125. case '5':
  2126. case '6':
  2127. case '7':
  2128. case '8':
  2129. case '9':
  2130. case '*':
  2131. case '#':
  2132. snprintf(cmd, sizeof(cmd), "AT+VTS=%c\r", digit);
  2133. return rfcomm_write(hfp->rsock, cmd);
  2134. default:
  2135. return -1;
  2136. }
  2137. }
  2138. /*!
  2139. * \brief Set the SMS mode.
  2140. * \param hfp an hfp_pvt struct
  2141. * \param mode the sms mode (0 = PDU, 1 = Text)
  2142. */
  2143. static int hfp_send_cmgf(struct hfp_pvt *hfp, int mode)
  2144. {
  2145. char cmd[32];
  2146. snprintf(cmd, sizeof(cmd), "AT+CMGF=%d\r", mode);
  2147. return rfcomm_write(hfp->rsock, cmd);
  2148. }
  2149. /*!
  2150. * \brief Setup SMS new message indication.
  2151. * \param hfp an hfp_pvt struct
  2152. */
  2153. static int hfp_send_cnmi(struct hfp_pvt *hfp)
  2154. {
  2155. return rfcomm_write(hfp->rsock, "AT+CNMI=2,1,0,0,0\r");
  2156. }
  2157. /*!
  2158. * \brief Read an SMS message.
  2159. * \param hfp an hfp_pvt struct
  2160. * \param index the location of the requested message
  2161. */
  2162. static int hfp_send_cmgr(struct hfp_pvt *hfp, int index)
  2163. {
  2164. char cmd[32];
  2165. snprintf(cmd, sizeof(cmd), "AT+CMGR=%d\r", index);
  2166. return rfcomm_write(hfp->rsock, cmd);
  2167. }
  2168. /*!
  2169. * \brief Start sending an SMS message.
  2170. * \param hfp an hfp_pvt struct
  2171. * \param number the destination of the message
  2172. */
  2173. static int hfp_send_cmgs(struct hfp_pvt *hfp, const char *number)
  2174. {
  2175. char cmd[64];
  2176. snprintf(cmd, sizeof(cmd), "AT+CMGS=\"%s\"\r", number);
  2177. return rfcomm_write(hfp->rsock, cmd);
  2178. }
  2179. /*!
  2180. * \brief Send the text of an SMS message.
  2181. * \param hfp an hfp_pvt struct
  2182. * \param message the text of the message
  2183. */
  2184. static int hfp_send_sms_text(struct hfp_pvt *hfp, const char *message)
  2185. {
  2186. char cmd[162];
  2187. snprintf(cmd, sizeof(cmd), "%.160s\x1a", message);
  2188. return rfcomm_write(hfp->rsock, cmd);
  2189. }
  2190. /*!
  2191. * \brief Send AT+CHUP.
  2192. * \param hfp an hfp_pvt struct
  2193. */
  2194. static int hfp_send_chup(struct hfp_pvt *hfp)
  2195. {
  2196. return rfcomm_write(hfp->rsock, "AT+CHUP\r");
  2197. }
  2198. /*!
  2199. * \brief Send ATD.
  2200. * \param hfp an hfp_pvt struct
  2201. * \param number the number to send
  2202. */
  2203. static int hfp_send_atd(struct hfp_pvt *hfp, const char *number)
  2204. {
  2205. char cmd[64];
  2206. snprintf(cmd, sizeof(cmd), "ATD%s;\r", number);
  2207. return rfcomm_write(hfp->rsock, cmd);
  2208. }
  2209. /*!
  2210. * \brief Send ATA.
  2211. * \param hfp an hfp_pvt struct
  2212. */
  2213. static int hfp_send_ata(struct hfp_pvt *hfp)
  2214. {
  2215. return rfcomm_write(hfp->rsock, "ATA\r");
  2216. }
  2217. /*!
  2218. * \brief Send CUSD.
  2219. * \param hfp an hfp_pvt struct
  2220. * \param code the CUSD code to send
  2221. */
  2222. static int hfp_send_cusd(struct hfp_pvt *hfp, const char *code)
  2223. {
  2224. char cmd[128];
  2225. snprintf(cmd, sizeof(cmd), "AT+CUSD=1,\"%s\",15\r", code);
  2226. return rfcomm_write(hfp->rsock, cmd);
  2227. }
  2228. /*!
  2229. * \brief Parse BRSF data.
  2230. * \param hfp an hfp_pvt struct
  2231. * \param buf the buffer to parse (null terminated)
  2232. */
  2233. static int hfp_parse_brsf(struct hfp_pvt *hfp, const char *buf)
  2234. {
  2235. int brsf;
  2236. if (!sscanf(buf, "+BRSF:%d", &brsf))
  2237. return -1;
  2238. hfp_int2brsf(brsf, &hfp->brsf);
  2239. return 0;
  2240. }
  2241. /*!
  2242. * \brief Parse and store the given indicator.
  2243. * \param hfp an hfp_pvt struct
  2244. * \param group the indicator group
  2245. * \param indicator the indicator to parse
  2246. */
  2247. static int hfp_parse_cind_indicator(struct hfp_pvt *hfp, int group, char *indicator)
  2248. {
  2249. int value;
  2250. /* store the current indicator */
  2251. if (group >= ARRAY_LEN(hfp->cind_state)) {
  2252. ast_debug(1, "ignoring CIND state '%s' for group %d, we only support up to %d indicators\n", indicator, group, (int) sizeof(hfp->cind_state));
  2253. return -1;
  2254. }
  2255. if (!sscanf(indicator, "%d", &value)) {
  2256. ast_debug(1, "error parsing CIND state '%s' for group %d\n", indicator, group);
  2257. return -1;
  2258. }
  2259. hfp->cind_state[group] = value;
  2260. return 0;
  2261. }
  2262. /*!
  2263. * \brief Read the result of the AT+CIND? command.
  2264. * \param hfp an hfp_pvt struct
  2265. * \param buf the buffer to parse (null terminated)
  2266. * \note hfp_send_cind_test() and hfp_parse_cind_test() should be called at
  2267. * least once before this function is called.
  2268. */
  2269. static int hfp_parse_cind(struct hfp_pvt *hfp, char *buf)
  2270. {
  2271. int i, state, group;
  2272. size_t s;
  2273. char *indicator = NULL;
  2274. /* parse current state of all of our indicators. The list is in the
  2275. * following format:
  2276. * +CIND: 1,0,2,0,0,0,0
  2277. */
  2278. group = 0;
  2279. state = 0;
  2280. s = strlen(buf);
  2281. for (i = 0; i < s; i++) {
  2282. switch (state) {
  2283. case 0: /* search for start of the status indicators (a space) */
  2284. if (buf[i] == ' ') {
  2285. group++;
  2286. state++;
  2287. }
  2288. break;
  2289. case 1: /* mark this indicator */
  2290. indicator = &buf[i];
  2291. state++;
  2292. break;
  2293. case 2: /* search for the start of the next indicator (a comma) */
  2294. if (buf[i] == ',') {
  2295. buf[i] = '\0';
  2296. hfp_parse_cind_indicator(hfp, group, indicator);
  2297. group++;
  2298. state = 1;
  2299. }
  2300. break;
  2301. }
  2302. }
  2303. /* store the last indicator */
  2304. if (state == 2)
  2305. hfp_parse_cind_indicator(hfp, group, indicator);
  2306. return 0;
  2307. }
  2308. /*!
  2309. * \brief Parse the result of the AT+CIND=? command.
  2310. * \param hfp an hfp_pvt struct
  2311. * \param buf the buffer to parse (null terminated)
  2312. */
  2313. static int hfp_parse_cind_test(struct hfp_pvt *hfp, char *buf)
  2314. {
  2315. int i, state, group;
  2316. size_t s;
  2317. char *indicator = NULL;
  2318. hfp->nocallsetup = 1;
  2319. /* parse the indications list. It is in the follwing format:
  2320. * +CIND: ("ind1",(0-1)),("ind2",(0-5))
  2321. */
  2322. group = 0;
  2323. state = 0;
  2324. s = strlen(buf);
  2325. for (i = 0; i < s; i++) {
  2326. switch (state) {
  2327. case 0: /* search for start of indicator block */
  2328. if (buf[i] == '(') {
  2329. group++;
  2330. state++;
  2331. }
  2332. break;
  2333. case 1: /* search for '"' in indicator block */
  2334. if (buf[i] == '"') {
  2335. state++;
  2336. }
  2337. break;
  2338. case 2: /* mark the start of the indicator name */
  2339. indicator = &buf[i];
  2340. state++;
  2341. break;
  2342. case 3: /* look for the end of the indicator name */
  2343. if (buf[i] == '"') {
  2344. buf[i] = '\0';
  2345. state++;
  2346. }
  2347. break;
  2348. case 4: /* find the start of the value range */
  2349. if (buf[i] == '(') {
  2350. state++;
  2351. }
  2352. break;
  2353. case 5: /* mark the start of the value range */
  2354. state++;
  2355. break;
  2356. case 6: /* find the end of the value range */
  2357. if (buf[i] == ')') {
  2358. buf[i] = '\0';
  2359. state++;
  2360. }
  2361. break;
  2362. case 7: /* process the values we found */
  2363. if (group < sizeof(hfp->cind_index)) {
  2364. if (!strcmp(indicator, "service")) {
  2365. hfp->cind_map.service = group;
  2366. hfp->cind_index[group] = HFP_CIND_SERVICE;
  2367. } else if (!strcmp(indicator, "call")) {
  2368. hfp->cind_map.call = group;
  2369. hfp->cind_index[group] = HFP_CIND_CALL;
  2370. } else if (!strcmp(indicator, "callsetup")) {
  2371. hfp->nocallsetup = 0;
  2372. hfp->cind_map.callsetup = group;
  2373. hfp->cind_index[group] = HFP_CIND_CALLSETUP;
  2374. } else if (!strcmp(indicator, "call_setup")) { /* non standard call setup identifier */
  2375. hfp->nocallsetup = 0;
  2376. hfp->cind_map.callsetup = group;
  2377. hfp->cind_index[group] = HFP_CIND_CALLSETUP;
  2378. } else if (!strcmp(indicator, "callheld")) {
  2379. hfp->cind_map.callheld = group;
  2380. hfp->cind_index[group] = HFP_CIND_CALLHELD;
  2381. } else if (!strcmp(indicator, "signal")) {
  2382. hfp->cind_map.signal = group;
  2383. hfp->cind_index[group] = HFP_CIND_SIGNAL;
  2384. } else if (!strcmp(indicator, "roam")) {
  2385. hfp->cind_map.roam = group;
  2386. hfp->cind_index[group] = HFP_CIND_ROAM;
  2387. } else if (!strcmp(indicator, "battchg")) {
  2388. hfp->cind_map.battchg = group;
  2389. hfp->cind_index[group] = HFP_CIND_BATTCHG;
  2390. } else {
  2391. hfp->cind_index[group] = HFP_CIND_UNKNOWN;
  2392. ast_debug(2, "ignoring unknown CIND indicator '%s'\n", indicator);
  2393. }
  2394. } else {
  2395. ast_debug(1, "can't store indicator %d (%s), we only support up to %d indicators", group, indicator, (int) sizeof(hfp->cind_index));
  2396. }
  2397. state = 0;
  2398. break;
  2399. }
  2400. }
  2401. hfp->owner->no_callsetup = hfp->nocallsetup;
  2402. return 0;
  2403. }
  2404. /*
  2405. * Bluetooth Headset Profile helpers
  2406. */
  2407. /*!
  2408. * \brief Send an OK AT response.
  2409. * \param rsock the rfcomm socket to use
  2410. */
  2411. static int hsp_send_ok(int rsock)
  2412. {
  2413. return rfcomm_write(rsock, "\r\nOK\r\n");
  2414. }
  2415. /*!
  2416. * \brief Send an ERROR AT response.
  2417. * \param rsock the rfcomm socket to use
  2418. */
  2419. static int hsp_send_error(int rsock)
  2420. {
  2421. return rfcomm_write(rsock, "\r\nERROR\r\n");
  2422. }
  2423. /*!
  2424. * \brief Send a speaker gain unsolicited AT response
  2425. * \param rsock the rfcomm socket to use
  2426. * \param gain the speaker gain value
  2427. */
  2428. static int hsp_send_vgs(int rsock, int gain)
  2429. {
  2430. char cmd[32];
  2431. snprintf(cmd, sizeof(cmd), "\r\n+VGS=%d\r\n", gain);
  2432. return rfcomm_write(rsock, cmd);
  2433. }
  2434. /*!
  2435. * \brief Send a microphone gain unsolicited AT response
  2436. * \param rsock the rfcomm socket to use
  2437. * \param gain the microphone gain value
  2438. */
  2439. static int hsp_send_vgm(int rsock, int gain)
  2440. {
  2441. char cmd[32];
  2442. snprintf(cmd, sizeof(cmd), "\r\n+VGM=%d\r\n", gain);
  2443. return rfcomm_write(rsock, cmd);
  2444. }
  2445. /*!
  2446. * \brief Send a RING unsolicited AT response.
  2447. * \param rsock the rfcomm socket to use
  2448. */
  2449. static int hsp_send_ring(int rsock)
  2450. {
  2451. return rfcomm_write(rsock, "\r\nRING\r\n");
  2452. }
  2453. /*
  2454. * message queue functions
  2455. */
  2456. /*!
  2457. * \brief Add an item to the back of the queue.
  2458. * \param pvt a mbl_pvt structure
  2459. * \param expect the msg we expect to receive
  2460. * \param response_to the message that was sent to generate the expected
  2461. * response
  2462. */
  2463. static int msg_queue_push(struct mbl_pvt *pvt, at_message_t expect, at_message_t response_to)
  2464. {
  2465. struct msg_queue_entry *msg;
  2466. if (!(msg = ast_calloc(1, sizeof(*msg)))) {
  2467. return -1;
  2468. }
  2469. msg->expected = expect;
  2470. msg->response_to = response_to;
  2471. AST_LIST_INSERT_TAIL(&pvt->msg_queue, msg, entry);
  2472. return 0;
  2473. }
  2474. /*!
  2475. * \brief Add an item to the back of the queue with data.
  2476. * \param pvt a mbl_pvt structure
  2477. * \param expect the msg we expect to receive
  2478. * \param response_to the message that was sent to generate the expected
  2479. * response
  2480. * \param data data associated with this message, it will be freed when the
  2481. * message is freed
  2482. */
  2483. static int msg_queue_push_data(struct mbl_pvt *pvt, at_message_t expect, at_message_t response_to, void *data)
  2484. {
  2485. struct msg_queue_entry *msg;
  2486. if (!(msg = ast_calloc(1, sizeof(*msg)))) {
  2487. return -1;
  2488. }
  2489. msg->expected = expect;
  2490. msg->response_to = response_to;
  2491. msg->data = data;
  2492. AST_LIST_INSERT_TAIL(&pvt->msg_queue, msg, entry);
  2493. return 0;
  2494. }
  2495. /*!
  2496. * \brief Remove an item from the front of the queue.
  2497. * \param pvt a mbl_pvt structure
  2498. * \return a pointer to the removed item
  2499. */
  2500. static struct msg_queue_entry *msg_queue_pop(struct mbl_pvt *pvt)
  2501. {
  2502. return AST_LIST_REMOVE_HEAD(&pvt->msg_queue, entry);
  2503. }
  2504. /*!
  2505. * \brief Remove an item from the front of the queue, and free it.
  2506. * \param pvt a mbl_pvt structure
  2507. */
  2508. static void msg_queue_free_and_pop(struct mbl_pvt *pvt)
  2509. {
  2510. struct msg_queue_entry *msg;
  2511. if ((msg = msg_queue_pop(pvt))) {
  2512. if (msg->data)
  2513. ast_free(msg->data);
  2514. ast_free(msg);
  2515. }
  2516. }
  2517. /*!
  2518. * \brief Remove all itmes from the queue and free them.
  2519. * \param pvt a mbl_pvt structure
  2520. */
  2521. static void msg_queue_flush(struct mbl_pvt *pvt)
  2522. {
  2523. struct msg_queue_entry *msg;
  2524. while ((msg = msg_queue_head(pvt)))
  2525. msg_queue_free_and_pop(pvt);
  2526. }
  2527. /*!
  2528. * \brief Get the head of a queue.
  2529. * \param pvt a mbl_pvt structure
  2530. * \return a pointer to the head of the given msg queue
  2531. */
  2532. static struct msg_queue_entry *msg_queue_head(struct mbl_pvt *pvt)
  2533. {
  2534. return AST_LIST_FIRST(&pvt->msg_queue);
  2535. }
  2536. /*
  2537. sdp helpers
  2538. */
  2539. static int sdp_search(char *addr, int profile)
  2540. {
  2541. sdp_session_t *session = 0;
  2542. bdaddr_t bdaddr;
  2543. uuid_t svc_uuid;
  2544. uint32_t range = 0x0000ffff;
  2545. sdp_list_t *response_list, *search_list, *attrid_list;
  2546. int status, port;
  2547. sdp_list_t *proto_list;
  2548. sdp_record_t *sdprec;
  2549. str2ba(addr, &bdaddr);
  2550. port = 0;
  2551. session = sdp_connect(BDADDR_ANY, &bdaddr, SDP_RETRY_IF_BUSY);
  2552. if (!session) {
  2553. ast_debug(1, "sdp_connect() failed on device %s.\n", addr);
  2554. return 0;
  2555. }
  2556. sdp_uuid32_create(&svc_uuid, profile);
  2557. search_list = sdp_list_append(0, &svc_uuid);
  2558. attrid_list = sdp_list_append(0, &range);
  2559. response_list = 0x00;
  2560. status = sdp_service_search_attr_req(session, search_list, SDP_ATTR_REQ_RANGE, attrid_list, &response_list);
  2561. if (status == 0) {
  2562. if (response_list) {
  2563. sdprec = (sdp_record_t *) response_list->data;
  2564. proto_list = 0x00;
  2565. if (sdp_get_access_protos(sdprec, &proto_list) == 0) {
  2566. port = sdp_get_proto_port(proto_list, RFCOMM_UUID);
  2567. sdp_list_free(proto_list, 0);
  2568. }
  2569. sdp_record_free(sdprec);
  2570. sdp_list_free(response_list, 0);
  2571. } else
  2572. ast_debug(1, "No responses returned for device %s.\n", addr);
  2573. } else
  2574. ast_debug(1, "sdp_service_search_attr_req() failed on device %s.\n", addr);
  2575. sdp_list_free(search_list, 0);
  2576. sdp_list_free(attrid_list, 0);
  2577. sdp_close(session);
  2578. return port;
  2579. }
  2580. static sdp_session_t *sdp_register(void)
  2581. {
  2582. uint32_t service_uuid_int[] = {0, 0, 0, GENERIC_AUDIO_SVCLASS_ID};
  2583. uint8_t rfcomm_channel = 1;
  2584. const char *service_name = "Asterisk PABX";
  2585. const char *service_dsc = "Asterisk PABX";
  2586. const char *service_prov = "Asterisk";
  2587. uuid_t root_uuid, l2cap_uuid, rfcomm_uuid, svc_uuid, svc_class1_uuid, svc_class2_uuid;
  2588. sdp_list_t *l2cap_list = 0, *rfcomm_list = 0, *root_list = 0, *proto_list = 0, *access_proto_list = 0, *svc_uuid_list = 0;
  2589. sdp_data_t *channel = 0;
  2590. sdp_session_t *session = 0;
  2591. sdp_record_t *record = sdp_record_alloc();
  2592. sdp_uuid128_create(&svc_uuid, &service_uuid_int);
  2593. sdp_set_service_id(record, svc_uuid);
  2594. sdp_uuid32_create(&svc_class1_uuid, GENERIC_AUDIO_SVCLASS_ID);
  2595. sdp_uuid32_create(&svc_class2_uuid, HEADSET_PROFILE_ID);
  2596. svc_uuid_list = sdp_list_append(0, &svc_class1_uuid);
  2597. svc_uuid_list = sdp_list_append(svc_uuid_list, &svc_class2_uuid);
  2598. sdp_set_service_classes(record, svc_uuid_list);
  2599. sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
  2600. root_list = sdp_list_append(0, &root_uuid);
  2601. sdp_set_browse_groups( record, root_list );
  2602. sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
  2603. l2cap_list = sdp_list_append(0, &l2cap_uuid);
  2604. proto_list = sdp_list_append(0, l2cap_list);
  2605. sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
  2606. channel = sdp_data_alloc(SDP_UINT8, &rfcomm_channel);
  2607. rfcomm_list = sdp_list_append(0, &rfcomm_uuid);
  2608. sdp_list_append(rfcomm_list, channel);
  2609. sdp_list_append(proto_list, rfcomm_list);
  2610. access_proto_list = sdp_list_append(0, proto_list);
  2611. sdp_set_access_protos(record, access_proto_list);
  2612. sdp_set_info_attr(record, service_name, service_prov, service_dsc);
  2613. if (!(session = sdp_connect(BDADDR_ANY, BDADDR_LOCAL, SDP_RETRY_IF_BUSY)))
  2614. ast_log(LOG_WARNING, "Failed to connect sdp and create session.\n");
  2615. else {
  2616. if (sdp_record_register(session, record, 0) < 0) {
  2617. ast_log(LOG_WARNING, "Failed to sdp_record_register error: %d\n", errno);
  2618. return NULL;
  2619. }
  2620. }
  2621. sdp_data_free(channel);
  2622. sdp_list_free(rfcomm_list, 0);
  2623. sdp_list_free(root_list, 0);
  2624. sdp_list_free(access_proto_list, 0);
  2625. sdp_list_free(svc_uuid_list, 0);
  2626. return session;
  2627. }
  2628. /*
  2629. Thread routines
  2630. */
  2631. /*!
  2632. * \brief Handle the BRSF response.
  2633. * \param pvt a mbl_pvt structure
  2634. * \param buf a null terminated buffer containing an AT message
  2635. * \retval 0 success
  2636. * \retval -1 error
  2637. */
  2638. static int handle_response_brsf(struct mbl_pvt *pvt, char *buf)
  2639. {
  2640. struct msg_queue_entry *entry;
  2641. if ((entry = msg_queue_head(pvt)) && entry->expected == AT_BRSF) {
  2642. if (hfp_parse_brsf(pvt->hfp, buf)) {
  2643. ast_debug(1, "[%s] error parsing BRSF\n", pvt->id);
  2644. goto e_return;
  2645. }
  2646. if (msg_queue_push(pvt, AT_OK, AT_BRSF)) {
  2647. ast_debug(1, "[%s] error handling BRSF\n", pvt->id);
  2648. goto e_return;
  2649. }
  2650. msg_queue_free_and_pop(pvt);
  2651. } else if (entry) {
  2652. ast_debug(1, "[%s] received unexpected AT message 'BRSF' when expecting %s, ignoring\n", pvt->id, at_msg2str(entry->expected));
  2653. } else {
  2654. ast_debug(1, "[%s] received unexpected AT message 'BRSF'\n", pvt->id);
  2655. }
  2656. return 0;
  2657. e_return:
  2658. msg_queue_free_and_pop(pvt);
  2659. return -1;
  2660. }
  2661. /*!
  2662. * \brief Handle the CIND response.
  2663. * \param pvt a mbl_pvt structure
  2664. * \param buf a null terminated buffer containing an AT message
  2665. * \retval 0 success
  2666. * \retval -1 error
  2667. */
  2668. static int handle_response_cind(struct mbl_pvt *pvt, char *buf)
  2669. {
  2670. struct msg_queue_entry *entry;
  2671. if ((entry = msg_queue_head(pvt)) && entry->expected == AT_CIND) {
  2672. switch (entry->response_to) {
  2673. case AT_CIND_TEST:
  2674. if (hfp_parse_cind_test(pvt->hfp, buf) || msg_queue_push(pvt, AT_OK, AT_CIND_TEST)) {
  2675. ast_debug(1, "[%s] error performing CIND test\n", pvt->id);
  2676. goto e_return;
  2677. }
  2678. break;
  2679. case AT_CIND:
  2680. if (hfp_parse_cind(pvt->hfp, buf) || msg_queue_push(pvt, AT_OK, AT_CIND)) {
  2681. ast_debug(1, "[%s] error getting CIND state\n", pvt->id);
  2682. goto e_return;
  2683. }
  2684. break;
  2685. default:
  2686. ast_debug(1, "[%s] error getting CIND state\n", pvt->id);
  2687. goto e_return;
  2688. }
  2689. msg_queue_free_and_pop(pvt);
  2690. } else if (entry) {
  2691. ast_debug(1, "[%s] received unexpected AT message 'CIND' when expecting %s, ignoring\n", pvt->id, at_msg2str(entry->expected));
  2692. } else {
  2693. ast_debug(1, "[%s] received unexpected AT message 'CIND'\n", pvt->id);
  2694. }
  2695. return 0;
  2696. e_return:
  2697. msg_queue_free_and_pop(pvt);
  2698. return -1;
  2699. }
  2700. /*!
  2701. * \brief Handle OK AT messages.
  2702. * \param pvt a mbl_pvt structure
  2703. * \param buf a null terminated buffer containing an AT message
  2704. * \retval 0 success
  2705. * \retval -1 error
  2706. */
  2707. static int handle_response_ok(struct mbl_pvt *pvt, char *buf)
  2708. {
  2709. struct msg_queue_entry *entry;
  2710. if ((entry = msg_queue_head(pvt)) && entry->expected == AT_OK) {
  2711. switch (entry->response_to) {
  2712. /* initialization stuff */
  2713. case AT_BRSF:
  2714. ast_debug(1, "[%s] BSRF sent successfully\n", pvt->id);
  2715. /* If this is a blackberry do CMER now, otherwise
  2716. * continue with CIND as normal. */
  2717. if (pvt->blackberry) {
  2718. if (hfp_send_cmer(pvt->hfp, 1) || msg_queue_push(pvt, AT_OK, AT_CMER)) {
  2719. ast_debug(1, "[%s] error sending CMER\n", pvt->id);
  2720. goto e_return;
  2721. }
  2722. } else {
  2723. if (hfp_send_cind_test(pvt->hfp) || msg_queue_push(pvt, AT_CIND, AT_CIND_TEST)) {
  2724. ast_debug(1, "[%s] error sending CIND test\n", pvt->id);
  2725. goto e_return;
  2726. }
  2727. }
  2728. break;
  2729. case AT_CIND_TEST:
  2730. ast_debug(1, "[%s] CIND test sent successfully\n", pvt->id);
  2731. ast_debug(2, "[%s] call: %d\n", pvt->id, pvt->hfp->cind_map.call);
  2732. ast_debug(2, "[%s] callsetup: %d\n", pvt->id, pvt->hfp->cind_map.callsetup);
  2733. ast_debug(2, "[%s] service: %d\n", pvt->id, pvt->hfp->cind_map.service);
  2734. if (hfp_send_cind(pvt->hfp) || msg_queue_push(pvt, AT_CIND, AT_CIND)) {
  2735. ast_debug(1, "[%s] error requesting CIND state\n", pvt->id);
  2736. goto e_return;
  2737. }
  2738. break;
  2739. case AT_CIND:
  2740. ast_debug(1, "[%s] CIND sent successfully\n", pvt->id);
  2741. /* check if a call is active */
  2742. if (pvt->hfp->cind_state[pvt->hfp->cind_map.call]) {
  2743. ast_verb(3, "Bluetooth Device %s has a call in progress - delaying connection.\n", pvt->id);
  2744. goto e_return;
  2745. }
  2746. /* If this is NOT a blackberry proceed with CMER,
  2747. * otherwise send CLIP. */
  2748. if (!pvt->blackberry) {
  2749. if (hfp_send_cmer(pvt->hfp, 1) || msg_queue_push(pvt, AT_OK, AT_CMER)) {
  2750. ast_debug(1, "[%s] error sending CMER\n", pvt->id);
  2751. goto e_return;
  2752. }
  2753. } else {
  2754. if (hfp_send_clip(pvt->hfp, 1) || msg_queue_push(pvt, AT_OK, AT_CLIP)) {
  2755. ast_debug(1, "[%s] error enabling calling line notification\n", pvt->id);
  2756. goto e_return;
  2757. }
  2758. }
  2759. break;
  2760. case AT_CMER:
  2761. ast_debug(1, "[%s] CMER sent successfully\n", pvt->id);
  2762. /* If this is a blackberry proceed with the CIND test,
  2763. * otherwise send CLIP. */
  2764. if (pvt->blackberry) {
  2765. if (hfp_send_cind_test(pvt->hfp) || msg_queue_push(pvt, AT_CIND, AT_CIND_TEST)) {
  2766. ast_debug(1, "[%s] error sending CIND test\n", pvt->id);
  2767. goto e_return;
  2768. }
  2769. } else {
  2770. if (hfp_send_clip(pvt->hfp, 1) || msg_queue_push(pvt, AT_OK, AT_CLIP)) {
  2771. ast_debug(1, "[%s] error enabling calling line notification\n", pvt->id);
  2772. goto e_return;
  2773. }
  2774. }
  2775. break;
  2776. case AT_CLIP:
  2777. ast_debug(1, "[%s] caling line indication enabled\n", pvt->id);
  2778. if (hfp_send_ecam(pvt->hfp) || msg_queue_push(pvt, AT_OK, AT_ECAM)) {
  2779. ast_debug(1, "[%s] error enabling Sony Ericsson call monitoring extensions\n", pvt->id);
  2780. goto e_return;
  2781. }
  2782. break;
  2783. case AT_ECAM:
  2784. ast_debug(1, "[%s] Sony Ericsson call monitoring is active on device\n", pvt->id);
  2785. if (hfp_send_vgs(pvt->hfp, 15) || msg_queue_push(pvt, AT_OK, AT_VGS)) {
  2786. ast_debug(1, "[%s] error synchronizing gain settings\n", pvt->id);
  2787. goto e_return;
  2788. }
  2789. pvt->timeout = -1;
  2790. pvt->hfp->initialized = 1;
  2791. ast_verb(3, "Bluetooth Device %s initialized and ready.\n", pvt->id);
  2792. break;
  2793. case AT_VGS:
  2794. ast_debug(1, "[%s] volume level synchronization successful\n", pvt->id);
  2795. /* set the SMS operating mode to text mode */
  2796. if (pvt->has_sms) {
  2797. if (hfp_send_cmgf(pvt->hfp, 1) || msg_queue_push(pvt, AT_OK, AT_CMGF)) {
  2798. ast_debug(1, "[%s] error setting CMGF\n", pvt->id);
  2799. goto e_return;
  2800. }
  2801. }
  2802. break;
  2803. case AT_CMGF:
  2804. ast_debug(1, "[%s] sms text mode enabled\n", pvt->id);
  2805. /* turn on SMS new message indication */
  2806. if (hfp_send_cnmi(pvt->hfp) || msg_queue_push(pvt, AT_OK, AT_CNMI)) {
  2807. ast_debug(1, "[%s] error setting CNMI\n", pvt->id);
  2808. goto e_return;
  2809. }
  2810. break;
  2811. case AT_CNMI:
  2812. ast_debug(1, "[%s] sms new message indication enabled\n", pvt->id);
  2813. pvt->has_sms = 1;
  2814. break;
  2815. /* end initialization stuff */
  2816. case AT_A:
  2817. ast_debug(1, "[%s] answer sent successfully\n", pvt->id);
  2818. pvt->needchup = 1;
  2819. break;
  2820. case AT_D:
  2821. ast_debug(1, "[%s] dial sent successfully\n", pvt->id);
  2822. pvt->needchup = 1;
  2823. pvt->outgoing = 1;
  2824. mbl_queue_control(pvt, AST_CONTROL_PROGRESS);
  2825. break;
  2826. case AT_CHUP:
  2827. ast_debug(1, "[%s] successful hangup\n", pvt->id);
  2828. break;
  2829. case AT_CMGS:
  2830. ast_debug(1, "[%s] successfully sent sms message\n", pvt->id);
  2831. pvt->outgoing_sms = 0;
  2832. break;
  2833. case AT_VTS:
  2834. ast_debug(1, "[%s] digit sent successfully\n", pvt->id);
  2835. break;
  2836. case AT_CUSD:
  2837. ast_debug(1, "[%s] CUSD code sent successfully\n", pvt->id);
  2838. break;
  2839. case AT_UNKNOWN:
  2840. default:
  2841. ast_debug(1, "[%s] received OK for unhandled request: %s\n", pvt->id, at_msg2str(entry->response_to));
  2842. break;
  2843. }
  2844. msg_queue_free_and_pop(pvt);
  2845. } else if (entry) {
  2846. ast_debug(1, "[%s] received AT message 'OK' when expecting %s, ignoring\n", pvt->id, at_msg2str(entry->expected));
  2847. } else {
  2848. ast_debug(1, "[%s] received unexpected AT message 'OK'\n", pvt->id);
  2849. }
  2850. return 0;
  2851. e_return:
  2852. msg_queue_free_and_pop(pvt);
  2853. return -1;
  2854. }
  2855. /*!
  2856. * \brief Handle ERROR AT messages.
  2857. * \param pvt a mbl_pvt structure
  2858. * \param buf a null terminated buffer containing an AT message
  2859. * \retval 0 success
  2860. * \retval -1 error
  2861. */
  2862. static int handle_response_error(struct mbl_pvt *pvt, char *buf)
  2863. {
  2864. struct msg_queue_entry *entry;
  2865. if ((entry = msg_queue_head(pvt))
  2866. && (entry->expected == AT_OK
  2867. || entry->expected == AT_ERROR
  2868. || entry->expected == AT_CMS_ERROR
  2869. || entry->expected == AT_CMGR
  2870. || entry->expected == AT_SMS_PROMPT)) {
  2871. switch (entry->response_to) {
  2872. /* initialization stuff */
  2873. case AT_BRSF:
  2874. ast_debug(1, "[%s] error reading BSRF\n", pvt->id);
  2875. goto e_return;
  2876. case AT_CIND_TEST:
  2877. ast_debug(1, "[%s] error during CIND test\n", pvt->id);
  2878. goto e_return;
  2879. case AT_CIND:
  2880. ast_debug(1, "[%s] error requesting CIND state\n", pvt->id);
  2881. goto e_return;
  2882. case AT_CMER:
  2883. ast_debug(1, "[%s] error during CMER request\n", pvt->id);
  2884. goto e_return;
  2885. case AT_CLIP:
  2886. ast_debug(1, "[%s] error enabling calling line indication\n", pvt->id);
  2887. goto e_return;
  2888. case AT_VGS:
  2889. ast_debug(1, "[%s] volume level synchronization failed\n", pvt->id);
  2890. /* this is not a fatal error, let's continue with initialization */
  2891. /* set the SMS operating mode to text mode */
  2892. if (hfp_send_cmgf(pvt->hfp, 1) || msg_queue_push(pvt, AT_OK, AT_CMGF)) {
  2893. ast_debug(1, "[%s] error setting CMGF\n", pvt->id);
  2894. goto e_return;
  2895. }
  2896. break;
  2897. case AT_CMGF:
  2898. pvt->has_sms = 0;
  2899. ast_debug(1, "[%s] error setting CMGF\n", pvt->id);
  2900. ast_debug(1, "[%s] no SMS support\n", pvt->id);
  2901. break;
  2902. case AT_CNMI:
  2903. pvt->has_sms = 0;
  2904. ast_debug(1, "[%s] error setting CNMI\n", pvt->id);
  2905. ast_debug(1, "[%s] no SMS support\n", pvt->id);
  2906. break;
  2907. case AT_ECAM:
  2908. ast_debug(1, "[%s] Mobile does not support Sony Ericsson extensions\n", pvt->id);
  2909. /* this is not a fatal error, let's continue with the initialization */
  2910. if (hfp_send_vgs(pvt->hfp, 15) || msg_queue_push(pvt, AT_OK, AT_VGS)) {
  2911. ast_debug(1, "[%s] error synchronizing gain settings\n", pvt->id);
  2912. goto e_return;
  2913. }
  2914. pvt->timeout = -1;
  2915. pvt->hfp->initialized = 1;
  2916. ast_verb(3, "Bluetooth Device %s initialized and ready.\n", pvt->id);
  2917. break;
  2918. /* end initialization stuff */
  2919. case AT_A:
  2920. ast_debug(1, "[%s] answer failed\n", pvt->id);
  2921. mbl_queue_hangup(pvt);
  2922. break;
  2923. case AT_D:
  2924. ast_debug(1, "[%s] dial failed\n", pvt->id);
  2925. pvt->needchup = 0;
  2926. mbl_queue_control(pvt, AST_CONTROL_CONGESTION);
  2927. break;
  2928. case AT_CHUP:
  2929. ast_debug(1, "[%s] error sending hangup, disconnecting\n", pvt->id);
  2930. goto e_return;
  2931. case AT_CMGR:
  2932. ast_debug(1, "[%s] error reading sms message\n", pvt->id);
  2933. pvt->incoming_sms = 0;
  2934. break;
  2935. case AT_CMGS:
  2936. ast_debug(1, "[%s] error sending sms message\n", pvt->id);
  2937. pvt->outgoing_sms = 0;
  2938. break;
  2939. case AT_VTS:
  2940. ast_debug(1, "[%s] error sending digit\n", pvt->id);
  2941. break;
  2942. case AT_CUSD:
  2943. ast_verb(0, "[%s] error sending CUSD command\n", pvt->id);
  2944. break;
  2945. case AT_UNKNOWN:
  2946. default:
  2947. ast_debug(1, "[%s] received ERROR for unhandled request: %s\n", pvt->id, at_msg2str(entry->response_to));
  2948. break;
  2949. }
  2950. msg_queue_free_and_pop(pvt);
  2951. } else if (entry) {
  2952. ast_debug(1, "[%s] received AT message 'ERROR' when expecting %s, ignoring\n", pvt->id, at_msg2str(entry->expected));
  2953. } else {
  2954. ast_debug(1, "[%s] received unexpected AT message 'ERROR'\n", pvt->id);
  2955. }
  2956. return 0;
  2957. e_return:
  2958. msg_queue_free_and_pop(pvt);
  2959. return -1;
  2960. }
  2961. /*!
  2962. * \brief Handle AT+CIEV messages.
  2963. * \param pvt a mbl_pvt structure
  2964. * \param buf a null terminated buffer containing an AT message
  2965. * \retval 0 success
  2966. * \retval -1 error
  2967. */
  2968. static int handle_response_ciev(struct mbl_pvt *pvt, char *buf)
  2969. {
  2970. int i;
  2971. switch (hfp_parse_ciev(pvt->hfp, buf, &i)) {
  2972. case HFP_CIND_CALL:
  2973. switch (i) {
  2974. case HFP_CIND_CALL_NONE:
  2975. ast_debug(1, "[%s] line disconnected\n", pvt->id);
  2976. if (pvt->owner) {
  2977. ast_debug(1, "[%s] hanging up owner\n", pvt->id);
  2978. if (mbl_queue_hangup(pvt)) {
  2979. ast_log(LOG_ERROR, "[%s] error queueing hangup, disconnectiong...\n", pvt->id);
  2980. return -1;
  2981. }
  2982. }
  2983. pvt->needchup = 0;
  2984. pvt->needcallerid = 0;
  2985. pvt->incoming = 0;
  2986. pvt->outgoing = 0;
  2987. break;
  2988. case HFP_CIND_CALL_ACTIVE:
  2989. if (pvt->outgoing) {
  2990. ast_debug(1, "[%s] remote end answered\n", pvt->id);
  2991. mbl_queue_control(pvt, AST_CONTROL_ANSWER);
  2992. } else if (pvt->incoming && pvt->answered) {
  2993. ast_setstate(pvt->owner, AST_STATE_UP);
  2994. } else if (pvt->incoming) {
  2995. /* user answered from handset, disconnecting */
  2996. ast_verb(3, "[%s] user answered bluetooth device from handset, disconnecting\n", pvt->id);
  2997. mbl_queue_hangup(pvt);
  2998. return -1;
  2999. }
  3000. break;
  3001. }
  3002. break;
  3003. case HFP_CIND_CALLSETUP:
  3004. switch (i) {
  3005. case HFP_CIND_CALLSETUP_NONE:
  3006. if (pvt->hfp->cind_state[pvt->hfp->cind_map.call] != HFP_CIND_CALL_ACTIVE) {
  3007. if (pvt->owner) {
  3008. if (pvt->hfp->sent_alerting == 1) {
  3009. handle_response_busy(pvt);
  3010. }
  3011. if (mbl_queue_hangup(pvt)) {
  3012. ast_log(LOG_ERROR, "[%s] error queueing hangup, disconnectiong...\n", pvt->id);
  3013. return -1;
  3014. }
  3015. }
  3016. pvt->needchup = 0;
  3017. pvt->needcallerid = 0;
  3018. pvt->incoming = 0;
  3019. pvt->outgoing = 0;
  3020. }
  3021. break;
  3022. case HFP_CIND_CALLSETUP_INCOMING:
  3023. ast_debug(1, "[%s] incoming call, waiting for caller id\n", pvt->id);
  3024. pvt->needcallerid = 1;
  3025. pvt->incoming = 1;
  3026. break;
  3027. case HFP_CIND_CALLSETUP_OUTGOING:
  3028. if (pvt->outgoing) {
  3029. pvt->hfp->sent_alerting = 0;
  3030. ast_debug(1, "[%s] outgoing call\n", pvt->id);
  3031. } else {
  3032. ast_verb(3, "[%s] user dialed from handset, disconnecting\n", pvt->id);
  3033. return -1;
  3034. }
  3035. break;
  3036. case HFP_CIND_CALLSETUP_ALERTING:
  3037. if (pvt->outgoing) {
  3038. ast_debug(1, "[%s] remote alerting\n", pvt->id);
  3039. mbl_queue_control(pvt, AST_CONTROL_RINGING);
  3040. pvt->hfp->sent_alerting = 1;
  3041. }
  3042. break;
  3043. }
  3044. break;
  3045. case HFP_CIND_NONE:
  3046. ast_debug(1, "[%s] error parsing CIND: %s\n", pvt->id, buf);
  3047. break;
  3048. }
  3049. return 0;
  3050. }
  3051. /*!
  3052. * \brief Handle AT+CLIP messages.
  3053. * \param pvt a mbl_pvt structure
  3054. * \param buf a null terminated buffer containing an AT message
  3055. * \retval 0 success
  3056. * \retval -1 error
  3057. */
  3058. static int handle_response_clip(struct mbl_pvt *pvt, char *buf)
  3059. {
  3060. char *clip;
  3061. struct msg_queue_entry *msg;
  3062. struct ast_channel *chan;
  3063. if ((msg = msg_queue_head(pvt)) && msg->expected == AT_CLIP) {
  3064. msg_queue_free_and_pop(pvt);
  3065. pvt->needcallerid = 0;
  3066. if (!(clip = hfp_parse_clip(pvt->hfp, buf))) {
  3067. ast_debug(1, "[%s] error parsing CLIP: %s\n", pvt->id, buf);
  3068. }
  3069. if (!(chan = mbl_new(AST_STATE_RING, pvt, clip, NULL, NULL))) {
  3070. ast_log(LOG_ERROR, "[%s] unable to allocate channel for incoming call\n", pvt->id);
  3071. hfp_send_chup(pvt->hfp);
  3072. msg_queue_push(pvt, AT_OK, AT_CHUP);
  3073. return -1;
  3074. }
  3075. /* from this point on, we need to send a chup in the event of a
  3076. * hangup */
  3077. pvt->needchup = 1;
  3078. if (ast_pbx_start(chan)) {
  3079. ast_log(LOG_ERROR, "[%s] unable to start pbx on incoming call\n", pvt->id);
  3080. mbl_ast_hangup(pvt);
  3081. return -1;
  3082. }
  3083. }
  3084. return 0;
  3085. }
  3086. /*!
  3087. * \brief Handle RING messages.
  3088. * \param pvt a mbl_pvt structure
  3089. * \param buf a null terminated buffer containing an AT message
  3090. * \retval 0 success
  3091. * \retval -1 error
  3092. */
  3093. static int handle_response_ring(struct mbl_pvt *pvt, char *buf)
  3094. {
  3095. if (pvt->needcallerid) {
  3096. ast_debug(1, "[%s] got ring while waiting for caller id\n", pvt->id);
  3097. return msg_queue_push(pvt, AT_CLIP, AT_UNKNOWN);
  3098. } else {
  3099. return 0;
  3100. }
  3101. }
  3102. /*!
  3103. * \brief Handle AT+CMTI messages.
  3104. * \param pvt a mbl_pvt structure
  3105. * \param buf a null terminated buffer containing an AT message
  3106. * \retval 0 success
  3107. * \retval -1 error
  3108. */
  3109. static int handle_response_cmti(struct mbl_pvt *pvt, char *buf)
  3110. {
  3111. int index = hfp_parse_cmti(pvt->hfp, buf);
  3112. if (index > 0) {
  3113. ast_debug(1, "[%s] incoming sms message\n", pvt->id);
  3114. if (hfp_send_cmgr(pvt->hfp, index)
  3115. || msg_queue_push(pvt, AT_CMGR, AT_CMGR)) {
  3116. ast_debug(1, "[%s] error sending CMGR to retrieve SMS message\n", pvt->id);
  3117. return -1;
  3118. }
  3119. pvt->incoming_sms = 1;
  3120. return 0;
  3121. } else {
  3122. ast_debug(1, "[%s] error parsing incoming sms message alert, disconnecting\n", pvt->id);
  3123. return -1;
  3124. }
  3125. }
  3126. /*!
  3127. * \brief Handle AT+CMGR messages.
  3128. * \param pvt a mbl_pvt structure
  3129. * \param buf a null terminated buffer containing an AT message
  3130. * \retval 0 success
  3131. * \retval -1 error
  3132. */
  3133. static int handle_response_cmgr(struct mbl_pvt *pvt, char *buf)
  3134. {
  3135. char *from_number = NULL, *text = NULL;
  3136. struct ast_channel *chan;
  3137. struct msg_queue_entry *msg;
  3138. if ((msg = msg_queue_head(pvt)) && msg->expected == AT_CMGR) {
  3139. msg_queue_free_and_pop(pvt);
  3140. if (hfp_parse_cmgr(pvt->hfp, buf, &from_number, &text)) {
  3141. ast_debug(1, "[%s] error parsing sms message, disconnecting\n", pvt->id);
  3142. return -1;
  3143. }
  3144. ast_debug(1, "[%s] successfully read sms message\n", pvt->id);
  3145. pvt->incoming_sms = 0;
  3146. /* XXX this channel probably does not need to be associated with this pvt */
  3147. if (!(chan = mbl_new(AST_STATE_DOWN, pvt, NULL, NULL, NULL))) {
  3148. ast_debug(1, "[%s] error creating sms message channel, disconnecting\n", pvt->id);
  3149. return -1;
  3150. }
  3151. ast_channel_exten_set(chan, "sms");
  3152. pbx_builtin_setvar_helper(chan, "SMSSRC", from_number);
  3153. pbx_builtin_setvar_helper(chan, "SMSTXT", text);
  3154. if (ast_pbx_start(chan)) {
  3155. ast_log(LOG_ERROR, "[%s] unable to start pbx on incoming sms\n", pvt->id);
  3156. mbl_ast_hangup(pvt);
  3157. }
  3158. } else {
  3159. ast_debug(1, "[%s] got unexpected +CMGR message, ignoring\n", pvt->id);
  3160. }
  3161. return 0;
  3162. }
  3163. /*!
  3164. * \brief Send an SMS message from the queue.
  3165. * \param pvt a mbl_pvt structure
  3166. * \param buf a null terminated buffer containing an AT message
  3167. * \retval 0 success
  3168. * \retval -1 error
  3169. */
  3170. static int handle_sms_prompt(struct mbl_pvt *pvt, char *buf)
  3171. {
  3172. struct msg_queue_entry *msg;
  3173. if (!(msg = msg_queue_head(pvt))) {
  3174. ast_debug(1, "[%s] error, got sms prompt with no pending sms messages\n", pvt->id);
  3175. return 0;
  3176. }
  3177. if (msg->expected != AT_SMS_PROMPT) {
  3178. ast_debug(1, "[%s] error, got sms prompt but no pending sms messages\n", pvt->id);
  3179. return 0;
  3180. }
  3181. if (hfp_send_sms_text(pvt->hfp, msg->data)
  3182. || msg_queue_push(pvt, AT_OK, AT_CMGS)) {
  3183. msg_queue_free_and_pop(pvt);
  3184. ast_debug(1, "[%s] error sending sms message\n", pvt->id);
  3185. return 0;
  3186. }
  3187. msg_queue_free_and_pop(pvt);
  3188. return 0;
  3189. }
  3190. /*!
  3191. * \brief Handle CUSD messages.
  3192. * \param pvt a mbl_pvt structure
  3193. * \param buf a null terminated buffer containing an AT message
  3194. * \retval 0 success
  3195. * \retval -1 error
  3196. */
  3197. static int handle_response_cusd(struct mbl_pvt *pvt, char *buf)
  3198. {
  3199. char *cusd;
  3200. if (!(cusd = hfp_parse_cusd(pvt->hfp, buf))) {
  3201. ast_verb(0, "[%s] error parsing CUSD: %s\n", pvt->id, buf);
  3202. return 0;
  3203. }
  3204. ast_verb(0, "[%s] CUSD response: %s\n", pvt->id, cusd);
  3205. return 0;
  3206. }
  3207. /*!
  3208. * \brief Handle BUSY messages.
  3209. * \param pvt a mbl_pvt structure
  3210. * \retval 0 success
  3211. * \retval -1 error
  3212. */
  3213. static int handle_response_busy(struct mbl_pvt *pvt)
  3214. {
  3215. pvt->hangupcause = AST_CAUSE_USER_BUSY;
  3216. pvt->needchup = 1;
  3217. mbl_queue_control(pvt, AST_CONTROL_BUSY);
  3218. return 0;
  3219. }
  3220. /*!
  3221. * \brief Handle NO DIALTONE messages.
  3222. * \param pvt a mbl_pvt structure
  3223. * \param buf a null terminated buffer containing an AT message
  3224. * \retval 0 success
  3225. * \retval -1 error
  3226. */
  3227. static int handle_response_no_dialtone(struct mbl_pvt *pvt, char *buf)
  3228. {
  3229. ast_verb(1, "[%s] mobile reports NO DIALTONE\n", pvt->id);
  3230. pvt->needchup = 1;
  3231. mbl_queue_control(pvt, AST_CONTROL_CONGESTION);
  3232. return 0;
  3233. }
  3234. /*!
  3235. * \brief Handle NO CARRIER messages.
  3236. * \param pvt a mbl_pvt structure
  3237. * \param buf a null terminated buffer containing an AT message
  3238. * \retval 0 success
  3239. * \retval -1 error
  3240. */
  3241. static int handle_response_no_carrier(struct mbl_pvt *pvt, char *buf)
  3242. {
  3243. ast_verb(1, "[%s] mobile reports NO CARRIER\n", pvt->id);
  3244. pvt->needchup = 1;
  3245. mbl_queue_control(pvt, AST_CONTROL_CONGESTION);
  3246. return 0;
  3247. }
  3248. static void *do_monitor_phone(void *data)
  3249. {
  3250. struct mbl_pvt *pvt = (struct mbl_pvt *)data;
  3251. struct hfp_pvt *hfp = pvt->hfp;
  3252. char buf[350];
  3253. int t;
  3254. at_message_t at_msg;
  3255. struct msg_queue_entry *entry;
  3256. /* Note: At one point the initialization procedure was neatly contained
  3257. * in the hfp_init() function, but that initialization method did not
  3258. * work with non standard devices. As a result, the initialization
  3259. * procedure is not spread throughout the event handling loop.
  3260. */
  3261. /* start initialization with the BRSF request */
  3262. ast_mutex_lock(&pvt->lock);
  3263. pvt->timeout = 10000;
  3264. if (hfp_send_brsf(hfp, &hfp_our_brsf) || msg_queue_push(pvt, AT_BRSF, AT_BRSF)) {
  3265. ast_debug(1, "[%s] error sending BRSF\n", hfp->owner->id);
  3266. goto e_cleanup;
  3267. }
  3268. ast_mutex_unlock(&pvt->lock);
  3269. while (!check_unloading()) {
  3270. ast_mutex_lock(&pvt->lock);
  3271. t = pvt->timeout;
  3272. ast_mutex_unlock(&pvt->lock);
  3273. if (!rfcomm_wait(pvt->rfcomm_socket, &t)) {
  3274. ast_debug(1, "[%s] timeout waiting for rfcomm data, disconnecting\n", pvt->id);
  3275. ast_mutex_lock(&pvt->lock);
  3276. if (!hfp->initialized) {
  3277. if ((entry = msg_queue_head(pvt))) {
  3278. switch (entry->response_to) {
  3279. case AT_CIND_TEST:
  3280. if (pvt->blackberry)
  3281. ast_debug(1, "[%s] timeout during CIND test\n", hfp->owner->id);
  3282. else
  3283. ast_debug(1, "[%s] timeout during CIND test, try setting 'blackberry=yes'\n", hfp->owner->id);
  3284. break;
  3285. case AT_CMER:
  3286. if (pvt->blackberry)
  3287. ast_debug(1, "[%s] timeout after sending CMER, try setting 'blackberry=no'\n", hfp->owner->id);
  3288. else
  3289. ast_debug(1, "[%s] timeout after sending CMER\n", hfp->owner->id);
  3290. break;
  3291. default:
  3292. ast_debug(1, "[%s] timeout while waiting for %s in response to %s\n", pvt->id, at_msg2str(entry->expected), at_msg2str(entry->response_to));
  3293. break;
  3294. }
  3295. }
  3296. }
  3297. ast_mutex_unlock(&pvt->lock);
  3298. goto e_cleanup;
  3299. }
  3300. if ((at_msg = at_read_full(hfp->rsock, buf, sizeof(buf))) < 0) {
  3301. /* XXX gnu specific strerror_r is assummed here, this
  3302. * is not really safe. See the strerror(3) man page
  3303. * for more info. */
  3304. ast_debug(1, "[%s] error reading from device: %s (%d)\n", pvt->id, strerror_r(errno, buf, sizeof(buf)), errno);
  3305. break;
  3306. }
  3307. ast_debug(1, "[%s] %s\n", pvt->id, buf);
  3308. switch (at_msg) {
  3309. case AT_BRSF:
  3310. ast_mutex_lock(&pvt->lock);
  3311. if (handle_response_brsf(pvt, buf)) {
  3312. ast_mutex_unlock(&pvt->lock);
  3313. goto e_cleanup;
  3314. }
  3315. ast_mutex_unlock(&pvt->lock);
  3316. break;
  3317. case AT_CIND:
  3318. ast_mutex_lock(&pvt->lock);
  3319. if (handle_response_cind(pvt, buf)) {
  3320. ast_mutex_unlock(&pvt->lock);
  3321. goto e_cleanup;
  3322. }
  3323. ast_mutex_unlock(&pvt->lock);
  3324. break;
  3325. case AT_OK:
  3326. ast_mutex_lock(&pvt->lock);
  3327. if (handle_response_ok(pvt, buf)) {
  3328. ast_mutex_unlock(&pvt->lock);
  3329. goto e_cleanup;
  3330. }
  3331. ast_mutex_unlock(&pvt->lock);
  3332. break;
  3333. case AT_CMS_ERROR:
  3334. case AT_ERROR:
  3335. ast_mutex_lock(&pvt->lock);
  3336. if (handle_response_error(pvt, buf)) {
  3337. ast_mutex_unlock(&pvt->lock);
  3338. goto e_cleanup;
  3339. }
  3340. ast_mutex_unlock(&pvt->lock);
  3341. break;
  3342. case AT_RING:
  3343. ast_mutex_lock(&pvt->lock);
  3344. if (handle_response_ring(pvt, buf)) {
  3345. ast_mutex_unlock(&pvt->lock);
  3346. goto e_cleanup;
  3347. }
  3348. ast_mutex_unlock(&pvt->lock);
  3349. break;
  3350. case AT_CIEV:
  3351. ast_mutex_lock(&pvt->lock);
  3352. if (handle_response_ciev(pvt, buf)) {
  3353. ast_mutex_unlock(&pvt->lock);
  3354. goto e_cleanup;
  3355. }
  3356. ast_mutex_unlock(&pvt->lock);
  3357. break;
  3358. case AT_CLIP:
  3359. ast_mutex_lock(&pvt->lock);
  3360. if (handle_response_clip(pvt, buf)) {
  3361. ast_mutex_unlock(&pvt->lock);
  3362. goto e_cleanup;
  3363. }
  3364. ast_mutex_unlock(&pvt->lock);
  3365. break;
  3366. case AT_CMTI:
  3367. ast_mutex_lock(&pvt->lock);
  3368. if (handle_response_cmti(pvt, buf)) {
  3369. ast_mutex_unlock(&pvt->lock);
  3370. goto e_cleanup;
  3371. }
  3372. ast_mutex_unlock(&pvt->lock);
  3373. break;
  3374. case AT_CMGR:
  3375. ast_mutex_lock(&pvt->lock);
  3376. if (handle_response_cmgr(pvt, buf)) {
  3377. ast_mutex_unlock(&pvt->lock);
  3378. goto e_cleanup;
  3379. }
  3380. ast_mutex_unlock(&pvt->lock);
  3381. break;
  3382. case AT_SMS_PROMPT:
  3383. ast_mutex_lock(&pvt->lock);
  3384. if (handle_sms_prompt(pvt, buf)) {
  3385. ast_mutex_unlock(&pvt->lock);
  3386. goto e_cleanup;
  3387. }
  3388. ast_mutex_unlock(&pvt->lock);
  3389. break;
  3390. case AT_CUSD:
  3391. ast_mutex_lock(&pvt->lock);
  3392. if (handle_response_cusd(pvt, buf)) {
  3393. ast_mutex_unlock(&pvt->lock);
  3394. goto e_cleanup;
  3395. }
  3396. ast_mutex_unlock(&pvt->lock);
  3397. break;
  3398. case AT_BUSY:
  3399. ast_mutex_lock(&pvt->lock);
  3400. if (handle_response_busy(pvt)) {
  3401. ast_mutex_unlock(&pvt->lock);
  3402. goto e_cleanup;
  3403. }
  3404. ast_mutex_unlock(&pvt->lock);
  3405. break;
  3406. case AT_NO_DIALTONE:
  3407. ast_mutex_lock(&pvt->lock);
  3408. if (handle_response_no_dialtone(pvt, buf)) {
  3409. ast_mutex_unlock(&pvt->lock);
  3410. goto e_cleanup;
  3411. }
  3412. ast_mutex_unlock(&pvt->lock);
  3413. break;
  3414. case AT_NO_CARRIER:
  3415. ast_mutex_lock(&pvt->lock);
  3416. if (handle_response_no_carrier(pvt, buf)) {
  3417. ast_mutex_unlock(&pvt->lock);
  3418. goto e_cleanup;
  3419. }
  3420. ast_mutex_unlock(&pvt->lock);
  3421. break;
  3422. case AT_ECAM:
  3423. ast_mutex_lock(&pvt->lock);
  3424. if (hfp_parse_ecav(hfp, buf) == 7) {
  3425. if (handle_response_busy(pvt)) {
  3426. ast_mutex_unlock(&pvt->lock);
  3427. goto e_cleanup;
  3428. }
  3429. }
  3430. ast_mutex_unlock(&pvt->lock);
  3431. break;
  3432. case AT_UNKNOWN:
  3433. ast_debug(1, "[%s] ignoring unknown message: %s\n", pvt->id, buf);
  3434. break;
  3435. case AT_PARSE_ERROR:
  3436. ast_debug(1, "[%s] error parsing message\n", pvt->id);
  3437. goto e_cleanup;
  3438. case AT_READ_ERROR:
  3439. ast_debug(1, "[%s] error reading from device: %s (%d)\n", pvt->id, strerror_r(errno, buf, sizeof(buf)), errno);
  3440. goto e_cleanup;
  3441. default:
  3442. break;
  3443. }
  3444. }
  3445. e_cleanup:
  3446. if (!hfp->initialized)
  3447. ast_verb(3, "Error initializing Bluetooth device %s.\n", pvt->id);
  3448. ast_mutex_lock(&pvt->lock);
  3449. if (pvt->owner) {
  3450. ast_debug(1, "[%s] device disconnected, hanging up owner\n", pvt->id);
  3451. pvt->needchup = 0;
  3452. mbl_queue_hangup(pvt);
  3453. }
  3454. close(pvt->rfcomm_socket);
  3455. close(pvt->sco_socket);
  3456. pvt->sco_socket = -1;
  3457. msg_queue_flush(pvt);
  3458. pvt->connected = 0;
  3459. hfp->initialized = 0;
  3460. pvt->adapter->inuse = 0;
  3461. ast_mutex_unlock(&pvt->lock);
  3462. ast_verb(3, "Bluetooth Device %s has disconnected.\n", pvt->id);
  3463. manager_event(EVENT_FLAG_SYSTEM, "MobileStatus", "Status: Disconnect\r\nDevice: %s\r\n", pvt->id);
  3464. return NULL;
  3465. }
  3466. static int headset_send_ring(const void *data)
  3467. {
  3468. struct mbl_pvt *pvt = (struct mbl_pvt *) data;
  3469. ast_mutex_lock(&pvt->lock);
  3470. if (!pvt->needring) {
  3471. ast_mutex_unlock(&pvt->lock);
  3472. return 0;
  3473. }
  3474. ast_mutex_unlock(&pvt->lock);
  3475. if (hsp_send_ring(pvt->rfcomm_socket)) {
  3476. ast_debug(1, "[%s] error sending RING\n", pvt->id);
  3477. return 0;
  3478. }
  3479. return 1;
  3480. }
  3481. static void *do_monitor_headset(void *data)
  3482. {
  3483. struct mbl_pvt *pvt = (struct mbl_pvt *)data;
  3484. char buf[256];
  3485. int t;
  3486. at_message_t at_msg;
  3487. struct ast_channel *chan = NULL;
  3488. ast_verb(3, "Bluetooth Device %s initialised and ready.\n", pvt->id);
  3489. while (!check_unloading()) {
  3490. t = ast_sched_wait(pvt->sched);
  3491. if (t == -1) {
  3492. t = 6000;
  3493. }
  3494. ast_sched_runq(pvt->sched);
  3495. if (rfcomm_wait(pvt->rfcomm_socket, &t) == 0)
  3496. continue;
  3497. if ((at_msg = at_read_full(pvt->rfcomm_socket, buf, sizeof(buf))) < 0) {
  3498. if (strerror_r(errno, buf, sizeof(buf)))
  3499. ast_debug(1, "[%s] error reading from device\n", pvt->id);
  3500. else
  3501. ast_debug(1, "[%s] error reading from device: %s (%d)\n", pvt->id, buf, errno);
  3502. goto e_cleanup;
  3503. }
  3504. ast_debug(1, "[%s] %s\n", pvt->id, buf);
  3505. switch (at_msg) {
  3506. case AT_VGS:
  3507. case AT_VGM:
  3508. /* XXX volume change requested, we will just
  3509. * pretend to do something with it */
  3510. if (hsp_send_ok(pvt->rfcomm_socket)) {
  3511. ast_debug(1, "[%s] error sending AT message 'OK'\n", pvt->id);
  3512. goto e_cleanup;
  3513. }
  3514. break;
  3515. case AT_CKPD:
  3516. ast_mutex_lock(&pvt->lock);
  3517. if (pvt->outgoing) {
  3518. pvt->needring = 0;
  3519. hsp_send_ok(pvt->rfcomm_socket);
  3520. if (pvt->answered) {
  3521. /* we have an answered call up to the
  3522. * HS, he wants to hangup */
  3523. mbl_queue_hangup(pvt);
  3524. } else {
  3525. /* we have an outgoing call to the HS,
  3526. * he wants to answer */
  3527. if ((pvt->sco_socket = sco_connect(pvt->adapter->addr, pvt->addr)) == -1) {
  3528. ast_log(LOG_ERROR, "[%s] unable to create audio connection\n", pvt->id);
  3529. mbl_queue_hangup(pvt);
  3530. ast_mutex_unlock(&pvt->lock);
  3531. goto e_cleanup;
  3532. }
  3533. ast_channel_set_fd(pvt->owner, 0, pvt->sco_socket);
  3534. mbl_queue_control(pvt, AST_CONTROL_ANSWER);
  3535. pvt->answered = 1;
  3536. if (hsp_send_vgs(pvt->rfcomm_socket, 13) || hsp_send_vgm(pvt->rfcomm_socket, 13)) {
  3537. ast_debug(1, "[%s] error sending VGS/VGM\n", pvt->id);
  3538. mbl_queue_hangup(pvt);
  3539. ast_mutex_unlock(&pvt->lock);
  3540. goto e_cleanup;
  3541. }
  3542. }
  3543. } else if (pvt->incoming) {
  3544. /* we have an incoming call from the
  3545. * HS, he wants to hang up */
  3546. mbl_queue_hangup(pvt);
  3547. } else {
  3548. /* no call is up, HS wants to dial */
  3549. hsp_send_ok(pvt->rfcomm_socket);
  3550. if ((pvt->sco_socket = sco_connect(pvt->adapter->addr, pvt->addr)) == -1) {
  3551. ast_log(LOG_ERROR, "[%s] unable to create audio connection\n", pvt->id);
  3552. ast_mutex_unlock(&pvt->lock);
  3553. goto e_cleanup;
  3554. }
  3555. pvt->incoming = 1;
  3556. if (!(chan = mbl_new(AST_STATE_UP, pvt, NULL, NULL, NULL))) {
  3557. ast_log(LOG_ERROR, "[%s] unable to allocate channel for incoming call\n", pvt->id);
  3558. ast_mutex_unlock(&pvt->lock);
  3559. goto e_cleanup;
  3560. }
  3561. ast_channel_set_fd(chan, 0, pvt->sco_socket);
  3562. ast_channel_exten_set(chan, "s");
  3563. if (ast_pbx_start(chan)) {
  3564. ast_log(LOG_ERROR, "[%s] unable to start pbx on incoming call\n", pvt->id);
  3565. ast_hangup(chan);
  3566. ast_mutex_unlock(&pvt->lock);
  3567. goto e_cleanup;
  3568. }
  3569. }
  3570. ast_mutex_unlock(&pvt->lock);
  3571. break;
  3572. default:
  3573. ast_debug(1, "[%s] received unknown AT command: %s (%s)\n", pvt->id, buf, at_msg2str(at_msg));
  3574. if (hsp_send_error(pvt->rfcomm_socket)) {
  3575. ast_debug(1, "[%s] error sending AT message 'ERROR'\n", pvt->id);
  3576. goto e_cleanup;
  3577. }
  3578. break;
  3579. }
  3580. }
  3581. e_cleanup:
  3582. ast_mutex_lock(&pvt->lock);
  3583. if (pvt->owner) {
  3584. ast_debug(1, "[%s] device disconnected, hanging up owner\n", pvt->id);
  3585. mbl_queue_hangup(pvt);
  3586. }
  3587. close(pvt->rfcomm_socket);
  3588. close(pvt->sco_socket);
  3589. pvt->sco_socket = -1;
  3590. pvt->connected = 0;
  3591. pvt->needring = 0;
  3592. pvt->outgoing = 0;
  3593. pvt->incoming = 0;
  3594. pvt->adapter->inuse = 0;
  3595. ast_mutex_unlock(&pvt->lock);
  3596. manager_event(EVENT_FLAG_SYSTEM, "MobileStatus", "Status: Disconnect\r\nDevice: %s\r\n", pvt->id);
  3597. ast_verb(3, "Bluetooth Device %s has disconnected\n", pvt->id);
  3598. return NULL;
  3599. }
  3600. static int start_monitor(struct mbl_pvt *pvt)
  3601. {
  3602. if (pvt->type == MBL_TYPE_PHONE) {
  3603. pvt->hfp->rsock = pvt->rfcomm_socket;
  3604. if (ast_pthread_create_background(&pvt->monitor_thread, NULL, do_monitor_phone, pvt) < 0) {
  3605. pvt->monitor_thread = AST_PTHREADT_NULL;
  3606. return 0;
  3607. }
  3608. } else {
  3609. if (ast_pthread_create_background(&pvt->monitor_thread, NULL, do_monitor_headset, pvt) < 0) {
  3610. pvt->monitor_thread = AST_PTHREADT_NULL;
  3611. return 0;
  3612. }
  3613. }
  3614. return 1;
  3615. }
  3616. static void *do_discovery(void *data)
  3617. {
  3618. struct adapter_pvt *adapter;
  3619. struct mbl_pvt *pvt;
  3620. while (!check_unloading()) {
  3621. AST_RWLIST_RDLOCK(&adapters);
  3622. AST_RWLIST_TRAVERSE(&adapters, adapter, entry) {
  3623. if (!adapter->inuse) {
  3624. AST_RWLIST_RDLOCK(&devices);
  3625. AST_RWLIST_TRAVERSE(&devices, pvt, entry) {
  3626. ast_mutex_lock(&pvt->lock);
  3627. if (!adapter->inuse && !pvt->connected && !strcmp(adapter->id, pvt->adapter->id)) {
  3628. if ((pvt->rfcomm_socket = rfcomm_connect(adapter->addr, pvt->addr, pvt->rfcomm_port)) > -1) {
  3629. if (start_monitor(pvt)) {
  3630. pvt->connected = 1;
  3631. adapter->inuse = 1;
  3632. manager_event(EVENT_FLAG_SYSTEM, "MobileStatus", "Status: Connect\r\nDevice: %s\r\n", pvt->id);
  3633. ast_verb(3, "Bluetooth Device %s has connected, initializing...\n", pvt->id);
  3634. }
  3635. }
  3636. }
  3637. ast_mutex_unlock(&pvt->lock);
  3638. }
  3639. AST_RWLIST_UNLOCK(&devices);
  3640. }
  3641. }
  3642. AST_RWLIST_UNLOCK(&adapters);
  3643. /* Go to sleep (only if we are not unloading) */
  3644. if (!check_unloading())
  3645. sleep(discovery_interval);
  3646. }
  3647. return NULL;
  3648. }
  3649. /*!
  3650. * \brief Service new and existing SCO connections.
  3651. * This thread accepts new sco connections and handles audio data. There is
  3652. * one do_sco_listen thread for each adapter.
  3653. */
  3654. static void *do_sco_listen(void *data)
  3655. {
  3656. struct adapter_pvt *adapter = (struct adapter_pvt *) data;
  3657. while (!check_unloading()) {
  3658. /* check for new sco connections */
  3659. if (ast_io_wait(adapter->accept_io, 0) == -1) {
  3660. /* handle errors */
  3661. ast_log(LOG_ERROR, "ast_io_wait() failed for adapter %s\n", adapter->id);
  3662. break;
  3663. }
  3664. /* handle audio data */
  3665. if (ast_io_wait(adapter->io, 1) == -1) {
  3666. ast_log(LOG_ERROR, "ast_io_wait() failed for audio on adapter %s\n", adapter->id);
  3667. break;
  3668. }
  3669. }
  3670. return NULL;
  3671. }
  3672. /*
  3673. Module
  3674. */
  3675. /*!
  3676. * \brief Load an adapter from the configuration file.
  3677. * \param cfg the config to load the adapter from
  3678. * \param cat the adapter to load
  3679. *
  3680. * This function loads the given adapter and starts the sco listener thread for
  3681. * that adapter.
  3682. *
  3683. * \return NULL on error, a pointer to the adapter that was loaded on success
  3684. */
  3685. static struct adapter_pvt *mbl_load_adapter(struct ast_config *cfg, const char *cat)
  3686. {
  3687. const char *id, *address;
  3688. struct adapter_pvt *adapter;
  3689. struct ast_variable *v;
  3690. struct hci_dev_req dr;
  3691. uint16_t vs;
  3692. id = ast_variable_retrieve(cfg, cat, "id");
  3693. address = ast_variable_retrieve(cfg, cat, "address");
  3694. if (ast_strlen_zero(id) || ast_strlen_zero(address)) {
  3695. ast_log(LOG_ERROR, "Skipping adapter. Missing id or address settings.\n");
  3696. goto e_return;
  3697. }
  3698. ast_debug(1, "Reading configuration for adapter %s %s.\n", id, address);
  3699. if (!(adapter = ast_calloc(1, sizeof(*adapter)))) {
  3700. ast_log(LOG_ERROR, "Skipping adapter %s. Error allocating memory.\n", id);
  3701. goto e_return;
  3702. }
  3703. ast_copy_string(adapter->id, id, sizeof(adapter->id));
  3704. str2ba(address, &adapter->addr);
  3705. /* attempt to connect to the adapter */
  3706. adapter->dev_id = hci_devid(address);
  3707. adapter->hci_socket = hci_open_dev(adapter->dev_id);
  3708. if (adapter->dev_id < 0 || adapter->hci_socket < 0) {
  3709. ast_log(LOG_ERROR, "Skipping adapter %s. Unable to communicate with adapter.\n", adapter->id);
  3710. goto e_free_adapter;
  3711. }
  3712. /* check voice setting */
  3713. hci_read_voice_setting(adapter->hci_socket, &vs, 1000);
  3714. vs = htobs(vs);
  3715. if (vs != 0x0060) {
  3716. ast_log(LOG_ERROR, "Skipping adapter %s. Voice setting must be 0x0060 - see 'man hciconfig' for details.\n", adapter->id);
  3717. goto e_hci_close_dev;
  3718. }
  3719. for (v = ast_variable_browse(cfg, cat); v; v = v->next) {
  3720. if (!strcasecmp(v->name, "forcemaster")) {
  3721. if (ast_true(v->value)) {
  3722. dr.dev_id = adapter->dev_id;
  3723. if (hci_strtolm("master", &dr.dev_opt)) {
  3724. if (ioctl(adapter->hci_socket, HCISETLINKMODE, (unsigned long) &dr) < 0) {
  3725. ast_log(LOG_WARNING, "Unable to set adapter %s link mode to MASTER. Ignoring 'forcemaster' option.\n", adapter->id);
  3726. }
  3727. }
  3728. }
  3729. } else if (!strcasecmp(v->name, "alignmentdetection")) {
  3730. adapter->alignment_detection = ast_true(v->value);
  3731. }
  3732. }
  3733. /* create io contexts */
  3734. if (!(adapter->accept_io = io_context_create())) {
  3735. ast_log(LOG_ERROR, "Unable to create I/O context for audio connection listener\n");
  3736. goto e_hci_close_dev;
  3737. }
  3738. if (!(adapter->io = io_context_create())) {
  3739. ast_log(LOG_ERROR, "Unable to create I/O context for audio connections\n");
  3740. goto e_destroy_accept_io;
  3741. }
  3742. /* bind the sco listener socket */
  3743. if (sco_bind(adapter) < 0) {
  3744. ast_log(LOG_ERROR, "Skipping adapter %s. Error binding audio connection listerner socket.\n", adapter->id);
  3745. goto e_destroy_io;
  3746. }
  3747. /* add the socket to the io context */
  3748. if (!(adapter->sco_id = ast_io_add(adapter->accept_io, adapter->sco_socket, sco_accept, AST_IO_IN, adapter))) {
  3749. ast_log(LOG_ERROR, "Skipping adapter %s. Error adding listener socket to I/O context.\n", adapter->id);
  3750. goto e_close_sco;
  3751. }
  3752. /* start the sco listener for this adapter */
  3753. if (ast_pthread_create_background(&adapter->sco_listener_thread, NULL, do_sco_listen, adapter)) {
  3754. ast_log(LOG_ERROR, "Skipping adapter %s. Error creating audio connection listerner thread.\n", adapter->id);
  3755. goto e_remove_sco;
  3756. }
  3757. /* add the adapter to our global list */
  3758. AST_RWLIST_WRLOCK(&adapters);
  3759. AST_RWLIST_INSERT_HEAD(&adapters, adapter, entry);
  3760. AST_RWLIST_UNLOCK(&adapters);
  3761. ast_debug(1, "Loaded adapter %s %s.\n", adapter->id, address);
  3762. return adapter;
  3763. e_remove_sco:
  3764. ast_io_remove(adapter->accept_io, adapter->sco_id);
  3765. e_close_sco:
  3766. close(adapter->sco_socket);
  3767. e_destroy_io:
  3768. io_context_destroy(adapter->io);
  3769. e_destroy_accept_io:
  3770. io_context_destroy(adapter->accept_io);
  3771. e_hci_close_dev:
  3772. hci_close_dev(adapter->hci_socket);
  3773. e_free_adapter:
  3774. ast_free(adapter);
  3775. e_return:
  3776. return NULL;
  3777. }
  3778. /*!
  3779. * \brief Load a device from the configuration file.
  3780. * \param cfg the config to load the device from
  3781. * \param cat the device to load
  3782. * \return NULL on error, a pointer to the device that was loaded on success
  3783. */
  3784. static struct mbl_pvt *mbl_load_device(struct ast_config *cfg, const char *cat)
  3785. {
  3786. struct mbl_pvt *pvt;
  3787. struct adapter_pvt *adapter;
  3788. struct ast_variable *v;
  3789. const char *address, *adapter_str, *port;
  3790. ast_debug(1, "Reading configuration for device %s.\n", cat);
  3791. adapter_str = ast_variable_retrieve(cfg, cat, "adapter");
  3792. if(ast_strlen_zero(adapter_str)) {
  3793. ast_log(LOG_ERROR, "Skipping device %s. No adapter specified.\n", cat);
  3794. goto e_return;
  3795. }
  3796. /* find the adapter */
  3797. AST_RWLIST_RDLOCK(&adapters);
  3798. AST_RWLIST_TRAVERSE(&adapters, adapter, entry) {
  3799. if (!strcmp(adapter->id, adapter_str))
  3800. break;
  3801. }
  3802. AST_RWLIST_UNLOCK(&adapters);
  3803. if (!adapter) {
  3804. ast_log(LOG_ERROR, "Skiping device %s. Unknown adapter '%s' specified.\n", cat, adapter_str);
  3805. goto e_return;
  3806. }
  3807. address = ast_variable_retrieve(cfg, cat, "address");
  3808. port = ast_variable_retrieve(cfg, cat, "port");
  3809. if (ast_strlen_zero(port) || ast_strlen_zero(address)) {
  3810. ast_log(LOG_ERROR, "Skipping device %s. Missing required port or address setting.\n", cat);
  3811. goto e_return;
  3812. }
  3813. /* create and initialize our pvt structure */
  3814. if (!(pvt = ast_calloc(1, sizeof(*pvt)))) {
  3815. ast_log(LOG_ERROR, "Skipping device %s. Error allocating memory.\n", cat);
  3816. goto e_return;
  3817. }
  3818. ast_mutex_init(&pvt->lock);
  3819. AST_LIST_HEAD_INIT_NOLOCK(&pvt->msg_queue);
  3820. /* set some defaults */
  3821. pvt->type = MBL_TYPE_PHONE;
  3822. ast_copy_string(pvt->context, "default", sizeof(pvt->context));
  3823. /* populate the pvt structure */
  3824. pvt->adapter = adapter;
  3825. ast_copy_string(pvt->id, cat, sizeof(pvt->id));
  3826. str2ba(address, &pvt->addr);
  3827. pvt->timeout = -1;
  3828. pvt->rfcomm_socket = -1;
  3829. pvt->rfcomm_port = atoi(port);
  3830. pvt->sco_socket = -1;
  3831. pvt->monitor_thread = AST_PTHREADT_NULL;
  3832. pvt->ring_sched_id = -1;
  3833. pvt->has_sms = 1;
  3834. /* setup the smoother */
  3835. if (!(pvt->smoother = ast_smoother_new(DEVICE_FRAME_SIZE))) {
  3836. ast_log(LOG_ERROR, "Skipping device %s. Error setting up frame smoother.\n", cat);
  3837. goto e_free_pvt;
  3838. }
  3839. /* setup the dsp */
  3840. if (!(pvt->dsp = ast_dsp_new())) {
  3841. ast_log(LOG_ERROR, "Skipping device %s. Error setting up dsp for dtmf detection.\n", cat);
  3842. goto e_free_smoother;
  3843. }
  3844. /* setup the scheduler */
  3845. if (!(pvt->sched = ast_sched_context_create())) {
  3846. ast_log(LOG_ERROR, "Unable to create scheduler context for headset device\n");
  3847. goto e_free_dsp;
  3848. }
  3849. ast_dsp_set_features(pvt->dsp, DSP_FEATURE_DIGIT_DETECT);
  3850. ast_dsp_set_digitmode(pvt->dsp, DSP_DIGITMODE_DTMF | DSP_DIGITMODE_RELAXDTMF);
  3851. for (v = ast_variable_browse(cfg, cat); v; v = v->next) {
  3852. if (!strcasecmp(v->name, "type")) {
  3853. if (!strcasecmp(v->value, "headset"))
  3854. pvt->type = MBL_TYPE_HEADSET;
  3855. else
  3856. pvt->type = MBL_TYPE_PHONE;
  3857. } else if (!strcasecmp(v->name, "context")) {
  3858. ast_copy_string(pvt->context, v->value, sizeof(pvt->context));
  3859. } else if (!strcasecmp(v->name, "group")) {
  3860. /* group is set to 0 if invalid */
  3861. pvt->group = atoi(v->value);
  3862. } else if (!strcasecmp(v->name, "sms")) {
  3863. pvt->has_sms = ast_true(v->value);
  3864. } else if (!strcasecmp(v->name, "nocallsetup")) {
  3865. pvt->no_callsetup = ast_true(v->value);
  3866. if (pvt->no_callsetup)
  3867. ast_debug(1, "Setting nocallsetup mode for device %s.\n", pvt->id);
  3868. } else if (!strcasecmp(v->name, "blackberry")) {
  3869. pvt->blackberry = ast_true(v->value);
  3870. pvt->has_sms = 0;
  3871. }
  3872. }
  3873. if (pvt->type == MBL_TYPE_PHONE) {
  3874. if (!(pvt->hfp = ast_calloc(1, sizeof(*pvt->hfp)))) {
  3875. ast_log(LOG_ERROR, "Skipping device %s. Error allocating memory.\n", pvt->id);
  3876. goto e_free_sched;
  3877. }
  3878. pvt->hfp->owner = pvt;
  3879. pvt->hfp->rport = pvt->rfcomm_port;
  3880. pvt->hfp->nocallsetup = pvt->no_callsetup;
  3881. } else {
  3882. pvt->has_sms = 0;
  3883. }
  3884. AST_RWLIST_WRLOCK(&devices);
  3885. AST_RWLIST_INSERT_HEAD(&devices, pvt, entry);
  3886. AST_RWLIST_UNLOCK(&devices);
  3887. ast_debug(1, "Loaded device %s.\n", pvt->id);
  3888. return pvt;
  3889. e_free_sched:
  3890. ast_sched_context_destroy(pvt->sched);
  3891. e_free_dsp:
  3892. ast_dsp_free(pvt->dsp);
  3893. e_free_smoother:
  3894. ast_smoother_free(pvt->smoother);
  3895. e_free_pvt:
  3896. ast_free(pvt);
  3897. e_return:
  3898. return NULL;
  3899. }
  3900. static int mbl_load_config(void)
  3901. {
  3902. struct ast_config *cfg;
  3903. const char *cat;
  3904. struct ast_variable *v;
  3905. struct ast_flags config_flags = { 0 };
  3906. cfg = ast_config_load(MBL_CONFIG, config_flags);
  3907. if (!cfg) {
  3908. cfg = ast_config_load(MBL_CONFIG_OLD, config_flags);
  3909. }
  3910. if (!cfg)
  3911. return -1;
  3912. /* parse [general] section */
  3913. for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
  3914. if (!strcasecmp(v->name, "interval")) {
  3915. if (!sscanf(v->value, "%d", &discovery_interval)) {
  3916. ast_log(LOG_NOTICE, "error parsing 'interval' in general section, using default value\n");
  3917. }
  3918. }
  3919. }
  3920. /* load adapters */
  3921. for (cat = ast_category_browse(cfg, NULL); cat; cat = ast_category_browse(cfg, cat)) {
  3922. if (!strcasecmp(cat, "adapter")) {
  3923. mbl_load_adapter(cfg, cat);
  3924. }
  3925. }
  3926. if (AST_RWLIST_EMPTY(&adapters)) {
  3927. ast_log(LOG_ERROR,
  3928. "***********************************************************************\n"
  3929. "No adapters could be loaded from the configuration file.\n"
  3930. "Please review mobile.conf. See sample for details.\n"
  3931. "***********************************************************************\n"
  3932. );
  3933. ast_config_destroy(cfg);
  3934. return -1;
  3935. }
  3936. /* now load devices */
  3937. for (cat = ast_category_browse(cfg, NULL); cat; cat = ast_category_browse(cfg, cat)) {
  3938. if (strcasecmp(cat, "general") && strcasecmp(cat, "adapter")) {
  3939. mbl_load_device(cfg, cat);
  3940. }
  3941. }
  3942. ast_config_destroy(cfg);
  3943. return 0;
  3944. }
  3945. /*!
  3946. * \brief Check if the module is unloading.
  3947. * \retval 0 not unloading
  3948. * \retval 1 unloading
  3949. */
  3950. static inline int check_unloading()
  3951. {
  3952. int res;
  3953. ast_mutex_lock(&unload_mutex);
  3954. res = unloading_flag;
  3955. ast_mutex_unlock(&unload_mutex);
  3956. return res;
  3957. }
  3958. /*!
  3959. * \brief Set the unloading flag.
  3960. */
  3961. static inline void set_unloading()
  3962. {
  3963. ast_mutex_lock(&unload_mutex);
  3964. unloading_flag = 1;
  3965. ast_mutex_unlock(&unload_mutex);
  3966. }
  3967. static int unload_module(void)
  3968. {
  3969. struct mbl_pvt *pvt;
  3970. struct adapter_pvt *adapter;
  3971. /* First, take us out of the channel loop */
  3972. ast_channel_unregister(&mbl_tech);
  3973. /* Unregister the CLI & APP */
  3974. ast_cli_unregister_multiple(mbl_cli, sizeof(mbl_cli) / sizeof(mbl_cli[0]));
  3975. ast_unregister_application(app_mblstatus);
  3976. ast_unregister_application(app_mblsendsms);
  3977. /* signal everyone we are unloading */
  3978. set_unloading();
  3979. /* Kill the discovery thread */
  3980. if (discovery_thread != AST_PTHREADT_NULL) {
  3981. pthread_kill(discovery_thread, SIGURG);
  3982. pthread_join(discovery_thread, NULL);
  3983. }
  3984. /* stop the sco listener threads */
  3985. AST_RWLIST_WRLOCK(&adapters);
  3986. AST_RWLIST_TRAVERSE(&adapters, adapter, entry) {
  3987. pthread_kill(adapter->sco_listener_thread, SIGURG);
  3988. pthread_join(adapter->sco_listener_thread, NULL);
  3989. }
  3990. AST_RWLIST_UNLOCK(&adapters);
  3991. /* Destroy the device list */
  3992. AST_RWLIST_WRLOCK(&devices);
  3993. while ((pvt = AST_RWLIST_REMOVE_HEAD(&devices, entry))) {
  3994. if (pvt->monitor_thread != AST_PTHREADT_NULL) {
  3995. pthread_kill(pvt->monitor_thread, SIGURG);
  3996. pthread_join(pvt->monitor_thread, NULL);
  3997. }
  3998. close(pvt->sco_socket);
  3999. close(pvt->rfcomm_socket);
  4000. msg_queue_flush(pvt);
  4001. if (pvt->hfp) {
  4002. ast_free(pvt->hfp);
  4003. }
  4004. ast_smoother_free(pvt->smoother);
  4005. ast_dsp_free(pvt->dsp);
  4006. ast_sched_context_destroy(pvt->sched);
  4007. ast_free(pvt);
  4008. }
  4009. AST_RWLIST_UNLOCK(&devices);
  4010. /* Destroy the adapter list */
  4011. AST_RWLIST_WRLOCK(&adapters);
  4012. while ((adapter = AST_RWLIST_REMOVE_HEAD(&adapters, entry))) {
  4013. close(adapter->sco_socket);
  4014. io_context_destroy(adapter->io);
  4015. io_context_destroy(adapter->accept_io);
  4016. hci_close_dev(adapter->hci_socket);
  4017. ast_free(adapter);
  4018. }
  4019. AST_RWLIST_UNLOCK(&adapters);
  4020. if (sdp_session)
  4021. sdp_close(sdp_session);
  4022. mbl_tech.capabilities = ast_format_cap_destroy(mbl_tech.capabilities);
  4023. return 0;
  4024. }
  4025. static int load_module(void)
  4026. {
  4027. int dev_id, s;
  4028. if (!(mbl_tech.capabilities = ast_format_cap_alloc(0))) {
  4029. return AST_MODULE_LOAD_DECLINE;
  4030. }
  4031. ast_format_set(&prefformat, DEVICE_FRAME_FORMAT, 0);
  4032. ast_format_cap_add(mbl_tech.capabilities, &prefformat);
  4033. /* Check if we have Bluetooth, no point loading otherwise... */
  4034. dev_id = hci_get_route(NULL);
  4035. s = hci_open_dev(dev_id);
  4036. if (dev_id < 0 || s < 0) {
  4037. ast_log(LOG_ERROR, "No Bluetooth devices found. Not loading module.\n");
  4038. return AST_MODULE_LOAD_DECLINE;
  4039. }
  4040. hci_close_dev(s);
  4041. if (mbl_load_config()) {
  4042. ast_log(LOG_ERROR, "Errors reading config file %s. Not loading module.\n", MBL_CONFIG);
  4043. return AST_MODULE_LOAD_DECLINE;
  4044. }
  4045. sdp_session = sdp_register();
  4046. /* Spin the discovery thread */
  4047. if (ast_pthread_create_background(&discovery_thread, NULL, do_discovery, NULL) < 0) {
  4048. ast_log(LOG_ERROR, "Unable to create discovery thread.\n");
  4049. goto e_cleanup;
  4050. }
  4051. /* register our channel type */
  4052. if (ast_channel_register(&mbl_tech)) {
  4053. ast_log(LOG_ERROR, "Unable to register channel class %s\n", "Mobile");
  4054. goto e_cleanup;
  4055. }
  4056. ast_cli_register_multiple(mbl_cli, sizeof(mbl_cli) / sizeof(mbl_cli[0]));
  4057. ast_register_application(app_mblstatus, mbl_status_exec, mblstatus_synopsis, mblstatus_desc);
  4058. ast_register_application(app_mblsendsms, mbl_sendsms_exec, mblsendsms_synopsis, mblsendsms_desc);
  4059. return AST_MODULE_LOAD_SUCCESS;
  4060. e_cleanup:
  4061. if (sdp_session)
  4062. sdp_close(sdp_session);
  4063. return AST_MODULE_LOAD_FAILURE;
  4064. }
  4065. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Bluetooth Mobile Device Channel Driver",
  4066. .load = load_module,
  4067. .unload = unload_module,
  4068. .load_pri = AST_MODPRI_CHANNEL_DRIVER,
  4069. );