gatt.c 176 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2014 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include <stdbool.h>
  14. #include <stdlib.h>
  15. #include <stdint.h>
  16. #include <glib.h>
  17. #include <errno.h>
  18. #include <sys/socket.h>
  19. #include "ipc.h"
  20. #include "ipc-common.h"
  21. #include "lib/sdp.h"
  22. #include "lib/sdp_lib.h"
  23. #include "lib/uuid.h"
  24. #include "bluetooth.h"
  25. #include "gatt.h"
  26. #include "src/log.h"
  27. #include "hal-msg.h"
  28. #include "utils.h"
  29. #include "src/shared/util.h"
  30. #include "src/shared/queue.h"
  31. #include "src/shared/att.h"
  32. #include "src/shared/gatt-db.h"
  33. #include "src/shared/ad.h"
  34. #include "attrib/gattrib.h"
  35. #include "attrib/att.h"
  36. #include "attrib/gatt.h"
  37. #include "btio/btio.h"
  38. /* set according to Android bt_gatt_client.h */
  39. #define GATT_MAX_ATTR_LEN 600
  40. #define GATT_SUCCESS 0x00000000
  41. #define GATT_FAILURE 0x00000101
  42. #define BASE_UUID16_OFFSET 12
  43. #define GATT_PERM_READ 0x00000001
  44. #define GATT_PERM_READ_ENCRYPTED 0x00000002
  45. #define GATT_PERM_READ_MITM 0x00000004
  46. #define GATT_PERM_READ_AUTHORIZATION 0x00000008
  47. #define GATT_PERM_WRITE 0x00000100
  48. #define GATT_PERM_WRITE_ENCRYPTED 0x00000200
  49. #define GATT_PERM_WRITE_MITM 0x00000400
  50. #define GATT_PERM_WRITE_AUTHORIZATION 0x00000800
  51. #define GATT_PERM_WRITE_SIGNED 0x00010000
  52. #define GATT_PERM_WRITE_SIGNED_MITM 0x00020000
  53. #define GATT_PERM_NONE 0x10000000
  54. #define GATT_PAIR_CONN_TIMEOUT 30
  55. #define GATT_CONN_TIMEOUT 2
  56. static const uint8_t BLUETOOTH_UUID[] = {
  57. 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
  58. 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  59. };
  60. typedef enum {
  61. DEVICE_DISCONNECTED = 0,
  62. DEVICE_CONNECT_INIT, /* connection procedure initiated */
  63. DEVICE_CONNECT_READY, /* dev found during LE scan */
  64. DEVICE_CONNECTED, /* connection has been established */
  65. } gatt_device_state_t;
  66. static const char *device_state_str[] = {
  67. "DISCONNECTED",
  68. "CONNECT INIT",
  69. "CONNECT READY",
  70. "CONNECTED",
  71. };
  72. struct pending_trans_data {
  73. unsigned int id;
  74. uint8_t opcode;
  75. struct gatt_db_attribute *attrib;
  76. unsigned int serial_id;
  77. };
  78. struct gatt_app {
  79. int32_t id;
  80. uint8_t uuid[16];
  81. gatt_type_t type;
  82. /* Valid for client applications */
  83. struct queue *notifications;
  84. gatt_conn_cb_t func;
  85. struct adv_instance *adv;
  86. };
  87. struct element_id {
  88. bt_uuid_t uuid;
  89. uint8_t instance;
  90. };
  91. struct descriptor {
  92. struct element_id id;
  93. uint16_t handle;
  94. };
  95. struct characteristic {
  96. struct element_id id;
  97. struct gatt_char ch;
  98. uint16_t end_handle;
  99. struct queue *descriptors;
  100. };
  101. struct service {
  102. struct element_id id;
  103. struct gatt_primary prim;
  104. struct gatt_included incl;
  105. bool primary;
  106. struct queue *chars;
  107. struct queue *included; /* Valid only for primary services */
  108. bool incl_search_done;
  109. };
  110. struct notification_data {
  111. struct hal_gatt_srvc_id service;
  112. struct hal_gatt_gatt_id ch;
  113. struct app_connection *conn;
  114. guint notif_id;
  115. guint ind_id;
  116. int ref;
  117. };
  118. struct gatt_device {
  119. bdaddr_t bdaddr;
  120. gatt_device_state_t state;
  121. GAttrib *attrib;
  122. GIOChannel *att_io;
  123. struct queue *services;
  124. bool partial_srvc_search;
  125. guint watch_id;
  126. guint server_id;
  127. guint ind_id;
  128. int ref;
  129. struct queue *autoconnect_apps;
  130. struct queue *pending_requests;
  131. };
  132. struct app_connection {
  133. struct gatt_device *device;
  134. struct gatt_app *app;
  135. struct queue *transactions;
  136. int32_t id;
  137. guint timeout_id;
  138. bool wait_execute_write;
  139. };
  140. struct service_sdp {
  141. int32_t service_handle;
  142. uint32_t sdp_handle;
  143. };
  144. static struct ipc *hal_ipc = NULL;
  145. static bdaddr_t adapter_addr;
  146. static bool scanning = false;
  147. static unsigned int advertising_cnt = 0;
  148. static uint32_t adv_inst_bits = 0;
  149. static struct queue *gatt_apps = NULL;
  150. static struct queue *gatt_devices = NULL;
  151. static struct queue *app_connections = NULL;
  152. static struct queue *services_sdp = NULL;
  153. static struct queue *listen_apps = NULL;
  154. static struct gatt_db *gatt_db = NULL;
  155. static struct gatt_db_attribute *service_changed_attrib = NULL;
  156. static GIOChannel *le_io = NULL;
  157. static GIOChannel *bredr_io = NULL;
  158. static uint32_t gatt_sdp_handle = 0;
  159. static uint32_t gap_sdp_handle = 0;
  160. static uint32_t dis_sdp_handle = 0;
  161. static struct bt_crypto *crypto = NULL;
  162. static int test_client_if = 0;
  163. static const uint8_t TEST_UUID[] = {
  164. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  165. 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04
  166. };
  167. static bool is_bluetooth_uuid(const uint8_t *uuid)
  168. {
  169. int i;
  170. for (i = 0; i < 16; i++) {
  171. /* ignore minimal uuid (16) value */
  172. if (i == 12 || i == 13)
  173. continue;
  174. if (uuid[i] != BLUETOOTH_UUID[i])
  175. return false;
  176. }
  177. return true;
  178. }
  179. static void android2uuid(const uint8_t *uuid, bt_uuid_t *dst)
  180. {
  181. if (is_bluetooth_uuid(uuid)) {
  182. /* copy 16 bit uuid value from full android 128bit uuid */
  183. dst->type = BT_UUID16;
  184. dst->value.u16 = (uuid[13] << 8) + uuid[12];
  185. } else {
  186. int i;
  187. dst->type = BT_UUID128;
  188. for (i = 0; i < 16; i++)
  189. dst->value.u128.data[i] = uuid[15 - i];
  190. }
  191. }
  192. static void uuid2android(const bt_uuid_t *src, uint8_t *uuid)
  193. {
  194. bt_uuid_t uu128;
  195. uint8_t i;
  196. if (src->type != BT_UUID128) {
  197. bt_uuid_to_uuid128(src, &uu128);
  198. src = &uu128;
  199. }
  200. for (i = 0; i < 16; i++)
  201. uuid[15 - i] = src->value.u128.data[i];
  202. }
  203. static void hal_srvc_id_to_element_id(const struct hal_gatt_srvc_id *from,
  204. struct element_id *to)
  205. {
  206. to->instance = from->inst_id;
  207. android2uuid(from->uuid, &to->uuid);
  208. }
  209. static void element_id_to_hal_srvc_id(const struct element_id *from,
  210. uint8_t primary,
  211. struct hal_gatt_srvc_id *to)
  212. {
  213. to->is_primary = primary;
  214. to->inst_id = from->instance;
  215. uuid2android(&from->uuid, to->uuid);
  216. }
  217. static void hal_gatt_id_to_element_id(const struct hal_gatt_gatt_id *from,
  218. struct element_id *to)
  219. {
  220. to->instance = from->inst_id;
  221. android2uuid(from->uuid, &to->uuid);
  222. }
  223. static void element_id_to_hal_gatt_id(const struct element_id *from,
  224. struct hal_gatt_gatt_id *to)
  225. {
  226. to->inst_id = from->instance;
  227. uuid2android(&from->uuid, to->uuid);
  228. }
  229. static void destroy_characteristic(void *data)
  230. {
  231. struct characteristic *chars = data;
  232. if (!chars)
  233. return;
  234. queue_destroy(chars->descriptors, free);
  235. free(chars);
  236. }
  237. static void destroy_service(void *data)
  238. {
  239. struct service *srvc = data;
  240. if (!srvc)
  241. return;
  242. queue_destroy(srvc->chars, destroy_characteristic);
  243. /*
  244. * Included services we keep on two queues.
  245. * 1. On the same queue with primary services.
  246. * 2. On the queue inside primary service.
  247. * So we need to free service memory only once but we need to destroy
  248. * two queues
  249. */
  250. queue_destroy(srvc->included, NULL);
  251. free(srvc);
  252. }
  253. static bool match_app_by_uuid(const void *data, const void *user_data)
  254. {
  255. const uint8_t *exp_uuid = user_data;
  256. const struct gatt_app *client = data;
  257. return !memcmp(exp_uuid, client->uuid, sizeof(client->uuid));
  258. }
  259. static bool match_app_by_id(const void *data, const void *user_data)
  260. {
  261. int32_t exp_id = PTR_TO_INT(user_data);
  262. const struct gatt_app *client = data;
  263. return client->id == exp_id;
  264. }
  265. static struct gatt_app *find_app_by_id(int32_t id)
  266. {
  267. return queue_find(gatt_apps, match_app_by_id, INT_TO_PTR(id));
  268. }
  269. static bool match_device_by_bdaddr(const void *data, const void *user_data)
  270. {
  271. const struct gatt_device *dev = data;
  272. const bdaddr_t *addr = user_data;
  273. return !bacmp(&dev->bdaddr, addr);
  274. }
  275. static bool match_device_by_state(const void *data, const void *user_data)
  276. {
  277. const struct gatt_device *dev = data;
  278. if (dev->state != PTR_TO_UINT(user_data))
  279. return false;
  280. return true;
  281. }
  282. static bool match_pending_device(const void *data, const void *user_data)
  283. {
  284. const struct gatt_device *dev = data;
  285. if ((dev->state == DEVICE_CONNECT_INIT) ||
  286. (dev->state == DEVICE_CONNECT_READY))
  287. return true;
  288. return false;
  289. }
  290. static bool match_connection_by_id(const void *data, const void *user_data)
  291. {
  292. const struct app_connection *conn = data;
  293. const int32_t id = PTR_TO_INT(user_data);
  294. return conn->id == id;
  295. }
  296. static bool match_connection_by_device_and_app(const void *data,
  297. const void *user_data)
  298. {
  299. const struct app_connection *conn = data;
  300. const struct app_connection *match = user_data;
  301. return conn->device == match->device && conn->app == match->app;
  302. }
  303. static struct app_connection *find_connection_by_id(int32_t conn_id)
  304. {
  305. struct app_connection *conn;
  306. conn = queue_find(app_connections, match_connection_by_id,
  307. INT_TO_PTR(conn_id));
  308. if (conn && conn->device->state == DEVICE_CONNECTED)
  309. return conn;
  310. return NULL;
  311. }
  312. static bool match_connection_by_device(const void *data, const void *user_data)
  313. {
  314. const struct app_connection *conn = data;
  315. const struct gatt_device *dev = user_data;
  316. return conn->device == dev;
  317. }
  318. static bool match_connection_by_app(const void *data, const void *user_data)
  319. {
  320. const struct app_connection *conn = data;
  321. const struct gatt_app *app = user_data;
  322. return conn->app == app;
  323. }
  324. static struct gatt_device *find_device_by_addr(const bdaddr_t *addr)
  325. {
  326. return queue_find(gatt_devices, match_device_by_bdaddr, addr);
  327. }
  328. static struct gatt_device *find_pending_device(void)
  329. {
  330. return queue_find(gatt_devices, match_pending_device, NULL);
  331. }
  332. static struct gatt_device *find_device_by_state(uint32_t state)
  333. {
  334. return queue_find(gatt_devices, match_device_by_state,
  335. UINT_TO_PTR(state));
  336. }
  337. static bool match_srvc_by_element_id(const void *data, const void *user_data)
  338. {
  339. const struct element_id *exp_id = user_data;
  340. const struct service *service = data;
  341. if (service->id.instance == exp_id->instance)
  342. return !bt_uuid_cmp(&service->id.uuid, &exp_id->uuid);
  343. return false;
  344. }
  345. static bool match_srvc_by_higher_inst_id(const void *data,
  346. const void *user_data)
  347. {
  348. const struct service *s = data;
  349. uint8_t inst_id = PTR_TO_INT(user_data);
  350. /* For now we match inst_id as it is unique */
  351. return inst_id < s->id.instance;
  352. }
  353. static bool match_srvc_by_bt_uuid(const void *data, const void *user_data)
  354. {
  355. const bt_uuid_t *exp_uuid = user_data;
  356. const struct service *service = data;
  357. return !bt_uuid_cmp(exp_uuid, &service->id.uuid);
  358. }
  359. static bool match_srvc_by_range(const void *data, const void *user_data)
  360. {
  361. const struct service *srvc = data;
  362. const struct att_range *range = user_data;
  363. return !memcmp(&srvc->prim.range, range, sizeof(srvc->prim.range));
  364. }
  365. static bool match_char_by_higher_inst_id(const void *data,
  366. const void *user_data)
  367. {
  368. const struct characteristic *ch = data;
  369. uint8_t inst_id = PTR_TO_INT(user_data);
  370. /* For now we match inst_id as it is unique, we'll match uuids later */
  371. return inst_id < ch->id.instance;
  372. }
  373. static bool match_descr_by_element_id(const void *data, const void *user_data)
  374. {
  375. const struct element_id *exp_id = user_data;
  376. const struct descriptor *descr = data;
  377. if (exp_id->instance == descr->id.instance)
  378. return !bt_uuid_cmp(&descr->id.uuid, &exp_id->uuid);
  379. return false;
  380. }
  381. static bool match_descr_by_higher_inst_id(const void *data,
  382. const void *user_data)
  383. {
  384. const struct descriptor *descr = data;
  385. uint8_t instance = PTR_TO_INT(user_data);
  386. /* For now we match instance as it is unique */
  387. return instance < descr->id.instance;
  388. }
  389. static bool match_notification(const void *a, const void *b)
  390. {
  391. const struct notification_data *a1 = a;
  392. const struct notification_data *b1 = b;
  393. if (a1->conn != b1->conn)
  394. return false;
  395. if (memcmp(&a1->ch, &b1->ch, sizeof(a1->ch)))
  396. return false;
  397. if (memcmp(&a1->service, &b1->service, sizeof(a1->service)))
  398. return false;
  399. return true;
  400. }
  401. static bool match_char_by_element_id(const void *data, const void *user_data)
  402. {
  403. const struct element_id *exp_id = user_data;
  404. const struct characteristic *chars = data;
  405. if (exp_id->instance == chars->id.instance)
  406. return !bt_uuid_cmp(&chars->id.uuid, &exp_id->uuid);
  407. return false;
  408. }
  409. static void destroy_notification(void *data)
  410. {
  411. struct notification_data *notification = data;
  412. struct gatt_app *app;
  413. if (!notification)
  414. return;
  415. if (--notification->ref)
  416. return;
  417. app = notification->conn->app;
  418. queue_remove_if(app->notifications, match_notification, notification);
  419. free(notification);
  420. }
  421. static void unregister_notification(void *data)
  422. {
  423. struct notification_data *notification = data;
  424. struct gatt_device *dev = notification->conn->device;
  425. /*
  426. * No device means it was already disconnected and client cleanup was
  427. * triggered afterwards, but once client unregisters, device stays if
  428. * used by others. Then just unregister single handle.
  429. */
  430. if (!queue_find(gatt_devices, NULL, dev))
  431. return;
  432. if (notification->notif_id && dev)
  433. g_attrib_unregister(dev->attrib, notification->notif_id);
  434. if (notification->ind_id && dev)
  435. g_attrib_unregister(dev->attrib, notification->ind_id);
  436. }
  437. static void device_set_state(struct gatt_device *dev, uint32_t state)
  438. {
  439. char bda[18];
  440. if (dev->state == state)
  441. return;
  442. ba2str(&dev->bdaddr, bda);
  443. DBG("gatt: Device %s state changed %s -> %s", bda,
  444. device_state_str[dev->state], device_state_str[state]);
  445. dev->state = state;
  446. }
  447. static bool auto_connect_le(struct gatt_device *dev)
  448. {
  449. /* For LE devices use auto connect feature if possible */
  450. if (bt_kernel_conn_control()) {
  451. if (!bt_auto_connect_add(bt_get_id_addr(&dev->bdaddr, NULL)))
  452. return false;
  453. } else {
  454. /* Trigger discovery if not already started */
  455. if (!scanning && !bt_le_discovery_start()) {
  456. error("gatt: Could not start scan");
  457. return false;
  458. }
  459. }
  460. device_set_state(dev, DEVICE_CONNECT_INIT);
  461. return true;
  462. }
  463. static void connection_cleanup(struct gatt_device *device)
  464. {
  465. if (device->watch_id) {
  466. g_source_remove(device->watch_id);
  467. device->watch_id = 0;
  468. }
  469. if (device->att_io) {
  470. g_io_channel_shutdown(device->att_io, FALSE, NULL);
  471. g_io_channel_unref(device->att_io);
  472. device->att_io = NULL;
  473. }
  474. if (device->attrib) {
  475. GAttrib *attrib = device->attrib;
  476. if (device->server_id > 0)
  477. g_attrib_unregister(device->attrib, device->server_id);
  478. if (device->ind_id > 0)
  479. g_attrib_unregister(device->attrib, device->ind_id);
  480. device->attrib = NULL;
  481. g_attrib_cancel_all(attrib);
  482. g_attrib_unref(attrib);
  483. }
  484. /*
  485. * If device was in connection_pending or connectable state we
  486. * search device list if we should stop the scan.
  487. */
  488. if (!scanning && (device->state == DEVICE_CONNECT_INIT ||
  489. device->state == DEVICE_CONNECT_READY)) {
  490. if (!find_pending_device())
  491. bt_le_discovery_stop(NULL);
  492. }
  493. /* If device is not bonded service cache should be refreshed */
  494. if (!bt_device_is_bonded(&device->bdaddr))
  495. queue_remove_all(device->services, NULL, NULL, destroy_service);
  496. device_set_state(device, DEVICE_DISCONNECTED);
  497. if (!queue_isempty(device->autoconnect_apps))
  498. auto_connect_le(device);
  499. else
  500. bt_auto_connect_remove(&device->bdaddr);
  501. }
  502. static void free_adv_instance(struct adv_instance *adv)
  503. {
  504. if (!adv)
  505. return;
  506. if (adv->instance)
  507. adv_inst_bits &= ~(1 << (adv->instance - 1));
  508. bt_ad_unref(adv->ad);
  509. bt_ad_unref(adv->sr);
  510. free(adv);
  511. }
  512. static void destroy_gatt_app(void *data)
  513. {
  514. struct gatt_app *app = data;
  515. if (!app)
  516. return;
  517. /*
  518. * First we want to get all notifications and unregister them.
  519. * We don't pass unregister_notification to queue_destroy,
  520. * because destroy notification performs operations on queue
  521. * too. So remove all elements and then destroy queue.
  522. */
  523. if (app->type == GATT_CLIENT)
  524. while (queue_peek_head(app->notifications)) {
  525. struct notification_data *notification;
  526. notification = queue_pop_head(app->notifications);
  527. unregister_notification(notification);
  528. }
  529. queue_destroy(app->notifications, free);
  530. free_adv_instance(app->adv);
  531. free(app);
  532. }
  533. struct pending_request {
  534. struct gatt_db_attribute *attrib;
  535. int length;
  536. uint8_t *value;
  537. uint16_t offset;
  538. uint8_t *filter_value;
  539. uint16_t filter_vlen;
  540. bool completed;
  541. uint8_t error;
  542. };
  543. static void destroy_pending_request(void *data)
  544. {
  545. struct pending_request *entry = data;
  546. if (!entry)
  547. return;
  548. free(entry->value);
  549. free(entry->filter_value);
  550. free(entry);
  551. }
  552. static void destroy_device(void *data)
  553. {
  554. struct gatt_device *dev = data;
  555. if (!dev)
  556. return;
  557. queue_destroy(dev->services, destroy_service);
  558. queue_destroy(dev->pending_requests, destroy_pending_request);
  559. queue_destroy(dev->autoconnect_apps, NULL);
  560. bt_auto_connect_remove(&dev->bdaddr);
  561. free(dev);
  562. }
  563. static struct gatt_device *device_ref(struct gatt_device *device)
  564. {
  565. if (!device)
  566. return NULL;
  567. device->ref++;
  568. return device;
  569. }
  570. static void device_unref(struct gatt_device *device)
  571. {
  572. if (!device)
  573. return;
  574. if (--device->ref)
  575. return;
  576. destroy_device(device);
  577. }
  578. static struct gatt_device *create_device(const bdaddr_t *addr)
  579. {
  580. struct gatt_device *dev;
  581. dev = new0(struct gatt_device, 1);
  582. bacpy(&dev->bdaddr, addr);
  583. dev->services = queue_new();
  584. dev->autoconnect_apps = queue_new();
  585. dev->pending_requests = queue_new();
  586. queue_push_head(gatt_devices, dev);
  587. return device_ref(dev);
  588. }
  589. static void send_client_connect_status_notify(struct app_connection *conn,
  590. int32_t status)
  591. {
  592. struct hal_ev_gatt_client_connect ev;
  593. if (conn->app->func) {
  594. conn->app->func(&conn->device->bdaddr,
  595. status == GATT_SUCCESS ? 0 : -ENOTCONN,
  596. conn->device->attrib);
  597. return;
  598. }
  599. ev.client_if = conn->app->id;
  600. ev.conn_id = conn->id;
  601. ev.status = status;
  602. bdaddr2android(&conn->device->bdaddr, &ev.bda);
  603. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT, HAL_EV_GATT_CLIENT_CONNECT,
  604. sizeof(ev), &ev);
  605. }
  606. static void send_server_connection_state_notify(struct app_connection *conn,
  607. bool connected)
  608. {
  609. struct hal_ev_gatt_server_connection ev;
  610. if (conn->app->func) {
  611. conn->app->func(&conn->device->bdaddr,
  612. connected ? 0 : -ENOTCONN,
  613. conn->device->attrib);
  614. return;
  615. }
  616. ev.server_if = conn->app->id;
  617. ev.conn_id = conn->id;
  618. ev.connected = connected;
  619. bdaddr2android(&conn->device->bdaddr, &ev.bdaddr);
  620. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  621. HAL_EV_GATT_SERVER_CONNECTION, sizeof(ev), &ev);
  622. }
  623. static void send_client_disconnect_status_notify(struct app_connection *conn,
  624. int32_t status)
  625. {
  626. struct hal_ev_gatt_client_disconnect ev;
  627. if (conn->app->func) {
  628. conn->app->func(&conn->device->bdaddr, -ENOTCONN,
  629. conn->device->attrib);
  630. return;
  631. }
  632. ev.client_if = conn->app->id;
  633. ev.conn_id = conn->id;
  634. ev.status = status;
  635. bdaddr2android(&conn->device->bdaddr, &ev.bda);
  636. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  637. HAL_EV_GATT_CLIENT_DISCONNECT, sizeof(ev), &ev);
  638. }
  639. static void notify_app_disconnect_status(struct app_connection *conn,
  640. int32_t status)
  641. {
  642. if (!conn->app)
  643. return;
  644. if (conn->app->type == GATT_CLIENT)
  645. send_client_disconnect_status_notify(conn, status);
  646. else
  647. send_server_connection_state_notify(conn, !!status);
  648. }
  649. static void notify_app_connect_status(struct app_connection *conn,
  650. int32_t status)
  651. {
  652. if (!conn->app)
  653. return;
  654. if (conn->app->type == GATT_CLIENT)
  655. send_client_connect_status_notify(conn, status);
  656. else
  657. send_server_connection_state_notify(conn, !status);
  658. }
  659. static void destroy_connection(void *data)
  660. {
  661. struct app_connection *conn = data;
  662. if (!conn)
  663. return;
  664. if (conn->timeout_id > 0)
  665. g_source_remove(conn->timeout_id);
  666. switch (conn->device->state) {
  667. case DEVICE_CONNECTED:
  668. notify_app_disconnect_status(conn, GATT_SUCCESS);
  669. break;
  670. case DEVICE_CONNECT_INIT:
  671. case DEVICE_CONNECT_READY:
  672. notify_app_connect_status(conn, GATT_FAILURE);
  673. break;
  674. case DEVICE_DISCONNECTED:
  675. break;
  676. }
  677. if (!queue_find(app_connections, match_connection_by_device,
  678. conn->device))
  679. connection_cleanup(conn->device);
  680. queue_destroy(conn->transactions, free);
  681. device_unref(conn->device);
  682. free(conn);
  683. }
  684. static gboolean disconnected_cb(GIOChannel *io, GIOCondition cond,
  685. gpointer user_data)
  686. {
  687. struct gatt_device *dev = user_data;
  688. int sock, err = 0;
  689. socklen_t len;
  690. sock = g_io_channel_unix_get_fd(io);
  691. len = sizeof(err);
  692. if (!getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &len))
  693. DBG("%s (%d)", strerror(err), err);
  694. queue_remove_all(app_connections, match_connection_by_device, dev,
  695. destroy_connection);
  696. return FALSE;
  697. }
  698. static bool get_local_mtu(struct gatt_device *dev, uint16_t *mtu)
  699. {
  700. GIOChannel *io;
  701. uint16_t imtu, omtu;
  702. io = g_attrib_get_channel(dev->attrib);
  703. if (!bt_io_get(io, NULL, BT_IO_OPT_IMTU, &imtu, BT_IO_OPT_OMTU, &omtu,
  704. BT_IO_OPT_INVALID)) {
  705. error("gatt: Failed to get local MTU");
  706. return false;
  707. }
  708. /*
  709. * Limit MTU to MIN(IMTU, OMTU). This is to avoid situation where
  710. * local OMTU < MIN(remote MTU, IMTU)
  711. */
  712. if (mtu)
  713. *mtu = MIN(imtu, omtu);
  714. return true;
  715. }
  716. static void notify_client_mtu_change(struct app_connection *conn, bool success)
  717. {
  718. struct hal_ev_gatt_client_configure_mtu ev;
  719. size_t mtu;
  720. g_attrib_get_buffer(conn->device->attrib, &mtu);
  721. ev.conn_id = conn->id;
  722. ev.status = success ? GATT_SUCCESS : GATT_FAILURE;
  723. ev.mtu = mtu;
  724. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  725. HAL_EV_GATT_CLIENT_CONFIGURE_MTU, sizeof(ev), &ev);
  726. }
  727. static void notify_server_mtu(struct app_connection *conn)
  728. {
  729. struct hal_ev_gatt_server_mtu_changed ev;
  730. size_t mtu;
  731. g_attrib_get_buffer(conn->device->attrib, &mtu);
  732. ev.conn_id = conn->id;
  733. ev.mtu = mtu;
  734. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  735. HAL_EV_GATT_SERVER_MTU_CHANGED, sizeof(ev), &ev);
  736. }
  737. static void notify_mtu_change(void *data, void *user_data)
  738. {
  739. struct gatt_device *device = user_data;
  740. struct app_connection *conn = data;
  741. if (conn->device != device)
  742. return;
  743. if (!conn->app) {
  744. error("gatt: can't notify mtu - no app registered for conn");
  745. return;
  746. }
  747. switch (conn->app->type) {
  748. case GATT_CLIENT:
  749. notify_client_mtu_change(conn, true);
  750. break;
  751. case GATT_SERVER:
  752. notify_server_mtu(conn);
  753. break;
  754. default:
  755. break;
  756. }
  757. }
  758. static bool update_mtu(struct gatt_device *device, uint16_t rmtu)
  759. {
  760. uint16_t mtu, lmtu;
  761. if (!get_local_mtu(device, &lmtu))
  762. return false;
  763. DBG("remote_mtu:%d local_mtu:%d", rmtu, lmtu);
  764. if (rmtu < ATT_DEFAULT_LE_MTU) {
  765. error("gatt: remote MTU invalid (%u bytes)", rmtu);
  766. return false;
  767. }
  768. mtu = MIN(lmtu, rmtu);
  769. if (mtu == ATT_DEFAULT_LE_MTU)
  770. return true;
  771. if (!g_attrib_set_mtu(device->attrib, mtu)) {
  772. error("gatt: Failed to set MTU");
  773. return false;
  774. }
  775. queue_foreach(app_connections, notify_mtu_change, device);
  776. return true;
  777. }
  778. static void att_handler(const uint8_t *ipdu, uint16_t len, gpointer user_data);
  779. static void exchange_mtu_cb(guint8 status, const guint8 *pdu, guint16 plen,
  780. gpointer user_data)
  781. {
  782. struct gatt_device *device = user_data;
  783. uint16_t rmtu;
  784. DBG("");
  785. if (status) {
  786. error("gatt: MTU exchange: %s", att_ecode2str(status));
  787. goto failed;
  788. }
  789. if (!dec_mtu_resp(pdu, plen, &rmtu)) {
  790. error("gatt: MTU exchange: protocol error");
  791. goto failed;
  792. }
  793. update_mtu(device, rmtu);
  794. failed:
  795. device_unref(device);
  796. }
  797. static void send_exchange_mtu_request(struct gatt_device *device)
  798. {
  799. uint16_t mtu;
  800. if (!get_local_mtu(device, &mtu))
  801. return;
  802. DBG("mtu %u", mtu);
  803. if (!gatt_exchange_mtu(device->attrib, mtu, exchange_mtu_cb,
  804. device_ref(device)))
  805. device_unref(device);
  806. }
  807. static void ignore_confirmation_cb(guint8 status, const guint8 *pdu,
  808. guint16 len, gpointer user_data)
  809. {
  810. /* Ignored. */
  811. }
  812. static void notify_att_range_change(struct gatt_device *dev,
  813. struct att_range *range)
  814. {
  815. uint16_t handle;
  816. uint16_t length = 0;
  817. uint16_t ccc;
  818. uint8_t *pdu;
  819. size_t mtu;
  820. GAttribResultFunc confirmation_cb = NULL;
  821. handle = gatt_db_attribute_get_handle(service_changed_attrib);
  822. if (!handle)
  823. return;
  824. ccc = bt_get_gatt_ccc(&dev->bdaddr);
  825. if (!ccc)
  826. return;
  827. pdu = g_attrib_get_buffer(dev->attrib, &mtu);
  828. switch (ccc) {
  829. case 0x0001:
  830. length = enc_notification(handle, (uint8_t *) range,
  831. sizeof(*range), pdu, mtu);
  832. break;
  833. case 0x0002:
  834. length = enc_indication(handle, (uint8_t *) range,
  835. sizeof(*range), pdu, mtu);
  836. confirmation_cb = ignore_confirmation_cb;
  837. break;
  838. default:
  839. /* 0xfff4 reserved for future use */
  840. break;
  841. }
  842. g_attrib_send(dev->attrib, 0, pdu, length, confirmation_cb, NULL, NULL);
  843. }
  844. static struct app_connection *create_connection(struct gatt_device *device,
  845. struct gatt_app *app)
  846. {
  847. struct app_connection *new_conn;
  848. static int32_t last_conn_id = 1;
  849. /* Check if already connected */
  850. new_conn = new0(struct app_connection, 1);
  851. /* Make connection id unique to connection record (app, device) pair */
  852. new_conn->app = app;
  853. new_conn->id = last_conn_id++;
  854. new_conn->transactions = queue_new();
  855. queue_push_head(app_connections, new_conn);
  856. new_conn->device = device_ref(device);
  857. return new_conn;
  858. }
  859. static struct service *create_service(uint8_t id, bool primary, char *uuid,
  860. void *data)
  861. {
  862. struct service *s;
  863. s = new0(struct service, 1);
  864. if (bt_string_to_uuid(&s->id.uuid, uuid) < 0) {
  865. error("gatt: Cannot convert string to uuid");
  866. free(s);
  867. return NULL;
  868. }
  869. s->chars = queue_new();
  870. s->included = queue_new();
  871. s->id.instance = id;
  872. /* Put primary service to our local list */
  873. s->primary = primary;
  874. if (s->primary)
  875. memcpy(&s->prim, data, sizeof(s->prim));
  876. else
  877. memcpy(&s->incl, data, sizeof(s->incl));
  878. return s;
  879. }
  880. static void send_client_primary_notify(void *data, void *user_data)
  881. {
  882. struct hal_ev_gatt_client_search_result ev;
  883. struct service *p = data;
  884. int32_t conn_id = PTR_TO_INT(user_data);
  885. /* In service queue we will have also included services */
  886. if (!p->primary)
  887. return;
  888. ev.conn_id = conn_id;
  889. element_id_to_hal_srvc_id(&p->id, 1, &ev.srvc_id);
  890. uuid2android(&p->id.uuid, ev.srvc_id.uuid);
  891. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  892. HAL_EV_GATT_CLIENT_SEARCH_RESULT, sizeof(ev), &ev);
  893. }
  894. static void send_client_search_complete_notify(int32_t status, int32_t conn_id)
  895. {
  896. struct hal_ev_gatt_client_search_complete ev;
  897. ev.status = status;
  898. ev.conn_id = conn_id;
  899. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  900. HAL_EV_GATT_CLIENT_SEARCH_COMPLETE, sizeof(ev), &ev);
  901. }
  902. struct discover_srvc_data {
  903. bt_uuid_t uuid;
  904. struct app_connection *conn;
  905. };
  906. static void discover_srvc_by_uuid_cb(uint8_t status, GSList *ranges,
  907. void *user_data)
  908. {
  909. struct discover_srvc_data *cb_data = user_data;
  910. struct gatt_primary prim;
  911. struct service *s;
  912. int32_t gatt_status;
  913. struct gatt_device *dev = cb_data->conn->device;
  914. uint8_t instance_id = queue_length(dev->services);
  915. DBG("Status %d", status);
  916. if (status) {
  917. error("gatt: Discover pri srvc filtered by uuid failed: %s",
  918. att_ecode2str(status));
  919. gatt_status = GATT_FAILURE;
  920. goto reply;
  921. }
  922. if (!ranges) {
  923. info("gatt: No primary services searched by uuid found");
  924. gatt_status = GATT_SUCCESS;
  925. goto reply;
  926. }
  927. bt_uuid_to_string(&cb_data->uuid, prim.uuid, sizeof(prim.uuid));
  928. for (; ranges; ranges = ranges->next) {
  929. memcpy(&prim.range, ranges->data, sizeof(prim.range));
  930. s = create_service(instance_id++, true, prim.uuid, &prim);
  931. if (!s) {
  932. gatt_status = GATT_FAILURE;
  933. goto reply;
  934. }
  935. queue_push_tail(dev->services, s);
  936. send_client_primary_notify(s, INT_TO_PTR(cb_data->conn->id));
  937. DBG("attr handle = 0x%04x, end grp handle = 0x%04x uuid: %s",
  938. prim.range.start, prim.range.end, prim.uuid);
  939. }
  940. /* Partial search service scanning was performed */
  941. dev->partial_srvc_search = true;
  942. gatt_status = GATT_SUCCESS;
  943. reply:
  944. send_client_search_complete_notify(gatt_status, cb_data->conn->id);
  945. free(cb_data);
  946. }
  947. static void discover_srvc_all_cb(uint8_t status, GSList *services,
  948. void *user_data)
  949. {
  950. struct discover_srvc_data *cb_data = user_data;
  951. struct gatt_device *dev = cb_data->conn->device;
  952. int32_t gatt_status;
  953. GSList *l;
  954. /*
  955. * There might be multiply services with same uuid. Therefore make sure
  956. * each primary service one has unique instance_id
  957. */
  958. uint8_t instance_id = queue_length(dev->services);
  959. DBG("Status %d", status);
  960. if (status) {
  961. error("gatt: Discover all primary services failed: %s",
  962. att_ecode2str(status));
  963. gatt_status = GATT_FAILURE;
  964. goto reply;
  965. }
  966. if (!services) {
  967. info("gatt: No primary services found");
  968. gatt_status = GATT_SUCCESS;
  969. goto reply;
  970. }
  971. for (l = services; l; l = l->next) {
  972. struct gatt_primary *prim = l->data;
  973. struct service *p;
  974. if (queue_find(dev->services, match_srvc_by_range,
  975. &prim->range))
  976. continue;
  977. p = create_service(instance_id++, true, prim->uuid, prim);
  978. if (!p)
  979. continue;
  980. queue_push_tail(dev->services, p);
  981. DBG("attr handle = 0x%04x, end grp handle = 0x%04x uuid: %s",
  982. prim->range.start, prim->range.end, prim->uuid);
  983. }
  984. /*
  985. * Send all found services notifications - first cache,
  986. * then send notifies
  987. */
  988. queue_foreach(dev->services, send_client_primary_notify,
  989. INT_TO_PTR(cb_data->conn->id));
  990. /* Full search service scanning was performed */
  991. dev->partial_srvc_search = false;
  992. gatt_status = GATT_SUCCESS;
  993. reply:
  994. send_client_search_complete_notify(gatt_status, cb_data->conn->id);
  995. free(cb_data);
  996. }
  997. static gboolean connection_timeout(void *user_data)
  998. {
  999. struct app_connection *conn = user_data;
  1000. conn->timeout_id = 0;
  1001. queue_remove(app_connections, conn);
  1002. destroy_connection(conn);
  1003. return FALSE;
  1004. }
  1005. static void discover_primary_cb(uint8_t status, GSList *services,
  1006. void *user_data)
  1007. {
  1008. struct discover_srvc_data *cb_data = user_data;
  1009. struct app_connection *conn = cb_data->conn;
  1010. struct gatt_device *dev = conn->device;
  1011. GSList *l, *uuids = NULL;
  1012. DBG("Status %d", status);
  1013. if (status) {
  1014. error("gatt: Discover all primary services failed: %s",
  1015. att_ecode2str(status));
  1016. free(cb_data);
  1017. return;
  1018. }
  1019. if (!services) {
  1020. info("gatt: No primary services found");
  1021. free(cb_data);
  1022. return;
  1023. }
  1024. for (l = services; l; l = l->next) {
  1025. struct gatt_primary *prim = l->data;
  1026. uint8_t *new_uuid;
  1027. bt_uuid_t uuid, u128;
  1028. DBG("uuid: %s", prim->uuid);
  1029. if (bt_string_to_uuid(&uuid, prim->uuid) < 0) {
  1030. error("gatt: Cannot convert string to uuid");
  1031. continue;
  1032. }
  1033. bt_uuid_to_uuid128(&uuid, &u128);
  1034. new_uuid = g_memdup(&u128.value.u128, sizeof(u128.value.u128));
  1035. uuids = g_slist_prepend(uuids, new_uuid);
  1036. }
  1037. bt_device_set_uuids(&dev->bdaddr, uuids);
  1038. free(cb_data);
  1039. conn->timeout_id = g_timeout_add_seconds(GATT_CONN_TIMEOUT,
  1040. connection_timeout, conn);
  1041. }
  1042. static guint search_dev_for_srvc(struct app_connection *conn, bt_uuid_t *uuid)
  1043. {
  1044. struct discover_srvc_data *cb_data;
  1045. cb_data = new0(struct discover_srvc_data, 1);
  1046. cb_data->conn = conn;
  1047. if (uuid) {
  1048. memcpy(&cb_data->uuid, uuid, sizeof(cb_data->uuid));
  1049. return gatt_discover_primary(conn->device->attrib, uuid,
  1050. discover_srvc_by_uuid_cb, cb_data);
  1051. }
  1052. if (conn->app)
  1053. return gatt_discover_primary(conn->device->attrib, NULL,
  1054. discover_srvc_all_cb, cb_data);
  1055. return gatt_discover_primary(conn->device->attrib, NULL,
  1056. discover_primary_cb, cb_data);
  1057. }
  1058. struct connect_data {
  1059. struct gatt_device *dev;
  1060. int32_t status;
  1061. };
  1062. static void notify_app_connect_status_by_device(void *data, void *user_data)
  1063. {
  1064. struct app_connection *conn = data;
  1065. struct connect_data *con_data = user_data;
  1066. if (conn->device == con_data->dev)
  1067. notify_app_connect_status(conn, con_data->status);
  1068. }
  1069. static struct app_connection *find_conn_without_app(struct gatt_device *dev)
  1070. {
  1071. struct app_connection conn_match;
  1072. conn_match.device = dev;
  1073. conn_match.app = NULL;
  1074. return queue_find(app_connections, match_connection_by_device_and_app,
  1075. &conn_match);
  1076. }
  1077. static struct app_connection *find_conn(const bdaddr_t *addr, int32_t app_id)
  1078. {
  1079. struct app_connection conn_match;
  1080. struct gatt_device *dev;
  1081. struct gatt_app *app;
  1082. /* Check if app is registered */
  1083. app = find_app_by_id(app_id);
  1084. if (!app) {
  1085. error("gatt: Client id %d not found", app_id);
  1086. return NULL;
  1087. }
  1088. /* Check if device is known */
  1089. dev = find_device_by_addr(addr);
  1090. if (!dev) {
  1091. error("gatt: Client id %d not found", app_id);
  1092. return NULL;
  1093. }
  1094. conn_match.device = dev;
  1095. conn_match.app = app;
  1096. return queue_find(app_connections, match_connection_by_device_and_app,
  1097. &conn_match);
  1098. }
  1099. static void create_app_connection(void *data, void *user_data)
  1100. {
  1101. struct gatt_device *dev = user_data;
  1102. struct gatt_app *app;
  1103. app = find_app_by_id(PTR_TO_INT(data));
  1104. if (!app)
  1105. return;
  1106. DBG("Autoconnect application id=%d", app->id);
  1107. if (!find_conn(&dev->bdaddr, PTR_TO_INT(data)))
  1108. create_connection(dev, app);
  1109. }
  1110. static void ind_handler(const uint8_t *cmd, uint16_t cmd_len,
  1111. gpointer user_data)
  1112. {
  1113. struct gatt_device *dev = user_data;
  1114. uint16_t resp_length = 0;
  1115. size_t length;
  1116. uint8_t *opdu = g_attrib_get_buffer(dev->attrib, &length);
  1117. /*
  1118. * We have to send confirmation here. If some client is
  1119. * registered for this indication, event will be send in
  1120. * handle_notification
  1121. */
  1122. resp_length = enc_confirmation(opdu, length);
  1123. g_attrib_send(dev->attrib, 0, opdu, resp_length, NULL, NULL, NULL);
  1124. }
  1125. static void connect_cb(GIOChannel *io, GError *gerr, gpointer user_data)
  1126. {
  1127. struct gatt_device *dev = user_data;
  1128. struct connect_data data;
  1129. struct att_range range;
  1130. uint32_t status;
  1131. GError *err = NULL;
  1132. GAttrib *attrib;
  1133. uint16_t mtu, cid;
  1134. if (dev->state != DEVICE_CONNECT_READY) {
  1135. error("gatt: Device not in a connecting state!?");
  1136. g_io_channel_shutdown(io, TRUE, NULL);
  1137. return;
  1138. }
  1139. if (dev->att_io) {
  1140. g_io_channel_unref(dev->att_io);
  1141. dev->att_io = NULL;
  1142. }
  1143. if (gerr) {
  1144. error("gatt: connection failed %s", gerr->message);
  1145. device_set_state(dev, DEVICE_DISCONNECTED);
  1146. status = GATT_FAILURE;
  1147. goto reply;
  1148. }
  1149. if (!bt_io_get(io, &err, BT_IO_OPT_IMTU, &mtu, BT_IO_OPT_CID, &cid,
  1150. BT_IO_OPT_INVALID)) {
  1151. error("gatt: Could not get imtu or cid: %s", err->message);
  1152. device_set_state(dev, DEVICE_DISCONNECTED);
  1153. status = GATT_FAILURE;
  1154. g_error_free(err);
  1155. goto reply;
  1156. }
  1157. /* on BR/EDR MTU must not be less then minimal allowed MTU */
  1158. if (cid != ATT_CID && mtu < ATT_DEFAULT_L2CAP_MTU) {
  1159. error("gatt: MTU too small (%u bytes)", mtu);
  1160. device_set_state(dev, DEVICE_DISCONNECTED);
  1161. status = GATT_FAILURE;
  1162. goto reply;
  1163. }
  1164. DBG("mtu %u cid %u", mtu, cid);
  1165. /* on LE we always start with default MTU */
  1166. if (cid == ATT_CID)
  1167. mtu = ATT_DEFAULT_LE_MTU;
  1168. attrib = g_attrib_new(io, mtu, true);
  1169. if (!attrib) {
  1170. error("gatt: unable to create new GAttrib instance");
  1171. device_set_state(dev, DEVICE_DISCONNECTED);
  1172. status = GATT_FAILURE;
  1173. goto reply;
  1174. }
  1175. dev->attrib = attrib;
  1176. dev->watch_id = g_io_add_watch(io, G_IO_HUP | G_IO_ERR | G_IO_NVAL,
  1177. disconnected_cb, dev);
  1178. dev->server_id = g_attrib_register(attrib, GATTRIB_ALL_REQS,
  1179. GATTRIB_ALL_HANDLES,
  1180. att_handler, dev, NULL);
  1181. dev->ind_id = g_attrib_register(attrib, ATT_OP_HANDLE_IND,
  1182. GATTRIB_ALL_HANDLES,
  1183. ind_handler, dev, NULL);
  1184. if ((dev->server_id && dev->ind_id) == 0)
  1185. error("gatt: Could not attach to server");
  1186. device_set_state(dev, DEVICE_CONNECTED);
  1187. /* Send exchange mtu request as we assume being client and server */
  1188. /* TODO: Dont exchange mtu if no client apps */
  1189. /* MTU exchange shall not be used on BR/EDR - Vol 3. Part G. 4.3.1 */
  1190. if (cid == ATT_CID)
  1191. send_exchange_mtu_request(dev);
  1192. /*
  1193. * Service Changed Characteristic and CCC Descriptor handles
  1194. * should not change if there are bonded devices. We have them
  1195. * constant all the time, thus they should be excluded from
  1196. * range indicating changes.
  1197. */
  1198. range.start = gatt_db_attribute_get_handle(service_changed_attrib) + 2;
  1199. range.end = 0xffff;
  1200. /*
  1201. * If there is ccc stored for that device we were acting as server for
  1202. * it, and as we dont have last connect and last services (de)activation
  1203. * timestamps we should always assume something has changed.
  1204. */
  1205. notify_att_range_change(dev, &range);
  1206. status = GATT_SUCCESS;
  1207. reply:
  1208. /*
  1209. * Make sure there are app_connections for all apps interested in auto
  1210. * connect to that device
  1211. */
  1212. queue_foreach(dev->autoconnect_apps, create_app_connection, dev);
  1213. if (!queue_find(app_connections, match_connection_by_device, dev)) {
  1214. struct app_connection *conn;
  1215. if (!dev->attrib)
  1216. return;
  1217. conn = create_connection(dev, NULL);
  1218. if (!conn)
  1219. return;
  1220. if (bt_is_pairing(&dev->bdaddr))
  1221. /*
  1222. * If there is bonding ongoing lets wait for paired
  1223. * callback. Once we get that we can start search
  1224. * services
  1225. */
  1226. conn->timeout_id = g_timeout_add_seconds(
  1227. GATT_PAIR_CONN_TIMEOUT,
  1228. connection_timeout, conn);
  1229. else
  1230. /*
  1231. * There is no ongoing bonding, lets search for primary
  1232. * services
  1233. */
  1234. search_dev_for_srvc(conn, NULL);
  1235. }
  1236. data.dev = dev;
  1237. data.status = status;
  1238. queue_foreach(app_connections, notify_app_connect_status_by_device,
  1239. &data);
  1240. /* For BR/EDR notify about MTU since it is not negotiable*/
  1241. if (cid != ATT_CID && status == GATT_SUCCESS)
  1242. queue_foreach(app_connections, notify_mtu_change, dev);
  1243. device_unref(dev);
  1244. /* Check if we should restart scan */
  1245. if (scanning)
  1246. bt_le_discovery_start();
  1247. /* FIXME: What to do if discovery won't start here. */
  1248. }
  1249. static int connect_le(struct gatt_device *dev)
  1250. {
  1251. GIOChannel *io;
  1252. GError *gerr = NULL;
  1253. char addr[18];
  1254. const bdaddr_t *bdaddr;
  1255. uint8_t bdaddr_type;
  1256. ba2str(&dev->bdaddr, addr);
  1257. /* There is one connection attempt going on */
  1258. if (dev->att_io) {
  1259. info("gatt: connection to dev %s is ongoing", addr);
  1260. return -EALREADY;
  1261. }
  1262. DBG("Connection attempt to: %s", addr);
  1263. bdaddr = bt_get_id_addr(&dev->bdaddr, &bdaddr_type);
  1264. /*
  1265. * This connection will help us catch any PDUs that comes before
  1266. * pairing finishes
  1267. */
  1268. io = bt_io_connect(connect_cb, device_ref(dev), NULL, &gerr,
  1269. BT_IO_OPT_SOURCE_BDADDR, &adapter_addr,
  1270. BT_IO_OPT_SOURCE_TYPE, BDADDR_LE_PUBLIC,
  1271. BT_IO_OPT_DEST_BDADDR, bdaddr,
  1272. BT_IO_OPT_DEST_TYPE, bdaddr_type,
  1273. BT_IO_OPT_CID, ATT_CID,
  1274. BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_LOW,
  1275. BT_IO_OPT_INVALID);
  1276. if (!io) {
  1277. error("gatt: Failed bt_io_connect(%s): %s", addr,
  1278. gerr->message);
  1279. g_error_free(gerr);
  1280. return -EIO;
  1281. }
  1282. /* Keep this, so we can cancel the connection */
  1283. dev->att_io = io;
  1284. device_set_state(dev, DEVICE_CONNECT_READY);
  1285. return 0;
  1286. }
  1287. static int connect_next_dev(void)
  1288. {
  1289. struct gatt_device *dev;
  1290. DBG("");
  1291. dev = find_device_by_state(DEVICE_CONNECT_READY);
  1292. if (!dev)
  1293. return -ENODEV;
  1294. return connect_le(dev);
  1295. }
  1296. static void bt_le_discovery_stop_cb(void)
  1297. {
  1298. DBG("");
  1299. /* Check now if there is any device ready to connect */
  1300. if (connect_next_dev() < 0)
  1301. bt_le_discovery_start();
  1302. }
  1303. static void le_device_found_handler(const bdaddr_t *addr, int rssi,
  1304. uint16_t eir_len, const void *eir,
  1305. bool connectable, bool bonded)
  1306. {
  1307. uint8_t buf[IPC_MTU];
  1308. struct hal_ev_gatt_client_scan_result *ev = (void *) buf;
  1309. struct gatt_device *dev;
  1310. char bda[18];
  1311. if (!scanning)
  1312. goto done;
  1313. ba2str(addr, bda);
  1314. DBG("LE Device found: %s, rssi: %d, adv_data: %d", bda, rssi, !!eir);
  1315. bdaddr2android(addr, ev->bda);
  1316. ev->rssi = rssi;
  1317. ev->len = eir_len;
  1318. memcpy(ev->adv_data, eir, ev->len);
  1319. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  1320. HAL_EV_GATT_CLIENT_SCAN_RESULT,
  1321. sizeof(*ev) + ev->len, ev);
  1322. done:
  1323. if (!connectable)
  1324. return;
  1325. /* We use auto connect feature from kernel if possible */
  1326. if (bt_kernel_conn_control())
  1327. return;
  1328. dev = find_device_by_addr(addr);
  1329. if (!dev) {
  1330. if (!bonded)
  1331. return;
  1332. dev = create_device(addr);
  1333. }
  1334. if (dev->state != DEVICE_CONNECT_INIT)
  1335. return;
  1336. device_set_state(dev, DEVICE_CONNECT_READY);
  1337. /*
  1338. * We are ok to perform connect now. Stop discovery
  1339. * and once it is stopped continue with creating ACL
  1340. */
  1341. bt_le_discovery_stop(bt_le_discovery_stop_cb);
  1342. }
  1343. static struct gatt_app *register_app(const uint8_t *uuid, gatt_type_t type)
  1344. {
  1345. static int32_t application_id = 1;
  1346. struct gatt_app *app;
  1347. if (queue_find(gatt_apps, match_app_by_uuid, uuid)) {
  1348. error("gatt: app uuid is already on list");
  1349. return NULL;
  1350. }
  1351. app = new0(struct gatt_app, 1);
  1352. app->type = type;
  1353. if (app->type == GATT_CLIENT)
  1354. app->notifications = queue_new();
  1355. memcpy(app->uuid, uuid, sizeof(app->uuid));
  1356. app->id = application_id++;
  1357. queue_push_head(gatt_apps, app);
  1358. if (app->type == GATT_SERVER)
  1359. queue_push_tail(listen_apps, INT_TO_PTR(app->id));
  1360. return app;
  1361. }
  1362. static void handle_client_register(const void *buf, uint16_t len)
  1363. {
  1364. const struct hal_cmd_gatt_client_register *cmd = buf;
  1365. struct hal_ev_gatt_client_register_client ev;
  1366. struct gatt_app *app;
  1367. DBG("");
  1368. memset(&ev, 0, sizeof(ev));
  1369. app = register_app(cmd->uuid, GATT_CLIENT);
  1370. if (app) {
  1371. ev.client_if = app->id;
  1372. ev.status = GATT_SUCCESS;
  1373. } else {
  1374. ev.status = GATT_FAILURE;
  1375. }
  1376. /* We should send notification with given in cmd UUID */
  1377. memcpy(ev.app_uuid, cmd->uuid, sizeof(ev.app_uuid));
  1378. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  1379. HAL_EV_GATT_CLIENT_REGISTER_CLIENT, sizeof(ev), &ev);
  1380. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT, HAL_OP_GATT_CLIENT_REGISTER,
  1381. HAL_STATUS_SUCCESS);
  1382. }
  1383. static void handle_client_scan(const void *buf, uint16_t len)
  1384. {
  1385. const struct hal_cmd_gatt_client_scan *cmd = buf;
  1386. uint8_t status;
  1387. DBG("new state %d", cmd->start);
  1388. if (cmd->client_if != 0) {
  1389. void *registered = find_app_by_id(cmd->client_if);
  1390. if (!registered) {
  1391. error("gatt: Client not registered");
  1392. status = HAL_STATUS_FAILED;
  1393. goto reply;
  1394. }
  1395. }
  1396. /* Turn off scan */
  1397. if (!cmd->start) {
  1398. DBG("Stopping LE SCAN");
  1399. if (scanning) {
  1400. bt_le_discovery_stop(NULL);
  1401. scanning = false;
  1402. }
  1403. status = HAL_STATUS_SUCCESS;
  1404. goto reply;
  1405. }
  1406. /* Reply success if we already do scan */
  1407. if (scanning) {
  1408. status = HAL_STATUS_SUCCESS;
  1409. goto reply;
  1410. }
  1411. /* Turn on scan */
  1412. if (!bt_le_discovery_start()) {
  1413. error("gatt: LE scan switch failed");
  1414. status = HAL_STATUS_FAILED;
  1415. goto reply;
  1416. }
  1417. scanning = true;
  1418. status = HAL_STATUS_SUCCESS;
  1419. reply:
  1420. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT, HAL_OP_GATT_CLIENT_SCAN,
  1421. status);
  1422. }
  1423. static int connect_bredr(struct gatt_device *dev)
  1424. {
  1425. BtIOSecLevel sec_level;
  1426. GIOChannel *io;
  1427. GError *gerr = NULL;
  1428. char addr[18];
  1429. ba2str(&dev->bdaddr, addr);
  1430. /* There is one connection attempt going on */
  1431. if (dev->att_io) {
  1432. info("gatt: connection to dev %s is ongoing", addr);
  1433. return -EALREADY;
  1434. }
  1435. DBG("Connection attempt to: %s", addr);
  1436. sec_level = bt_device_is_bonded(&dev->bdaddr) ? BT_IO_SEC_MEDIUM :
  1437. BT_IO_SEC_LOW;
  1438. io = bt_io_connect(connect_cb, device_ref(dev), NULL, &gerr,
  1439. BT_IO_OPT_SOURCE_BDADDR, &adapter_addr,
  1440. BT_IO_OPT_SOURCE_TYPE, BDADDR_BREDR,
  1441. BT_IO_OPT_DEST_BDADDR, &dev->bdaddr,
  1442. BT_IO_OPT_DEST_TYPE, BDADDR_BREDR,
  1443. BT_IO_OPT_PSM, ATT_PSM,
  1444. BT_IO_OPT_SEC_LEVEL, sec_level,
  1445. BT_IO_OPT_INVALID);
  1446. if (!io) {
  1447. error("gatt: Failed bt_io_connect(%s): %s", addr,
  1448. gerr->message);
  1449. g_error_free(gerr);
  1450. return -EIO;
  1451. }
  1452. device_set_state(dev, DEVICE_CONNECT_READY);
  1453. /* Keep this, so we can cancel the connection */
  1454. dev->att_io = io;
  1455. return 0;
  1456. }
  1457. static bool trigger_connection(struct app_connection *conn, bool direct)
  1458. {
  1459. switch (conn->device->state) {
  1460. case DEVICE_DISCONNECTED:
  1461. /*
  1462. * If device was last seen over BR/EDR connect over it.
  1463. * Note: Connection state is handled in connect_bredr() func
  1464. */
  1465. if (bt_device_last_seen_bearer(&conn->device->bdaddr) ==
  1466. BDADDR_BREDR)
  1467. return connect_bredr(conn->device) == 0;
  1468. if (direct)
  1469. return connect_le(conn->device) == 0;
  1470. bt_gatt_add_autoconnect(conn->app->id, &conn->device->bdaddr);
  1471. return auto_connect_le(conn->device);
  1472. case DEVICE_CONNECTED:
  1473. notify_app_connect_status(conn, GATT_SUCCESS);
  1474. return true;
  1475. case DEVICE_CONNECT_READY:
  1476. case DEVICE_CONNECT_INIT:
  1477. default:
  1478. /* In those cases connection is already triggered. */
  1479. return true;
  1480. }
  1481. }
  1482. static void remove_autoconnect_device(struct gatt_device *dev)
  1483. {
  1484. bt_auto_connect_remove(&dev->bdaddr);
  1485. if (dev->state == DEVICE_CONNECT_INIT)
  1486. device_set_state(dev, DEVICE_DISCONNECTED);
  1487. device_unref(dev);
  1488. }
  1489. static void clear_autoconnect_devices(void *data, void *user_data)
  1490. {
  1491. struct gatt_device *dev = data;
  1492. if (queue_remove(dev->autoconnect_apps, user_data))
  1493. if (queue_isempty(dev->autoconnect_apps))
  1494. remove_autoconnect_device(dev);
  1495. }
  1496. static uint8_t unregister_app(int client_if)
  1497. {
  1498. struct gatt_app *cl;
  1499. /*
  1500. * Make sure that there is no devices in auto connect list for this
  1501. * application
  1502. */
  1503. queue_foreach(gatt_devices, clear_autoconnect_devices,
  1504. INT_TO_PTR(client_if));
  1505. cl = queue_remove_if(gatt_apps, match_app_by_id, INT_TO_PTR(client_if));
  1506. if (!cl) {
  1507. error("gatt: client_if=%d not found", client_if);
  1508. return HAL_STATUS_FAILED;
  1509. }
  1510. /* Destroy app connections with proper notifications for this app. */
  1511. queue_remove_all(app_connections, match_connection_by_app, cl,
  1512. destroy_connection);
  1513. destroy_gatt_app(cl);
  1514. return HAL_STATUS_SUCCESS;
  1515. }
  1516. static void send_client_listen_notify(int32_t id, int32_t status)
  1517. {
  1518. struct hal_ev_gatt_client_listen ev;
  1519. /* Server if because of typo in android headers */
  1520. ev.server_if = id;
  1521. ev.status = status;
  1522. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT, HAL_EV_GATT_CLIENT_LISTEN,
  1523. sizeof(ev), &ev);
  1524. }
  1525. struct listen_data {
  1526. int32_t client_id;
  1527. bool start;
  1528. };
  1529. static struct listen_data *create_listen_data(int32_t client_id, bool start)
  1530. {
  1531. struct listen_data *d;
  1532. d = new0(struct listen_data, 1);
  1533. d->client_id = client_id;
  1534. d->start = start;
  1535. return d;
  1536. }
  1537. static void set_advertising_cb(uint8_t status, void *user_data)
  1538. {
  1539. struct listen_data *l = user_data;
  1540. send_client_listen_notify(l->client_id, status);
  1541. /* In case of success update advertising state*/
  1542. if (!status)
  1543. advertising_cnt = l->start ? 1 : 0;
  1544. /*
  1545. * Let's remove client from the list in two cases
  1546. * 1. Start failed
  1547. * 2. Stop succeed
  1548. */
  1549. if ((l->start && status) || (!l->start && !status))
  1550. queue_remove(listen_apps, INT_TO_PTR(l->client_id));
  1551. free(l);
  1552. }
  1553. static void handle_client_unregister(const void *buf, uint16_t len)
  1554. {
  1555. const struct hal_cmd_gatt_client_unregister *cmd = buf;
  1556. uint8_t status;
  1557. void *listening_client;
  1558. struct listen_data *data;
  1559. DBG("");
  1560. listening_client = queue_find(listen_apps, NULL,
  1561. INT_TO_PTR(cmd->client_if));
  1562. if (listening_client) {
  1563. advertising_cnt--;
  1564. queue_remove(listen_apps, INT_TO_PTR(cmd->client_if));
  1565. } else {
  1566. status = unregister_app(cmd->client_if);
  1567. goto reply;
  1568. }
  1569. if (!advertising_cnt) {
  1570. data = create_listen_data(cmd->client_if, false);
  1571. if (!bt_le_set_advertising(data->start, set_advertising_cb,
  1572. data)) {
  1573. error("gatt: Could not set advertising");
  1574. status = HAL_STATUS_FAILED;
  1575. free(data);
  1576. goto reply;
  1577. }
  1578. }
  1579. status = unregister_app(cmd->client_if);
  1580. reply:
  1581. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  1582. HAL_OP_GATT_CLIENT_UNREGISTER, status);
  1583. }
  1584. static uint8_t handle_connect(int32_t app_id, const bdaddr_t *addr, bool direct)
  1585. {
  1586. struct app_connection conn_match;
  1587. struct app_connection *conn;
  1588. struct gatt_device *device;
  1589. struct gatt_app *app;
  1590. DBG("");
  1591. app = find_app_by_id(app_id);
  1592. if (!app)
  1593. return HAL_STATUS_FAILED;
  1594. device = find_device_by_addr(addr);
  1595. if (!device)
  1596. device = create_device(addr);
  1597. conn_match.device = device;
  1598. conn_match.app = app;
  1599. conn = queue_find(app_connections, match_connection_by_device_and_app,
  1600. &conn_match);
  1601. if (!conn) {
  1602. conn = create_connection(device, app);
  1603. if (!conn)
  1604. return HAL_STATUS_NOMEM;
  1605. }
  1606. if (!trigger_connection(conn, direct))
  1607. return HAL_STATUS_FAILED;
  1608. return HAL_STATUS_SUCCESS;
  1609. }
  1610. static void handle_client_connect(const void *buf, uint16_t len)
  1611. {
  1612. const struct hal_cmd_gatt_client_connect *cmd = buf;
  1613. uint8_t status;
  1614. bdaddr_t addr;
  1615. DBG("is_direct:%u transport:%u", cmd->is_direct, cmd->transport);
  1616. android2bdaddr(&cmd->bdaddr, &addr);
  1617. /* TODO handle transport flag */
  1618. status = handle_connect(cmd->client_if, &addr, cmd->is_direct);
  1619. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT, HAL_OP_GATT_CLIENT_CONNECT,
  1620. status);
  1621. }
  1622. static void handle_client_disconnect(const void *buf, uint16_t len)
  1623. {
  1624. const struct hal_cmd_gatt_client_disconnect *cmd = buf;
  1625. struct app_connection *conn;
  1626. uint8_t status;
  1627. DBG("");
  1628. /* TODO: should we care to match also bdaddr when conn_id is unique? */
  1629. conn = queue_remove_if(app_connections, match_connection_by_id,
  1630. INT_TO_PTR(cmd->conn_id));
  1631. destroy_connection(conn);
  1632. status = HAL_STATUS_SUCCESS;
  1633. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  1634. HAL_OP_GATT_CLIENT_DISCONNECT, status);
  1635. }
  1636. static void handle_client_listen(const void *buf, uint16_t len)
  1637. {
  1638. const struct hal_cmd_gatt_client_listen *cmd = buf;
  1639. uint8_t status;
  1640. struct listen_data *data;
  1641. bool req_sent = false;
  1642. void *listening_client;
  1643. DBG("");
  1644. if (!find_app_by_id(cmd->client_if)) {
  1645. error("gatt: Client not registered");
  1646. status = HAL_STATUS_FAILED;
  1647. goto reply;
  1648. }
  1649. listening_client = queue_find(listen_apps, NULL,
  1650. INT_TO_PTR(cmd->client_if));
  1651. /* Start listening */
  1652. if (cmd->start) {
  1653. if (listening_client) {
  1654. status = HAL_STATUS_SUCCESS;
  1655. goto reply;
  1656. }
  1657. queue_push_tail(listen_apps, INT_TO_PTR(cmd->client_if));
  1658. /* If listen is already on just return success*/
  1659. if (advertising_cnt > 0) {
  1660. advertising_cnt++;
  1661. status = HAL_STATUS_SUCCESS;
  1662. goto reply;
  1663. }
  1664. } else {
  1665. /* Stop listening. Check if client was listening */
  1666. if (!listening_client) {
  1667. error("gatt: This client %d does not listen",
  1668. cmd->client_if);
  1669. status = HAL_STATUS_FAILED;
  1670. goto reply;
  1671. }
  1672. /*
  1673. * In case there is more listening clients don't stop
  1674. * advertising
  1675. */
  1676. if (advertising_cnt > 1) {
  1677. advertising_cnt--;
  1678. queue_remove(listen_apps, INT_TO_PTR(cmd->client_if));
  1679. status = HAL_STATUS_SUCCESS;
  1680. goto reply;
  1681. }
  1682. }
  1683. data = create_listen_data(cmd->client_if, cmd->start);
  1684. if (!bt_le_set_advertising(cmd->start, set_advertising_cb, data)) {
  1685. error("gatt: Could not set advertising");
  1686. status = HAL_STATUS_FAILED;
  1687. free(data);
  1688. goto reply;
  1689. }
  1690. /*
  1691. * Use this flag to keep in mind that we are waiting for callback with
  1692. * result
  1693. */
  1694. req_sent = true;
  1695. status = HAL_STATUS_SUCCESS;
  1696. reply:
  1697. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT, HAL_OP_GATT_CLIENT_LISTEN,
  1698. status);
  1699. /* In case of early success or error, just send notification up */
  1700. if (!req_sent) {
  1701. int32_t gatt_status = status == HAL_STATUS_SUCCESS ?
  1702. GATT_SUCCESS : GATT_FAILURE;
  1703. send_client_listen_notify(cmd->client_if, gatt_status);
  1704. }
  1705. }
  1706. static void handle_client_refresh(const void *buf, uint16_t len)
  1707. {
  1708. const struct hal_cmd_gatt_client_refresh *cmd = buf;
  1709. struct gatt_device *dev;
  1710. uint8_t status;
  1711. bdaddr_t bda;
  1712. /*
  1713. * This is Android's framework hidden API call. It seams that no
  1714. * notification is expected and Bluedroid silently updates device's
  1715. * cache under the hood. As we use lazy caching ,we can just clear the
  1716. * cache and we're done.
  1717. */
  1718. DBG("");
  1719. android2bdaddr(&cmd->bdaddr, &bda);
  1720. dev = find_device_by_addr(&bda);
  1721. if (!dev) {
  1722. status = HAL_STATUS_FAILED;
  1723. goto done;
  1724. }
  1725. queue_remove_all(dev->services, NULL, NULL, destroy_service);
  1726. status = HAL_STATUS_SUCCESS;
  1727. done:
  1728. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT, HAL_OP_GATT_CLIENT_REFRESH,
  1729. status);
  1730. }
  1731. static void handle_client_search_service(const void *buf, uint16_t len)
  1732. {
  1733. const struct hal_cmd_gatt_client_search_service *cmd = buf;
  1734. struct app_connection *conn;
  1735. uint8_t status;
  1736. struct service *s;
  1737. bt_uuid_t uuid;
  1738. guint srvc_search_success;
  1739. DBG("");
  1740. if (len != sizeof(*cmd) + (cmd->filtered ? 16 : 0)) {
  1741. error("Invalid search service size (%u bytes), terminating",
  1742. len);
  1743. raise(SIGTERM);
  1744. return;
  1745. }
  1746. conn = find_connection_by_id(cmd->conn_id);
  1747. if (!conn) {
  1748. error("gatt: dev with conn_id=%d not found", cmd->conn_id);
  1749. status = HAL_STATUS_FAILED;
  1750. goto reply;
  1751. }
  1752. if (conn->device->state != DEVICE_CONNECTED) {
  1753. char bda[18];
  1754. ba2str(&conn->device->bdaddr, bda);
  1755. error("gatt: device %s not connected", bda);
  1756. status = HAL_STATUS_FAILED;
  1757. goto reply;
  1758. }
  1759. if (cmd->filtered)
  1760. android2uuid(cmd->filter_uuid, &uuid);
  1761. /* Services not cached yet */
  1762. if (queue_isempty(conn->device->services)) {
  1763. if (cmd->filtered)
  1764. srvc_search_success = search_dev_for_srvc(conn, &uuid);
  1765. else
  1766. srvc_search_success = search_dev_for_srvc(conn, NULL);
  1767. if (!srvc_search_success) {
  1768. status = HAL_STATUS_FAILED;
  1769. goto reply;
  1770. }
  1771. status = HAL_STATUS_SUCCESS;
  1772. goto reply;
  1773. }
  1774. /* Search in cached services for given service */
  1775. if (cmd->filtered) {
  1776. /* Search in cache for service by uuid */
  1777. s = queue_find(conn->device->services, match_srvc_by_bt_uuid,
  1778. &uuid);
  1779. if (s) {
  1780. send_client_primary_notify(s, INT_TO_PTR(conn->id));
  1781. } else {
  1782. if (!search_dev_for_srvc(conn, &uuid)) {
  1783. status = HAL_STATUS_FAILED;
  1784. goto reply;
  1785. }
  1786. status = HAL_STATUS_SUCCESS;
  1787. goto reply;
  1788. }
  1789. } else {
  1790. /* Refresh service cache if only partial search was performed */
  1791. if (conn->device->partial_srvc_search) {
  1792. srvc_search_success = search_dev_for_srvc(conn, NULL);
  1793. if (!srvc_search_success) {
  1794. status = HAL_STATUS_FAILED;
  1795. goto reply;
  1796. }
  1797. } else
  1798. queue_foreach(conn->device->services,
  1799. send_client_primary_notify,
  1800. INT_TO_PTR(cmd->conn_id));
  1801. }
  1802. send_client_search_complete_notify(GATT_SUCCESS, conn->id);
  1803. status = HAL_STATUS_SUCCESS;
  1804. reply:
  1805. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  1806. HAL_OP_GATT_CLIENT_SEARCH_SERVICE, status);
  1807. }
  1808. static void send_client_incl_service_notify(const struct element_id *srvc_id,
  1809. const struct service *incl,
  1810. int32_t conn_id)
  1811. {
  1812. struct hal_ev_gatt_client_get_inc_service ev;
  1813. memset(&ev, 0, sizeof(ev));
  1814. ev.conn_id = conn_id;
  1815. element_id_to_hal_srvc_id(srvc_id, 1, &ev.srvc_id);
  1816. if (incl) {
  1817. element_id_to_hal_srvc_id(&incl->id, 0, &ev.incl_srvc_id);
  1818. ev.status = GATT_SUCCESS;
  1819. } else {
  1820. ev.status = GATT_FAILURE;
  1821. }
  1822. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT ,
  1823. HAL_EV_GATT_CLIENT_GET_INC_SERVICE,
  1824. sizeof(ev), &ev);
  1825. }
  1826. struct get_included_data {
  1827. struct service *prim;
  1828. struct app_connection *conn;
  1829. };
  1830. static int get_inst_id_of_prim_services(const struct gatt_device *dev)
  1831. {
  1832. struct service *s = queue_peek_tail(dev->services);
  1833. if (s)
  1834. return s->id.instance;
  1835. return -1;
  1836. }
  1837. static void get_included_cb(uint8_t status, GSList *included, void *user_data)
  1838. {
  1839. struct get_included_data *data = user_data;
  1840. struct app_connection *conn = data->conn;
  1841. struct service *service = data->prim;
  1842. struct service *incl = NULL;
  1843. int instance_id;
  1844. DBG("");
  1845. free(data);
  1846. if (status) {
  1847. error("gatt: no included services found");
  1848. goto failed;
  1849. }
  1850. /* Remember that we already search included services.*/
  1851. service->incl_search_done = true;
  1852. /*
  1853. * There might be multiply services with same uuid. Therefore make sure
  1854. * each service has unique instance id. Let's take the latest instance
  1855. * id of primary service and start iterate included services from this
  1856. * point.
  1857. */
  1858. instance_id = get_inst_id_of_prim_services(conn->device);
  1859. if (instance_id < 0)
  1860. goto failed;
  1861. for (; included; included = included->next) {
  1862. struct gatt_included *included_service = included->data;
  1863. incl = create_service(++instance_id, false,
  1864. included_service->uuid,
  1865. included_service);
  1866. if (!incl)
  1867. continue;
  1868. /*
  1869. * Lets keep included service on two queues.
  1870. * 1. on services queue together with primary service
  1871. * 2. on special queue inside primary service
  1872. */
  1873. queue_push_tail(service->included, incl);
  1874. queue_push_tail(conn->device->services, incl);
  1875. }
  1876. /*
  1877. * Notify upper layer about first included service.
  1878. * Android framework will iterate for next one.
  1879. */
  1880. incl = queue_peek_head(service->included);
  1881. failed:
  1882. send_client_incl_service_notify(&service->id, incl, conn->id);
  1883. }
  1884. static void search_included_services(struct app_connection *conn,
  1885. struct service *service)
  1886. {
  1887. struct get_included_data *data;
  1888. uint16_t start, end;
  1889. data = new0(struct get_included_data, 1);
  1890. data->prim = service;
  1891. data->conn = conn;
  1892. if (service->primary) {
  1893. start = service->prim.range.start;
  1894. end = service->prim.range.end;
  1895. } else {
  1896. start = service->incl.range.start;
  1897. end = service->incl.range.end;
  1898. }
  1899. gatt_find_included(conn->device->attrib, start, end, get_included_cb,
  1900. data);
  1901. }
  1902. static bool find_service(int32_t conn_id, struct element_id *service_id,
  1903. struct app_connection **connection,
  1904. struct service **service)
  1905. {
  1906. struct service *srvc;
  1907. struct app_connection *conn;
  1908. conn = find_connection_by_id(conn_id);
  1909. if (!conn) {
  1910. error("gatt: conn_id=%d not found", conn_id);
  1911. return false;
  1912. }
  1913. srvc = queue_find(conn->device->services, match_srvc_by_element_id,
  1914. service_id);
  1915. if (!srvc) {
  1916. error("gatt: Service with inst_id: %d not found",
  1917. service_id->instance);
  1918. return false;
  1919. }
  1920. *connection = conn;
  1921. *service = srvc;
  1922. return true;
  1923. }
  1924. static void handle_client_get_included_service(const void *buf, uint16_t len)
  1925. {
  1926. const struct hal_cmd_gatt_client_get_included_service *cmd = buf;
  1927. struct app_connection *conn;
  1928. struct service *prim_service;
  1929. struct service *incl_service = NULL;
  1930. struct element_id match_id;
  1931. struct element_id srvc_id;
  1932. uint8_t status;
  1933. DBG("");
  1934. hal_srvc_id_to_element_id(&cmd->srvc_id, &srvc_id);
  1935. if (len != sizeof(*cmd) +
  1936. (cmd->continuation ? sizeof(cmd->incl_srvc_id[0]) : 0)) {
  1937. error("Invalid get incl services size (%u bytes), terminating",
  1938. len);
  1939. raise(SIGTERM);
  1940. return;
  1941. }
  1942. hal_srvc_id_to_element_id(&cmd->srvc_id, &match_id);
  1943. if (!find_service(cmd->conn_id, &match_id, &conn, &prim_service)) {
  1944. status = HAL_STATUS_FAILED;
  1945. goto notify;
  1946. }
  1947. if (!prim_service->incl_search_done) {
  1948. search_included_services(conn, prim_service);
  1949. status = HAL_STATUS_SUCCESS;
  1950. goto reply;
  1951. }
  1952. /* Try to use cache here */
  1953. if (!cmd->continuation) {
  1954. incl_service = queue_peek_head(prim_service->included);
  1955. } else {
  1956. uint8_t inst_id = cmd->incl_srvc_id[0].inst_id;
  1957. incl_service = queue_find(prim_service->included,
  1958. match_srvc_by_higher_inst_id,
  1959. INT_TO_PTR(inst_id));
  1960. }
  1961. status = HAL_STATUS_SUCCESS;
  1962. notify:
  1963. /*
  1964. * In case of error in handling request we need to send event with
  1965. * service id of cmd and gatt failure status.
  1966. */
  1967. send_client_incl_service_notify(&srvc_id, incl_service, cmd->conn_id);
  1968. reply:
  1969. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  1970. HAL_OP_GATT_CLIENT_GET_INCLUDED_SERVICE, status);
  1971. }
  1972. static void send_client_char_notify(const struct hal_gatt_srvc_id *service,
  1973. const struct hal_gatt_gatt_id *charac,
  1974. int32_t char_prop, int32_t conn_id)
  1975. {
  1976. struct hal_ev_gatt_client_get_characteristic ev;
  1977. ev.conn_id = conn_id;
  1978. if (charac) {
  1979. memcpy(&ev.char_id, charac, sizeof(struct hal_gatt_gatt_id));
  1980. ev.char_prop = char_prop;
  1981. ev.status = GATT_SUCCESS;
  1982. } else {
  1983. memset(&ev.char_id, 0, sizeof(struct hal_gatt_gatt_id));
  1984. ev.char_prop = 0;
  1985. ev.status = GATT_FAILURE;
  1986. }
  1987. memcpy(&ev.srvc_id, service, sizeof(struct hal_gatt_srvc_id));
  1988. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  1989. HAL_EV_GATT_CLIENT_GET_CHARACTERISTIC,
  1990. sizeof(ev), &ev);
  1991. }
  1992. static void convert_send_client_char_notify(const struct characteristic *ch,
  1993. int32_t conn_id,
  1994. const struct service *service)
  1995. {
  1996. struct hal_gatt_srvc_id srvc;
  1997. struct hal_gatt_gatt_id charac;
  1998. element_id_to_hal_srvc_id(&service->id, service->primary, &srvc);
  1999. if (ch) {
  2000. element_id_to_hal_gatt_id(&ch->id, &charac);
  2001. send_client_char_notify(&srvc, &charac, ch->ch.properties,
  2002. conn_id);
  2003. } else {
  2004. send_client_char_notify(&srvc, NULL, 0, conn_id);
  2005. }
  2006. }
  2007. static void cache_all_srvc_chars(struct service *srvc, GSList *characteristics)
  2008. {
  2009. uint16_t inst_id = 0;
  2010. bt_uuid_t uuid;
  2011. for (; characteristics; characteristics = characteristics->next) {
  2012. struct characteristic *ch;
  2013. ch = new0(struct characteristic, 1);
  2014. ch->descriptors = queue_new();
  2015. memcpy(&ch->ch, characteristics->data, sizeof(ch->ch));
  2016. bt_string_to_uuid(&uuid, ch->ch.uuid);
  2017. bt_uuid_to_uuid128(&uuid, &ch->id.uuid);
  2018. /*
  2019. * For now we increment inst_id and use it as characteristic
  2020. * handle
  2021. */
  2022. ch->id.instance = ++inst_id;
  2023. /* Store end handle to use later for descriptors discovery */
  2024. if (characteristics->next) {
  2025. struct gatt_char *next = characteristics->next->data;
  2026. ch->end_handle = next->handle - 1;
  2027. } else {
  2028. ch->end_handle = srvc->primary ? srvc->prim.range.end :
  2029. srvc->incl.range.end;
  2030. }
  2031. DBG("attr handle = 0x%04x, end handle = 0x%04x uuid: %s",
  2032. ch->ch.handle, ch->end_handle, ch->ch.uuid);
  2033. queue_push_tail(srvc->chars, ch);
  2034. }
  2035. }
  2036. struct discover_char_data {
  2037. int32_t conn_id;
  2038. struct service *service;
  2039. };
  2040. static void discover_char_cb(uint8_t status, GSList *characteristics,
  2041. void *user_data)
  2042. {
  2043. struct discover_char_data *data = user_data;
  2044. struct service *srvc = data->service;
  2045. if (status) {
  2046. error("gatt: Failed to get characteristics: %s",
  2047. att_ecode2str(status));
  2048. convert_send_client_char_notify(NULL, data->conn_id, srvc);
  2049. goto done;
  2050. }
  2051. if (queue_isempty(srvc->chars))
  2052. cache_all_srvc_chars(srvc, characteristics);
  2053. convert_send_client_char_notify(queue_peek_head(srvc->chars),
  2054. data->conn_id, srvc);
  2055. done:
  2056. free(data);
  2057. }
  2058. static void handle_client_get_characteristic(const void *buf, uint16_t len)
  2059. {
  2060. const struct hal_cmd_gatt_client_get_characteristic *cmd = buf;
  2061. struct characteristic *ch;
  2062. struct element_id match_id;
  2063. struct app_connection *conn;
  2064. struct service *srvc;
  2065. uint8_t status;
  2066. DBG("");
  2067. if (len != sizeof(*cmd) + (cmd->continuation ? sizeof(cmd->char_id[0]) : 0)) {
  2068. error("Invalid get characteristic size (%u bytes), terminating",
  2069. len);
  2070. raise(SIGTERM);
  2071. return;
  2072. }
  2073. hal_srvc_id_to_element_id(&cmd->srvc_id, &match_id);
  2074. if (!find_service(cmd->conn_id, &match_id, &conn, &srvc)) {
  2075. status = HAL_STATUS_FAILED;
  2076. goto done;
  2077. }
  2078. /* Discover all characteristics for services if not cached yet */
  2079. if (queue_isempty(srvc->chars)) {
  2080. struct discover_char_data *cb_data;
  2081. struct att_range range;
  2082. cb_data = new0(struct discover_char_data, 1);
  2083. cb_data->service = srvc;
  2084. cb_data->conn_id = conn->id;
  2085. range = srvc->primary ? srvc->prim.range : srvc->incl.range;
  2086. if (!gatt_discover_char(conn->device->attrib, range.start,
  2087. range.end, NULL,
  2088. discover_char_cb, cb_data)) {
  2089. free(cb_data);
  2090. status = HAL_STATUS_FAILED;
  2091. goto done;
  2092. }
  2093. status = HAL_STATUS_SUCCESS;
  2094. goto done;
  2095. }
  2096. if (cmd->continuation)
  2097. ch = queue_find(srvc->chars, match_char_by_higher_inst_id,
  2098. INT_TO_PTR(cmd->char_id[0].inst_id));
  2099. else
  2100. ch = queue_peek_head(srvc->chars);
  2101. convert_send_client_char_notify(ch, conn->id, srvc);
  2102. status = HAL_STATUS_SUCCESS;
  2103. done:
  2104. if (status != HAL_STATUS_SUCCESS)
  2105. send_client_char_notify(&cmd->srvc_id, NULL, 0, cmd->conn_id);
  2106. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  2107. HAL_OP_GATT_CLIENT_GET_CHARACTERISTIC, status);
  2108. }
  2109. static void send_client_descr_notify(int32_t status, int32_t conn_id,
  2110. bool primary,
  2111. const struct element_id *srvc,
  2112. const struct element_id *ch,
  2113. const struct element_id *opt_descr)
  2114. {
  2115. struct hal_ev_gatt_client_get_descriptor ev;
  2116. memset(&ev, 0, sizeof(ev));
  2117. ev.status = status;
  2118. ev.conn_id = conn_id;
  2119. element_id_to_hal_srvc_id(srvc, primary, &ev.srvc_id);
  2120. element_id_to_hal_gatt_id(ch, &ev.char_id);
  2121. if (opt_descr)
  2122. element_id_to_hal_gatt_id(opt_descr, &ev.descr_id);
  2123. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  2124. HAL_EV_GATT_CLIENT_GET_DESCRIPTOR, sizeof(ev), &ev);
  2125. }
  2126. struct discover_desc_data {
  2127. struct app_connection *conn;
  2128. struct service *srvc;
  2129. struct characteristic *ch;
  2130. };
  2131. static void gatt_discover_desc_cb(guint8 status, GSList *descs,
  2132. gpointer user_data)
  2133. {
  2134. struct discover_desc_data *data = user_data;
  2135. struct app_connection *conn = data->conn;
  2136. struct service *srvc = data->srvc;
  2137. struct characteristic *ch = data->ch;
  2138. struct descriptor *descr;
  2139. int i = 0;
  2140. if (status != 0) {
  2141. error("Discover all characteristic descriptors failed [%s]: %s",
  2142. ch->ch.uuid, att_ecode2str(status));
  2143. goto reply;
  2144. }
  2145. for ( ; descs; descs = descs->next) {
  2146. struct gatt_desc *desc = descs->data;
  2147. bt_uuid_t uuid;
  2148. descr = new0(struct descriptor, 1);
  2149. bt_string_to_uuid(&uuid, desc->uuid);
  2150. bt_uuid_to_uuid128(&uuid, &descr->id.uuid);
  2151. descr->id.instance = ++i;
  2152. descr->handle = desc->handle;
  2153. DBG("attr handle = 0x%04x, uuid: %s", desc->handle, desc->uuid);
  2154. queue_push_tail(ch->descriptors, descr);
  2155. }
  2156. reply:
  2157. descr = queue_peek_head(ch->descriptors);
  2158. send_client_descr_notify(status ? GATT_FAILURE : GATT_SUCCESS, conn->id,
  2159. srvc->primary, &srvc->id, &ch->id,
  2160. descr ? &descr->id : NULL);
  2161. free(data);
  2162. }
  2163. static bool build_descr_cache(struct app_connection *conn, struct service *srvc,
  2164. struct characteristic *ch)
  2165. {
  2166. struct discover_desc_data *cb_data;
  2167. uint16_t start, end;
  2168. /* Clip range to given characteristic */
  2169. start = ch->ch.value_handle + 1;
  2170. end = ch->end_handle;
  2171. /* If there are no descriptors, notify with fail status. */
  2172. if (start > end)
  2173. return false;
  2174. cb_data = new0(struct discover_desc_data, 1);
  2175. cb_data->conn = conn;
  2176. cb_data->srvc = srvc;
  2177. cb_data->ch = ch;
  2178. if (!gatt_discover_desc(conn->device->attrib, start, end, NULL,
  2179. gatt_discover_desc_cb, cb_data)) {
  2180. free(cb_data);
  2181. return false;
  2182. }
  2183. return true;
  2184. }
  2185. static void handle_client_get_descriptor(const void *buf, uint16_t len)
  2186. {
  2187. const struct hal_cmd_gatt_client_get_descriptor *cmd = buf;
  2188. struct descriptor *descr = NULL;
  2189. struct characteristic *ch;
  2190. struct service *srvc;
  2191. struct element_id srvc_id;
  2192. struct element_id char_id;
  2193. struct app_connection *conn;
  2194. int32_t conn_id;
  2195. uint8_t primary;
  2196. uint8_t status;
  2197. DBG("");
  2198. if (len != sizeof(*cmd) +
  2199. (cmd->continuation ? sizeof(cmd->descr_id[0]) : 0)) {
  2200. error("gatt: Invalid get descr command (%u bytes), terminating",
  2201. len);
  2202. raise(SIGTERM);
  2203. return;
  2204. }
  2205. conn_id = cmd->conn_id;
  2206. primary = cmd->srvc_id.is_primary;
  2207. hal_srvc_id_to_element_id(&cmd->srvc_id, &srvc_id);
  2208. hal_gatt_id_to_element_id(&cmd->char_id, &char_id);
  2209. if (!find_service(conn_id, &srvc_id, &conn, &srvc)) {
  2210. error("gatt: Get descr. could not find service");
  2211. status = HAL_STATUS_FAILED;
  2212. goto failed;
  2213. }
  2214. ch = queue_find(srvc->chars, match_char_by_element_id, &char_id);
  2215. if (!ch) {
  2216. error("gatt: Get descr. could not find characteristic");
  2217. status = HAL_STATUS_FAILED;
  2218. goto failed;
  2219. }
  2220. if (queue_isempty(ch->descriptors)) {
  2221. if (build_descr_cache(conn, srvc, ch)) {
  2222. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  2223. HAL_OP_GATT_CLIENT_GET_DESCRIPTOR,
  2224. HAL_STATUS_SUCCESS);
  2225. return;
  2226. }
  2227. }
  2228. status = HAL_STATUS_SUCCESS;
  2229. /* Send from cache */
  2230. if (cmd->continuation)
  2231. descr = queue_find(ch->descriptors,
  2232. match_descr_by_higher_inst_id,
  2233. INT_TO_PTR(cmd->descr_id[0].inst_id));
  2234. else
  2235. descr = queue_peek_head(ch->descriptors);
  2236. failed:
  2237. send_client_descr_notify(descr ? GATT_SUCCESS : GATT_FAILURE, conn_id,
  2238. primary, &srvc_id, &char_id,
  2239. &descr->id);
  2240. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  2241. HAL_OP_GATT_CLIENT_GET_DESCRIPTOR, status);
  2242. }
  2243. struct char_op_data {
  2244. int32_t conn_id;
  2245. const struct element_id *srvc_id;
  2246. const struct element_id *char_id;
  2247. uint8_t primary;
  2248. };
  2249. static struct char_op_data *create_char_op_data(int32_t conn_id,
  2250. const struct element_id *s_id,
  2251. const struct element_id *ch_id,
  2252. bool primary)
  2253. {
  2254. struct char_op_data *d;
  2255. d = new0(struct char_op_data, 1);
  2256. d->conn_id = conn_id;
  2257. d->srvc_id = s_id;
  2258. d->char_id = ch_id;
  2259. d->primary = primary;
  2260. return d;
  2261. }
  2262. static void send_client_read_char_notify(int32_t status, const uint8_t *pdu,
  2263. uint16_t len, int32_t conn_id,
  2264. const struct element_id *s_id,
  2265. const struct element_id *ch_id,
  2266. uint8_t primary)
  2267. {
  2268. uint8_t buf[IPC_MTU];
  2269. struct hal_ev_gatt_client_read_characteristic *ev = (void *) buf;
  2270. ssize_t vlen;
  2271. memset(buf, 0, sizeof(buf));
  2272. ev->conn_id = conn_id;
  2273. ev->status = status;
  2274. ev->data.status = status;
  2275. element_id_to_hal_srvc_id(s_id, primary, &ev->data.srvc_id);
  2276. element_id_to_hal_gatt_id(ch_id, &ev->data.char_id);
  2277. if (status == 0 && pdu) {
  2278. vlen = dec_read_resp(pdu, len, ev->data.value, sizeof(buf));
  2279. if (vlen < 0) {
  2280. error("gatt: Protocol error");
  2281. ev->status = GATT_FAILURE;
  2282. } else {
  2283. ev->data.len = vlen;
  2284. }
  2285. }
  2286. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  2287. HAL_EV_GATT_CLIENT_READ_CHARACTERISTIC,
  2288. sizeof(*ev) + ev->data.len, ev);
  2289. }
  2290. static void read_char_cb(guint8 status, const guint8 *pdu, guint16 len,
  2291. gpointer user_data)
  2292. {
  2293. struct char_op_data *data = user_data;
  2294. send_client_read_char_notify(status, pdu, len, data->conn_id,
  2295. data->srvc_id, data->char_id,
  2296. data->primary);
  2297. free(data);
  2298. }
  2299. static int get_cid(struct gatt_device *dev)
  2300. {
  2301. GIOChannel *io;
  2302. uint16_t cid;
  2303. io = g_attrib_get_channel(dev->attrib);
  2304. if (!bt_io_get(io, NULL, BT_IO_OPT_CID, &cid, BT_IO_OPT_INVALID)) {
  2305. error("gatt: Failed to get CID");
  2306. return -1;
  2307. }
  2308. return cid;
  2309. }
  2310. static int get_sec_level(struct gatt_device *dev)
  2311. {
  2312. GIOChannel *io;
  2313. int sec_level;
  2314. io = g_attrib_get_channel(dev->attrib);
  2315. if (!bt_io_get(io, NULL, BT_IO_OPT_SEC_LEVEL, &sec_level,
  2316. BT_IO_OPT_INVALID)) {
  2317. error("gatt: Failed to get sec_level");
  2318. return -1;
  2319. }
  2320. return sec_level;
  2321. }
  2322. static bool set_security(struct gatt_device *device, int req_sec_level)
  2323. {
  2324. int sec_level;
  2325. GError *gerr = NULL;
  2326. GIOChannel *io;
  2327. sec_level = get_sec_level(device);
  2328. if (sec_level < 0)
  2329. return false;
  2330. if (req_sec_level <= sec_level)
  2331. return true;
  2332. io = g_attrib_get_channel(device->attrib);
  2333. if (!io)
  2334. return false;
  2335. bt_io_set(io, &gerr, BT_IO_OPT_SEC_LEVEL, req_sec_level,
  2336. BT_IO_OPT_INVALID);
  2337. if (gerr) {
  2338. error("gatt: Failed to set security level: %s", gerr->message);
  2339. g_error_free(gerr);
  2340. return false;
  2341. }
  2342. return true;
  2343. }
  2344. bool bt_gatt_set_security(const bdaddr_t *bdaddr, int sec_level)
  2345. {
  2346. struct gatt_device *device;
  2347. device = find_device_by_addr(bdaddr);
  2348. if (!device)
  2349. return false;
  2350. return set_security(device, sec_level);
  2351. }
  2352. static bool set_auth_type(struct gatt_device *device, int auth_type)
  2353. {
  2354. int sec_level;
  2355. switch (auth_type) {
  2356. case HAL_GATT_AUTHENTICATION_MITM:
  2357. sec_level = BT_SECURITY_HIGH;
  2358. break;
  2359. case HAL_GATT_AUTHENTICATION_NO_MITM:
  2360. sec_level = BT_SECURITY_MEDIUM;
  2361. break;
  2362. case HAL_GATT_AUTHENTICATION_NONE:
  2363. sec_level = BT_SECURITY_LOW;
  2364. break;
  2365. default:
  2366. error("gatt: Invalid auth_type value: %d", auth_type);
  2367. return false;
  2368. }
  2369. return set_security(device, sec_level);
  2370. }
  2371. static void handle_client_read_characteristic(const void *buf, uint16_t len)
  2372. {
  2373. const struct hal_cmd_gatt_client_read_characteristic *cmd = buf;
  2374. struct char_op_data *cb_data;
  2375. struct characteristic *ch;
  2376. struct app_connection *conn;
  2377. struct service *srvc;
  2378. struct element_id srvc_id;
  2379. struct element_id char_id;
  2380. uint8_t status;
  2381. DBG("");
  2382. /* TODO authorization needs to be handled */
  2383. hal_srvc_id_to_element_id(&cmd->srvc_id, &srvc_id);
  2384. hal_gatt_id_to_element_id(&cmd->char_id, &char_id);
  2385. if (!find_service(cmd->conn_id, &srvc_id, &conn, &srvc)) {
  2386. status = HAL_STATUS_FAILED;
  2387. goto failed;
  2388. }
  2389. /* search characteristics by element id */
  2390. ch = queue_find(srvc->chars, match_char_by_element_id, &char_id);
  2391. if (!ch) {
  2392. error("gatt: Characteristic with inst_id: %d not found",
  2393. cmd->char_id.inst_id);
  2394. status = HAL_STATUS_FAILED;
  2395. goto failed;
  2396. }
  2397. cb_data = create_char_op_data(cmd->conn_id, &srvc->id, &ch->id,
  2398. cmd->srvc_id.is_primary);
  2399. if (!set_auth_type(conn->device, cmd->auth_req)) {
  2400. error("gatt: Failed to set security %d", cmd->auth_req);
  2401. status = HAL_STATUS_FAILED;
  2402. free(cb_data);
  2403. goto failed;
  2404. }
  2405. if (!gatt_read_char(conn->device->attrib, ch->ch.value_handle,
  2406. read_char_cb, cb_data)) {
  2407. error("gatt: Cannot read characteristic with inst_id: %d",
  2408. cmd->char_id.inst_id);
  2409. status = HAL_STATUS_FAILED;
  2410. free(cb_data);
  2411. goto failed;
  2412. }
  2413. status = HAL_STATUS_SUCCESS;
  2414. failed:
  2415. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  2416. HAL_OP_GATT_CLIENT_READ_CHARACTERISTIC, status);
  2417. /*
  2418. * We should send notification with service, characteristic id in case
  2419. * of errors.
  2420. */
  2421. if (status != HAL_STATUS_SUCCESS)
  2422. send_client_read_char_notify(GATT_FAILURE, NULL, 0,
  2423. cmd->conn_id, &srvc_id,
  2424. &char_id,
  2425. cmd->srvc_id.is_primary);
  2426. }
  2427. static void send_client_write_char_notify(int32_t status, int32_t conn_id,
  2428. const struct element_id *srvc_id,
  2429. const struct element_id *char_id,
  2430. uint8_t primary)
  2431. {
  2432. struct hal_ev_gatt_client_write_characteristic ev;
  2433. memset(&ev, 0, sizeof(ev));
  2434. ev.conn_id = conn_id;
  2435. ev.status = status;
  2436. ev.data.status = status;
  2437. element_id_to_hal_srvc_id(srvc_id, primary, &ev.data.srvc_id);
  2438. element_id_to_hal_gatt_id(char_id, &ev.data.char_id);
  2439. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  2440. HAL_EV_GATT_CLIENT_WRITE_CHARACTERISTIC,
  2441. sizeof(ev), &ev);
  2442. }
  2443. static void write_char_cb(guint8 status, const guint8 *pdu, guint16 len,
  2444. gpointer user_data)
  2445. {
  2446. struct char_op_data *data = user_data;
  2447. send_client_write_char_notify(status, data->conn_id, data->srvc_id,
  2448. data->char_id, data->primary);
  2449. free(data);
  2450. }
  2451. static guint signed_write_cmd(struct gatt_device *dev, uint16_t handle,
  2452. const uint8_t *value, uint16_t vlen)
  2453. {
  2454. uint8_t csrk[16];
  2455. uint32_t sign_cnt;
  2456. guint res;
  2457. memset(csrk, 0, 16);
  2458. if (!bt_get_csrk(&dev->bdaddr, true, csrk, &sign_cnt, NULL)) {
  2459. error("gatt: Could not get csrk key");
  2460. return 0;
  2461. }
  2462. res = gatt_signed_write_cmd(dev->attrib, handle, value, vlen, crypto,
  2463. csrk, sign_cnt, NULL, NULL);
  2464. if (!res) {
  2465. error("gatt: Signed write command failed");
  2466. return 0;
  2467. }
  2468. bt_update_sign_counter(&dev->bdaddr, true, ++sign_cnt);
  2469. return res;
  2470. }
  2471. static void handle_client_write_characteristic(const void *buf, uint16_t len)
  2472. {
  2473. const struct hal_cmd_gatt_client_write_characteristic *cmd = buf;
  2474. struct char_op_data *cb_data = NULL;
  2475. struct characteristic *ch;
  2476. struct app_connection *conn;
  2477. struct service *srvc;
  2478. struct element_id srvc_id;
  2479. struct element_id char_id;
  2480. uint8_t status;
  2481. guint res;
  2482. DBG("");
  2483. if (len != sizeof(*cmd) + cmd->len) {
  2484. error("Invalid write char size (%u bytes), terminating", len);
  2485. raise(SIGTERM);
  2486. return;
  2487. }
  2488. hal_srvc_id_to_element_id(&cmd->srvc_id, &srvc_id);
  2489. hal_gatt_id_to_element_id(&cmd->char_id, &char_id);
  2490. if (!find_service(cmd->conn_id, &srvc_id, &conn, &srvc)) {
  2491. status = HAL_STATUS_FAILED;
  2492. goto failed;
  2493. }
  2494. /* search characteristics by instance id */
  2495. ch = queue_find(srvc->chars, match_char_by_element_id, &char_id);
  2496. if (!ch) {
  2497. error("gatt: Characteristic with inst_id: %d not found",
  2498. cmd->char_id.inst_id);
  2499. status = HAL_STATUS_FAILED;
  2500. goto failed;
  2501. }
  2502. if (cmd->write_type == GATT_WRITE_TYPE_PREPARE ||
  2503. cmd->write_type == GATT_WRITE_TYPE_DEFAULT) {
  2504. cb_data = create_char_op_data(cmd->conn_id, &srvc->id, &ch->id,
  2505. cmd->srvc_id.is_primary);
  2506. }
  2507. if (!set_auth_type(conn->device, cmd->auth_req)) {
  2508. error("gatt: Failed to set security %d", cmd->auth_req);
  2509. status = HAL_STATUS_FAILED;
  2510. goto failed;
  2511. }
  2512. switch (cmd->write_type) {
  2513. case GATT_WRITE_TYPE_NO_RESPONSE:
  2514. res = gatt_write_cmd(conn->device->attrib, ch->ch.value_handle,
  2515. cmd->value, cmd->len,
  2516. NULL, NULL);
  2517. break;
  2518. case GATT_WRITE_TYPE_PREPARE:
  2519. res = gatt_reliable_write_char(conn->device->attrib,
  2520. ch->ch.value_handle,
  2521. cmd->value, cmd->len,
  2522. write_char_cb, cb_data);
  2523. break;
  2524. case GATT_WRITE_TYPE_DEFAULT:
  2525. res = gatt_write_char(conn->device->attrib, ch->ch.value_handle,
  2526. cmd->value, cmd->len,
  2527. write_char_cb, cb_data);
  2528. break;
  2529. case GATT_WRITE_TYPE_SIGNED:
  2530. if (get_cid(conn->device) != ATT_CID) {
  2531. error("gatt: Cannot write signed on BR/EDR bearer");
  2532. status = HAL_STATUS_FAILED;
  2533. goto failed;
  2534. }
  2535. if (get_sec_level(conn->device) > BT_SECURITY_LOW)
  2536. res = gatt_write_cmd(conn->device->attrib,
  2537. ch->ch.value_handle, cmd->value,
  2538. cmd->len, NULL, NULL);
  2539. else
  2540. res = signed_write_cmd(conn->device,
  2541. ch->ch.value_handle, cmd->value,
  2542. cmd->len);
  2543. break;
  2544. default:
  2545. error("gatt: Write type %d unsupported", cmd->write_type);
  2546. status = HAL_STATUS_UNSUPPORTED;
  2547. goto failed;
  2548. }
  2549. if (!res) {
  2550. error("gatt: Cannot write char. with inst_id: %d",
  2551. cmd->char_id.inst_id);
  2552. status = HAL_STATUS_FAILED;
  2553. goto failed;
  2554. }
  2555. status = HAL_STATUS_SUCCESS;
  2556. failed:
  2557. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  2558. HAL_OP_GATT_CLIENT_WRITE_CHARACTERISTIC, status);
  2559. /*
  2560. * We should send notification with service, characteristic id in case
  2561. * of error and write with no response
  2562. */
  2563. if (status != HAL_STATUS_SUCCESS ||
  2564. cmd->write_type == GATT_WRITE_TYPE_NO_RESPONSE ||
  2565. cmd->write_type == GATT_WRITE_TYPE_SIGNED) {
  2566. int32_t gatt_status = (status == HAL_STATUS_SUCCESS) ?
  2567. GATT_SUCCESS : GATT_FAILURE;
  2568. send_client_write_char_notify(gatt_status, cmd->conn_id,
  2569. &srvc_id, &char_id,
  2570. cmd->srvc_id.is_primary);
  2571. free(cb_data);
  2572. }
  2573. }
  2574. static void send_client_descr_read_notify(int32_t status, const uint8_t *pdu,
  2575. guint16 len, int32_t conn_id,
  2576. const struct element_id *srvc,
  2577. const struct element_id *ch,
  2578. const struct element_id *descr,
  2579. uint8_t primary)
  2580. {
  2581. uint8_t buf[IPC_MTU];
  2582. struct hal_ev_gatt_client_read_descriptor *ev = (void *) buf;
  2583. memset(buf, 0, sizeof(buf));
  2584. ev->status = status;
  2585. ev->conn_id = conn_id;
  2586. ev->data.status = ev->status;
  2587. element_id_to_hal_srvc_id(srvc, primary, &ev->data.srvc_id);
  2588. element_id_to_hal_gatt_id(ch, &ev->data.char_id);
  2589. element_id_to_hal_gatt_id(descr, &ev->data.descr_id);
  2590. if (status == 0 && pdu) {
  2591. ssize_t ret;
  2592. ret = dec_read_resp(pdu, len, ev->data.value,
  2593. GATT_MAX_ATTR_LEN);
  2594. if (ret < 0) {
  2595. error("gatt: Protocol error");
  2596. ev->status = GATT_FAILURE;
  2597. } else {
  2598. ev->data.len = ret;
  2599. }
  2600. }
  2601. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  2602. HAL_EV_GATT_CLIENT_READ_DESCRIPTOR,
  2603. sizeof(*ev) + ev->data.len, ev);
  2604. }
  2605. struct desc_data {
  2606. int32_t conn_id;
  2607. const struct element_id *srvc_id;
  2608. const struct element_id *char_id;
  2609. const struct element_id *descr_id;
  2610. uint8_t primary;
  2611. };
  2612. static void read_desc_cb(guint8 status, const guint8 *pdu, guint16 len,
  2613. gpointer user_data)
  2614. {
  2615. struct desc_data *cb_data = user_data;
  2616. if (status != 0)
  2617. error("gatt: Discover all char descriptors failed: %s",
  2618. att_ecode2str(status));
  2619. send_client_descr_read_notify(status, pdu, len, cb_data->conn_id,
  2620. cb_data->srvc_id, cb_data->char_id,
  2621. cb_data->descr_id, cb_data->primary);
  2622. free(cb_data);
  2623. }
  2624. static struct desc_data *create_desc_data(int32_t conn_id,
  2625. const struct element_id *s_id,
  2626. const struct element_id *ch_id,
  2627. const struct element_id *d_id,
  2628. uint8_t primary)
  2629. {
  2630. struct desc_data *d;
  2631. d = new0(struct desc_data, 1);
  2632. d->conn_id = conn_id;
  2633. d->srvc_id = s_id;
  2634. d->char_id = ch_id;
  2635. d->descr_id = d_id;
  2636. d->primary = primary;
  2637. return d;
  2638. }
  2639. static void handle_client_read_descriptor(const void *buf, uint16_t len)
  2640. {
  2641. const struct hal_cmd_gatt_client_read_descriptor *cmd = buf;
  2642. struct desc_data *cb_data;
  2643. struct characteristic *ch;
  2644. struct descriptor *descr;
  2645. struct service *srvc;
  2646. struct element_id char_id;
  2647. struct element_id descr_id;
  2648. struct element_id srvc_id;
  2649. struct app_connection *conn;
  2650. int32_t conn_id = 0;
  2651. uint8_t primary;
  2652. uint8_t status;
  2653. DBG("");
  2654. conn_id = cmd->conn_id;
  2655. primary = cmd->srvc_id.is_primary;
  2656. hal_srvc_id_to_element_id(&cmd->srvc_id, &srvc_id);
  2657. hal_gatt_id_to_element_id(&cmd->char_id, &char_id);
  2658. hal_gatt_id_to_element_id(&cmd->descr_id, &descr_id);
  2659. if (!find_service(conn_id, &srvc_id, &conn, &srvc)) {
  2660. error("gatt: Read descr. could not find service");
  2661. status = HAL_STATUS_FAILED;
  2662. goto failed;
  2663. }
  2664. ch = queue_find(srvc->chars, match_char_by_element_id, &char_id);
  2665. if (!ch) {
  2666. error("gatt: Read descr. could not find characteristic");
  2667. status = HAL_STATUS_FAILED;
  2668. goto failed;
  2669. }
  2670. descr = queue_find(ch->descriptors, match_descr_by_element_id,
  2671. &descr_id);
  2672. if (!descr) {
  2673. error("gatt: Read descr. could not find descriptor");
  2674. status = HAL_STATUS_FAILED;
  2675. goto failed;
  2676. }
  2677. cb_data = create_desc_data(conn_id, &srvc->id, &ch->id, &descr->id,
  2678. primary);
  2679. if (!set_auth_type(conn->device, cmd->auth_req)) {
  2680. error("gatt: Failed to set security %d", cmd->auth_req);
  2681. status = HAL_STATUS_FAILED;
  2682. free(cb_data);
  2683. goto failed;
  2684. }
  2685. if (!gatt_read_char(conn->device->attrib, descr->handle, read_desc_cb,
  2686. cb_data)) {
  2687. free(cb_data);
  2688. status = HAL_STATUS_FAILED;
  2689. goto failed;
  2690. }
  2691. status = HAL_STATUS_SUCCESS;
  2692. failed:
  2693. if (status != HAL_STATUS_SUCCESS)
  2694. send_client_descr_read_notify(GATT_FAILURE, NULL, 0, conn_id,
  2695. &srvc_id, &char_id, &descr_id,
  2696. primary);
  2697. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  2698. HAL_OP_GATT_CLIENT_READ_DESCRIPTOR, status);
  2699. }
  2700. static void send_client_descr_write_notify(int32_t status, int32_t conn_id,
  2701. const struct element_id *srvc,
  2702. const struct element_id *ch,
  2703. const struct element_id *descr,
  2704. uint8_t primary) {
  2705. uint8_t buf[IPC_MTU];
  2706. struct hal_ev_gatt_client_write_descriptor *ev = (void *) buf;
  2707. memset(buf, 0, sizeof(buf));
  2708. ev->status = status;
  2709. ev->conn_id = conn_id;
  2710. element_id_to_hal_srvc_id(srvc, primary, &ev->data.srvc_id);
  2711. element_id_to_hal_gatt_id(ch, &ev->data.char_id);
  2712. element_id_to_hal_gatt_id(descr, &ev->data.descr_id);
  2713. ev->data.status = status;
  2714. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  2715. HAL_EV_GATT_CLIENT_WRITE_DESCRIPTOR,
  2716. sizeof(*ev), ev);
  2717. }
  2718. static void write_descr_cb(guint8 status, const guint8 *pdu, guint16 len,
  2719. gpointer user_data)
  2720. {
  2721. struct desc_data *cb_data = user_data;
  2722. if (status)
  2723. error("gatt: Write descriptors failed: %s",
  2724. att_ecode2str(status));
  2725. send_client_descr_write_notify(status, cb_data->conn_id,
  2726. cb_data->srvc_id, cb_data->char_id,
  2727. cb_data->descr_id, cb_data->primary);
  2728. free(cb_data);
  2729. }
  2730. static void handle_client_write_descriptor(const void *buf, uint16_t len)
  2731. {
  2732. const struct hal_cmd_gatt_client_write_descriptor *cmd = buf;
  2733. struct desc_data *cb_data = NULL;
  2734. struct characteristic *ch;
  2735. struct descriptor *descr;
  2736. struct service *srvc;
  2737. struct element_id srvc_id;
  2738. struct element_id char_id;
  2739. struct element_id descr_id;
  2740. struct app_connection *conn;
  2741. int32_t conn_id;
  2742. uint8_t primary;
  2743. uint8_t status;
  2744. guint res;
  2745. DBG("");
  2746. if (len != sizeof(*cmd) + cmd->len) {
  2747. error("Invalid write desriptor command (%u bytes), terminating",
  2748. len);
  2749. raise(SIGTERM);
  2750. return;
  2751. }
  2752. primary = cmd->srvc_id.is_primary;
  2753. conn_id = cmd->conn_id;
  2754. hal_srvc_id_to_element_id(&cmd->srvc_id, &srvc_id);
  2755. hal_gatt_id_to_element_id(&cmd->char_id, &char_id);
  2756. hal_gatt_id_to_element_id(&cmd->descr_id, &descr_id);
  2757. if (!find_service(cmd->conn_id, &srvc_id, &conn, &srvc)) {
  2758. error("gatt: Write descr. could not find service");
  2759. status = HAL_STATUS_FAILED;
  2760. goto failed;
  2761. }
  2762. ch = queue_find(srvc->chars, match_char_by_element_id, &char_id);
  2763. if (!ch) {
  2764. error("gatt: Write descr. could not find characteristic");
  2765. status = HAL_STATUS_FAILED;
  2766. goto failed;
  2767. }
  2768. descr = queue_find(ch->descriptors, match_descr_by_element_id,
  2769. &descr_id);
  2770. if (!descr) {
  2771. error("gatt: Write descr. could not find descriptor");
  2772. status = HAL_STATUS_FAILED;
  2773. goto failed;
  2774. }
  2775. if (cmd->write_type != GATT_WRITE_TYPE_NO_RESPONSE)
  2776. cb_data = create_desc_data(conn_id, &srvc->id, &ch->id,
  2777. &descr->id, primary);
  2778. if (!set_auth_type(conn->device, cmd->auth_req)) {
  2779. error("gatt: Failed to set security %d", cmd->auth_req);
  2780. status = HAL_STATUS_FAILED;
  2781. goto failed;
  2782. }
  2783. switch (cmd->write_type) {
  2784. case GATT_WRITE_TYPE_NO_RESPONSE:
  2785. res = gatt_write_cmd(conn->device->attrib, descr->handle,
  2786. cmd->value, cmd->len, NULL , NULL);
  2787. break;
  2788. case GATT_WRITE_TYPE_PREPARE:
  2789. res = gatt_reliable_write_char(conn->device->attrib,
  2790. descr->handle, cmd->value,
  2791. cmd->len, write_descr_cb,
  2792. cb_data);
  2793. break;
  2794. case GATT_WRITE_TYPE_DEFAULT:
  2795. res = gatt_write_char(conn->device->attrib, descr->handle,
  2796. cmd->value, cmd->len,
  2797. write_descr_cb, cb_data);
  2798. break;
  2799. default:
  2800. error("gatt: Write type %d unsupported", cmd->write_type);
  2801. status = HAL_STATUS_UNSUPPORTED;
  2802. goto failed;
  2803. }
  2804. if (!res) {
  2805. error("gatt: Write desc, could not write desc");
  2806. status = HAL_STATUS_FAILED;
  2807. goto failed;
  2808. }
  2809. status = HAL_STATUS_SUCCESS;
  2810. failed:
  2811. if (status != HAL_STATUS_SUCCESS ||
  2812. cmd->write_type == GATT_WRITE_TYPE_NO_RESPONSE) {
  2813. int32_t gatt_status = (status == HAL_STATUS_SUCCESS) ?
  2814. GATT_SUCCESS : GATT_FAILURE;
  2815. send_client_descr_write_notify(gatt_status, conn_id, &srvc_id,
  2816. &char_id, &descr_id, primary);
  2817. free(cb_data);
  2818. }
  2819. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  2820. HAL_OP_GATT_CLIENT_WRITE_DESCRIPTOR, status);
  2821. }
  2822. static void send_client_write_execute_notify(int32_t id, int32_t status)
  2823. {
  2824. struct hal_ev_gatt_client_exec_write ev;
  2825. ev.conn_id = id;
  2826. ev.status = status;
  2827. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  2828. HAL_EV_GATT_CLIENT_EXEC_WRITE,
  2829. sizeof(ev), &ev);
  2830. }
  2831. static void write_execute_cb(guint8 status, const guint8 *pdu, guint16 len,
  2832. gpointer user_data)
  2833. {
  2834. send_client_write_execute_notify(PTR_TO_INT(user_data), status);
  2835. }
  2836. static void handle_client_execute_write(const void *buf, uint16_t len)
  2837. {
  2838. const struct hal_cmd_gatt_client_execute_write *cmd = buf;
  2839. struct app_connection *conn;
  2840. uint8_t status;
  2841. uint8_t flags;
  2842. DBG("");
  2843. conn = find_connection_by_id(cmd->conn_id);
  2844. if (!conn) {
  2845. status = HAL_STATUS_FAILED;
  2846. goto reply;
  2847. }
  2848. flags = cmd->execute ? ATT_WRITE_ALL_PREP_WRITES :
  2849. ATT_CANCEL_ALL_PREP_WRITES;
  2850. if (!gatt_execute_write(conn->device->attrib, flags, write_execute_cb,
  2851. INT_TO_PTR(cmd->conn_id))) {
  2852. error("gatt: Could not send execute write");
  2853. status = HAL_STATUS_FAILED;
  2854. goto reply;
  2855. }
  2856. status = HAL_STATUS_SUCCESS;
  2857. reply:
  2858. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  2859. HAL_OP_GATT_CLIENT_EXECUTE_WRITE, status);
  2860. /* In case of early error send also notification.*/
  2861. if (status != HAL_STATUS_SUCCESS)
  2862. send_client_write_execute_notify(cmd->conn_id, GATT_FAILURE);
  2863. }
  2864. static void handle_notification(const uint8_t *pdu, uint16_t len,
  2865. gpointer user_data)
  2866. {
  2867. uint8_t buf[IPC_MTU];
  2868. struct hal_ev_gatt_client_notify *ev = (void *) buf;
  2869. struct notification_data *notification = user_data;
  2870. uint8_t data_offset = sizeof(uint8_t) + sizeof(uint16_t);
  2871. if (len < data_offset)
  2872. return;
  2873. memcpy(&ev->char_id, &notification->ch, sizeof(ev->char_id));
  2874. memcpy(&ev->srvc_id, &notification->service, sizeof(ev->srvc_id));
  2875. bdaddr2android(&notification->conn->device->bdaddr, &ev->bda);
  2876. ev->conn_id = notification->conn->id;
  2877. ev->is_notify = pdu[0] == ATT_OP_HANDLE_NOTIFY;
  2878. /* We have to cut opcode and handle from data */
  2879. ev->len = len - data_offset;
  2880. memcpy(ev->value, pdu + data_offset, len - data_offset);
  2881. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT, HAL_EV_GATT_CLIENT_NOTIFY,
  2882. sizeof(*ev) + ev->len, ev);
  2883. }
  2884. static void send_register_for_notification_ev(int32_t id, int32_t registered,
  2885. int32_t status,
  2886. const struct hal_gatt_srvc_id *srvc,
  2887. const struct hal_gatt_gatt_id *ch)
  2888. {
  2889. struct hal_ev_gatt_client_reg_for_notif ev;
  2890. ev.conn_id = id;
  2891. ev.status = status;
  2892. ev.registered = registered;
  2893. memcpy(&ev.srvc_id, srvc, sizeof(ev.srvc_id));
  2894. memcpy(&ev.char_id, ch, sizeof(ev.char_id));
  2895. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  2896. HAL_EV_GATT_CLIENT_REGISTER_FOR_NOTIF, sizeof(ev), &ev);
  2897. }
  2898. static void handle_client_register_for_notification(const void *buf,
  2899. uint16_t len)
  2900. {
  2901. const struct hal_cmd_gatt_client_register_for_notification *cmd = buf;
  2902. struct notification_data *notification;
  2903. struct characteristic *c;
  2904. struct element_id match_id;
  2905. struct app_connection *conn;
  2906. int32_t conn_id = 0;
  2907. struct service *service;
  2908. uint8_t status;
  2909. int32_t gatt_status;
  2910. bdaddr_t addr;
  2911. DBG("");
  2912. android2bdaddr(&cmd->bdaddr, &addr);
  2913. conn = find_conn(&addr, cmd->client_if);
  2914. if (!conn) {
  2915. status = HAL_STATUS_FAILED;
  2916. goto failed;
  2917. }
  2918. conn_id = conn->id;
  2919. hal_srvc_id_to_element_id(&cmd->srvc_id, &match_id);
  2920. service = queue_find(conn->device->services, match_srvc_by_element_id,
  2921. &match_id);
  2922. if (!service) {
  2923. status = HAL_STATUS_FAILED;
  2924. goto failed;
  2925. }
  2926. hal_gatt_id_to_element_id(&cmd->char_id, &match_id);
  2927. c = queue_find(service->chars, match_char_by_element_id, &match_id);
  2928. if (!c) {
  2929. status = HAL_STATUS_FAILED;
  2930. goto failed;
  2931. }
  2932. notification = new0(struct notification_data, 1);
  2933. memcpy(&notification->ch, &cmd->char_id, sizeof(notification->ch));
  2934. memcpy(&notification->service, &cmd->srvc_id,
  2935. sizeof(notification->service));
  2936. notification->conn = conn;
  2937. if (queue_find(conn->app->notifications, match_notification,
  2938. notification)) {
  2939. free(notification);
  2940. status = HAL_STATUS_SUCCESS;
  2941. goto failed;
  2942. }
  2943. notification->notif_id = g_attrib_register(conn->device->attrib,
  2944. ATT_OP_HANDLE_NOTIFY,
  2945. c->ch.value_handle,
  2946. handle_notification,
  2947. notification,
  2948. destroy_notification);
  2949. if (!notification->notif_id) {
  2950. free(notification);
  2951. status = HAL_STATUS_FAILED;
  2952. goto failed;
  2953. }
  2954. notification->ind_id = g_attrib_register(conn->device->attrib,
  2955. ATT_OP_HANDLE_IND,
  2956. c->ch.value_handle,
  2957. handle_notification,
  2958. notification,
  2959. destroy_notification);
  2960. if (!notification->ind_id) {
  2961. g_attrib_unregister(conn->device->attrib,
  2962. notification->notif_id);
  2963. free(notification);
  2964. status = HAL_STATUS_FAILED;
  2965. goto failed;
  2966. }
  2967. /*
  2968. * Because same data - notification - is shared by two handlers, we
  2969. * introduce ref counter to be sure that data can be freed with no risk.
  2970. * Counter is decremented in destroy_notification.
  2971. */
  2972. notification->ref = 2;
  2973. queue_push_tail(conn->app->notifications, notification);
  2974. status = HAL_STATUS_SUCCESS;
  2975. failed:
  2976. gatt_status = status ? GATT_FAILURE : GATT_SUCCESS;
  2977. send_register_for_notification_ev(conn_id, 1, gatt_status,
  2978. &cmd->srvc_id, &cmd->char_id);
  2979. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  2980. HAL_OP_GATT_CLIENT_REGISTER_FOR_NOTIFICATION, status);
  2981. }
  2982. static void handle_client_deregister_for_notification(const void *buf,
  2983. uint16_t len)
  2984. {
  2985. const struct hal_cmd_gatt_client_deregister_for_notification *cmd = buf;
  2986. struct notification_data *notification, notif;
  2987. struct app_connection *conn;
  2988. int32_t conn_id = 0;
  2989. uint8_t status;
  2990. int32_t gatt_status;
  2991. bdaddr_t addr;
  2992. DBG("");
  2993. android2bdaddr(&cmd->bdaddr, &addr);
  2994. conn = find_conn(&addr, cmd->client_if);
  2995. if (!conn) {
  2996. status = HAL_STATUS_FAILED;
  2997. goto failed;
  2998. }
  2999. conn_id = conn->id;
  3000. memcpy(&notif.ch, &cmd->char_id, sizeof(notif.ch));
  3001. memcpy(&notif.service, &cmd->srvc_id, sizeof(notif.service));
  3002. notif.conn = conn;
  3003. notification = queue_find(conn->app->notifications,
  3004. match_notification, &notif);
  3005. if (!notification) {
  3006. status = HAL_STATUS_FAILED;
  3007. goto failed;
  3008. }
  3009. unregister_notification(notification);
  3010. status = HAL_STATUS_SUCCESS;
  3011. failed:
  3012. gatt_status = status ? GATT_FAILURE : GATT_SUCCESS;
  3013. send_register_for_notification_ev(conn_id, 0, gatt_status,
  3014. &cmd->srvc_id, &cmd->char_id);
  3015. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  3016. HAL_OP_GATT_CLIENT_DEREGISTER_FOR_NOTIFICATION, status);
  3017. }
  3018. static void send_client_remote_rssi_notify(int32_t client_if,
  3019. const bdaddr_t *addr,
  3020. int32_t rssi, int32_t status)
  3021. {
  3022. struct hal_ev_gatt_client_read_remote_rssi ev;
  3023. ev.client_if = client_if;
  3024. bdaddr2android(addr, &ev.address);
  3025. ev.rssi = rssi;
  3026. ev.status = status;
  3027. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  3028. HAL_EV_GATT_CLIENT_READ_REMOTE_RSSI, sizeof(ev), &ev);
  3029. }
  3030. static void read_remote_rssi_cb(uint8_t status, const bdaddr_t *addr,
  3031. int8_t rssi, void *user_data)
  3032. {
  3033. int32_t client_if = PTR_TO_INT(user_data);
  3034. int32_t gatt_status = status ? GATT_FAILURE : GATT_SUCCESS;
  3035. send_client_remote_rssi_notify(client_if, addr, rssi, gatt_status);
  3036. }
  3037. static void handle_client_read_remote_rssi(const void *buf, uint16_t len)
  3038. {
  3039. const struct hal_cmd_gatt_client_read_remote_rssi *cmd = buf;
  3040. uint8_t status;
  3041. bdaddr_t bdaddr;
  3042. DBG("");
  3043. if (!find_app_by_id(cmd->client_if)) {
  3044. status = HAL_STATUS_FAILED;
  3045. goto failed;
  3046. }
  3047. android2bdaddr(cmd->bdaddr, &bdaddr);
  3048. if (!bt_read_device_rssi(&bdaddr, read_remote_rssi_cb,
  3049. INT_TO_PTR(cmd->client_if))) {
  3050. error("gatt: Could not read RSSI");
  3051. status = HAL_STATUS_FAILED;
  3052. goto failed;
  3053. }
  3054. status = HAL_STATUS_SUCCESS;
  3055. failed:
  3056. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  3057. HAL_OP_GATT_CLIENT_READ_REMOTE_RSSI, status);
  3058. if (status != HAL_STATUS_SUCCESS)
  3059. send_client_remote_rssi_notify(cmd->client_if, &bdaddr, 0,
  3060. GATT_FAILURE);
  3061. }
  3062. static void handle_client_get_device_type(const void *buf, uint16_t len)
  3063. {
  3064. const struct hal_cmd_gatt_client_get_device_type *cmd = buf;
  3065. struct hal_rsp_gatt_client_get_device_type rsp;
  3066. bdaddr_t bdaddr;
  3067. DBG("");
  3068. android2bdaddr(cmd->bdaddr, &bdaddr);
  3069. rsp.type = bt_get_device_android_type(&bdaddr);
  3070. ipc_send_rsp_full(hal_ipc, HAL_SERVICE_ID_GATT,
  3071. HAL_OP_GATT_CLIENT_GET_DEVICE_TYPE,
  3072. sizeof(rsp), &rsp, -1);
  3073. }
  3074. static void handle_client_set_adv_data(const void *buf, uint16_t len)
  3075. {
  3076. const struct hal_cmd_gatt_client_set_adv_data *cmd = buf;
  3077. uint8_t status;
  3078. if (len != sizeof(*cmd) + cmd->manufacturer_len) {
  3079. error("Invalid set adv data command (%u bytes), terminating",
  3080. len);
  3081. raise(SIGTERM);
  3082. return;
  3083. }
  3084. DBG("scan_rsp=%u name=%u tx=%u min=%d max=%d app=%d",
  3085. cmd->set_scan_rsp, cmd->include_name, cmd->include_txpower,
  3086. cmd->min_interval, cmd->max_interval, cmd->appearance);
  3087. DBG("manufacturer=%u service_data=%u service_uuid=%u",
  3088. cmd->manufacturer_len, cmd->service_data_len,
  3089. cmd->service_uuid_len);
  3090. /* TODO This should be implemented when kernel supports it */
  3091. if (cmd->manufacturer_len || cmd->service_data_len ||
  3092. cmd->service_uuid_len) {
  3093. error("gatt: Extra advertising data not supported");
  3094. status = HAL_STATUS_FAILED;
  3095. goto failed;
  3096. }
  3097. status = HAL_STATUS_SUCCESS;
  3098. failed:
  3099. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  3100. HAL_OP_GATT_CLIENT_SET_ADV_DATA, status);
  3101. }
  3102. static void test_command_result(guint8 status, const guint8 *pdu,
  3103. guint16 len, gpointer user_data)
  3104. {
  3105. DBG("status: %d", status);
  3106. }
  3107. static uint8_t test_read_write(bdaddr_t *bdaddr, bt_uuid_t *uuid, uint16_t op,
  3108. uint16_t u2, uint16_t u3,
  3109. uint16_t u4, uint16_t u5)
  3110. {
  3111. guint16 length = 0;
  3112. struct gatt_device *dev;
  3113. uint8_t *pdu;
  3114. size_t mtu;
  3115. dev = find_device_by_addr(bdaddr);
  3116. if (!dev || dev->state != DEVICE_CONNECTED)
  3117. return HAL_STATUS_FAILED;
  3118. pdu = g_attrib_get_buffer(dev->attrib, &mtu);
  3119. if (!pdu)
  3120. return HAL_STATUS_FAILED;
  3121. switch (op) {
  3122. case ATT_OP_READ_REQ:
  3123. length = enc_read_req(u2, pdu, mtu);
  3124. break;
  3125. case ATT_OP_READ_BY_TYPE_REQ:
  3126. length = enc_read_by_type_req(u2, u3, uuid, pdu, mtu);
  3127. break;
  3128. case ATT_OP_READ_BLOB_REQ:
  3129. length = enc_read_blob_req(u2, u3, pdu, mtu);
  3130. break;
  3131. case ATT_OP_READ_BY_GROUP_REQ:
  3132. length = enc_read_by_grp_req(u2, u3, uuid, pdu, mtu);
  3133. break;
  3134. case ATT_OP_READ_MULTI_REQ:
  3135. return HAL_STATUS_UNSUPPORTED;
  3136. case ATT_OP_WRITE_REQ:
  3137. length = enc_write_req(u2, (uint8_t *) &u3, sizeof(u3), pdu,
  3138. mtu);
  3139. break;
  3140. case ATT_OP_WRITE_CMD:
  3141. length = enc_write_cmd(u2, (uint8_t *) &u3, sizeof(u3), pdu,
  3142. mtu);
  3143. break;
  3144. case ATT_OP_PREP_WRITE_REQ:
  3145. length = enc_prep_write_req(u2, u3, (uint8_t *) &u4, sizeof(u4),
  3146. pdu, mtu);
  3147. break;
  3148. case ATT_OP_EXEC_WRITE_REQ:
  3149. length = enc_exec_write_req(u2, pdu, mtu);
  3150. break;
  3151. case ATT_OP_SIGNED_WRITE_CMD:
  3152. if (signed_write_cmd(dev, u2, (uint8_t *) &u3, sizeof(u3)))
  3153. return HAL_STATUS_SUCCESS;
  3154. else
  3155. return HAL_STATUS_FAILED;
  3156. default:
  3157. error("gatt: Unknown operation type");
  3158. return HAL_STATUS_UNSUPPORTED;
  3159. }
  3160. if (!g_attrib_send(dev->attrib, 0, pdu, length, test_command_result,
  3161. NULL, NULL))
  3162. return HAL_STATUS_FAILED;
  3163. return HAL_STATUS_SUCCESS;
  3164. }
  3165. static uint8_t test_increase_security(bdaddr_t *bdaddr, uint16_t u1)
  3166. {
  3167. struct gatt_device *device;
  3168. device = find_device_by_addr(bdaddr);
  3169. if (!device)
  3170. return HAL_STATUS_FAILED;
  3171. if (!set_auth_type(device, u1))
  3172. return HAL_STATUS_FAILED;
  3173. return HAL_STATUS_SUCCESS;
  3174. }
  3175. static void handle_client_test_command(const void *buf, uint16_t len)
  3176. {
  3177. const struct hal_cmd_gatt_client_test_command *cmd = buf;
  3178. struct gatt_app *app;
  3179. bdaddr_t bdaddr;
  3180. bt_uuid_t uuid;
  3181. uint8_t status;
  3182. DBG("");
  3183. android2bdaddr(cmd->bda1, &bdaddr);
  3184. android2uuid(cmd->uuid1, &uuid);
  3185. switch (cmd->command) {
  3186. case GATT_CLIENT_TEST_CMD_ENABLE:
  3187. if (cmd->u1) {
  3188. if (!test_client_if) {
  3189. app = register_app(TEST_UUID, GATT_CLIENT);
  3190. if (app)
  3191. test_client_if = app->id;
  3192. }
  3193. if (test_client_if)
  3194. status = HAL_STATUS_SUCCESS;
  3195. else
  3196. status = HAL_STATUS_FAILED;
  3197. } else {
  3198. status = unregister_app(test_client_if);
  3199. test_client_if = 0;
  3200. }
  3201. break;
  3202. case GATT_CLIENT_TEST_CMD_CONNECT:
  3203. /* TODO u1 holds device type, for now assume BLE */
  3204. status = handle_connect(test_client_if, &bdaddr, false);
  3205. break;
  3206. case GATT_CLIENT_TEST_CMD_DISCONNECT:
  3207. app = queue_find(gatt_apps, match_app_by_id,
  3208. INT_TO_PTR(test_client_if));
  3209. queue_remove_all(app_connections, match_connection_by_app, app,
  3210. destroy_connection);
  3211. status = HAL_STATUS_SUCCESS;
  3212. break;
  3213. case GATT_CLIENT_TEST_CMD_DISCOVER:
  3214. status = HAL_STATUS_FAILED;
  3215. break;
  3216. case GATT_CLIENT_TEST_CMD_READ:
  3217. case GATT_CLIENT_TEST_CMD_WRITE:
  3218. status = test_read_write(&bdaddr, &uuid, cmd->u1, cmd->u2,
  3219. cmd->u3, cmd->u4, cmd->u5);
  3220. break;
  3221. case GATT_CLIENT_TEST_CMD_INCREASE_SECURITY:
  3222. status = test_increase_security(&bdaddr, cmd->u1);
  3223. break;
  3224. case GATT_CLIENT_TEST_CMD_PAIRING_CONFIG:
  3225. default:
  3226. status = HAL_STATUS_FAILED;
  3227. break;
  3228. }
  3229. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  3230. HAL_OP_GATT_CLIENT_TEST_COMMAND, status);
  3231. }
  3232. static void handle_server_register(const void *buf, uint16_t len)
  3233. {
  3234. const struct hal_cmd_gatt_server_register *cmd = buf;
  3235. struct hal_ev_gatt_server_register ev;
  3236. struct gatt_app *app;
  3237. DBG("");
  3238. memset(&ev, 0, sizeof(ev));
  3239. app = register_app(cmd->uuid, GATT_SERVER);
  3240. if (app) {
  3241. ev.server_if = app->id;
  3242. ev.status = GATT_SUCCESS;
  3243. } else {
  3244. ev.status = GATT_FAILURE;
  3245. }
  3246. memcpy(ev.uuid, cmd->uuid, sizeof(ev.uuid));
  3247. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  3248. HAL_EV_GATT_SERVER_REGISTER, sizeof(ev), &ev);
  3249. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT, HAL_OP_GATT_SERVER_REGISTER,
  3250. HAL_STATUS_SUCCESS);
  3251. }
  3252. static void handle_server_unregister(const void *buf, uint16_t len)
  3253. {
  3254. const struct hal_cmd_gatt_server_unregister *cmd = buf;
  3255. uint8_t status;
  3256. DBG("");
  3257. status = unregister_app(cmd->server_if);
  3258. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  3259. HAL_OP_GATT_SERVER_UNREGISTER, status);
  3260. }
  3261. static void handle_server_connect(const void *buf, uint16_t len)
  3262. {
  3263. const struct hal_cmd_gatt_server_connect *cmd = buf;
  3264. uint8_t status;
  3265. bdaddr_t addr;
  3266. DBG("");
  3267. android2bdaddr(&cmd->bdaddr, &addr);
  3268. /* TODO: Handle transport flag */
  3269. status = handle_connect(cmd->server_if, &addr, cmd->is_direct);
  3270. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT, HAL_OP_GATT_SERVER_CONNECT,
  3271. status);
  3272. }
  3273. static void handle_server_disconnect(const void *buf, uint16_t len)
  3274. {
  3275. const struct hal_cmd_gatt_server_disconnect *cmd = buf;
  3276. struct app_connection *conn;
  3277. uint8_t status;
  3278. DBG("");
  3279. /* TODO: should we care to match also bdaddr when conn_id is unique? */
  3280. conn = queue_remove_if(app_connections, match_connection_by_id,
  3281. INT_TO_PTR(cmd->conn_id));
  3282. destroy_connection(conn);
  3283. status = HAL_STATUS_SUCCESS;
  3284. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  3285. HAL_OP_GATT_SERVER_DISCONNECT, status);
  3286. }
  3287. static void handle_server_add_service(const void *buf, uint16_t len)
  3288. {
  3289. const struct hal_cmd_gatt_server_add_service *cmd = buf;
  3290. struct hal_ev_gatt_server_service_added ev;
  3291. struct gatt_app *server;
  3292. struct gatt_db_attribute *service;
  3293. uint8_t status;
  3294. bt_uuid_t uuid;
  3295. DBG("");
  3296. memset(&ev, 0, sizeof(ev));
  3297. server = find_app_by_id(cmd->server_if);
  3298. if (!server) {
  3299. status = HAL_STATUS_FAILED;
  3300. goto failed;
  3301. }
  3302. android2uuid(cmd->srvc_id.uuid, &uuid);
  3303. service = gatt_db_add_service(gatt_db, &uuid, cmd->srvc_id.is_primary,
  3304. cmd->num_handles);
  3305. if (!service) {
  3306. status = HAL_STATUS_FAILED;
  3307. goto failed;
  3308. }
  3309. ev.srvc_handle = gatt_db_attribute_get_handle(service);
  3310. if (!ev.srvc_handle) {
  3311. status = HAL_STATUS_FAILED;
  3312. goto failed;
  3313. }
  3314. status = HAL_STATUS_SUCCESS;
  3315. failed:
  3316. ev.status = status == HAL_STATUS_SUCCESS ? GATT_SUCCESS : GATT_FAILURE;
  3317. ev.srvc_id = cmd->srvc_id;
  3318. ev.server_if = cmd->server_if;
  3319. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  3320. HAL_EV_GATT_SERVER_SERVICE_ADDED, sizeof(ev), &ev);
  3321. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  3322. HAL_OP_GATT_SERVER_ADD_SERVICE, status);
  3323. }
  3324. static void handle_server_add_included_service(const void *buf, uint16_t len)
  3325. {
  3326. const struct hal_cmd_gatt_server_add_inc_service *cmd = buf;
  3327. struct hal_ev_gatt_server_inc_srvc_added ev;
  3328. struct gatt_app *server;
  3329. struct gatt_db_attribute *service, *include;
  3330. uint8_t status;
  3331. DBG("");
  3332. memset(&ev, 0, sizeof(ev));
  3333. server = find_app_by_id(cmd->server_if);
  3334. if (!server) {
  3335. status = HAL_STATUS_FAILED;
  3336. goto failed;
  3337. }
  3338. service = gatt_db_get_attribute(gatt_db, cmd->service_handle);
  3339. if (!service) {
  3340. status = HAL_STATUS_FAILED;
  3341. goto failed;
  3342. }
  3343. include = gatt_db_get_attribute(gatt_db, cmd->included_handle);
  3344. if (!include) {
  3345. status = HAL_STATUS_FAILED;
  3346. goto failed;
  3347. }
  3348. service = gatt_db_service_add_included(service, include);
  3349. if (!service) {
  3350. status = HAL_STATUS_FAILED;
  3351. goto failed;
  3352. }
  3353. ev.incl_srvc_handle = gatt_db_attribute_get_handle(service);
  3354. status = HAL_STATUS_SUCCESS;
  3355. failed:
  3356. ev.srvc_handle = cmd->service_handle;
  3357. ev.status = status;
  3358. ev.server_if = cmd->server_if;
  3359. ev.status = status == HAL_STATUS_SUCCESS ? GATT_SUCCESS : GATT_FAILURE;
  3360. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  3361. HAL_EV_GATT_SERVER_INC_SRVC_ADDED, sizeof(ev), &ev);
  3362. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  3363. HAL_OP_GATT_SERVER_ADD_INC_SERVICE, status);
  3364. }
  3365. static bool is_service(const bt_uuid_t *type)
  3366. {
  3367. bt_uuid_t uuid;
  3368. bt_uuid16_create(&uuid, GATT_PRIM_SVC_UUID);
  3369. if (!bt_uuid_cmp(&uuid, type))
  3370. return true;
  3371. bt_uuid16_create(&uuid, GATT_SND_SVC_UUID);
  3372. if (!bt_uuid_cmp(&uuid, type))
  3373. return true;
  3374. return false;
  3375. }
  3376. static bool match_pending_dev_request(const void *data, const void *user_data)
  3377. {
  3378. const struct pending_request *pending_request = data;
  3379. return !pending_request->completed;
  3380. }
  3381. static void send_dev_complete_response(struct gatt_device *device,
  3382. uint8_t opcode)
  3383. {
  3384. size_t mtu;
  3385. uint8_t *rsp = g_attrib_get_buffer(device->attrib, &mtu);
  3386. struct pending_request *val;
  3387. uint16_t len = 0;
  3388. uint8_t error = 0;
  3389. if (queue_isempty(device->pending_requests))
  3390. return;
  3391. if (queue_find(device->pending_requests, match_pending_dev_request,
  3392. NULL)) {
  3393. DBG("Still pending requests");
  3394. return;
  3395. }
  3396. val = queue_peek_head(device->pending_requests);
  3397. if (!val) {
  3398. error = ATT_ECODE_ATTR_NOT_FOUND;
  3399. goto done;
  3400. }
  3401. if (val->error) {
  3402. error = val->error;
  3403. goto done;
  3404. }
  3405. switch (opcode) {
  3406. case ATT_OP_READ_BY_TYPE_REQ: {
  3407. struct att_data_list *adl;
  3408. int iterator = 0;
  3409. int length;
  3410. struct queue *temp;
  3411. temp = queue_new();
  3412. val = queue_pop_head(device->pending_requests);
  3413. if (!val) {
  3414. queue_destroy(temp, NULL);
  3415. error = ATT_ECODE_ATTR_NOT_FOUND;
  3416. goto done;
  3417. }
  3418. if (val->error) {
  3419. queue_destroy(temp, NULL);
  3420. error = val->error;
  3421. destroy_pending_request(val);
  3422. goto done;
  3423. }
  3424. length = val->length;
  3425. while (val && val->length == length && val->error == 0) {
  3426. queue_push_tail(temp, val);
  3427. val = queue_pop_head(device->pending_requests);
  3428. }
  3429. adl = att_data_list_alloc(queue_length(temp),
  3430. sizeof(uint16_t) + length);
  3431. destroy_pending_request(val);
  3432. val = queue_pop_head(temp);
  3433. while (val) {
  3434. uint8_t *value = adl->data[iterator++];
  3435. uint16_t handle;
  3436. handle = gatt_db_attribute_get_handle(val->attrib);
  3437. put_le16(handle, value);
  3438. memcpy(&value[2], val->value, val->length);
  3439. destroy_pending_request(val);
  3440. val = queue_pop_head(temp);
  3441. }
  3442. len = enc_read_by_type_resp(adl, rsp, mtu);
  3443. att_data_list_free(adl);
  3444. queue_destroy(temp, destroy_pending_request);
  3445. break;
  3446. }
  3447. case ATT_OP_READ_BLOB_REQ:
  3448. len = enc_read_blob_resp(val->value, val->length, val->offset,
  3449. rsp, mtu);
  3450. break;
  3451. case ATT_OP_READ_REQ:
  3452. len = enc_read_resp(val->value, val->length, rsp, mtu);
  3453. break;
  3454. case ATT_OP_READ_BY_GROUP_REQ: {
  3455. struct att_data_list *adl;
  3456. int iterator = 0;
  3457. int length;
  3458. struct queue *temp;
  3459. temp = queue_new();
  3460. val = queue_pop_head(device->pending_requests);
  3461. if (!val) {
  3462. queue_destroy(temp, NULL);
  3463. error = ATT_ECODE_ATTR_NOT_FOUND;
  3464. goto done;
  3465. }
  3466. length = val->length;
  3467. while (val && val->length == length) {
  3468. queue_push_tail(temp, val);
  3469. val = queue_pop_head(device->pending_requests);
  3470. }
  3471. adl = att_data_list_alloc(queue_length(temp),
  3472. 2 * sizeof(uint16_t) + length);
  3473. val = queue_pop_head(temp);
  3474. while (val) {
  3475. uint8_t *value = adl->data[iterator++];
  3476. uint16_t start_handle, end_handle;
  3477. gatt_db_attribute_get_service_handles(val->attrib,
  3478. &start_handle,
  3479. &end_handle);
  3480. put_le16(start_handle, value);
  3481. put_le16(end_handle, &value[2]);
  3482. memcpy(&value[4], val->value, val->length);
  3483. destroy_pending_request(val);
  3484. val = queue_pop_head(temp);
  3485. }
  3486. len = enc_read_by_grp_resp(adl, rsp, mtu);
  3487. att_data_list_free(adl);
  3488. queue_destroy(temp, destroy_pending_request);
  3489. break;
  3490. }
  3491. case ATT_OP_FIND_BY_TYPE_REQ: {
  3492. GSList *list = NULL;
  3493. val = queue_pop_head(device->pending_requests);
  3494. while (val) {
  3495. struct att_range *range;
  3496. const bt_uuid_t *type;
  3497. /* Its find by type and value - filter by value here */
  3498. if ((val->length != val->filter_vlen) ||
  3499. memcmp(val->value, val->filter_value,
  3500. val->length)) {
  3501. destroy_pending_request(val);
  3502. val = queue_pop_head(device->pending_requests);
  3503. continue;
  3504. }
  3505. range = new0(struct att_range, 1);
  3506. range->start = gatt_db_attribute_get_handle(
  3507. val->attrib);
  3508. type = gatt_db_attribute_get_type(val->attrib);
  3509. if (is_service(type))
  3510. gatt_db_attribute_get_service_handles(
  3511. val->attrib,
  3512. NULL,
  3513. &range->end);
  3514. else
  3515. range->end = range->start;
  3516. list = g_slist_append(list, range);
  3517. destroy_pending_request(val);
  3518. val = queue_pop_head(device->pending_requests);
  3519. }
  3520. if (list && !error)
  3521. len = enc_find_by_type_resp(list, rsp, mtu);
  3522. else
  3523. error = ATT_ECODE_ATTR_NOT_FOUND;
  3524. g_slist_free_full(list, free);
  3525. break;
  3526. }
  3527. case ATT_OP_EXEC_WRITE_REQ:
  3528. len = enc_exec_write_resp(rsp);
  3529. break;
  3530. case ATT_OP_WRITE_REQ:
  3531. len = enc_write_resp(rsp);
  3532. break;
  3533. case ATT_OP_PREP_WRITE_REQ: {
  3534. uint16_t handle;
  3535. handle = gatt_db_attribute_get_handle(val->attrib);
  3536. len = enc_prep_write_resp(handle, val->offset, val->value,
  3537. val->length, rsp, mtu);
  3538. break;
  3539. }
  3540. default:
  3541. break;
  3542. }
  3543. done:
  3544. if (!len)
  3545. len = enc_error_resp(opcode, 0x0000, error, rsp, mtu);
  3546. g_attrib_send(device->attrib, 0, rsp, len, NULL, NULL, NULL);
  3547. queue_remove_all(device->pending_requests, NULL, NULL,
  3548. destroy_pending_request);
  3549. }
  3550. struct request_processing_data {
  3551. uint8_t opcode;
  3552. struct gatt_device *device;
  3553. };
  3554. static uint8_t check_device_permissions(struct gatt_device *device,
  3555. uint8_t opcode, uint32_t permissions)
  3556. {
  3557. GIOChannel *io;
  3558. int sec_level;
  3559. io = g_attrib_get_channel(device->attrib);
  3560. if (!bt_io_get(io, NULL, BT_IO_OPT_SEC_LEVEL, &sec_level,
  3561. BT_IO_OPT_INVALID))
  3562. return ATT_ECODE_UNLIKELY;
  3563. DBG("opcode 0x%02x permissions %u sec_level %u", opcode, permissions,
  3564. sec_level);
  3565. switch (opcode) {
  3566. case ATT_OP_SIGNED_WRITE_CMD:
  3567. if (!(permissions & GATT_PERM_WRITE_SIGNED))
  3568. return ATT_ECODE_WRITE_NOT_PERM;
  3569. if (permissions & GATT_PERM_WRITE_SIGNED_MITM) {
  3570. bool auth;
  3571. if (bt_get_csrk(&device->bdaddr, true, NULL, NULL,
  3572. &auth) && auth)
  3573. break;
  3574. return ATT_ECODE_AUTHENTICATION;
  3575. }
  3576. break;
  3577. case ATT_OP_READ_BY_TYPE_REQ:
  3578. case ATT_OP_READ_REQ:
  3579. case ATT_OP_READ_BLOB_REQ:
  3580. case ATT_OP_READ_MULTI_REQ:
  3581. case ATT_OP_READ_BY_GROUP_REQ:
  3582. case ATT_OP_FIND_BY_TYPE_REQ:
  3583. case ATT_OP_FIND_INFO_REQ:
  3584. if (!(permissions & GATT_PERM_READ))
  3585. return ATT_ECODE_READ_NOT_PERM;
  3586. if ((permissions & GATT_PERM_READ_MITM) &&
  3587. sec_level < BT_SECURITY_HIGH)
  3588. return ATT_ECODE_AUTHENTICATION;
  3589. if ((permissions & GATT_PERM_READ_ENCRYPTED) &&
  3590. sec_level < BT_SECURITY_MEDIUM)
  3591. return ATT_ECODE_INSUFF_ENC;
  3592. if (permissions & GATT_PERM_READ_AUTHORIZATION)
  3593. return ATT_ECODE_AUTHORIZATION;
  3594. break;
  3595. case ATT_OP_WRITE_REQ:
  3596. case ATT_OP_WRITE_CMD:
  3597. case ATT_OP_PREP_WRITE_REQ:
  3598. case ATT_OP_EXEC_WRITE_REQ:
  3599. if (!(permissions & GATT_PERM_WRITE))
  3600. return ATT_ECODE_WRITE_NOT_PERM;
  3601. if ((permissions & GATT_PERM_WRITE_MITM) &&
  3602. sec_level < BT_SECURITY_HIGH)
  3603. return ATT_ECODE_AUTHENTICATION;
  3604. if ((permissions & GATT_PERM_WRITE_ENCRYPTED) &&
  3605. sec_level < BT_SECURITY_MEDIUM)
  3606. return ATT_ECODE_INSUFF_ENC;
  3607. if (permissions & GATT_PERM_WRITE_AUTHORIZATION)
  3608. return ATT_ECODE_AUTHORIZATION;
  3609. break;
  3610. default:
  3611. return ATT_ECODE_UNLIKELY;
  3612. }
  3613. return 0;
  3614. }
  3615. static uint8_t err_to_att(int err)
  3616. {
  3617. if (!err || (err > 0 && err < UINT8_MAX))
  3618. return err;
  3619. switch (err) {
  3620. case -ENOENT:
  3621. return ATT_ECODE_INVALID_HANDLE;
  3622. case -ENOMEM:
  3623. return ATT_ECODE_INSUFF_RESOURCES;
  3624. default:
  3625. return ATT_ECODE_UNLIKELY;
  3626. }
  3627. }
  3628. static void attribute_read_cb(struct gatt_db_attribute *attrib, int err,
  3629. const uint8_t *value, size_t length,
  3630. void *user_data)
  3631. {
  3632. struct pending_request *resp_data = user_data;
  3633. uint8_t error = err_to_att(err);
  3634. resp_data->attrib = attrib;
  3635. resp_data->length = length;
  3636. resp_data->error = error;
  3637. resp_data->completed = true;
  3638. if (!length)
  3639. return;
  3640. resp_data->value = malloc0(length);
  3641. if (!resp_data->value) {
  3642. resp_data->error = ATT_ECODE_INSUFF_RESOURCES;
  3643. return;
  3644. }
  3645. memcpy(resp_data->value, value, length);
  3646. }
  3647. static void read_requested_attributes(void *data, void *user_data)
  3648. {
  3649. struct pending_request *resp_data = data;
  3650. struct request_processing_data *process_data = user_data;
  3651. struct bt_att *att = g_attrib_get_att(process_data->device->attrib);
  3652. struct gatt_db_attribute *attrib;
  3653. uint32_t permissions;
  3654. uint8_t error;
  3655. attrib = resp_data->attrib;
  3656. if (!attrib) {
  3657. resp_data->error = ATT_ECODE_ATTR_NOT_FOUND;
  3658. resp_data->completed = true;
  3659. return;
  3660. }
  3661. permissions = gatt_db_attribute_get_permissions(attrib);
  3662. /*
  3663. * Check if it is attribute we didn't declare permissions, like service
  3664. * declaration or included service. Set permissions to read only
  3665. */
  3666. if (permissions == 0)
  3667. permissions = GATT_PERM_READ;
  3668. error = check_device_permissions(process_data->device,
  3669. process_data->opcode,
  3670. permissions);
  3671. if (error != 0) {
  3672. resp_data->error = error;
  3673. resp_data->completed = true;
  3674. return;
  3675. }
  3676. gatt_db_attribute_read(attrib, resp_data->offset, process_data->opcode,
  3677. att, attribute_read_cb, resp_data);
  3678. }
  3679. static void process_dev_pending_requests(struct gatt_device *device,
  3680. uint8_t att_opcode)
  3681. {
  3682. struct request_processing_data process_data;
  3683. if (queue_isempty(device->pending_requests))
  3684. return;
  3685. process_data.device = device;
  3686. process_data.opcode = att_opcode;
  3687. /* Process pending requests and prepare response */
  3688. queue_foreach(device->pending_requests, read_requested_attributes,
  3689. &process_data);
  3690. send_dev_complete_response(device, att_opcode);
  3691. }
  3692. static struct pending_trans_data *conn_add_transact(struct app_connection *conn,
  3693. uint8_t opcode,
  3694. struct gatt_db_attribute *attrib,
  3695. unsigned int serial_id)
  3696. {
  3697. struct pending_trans_data *transaction;
  3698. static int32_t trans_id = 1;
  3699. transaction = new0(struct pending_trans_data, 1);
  3700. transaction->id = trans_id++;
  3701. transaction->opcode = opcode;
  3702. transaction->attrib = attrib;
  3703. transaction->serial_id = serial_id;
  3704. queue_push_tail(conn->transactions, transaction);
  3705. return transaction;
  3706. }
  3707. static bool get_dst_addr(struct bt_att *att, bdaddr_t *dst)
  3708. {
  3709. GIOChannel *io = NULL;
  3710. GError *gerr = NULL;
  3711. io = g_io_channel_unix_new(bt_att_get_fd(att));
  3712. if (!io)
  3713. return false;
  3714. bt_io_get(io, &gerr, BT_IO_OPT_DEST_BDADDR, dst, BT_IO_OPT_INVALID);
  3715. if (gerr) {
  3716. error("gatt: bt_io_get: %s", gerr->message);
  3717. g_error_free(gerr);
  3718. g_io_channel_unref(io);
  3719. return false;
  3720. }
  3721. g_io_channel_unref(io);
  3722. return true;
  3723. }
  3724. static void read_cb(struct gatt_db_attribute *attrib, unsigned int id,
  3725. uint16_t offset, uint8_t opcode, struct bt_att *att,
  3726. void *user_data)
  3727. {
  3728. struct pending_trans_data *transaction;
  3729. struct hal_ev_gatt_server_request_read ev;
  3730. struct gatt_app *app;
  3731. struct app_connection *conn;
  3732. int32_t app_id = PTR_TO_INT(user_data);
  3733. bdaddr_t bdaddr;
  3734. DBG("id %u", id);
  3735. app = find_app_by_id(app_id);
  3736. if (!app) {
  3737. error("gatt: read_cb, cound not found app id");
  3738. goto failed;
  3739. }
  3740. if (!get_dst_addr(att, &bdaddr)) {
  3741. error("gatt: read_cb, could not obtain dst BDADDR");
  3742. goto failed;
  3743. }
  3744. conn = find_conn(&bdaddr, app->id);
  3745. if (!conn) {
  3746. error("gatt: read_cb, cound not found connection");
  3747. goto failed;
  3748. }
  3749. memset(&ev, 0, sizeof(ev));
  3750. /* Store the request data, complete callback and transaction id */
  3751. transaction = conn_add_transact(conn, opcode, attrib, id);
  3752. bdaddr2android(&bdaddr, ev.bdaddr);
  3753. ev.conn_id = conn->id;
  3754. ev.attr_handle = gatt_db_attribute_get_handle(attrib);
  3755. ev.offset = offset;
  3756. ev.is_long = opcode == ATT_OP_READ_BLOB_REQ;
  3757. ev.trans_id = transaction->id;
  3758. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  3759. HAL_EV_GATT_SERVER_REQUEST_READ,
  3760. sizeof(ev), &ev);
  3761. return;
  3762. failed:
  3763. gatt_db_attribute_read_result(attrib, id, -ENOENT, NULL, 0);
  3764. }
  3765. static void write_cb(struct gatt_db_attribute *attrib, unsigned int id,
  3766. uint16_t offset, const uint8_t *value, size_t len,
  3767. uint8_t opcode, struct bt_att *att, void *user_data)
  3768. {
  3769. uint8_t buf[IPC_MTU];
  3770. struct hal_ev_gatt_server_request_write *ev = (void *) buf;
  3771. struct pending_trans_data *transaction;
  3772. struct gatt_app *app;
  3773. int32_t app_id = PTR_TO_INT(user_data);
  3774. struct app_connection *conn;
  3775. bdaddr_t bdaddr;
  3776. DBG("id %u", id);
  3777. app = find_app_by_id(app_id);
  3778. if (!app) {
  3779. error("gatt: write_cb could not found app id");
  3780. goto failed;
  3781. }
  3782. if (!get_dst_addr(att, &bdaddr)) {
  3783. error("gatt: write_cb, could not obtain dst BDADDR");
  3784. goto failed;
  3785. }
  3786. conn = find_conn(&bdaddr, app->id);
  3787. if (!conn) {
  3788. error("gatt: write_cb could not found connection");
  3789. goto failed;
  3790. }
  3791. /*
  3792. * Remember that this application has ongoing prep write
  3793. * Need it later to find out where to send execute write
  3794. */
  3795. if (opcode == ATT_OP_PREP_WRITE_REQ)
  3796. conn->wait_execute_write = true;
  3797. /* Store the request data, complete callback and transaction id */
  3798. transaction = conn_add_transact(conn, opcode, attrib, id);
  3799. memset(ev, 0, sizeof(*ev));
  3800. bdaddr2android(&bdaddr, &ev->bdaddr);
  3801. ev->attr_handle = gatt_db_attribute_get_handle(attrib);
  3802. ev->offset = offset;
  3803. ev->conn_id = conn->id;
  3804. ev->trans_id = transaction->id;
  3805. ev->is_prep = opcode == ATT_OP_PREP_WRITE_REQ;
  3806. if (opcode == ATT_OP_WRITE_REQ || opcode == ATT_OP_PREP_WRITE_REQ)
  3807. ev->need_rsp = 0x01;
  3808. else
  3809. gatt_db_attribute_write_result(attrib, id, 0);
  3810. ev->length = len;
  3811. memcpy(ev->value, value, len);
  3812. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  3813. HAL_EV_GATT_SERVER_REQUEST_WRITE,
  3814. sizeof(*ev) + ev->length , ev);
  3815. return;
  3816. failed:
  3817. gatt_db_attribute_write_result(attrib, id, ATT_ECODE_UNLIKELY);
  3818. }
  3819. static uint32_t android_to_gatt_permissions(int32_t hal_permissions)
  3820. {
  3821. uint32_t permissions = 0;
  3822. if (hal_permissions & HAL_GATT_PERMISSION_READ)
  3823. permissions |= GATT_PERM_READ;
  3824. if (hal_permissions & HAL_GATT_PERMISSION_READ_ENCRYPTED)
  3825. permissions |= GATT_PERM_READ_ENCRYPTED | GATT_PERM_READ;
  3826. if (hal_permissions & HAL_GATT_PERMISSION_READ_ENCRYPTED_MITM)
  3827. permissions |= GATT_PERM_READ_MITM | GATT_PERM_READ_ENCRYPTED |
  3828. GATT_PERM_READ;
  3829. if (hal_permissions & HAL_GATT_PERMISSION_WRITE)
  3830. permissions |= GATT_PERM_WRITE;
  3831. if (hal_permissions & HAL_GATT_PERMISSION_WRITE_ENCRYPTED)
  3832. permissions |= GATT_PERM_WRITE_ENCRYPTED | GATT_PERM_WRITE;
  3833. if (hal_permissions & HAL_GATT_PERMISSION_WRITE_ENCRYPTED_MITM)
  3834. permissions |= GATT_PERM_WRITE_MITM |
  3835. GATT_PERM_WRITE_ENCRYPTED | GATT_PERM_WRITE;
  3836. if (hal_permissions & HAL_GATT_PERMISSION_WRITE_SIGNED)
  3837. permissions |= GATT_PERM_WRITE_SIGNED;
  3838. if (hal_permissions & HAL_GATT_PERMISSION_WRITE_SIGNED_MITM)
  3839. permissions |= GATT_PERM_WRITE_SIGNED_MITM |
  3840. GATT_PERM_WRITE_SIGNED;
  3841. return permissions;
  3842. }
  3843. static void handle_server_add_characteristic(const void *buf, uint16_t len)
  3844. {
  3845. const struct hal_cmd_gatt_server_add_characteristic *cmd = buf;
  3846. struct hal_ev_gatt_server_characteristic_added ev;
  3847. struct gatt_app *server;
  3848. struct gatt_db_attribute *attrib;
  3849. bt_uuid_t uuid;
  3850. uint8_t status;
  3851. uint32_t permissions;
  3852. int32_t app_id = cmd->server_if;
  3853. DBG("");
  3854. memset(&ev, 0, sizeof(ev));
  3855. server = find_app_by_id(app_id);
  3856. if (!server) {
  3857. status = HAL_STATUS_FAILED;
  3858. goto failed;
  3859. }
  3860. attrib = gatt_db_get_attribute(gatt_db, cmd->service_handle);
  3861. if (!attrib) {
  3862. status = HAL_STATUS_FAILED;
  3863. goto failed;
  3864. }
  3865. android2uuid(cmd->uuid, &uuid);
  3866. permissions = android_to_gatt_permissions(cmd->permissions);
  3867. attrib = gatt_db_service_add_characteristic(attrib,
  3868. &uuid, permissions,
  3869. cmd->properties,
  3870. read_cb, write_cb,
  3871. INT_TO_PTR(app_id));
  3872. if (!attrib) {
  3873. status = HAL_STATUS_FAILED;
  3874. goto failed;
  3875. }
  3876. ev.char_handle = gatt_db_attribute_get_handle(attrib);
  3877. status = HAL_STATUS_SUCCESS;
  3878. failed:
  3879. ev.srvc_handle = cmd->service_handle;
  3880. ev.status = status;
  3881. ev.server_if = app_id;
  3882. ev.status = status == HAL_STATUS_SUCCESS ? GATT_SUCCESS : GATT_FAILURE;
  3883. memcpy(ev.uuid, cmd->uuid, sizeof(cmd->uuid));
  3884. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  3885. HAL_EV_GATT_SERVER_CHAR_ADDED, sizeof(ev), &ev);
  3886. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  3887. HAL_OP_GATT_SERVER_ADD_CHARACTERISTIC, status);
  3888. }
  3889. static void handle_server_add_descriptor(const void *buf, uint16_t len)
  3890. {
  3891. const struct hal_cmd_gatt_server_add_descriptor *cmd = buf;
  3892. struct hal_ev_gatt_server_descriptor_added ev;
  3893. struct gatt_app *server;
  3894. struct gatt_db_attribute *attrib;
  3895. bt_uuid_t uuid;
  3896. uint8_t status;
  3897. uint32_t permissions;
  3898. int32_t app_id = cmd->server_if;
  3899. DBG("");
  3900. memset(&ev, 0, sizeof(ev));
  3901. server = find_app_by_id(app_id);
  3902. if (!server) {
  3903. status = HAL_STATUS_FAILED;
  3904. goto failed;
  3905. }
  3906. android2uuid(cmd->uuid, &uuid);
  3907. permissions = android_to_gatt_permissions(cmd->permissions);
  3908. attrib = gatt_db_get_attribute(gatt_db, cmd->service_handle);
  3909. if (!attrib) {
  3910. status = HAL_STATUS_FAILED;
  3911. goto failed;
  3912. }
  3913. attrib = gatt_db_service_add_descriptor(attrib, &uuid, permissions,
  3914. read_cb, write_cb,
  3915. INT_TO_PTR(app_id));
  3916. if (!attrib) {
  3917. status = HAL_STATUS_FAILED;
  3918. goto failed;
  3919. }
  3920. ev.descr_handle = gatt_db_attribute_get_handle(attrib);
  3921. status = HAL_STATUS_SUCCESS;
  3922. failed:
  3923. ev.server_if = app_id;
  3924. ev.srvc_handle = cmd->service_handle;
  3925. memcpy(ev.uuid, cmd->uuid, sizeof(cmd->uuid));
  3926. ev.status = status == HAL_STATUS_SUCCESS ? GATT_SUCCESS : GATT_FAILURE;
  3927. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  3928. HAL_EV_GATT_SERVER_DESCRIPTOR_ADDED, sizeof(ev), &ev);
  3929. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  3930. HAL_OP_GATT_SERVER_ADD_DESCRIPTOR, status);
  3931. }
  3932. static void notify_service_change(void *data, void *user_data)
  3933. {
  3934. struct att_range range;
  3935. struct gatt_db_attribute *attrib = user_data;
  3936. gatt_db_attribute_get_service_handles(attrib, &range.start, &range.end);
  3937. /* In case of db error */
  3938. if (!range.end)
  3939. return;
  3940. notify_att_range_change(data, &range);
  3941. }
  3942. static sdp_record_t *get_sdp_record(uuid_t *uuid, uint16_t start, uint16_t end,
  3943. const char *name)
  3944. {
  3945. sdp_list_t *svclass_id, *apseq, *proto[2], *root, *aproto;
  3946. uuid_t root_uuid, proto_uuid, l2cap;
  3947. sdp_record_t *record;
  3948. sdp_data_t *psm, *sh, *eh;
  3949. uint16_t lp = ATT_PSM;
  3950. record = sdp_record_alloc();
  3951. if (!record)
  3952. return NULL;
  3953. sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
  3954. root = sdp_list_append(NULL, &root_uuid);
  3955. sdp_set_browse_groups(record, root);
  3956. sdp_list_free(root, NULL);
  3957. svclass_id = sdp_list_append(NULL, uuid);
  3958. sdp_set_service_classes(record, svclass_id);
  3959. sdp_list_free(svclass_id, NULL);
  3960. sdp_uuid16_create(&l2cap, L2CAP_UUID);
  3961. proto[0] = sdp_list_append(NULL, &l2cap);
  3962. psm = sdp_data_alloc(SDP_UINT16, &lp);
  3963. proto[0] = sdp_list_append(proto[0], psm);
  3964. apseq = sdp_list_append(NULL, proto[0]);
  3965. sdp_uuid16_create(&proto_uuid, ATT_UUID);
  3966. proto[1] = sdp_list_append(NULL, &proto_uuid);
  3967. sh = sdp_data_alloc(SDP_UINT16, &start);
  3968. proto[1] = sdp_list_append(proto[1], sh);
  3969. eh = sdp_data_alloc(SDP_UINT16, &end);
  3970. proto[1] = sdp_list_append(proto[1], eh);
  3971. apseq = sdp_list_append(apseq, proto[1]);
  3972. aproto = sdp_list_append(NULL, apseq);
  3973. sdp_set_access_protos(record, aproto);
  3974. if (name)
  3975. sdp_set_info_attr(record, name, "BlueZ for Android", NULL);
  3976. sdp_data_free(psm);
  3977. sdp_data_free(sh);
  3978. sdp_data_free(eh);
  3979. sdp_list_free(proto[0], NULL);
  3980. sdp_list_free(proto[1], NULL);
  3981. sdp_list_free(apseq, NULL);
  3982. sdp_list_free(aproto, NULL);
  3983. return record;
  3984. }
  3985. static uint32_t add_sdp_record(const bt_uuid_t *uuid, uint16_t start,
  3986. uint16_t end, const char *name)
  3987. {
  3988. sdp_record_t *rec;
  3989. uuid_t u, u32;
  3990. switch (uuid->type) {
  3991. case BT_UUID16:
  3992. sdp_uuid16_create(&u, uuid->value.u16);
  3993. break;
  3994. case BT_UUID32:
  3995. sdp_uuid32_create(&u32, uuid->value.u32);
  3996. sdp_uuid32_to_uuid128(&u, &u32);
  3997. break;
  3998. case BT_UUID128:
  3999. sdp_uuid128_create(&u, &uuid->value.u128);
  4000. break;
  4001. case BT_UUID_UNSPEC:
  4002. default:
  4003. return 0;
  4004. }
  4005. rec = get_sdp_record(&u, start, end, name);
  4006. if (!rec)
  4007. return 0;
  4008. if (bt_adapter_add_record(rec, 0) < 0) {
  4009. error("gatt: Failed to register SDP record");
  4010. sdp_record_free(rec);
  4011. return 0;
  4012. }
  4013. return rec->handle;
  4014. }
  4015. static bool match_service_sdp(const void *data, const void *user_data)
  4016. {
  4017. const struct service_sdp *s = data;
  4018. return s->service_handle == PTR_TO_INT(user_data);
  4019. }
  4020. static struct service_sdp *new_service_sdp_record(int32_t service_handle)
  4021. {
  4022. bt_uuid_t uuid;
  4023. struct service_sdp *s;
  4024. struct gatt_db_attribute *attrib;
  4025. uint16_t end_handle;
  4026. attrib = gatt_db_get_attribute(gatt_db, service_handle);
  4027. if (!attrib)
  4028. return NULL;
  4029. gatt_db_attribute_get_service_handles(attrib, NULL, &end_handle);
  4030. if (!end_handle)
  4031. return NULL;
  4032. if (!gatt_db_attribute_get_service_uuid(attrib, &uuid))
  4033. return NULL;
  4034. s = new0(struct service_sdp, 1);
  4035. s->service_handle = service_handle;
  4036. s->sdp_handle = add_sdp_record(&uuid, service_handle, end_handle, NULL);
  4037. if (!s->sdp_handle) {
  4038. free(s);
  4039. return NULL;
  4040. }
  4041. return s;
  4042. }
  4043. static void free_service_sdp_record(void *data)
  4044. {
  4045. struct service_sdp *s = data;
  4046. if (!s)
  4047. return;
  4048. bt_adapter_remove_record(s->sdp_handle);
  4049. free(s);
  4050. }
  4051. static bool add_service_sdp_record(int32_t service_handle)
  4052. {
  4053. struct service_sdp *s;
  4054. s = queue_find(services_sdp, match_service_sdp,
  4055. INT_TO_PTR(service_handle));
  4056. if (s)
  4057. return true;
  4058. s = new_service_sdp_record(service_handle);
  4059. if (!s)
  4060. return false;
  4061. queue_push_tail(services_sdp, s);
  4062. return true;
  4063. }
  4064. static void remove_service_sdp_record(int32_t service_handle)
  4065. {
  4066. struct service_sdp *s;
  4067. s = queue_remove_if(services_sdp, match_service_sdp,
  4068. INT_TO_PTR(service_handle));
  4069. if (!s)
  4070. return;
  4071. free_service_sdp_record(s);
  4072. }
  4073. static void handle_server_start_service(const void *buf, uint16_t len)
  4074. {
  4075. const struct hal_cmd_gatt_server_start_service *cmd = buf;
  4076. struct hal_ev_gatt_server_service_started ev;
  4077. struct gatt_app *server;
  4078. struct gatt_db_attribute *attrib;
  4079. uint8_t status;
  4080. DBG("transport 0x%02x", cmd->transport);
  4081. memset(&ev, 0, sizeof(ev));
  4082. if (cmd->transport == 0) {
  4083. status = HAL_STATUS_FAILED;
  4084. goto failed;
  4085. }
  4086. server = find_app_by_id(cmd->server_if);
  4087. if (!server) {
  4088. status = HAL_STATUS_FAILED;
  4089. goto failed;
  4090. }
  4091. if (cmd->transport & GATT_SERVER_TRANSPORT_BREDR_BIT) {
  4092. if (!add_service_sdp_record(cmd->service_handle)) {
  4093. status = HAL_STATUS_FAILED;
  4094. goto failed;
  4095. }
  4096. }
  4097. /* TODO: Handle BREDR only */
  4098. attrib = gatt_db_get_attribute(gatt_db, cmd->service_handle);
  4099. if (!attrib) {
  4100. status = HAL_STATUS_FAILED;
  4101. goto failed;
  4102. }
  4103. if (!gatt_db_service_set_active(attrib, true)) {
  4104. /*
  4105. * no need to clean SDP since this can fail only if service
  4106. * handle is invalid in which case add_sdp_record() also fails
  4107. */
  4108. status = HAL_STATUS_FAILED;
  4109. goto failed;
  4110. }
  4111. queue_foreach(gatt_devices, notify_service_change, attrib);
  4112. status = HAL_STATUS_SUCCESS;
  4113. failed:
  4114. ev.status = status == HAL_STATUS_SUCCESS ? GATT_SUCCESS : GATT_FAILURE;
  4115. ev.server_if = cmd->server_if;
  4116. ev.srvc_handle = cmd->service_handle;
  4117. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  4118. HAL_EV_GATT_SERVER_SERVICE_STARTED, sizeof(ev), &ev);
  4119. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  4120. HAL_OP_GATT_SERVER_START_SERVICE, status);
  4121. }
  4122. static void handle_server_stop_service(const void *buf, uint16_t len)
  4123. {
  4124. const struct hal_cmd_gatt_server_stop_service *cmd = buf;
  4125. struct hal_ev_gatt_server_service_stopped ev;
  4126. struct gatt_app *server;
  4127. struct gatt_db_attribute *attrib;
  4128. uint8_t status;
  4129. DBG("");
  4130. memset(&ev, 0, sizeof(ev));
  4131. server = find_app_by_id(cmd->server_if);
  4132. if (!server) {
  4133. status = HAL_STATUS_FAILED;
  4134. goto failed;
  4135. }
  4136. attrib = gatt_db_get_attribute(gatt_db, cmd->service_handle);
  4137. if (!attrib) {
  4138. status = HAL_STATUS_FAILED;
  4139. goto failed;
  4140. }
  4141. if (!gatt_db_service_set_active(attrib, false)) {
  4142. status = HAL_STATUS_FAILED;
  4143. goto failed;
  4144. }
  4145. remove_service_sdp_record(cmd->service_handle);
  4146. status = HAL_STATUS_SUCCESS;
  4147. queue_foreach(gatt_devices, notify_service_change, attrib);
  4148. failed:
  4149. ev.status = status == HAL_STATUS_SUCCESS ? GATT_SUCCESS : GATT_FAILURE;
  4150. ev.server_if = cmd->server_if;
  4151. ev.srvc_handle = cmd->service_handle;
  4152. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  4153. HAL_EV_GATT_SERVER_SERVICE_STOPPED, sizeof(ev), &ev);
  4154. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  4155. HAL_OP_GATT_SERVER_STOP_SERVICE, status);
  4156. }
  4157. static void handle_server_delete_service(const void *buf, uint16_t len)
  4158. {
  4159. const struct hal_cmd_gatt_server_delete_service *cmd = buf;
  4160. struct hal_ev_gatt_server_service_deleted ev;
  4161. struct gatt_app *server;
  4162. struct gatt_db_attribute *attrib;
  4163. uint8_t status;
  4164. DBG("");
  4165. memset(&ev, 0, sizeof(ev));
  4166. server = find_app_by_id(cmd->server_if);
  4167. if (!server) {
  4168. status = HAL_STATUS_FAILED;
  4169. goto failed;
  4170. }
  4171. attrib = gatt_db_get_attribute(gatt_db, cmd->service_handle);
  4172. if (!attrib) {
  4173. status = HAL_STATUS_FAILED;
  4174. goto failed;
  4175. }
  4176. if (!gatt_db_remove_service(gatt_db, attrib)) {
  4177. status = HAL_STATUS_FAILED;
  4178. goto failed;
  4179. }
  4180. remove_service_sdp_record(cmd->service_handle);
  4181. status = HAL_STATUS_SUCCESS;
  4182. failed:
  4183. ev.status = status == HAL_STATUS_SUCCESS ? GATT_SUCCESS : GATT_FAILURE;
  4184. ev.srvc_handle = cmd->service_handle;
  4185. ev.server_if = cmd->server_if;
  4186. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  4187. HAL_EV_GATT_SERVER_SERVICE_DELETED, sizeof(ev), &ev);
  4188. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  4189. HAL_OP_GATT_SERVER_DELETE_SERVICE, status);
  4190. }
  4191. static void indication_confirmation_cb(guint8 status, const guint8 *pdu,
  4192. guint16 len, gpointer user_data)
  4193. {
  4194. struct hal_ev_gatt_server_indication_sent ev;
  4195. ev.status = status;
  4196. ev.conn_id = PTR_TO_UINT(user_data);
  4197. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  4198. HAL_EV_GATT_SERVER_INDICATION_SENT, sizeof(ev), &ev);
  4199. }
  4200. static void handle_server_send_indication(const void *buf, uint16_t len)
  4201. {
  4202. const struct hal_cmd_gatt_server_send_indication *cmd = buf;
  4203. struct app_connection *conn;
  4204. uint8_t status;
  4205. uint16_t length;
  4206. uint8_t *pdu;
  4207. size_t mtu;
  4208. GAttribResultFunc confirmation_cb = NULL;
  4209. DBG("");
  4210. conn = find_connection_by_id(cmd->conn_id);
  4211. if (!conn) {
  4212. error("gatt: Could not find connection");
  4213. status = HAL_STATUS_FAILED;
  4214. goto reply;
  4215. }
  4216. pdu = g_attrib_get_buffer(conn->device->attrib, &mtu);
  4217. if (cmd->confirm) {
  4218. length = enc_indication(cmd->attribute_handle,
  4219. (uint8_t *) cmd->value, cmd->len, pdu,
  4220. mtu);
  4221. confirmation_cb = indication_confirmation_cb;
  4222. } else {
  4223. length = enc_notification(cmd->attribute_handle,
  4224. (uint8_t *) cmd->value,
  4225. cmd->len, pdu, mtu);
  4226. }
  4227. if (!g_attrib_send(conn->device->attrib, 0, pdu, length,
  4228. confirmation_cb, UINT_TO_PTR(conn->id), NULL)) {
  4229. error("gatt: Failed to send indication");
  4230. status = HAL_STATUS_FAILED;
  4231. } else {
  4232. status = HAL_STATUS_SUCCESS;
  4233. }
  4234. /* Here we confirm failed indications and all notifications */
  4235. if (status || !confirmation_cb)
  4236. indication_confirmation_cb(status, NULL, 0,
  4237. UINT_TO_PTR(conn->id));
  4238. reply:
  4239. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  4240. HAL_OP_GATT_SERVER_SEND_INDICATION, status);
  4241. }
  4242. static bool match_trans_id(const void *data, const void *user_data)
  4243. {
  4244. const struct pending_trans_data *transaction = data;
  4245. return transaction->id == PTR_TO_UINT(user_data);
  4246. }
  4247. static bool find_conn_waiting_exec_write(const void *data,
  4248. const void *user_data)
  4249. {
  4250. const struct app_connection *conn = data;
  4251. return conn->wait_execute_write;
  4252. }
  4253. static bool pending_execute_write(void)
  4254. {
  4255. return queue_find(app_connections, find_conn_waiting_exec_write, NULL);
  4256. }
  4257. static void handle_server_send_response(const void *buf, uint16_t len)
  4258. {
  4259. const struct hal_cmd_gatt_server_send_response *cmd = buf;
  4260. struct pending_trans_data *transaction;
  4261. struct app_connection *conn;
  4262. uint8_t status;
  4263. DBG("");
  4264. conn = find_connection_by_id(cmd->conn_id);
  4265. if (!conn) {
  4266. error("gatt: could not found connection");
  4267. status = HAL_STATUS_FAILED;
  4268. goto reply;
  4269. }
  4270. transaction = queue_remove_if(conn->transactions, match_trans_id,
  4271. UINT_TO_PTR(cmd->trans_id));
  4272. if (!transaction) {
  4273. error("gatt: transaction ID = %d not found", cmd->trans_id);
  4274. status = HAL_STATUS_FAILED;
  4275. goto reply;
  4276. }
  4277. if (transaction->opcode == ATT_OP_EXEC_WRITE_REQ) {
  4278. struct pending_request *req;
  4279. conn->wait_execute_write = false;
  4280. /* Check for execute response from all server applications */
  4281. if (pending_execute_write())
  4282. goto done;
  4283. /*
  4284. * This is usually done through db write callback but for
  4285. * execute write we dont have the attribute or handle to call
  4286. * gatt_db_attribute_write().
  4287. */
  4288. req = queue_peek_head(conn->device->pending_requests);
  4289. if (!req)
  4290. goto done;
  4291. /* Cast status to uint8_t, due to (byte) cast in java layer. */
  4292. req->error = err_to_att((uint8_t) cmd->status);
  4293. req->completed = true;
  4294. /*
  4295. * FIXME: Handle situation when not all server applications
  4296. * respond with a success.
  4297. */
  4298. }
  4299. /* Cast status to uint8_t, due to (byte) cast in java layer. */
  4300. if (transaction->opcode < ATT_OP_WRITE_REQ)
  4301. gatt_db_attribute_read_result(transaction->attrib,
  4302. transaction->serial_id,
  4303. err_to_att((uint8_t) cmd->status),
  4304. cmd->data, cmd->len);
  4305. else
  4306. gatt_db_attribute_write_result(transaction->attrib,
  4307. transaction->serial_id,
  4308. err_to_att((uint8_t) cmd->status));
  4309. send_dev_complete_response(conn->device, transaction->opcode);
  4310. done:
  4311. /* Clean request data */
  4312. free(transaction);
  4313. status = HAL_STATUS_SUCCESS;
  4314. reply:
  4315. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  4316. HAL_OP_GATT_SERVER_SEND_RESPONSE, status);
  4317. }
  4318. static void handle_client_scan_filter_setup(const void *buf, uint16_t len)
  4319. {
  4320. const struct hal_cmd_gatt_client_scan_filter_setup *cmd = buf;
  4321. DBG("client_if %u", cmd->client_if);
  4322. /* TODO */
  4323. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  4324. HAL_OP_GATT_CLIENT_SCAN_FILTER_SETUP,
  4325. HAL_STATUS_UNSUPPORTED);
  4326. }
  4327. static void handle_client_scan_filter_add_remove(const void *buf, uint16_t len)
  4328. {
  4329. const struct hal_cmd_gatt_client_scan_filter_add_remove *cmd = buf;
  4330. DBG("client_if %u", cmd->client_if);
  4331. /* TODO */
  4332. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  4333. HAL_OP_GATT_CLIENT_SCAN_FILTER_ADD_REMOVE,
  4334. HAL_STATUS_UNSUPPORTED);
  4335. }
  4336. static void handle_client_scan_filter_clear(const void *buf, uint16_t len)
  4337. {
  4338. const struct hal_cmd_gatt_client_scan_filter_clear *cmd = buf;
  4339. DBG("client_if %u", cmd->client_if);
  4340. /* TODO */
  4341. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  4342. HAL_OP_GATT_CLIENT_SCAN_FILTER_CLEAR,
  4343. HAL_STATUS_UNSUPPORTED);
  4344. }
  4345. static void handle_client_scan_filter_enable(const void *buf, uint16_t len)
  4346. {
  4347. const struct hal_cmd_gatt_client_scan_filter_enable *cmd = buf;
  4348. DBG("client_if %u", cmd->client_if);
  4349. /* TODO */
  4350. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  4351. HAL_OP_GATT_CLIENT_SCAN_FILTER_ENABLE,
  4352. HAL_STATUS_UNSUPPORTED);
  4353. }
  4354. static void handle_client_configure_mtu(const void *buf, uint16_t len)
  4355. {
  4356. const struct hal_cmd_gatt_client_configure_mtu *cmd = buf;
  4357. static struct app_connection *conn;
  4358. uint8_t status;
  4359. DBG("conn_id %u mtu %d", cmd->conn_id, cmd->mtu);
  4360. conn = find_connection_by_id(cmd->conn_id);
  4361. if (!conn) {
  4362. status = HAL_STATUS_FAILED;
  4363. goto failed;
  4364. }
  4365. /*
  4366. * currently MTU is always exchanged on connection, just report current
  4367. * value
  4368. *
  4369. * TODO figure out when send failed status in notification
  4370. * TODO should we fail for BR/EDR?
  4371. */
  4372. notify_client_mtu_change(conn, false);
  4373. status = HAL_STATUS_SUCCESS;
  4374. failed:
  4375. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  4376. HAL_OP_GATT_CLIENT_CONFIGURE_MTU,
  4377. status);
  4378. }
  4379. static void handle_client_conn_param_update(const void *buf, uint16_t len)
  4380. {
  4381. const struct hal_cmd_gatt_client_conn_param_update *cmd = buf;
  4382. char address[18];
  4383. bdaddr_t bdaddr;
  4384. android2bdaddr(cmd->address, &bdaddr);
  4385. ba2str(&bdaddr, address);
  4386. DBG("%s", address);
  4387. /* TODO */
  4388. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  4389. HAL_OP_GATT_CLIENT_CONN_PARAM_UPDATE,
  4390. HAL_STATUS_UNSUPPORTED);
  4391. }
  4392. static void handle_client_set_scan_param(const void *buf, uint16_t len)
  4393. {
  4394. const struct hal_cmd_gatt_client_set_scan_param *cmd = buf;
  4395. DBG("interval %d window %d", cmd->interval, cmd->window);
  4396. /* TODO */
  4397. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  4398. HAL_OP_GATT_CLIENT_SET_SCAN_PARAM,
  4399. HAL_STATUS_UNSUPPORTED);
  4400. }
  4401. static struct adv_instance *find_adv_instance(uint32_t client_if)
  4402. {
  4403. struct gatt_app *app;
  4404. struct adv_instance *adv;
  4405. uint8_t inst = 0;
  4406. unsigned int i;
  4407. app = find_app_by_id(client_if);
  4408. if (!app)
  4409. return NULL;
  4410. if (app->adv)
  4411. return app->adv;
  4412. /* Assume that kernel supports <= 32 advertising instances (5 today)
  4413. * We have already indicated the number to the android framework layers
  4414. * via the LE features so we don't check again here.
  4415. * The kernel will detect the error if needed
  4416. */
  4417. for (i = 0; i < sizeof(adv_inst_bits) * 8; i++) {
  4418. uint32_t mask = 1 << i;
  4419. if (!(adv_inst_bits & mask)) {
  4420. inst = i + 1;
  4421. adv_inst_bits |= mask;
  4422. break;
  4423. }
  4424. }
  4425. if (!inst)
  4426. return NULL;
  4427. adv = new0(__typeof__(*adv), 1);
  4428. adv->instance = inst;
  4429. app->adv = adv;
  4430. DBG("Assigned advertising instance %d for client %d", inst, client_if);
  4431. return adv;
  4432. };
  4433. /* Build advertising data object from a data buffer containing
  4434. * manufacturer_data, service_data, service uuids (in that order)
  4435. * The input data is raw with no TLV structure and the service uuids are 128 bit
  4436. */
  4437. static struct bt_ad *build_adv_data(int32_t manufacturer_data_len,
  4438. int32_t service_data_len,
  4439. int32_t service_uuid_len,
  4440. const uint8_t *data_in)
  4441. {
  4442. const int one_svc_uuid_len = 128 / 8; /* Android uses 128bit UUIDs */
  4443. uint8_t *src = (uint8_t *)data_in;
  4444. struct bt_ad *ad;
  4445. unsigned num_svc_uuids, i;
  4446. ad = bt_ad_new();
  4447. if (manufacturer_data_len >= 2) { /* Includes manufacturer id */
  4448. uint16_t manufacturer_id;
  4449. manufacturer_id = bt_get_le16(src);
  4450. src += 2;
  4451. if (!bt_ad_add_manufacturer_data(ad,
  4452. manufacturer_id,
  4453. src,
  4454. manufacturer_data_len - 2))
  4455. goto err;
  4456. src += manufacturer_data_len - 2;
  4457. }
  4458. if (service_data_len >= 2) { /* Includes service uuid (always 16 bit) */
  4459. bt_uuid_t bt_uuid;
  4460. uint16_t uuid16;
  4461. uuid16 = bt_get_le16(src);
  4462. src += 2;
  4463. bt_uuid16_create(&bt_uuid, uuid16);
  4464. if (!bt_ad_add_service_data(ad,
  4465. &bt_uuid,
  4466. src,
  4467. service_data_len - 2))
  4468. goto err;
  4469. src += service_data_len - 2;
  4470. }
  4471. if (service_uuid_len % one_svc_uuid_len) {
  4472. error("Service UUIDs not multiple of %d bytes (%d)",
  4473. one_svc_uuid_len, service_uuid_len);
  4474. num_svc_uuids = 0;
  4475. } else {
  4476. num_svc_uuids = service_uuid_len / one_svc_uuid_len;
  4477. }
  4478. for (i = 0; i < num_svc_uuids; i++) {
  4479. bt_uuid_t bt_uuid;
  4480. android2uuid(src, &bt_uuid);
  4481. src += one_svc_uuid_len;
  4482. if (!bt_ad_add_service_uuid(ad, &bt_uuid))
  4483. goto err;
  4484. }
  4485. return ad;
  4486. err:
  4487. bt_ad_unref(ad);
  4488. return NULL;
  4489. }
  4490. static void handle_client_setup_multi_adv(const void *buf, uint16_t len)
  4491. {
  4492. const struct hal_cmd_gatt_client_setup_multi_adv *cmd = buf;
  4493. struct hal_ev_gatt_client_multi_adv_enable ev;
  4494. struct adv_instance *adv;
  4495. uint8_t status;
  4496. DBG("client_if %d min_interval=%d max_interval=%d type=%d channel_map=0x%x tx_power=%d timeout=%d",
  4497. cmd->client_if,
  4498. cmd->min_interval,
  4499. cmd->max_interval,
  4500. cmd->type,
  4501. cmd->channel_map,
  4502. cmd->tx_power,
  4503. cmd->timeout);
  4504. adv = find_adv_instance(cmd->client_if);
  4505. if (!adv) {
  4506. status = HAL_STATUS_FAILED;
  4507. goto out;
  4508. }
  4509. status = HAL_STATUS_SUCCESS;
  4510. adv->timeout = cmd->timeout;
  4511. adv->type = cmd->type;
  4512. if (adv->type != ANDROID_ADVERTISING_EVENT_TYPE_SCANNABLE) {
  4513. bt_ad_unref(adv->sr);
  4514. adv->sr = NULL;
  4515. }
  4516. out:
  4517. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  4518. HAL_OP_GATT_CLIENT_SETUP_MULTI_ADV,
  4519. status);
  4520. ev.client_if = cmd->client_if;
  4521. ev.status = status;
  4522. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  4523. HAL_EV_GATT_CLIENT_MULTI_ADV_ENABLE, sizeof(ev), &ev);
  4524. }
  4525. /* This is not currently called by Android 5.1 */
  4526. static void handle_client_update_multi_adv(const void *buf, uint16_t len)
  4527. {
  4528. const struct hal_cmd_gatt_client_update_multi_adv *cmd = buf;
  4529. DBG("client_if %d", cmd->client_if);
  4530. /* TODO */
  4531. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  4532. HAL_OP_GATT_CLIENT_UPDATE_MULTI_ADV,
  4533. HAL_STATUS_UNSUPPORTED);
  4534. }
  4535. struct addrm_adv_cb_data {
  4536. int32_t client_if;
  4537. struct adv_instance *adv;
  4538. };
  4539. static void add_advertising_cb(uint8_t status, void *user_data)
  4540. {
  4541. struct addrm_adv_cb_data *cb_data = user_data;
  4542. struct hal_ev_gatt_client_multi_adv_data ev = {
  4543. .status = status,
  4544. .client_if = cb_data->client_if,
  4545. };
  4546. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  4547. HAL_EV_GATT_CLIENT_MULTI_ADV_DATA,
  4548. sizeof(ev), &ev);
  4549. free(cb_data);
  4550. }
  4551. static void handle_client_setup_multi_adv_inst(const void *buf, uint16_t len)
  4552. {
  4553. const struct hal_cmd_gatt_client_setup_multi_adv_inst *cmd = buf;
  4554. struct adv_instance *adv;
  4555. struct bt_ad *adv_data;
  4556. struct addrm_adv_cb_data *cb_data = NULL;
  4557. uint8_t status = HAL_STATUS_FAILED;
  4558. DBG("client_if %d set_scan_rsp=%d include_name=%d include_tx_power=%d appearance=%d manuf_data_len=%d svc_data_len=%d svc_uuid_len=%d",
  4559. cmd->client_if,
  4560. cmd->set_scan_rsp,
  4561. cmd->include_name,
  4562. cmd->include_tx_power,
  4563. cmd->appearance,
  4564. cmd->manufacturer_data_len,
  4565. cmd->service_data_len,
  4566. cmd->service_uuid_len
  4567. );
  4568. adv = find_adv_instance(cmd->client_if);
  4569. if (!adv)
  4570. goto out;
  4571. adv->include_tx_power = cmd->include_tx_power ? 1 : 0;
  4572. adv_data = build_adv_data(cmd->manufacturer_data_len,
  4573. cmd->service_data_len,
  4574. cmd->service_uuid_len,
  4575. cmd->data_service_uuid);
  4576. if (!adv_data)
  4577. goto out;
  4578. if (cmd->set_scan_rsp) {
  4579. bt_ad_unref(adv->sr);
  4580. adv->sr = adv_data;
  4581. } else {
  4582. bt_ad_unref(adv->ad);
  4583. adv->ad = adv_data;
  4584. }
  4585. cb_data = new0(__typeof__(*cb_data), 1);
  4586. cb_data->client_if = cmd->client_if;
  4587. cb_data->adv = adv;
  4588. if (!bt_le_add_advertising(adv, add_advertising_cb, cb_data)) {
  4589. error("gatt: Could not add advertising");
  4590. free(cb_data);
  4591. goto out;
  4592. }
  4593. status = HAL_STATUS_SUCCESS;
  4594. out:
  4595. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  4596. HAL_OP_GATT_CLIENT_SETUP_MULTI_ADV_INST,
  4597. status);
  4598. if (status != HAL_STATUS_SUCCESS) {
  4599. struct hal_ev_gatt_client_multi_adv_data ev = {
  4600. .status = status,
  4601. .client_if = cmd->client_if,
  4602. };
  4603. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  4604. HAL_EV_GATT_CLIENT_MULTI_ADV_DATA,
  4605. sizeof(ev), &ev);
  4606. }
  4607. }
  4608. static void remove_advertising_cb(uint8_t status, void *user_data)
  4609. {
  4610. struct addrm_adv_cb_data *cb_data = user_data;
  4611. struct hal_ev_gatt_client_multi_adv_data ev = {
  4612. .status = status,
  4613. .client_if = cb_data->client_if,
  4614. };
  4615. free_adv_instance(cb_data->adv);
  4616. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  4617. HAL_EV_GATT_CLIENT_MULTI_ADV_DISABLE,
  4618. sizeof(ev), &ev);
  4619. free(cb_data);
  4620. }
  4621. static void handle_client_disable_multi_adv_inst(const void *buf, uint16_t len)
  4622. {
  4623. const struct hal_cmd_gatt_client_disable_multi_adv_inst *cmd = buf;
  4624. struct adv_instance *adv;
  4625. struct gatt_app *app;
  4626. struct addrm_adv_cb_data *cb_data = NULL;
  4627. uint8_t status = HAL_STATUS_FAILED;
  4628. DBG("client_if %d", cmd->client_if);
  4629. adv = find_adv_instance(cmd->client_if);
  4630. if (!adv)
  4631. goto out;
  4632. cb_data = new0(__typeof__(*cb_data), 1);
  4633. cb_data->client_if = cmd->client_if;
  4634. cb_data->adv = adv;
  4635. if (!bt_le_remove_advertising(adv, remove_advertising_cb, cb_data)) {
  4636. error("gatt: Could not remove advertising");
  4637. free(cb_data);
  4638. goto out;
  4639. }
  4640. app = find_app_by_id(cmd->client_if);
  4641. if (app)
  4642. app->adv = NULL;
  4643. status = HAL_STATUS_SUCCESS;
  4644. out:
  4645. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  4646. HAL_OP_GATT_CLIENT_DISABLE_MULTI_ADV_INST,
  4647. status);
  4648. if (status != HAL_STATUS_SUCCESS) {
  4649. struct hal_ev_gatt_client_multi_adv_data ev = {
  4650. .status = status,
  4651. .client_if = cmd->client_if,
  4652. };
  4653. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  4654. HAL_EV_GATT_CLIENT_MULTI_ADV_DISABLE,
  4655. sizeof(ev), &ev);
  4656. }
  4657. }
  4658. static void handle_client_configure_batchscan(const void *buf, uint16_t len)
  4659. {
  4660. const struct hal_cmd_gatt_client_configure_batchscan *cmd = buf;
  4661. DBG("client_if %d", cmd->client_if);
  4662. /* TODO */
  4663. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  4664. HAL_OP_GATT_CLIENT_CONFIGURE_BATCHSCAN,
  4665. HAL_STATUS_UNSUPPORTED);
  4666. }
  4667. static void handle_client_enable_batchscan(const void *buf, uint16_t len)
  4668. {
  4669. const struct hal_cmd_gatt_client_enable_batchscan *cmd = buf;
  4670. DBG("client_if %d", cmd->client_if);
  4671. /* TODO */
  4672. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  4673. HAL_OP_GATT_CLIENT_ENABLE_BATCHSCAN,
  4674. HAL_STATUS_UNSUPPORTED);
  4675. }
  4676. static void handle_client_disable_batchscan(const void *buf, uint16_t len)
  4677. {
  4678. const struct hal_cmd_gatt_client_disable_batchscan *cmd = buf;
  4679. DBG("client_if %d", cmd->client_if);
  4680. /* TODO */
  4681. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  4682. HAL_OP_GATT_CLIENT_DISABLE_BATCHSCAN,
  4683. HAL_STATUS_UNSUPPORTED);
  4684. }
  4685. static void handle_client_read_batchscan_reports(const void *buf, uint16_t len)
  4686. {
  4687. const struct hal_cmd_gatt_client_read_batchscan_reports *cmd = buf;
  4688. DBG("client_if %d", cmd->client_if);
  4689. /* TODO */
  4690. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
  4691. HAL_OP_GATT_CLIENT_READ_BATCHSCAN_REPORTS,
  4692. HAL_STATUS_UNSUPPORTED);
  4693. }
  4694. static const struct ipc_handler cmd_handlers[] = {
  4695. /* HAL_OP_GATT_CLIENT_REGISTER */
  4696. { handle_client_register, false,
  4697. sizeof(struct hal_cmd_gatt_client_register) },
  4698. /* HAL_OP_GATT_CLIENT_UNREGISTER */
  4699. { handle_client_unregister, false,
  4700. sizeof(struct hal_cmd_gatt_client_unregister) },
  4701. /* HAL_OP_GATT_CLIENT_SCAN */
  4702. { handle_client_scan, false,
  4703. sizeof(struct hal_cmd_gatt_client_scan) },
  4704. /* HAL_OP_GATT_CLIENT_CONNECT */
  4705. { handle_client_connect, false,
  4706. sizeof(struct hal_cmd_gatt_client_connect) },
  4707. /* HAL_OP_GATT_CLIENT_DISCONNECT */
  4708. { handle_client_disconnect, false,
  4709. sizeof(struct hal_cmd_gatt_client_disconnect) },
  4710. /* HAL_OP_GATT_CLIENT_LISTEN */
  4711. { handle_client_listen, false,
  4712. sizeof(struct hal_cmd_gatt_client_listen) },
  4713. /* HAL_OP_GATT_CLIENT_REFRESH */
  4714. { handle_client_refresh, false,
  4715. sizeof(struct hal_cmd_gatt_client_refresh) },
  4716. /* HAL_OP_GATT_CLIENT_SEARCH_SERVICE */
  4717. { handle_client_search_service, true,
  4718. sizeof(struct hal_cmd_gatt_client_search_service) },
  4719. /* HAL_OP_GATT_CLIENT_GET_INCLUDED_SERVICE */
  4720. { handle_client_get_included_service, true,
  4721. sizeof(struct hal_cmd_gatt_client_get_included_service) },
  4722. /* HAL_OP_GATT_CLIENT_GET_CHARACTERISTIC */
  4723. { handle_client_get_characteristic, true,
  4724. sizeof(struct hal_cmd_gatt_client_get_characteristic) },
  4725. /* HAL_OP_GATT_CLIENT_GET_DESCRIPTOR */
  4726. { handle_client_get_descriptor, true,
  4727. sizeof(struct hal_cmd_gatt_client_get_descriptor) },
  4728. /* HAL_OP_GATT_CLIENT_READ_CHARACTERISTIC */
  4729. { handle_client_read_characteristic, false,
  4730. sizeof(struct hal_cmd_gatt_client_read_characteristic) },
  4731. /* HAL_OP_GATT_CLIENT_WRITE_CHARACTERISTIC */
  4732. { handle_client_write_characteristic, true,
  4733. sizeof(struct hal_cmd_gatt_client_write_characteristic) },
  4734. /* HAL_OP_GATT_CLIENT_READ_DESCRIPTOR */
  4735. { handle_client_read_descriptor, false,
  4736. sizeof(struct hal_cmd_gatt_client_read_descriptor) },
  4737. /* HAL_OP_GATT_CLIENT_WRITE_DESCRIPTOR */
  4738. { handle_client_write_descriptor, true,
  4739. sizeof(struct hal_cmd_gatt_client_write_descriptor) },
  4740. /* HAL_OP_GATT_CLIENT_EXECUTE_WRITE */
  4741. { handle_client_execute_write, false,
  4742. sizeof(struct hal_cmd_gatt_client_execute_write)},
  4743. /* HAL_OP_GATT_CLIENT_REGISTER_FOR_NOTIFICATION */
  4744. { handle_client_register_for_notification, false,
  4745. sizeof(struct hal_cmd_gatt_client_register_for_notification) },
  4746. /* HAL_OP_GATT_CLIENT_DEREGISTER_FOR_NOTIFICATION */
  4747. { handle_client_deregister_for_notification, false,
  4748. sizeof(struct hal_cmd_gatt_client_deregister_for_notification) },
  4749. /* HAL_OP_GATT_CLIENT_READ_REMOTE_RSSI */
  4750. { handle_client_read_remote_rssi, false,
  4751. sizeof(struct hal_cmd_gatt_client_read_remote_rssi) },
  4752. /* HAL_OP_GATT_CLIENT_GET_DEVICE_TYPE */
  4753. { handle_client_get_device_type, false,
  4754. sizeof(struct hal_cmd_gatt_client_get_device_type) },
  4755. /* HAL_OP_GATT_CLIENT_SET_ADV_DATA */
  4756. { handle_client_set_adv_data, true,
  4757. sizeof(struct hal_cmd_gatt_client_set_adv_data) },
  4758. /* HAL_OP_GATT_CLIENT_TEST_COMMAND */
  4759. { handle_client_test_command, false,
  4760. sizeof(struct hal_cmd_gatt_client_test_command) },
  4761. /* HAL_OP_GATT_SERVER_REGISTER */
  4762. { handle_server_register, false,
  4763. sizeof(struct hal_cmd_gatt_server_register) },
  4764. /* HAL_OP_GATT_SERVER_UNREGISTER */
  4765. { handle_server_unregister, false,
  4766. sizeof(struct hal_cmd_gatt_server_unregister) },
  4767. /* HAL_OP_GATT_SERVER_CONNECT */
  4768. { handle_server_connect, false,
  4769. sizeof(struct hal_cmd_gatt_server_connect) },
  4770. /* HAL_OP_GATT_SERVER_DISCONNECT */
  4771. { handle_server_disconnect, false,
  4772. sizeof(struct hal_cmd_gatt_server_disconnect) },
  4773. /* HAL_OP_GATT_SERVER_ADD_SERVICE */
  4774. { handle_server_add_service, false,
  4775. sizeof(struct hal_cmd_gatt_server_add_service) },
  4776. /* HAL_OP_GATT_SERVER_ADD_INC_SERVICE */
  4777. { handle_server_add_included_service, false,
  4778. sizeof(struct hal_cmd_gatt_server_add_inc_service) },
  4779. /* HAL_OP_GATT_SERVER_ADD_CHARACTERISTIC */
  4780. { handle_server_add_characteristic, false,
  4781. sizeof(struct hal_cmd_gatt_server_add_characteristic) },
  4782. /* HAL_OP_GATT_SERVER_ADD_DESCRIPTOR */
  4783. { handle_server_add_descriptor, false,
  4784. sizeof(struct hal_cmd_gatt_server_add_descriptor) },
  4785. /* HAL_OP_GATT_SERVER_START_SERVICE */
  4786. { handle_server_start_service, false,
  4787. sizeof(struct hal_cmd_gatt_server_start_service) },
  4788. /* HAL_OP_GATT_SERVER_STOP_SERVICE */
  4789. { handle_server_stop_service, false,
  4790. sizeof(struct hal_cmd_gatt_server_stop_service) },
  4791. /* HAL_OP_GATT_SERVER_DELETE_SERVICE */
  4792. { handle_server_delete_service, false,
  4793. sizeof(struct hal_cmd_gatt_server_delete_service) },
  4794. /* HAL_OP_GATT_SERVER_SEND_INDICATION */
  4795. { handle_server_send_indication, true,
  4796. sizeof(struct hal_cmd_gatt_server_send_indication) },
  4797. /* HAL_OP_GATT_SERVER_SEND_RESPONSE */
  4798. { handle_server_send_response, true,
  4799. sizeof(struct hal_cmd_gatt_server_send_response) },
  4800. /* HAL_OP_GATT_CLIENT_SCAN_FILTER_SETUP */
  4801. { handle_client_scan_filter_setup, false,
  4802. sizeof(struct hal_cmd_gatt_client_scan_filter_setup) },
  4803. /* HAL_OP_GATT_CLIENT_SCAN_FILTER_ADD_REMOVE */
  4804. { handle_client_scan_filter_add_remove, true,
  4805. sizeof(struct hal_cmd_gatt_client_scan_filter_add_remove) },
  4806. /* HAL_OP_GATT_CLIENT_SCAN_FILTER_CLEAR */
  4807. { handle_client_scan_filter_clear, false,
  4808. sizeof(struct hal_cmd_gatt_client_scan_filter_clear) },
  4809. /* HAL_OP_GATT_CLIENT_SCAN_FILTER_ENABLE */
  4810. { handle_client_scan_filter_enable, false,
  4811. sizeof(struct hal_cmd_gatt_client_scan_filter_enable) },
  4812. /* HAL_OP_GATT_CLIENT_CONFIGURE_MTU */
  4813. { handle_client_configure_mtu, false,
  4814. sizeof(struct hal_cmd_gatt_client_configure_mtu) },
  4815. /* HAL_OP_GATT_CLIENT_CONN_PARAM_UPDATE */
  4816. { handle_client_conn_param_update, false,
  4817. sizeof(struct hal_cmd_gatt_client_conn_param_update) },
  4818. /* HAL_OP_GATT_CLIENT_SET_SCAN_PARAM */
  4819. { handle_client_set_scan_param, false,
  4820. sizeof(struct hal_cmd_gatt_client_set_scan_param) },
  4821. /* HAL_OP_GATT_CLIENT_SETUP_MULTI_ADV */
  4822. { handle_client_setup_multi_adv, false,
  4823. sizeof(struct hal_cmd_gatt_client_setup_multi_adv) },
  4824. /* HAL_OP_GATT_CLIENT_UPDATE_MULTI_ADV */
  4825. { handle_client_update_multi_adv, false,
  4826. sizeof(struct hal_cmd_gatt_client_update_multi_adv) },
  4827. /* HAL_OP_GATT_CLIENT_SETUP_MULTI_ADV_INST */
  4828. { handle_client_setup_multi_adv_inst, true,
  4829. sizeof(struct hal_cmd_gatt_client_setup_multi_adv_inst) },
  4830. /* HAL_OP_GATT_CLIENT_DISABLE_MULTI_ADV_INST */
  4831. { handle_client_disable_multi_adv_inst, false,
  4832. sizeof(struct hal_cmd_gatt_client_disable_multi_adv_inst) },
  4833. /* HAL_OP_GATT_CLIENT_CONFIGURE_BATCHSCAN */
  4834. { handle_client_configure_batchscan, false,
  4835. sizeof(struct hal_cmd_gatt_client_configure_batchscan) },
  4836. /* HAL_OP_GATT_CLIENT_ENABLE_BATCHSCAN */
  4837. { handle_client_enable_batchscan, false,
  4838. sizeof(struct hal_cmd_gatt_client_enable_batchscan) },
  4839. /* HAL_OP_GATT_CLIENT_DISABLE_BATCHSCAN */
  4840. { handle_client_disable_batchscan, false,
  4841. sizeof(struct hal_cmd_gatt_client_disable_batchscan) },
  4842. /* HAL_OP_GATT_CLIENT_READ_BATCHSCAN_REPORTS */
  4843. { handle_client_read_batchscan_reports, false,
  4844. sizeof(struct hal_cmd_gatt_client_read_batchscan_reports) },
  4845. };
  4846. static uint8_t read_by_type(const uint8_t *cmd, uint16_t cmd_len,
  4847. struct gatt_device *device)
  4848. {
  4849. uint16_t start, end;
  4850. uint16_t len = 0;
  4851. bt_uuid_t uuid;
  4852. struct queue *q;
  4853. DBG("");
  4854. switch (cmd[0]) {
  4855. case ATT_OP_READ_BY_TYPE_REQ:
  4856. len = dec_read_by_type_req(cmd, cmd_len, &start, &end, &uuid);
  4857. break;
  4858. case ATT_OP_READ_BY_GROUP_REQ:
  4859. len = dec_read_by_grp_req(cmd, cmd_len, &start, &end, &uuid);
  4860. break;
  4861. default:
  4862. break;
  4863. }
  4864. if (!len)
  4865. return ATT_ECODE_INVALID_PDU;
  4866. if (start > end || start == 0)
  4867. return ATT_ECODE_INVALID_HANDLE;
  4868. q = queue_new();
  4869. switch (cmd[0]) {
  4870. case ATT_OP_READ_BY_TYPE_REQ:
  4871. gatt_db_read_by_type(gatt_db, start, end, uuid, q);
  4872. break;
  4873. case ATT_OP_READ_BY_GROUP_REQ:
  4874. gatt_db_read_by_group_type(gatt_db, start, end, uuid, q);
  4875. break;
  4876. default:
  4877. break;
  4878. }
  4879. if (queue_isempty(q)) {
  4880. queue_destroy(q, NULL);
  4881. return ATT_ECODE_ATTR_NOT_FOUND;
  4882. }
  4883. while (queue_peek_head(q)) {
  4884. struct pending_request *data;
  4885. struct gatt_db_attribute *attrib = queue_pop_head(q);
  4886. data = new0(struct pending_request, 1);
  4887. data->attrib = attrib;
  4888. queue_push_tail(device->pending_requests, data);
  4889. }
  4890. queue_destroy(q, NULL);
  4891. process_dev_pending_requests(device, cmd[0]);
  4892. return 0;
  4893. }
  4894. static uint8_t read_request(const uint8_t *cmd, uint16_t cmd_len,
  4895. struct gatt_device *dev)
  4896. {
  4897. struct gatt_db_attribute *attrib;
  4898. uint16_t handle;
  4899. uint16_t len;
  4900. uint16_t offset;
  4901. struct pending_request *data;
  4902. DBG("");
  4903. switch (cmd[0]) {
  4904. case ATT_OP_READ_BLOB_REQ:
  4905. len = dec_read_blob_req(cmd, cmd_len, &handle, &offset);
  4906. if (!len)
  4907. return ATT_ECODE_INVALID_PDU;
  4908. break;
  4909. case ATT_OP_READ_REQ:
  4910. len = dec_read_req(cmd, cmd_len, &handle);
  4911. if (!len)
  4912. return ATT_ECODE_INVALID_PDU;
  4913. offset = 0;
  4914. break;
  4915. default:
  4916. error("gatt: Unexpected read type 0x%02x", cmd[0]);
  4917. return ATT_ECODE_REQ_NOT_SUPP;
  4918. }
  4919. attrib = gatt_db_get_attribute(gatt_db, handle);
  4920. if (attrib == 0)
  4921. return ATT_ECODE_INVALID_HANDLE;
  4922. data = new0(struct pending_request, 1);
  4923. data->offset = offset;
  4924. data->attrib = attrib;
  4925. queue_push_tail(dev->pending_requests, data);
  4926. process_dev_pending_requests(dev, cmd[0]);
  4927. return 0;
  4928. }
  4929. static uint8_t mtu_att_handle(const uint8_t *cmd, uint16_t cmd_len,
  4930. struct gatt_device *dev)
  4931. {
  4932. uint16_t rmtu, mtu, len;
  4933. size_t length;
  4934. uint8_t *rsp;
  4935. DBG("");
  4936. len = dec_mtu_req(cmd, cmd_len, &rmtu);
  4937. if (!len)
  4938. return ATT_ECODE_INVALID_PDU;
  4939. /* MTU exchange shall not be used on BR/EDR - Vol 3. Part G. 4.3.1 */
  4940. if (get_cid(dev) != ATT_CID)
  4941. return ATT_ECODE_UNLIKELY;
  4942. if (!get_local_mtu(dev, &mtu))
  4943. return ATT_ECODE_UNLIKELY;
  4944. if (!update_mtu(dev, rmtu))
  4945. return ATT_ECODE_UNLIKELY;
  4946. rsp = g_attrib_get_buffer(dev->attrib, &length);
  4947. /* Respond with our MTU */
  4948. len = enc_mtu_resp(mtu, rsp, length);
  4949. if (!g_attrib_send(dev->attrib, 0, rsp, len, NULL, NULL, NULL))
  4950. return ATT_ECODE_UNLIKELY;
  4951. return 0;
  4952. }
  4953. static uint8_t find_info_handle(const uint8_t *cmd, uint16_t cmd_len,
  4954. uint8_t *rsp, size_t rsp_size, uint16_t *length)
  4955. {
  4956. struct gatt_db_attribute *attrib;
  4957. struct queue *q, *temp;
  4958. struct att_data_list *adl;
  4959. int iterator = 0;
  4960. uint16_t start, end;
  4961. uint16_t len, queue_len;
  4962. uint8_t format;
  4963. uint8_t ret = 0;
  4964. DBG("");
  4965. len = dec_find_info_req(cmd, cmd_len, &start, &end);
  4966. if (!len)
  4967. return ATT_ECODE_INVALID_PDU;
  4968. if (start > end || start == 0)
  4969. return ATT_ECODE_INVALID_HANDLE;
  4970. q = queue_new();
  4971. gatt_db_find_information(gatt_db, start, end, q);
  4972. if (queue_isempty(q)) {
  4973. queue_destroy(q, NULL);
  4974. return ATT_ECODE_ATTR_NOT_FOUND;
  4975. }
  4976. temp = queue_new();
  4977. attrib = queue_peek_head(q);
  4978. /* UUIDS can be only 128 bit and 16 bit */
  4979. len = bt_uuid_len(gatt_db_attribute_get_type(attrib));
  4980. if (len != 2 && len != 16) {
  4981. queue_destroy(q, NULL);
  4982. queue_destroy(temp, NULL);
  4983. return ATT_ECODE_UNLIKELY;
  4984. }
  4985. while (attrib) {
  4986. const bt_uuid_t *type;
  4987. type = gatt_db_attribute_get_type(attrib);
  4988. if (bt_uuid_len(type) != len)
  4989. break;
  4990. queue_push_tail(temp, queue_pop_head(q));
  4991. attrib = queue_peek_head(q);
  4992. }
  4993. queue_destroy(q, NULL);
  4994. queue_len = queue_length(temp);
  4995. adl = att_data_list_alloc(queue_len, len + sizeof(uint16_t));
  4996. if (!adl) {
  4997. queue_destroy(temp, NULL);
  4998. return ATT_ECODE_INSUFF_RESOURCES;
  4999. }
  5000. while (queue_peek_head(temp)) {
  5001. uint8_t *value;
  5002. const bt_uuid_t *type;
  5003. struct gatt_db_attribute *attrib = queue_pop_head(temp);
  5004. uint16_t handle;
  5005. type = gatt_db_attribute_get_type(attrib);
  5006. if (!type)
  5007. break;
  5008. value = adl->data[iterator++];
  5009. handle = gatt_db_attribute_get_handle(attrib);
  5010. put_le16(handle, value);
  5011. memcpy(&value[2], &type->value, len);
  5012. }
  5013. if (len == 2)
  5014. format = ATT_FIND_INFO_RESP_FMT_16BIT;
  5015. else
  5016. format = ATT_FIND_INFO_RESP_FMT_128BIT;
  5017. len = enc_find_info_resp(format, adl, rsp, rsp_size);
  5018. if (!len)
  5019. ret = ATT_ECODE_UNLIKELY;
  5020. *length = len;
  5021. att_data_list_free(adl);
  5022. queue_destroy(temp, NULL);
  5023. return ret;
  5024. }
  5025. struct find_by_type_request_data {
  5026. struct gatt_device *device;
  5027. uint8_t *search_value;
  5028. size_t search_vlen;
  5029. uint8_t error;
  5030. };
  5031. static void find_by_type_request_cb(struct gatt_db_attribute *attrib,
  5032. void *user_data)
  5033. {
  5034. struct find_by_type_request_data *find_data = user_data;
  5035. struct pending_request *request_data;
  5036. if (find_data->error)
  5037. return;
  5038. request_data = new0(struct pending_request, 1);
  5039. request_data->filter_value = malloc0(find_data->search_vlen);
  5040. if (!request_data->filter_value) {
  5041. destroy_pending_request(request_data);
  5042. find_data->error = ATT_ECODE_INSUFF_RESOURCES;
  5043. return;
  5044. }
  5045. request_data->attrib = attrib;
  5046. request_data->filter_vlen = find_data->search_vlen;
  5047. memcpy(request_data->filter_value, find_data->search_value,
  5048. find_data->search_vlen);
  5049. queue_push_tail(find_data->device->pending_requests, request_data);
  5050. }
  5051. static uint8_t find_by_type_request(const uint8_t *cmd, uint16_t cmd_len,
  5052. struct gatt_device *device)
  5053. {
  5054. uint8_t search_value[cmd_len];
  5055. size_t search_vlen;
  5056. uint16_t start, end;
  5057. bt_uuid_t uuid;
  5058. uint16_t len;
  5059. struct find_by_type_request_data data;
  5060. DBG("");
  5061. len = dec_find_by_type_req(cmd, cmd_len, &start, &end, &uuid,
  5062. search_value, &search_vlen);
  5063. if (!len)
  5064. return ATT_ECODE_INVALID_PDU;
  5065. if (start > end || start == 0)
  5066. return ATT_ECODE_INVALID_HANDLE;
  5067. data.error = 0;
  5068. data.search_vlen = search_vlen;
  5069. data.search_value = search_value;
  5070. data.device = device;
  5071. if (gatt_db_find_by_type(gatt_db, start, end, &uuid,
  5072. find_by_type_request_cb, &data) == 0) {
  5073. size_t mtu;
  5074. uint8_t *rsp = g_attrib_get_buffer(device->attrib, &mtu);
  5075. len = enc_error_resp(ATT_OP_FIND_BY_TYPE_REQ, start,
  5076. ATT_ECODE_ATTR_NOT_FOUND, rsp, mtu);
  5077. g_attrib_send(device->attrib, 0, rsp, len, NULL, NULL, NULL);
  5078. return 0;
  5079. }
  5080. if (!data.error)
  5081. process_dev_pending_requests(device, ATT_OP_FIND_BY_TYPE_REQ);
  5082. return data.error;
  5083. }
  5084. static void write_confirm(struct gatt_db_attribute *attrib,
  5085. int err, void *user_data)
  5086. {
  5087. if (!err)
  5088. return;
  5089. error("Error writting attribute %p", attrib);
  5090. }
  5091. static void write_cmd_request(const uint8_t *cmd, uint16_t cmd_len,
  5092. struct gatt_device *dev)
  5093. {
  5094. uint8_t value[cmd_len];
  5095. struct gatt_db_attribute *attrib;
  5096. uint32_t permissions;
  5097. uint16_t handle;
  5098. uint16_t len;
  5099. size_t vlen;
  5100. len = dec_write_cmd(cmd, cmd_len, &handle, value, &vlen);
  5101. if (!len)
  5102. return;
  5103. if (handle == 0)
  5104. return;
  5105. attrib = gatt_db_get_attribute(gatt_db, handle);
  5106. if (!attrib)
  5107. return;
  5108. permissions = gatt_db_attribute_get_permissions(attrib);
  5109. if (check_device_permissions(dev, cmd[0], permissions))
  5110. return;
  5111. gatt_db_attribute_write(attrib, 0, value, vlen, cmd[0],
  5112. g_attrib_get_att(dev->attrib),
  5113. write_confirm, NULL);
  5114. }
  5115. static void write_signed_cmd_request(const uint8_t *cmd, uint16_t cmd_len,
  5116. struct gatt_device *dev)
  5117. {
  5118. uint8_t value[cmd_len];
  5119. uint8_t s[ATT_SIGNATURE_LEN];
  5120. struct gatt_db_attribute *attrib;
  5121. uint32_t permissions;
  5122. uint16_t handle;
  5123. uint16_t len;
  5124. size_t vlen;
  5125. uint8_t csrk[16];
  5126. uint32_t sign_cnt;
  5127. if (get_cid(dev) != ATT_CID) {
  5128. error("gatt: Remote tries write signed on BR/EDR bearer");
  5129. connection_cleanup(dev);
  5130. return;
  5131. }
  5132. if (get_sec_level(dev) != BT_SECURITY_LOW) {
  5133. error("gatt: Remote tries write signed on encrypted link");
  5134. connection_cleanup(dev);
  5135. return;
  5136. }
  5137. if (!bt_get_csrk(&dev->bdaddr, false, csrk, &sign_cnt, NULL)) {
  5138. error("gatt: No valid csrk from remote device");
  5139. return;
  5140. }
  5141. len = dec_signed_write_cmd(cmd, cmd_len, &handle, value, &vlen, s);
  5142. if (handle == 0)
  5143. return;
  5144. attrib = gatt_db_get_attribute(gatt_db, handle);
  5145. if (!attrib)
  5146. return;
  5147. permissions = gatt_db_attribute_get_permissions(attrib);
  5148. if (check_device_permissions(dev, cmd[0], permissions))
  5149. return;
  5150. if (len) {
  5151. uint8_t t[ATT_SIGNATURE_LEN];
  5152. uint32_t r_sign_cnt = get_le32(s);
  5153. if (r_sign_cnt < sign_cnt) {
  5154. error("gatt: Invalid sign counter (%d<%d)",
  5155. r_sign_cnt, sign_cnt);
  5156. return;
  5157. }
  5158. /* Generate signature and verify it */
  5159. if (!bt_crypto_sign_att(crypto, csrk, cmd,
  5160. cmd_len - ATT_SIGNATURE_LEN,
  5161. r_sign_cnt, t)) {
  5162. error("gatt: Error when generating att signature");
  5163. return;
  5164. }
  5165. if (memcmp(t, s, ATT_SIGNATURE_LEN)) {
  5166. error("gatt: signature does not match");
  5167. return;
  5168. }
  5169. /* Signature OK, proceed with write */
  5170. bt_update_sign_counter(&dev->bdaddr, false, r_sign_cnt);
  5171. gatt_db_attribute_write(attrib, 0, value, vlen, cmd[0],
  5172. g_attrib_get_att(dev->attrib),
  5173. write_confirm, NULL);
  5174. }
  5175. }
  5176. static void attribute_write_cb(struct gatt_db_attribute *attrib, int err,
  5177. void *user_data)
  5178. {
  5179. struct pending_request *data = user_data;
  5180. uint8_t error = err_to_att(err);
  5181. DBG("");
  5182. data->attrib = attrib;
  5183. data->error = error;
  5184. data->completed = true;
  5185. }
  5186. static uint8_t write_req_request(const uint8_t *cmd, uint16_t cmd_len,
  5187. struct gatt_device *dev)
  5188. {
  5189. uint8_t value[cmd_len];
  5190. struct pending_request *data;
  5191. struct gatt_db_attribute *attrib;
  5192. uint32_t permissions;
  5193. uint16_t handle;
  5194. uint16_t len;
  5195. uint8_t error;
  5196. size_t vlen;
  5197. len = dec_write_req(cmd, cmd_len, &handle, value, &vlen);
  5198. if (!len)
  5199. return ATT_ECODE_INVALID_PDU;
  5200. if (handle == 0)
  5201. return ATT_ECODE_INVALID_HANDLE;
  5202. attrib = gatt_db_get_attribute(gatt_db, handle);
  5203. if (!attrib)
  5204. return ATT_ECODE_ATTR_NOT_FOUND;
  5205. permissions = gatt_db_attribute_get_permissions(attrib);
  5206. error = check_device_permissions(dev, cmd[0], permissions);
  5207. if (error)
  5208. return error;
  5209. data = new0(struct pending_request, 1);
  5210. data->attrib = attrib;
  5211. queue_push_tail(dev->pending_requests, data);
  5212. if (!gatt_db_attribute_write(attrib, 0, value, vlen, cmd[0],
  5213. g_attrib_get_att(dev->attrib),
  5214. attribute_write_cb, data)) {
  5215. queue_remove(dev->pending_requests, data);
  5216. free(data);
  5217. return ATT_ECODE_UNLIKELY;
  5218. }
  5219. send_dev_complete_response(dev, cmd[0]);
  5220. return 0;
  5221. }
  5222. static uint8_t write_prep_request(const uint8_t *cmd, uint16_t cmd_len,
  5223. struct gatt_device *dev)
  5224. {
  5225. uint8_t value[cmd_len];
  5226. struct pending_request *data;
  5227. struct gatt_db_attribute *attrib;
  5228. uint32_t permissions;
  5229. uint16_t handle;
  5230. uint16_t offset;
  5231. uint8_t error;
  5232. uint16_t len;
  5233. size_t vlen;
  5234. len = dec_prep_write_req(cmd, cmd_len, &handle, &offset,
  5235. value, &vlen);
  5236. if (!len)
  5237. return ATT_ECODE_INVALID_PDU;
  5238. if (handle == 0)
  5239. return ATT_ECODE_INVALID_HANDLE;
  5240. attrib = gatt_db_get_attribute(gatt_db, handle);
  5241. if (!attrib)
  5242. return ATT_ECODE_ATTR_NOT_FOUND;
  5243. permissions = gatt_db_attribute_get_permissions(attrib);
  5244. error = check_device_permissions(dev, cmd[0], permissions);
  5245. if (error)
  5246. return error;
  5247. data = new0(struct pending_request, 1);
  5248. data->attrib = attrib;
  5249. data->offset = offset;
  5250. queue_push_tail(dev->pending_requests, data);
  5251. data->value = g_memdup(value, vlen);
  5252. data->length = vlen;
  5253. if (!gatt_db_attribute_write(attrib, offset, value, vlen, cmd[0],
  5254. g_attrib_get_att(dev->attrib),
  5255. attribute_write_cb, data)) {
  5256. queue_remove(dev->pending_requests, data);
  5257. g_free(data->value);
  5258. free(data);
  5259. return ATT_ECODE_UNLIKELY;
  5260. }
  5261. send_dev_complete_response(dev, cmd[0]);
  5262. return 0;
  5263. }
  5264. static void send_server_write_execute_notify(void *data, void *user_data)
  5265. {
  5266. struct hal_ev_gatt_server_request_exec_write *ev = user_data;
  5267. struct pending_trans_data *transaction;
  5268. struct app_connection *conn = data;
  5269. if (!conn->wait_execute_write)
  5270. return;
  5271. ev->conn_id = conn->id;
  5272. transaction = conn_add_transact(conn, ATT_OP_EXEC_WRITE_REQ, NULL, 0);
  5273. ev->trans_id = transaction->id;
  5274. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
  5275. HAL_EV_GATT_SERVER_REQUEST_EXEC_WRITE, sizeof(*ev), ev);
  5276. }
  5277. static uint8_t write_execute_request(const uint8_t *cmd, uint16_t cmd_len,
  5278. struct gatt_device *dev)
  5279. {
  5280. struct hal_ev_gatt_server_request_exec_write ev;
  5281. uint8_t value;
  5282. struct pending_request *data;
  5283. /*
  5284. * Check if there was any write prep before.
  5285. * TODO: Try to find better error code if possible
  5286. */
  5287. if (!pending_execute_write())
  5288. return ATT_ECODE_UNLIKELY;
  5289. if (!dec_exec_write_req(cmd, cmd_len, &value))
  5290. return ATT_ECODE_INVALID_PDU;
  5291. memset(&ev, 0, sizeof(ev));
  5292. bdaddr2android(&dev->bdaddr, &ev.bdaddr);
  5293. ev.exec_write = value;
  5294. data = new0(struct pending_request, 1);
  5295. queue_push_tail(dev->pending_requests, data);
  5296. queue_foreach(app_connections, send_server_write_execute_notify, &ev);
  5297. send_dev_complete_response(dev, cmd[0]);
  5298. return 0;
  5299. }
  5300. static void att_handler(const uint8_t *ipdu, uint16_t len, gpointer user_data)
  5301. {
  5302. struct gatt_device *dev = user_data;
  5303. uint8_t status;
  5304. uint16_t resp_length = 0;
  5305. size_t length;
  5306. uint8_t *opdu = g_attrib_get_buffer(dev->attrib, &length);
  5307. DBG("op 0x%02x", ipdu[0]);
  5308. if (len > length) {
  5309. error("gatt: Too much data on ATT socket %p", opdu);
  5310. status = ATT_ECODE_INVALID_PDU;
  5311. goto done;
  5312. }
  5313. switch (ipdu[0]) {
  5314. case ATT_OP_READ_BY_GROUP_REQ:
  5315. case ATT_OP_READ_BY_TYPE_REQ:
  5316. status = read_by_type(ipdu, len, dev);
  5317. break;
  5318. case ATT_OP_READ_REQ:
  5319. case ATT_OP_READ_BLOB_REQ:
  5320. status = read_request(ipdu, len, dev);
  5321. break;
  5322. case ATT_OP_MTU_REQ:
  5323. status = mtu_att_handle(ipdu, len, dev);
  5324. break;
  5325. case ATT_OP_FIND_INFO_REQ:
  5326. status = find_info_handle(ipdu, len, opdu, length,
  5327. &resp_length);
  5328. break;
  5329. case ATT_OP_WRITE_REQ:
  5330. status = write_req_request(ipdu, len, dev);
  5331. break;
  5332. case ATT_OP_WRITE_CMD:
  5333. write_cmd_request(ipdu, len, dev);
  5334. /* No response on write cmd */
  5335. return;
  5336. case ATT_OP_SIGNED_WRITE_CMD:
  5337. write_signed_cmd_request(ipdu, len, dev);
  5338. /* No response on write signed cmd */
  5339. return;
  5340. case ATT_OP_PREP_WRITE_REQ:
  5341. status = write_prep_request(ipdu, len, dev);
  5342. break;
  5343. case ATT_OP_FIND_BY_TYPE_REQ:
  5344. status = find_by_type_request(ipdu, len, dev);
  5345. break;
  5346. case ATT_OP_EXEC_WRITE_REQ:
  5347. status = write_execute_request(ipdu, len, dev);
  5348. break;
  5349. case ATT_OP_READ_MULTI_REQ:
  5350. default:
  5351. DBG("Unsupported request 0x%02x", ipdu[0]);
  5352. status = ATT_ECODE_REQ_NOT_SUPP;
  5353. break;
  5354. }
  5355. done:
  5356. if (status)
  5357. resp_length = enc_error_resp(ipdu[0], 0x0000, status, opdu,
  5358. length);
  5359. g_attrib_send(dev->attrib, 0, opdu, resp_length, NULL, NULL, NULL);
  5360. }
  5361. static void connect_confirm(GIOChannel *io, void *user_data)
  5362. {
  5363. struct gatt_device *dev;
  5364. bdaddr_t dst;
  5365. GError *gerr = NULL;
  5366. DBG("");
  5367. bt_io_get(io, &gerr, BT_IO_OPT_DEST_BDADDR, &dst, BT_IO_OPT_INVALID);
  5368. if (gerr) {
  5369. error("gatt: bt_io_get: %s", gerr->message);
  5370. g_error_free(gerr);
  5371. return;
  5372. }
  5373. /* TODO Handle collision */
  5374. dev = find_device_by_addr(&dst);
  5375. if (!dev) {
  5376. dev = create_device(&dst);
  5377. } else {
  5378. if ((dev->state != DEVICE_DISCONNECTED) &&
  5379. !(dev->state == DEVICE_CONNECT_INIT &&
  5380. bt_kernel_conn_control())) {
  5381. char addr[18];
  5382. ba2str(&dst, addr);
  5383. info("gatt: Rejecting incoming connection from %s",
  5384. addr);
  5385. goto drop;
  5386. }
  5387. }
  5388. if (!bt_io_accept(io, connect_cb, device_ref(dev), NULL, NULL)) {
  5389. error("gatt: failed to accept connection");
  5390. device_unref(dev);
  5391. goto drop;
  5392. }
  5393. queue_foreach(listen_apps, create_app_connection, dev);
  5394. device_set_state(dev, DEVICE_CONNECT_READY);
  5395. return;
  5396. drop:
  5397. g_io_channel_shutdown(io, TRUE, NULL);
  5398. }
  5399. struct gap_srvc_handles {
  5400. struct gatt_db_attribute *srvc;
  5401. /* Characteristics */
  5402. struct gatt_db_attribute *dev_name;
  5403. struct gatt_db_attribute *appear;
  5404. struct gatt_db_attribute *priv;
  5405. };
  5406. static struct gap_srvc_handles gap_srvc_data;
  5407. #define APPEARANCE_GENERIC_PHONE 0x0040
  5408. #define PERIPHERAL_PRIVACY_DISABLE 0x00
  5409. static void device_name_read_cb(struct gatt_db_attribute *attrib,
  5410. unsigned int id, uint16_t offset,
  5411. uint8_t opcode, struct bt_att *att,
  5412. void *user_data)
  5413. {
  5414. const char *name = bt_get_adapter_name();
  5415. gatt_db_attribute_read_result(attrib, id, 0, (void *) name,
  5416. strlen(name));
  5417. }
  5418. static void register_gap_service(void)
  5419. {
  5420. uint16_t start, end;
  5421. bt_uuid_t uuid;
  5422. /* GAP UUID */
  5423. bt_uuid16_create(&uuid, 0x1800);
  5424. gap_srvc_data.srvc = gatt_db_add_service(gatt_db, &uuid, true, 7);
  5425. /* Device name characteristic */
  5426. bt_uuid16_create(&uuid, GATT_CHARAC_DEVICE_NAME);
  5427. gap_srvc_data.dev_name =
  5428. gatt_db_service_add_characteristic(gap_srvc_data.srvc,
  5429. &uuid, GATT_PERM_READ,
  5430. GATT_CHR_PROP_READ,
  5431. device_name_read_cb,
  5432. NULL, NULL);
  5433. /* Appearance */
  5434. bt_uuid16_create(&uuid, GATT_CHARAC_APPEARANCE);
  5435. gap_srvc_data.appear =
  5436. gatt_db_service_add_characteristic(gap_srvc_data.srvc,
  5437. &uuid, GATT_PERM_READ,
  5438. GATT_CHR_PROP_READ,
  5439. NULL, NULL, NULL);
  5440. if (gap_srvc_data.appear) {
  5441. uint16_t value;
  5442. /* Store appearance into db */
  5443. value = cpu_to_le16(APPEARANCE_GENERIC_PHONE);
  5444. gatt_db_attribute_write(gap_srvc_data.appear, 0,
  5445. (void *) &value, sizeof(value),
  5446. ATT_OP_WRITE_REQ, NULL,
  5447. write_confirm, NULL);
  5448. }
  5449. /* Pripheral privacy flag */
  5450. bt_uuid16_create(&uuid, GATT_CHARAC_PERIPHERAL_PRIV_FLAG);
  5451. gap_srvc_data.priv =
  5452. gatt_db_service_add_characteristic(gap_srvc_data.srvc,
  5453. &uuid, GATT_PERM_READ,
  5454. GATT_CHR_PROP_READ,
  5455. NULL, NULL, NULL);
  5456. if (gap_srvc_data.priv) {
  5457. uint8_t value;
  5458. /* Store privacy into db */
  5459. value = PERIPHERAL_PRIVACY_DISABLE;
  5460. gatt_db_attribute_write(gap_srvc_data.priv, 0,
  5461. &value, sizeof(value),
  5462. ATT_OP_WRITE_REQ, NULL,
  5463. write_confirm, NULL);
  5464. }
  5465. gatt_db_service_set_active(gap_srvc_data.srvc , true);
  5466. /* SDP */
  5467. bt_uuid16_create(&uuid, 0x1800);
  5468. gatt_db_attribute_get_service_handles(gap_srvc_data.srvc, &start, &end);
  5469. gap_sdp_handle = add_sdp_record(&uuid, start, end,
  5470. "Generic Access Profile");
  5471. if (!gap_sdp_handle)
  5472. error("gatt: Failed to register GAP SDP record");
  5473. }
  5474. static void device_info_read_cb(struct gatt_db_attribute *attrib,
  5475. unsigned int id, uint16_t offset,
  5476. uint8_t opcode, struct bt_att *att,
  5477. void *user_data)
  5478. {
  5479. char *buf = user_data;
  5480. gatt_db_attribute_read_result(attrib, id, 0, user_data, strlen(buf));
  5481. }
  5482. static void device_info_read_system_id_cb(struct gatt_db_attribute *attrib,
  5483. unsigned int id, uint16_t offset,
  5484. uint8_t opcode, struct bt_att *att,
  5485. void *user_data)
  5486. {
  5487. uint8_t pdu[8];
  5488. put_le64(bt_config_get_system_id(), pdu);
  5489. gatt_db_attribute_read_result(attrib, id, 0, pdu, sizeof(pdu));
  5490. }
  5491. static void device_info_read_pnp_id_cb(struct gatt_db_attribute *attrib,
  5492. unsigned int id, uint16_t offset,
  5493. uint8_t opcode, struct bt_att *att,
  5494. void *user_data)
  5495. {
  5496. uint8_t pdu[7];
  5497. pdu[0] = bt_config_get_pnp_source();
  5498. put_le16(bt_config_get_pnp_vendor(), &pdu[1]);
  5499. put_le16(bt_config_get_pnp_product(), &pdu[3]);
  5500. put_le16(bt_config_get_pnp_version(), &pdu[5]);
  5501. gatt_db_attribute_read_result(attrib, id, 0, pdu, sizeof(pdu));
  5502. }
  5503. static void register_device_info_service(void)
  5504. {
  5505. bt_uuid_t uuid;
  5506. struct gatt_db_attribute *service;
  5507. uint16_t start_handle, end_handle;
  5508. const char *data;
  5509. uint32_t enc_perm = GATT_PERM_READ | GATT_PERM_READ_ENCRYPTED;
  5510. DBG("");
  5511. /* Device Information Service */
  5512. bt_uuid16_create(&uuid, 0x180a);
  5513. service = gatt_db_add_service(gatt_db, &uuid, true, 17);
  5514. /* User data are not const hence (void *) cast is used */
  5515. data = bt_config_get_name();
  5516. if (data) {
  5517. bt_uuid16_create(&uuid, GATT_CHARAC_MODEL_NUMBER_STRING);
  5518. gatt_db_service_add_characteristic(service, &uuid,
  5519. GATT_PERM_READ,
  5520. GATT_CHR_PROP_READ,
  5521. device_info_read_cb, NULL,
  5522. (void *) data);
  5523. }
  5524. data = bt_config_get_serial();
  5525. if (data) {
  5526. bt_uuid16_create(&uuid, GATT_CHARAC_SERIAL_NUMBER_STRING);
  5527. gatt_db_service_add_characteristic(service, &uuid,
  5528. enc_perm, GATT_CHR_PROP_READ,
  5529. device_info_read_cb, NULL,
  5530. (void *) data);
  5531. }
  5532. if (bt_config_get_system_id()) {
  5533. bt_uuid16_create(&uuid, GATT_CHARAC_SYSTEM_ID);
  5534. gatt_db_service_add_characteristic(service, &uuid,
  5535. enc_perm, GATT_CHR_PROP_READ,
  5536. device_info_read_system_id_cb,
  5537. NULL, NULL);
  5538. }
  5539. data = bt_config_get_fw_rev();
  5540. if (data) {
  5541. bt_uuid16_create(&uuid, GATT_CHARAC_FIRMWARE_REVISION_STRING);
  5542. gatt_db_service_add_characteristic(service, &uuid,
  5543. GATT_PERM_READ,
  5544. GATT_CHR_PROP_READ,
  5545. device_info_read_cb, NULL,
  5546. (void *) data);
  5547. }
  5548. data = bt_config_get_hw_rev();
  5549. if (data) {
  5550. bt_uuid16_create(&uuid, GATT_CHARAC_HARDWARE_REVISION_STRING);
  5551. gatt_db_service_add_characteristic(service, &uuid,
  5552. GATT_PERM_READ,
  5553. GATT_CHR_PROP_READ,
  5554. device_info_read_cb, NULL,
  5555. (void *) data);
  5556. }
  5557. bt_uuid16_create(&uuid, GATT_CHARAC_SOFTWARE_REVISION_STRING);
  5558. gatt_db_service_add_characteristic(service, &uuid, GATT_PERM_READ,
  5559. GATT_CHR_PROP_READ, device_info_read_cb,
  5560. NULL, VERSION);
  5561. data = bt_config_get_vendor();
  5562. if (data) {
  5563. bt_uuid16_create(&uuid, GATT_CHARAC_MANUFACTURER_NAME_STRING);
  5564. gatt_db_service_add_characteristic(service, &uuid,
  5565. GATT_PERM_READ,
  5566. GATT_CHR_PROP_READ,
  5567. device_info_read_cb, NULL,
  5568. (void *) data);
  5569. }
  5570. if (bt_config_get_pnp_source()) {
  5571. bt_uuid16_create(&uuid, GATT_CHARAC_PNP_ID);
  5572. gatt_db_service_add_characteristic(service, &uuid,
  5573. GATT_PERM_READ,
  5574. GATT_CHR_PROP_READ,
  5575. device_info_read_pnp_id_cb,
  5576. NULL, NULL);
  5577. }
  5578. gatt_db_service_set_active(service, true);
  5579. /* SDP */
  5580. bt_uuid16_create(&uuid, 0x180a);
  5581. gatt_db_attribute_get_service_handles(service, &start_handle,
  5582. &end_handle);
  5583. dis_sdp_handle = add_sdp_record(&uuid, start_handle, end_handle,
  5584. "Device Information Service");
  5585. if (!dis_sdp_handle)
  5586. error("gatt: Failed to register DIS SDP record");
  5587. }
  5588. static void gatt_srvc_change_write_cb(struct gatt_db_attribute *attrib,
  5589. unsigned int id, uint16_t offset,
  5590. const uint8_t *value, size_t len,
  5591. uint8_t opcode, struct bt_att *att,
  5592. void *user_data)
  5593. {
  5594. struct gatt_device *dev;
  5595. bdaddr_t bdaddr;
  5596. if (!get_dst_addr(att, &bdaddr)) {
  5597. error("gatt: srvc_change_write_cb, could not obtain BDADDR");
  5598. return;
  5599. }
  5600. dev = find_device_by_addr(&bdaddr);
  5601. if (!dev) {
  5602. error("gatt: Could not find device ?!");
  5603. return;
  5604. }
  5605. if (!bt_device_is_bonded(&bdaddr)) {
  5606. gatt_db_attribute_write_result(attrib, id,
  5607. ATT_ECODE_AUTHORIZATION);
  5608. return;
  5609. }
  5610. /* 2 octets are expected as CCC value */
  5611. if (len != 2) {
  5612. gatt_db_attribute_write_result(attrib, id,
  5613. ATT_ECODE_INVAL_ATTR_VALUE_LEN);
  5614. return;
  5615. }
  5616. /* Set services changed indication value */
  5617. bt_store_gatt_ccc(&bdaddr, get_le16(value));
  5618. gatt_db_attribute_write_result(attrib, id, 0);
  5619. }
  5620. static void gatt_srvc_change_read_cb(struct gatt_db_attribute *attrib,
  5621. unsigned int id, uint16_t offset,
  5622. uint8_t opcode, struct bt_att *att,
  5623. void *user_data)
  5624. {
  5625. struct gatt_device *dev;
  5626. uint8_t pdu[2];
  5627. bdaddr_t bdaddr;
  5628. if (!get_dst_addr(att, &bdaddr)) {
  5629. error("gatt: srvc_change_read_cb, could not obtain BDADDR");
  5630. return;
  5631. }
  5632. dev = find_device_by_addr(&bdaddr);
  5633. if (!dev) {
  5634. error("gatt: Could not find device ?!");
  5635. return;
  5636. }
  5637. put_le16(bt_get_gatt_ccc(&dev->bdaddr), pdu);
  5638. gatt_db_attribute_read_result(attrib, id, 0, pdu, sizeof(pdu));
  5639. }
  5640. static void register_gatt_service(void)
  5641. {
  5642. struct gatt_db_attribute *service;
  5643. uint16_t start_handle, end_handle;
  5644. bt_uuid_t uuid;
  5645. DBG("");
  5646. bt_uuid16_create(&uuid, 0x1801);
  5647. service = gatt_db_add_service(gatt_db, &uuid, true, 4);
  5648. bt_uuid16_create(&uuid, GATT_CHARAC_SERVICE_CHANGED);
  5649. service_changed_attrib = gatt_db_service_add_characteristic(service,
  5650. &uuid, GATT_PERM_NONE,
  5651. GATT_CHR_PROP_INDICATE,
  5652. NULL, NULL, NULL);
  5653. bt_uuid16_create(&uuid, GATT_CLIENT_CHARAC_CFG_UUID);
  5654. gatt_db_service_add_descriptor(service, &uuid,
  5655. GATT_PERM_READ | GATT_PERM_WRITE,
  5656. gatt_srvc_change_read_cb,
  5657. gatt_srvc_change_write_cb, NULL);
  5658. gatt_db_service_set_active(service, true);
  5659. /* SDP */
  5660. bt_uuid16_create(&uuid, 0x1801);
  5661. gatt_db_attribute_get_service_handles(service, &start_handle,
  5662. &end_handle);
  5663. gatt_sdp_handle = add_sdp_record(&uuid, start_handle, end_handle,
  5664. "Generic Attribute Profile");
  5665. if (!gatt_sdp_handle)
  5666. error("gatt: Failed to register GATT SDP record");
  5667. }
  5668. static bool start_listening(void)
  5669. {
  5670. /* BR/EDR socket */
  5671. bredr_io = bt_io_listen(NULL, connect_confirm, NULL, NULL, NULL,
  5672. BT_IO_OPT_SOURCE_TYPE, BDADDR_BREDR,
  5673. BT_IO_OPT_PSM, ATT_PSM,
  5674. BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_LOW,
  5675. BT_IO_OPT_INVALID);
  5676. /* LE socket */
  5677. le_io = bt_io_listen(NULL, connect_confirm, NULL, NULL, NULL,
  5678. BT_IO_OPT_SOURCE_TYPE, BDADDR_LE_PUBLIC,
  5679. BT_IO_OPT_CID, ATT_CID,
  5680. BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_LOW,
  5681. BT_IO_OPT_INVALID);
  5682. if (!le_io && !bredr_io) {
  5683. error("gatt: Failed to start listening IO");
  5684. return false;
  5685. }
  5686. return true;
  5687. }
  5688. static void gatt_paired_cb(const bdaddr_t *addr)
  5689. {
  5690. struct gatt_device *dev;
  5691. char address[18];
  5692. struct app_connection *conn;
  5693. dev = find_device_by_addr(addr);
  5694. if (!dev)
  5695. return;
  5696. ba2str(addr, address);
  5697. DBG("Paired device %s", address);
  5698. /* conn without app is internal one used for search primary services */
  5699. conn = find_conn_without_app(dev);
  5700. if (!conn)
  5701. return;
  5702. if (conn->timeout_id > 0) {
  5703. g_source_remove(conn->timeout_id);
  5704. conn->timeout_id = 0;
  5705. }
  5706. search_dev_for_srvc(conn, NULL);
  5707. }
  5708. static void gatt_unpaired_cb(const bdaddr_t *addr)
  5709. {
  5710. struct gatt_device *dev;
  5711. char address[18];
  5712. dev = find_device_by_addr(addr);
  5713. if (!dev)
  5714. return;
  5715. ba2str(addr, address);
  5716. DBG("Unpaired device %s", address);
  5717. queue_remove(gatt_devices, dev);
  5718. destroy_device(dev);
  5719. }
  5720. bool bt_gatt_register(struct ipc *ipc, const bdaddr_t *addr)
  5721. {
  5722. DBG("");
  5723. if (!bt_paired_register(gatt_paired_cb)) {
  5724. error("gatt: Could not register paired callback");
  5725. return false;
  5726. }
  5727. if (!bt_unpaired_register(gatt_unpaired_cb)) {
  5728. error("gatt: Could not register unpaired callback");
  5729. return false;
  5730. }
  5731. if (!start_listening())
  5732. return false;
  5733. crypto = bt_crypto_new();
  5734. if (!crypto) {
  5735. error("gatt: Failed to setup crypto");
  5736. goto failed;
  5737. }
  5738. gatt_devices = queue_new();
  5739. gatt_apps = queue_new();
  5740. app_connections = queue_new();
  5741. listen_apps = queue_new();
  5742. services_sdp = queue_new();
  5743. gatt_db = gatt_db_new();
  5744. if (!gatt_db) {
  5745. error("gatt: Failed to allocate memory for database");
  5746. goto failed;
  5747. }
  5748. if (!bt_le_register(le_device_found_handler)) {
  5749. error("gatt: bt_le_register failed");
  5750. goto failed;
  5751. }
  5752. bacpy(&adapter_addr, addr);
  5753. hal_ipc = ipc;
  5754. ipc_register(hal_ipc, HAL_SERVICE_ID_GATT, cmd_handlers,
  5755. G_N_ELEMENTS(cmd_handlers));
  5756. register_gap_service();
  5757. register_device_info_service();
  5758. register_gatt_service();
  5759. info("gatt: LE: %s BR/EDR: %s", le_io ? "enabled" : "disabled",
  5760. bredr_io ? "enabled" : "disabled");
  5761. return true;
  5762. failed:
  5763. bt_paired_unregister(gatt_paired_cb);
  5764. bt_unpaired_unregister(gatt_unpaired_cb);
  5765. queue_destroy(gatt_apps, NULL);
  5766. gatt_apps = NULL;
  5767. queue_destroy(gatt_devices, NULL);
  5768. gatt_devices = NULL;
  5769. queue_destroy(app_connections, NULL);
  5770. app_connections = NULL;
  5771. queue_destroy(listen_apps, NULL);
  5772. listen_apps = NULL;
  5773. queue_destroy(services_sdp, NULL);
  5774. services_sdp = NULL;
  5775. gatt_db_unref(gatt_db);
  5776. gatt_db = NULL;
  5777. bt_crypto_unref(crypto);
  5778. crypto = NULL;
  5779. if (le_io) {
  5780. g_io_channel_unref(le_io);
  5781. le_io = NULL;
  5782. }
  5783. if (bredr_io) {
  5784. g_io_channel_unref(bredr_io);
  5785. bredr_io = NULL;
  5786. }
  5787. return false;
  5788. }
  5789. void bt_gatt_unregister(void)
  5790. {
  5791. DBG("");
  5792. ipc_unregister(hal_ipc, HAL_SERVICE_ID_GATT);
  5793. hal_ipc = NULL;
  5794. queue_destroy(app_connections, destroy_connection);
  5795. app_connections = NULL;
  5796. queue_destroy(gatt_apps, destroy_gatt_app);
  5797. gatt_apps = NULL;
  5798. queue_destroy(gatt_devices, destroy_device);
  5799. gatt_devices = NULL;
  5800. queue_destroy(services_sdp, free_service_sdp_record);
  5801. services_sdp = NULL;
  5802. queue_destroy(listen_apps, NULL);
  5803. listen_apps = NULL;
  5804. gatt_db_unref(gatt_db);
  5805. gatt_db = NULL;
  5806. if (le_io) {
  5807. g_io_channel_unref(le_io);
  5808. le_io = NULL;
  5809. }
  5810. if (bredr_io) {
  5811. g_io_channel_unref(bredr_io);
  5812. bredr_io = NULL;
  5813. }
  5814. if (gap_sdp_handle) {
  5815. bt_adapter_remove_record(gap_sdp_handle);
  5816. gap_sdp_handle = 0;
  5817. }
  5818. if (gatt_sdp_handle) {
  5819. bt_adapter_remove_record(gatt_sdp_handle);
  5820. gatt_sdp_handle = 0;
  5821. }
  5822. if (dis_sdp_handle) {
  5823. bt_adapter_remove_record(dis_sdp_handle);
  5824. dis_sdp_handle = 0;
  5825. }
  5826. bt_crypto_unref(crypto);
  5827. crypto = NULL;
  5828. bt_le_unregister();
  5829. bt_unpaired_unregister(gatt_unpaired_cb);
  5830. }
  5831. unsigned int bt_gatt_register_app(const char *uuid, gatt_type_t type,
  5832. gatt_conn_cb_t func)
  5833. {
  5834. struct gatt_app *app;
  5835. bt_uuid_t u, u128;
  5836. bt_string_to_uuid(&u, uuid);
  5837. bt_uuid_to_uuid128(&u, &u128);
  5838. app = register_app((void *) &u128.value.u128, type);
  5839. if (!app)
  5840. return 0;
  5841. app->func = func;
  5842. return app->id;
  5843. }
  5844. bool bt_gatt_unregister_app(unsigned int id)
  5845. {
  5846. uint8_t status;
  5847. status = unregister_app(id);
  5848. return status != HAL_STATUS_FAILED;
  5849. }
  5850. bool bt_gatt_connect_app(unsigned int id, const bdaddr_t *addr)
  5851. {
  5852. uint8_t status;
  5853. status = handle_connect(id, addr, false);
  5854. return status != HAL_STATUS_FAILED;
  5855. }
  5856. bool bt_gatt_disconnect_app(unsigned int id, const bdaddr_t *addr)
  5857. {
  5858. struct app_connection match;
  5859. struct app_connection *conn;
  5860. struct gatt_device *device;
  5861. struct gatt_app *app;
  5862. app = find_app_by_id(id);
  5863. if (!app)
  5864. return false;
  5865. device = find_device_by_addr(addr);
  5866. if (!device)
  5867. return false;
  5868. match.device = device;
  5869. match.app = app;
  5870. conn = queue_remove_if(app_connections,
  5871. match_connection_by_device_and_app, &match);
  5872. if (!conn)
  5873. return false;
  5874. destroy_connection(conn);
  5875. return true;
  5876. }
  5877. bool bt_gatt_add_autoconnect(unsigned int id, const bdaddr_t *addr)
  5878. {
  5879. struct gatt_device *dev;
  5880. struct gatt_app *app;
  5881. DBG("");
  5882. app = find_app_by_id(id);
  5883. if (!app) {
  5884. error("gatt: App ID=%d not found", id);
  5885. return false;
  5886. }
  5887. dev = find_device_by_addr(addr);
  5888. if (!dev) {
  5889. error("gatt: Device not found");
  5890. return false;
  5891. }
  5892. /* Take reference of device for auto connect purpose */
  5893. if (queue_isempty(dev->autoconnect_apps))
  5894. device_ref(dev);
  5895. if (!queue_find(dev->autoconnect_apps, NULL, INT_TO_PTR(id)))
  5896. return queue_push_head(dev->autoconnect_apps, INT_TO_PTR(id));
  5897. return true;
  5898. }
  5899. void bt_gatt_remove_autoconnect(unsigned int id, const bdaddr_t *addr)
  5900. {
  5901. struct gatt_device *dev;
  5902. DBG("");
  5903. dev = find_device_by_addr(addr);
  5904. if (!dev) {
  5905. error("gatt: Device not found");
  5906. return;
  5907. }
  5908. queue_remove(dev->autoconnect_apps, INT_TO_PTR(id));
  5909. if (queue_isempty(dev->autoconnect_apps))
  5910. remove_autoconnect_device(dev);
  5911. }