hcitool.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2000-2001 Qualcomm Incorporated
  7. * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
  8. * Copyright (C) 2002-2010 Marcel Holtmann <marcel@holtmann.org>
  9. *
  10. *
  11. */
  12. #ifdef HAVE_CONFIG_H
  13. #include <config.h>
  14. #endif
  15. #define _GNU_SOURCE
  16. #include <stdio.h>
  17. #include <errno.h>
  18. #include <ctype.h>
  19. #include <fcntl.h>
  20. #include <unistd.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <getopt.h>
  24. #include <sys/param.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/socket.h>
  27. #include <signal.h>
  28. #include "lib/bluetooth.h"
  29. #include "lib/hci.h"
  30. #include "lib/hci_lib.h"
  31. #include "src/oui.h"
  32. #ifndef MIN
  33. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  34. #endif
  35. /* Unofficial value, might still change */
  36. #define LE_LINK 0x80
  37. #define FLAGS_AD_TYPE 0x01
  38. #define FLAGS_LIMITED_MODE_BIT 0x01
  39. #define FLAGS_GENERAL_MODE_BIT 0x02
  40. #define EIR_FLAGS 0x01 /* flags */
  41. #define EIR_UUID16_SOME 0x02 /* 16-bit UUID, more available */
  42. #define EIR_UUID16_ALL 0x03 /* 16-bit UUID, all listed */
  43. #define EIR_UUID32_SOME 0x04 /* 32-bit UUID, more available */
  44. #define EIR_UUID32_ALL 0x05 /* 32-bit UUID, all listed */
  45. #define EIR_UUID128_SOME 0x06 /* 128-bit UUID, more available */
  46. #define EIR_UUID128_ALL 0x07 /* 128-bit UUID, all listed */
  47. #define EIR_NAME_SHORT 0x08 /* shortened local name */
  48. #define EIR_NAME_COMPLETE 0x09 /* complete local name */
  49. #define EIR_TX_POWER 0x0A /* transmit power level */
  50. #define EIR_DEVICE_ID 0x10 /* device ID */
  51. #define for_each_opt(opt, long, short) while ((opt=getopt_long(argc, argv, short ? short:"+", long, NULL)) != -1)
  52. static volatile int signal_received = 0;
  53. static void usage(void);
  54. static int str2buf(const char *str, uint8_t *buf, size_t blen)
  55. {
  56. int i, dlen;
  57. if (str == NULL)
  58. return -EINVAL;
  59. memset(buf, 0, blen);
  60. dlen = MIN((strlen(str) / 2), blen);
  61. for (i = 0; i < dlen; i++)
  62. sscanf(str + (i * 2), "%02hhX", &buf[i]);
  63. return 0;
  64. }
  65. static int dev_info(int s, int dev_id, long arg)
  66. {
  67. struct hci_dev_info di = { .dev_id = dev_id };
  68. char addr[18];
  69. if (ioctl(s, HCIGETDEVINFO, (void *) &di))
  70. return 0;
  71. ba2str(&di.bdaddr, addr);
  72. printf("\t%s\t%s\n", di.name, addr);
  73. return 0;
  74. }
  75. static void helper_arg(int min_num_arg, int max_num_arg, int *argc,
  76. char ***argv, const char *usage)
  77. {
  78. *argc -= optind;
  79. /* too many arguments, but when "max_num_arg < min_num_arg" then no
  80. limiting (prefer "max_num_arg=-1" to gen infinity)
  81. */
  82. if ( (*argc > max_num_arg) && (max_num_arg >= min_num_arg ) ) {
  83. fprintf(stderr, "%s: too many arguments (maximal: %i)\n",
  84. *argv[0], max_num_arg);
  85. printf("%s", usage);
  86. exit(1);
  87. }
  88. /* print usage */
  89. if (*argc < min_num_arg) {
  90. fprintf(stderr, "%s: too few arguments (minimal: %i)\n",
  91. *argv[0], min_num_arg);
  92. printf("%s", usage);
  93. exit(0);
  94. }
  95. *argv += optind;
  96. }
  97. static char *type2str(uint8_t type)
  98. {
  99. switch (type) {
  100. case SCO_LINK:
  101. return "SCO";
  102. case ACL_LINK:
  103. return "ACL";
  104. case ESCO_LINK:
  105. return "eSCO";
  106. case LE_LINK:
  107. return "LE";
  108. default:
  109. return "Unknown";
  110. }
  111. }
  112. static int conn_list(int s, int dev_id, long arg)
  113. {
  114. struct hci_conn_list_req *cl;
  115. struct hci_conn_info *ci;
  116. int id = arg;
  117. int i;
  118. if (id != -1 && dev_id != id)
  119. return 0;
  120. if (!(cl = malloc(10 * sizeof(*ci) + sizeof(*cl)))) {
  121. perror("Can't allocate memory");
  122. exit(1);
  123. }
  124. cl->dev_id = dev_id;
  125. cl->conn_num = 10;
  126. ci = cl->conn_info;
  127. if (ioctl(s, HCIGETCONNLIST, (void *) cl)) {
  128. perror("Can't get connection list");
  129. exit(1);
  130. }
  131. for (i = 0; i < cl->conn_num; i++, ci++) {
  132. char addr[18];
  133. char *str;
  134. ba2str(&ci->bdaddr, addr);
  135. str = hci_lmtostr(ci->link_mode);
  136. printf("\t%s %s %s handle %d state %d lm %s\n",
  137. ci->out ? "<" : ">", type2str(ci->type),
  138. addr, ci->handle, ci->state, str);
  139. bt_free(str);
  140. }
  141. free(cl);
  142. return 0;
  143. }
  144. static int find_conn(int s, int dev_id, long arg)
  145. {
  146. struct hci_conn_list_req *cl;
  147. struct hci_conn_info *ci;
  148. int i;
  149. if (!(cl = malloc(10 * sizeof(*ci) + sizeof(*cl)))) {
  150. perror("Can't allocate memory");
  151. exit(1);
  152. }
  153. cl->dev_id = dev_id;
  154. cl->conn_num = 10;
  155. ci = cl->conn_info;
  156. if (ioctl(s, HCIGETCONNLIST, (void *) cl)) {
  157. perror("Can't get connection list");
  158. exit(1);
  159. }
  160. for (i = 0; i < cl->conn_num; i++, ci++)
  161. if (!bacmp((bdaddr_t *) arg, &ci->bdaddr)) {
  162. free(cl);
  163. return 1;
  164. }
  165. free(cl);
  166. return 0;
  167. }
  168. static void hex_dump(char *pref, int width, unsigned char *buf, int len)
  169. {
  170. register int i,n;
  171. for (i = 0, n = 1; i < len; i++, n++) {
  172. if (n == 1)
  173. printf("%s", pref);
  174. printf("%2.2X ", buf[i]);
  175. if (n == width) {
  176. printf("\n");
  177. n = 0;
  178. }
  179. }
  180. if (i && n!=1)
  181. printf("\n");
  182. }
  183. static char *get_minor_device_name(int major, int minor)
  184. {
  185. switch (major) {
  186. case 0: /* misc */
  187. return "";
  188. case 1: /* computer */
  189. switch (minor) {
  190. case 0:
  191. return "Uncategorized";
  192. case 1:
  193. return "Desktop workstation";
  194. case 2:
  195. return "Server";
  196. case 3:
  197. return "Laptop";
  198. case 4:
  199. return "Handheld";
  200. case 5:
  201. return "Palm";
  202. case 6:
  203. return "Wearable";
  204. }
  205. break;
  206. case 2: /* phone */
  207. switch (minor) {
  208. case 0:
  209. return "Uncategorized";
  210. case 1:
  211. return "Cellular";
  212. case 2:
  213. return "Cordless";
  214. case 3:
  215. return "Smart phone";
  216. case 4:
  217. return "Wired modem or voice gateway";
  218. case 5:
  219. return "Common ISDN Access";
  220. case 6:
  221. return "Sim Card Reader";
  222. }
  223. break;
  224. case 3: /* lan access */
  225. if (minor == 0)
  226. return "Uncategorized";
  227. switch (minor / 8) {
  228. case 0:
  229. return "Fully available";
  230. case 1:
  231. return "1-17% utilized";
  232. case 2:
  233. return "17-33% utilized";
  234. case 3:
  235. return "33-50% utilized";
  236. case 4:
  237. return "50-67% utilized";
  238. case 5:
  239. return "67-83% utilized";
  240. case 6:
  241. return "83-99% utilized";
  242. case 7:
  243. return "No service available";
  244. }
  245. break;
  246. case 4: /* audio/video */
  247. switch (minor) {
  248. case 0:
  249. return "Uncategorized";
  250. case 1:
  251. return "Device conforms to the Headset profile";
  252. case 2:
  253. return "Hands-free";
  254. /* 3 is reserved */
  255. case 4:
  256. return "Microphone";
  257. case 5:
  258. return "Loudspeaker";
  259. case 6:
  260. return "Headphones";
  261. case 7:
  262. return "Portable Audio";
  263. case 8:
  264. return "Car Audio";
  265. case 9:
  266. return "Set-top box";
  267. case 10:
  268. return "HiFi Audio Device";
  269. case 11:
  270. return "VCR";
  271. case 12:
  272. return "Video Camera";
  273. case 13:
  274. return "Camcorder";
  275. case 14:
  276. return "Video Monitor";
  277. case 15:
  278. return "Video Display and Loudspeaker";
  279. case 16:
  280. return "Video Conferencing";
  281. /* 17 is reserved */
  282. case 18:
  283. return "Gaming/Toy";
  284. }
  285. break;
  286. case 5: /* peripheral */ {
  287. static char cls_str[48]; cls_str[0] = 0;
  288. switch (minor & 48) {
  289. case 16:
  290. strncpy(cls_str, "Keyboard", sizeof(cls_str));
  291. break;
  292. case 32:
  293. strncpy(cls_str, "Pointing device", sizeof(cls_str));
  294. break;
  295. case 48:
  296. strncpy(cls_str, "Combo keyboard/pointing device", sizeof(cls_str));
  297. break;
  298. }
  299. if ((minor & 15) && (strlen(cls_str) > 0))
  300. strcat(cls_str, "/");
  301. switch (minor & 15) {
  302. case 0:
  303. break;
  304. case 1:
  305. strncat(cls_str, "Joystick",
  306. sizeof(cls_str) - strlen(cls_str) - 1);
  307. break;
  308. case 2:
  309. strncat(cls_str, "Gamepad",
  310. sizeof(cls_str) - strlen(cls_str) - 1);
  311. break;
  312. case 3:
  313. strncat(cls_str, "Remote control",
  314. sizeof(cls_str) - strlen(cls_str) - 1);
  315. break;
  316. case 4:
  317. strncat(cls_str, "Sensing device",
  318. sizeof(cls_str) - strlen(cls_str) - 1);
  319. break;
  320. case 5:
  321. strncat(cls_str, "Digitizer tablet",
  322. sizeof(cls_str) - strlen(cls_str) - 1);
  323. break;
  324. case 6:
  325. strncat(cls_str, "Card reader",
  326. sizeof(cls_str) - strlen(cls_str) - 1);
  327. break;
  328. default:
  329. strncat(cls_str, "(reserved)",
  330. sizeof(cls_str) - strlen(cls_str) - 1);
  331. break;
  332. }
  333. if (strlen(cls_str) > 0)
  334. return cls_str;
  335. break;
  336. }
  337. case 6: /* imaging */
  338. if (minor & 4)
  339. return "Display";
  340. if (minor & 8)
  341. return "Camera";
  342. if (minor & 16)
  343. return "Scanner";
  344. if (minor & 32)
  345. return "Printer";
  346. break;
  347. case 7: /* wearable */
  348. switch (minor) {
  349. case 1:
  350. return "Wrist Watch";
  351. case 2:
  352. return "Pager";
  353. case 3:
  354. return "Jacket";
  355. case 4:
  356. return "Helmet";
  357. case 5:
  358. return "Glasses";
  359. }
  360. break;
  361. case 8: /* toy */
  362. switch (minor) {
  363. case 1:
  364. return "Robot";
  365. case 2:
  366. return "Vehicle";
  367. case 3:
  368. return "Doll / Action Figure";
  369. case 4:
  370. return "Controller";
  371. case 5:
  372. return "Game";
  373. }
  374. break;
  375. case 63: /* uncategorised */
  376. return "";
  377. }
  378. return "Unknown (reserved) minor device class";
  379. }
  380. static char *major_classes[] = {
  381. "Miscellaneous", "Computer", "Phone", "LAN Access",
  382. "Audio/Video", "Peripheral", "Imaging", "Uncategorized"
  383. };
  384. /* Display local devices */
  385. static struct option dev_options[] = {
  386. { "help", 0, 0, 'h' },
  387. {0, 0, 0, 0 }
  388. };
  389. static const char *dev_help =
  390. "Usage:\n"
  391. "\tdev\n";
  392. static void cmd_dev(int dev_id, int argc, char **argv)
  393. {
  394. int opt;
  395. for_each_opt(opt, dev_options, NULL) {
  396. switch (opt) {
  397. default:
  398. printf("%s", dev_help);
  399. return;
  400. }
  401. }
  402. helper_arg(0, 0, &argc, &argv, dev_help);
  403. printf("Devices:\n");
  404. hci_for_each_dev(HCI_UP, dev_info, 0);
  405. }
  406. /* Inquiry */
  407. static struct option inq_options[] = {
  408. { "help", 0, 0, 'h' },
  409. { "length", 1, 0, 'l' },
  410. { "numrsp", 1, 0, 'n' },
  411. { "iac", 1, 0, 'i' },
  412. { "flush", 0, 0, 'f' },
  413. { 0, 0, 0, 0 }
  414. };
  415. static const char *inq_help =
  416. "Usage:\n"
  417. "\tinq [--length=N] maximum inquiry duration in 1.28 s units\n"
  418. "\t [--numrsp=N] specify maximum number of inquiry responses\n"
  419. "\t [--iac=lap] specify the inquiry access code\n"
  420. "\t [--flush] flush the inquiry cache\n";
  421. static void cmd_inq(int dev_id, int argc, char **argv)
  422. {
  423. inquiry_info *info = NULL;
  424. uint8_t lap[3] = { 0x33, 0x8b, 0x9e };
  425. int num_rsp, length, flags;
  426. char addr[18];
  427. int i, l, opt;
  428. length = 8; /* ~10 seconds */
  429. num_rsp = 0;
  430. flags = 0;
  431. for_each_opt(opt, inq_options, NULL) {
  432. switch (opt) {
  433. case 'l':
  434. length = atoi(optarg);
  435. break;
  436. case 'n':
  437. num_rsp = atoi(optarg);
  438. break;
  439. case 'i':
  440. l = strtoul(optarg, 0, 16);
  441. if (!strcasecmp(optarg, "giac")) {
  442. l = 0x9e8b33;
  443. } else if (!strcasecmp(optarg, "liac")) {
  444. l = 0x9e8b00;
  445. } if (l < 0x9e8b00 || l > 0x9e8b3f) {
  446. printf("Invalid access code 0x%x\n", l);
  447. exit(1);
  448. }
  449. lap[0] = (l & 0xff);
  450. lap[1] = (l >> 8) & 0xff;
  451. lap[2] = (l >> 16) & 0xff;
  452. break;
  453. case 'f':
  454. flags |= IREQ_CACHE_FLUSH;
  455. break;
  456. default:
  457. printf("%s", inq_help);
  458. return;
  459. }
  460. }
  461. helper_arg(0, 0, &argc, &argv, inq_help);
  462. printf("Inquiring ...\n");
  463. num_rsp = hci_inquiry(dev_id, length, num_rsp, lap, &info, flags);
  464. if (num_rsp < 0) {
  465. perror("Inquiry failed.");
  466. exit(1);
  467. }
  468. for (i = 0; i < num_rsp; i++) {
  469. ba2str(&(info+i)->bdaddr, addr);
  470. printf("\t%s\tclock offset: 0x%4.4x\tclass: 0x%2.2x%2.2x%2.2x\n",
  471. addr, btohs((info+i)->clock_offset),
  472. (info+i)->dev_class[2],
  473. (info+i)->dev_class[1],
  474. (info+i)->dev_class[0]);
  475. }
  476. bt_free(info);
  477. }
  478. /* Device scanning */
  479. static struct option scan_options[] = {
  480. { "help", 0, 0, 'h' },
  481. { "length", 1, 0, 'l' },
  482. { "numrsp", 1, 0, 'n' },
  483. { "iac", 1, 0, 'i' },
  484. { "flush", 0, 0, 'f' },
  485. { "class", 0, 0, 'C' },
  486. { "info", 0, 0, 'I' },
  487. { "oui", 0, 0, 'O' },
  488. { "all", 0, 0, 'A' },
  489. { "ext", 0, 0, 'A' },
  490. { 0, 0, 0, 0 }
  491. };
  492. static const char *scan_help =
  493. "Usage:\n"
  494. "\tscan [--length=N] [--numrsp=N] [--iac=lap] [--flush] [--class] [--info] [--oui] [--refresh]\n";
  495. static void cmd_scan(int dev_id, int argc, char **argv)
  496. {
  497. inquiry_info *info = NULL;
  498. uint8_t lap[3] = { 0x33, 0x8b, 0x9e };
  499. int num_rsp, length, flags;
  500. uint8_t cls[3], features[8];
  501. char addr[18], name[249], *comp;
  502. struct hci_version version;
  503. struct hci_dev_info di;
  504. struct hci_conn_info_req *cr;
  505. int extcls = 0, extinf = 0, extoui = 0;
  506. int i, n, l, opt, dd, cc;
  507. length = 8; /* ~10 seconds */
  508. num_rsp = 0;
  509. flags = 0;
  510. for_each_opt(opt, scan_options, NULL) {
  511. switch (opt) {
  512. case 'l':
  513. length = atoi(optarg);
  514. break;
  515. case 'n':
  516. num_rsp = atoi(optarg);
  517. break;
  518. case 'i':
  519. l = strtoul(optarg, 0, 16);
  520. if (!strcasecmp(optarg, "giac")) {
  521. l = 0x9e8b33;
  522. } else if (!strcasecmp(optarg, "liac")) {
  523. l = 0x9e8b00;
  524. } else if (l < 0x9e8b00 || l > 0x9e8b3f) {
  525. printf("Invalid access code 0x%x\n", l);
  526. exit(1);
  527. }
  528. lap[0] = (l & 0xff);
  529. lap[1] = (l >> 8) & 0xff;
  530. lap[2] = (l >> 16) & 0xff;
  531. break;
  532. case 'f':
  533. flags |= IREQ_CACHE_FLUSH;
  534. break;
  535. case 'C':
  536. extcls = 1;
  537. break;
  538. case 'I':
  539. extinf = 1;
  540. break;
  541. case 'O':
  542. extoui = 1;
  543. break;
  544. case 'A':
  545. extcls = 1;
  546. extinf = 1;
  547. extoui = 1;
  548. break;
  549. default:
  550. printf("%s", scan_help);
  551. return;
  552. }
  553. }
  554. helper_arg(0, 0, &argc, &argv, scan_help);
  555. if (dev_id < 0) {
  556. dev_id = hci_get_route(NULL);
  557. if (dev_id < 0) {
  558. perror("Device is not available");
  559. exit(1);
  560. }
  561. }
  562. if (hci_devinfo(dev_id, &di) < 0) {
  563. perror("Can't get device info");
  564. exit(1);
  565. }
  566. printf("Scanning ...\n");
  567. num_rsp = hci_inquiry(dev_id, length, num_rsp, lap, &info, flags);
  568. if (num_rsp < 0) {
  569. perror("Inquiry failed");
  570. exit(1);
  571. }
  572. dd = hci_open_dev(dev_id);
  573. if (dd < 0) {
  574. perror("HCI device open failed");
  575. free(info);
  576. exit(1);
  577. }
  578. if (extcls || extinf || extoui)
  579. printf("\n");
  580. for (i = 0; i < num_rsp; i++) {
  581. uint16_t handle = 0;
  582. if (!extcls && !extinf && !extoui) {
  583. ba2str(&(info+i)->bdaddr, addr);
  584. if (hci_read_remote_name_with_clock_offset(dd,
  585. &(info+i)->bdaddr,
  586. (info+i)->pscan_rep_mode,
  587. (info+i)->clock_offset | 0x8000,
  588. sizeof(name), name, 100000) < 0)
  589. strcpy(name, "n/a");
  590. for (n = 0; n < 248 && name[n]; n++) {
  591. if ((unsigned char) name[i] < 32 || name[i] == 127)
  592. name[i] = '.';
  593. }
  594. name[248] = '\0';
  595. printf("\t%s\t%s\n", addr, name);
  596. continue;
  597. }
  598. ba2str(&(info+i)->bdaddr, addr);
  599. printf("BD Address:\t%s [mode %d, clkoffset 0x%4.4x]\n", addr,
  600. (info+i)->pscan_rep_mode, btohs((info+i)->clock_offset));
  601. if (extoui) {
  602. comp = batocomp(&(info+i)->bdaddr);
  603. if (comp) {
  604. char oui[9];
  605. ba2oui(&(info+i)->bdaddr, oui);
  606. printf("OUI company:\t%s (%s)\n", comp, oui);
  607. free(comp);
  608. }
  609. }
  610. cc = 0;
  611. if (extinf) {
  612. cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
  613. if (cr) {
  614. bacpy(&cr->bdaddr, &(info+i)->bdaddr);
  615. cr->type = ACL_LINK;
  616. if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
  617. handle = 0;
  618. cc = 1;
  619. } else {
  620. handle = htobs(cr->conn_info->handle);
  621. cc = 0;
  622. }
  623. free(cr);
  624. }
  625. if (cc) {
  626. if (hci_create_connection(dd, &(info+i)->bdaddr,
  627. htobs(di.pkt_type & ACL_PTYPE_MASK),
  628. (info+i)->clock_offset | 0x8000,
  629. 0x01, &handle, 25000) < 0) {
  630. handle = 0;
  631. cc = 0;
  632. }
  633. }
  634. }
  635. if (hci_read_remote_name_with_clock_offset(dd,
  636. &(info+i)->bdaddr,
  637. (info+i)->pscan_rep_mode,
  638. (info+i)->clock_offset | 0x8000,
  639. sizeof(name), name, 100000) < 0) {
  640. } else {
  641. for (n = 0; n < 248 && name[n]; n++) {
  642. if ((unsigned char) name[i] < 32 || name[i] == 127)
  643. name[i] = '.';
  644. }
  645. name[248] = '\0';
  646. }
  647. if (strlen(name) > 0)
  648. printf("Device name:\t%s\n", name);
  649. if (extcls) {
  650. memcpy(cls, (info+i)->dev_class, 3);
  651. printf("Device class:\t");
  652. if ((cls[1] & 0x1f) > sizeof(major_classes) / sizeof(char *))
  653. printf("Invalid");
  654. else
  655. printf("%s, %s", major_classes[cls[1] & 0x1f],
  656. get_minor_device_name(cls[1] & 0x1f, cls[0] >> 2));
  657. printf(" (0x%2.2x%2.2x%2.2x)\n", cls[2], cls[1], cls[0]);
  658. }
  659. if (extinf && handle > 0) {
  660. if (hci_read_remote_version(dd, handle, &version, 20000) == 0) {
  661. char *ver = lmp_vertostr(version.lmp_ver);
  662. printf("Manufacturer:\t%s (%d)\n",
  663. bt_compidtostr(version.manufacturer),
  664. version.manufacturer);
  665. printf("LMP version:\t%s (0x%x) [subver 0x%x]\n",
  666. ver ? ver : "n/a",
  667. version.lmp_ver, version.lmp_subver);
  668. if (ver)
  669. bt_free(ver);
  670. }
  671. if (hci_read_remote_features(dd, handle, features, 20000) == 0) {
  672. char *tmp = lmp_featurestostr(features, "\t\t", 63);
  673. printf("LMP features:\t0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x"
  674. " 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n",
  675. features[0], features[1],
  676. features[2], features[3],
  677. features[4], features[5],
  678. features[6], features[7]);
  679. printf("%s\n", tmp);
  680. bt_free(tmp);
  681. }
  682. if (cc) {
  683. usleep(10000);
  684. hci_disconnect(dd, handle, HCI_OE_USER_ENDED_CONNECTION, 10000);
  685. }
  686. }
  687. printf("\n");
  688. }
  689. bt_free(info);
  690. hci_close_dev(dd);
  691. }
  692. /* Remote name */
  693. static struct option name_options[] = {
  694. { "help", 0, 0, 'h' },
  695. { 0, 0, 0, 0 }
  696. };
  697. static const char *name_help =
  698. "Usage:\n"
  699. "\tname <bdaddr>\n";
  700. static void cmd_name(int dev_id, int argc, char **argv)
  701. {
  702. bdaddr_t bdaddr;
  703. char name[248];
  704. int opt, dd;
  705. for_each_opt(opt, name_options, NULL) {
  706. switch (opt) {
  707. default:
  708. printf("%s", name_help);
  709. return;
  710. }
  711. }
  712. helper_arg(1, 1, &argc, &argv, name_help);
  713. str2ba(argv[0], &bdaddr);
  714. if (dev_id < 0) {
  715. dev_id = hci_get_route(&bdaddr);
  716. if (dev_id < 0) {
  717. fprintf(stderr, "Device is not available.\n");
  718. exit(1);
  719. }
  720. }
  721. dd = hci_open_dev(dev_id);
  722. if (dd < 0) {
  723. perror("HCI device open failed");
  724. exit(1);
  725. }
  726. if (hci_read_remote_name(dd, &bdaddr, sizeof(name), name, 25000) == 0)
  727. printf("%s\n", name);
  728. hci_close_dev(dd);
  729. }
  730. /* Info about remote device */
  731. static struct option info_options[] = {
  732. { "help", 0, 0, 'h' },
  733. { 0, 0, 0, 0 }
  734. };
  735. static const char *info_help =
  736. "Usage:\n"
  737. "\tinfo <bdaddr>\n";
  738. static void cmd_info(int dev_id, int argc, char **argv)
  739. {
  740. bdaddr_t bdaddr;
  741. uint16_t handle;
  742. uint8_t features[8], max_page = 0;
  743. char name[249], *comp, *tmp;
  744. struct hci_version version;
  745. struct hci_dev_info di;
  746. struct hci_conn_info_req *cr;
  747. int i, opt, dd, cc = 0;
  748. for_each_opt(opt, info_options, NULL) {
  749. switch (opt) {
  750. default:
  751. printf("%s", info_help);
  752. return;
  753. }
  754. }
  755. helper_arg(1, 1, &argc, &argv, info_help);
  756. str2ba(argv[0], &bdaddr);
  757. if (dev_id < 0)
  758. dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
  759. if (dev_id < 0)
  760. dev_id = hci_get_route(&bdaddr);
  761. if (dev_id < 0) {
  762. fprintf(stderr, "Device is not available or not connected.\n");
  763. exit(1);
  764. }
  765. if (hci_devinfo(dev_id, &di) < 0) {
  766. perror("Can't get device info");
  767. exit(1);
  768. }
  769. printf("Requesting information ...\n");
  770. dd = hci_open_dev(dev_id);
  771. if (dd < 0) {
  772. perror("HCI device open failed");
  773. exit(1);
  774. }
  775. cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
  776. if (!cr) {
  777. perror("Can't get connection info");
  778. close(dd);
  779. exit(1);
  780. }
  781. bacpy(&cr->bdaddr, &bdaddr);
  782. cr->type = ACL_LINK;
  783. if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
  784. if (hci_create_connection(dd, &bdaddr,
  785. htobs(di.pkt_type & ACL_PTYPE_MASK),
  786. 0, 0x01, &handle, 25000) < 0) {
  787. perror("Can't create connection");
  788. free(cr);
  789. close(dd);
  790. exit(1);
  791. }
  792. sleep(1);
  793. cc = 1;
  794. } else
  795. handle = htobs(cr->conn_info->handle);
  796. free(cr);
  797. printf("\tBD Address: %s\n", argv[0]);
  798. comp = batocomp(&bdaddr);
  799. if (comp) {
  800. char oui[9];
  801. ba2oui(&bdaddr, oui);
  802. printf("\tOUI Company: %s (%s)\n", comp, oui);
  803. free(comp);
  804. }
  805. if (hci_read_remote_name(dd, &bdaddr, sizeof(name), name, 25000) == 0)
  806. printf("\tDevice Name: %s\n", name);
  807. if (hci_read_remote_version(dd, handle, &version, 20000) == 0) {
  808. char *ver = lmp_vertostr(version.lmp_ver);
  809. printf("\tLMP Version: %s (0x%x) LMP Subversion: 0x%x\n"
  810. "\tManufacturer: %s (%d)\n",
  811. ver ? ver : "n/a",
  812. version.lmp_ver,
  813. version.lmp_subver,
  814. bt_compidtostr(version.manufacturer),
  815. version.manufacturer);
  816. if (ver)
  817. bt_free(ver);
  818. }
  819. memset(features, 0, sizeof(features));
  820. hci_read_remote_features(dd, handle, features, 20000);
  821. if ((di.features[7] & LMP_EXT_FEAT) && (features[7] & LMP_EXT_FEAT))
  822. hci_read_remote_ext_features(dd, handle, 0, &max_page,
  823. features, 20000);
  824. if (max_page < 1 && (features[6] & LMP_SIMPLE_PAIR))
  825. max_page = 1;
  826. printf("\tFeatures%s: 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x "
  827. "0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n",
  828. (max_page > 0) ? " page 0" : "",
  829. features[0], features[1], features[2], features[3],
  830. features[4], features[5], features[6], features[7]);
  831. tmp = lmp_featurestostr(features, "\t\t", 63);
  832. printf("%s\n", tmp);
  833. bt_free(tmp);
  834. for (i = 1; i <= max_page; i++) {
  835. if (hci_read_remote_ext_features(dd, handle, i, NULL,
  836. features, 20000) < 0)
  837. continue;
  838. printf("\tFeatures page %d: 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x "
  839. "0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n", i,
  840. features[0], features[1], features[2], features[3],
  841. features[4], features[5], features[6], features[7]);
  842. }
  843. if (cc) {
  844. usleep(10000);
  845. hci_disconnect(dd, handle, HCI_OE_USER_ENDED_CONNECTION, 10000);
  846. }
  847. hci_close_dev(dd);
  848. }
  849. /* Start periodic inquiry */
  850. static struct option spinq_options[] = {
  851. { "help", 0, 0, 'h' },
  852. { 0, 0, 0, 0 }
  853. };
  854. static const char *spinq_help =
  855. "Usage:\n"
  856. "\tspinq\n";
  857. static void cmd_spinq(int dev_id, int argc, char **argv)
  858. {
  859. uint8_t lap[3] = { 0x33, 0x8b, 0x9e };
  860. struct hci_request rq;
  861. periodic_inquiry_cp cp;
  862. int opt, dd;
  863. for_each_opt(opt, spinq_options, NULL) {
  864. switch (opt) {
  865. default:
  866. printf("%s", spinq_help);
  867. return;
  868. }
  869. }
  870. helper_arg(0, 0, &argc, &argv, spinq_help);
  871. if (dev_id < 0)
  872. dev_id = hci_get_route(NULL);
  873. dd = hci_open_dev(dev_id);
  874. if (dd < 0) {
  875. perror("Device open failed");
  876. exit(EXIT_FAILURE);
  877. }
  878. memset(&cp, 0, sizeof(cp));
  879. memcpy(cp.lap, lap, 3);
  880. cp.max_period = htobs(16);
  881. cp.min_period = htobs(10);
  882. cp.length = 8;
  883. cp.num_rsp = 0;
  884. memset(&rq, 0, sizeof(rq));
  885. rq.ogf = OGF_LINK_CTL;
  886. rq.ocf = OCF_PERIODIC_INQUIRY;
  887. rq.cparam = &cp;
  888. rq.clen = PERIODIC_INQUIRY_CP_SIZE;
  889. if (hci_send_req(dd, &rq, 100) < 0) {
  890. perror("Periodic inquiry failed");
  891. exit(EXIT_FAILURE);
  892. }
  893. hci_close_dev(dd);
  894. }
  895. /* Exit periodic inquiry */
  896. static struct option epinq_options[] = {
  897. { "help", 0, 0, 'h' },
  898. { 0, 0, 0, 0 }
  899. };
  900. static const char *epinq_help =
  901. "Usage:\n"
  902. "\tepinq\n";
  903. static void cmd_epinq(int dev_id, int argc, char **argv)
  904. {
  905. int opt, dd;
  906. for_each_opt(opt, epinq_options, NULL) {
  907. switch (opt) {
  908. default:
  909. printf("%s", epinq_help);
  910. return;
  911. }
  912. }
  913. helper_arg(0, 0, &argc, &argv, epinq_help);
  914. if (dev_id < 0)
  915. dev_id = hci_get_route(NULL);
  916. dd = hci_open_dev(dev_id);
  917. if (dd < 0) {
  918. perror("Device open failed");
  919. exit(EXIT_FAILURE);
  920. }
  921. if (hci_send_cmd(dd, OGF_LINK_CTL,
  922. OCF_EXIT_PERIODIC_INQUIRY, 0, NULL) < 0) {
  923. perror("Exit periodic inquiry failed");
  924. exit(EXIT_FAILURE);
  925. }
  926. hci_close_dev(dd);
  927. }
  928. /* Send arbitrary HCI commands */
  929. static struct option cmd_options[] = {
  930. { "help", 0, 0, 'h' },
  931. { 0, 0, 0, 0 }
  932. };
  933. static const char *cmd_help =
  934. "Usage:\n"
  935. "\tcmd <ogf> <ocf> [parameters]\n"
  936. "Example:\n"
  937. "\tcmd 0x03 0x0013 0x41 0x42 0x43 0x44\n";
  938. static void cmd_cmd(int dev_id, int argc, char **argv)
  939. {
  940. unsigned char buf[HCI_MAX_EVENT_SIZE], *ptr = buf;
  941. struct hci_filter flt;
  942. hci_event_hdr *hdr;
  943. int i, opt, len, dd;
  944. uint16_t ocf;
  945. uint8_t ogf;
  946. for_each_opt(opt, cmd_options, NULL) {
  947. switch (opt) {
  948. default:
  949. printf("%s", cmd_help);
  950. return;
  951. }
  952. }
  953. helper_arg(2, -1, &argc, &argv, cmd_help);
  954. if (dev_id < 0)
  955. dev_id = hci_get_route(NULL);
  956. errno = 0;
  957. ogf = strtol(argv[0], NULL, 16);
  958. ocf = strtol(argv[1], NULL, 16);
  959. if (errno == ERANGE || (ogf > 0x3f) || (ocf > 0x3ff)) {
  960. printf("%s", cmd_help);
  961. return;
  962. }
  963. for (i = 2, len = 0; i < argc && len < (int) sizeof(buf); i++, len++)
  964. *ptr++ = (uint8_t) strtol(argv[i], NULL, 16);
  965. dd = hci_open_dev(dev_id);
  966. if (dd < 0) {
  967. perror("Device open failed");
  968. exit(EXIT_FAILURE);
  969. }
  970. /* Setup filter */
  971. hci_filter_clear(&flt);
  972. hci_filter_set_ptype(HCI_EVENT_PKT, &flt);
  973. hci_filter_all_events(&flt);
  974. if (setsockopt(dd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
  975. perror("HCI filter setup failed");
  976. exit(EXIT_FAILURE);
  977. }
  978. printf("< HCI Command: ogf 0x%02x, ocf 0x%04x, plen %d\n", ogf, ocf, len);
  979. hex_dump(" ", 20, buf, len); fflush(stdout);
  980. if (hci_send_cmd(dd, ogf, ocf, len, buf) < 0) {
  981. perror("Send failed");
  982. exit(EXIT_FAILURE);
  983. }
  984. len = read(dd, buf, sizeof(buf));
  985. if (len < 0) {
  986. perror("Read failed");
  987. exit(EXIT_FAILURE);
  988. }
  989. hdr = (void *)(buf + 1);
  990. ptr = buf + (1 + HCI_EVENT_HDR_SIZE);
  991. len -= (1 + HCI_EVENT_HDR_SIZE);
  992. printf("> HCI Event: 0x%02x plen %d\n", hdr->evt, hdr->plen);
  993. hex_dump(" ", 20, ptr, len); fflush(stdout);
  994. hci_close_dev(dd);
  995. }
  996. /* Display active connections */
  997. static struct option con_options[] = {
  998. { "help", 0, 0, 'h' },
  999. { 0, 0, 0, 0 }
  1000. };
  1001. static const char *con_help =
  1002. "Usage:\n"
  1003. "\tcon\n";
  1004. static void cmd_con(int dev_id, int argc, char **argv)
  1005. {
  1006. int opt;
  1007. for_each_opt(opt, con_options, NULL) {
  1008. switch (opt) {
  1009. default:
  1010. printf("%s", con_help);
  1011. return;
  1012. }
  1013. }
  1014. helper_arg(0, 0, &argc, &argv, con_help);
  1015. printf("Connections:\n");
  1016. hci_for_each_dev(HCI_UP, conn_list, dev_id);
  1017. }
  1018. /* Create connection */
  1019. static struct option cc_options[] = {
  1020. { "help", 0, 0, 'h' },
  1021. { "role", 1, 0, 'r' },
  1022. { "ptype", 1, 0, 'p' },
  1023. { 0, 0, 0, 0 }
  1024. };
  1025. static const char *cc_help =
  1026. "Usage:\n"
  1027. "\tcc [--role=c|p] [--ptype=pkt_types] <bdaddr>\n"
  1028. "Example:\n"
  1029. "\tcc --ptype=dm1,dh3,dh5 01:02:03:04:05:06\n"
  1030. "\tcc --role=c 01:02:03:04:05:06\n";
  1031. static void cmd_cc(int dev_id, int argc, char **argv)
  1032. {
  1033. bdaddr_t bdaddr;
  1034. uint16_t handle;
  1035. uint8_t role;
  1036. unsigned int ptype;
  1037. int dd, opt;
  1038. role = 0x01;
  1039. ptype = HCI_DM1 | HCI_DM3 | HCI_DM5 | HCI_DH1 | HCI_DH3 | HCI_DH5;
  1040. for_each_opt(opt, cc_options, NULL) {
  1041. switch (opt) {
  1042. case 'p':
  1043. hci_strtoptype(optarg, &ptype);
  1044. break;
  1045. case 'r':
  1046. role = optarg[0] == 'm' || optarg[0] == 'c' ? 0 : 1;
  1047. break;
  1048. default:
  1049. printf("%s", cc_help);
  1050. return;
  1051. }
  1052. }
  1053. helper_arg(1, 1, &argc, &argv, cc_help);
  1054. str2ba(argv[0], &bdaddr);
  1055. if (dev_id < 0) {
  1056. dev_id = hci_get_route(&bdaddr);
  1057. if (dev_id < 0) {
  1058. fprintf(stderr, "Device is not available.\n");
  1059. exit(1);
  1060. }
  1061. }
  1062. dd = hci_open_dev(dev_id);
  1063. if (dd < 0) {
  1064. perror("HCI device open failed");
  1065. exit(1);
  1066. }
  1067. if (hci_create_connection(dd, &bdaddr, htobs(ptype),
  1068. htobs(0x0000), role, &handle, 25000) < 0)
  1069. perror("Can't create connection");
  1070. hci_close_dev(dd);
  1071. }
  1072. /* Close connection */
  1073. static struct option dc_options[] = {
  1074. { "help", 0, 0, 'h' },
  1075. { 0, 0, 0, 0 }
  1076. };
  1077. static const char *dc_help =
  1078. "Usage:\n"
  1079. "\tdc <bdaddr> [reason]\n";
  1080. static void cmd_dc(int dev_id, int argc, char **argv)
  1081. {
  1082. struct hci_conn_info_req *cr;
  1083. bdaddr_t bdaddr;
  1084. uint8_t reason;
  1085. int opt, dd;
  1086. for_each_opt(opt, dc_options, NULL) {
  1087. switch (opt) {
  1088. default:
  1089. printf("%s", dc_help);
  1090. return;
  1091. }
  1092. }
  1093. helper_arg(1, 2, &argc, &argv, dc_help);
  1094. str2ba(argv[0], &bdaddr);
  1095. reason = (argc > 1) ? atoi(argv[1]) : HCI_OE_USER_ENDED_CONNECTION;
  1096. if (dev_id < 0) {
  1097. dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
  1098. if (dev_id < 0) {
  1099. fprintf(stderr, "Not connected.\n");
  1100. exit(1);
  1101. }
  1102. }
  1103. dd = hci_open_dev(dev_id);
  1104. if (dd < 0) {
  1105. perror("HCI device open failed");
  1106. exit(1);
  1107. }
  1108. cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
  1109. if (!cr) {
  1110. perror("Can't allocate memory");
  1111. exit(1);
  1112. }
  1113. bacpy(&cr->bdaddr, &bdaddr);
  1114. cr->type = ACL_LINK;
  1115. if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
  1116. perror("Get connection info failed");
  1117. exit(1);
  1118. }
  1119. if (hci_disconnect(dd, htobs(cr->conn_info->handle),
  1120. reason, 10000) < 0)
  1121. perror("Disconnect failed");
  1122. free(cr);
  1123. hci_close_dev(dd);
  1124. }
  1125. /* Role switch */
  1126. static struct option sr_options[] = {
  1127. { "help", 0, 0, 'h' },
  1128. { 0, 0, 0, 0 }
  1129. };
  1130. static const char *sr_help =
  1131. "Usage:\n"
  1132. "\tsr <bdaddr> <role>\n";
  1133. static void cmd_sr(int dev_id, int argc, char **argv)
  1134. {
  1135. bdaddr_t bdaddr;
  1136. uint8_t role;
  1137. int opt, dd;
  1138. for_each_opt(opt, sr_options, NULL) {
  1139. switch (opt) {
  1140. default:
  1141. printf("%s", sr_help);
  1142. return;
  1143. }
  1144. }
  1145. helper_arg(2, 2, &argc, &argv, sr_help);
  1146. str2ba(argv[0], &bdaddr);
  1147. switch (argv[1][0]) {
  1148. case 'm': /* Deprecated. Kept for compatibility. */
  1149. case 'c':
  1150. role = 0;
  1151. break;
  1152. case 's': /* Deprecated. Kept for compatibility. */
  1153. case 'p':
  1154. role = 1;
  1155. break;
  1156. default:
  1157. role = atoi(argv[1]);
  1158. break;
  1159. }
  1160. if (dev_id < 0) {
  1161. dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
  1162. if (dev_id < 0) {
  1163. fprintf(stderr, "Not connected.\n");
  1164. exit(1);
  1165. }
  1166. }
  1167. dd = hci_open_dev(dev_id);
  1168. if (dd < 0) {
  1169. perror("HCI device open failed");
  1170. exit(1);
  1171. }
  1172. if (hci_switch_role(dd, &bdaddr, role, 10000) < 0) {
  1173. perror("Switch role request failed");
  1174. exit(1);
  1175. }
  1176. hci_close_dev(dd);
  1177. }
  1178. /* Read RSSI */
  1179. static struct option rssi_options[] = {
  1180. { "help", 0, 0, 'h' },
  1181. { 0, 0, 0, 0 }
  1182. };
  1183. static const char *rssi_help =
  1184. "Usage:\n"
  1185. "\trssi <bdaddr>\n";
  1186. static void cmd_rssi(int dev_id, int argc, char **argv)
  1187. {
  1188. struct hci_conn_info_req *cr;
  1189. bdaddr_t bdaddr;
  1190. int8_t rssi;
  1191. int opt, dd;
  1192. for_each_opt(opt, rssi_options, NULL) {
  1193. switch (opt) {
  1194. default:
  1195. printf("%s", rssi_help);
  1196. return;
  1197. }
  1198. }
  1199. helper_arg(1, 1, &argc, &argv, rssi_help);
  1200. str2ba(argv[0], &bdaddr);
  1201. if (dev_id < 0) {
  1202. dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
  1203. if (dev_id < 0) {
  1204. fprintf(stderr, "Not connected.\n");
  1205. exit(1);
  1206. }
  1207. }
  1208. dd = hci_open_dev(dev_id);
  1209. if (dd < 0) {
  1210. perror("HCI device open failed");
  1211. exit(1);
  1212. }
  1213. cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
  1214. if (!cr) {
  1215. perror("Can't allocate memory");
  1216. exit(1);
  1217. }
  1218. bacpy(&cr->bdaddr, &bdaddr);
  1219. cr->type = ACL_LINK;
  1220. if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
  1221. perror("Get connection info failed");
  1222. exit(1);
  1223. }
  1224. if (hci_read_rssi(dd, htobs(cr->conn_info->handle), &rssi, 1000) < 0) {
  1225. perror("Read RSSI failed");
  1226. exit(1);
  1227. }
  1228. printf("RSSI return value: %d\n", rssi);
  1229. free(cr);
  1230. hci_close_dev(dd);
  1231. }
  1232. /* Get link quality */
  1233. static struct option lq_options[] = {
  1234. { "help", 0, 0, 'h' },
  1235. { 0, 0, 0, 0 }
  1236. };
  1237. static const char *lq_help =
  1238. "Usage:\n"
  1239. "\tlq <bdaddr>\n";
  1240. static void cmd_lq(int dev_id, int argc, char **argv)
  1241. {
  1242. struct hci_conn_info_req *cr;
  1243. bdaddr_t bdaddr;
  1244. uint8_t lq;
  1245. int opt, dd;
  1246. for_each_opt(opt, lq_options, NULL) {
  1247. switch (opt) {
  1248. default:
  1249. printf("%s", lq_help);
  1250. return;
  1251. }
  1252. }
  1253. helper_arg(1, 1, &argc, &argv, lq_help);
  1254. str2ba(argv[0], &bdaddr);
  1255. if (dev_id < 0) {
  1256. dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
  1257. if (dev_id < 0) {
  1258. fprintf(stderr, "Not connected.\n");
  1259. exit(1);
  1260. }
  1261. }
  1262. dd = hci_open_dev(dev_id);
  1263. if (dd < 0) {
  1264. perror("HCI device open failed");
  1265. exit(1);
  1266. }
  1267. cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
  1268. if (!cr) {
  1269. perror("Can't allocate memory");
  1270. exit(1);
  1271. }
  1272. bacpy(&cr->bdaddr, &bdaddr);
  1273. cr->type = ACL_LINK;
  1274. if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
  1275. perror("Get connection info failed");
  1276. exit(1);
  1277. }
  1278. if (hci_read_link_quality(dd, htobs(cr->conn_info->handle), &lq, 1000) < 0) {
  1279. perror("HCI read_link_quality request failed");
  1280. exit(1);
  1281. }
  1282. printf("Link quality: %d\n", lq);
  1283. free(cr);
  1284. hci_close_dev(dd);
  1285. }
  1286. /* Get transmit power level */
  1287. static struct option tpl_options[] = {
  1288. { "help", 0, 0, 'h' },
  1289. { 0, 0, 0, 0 }
  1290. };
  1291. static const char *tpl_help =
  1292. "Usage:\n"
  1293. "\ttpl <bdaddr> [type]\n";
  1294. static void cmd_tpl(int dev_id, int argc, char **argv)
  1295. {
  1296. struct hci_conn_info_req *cr;
  1297. bdaddr_t bdaddr;
  1298. uint8_t type;
  1299. int8_t level;
  1300. int opt, dd;
  1301. for_each_opt(opt, tpl_options, NULL) {
  1302. switch (opt) {
  1303. default:
  1304. printf("%s", tpl_help);
  1305. return;
  1306. }
  1307. }
  1308. helper_arg(1, 2, &argc, &argv, tpl_help);
  1309. str2ba(argv[0], &bdaddr);
  1310. type = (argc > 1) ? atoi(argv[1]) : 0;
  1311. if (dev_id < 0) {
  1312. dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
  1313. if (dev_id < 0) {
  1314. fprintf(stderr, "Not connected.\n");
  1315. exit(1);
  1316. }
  1317. }
  1318. dd = hci_open_dev(dev_id);
  1319. if (dd < 0) {
  1320. perror("HCI device open failed");
  1321. exit(1);
  1322. }
  1323. cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
  1324. if (!cr) {
  1325. perror("Can't allocate memory");
  1326. exit(1);
  1327. }
  1328. bacpy(&cr->bdaddr, &bdaddr);
  1329. cr->type = ACL_LINK;
  1330. if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
  1331. perror("Get connection info failed");
  1332. exit(1);
  1333. }
  1334. if (hci_read_transmit_power_level(dd, htobs(cr->conn_info->handle), type, &level, 1000) < 0) {
  1335. perror("HCI read transmit power level request failed");
  1336. exit(1);
  1337. }
  1338. printf("%s transmit power level: %d\n",
  1339. (type == 0) ? "Current" : "Maximum", level);
  1340. free(cr);
  1341. hci_close_dev(dd);
  1342. }
  1343. /* Get AFH channel map */
  1344. static struct option afh_options[] = {
  1345. { "help", 0, 0, 'h' },
  1346. { 0, 0, 0, 0 }
  1347. };
  1348. static const char *afh_help =
  1349. "Usage:\n"
  1350. "\tafh <bdaddr>\n";
  1351. static void cmd_afh(int dev_id, int argc, char **argv)
  1352. {
  1353. struct hci_conn_info_req *cr;
  1354. bdaddr_t bdaddr;
  1355. uint16_t handle;
  1356. uint8_t mode, map[10];
  1357. int opt, dd;
  1358. for_each_opt(opt, afh_options, NULL) {
  1359. switch (opt) {
  1360. default:
  1361. printf("%s", afh_help);
  1362. return;
  1363. }
  1364. }
  1365. helper_arg(1, 1, &argc, &argv, afh_help);
  1366. str2ba(argv[0], &bdaddr);
  1367. if (dev_id < 0) {
  1368. dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
  1369. if (dev_id < 0) {
  1370. fprintf(stderr, "Not connected.\n");
  1371. exit(1);
  1372. }
  1373. }
  1374. dd = hci_open_dev(dev_id);
  1375. if (dd < 0) {
  1376. perror("HCI device open failed");
  1377. exit(1);
  1378. }
  1379. cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
  1380. if (!cr) {
  1381. perror("Can't allocate memory");
  1382. exit(1);
  1383. }
  1384. bacpy(&cr->bdaddr, &bdaddr);
  1385. cr->type = ACL_LINK;
  1386. if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
  1387. perror("Get connection info failed");
  1388. exit(1);
  1389. }
  1390. handle = htobs(cr->conn_info->handle);
  1391. if (hci_read_afh_map(dd, handle, &mode, map, 1000) < 0) {
  1392. perror("HCI read AFH map request failed");
  1393. exit(1);
  1394. }
  1395. if (mode == 0x01) {
  1396. int i;
  1397. printf("AFH map: 0x");
  1398. for (i = 0; i < 10; i++)
  1399. printf("%02x", map[i]);
  1400. printf("\n");
  1401. } else
  1402. printf("AFH disabled\n");
  1403. free(cr);
  1404. hci_close_dev(dd);
  1405. }
  1406. /* Set connection packet type */
  1407. static struct option cpt_options[] = {
  1408. { "help", 0, 0, 'h' },
  1409. { 0, 0, 0, 0 }
  1410. };
  1411. static const char *cpt_help =
  1412. "Usage:\n"
  1413. "\tcpt <bdaddr> <packet_types>\n";
  1414. static void cmd_cpt(int dev_id, int argc, char **argv)
  1415. {
  1416. struct hci_conn_info_req *cr;
  1417. struct hci_request rq;
  1418. set_conn_ptype_cp cp;
  1419. evt_conn_ptype_changed rp;
  1420. bdaddr_t bdaddr;
  1421. unsigned int ptype;
  1422. int dd, opt;
  1423. for_each_opt(opt, cpt_options, NULL) {
  1424. switch (opt) {
  1425. default:
  1426. printf("%s", cpt_help);
  1427. return;
  1428. }
  1429. }
  1430. helper_arg(2, 2, &argc, &argv, cpt_help);
  1431. str2ba(argv[0], &bdaddr);
  1432. hci_strtoptype(argv[1], &ptype);
  1433. if (dev_id < 0) {
  1434. dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
  1435. if (dev_id < 0) {
  1436. fprintf(stderr, "Not connected.\n");
  1437. exit(1);
  1438. }
  1439. }
  1440. dd = hci_open_dev(dev_id);
  1441. if (dd < 0) {
  1442. perror("HCI device open failed");
  1443. exit(1);
  1444. }
  1445. cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
  1446. if (!cr) {
  1447. perror("Can't allocate memory");
  1448. exit(1);
  1449. }
  1450. bacpy(&cr->bdaddr, &bdaddr);
  1451. cr->type = ACL_LINK;
  1452. if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
  1453. perror("Get connection info failed");
  1454. exit(1);
  1455. }
  1456. cp.handle = htobs(cr->conn_info->handle);
  1457. cp.pkt_type = ptype;
  1458. memset(&rq, 0, sizeof(rq));
  1459. rq.ogf = OGF_LINK_CTL;
  1460. rq.ocf = OCF_SET_CONN_PTYPE;
  1461. rq.cparam = &cp;
  1462. rq.clen = SET_CONN_PTYPE_CP_SIZE;
  1463. rq.rparam = &rp;
  1464. rq.rlen = EVT_CONN_PTYPE_CHANGED_SIZE;
  1465. rq.event = EVT_CONN_PTYPE_CHANGED;
  1466. if (hci_send_req(dd, &rq, 100) < 0) {
  1467. perror("Packet type change failed");
  1468. exit(1);
  1469. }
  1470. free(cr);
  1471. hci_close_dev(dd);
  1472. }
  1473. /* Get/Set link policy settings */
  1474. static struct option lp_options[] = {
  1475. { "help", 0, 0, 'h' },
  1476. { 0, 0, 0, 0 }
  1477. };
  1478. static const char *lp_help =
  1479. "Usage:\n"
  1480. "\tlp <bdaddr> [link policy]\n";
  1481. static void cmd_lp(int dev_id, int argc, char **argv)
  1482. {
  1483. struct hci_conn_info_req *cr;
  1484. bdaddr_t bdaddr;
  1485. uint16_t policy;
  1486. int opt, dd;
  1487. for_each_opt(opt, lp_options, NULL) {
  1488. switch (opt) {
  1489. default:
  1490. printf("%s", lp_help);
  1491. return;
  1492. }
  1493. }
  1494. helper_arg(1, 2, &argc, &argv, lp_help);
  1495. str2ba(argv[0], &bdaddr);
  1496. if (dev_id < 0) {
  1497. dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
  1498. if (dev_id < 0) {
  1499. fprintf(stderr, "Not connected.\n");
  1500. exit(1);
  1501. }
  1502. }
  1503. dd = hci_open_dev(dev_id);
  1504. if (dd < 0) {
  1505. perror("HCI device open failed");
  1506. exit(1);
  1507. }
  1508. cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
  1509. if (!cr) {
  1510. perror("Can't allocate memory");
  1511. exit(1);
  1512. }
  1513. bacpy(&cr->bdaddr, &bdaddr);
  1514. cr->type = ACL_LINK;
  1515. if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
  1516. perror("Get connection info failed");
  1517. exit(1);
  1518. }
  1519. if (argc == 1) {
  1520. char *str;
  1521. if (hci_read_link_policy(dd, htobs(cr->conn_info->handle),
  1522. &policy, 1000) < 0) {
  1523. perror("HCI read_link_policy_settings request failed");
  1524. exit(1);
  1525. }
  1526. policy = btohs(policy);
  1527. str = hci_lptostr(policy);
  1528. if (str) {
  1529. printf("Link policy settings: %s\n", str);
  1530. bt_free(str);
  1531. } else {
  1532. fprintf(stderr, "Invalig settings\n");
  1533. exit(1);
  1534. }
  1535. } else {
  1536. unsigned int val;
  1537. if (hci_strtolp(argv[1], &val) < 0) {
  1538. fprintf(stderr, "Invalig arguments\n");
  1539. exit(1);
  1540. }
  1541. policy = val;
  1542. if (hci_write_link_policy(dd, htobs(cr->conn_info->handle),
  1543. htobs(policy), 1000) < 0) {
  1544. perror("HCI write_link_policy_settings request failed");
  1545. exit(1);
  1546. }
  1547. }
  1548. free(cr);
  1549. hci_close_dev(dd);
  1550. }
  1551. /* Get/Set link supervision timeout */
  1552. static struct option lst_options[] = {
  1553. { "help", 0, 0, 'h' },
  1554. { 0, 0, 0, 0 }
  1555. };
  1556. static const char *lst_help =
  1557. "Usage:\n"
  1558. "\tlst <bdaddr> [new value in slots]\n";
  1559. static void cmd_lst(int dev_id, int argc, char **argv)
  1560. {
  1561. struct hci_conn_info_req *cr;
  1562. bdaddr_t bdaddr;
  1563. uint16_t timeout;
  1564. int opt, dd;
  1565. for_each_opt(opt, lst_options, NULL) {
  1566. switch (opt) {
  1567. default:
  1568. printf("%s", lst_help);
  1569. return;
  1570. }
  1571. }
  1572. helper_arg(1, 2, &argc, &argv, lst_help);
  1573. str2ba(argv[0], &bdaddr);
  1574. if (dev_id < 0) {
  1575. dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
  1576. if (dev_id < 0) {
  1577. fprintf(stderr, "Not connected.\n");
  1578. exit(1);
  1579. }
  1580. }
  1581. dd = hci_open_dev(dev_id);
  1582. if (dd < 0) {
  1583. perror("HCI device open failed");
  1584. exit(1);
  1585. }
  1586. cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
  1587. if (!cr) {
  1588. perror("Can't allocate memory");
  1589. exit(1);
  1590. }
  1591. bacpy(&cr->bdaddr, &bdaddr);
  1592. cr->type = ACL_LINK;
  1593. if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
  1594. perror("Get connection info failed");
  1595. exit(1);
  1596. }
  1597. if (argc == 1) {
  1598. if (hci_read_link_supervision_timeout(dd, htobs(cr->conn_info->handle),
  1599. &timeout, 1000) < 0) {
  1600. perror("HCI read_link_supervision_timeout request failed");
  1601. exit(1);
  1602. }
  1603. timeout = btohs(timeout);
  1604. if (timeout)
  1605. printf("Link supervision timeout: %u slots (%.2f msec)\n",
  1606. timeout, (float) timeout * 0.625);
  1607. else
  1608. printf("Link supervision timeout never expires\n");
  1609. } else {
  1610. timeout = strtol(argv[1], NULL, 10);
  1611. if (hci_write_link_supervision_timeout(dd, htobs(cr->conn_info->handle),
  1612. htobs(timeout), 1000) < 0) {
  1613. perror("HCI write_link_supervision_timeout request failed");
  1614. exit(1);
  1615. }
  1616. }
  1617. free(cr);
  1618. hci_close_dev(dd);
  1619. }
  1620. /* Request authentication */
  1621. static struct option auth_options[] = {
  1622. { "help", 0, 0, 'h' },
  1623. { 0, 0, 0, 0 }
  1624. };
  1625. static const char *auth_help =
  1626. "Usage:\n"
  1627. "\tauth <bdaddr>\n";
  1628. static void cmd_auth(int dev_id, int argc, char **argv)
  1629. {
  1630. struct hci_conn_info_req *cr;
  1631. bdaddr_t bdaddr;
  1632. int opt, dd;
  1633. for_each_opt(opt, auth_options, NULL) {
  1634. switch (opt) {
  1635. default:
  1636. printf("%s", auth_help);
  1637. return;
  1638. }
  1639. }
  1640. helper_arg(1, 1, &argc, &argv, auth_help);
  1641. str2ba(argv[0], &bdaddr);
  1642. if (dev_id < 0) {
  1643. dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
  1644. if (dev_id < 0) {
  1645. fprintf(stderr, "Not connected.\n");
  1646. exit(1);
  1647. }
  1648. }
  1649. dd = hci_open_dev(dev_id);
  1650. if (dd < 0) {
  1651. perror("HCI device open failed");
  1652. exit(1);
  1653. }
  1654. cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
  1655. if (!cr) {
  1656. perror("Can't allocate memory");
  1657. exit(1);
  1658. }
  1659. bacpy(&cr->bdaddr, &bdaddr);
  1660. cr->type = ACL_LINK;
  1661. if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
  1662. perror("Get connection info failed");
  1663. exit(1);
  1664. }
  1665. if (hci_authenticate_link(dd, htobs(cr->conn_info->handle), 25000) < 0) {
  1666. perror("HCI authentication request failed");
  1667. exit(1);
  1668. }
  1669. free(cr);
  1670. hci_close_dev(dd);
  1671. }
  1672. /* Activate encryption */
  1673. static struct option enc_options[] = {
  1674. { "help", 0, 0, 'h' },
  1675. { 0, 0, 0, 0 }
  1676. };
  1677. static const char *enc_help =
  1678. "Usage:\n"
  1679. "\tenc <bdaddr> [encrypt enable]\n";
  1680. static void cmd_enc(int dev_id, int argc, char **argv)
  1681. {
  1682. struct hci_conn_info_req *cr;
  1683. bdaddr_t bdaddr;
  1684. uint8_t encrypt;
  1685. int opt, dd;
  1686. for_each_opt(opt, enc_options, NULL) {
  1687. switch (opt) {
  1688. default:
  1689. printf("%s", enc_help);
  1690. return;
  1691. }
  1692. }
  1693. helper_arg(1, 2, &argc, &argv, enc_help);
  1694. str2ba(argv[0], &bdaddr);
  1695. if (dev_id < 0) {
  1696. dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
  1697. if (dev_id < 0) {
  1698. fprintf(stderr, "Not connected.\n");
  1699. exit(1);
  1700. }
  1701. }
  1702. dd = hci_open_dev(dev_id);
  1703. if (dd < 0) {
  1704. perror("HCI device open failed");
  1705. exit(1);
  1706. }
  1707. cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
  1708. if (!cr) {
  1709. perror("Can't allocate memory");
  1710. exit(1);
  1711. }
  1712. bacpy(&cr->bdaddr, &bdaddr);
  1713. cr->type = ACL_LINK;
  1714. if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
  1715. perror("Get connection info failed");
  1716. exit(1);
  1717. }
  1718. encrypt = (argc > 1) ? atoi(argv[1]) : 1;
  1719. if (hci_encrypt_link(dd, htobs(cr->conn_info->handle), encrypt, 25000) < 0) {
  1720. perror("HCI set encryption request failed");
  1721. exit(1);
  1722. }
  1723. free(cr);
  1724. hci_close_dev(dd);
  1725. }
  1726. /* Change connection link key */
  1727. static struct option key_options[] = {
  1728. { "help", 0, 0, 'h' },
  1729. { 0, 0, 0, 0 }
  1730. };
  1731. static const char *key_help =
  1732. "Usage:\n"
  1733. "\tkey <bdaddr>\n";
  1734. static void cmd_key(int dev_id, int argc, char **argv)
  1735. {
  1736. struct hci_conn_info_req *cr;
  1737. bdaddr_t bdaddr;
  1738. int opt, dd;
  1739. for_each_opt(opt, key_options, NULL) {
  1740. switch (opt) {
  1741. default:
  1742. printf("%s", key_help);
  1743. return;
  1744. }
  1745. }
  1746. helper_arg(1, 1, &argc, &argv, key_help);
  1747. str2ba(argv[0], &bdaddr);
  1748. if (dev_id < 0) {
  1749. dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
  1750. if (dev_id < 0) {
  1751. fprintf(stderr, "Not connected.\n");
  1752. exit(1);
  1753. }
  1754. }
  1755. dd = hci_open_dev(dev_id);
  1756. if (dd < 0) {
  1757. perror("HCI device open failed");
  1758. exit(1);
  1759. }
  1760. cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
  1761. if (!cr) {
  1762. perror("Can't allocate memory");
  1763. exit(1);
  1764. }
  1765. bacpy(&cr->bdaddr, &bdaddr);
  1766. cr->type = ACL_LINK;
  1767. if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
  1768. perror("Get connection info failed");
  1769. exit(1);
  1770. }
  1771. if (hci_change_link_key(dd, htobs(cr->conn_info->handle), 25000) < 0) {
  1772. perror("Changing link key failed");
  1773. exit(1);
  1774. }
  1775. free(cr);
  1776. hci_close_dev(dd);
  1777. }
  1778. /* Read clock offset */
  1779. static struct option clkoff_options[] = {
  1780. { "help", 0, 0, 'h' },
  1781. { 0, 0, 0, 0 }
  1782. };
  1783. static const char *clkoff_help =
  1784. "Usage:\n"
  1785. "\tclkoff <bdaddr>\n";
  1786. static void cmd_clkoff(int dev_id, int argc, char **argv)
  1787. {
  1788. struct hci_conn_info_req *cr;
  1789. bdaddr_t bdaddr;
  1790. uint16_t offset;
  1791. int opt, dd;
  1792. for_each_opt(opt, clkoff_options, NULL) {
  1793. switch (opt) {
  1794. default:
  1795. printf("%s", clkoff_help);
  1796. return;
  1797. }
  1798. }
  1799. helper_arg(1, 1, &argc, &argv, clkoff_help);
  1800. str2ba(argv[0], &bdaddr);
  1801. if (dev_id < 0) {
  1802. dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
  1803. if (dev_id < 0) {
  1804. fprintf(stderr, "Not connected.\n");
  1805. exit(1);
  1806. }
  1807. }
  1808. dd = hci_open_dev(dev_id);
  1809. if (dd < 0) {
  1810. perror("HCI device open failed");
  1811. exit(1);
  1812. }
  1813. cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
  1814. if (!cr) {
  1815. perror("Can't allocate memory");
  1816. exit(1);
  1817. }
  1818. bacpy(&cr->bdaddr, &bdaddr);
  1819. cr->type = ACL_LINK;
  1820. if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
  1821. perror("Get connection info failed");
  1822. exit(1);
  1823. }
  1824. if (hci_read_clock_offset(dd, htobs(cr->conn_info->handle), &offset, 1000) < 0) {
  1825. perror("Reading clock offset failed");
  1826. exit(1);
  1827. }
  1828. printf("Clock offset: 0x%4.4x\n", btohs(offset));
  1829. free(cr);
  1830. hci_close_dev(dd);
  1831. }
  1832. /* Read clock */
  1833. static struct option clock_options[] = {
  1834. { "help", 0, 0, 'h' },
  1835. { 0, 0, 0, 0 }
  1836. };
  1837. static const char *clock_help =
  1838. "Usage:\n"
  1839. "\tclock [bdaddr] [which clock]\n";
  1840. static void cmd_clock(int dev_id, int argc, char **argv)
  1841. {
  1842. struct hci_conn_info_req *cr;
  1843. bdaddr_t bdaddr;
  1844. uint8_t which;
  1845. uint32_t handle, clock;
  1846. uint16_t accuracy;
  1847. int opt, dd;
  1848. for_each_opt(opt, clock_options, NULL) {
  1849. switch (opt) {
  1850. default:
  1851. printf("%s", clock_help);
  1852. return;
  1853. }
  1854. }
  1855. helper_arg(0, 2, &argc, &argv, clock_help);
  1856. if (argc > 0)
  1857. str2ba(argv[0], &bdaddr);
  1858. else
  1859. bacpy(&bdaddr, BDADDR_ANY);
  1860. if (dev_id < 0 && !bacmp(&bdaddr, BDADDR_ANY))
  1861. dev_id = hci_get_route(NULL);
  1862. if (dev_id < 0) {
  1863. dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
  1864. if (dev_id < 0) {
  1865. fprintf(stderr, "Not connected.\n");
  1866. exit(1);
  1867. }
  1868. }
  1869. dd = hci_open_dev(dev_id);
  1870. if (dd < 0) {
  1871. perror("HCI device open failed");
  1872. exit(1);
  1873. }
  1874. if (bacmp(&bdaddr, BDADDR_ANY)) {
  1875. cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
  1876. if (!cr) {
  1877. perror("Can't allocate memory");
  1878. exit(1);
  1879. }
  1880. bacpy(&cr->bdaddr, &bdaddr);
  1881. cr->type = ACL_LINK;
  1882. if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
  1883. perror("Get connection info failed");
  1884. free(cr);
  1885. exit(1);
  1886. }
  1887. handle = htobs(cr->conn_info->handle);
  1888. which = (argc > 1) ? atoi(argv[1]) : 0x01;
  1889. free(cr);
  1890. } else {
  1891. handle = 0x00;
  1892. which = 0x00;
  1893. }
  1894. if (hci_read_clock(dd, handle, which, &clock, &accuracy, 1000) < 0) {
  1895. perror("Reading clock failed");
  1896. exit(1);
  1897. }
  1898. accuracy = btohs(accuracy);
  1899. printf("Clock: 0x%4.4x\n", btohl(clock));
  1900. printf("Accuracy: %.2f msec\n", (float) accuracy * 0.3125);
  1901. hci_close_dev(dd);
  1902. }
  1903. static int read_flags(uint8_t *flags, const uint8_t *data, size_t size)
  1904. {
  1905. size_t offset;
  1906. if (!flags || !data)
  1907. return -EINVAL;
  1908. offset = 0;
  1909. while (offset < size) {
  1910. uint8_t len = data[offset];
  1911. uint8_t type;
  1912. /* Check if it is the end of the significant part */
  1913. if (len == 0)
  1914. break;
  1915. if (len + offset > size)
  1916. break;
  1917. type = data[offset + 1];
  1918. if (type == FLAGS_AD_TYPE) {
  1919. *flags = data[offset + 2];
  1920. return 0;
  1921. }
  1922. offset += 1 + len;
  1923. }
  1924. return -ENOENT;
  1925. }
  1926. static int check_report_filter(uint8_t procedure, le_advertising_info *info)
  1927. {
  1928. uint8_t flags;
  1929. /* If no discovery procedure is set, all reports are treat as valid */
  1930. if (procedure == 0)
  1931. return 1;
  1932. /* Read flags AD type value from the advertising report if it exists */
  1933. if (read_flags(&flags, info->data, info->length))
  1934. return 0;
  1935. switch (procedure) {
  1936. case 'l': /* Limited Discovery Procedure */
  1937. if (flags & FLAGS_LIMITED_MODE_BIT)
  1938. return 1;
  1939. break;
  1940. case 'g': /* General Discovery Procedure */
  1941. if (flags & (FLAGS_LIMITED_MODE_BIT | FLAGS_GENERAL_MODE_BIT))
  1942. return 1;
  1943. break;
  1944. default:
  1945. fprintf(stderr, "Unknown discovery procedure\n");
  1946. }
  1947. return 0;
  1948. }
  1949. static void sigint_handler(int sig)
  1950. {
  1951. signal_received = sig;
  1952. }
  1953. static void eir_parse_name(uint8_t *eir, size_t eir_len,
  1954. char *buf, size_t buf_len)
  1955. {
  1956. size_t offset;
  1957. offset = 0;
  1958. while (offset < eir_len) {
  1959. uint8_t field_len = eir[0];
  1960. size_t name_len;
  1961. /* Check for the end of EIR */
  1962. if (field_len == 0)
  1963. break;
  1964. if (offset + field_len > eir_len)
  1965. goto failed;
  1966. switch (eir[1]) {
  1967. case EIR_NAME_SHORT:
  1968. case EIR_NAME_COMPLETE:
  1969. name_len = field_len - 1;
  1970. if (name_len > buf_len)
  1971. goto failed;
  1972. memcpy(buf, &eir[2], name_len);
  1973. return;
  1974. }
  1975. offset += field_len + 1;
  1976. eir += field_len + 1;
  1977. }
  1978. failed:
  1979. snprintf(buf, buf_len, "(unknown)");
  1980. }
  1981. static int print_advertising_devices(int dd, uint8_t filter_type)
  1982. {
  1983. unsigned char buf[HCI_MAX_EVENT_SIZE], *ptr;
  1984. struct hci_filter nf, of;
  1985. struct sigaction sa;
  1986. socklen_t olen;
  1987. int len;
  1988. olen = sizeof(of);
  1989. if (getsockopt(dd, SOL_HCI, HCI_FILTER, &of, &olen) < 0) {
  1990. printf("Could not get socket options\n");
  1991. return -1;
  1992. }
  1993. hci_filter_clear(&nf);
  1994. hci_filter_set_ptype(HCI_EVENT_PKT, &nf);
  1995. hci_filter_set_event(EVT_LE_META_EVENT, &nf);
  1996. if (setsockopt(dd, SOL_HCI, HCI_FILTER, &nf, sizeof(nf)) < 0) {
  1997. printf("Could not set socket options\n");
  1998. return -1;
  1999. }
  2000. memset(&sa, 0, sizeof(sa));
  2001. sa.sa_flags = SA_NOCLDSTOP;
  2002. sa.sa_handler = sigint_handler;
  2003. sigaction(SIGINT, &sa, NULL);
  2004. while (1) {
  2005. evt_le_meta_event *meta;
  2006. le_advertising_info *info;
  2007. char addr[18];
  2008. while ((len = read(dd, buf, sizeof(buf))) < 0) {
  2009. if (errno == EINTR && signal_received == SIGINT) {
  2010. len = 0;
  2011. goto done;
  2012. }
  2013. if (errno == EAGAIN || errno == EINTR)
  2014. continue;
  2015. goto done;
  2016. }
  2017. ptr = buf + (1 + HCI_EVENT_HDR_SIZE);
  2018. len -= (1 + HCI_EVENT_HDR_SIZE);
  2019. meta = (void *) ptr;
  2020. if (meta->subevent != 0x02)
  2021. goto done;
  2022. /* Ignoring multiple reports */
  2023. info = (le_advertising_info *) (meta->data + 1);
  2024. if (check_report_filter(filter_type, info)) {
  2025. char name[30];
  2026. memset(name, 0, sizeof(name));
  2027. ba2str(&info->bdaddr, addr);
  2028. eir_parse_name(info->data, info->length,
  2029. name, sizeof(name) - 1);
  2030. printf("%s %s\n", addr, name);
  2031. }
  2032. }
  2033. done:
  2034. setsockopt(dd, SOL_HCI, HCI_FILTER, &of, sizeof(of));
  2035. if (len < 0)
  2036. return -1;
  2037. return 0;
  2038. }
  2039. static struct option lescan_options[] = {
  2040. { "help", 0, 0, 'h' },
  2041. { "static", 0, 0, 's' },
  2042. { "privacy", 0, 0, 'p' },
  2043. { "passive", 0, 0, 'P' },
  2044. { "whitelist", 0, 0, 'w' }, /* Deprecated. Kept for compatibility. */
  2045. { "acceptlist", 0, 0, 'a' },
  2046. { "discovery", 1, 0, 'd' },
  2047. { "duplicates", 0, 0, 'D' },
  2048. { 0, 0, 0, 0 }
  2049. };
  2050. static const char *lescan_help =
  2051. "Usage:\n"
  2052. "\tlescan [--privacy] enable privacy\n"
  2053. "\tlescan [--passive] set scan type passive (default active)\n"
  2054. "\tlescan [--acceptlist] scan for address in the accept list only\n"
  2055. "\tlescan [--discovery=g|l] enable general or limited discovery"
  2056. "procedure\n"
  2057. "\tlescan [--duplicates] don't filter duplicates\n";
  2058. static void cmd_lescan(int dev_id, int argc, char **argv)
  2059. {
  2060. int err, opt, dd;
  2061. uint8_t own_type = LE_PUBLIC_ADDRESS;
  2062. uint8_t scan_type = 0x01;
  2063. uint8_t filter_type = 0;
  2064. uint8_t filter_policy = 0x00;
  2065. uint16_t interval = htobs(0x0010);
  2066. uint16_t window = htobs(0x0010);
  2067. uint8_t filter_dup = 0x01;
  2068. for_each_opt(opt, lescan_options, NULL) {
  2069. switch (opt) {
  2070. case 's':
  2071. own_type = LE_RANDOM_ADDRESS;
  2072. break;
  2073. case 'p':
  2074. own_type = LE_RANDOM_ADDRESS;
  2075. break;
  2076. case 'P':
  2077. scan_type = 0x00; /* Passive */
  2078. break;
  2079. case 'w': /* Deprecated. Kept for compatibility. */
  2080. case 'a':
  2081. filter_policy = 0x01; /* Accept list */
  2082. break;
  2083. case 'd':
  2084. filter_type = optarg[0];
  2085. if (filter_type != 'g' && filter_type != 'l') {
  2086. fprintf(stderr, "Unknown discovery procedure\n");
  2087. exit(1);
  2088. }
  2089. interval = htobs(0x0012);
  2090. window = htobs(0x0012);
  2091. break;
  2092. case 'D':
  2093. filter_dup = 0x00;
  2094. break;
  2095. default:
  2096. printf("%s", lescan_help);
  2097. return;
  2098. }
  2099. }
  2100. helper_arg(0, 1, &argc, &argv, lescan_help);
  2101. if (dev_id < 0)
  2102. dev_id = hci_get_route(NULL);
  2103. dd = hci_open_dev(dev_id);
  2104. if (dd < 0) {
  2105. perror("Could not open device");
  2106. exit(1);
  2107. }
  2108. err = hci_le_set_scan_parameters(dd, scan_type, interval, window,
  2109. own_type, filter_policy, 10000);
  2110. if (err < 0) {
  2111. perror("Set scan parameters failed");
  2112. exit(1);
  2113. }
  2114. err = hci_le_set_scan_enable(dd, 0x01, filter_dup, 10000);
  2115. if (err < 0) {
  2116. perror("Enable scan failed");
  2117. exit(1);
  2118. }
  2119. printf("LE Scan ...\n");
  2120. err = print_advertising_devices(dd, filter_type);
  2121. if (err < 0) {
  2122. perror("Could not receive advertising events");
  2123. exit(1);
  2124. }
  2125. err = hci_le_set_scan_enable(dd, 0x00, filter_dup, 10000);
  2126. if (err < 0) {
  2127. perror("Disable scan failed");
  2128. exit(1);
  2129. }
  2130. hci_close_dev(dd);
  2131. }
  2132. static struct option leinfo_options[] = {
  2133. { "help", 0, 0, 'h' },
  2134. { "static", 0, 0, 's' },
  2135. { "random", 0, 0, 'r' },
  2136. { 0, 0, 0, 0 }
  2137. };
  2138. static const char *leinfo_help =
  2139. "Usage:\n"
  2140. "\tleinfo [--static] [--random] <bdaddr>\n";
  2141. static void cmd_leinfo(int dev_id, int argc, char **argv)
  2142. {
  2143. bdaddr_t bdaddr;
  2144. uint16_t handle;
  2145. uint8_t features[8];
  2146. struct hci_version version;
  2147. uint16_t interval, latency, max_ce_length, max_interval, min_ce_length;
  2148. uint16_t min_interval, supervision_timeout, window;
  2149. uint8_t initiator_filter, own_bdaddr_type, peer_bdaddr_type;
  2150. int opt, err, dd;
  2151. own_bdaddr_type = LE_PUBLIC_ADDRESS;
  2152. peer_bdaddr_type = LE_PUBLIC_ADDRESS;
  2153. for_each_opt(opt, leinfo_options, NULL) {
  2154. switch (opt) {
  2155. case 's':
  2156. own_bdaddr_type = LE_RANDOM_ADDRESS;
  2157. break;
  2158. case 'r':
  2159. peer_bdaddr_type = LE_RANDOM_ADDRESS;
  2160. break;
  2161. default:
  2162. printf("%s", leinfo_help);
  2163. return;
  2164. }
  2165. }
  2166. helper_arg(1, 1, &argc, &argv, leinfo_help);
  2167. str2ba(argv[0], &bdaddr);
  2168. printf("Requesting information ...\n");
  2169. if (dev_id < 0)
  2170. dev_id = hci_get_route(NULL);
  2171. dd = hci_open_dev(dev_id);
  2172. if (dd < 0) {
  2173. perror("Could not open device");
  2174. exit(1);
  2175. }
  2176. interval = htobs(0x0004);
  2177. window = htobs(0x0004);
  2178. initiator_filter = 0;
  2179. min_interval = htobs(0x000F);
  2180. max_interval = htobs(0x000F);
  2181. latency = htobs(0x0000);
  2182. supervision_timeout = htobs(0x0C80);
  2183. min_ce_length = htobs(0x0000);
  2184. max_ce_length = htobs(0x0000);
  2185. err = hci_le_create_conn(dd, interval, window, initiator_filter,
  2186. peer_bdaddr_type, bdaddr, own_bdaddr_type, min_interval,
  2187. max_interval, latency, supervision_timeout,
  2188. min_ce_length, max_ce_length, &handle, 25000);
  2189. if (err < 0) {
  2190. perror("Could not create connection");
  2191. exit(1);
  2192. }
  2193. printf("\tHandle: %d (0x%04x)\n", handle, handle);
  2194. if (hci_read_remote_version(dd, handle, &version, 20000) == 0) {
  2195. char *ver = lmp_vertostr(version.lmp_ver);
  2196. printf("\tLMP Version: %s (0x%x) LMP Subversion: 0x%x\n"
  2197. "\tManufacturer: %s (%d)\n",
  2198. ver ? ver : "n/a",
  2199. version.lmp_ver,
  2200. version.lmp_subver,
  2201. bt_compidtostr(version.manufacturer),
  2202. version.manufacturer);
  2203. if (ver)
  2204. bt_free(ver);
  2205. }
  2206. memset(features, 0, sizeof(features));
  2207. hci_le_read_remote_features(dd, handle, features, 20000);
  2208. printf("\tFeatures: 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x "
  2209. "0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n",
  2210. features[0], features[1], features[2], features[3],
  2211. features[4], features[5], features[6], features[7]);
  2212. usleep(10000);
  2213. hci_disconnect(dd, handle, HCI_OE_USER_ENDED_CONNECTION, 10000);
  2214. hci_close_dev(dd);
  2215. }
  2216. static struct option lecc_options[] = {
  2217. { "help", 0, 0, 'h' },
  2218. { "static", 0, 0, 's' },
  2219. { "random", 0, 0, 'r' },
  2220. { "whitelist", 0, 0, 'w' }, /* Deprecated. Kept for compatibility. */
  2221. { "acceptlist", 0, 0, 'a' },
  2222. { 0, 0, 0, 0 }
  2223. };
  2224. static const char *lecc_help =
  2225. "Usage:\n"
  2226. "\tlecc [--static] [--random] <bdaddr>\n"
  2227. "\tlecc --acceptlist\n";
  2228. static void cmd_lecc(int dev_id, int argc, char **argv)
  2229. {
  2230. int err, opt, dd;
  2231. bdaddr_t bdaddr;
  2232. uint16_t interval, latency, max_ce_length, max_interval, min_ce_length;
  2233. uint16_t min_interval, supervision_timeout, window, handle;
  2234. uint8_t initiator_filter, own_bdaddr_type, peer_bdaddr_type;
  2235. own_bdaddr_type = LE_PUBLIC_ADDRESS;
  2236. peer_bdaddr_type = LE_PUBLIC_ADDRESS;
  2237. initiator_filter = 0; /* Use peer address */
  2238. for_each_opt(opt, lecc_options, NULL) {
  2239. switch (opt) {
  2240. case 's':
  2241. own_bdaddr_type = LE_RANDOM_ADDRESS;
  2242. break;
  2243. case 'r':
  2244. peer_bdaddr_type = LE_RANDOM_ADDRESS;
  2245. break;
  2246. case 'w': /* Deprecated. Kept for compatibility. */
  2247. case 'a':
  2248. initiator_filter = 0x01; /* Use accept list */
  2249. break;
  2250. default:
  2251. printf("%s", lecc_help);
  2252. return;
  2253. }
  2254. }
  2255. helper_arg(0, 1, &argc, &argv, lecc_help);
  2256. if (dev_id < 0)
  2257. dev_id = hci_get_route(NULL);
  2258. dd = hci_open_dev(dev_id);
  2259. if (dd < 0) {
  2260. perror("Could not open device");
  2261. exit(1);
  2262. }
  2263. memset(&bdaddr, 0, sizeof(bdaddr_t));
  2264. if (argv[0])
  2265. str2ba(argv[0], &bdaddr);
  2266. interval = htobs(0x0004);
  2267. window = htobs(0x0004);
  2268. min_interval = htobs(0x000F);
  2269. max_interval = htobs(0x000F);
  2270. latency = htobs(0x0000);
  2271. supervision_timeout = htobs(0x0C80);
  2272. min_ce_length = htobs(0x0001);
  2273. max_ce_length = htobs(0x0001);
  2274. err = hci_le_create_conn(dd, interval, window, initiator_filter,
  2275. peer_bdaddr_type, bdaddr, own_bdaddr_type, min_interval,
  2276. max_interval, latency, supervision_timeout,
  2277. min_ce_length, max_ce_length, &handle, 25000);
  2278. if (err < 0) {
  2279. perror("Could not create connection");
  2280. exit(1);
  2281. }
  2282. printf("Connection handle %d\n", handle);
  2283. hci_close_dev(dd);
  2284. }
  2285. static struct option lealadd_options[] = {
  2286. { "help", 0, 0, 'h' },
  2287. { "random", 0, 0, 'r' },
  2288. { 0, 0, 0, 0 }
  2289. };
  2290. static const char *lealadd_help =
  2291. "Usage:\n"
  2292. "\tlealadd [--random] <bdaddr>\n";
  2293. static void cmd_lealadd(int dev_id, int argc, char **argv)
  2294. {
  2295. int err, opt, dd;
  2296. bdaddr_t bdaddr;
  2297. uint8_t bdaddr_type = LE_PUBLIC_ADDRESS;
  2298. for_each_opt(opt, lealadd_options, NULL) {
  2299. switch (opt) {
  2300. case 'r':
  2301. bdaddr_type = LE_RANDOM_ADDRESS;
  2302. break;
  2303. default:
  2304. printf("%s", lealadd_help);
  2305. return;
  2306. }
  2307. }
  2308. helper_arg(1, 1, &argc, &argv, lealadd_help);
  2309. if (dev_id < 0)
  2310. dev_id = hci_get_route(NULL);
  2311. dd = hci_open_dev(dev_id);
  2312. if (dd < 0) {
  2313. perror("Could not open device");
  2314. exit(1);
  2315. }
  2316. str2ba(argv[0], &bdaddr);
  2317. err = hci_le_add_white_list(dd, &bdaddr, bdaddr_type, 1000);
  2318. hci_close_dev(dd);
  2319. if (err < 0) {
  2320. err = -errno;
  2321. fprintf(stderr, "Can't add to accept list: %s(%d)\n",
  2322. strerror(-err), -err);
  2323. exit(1);
  2324. }
  2325. }
  2326. static struct option lealrm_options[] = {
  2327. { "help", 0, 0, 'h' },
  2328. { 0, 0, 0, 0 }
  2329. };
  2330. static const char *lealrm_help =
  2331. "Usage:\n"
  2332. "\tlealrm <bdaddr>\n";
  2333. static void cmd_lealrm(int dev_id, int argc, char **argv)
  2334. {
  2335. int err, opt, dd;
  2336. bdaddr_t bdaddr;
  2337. for_each_opt(opt, lealrm_options, NULL) {
  2338. switch (opt) {
  2339. default:
  2340. printf("%s", lealrm_help);
  2341. return;
  2342. }
  2343. }
  2344. helper_arg(1, 1, &argc, &argv, lealrm_help);
  2345. if (dev_id < 0)
  2346. dev_id = hci_get_route(NULL);
  2347. dd = hci_open_dev(dev_id);
  2348. if (dd < 0) {
  2349. perror("Could not open device");
  2350. exit(1);
  2351. }
  2352. str2ba(argv[0], &bdaddr);
  2353. err = hci_le_rm_white_list(dd, &bdaddr, LE_PUBLIC_ADDRESS, 1000);
  2354. hci_close_dev(dd);
  2355. if (err < 0) {
  2356. err = errno;
  2357. fprintf(stderr, "Can't remove from accept list: %s(%d)\n",
  2358. strerror(err), err);
  2359. exit(1);
  2360. }
  2361. }
  2362. static struct option lealsz_options[] = {
  2363. { "help", 0, 0, 'h' },
  2364. { 0, 0, 0, 0 }
  2365. };
  2366. static const char *lealsz_help =
  2367. "Usage:\n"
  2368. "\tlealsz\n";
  2369. static void cmd_lealsz(int dev_id, int argc, char **argv)
  2370. {
  2371. int err, dd, opt;
  2372. uint8_t size;
  2373. for_each_opt(opt, lealsz_options, NULL) {
  2374. switch (opt) {
  2375. default:
  2376. printf("%s", lealsz_help);
  2377. return;
  2378. }
  2379. }
  2380. helper_arg(0, 0, &argc, &argv, lealsz_help);
  2381. if (dev_id < 0)
  2382. dev_id = hci_get_route(NULL);
  2383. dd = hci_open_dev(dev_id);
  2384. if (dd < 0) {
  2385. perror("Could not open device");
  2386. exit(1);
  2387. }
  2388. err = hci_le_read_white_list_size(dd, &size, 1000);
  2389. hci_close_dev(dd);
  2390. if (err < 0) {
  2391. err = -errno;
  2392. fprintf(stderr, "Can't read accept list size: %s(%d)\n",
  2393. strerror(-err), -err);
  2394. exit(1);
  2395. }
  2396. printf("Accept list size: %d\n", size);
  2397. }
  2398. static struct option lealclr_options[] = {
  2399. { "help", 0, 0, 'h' },
  2400. { 0, 0, 0, 0 }
  2401. };
  2402. static const char *lealclr_help =
  2403. "Usage:\n"
  2404. "\tlealclr\n";
  2405. static void cmd_lealclr(int dev_id, int argc, char **argv)
  2406. {
  2407. int err, dd, opt;
  2408. for_each_opt(opt, lealclr_options, NULL) {
  2409. switch (opt) {
  2410. default:
  2411. printf("%s", lealclr_help);
  2412. return;
  2413. }
  2414. }
  2415. helper_arg(0, 0, &argc, &argv, lealclr_help);
  2416. if (dev_id < 0)
  2417. dev_id = hci_get_route(NULL);
  2418. dd = hci_open_dev(dev_id);
  2419. if (dd < 0) {
  2420. perror("Could not open device");
  2421. exit(1);
  2422. }
  2423. err = hci_le_clear_white_list(dd, 1000);
  2424. hci_close_dev(dd);
  2425. if (err < 0) {
  2426. err = -errno;
  2427. fprintf(stderr, "Can't clear accept list: %s(%d)\n",
  2428. strerror(-err), -err);
  2429. exit(1);
  2430. }
  2431. }
  2432. static struct option lerladd_options[] = {
  2433. { "help", 0, 0, 'h' },
  2434. { "random", 0, 0, 'r' },
  2435. { "local", 1, 0, 'l' },
  2436. { "peer", 1, 0, 'p' },
  2437. { 0, 0, 0, 0 }
  2438. };
  2439. static const char *lerladd_help =
  2440. "Usage:\n"
  2441. "\tlerladd [--local irk] [--peer irk] [--random] <bdaddr>\n";
  2442. static void cmd_lerladd(int dev_id, int argc, char **argv)
  2443. {
  2444. int err, opt, dd;
  2445. bdaddr_t bdaddr;
  2446. uint8_t bdaddr_type = LE_PUBLIC_ADDRESS;
  2447. uint8_t local_irk[16], peer_irk[16];
  2448. memset(local_irk, 0, 16);
  2449. memset(peer_irk, 0, 16);
  2450. for_each_opt(opt, lerladd_options, NULL) {
  2451. switch (opt) {
  2452. case 'r':
  2453. bdaddr_type = LE_RANDOM_ADDRESS;
  2454. break;
  2455. case 'l':
  2456. str2buf(optarg, local_irk, 16);
  2457. break;
  2458. case 'p':
  2459. str2buf(optarg, peer_irk, 16);
  2460. break;
  2461. default:
  2462. printf("%s", lerladd_help);
  2463. return;
  2464. }
  2465. }
  2466. helper_arg(1, 1, &argc, &argv, lerladd_help);
  2467. if (dev_id < 0)
  2468. dev_id = hci_get_route(NULL);
  2469. dd = hci_open_dev(dev_id);
  2470. if (dd < 0) {
  2471. perror("Could not open device");
  2472. exit(1);
  2473. }
  2474. str2ba(argv[0], &bdaddr);
  2475. err = hci_le_add_resolving_list(dd, &bdaddr, bdaddr_type,
  2476. peer_irk, local_irk, 1000);
  2477. hci_close_dev(dd);
  2478. if (err < 0) {
  2479. err = -errno;
  2480. fprintf(stderr, "Can't add to resolving list: %s(%d)\n",
  2481. strerror(-err), -err);
  2482. exit(1);
  2483. }
  2484. }
  2485. static struct option lerlrm_options[] = {
  2486. { "help", 0, 0, 'h' },
  2487. { 0, 0, 0, 0 }
  2488. };
  2489. static const char *lerlrm_help =
  2490. "Usage:\n"
  2491. "\tlerlrm <bdaddr>\n";
  2492. static void cmd_lerlrm(int dev_id, int argc, char **argv)
  2493. {
  2494. int err, opt, dd;
  2495. bdaddr_t bdaddr;
  2496. for_each_opt(opt, lerlrm_options, NULL) {
  2497. switch (opt) {
  2498. default:
  2499. printf("%s", lerlrm_help);
  2500. return;
  2501. }
  2502. }
  2503. helper_arg(1, 1, &argc, &argv, lerlrm_help);
  2504. if (dev_id < 0)
  2505. dev_id = hci_get_route(NULL);
  2506. dd = hci_open_dev(dev_id);
  2507. if (dd < 0) {
  2508. perror("Could not open device");
  2509. exit(1);
  2510. }
  2511. str2ba(argv[0], &bdaddr);
  2512. err = hci_le_rm_resolving_list(dd, &bdaddr, LE_PUBLIC_ADDRESS, 1000);
  2513. hci_close_dev(dd);
  2514. if (err < 0) {
  2515. err = errno;
  2516. fprintf(stderr, "Can't remove from resolving list: %s(%d)\n",
  2517. strerror(err), err);
  2518. exit(1);
  2519. }
  2520. }
  2521. static struct option lerlclr_options[] = {
  2522. { "help", 0, 0, 'h' },
  2523. { 0, 0, 0, 0 }
  2524. };
  2525. static const char *lerlclr_help =
  2526. "Usage:\n"
  2527. "\tlerlclr\n";
  2528. static void cmd_lerlclr(int dev_id, int argc, char **argv)
  2529. {
  2530. int err, dd, opt;
  2531. for_each_opt(opt, lerlclr_options, NULL) {
  2532. switch (opt) {
  2533. default:
  2534. printf("%s", lerlclr_help);
  2535. return;
  2536. }
  2537. }
  2538. helper_arg(0, 0, &argc, &argv, lerlclr_help);
  2539. if (dev_id < 0)
  2540. dev_id = hci_get_route(NULL);
  2541. dd = hci_open_dev(dev_id);
  2542. if (dd < 0) {
  2543. perror("Could not open device");
  2544. exit(1);
  2545. }
  2546. err = hci_le_clear_resolving_list(dd, 1000);
  2547. hci_close_dev(dd);
  2548. if (err < 0) {
  2549. err = -errno;
  2550. fprintf(stderr, "Can't clear resolving list: %s(%d)\n",
  2551. strerror(-err), -err);
  2552. exit(1);
  2553. }
  2554. }
  2555. static struct option lerlsz_options[] = {
  2556. { "help", 0, 0, 'h' },
  2557. { 0, 0, 0, 0 }
  2558. };
  2559. static const char *lerlsz_help =
  2560. "Usage:\n"
  2561. "\tlerlsz\n";
  2562. static void cmd_lerlsz(int dev_id, int argc, char **argv)
  2563. {
  2564. int err, dd, opt;
  2565. uint8_t size;
  2566. for_each_opt(opt, lerlsz_options, NULL) {
  2567. switch (opt) {
  2568. default:
  2569. printf("%s", lerlsz_help);
  2570. return;
  2571. }
  2572. }
  2573. helper_arg(0, 0, &argc, &argv, lerlsz_help);
  2574. if (dev_id < 0)
  2575. dev_id = hci_get_route(NULL);
  2576. dd = hci_open_dev(dev_id);
  2577. if (dd < 0) {
  2578. perror("Could not open device");
  2579. exit(1);
  2580. }
  2581. err = hci_le_read_resolving_list_size(dd, &size, 1000);
  2582. hci_close_dev(dd);
  2583. if (err < 0) {
  2584. err = -errno;
  2585. fprintf(stderr, "Can't read resolving list size: %s(%d)\n",
  2586. strerror(-err), -err);
  2587. exit(1);
  2588. }
  2589. printf("Resolving list size: %d\n", size);
  2590. }
  2591. static struct option lerlon_options[] = {
  2592. { "help", 0, 0, 'h' },
  2593. { 0, 0, 0, 0 }
  2594. };
  2595. static const char *lerlon_help =
  2596. "Usage:\n"
  2597. "\tlerlon\n";
  2598. static void cmd_lerlon(int dev_id, int argc, char **argv)
  2599. {
  2600. int err, dd, opt;
  2601. for_each_opt(opt, lerlon_options, NULL) {
  2602. switch (opt) {
  2603. default:
  2604. printf("%s", lerlon_help);
  2605. return;
  2606. }
  2607. }
  2608. helper_arg(0, 0, &argc, &argv, lerlon_help);
  2609. if (dev_id < 0)
  2610. dev_id = hci_get_route(NULL);
  2611. dd = hci_open_dev(dev_id);
  2612. if (dd < 0) {
  2613. perror("Could not open device");
  2614. exit(1);
  2615. }
  2616. err = hci_le_set_address_resolution_enable(dd, 0x01, 1000);
  2617. hci_close_dev(dd);
  2618. if (err < 0) {
  2619. err = -errno;
  2620. fprintf(stderr, "Can't set address resolution enable: %s(%d)\n",
  2621. strerror(-err), -err);
  2622. exit(1);
  2623. }
  2624. }
  2625. static struct option lerloff_options[] = {
  2626. { "help", 0, 0, 'h' },
  2627. { 0, 0, 0, 0 }
  2628. };
  2629. static const char *lerloff_help =
  2630. "Usage:\n"
  2631. "\tlerloff\n";
  2632. static void cmd_lerloff(int dev_id, int argc, char **argv)
  2633. {
  2634. int err, dd, opt;
  2635. for_each_opt(opt, lerloff_options, NULL) {
  2636. switch (opt) {
  2637. default:
  2638. printf("%s", lerloff_help);
  2639. return;
  2640. }
  2641. }
  2642. helper_arg(0, 0, &argc, &argv, lerloff_help);
  2643. if (dev_id < 0)
  2644. dev_id = hci_get_route(NULL);
  2645. dd = hci_open_dev(dev_id);
  2646. if (dd < 0) {
  2647. perror("Could not open device");
  2648. exit(1);
  2649. }
  2650. err = hci_le_set_address_resolution_enable(dd, 0x00, 1000);
  2651. hci_close_dev(dd);
  2652. if (err < 0) {
  2653. err = -errno;
  2654. fprintf(stderr, "Can't set address resolution enable: %s(%d)\n",
  2655. strerror(-err), -err);
  2656. exit(1);
  2657. }
  2658. }
  2659. static struct option ledc_options[] = {
  2660. { "help", 0, 0, 'h' },
  2661. { 0, 0, 0, 0 }
  2662. };
  2663. static const char *ledc_help =
  2664. "Usage:\n"
  2665. "\tledc <handle> [reason]\n";
  2666. static void cmd_ledc(int dev_id, int argc, char **argv)
  2667. {
  2668. int err, opt, dd;
  2669. uint16_t handle;
  2670. uint8_t reason;
  2671. for_each_opt(opt, ledc_options, NULL) {
  2672. switch (opt) {
  2673. default:
  2674. printf("%s", ledc_help);
  2675. return;
  2676. }
  2677. }
  2678. helper_arg(1, 2, &argc, &argv, ledc_help);
  2679. if (dev_id < 0)
  2680. dev_id = hci_get_route(NULL);
  2681. dd = hci_open_dev(dev_id);
  2682. if (dd < 0) {
  2683. perror("Could not open device");
  2684. exit(1);
  2685. }
  2686. handle = atoi(argv[0]);
  2687. reason = (argc > 1) ? atoi(argv[1]) : HCI_OE_USER_ENDED_CONNECTION;
  2688. err = hci_disconnect(dd, handle, reason, 10000);
  2689. if (err < 0) {
  2690. perror("Could not disconnect");
  2691. exit(1);
  2692. }
  2693. hci_close_dev(dd);
  2694. }
  2695. static struct option lecup_options[] = {
  2696. { "help", 0, 0, 'h' },
  2697. { "handle", 1, 0, 'H' },
  2698. { "min", 1, 0, 'm' },
  2699. { "max", 1, 0, 'M' },
  2700. { "latency", 1, 0, 'l' },
  2701. { "timeout", 1, 0, 't' },
  2702. { 0, 0, 0, 0 }
  2703. };
  2704. static const char *lecup_help =
  2705. "Usage:\n"
  2706. "\tlecup <handle> <min> <max> <latency> <timeout>\n"
  2707. "\tOptions:\n"
  2708. "\t --handle=<0xXXXX> LE connection handle\n"
  2709. "\t --min=<interval> Range: 0x0006 to 0x0C80\n"
  2710. "\t --max=<interval> Range: 0x0006 to 0x0C80\n"
  2711. "\t --latency=<range> Peripheral latency. Range: 0x0000 to 0x03E8\n"
  2712. "\t --timeout=<time> N * 10ms. Range: 0x000A to 0x0C80\n"
  2713. "\n\t min/max range: 7.5ms to 4s. Multiply factor: 1.25ms"
  2714. "\n\t timeout range: 100ms to 32.0s. Larger than max interval\n";
  2715. static void cmd_lecup(int dev_id, int argc, char **argv)
  2716. {
  2717. uint16_t handle = 0, min, max, latency, timeout;
  2718. int opt, dd;
  2719. int options = 0;
  2720. /* Aleatory valid values */
  2721. min = 0x0C8;
  2722. max = 0x0960;
  2723. latency = 0x0007;
  2724. timeout = 0x0C80;
  2725. for_each_opt(opt, lecup_options, NULL) {
  2726. switch (opt) {
  2727. case 'H':
  2728. handle = strtoul(optarg, NULL, 0);
  2729. break;
  2730. case 'm':
  2731. min = strtoul(optarg, NULL, 0);
  2732. break;
  2733. case 'M':
  2734. max = strtoul(optarg, NULL, 0);
  2735. break;
  2736. case 'l':
  2737. latency = strtoul(optarg, NULL, 0);
  2738. break;
  2739. case 't':
  2740. timeout = strtoul(optarg, NULL, 0);
  2741. break;
  2742. default:
  2743. printf("%s", lecup_help);
  2744. return;
  2745. }
  2746. options = 1;
  2747. }
  2748. if (options == 0) {
  2749. helper_arg(5, 5, &argc, &argv, lecup_help);
  2750. handle = strtoul(argv[0], NULL, 0);
  2751. min = strtoul(argv[1], NULL, 0);
  2752. max = strtoul(argv[2], NULL, 0);
  2753. latency = strtoul(argv[3], NULL, 0);
  2754. timeout = strtoul(argv[4], NULL, 0);
  2755. }
  2756. if (handle > 0x0EFF) {
  2757. printf("%s", lecup_help);
  2758. return;
  2759. }
  2760. if (dev_id < 0)
  2761. dev_id = hci_get_route(NULL);
  2762. dd = hci_open_dev(dev_id);
  2763. if (dd < 0) {
  2764. fprintf(stderr, "HCI device open failed\n");
  2765. exit(1);
  2766. }
  2767. if (hci_le_conn_update(dd, htobs(handle), htobs(min), htobs(max),
  2768. htobs(latency), htobs(timeout), 5000) < 0) {
  2769. int err = -errno;
  2770. fprintf(stderr, "Could not change connection params: %s(%d)\n",
  2771. strerror(-err), -err);
  2772. }
  2773. hci_close_dev(dd);
  2774. }
  2775. static struct {
  2776. char *cmd;
  2777. void (*func)(int dev_id, int argc, char **argv);
  2778. char *doc;
  2779. } command[] = {
  2780. { "dev", cmd_dev, "Display local devices" },
  2781. { "inq", cmd_inq, "Inquire remote devices" },
  2782. { "scan", cmd_scan, "Scan for remote devices" },
  2783. { "name", cmd_name, "Get name from remote device" },
  2784. { "info", cmd_info, "Get information from remote device" },
  2785. { "spinq", cmd_spinq, "Start periodic inquiry" },
  2786. { "epinq", cmd_epinq, "Exit periodic inquiry" },
  2787. { "cmd", cmd_cmd, "Submit arbitrary HCI commands" },
  2788. { "con", cmd_con, "Display active connections" },
  2789. { "cc", cmd_cc, "Create connection to remote device" },
  2790. { "dc", cmd_dc, "Disconnect from remote device" },
  2791. { "sr", cmd_sr, "Switch central/peripheral role" },
  2792. { "cpt", cmd_cpt, "Change connection packet type" },
  2793. { "rssi", cmd_rssi, "Display connection RSSI" },
  2794. { "lq", cmd_lq, "Display link quality" },
  2795. { "tpl", cmd_tpl, "Display transmit power level" },
  2796. { "afh", cmd_afh, "Display AFH channel map" },
  2797. { "lp", cmd_lp, "Set/display link policy settings" },
  2798. { "lst", cmd_lst, "Set/display link supervision timeout" },
  2799. { "auth", cmd_auth, "Request authentication" },
  2800. { "enc", cmd_enc, "Set connection encryption" },
  2801. { "key", cmd_key, "Change connection link key" },
  2802. { "clkoff", cmd_clkoff, "Read clock offset" },
  2803. { "clock", cmd_clock, "Read local or remote clock" },
  2804. { "lescan", cmd_lescan, "Start LE scan" },
  2805. { "leinfo", cmd_leinfo, "Get LE remote information" },
  2806. { "lealadd", cmd_lealadd, "Add device to LE White List" },
  2807. { "lealrm", cmd_lealrm, "Remove device from LE White List" },
  2808. { "lealsz", cmd_lealsz, "Read size of LE White List" },
  2809. { "lealclr", cmd_lealclr, "Clear LE White List" },
  2810. { "lewladd", cmd_lealadd, "Deprecated. Use lealadd instead." },
  2811. { "lewlrm", cmd_lealrm, "Deprecated. Use lealrm instead." },
  2812. { "lewlsz", cmd_lealsz, "Deprecated. Use lealsz instead." },
  2813. { "lewlclr", cmd_lealclr, "Deprecated. Use lealclr instead." },
  2814. { "lerladd", cmd_lerladd, "Add device to LE Resolving List" },
  2815. { "lerlrm", cmd_lerlrm, "Remove device from LE Resolving List" },
  2816. { "lerlclr", cmd_lerlclr, "Clear LE Resolving List" },
  2817. { "lerlsz", cmd_lerlsz, "Read size of LE Resolving List" },
  2818. { "lerlon", cmd_lerlon, "Enable LE Address Resolution" },
  2819. { "lerloff", cmd_lerloff, "Disable LE Address Resolution" },
  2820. { "lecc", cmd_lecc, "Create a LE Connection" },
  2821. { "ledc", cmd_ledc, "Disconnect a LE Connection" },
  2822. { "lecup", cmd_lecup, "LE Connection Update" },
  2823. { NULL, NULL, 0 }
  2824. };
  2825. static void usage(void)
  2826. {
  2827. int i;
  2828. printf("hcitool - HCI Tool ver %s\n", VERSION);
  2829. printf("Usage:\n"
  2830. "\thcitool [options] <command> [command parameters]\n");
  2831. printf("Options:\n"
  2832. "\t--help\tDisplay help\n"
  2833. "\t-i dev\tHCI device\n");
  2834. printf("Commands:\n");
  2835. for (i = 0; command[i].cmd; i++)
  2836. printf("\t%-4s\t%s\n", command[i].cmd,
  2837. command[i].doc);
  2838. printf("\n"
  2839. "For more information on the usage of each command use:\n"
  2840. "\thcitool <command> --help\n" );
  2841. }
  2842. static struct option main_options[] = {
  2843. { "help", 0, 0, 'h' },
  2844. { "device", 1, 0, 'i' },
  2845. { 0, 0, 0, 0 }
  2846. };
  2847. int main(int argc, char *argv[])
  2848. {
  2849. int opt, i, dev_id = -1;
  2850. bdaddr_t ba;
  2851. while ((opt=getopt_long(argc, argv, "+i:h", main_options, NULL)) != -1) {
  2852. switch (opt) {
  2853. case 'i':
  2854. dev_id = hci_devid(optarg);
  2855. if (dev_id < 0) {
  2856. perror("Invalid device");
  2857. exit(1);
  2858. }
  2859. break;
  2860. case 'h':
  2861. default:
  2862. usage();
  2863. exit(0);
  2864. }
  2865. }
  2866. argc -= optind;
  2867. argv += optind;
  2868. optind = 0;
  2869. if (argc < 1) {
  2870. usage();
  2871. exit(0);
  2872. }
  2873. if (dev_id != -1 && hci_devba(dev_id, &ba) < 0) {
  2874. perror("Device is not available");
  2875. exit(1);
  2876. }
  2877. for (i = 0; command[i].cmd; i++) {
  2878. if (strncmp(command[i].cmd,
  2879. argv[0], strlen(command[i].cmd)))
  2880. continue;
  2881. command[i].func(dev_id, argc, argv);
  2882. break;
  2883. }
  2884. if (command[i].cmd == 0) {
  2885. fprintf(stderr, "Unknown command - \"%s\"\n", *argv);
  2886. exit(1);
  2887. }
  2888. return 0;
  2889. }