chan_mobile.c 125 KB

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