chan_mobile.c 125 KB

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