btpclient.c 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2011-2017 Intel Corporation. All rights reserved.
  7. *
  8. */
  9. #ifdef HAVE_CONFIG_H
  10. #include <config.h>
  11. #endif
  12. #include <stdio.h>
  13. #include <unistd.h>
  14. #include <stdlib.h>
  15. #include <assert.h>
  16. #include <getopt.h>
  17. #include <signal.h>
  18. #include <ell/ell.h>
  19. #include "lib/bluetooth.h"
  20. #include "src/shared/btp.h"
  21. #define AD_PATH "/org/bluez/advertising"
  22. #define AG_PATH "/org/bluez/agent"
  23. #define AD_IFACE "org.bluez.LEAdvertisement1"
  24. #define AG_IFACE "org.bluez.Agent1"
  25. /* List of assigned numbers for advetising data and scan response */
  26. #define AD_TYPE_FLAGS 0x01
  27. #define AD_TYPE_INCOMPLETE_UUID16_SERVICE_LIST 0x02
  28. #define AD_TYPE_SHORT_NAME 0x08
  29. #define AD_TYPE_TX_POWER 0x0a
  30. #define AD_TYPE_SOLICIT_UUID16_SERVICE_LIST 0x14
  31. #define AD_TYPE_SERVICE_DATA_UUID16 0x16
  32. #define AD_TYPE_APPEARANCE 0x19
  33. #define AD_TYPE_MANUFACTURER_DATA 0xff
  34. static void register_gap_service(void);
  35. static struct l_dbus *dbus;
  36. struct btp_adapter {
  37. struct l_dbus_proxy *proxy;
  38. struct l_dbus_proxy *ad_proxy;
  39. uint8_t index;
  40. uint32_t supported_settings;
  41. uint32_t current_settings;
  42. uint32_t default_settings;
  43. struct l_queue *devices;
  44. };
  45. struct btp_device {
  46. struct l_dbus_proxy *proxy;
  47. uint8_t address_type;
  48. bdaddr_t address;
  49. };
  50. static struct l_queue *adapters;
  51. static char *socket_path;
  52. static struct btp *btp;
  53. static bool gap_service_registered;
  54. struct ad_data {
  55. uint8_t data[25];
  56. uint8_t len;
  57. };
  58. struct service_data {
  59. char *uuid;
  60. struct ad_data data;
  61. };
  62. struct manufacturer_data {
  63. uint16_t id;
  64. struct ad_data data;
  65. };
  66. static struct ad {
  67. bool registered;
  68. char *type;
  69. char *local_name;
  70. uint16_t local_appearance;
  71. uint16_t duration;
  72. uint16_t timeout;
  73. struct l_queue *uuids;
  74. struct l_queue *services;
  75. struct l_queue *manufacturers;
  76. struct l_queue *solicits;
  77. bool tx_power;
  78. bool name;
  79. bool appearance;
  80. } ad;
  81. static struct btp_agent {
  82. bool registered;
  83. struct l_dbus_proxy *proxy;
  84. struct l_dbus_message *pending_req;
  85. } ag;
  86. static char *dupuuid2str(const uint8_t *uuid, uint8_t len)
  87. {
  88. switch (len) {
  89. case 16:
  90. return l_strdup_printf("%hhx%hhx", uuid[0], uuid[1]);
  91. case 128:
  92. return l_strdup_printf("%hhx%hhx%hhx%hhx%hhx%hhx%hhx%hhx%hhx"
  93. "%hhx%hhx%hhx%hhx%hhx%hhx%hhx", uuid[0],
  94. uuid[1], uuid[2], uuid[3], uuid[4],
  95. uuid[5], uuid[6], uuid[6], uuid[8],
  96. uuid[7], uuid[10], uuid[11], uuid[12],
  97. uuid[13], uuid[14], uuid[15]);
  98. default:
  99. return NULL;
  100. }
  101. }
  102. static bool match_dev_addr_type(const char *addr_type_str, uint8_t addr_type)
  103. {
  104. if (addr_type == BTP_GAP_ADDR_PUBLIC && strcmp(addr_type_str, "public"))
  105. return false;
  106. if (addr_type == BTP_GAP_ADDR_RANDOM && strcmp(addr_type_str, "random"))
  107. return false;
  108. return true;
  109. }
  110. static struct btp_adapter *find_adapter_by_proxy(struct l_dbus_proxy *proxy)
  111. {
  112. const struct l_queue_entry *entry;
  113. for (entry = l_queue_get_entries(adapters); entry;
  114. entry = entry->next) {
  115. struct btp_adapter *adapter = entry->data;
  116. if (adapter->proxy == proxy)
  117. return adapter;
  118. }
  119. return NULL;
  120. }
  121. static struct btp_adapter *find_adapter_by_index(uint8_t index)
  122. {
  123. const struct l_queue_entry *entry;
  124. for (entry = l_queue_get_entries(adapters); entry;
  125. entry = entry->next) {
  126. struct btp_adapter *adapter = entry->data;
  127. if (adapter->index == index)
  128. return adapter;
  129. }
  130. return NULL;
  131. }
  132. static struct btp_adapter *find_adapter_by_path(const char *path)
  133. {
  134. const struct l_queue_entry *entry;
  135. for (entry = l_queue_get_entries(adapters); entry;
  136. entry = entry->next) {
  137. struct btp_adapter *adapter = entry->data;
  138. if (!strcmp(l_dbus_proxy_get_path(adapter->proxy), path))
  139. return adapter;
  140. }
  141. return NULL;
  142. }
  143. static struct btp_device *find_device_by_address(struct btp_adapter *adapter,
  144. const bdaddr_t *addr,
  145. uint8_t addr_type)
  146. {
  147. const struct l_queue_entry *entry;
  148. const char *str;
  149. char addr_str[18];
  150. if (!ba2str(addr, addr_str))
  151. return NULL;
  152. for (entry = l_queue_get_entries(adapter->devices); entry;
  153. entry = entry->next) {
  154. struct btp_device *device = entry->data;
  155. l_dbus_proxy_get_property(device->proxy, "Address", "s", &str);
  156. if (strcmp(str, addr_str))
  157. continue;
  158. l_dbus_proxy_get_property(device->proxy, "AddressType", "s",
  159. &str);
  160. if (match_dev_addr_type(str, addr_type))
  161. return device;
  162. }
  163. return NULL;
  164. }
  165. static bool match_device_paths(const void *device, const void *path)
  166. {
  167. const struct btp_device *dev = device;
  168. return !strcmp(l_dbus_proxy_get_path(dev->proxy), path);
  169. }
  170. static struct btp_device *find_device_by_path(const char *path)
  171. {
  172. const struct l_queue_entry *entry;
  173. struct btp_device *device;
  174. for (entry = l_queue_get_entries(adapters); entry;
  175. entry = entry->next) {
  176. struct btp_adapter *adapter = entry->data;
  177. device = l_queue_find(adapter->devices, match_device_paths,
  178. path);
  179. if (device)
  180. return device;
  181. }
  182. return NULL;
  183. }
  184. static bool match_adapter_dev_proxy(const void *device, const void *proxy)
  185. {
  186. const struct btp_device *d = device;
  187. return d->proxy == proxy;
  188. }
  189. static bool match_adapter_dev(const void *device_a, const void *device_b)
  190. {
  191. return device_a == device_b;
  192. }
  193. static struct btp_adapter *find_adapter_by_device(struct btp_device *device)
  194. {
  195. const struct l_queue_entry *entry;
  196. for (entry = l_queue_get_entries(adapters); entry;
  197. entry = entry->next) {
  198. struct btp_adapter *adapter = entry->data;
  199. if (l_queue_find(adapter->devices, match_adapter_dev, device))
  200. return adapter;
  201. }
  202. return NULL;
  203. }
  204. static struct btp_device *find_device_by_proxy(struct l_dbus_proxy *proxy)
  205. {
  206. const struct l_queue_entry *entry;
  207. struct btp_device *device;
  208. for (entry = l_queue_get_entries(adapters); entry;
  209. entry = entry->next) {
  210. struct btp_adapter *adapter = entry->data;
  211. device = l_queue_find(adapter->devices, match_adapter_dev_proxy,
  212. proxy);
  213. if (device)
  214. return device;
  215. }
  216. return NULL;
  217. }
  218. static void btp_gap_read_commands(uint8_t index, const void *param,
  219. uint16_t length, void *user_data)
  220. {
  221. uint16_t commands = 0;
  222. if (index != BTP_INDEX_NON_CONTROLLER) {
  223. btp_send_error(btp, BTP_GAP_SERVICE, index,
  224. BTP_ERROR_INVALID_INDEX);
  225. return;
  226. }
  227. commands |= (1 << BTP_OP_GAP_READ_SUPPORTED_COMMANDS);
  228. commands |= (1 << BTP_OP_GAP_READ_CONTROLLER_INDEX_LIST);
  229. commands |= (1 << BTP_OP_GAP_READ_COTROLLER_INFO);
  230. commands |= (1 << BTP_OP_GAP_RESET);
  231. commands |= (1 << BTP_OP_GAP_SET_POWERED);
  232. commands |= (1 << BTP_OP_GAP_SET_CONNECTABLE);
  233. commands |= (1 << BTP_OP_GAP_SET_DISCOVERABLE);
  234. commands |= (1 << BTP_OP_GAP_SET_BONDABLE);
  235. commands |= (1 << BTP_OP_GAP_START_ADVERTISING);
  236. commands |= (1 << BTP_OP_GAP_STOP_ADVERTISING);
  237. commands |= (1 << BTP_OP_GAP_START_DISCOVERY);
  238. commands |= (1 << BTP_OP_GAP_STOP_DISCOVERY);
  239. commands |= (1 << BTP_OP_GAP_CONNECT);
  240. commands |= (1 << BTP_OP_GAP_DISCONNECT);
  241. commands |= (1 << BTP_OP_GAP_SET_IO_CAPA);
  242. commands |= (1 << BTP_OP_GAP_PAIR);
  243. commands |= (1 << BTP_OP_GAP_UNPAIR);
  244. commands = L_CPU_TO_LE16(commands);
  245. btp_send(btp, BTP_GAP_SERVICE, BTP_OP_GAP_READ_SUPPORTED_COMMANDS,
  246. BTP_INDEX_NON_CONTROLLER, sizeof(commands), &commands);
  247. }
  248. static void btp_gap_read_controller_index(uint8_t index, const void *param,
  249. uint16_t length, void *user_data)
  250. {
  251. const struct l_queue_entry *entry;
  252. struct btp_gap_read_index_rp *rp;
  253. uint8_t cnt;
  254. int i;
  255. if (index != BTP_INDEX_NON_CONTROLLER) {
  256. btp_send_error(btp, BTP_GAP_SERVICE, index,
  257. BTP_ERROR_INVALID_INDEX);
  258. return;
  259. }
  260. cnt = l_queue_length(adapters);
  261. rp = l_malloc(sizeof(*rp) + cnt);
  262. rp->num = cnt;
  263. for (i = 0, entry = l_queue_get_entries(adapters); entry;
  264. i++, entry = entry->next) {
  265. struct btp_adapter *adapter = entry->data;
  266. rp->indexes[i] = adapter->index;
  267. }
  268. btp_send(btp, BTP_GAP_SERVICE, BTP_OP_GAP_READ_CONTROLLER_INDEX_LIST,
  269. BTP_INDEX_NON_CONTROLLER, sizeof(*rp) + cnt, rp);
  270. }
  271. static void btp_gap_read_info(uint8_t index, const void *param, uint16_t length,
  272. void *user_data)
  273. {
  274. struct btp_adapter *adapter = find_adapter_by_index(index);
  275. struct btp_gap_read_info_rp rp;
  276. const char *str;
  277. uint8_t status = BTP_ERROR_FAIL;
  278. if (!adapter) {
  279. status = BTP_ERROR_INVALID_INDEX;
  280. goto failed;
  281. }
  282. memset(&rp, 0, sizeof(rp));
  283. if (!l_dbus_proxy_get_property(adapter->proxy, "Address", "s", &str))
  284. goto failed;
  285. if (str2ba(str, &rp.address) < 0)
  286. goto failed;
  287. if (!l_dbus_proxy_get_property(adapter->proxy, "Name", "s", &str)) {
  288. goto failed;
  289. }
  290. snprintf((char *)rp.name, sizeof(rp.name), "%s", str);
  291. snprintf((char *)rp.short_name, sizeof(rp.short_name), "%s", str);
  292. rp.supported_settings = L_CPU_TO_LE32(adapter->supported_settings);
  293. rp.current_settings = L_CPU_TO_LE32(adapter->current_settings);
  294. btp_send(btp, BTP_GAP_SERVICE, BTP_OP_GAP_READ_COTROLLER_INFO, index,
  295. sizeof(rp), &rp);
  296. return;
  297. failed:
  298. btp_send_error(btp, BTP_GAP_SERVICE, index, status);
  299. }
  300. static void remove_device_setup(struct l_dbus_message *message,
  301. void *user_data)
  302. {
  303. struct btp_device *device = user_data;
  304. l_dbus_message_set_arguments(message, "o",
  305. l_dbus_proxy_get_path(device->proxy));
  306. }
  307. static void remove_device_reply(struct l_dbus_proxy *proxy,
  308. struct l_dbus_message *result,
  309. void *user_data)
  310. {
  311. struct btp_device *device = user_data;
  312. struct btp_adapter *adapter = find_adapter_by_proxy(proxy);
  313. if (!adapter)
  314. return;
  315. if (l_dbus_message_is_error(result)) {
  316. const char *name;
  317. l_dbus_message_get_error(result, &name, NULL);
  318. l_error("Failed to remove device %s (%s)",
  319. l_dbus_proxy_get_path(device->proxy),
  320. name);
  321. return;
  322. }
  323. l_queue_remove(adapter->devices, device);
  324. }
  325. static void unreg_advertising_setup(struct l_dbus_message *message,
  326. void *user_data)
  327. {
  328. struct l_dbus_message_builder *builder;
  329. builder = l_dbus_message_builder_new(message);
  330. l_dbus_message_builder_append_basic(builder, 'o', AD_PATH);
  331. l_dbus_message_builder_finalize(builder);
  332. l_dbus_message_builder_destroy(builder);
  333. }
  334. static void ad_cleanup_service(void *service)
  335. {
  336. struct service_data *s = service;
  337. l_free(s->uuid);
  338. l_free(s);
  339. }
  340. static void ad_cleanup(void)
  341. {
  342. l_free(ad.local_name);
  343. l_queue_destroy(ad.uuids, l_free);
  344. l_queue_destroy(ad.services, ad_cleanup_service);
  345. l_queue_destroy(ad.manufacturers, l_free);
  346. l_queue_destroy(ad.solicits, l_free);
  347. memset(&ad, 0, sizeof(ad));
  348. }
  349. static void unreg_advertising_reply(struct l_dbus_proxy *proxy,
  350. struct l_dbus_message *result,
  351. void *user_data)
  352. {
  353. const char *path = l_dbus_proxy_get_path(proxy);
  354. struct btp_adapter *adapter = find_adapter_by_path(path);
  355. if (!adapter)
  356. return;
  357. if (l_dbus_message_is_error(result)) {
  358. const char *name;
  359. l_dbus_message_get_error(result, &name, NULL);
  360. l_error("Failed to stop advertising %s (%s)",
  361. l_dbus_proxy_get_path(proxy), name);
  362. return;
  363. }
  364. if (!l_dbus_object_remove_interface(dbus, AD_PATH, AD_IFACE))
  365. l_info("Unable to remove ad instance");
  366. if (!l_dbus_object_remove_interface(dbus, AD_PATH,
  367. L_DBUS_INTERFACE_PROPERTIES))
  368. l_info("Unable to remove propety instance");
  369. if (!l_dbus_unregister_interface(dbus, AD_IFACE))
  370. l_info("Unable to unregister ad interface");
  371. ad_cleanup();
  372. }
  373. static void unreg_agent_setup(struct l_dbus_message *message, void *user_data)
  374. {
  375. struct l_dbus_message_builder *builder;
  376. builder = l_dbus_message_builder_new(message);
  377. l_dbus_message_builder_append_basic(builder, 'o', AG_PATH);
  378. l_dbus_message_builder_finalize(builder);
  379. l_dbus_message_builder_destroy(builder);
  380. }
  381. static void reset_unreg_agent_reply(struct l_dbus_proxy *proxy,
  382. struct l_dbus_message *result,
  383. void *user_data)
  384. {
  385. if (l_dbus_message_is_error(result)) {
  386. const char *name;
  387. l_dbus_message_get_error(result, &name, NULL);
  388. l_error("Failed to unregister agent %s (%s)",
  389. l_dbus_proxy_get_path(proxy), name);
  390. return;
  391. }
  392. if (!l_dbus_object_remove_interface(dbus, AG_PATH,
  393. L_DBUS_INTERFACE_PROPERTIES))
  394. l_info("Unable to remove propety instance");
  395. if (!l_dbus_object_remove_interface(dbus, AG_PATH, AG_IFACE))
  396. l_info("Unable to remove agent instance");
  397. if (!l_dbus_unregister_interface(dbus, AG_IFACE))
  398. l_info("Unable to unregister agent interface");
  399. ag.registered = false;
  400. }
  401. static void update_current_settings(struct btp_adapter *adapter,
  402. uint32_t new_settings)
  403. {
  404. struct btp_new_settings_ev ev;
  405. adapter->current_settings = new_settings;
  406. ev.current_settings = L_CPU_TO_LE32(adapter->current_settings);
  407. btp_send(btp, BTP_GAP_SERVICE, BTP_EV_GAP_NEW_SETTINGS, adapter->index,
  408. sizeof(ev), &ev);
  409. }
  410. static void btp_gap_reset(uint8_t index, const void *param, uint16_t length,
  411. void *user_data)
  412. {
  413. struct btp_adapter *adapter = find_adapter_by_index(index);
  414. const struct l_queue_entry *entry;
  415. uint8_t status;
  416. bool prop;
  417. uint32_t default_settings;
  418. if (!adapter) {
  419. status = BTP_ERROR_INVALID_INDEX;
  420. goto failed;
  421. }
  422. /* Adapter needs to be powered to be able to remove devices */
  423. if (!l_dbus_proxy_get_property(adapter->proxy, "Powered", "b", &prop) ||
  424. !prop) {
  425. status = BTP_ERROR_FAIL;
  426. goto failed;
  427. }
  428. for (entry = l_queue_get_entries(adapter->devices); entry;
  429. entry = entry->next) {
  430. struct btp_device *device = entry->data;
  431. l_dbus_proxy_method_call(adapter->proxy, "RemoveDevice",
  432. remove_device_setup,
  433. remove_device_reply, device,
  434. NULL);
  435. }
  436. if (adapter->ad_proxy && ad.registered)
  437. if (!l_dbus_proxy_method_call(adapter->ad_proxy,
  438. "UnregisterAdvertisement",
  439. unreg_advertising_setup,
  440. unreg_advertising_reply,
  441. NULL, NULL)) {
  442. status = BTP_ERROR_FAIL;
  443. goto failed;
  444. }
  445. if (ag.proxy && ag.registered)
  446. if (!l_dbus_proxy_method_call(ag.proxy, "UnregisterAgent",
  447. unreg_agent_setup,
  448. reset_unreg_agent_reply,
  449. NULL, NULL)) {
  450. status = BTP_ERROR_FAIL;
  451. goto failed;
  452. }
  453. default_settings = adapter->default_settings;
  454. update_current_settings(adapter, default_settings);
  455. /* TODO for we assume all went well */
  456. btp_send(btp, BTP_GAP_SERVICE, BTP_OP_GAP_RESET, index,
  457. sizeof(default_settings), &default_settings);
  458. return;
  459. failed:
  460. btp_send_error(btp, BTP_GAP_SERVICE, index, status);
  461. }
  462. struct set_setting_data {
  463. struct btp_adapter *adapter;
  464. uint8_t opcode;
  465. uint32_t setting;
  466. bool value;
  467. };
  468. static void set_setting_reply(struct l_dbus_proxy *proxy,
  469. struct l_dbus_message *result, void *user_data)
  470. {
  471. struct set_setting_data *data = user_data;
  472. struct btp_adapter *adapter = data->adapter;
  473. uint32_t settings;
  474. if (l_dbus_message_is_error(result)) {
  475. btp_send_error(btp, BTP_GAP_SERVICE, data->adapter->index,
  476. BTP_ERROR_FAIL);
  477. return;
  478. }
  479. if (data->value)
  480. adapter->current_settings |= data->setting;
  481. else
  482. adapter->current_settings &= ~data->setting;
  483. settings = L_CPU_TO_LE32(adapter->current_settings);
  484. btp_send(btp, BTP_GAP_SERVICE, data->opcode, adapter->index,
  485. sizeof(settings), &settings);
  486. }
  487. static void btp_gap_set_powered(uint8_t index, const void *param,
  488. uint16_t length, void *user_data)
  489. {
  490. struct btp_adapter *adapter = find_adapter_by_index(index);
  491. const struct btp_gap_set_powered_cp *cp = param;
  492. uint8_t status = BTP_ERROR_FAIL;
  493. struct set_setting_data *data;
  494. if (length < sizeof(*cp))
  495. goto failed;
  496. if (!adapter) {
  497. status = BTP_ERROR_INVALID_INDEX;
  498. goto failed;
  499. }
  500. data = l_new(struct set_setting_data, 1);
  501. data->adapter = adapter;
  502. data->opcode = BTP_OP_GAP_SET_POWERED;
  503. data->setting = BTP_GAP_SETTING_POWERED;
  504. data->value = cp->powered;
  505. if (l_dbus_proxy_set_property(adapter->proxy, set_setting_reply,
  506. data, l_free, "Powered", "b",
  507. data->value))
  508. return;
  509. l_free(data);
  510. failed:
  511. btp_send_error(btp, BTP_GAP_SERVICE, index, status);
  512. }
  513. static void btp_gap_set_connectable(uint8_t index, const void *param,
  514. uint16_t length, void *user_data)
  515. {
  516. struct btp_adapter *adapter = find_adapter_by_index(index);
  517. const struct btp_gap_set_connectable_cp *cp = param;
  518. uint8_t status = BTP_ERROR_FAIL;
  519. uint32_t new_settings;
  520. if (length < sizeof(*cp))
  521. goto failed;
  522. if (!adapter) {
  523. status = BTP_ERROR_INVALID_INDEX;
  524. goto failed;
  525. }
  526. new_settings = adapter->current_settings;
  527. if (cp->connectable)
  528. new_settings |= BTP_GAP_SETTING_CONNECTABLE;
  529. else
  530. new_settings &= ~BTP_GAP_SETTING_CONNECTABLE;
  531. update_current_settings(adapter, new_settings);
  532. btp_send(btp, BTP_GAP_SERVICE, BTP_OP_GAP_SET_CONNECTABLE, index,
  533. sizeof(new_settings), &new_settings);
  534. return;
  535. failed:
  536. btp_send_error(btp, BTP_GAP_SERVICE, index, status);
  537. }
  538. static void btp_gap_set_discoverable(uint8_t index, const void *param,
  539. uint16_t length, void *user_data)
  540. {
  541. struct btp_adapter *adapter = find_adapter_by_index(index);
  542. const struct btp_gap_set_discoverable_cp *cp = param;
  543. uint8_t status = BTP_ERROR_FAIL;
  544. struct set_setting_data *data;
  545. if (length < sizeof(*cp))
  546. goto failed;
  547. if (!adapter) {
  548. status = BTP_ERROR_INVALID_INDEX;
  549. goto failed;
  550. }
  551. data = l_new(struct set_setting_data, 1);
  552. data->adapter = adapter;
  553. data->opcode = BTP_OP_GAP_SET_DISCOVERABLE;
  554. data->setting = BTP_GAP_SETTING_DISCOVERABLE;
  555. data->value = cp->discoverable;
  556. if (l_dbus_proxy_set_property(adapter->proxy, set_setting_reply,
  557. data, l_free, "Discoverable", "b",
  558. data->value))
  559. return;
  560. l_free(data);
  561. failed:
  562. btp_send_error(btp, BTP_GAP_SERVICE, index, status);
  563. }
  564. static void btp_gap_set_bondable(uint8_t index, const void *param,
  565. uint16_t length, void *user_data)
  566. {
  567. struct btp_adapter *adapter = find_adapter_by_index(index);
  568. const struct btp_gap_set_bondable_cp *cp = param;
  569. uint8_t status = BTP_ERROR_FAIL;
  570. struct set_setting_data *data;
  571. if (length < sizeof(*cp))
  572. goto failed;
  573. if (!adapter) {
  574. status = BTP_ERROR_INVALID_INDEX;
  575. goto failed;
  576. }
  577. data = l_new(struct set_setting_data, 1);
  578. data->adapter = adapter;
  579. data->opcode = BTP_OP_GAP_SET_BONDABLE;
  580. data->setting = BTP_GAP_SETTING_BONDABLE;
  581. data->value = cp->bondable;
  582. if (l_dbus_proxy_set_property(adapter->proxy, set_setting_reply,
  583. data, l_free, "Pairable", "b",
  584. data->value))
  585. return;
  586. l_free(data);
  587. failed:
  588. btp_send_error(btp, BTP_GAP_SERVICE, index, status);
  589. }
  590. static void ad_init(void)
  591. {
  592. ad.uuids = l_queue_new();
  593. ad.services = l_queue_new();
  594. ad.manufacturers = l_queue_new();
  595. ad.solicits = l_queue_new();
  596. ad.local_appearance = UINT16_MAX;
  597. }
  598. static struct l_dbus_message *ad_release_call(struct l_dbus *dbus,
  599. struct l_dbus_message *message,
  600. void *user_data)
  601. {
  602. struct l_dbus_message *reply;
  603. l_dbus_unregister_object(dbus, AD_PATH);
  604. l_dbus_unregister_interface(dbus, AD_IFACE);
  605. reply = l_dbus_message_new_method_return(message);
  606. l_dbus_message_set_arguments(reply, "");
  607. ad_cleanup();
  608. return reply;
  609. }
  610. static bool ad_type_getter(struct l_dbus *dbus, struct l_dbus_message *message,
  611. struct l_dbus_message_builder *builder,
  612. void *user_data)
  613. {
  614. l_dbus_message_builder_append_basic(builder, 's', ad.type);
  615. return true;
  616. }
  617. static bool ad_serviceuuids_getter(struct l_dbus *dbus,
  618. struct l_dbus_message *message,
  619. struct l_dbus_message_builder *builder,
  620. void *user_data)
  621. {
  622. const struct l_queue_entry *entry;
  623. if (l_queue_isempty(ad.uuids))
  624. return false;
  625. l_dbus_message_builder_enter_array(builder, "s");
  626. for (entry = l_queue_get_entries(ad.uuids); entry; entry = entry->next)
  627. l_dbus_message_builder_append_basic(builder, 's', entry->data);
  628. l_dbus_message_builder_leave_array(builder);
  629. return true;
  630. }
  631. static bool ad_servicedata_getter(struct l_dbus *dbus,
  632. struct l_dbus_message *message,
  633. struct l_dbus_message_builder *builder,
  634. void *user_data)
  635. {
  636. const struct l_queue_entry *entry;
  637. size_t i;
  638. if (l_queue_isempty(ad.services))
  639. return false;
  640. l_dbus_message_builder_enter_array(builder, "{sv}");
  641. for (entry = l_queue_get_entries(ad.services); entry;
  642. entry = entry->next) {
  643. struct service_data *sd = entry->data;
  644. l_dbus_message_builder_enter_dict(builder, "sv");
  645. l_dbus_message_builder_append_basic(builder, 's', sd->uuid);
  646. l_dbus_message_builder_enter_variant(builder, "ay");
  647. l_dbus_message_builder_enter_array(builder, "y");
  648. for (i = 0; i < sd->data.len; i++)
  649. l_dbus_message_builder_append_basic(builder, 'y',
  650. &(sd->data.data[i]));
  651. l_dbus_message_builder_leave_array(builder);
  652. l_dbus_message_builder_leave_variant(builder);
  653. l_dbus_message_builder_leave_dict(builder);
  654. }
  655. l_dbus_message_builder_leave_array(builder);
  656. return true;
  657. }
  658. static bool ad_manufacturerdata_getter(struct l_dbus *dbus,
  659. struct l_dbus_message *message,
  660. struct l_dbus_message_builder *builder,
  661. void *user_data)
  662. {
  663. const struct l_queue_entry *entry;
  664. size_t i;
  665. if (l_queue_isempty(ad.manufacturers))
  666. return false;
  667. l_dbus_message_builder_enter_array(builder, "{qv}");
  668. for (entry = l_queue_get_entries(ad.manufacturers); entry;
  669. entry = entry->next) {
  670. struct manufacturer_data *md = entry->data;
  671. l_dbus_message_builder_enter_dict(builder, "qv");
  672. l_dbus_message_builder_append_basic(builder, 'q', &md->id);
  673. l_dbus_message_builder_enter_variant(builder, "ay");
  674. l_dbus_message_builder_enter_array(builder, "y");
  675. for (i = 0; i < md->data.len; i++)
  676. l_dbus_message_builder_append_basic(builder, 'y',
  677. &(md->data.data[i]));
  678. l_dbus_message_builder_leave_array(builder);
  679. l_dbus_message_builder_leave_variant(builder);
  680. l_dbus_message_builder_leave_dict(builder);
  681. }
  682. l_dbus_message_builder_leave_array(builder);
  683. return true;
  684. }
  685. static bool ad_solicituuids_getter(struct l_dbus *dbus,
  686. struct l_dbus_message *message,
  687. struct l_dbus_message_builder *builder,
  688. void *user_data)
  689. {
  690. const struct l_queue_entry *entry;
  691. if (l_queue_isempty(ad.solicits))
  692. return false;
  693. l_dbus_message_builder_enter_array(builder, "s");
  694. for (entry = l_queue_get_entries(ad.solicits); entry;
  695. entry = entry->next)
  696. l_dbus_message_builder_append_basic(builder, 's', entry->data);
  697. l_dbus_message_builder_leave_array(builder);
  698. return true;
  699. }
  700. static bool ad_includes_getter(struct l_dbus *dbus,
  701. struct l_dbus_message *message,
  702. struct l_dbus_message_builder *builder,
  703. void *user_data)
  704. {
  705. l_dbus_message_builder_enter_array(builder, "s");
  706. if (!(ad.tx_power || ad.name || ad.appearance))
  707. return false;
  708. if (ad.tx_power) {
  709. const char *str = "tx-power";
  710. l_dbus_message_builder_append_basic(builder, 's', str);
  711. }
  712. if (ad.name) {
  713. const char *str = "local-name";
  714. l_dbus_message_builder_append_basic(builder, 's', str);
  715. }
  716. if (ad.appearance) {
  717. const char *str = "appearance";
  718. l_dbus_message_builder_append_basic(builder, 's', str);
  719. }
  720. l_dbus_message_builder_leave_array(builder);
  721. return true;
  722. }
  723. static bool ad_localname_getter(struct l_dbus *dbus,
  724. struct l_dbus_message *message,
  725. struct l_dbus_message_builder *builder,
  726. void *user_data)
  727. {
  728. if (!ad.local_name)
  729. return false;
  730. l_dbus_message_builder_append_basic(builder, 's', ad.local_name);
  731. return true;
  732. }
  733. static bool ad_appearance_getter(struct l_dbus *dbus,
  734. struct l_dbus_message *message,
  735. struct l_dbus_message_builder *builder,
  736. void *user_data)
  737. {
  738. if (!ad.local_appearance)
  739. return false;
  740. l_dbus_message_builder_append_basic(builder, 'q', &ad.local_appearance);
  741. return true;
  742. }
  743. static bool ad_duration_getter(struct l_dbus *dbus,
  744. struct l_dbus_message *message,
  745. struct l_dbus_message_builder *builder,
  746. void *user_data)
  747. {
  748. if (!ad.duration)
  749. return false;
  750. l_dbus_message_builder_append_basic(builder, 'q', &ad.duration);
  751. return true;
  752. }
  753. static bool ad_timeout_getter(struct l_dbus *dbus,
  754. struct l_dbus_message *message,
  755. struct l_dbus_message_builder *builder,
  756. void *user_data)
  757. {
  758. if (!ad.timeout)
  759. return false;
  760. l_dbus_message_builder_append_basic(builder, 'q', &ad.timeout);
  761. return true;
  762. }
  763. static void setup_ad_interface(struct l_dbus_interface *interface)
  764. {
  765. l_dbus_interface_method(interface, "Release",
  766. L_DBUS_METHOD_FLAG_NOREPLY,
  767. ad_release_call, "", "");
  768. l_dbus_interface_property(interface, "Type", 0, "s", ad_type_getter,
  769. NULL);
  770. l_dbus_interface_property(interface, "ServiceUUIDs", 0, "as",
  771. ad_serviceuuids_getter, NULL);
  772. l_dbus_interface_property(interface, "ServiceData", 0, "a{sv}",
  773. ad_servicedata_getter, NULL);
  774. l_dbus_interface_property(interface, "ManufacturerData", 0,
  775. "a{qv}", ad_manufacturerdata_getter,
  776. NULL);
  777. l_dbus_interface_property(interface, "SolicitUUIDs", 0, "as",
  778. ad_solicituuids_getter, NULL);
  779. l_dbus_interface_property(interface, "Includes", 0, "as",
  780. ad_includes_getter, NULL);
  781. l_dbus_interface_property(interface, "LocalName", 0, "s",
  782. ad_localname_getter, NULL);
  783. l_dbus_interface_property(interface, "Appearance", 0, "q",
  784. ad_appearance_getter, NULL);
  785. l_dbus_interface_property(interface, "Duration", 0, "q",
  786. ad_duration_getter, NULL);
  787. l_dbus_interface_property(interface, "Timeout", 0, "q",
  788. ad_timeout_getter, NULL);
  789. }
  790. static void start_advertising_reply(struct l_dbus_proxy *proxy,
  791. struct l_dbus_message *result,
  792. void *user_data)
  793. {
  794. const char *path = l_dbus_proxy_get_path(proxy);
  795. struct btp_adapter *adapter = find_adapter_by_path(path);
  796. uint32_t new_settings;
  797. if (!adapter) {
  798. btp_send_error(btp, BTP_GAP_SERVICE, BTP_INDEX_NON_CONTROLLER,
  799. BTP_ERROR_FAIL);
  800. return;
  801. }
  802. if (l_dbus_message_is_error(result)) {
  803. const char *name, *desc;
  804. l_dbus_message_get_error(result, &name, &desc);
  805. l_error("Failed to start advertising (%s), %s", name, desc);
  806. btp_send_error(btp, BTP_GAP_SERVICE, adapter->index,
  807. BTP_ERROR_FAIL);
  808. return;
  809. }
  810. new_settings = adapter->current_settings;
  811. new_settings |= BTP_GAP_SETTING_ADVERTISING;
  812. update_current_settings(adapter, new_settings);
  813. ad.registered = true;
  814. btp_send(btp, BTP_GAP_SERVICE, BTP_OP_GAP_START_ADVERTISING,
  815. adapter->index, sizeof(new_settings),
  816. &new_settings);
  817. }
  818. static void create_advertising_data(uint8_t adv_data_len, const uint8_t *data)
  819. {
  820. const uint8_t *ad_data;
  821. uint8_t ad_type, ad_len;
  822. uint8_t remaining_data_len = adv_data_len;
  823. while (remaining_data_len) {
  824. ad_type = data[adv_data_len - remaining_data_len];
  825. ad_len = data[adv_data_len - remaining_data_len + 1];
  826. ad_data = &data[adv_data_len - remaining_data_len + 2];
  827. switch (ad_type) {
  828. case AD_TYPE_INCOMPLETE_UUID16_SERVICE_LIST:
  829. {
  830. char *uuid = dupuuid2str(ad_data, 16);
  831. l_queue_push_tail(ad.uuids, uuid);
  832. break;
  833. }
  834. case AD_TYPE_SHORT_NAME:
  835. ad.local_name = malloc(ad_len + 1);
  836. memcpy(ad.local_name, ad_data, ad_len);
  837. ad.local_name[ad_len] = '\0';
  838. break;
  839. case AD_TYPE_TX_POWER:
  840. ad.tx_power = true;
  841. /* XXX Value is ommited cause, stack fills it */
  842. break;
  843. case AD_TYPE_SERVICE_DATA_UUID16:
  844. {
  845. struct service_data *sd;
  846. sd = l_new(struct service_data, 1);
  847. sd->uuid = dupuuid2str(ad_data, 16);
  848. sd->data.len = ad_len - 2;
  849. memcpy(sd->data.data, ad_data + 2, sd->data.len);
  850. l_queue_push_tail(ad.services, sd);
  851. break;
  852. }
  853. case AD_TYPE_APPEARANCE:
  854. memcpy(&ad.local_appearance, ad_data, ad_len);
  855. break;
  856. case AD_TYPE_MANUFACTURER_DATA:
  857. {
  858. struct manufacturer_data *md;
  859. md = l_new(struct manufacturer_data, 1);
  860. /* The first 2 octets contain the Company Identifier
  861. * Code followed by additional manufacturer specific
  862. * data.
  863. */
  864. memcpy(&md->id, ad_data, 2);
  865. md->data.len = ad_len - 2;
  866. memcpy(md->data.data, ad_data + 2, md->data.len);
  867. l_queue_push_tail(ad.manufacturers, md);
  868. break;
  869. }
  870. case AD_TYPE_SOLICIT_UUID16_SERVICE_LIST:
  871. {
  872. char *uuid = dupuuid2str(ad_data, 16);
  873. l_queue_push_tail(ad.solicits, uuid);
  874. break;
  875. }
  876. default:
  877. l_info("Unsupported advertising data type");
  878. break;
  879. }
  880. /* Advertising entity data len + advertising entity header
  881. * (type, len)
  882. */
  883. remaining_data_len -= ad_len + 2;
  884. }
  885. }
  886. static void create_scan_response(uint8_t scan_rsp_len, const uint8_t *data)
  887. {
  888. /* TODO */
  889. }
  890. static void start_advertising_setup(struct l_dbus_message *message,
  891. void *user_data)
  892. {
  893. struct l_dbus_message_builder *builder;
  894. builder = l_dbus_message_builder_new(message);
  895. l_dbus_message_builder_append_basic(builder, 'o', AD_PATH);
  896. l_dbus_message_builder_enter_array(builder, "{sv}");
  897. l_dbus_message_builder_enter_dict(builder, "sv");
  898. l_dbus_message_builder_leave_dict(builder);
  899. l_dbus_message_builder_leave_array(builder);
  900. l_dbus_message_builder_finalize(builder);
  901. l_dbus_message_builder_destroy(builder);
  902. }
  903. static void btp_gap_start_advertising(uint8_t index, const void *param,
  904. uint16_t length, void *user_data)
  905. {
  906. struct btp_adapter *adapter = find_adapter_by_index(index);
  907. const struct btp_gap_start_adv_cp *cp = param;
  908. uint8_t status = BTP_ERROR_FAIL;
  909. bool prop;
  910. if (!adapter) {
  911. status = BTP_ERROR_INVALID_INDEX;
  912. goto failed;
  913. }
  914. /* Adapter needs to be powered to be able to advertise */
  915. if (!l_dbus_proxy_get_property(adapter->proxy, "Powered", "b", &prop) ||
  916. !prop || ad.registered)
  917. goto failed;
  918. if (!l_dbus_register_interface(dbus, AD_IFACE, setup_ad_interface, NULL,
  919. false)) {
  920. l_info("Unable to register ad interface");
  921. goto failed;
  922. }
  923. if (!l_dbus_object_add_interface(dbus, AD_PATH, AD_IFACE, NULL)) {
  924. l_info("Unable to instantiate ad interface");
  925. if (!l_dbus_unregister_interface(dbus, AD_IFACE))
  926. l_info("Unable to unregister ad interface");
  927. goto failed;
  928. }
  929. if (!l_dbus_object_add_interface(dbus, AD_PATH,
  930. L_DBUS_INTERFACE_PROPERTIES,
  931. NULL)) {
  932. l_info("Unable to instantiate the properties interface");
  933. if (!l_dbus_object_remove_interface(dbus, AD_PATH, AD_IFACE))
  934. l_info("Unable to remove ad instance");
  935. if (!l_dbus_unregister_interface(dbus, AD_IFACE))
  936. l_info("Unable to unregister ad interface");
  937. goto failed;
  938. }
  939. ad_init();
  940. if (adapter->current_settings & BTP_GAP_SETTING_CONNECTABLE)
  941. ad.type = "peripheral";
  942. else
  943. ad.type = "broadcast";
  944. if (cp->adv_data_len > 0)
  945. create_advertising_data(cp->adv_data_len, cp->data);
  946. if (cp->scan_rsp_len > 0)
  947. create_scan_response(cp->scan_rsp_len,
  948. cp->data + cp->scan_rsp_len);
  949. if (!l_dbus_proxy_method_call(adapter->ad_proxy,
  950. "RegisterAdvertisement",
  951. start_advertising_setup,
  952. start_advertising_reply,
  953. NULL, NULL)) {
  954. if (!l_dbus_object_remove_interface(dbus, AD_PATH, AD_IFACE))
  955. l_info("Unable to remove ad instance");
  956. if (!l_dbus_unregister_interface(dbus, AD_IFACE))
  957. l_info("Unable to unregister ad interface");
  958. goto failed;
  959. }
  960. return;
  961. failed:
  962. btp_send_error(btp, BTP_GAP_SERVICE, index, status);
  963. }
  964. static void stop_advertising_reply(struct l_dbus_proxy *proxy,
  965. struct l_dbus_message *result,
  966. void *user_data)
  967. {
  968. const char *path = l_dbus_proxy_get_path(proxy);
  969. struct btp_adapter *adapter = find_adapter_by_path(path);
  970. uint32_t new_settings;
  971. if (!adapter)
  972. return;
  973. if (l_dbus_message_is_error(result)) {
  974. const char *name;
  975. l_dbus_message_get_error(result, &name, NULL);
  976. l_error("Failed to stop advertising %s (%s)",
  977. l_dbus_proxy_get_path(proxy), name);
  978. return;
  979. }
  980. if (!l_dbus_object_remove_interface(dbus, AD_PATH, AD_IFACE))
  981. l_info("Unable to remove ad instance");
  982. if (!l_dbus_object_remove_interface(dbus, AD_PATH,
  983. L_DBUS_INTERFACE_PROPERTIES))
  984. l_info("Unable to remove propety instance");
  985. if (!l_dbus_unregister_interface(dbus, AD_IFACE))
  986. l_info("Unable to unregister ad interface");
  987. new_settings = adapter->current_settings;
  988. new_settings &= ~BTP_GAP_SETTING_ADVERTISING;
  989. update_current_settings(adapter, new_settings);
  990. ad_cleanup();
  991. btp_send(btp, BTP_GAP_SERVICE, BTP_OP_GAP_STOP_ADVERTISING,
  992. adapter->index, sizeof(new_settings),
  993. &new_settings);
  994. }
  995. static void btp_gap_stop_advertising(uint8_t index, const void *param,
  996. uint16_t length, void *user_data)
  997. {
  998. struct btp_adapter *adapter = find_adapter_by_index(index);
  999. uint8_t status = BTP_ERROR_FAIL;
  1000. bool prop;
  1001. if (!adapter) {
  1002. status = BTP_ERROR_INVALID_INDEX;
  1003. goto failed;
  1004. }
  1005. if (!l_dbus_proxy_get_property(adapter->proxy, "Powered", "b", &prop) ||
  1006. !prop || !adapter->ad_proxy || !ad.registered)
  1007. goto failed;
  1008. if (!l_dbus_proxy_method_call(adapter->ad_proxy,
  1009. "UnregisterAdvertisement",
  1010. unreg_advertising_setup,
  1011. stop_advertising_reply,
  1012. NULL, NULL))
  1013. goto failed;
  1014. return;
  1015. failed:
  1016. btp_send_error(btp, BTP_GAP_SERVICE, index, status);
  1017. }
  1018. static void start_discovery_reply(struct l_dbus_proxy *proxy,
  1019. struct l_dbus_message *result,
  1020. void *user_data)
  1021. {
  1022. struct btp_adapter *adapter = find_adapter_by_proxy(proxy);
  1023. if (!adapter) {
  1024. btp_send_error(btp, BTP_GAP_SERVICE, BTP_INDEX_NON_CONTROLLER,
  1025. BTP_ERROR_FAIL);
  1026. return;
  1027. }
  1028. if (l_dbus_message_is_error(result)) {
  1029. const char *name, *desc;
  1030. l_dbus_message_get_error(result, &name, &desc);
  1031. l_error("Failed to start discovery (%s), %s", name, desc);
  1032. btp_send_error(btp, BTP_GAP_SERVICE, adapter->index,
  1033. BTP_ERROR_FAIL);
  1034. return;
  1035. }
  1036. btp_send(btp, BTP_GAP_SERVICE, BTP_OP_GAP_START_DISCOVERY,
  1037. adapter->index, 0, NULL);
  1038. }
  1039. static void set_discovery_filter_setup(struct l_dbus_message *message,
  1040. void *user_data)
  1041. {
  1042. uint8_t flags = L_PTR_TO_UINT(user_data);
  1043. struct l_dbus_message_builder *builder;
  1044. if (!(flags & (BTP_GAP_DISCOVERY_FLAG_LE |
  1045. BTP_GAP_DISCOVERY_FLAG_BREDR))) {
  1046. l_info("Failed to start discovery - no transport set");
  1047. return;
  1048. }
  1049. builder = l_dbus_message_builder_new(message);
  1050. l_dbus_message_builder_enter_array(builder, "{sv}");
  1051. l_dbus_message_builder_enter_dict(builder, "sv");
  1052. /* Be in observer mode or in general mode (default in Bluez) */
  1053. if (flags & BTP_GAP_DISCOVERY_FLAG_OBSERVATION) {
  1054. l_dbus_message_builder_append_basic(builder, 's', "Transport");
  1055. l_dbus_message_builder_enter_variant(builder, "s");
  1056. if (flags & (BTP_GAP_DISCOVERY_FLAG_LE |
  1057. BTP_GAP_DISCOVERY_FLAG_BREDR))
  1058. l_dbus_message_builder_append_basic(builder, 's',
  1059. "auto");
  1060. else if (flags & BTP_GAP_DISCOVERY_FLAG_LE)
  1061. l_dbus_message_builder_append_basic(builder, 's', "le");
  1062. else if (flags & BTP_GAP_DISCOVERY_FLAG_BREDR)
  1063. l_dbus_message_builder_append_basic(builder, 's',
  1064. "bredr");
  1065. l_dbus_message_builder_leave_variant(builder);
  1066. }
  1067. l_dbus_message_builder_leave_dict(builder);
  1068. l_dbus_message_builder_leave_array(builder);
  1069. /* TODO add passive, limited discovery */
  1070. l_dbus_message_builder_finalize(builder);
  1071. l_dbus_message_builder_destroy(builder);
  1072. }
  1073. static void set_discovery_filter_reply(struct l_dbus_proxy *proxy,
  1074. struct l_dbus_message *result,
  1075. void *user_data)
  1076. {
  1077. struct btp_adapter *adapter = find_adapter_by_proxy(proxy);
  1078. if (!adapter) {
  1079. btp_send_error(btp, BTP_GAP_SERVICE, BTP_INDEX_NON_CONTROLLER,
  1080. BTP_ERROR_FAIL);
  1081. return;
  1082. }
  1083. if (l_dbus_message_is_error(result)) {
  1084. const char *name, *desc;
  1085. l_dbus_message_get_error(result, &name, &desc);
  1086. l_error("Failed to set discovery filter (%s), %s", name, desc);
  1087. btp_send_error(btp, BTP_GAP_SERVICE, adapter->index,
  1088. BTP_ERROR_FAIL);
  1089. return;
  1090. }
  1091. l_dbus_proxy_method_call(adapter->proxy, "StartDiscovery", NULL,
  1092. start_discovery_reply, NULL, NULL);
  1093. }
  1094. static void btp_gap_start_discovery(uint8_t index, const void *param,
  1095. uint16_t length, void *user_data)
  1096. {
  1097. struct btp_adapter *adapter = find_adapter_by_index(index);
  1098. const struct btp_gap_start_discovery_cp *cp = param;
  1099. bool prop;
  1100. if (!adapter) {
  1101. btp_send_error(btp, BTP_GAP_SERVICE, index,
  1102. BTP_ERROR_INVALID_INDEX);
  1103. return;
  1104. }
  1105. /* Adapter needs to be powered to start discovery */
  1106. if (!l_dbus_proxy_get_property(adapter->proxy, "Powered", "b", &prop) ||
  1107. !prop) {
  1108. btp_send_error(btp, BTP_GAP_SERVICE, index, BTP_ERROR_FAIL);
  1109. return;
  1110. }
  1111. l_dbus_proxy_method_call(adapter->proxy, "SetDiscoveryFilter",
  1112. set_discovery_filter_setup,
  1113. set_discovery_filter_reply,
  1114. L_UINT_TO_PTR(cp->flags), NULL);
  1115. }
  1116. static void clear_discovery_filter_setup(struct l_dbus_message *message,
  1117. void *user_data)
  1118. {
  1119. struct l_dbus_message_builder *builder;
  1120. builder = l_dbus_message_builder_new(message);
  1121. /* Clear discovery filter setup */
  1122. l_dbus_message_builder_enter_array(builder, "{sv}");
  1123. l_dbus_message_builder_enter_dict(builder, "sv");
  1124. l_dbus_message_builder_leave_dict(builder);
  1125. l_dbus_message_builder_leave_array(builder);
  1126. l_dbus_message_builder_finalize(builder);
  1127. l_dbus_message_builder_destroy(builder);
  1128. }
  1129. static void clear_discovery_filter_reaply(struct l_dbus_proxy *proxy,
  1130. struct l_dbus_message *result,
  1131. void *user_data)
  1132. {
  1133. struct btp_adapter *adapter = find_adapter_by_proxy(proxy);
  1134. if (!adapter) {
  1135. btp_send_error(btp, BTP_GAP_SERVICE, BTP_INDEX_NON_CONTROLLER,
  1136. BTP_ERROR_FAIL);
  1137. return;
  1138. }
  1139. if (l_dbus_message_is_error(result)) {
  1140. const char *name, *desc;
  1141. l_dbus_message_get_error(result, &name, &desc);
  1142. l_error("Failed to set discovery filter (%s), %s", name, desc);
  1143. btp_send_error(btp, BTP_GAP_SERVICE, adapter->index,
  1144. BTP_ERROR_FAIL);
  1145. return;
  1146. }
  1147. btp_send(btp, BTP_GAP_SERVICE, BTP_OP_GAP_STOP_DISCOVERY,
  1148. adapter->index, 0, NULL);
  1149. }
  1150. static void stop_discovery_reply(struct l_dbus_proxy *proxy,
  1151. struct l_dbus_message *result,
  1152. void *user_data)
  1153. {
  1154. struct btp_adapter *adapter = find_adapter_by_proxy(proxy);
  1155. if (!adapter) {
  1156. btp_send_error(btp, BTP_GAP_SERVICE, BTP_INDEX_NON_CONTROLLER,
  1157. BTP_ERROR_FAIL);
  1158. return;
  1159. }
  1160. if (l_dbus_message_is_error(result)) {
  1161. const char *name;
  1162. l_dbus_message_get_error(result, &name, NULL);
  1163. l_error("Failed to stop discovery (%s)", name);
  1164. btp_send_error(btp, BTP_GAP_SERVICE, adapter->index,
  1165. BTP_ERROR_FAIL);
  1166. return;
  1167. }
  1168. l_dbus_proxy_method_call(adapter->proxy, "SetDiscoveryFilter",
  1169. clear_discovery_filter_setup,
  1170. clear_discovery_filter_reaply,
  1171. NULL, NULL);
  1172. }
  1173. static void btp_gap_stop_discovery(uint8_t index, const void *param,
  1174. uint16_t length, void *user_data)
  1175. {
  1176. struct btp_adapter *adapter = find_adapter_by_index(index);
  1177. bool prop;
  1178. if (!adapter) {
  1179. btp_send_error(btp, BTP_GAP_SERVICE, index,
  1180. BTP_ERROR_INVALID_INDEX);
  1181. return;
  1182. }
  1183. /* Adapter needs to be powered to be able to remove devices */
  1184. if (!l_dbus_proxy_get_property(adapter->proxy, "Powered", "b", &prop) ||
  1185. !prop) {
  1186. btp_send_error(btp, BTP_GAP_SERVICE, index, BTP_ERROR_FAIL);
  1187. return;
  1188. }
  1189. l_dbus_proxy_method_call(adapter->proxy, "StopDiscovery", NULL,
  1190. stop_discovery_reply, NULL, NULL);
  1191. }
  1192. static void connect_reply(struct l_dbus_proxy *proxy,
  1193. struct l_dbus_message *result, void *user_data)
  1194. {
  1195. uint8_t adapter_index = L_PTR_TO_UINT(user_data);
  1196. struct btp_adapter *adapter = find_adapter_by_index(adapter_index);
  1197. if (!adapter) {
  1198. btp_send_error(btp, BTP_GAP_SERVICE, BTP_INDEX_NON_CONTROLLER,
  1199. BTP_ERROR_FAIL);
  1200. return;
  1201. }
  1202. if (l_dbus_message_is_error(result)) {
  1203. const char *name, *desc;
  1204. l_dbus_message_get_error(result, &name, &desc);
  1205. l_error("Failed to connect (%s), %s", name, desc);
  1206. btp_send_error(btp, BTP_GAP_SERVICE, adapter_index,
  1207. BTP_ERROR_FAIL);
  1208. return;
  1209. }
  1210. btp_send(btp, BTP_GAP_SERVICE, BTP_OP_GAP_CONNECT, adapter_index, 0,
  1211. NULL);
  1212. }
  1213. struct connect_device_data {
  1214. bdaddr_t addr;
  1215. uint8_t addr_type;
  1216. };
  1217. static void connect_device_destroy(void *connect_device_data)
  1218. {
  1219. l_free(connect_device_data);
  1220. }
  1221. static void connect_device_reply(struct l_dbus_proxy *proxy,
  1222. struct l_dbus_message *result,
  1223. void *user_data)
  1224. {
  1225. struct btp_adapter *adapter = find_adapter_by_proxy(proxy);
  1226. if (!adapter) {
  1227. btp_send_error(btp, BTP_GAP_SERVICE, BTP_INDEX_NON_CONTROLLER,
  1228. BTP_ERROR_FAIL);
  1229. return;
  1230. }
  1231. if (l_dbus_message_is_error(result)) {
  1232. const char *name, *desc;
  1233. l_dbus_message_get_error(result, &name, &desc);
  1234. l_error("Failed to connect device (%s), %s", name, desc);
  1235. return;
  1236. }
  1237. }
  1238. static void connect_device_setup(struct l_dbus_message *message,
  1239. void *user_data)
  1240. {
  1241. struct connect_device_data *cdd = user_data;
  1242. struct l_dbus_message_builder *builder;
  1243. char str_addr[18];
  1244. ba2str(&cdd->addr, str_addr);
  1245. builder = l_dbus_message_builder_new(message);
  1246. l_dbus_message_builder_enter_array(builder, "{sv}");
  1247. l_dbus_message_builder_enter_dict(builder, "sv");
  1248. l_dbus_message_builder_append_basic(builder, 's', "Address");
  1249. l_dbus_message_builder_enter_variant(builder, "s");
  1250. l_dbus_message_builder_append_basic(builder, 's', str_addr);
  1251. l_dbus_message_builder_leave_variant(builder);
  1252. l_dbus_message_builder_leave_dict(builder);
  1253. l_dbus_message_builder_enter_dict(builder, "sv");
  1254. l_dbus_message_builder_append_basic(builder, 's', "AddressType");
  1255. l_dbus_message_builder_enter_variant(builder, "s");
  1256. if (cdd->addr_type == BTP_GAP_ADDR_RANDOM)
  1257. l_dbus_message_builder_append_basic(builder, 's', "random");
  1258. else
  1259. l_dbus_message_builder_append_basic(builder, 's', "public");
  1260. l_dbus_message_builder_leave_variant(builder);
  1261. l_dbus_message_builder_leave_dict(builder);
  1262. l_dbus_message_builder_leave_array(builder);
  1263. l_dbus_message_builder_finalize(builder);
  1264. l_dbus_message_builder_destroy(builder);
  1265. }
  1266. static void btp_gap_connect(uint8_t index, const void *param, uint16_t length,
  1267. void *user_data)
  1268. {
  1269. struct btp_adapter *adapter = find_adapter_by_index(index);
  1270. const struct btp_gap_connect_cp *cp = param;
  1271. struct btp_device *device;
  1272. struct connect_device_data *cdd;
  1273. bool prop;
  1274. uint8_t status = BTP_ERROR_FAIL;
  1275. if (!adapter) {
  1276. status = BTP_ERROR_INVALID_INDEX;
  1277. goto failed;
  1278. }
  1279. /* Adapter needs to be powered to be able to connect */
  1280. if (!l_dbus_proxy_get_property(adapter->proxy, "Powered", "b", &prop) ||
  1281. !prop)
  1282. goto failed;
  1283. device = find_device_by_address(adapter, &cp->address,
  1284. cp->address_type);
  1285. if (!device) {
  1286. cdd = l_new(struct connect_device_data, 1);
  1287. memcpy(&cdd->addr, &cp->address, sizeof(cdd->addr));
  1288. cdd->addr_type = cp->address_type;
  1289. l_dbus_proxy_method_call(adapter->proxy, "ConnectDevice",
  1290. connect_device_setup,
  1291. connect_device_reply,
  1292. cdd,
  1293. connect_device_destroy);
  1294. btp_send(btp, BTP_GAP_SERVICE, BTP_OP_GAP_CONNECT,
  1295. adapter->index, 0, NULL);
  1296. return;
  1297. }
  1298. l_dbus_proxy_method_call(device->proxy, "Connect", NULL, connect_reply,
  1299. L_UINT_TO_PTR(adapter->index), NULL);
  1300. return;
  1301. failed:
  1302. btp_send_error(btp, BTP_GAP_SERVICE, index, status);
  1303. }
  1304. static void disconnect_reply(struct l_dbus_proxy *proxy,
  1305. struct l_dbus_message *result, void *user_data)
  1306. {
  1307. uint8_t adapter_index = L_PTR_TO_UINT(user_data);
  1308. struct btp_adapter *adapter = find_adapter_by_index(adapter_index);
  1309. if (!adapter) {
  1310. btp_send_error(btp, BTP_GAP_SERVICE, BTP_INDEX_NON_CONTROLLER,
  1311. BTP_ERROR_FAIL);
  1312. return;
  1313. }
  1314. if (l_dbus_message_is_error(result)) {
  1315. const char *name, *desc;
  1316. l_dbus_message_get_error(result, &name, &desc);
  1317. l_error("Failed to disconnect (%s), %s", name, desc);
  1318. btp_send_error(btp, BTP_GAP_SERVICE, adapter_index,
  1319. BTP_ERROR_FAIL);
  1320. return;
  1321. }
  1322. btp_send(btp, BTP_GAP_SERVICE, BTP_OP_GAP_DISCONNECT, adapter_index, 0,
  1323. NULL);
  1324. }
  1325. static void btp_gap_disconnect(uint8_t index, const void *param,
  1326. uint16_t length, void *user_data)
  1327. {
  1328. struct btp_adapter *adapter = find_adapter_by_index(index);
  1329. const struct btp_gap_disconnect_cp *cp = param;
  1330. uint8_t status = BTP_ERROR_FAIL;
  1331. struct btp_device *device;
  1332. bool prop;
  1333. if (!adapter) {
  1334. status = BTP_ERROR_INVALID_INDEX;
  1335. goto failed;
  1336. }
  1337. /* Adapter needs to be powered to be able to connect */
  1338. if (!l_dbus_proxy_get_property(adapter->proxy, "Powered", "b", &prop) ||
  1339. !prop)
  1340. goto failed;
  1341. device = find_device_by_address(adapter, &cp->address,
  1342. cp->address_type);
  1343. if (!device)
  1344. goto failed;
  1345. l_dbus_proxy_method_call(device->proxy, "Disconnect", NULL,
  1346. disconnect_reply,
  1347. L_UINT_TO_PTR(adapter->index), NULL);
  1348. return;
  1349. failed:
  1350. btp_send_error(btp, BTP_GAP_SERVICE, index, status);
  1351. }
  1352. static struct l_dbus_message *ag_release_call(struct l_dbus *dbus,
  1353. struct l_dbus_message *message,
  1354. void *user_data)
  1355. {
  1356. struct l_dbus_message *reply;
  1357. reply = l_dbus_message_new_method_return(message);
  1358. l_dbus_message_set_arguments(reply, "");
  1359. return reply;
  1360. }
  1361. static struct l_dbus_message *ag_request_passkey_call(struct l_dbus *dbus,
  1362. struct l_dbus_message *message,
  1363. void *user_data)
  1364. {
  1365. struct btp_gap_passkey_req_ev ev;
  1366. struct btp_device *device;
  1367. struct btp_adapter *adapter;
  1368. const char *path, *str_addr, *str_addr_type;
  1369. l_dbus_message_get_arguments(message, "o", &path);
  1370. device = find_device_by_path(path);
  1371. if (!l_dbus_proxy_get_property(device->proxy, "Address", "s", &str_addr)
  1372. || !l_dbus_proxy_get_property(device->proxy, "AddressType", "s",
  1373. &str_addr_type)) {
  1374. l_info("Cannot get device properties");
  1375. return NULL;
  1376. }
  1377. ev.address_type = strcmp(str_addr_type, "public") ?
  1378. BTP_GAP_ADDR_RANDOM :
  1379. BTP_GAP_ADDR_PUBLIC;
  1380. if (!str2ba(str_addr, &ev.address))
  1381. return NULL;
  1382. adapter = find_adapter_by_device(device);
  1383. ag.pending_req = l_dbus_message_ref(message);
  1384. btp_send(btp, BTP_GAP_SERVICE, BTP_EV_GAP_PASSKEY_REQUEST,
  1385. adapter->index, sizeof(ev), &ev);
  1386. return NULL;
  1387. }
  1388. static struct l_dbus_message *ag_display_passkey_call(struct l_dbus *dbus,
  1389. struct l_dbus_message *message,
  1390. void *user_data)
  1391. {
  1392. struct btp_gap_passkey_display_ev ev;
  1393. struct btp_device *device;
  1394. struct btp_adapter *adapter;
  1395. struct l_dbus_message *reply;
  1396. const char *path, *str_addr, *str_addr_type;
  1397. uint32_t passkey;
  1398. uint16_t entered;
  1399. reply = l_dbus_message_new_method_return(message);
  1400. l_dbus_message_set_arguments(reply, "");
  1401. l_dbus_message_get_arguments(message, "ouq", &path, &passkey, &entered);
  1402. device = find_device_by_path(path);
  1403. if (!l_dbus_proxy_get_property(device->proxy, "Address", "s", &str_addr)
  1404. || !l_dbus_proxy_get_property(device->proxy, "AddressType", "s",
  1405. &str_addr_type)) {
  1406. l_info("Cannot get device properties");
  1407. return reply;
  1408. }
  1409. ev.passkey = L_CPU_TO_LE32(passkey);
  1410. ev.address_type = strcmp(str_addr_type, "public") ?
  1411. BTP_GAP_ADDR_RANDOM :
  1412. BTP_GAP_ADDR_PUBLIC;
  1413. if (str2ba(str_addr, &ev.address) < 0) {
  1414. l_info("Incorrect device addres");
  1415. return reply;
  1416. }
  1417. adapter = find_adapter_by_device(device);
  1418. btp_send(btp, BTP_GAP_SERVICE, BTP_EV_GAP_PASSKEY_DISPLAY,
  1419. adapter->index, sizeof(ev), &ev);
  1420. return reply;
  1421. }
  1422. static struct l_dbus_message *ag_request_confirmation_call(struct l_dbus *dbus,
  1423. struct l_dbus_message *message,
  1424. void *user_data)
  1425. {
  1426. struct btp_gap_passkey_confirm_ev ev;
  1427. struct btp_device *device;
  1428. struct btp_adapter *adapter;
  1429. const char *path, *str_addr, *str_addr_type;
  1430. uint32_t passkey;
  1431. l_dbus_message_get_arguments(message, "ou", &path, &passkey);
  1432. device = find_device_by_path(path);
  1433. if (!l_dbus_proxy_get_property(device->proxy, "Address", "s", &str_addr)
  1434. || !l_dbus_proxy_get_property(device->proxy, "AddressType", "s",
  1435. &str_addr_type)) {
  1436. l_info("Cannot get device properties");
  1437. return NULL;
  1438. }
  1439. ev.passkey = L_CPU_TO_LE32(passkey);
  1440. ev.address_type = strcmp(str_addr_type, "public") ?
  1441. BTP_GAP_ADDR_RANDOM :
  1442. BTP_GAP_ADDR_PUBLIC;
  1443. if (str2ba(str_addr, &ev.address) < 0) {
  1444. l_info("Incorrect device address");
  1445. return NULL;
  1446. }
  1447. adapter = find_adapter_by_device(device);
  1448. ag.pending_req = l_dbus_message_ref(message);
  1449. btp_send(btp, BTP_GAP_SERVICE, BTP_EV_GAP_PASSKEY_CONFIRM,
  1450. adapter->index, sizeof(ev), &ev);
  1451. return NULL;
  1452. }
  1453. static struct l_dbus_message *ag_request_authorization_call(struct l_dbus *dbus,
  1454. struct l_dbus_message *message,
  1455. void *user_data)
  1456. {
  1457. struct l_dbus_message *reply;
  1458. reply = l_dbus_message_new_method_return(message);
  1459. l_dbus_message_set_arguments(reply, "");
  1460. return reply;
  1461. }
  1462. static struct l_dbus_message *ag_authorize_service_call(struct l_dbus *dbus,
  1463. struct l_dbus_message *message,
  1464. void *user_data)
  1465. {
  1466. struct l_dbus_message *reply;
  1467. reply = l_dbus_message_new_method_return(message);
  1468. l_dbus_message_set_arguments(reply, "");
  1469. return reply;
  1470. }
  1471. static struct l_dbus_message *ag_cancel_call(struct l_dbus *dbus,
  1472. struct l_dbus_message *message,
  1473. void *user_data)
  1474. {
  1475. struct l_dbus_message *reply;
  1476. reply = l_dbus_message_new_method_return(message);
  1477. l_dbus_message_set_arguments(reply, "");
  1478. return reply;
  1479. }
  1480. static void setup_ag_interface(struct l_dbus_interface *iface)
  1481. {
  1482. l_dbus_interface_method(iface, "Release", 0, ag_release_call, "", "");
  1483. l_dbus_interface_method(iface, "RequestPasskey", 0,
  1484. ag_request_passkey_call, "u", "o",
  1485. "passkey", "device");
  1486. l_dbus_interface_method(iface, "DisplayPasskey", 0,
  1487. ag_display_passkey_call, "", "ouq",
  1488. "device", "passkey", "entered");
  1489. l_dbus_interface_method(iface, "RequestConfirmation", 0,
  1490. ag_request_confirmation_call, "", "ou",
  1491. "device", "passkey");
  1492. l_dbus_interface_method(iface, "RequestAuthorization", 0,
  1493. ag_request_authorization_call, "", "o",
  1494. "device");
  1495. l_dbus_interface_method(iface, "AuthorizeService", 0,
  1496. ag_authorize_service_call, "", "os",
  1497. "device", "uuid");
  1498. l_dbus_interface_method(iface, "Cancel", 0, ag_cancel_call, "", "");
  1499. }
  1500. struct set_io_capabilities_data {
  1501. uint8_t capa;
  1502. struct btp_adapter *adapter;
  1503. };
  1504. static void set_io_capabilities_setup(struct l_dbus_message *message,
  1505. void *user_data)
  1506. {
  1507. struct set_io_capabilities_data *sicd = user_data;
  1508. struct l_dbus_message_builder *builder;
  1509. char *capa_str;
  1510. builder = l_dbus_message_builder_new(message);
  1511. l_dbus_message_builder_append_basic(builder, 'o', AG_PATH);
  1512. switch (sicd->capa) {
  1513. case BTP_GAP_IOCAPA_DISPLAY_ONLY:
  1514. capa_str = "DisplayOnly";
  1515. break;
  1516. case BTP_GAP_IOCAPA_DISPLAY_YESNO:
  1517. capa_str = "DisplayYesNo";
  1518. break;
  1519. case BTP_GAP_IOCAPA_KEYBOARD_ONLY:
  1520. capa_str = "KeyboardOnly";
  1521. break;
  1522. case BTP_GAP_IOCAPA_KEYBOARD_DISPLAY:
  1523. capa_str = "KeyboardDisplay";
  1524. break;
  1525. case BTP_GAP_IOCAPA_NO_INPUT_NO_OUTPUT:
  1526. default:
  1527. capa_str = "NoInputNoOutput";
  1528. break;
  1529. }
  1530. l_dbus_message_builder_append_basic(builder, 's', capa_str);
  1531. l_dbus_message_builder_finalize(builder);
  1532. l_dbus_message_builder_destroy(builder);
  1533. }
  1534. static void reg_def_req_default_agent_reply(struct l_dbus_proxy *proxy,
  1535. struct l_dbus_message *result,
  1536. void *user_data)
  1537. {
  1538. if (l_dbus_message_is_error(result)) {
  1539. const char *name, *desc;
  1540. if (!l_dbus_object_remove_interface(dbus, AG_PATH, AG_IFACE))
  1541. l_info("Unable to remove agent instance");
  1542. if (!l_dbus_unregister_interface(dbus, AG_IFACE))
  1543. l_info("Unable to unregister agent interface");
  1544. l_dbus_message_get_error(result, &name, &desc);
  1545. l_error("Failed to request default agent (%s), %s", name, desc);
  1546. btp_send_error(btp, BTP_CORE_SERVICE, BTP_INDEX_NON_CONTROLLER,
  1547. BTP_ERROR_FAIL);
  1548. return;
  1549. }
  1550. register_gap_service();
  1551. gap_service_registered = true;
  1552. ag.registered = true;
  1553. btp_send(btp, BTP_CORE_SERVICE, BTP_OP_CORE_REGISTER,
  1554. BTP_INDEX_NON_CONTROLLER, 0, NULL);
  1555. }
  1556. static void set_io_req_default_agent_reply(struct l_dbus_proxy *proxy,
  1557. struct l_dbus_message *result,
  1558. void *user_data)
  1559. {
  1560. struct btp_adapter *adapter = user_data;
  1561. if (!adapter) {
  1562. btp_send_error(btp, BTP_GAP_SERVICE, BTP_INDEX_NON_CONTROLLER,
  1563. BTP_ERROR_FAIL);
  1564. goto failed;
  1565. }
  1566. if (l_dbus_message_is_error(result)) {
  1567. const char *name, *desc;
  1568. l_dbus_message_get_error(result, &name, &desc);
  1569. l_error("Failed to set io capabilities (%s), %s", name, desc);
  1570. btp_send_error(btp, BTP_GAP_SERVICE, adapter->index,
  1571. BTP_ERROR_FAIL);
  1572. goto failed;
  1573. }
  1574. ag.registered = true;
  1575. btp_send(btp, BTP_GAP_SERVICE, BTP_OP_GAP_SET_IO_CAPA,
  1576. adapter->index, 0, NULL);
  1577. return;
  1578. failed:
  1579. if (!l_dbus_object_remove_interface(dbus, AG_PATH, AG_IFACE))
  1580. l_info("Unable to remove agent instance");
  1581. if (!l_dbus_unregister_interface(dbus, AG_IFACE))
  1582. l_info("Unable to unregister agent interface");
  1583. }
  1584. static void request_default_agent_setup(struct l_dbus_message *message,
  1585. void *user_data)
  1586. {
  1587. struct l_dbus_message_builder *builder;
  1588. builder = l_dbus_message_builder_new(message);
  1589. l_dbus_message_builder_append_basic(builder, 'o', AG_PATH);
  1590. l_dbus_message_builder_finalize(builder);
  1591. l_dbus_message_builder_destroy(builder);
  1592. }
  1593. static void set_io_capabilities_reply(struct l_dbus_proxy *proxy,
  1594. struct l_dbus_message *result,
  1595. void *user_data)
  1596. {
  1597. struct set_io_capabilities_data *sicd = user_data;
  1598. if (!sicd->adapter) {
  1599. btp_send_error(btp, BTP_GAP_SERVICE, BTP_INDEX_NON_CONTROLLER,
  1600. BTP_ERROR_FAIL);
  1601. goto failed;
  1602. }
  1603. if (l_dbus_message_is_error(result)) {
  1604. const char *name, *desc;
  1605. l_dbus_message_get_error(result, &name, &desc);
  1606. l_error("Failed to set io capabilities (%s), %s", name, desc);
  1607. btp_send_error(btp, BTP_GAP_SERVICE, sicd->adapter->index,
  1608. BTP_ERROR_FAIL);
  1609. goto failed;
  1610. }
  1611. if (l_dbus_proxy_method_call(ag.proxy, "RequestDefaultAgent",
  1612. request_default_agent_setup,
  1613. set_io_req_default_agent_reply,
  1614. sicd->adapter, NULL))
  1615. return;
  1616. failed:
  1617. if (!l_dbus_object_remove_interface(dbus, AG_PATH, AG_IFACE))
  1618. l_info("Unable to remove agent instance");
  1619. if (!l_dbus_unregister_interface(dbus, AG_IFACE))
  1620. l_info("Unable to unregister agent interface");
  1621. }
  1622. static void register_default_agent_reply(struct l_dbus_proxy *proxy,
  1623. struct l_dbus_message *result,
  1624. void *user_data)
  1625. {
  1626. const char *name, *desc;
  1627. if (l_dbus_message_is_error(result)) {
  1628. if (!l_dbus_object_remove_interface(dbus, AG_PATH, AG_IFACE))
  1629. l_info("Unable to remove agent instance");
  1630. if (!l_dbus_unregister_interface(dbus, AG_IFACE))
  1631. l_info("Unable to unregister agent interface");
  1632. l_dbus_message_get_error(result, &name, &desc);
  1633. l_error("Failed to register default agent (%s), %s", name,
  1634. desc);
  1635. return;
  1636. }
  1637. if (!l_dbus_proxy_method_call(ag.proxy, "RequestDefaultAgent",
  1638. request_default_agent_setup,
  1639. reg_def_req_default_agent_reply,
  1640. NULL, NULL)) {
  1641. if (!l_dbus_object_remove_interface(dbus, AG_PATH, AG_IFACE))
  1642. l_info("Unable to remove agent instance");
  1643. if (!l_dbus_unregister_interface(dbus, AG_IFACE))
  1644. l_info("Unable to unregister agent interface");
  1645. }
  1646. }
  1647. static void set_io_capabilities_destroy(void *user_data)
  1648. {
  1649. l_free(user_data);
  1650. }
  1651. static bool register_default_agent(struct btp_adapter *adapter, uint8_t capa,
  1652. l_dbus_client_proxy_result_func_t set_io_cb)
  1653. {
  1654. struct set_io_capabilities_data *data;
  1655. if (!l_dbus_register_interface(dbus, AG_IFACE, setup_ag_interface, NULL,
  1656. false)) {
  1657. l_info("Unable to register agent interface");
  1658. return false;
  1659. }
  1660. if (!l_dbus_object_add_interface(dbus, AG_PATH, AG_IFACE, NULL)) {
  1661. l_info("Unable to instantiate agent interface");
  1662. if (!l_dbus_unregister_interface(dbus, AG_IFACE))
  1663. l_info("Unable to unregister agent interface");
  1664. return false;
  1665. }
  1666. if (!l_dbus_object_add_interface(dbus, AG_PATH,
  1667. L_DBUS_INTERFACE_PROPERTIES,
  1668. NULL)) {
  1669. l_info("Unable to instantiate the ag properties interface");
  1670. if (!l_dbus_object_remove_interface(dbus, AG_PATH, AG_IFACE))
  1671. l_info("Unable to remove agent instance");
  1672. if (!l_dbus_unregister_interface(dbus, AG_IFACE))
  1673. l_info("Unable to unregister agent interface");
  1674. return false;
  1675. }
  1676. data = l_new(struct set_io_capabilities_data, 1);
  1677. data->adapter = adapter;
  1678. data->capa = capa;
  1679. if (!l_dbus_proxy_method_call(ag.proxy, "RegisterAgent",
  1680. set_io_capabilities_setup, set_io_cb,
  1681. data, set_io_capabilities_destroy)) {
  1682. if (!l_dbus_object_remove_interface(dbus, AG_PATH, AG_IFACE))
  1683. l_info("Unable to remove agent instance");
  1684. if (!l_dbus_unregister_interface(dbus, AG_IFACE))
  1685. l_info("Unable to unregister agent interface");
  1686. return false;
  1687. }
  1688. return true;
  1689. }
  1690. struct rereg_unreg_agent_data {
  1691. struct btp_adapter *adapter;
  1692. l_dbus_client_proxy_result_func_t cb;
  1693. uint8_t capa;
  1694. };
  1695. static void rereg_unreg_agent_reply(struct l_dbus_proxy *proxy,
  1696. struct l_dbus_message *result,
  1697. void *user_data)
  1698. {
  1699. struct rereg_unreg_agent_data *ruad = user_data;
  1700. if (l_dbus_message_is_error(result)) {
  1701. const char *name;
  1702. l_dbus_message_get_error(result, &name, NULL);
  1703. l_error("Failed to unregister agent %s (%s)",
  1704. l_dbus_proxy_get_path(proxy), name);
  1705. return;
  1706. }
  1707. if (!l_dbus_object_remove_interface(dbus, AG_PATH,
  1708. L_DBUS_INTERFACE_PROPERTIES))
  1709. l_info("Unable to remove propety instance");
  1710. if (!l_dbus_object_remove_interface(dbus, AG_PATH, AG_IFACE))
  1711. l_info("Unable to remove agent instance");
  1712. if (!l_dbus_unregister_interface(dbus, AG_IFACE))
  1713. l_info("Unable to unregister agent interface");
  1714. ag.registered = false;
  1715. if (!register_default_agent(ruad->adapter, ruad->capa, ruad->cb))
  1716. btp_send_error(btp, BTP_GAP_SERVICE, ruad->adapter->index,
  1717. BTP_ERROR_FAIL);
  1718. }
  1719. static void rereg_unreg_agent_destroy(void *rereg_unreg_agent_data)
  1720. {
  1721. l_free(rereg_unreg_agent_data);
  1722. }
  1723. static void btp_gap_set_io_capabilities(uint8_t index, const void *param,
  1724. uint16_t length, void *user_data)
  1725. {
  1726. struct btp_adapter *adapter = find_adapter_by_index(index);
  1727. const struct btp_gap_set_io_capa_cp *cp = param;
  1728. uint8_t status = BTP_ERROR_FAIL;
  1729. struct rereg_unreg_agent_data *data;
  1730. bool prop;
  1731. switch (cp->capa) {
  1732. case BTP_GAP_IOCAPA_DISPLAY_ONLY:
  1733. case BTP_GAP_IOCAPA_DISPLAY_YESNO:
  1734. case BTP_GAP_IOCAPA_KEYBOARD_ONLY:
  1735. case BTP_GAP_IOCAPA_NO_INPUT_NO_OUTPUT:
  1736. case BTP_GAP_IOCAPA_KEYBOARD_DISPLAY:
  1737. break;
  1738. default:
  1739. l_error("Wrong iocapa given!");
  1740. goto failed;
  1741. }
  1742. if (!adapter) {
  1743. status = BTP_ERROR_INVALID_INDEX;
  1744. goto failed;
  1745. }
  1746. /* Adapter needs to be powered to be able to set io cap */
  1747. if (!l_dbus_proxy_get_property(adapter->proxy, "Powered", "b", &prop) ||
  1748. !prop)
  1749. goto failed;
  1750. if (ag.registered) {
  1751. data = l_new(struct rereg_unreg_agent_data, 1);
  1752. data->adapter = adapter;
  1753. data->capa = cp->capa;
  1754. data->cb = set_io_capabilities_reply;
  1755. if (!l_dbus_proxy_method_call(ag.proxy, "UnregisterAgent",
  1756. unreg_agent_setup,
  1757. rereg_unreg_agent_reply, data,
  1758. rereg_unreg_agent_destroy))
  1759. goto failed;
  1760. return;
  1761. }
  1762. if (!register_default_agent(adapter, cp->capa,
  1763. set_io_capabilities_reply))
  1764. goto failed;
  1765. return;
  1766. failed:
  1767. btp_send_error(btp, BTP_GAP_SERVICE, index, status);
  1768. }
  1769. static void pair_reply(struct l_dbus_proxy *proxy,
  1770. struct l_dbus_message *result, void *user_data)
  1771. {
  1772. uint8_t adapter_index = L_PTR_TO_UINT(user_data);
  1773. struct btp_adapter *adapter = find_adapter_by_index(adapter_index);
  1774. if (!adapter)
  1775. return;
  1776. if (l_dbus_message_is_error(result)) {
  1777. const char *name, *desc;
  1778. l_dbus_message_get_error(result, &name, &desc);
  1779. l_error("Failed to pair (%s), %s", name, desc);
  1780. return;
  1781. }
  1782. }
  1783. static void btp_gap_pair(uint8_t index, const void *param, uint16_t length,
  1784. void *user_data)
  1785. {
  1786. struct btp_adapter *adapter = find_adapter_by_index(index);
  1787. const struct btp_gap_pair_cp *cp = param;
  1788. uint8_t status = BTP_ERROR_FAIL;
  1789. struct btp_device *device;
  1790. bool prop;
  1791. if (!adapter) {
  1792. status = BTP_ERROR_INVALID_INDEX;
  1793. goto failed;
  1794. }
  1795. /* Adapter needs to be powered to be able to pair */
  1796. if (!l_dbus_proxy_get_property(adapter->proxy, "Powered", "b", &prop) ||
  1797. !prop)
  1798. goto failed;
  1799. device = find_device_by_address(adapter, &cp->address,
  1800. cp->address_type);
  1801. if (!device)
  1802. goto failed;
  1803. /* This command is asynchronous, send reply immediatelly to not block
  1804. * pairing process eg. passkey request.
  1805. */
  1806. btp_send(btp, BTP_GAP_SERVICE, BTP_OP_GAP_PAIR, adapter->index, 0,
  1807. NULL);
  1808. l_dbus_proxy_method_call(device->proxy, "Pair", NULL, pair_reply,
  1809. L_UINT_TO_PTR(adapter->index), NULL);
  1810. return;
  1811. failed:
  1812. btp_send_error(btp, BTP_GAP_SERVICE, index, status);
  1813. }
  1814. static void unpair_reply(struct l_dbus_proxy *proxy,
  1815. struct l_dbus_message *result, void *user_data)
  1816. {
  1817. struct btp_device *device = user_data;
  1818. struct btp_adapter *adapter = find_adapter_by_device(device);
  1819. if (!adapter) {
  1820. btp_send_error(btp, BTP_GAP_SERVICE, BTP_INDEX_NON_CONTROLLER,
  1821. BTP_ERROR_FAIL);
  1822. return;
  1823. }
  1824. if (l_dbus_message_is_error(result)) {
  1825. const char *name, *desc;
  1826. l_dbus_message_get_error(result, &name, &desc);
  1827. l_error("Failed to unpair (%s), %s", name, desc);
  1828. btp_send_error(btp, BTP_GAP_SERVICE, adapter->index,
  1829. BTP_ERROR_FAIL);
  1830. return;
  1831. }
  1832. btp_send(btp, BTP_GAP_SERVICE, BTP_OP_GAP_UNPAIR, adapter->index, 0,
  1833. NULL);
  1834. }
  1835. static void unpair_setup(struct l_dbus_message *message, void *user_data)
  1836. {
  1837. struct btp_device *device = user_data;
  1838. const char *path = l_dbus_proxy_get_path(device->proxy);
  1839. struct l_dbus_message_builder *builder;
  1840. builder = l_dbus_message_builder_new(message);
  1841. l_dbus_message_builder_append_basic(builder, 'o', path);
  1842. l_dbus_message_builder_finalize(builder);
  1843. l_dbus_message_builder_destroy(builder);
  1844. }
  1845. static void btp_gap_unpair(uint8_t index, const void *param, uint16_t length,
  1846. void *user_data)
  1847. {
  1848. struct btp_adapter *adapter = find_adapter_by_index(index);
  1849. const struct btp_gap_pair_cp *cp = param;
  1850. uint8_t status = BTP_ERROR_FAIL;
  1851. struct btp_device *device;
  1852. bool prop;
  1853. if (!adapter) {
  1854. status = BTP_ERROR_INVALID_INDEX;
  1855. goto failed;
  1856. }
  1857. /* Adapter needs to be powered to be able to unpair */
  1858. if (!l_dbus_proxy_get_property(adapter->proxy, "Powered", "b", &prop) ||
  1859. !prop)
  1860. goto failed;
  1861. device = find_device_by_address(adapter, &cp->address,
  1862. cp->address_type);
  1863. if (!device)
  1864. goto failed;
  1865. /* There is no direct unpair method, removing device will clear pairing
  1866. * information.
  1867. */
  1868. l_dbus_proxy_method_call(adapter->proxy, "RemoveDevice", unpair_setup,
  1869. unpair_reply, device, NULL);
  1870. return;
  1871. failed:
  1872. btp_send_error(btp, BTP_GAP_SERVICE, index, status);
  1873. }
  1874. static void passkey_entry_rsp_reply(struct l_dbus_message *result,
  1875. void *user_data)
  1876. {
  1877. struct btp_adapter *adapter = user_data;
  1878. if (l_dbus_message_is_error(result)) {
  1879. const char *name, *desc;
  1880. l_dbus_message_get_error(result, &name, &desc);
  1881. l_error("Failed to reply with passkey (%s), %s", name, desc);
  1882. btp_send_error(btp, BTP_GAP_SERVICE, adapter->index,
  1883. BTP_ERROR_FAIL);
  1884. return;
  1885. }
  1886. l_dbus_message_unref(ag.pending_req);
  1887. btp_send(btp, BTP_GAP_SERVICE, BTP_OP_GAP_PASSKEY_ENTRY_RSP,
  1888. adapter->index, 0, NULL);
  1889. }
  1890. static void btp_gap_passkey_entry_rsp(uint8_t index, const void *param,
  1891. uint16_t length, void *user_data)
  1892. {
  1893. const struct btp_gap_passkey_entry_rsp_cp *cp = param;
  1894. struct btp_adapter *adapter = find_adapter_by_index(index);
  1895. struct l_dbus_message_builder *builder;
  1896. uint8_t status = BTP_ERROR_FAIL;
  1897. uint32_t passkey = L_CPU_TO_LE32(cp->passkey);
  1898. bool prop;
  1899. if (!adapter) {
  1900. status = BTP_ERROR_INVALID_INDEX;
  1901. goto failed;
  1902. }
  1903. /* Adapter needs to be powered to be able to response with passkey */
  1904. if (!l_dbus_proxy_get_property(adapter->proxy, "Powered", "b", &prop) ||
  1905. !prop || !ag.pending_req)
  1906. goto failed;
  1907. builder = l_dbus_message_builder_new(ag.pending_req);
  1908. l_dbus_message_builder_append_basic(builder, 'u', &passkey);
  1909. l_dbus_message_builder_finalize(builder);
  1910. l_dbus_message_builder_destroy(builder);
  1911. l_dbus_send_with_reply(dbus, ag.pending_req, passkey_entry_rsp_reply,
  1912. adapter, NULL);
  1913. return;
  1914. failed:
  1915. btp_send_error(btp, BTP_GAP_SERVICE, index, status);
  1916. }
  1917. static void passkey_confirm_rsp_reply(struct l_dbus_message *result,
  1918. void *user_data)
  1919. {
  1920. struct btp_adapter *adapter = user_data;
  1921. if (l_dbus_message_is_error(result)) {
  1922. const char *name, *desc;
  1923. l_dbus_message_get_error(result, &name, &desc);
  1924. l_error("Failed to confirm passkey (%s), %s", name, desc);
  1925. btp_send_error(btp, BTP_GAP_SERVICE, adapter->index,
  1926. BTP_ERROR_FAIL);
  1927. return;
  1928. }
  1929. l_dbus_message_unref(ag.pending_req);
  1930. btp_send(btp, BTP_GAP_SERVICE, BTP_OP_GAP_PASSKEY_CONFIRM_RSP,
  1931. adapter->index, 0, NULL);
  1932. }
  1933. static void btp_gap_confirm_entry_rsp(uint8_t index, const void *param,
  1934. uint16_t length, void *user_data)
  1935. {
  1936. const struct btp_gap_passkey_confirm_rsp_cp *cp = param;
  1937. struct btp_adapter *adapter = find_adapter_by_index(index);
  1938. struct l_dbus_message *reply;
  1939. uint8_t status = BTP_ERROR_FAIL;
  1940. bool prop;
  1941. if (!adapter) {
  1942. status = BTP_ERROR_INVALID_INDEX;
  1943. goto failed;
  1944. }
  1945. /* Adapter needs to be powered to be able to confirm passkey */
  1946. if (!l_dbus_proxy_get_property(adapter->proxy, "Powered", "b", &prop) ||
  1947. !prop || !ag.pending_req)
  1948. goto failed;
  1949. if (cp->match) {
  1950. reply = l_dbus_message_new_method_return(ag.pending_req);
  1951. l_dbus_message_set_arguments(reply, "");
  1952. } else {
  1953. reply = l_dbus_message_new_error(ag.pending_req,
  1954. "org.bluez.Error.Rejected",
  1955. "Passkey missmatch");
  1956. }
  1957. l_dbus_send_with_reply(dbus, ag.pending_req, passkey_confirm_rsp_reply,
  1958. adapter, NULL);
  1959. return;
  1960. failed:
  1961. btp_send_error(btp, BTP_GAP_SERVICE, index, status);
  1962. }
  1963. static void btp_gap_device_found_ev(struct l_dbus_proxy *proxy)
  1964. {
  1965. struct btp_device *device = find_device_by_proxy(proxy);
  1966. struct btp_adapter *adapter = find_adapter_by_device(device);
  1967. struct btp_device_found_ev ev;
  1968. struct btp_gap_device_connected_ev ev_conn;
  1969. const char *str, *addr_str;
  1970. int16_t rssi;
  1971. uint8_t address_type;
  1972. bool connected;
  1973. if (!l_dbus_proxy_get_property(proxy, "Address", "s", &addr_str) ||
  1974. str2ba(addr_str, &ev.address) < 0)
  1975. return;
  1976. if (!l_dbus_proxy_get_property(proxy, "AddressType", "s", &str))
  1977. return;
  1978. address_type = strcmp(str, "public") ? BTP_GAP_ADDR_RANDOM :
  1979. BTP_GAP_ADDR_PUBLIC;
  1980. ev.address_type = address_type;
  1981. if (!l_dbus_proxy_get_property(proxy, "RSSI", "n", &rssi))
  1982. ev.rssi = 0x81;
  1983. else
  1984. ev.rssi = rssi;
  1985. /* TODO Temporary set all flags */
  1986. ev.flags = (BTP_EV_GAP_DEVICE_FOUND_FLAG_RSSI |
  1987. BTP_EV_GAP_DEVICE_FOUND_FLAG_AD |
  1988. BTP_EV_GAP_DEVICE_FOUND_FLAG_SR);
  1989. /* TODO Add eir to device found event */
  1990. ev.eir_len = 0;
  1991. btp_send(btp, BTP_GAP_SERVICE, BTP_EV_GAP_DEVICE_FOUND, adapter->index,
  1992. sizeof(ev) + ev.eir_len, &ev);
  1993. if (l_dbus_proxy_get_property(proxy, "Connected", "b", &connected) &&
  1994. connected) {
  1995. ev_conn.address_type = address_type;
  1996. str2ba(addr_str, &ev_conn.address);
  1997. btp_send(btp, BTP_GAP_SERVICE, BTP_EV_GAP_DEVICE_CONNECTED,
  1998. adapter->index, sizeof(ev_conn), &ev_conn);
  1999. }
  2000. }
  2001. static void btp_gap_device_connection_ev(struct l_dbus_proxy *proxy,
  2002. bool connected)
  2003. {
  2004. struct btp_adapter *adapter;
  2005. struct btp_device *device;
  2006. const char *str_addr, *str_addr_type;
  2007. uint8_t address_type;
  2008. device = find_device_by_proxy(proxy);
  2009. adapter = find_adapter_by_device(device);
  2010. if (!device || !adapter)
  2011. return;
  2012. if (!l_dbus_proxy_get_property(proxy, "Address", "s", &str_addr))
  2013. return;
  2014. if (!l_dbus_proxy_get_property(proxy, "AddressType", "s",
  2015. &str_addr_type))
  2016. return;
  2017. address_type = strcmp(str_addr_type, "public") ? BTP_GAP_ADDR_RANDOM :
  2018. BTP_GAP_ADDR_PUBLIC;
  2019. if (connected) {
  2020. struct btp_gap_device_connected_ev ev;
  2021. str2ba(str_addr, &ev.address);
  2022. ev.address_type = address_type;
  2023. btp_send(btp, BTP_GAP_SERVICE, BTP_EV_GAP_DEVICE_CONNECTED,
  2024. adapter->index, sizeof(ev), &ev);
  2025. } else {
  2026. struct btp_gap_device_disconnected_ev ev;
  2027. str2ba(str_addr, &ev.address);
  2028. ev.address_type = address_type;
  2029. btp_send(btp, BTP_GAP_SERVICE, BTP_EV_GAP_DEVICE_DISCONNECTED,
  2030. adapter->index, sizeof(ev), &ev);
  2031. }
  2032. }
  2033. static void btp_identity_resolved_ev(struct l_dbus_proxy *proxy)
  2034. {
  2035. struct btp_device *dev = find_device_by_proxy(proxy);
  2036. struct btp_adapter *adapter = find_adapter_by_device(dev);
  2037. struct btp_gap_identity_resolved_ev ev;
  2038. char *str_addr, *str_addr_type;
  2039. uint8_t identity_address_type;
  2040. if (!l_dbus_proxy_get_property(proxy, "Address", "s", &str_addr))
  2041. return;
  2042. if (!l_dbus_proxy_get_property(proxy, "AddressType", "s",
  2043. &str_addr_type))
  2044. return;
  2045. identity_address_type = strcmp(str_addr_type, "public") ?
  2046. BTP_GAP_ADDR_RANDOM : BTP_GAP_ADDR_PUBLIC;
  2047. str2ba(str_addr, &ev.identity_address);
  2048. ev.identity_address_type = identity_address_type;
  2049. memcpy(&ev.address, &dev->address, sizeof(ev.address));
  2050. ev.address_type = dev->address_type;
  2051. btp_send(btp, BTP_GAP_SERVICE, BTP_EV_GAP_IDENTITY_RESOLVED,
  2052. adapter->index, sizeof(ev), &ev);
  2053. }
  2054. static void register_gap_service(void)
  2055. {
  2056. btp_register(btp, BTP_GAP_SERVICE, BTP_OP_GAP_READ_SUPPORTED_COMMANDS,
  2057. btp_gap_read_commands, NULL, NULL);
  2058. btp_register(btp, BTP_GAP_SERVICE,
  2059. BTP_OP_GAP_READ_CONTROLLER_INDEX_LIST,
  2060. btp_gap_read_controller_index, NULL, NULL);
  2061. btp_register(btp, BTP_GAP_SERVICE, BTP_OP_GAP_READ_COTROLLER_INFO,
  2062. btp_gap_read_info, NULL, NULL);
  2063. btp_register(btp, BTP_GAP_SERVICE, BTP_OP_GAP_RESET,
  2064. btp_gap_reset, NULL, NULL);
  2065. btp_register(btp, BTP_GAP_SERVICE, BTP_OP_GAP_SET_POWERED,
  2066. btp_gap_set_powered, NULL, NULL);
  2067. btp_register(btp, BTP_GAP_SERVICE, BTP_OP_GAP_SET_CONNECTABLE,
  2068. btp_gap_set_connectable, NULL, NULL);
  2069. btp_register(btp, BTP_GAP_SERVICE, BTP_OP_GAP_SET_DISCOVERABLE,
  2070. btp_gap_set_discoverable, NULL, NULL);
  2071. btp_register(btp, BTP_GAP_SERVICE, BTP_OP_GAP_SET_BONDABLE,
  2072. btp_gap_set_bondable, NULL, NULL);
  2073. btp_register(btp, BTP_GAP_SERVICE, BTP_OP_GAP_START_ADVERTISING,
  2074. btp_gap_start_advertising, NULL, NULL);
  2075. btp_register(btp, BTP_GAP_SERVICE, BTP_OP_GAP_STOP_ADVERTISING,
  2076. btp_gap_stop_advertising, NULL, NULL);
  2077. btp_register(btp, BTP_GAP_SERVICE, BTP_OP_GAP_START_DISCOVERY,
  2078. btp_gap_start_discovery, NULL, NULL);
  2079. btp_register(btp, BTP_GAP_SERVICE, BTP_OP_GAP_STOP_DISCOVERY,
  2080. btp_gap_stop_discovery, NULL, NULL);
  2081. btp_register(btp, BTP_GAP_SERVICE, BTP_OP_GAP_CONNECT, btp_gap_connect,
  2082. NULL, NULL);
  2083. btp_register(btp, BTP_GAP_SERVICE, BTP_OP_GAP_DISCONNECT,
  2084. btp_gap_disconnect, NULL, NULL);
  2085. btp_register(btp, BTP_GAP_SERVICE, BTP_OP_GAP_SET_IO_CAPA,
  2086. btp_gap_set_io_capabilities, NULL, NULL);
  2087. btp_register(btp, BTP_GAP_SERVICE, BTP_OP_GAP_PAIR, btp_gap_pair, NULL,
  2088. NULL);
  2089. btp_register(btp, BTP_GAP_SERVICE, BTP_OP_GAP_UNPAIR, btp_gap_unpair,
  2090. NULL, NULL);
  2091. btp_register(btp, BTP_GAP_SERVICE, BTP_OP_GAP_PASSKEY_ENTRY_RSP,
  2092. btp_gap_passkey_entry_rsp, NULL, NULL);
  2093. btp_register(btp, BTP_GAP_SERVICE, BTP_OP_GAP_PASSKEY_CONFIRM_RSP,
  2094. btp_gap_confirm_entry_rsp, NULL, NULL);
  2095. }
  2096. static void btp_core_read_commands(uint8_t index, const void *param,
  2097. uint16_t length, void *user_data)
  2098. {
  2099. uint8_t commands = 0;
  2100. if (index != BTP_INDEX_NON_CONTROLLER) {
  2101. btp_send_error(btp, BTP_CORE_SERVICE, index,
  2102. BTP_ERROR_INVALID_INDEX);
  2103. return;
  2104. }
  2105. commands |= (1 << BTP_OP_CORE_READ_SUPPORTED_COMMANDS);
  2106. commands |= (1 << BTP_OP_CORE_READ_SUPPORTED_SERVICES);
  2107. commands |= (1 << BTP_OP_CORE_REGISTER);
  2108. commands |= (1 << BTP_OP_CORE_UNREGISTER);
  2109. btp_send(btp, BTP_CORE_SERVICE, BTP_OP_CORE_READ_SUPPORTED_COMMANDS,
  2110. BTP_INDEX_NON_CONTROLLER, sizeof(commands), &commands);
  2111. }
  2112. static void btp_core_read_services(uint8_t index, const void *param,
  2113. uint16_t length, void *user_data)
  2114. {
  2115. uint8_t services = 0;
  2116. if (index != BTP_INDEX_NON_CONTROLLER) {
  2117. btp_send_error(btp, BTP_CORE_SERVICE, index,
  2118. BTP_ERROR_INVALID_INDEX);
  2119. return;
  2120. }
  2121. services |= (1 << BTP_CORE_SERVICE);
  2122. services |= (1 << BTP_GAP_SERVICE);
  2123. btp_send(btp, BTP_CORE_SERVICE, BTP_OP_CORE_READ_SUPPORTED_SERVICES,
  2124. BTP_INDEX_NON_CONTROLLER, sizeof(services), &services);
  2125. }
  2126. static void btp_core_register(uint8_t index, const void *param,
  2127. uint16_t length, void *user_data)
  2128. {
  2129. const struct btp_core_register_cp *cp = param;
  2130. if (length < sizeof(*cp))
  2131. goto failed;
  2132. if (index != BTP_INDEX_NON_CONTROLLER) {
  2133. btp_send_error(btp, BTP_CORE_SERVICE, index,
  2134. BTP_ERROR_INVALID_INDEX);
  2135. return;
  2136. }
  2137. switch (cp->service_id) {
  2138. case BTP_GAP_SERVICE:
  2139. if (gap_service_registered)
  2140. goto failed;
  2141. if (!register_default_agent(NULL,
  2142. BTP_GAP_IOCAPA_NO_INPUT_NO_OUTPUT,
  2143. register_default_agent_reply))
  2144. goto failed;
  2145. return;
  2146. case BTP_GATT_SERVICE:
  2147. case BTP_L2CAP_SERVICE:
  2148. case BTP_MESH_NODE_SERVICE:
  2149. case BTP_CORE_SERVICE:
  2150. default:
  2151. goto failed;
  2152. }
  2153. btp_send(btp, BTP_CORE_SERVICE, BTP_OP_CORE_REGISTER,
  2154. BTP_INDEX_NON_CONTROLLER, 0, NULL);
  2155. return;
  2156. failed:
  2157. btp_send_error(btp, BTP_CORE_SERVICE, index, BTP_ERROR_FAIL);
  2158. }
  2159. static void btp_core_unregister(uint8_t index, const void *param,
  2160. uint16_t length, void *user_data)
  2161. {
  2162. const struct btp_core_unregister_cp *cp = param;
  2163. if (length < sizeof(*cp))
  2164. goto failed;
  2165. if (index != BTP_INDEX_NON_CONTROLLER) {
  2166. btp_send_error(btp, BTP_CORE_SERVICE, index,
  2167. BTP_ERROR_INVALID_INDEX);
  2168. return;
  2169. }
  2170. switch (cp->service_id) {
  2171. case BTP_GAP_SERVICE:
  2172. if (!gap_service_registered)
  2173. goto failed;
  2174. btp_unregister_service(btp, BTP_GAP_SERVICE);
  2175. gap_service_registered = false;
  2176. break;
  2177. case BTP_GATT_SERVICE:
  2178. case BTP_L2CAP_SERVICE:
  2179. case BTP_MESH_NODE_SERVICE:
  2180. case BTP_CORE_SERVICE:
  2181. default:
  2182. goto failed;
  2183. }
  2184. btp_send(btp, BTP_CORE_SERVICE, BTP_OP_CORE_UNREGISTER,
  2185. BTP_INDEX_NON_CONTROLLER, 0, NULL);
  2186. return;
  2187. failed:
  2188. btp_send_error(btp, BTP_CORE_SERVICE, index, BTP_ERROR_FAIL);
  2189. }
  2190. static void register_core_service(void)
  2191. {
  2192. btp_register(btp, BTP_CORE_SERVICE,
  2193. BTP_OP_CORE_READ_SUPPORTED_COMMANDS,
  2194. btp_core_read_commands, NULL, NULL);
  2195. btp_register(btp, BTP_CORE_SERVICE,
  2196. BTP_OP_CORE_READ_SUPPORTED_SERVICES,
  2197. btp_core_read_services, NULL, NULL);
  2198. btp_register(btp, BTP_CORE_SERVICE, BTP_OP_CORE_REGISTER,
  2199. btp_core_register, NULL, NULL);
  2200. btp_register(btp, BTP_CORE_SERVICE, BTP_OP_CORE_UNREGISTER,
  2201. btp_core_unregister, NULL, NULL);
  2202. }
  2203. static void signal_handler(uint32_t signo, void *user_data)
  2204. {
  2205. switch (signo) {
  2206. case SIGINT:
  2207. case SIGTERM:
  2208. l_info("Terminating");
  2209. l_main_quit();
  2210. break;
  2211. }
  2212. }
  2213. static void btp_device_free(struct btp_device *device)
  2214. {
  2215. l_free(device);
  2216. }
  2217. static void btp_adapter_free(struct btp_adapter *adapter)
  2218. {
  2219. l_queue_destroy(adapter->devices,
  2220. (l_queue_destroy_func_t)btp_device_free);
  2221. l_free(adapter);
  2222. }
  2223. static void extract_settings(struct l_dbus_proxy *proxy, uint32_t *current,
  2224. uint32_t *supported)
  2225. {
  2226. bool prop;
  2227. *supported = 0;
  2228. *current = 0;
  2229. /* TODO not all info is available via D-Bus API */
  2230. *supported |= BTP_GAP_SETTING_POWERED;
  2231. *supported |= BTP_GAP_SETTING_CONNECTABLE;
  2232. *supported |= BTP_GAP_SETTING_DISCOVERABLE;
  2233. *supported |= BTP_GAP_SETTING_BONDABLE;
  2234. *supported |= BTP_GAP_SETTING_SSP;
  2235. *supported |= BTP_GAP_SETTING_BREDR;
  2236. *supported |= BTP_GAP_SETTING_LE;
  2237. *supported |= BTP_GAP_SETTING_ADVERTISING;
  2238. *supported |= BTP_GAP_SETTING_SC;
  2239. *supported |= BTP_GAP_SETTING_PRIVACY;
  2240. /* *supported |= BTP_GAP_SETTING_STATIC_ADDRESS; */
  2241. /* TODO not all info is availbe via D-Bus API so some are assumed to be
  2242. * enabled by bluetoothd or simply hardcoded until API is extended
  2243. */
  2244. *current |= BTP_GAP_SETTING_CONNECTABLE;
  2245. *current |= BTP_GAP_SETTING_SSP;
  2246. *current |= BTP_GAP_SETTING_BREDR;
  2247. *current |= BTP_GAP_SETTING_LE;
  2248. *current |= BTP_GAP_SETTING_PRIVACY;
  2249. *current |= BTP_GAP_SETTING_SC;
  2250. /* *supported |= BTP_GAP_SETTING_STATIC_ADDRESS; */
  2251. if (l_dbus_proxy_get_property(proxy, "Powered", "b", &prop) && prop)
  2252. *current |= BTP_GAP_SETTING_POWERED;
  2253. if (l_dbus_proxy_get_property(proxy, "Discoverable", "b", &prop) &&
  2254. prop)
  2255. *current |= BTP_GAP_SETTING_DISCOVERABLE;
  2256. if (l_dbus_proxy_get_property(proxy, "Pairable", "b", &prop) && prop)
  2257. *current |= BTP_GAP_SETTING_BONDABLE;
  2258. }
  2259. static void proxy_added(struct l_dbus_proxy *proxy, void *user_data)
  2260. {
  2261. const char *interface = l_dbus_proxy_get_interface(proxy);
  2262. const char *path = l_dbus_proxy_get_path(proxy);
  2263. l_info("Proxy added: %s (%s)", interface, path);
  2264. if (!strcmp(interface, "org.bluez.Adapter1")) {
  2265. struct btp_adapter *adapter;
  2266. adapter = l_new(struct btp_adapter, 1);
  2267. adapter->proxy = proxy;
  2268. adapter->index = l_queue_length(adapters);
  2269. adapter->devices = l_queue_new();
  2270. extract_settings(proxy, &adapter->current_settings,
  2271. &adapter->supported_settings);
  2272. adapter->default_settings = adapter->current_settings;
  2273. l_queue_push_tail(adapters, adapter);
  2274. return;
  2275. }
  2276. if (!strcmp(interface, "org.bluez.Device1")) {
  2277. struct btp_adapter *adapter;
  2278. struct btp_device *device;
  2279. char *str, *str_addr, *str_addr_type;
  2280. if (!l_dbus_proxy_get_property(proxy, "Adapter", "o", &str))
  2281. return;
  2282. adapter = find_adapter_by_path(str);
  2283. if (!adapter)
  2284. return;
  2285. device = l_new(struct btp_device, 1);
  2286. device->proxy = proxy;
  2287. l_queue_push_tail(adapter->devices, device);
  2288. btp_gap_device_found_ev(proxy);
  2289. if (!l_dbus_proxy_get_property(proxy, "Address", "s",
  2290. &str_addr))
  2291. return;
  2292. if (!l_dbus_proxy_get_property(proxy, "AddressType", "s",
  2293. &str_addr_type))
  2294. return;
  2295. device->address_type = strcmp(str_addr_type, "public") ?
  2296. BTP_GAP_ADDR_RANDOM :
  2297. BTP_GAP_ADDR_PUBLIC;
  2298. if (!str2ba(str_addr, &device->address))
  2299. return;
  2300. return;
  2301. }
  2302. if (!strcmp(interface, "org.bluez.LEAdvertisingManager1")) {
  2303. struct btp_adapter *adapter;
  2304. adapter = find_adapter_by_path(path);
  2305. if (!adapter)
  2306. return;
  2307. adapter->ad_proxy = proxy;
  2308. return;
  2309. }
  2310. if (!strcmp(interface, "org.bluez.AgentManager1")) {
  2311. ag.proxy = proxy;
  2312. return;
  2313. }
  2314. }
  2315. static bool device_match_by_proxy(const void *a, const void *b)
  2316. {
  2317. const struct btp_device *device = a;
  2318. const struct l_dbus_proxy *proxy = b;
  2319. return device->proxy == proxy;
  2320. }
  2321. static void proxy_removed(struct l_dbus_proxy *proxy, void *user_data)
  2322. {
  2323. const char *interface = l_dbus_proxy_get_interface(proxy);
  2324. const char *path = l_dbus_proxy_get_path(proxy);
  2325. l_info("Proxy removed: %s (%s)", interface, path);
  2326. if (!strcmp(interface, "org.bluez.Adapter1")) {
  2327. l_info("Adapter removed, terminating.");
  2328. l_main_quit();
  2329. return;
  2330. }
  2331. if (!strcmp(interface, "org.bluez.Device1")) {
  2332. struct btp_adapter *adapter;
  2333. char *str;
  2334. if (!l_dbus_proxy_get_property(proxy, "Adapter", "o", &str))
  2335. return;
  2336. adapter = find_adapter_by_path(str);
  2337. if (!adapter)
  2338. return;
  2339. l_queue_remove_if(adapter->devices, device_match_by_proxy,
  2340. proxy);
  2341. return;
  2342. }
  2343. }
  2344. static void property_changed(struct l_dbus_proxy *proxy, const char *name,
  2345. struct l_dbus_message *msg, void *user_data)
  2346. {
  2347. const char *interface = l_dbus_proxy_get_interface(proxy);
  2348. const char *path = l_dbus_proxy_get_path(proxy);
  2349. l_info("property_changed %s %s %s", name, path, interface);
  2350. if (!strcmp(interface, "org.bluez.Adapter1")) {
  2351. struct btp_adapter *adapter = find_adapter_by_proxy(proxy);
  2352. uint32_t new_settings;
  2353. if (!adapter)
  2354. return;
  2355. new_settings = adapter->current_settings;
  2356. if (!strcmp(name, "Powered")) {
  2357. bool prop;
  2358. if (!l_dbus_message_get_arguments(msg, "b", &prop))
  2359. return;
  2360. if (prop)
  2361. new_settings |= BTP_GAP_SETTING_POWERED;
  2362. else
  2363. new_settings &= ~BTP_GAP_SETTING_POWERED;
  2364. } else if (!strcmp(name, "Discoverable")) {
  2365. bool prop;
  2366. if (!l_dbus_message_get_arguments(msg, "b", &prop))
  2367. return;
  2368. if (prop)
  2369. new_settings |= BTP_GAP_SETTING_DISCOVERABLE;
  2370. else
  2371. new_settings &= ~BTP_GAP_SETTING_DISCOVERABLE;
  2372. }
  2373. if (!strcmp(name, "Pairable")) {
  2374. bool prop;
  2375. if (!l_dbus_message_get_arguments(msg, "b", &prop))
  2376. return;
  2377. if (prop)
  2378. new_settings |= BTP_GAP_SETTING_BONDABLE;
  2379. else
  2380. new_settings &= ~BTP_GAP_SETTING_BONDABLE;
  2381. }
  2382. if (new_settings != adapter->current_settings)
  2383. update_current_settings(adapter, new_settings);
  2384. return;
  2385. } else if (!strcmp(interface, "org.bluez.Device1")) {
  2386. if (!strcmp(name, "RSSI")) {
  2387. int16_t rssi;
  2388. if (!l_dbus_message_get_arguments(msg, "n", &rssi))
  2389. return;
  2390. btp_gap_device_found_ev(proxy);
  2391. } else if (!strcmp(name, "Connected")) {
  2392. bool prop;
  2393. if (!l_dbus_message_get_arguments(msg, "b", &prop))
  2394. return;
  2395. btp_gap_device_connection_ev(proxy, prop);
  2396. } else if (!strcmp(name, "AddressType")) {
  2397. /* Addres property change came first along with address
  2398. * type.
  2399. */
  2400. btp_identity_resolved_ev(proxy);
  2401. }
  2402. }
  2403. }
  2404. static void client_connected(struct l_dbus *dbus, void *user_data)
  2405. {
  2406. l_info("D-Bus client connected");
  2407. }
  2408. static void client_disconnected(struct l_dbus *dbus, void *user_data)
  2409. {
  2410. l_info("D-Bus client disconnected, terminated");
  2411. l_main_quit();
  2412. }
  2413. static void btp_disconnect_handler(struct btp *btp, void *user_data)
  2414. {
  2415. l_info("btp disconnected");
  2416. l_main_quit();
  2417. }
  2418. static void client_ready(struct l_dbus_client *client, void *user_data)
  2419. {
  2420. l_info("D-Bus client ready, connecting BTP");
  2421. btp = btp_new(socket_path);
  2422. if (!btp) {
  2423. l_error("Failed to connect BTP, terminating");
  2424. l_main_quit();
  2425. return;
  2426. }
  2427. btp_set_disconnect_handler(btp, btp_disconnect_handler, NULL, NULL);
  2428. register_core_service();
  2429. btp_send(btp, BTP_CORE_SERVICE, BTP_EV_CORE_READY,
  2430. BTP_INDEX_NON_CONTROLLER, 0, NULL);
  2431. }
  2432. static void ready_callback(void *user_data)
  2433. {
  2434. if (!l_dbus_object_manager_enable(dbus, "/"))
  2435. l_info("Unable to register the ObjectManager");
  2436. }
  2437. static void usage(void)
  2438. {
  2439. l_info("btpclient - Bluetooth tester");
  2440. l_info("Usage:");
  2441. l_info("\tbtpclient [options]");
  2442. l_info("options:\n"
  2443. "\t-s, --socket <socket> Socket to use for BTP\n"
  2444. "\t-q, --quiet Don't emit any logs\n"
  2445. "\t-v, --version Show version\n"
  2446. "\t-h, --help Show help options");
  2447. }
  2448. static const struct option options[] = {
  2449. { "socket", 1, 0, 's' },
  2450. { "quiet", 0, 0, 'q' },
  2451. { "version", 0, 0, 'v' },
  2452. { "help", 0, 0, 'h' },
  2453. { 0, 0, 0, 0 }
  2454. };
  2455. int main(int argc, char *argv[])
  2456. {
  2457. struct l_dbus_client *client;
  2458. int opt;
  2459. l_log_set_stderr();
  2460. while ((opt = getopt_long(argc, argv, "+hs:vq", options, NULL)) != -1) {
  2461. switch (opt) {
  2462. case 's':
  2463. socket_path = l_strdup(optarg);
  2464. break;
  2465. case 'q':
  2466. l_log_set_null();
  2467. break;
  2468. case 'd':
  2469. break;
  2470. case 'v':
  2471. l_info("%s", VERSION);
  2472. return EXIT_SUCCESS;
  2473. case 'h':
  2474. default:
  2475. usage();
  2476. return EXIT_SUCCESS;
  2477. }
  2478. }
  2479. if (!socket_path) {
  2480. l_info("Socket option is required");
  2481. l_info("Type --help for usage");
  2482. return EXIT_FAILURE;
  2483. }
  2484. if (!l_main_init())
  2485. return EXIT_FAILURE;
  2486. adapters = l_queue_new();
  2487. dbus = l_dbus_new_default(L_DBUS_SYSTEM_BUS);
  2488. l_dbus_set_ready_handler(dbus, ready_callback, NULL, NULL);
  2489. client = l_dbus_client_new(dbus, "org.bluez", "/org/bluez");
  2490. l_dbus_client_set_connect_handler(client, client_connected, NULL, NULL);
  2491. l_dbus_client_set_disconnect_handler(client, client_disconnected, NULL,
  2492. NULL);
  2493. l_dbus_client_set_proxy_handlers(client, proxy_added, proxy_removed,
  2494. property_changed, NULL, NULL);
  2495. l_dbus_client_set_ready_handler(client, client_ready, NULL, NULL);
  2496. l_main_run_with_signal(signal_handler, NULL);
  2497. l_dbus_client_destroy(client);
  2498. l_dbus_destroy(dbus);
  2499. btp_cleanup(btp);
  2500. l_queue_destroy(adapters, (l_queue_destroy_func_t)btp_adapter_free);
  2501. l_free(socket_path);
  2502. l_main_exit();
  2503. return EXIT_SUCCESS;
  2504. }