tester-main.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367
  1. // SPDX-License-Identifier: Apache-2.0
  2. /*
  3. * Copyright (C) 2014 Intel Corporation
  4. *
  5. */
  6. #define _GNU_SOURCE
  7. #include <stdbool.h>
  8. #include <unistd.h>
  9. #include <libgen.h>
  10. #include <sys/un.h>
  11. #include <sys/wait.h>
  12. #include <sys/signalfd.h>
  13. #include "lib/bluetooth.h"
  14. #include "lib/mgmt.h"
  15. #include "src/shared/util.h"
  16. #include "src/shared/tester.h"
  17. #include "src/shared/mgmt.h"
  18. #include "src/shared/queue.h"
  19. #include "emulator/bthost.h"
  20. #include "monitor/bt.h"
  21. #include "tester-main.h"
  22. static char exec_dir[PATH_MAX + 1];
  23. static gint scheduled_cbacks_num;
  24. #define EMULATOR_SIGNAL_TIMEOUT 2 /* in seconds */
  25. #define EMULATOR_SIGNAL "emulator_started"
  26. #define BT_TRANSPORT_UNKNOWN 0x00
  27. static struct {
  28. uint16_t cb_num;
  29. const char *str;
  30. } cb_table[] = {
  31. DBG_CB(CB_BT_NONE),
  32. DBG_CB(CB_BT_ADAPTER_STATE_CHANGED),
  33. DBG_CB(CB_BT_ADAPTER_PROPERTIES),
  34. DBG_CB(CB_BT_REMOTE_DEVICE_PROPERTIES),
  35. DBG_CB(CB_BT_DEVICE_FOUND),
  36. DBG_CB(CB_BT_DISCOVERY_STATE_CHANGED),
  37. DBG_CB(CB_BT_PIN_REQUEST),
  38. DBG_CB(CB_BT_SSP_REQUEST),
  39. DBG_CB(CB_BT_BOND_STATE_CHANGED),
  40. DBG_CB(CB_BT_ACL_STATE_CHANGED),
  41. DBG_CB(CB_BT_THREAD_EVT),
  42. DBG_CB(CB_BT_DUT_MODE_RECV),
  43. DBG_CB(CB_BT_LE_TEST_MODE),
  44. /* Hidhost cb */
  45. DBG_CB(CB_HH_CONNECTION_STATE),
  46. DBG_CB(CB_HH_HID_INFO),
  47. DBG_CB(CB_HH_PROTOCOL_MODE),
  48. DBG_CB(CB_HH_IDLE_TIME),
  49. DBG_CB(CB_HH_GET_REPORT),
  50. DBG_CB(CB_HH_VIRTUAL_UNPLUG),
  51. /* PAN cb */
  52. DBG_CB(CB_PAN_CONTROL_STATE),
  53. DBG_CB(CB_PAN_CONNECTION_STATE),
  54. /* HDP cb */
  55. DBG_CB(CB_HDP_APP_REG_STATE),
  56. DBG_CB(CB_HDP_CHANNEL_STATE),
  57. /* A2DP cb */
  58. DBG_CB(CB_A2DP_CONN_STATE),
  59. DBG_CB(CB_A2DP_AUDIO_STATE),
  60. /* AVRCP */
  61. DBG_CB(CB_AVRCP_PLAY_STATUS_REQ),
  62. DBG_CB(CB_AVRCP_PLAY_STATUS_RSP),
  63. DBG_CB(CB_AVRCP_REG_NOTIF_REQ),
  64. DBG_CB(CB_AVRCP_REG_NOTIF_RSP),
  65. DBG_CB(CB_AVRCP_GET_ATTR_REQ),
  66. DBG_CB(CB_AVRCP_GET_ATTR_RSP),
  67. /* Gatt client */
  68. DBG_CB(CB_GATTC_REGISTER_CLIENT),
  69. DBG_CB(CB_GATTC_SCAN_RESULT),
  70. DBG_CB(CB_GATTC_OPEN),
  71. DBG_CB(CB_GATTC_CLOSE),
  72. DBG_CB(CB_GATTC_SEARCH_COMPLETE),
  73. DBG_CB(CB_GATTC_SEARCH_RESULT),
  74. DBG_CB(CB_GATTC_GET_CHARACTERISTIC),
  75. DBG_CB(CB_GATTC_GET_DESCRIPTOR),
  76. DBG_CB(CB_GATTC_GET_INCLUDED_SERVICE),
  77. DBG_CB(CB_GATTC_REGISTER_FOR_NOTIFICATION),
  78. DBG_CB(CB_GATTC_NOTIFY),
  79. DBG_CB(CB_GATTC_READ_CHARACTERISTIC),
  80. DBG_CB(CB_GATTC_WRITE_CHARACTERISTIC),
  81. DBG_CB(CB_GATTC_READ_DESCRIPTOR),
  82. DBG_CB(CB_GATTC_WRITE_DESCRIPTOR),
  83. DBG_CB(CB_GATTC_EXECUTE_WRITE),
  84. DBG_CB(CB_GATTC_READ_REMOTE_RSSI),
  85. DBG_CB(CB_GATTC_LISTEN),
  86. /* Gatt server */
  87. DBG_CB(CB_GATTS_REGISTER_SERVER),
  88. DBG_CB(CB_GATTS_CONNECTION),
  89. DBG_CB(CB_GATTS_SERVICE_ADDED),
  90. DBG_CB(CB_GATTS_INCLUDED_SERVICE_ADDED),
  91. DBG_CB(CB_GATTS_CHARACTERISTIC_ADDED),
  92. DBG_CB(CB_GATTS_DESCRIPTOR_ADDED),
  93. DBG_CB(CB_GATTS_SERVICE_STARTED),
  94. DBG_CB(CB_GATTS_SERVICE_STOPPED),
  95. DBG_CB(CB_GATTS_SERVICE_DELETED),
  96. DBG_CB(CB_GATTS_REQUEST_READ),
  97. DBG_CB(CB_GATTS_REQUEST_WRITE),
  98. DBG_CB(CB_GATTS_REQUEST_EXEC_WRITE),
  99. DBG_CB(CB_GATTS_RESPONSE_CONFIRMATION),
  100. DBG_CB(CB_GATTS_INDICATION_SEND),
  101. /* Map client */
  102. DBG_CB(CB_MAP_CLIENT_REMOTE_MAS_INSTANCES),
  103. /* Emulator callbacks */
  104. DBG_CB(CB_EMU_CONFIRM_SEND_DATA),
  105. DBG_CB(CB_EMU_ENCRYPTION_ENABLED),
  106. DBG_CB(CB_EMU_ENCRYPTION_DISABLED),
  107. DBG_CB(CB_EMU_CONNECTION_REJECTED),
  108. DBG_CB(CB_EMU_VALUE_INDICATION),
  109. DBG_CB(CB_EMU_VALUE_NOTIFICATION),
  110. DBG_CB(CB_EMU_READ_RESPONSE),
  111. DBG_CB(CB_EMU_WRITE_RESPONSE),
  112. DBG_CB(CB_EMU_ATT_ERROR),
  113. };
  114. static gboolean check_callbacks_called(gpointer user_data)
  115. {
  116. /*
  117. * Wait for all callbacks scheduled in current test context to execute
  118. * in main loop. This will avoid late callback calls after test case has
  119. * already failed or timed out.
  120. */
  121. if (g_atomic_int_get(&scheduled_cbacks_num) == 0) {
  122. tester_teardown_complete();
  123. return FALSE;
  124. } else if (scheduled_cbacks_num < 0) {
  125. tester_warn("Unscheduled callback called!");
  126. return FALSE;
  127. }
  128. return TRUE;
  129. }
  130. static void check_daemon_term(void)
  131. {
  132. int status;
  133. pid_t pid;
  134. struct test_data *data = tester_get_data();
  135. if (!data)
  136. return;
  137. pid = waitpid(data->bluetoothd_pid, &status, WNOHANG);
  138. if (pid != data->bluetoothd_pid)
  139. return;
  140. data->bluetoothd_pid = 0;
  141. if (WIFEXITED(status) && (WEXITSTATUS(status) == EXIT_SUCCESS)) {
  142. g_idle_add(check_callbacks_called, NULL);
  143. return;
  144. }
  145. tester_warn("Unexpected Daemon shutdown with status %d", status);
  146. }
  147. static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
  148. gpointer user_data)
  149. {
  150. struct signalfd_siginfo si;
  151. ssize_t result;
  152. int fd;
  153. if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
  154. return FALSE;
  155. fd = g_io_channel_unix_get_fd(channel);
  156. result = read(fd, &si, sizeof(si));
  157. if (result != sizeof(si))
  158. return FALSE;
  159. switch (si.ssi_signo) {
  160. case SIGCHLD:
  161. check_daemon_term();
  162. break;
  163. }
  164. return TRUE;
  165. }
  166. static guint setup_signalfd(void)
  167. {
  168. GIOChannel *channel;
  169. guint source;
  170. sigset_t mask;
  171. int fd;
  172. sigemptyset(&mask);
  173. sigaddset(&mask, SIGCHLD);
  174. if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0)
  175. return 0;
  176. fd = signalfd(-1, &mask, 0);
  177. if (fd < 0)
  178. return 0;
  179. channel = g_io_channel_unix_new(fd);
  180. g_io_channel_set_close_on_unref(channel, TRUE);
  181. g_io_channel_set_encoding(channel, NULL, NULL);
  182. g_io_channel_set_buffered(channel, FALSE);
  183. source = g_io_add_watch(channel,
  184. G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
  185. signal_handler, NULL);
  186. g_io_channel_unref(channel);
  187. return source;
  188. }
  189. static void test_post_teardown(const void *test_data)
  190. {
  191. struct test_data *data = tester_get_data();
  192. /* remove hook for encryption change */
  193. hciemu_del_hook(data->hciemu, HCIEMU_HOOK_POST_EVT, 0x08);
  194. hciemu_unref(data->hciemu);
  195. data->hciemu = NULL;
  196. g_source_remove(data->signalfd);
  197. data->signalfd = 0;
  198. }
  199. static void bluetoothd_start(int hci_index)
  200. {
  201. char prg_name[PATH_MAX + 1 + 11];
  202. char index[8];
  203. char *prg_argv[5];
  204. snprintf(prg_name, sizeof(prg_name), "%s/%s", exec_dir, "bluetoothd");
  205. snprintf(index, sizeof(index), "%d", hci_index);
  206. prg_argv[0] = prg_name;
  207. prg_argv[1] = "-i";
  208. prg_argv[2] = index;
  209. prg_argv[3] = "-d";
  210. prg_argv[4] = NULL;
  211. if (!tester_use_debug())
  212. fclose(stderr);
  213. execve(prg_argv[0], prg_argv, NULL);
  214. }
  215. static void emulator(int pipe, int hci_index)
  216. {
  217. static const char SYSTEM_SOCKET_PATH[] = "\0android_system";
  218. char buf[1024];
  219. struct sockaddr_un addr;
  220. struct timeval tv;
  221. int fd;
  222. ssize_t len;
  223. fd = socket(PF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0);
  224. if (fd < 0)
  225. goto failed;
  226. tv.tv_sec = EMULATOR_SIGNAL_TIMEOUT;
  227. tv.tv_usec = 0;
  228. setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv));
  229. memset(&addr, 0, sizeof(addr));
  230. addr.sun_family = AF_UNIX;
  231. memcpy(addr.sun_path, SYSTEM_SOCKET_PATH, sizeof(SYSTEM_SOCKET_PATH));
  232. if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  233. perror("Failed to bind system socket");
  234. goto failed;
  235. }
  236. len = write(pipe, EMULATOR_SIGNAL, sizeof(EMULATOR_SIGNAL));
  237. if (len != sizeof(EMULATOR_SIGNAL))
  238. goto failed;
  239. memset(buf, 0, sizeof(buf));
  240. len = read(fd, buf, sizeof(buf));
  241. if (len <= 0 || strcmp(buf, "bluetooth.start=daemon"))
  242. goto failed;
  243. close(pipe);
  244. close(fd);
  245. return bluetoothd_start(hci_index);
  246. failed:
  247. close(pipe);
  248. if (fd >= 0)
  249. close(fd);
  250. }
  251. static void mgmt_debug(const char *str, void *user_data)
  252. {
  253. const char *prefix = user_data;
  254. tester_print("%s%s", prefix, str);
  255. }
  256. static bool hciemu_post_encr_hook(const void *data, uint16_t len,
  257. void *user_data)
  258. {
  259. struct step *step;
  260. /*
  261. * Expected data: status (1 octet) + conn. handle (2 octets) +
  262. * encryption flag (1 octet)
  263. */
  264. if (len < 4)
  265. return true;
  266. step = g_new0(struct step, 1);
  267. step->callback = ((uint8_t *)data)[3] ? CB_EMU_ENCRYPTION_ENABLED :
  268. CB_EMU_ENCRYPTION_DISABLED;
  269. schedule_callback_verification(step);
  270. return true;
  271. }
  272. static void read_info_callback(uint8_t status, uint16_t length,
  273. const void *param, void *user_data)
  274. {
  275. struct test_data *data = tester_get_data();
  276. const struct mgmt_rp_read_info *rp = param;
  277. char addr[18];
  278. uint16_t manufacturer;
  279. uint32_t supported_settings, current_settings;
  280. tester_print("Read Info callback");
  281. tester_print(" Status: 0x%02x", status);
  282. if (status || !param) {
  283. tester_pre_setup_failed();
  284. return;
  285. }
  286. ba2str(&rp->bdaddr, addr);
  287. manufacturer = btohs(rp->manufacturer);
  288. supported_settings = btohl(rp->supported_settings);
  289. current_settings = btohl(rp->current_settings);
  290. tester_print(" Address: %s", addr);
  291. tester_print(" Version: 0x%02x", rp->version);
  292. tester_print(" Manufacturer: 0x%04x", manufacturer);
  293. tester_print(" Supported settings: 0x%08x", supported_settings);
  294. tester_print(" Current settings: 0x%08x", current_settings);
  295. tester_print(" Class: 0x%02x%02x%02x",
  296. rp->dev_class[2], rp->dev_class[1], rp->dev_class[0]);
  297. tester_print(" Name: %s", rp->name);
  298. tester_print(" Short name: %s", rp->short_name);
  299. if (strcmp(hciemu_get_address(data->hciemu), addr)) {
  300. tester_pre_setup_failed();
  301. return;
  302. }
  303. /* set hook for encryption change */
  304. hciemu_add_hook(data->hciemu, HCIEMU_HOOK_POST_EVT, 0x08,
  305. hciemu_post_encr_hook, NULL);
  306. tester_pre_setup_complete();
  307. }
  308. static void index_added_callback(uint16_t index, uint16_t length,
  309. const void *param, void *user_data)
  310. {
  311. struct test_data *data = tester_get_data();
  312. tester_print("Index Added callback");
  313. tester_print(" Index: 0x%04x", index);
  314. data->mgmt_index = index;
  315. mgmt_send(data->mgmt, MGMT_OP_READ_INFO, data->mgmt_index, 0, NULL,
  316. read_info_callback, NULL, NULL);
  317. }
  318. static void index_removed_callback(uint16_t index, uint16_t length,
  319. const void *param, void *user_data)
  320. {
  321. struct test_data *data = tester_get_data();
  322. tester_print("Index Removed callback");
  323. tester_print(" Index: 0x%04x", index);
  324. if (index != data->mgmt_index)
  325. return;
  326. mgmt_unregister_index(data->mgmt, data->mgmt_index);
  327. mgmt_unref(data->mgmt);
  328. data->mgmt = NULL;
  329. tester_post_teardown_complete();
  330. }
  331. static void read_index_list_callback(uint8_t status, uint16_t length,
  332. const void *param, void *user_data)
  333. {
  334. struct test_data *data = tester_get_data();
  335. tester_print("Read Index List callback");
  336. tester_print(" Status: 0x%02x", status);
  337. if (status || !param) {
  338. tester_pre_setup_failed();
  339. return;
  340. }
  341. mgmt_register(data->mgmt, MGMT_EV_INDEX_ADDED, MGMT_INDEX_NONE,
  342. index_added_callback, NULL, NULL);
  343. mgmt_register(data->mgmt, MGMT_EV_INDEX_REMOVED, MGMT_INDEX_NONE,
  344. index_removed_callback, NULL, NULL);
  345. data->hciemu = hciemu_new(data->hciemu_type);
  346. if (!data->hciemu) {
  347. tester_warn("Failed to setup HCI emulation");
  348. tester_pre_setup_failed();
  349. return;
  350. }
  351. tester_print("New hciemu instance created");
  352. }
  353. static void test_pre_setup(const void *test_data)
  354. {
  355. struct test_data *data = tester_get_data();
  356. data->signalfd = setup_signalfd();
  357. if (!data->signalfd) {
  358. tester_warn("Failed to setup signalfd");
  359. tester_pre_setup_failed();
  360. return;
  361. }
  362. data->mgmt = mgmt_new_default();
  363. if (!data->mgmt) {
  364. tester_warn("Failed to setup management interface");
  365. tester_pre_setup_failed();
  366. return;
  367. }
  368. if (!tester_use_debug())
  369. fclose(stderr);
  370. else
  371. mgmt_set_debug(data->mgmt, mgmt_debug, "mgmt: ", NULL);
  372. mgmt_send(data->mgmt, MGMT_OP_READ_INDEX_LIST, MGMT_INDEX_NONE, 0,
  373. NULL, read_index_list_callback, NULL, NULL);
  374. }
  375. static bool match_property(bt_property_t *exp_prop, bt_property_t *rec_prop,
  376. int prop_num)
  377. {
  378. if (exp_prop->type && (exp_prop->type != rec_prop->type))
  379. return 0;
  380. if (exp_prop->len && (exp_prop->len != rec_prop->len)) {
  381. tester_debug("Property [%d] len don't match! received=%d, "
  382. "expected=%d", prop_num, rec_prop->len,
  383. exp_prop->len);
  384. return 0;
  385. }
  386. if (exp_prop->val && memcmp(exp_prop->val, rec_prop->val,
  387. exp_prop->len)) {
  388. tester_debug("Property [%d] value don't match!", prop_num);
  389. return 0;
  390. }
  391. return 1;
  392. }
  393. static bool match_mas_inst(btmce_mas_instance_t *exp_inst,
  394. btmce_mas_instance_t *rec_inst, int inst_num)
  395. {
  396. if (exp_inst->id && (exp_inst->id != rec_inst->id)) {
  397. tester_debug("MAS inst. [%d] id missmatch %d vs %d", inst_num,
  398. rec_inst->id, exp_inst->id);
  399. return 0;
  400. }
  401. if (exp_inst->scn && (exp_inst->scn != rec_inst->scn)) {
  402. tester_debug("MAS inst. [%d] scn missmatch %d vs %d", inst_num,
  403. rec_inst->scn, exp_inst->scn);
  404. return 0;
  405. }
  406. if (exp_inst->msg_types &&
  407. (exp_inst->msg_types != rec_inst->msg_types)) {
  408. tester_debug("Mas inst. [%d] mesg type missmatch %d vs %d",
  409. inst_num, rec_inst->scn, exp_inst->scn);
  410. return 0;
  411. }
  412. if (exp_inst->p_name && memcmp(exp_inst->p_name, rec_inst->p_name,
  413. strlen(exp_inst->p_name))) {
  414. tester_debug("Mas inst. [%d] name don't match!", inst_num);
  415. return 0;
  416. }
  417. return 1;
  418. }
  419. static int verify_property(bt_property_t *exp_props, int exp_num_props,
  420. bt_property_t *rec_props, int rec_num_props)
  421. {
  422. int i, j;
  423. int exp_prop_to_find = exp_num_props;
  424. if (rec_num_props == 0)
  425. return 1;
  426. if (exp_num_props == 0) {
  427. tester_debug("Wrong number of expected properties given");
  428. tester_test_failed();
  429. return 1;
  430. }
  431. /* Get first exp prop to match and search for it */
  432. for (i = 0; i < exp_num_props; i++) {
  433. for (j = 0; j < rec_num_props; j++) {
  434. if (match_property(&exp_props[i], &rec_props[j], i)) {
  435. exp_prop_to_find--;
  436. break;
  437. }
  438. }
  439. }
  440. return exp_prop_to_find;
  441. }
  442. static int verify_mas_inst(btmce_mas_instance_t *exp_inst, int exp_num_inst,
  443. btmce_mas_instance_t *rec_inst,
  444. int rec_num_inst)
  445. {
  446. int i, j;
  447. int exp_inst_to_find = exp_num_inst;
  448. if (rec_num_inst == 0)
  449. return 1;
  450. if (exp_num_inst == 0) {
  451. tester_debug("Wrong number of expected MAS instances given");
  452. tester_test_failed();
  453. return 1;
  454. }
  455. for (i = 0; i < exp_num_inst; i++) {
  456. for (j = 0; j < rec_num_inst; j++) {
  457. if (match_mas_inst(&exp_inst[i], &rec_inst[i], i)) {
  458. exp_inst_to_find--;
  459. break;
  460. }
  461. }
  462. }
  463. return exp_inst_to_find;
  464. }
  465. /*
  466. * Check each test case step if test case expected
  467. * data is set and match it with expected result.
  468. */
  469. static bool verify_gatt_ids(btgatt_gatt_id_t *a, btgatt_gatt_id_t *b)
  470. {
  471. if (memcmp(&a->uuid, &b->uuid, sizeof(bt_uuid_t)))
  472. return false;
  473. if (a->inst_id != b->inst_id)
  474. return false;
  475. return true;
  476. }
  477. static bool verify_services(btgatt_srvc_id_t *a, btgatt_srvc_id_t *b)
  478. {
  479. if (a->is_primary != b->is_primary)
  480. return false;
  481. return verify_gatt_ids(&a->id, &b->id);
  482. }
  483. static bool match_data(struct step *step)
  484. {
  485. struct test_data *data = tester_get_data();
  486. const struct step *exp;
  487. exp = queue_peek_head(data->steps);
  488. if (!exp) {
  489. /* Can occure while test passed already */
  490. tester_debug("Cannot get step to match");
  491. return false;
  492. }
  493. if (exp->action_status != step->action_status) {
  494. tester_debug("Action status don't match");
  495. return false;
  496. }
  497. if (!exp->callback && !step->callback)
  498. return true;
  499. if (exp->callback != step->callback) {
  500. tester_debug("Callback type mismatch: %s vs %s",
  501. cb_table[step->callback].str,
  502. cb_table[exp->callback].str);
  503. return false;
  504. }
  505. if (exp->callback_result.state != step->callback_result.state) {
  506. tester_debug("Callback state mismatch: %d vs %d",
  507. step->callback_result.state,
  508. exp->callback_result.state);
  509. return false;
  510. }
  511. if (exp->callback_result.status != step->callback_result.status) {
  512. tester_debug("Callback status mismatch: %d vs %d",
  513. step->callback_result.status,
  514. exp->callback_result.status);
  515. return false;
  516. }
  517. if (exp->callback_result.mode != step->callback_result.mode) {
  518. tester_debug("Callback mode mismatch: %02x vs %02x",
  519. step->callback_result.mode,
  520. exp->callback_result.mode);
  521. return false;
  522. }
  523. if (exp->callback_result.report_size !=
  524. step->callback_result.report_size) {
  525. tester_debug("Callback report size mismatch: %d vs %d",
  526. step->callback_result.report_size,
  527. exp->callback_result.report_size);
  528. return false;
  529. }
  530. if (exp->callback_result.ctrl_state !=
  531. step->callback_result.ctrl_state) {
  532. tester_debug("Callback ctrl state mismatch: %d vs %d",
  533. step->callback_result.ctrl_state,
  534. exp->callback_result.ctrl_state);
  535. return false;
  536. }
  537. if (exp->callback_result.conn_state !=
  538. step->callback_result.conn_state) {
  539. tester_debug("Callback connection state mismatch: %d vs %d",
  540. step->callback_result.conn_state,
  541. exp->callback_result.conn_state);
  542. return false;
  543. }
  544. if (exp->callback_result.local_role !=
  545. step->callback_result.local_role) {
  546. tester_debug("Callback local_role mismatch: %d vs %d",
  547. step->callback_result.local_role,
  548. exp->callback_result.local_role);
  549. return false;
  550. }
  551. if (exp->callback_result.remote_role !=
  552. step->callback_result.remote_role) {
  553. tester_debug("Callback remote_role mismatch: %d vs %d",
  554. step->callback_result.remote_role,
  555. exp->callback_result.remote_role);
  556. return false;
  557. }
  558. if (exp->callback_result.app_id != step->callback_result.app_id) {
  559. tester_debug("Callback app_id mismatch: %d vs %d",
  560. step->callback_result.app_id,
  561. exp->callback_result.app_id);
  562. return false;
  563. }
  564. if (exp->callback_result.channel_id !=
  565. step->callback_result.channel_id) {
  566. tester_debug("Callback channel_id mismatch: %d vs %d",
  567. step->callback_result.channel_id,
  568. exp->callback_result.channel_id);
  569. return false;
  570. }
  571. if (exp->callback_result.mdep_cfg_index !=
  572. step->callback_result.mdep_cfg_index) {
  573. tester_debug("Callback mdep_cfg_index mismatch: %d vs %d",
  574. step->callback_result.mdep_cfg_index,
  575. exp->callback_result.mdep_cfg_index);
  576. return false;
  577. }
  578. if (exp->callback_result.app_state != step->callback_result.app_state) {
  579. tester_debug("Callback app_state mismatch: %d vs %d",
  580. step->callback_result.app_state,
  581. exp->callback_result.app_state);
  582. return false;
  583. }
  584. if (exp->callback_result.channel_state !=
  585. step->callback_result.channel_state) {
  586. tester_debug("Callback channel_state mismatch: %d vs %d",
  587. step->callback_result.channel_state,
  588. exp->callback_result.channel_state);
  589. return false;
  590. }
  591. if (exp->callback_result.av_conn_state !=
  592. step->callback_result.av_conn_state) {
  593. tester_debug("Callback av conn state mismatch: 0x%x vs 0x%x",
  594. step->callback_result.av_conn_state,
  595. exp->callback_result.av_conn_state);
  596. return false;
  597. }
  598. if (exp->callback_result.av_audio_state !=
  599. step->callback_result.av_audio_state) {
  600. tester_debug("Callback av audio state mismatch: 0x%x vs 0x%x",
  601. step->callback_result.av_audio_state,
  602. exp->callback_result.av_audio_state);
  603. return false;
  604. }
  605. if (exp->callback_result.song_length !=
  606. step->callback_result.song_length) {
  607. tester_debug("Callback song_length mismatch: 0x%x vs 0x%x",
  608. step->callback_result.song_length,
  609. exp->callback_result.song_length);
  610. return false;
  611. }
  612. if (exp->callback_result.song_position !=
  613. step->callback_result.song_position) {
  614. tester_debug("Callback song_position mismatch: 0x%x vs 0x%x",
  615. step->callback_result.song_position,
  616. exp->callback_result.song_position);
  617. return false;
  618. }
  619. if (exp->callback_result.play_status !=
  620. step->callback_result.play_status) {
  621. tester_debug("Callback play_status mismatch: 0x%x vs 0x%x",
  622. step->callback_result.play_status,
  623. exp->callback_result.play_status);
  624. return false;
  625. }
  626. if (exp->callback_result.rc_index !=
  627. step->callback_result.rc_index) {
  628. tester_debug("Callback rc_index mismatch");
  629. return false;
  630. }
  631. if (exp->callback_result.num_of_attrs !=
  632. step->callback_result.num_of_attrs) {
  633. tester_debug("Callback rc num of attrs mismatch");
  634. return false;
  635. }
  636. if (exp->callback_result.attrs) {
  637. if (memcmp(step->callback_result.attrs,
  638. exp->callback_result.attrs,
  639. exp->callback_result.num_of_attrs *
  640. sizeof(btrc_element_attr_val_t))) {
  641. tester_debug("Callback rc element attributes doesn't match");
  642. return false;
  643. }
  644. }
  645. if (exp->callback_result.pairing_variant !=
  646. step->callback_result.pairing_variant) {
  647. tester_debug("Callback pairing result mismatch: %d vs %d",
  648. step->callback_result.pairing_variant,
  649. exp->callback_result.pairing_variant);
  650. return false;
  651. }
  652. if (exp->callback_result.adv_data != step->callback_result.adv_data) {
  653. tester_debug("Callback adv. data status mismatch: %d vs %d",
  654. step->callback_result.adv_data,
  655. exp->callback_result.adv_data);
  656. return false;
  657. }
  658. if (exp->callback_result.conn_id != step->callback_result.conn_id) {
  659. tester_debug("Callback conn_id mismatch: %d vs %d",
  660. step->callback_result.conn_id,
  661. exp->callback_result.conn_id);
  662. return false;
  663. }
  664. if (exp->callback_result.gatt_app_id !=
  665. step->callback_result.gatt_app_id) {
  666. tester_debug("Callback gatt_app_id mismatch: %d vs %d",
  667. step->callback_result.gatt_app_id,
  668. exp->callback_result.gatt_app_id);
  669. return false;
  670. }
  671. if (exp->callback_result.properties &&
  672. verify_property(exp->callback_result.properties,
  673. exp->callback_result.num_properties,
  674. step->callback_result.properties,
  675. step->callback_result.num_properties)) {
  676. tester_debug("Gatt properties don't match");
  677. return false;
  678. }
  679. if (exp->callback_result.service &&
  680. !verify_services(step->callback_result.service,
  681. exp->callback_result.service)) {
  682. tester_debug("Gatt service doesn't match");
  683. return false;
  684. }
  685. if (exp->callback_result.characteristic) {
  686. btgatt_gatt_id_t *a;
  687. btgatt_gatt_id_t *b;
  688. a = step->callback_result.characteristic;
  689. b = exp->callback_result.characteristic;
  690. if (!verify_gatt_ids(a, b)) {
  691. tester_debug("Gatt char doesn't match");
  692. return false;
  693. }
  694. }
  695. if (exp->callback_result.char_prop != step->callback_result.char_prop) {
  696. tester_debug("Gatt char prop mismatch: %d vs %d",
  697. step->callback_result.char_prop,
  698. exp->callback_result.char_prop);
  699. return false;
  700. }
  701. if (exp->callback_result.descriptor) {
  702. btgatt_gatt_id_t *a;
  703. btgatt_gatt_id_t *b;
  704. a = step->callback_result.descriptor;
  705. b = exp->callback_result.descriptor;
  706. if (!verify_gatt_ids(a, b)) {
  707. tester_debug("Gatt desc doesn't match");
  708. return false;
  709. }
  710. }
  711. if (exp->callback_result.included) {
  712. if (!verify_services(step->callback_result.included,
  713. exp->callback_result.included)) {
  714. tester_debug("Gatt include srvc doesn't match");
  715. return false;
  716. }
  717. }
  718. if (exp->callback_result.read_params) {
  719. if (memcmp(step->callback_result.read_params,
  720. exp->callback_result.read_params,
  721. sizeof(btgatt_read_params_t))) {
  722. tester_debug("Gatt read_param doesn't match");
  723. return false;
  724. }
  725. }
  726. if (exp->callback_result.write_params) {
  727. if (memcmp(step->callback_result.write_params,
  728. exp->callback_result.write_params,
  729. sizeof(btgatt_write_params_t))) {
  730. tester_debug("Gatt write_param doesn't match");
  731. return false;
  732. }
  733. if (exp->callback_result.notification_registered !=
  734. step->callback_result.notification_registered) {
  735. tester_debug("Gatt registered flag mismatch");
  736. return false;
  737. }
  738. if (exp->callback_result.notify_params) {
  739. if (memcmp(step->callback_result.notify_params,
  740. exp->callback_result.notify_params,
  741. sizeof(btgatt_notify_params_t))) {
  742. tester_debug("Gatt notify_param doesn't match");
  743. return false;
  744. }
  745. }
  746. }
  747. if (exp->callback_result.connected !=
  748. step->callback_result.connected) {
  749. tester_debug("Gatt server conn status mismatch: %d vs %d",
  750. step->callback_result.connected,
  751. exp->callback_result.connected);
  752. return false;
  753. }
  754. if (exp->callback_result.attr_handle &&
  755. step->callback_result.attr_handle)
  756. if (*exp->callback_result.attr_handle !=
  757. *step->callback_result.attr_handle) {
  758. tester_debug("Gatt attribute handle mismatch: %d vs %d",
  759. *step->callback_result.attr_handle,
  760. *exp->callback_result.attr_handle);
  761. return false;
  762. }
  763. if (exp->callback_result.srvc_handle &&
  764. step->callback_result.srvc_handle)
  765. if (*exp->callback_result.srvc_handle !=
  766. *step->callback_result.srvc_handle) {
  767. tester_debug("Gatt service handle mismatch: %d vs %d",
  768. *step->callback_result.srvc_handle,
  769. *exp->callback_result.srvc_handle);
  770. return false;
  771. }
  772. if (exp->callback_result.inc_srvc_handle &&
  773. step->callback_result.inc_srvc_handle)
  774. if (*exp->callback_result.inc_srvc_handle !=
  775. *step->callback_result.inc_srvc_handle) {
  776. tester_debug("Gatt inc. srvc handle mismatch: %d vs %d",
  777. *step->callback_result.inc_srvc_handle,
  778. *exp->callback_result.inc_srvc_handle);
  779. return false;
  780. }
  781. if (exp->callback_result.uuid && step->callback_result.uuid)
  782. if (memcmp(exp->callback_result.uuid,
  783. step->callback_result.uuid,
  784. sizeof(*exp->callback_result.uuid))) {
  785. tester_debug("Uuid mismatch");
  786. return false;
  787. }
  788. if (exp->callback_result.trans_id != step->callback_result.trans_id) {
  789. tester_debug("Gatt trans id mismatch: %d vs %d",
  790. exp->callback_result.trans_id,
  791. step->callback_result.trans_id);
  792. return false;
  793. }
  794. if (exp->callback_result.offset != step->callback_result.offset) {
  795. tester_debug("Gatt offset mismatch: %d vs %d",
  796. exp->callback_result.offset,
  797. step->callback_result.offset);
  798. return false;
  799. }
  800. if (exp->callback_result.is_long != step->callback_result.is_long) {
  801. tester_debug("Gatt is long attr value flag mismatch: %d vs %d",
  802. exp->callback_result.is_long,
  803. step->callback_result.is_long);
  804. return false;
  805. }
  806. if (exp->callback_result.length > 0) {
  807. if (exp->callback_result.length !=
  808. step->callback_result.length) {
  809. tester_debug("Gatt attr length mismatch: %d vs %d",
  810. exp->callback_result.length,
  811. step->callback_result.length);
  812. return false;
  813. }
  814. if (!exp->callback_result.value ||
  815. !step->callback_result.value) {
  816. tester_debug("Gatt attr values are wrong set");
  817. return false;
  818. }
  819. if (!memcmp(exp->callback_result.value,
  820. step->callback_result.value,
  821. exp->callback_result.length)) {
  822. tester_debug("Gatt attr value mismatch");
  823. return false;
  824. }
  825. }
  826. if (exp->callback_result.need_rsp != step->callback_result.need_rsp) {
  827. tester_debug("Gatt need response value flag mismatch: %d vs %d",
  828. exp->callback_result.need_rsp,
  829. step->callback_result.need_rsp);
  830. return false;
  831. }
  832. if (exp->callback_result.is_prep != step->callback_result.is_prep) {
  833. tester_debug("Gatt is prepared value flag mismatch: %d vs %d",
  834. exp->callback_result.is_prep,
  835. step->callback_result.is_prep);
  836. return false;
  837. }
  838. if (exp->callback_result.num_mas_instances !=
  839. step->callback_result.num_mas_instances) {
  840. tester_debug("Mas instance count mismatch: %d vs %d",
  841. exp->callback_result.num_mas_instances,
  842. step->callback_result.num_mas_instances);
  843. return false;
  844. }
  845. if (exp->callback_result.mas_instances &&
  846. verify_mas_inst(exp->callback_result.mas_instances,
  847. exp->callback_result.num_mas_instances,
  848. step->callback_result.mas_instances,
  849. step->callback_result.num_mas_instances)) {
  850. tester_debug("Mas instances don't match");
  851. return false;
  852. }
  853. if (exp->callback_result.error != step->callback_result.error) {
  854. tester_debug("Err mismatch: %d vs %d",
  855. exp->callback_result.error,
  856. step->callback_result.error);
  857. return false;
  858. }
  859. if (exp->store_srvc_handle)
  860. memcpy(exp->store_srvc_handle,
  861. step->callback_result.srvc_handle,
  862. sizeof(*exp->store_srvc_handle));
  863. if (exp->store_char_handle)
  864. memcpy(exp->store_char_handle,
  865. step->callback_result.char_handle,
  866. sizeof(*exp->store_char_handle));
  867. return true;
  868. }
  869. static void init_test_steps(struct test_data *data)
  870. {
  871. const struct test_case *test_steps = data->test_data;
  872. int i = 0;
  873. for (i = 0; i < test_steps->step_num; i++)
  874. queue_push_tail(data->steps, (void *) &(test_steps->step[i]));
  875. tester_print("tester: Number of test steps=%d",
  876. queue_length(data->steps));
  877. }
  878. /*
  879. * Each test case step should be verified, if match with
  880. * expected result tester should go to next test step.
  881. */
  882. static void verify_step(struct step *step, queue_destroy_func_t cleanup_cb)
  883. {
  884. struct test_data *data = tester_get_data();
  885. const struct test_case *test_steps = data->test_data;
  886. struct step *next_step;
  887. tester_debug("tester: STEP[%d] check",
  888. test_steps->step_num-queue_length(data->steps) + 1);
  889. if (step && !match_data(step)) {
  890. if (cleanup_cb)
  891. cleanup_cb(step);
  892. return;
  893. }
  894. queue_pop_head(data->steps);
  895. if (cleanup_cb)
  896. cleanup_cb(step);
  897. tester_debug("tester: STEP[%d] pass",
  898. test_steps->step_num-queue_length(data->steps));
  899. if (queue_isempty(data->steps)) {
  900. tester_print("tester: All steps done, passing");
  901. tester_test_passed();
  902. return;
  903. }
  904. /* goto next step action if declared in step */
  905. next_step = queue_peek_head(data->steps);
  906. if (next_step->action)
  907. next_step->action();
  908. }
  909. /*
  910. * NOTICE:
  911. * Its mandatory for callback to set proper step.callback value so that
  912. * step verification could pass and move to next test step
  913. */
  914. static void free_properties(struct step *step)
  915. {
  916. bt_property_t *properties = step->callback_result.properties;
  917. int num_properties = step->callback_result.num_properties;
  918. int i;
  919. for (i = 0; i < num_properties; i++)
  920. g_free(properties[i].val);
  921. g_free(properties);
  922. }
  923. static void free_mas_instances(struct step *step)
  924. {
  925. btmce_mas_instance_t *mas_instances;
  926. int num_instances;
  927. int i;
  928. mas_instances = step->callback_result.mas_instances;
  929. num_instances = step->callback_result.num_mas_instances;
  930. for (i = 0; i < num_instances; i++)
  931. g_free(mas_instances[i].p_name);
  932. g_free(mas_instances);
  933. }
  934. static void destroy_callback_step(void *data)
  935. {
  936. struct step *step = data;
  937. if (step->callback_result.properties)
  938. free_properties(step);
  939. if (step->callback_result.service)
  940. free(step->callback_result.service);
  941. if (step->callback_result.characteristic)
  942. free(step->callback_result.characteristic);
  943. if (step->callback_result.descriptor)
  944. free(step->callback_result.descriptor);
  945. if (step->callback_result.included)
  946. free(step->callback_result.included);
  947. if (step->callback_result.read_params)
  948. free(step->callback_result.read_params);
  949. if (step->callback_result.write_params)
  950. free(step->callback_result.write_params);
  951. if (step->callback_result.notify_params)
  952. free(step->callback_result.notify_params);
  953. if (step->callback_result.srvc_handle)
  954. free(step->callback_result.srvc_handle);
  955. if (step->callback_result.inc_srvc_handle)
  956. free(step->callback_result.inc_srvc_handle);
  957. if (step->callback_result.uuid)
  958. free(step->callback_result.uuid);
  959. if (step->callback_result.char_handle)
  960. free(step->callback_result.char_handle);
  961. if (step->callback_result.desc_handle)
  962. free(step->callback_result.desc_handle);
  963. if (step->callback_result.attr_handle)
  964. free(step->callback_result.attr_handle);
  965. if (step->callback_result.value)
  966. free(step->callback_result.value);
  967. if (step->callback_result.mas_instances)
  968. free_mas_instances(step);
  969. g_free(step);
  970. g_atomic_int_dec_and_test(&scheduled_cbacks_num);
  971. }
  972. static gboolean verify_action(gpointer user_data)
  973. {
  974. struct step *step = user_data;
  975. verify_step(step, g_free);
  976. return FALSE;
  977. }
  978. static gboolean verify_callback(gpointer user_data)
  979. {
  980. struct test_data *data = tester_get_data();
  981. struct step *step = user_data;
  982. /* Return if callback came when all steps are already verified */
  983. if (queue_isempty(data->steps)) {
  984. destroy_callback_step(step);
  985. return FALSE;
  986. }
  987. /*
  988. * TODO: This may call action from next step before callback data
  989. * from previous step was freed.
  990. */
  991. verify_step(step, destroy_callback_step);
  992. return FALSE;
  993. }
  994. void schedule_callback_verification(struct step *step)
  995. {
  996. g_atomic_int_inc(&scheduled_cbacks_num);
  997. g_idle_add(verify_callback, step);
  998. }
  999. void schedule_action_verification(struct step *step)
  1000. {
  1001. g_idle_add_full(G_PRIORITY_HIGH_IDLE, verify_action, step, NULL);
  1002. }
  1003. static void adapter_state_changed_cb(bt_state_t state)
  1004. {
  1005. struct step *step = g_new0(struct step, 1);
  1006. step->callback_result.state = state;
  1007. step->callback = CB_BT_ADAPTER_STATE_CHANGED;
  1008. schedule_callback_verification(step);
  1009. }
  1010. static bt_property_t *copy_properties(int num_properties,
  1011. bt_property_t *properties)
  1012. {
  1013. int i;
  1014. bt_property_t *props = g_new0(bt_property_t, num_properties);
  1015. for (i = 0; i < num_properties; i++) {
  1016. props[i].type = properties[i].type;
  1017. props[i].len = properties[i].len;
  1018. props[i].val = g_memdup(properties[i].val, properties[i].len);
  1019. }
  1020. return props;
  1021. }
  1022. static bt_property_t *repack_properties(int num_properties,
  1023. bt_property_t **properties)
  1024. {
  1025. int i;
  1026. bt_property_t *props = g_new0(bt_property_t, num_properties);
  1027. for (i = 0; i < num_properties; i++) {
  1028. props[i].type = properties[i]->type;
  1029. props[i].len = properties[i]->len;
  1030. props[i].val = g_memdup(properties[i]->val, properties[i]->len);
  1031. }
  1032. return props;
  1033. }
  1034. static bt_property_t *create_property(bt_property_type_t type, void *val,
  1035. int len)
  1036. {
  1037. bt_property_t *prop = g_new0(bt_property_t, 1);
  1038. prop->type = type;
  1039. prop->len = len;
  1040. prop->val = g_memdup(val, len);
  1041. return prop;
  1042. }
  1043. static void adapter_properties_cb(bt_status_t status, int num_properties,
  1044. bt_property_t *properties)
  1045. {
  1046. struct step *step = g_new0(struct step, 1);
  1047. step->callback_result.status = status;
  1048. step->callback_result.num_properties = num_properties;
  1049. step->callback_result.properties = copy_properties(num_properties,
  1050. properties);
  1051. step->callback = CB_BT_ADAPTER_PROPERTIES;
  1052. schedule_callback_verification(step);
  1053. }
  1054. static void discovery_state_changed_cb(bt_discovery_state_t state)
  1055. {
  1056. struct step *step = g_new0(struct step, 1);
  1057. step->callback = CB_BT_DISCOVERY_STATE_CHANGED;
  1058. step->callback_result.state = state;
  1059. schedule_callback_verification(step);
  1060. }
  1061. static void device_found_cb(int num_properties, bt_property_t *properties)
  1062. {
  1063. struct step *step = g_new0(struct step, 1);
  1064. step->callback_result.num_properties = num_properties;
  1065. step->callback_result.properties = copy_properties(num_properties,
  1066. properties);
  1067. step->callback = CB_BT_DEVICE_FOUND;
  1068. schedule_callback_verification(step);
  1069. }
  1070. static void remote_device_properties_cb(bt_status_t status,
  1071. bt_bdaddr_t *bd_addr, int num_properties,
  1072. bt_property_t *properties)
  1073. {
  1074. struct step *step = g_new0(struct step, 1);
  1075. step->callback_result.num_properties = num_properties;
  1076. step->callback_result.properties = copy_properties(num_properties,
  1077. properties);
  1078. step->callback = CB_BT_REMOTE_DEVICE_PROPERTIES;
  1079. schedule_callback_verification(step);
  1080. }
  1081. static void bond_state_changed_cb(bt_status_t status,
  1082. bt_bdaddr_t *remote_bd_addr,
  1083. bt_bond_state_t state)
  1084. {
  1085. struct step *step = g_new0(struct step, 1);
  1086. step->callback_result.status = status;
  1087. step->callback_result.state = state;
  1088. /* Utilize property verification mechanism for bdaddr */
  1089. step->callback_result.num_properties = 1;
  1090. step->callback_result.properties =
  1091. create_property(BT_PROPERTY_BDADDR, remote_bd_addr,
  1092. sizeof(*remote_bd_addr));
  1093. step->callback = CB_BT_BOND_STATE_CHANGED;
  1094. schedule_callback_verification(step);
  1095. }
  1096. static void pin_request_cb(bt_bdaddr_t *remote_bd_addr,
  1097. bt_bdname_t *bd_name, uint32_t cod)
  1098. {
  1099. struct step *step = g_new0(struct step, 1);
  1100. bt_property_t *props[3];
  1101. step->callback = CB_BT_PIN_REQUEST;
  1102. /* Utilize property verification mechanism for those */
  1103. props[0] = create_property(BT_PROPERTY_BDADDR, remote_bd_addr,
  1104. sizeof(*remote_bd_addr));
  1105. props[1] = create_property(BT_PROPERTY_BDNAME, bd_name->name,
  1106. strlen((char *) bd_name->name));
  1107. props[2] = create_property(BT_PROPERTY_CLASS_OF_DEVICE, &cod,
  1108. sizeof(cod));
  1109. step->callback_result.num_properties = 3;
  1110. step->callback_result.properties = repack_properties(3, props);
  1111. g_free(props[0]->val);
  1112. g_free(props[0]);
  1113. g_free(props[1]->val);
  1114. g_free(props[1]);
  1115. g_free(props[2]->val);
  1116. g_free(props[2]);
  1117. schedule_callback_verification(step);
  1118. }
  1119. static void ssp_request_cb(bt_bdaddr_t *remote_bd_addr,
  1120. bt_bdname_t *bd_name, uint32_t cod,
  1121. bt_ssp_variant_t pairing_variant,
  1122. uint32_t pass_key)
  1123. {
  1124. struct step *step = g_new0(struct step, 1);
  1125. bt_property_t *props[3];
  1126. step->callback = CB_BT_SSP_REQUEST;
  1127. /* Utilize property verification mechanism for those */
  1128. props[0] = create_property(BT_PROPERTY_BDADDR, remote_bd_addr,
  1129. sizeof(*remote_bd_addr));
  1130. props[1] = create_property(BT_PROPERTY_BDNAME, bd_name->name,
  1131. strlen((char *) bd_name->name));
  1132. props[2] = create_property(BT_PROPERTY_CLASS_OF_DEVICE, &cod,
  1133. sizeof(cod));
  1134. step->callback_result.num_properties = 3;
  1135. step->callback_result.properties = repack_properties(3, props);
  1136. g_free(props[0]->val);
  1137. g_free(props[0]);
  1138. g_free(props[1]->val);
  1139. g_free(props[1]);
  1140. g_free(props[2]->val);
  1141. g_free(props[2]);
  1142. schedule_callback_verification(step);
  1143. }
  1144. static void acl_state_changed_cb(bt_status_t status,
  1145. bt_bdaddr_t *remote_bd_addr,
  1146. bt_acl_state_t state) {
  1147. struct step *step = g_new0(struct step, 1);
  1148. step->callback = CB_BT_ACL_STATE_CHANGED;
  1149. step->callback_result.status = status;
  1150. step->callback_result.state = state;
  1151. schedule_callback_verification(step);
  1152. }
  1153. static bt_callbacks_t bt_callbacks = {
  1154. .size = sizeof(bt_callbacks),
  1155. .adapter_state_changed_cb = adapter_state_changed_cb,
  1156. .adapter_properties_cb = adapter_properties_cb,
  1157. .remote_device_properties_cb = remote_device_properties_cb,
  1158. .device_found_cb = device_found_cb,
  1159. .discovery_state_changed_cb = discovery_state_changed_cb,
  1160. .pin_request_cb = pin_request_cb,
  1161. .ssp_request_cb = ssp_request_cb,
  1162. .bond_state_changed_cb = bond_state_changed_cb,
  1163. .acl_state_changed_cb = acl_state_changed_cb,
  1164. .thread_evt_cb = NULL,
  1165. .dut_mode_recv_cb = NULL,
  1166. .le_test_mode_cb = NULL,
  1167. .energy_info_cb = NULL,
  1168. };
  1169. static void hidhost_connection_state_cb(bt_bdaddr_t *bd_addr,
  1170. bthh_connection_state_t state)
  1171. {
  1172. struct step *step = g_new0(struct step, 1);
  1173. step->callback = CB_HH_CONNECTION_STATE;
  1174. step->callback_result.state = state;
  1175. schedule_callback_verification(step);
  1176. }
  1177. static void hidhost_virtual_unplug_cb(bt_bdaddr_t *bd_addr, bthh_status_t status)
  1178. {
  1179. struct step *step = g_new0(struct step, 1);
  1180. step->callback = CB_HH_VIRTUAL_UNPLUG;
  1181. step->callback_result.status = status;
  1182. schedule_callback_verification(step);
  1183. }
  1184. static void hidhost_protocol_mode_cb(bt_bdaddr_t *bd_addr,
  1185. bthh_status_t status,
  1186. bthh_protocol_mode_t mode)
  1187. {
  1188. struct step *step = g_new0(struct step, 1);
  1189. step->callback = CB_HH_PROTOCOL_MODE;
  1190. step->callback_result.status = status;
  1191. step->callback_result.mode = mode;
  1192. /* TODO: add bdaddr to verify? */
  1193. schedule_callback_verification(step);
  1194. }
  1195. static void hidhost_hid_info_cb(bt_bdaddr_t *bd_addr, bthh_hid_info_t hid)
  1196. {
  1197. struct step *step = g_new0(struct step, 1);
  1198. step->callback = CB_HH_HID_INFO;
  1199. schedule_callback_verification(step);
  1200. }
  1201. static void hidhost_get_report_cb(bt_bdaddr_t *bd_addr, bthh_status_t status,
  1202. uint8_t *report, int size)
  1203. {
  1204. struct step *step = g_new0(struct step, 1);
  1205. step->callback = CB_HH_GET_REPORT;
  1206. step->callback_result.status = status;
  1207. step->callback_result.report_size = size;
  1208. schedule_callback_verification(step);
  1209. }
  1210. static bthh_callbacks_t bthh_callbacks = {
  1211. .size = sizeof(bthh_callbacks),
  1212. .connection_state_cb = hidhost_connection_state_cb,
  1213. .hid_info_cb = hidhost_hid_info_cb,
  1214. .protocol_mode_cb = hidhost_protocol_mode_cb,
  1215. .idle_time_cb = NULL,
  1216. .get_report_cb = hidhost_get_report_cb,
  1217. .virtual_unplug_cb = hidhost_virtual_unplug_cb,
  1218. .handshake_cb = NULL,
  1219. };
  1220. static void gattc_register_client_cb(int status, int client_if,
  1221. bt_uuid_t *app_uuid)
  1222. {
  1223. struct step *step = g_new0(struct step, 1);
  1224. step->callback = CB_GATTC_REGISTER_CLIENT;
  1225. step->callback_result.status = status;
  1226. schedule_callback_verification(step);
  1227. }
  1228. static void gattc_scan_result_cb(bt_bdaddr_t *bda, int rssi, uint8_t *adv_data)
  1229. {
  1230. struct step *step = g_new0(struct step, 1);
  1231. bt_property_t *props[2];
  1232. step->callback = CB_GATTC_SCAN_RESULT;
  1233. step->callback_result.adv_data = adv_data ? TRUE : FALSE;
  1234. /* Utilize property verification mechanism for those */
  1235. props[0] = create_property(BT_PROPERTY_BDADDR, bda, sizeof(*bda));
  1236. props[1] = create_property(BT_PROPERTY_REMOTE_RSSI, &rssi,
  1237. sizeof(rssi));
  1238. step->callback_result.num_properties = 2;
  1239. step->callback_result.properties = repack_properties(2, props);
  1240. g_free(props[0]->val);
  1241. g_free(props[0]);
  1242. g_free(props[1]->val);
  1243. g_free(props[1]);
  1244. schedule_callback_verification(step);
  1245. }
  1246. static void gattc_connect_cb(int conn_id, int status, int client_if,
  1247. bt_bdaddr_t *bda)
  1248. {
  1249. struct step *step = g_new0(struct step, 1);
  1250. bt_property_t *props[1];
  1251. step->callback = CB_GATTC_OPEN;
  1252. step->callback_result.status = status;
  1253. step->callback_result.conn_id = conn_id;
  1254. step->callback_result.gatt_app_id = client_if;
  1255. /* Utilize property verification mechanism for bdaddr */
  1256. props[0] = create_property(BT_PROPERTY_BDADDR, bda, sizeof(*bda));
  1257. step->callback_result.num_properties = 1;
  1258. step->callback_result.properties = repack_properties(1, props);
  1259. g_free(props[0]->val);
  1260. g_free(props[0]);
  1261. schedule_callback_verification(step);
  1262. }
  1263. static void gattc_disconnect_cb(int conn_id, int status, int client_if,
  1264. bt_bdaddr_t *bda)
  1265. {
  1266. struct step *step = g_new0(struct step, 1);
  1267. bt_property_t *props[1];
  1268. step->callback = CB_GATTC_CLOSE;
  1269. step->callback_result.status = status;
  1270. step->callback_result.conn_id = conn_id;
  1271. step->callback_result.gatt_app_id = client_if;
  1272. /* Utilize property verification mechanism for bdaddr */
  1273. props[0] = create_property(BT_PROPERTY_BDADDR, bda, sizeof(*bda));
  1274. step->callback_result.num_properties = 1;
  1275. step->callback_result.properties = repack_properties(1, props);
  1276. g_free(props[0]->val);
  1277. g_free(props[0]);
  1278. schedule_callback_verification(step);
  1279. }
  1280. static void gattc_listen_cb(int status, int server_if)
  1281. {
  1282. struct step *step = g_new0(struct step, 1);
  1283. step->callback = CB_GATTC_LISTEN;
  1284. step->callback_result.status = status;
  1285. schedule_callback_verification(step);
  1286. }
  1287. static void gattc_search_result_cb(int conn_id, btgatt_srvc_id_t *srvc_id)
  1288. {
  1289. struct step *step = g_new0(struct step, 1);
  1290. step->callback = CB_GATTC_SEARCH_RESULT;
  1291. step->callback_result.conn_id = conn_id;
  1292. step->callback_result.service = g_memdup(srvc_id, sizeof(*srvc_id));
  1293. schedule_callback_verification(step);
  1294. }
  1295. static void gattc_search_complete_cb(int conn_id, int status)
  1296. {
  1297. struct step *step = g_new0(struct step, 1);
  1298. step->callback = CB_GATTC_SEARCH_COMPLETE;
  1299. step->callback_result.conn_id = conn_id;
  1300. schedule_callback_verification(step);
  1301. }
  1302. static void gattc_get_characteristic_cb(int conn_id, int status,
  1303. btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
  1304. int char_prop)
  1305. {
  1306. struct step *step = g_new0(struct step, 1);
  1307. step->callback = CB_GATTC_GET_CHARACTERISTIC;
  1308. step->callback_result.status = status;
  1309. step->callback_result.conn_id = conn_id;
  1310. step->callback_result.service = g_memdup(srvc_id, sizeof(*srvc_id));
  1311. step->callback_result.characteristic = g_memdup(char_id,
  1312. sizeof(*char_id));
  1313. step->callback_result.char_prop = char_prop;
  1314. schedule_callback_verification(step);
  1315. }
  1316. static void gattc_get_descriptor_cb(int conn_id, int status,
  1317. btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
  1318. btgatt_gatt_id_t *descr_id)
  1319. {
  1320. struct step *step = g_new0(struct step, 1);
  1321. step->callback = CB_GATTC_GET_DESCRIPTOR;
  1322. step->callback_result.status = status;
  1323. step->callback_result.conn_id = conn_id;
  1324. step->callback_result.service = g_memdup(srvc_id, sizeof(*srvc_id));
  1325. step->callback_result.characteristic = g_memdup(char_id,
  1326. sizeof(*char_id));
  1327. step->callback_result.descriptor = g_memdup(descr_id,
  1328. sizeof(*descr_id));
  1329. schedule_callback_verification(step);
  1330. }
  1331. static void gattc_get_included_service_cb(int conn_id, int status,
  1332. btgatt_srvc_id_t *srvc_id, btgatt_srvc_id_t *incl_srvc_id)
  1333. {
  1334. struct step *step = g_new0(struct step, 1);
  1335. step->callback = CB_GATTC_GET_INCLUDED_SERVICE;
  1336. step->callback_result.status = status;
  1337. step->callback_result.conn_id = conn_id;
  1338. step->callback_result.service = g_memdup(srvc_id, sizeof(*srvc_id));
  1339. step->callback_result.included = g_memdup(incl_srvc_id,
  1340. sizeof(*srvc_id));
  1341. schedule_callback_verification(step);
  1342. }
  1343. static void gattc_read_characteristic_cb(int conn_id, int status,
  1344. btgatt_read_params_t *p_data)
  1345. {
  1346. struct step *step = g_new0(struct step, 1);
  1347. step->callback = CB_GATTC_READ_CHARACTERISTIC;
  1348. step->callback_result.status = status;
  1349. step->callback_result.conn_id = conn_id;
  1350. step->callback_result.read_params = g_memdup(p_data, sizeof(*p_data));
  1351. schedule_callback_verification(step);
  1352. }
  1353. static void gattc_read_descriptor_cb(int conn_id, int status,
  1354. btgatt_read_params_t *p_data)
  1355. {
  1356. struct step *step = g_new0(struct step, 1);
  1357. step->callback = CB_GATTC_READ_DESCRIPTOR;
  1358. step->callback_result.status = status;
  1359. step->callback_result.conn_id = conn_id;
  1360. step->callback_result.read_params = g_memdup(p_data, sizeof(*p_data));
  1361. schedule_callback_verification(step);
  1362. }
  1363. static void gattc_write_characteristic_cb(int conn_id, int status,
  1364. btgatt_write_params_t *p_data)
  1365. {
  1366. struct step *step = g_new0(struct step, 1);
  1367. step->callback = CB_GATTC_WRITE_CHARACTERISTIC;
  1368. step->callback_result.status = status;
  1369. step->callback_result.conn_id = conn_id;
  1370. step->callback_result.write_params = g_memdup(p_data, sizeof(*p_data));
  1371. schedule_callback_verification(step);
  1372. }
  1373. static void gattc_write_descriptor_cb(int conn_id, int status,
  1374. btgatt_write_params_t *p_data)
  1375. {
  1376. struct step *step = g_new0(struct step, 1);
  1377. step->callback = CB_GATTC_WRITE_DESCRIPTOR;
  1378. step->callback_result.status = status;
  1379. step->callback_result.conn_id = conn_id;
  1380. step->callback_result.write_params = g_memdup(p_data, sizeof(*p_data));
  1381. schedule_callback_verification(step);
  1382. }
  1383. static void gattc_register_for_notification_cb(int conn_id, int registered,
  1384. int status,
  1385. btgatt_srvc_id_t *srvc_id,
  1386. btgatt_gatt_id_t *char_id)
  1387. {
  1388. struct step *step = g_new0(struct step, 1);
  1389. step->callback = CB_GATTC_REGISTER_FOR_NOTIFICATION;
  1390. step->callback_result.status = status;
  1391. step->callback_result.conn_id = conn_id;
  1392. step->callback_result.service = g_memdup(srvc_id, sizeof(*srvc_id));
  1393. step->callback_result.characteristic = g_memdup(char_id,
  1394. sizeof(*char_id));
  1395. step->callback_result.notification_registered = registered;
  1396. schedule_callback_verification(step);
  1397. }
  1398. static void gattc_notif_cb(int conn_id, btgatt_notify_params_t *p_data)
  1399. {
  1400. struct step *step = g_new0(struct step, 1);
  1401. step->callback = CB_GATTC_NOTIFY;
  1402. step->callback_result.conn_id = conn_id;
  1403. step->callback_result.notify_params = g_memdup(p_data, sizeof(*p_data));
  1404. schedule_callback_verification(step);
  1405. }
  1406. static const btgatt_client_callbacks_t btgatt_client_callbacks = {
  1407. .register_client_cb = gattc_register_client_cb,
  1408. .scan_result_cb = gattc_scan_result_cb,
  1409. .open_cb = gattc_connect_cb,
  1410. .close_cb = gattc_disconnect_cb,
  1411. .search_complete_cb = gattc_search_complete_cb,
  1412. .search_result_cb = gattc_search_result_cb,
  1413. .get_characteristic_cb = gattc_get_characteristic_cb,
  1414. .get_descriptor_cb = gattc_get_descriptor_cb,
  1415. .get_included_service_cb = gattc_get_included_service_cb,
  1416. .register_for_notification_cb = gattc_register_for_notification_cb,
  1417. .notify_cb = gattc_notif_cb,
  1418. .read_characteristic_cb = gattc_read_characteristic_cb,
  1419. .write_characteristic_cb = gattc_write_characteristic_cb,
  1420. .read_descriptor_cb = gattc_read_descriptor_cb,
  1421. .write_descriptor_cb = gattc_write_descriptor_cb,
  1422. .execute_write_cb = NULL,
  1423. .read_remote_rssi_cb = NULL,
  1424. .listen_cb = gattc_listen_cb
  1425. };
  1426. static void gatts_register_server_cb(int status, int server_if,
  1427. bt_uuid_t *app_uuid)
  1428. {
  1429. struct step *step = g_new0(struct step, 1);
  1430. step->callback = CB_GATTS_REGISTER_SERVER;
  1431. step->callback_result.status = status;
  1432. schedule_callback_verification(step);
  1433. }
  1434. static void gatts_connection_cb(int conn_id, int server_if, int connected,
  1435. bt_bdaddr_t *bda)
  1436. {
  1437. struct step *step = g_new0(struct step, 1);
  1438. bt_property_t *props[1];
  1439. step->callback = CB_GATTS_CONNECTION;
  1440. step->callback_result.conn_id = conn_id;
  1441. step->callback_result.gatt_app_id = server_if;
  1442. step->callback_result.connected = connected;
  1443. /* Utilize property verification mechanism for bdaddr */
  1444. props[0] = create_property(BT_PROPERTY_BDADDR, bda, sizeof(*bda));
  1445. step->callback_result.num_properties = 1;
  1446. step->callback_result.properties = repack_properties(1, props);
  1447. g_free(props[0]->val);
  1448. g_free(props[0]);
  1449. schedule_callback_verification(step);
  1450. }
  1451. static void gatts_service_added_cb(int status, int server_if,
  1452. btgatt_srvc_id_t *srvc_id,
  1453. int srvc_handle)
  1454. {
  1455. struct step *step = g_new0(struct step, 1);
  1456. step->callback = CB_GATTS_SERVICE_ADDED;
  1457. step->callback_result.status = status;
  1458. step->callback_result.gatt_app_id = server_if;
  1459. step->callback_result.service = g_memdup(srvc_id, sizeof(*srvc_id));
  1460. step->callback_result.srvc_handle = g_memdup(&srvc_handle,
  1461. sizeof(srvc_handle));
  1462. schedule_callback_verification(step);
  1463. }
  1464. static void gatts_included_service_added_cb(int status, int server_if,
  1465. int srvc_handle,
  1466. int inc_srvc_handle)
  1467. {
  1468. struct step *step = g_new0(struct step, 1);
  1469. step->callback = CB_GATTS_INCLUDED_SERVICE_ADDED;
  1470. step->callback_result.status = status;
  1471. step->callback_result.gatt_app_id = server_if;
  1472. step->callback_result.srvc_handle = g_memdup(&srvc_handle,
  1473. sizeof(srvc_handle));
  1474. step->callback_result.inc_srvc_handle = g_memdup(&inc_srvc_handle,
  1475. sizeof(inc_srvc_handle));
  1476. schedule_callback_verification(step);
  1477. }
  1478. static void gatts_characteristic_added_cb(int status, int server_if,
  1479. bt_uuid_t *uuid,
  1480. int srvc_handle,
  1481. int char_handle)
  1482. {
  1483. struct step *step = g_new0(struct step, 1);
  1484. step->callback = CB_GATTS_CHARACTERISTIC_ADDED;
  1485. step->callback_result.status = status;
  1486. step->callback_result.gatt_app_id = server_if;
  1487. step->callback_result.srvc_handle = g_memdup(&srvc_handle,
  1488. sizeof(srvc_handle));
  1489. step->callback_result.uuid = g_memdup(uuid, sizeof(*uuid));
  1490. step->callback_result.char_handle = g_memdup(&char_handle,
  1491. sizeof(char_handle));
  1492. schedule_callback_verification(step);
  1493. }
  1494. static void gatts_descriptor_added_cb(int status, int server_if,
  1495. bt_uuid_t *uuid,
  1496. int srvc_handle,
  1497. int desc_handle)
  1498. {
  1499. struct step *step = g_new0(struct step, 1);
  1500. step->callback = CB_GATTS_DESCRIPTOR_ADDED;
  1501. step->callback_result.status = status;
  1502. step->callback_result.gatt_app_id = server_if;
  1503. step->callback_result.srvc_handle = g_memdup(&srvc_handle,
  1504. sizeof(srvc_handle));
  1505. step->callback_result.uuid = g_memdup(uuid, sizeof(*uuid));
  1506. step->callback_result.desc_handle = g_memdup(&desc_handle,
  1507. sizeof(desc_handle));
  1508. schedule_callback_verification(step);
  1509. }
  1510. static void gatts_service_started_cb(int status, int server_if, int srvc_handle)
  1511. {
  1512. struct step *step = g_new0(struct step, 1);
  1513. step->callback = CB_GATTS_SERVICE_STARTED;
  1514. step->callback_result.status = status;
  1515. step->callback_result.gatt_app_id = server_if;
  1516. step->callback_result.srvc_handle = g_memdup(&srvc_handle,
  1517. sizeof(srvc_handle));
  1518. schedule_callback_verification(step);
  1519. }
  1520. static void gatts_service_stopped_cb(int status, int server_if, int srvc_handle)
  1521. {
  1522. struct step *step = g_new0(struct step, 1);
  1523. step->callback = CB_GATTS_SERVICE_STOPPED;
  1524. step->callback_result.status = status;
  1525. step->callback_result.gatt_app_id = server_if;
  1526. step->callback_result.srvc_handle = g_memdup(&srvc_handle,
  1527. sizeof(srvc_handle));
  1528. schedule_callback_verification(step);
  1529. }
  1530. static void gatts_service_deleted_cb(int status, int server_if, int srvc_handle)
  1531. {
  1532. struct step *step = g_new0(struct step, 1);
  1533. step->callback = CB_GATTS_SERVICE_DELETED;
  1534. step->callback_result.status = status;
  1535. step->callback_result.gatt_app_id = server_if;
  1536. step->callback_result.srvc_handle = g_memdup(&srvc_handle,
  1537. sizeof(srvc_handle));
  1538. schedule_callback_verification(step);
  1539. }
  1540. static void gatts_request_read_cb(int conn_id, int trans_id, bt_bdaddr_t *bda,
  1541. int attr_handle, int offset,
  1542. bool is_long)
  1543. {
  1544. struct step *step = g_new0(struct step, 1);
  1545. bt_property_t *props[1];
  1546. step->callback = CB_GATTS_REQUEST_READ;
  1547. step->callback_result.conn_id = conn_id;
  1548. step->callback_result.trans_id = trans_id;
  1549. step->callback_result.attr_handle = g_memdup(&attr_handle,
  1550. sizeof(attr_handle));
  1551. step->callback_result.offset = offset;
  1552. step->callback_result.is_long = is_long;
  1553. /* Utilize property verification mechanism for bdaddr */
  1554. props[0] = create_property(BT_PROPERTY_BDADDR, bda, sizeof(*bda));
  1555. step->callback_result.num_properties = 1;
  1556. step->callback_result.properties = repack_properties(1, props);
  1557. g_free(props[0]->val);
  1558. g_free(props[0]);
  1559. schedule_callback_verification(step);
  1560. }
  1561. static void gatts_request_write_cb(int conn_id, int trans_id, bt_bdaddr_t *bda,
  1562. int attr_handle, int offset,
  1563. int length, bool need_rsp,
  1564. bool is_prep, uint8_t *value)
  1565. {
  1566. struct step *step = g_new0(struct step, 1);
  1567. bt_property_t *props[1];
  1568. step->callback = CB_GATTS_REQUEST_WRITE;
  1569. step->callback_result.conn_id = conn_id;
  1570. step->callback_result.trans_id = trans_id;
  1571. step->callback_result.attr_handle = g_memdup(&attr_handle,
  1572. sizeof(attr_handle));
  1573. step->callback_result.offset = offset;
  1574. step->callback_result.length = length;
  1575. step->callback_result.need_rsp = need_rsp;
  1576. step->callback_result.is_prep = is_prep;
  1577. step->callback_result.value = g_memdup(&value, length);
  1578. /* Utilize property verification mechanism for bdaddr */
  1579. props[0] = create_property(BT_PROPERTY_BDADDR, bda, sizeof(*bda));
  1580. step->callback_result.num_properties = 1;
  1581. step->callback_result.properties = repack_properties(1, props);
  1582. g_free(props[0]->val);
  1583. g_free(props[0]);
  1584. schedule_callback_verification(step);
  1585. }
  1586. static void gatts_indication_send_cb(int conn_id, int status)
  1587. {
  1588. struct step *step = g_new0(struct step, 1);
  1589. step->callback = CB_GATTS_INDICATION_SEND;
  1590. step->callback_result.conn_id = conn_id;
  1591. step->callback_result.status = status;
  1592. schedule_callback_verification(step);
  1593. }
  1594. static const btgatt_server_callbacks_t btgatt_server_callbacks = {
  1595. .register_server_cb = gatts_register_server_cb,
  1596. .connection_cb = gatts_connection_cb,
  1597. .service_added_cb = gatts_service_added_cb,
  1598. .included_service_added_cb = gatts_included_service_added_cb,
  1599. .characteristic_added_cb = gatts_characteristic_added_cb,
  1600. .descriptor_added_cb = gatts_descriptor_added_cb,
  1601. .service_started_cb = gatts_service_started_cb,
  1602. .service_stopped_cb = gatts_service_stopped_cb,
  1603. .service_deleted_cb = gatts_service_deleted_cb,
  1604. .request_read_cb = gatts_request_read_cb,
  1605. .request_write_cb = gatts_request_write_cb,
  1606. .request_exec_write_cb = NULL,
  1607. .response_confirmation_cb = NULL,
  1608. .indication_sent_cb = gatts_indication_send_cb,
  1609. .congestion_cb = NULL,
  1610. };
  1611. static const btgatt_callbacks_t btgatt_callbacks = {
  1612. .size = sizeof(btgatt_callbacks),
  1613. .client = &btgatt_client_callbacks,
  1614. .server = &btgatt_server_callbacks
  1615. };
  1616. static void pan_control_state_cb(btpan_control_state_t state, int local_role,
  1617. bt_status_t error, const char *ifname)
  1618. {
  1619. struct step *step = g_new0(struct step, 1);
  1620. step->callback = CB_PAN_CONTROL_STATE;
  1621. step->callback_result.state = error;
  1622. step->callback_result.ctrl_state = state;
  1623. step->callback_result.local_role = local_role;
  1624. schedule_callback_verification(step);
  1625. }
  1626. static void pan_connection_state_cb(btpan_connection_state_t state,
  1627. bt_status_t error,
  1628. const bt_bdaddr_t *bd_addr,
  1629. int local_role, int remote_role)
  1630. {
  1631. struct step *step = g_new0(struct step, 1);
  1632. step->callback = CB_PAN_CONNECTION_STATE;
  1633. step->callback_result.state = error;
  1634. step->callback_result.conn_state = state;
  1635. step->callback_result.local_role = local_role;
  1636. step->callback_result.remote_role = remote_role;
  1637. schedule_callback_verification(step);
  1638. }
  1639. static btpan_callbacks_t btpan_callbacks = {
  1640. .size = sizeof(btpan_callbacks),
  1641. .control_state_cb = pan_control_state_cb,
  1642. .connection_state_cb = pan_connection_state_cb,
  1643. };
  1644. static void hdp_app_reg_state_cb(int app_id, bthl_app_reg_state_t state)
  1645. {
  1646. struct step *step = g_new0(struct step, 1);
  1647. step->callback = CB_HDP_APP_REG_STATE;
  1648. step->callback_result.app_id = app_id;
  1649. step->callback_result.app_state = state;
  1650. schedule_callback_verification(step);
  1651. }
  1652. static void hdp_channel_state_cb(int app_id, bt_bdaddr_t *bd_addr,
  1653. int mdep_cfg_index, int channel_id,
  1654. bthl_channel_state_t state, int fd)
  1655. {
  1656. struct step *step = g_new0(struct step, 1);
  1657. step->callback = CB_HDP_CHANNEL_STATE;
  1658. step->callback_result.app_id = app_id;
  1659. step->callback_result.channel_id = channel_id;
  1660. step->callback_result.mdep_cfg_index = mdep_cfg_index;
  1661. step->callback_result.channel_state = state;
  1662. schedule_callback_verification(step);
  1663. }
  1664. static bthl_callbacks_t bthl_callbacks = {
  1665. .size = sizeof(bthl_callbacks),
  1666. .app_reg_state_cb = hdp_app_reg_state_cb,
  1667. .channel_state_cb = hdp_channel_state_cb,
  1668. };
  1669. static void a2dp_connection_state_cb(btav_connection_state_t state,
  1670. bt_bdaddr_t *bd_addr)
  1671. {
  1672. struct step *step = g_new0(struct step, 1);
  1673. step->callback = CB_A2DP_CONN_STATE;
  1674. step->callback_result.av_conn_state = state;
  1675. schedule_callback_verification(step);
  1676. }
  1677. static void a2dp_audio_state_cb(btav_audio_state_t state, bt_bdaddr_t *bd_addr)
  1678. {
  1679. struct step *step = g_new0(struct step, 1);
  1680. step->callback = CB_A2DP_AUDIO_STATE;
  1681. step->callback_result.av_audio_state = state;
  1682. schedule_callback_verification(step);
  1683. }
  1684. static btav_callbacks_t bta2dp_callbacks = {
  1685. .size = sizeof(bta2dp_callbacks),
  1686. .connection_state_cb = a2dp_connection_state_cb,
  1687. .audio_state_cb = a2dp_audio_state_cb,
  1688. };
  1689. static void avrcp_get_play_status_cb(void)
  1690. {
  1691. struct step *step = g_new0(struct step, 1);
  1692. step->callback = CB_AVRCP_PLAY_STATUS_REQ;
  1693. schedule_callback_verification(step);
  1694. }
  1695. static void avrcp_register_notification_cb(btrc_event_id_t event_id,
  1696. uint32_t param)
  1697. {
  1698. struct step *step = g_new0(struct step, 1);
  1699. step->callback = CB_AVRCP_REG_NOTIF_REQ;
  1700. schedule_callback_verification(step);
  1701. }
  1702. static void avrcp_get_element_attr_cb(uint8_t num_attr,
  1703. btrc_media_attr_t *p_attrs)
  1704. {
  1705. struct step *step = g_new0(struct step, 1);
  1706. step->callback = CB_AVRCP_GET_ATTR_REQ;
  1707. schedule_callback_verification(step);
  1708. }
  1709. static btrc_callbacks_t btavrcp_callbacks = {
  1710. .size = sizeof(btavrcp_callbacks),
  1711. .get_play_status_cb = avrcp_get_play_status_cb,
  1712. .register_notification_cb = avrcp_register_notification_cb,
  1713. .get_element_attr_cb = avrcp_get_element_attr_cb,
  1714. };
  1715. static btmce_mas_instance_t *copy_mas_instances(int num_instances,
  1716. btmce_mas_instance_t *instances)
  1717. {
  1718. int i;
  1719. btmce_mas_instance_t *inst;
  1720. inst = g_new0(btmce_mas_instance_t, num_instances);
  1721. for (i = 0; i < num_instances; i++) {
  1722. inst[i].id = instances[i].id;
  1723. inst[i].scn = instances[i].scn;
  1724. inst[i].msg_types = instances[i].msg_types;
  1725. inst[i].p_name = g_memdup(instances[i].p_name,
  1726. strlen(instances[i].p_name));
  1727. }
  1728. return inst;
  1729. }
  1730. static void map_client_get_remote_mas_instances_cb(bt_status_t status,
  1731. bt_bdaddr_t *bd_addr,
  1732. int num_instances,
  1733. btmce_mas_instance_t
  1734. *instances)
  1735. {
  1736. struct step *step = g_new0(struct step, 1);
  1737. step->callback_result.status = status;
  1738. step->callback_result.num_mas_instances = num_instances;
  1739. step->callback_result.mas_instances = copy_mas_instances(num_instances,
  1740. instances);
  1741. step->callback = CB_MAP_CLIENT_REMOTE_MAS_INSTANCES;
  1742. schedule_callback_verification(step);
  1743. }
  1744. static btmce_callbacks_t btmap_client_callbacks = {
  1745. .size = sizeof(btmap_client_callbacks),
  1746. .remote_mas_instances_cb = map_client_get_remote_mas_instances_cb,
  1747. };
  1748. static bool setup_base(struct test_data *data)
  1749. {
  1750. const hw_module_t *module;
  1751. hw_device_t *device;
  1752. int signal_fd[2];
  1753. char buf[1024];
  1754. pid_t pid;
  1755. int len;
  1756. int err;
  1757. if (pipe(signal_fd))
  1758. return false;
  1759. pid = fork();
  1760. if (pid < 0) {
  1761. close(signal_fd[0]);
  1762. close(signal_fd[1]);
  1763. return false;
  1764. }
  1765. if (pid == 0) {
  1766. if (!tester_use_debug())
  1767. fclose(stderr);
  1768. close(signal_fd[0]);
  1769. emulator(signal_fd[1], data->mgmt_index);
  1770. exit(0);
  1771. }
  1772. close(signal_fd[1]);
  1773. data->bluetoothd_pid = pid;
  1774. len = read(signal_fd[0], buf, sizeof(buf));
  1775. if (len <= 0 || strcmp(buf, EMULATOR_SIGNAL)) {
  1776. close(signal_fd[0]);
  1777. return false;
  1778. }
  1779. close(signal_fd[0]);
  1780. err = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID,
  1781. AUDIO_HARDWARE_MODULE_ID_A2DP, &module);
  1782. if (err)
  1783. return false;
  1784. err = audio_hw_device_open(module, &data->audio);
  1785. if (err)
  1786. return false;
  1787. err = hw_get_module(BT_HARDWARE_MODULE_ID, &module);
  1788. if (err)
  1789. return false;
  1790. err = module->methods->open(module, BT_HARDWARE_MODULE_ID, &device);
  1791. if (err)
  1792. return false;
  1793. data->device = device;
  1794. data->if_bluetooth = ((bluetooth_device_t *)
  1795. device)->get_bluetooth_interface();
  1796. if (!data->if_bluetooth)
  1797. return false;
  1798. if (!(data->steps = queue_new()))
  1799. return false;
  1800. data->pdus = queue_new();
  1801. if (!data->pdus) {
  1802. queue_destroy(data->steps, NULL);
  1803. return false;
  1804. }
  1805. return true;
  1806. }
  1807. static void setup(const void *test_data)
  1808. {
  1809. struct test_data *data = tester_get_data();
  1810. bt_status_t status;
  1811. if (!setup_base(data)) {
  1812. tester_setup_failed();
  1813. return;
  1814. }
  1815. status = data->if_bluetooth->init(&bt_callbacks);
  1816. if (status != BT_STATUS_SUCCESS) {
  1817. data->if_bluetooth = NULL;
  1818. tester_setup_failed();
  1819. return;
  1820. }
  1821. tester_setup_complete();
  1822. }
  1823. static void setup_socket(const void *test_data)
  1824. {
  1825. struct test_data *data = tester_get_data();
  1826. bt_status_t status;
  1827. const void *sock;
  1828. if (!setup_base(data)) {
  1829. tester_setup_failed();
  1830. return;
  1831. }
  1832. status = data->if_bluetooth->init(&bt_callbacks);
  1833. if (status != BT_STATUS_SUCCESS) {
  1834. data->if_bluetooth = NULL;
  1835. tester_setup_failed();
  1836. return;
  1837. }
  1838. sock = data->if_bluetooth->get_profile_interface(BT_PROFILE_SOCKETS_ID);
  1839. if (!sock) {
  1840. tester_setup_failed();
  1841. return;
  1842. }
  1843. data->if_sock = sock;
  1844. tester_setup_complete();
  1845. }
  1846. static void setup_hidhost(const void *test_data)
  1847. {
  1848. struct test_data *data = tester_get_data();
  1849. bt_status_t status;
  1850. const void *hid;
  1851. if (!setup_base(data)) {
  1852. tester_setup_failed();
  1853. return;
  1854. }
  1855. status = data->if_bluetooth->init(&bt_callbacks);
  1856. if (status != BT_STATUS_SUCCESS) {
  1857. data->if_bluetooth = NULL;
  1858. tester_setup_failed();
  1859. return;
  1860. }
  1861. hid = data->if_bluetooth->get_profile_interface(BT_PROFILE_HIDHOST_ID);
  1862. if (!hid) {
  1863. tester_setup_failed();
  1864. return;
  1865. }
  1866. data->if_hid = hid;
  1867. status = data->if_hid->init(&bthh_callbacks);
  1868. if (status != BT_STATUS_SUCCESS) {
  1869. data->if_hid = NULL;
  1870. tester_setup_failed();
  1871. return;
  1872. }
  1873. tester_setup_complete();
  1874. }
  1875. static void setup_pan(const void *test_data)
  1876. {
  1877. struct test_data *data = tester_get_data();
  1878. bt_status_t status;
  1879. const void *pan;
  1880. if (!setup_base(data)) {
  1881. tester_setup_failed();
  1882. return;
  1883. }
  1884. status = data->if_bluetooth->init(&bt_callbacks);
  1885. if (status != BT_STATUS_SUCCESS) {
  1886. data->if_bluetooth = NULL;
  1887. tester_setup_failed();
  1888. return;
  1889. }
  1890. pan = data->if_bluetooth->get_profile_interface(BT_PROFILE_PAN_ID);
  1891. if (!pan) {
  1892. tester_setup_failed();
  1893. return;
  1894. }
  1895. data->if_pan = pan;
  1896. status = data->if_pan->init(&btpan_callbacks);
  1897. if (status != BT_STATUS_SUCCESS) {
  1898. data->if_pan = NULL;
  1899. tester_setup_failed();
  1900. return;
  1901. }
  1902. tester_setup_complete();
  1903. }
  1904. static void setup_hdp(const void *test_data)
  1905. {
  1906. struct test_data *data = tester_get_data();
  1907. bt_status_t status;
  1908. const void *hdp;
  1909. if (!setup_base(data)) {
  1910. tester_setup_failed();
  1911. return;
  1912. }
  1913. status = data->if_bluetooth->init(&bt_callbacks);
  1914. if (status != BT_STATUS_SUCCESS) {
  1915. data->if_bluetooth = NULL;
  1916. tester_setup_failed();
  1917. return;
  1918. }
  1919. hdp = data->if_bluetooth->get_profile_interface(BT_PROFILE_HEALTH_ID);
  1920. if (!hdp) {
  1921. tester_setup_failed();
  1922. return;
  1923. }
  1924. data->if_hdp = hdp;
  1925. status = data->if_hdp->init(&bthl_callbacks);
  1926. if (status != BT_STATUS_SUCCESS) {
  1927. data->if_hdp = NULL;
  1928. tester_setup_failed();
  1929. return;
  1930. }
  1931. tester_setup_complete();
  1932. }
  1933. static void setup_a2dp(const void *test_data)
  1934. {
  1935. struct test_data *data = tester_get_data();
  1936. const bt_interface_t *if_bt;
  1937. bt_status_t status;
  1938. const void *a2dp;
  1939. if (!setup_base(data)) {
  1940. tester_setup_failed();
  1941. return;
  1942. }
  1943. if_bt = data->if_bluetooth;
  1944. status = if_bt->init(&bt_callbacks);
  1945. if (status != BT_STATUS_SUCCESS) {
  1946. data->if_bluetooth = NULL;
  1947. tester_setup_failed();
  1948. return;
  1949. }
  1950. a2dp = if_bt->get_profile_interface(BT_PROFILE_ADVANCED_AUDIO_ID);
  1951. if (!a2dp) {
  1952. tester_setup_failed();
  1953. return;
  1954. }
  1955. data->if_a2dp = a2dp;
  1956. status = data->if_a2dp->init(&bta2dp_callbacks);
  1957. if (status != BT_STATUS_SUCCESS) {
  1958. data->if_a2dp = NULL;
  1959. tester_setup_failed();
  1960. return;
  1961. }
  1962. tester_setup_complete();
  1963. }
  1964. static void setup_avrcp(const void *test_data)
  1965. {
  1966. struct test_data *data = tester_get_data();
  1967. const bt_interface_t *if_bt;
  1968. bt_status_t status;
  1969. const void *a2dp, *avrcp;
  1970. if (!setup_base(data)) {
  1971. tester_setup_failed();
  1972. return;
  1973. }
  1974. if_bt = data->if_bluetooth;
  1975. status = if_bt->init(&bt_callbacks);
  1976. if (status != BT_STATUS_SUCCESS) {
  1977. data->if_bluetooth = NULL;
  1978. tester_setup_failed();
  1979. return;
  1980. }
  1981. a2dp = if_bt->get_profile_interface(BT_PROFILE_ADVANCED_AUDIO_ID);
  1982. if (!a2dp) {
  1983. tester_setup_failed();
  1984. return;
  1985. }
  1986. data->if_a2dp = a2dp;
  1987. status = data->if_a2dp->init(&bta2dp_callbacks);
  1988. if (status != BT_STATUS_SUCCESS) {
  1989. data->if_a2dp = NULL;
  1990. tester_setup_failed();
  1991. return;
  1992. }
  1993. avrcp = if_bt->get_profile_interface(BT_PROFILE_AV_RC_ID);
  1994. if (!avrcp) {
  1995. tester_setup_failed();
  1996. return;
  1997. }
  1998. data->if_avrcp = avrcp;
  1999. status = data->if_avrcp->init(&btavrcp_callbacks);
  2000. if (status != BT_STATUS_SUCCESS) {
  2001. data->if_avrcp = NULL;
  2002. tester_setup_failed();
  2003. return;
  2004. }
  2005. tester_setup_complete();
  2006. }
  2007. static void setup_gatt(const void *test_data)
  2008. {
  2009. struct test_data *data = tester_get_data();
  2010. bt_status_t status;
  2011. const void *gatt;
  2012. if (!setup_base(data)) {
  2013. tester_setup_failed();
  2014. return;
  2015. }
  2016. status = data->if_bluetooth->init(&bt_callbacks);
  2017. if (status != BT_STATUS_SUCCESS) {
  2018. data->if_bluetooth = NULL;
  2019. tester_setup_failed();
  2020. return;
  2021. }
  2022. gatt = data->if_bluetooth->get_profile_interface(BT_PROFILE_GATT_ID);
  2023. if (!gatt) {
  2024. tester_setup_failed();
  2025. return;
  2026. }
  2027. data->if_gatt = gatt;
  2028. status = data->if_gatt->init(&btgatt_callbacks);
  2029. if (status != BT_STATUS_SUCCESS) {
  2030. data->if_gatt = NULL;
  2031. tester_setup_failed();
  2032. return;
  2033. }
  2034. tester_setup_complete();
  2035. }
  2036. static void setup_map_client(const void *test_data)
  2037. {
  2038. struct test_data *data = tester_get_data();
  2039. bt_status_t status;
  2040. const void *map_client;
  2041. if (!setup_base(data)) {
  2042. tester_setup_failed();
  2043. return;
  2044. }
  2045. status = data->if_bluetooth->init(&bt_callbacks);
  2046. if (status != BT_STATUS_SUCCESS) {
  2047. data->if_bluetooth = NULL;
  2048. tester_setup_failed();
  2049. return;
  2050. }
  2051. map_client = data->if_bluetooth->get_profile_interface(
  2052. BT_PROFILE_MAP_CLIENT_ID);
  2053. if (!map_client) {
  2054. tester_setup_failed();
  2055. return;
  2056. }
  2057. data->if_map_client = map_client;
  2058. status = data->if_map_client->init(&btmap_client_callbacks);
  2059. if (status != BT_STATUS_SUCCESS) {
  2060. data->if_map_client = NULL;
  2061. tester_setup_failed();
  2062. return;
  2063. }
  2064. tester_setup_complete();
  2065. }
  2066. static void teardown(const void *test_data)
  2067. {
  2068. struct test_data *data = tester_get_data();
  2069. queue_destroy(data->steps, NULL);
  2070. data->steps = NULL;
  2071. queue_destroy(data->pdus, NULL);
  2072. data->pdus = NULL;
  2073. /* no cleanup for map_client */
  2074. data->if_map_client = NULL;
  2075. if (data->if_gatt) {
  2076. data->if_gatt->cleanup();
  2077. data->if_gatt = NULL;
  2078. }
  2079. if (data->if_hid) {
  2080. data->if_hid->cleanup();
  2081. data->if_hid = NULL;
  2082. }
  2083. if (data->if_pan) {
  2084. data->if_pan->cleanup();
  2085. data->if_pan = NULL;
  2086. }
  2087. if (data->if_hdp) {
  2088. data->if_hdp->cleanup();
  2089. data->if_hdp = NULL;
  2090. }
  2091. if (data->if_stream) {
  2092. data->audio->close_output_stream(data->audio, data->if_stream);
  2093. data->if_stream = NULL;
  2094. }
  2095. if (data->if_a2dp) {
  2096. data->if_a2dp->cleanup();
  2097. data->if_a2dp = NULL;
  2098. }
  2099. if (data->if_avrcp) {
  2100. data->if_avrcp->cleanup();
  2101. data->if_avrcp = NULL;
  2102. }
  2103. if (data->if_bluetooth) {
  2104. data->if_bluetooth->cleanup();
  2105. data->if_bluetooth = NULL;
  2106. }
  2107. data->device->close(data->device);
  2108. audio_hw_device_close(data->audio);
  2109. /*
  2110. * Ssp_request_cb pointer can be set do default_ssp_req_cb.
  2111. * Set it back to ssp_request_cb
  2112. */
  2113. bt_callbacks.ssp_request_cb = ssp_request_cb;
  2114. if (!data->bluetoothd_pid)
  2115. tester_teardown_complete();
  2116. }
  2117. static void emu_connectable_complete(uint16_t opcode, uint8_t status,
  2118. const void *param, uint8_t len,
  2119. void *user_data)
  2120. {
  2121. struct step *step;
  2122. struct test_data *data = user_data;
  2123. switch (opcode) {
  2124. case BT_HCI_CMD_WRITE_SCAN_ENABLE:
  2125. break;
  2126. case BT_HCI_CMD_LE_SET_ADV_ENABLE:
  2127. /*
  2128. * For BREDRLE emulator we want to verify step after scan
  2129. * enable and not after le_set_adv_enable
  2130. */
  2131. if (data->hciemu_type == HCIEMU_TYPE_BREDRLE)
  2132. return;
  2133. break;
  2134. default:
  2135. return;
  2136. }
  2137. step = g_new0(struct step, 1);
  2138. if (status) {
  2139. tester_warn("Emulated remote setup failed.");
  2140. step->action_status = BT_STATUS_FAIL;
  2141. } else {
  2142. tester_debug("Emulated remote setup done.");
  2143. step->action_status = BT_STATUS_SUCCESS;
  2144. }
  2145. schedule_action_verification(step);
  2146. }
  2147. void emu_setup_powered_remote_action(void)
  2148. {
  2149. struct test_data *data = tester_get_data();
  2150. struct bthost *bthost;
  2151. bthost = hciemu_client_get_host(data->hciemu);
  2152. bthost_set_cmd_complete_cb(bthost, emu_connectable_complete, data);
  2153. if ((data->hciemu_type == HCIEMU_TYPE_LE) ||
  2154. (data->hciemu_type == HCIEMU_TYPE_BREDRLE)) {
  2155. uint8_t adv[3];
  2156. adv[0] = 0x02; /* Field length */
  2157. adv[1] = 0x01; /* Flags */
  2158. adv[2] = 0x02; /* Flags value */
  2159. bthost_set_adv_data(bthost, adv, sizeof(adv));
  2160. bthost_set_adv_enable(bthost, 0x01);
  2161. }
  2162. if (data->hciemu_type != HCIEMU_TYPE_LE)
  2163. bthost_write_scan_enable(bthost, 0x03);
  2164. }
  2165. void emu_set_pin_code_action(void)
  2166. {
  2167. struct test_data *data = tester_get_data();
  2168. struct step *current_data_step = queue_peek_head(data->steps);
  2169. struct bt_action_data *action_data = current_data_step->set_data;
  2170. struct bthost *bthost;
  2171. struct step *step = g_new0(struct step, 1);
  2172. bthost = hciemu_client_get_host(data->hciemu);
  2173. bthost_set_pin_code(bthost, action_data->pin->pin,
  2174. action_data->pin_len);
  2175. step->action_status = BT_STATUS_SUCCESS;
  2176. tester_print("Setting emu pin done.");
  2177. schedule_action_verification(step);
  2178. }
  2179. void emu_set_ssp_mode_action(void)
  2180. {
  2181. struct test_data *data = tester_get_data();
  2182. struct bthost *bthost;
  2183. struct step *step = g_new0(struct step, 1);
  2184. bthost = hciemu_client_get_host(data->hciemu);
  2185. bthost_write_ssp_mode(bthost, 0x01);
  2186. step->action_status = BT_STATUS_SUCCESS;
  2187. schedule_action_verification(step);
  2188. }
  2189. void emu_set_connect_cb_action(void)
  2190. {
  2191. struct test_data *data = tester_get_data();
  2192. struct bthost *bthost = hciemu_client_get_host(data->hciemu);
  2193. struct step *current_data_step = queue_peek_head(data->steps);
  2194. void *cb = current_data_step->set_data;
  2195. struct step *step = g_new0(struct step, 1);
  2196. bthost_set_connect_cb(bthost, cb, data);
  2197. step->action_status = BT_STATUS_SUCCESS;
  2198. schedule_action_verification(step);
  2199. }
  2200. void emu_remote_connect_hci_action(void)
  2201. {
  2202. struct test_data *data = tester_get_data();
  2203. struct bthost *bthost = hciemu_client_get_host(data->hciemu);
  2204. struct step *current_data_step = queue_peek_head(data->steps);
  2205. struct bt_action_data *action_data = current_data_step->set_data;
  2206. struct step *step = g_new0(struct step, 1);
  2207. const uint8_t *master_addr;
  2208. master_addr = hciemu_get_central_bdaddr(data->hciemu);
  2209. tester_print("Trying to connect hci");
  2210. if (action_data)
  2211. bthost_hci_connect(bthost, master_addr,
  2212. action_data->bearer_type);
  2213. else
  2214. bthost_hci_connect(bthost, master_addr, BDADDR_BREDR);
  2215. step->action_status = BT_STATUS_SUCCESS;
  2216. schedule_action_verification(step);
  2217. }
  2218. void emu_remote_disconnect_hci_action(void)
  2219. {
  2220. struct test_data *data = tester_get_data();
  2221. struct bthost *bthost = hciemu_client_get_host(data->hciemu);
  2222. struct step *current_data_step = queue_peek_head(data->steps);
  2223. uint16_t *handle = current_data_step->set_data;
  2224. struct step *step = g_new0(struct step, 1);
  2225. if (handle) {
  2226. bthost_hci_disconnect(bthost, *handle, 0x13);
  2227. step->action_status = BT_STATUS_SUCCESS;
  2228. } else {
  2229. step->action_status = BT_STATUS_FAIL;
  2230. }
  2231. schedule_action_verification(step);
  2232. }
  2233. void emu_set_io_cap(void)
  2234. {
  2235. struct test_data *data = tester_get_data();
  2236. struct bthost *bthost;
  2237. struct step *current_data_step = queue_peek_head(data->steps);
  2238. struct bt_action_data *action_data = current_data_step->set_data;
  2239. struct step *step = g_new0(struct step, 1);
  2240. bthost = hciemu_client_get_host(data->hciemu);
  2241. if (action_data)
  2242. bthost_set_io_capability(bthost, action_data->io_cap);
  2243. else
  2244. bthost_set_io_capability(bthost, 0x01);
  2245. step->action_status = BT_STATUS_SUCCESS;
  2246. schedule_action_verification(step);
  2247. }
  2248. void emu_add_l2cap_server_action(void)
  2249. {
  2250. struct test_data *data = tester_get_data();
  2251. struct step *current_data_step = queue_peek_head(data->steps);
  2252. struct emu_set_l2cap_data *l2cap_data = current_data_step->set_data;
  2253. struct bthost *bthost;
  2254. struct step *step = g_new0(struct step, 1);
  2255. if (!l2cap_data) {
  2256. tester_warn("Invalid l2cap_data params");
  2257. step->action_status = BT_STATUS_FAIL;
  2258. goto done;
  2259. }
  2260. bthost = hciemu_client_get_host(data->hciemu);
  2261. bthost_add_l2cap_server(bthost, l2cap_data->psm, l2cap_data->func,
  2262. NULL, l2cap_data->user_data);
  2263. step->action_status = BT_STATUS_SUCCESS;
  2264. done:
  2265. schedule_action_verification(step);
  2266. }
  2267. static void print_data(const char *str, void *user_data)
  2268. {
  2269. tester_debug("tester: %s", str);
  2270. }
  2271. static void emu_generic_cid_hook_cb(const void *data, uint16_t len,
  2272. void *user_data)
  2273. {
  2274. struct test_data *t_data = tester_get_data();
  2275. struct emu_l2cap_cid_data *cid_data = user_data;
  2276. const struct pdu_set *pdus = cid_data->pdu;
  2277. struct bthost *bthost = hciemu_client_get_host(t_data->hciemu);
  2278. int i;
  2279. for (i = 0; pdus[i].rsp.iov_base; i++) {
  2280. if (pdus[i].req.iov_base) {
  2281. if (pdus[i].req.iov_len != len)
  2282. continue;
  2283. if (memcmp(pdus[i].req.iov_base, data, len))
  2284. continue;
  2285. }
  2286. if (pdus[i].rsp.iov_base) {
  2287. util_hexdump('>', pdus[i].rsp.iov_base,
  2288. pdus[i].rsp.iov_len, print_data, NULL);
  2289. /* if its sdp pdu use transaction ID from request */
  2290. if (cid_data->is_sdp) {
  2291. struct iovec rsp[3];
  2292. rsp[0].iov_base = pdus[i].rsp.iov_base;
  2293. rsp[0].iov_len = 1;
  2294. rsp[1].iov_base = ((uint8_t *) data) + 1;
  2295. rsp[1].iov_len = 2;
  2296. rsp[2].iov_base = pdus[i].rsp.iov_base + 3;
  2297. rsp[2].iov_len = pdus[i].rsp.iov_len - 3;
  2298. bthost_send_cid_v(bthost, cid_data->handle,
  2299. cid_data->cid, rsp, 3);
  2300. } else {
  2301. bthost_send_cid_v(bthost, cid_data->handle,
  2302. cid_data->cid, &pdus[i].rsp, 1);
  2303. }
  2304. }
  2305. }
  2306. }
  2307. void tester_handle_l2cap_data_exchange(struct emu_l2cap_cid_data *cid_data)
  2308. {
  2309. struct test_data *t_data = tester_get_data();
  2310. struct bthost *bthost = hciemu_client_get_host(t_data->hciemu);
  2311. bthost_add_cid_hook(bthost, cid_data->handle, cid_data->cid,
  2312. emu_generic_cid_hook_cb, cid_data);
  2313. }
  2314. void tester_generic_connect_cb(uint16_t handle, uint16_t cid, void *user_data)
  2315. {
  2316. struct emu_l2cap_cid_data *cid_data = user_data;
  2317. cid_data->handle = handle;
  2318. cid_data->cid = cid;
  2319. tester_handle_l2cap_data_exchange(cid_data);
  2320. }
  2321. static void rfcomm_connect_cb(uint16_t handle, uint16_t cid, void *user_data,
  2322. bool status)
  2323. {
  2324. struct step *step = g_new0(struct step, 1);
  2325. tester_print("Connect handle %d, cid %d cb status: %d", handle, cid,
  2326. status);
  2327. step->action_status = BT_STATUS_SUCCESS;
  2328. schedule_action_verification(step);
  2329. }
  2330. void emu_add_rfcomm_server_action(void)
  2331. {
  2332. struct test_data *data = tester_get_data();
  2333. struct step *current_data_step = queue_peek_head(data->steps);
  2334. struct bt_action_data *rfcomm_data = current_data_step->set_data;
  2335. struct bthost *bthost;
  2336. struct step *step;
  2337. if (!rfcomm_data) {
  2338. tester_warn("Invalid l2cap_data params");
  2339. return;
  2340. }
  2341. step = g_new0(struct step, 1);
  2342. bthost = hciemu_client_get_host(data->hciemu);
  2343. bthost_add_rfcomm_server(bthost, rfcomm_data->channel,
  2344. rfcomm_connect_cb, data);
  2345. step->action_status = BT_STATUS_SUCCESS;
  2346. schedule_action_verification(step);
  2347. }
  2348. void dummy_action(void)
  2349. {
  2350. struct step *step = g_new0(struct step, 1);
  2351. step->action = dummy_action;
  2352. schedule_action_verification(step);
  2353. }
  2354. void bluetooth_enable_action(void)
  2355. {
  2356. struct test_data *data = tester_get_data();
  2357. struct step *step = g_new0(struct step, 1);
  2358. step->action_status = data->if_bluetooth->enable();
  2359. schedule_action_verification(step);
  2360. }
  2361. void bluetooth_disable_action(void)
  2362. {
  2363. struct test_data *data = tester_get_data();
  2364. struct step *step = g_new0(struct step, 1);
  2365. step->action_status = data->if_bluetooth->disable();
  2366. schedule_action_verification(step);
  2367. }
  2368. void bt_set_property_action(void)
  2369. {
  2370. struct test_data *data = tester_get_data();
  2371. struct step *step;
  2372. struct step *current_data_step = queue_peek_head(data->steps);
  2373. bt_property_t *prop;
  2374. if (!current_data_step->set_data) {
  2375. tester_debug("BT property not set for step");
  2376. tester_test_failed();
  2377. return;
  2378. }
  2379. step = g_new0(struct step, 1);
  2380. prop = (bt_property_t *)current_data_step->set_data;
  2381. step->action_status = data->if_bluetooth->set_adapter_property(prop);
  2382. schedule_action_verification(step);
  2383. }
  2384. void bt_get_property_action(void)
  2385. {
  2386. struct test_data *data = tester_get_data();
  2387. struct step *step;
  2388. struct step *current_data_step = queue_peek_head(data->steps);
  2389. bt_property_t *prop;
  2390. if (!current_data_step->set_data) {
  2391. tester_debug("BT property to get not defined");
  2392. tester_test_failed();
  2393. return;
  2394. }
  2395. step = g_new0(struct step, 1);
  2396. prop = (bt_property_t *)current_data_step->set_data;
  2397. step->action_status = data->if_bluetooth->get_adapter_property(
  2398. prop->type);
  2399. schedule_action_verification(step);
  2400. }
  2401. void bt_start_discovery_action(void)
  2402. {
  2403. struct test_data *data = tester_get_data();
  2404. struct step *step = g_new0(struct step, 1);
  2405. step->action_status = data->if_bluetooth->start_discovery();
  2406. schedule_action_verification(step);
  2407. }
  2408. void bt_cancel_discovery_action(void)
  2409. {
  2410. struct test_data *data = tester_get_data();
  2411. struct step *step = g_new0(struct step, 1);
  2412. step->action_status = data->if_bluetooth->cancel_discovery();
  2413. schedule_action_verification(step);
  2414. }
  2415. void bt_get_device_props_action(void)
  2416. {
  2417. struct test_data *data = tester_get_data();
  2418. struct step *current_data_step = queue_peek_head(data->steps);
  2419. struct step *step;
  2420. if (!current_data_step->set_data) {
  2421. tester_debug("bdaddr not defined");
  2422. tester_test_failed();
  2423. return;
  2424. }
  2425. step = g_new0(struct step, 1);
  2426. step->action_status =
  2427. data->if_bluetooth->get_remote_device_properties(
  2428. current_data_step->set_data);
  2429. schedule_action_verification(step);
  2430. }
  2431. void bt_get_device_prop_action(void)
  2432. {
  2433. struct test_data *data = tester_get_data();
  2434. struct step *current_data_step = queue_peek_head(data->steps);
  2435. struct bt_action_data *action_data = current_data_step->set_data;
  2436. struct step *step;
  2437. if (!action_data) {
  2438. tester_warn("No arguments for 'get remote device prop' req.");
  2439. tester_test_failed();
  2440. return;
  2441. }
  2442. step = g_new0(struct step, 1);
  2443. step->action_status = data->if_bluetooth->get_remote_device_property(
  2444. action_data->addr,
  2445. action_data->prop_type);
  2446. schedule_action_verification(step);
  2447. }
  2448. void bt_set_device_prop_action(void)
  2449. {
  2450. struct test_data *data = tester_get_data();
  2451. struct step *current_data_step = queue_peek_head(data->steps);
  2452. struct bt_action_data *action_data = current_data_step->set_data;
  2453. struct step *step;
  2454. if (!action_data) {
  2455. tester_warn("No arguments for 'set remote device prop' req.");
  2456. tester_test_failed();
  2457. return;
  2458. }
  2459. step = g_new0(struct step, 1);
  2460. step->action_status = data->if_bluetooth->set_remote_device_property(
  2461. action_data->addr,
  2462. action_data->prop);
  2463. schedule_action_verification(step);
  2464. }
  2465. void bt_create_bond_action(void)
  2466. {
  2467. struct test_data *data = tester_get_data();
  2468. struct step *current_data_step = queue_peek_head(data->steps);
  2469. struct bt_action_data *action_data = current_data_step->set_data;
  2470. struct step *step;
  2471. if (!action_data || !action_data->addr) {
  2472. tester_warn("Bad arguments for 'create bond' req.");
  2473. tester_test_failed();
  2474. return;
  2475. }
  2476. step = g_new0(struct step, 1);
  2477. step->action_status =
  2478. data->if_bluetooth->create_bond(action_data->addr,
  2479. action_data->transport_type ?
  2480. action_data->transport_type :
  2481. BT_TRANSPORT_UNKNOWN);
  2482. schedule_action_verification(step);
  2483. }
  2484. void bt_pin_reply_accept_action(void)
  2485. {
  2486. struct test_data *data = tester_get_data();
  2487. struct step *current_data_step = queue_peek_head(data->steps);
  2488. struct bt_action_data *action_data = current_data_step->set_data;
  2489. struct step *step;
  2490. if (!action_data || !action_data->addr || !action_data->pin) {
  2491. tester_warn("Bad arguments for 'pin reply' req.");
  2492. tester_test_failed();
  2493. return;
  2494. }
  2495. step = g_new0(struct step, 1);
  2496. step->action_status = data->if_bluetooth->pin_reply(action_data->addr,
  2497. TRUE,
  2498. action_data->pin_len,
  2499. action_data->pin);
  2500. schedule_action_verification(step);
  2501. }
  2502. void bt_ssp_reply_accept_action(void)
  2503. {
  2504. struct test_data *data = tester_get_data();
  2505. struct step *current_data_step = queue_peek_head(data->steps);
  2506. struct bt_action_data *action_data = current_data_step->set_data;
  2507. struct step *step = g_new0(struct step, 1);
  2508. step->action_status = data->if_bluetooth->ssp_reply(action_data->addr,
  2509. action_data->ssp_variant,
  2510. action_data->accept, 0);
  2511. schedule_action_verification(step);
  2512. }
  2513. void bt_cancel_bond_action(void)
  2514. {
  2515. struct test_data *data = tester_get_data();
  2516. struct step *current_data_step = queue_peek_head(data->steps);
  2517. bt_bdaddr_t *addr = current_data_step->set_data;
  2518. struct step *step = g_new0(struct step, 1);
  2519. step->action_status = data->if_bluetooth->cancel_bond(addr);
  2520. schedule_action_verification(step);
  2521. }
  2522. void bt_remove_bond_action(void)
  2523. {
  2524. struct test_data *data = tester_get_data();
  2525. struct step *current_data_step = queue_peek_head(data->steps);
  2526. bt_bdaddr_t *addr = current_data_step->set_data;
  2527. struct step *step = g_new0(struct step, 1);
  2528. step->action_status = data->if_bluetooth->remove_bond(addr);
  2529. schedule_action_verification(step);
  2530. }
  2531. static void default_ssp_req_cb(bt_bdaddr_t *remote_bd_addr, bt_bdname_t *name,
  2532. uint32_t cod, bt_ssp_variant_t pairing_variant,
  2533. uint32_t pass_key)
  2534. {
  2535. struct test_data *t_data = tester_get_data();
  2536. t_data->if_bluetooth->ssp_reply(remote_bd_addr, pairing_variant, true,
  2537. pass_key);
  2538. }
  2539. void set_default_ssp_request_handler(void)
  2540. {
  2541. struct step *step = g_new0(struct step, 1);
  2542. bt_callbacks.ssp_request_cb = default_ssp_req_cb;
  2543. step->action_status = BT_STATUS_SUCCESS;
  2544. schedule_action_verification(step);
  2545. }
  2546. static void generic_test_function(const void *test_data)
  2547. {
  2548. struct test_data *data = tester_get_data();
  2549. struct step *first_step;
  2550. init_test_steps(data);
  2551. /* first step action */
  2552. first_step = queue_peek_head(data->steps);
  2553. if (!first_step->action) {
  2554. tester_print("tester: No initial action declared");
  2555. tester_test_failed();
  2556. return;
  2557. }
  2558. first_step->action();
  2559. }
  2560. #define test(data, test_setup, test, test_teardown) \
  2561. do { \
  2562. struct test_data *user; \
  2563. user = g_malloc0(sizeof(struct test_data)); \
  2564. if (!user) \
  2565. break; \
  2566. user->hciemu_type = data->emu_type; \
  2567. user->test_data = data; \
  2568. tester_add_full(data->title, data, test_pre_setup, \
  2569. test_setup, test, test_teardown, \
  2570. test_post_teardown, 3, user, g_free); \
  2571. } while (0)
  2572. static void tester_testcases_cleanup(void)
  2573. {
  2574. remove_bluetooth_tests();
  2575. remove_socket_tests();
  2576. remove_hidhost_tests();
  2577. remove_gatt_tests();
  2578. remove_a2dp_tests();
  2579. remove_avrcp_tests();
  2580. remove_hdp_tests();
  2581. remove_pan_tests();
  2582. }
  2583. static void add_bluetooth_tests(void *data, void *user_data)
  2584. {
  2585. struct test_case *tc = data;
  2586. test(tc, setup, generic_test_function, teardown);
  2587. }
  2588. static void add_socket_tests(void *data, void *user_data)
  2589. {
  2590. struct test_case *tc = data;
  2591. test(tc, setup_socket, generic_test_function, teardown);
  2592. }
  2593. static void add_hidhost_tests(void *data, void *user_data)
  2594. {
  2595. struct test_case *tc = data;
  2596. test(tc, setup_hidhost, generic_test_function, teardown);
  2597. }
  2598. static void add_pan_tests(void *data, void *user_data)
  2599. {
  2600. struct test_case *tc = data;
  2601. test(tc, setup_pan, generic_test_function, teardown);
  2602. }
  2603. static void add_hdp_tests(void *data, void *user_data)
  2604. {
  2605. struct test_case *tc = data;
  2606. test(tc, setup_hdp, generic_test_function, teardown);
  2607. }
  2608. static void add_a2dp_tests(void *data, void *user_data)
  2609. {
  2610. struct test_case *tc = data;
  2611. test(tc, setup_a2dp, generic_test_function, teardown);
  2612. }
  2613. static void add_avrcp_tests(void *data, void *user_data)
  2614. {
  2615. struct test_case *tc = data;
  2616. test(tc, setup_avrcp, generic_test_function, teardown);
  2617. }
  2618. static void add_gatt_tests(void *data, void *user_data)
  2619. {
  2620. struct test_case *tc = data;
  2621. test(tc, setup_gatt, generic_test_function, teardown);
  2622. }
  2623. static void add_map_client_tests(void *data, void *user_data)
  2624. {
  2625. struct test_case *tc = data;
  2626. test(tc, setup_map_client, generic_test_function, teardown);
  2627. }
  2628. int main(int argc, char *argv[])
  2629. {
  2630. snprintf(exec_dir, sizeof(exec_dir), "%s", dirname(argv[0]));
  2631. tester_init(&argc, &argv);
  2632. queue_foreach(get_bluetooth_tests(), add_bluetooth_tests, NULL);
  2633. queue_foreach(get_socket_tests(), add_socket_tests, NULL);
  2634. queue_foreach(get_hidhost_tests(), add_hidhost_tests, NULL);
  2635. queue_foreach(get_pan_tests(), add_pan_tests, NULL);
  2636. queue_foreach(get_hdp_tests(), add_hdp_tests, NULL);
  2637. queue_foreach(get_a2dp_tests(), add_a2dp_tests, NULL);
  2638. queue_foreach(get_avrcp_tests(), add_avrcp_tests, NULL);
  2639. queue_foreach(get_gatt_tests(), add_gatt_tests, NULL);
  2640. queue_foreach(get_map_client_tests(), add_map_client_tests, NULL);
  2641. if (tester_run())
  2642. return 1;
  2643. tester_testcases_cleanup();
  2644. return 0;
  2645. }