handsfree-client.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2014 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include <stdlib.h>
  14. #include <stdbool.h>
  15. #include <errno.h>
  16. #include <unistd.h>
  17. #include <glib.h>
  18. #include "lib/bluetooth.h"
  19. #include "lib/sdp.h"
  20. #include "lib/sdp_lib.h"
  21. #include "src/sdp-client.h"
  22. #include "src/shared/hfp.h"
  23. #include "src/shared/queue.h"
  24. #include "src/shared/util.h"
  25. #include "btio/btio.h"
  26. #include "ipc.h"
  27. #include "ipc-common.h"
  28. #include "src/log.h"
  29. #include "utils.h"
  30. #include "bluetooth.h"
  31. #include "hal-msg.h"
  32. #include "handsfree-client.h"
  33. #include "sco.h"
  34. #define HFP_HF_CHANNEL 7
  35. #define HFP_HF_FEAT_ECNR 0x00000001
  36. #define HFP_HF_FEAT_3WAY 0x00000002
  37. #define HFP_HF_FEAT_CLI 0x00000004
  38. #define HFP_HF_FEAT_VR 0x00000008
  39. #define HFP_HF_FEAT_RVC 0x00000010
  40. #define HFP_HF_FEAT_ECS 0x00000020
  41. #define HFP_HF_FEAT_ECC 0x00000040
  42. #define HFP_HF_FEAT_CODEC 0x00000080
  43. #define HFP_HF_FEAT_HF_IND 0x00000100
  44. #define HFP_HF_FEAT_ESCO_S4_T2 0x00000200
  45. #define HFP_AG_FEAT_3WAY 0x00000001
  46. #define HFP_AG_FEAT_ECNR 0x00000002
  47. #define HFP_AG_FEAT_VR 0x00000004
  48. #define HFP_AG_FEAT_INBAND 0x00000008
  49. #define HFP_AG_FEAT_VTAG 0x00000010
  50. #define HFP_AG_FEAT_REJ_CALL 0x00000020
  51. #define HFP_AG_FEAT_ECS 0x00000040
  52. #define HFP_AG_FEAT_ECC 0x00000080
  53. #define HFP_AG_FEAT_EXT_ERR 0x00000100
  54. #define HFP_AG_FEAT_CODEC 0x00000200
  55. #define HFP_HF_FEATURES (HFP_HF_FEAT_ECNR | HFP_HF_FEAT_3WAY |\
  56. HFP_HF_FEAT_CLI | HFP_HF_FEAT_VR |\
  57. HFP_HF_FEAT_RVC | HFP_HF_FEAT_ECS |\
  58. HFP_HF_FEAT_ECC)
  59. #define CVSD_OFFSET 0
  60. #define MSBC_OFFSET 1
  61. #define CODECS_COUNT (MSBC_OFFSET + 1)
  62. #define CODEC_ID_CVSD 0x01
  63. #define CODEC_ID_MSBC 0x02
  64. #define MAX_NUMBER_LEN 33
  65. #define MAX_OPERATOR_NAME_LEN 17
  66. enum hfp_indicator {
  67. HFP_INDICATOR_SERVICE = 0,
  68. HFP_INDICATOR_CALL,
  69. HFP_INDICATOR_CALLSETUP,
  70. HFP_INDICATOR_CALLHELD,
  71. HFP_INDICATOR_SIGNAL,
  72. HFP_INDICATOR_ROAM,
  73. HFP_INDICATOR_BATTCHG,
  74. HFP_INDICATOR_LAST
  75. };
  76. typedef void (*ciev_func_t)(uint8_t val);
  77. struct indicator {
  78. uint8_t index;
  79. uint32_t min;
  80. uint32_t max;
  81. uint32_t val;
  82. ciev_func_t cb;
  83. };
  84. struct hfp_codec {
  85. uint8_t type;
  86. bool local_supported;
  87. bool remote_supported;
  88. };
  89. struct device {
  90. bdaddr_t bdaddr;
  91. struct hfp_hf *hf;
  92. uint8_t state;
  93. uint8_t audio_state;
  94. uint8_t negotiated_codec;
  95. uint32_t features;
  96. struct hfp_codec codecs[2];
  97. struct indicator ag_ind[HFP_INDICATOR_LAST];
  98. uint32_t chld_features;
  99. };
  100. static const struct hfp_codec codecs_defaults[] = {
  101. { CODEC_ID_CVSD, true, false},
  102. { CODEC_ID_MSBC, false, false},
  103. };
  104. static bdaddr_t adapter_addr;
  105. static struct ipc *hal_ipc = NULL;
  106. static uint32_t hfp_hf_features = 0;
  107. static uint32_t hfp_hf_record_id = 0;
  108. static struct queue *devices = NULL;
  109. static GIOChannel *hfp_hf_server = NULL;
  110. static struct bt_sco *sco = NULL;
  111. static struct device *find_default_device(void)
  112. {
  113. return queue_peek_head(devices);
  114. }
  115. static bool match_by_bdaddr(const void *data, const void *user_data)
  116. {
  117. const bdaddr_t *addr1 = data;
  118. const bdaddr_t *addr2 = user_data;
  119. return !bacmp(addr1, addr2);
  120. }
  121. static struct device *find_device(const bdaddr_t *addr)
  122. {
  123. return queue_find(devices, match_by_bdaddr, addr);
  124. }
  125. static void init_codecs(struct device *dev)
  126. {
  127. memcpy(&dev->codecs, codecs_defaults, sizeof(dev->codecs));
  128. if (hfp_hf_features & HFP_HF_FEAT_CODEC)
  129. dev->codecs[MSBC_OFFSET].local_supported = true;
  130. }
  131. static struct device *device_create(const bdaddr_t *bdaddr)
  132. {
  133. struct device *dev;
  134. dev = new0(struct device, 1);
  135. bacpy(&dev->bdaddr, bdaddr);
  136. dev->state = HAL_HF_CLIENT_CONN_STATE_DISCONNECTED;
  137. dev->audio_state = HAL_HF_CLIENT_AUDIO_STATE_DISCONNECTED;
  138. init_codecs(dev);
  139. queue_push_tail(devices, dev);
  140. return dev;
  141. }
  142. static struct device *get_device(const bdaddr_t *addr)
  143. {
  144. struct device *dev;
  145. dev = find_device(addr);
  146. if (dev)
  147. return dev;
  148. /* We do support only one device as for now */
  149. if (queue_isempty(devices))
  150. return device_create(addr);
  151. return NULL;
  152. }
  153. static void device_set_state(struct device *dev, uint8_t state)
  154. {
  155. struct hal_ev_hf_client_conn_state ev;
  156. char address[18];
  157. if (dev->state == state)
  158. return;
  159. memset(&ev, 0, sizeof(ev));
  160. dev->state = state;
  161. ba2str(&dev->bdaddr, address);
  162. DBG("device %s state %u", address, state);
  163. bdaddr2android(&dev->bdaddr, ev.bdaddr);
  164. ev.state = state;
  165. ev.chld_feat = dev->chld_features;
  166. ev.peer_feat = dev->features;
  167. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  168. HAL_EV_HF_CLIENT_CONN_STATE, sizeof(ev), &ev);
  169. }
  170. static void device_destroy(struct device *dev)
  171. {
  172. device_set_state(dev, HAL_HF_CLIENT_CONN_STATE_DISCONNECTED);
  173. queue_remove(devices, dev);
  174. if (dev->hf)
  175. hfp_hf_unref(dev->hf);
  176. free(dev);
  177. }
  178. static void handle_disconnect(const void *buf, uint16_t len)
  179. {
  180. const struct hal_cmd_hf_client_disconnect *cmd = buf;
  181. struct device *dev;
  182. uint32_t status;
  183. bdaddr_t bdaddr;
  184. char addr[18];
  185. DBG("");
  186. android2bdaddr(&cmd->bdaddr, &bdaddr);
  187. ba2str(&bdaddr, addr);
  188. DBG("Disconnect %s", addr);
  189. dev = get_device(&bdaddr);
  190. if (!dev) {
  191. status = HAL_STATUS_FAILED;
  192. goto done;
  193. }
  194. if (dev->state == HAL_HF_CLIENT_CONN_STATE_DISCONNECTED) {
  195. status = HAL_STATUS_FAILED;
  196. goto done;
  197. }
  198. if (dev->state == HAL_HF_CLIENT_CONN_STATE_DISCONNECTING) {
  199. status = HAL_STATUS_SUCCESS;
  200. goto done;
  201. }
  202. if (dev->state == HAL_HF_CLIENT_CONN_STATE_CONNECTING) {
  203. device_destroy(dev);
  204. status = HAL_STATUS_SUCCESS;
  205. goto done;
  206. }
  207. status = hfp_hf_disconnect(dev->hf) ? HAL_STATUS_SUCCESS :
  208. HAL_STATUS_FAILED;
  209. if (status)
  210. device_set_state(dev, HAL_HF_CLIENT_CONN_STATE_DISCONNECTING);
  211. done:
  212. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  213. HAL_OP_HF_CLIENT_DISCONNECT, status);
  214. }
  215. static void set_audio_state(struct device *dev, uint8_t state)
  216. {
  217. struct hal_ev_hf_client_audio_state ev;
  218. char address[18];
  219. if (dev->audio_state == state)
  220. return;
  221. dev->audio_state = state;
  222. ba2str(&dev->bdaddr, address);
  223. DBG("device %s audio state %u", address, state);
  224. bdaddr2android(&dev->bdaddr, ev.bdaddr);
  225. ev.state = state;
  226. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  227. HAL_EV_HF_CLIENT_AUDIO_STATE, sizeof(ev), &ev);
  228. }
  229. static void bcc_cb(enum hfp_result result, enum hfp_error cme_err,
  230. void *user_data)
  231. {
  232. struct device *dev = user_data;
  233. if (result != HFP_RESULT_OK)
  234. set_audio_state(dev, HAL_HF_CLIENT_AUDIO_STATE_DISCONNECTED);
  235. }
  236. static bool codec_negotiation_supported(struct device *dev)
  237. {
  238. return (dev->features & HFP_AG_FEAT_CODEC) &&
  239. (hfp_hf_features & HFP_HF_FEAT_CODEC);
  240. }
  241. static bool connect_sco(struct device *dev)
  242. {
  243. if (codec_negotiation_supported(dev))
  244. return hfp_hf_send_command(dev->hf, bcc_cb, dev,
  245. "AT+BCC");
  246. return bt_sco_connect(sco, &dev->bdaddr, BT_VOICE_CVSD_16BIT);
  247. }
  248. static void handle_connect_audio(const void *buf, uint16_t len)
  249. {
  250. const struct hal_cmd_hf_client_connect_audio *cmd = (void *) buf;
  251. struct device *dev;
  252. uint8_t status;
  253. bdaddr_t bdaddr;
  254. DBG("");
  255. android2bdaddr(&cmd->bdaddr, &bdaddr);
  256. dev = find_device(&bdaddr);
  257. if (!dev || dev->state != HAL_HF_CLIENT_CONN_STATE_SLC_CONNECTED ||
  258. dev->audio_state != HAL_HF_CLIENT_AUDIO_STATE_DISCONNECTED) {
  259. error("hf-client: Cannot create SCO, check SLC or audio state");
  260. status = HAL_STATUS_FAILED;
  261. goto done;
  262. }
  263. if (connect_sco(dev)) {
  264. status = HAL_STATUS_SUCCESS;
  265. set_audio_state(dev, HAL_HF_CLIENT_AUDIO_STATE_CONNECTING);
  266. } else {
  267. status = HAL_STATUS_FAILED;
  268. }
  269. done:
  270. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  271. HAL_OP_HF_CLIENT_CONNECT_AUDIO, status);
  272. }
  273. static void handle_disconnect_audio(const void *buf, uint16_t len)
  274. {
  275. const struct hal_cmd_hf_client_disconnect_audio *cmd = (void *) buf;
  276. struct device *dev;
  277. uint8_t status;
  278. bdaddr_t bdaddr;
  279. DBG("");
  280. android2bdaddr(&cmd->bdaddr, &bdaddr);
  281. dev = find_device(&bdaddr);
  282. if (!dev ||
  283. dev->audio_state == HAL_HF_CLIENT_AUDIO_STATE_DISCONNECTED) {
  284. error("hf-client: Device not found or audio not connected");
  285. status = HAL_STATUS_FAILED;
  286. goto done;
  287. }
  288. bt_sco_disconnect(sco);
  289. status = HAL_STATUS_SUCCESS;
  290. done:
  291. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  292. HAL_OP_HF_CLIENT_DISCONNECT_AUDIO, status);
  293. }
  294. static void cmd_complete_cb(enum hfp_result result, enum hfp_error cme_err,
  295. void *user_data)
  296. {
  297. struct hal_ev_hf_client_command_complete ev;
  298. DBG("");
  299. memset(&ev, 0, sizeof(ev));
  300. switch (result) {
  301. case HFP_RESULT_OK:
  302. ev.type = HAL_HF_CLIENT_CMD_COMP_OK;
  303. break;
  304. case HFP_RESULT_NO_CARRIER:
  305. ev.type = HAL_HF_CLIENT_CMD_COMP_ERR_NO_CARRIER;
  306. break;
  307. case HFP_RESULT_ERROR:
  308. ev.type = HAL_HF_CLIENT_CMD_COMP_ERR;
  309. break;
  310. case HFP_RESULT_BUSY:
  311. ev.type = HAL_HF_CLIENT_CMD_COMP_ERR_BUSY;
  312. break;
  313. case HFP_RESULT_NO_ANSWER:
  314. ev.type = HAL_HF_CLIENT_CMD_COMP_ERR_NO_ANSWER;
  315. break;
  316. case HFP_RESULT_DELAYED:
  317. ev.type = HAL_HF_CLIENT_CMD_COMP_ERR_DELAYED;
  318. break;
  319. case HFP_RESULT_REJECTED:
  320. ev.type = HAL_HF_CLIENT_CMD_COMP_ERR_BACKLISTED;
  321. break;
  322. case HFP_RESULT_CME_ERROR:
  323. ev.type = HAL_HF_CLIENT_CMD_COMP_ERR_CME;
  324. ev.cme = cme_err;
  325. break;
  326. case HFP_RESULT_CONNECT:
  327. case HFP_RESULT_RING:
  328. case HFP_RESULT_NO_DIALTONE:
  329. default:
  330. error("hf-client: Unknown error code %d", result);
  331. ev.type = HAL_HF_CLIENT_CMD_COMP_ERR;
  332. break;
  333. }
  334. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  335. HAL_EV_CLIENT_COMMAND_COMPLETE, sizeof(ev), &ev);
  336. }
  337. static void handle_start_vr(const void *buf, uint16_t len)
  338. {
  339. struct device *dev;
  340. uint8_t status;
  341. DBG("");
  342. dev = find_default_device();
  343. if (!dev) {
  344. status = HAL_STATUS_FAILED;
  345. goto done;
  346. }
  347. if (hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL, "AT+BVRA=1"))
  348. status = HAL_STATUS_SUCCESS;
  349. else
  350. status = HAL_STATUS_FAILED;
  351. done:
  352. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  353. HAL_OP_HF_CLIENT_START_VR, status);
  354. }
  355. static void handle_stop_vr(const void *buf, uint16_t len)
  356. {
  357. struct device *dev;
  358. uint8_t status;
  359. DBG("");
  360. dev = find_default_device();
  361. if (!dev) {
  362. status = HAL_STATUS_FAILED;
  363. goto done;
  364. }
  365. if (hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL, "AT+BVRA=0"))
  366. status = HAL_STATUS_SUCCESS;
  367. else
  368. status = HAL_STATUS_FAILED;
  369. done:
  370. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  371. HAL_OP_HF_CLIENT_STOP_VR, status);
  372. }
  373. static void handle_volume_control(const void *buf, uint16_t len)
  374. {
  375. const struct hal_cmd_hf_client_volume_control *cmd = buf;
  376. struct device *dev;
  377. uint8_t status;
  378. uint8_t vol;
  379. bool ret;
  380. DBG("");
  381. dev = find_default_device();
  382. if (!dev) {
  383. status = HAL_STATUS_FAILED;
  384. goto done;
  385. }
  386. /*
  387. * Volume is in the range 0-15. Make sure we send correct value
  388. * to remote device
  389. */
  390. vol = cmd->volume > 15 ? 15 : cmd->volume;
  391. switch (cmd->type) {
  392. case HF_CLIENT_VOLUME_TYPE_SPEAKER:
  393. ret = hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL,
  394. "AT+VGS=%u", vol);
  395. break;
  396. case HF_CLIENT_VOLUME_TYPE_MIC:
  397. ret = hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL,
  398. "AT+VGM=%u", vol);
  399. break;
  400. default:
  401. ret = false;
  402. break;
  403. }
  404. status = ret ? HAL_STATUS_SUCCESS : HAL_STATUS_FAILED;
  405. done:
  406. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  407. HAL_OP_HF_CLIENT_VOLUME_CONTROL,
  408. status);
  409. }
  410. static void handle_dial(const void *buf, uint16_t len)
  411. {
  412. const struct hal_cmd_hf_client_dial *cmd = buf;
  413. struct device *dev;
  414. uint8_t status;
  415. bool ret;
  416. DBG("");
  417. if (len != sizeof(*cmd) + cmd->number_len)
  418. goto failed;
  419. dev = find_default_device();
  420. if (!dev) {
  421. status = HAL_STATUS_FAILED;
  422. goto done;
  423. }
  424. if (cmd->number_len > 0) {
  425. if (cmd->number[cmd->number_len - 1] != '\0')
  426. goto failed;
  427. DBG("Dialing %s", cmd->number);
  428. ret = hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL,
  429. "ATD%s;", cmd->number);
  430. } else {
  431. DBG("Redialing");
  432. ret = hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL,
  433. "AT+BLDN");
  434. }
  435. status = ret ? HAL_STATUS_SUCCESS : HAL_STATUS_FAILED;
  436. done:
  437. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  438. HAL_OP_HF_CLIENT_DIAL, status);
  439. return;
  440. failed:
  441. error("Malformed number data, size (%u bytes), terminating", len);
  442. raise(SIGTERM);
  443. }
  444. static void handle_dial_memory(const void *buf, uint16_t len)
  445. {
  446. const struct hal_cmd_hf_client_dial_memory *cmd = buf;
  447. struct device *dev;
  448. uint8_t status;
  449. DBG("");
  450. dev = find_default_device();
  451. if (!dev) {
  452. status = HAL_STATUS_FAILED;
  453. goto done;
  454. }
  455. /* For some reason location in BT HAL is int. Therefore that check */
  456. if (cmd->location < 0) {
  457. status = HAL_STATUS_FAILED;
  458. goto done;
  459. }
  460. if (hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL , "ATD>%d;",
  461. cmd->location))
  462. status = HAL_STATUS_SUCCESS;
  463. else
  464. status = HAL_STATUS_FAILED;
  465. done:
  466. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  467. HAL_OP_HF_CLIENT_DIAL_MEMORY, status);
  468. }
  469. static void handle_call_action(const void *buf, uint16_t len)
  470. {
  471. const struct hal_cmd_hf_client_call_action *cmd = buf;
  472. struct device *dev;
  473. uint8_t status;
  474. bool ret;
  475. DBG("");
  476. dev = find_default_device();
  477. if (!dev) {
  478. status = HAL_STATUS_FAILED;
  479. goto done;
  480. }
  481. switch (cmd->action) {
  482. case HAL_HF_CLIENT_ACTION_CHLD_0:
  483. ret = hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL,
  484. "AT+CHLD=0");
  485. break;
  486. case HAL_HF_CLIENT_ACTION_CHLD_1:
  487. ret = hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL,
  488. "AT+CHLD=1");
  489. break;
  490. case HAL_HF_CLIENT_ACTION_CHLD_2:
  491. ret = hfp_hf_send_command(dev->hf, cmd_complete_cb,
  492. NULL, "AT+CHLD=2");
  493. break;
  494. case HAL_HF_CLIENT_ACTION_CHLD_3:
  495. ret = hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL,
  496. "AT+CHLD=3");
  497. break;
  498. case HAL_HF_CLIENT_ACTION_CHLD_4:
  499. ret = hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL,
  500. "AT+CHLD=4");
  501. break;
  502. case HAL_HF_CLIENT_ACTION_CHLD_1x:
  503. /* Index is int in BT HAL. Let's be paranoid here */
  504. if (cmd->index <= 0)
  505. ret = false;
  506. else
  507. ret = hfp_hf_send_command(dev->hf, cmd_complete_cb,
  508. NULL, "AT+CHLD=1%d", cmd->index);
  509. break;
  510. case HAL_HF_CLIENT_ACTION_CHLD_2x:
  511. /* Index is int in BT HAL. Let's be paranoid here */
  512. if (cmd->index <= 0)
  513. ret = false;
  514. else
  515. ret = hfp_hf_send_command(dev->hf, cmd_complete_cb,
  516. NULL, "AT+CHLD=2%d", cmd->index);
  517. break;
  518. case HAL_HF_CLIENT_ACTION_ATA:
  519. ret = hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL,
  520. "ATA");
  521. break;
  522. case HAL_HF_CLIENT_ACTION_CHUP:
  523. ret = hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL,
  524. "AT+CHUP");
  525. break;
  526. case HAL_HF_CLIENT_ACTION_BRTH_0:
  527. ret = hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL,
  528. "AT+BTRH=0");
  529. break;
  530. case HAL_HF_CLIENT_ACTION_BRTH_1:
  531. ret = hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL,
  532. "AT+BTRH=1");
  533. break;
  534. case HAL_HF_CLIENT_ACTION_BRTH_2:
  535. ret = hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL,
  536. "AT+BTRH=2");
  537. break;
  538. default:
  539. error("hf-client: Unknown action %d", cmd->action);
  540. ret = false;
  541. break;
  542. }
  543. status = ret ? HAL_STATUS_SUCCESS : HAL_STATUS_FAILED;
  544. done:
  545. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  546. HAL_OP_HF_CLIENT_CALL_ACTION, status);
  547. }
  548. static void handle_query_current_calls(const void *buf, uint16_t len)
  549. {
  550. struct device *dev;
  551. uint8_t status;
  552. DBG("");
  553. dev = find_default_device();
  554. if (!dev) {
  555. status = HAL_STATUS_FAILED;
  556. goto done;
  557. }
  558. if (hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL, "AT+CLCC"))
  559. status = HAL_STATUS_SUCCESS;
  560. else
  561. status = HAL_STATUS_FAILED;
  562. done:
  563. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  564. HAL_OP_HF_CLIENT_QUERY_CURRENT_CALLS,
  565. status);
  566. }
  567. static void handle_query_operator_name(const void *buf, uint16_t len)
  568. {
  569. struct device *dev;
  570. uint8_t status;
  571. DBG("");
  572. dev = find_default_device();
  573. if (!dev) {
  574. status = HAL_STATUS_FAILED;
  575. goto done;
  576. }
  577. if (hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL, "AT+COPS?"))
  578. status = HAL_STATUS_SUCCESS;
  579. else
  580. status = HAL_STATUS_FAILED;
  581. done:
  582. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  583. HAL_OP_HF_CLIENT_QUERY_OPERATOR_NAME,
  584. status);
  585. }
  586. static void handle_retrieve_subscr_info(const void *buf, uint16_t len)
  587. {
  588. struct device *dev;
  589. uint8_t status;
  590. DBG("");
  591. dev = find_default_device();
  592. if (!dev) {
  593. status = HAL_STATUS_FAILED;
  594. goto done;
  595. }
  596. if (hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL, "AT+CNUM"))
  597. status = HAL_STATUS_SUCCESS;
  598. else
  599. status = HAL_STATUS_FAILED;
  600. done:
  601. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  602. HAL_OP_HF_CLIENT_RETRIEVE_SUBSCR_INFO,
  603. status);
  604. }
  605. static void handle_send_dtmf(const void *buf, uint16_t len)
  606. {
  607. const struct hal_cmd_hf_client_send_dtmf *cmd = buf;
  608. struct device *dev;
  609. uint8_t status;
  610. dev = find_default_device();
  611. if (!dev) {
  612. status = HAL_STATUS_FAILED;
  613. goto done;
  614. }
  615. if (hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL, "AT+VTS=%c",
  616. (char) cmd->tone))
  617. status = HAL_STATUS_SUCCESS;
  618. else
  619. status = HAL_STATUS_FAILED;
  620. done:
  621. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  622. HAL_OP_HF_CLIENT_SEND_DTMF, status);
  623. }
  624. static void handle_get_last_vc_tag_num(const void *buf, uint16_t len)
  625. {
  626. struct device *dev;
  627. uint8_t status;
  628. dev = find_default_device();
  629. if (!dev) {
  630. status = HAL_STATUS_FAILED;
  631. goto done;
  632. }
  633. if (hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL, "AT+BINP=1"))
  634. status = HAL_STATUS_SUCCESS;
  635. else
  636. status = HAL_STATUS_FAILED;
  637. done:
  638. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  639. HAL_OP_HF_CLIENT_GET_LAST_VOICE_TAG_NUM, status);
  640. }
  641. static void disconnect_watch(void *user_data)
  642. {
  643. DBG("");
  644. device_destroy(user_data);
  645. }
  646. static void slc_error(struct device *dev)
  647. {
  648. error("hf-client: Could not create SLC - dropping connection");
  649. hfp_hf_disconnect(dev->hf);
  650. }
  651. static void set_chld_feat(struct device *dev, char *feat)
  652. {
  653. DBG(" %s", feat);
  654. if (strcmp(feat, "0") == 0)
  655. dev->chld_features |= HAL_HF_CLIENT_CHLD_FEAT_REL;
  656. else if (strcmp(feat, "1") == 0)
  657. dev->chld_features |= HAL_HF_CLIENT_CHLD_FEAT_REL_ACC;
  658. else if (strcmp(feat, "1x") == 0)
  659. dev->chld_features |= HAL_HF_CLIENT_CHLD_FEAT_REL_X;
  660. else if (strcmp(feat, "2") == 0)
  661. dev->chld_features |= HAL_HF_CLIENT_CHLD_FEAT_HOLD_ACC;
  662. else if (strcmp(feat, "2x") == 0)
  663. dev->chld_features |= HAL_HF_CLIENT_CHLD_FEAT_PRIV_X;
  664. else if (strcmp(feat, "3") == 0)
  665. dev->chld_features |= HAL_HF_CLIENT_CHLD_FEAT_MERGE;
  666. else if (strcmp(feat, "4") == 0)
  667. dev->chld_features |= HAL_HF_CLIENT_CHLD_FEAT_MERGE_DETACH;
  668. }
  669. static void get_local_codecs_string(struct device *dev, char *buf,
  670. uint8_t len)
  671. {
  672. int i;
  673. uint8_t offset;
  674. memset(buf, 0, len);
  675. offset = 0;
  676. for (i = 0; i < CODECS_COUNT; i++) {
  677. char c[8];
  678. int l;
  679. if (!dev->codecs[i].local_supported)
  680. continue;
  681. memset(c, 0, sizeof(c));
  682. l = sprintf(c, "%d,", dev->codecs[i].type);
  683. if (l > (len - offset - 1)) {
  684. error("hf-client: Codecs cannot fit into buffer");
  685. return;
  686. }
  687. strcat(&buf[offset], c);
  688. offset += l;
  689. }
  690. }
  691. static void bvra_cb(struct hfp_context *context, void *user_data)
  692. {
  693. struct hal_ev_hf_client_vr_state ev;
  694. unsigned int val;
  695. if (!hfp_context_get_number(context, &val) || val > 1)
  696. return;
  697. ev.state = val ? HAL_HF_CLIENT_VR_STARTED : HAL_HF_CLIENT_VR_STOPPED;
  698. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  699. HAL_EV_HF_CLIENT_VR_STATE, sizeof(ev), &ev);
  700. }
  701. static void vgm_cb(struct hfp_context *context, void *user_data)
  702. {
  703. struct hal_ev_hf_client_volume_changed ev;
  704. unsigned int val;
  705. if (!hfp_context_get_number(context, &val) || val > 15)
  706. return;
  707. ev.type = HF_CLIENT_VOLUME_TYPE_MIC;
  708. ev.volume = val;
  709. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  710. HAL_EV_HF_CLIENT_VR_STATE, sizeof(ev), &ev);
  711. }
  712. static void vgs_cb(struct hfp_context *context, void *user_data)
  713. {
  714. struct hal_ev_hf_client_volume_changed ev;
  715. unsigned int val;
  716. if (!hfp_context_get_number(context, &val) || val > 15)
  717. return;
  718. ev.type = HF_CLIENT_VOLUME_TYPE_SPEAKER;
  719. ev.volume = val;
  720. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  721. HAL_EV_CLIENT_VOLUME_CHANGED, sizeof(ev), &ev);
  722. }
  723. static void brth_cb(struct hfp_context *context, void *user_data)
  724. {
  725. struct hal_ev_hf_client_response_and_hold_status ev;
  726. unsigned int val;
  727. DBG("");
  728. if (!hfp_context_get_number(context, &val) ||
  729. val > HAL_HF_CLIENT_RESP_AND_HOLD_STATUS_REJECT) {
  730. error("hf-client: incorrect BTRH response ");
  731. return;
  732. }
  733. ev.status = val;
  734. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  735. HAL_EV_HF_CLIENT_RESPONSE_AND_HOLD_STATUS,
  736. sizeof(ev), &ev);
  737. }
  738. static void clcc_cb(struct hfp_context *context, void *user_data)
  739. {
  740. uint8_t buf[IPC_MTU];
  741. struct hal_ev_hf_client_current_call *ev = (void *) buf;
  742. unsigned int val;
  743. DBG("");
  744. memset(buf, 0, sizeof(buf));
  745. if (!hfp_context_get_number(context, &val)) {
  746. error("hf-client: Could not get index");
  747. return;
  748. }
  749. ev->index = val;
  750. if (!hfp_context_get_number(context, &val) ||
  751. val > HAL_HF_CLIENT_DIRECTION_INCOMING) {
  752. error("hf-client: Could not get direction");
  753. return;
  754. }
  755. ev->direction = val;
  756. if (!hfp_context_get_number(context, &val) ||
  757. val > HAL_HF_CLIENT_CALL_STATE_HELD_BY_RESP_AND_HOLD) {
  758. error("hf-client: Could not get callstate");
  759. return;
  760. }
  761. ev->call_state = val;
  762. /* Next field is MODE but Android is not interested in this. Skip it */
  763. if (!hfp_context_get_number(context, &val)) {
  764. error("hf-client: Could not get mode");
  765. return;
  766. }
  767. if (!hfp_context_get_number(context, &val) || val > 1) {
  768. error("hf-client: Could not get multiparty");
  769. return;
  770. }
  771. ev->multiparty = val;
  772. if (hfp_context_get_string(context, (char *) &ev->number[0],
  773. MAX_NUMBER_LEN))
  774. ev->number_len = strlen((char *) ev->number) + 1;
  775. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  776. HAL_EV_HF_CLIENT_CURRENT_CALL,
  777. sizeof(*ev) + ev->number_len, ev);
  778. }
  779. static void ciev_cb(struct hfp_context *context, void *user_data)
  780. {
  781. struct device *dev = user_data;
  782. unsigned int index, val;
  783. int i;
  784. DBG("");
  785. if (!hfp_context_get_number(context, &index))
  786. return;
  787. if (!hfp_context_get_number(context, &val))
  788. return;
  789. for (i = 0; i < HFP_INDICATOR_LAST; i++) {
  790. if (dev->ag_ind[i].index != index)
  791. continue;
  792. if (dev->ag_ind[i].cb) {
  793. dev->ag_ind[i].val = val;
  794. dev->ag_ind[i].cb(val);
  795. return;
  796. }
  797. }
  798. }
  799. static void cnum_cb(struct hfp_context *context, void *user_data)
  800. {
  801. uint8_t buf[IPC_MTU];
  802. struct hal_ev_hf_client_subscriber_service_info *ev = (void *) buf;
  803. unsigned int service;
  804. DBG("");
  805. /* Alpha field is empty string, just skip it */
  806. hfp_context_skip_field(context);
  807. if (!hfp_context_get_string(context, (char *) &ev->name[0],
  808. MAX_NUMBER_LEN)) {
  809. error("hf-client: Could not get number");
  810. return;
  811. }
  812. ev->name_len = strlen((char *) &ev->name[0]) + 1;
  813. /* Type is not used in Android */
  814. hfp_context_skip_field(context);
  815. /* Speed field is empty string, just skip it */
  816. hfp_context_skip_field(context);
  817. if (!hfp_context_get_number(context, &service))
  818. return;
  819. switch (service) {
  820. case 4:
  821. ev->type = HAL_HF_CLIENT_SUBSCR_TYPE_VOICE;
  822. break;
  823. case 5:
  824. ev->type = HAL_HF_CLIENT_SUBSCR_TYPE_FAX;
  825. break;
  826. default:
  827. ev->type = HAL_HF_CLIENT_SUBSCR_TYPE_UNKNOWN;
  828. break;
  829. }
  830. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  831. HAL_EV_CLIENT_SUBSCRIBER_SERVICE_INFO,
  832. sizeof(*ev) + ev->name_len, ev);
  833. }
  834. static void cops_cb(struct hfp_context *context, void *user_data)
  835. {
  836. uint8_t buf[IPC_MTU];
  837. struct hal_ev_hf_client_operator_name *ev = (void *) buf;
  838. unsigned int format;
  839. DBG("");
  840. /* Not interested in mode */
  841. hfp_context_skip_field(context);
  842. if (!hfp_context_get_number(context, &format))
  843. return;
  844. if (format != 0)
  845. info("hf-client: Not correct string format in +COSP");
  846. if (!hfp_context_get_string(context, (char *) &ev->name[0],
  847. MAX_OPERATOR_NAME_LEN)) {
  848. error("hf-client: incorrect COPS response");
  849. return;
  850. }
  851. ev->name_len = strlen((char *) &ev->name[0]) + 1;
  852. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  853. HAL_EV_HF_CLIENT_OPERATOR_NAME,
  854. sizeof(*ev) + ev->name_len, ev);
  855. }
  856. static void binp_cb(struct hfp_context *context, void *user_data)
  857. {
  858. uint8_t buf[IPC_MTU];
  859. struct hal_ev_hf_client_last_void_call_tag_num *ev = (void *) buf;
  860. char number[33];
  861. DBG("");
  862. if (!hfp_context_get_string(context, number, sizeof(number))) {
  863. error("hf-client: incorrect COPS response");
  864. return;
  865. }
  866. ev->number_len = strlen(number) + 1;
  867. memcpy(ev->number, number, ev->number_len);
  868. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  869. HAL_EV_CLIENT_LAST_VOICE_CALL_TAG_NUM,
  870. sizeof(*ev) + ev->number_len, ev);
  871. }
  872. static bool is_codec_supported_localy(struct device *dev, uint8_t codec)
  873. {
  874. int i;
  875. for (i = 0; i < CODECS_COUNT; i++) {
  876. if (dev->codecs[i].type != codec)
  877. continue;
  878. return dev->codecs[i].local_supported;
  879. }
  880. return false;
  881. }
  882. static void bcs_resp(enum hfp_result result, enum hfp_error cme_err,
  883. void *user_data)
  884. {
  885. if (result != HFP_RESULT_OK)
  886. error("hf-client: Error on AT+BCS (err=%u)", result);
  887. }
  888. static void bcs_cb(struct hfp_context *context, void *user_data)
  889. {
  890. struct device *dev = user_data;
  891. unsigned int codec;
  892. char codecs_string[8];
  893. DBG("");
  894. if (!hfp_context_get_number(context, &codec))
  895. goto failed;
  896. if (!is_codec_supported_localy(dev, codec))
  897. goto failed;
  898. dev->negotiated_codec = codec;
  899. hfp_hf_send_command(dev->hf, bcs_resp, dev, "AT+BCS=%u", codec);
  900. return;
  901. failed:
  902. error("hf-client: Could not get codec");
  903. get_local_codecs_string(dev, codecs_string, sizeof(codecs_string));
  904. hfp_hf_send_command(dev->hf, bcs_resp, dev, "AT+BCS=%s", codecs_string);
  905. }
  906. static void slc_completed(struct device *dev)
  907. {
  908. int i;
  909. struct indicator *ag_ind;
  910. DBG("");
  911. ag_ind = dev->ag_ind;
  912. device_set_state(dev, HAL_HF_CLIENT_CONN_STATE_SLC_CONNECTED);
  913. /* Notify Android with indicators */
  914. for (i = 0; i < HFP_INDICATOR_LAST; i++) {
  915. if (!ag_ind[i].cb)
  916. continue;
  917. ag_ind[i].cb(ag_ind[i].val);
  918. }
  919. /* TODO: register unsolicited results handlers */
  920. hfp_hf_register(dev->hf, bvra_cb, "+BRVA", dev, NULL);
  921. hfp_hf_register(dev->hf, vgm_cb, "+VGM", dev, NULL);
  922. hfp_hf_register(dev->hf, vgs_cb, "+VGS", dev, NULL);
  923. hfp_hf_register(dev->hf, brth_cb, "+BTRH", dev, NULL);
  924. hfp_hf_register(dev->hf, clcc_cb, "+CLCC", dev, NULL);
  925. hfp_hf_register(dev->hf, ciev_cb, "+CIEV", dev, NULL);
  926. hfp_hf_register(dev->hf, cops_cb, "+COPS", dev, NULL);
  927. hfp_hf_register(dev->hf, cnum_cb, "+CNUM", dev, NULL);
  928. hfp_hf_register(dev->hf, binp_cb, "+BINP", dev, NULL);
  929. hfp_hf_register(dev->hf, bcs_cb, "+BCS", dev, NULL);
  930. if (!hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL, "AT+COPS=3,0"))
  931. info("hf-client: Could not send AT+COPS=3,0");
  932. }
  933. static void slc_chld_cb(struct hfp_context *context, void *user_data)
  934. {
  935. struct device *dev = user_data;
  936. char feat[3];
  937. if (!hfp_context_open_container(context))
  938. goto failed;
  939. while (hfp_context_get_unquoted_string(context, feat, sizeof(feat)))
  940. set_chld_feat(dev, feat);
  941. if (!hfp_context_close_container(context))
  942. goto failed;
  943. return;
  944. failed:
  945. error("hf-client: Error on CHLD response");
  946. slc_error(dev);
  947. }
  948. static void slc_chld_resp(enum hfp_result result, enum hfp_error cme_err,
  949. void *user_data)
  950. {
  951. struct device *dev = user_data;
  952. DBG("");
  953. hfp_hf_unregister(dev->hf, "+CHLD");
  954. if (result != HFP_RESULT_OK) {
  955. error("hf-client: CHLD error: %d", result);
  956. slc_error(dev);
  957. return;
  958. }
  959. slc_completed(dev);
  960. }
  961. static void slc_cmer_resp(enum hfp_result result, enum hfp_error cme_err,
  962. void *user_data)
  963. {
  964. struct device *dev = user_data;
  965. DBG("");
  966. if (result != HFP_RESULT_OK) {
  967. error("hf-client: CMER error: %d", result);
  968. goto failed;
  969. }
  970. /* Continue with SLC creation */
  971. if (!(dev->features & HFP_AG_FEAT_3WAY)) {
  972. slc_completed(dev);
  973. return;
  974. }
  975. if (!hfp_hf_register(dev->hf, slc_chld_cb, "+CHLD", dev, NULL)) {
  976. error("hf-client: Could not register +CHLD");
  977. goto failed;
  978. }
  979. if (!hfp_hf_send_command(dev->hf, slc_chld_resp, dev, "AT+CHLD=?")) {
  980. error("hf-client: Could not send AT+CHLD");
  981. goto failed;
  982. }
  983. return;
  984. failed:
  985. slc_error(dev);
  986. }
  987. static void set_indicator_value(uint8_t index, unsigned int val,
  988. struct indicator *ag_ind)
  989. {
  990. int i;
  991. for (i = 0; i < HFP_INDICATOR_LAST; i++) {
  992. if (index != ag_ind[i].index)
  993. continue;
  994. ag_ind[i].val = val;
  995. ag_ind[i].cb(val);
  996. return;
  997. }
  998. }
  999. static void slc_cind_status_cb(struct hfp_context *context,
  1000. void *user_data)
  1001. {
  1002. struct device *dev = user_data;
  1003. uint8_t index = 1;
  1004. DBG("");
  1005. while (hfp_context_has_next(context)) {
  1006. uint32_t val;
  1007. if (!hfp_context_get_number(context, &val)) {
  1008. error("hf-client: Error on CIND status response");
  1009. return;
  1010. }
  1011. set_indicator_value(index++, val, dev->ag_ind);
  1012. }
  1013. }
  1014. static void slc_cind_status_resp(enum hfp_result result,
  1015. enum hfp_error cme_err,
  1016. void *user_data)
  1017. {
  1018. struct device *dev = user_data;
  1019. DBG("");
  1020. hfp_hf_unregister(dev->hf, "+CIND");
  1021. if (result != HFP_RESULT_OK) {
  1022. error("hf-client: CIND error: %d", result);
  1023. goto failed;
  1024. }
  1025. /* Continue with SLC creation */
  1026. if (!hfp_hf_send_command(dev->hf, slc_cmer_resp, dev,
  1027. "AT+CMER=3,0,0,1")) {
  1028. error("hf-client: Counld not send AT+CMER");
  1029. goto failed;
  1030. }
  1031. return;
  1032. failed:
  1033. slc_error(dev);
  1034. }
  1035. static void slc_cind_resp(enum hfp_result result, enum hfp_error cme_err,
  1036. void *user_data)
  1037. {
  1038. struct device *dev = user_data;
  1039. DBG("");
  1040. hfp_hf_unregister(dev->hf, "+CIND");
  1041. if (result != HFP_RESULT_OK) {
  1042. error("hf-client: CIND error: %d", result);
  1043. goto failed;
  1044. }
  1045. /* Continue with SLC creation */
  1046. if (!hfp_hf_register(dev->hf, slc_cind_status_cb, "+CIND", dev,
  1047. NULL)) {
  1048. error("hf-client: Counld not register +CIND");
  1049. goto failed;
  1050. }
  1051. if (!hfp_hf_send_command(dev->hf, slc_cind_status_resp, dev,
  1052. "AT+CIND?")) {
  1053. error("hf-client: Counld not send AT+CIND?");
  1054. goto failed;
  1055. }
  1056. return;
  1057. failed:
  1058. slc_error(dev);
  1059. }
  1060. static void ciev_service_cb(uint8_t val)
  1061. {
  1062. struct hal_ev_hf_client_net_state ev;
  1063. DBG("");
  1064. if (val > HAL_HF_CLIENT_NET_ROAMING_TYPE_ROAMING) {
  1065. error("hf-client: Incorrect state %u:", val);
  1066. return;
  1067. }
  1068. ev.state = val;
  1069. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  1070. HAL_EV_HF_CLIENT_NET_STATE, sizeof(ev), &ev);
  1071. }
  1072. static void ciev_call_cb(uint8_t val)
  1073. {
  1074. struct hal_ev_hf_client_call_indicator ev;
  1075. DBG("");
  1076. if (val > HAL_HF_CLIENT_CALL_IND_CALL_IN_PROGERSS) {
  1077. error("hf-client: Incorrect call state %u:", val);
  1078. return;
  1079. }
  1080. ev.call = val;
  1081. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  1082. HAL_EV_HF_CLIENT_CALL_INDICATOR, sizeof(ev), &ev);
  1083. }
  1084. static void ciev_callsetup_cb(uint8_t val)
  1085. {
  1086. struct hal_ev_hf_client_call_setup_indicator ev;
  1087. DBG("");
  1088. if (val > HAL_HF_CLIENT_CALL_SETUP_ALERTING) {
  1089. error("hf-client: Incorrect call setup state %u:", val);
  1090. return;
  1091. }
  1092. ev.call_setup = val;
  1093. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  1094. HAL_EV_HF_CLIENT_CALL_SETUP_INDICATOR,
  1095. sizeof(ev), &ev);
  1096. }
  1097. static void ciev_callheld_cb(uint8_t val)
  1098. {
  1099. struct hal_ev_hf_client_call_held_indicator ev;
  1100. DBG("");
  1101. if (val > HAL_HF_CLIENT_CALL_SETUP_IND_HOLD) {
  1102. error("hf-client: Incorrect call held state %u:", val);
  1103. return;
  1104. }
  1105. ev.call_held = val;
  1106. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  1107. HAL_EV_HF_CLIENT_CALL_HELD_INDICATOR,
  1108. sizeof(ev), &ev);
  1109. }
  1110. static void ciev_signal_cb(uint8_t val)
  1111. {
  1112. struct hal_ev_hf_client_net_signal_strength ev;
  1113. DBG("");
  1114. if (val > 5) {
  1115. error("hf-client: Incorrect signal value %u:", val);
  1116. return;
  1117. }
  1118. ev.signal_strength = val;
  1119. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  1120. HAL_EV_HF_CLIENT_NET_SIGNAL_STRENGTH,
  1121. sizeof(ev), &ev);
  1122. }
  1123. static void ciev_roam_cb(uint8_t val)
  1124. {
  1125. struct hal_ev_hf_client_net_roaming_type ev;
  1126. DBG("");
  1127. if (val > HAL_HF_CLIENT_NET_ROAMING_TYPE_ROAMING) {
  1128. error("hf-client: Incorrect roaming state %u:", val);
  1129. return;
  1130. }
  1131. ev.state = val;
  1132. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  1133. HAL_EV_HF_CLIENT_NET_ROAMING_TYPE,
  1134. sizeof(ev), &ev);
  1135. }
  1136. static void ciev_battchg_cb(uint8_t val)
  1137. {
  1138. struct hal_ev_hf_client_battery_level ev;
  1139. DBG("");
  1140. if (val > 5) {
  1141. error("hf-client: Incorrect battery charge value %u:", val);
  1142. return;
  1143. }
  1144. ev.battery_level = val;
  1145. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  1146. HAL_EV_HF_CLIENT_BATTERY_LEVEL, sizeof(ev), &ev);
  1147. }
  1148. static void set_indicator_parameters(uint8_t index, const char *indicator,
  1149. unsigned int min,
  1150. unsigned int max,
  1151. struct indicator *ag_ind)
  1152. {
  1153. DBG("%s, %i", indicator, index);
  1154. /* TODO: Verify min/max values ? */
  1155. if (strcmp("service", indicator) == 0) {
  1156. ag_ind[HFP_INDICATOR_SERVICE].index = index;
  1157. ag_ind[HFP_INDICATOR_SERVICE].min = min;
  1158. ag_ind[HFP_INDICATOR_SERVICE].max = max;
  1159. ag_ind[HFP_INDICATOR_SERVICE].cb = ciev_service_cb;
  1160. return;
  1161. }
  1162. if (strcmp("call", indicator) == 0) {
  1163. ag_ind[HFP_INDICATOR_CALL].index = index;
  1164. ag_ind[HFP_INDICATOR_CALL].min = min;
  1165. ag_ind[HFP_INDICATOR_CALL].max = max;
  1166. ag_ind[HFP_INDICATOR_CALL].cb = ciev_call_cb;
  1167. return;
  1168. }
  1169. if (strcmp("callsetup", indicator) == 0) {
  1170. ag_ind[HFP_INDICATOR_CALLSETUP].index = index;
  1171. ag_ind[HFP_INDICATOR_CALLSETUP].min = min;
  1172. ag_ind[HFP_INDICATOR_CALLSETUP].max = max;
  1173. ag_ind[HFP_INDICATOR_CALLSETUP].cb = ciev_callsetup_cb;
  1174. return;
  1175. }
  1176. if (strcmp("callheld", indicator) == 0) {
  1177. ag_ind[HFP_INDICATOR_CALLHELD].index = index;
  1178. ag_ind[HFP_INDICATOR_CALLHELD].min = min;
  1179. ag_ind[HFP_INDICATOR_CALLHELD].max = max;
  1180. ag_ind[HFP_INDICATOR_CALLHELD].cb = ciev_callheld_cb;
  1181. return;
  1182. }
  1183. if (strcmp("signal", indicator) == 0) {
  1184. ag_ind[HFP_INDICATOR_SIGNAL].index = index;
  1185. ag_ind[HFP_INDICATOR_SIGNAL].min = min;
  1186. ag_ind[HFP_INDICATOR_SIGNAL].max = max;
  1187. ag_ind[HFP_INDICATOR_SIGNAL].cb = ciev_signal_cb;
  1188. return;
  1189. }
  1190. if (strcmp("roam", indicator) == 0) {
  1191. ag_ind[HFP_INDICATOR_ROAM].index = index;
  1192. ag_ind[HFP_INDICATOR_ROAM].min = min;
  1193. ag_ind[HFP_INDICATOR_ROAM].max = max;
  1194. ag_ind[HFP_INDICATOR_ROAM].cb = ciev_roam_cb;
  1195. return;
  1196. }
  1197. if (strcmp("battchg", indicator) == 0) {
  1198. ag_ind[HFP_INDICATOR_BATTCHG].index = index;
  1199. ag_ind[HFP_INDICATOR_BATTCHG].min = min;
  1200. ag_ind[HFP_INDICATOR_BATTCHG].max = max;
  1201. ag_ind[HFP_INDICATOR_BATTCHG].cb = ciev_battchg_cb;
  1202. return;
  1203. }
  1204. error("hf-client: Unknown indicator: %s", indicator);
  1205. }
  1206. static void slc_cind_cb(struct hfp_context *context, void *user_data)
  1207. {
  1208. struct device *dev = user_data;
  1209. int index = 1;
  1210. DBG("");
  1211. while (hfp_context_has_next(context)) {
  1212. char name[255];
  1213. unsigned int min, max;
  1214. /* e.g ("callsetup",(0-3)) */
  1215. if (!hfp_context_open_container(context))
  1216. break;
  1217. if (!hfp_context_get_string(context, name, sizeof(name))) {
  1218. error("hf-client: Could not get string");
  1219. goto failed;
  1220. }
  1221. if (!hfp_context_open_container(context)) {
  1222. error("hf-client: Could not open container");
  1223. goto failed;
  1224. }
  1225. if (!hfp_context_get_range(context, &min, &max)) {
  1226. if (!hfp_context_get_number(context, &min)) {
  1227. error("hf-client: Could not get number");
  1228. goto failed;
  1229. }
  1230. if (!hfp_context_get_number(context, &max)) {
  1231. error("hf-client: Could not get number");
  1232. goto failed;
  1233. }
  1234. }
  1235. if (!hfp_context_close_container(context)) {
  1236. error("hf-client: Could not close container");
  1237. goto failed;
  1238. }
  1239. if (!hfp_context_close_container(context)) {
  1240. error("hf-client: Could not close container");
  1241. goto failed;
  1242. }
  1243. set_indicator_parameters(index, name, min, max, dev->ag_ind);
  1244. index++;
  1245. }
  1246. return;
  1247. failed:
  1248. error("hf-client: Error on CIND response");
  1249. slc_error(dev);
  1250. }
  1251. static void slc_bac_resp(enum hfp_result result, enum hfp_error cme_err,
  1252. void *user_data)
  1253. {
  1254. struct device *dev = user_data;
  1255. DBG("");
  1256. if (result != HFP_RESULT_OK)
  1257. goto failed;
  1258. /* Continue with SLC creation */
  1259. if (!hfp_hf_register(dev->hf, slc_cind_cb, "+CIND", dev, NULL)) {
  1260. error("hf-client: Could not register for +CIND");
  1261. goto failed;
  1262. }
  1263. if (!hfp_hf_send_command(dev->hf, slc_cind_resp, dev, "AT+CIND=?"))
  1264. goto failed;
  1265. return;
  1266. failed:
  1267. error("hf-client: Error on BAC response");
  1268. slc_error(dev);
  1269. }
  1270. static bool send_supported_codecs(struct device *dev)
  1271. {
  1272. char codecs_string[8];
  1273. char bac[16];
  1274. memset(bac, 0, sizeof(bac));
  1275. strcpy(bac, "AT+BAC=");
  1276. get_local_codecs_string(dev, codecs_string, sizeof(codecs_string));
  1277. strcat(bac, codecs_string);
  1278. return hfp_hf_send_command(dev->hf, slc_bac_resp, dev, bac);
  1279. }
  1280. static void slc_brsf_cb(struct hfp_context *context, void *user_data)
  1281. {
  1282. unsigned int feat;
  1283. struct device *dev = user_data;
  1284. DBG("");
  1285. if (hfp_context_get_number(context, &feat))
  1286. dev->features = feat;
  1287. }
  1288. static void slc_brsf_resp(enum hfp_result result, enum hfp_error cme_err,
  1289. void *user_data)
  1290. {
  1291. struct device *dev = user_data;
  1292. hfp_hf_unregister(dev->hf, "+BRSF");
  1293. if (result != HFP_RESULT_OK) {
  1294. error("hf-client: BRSF error: %d", result);
  1295. goto failed;
  1296. }
  1297. /* Continue with SLC creation */
  1298. if (codec_negotiation_supported(dev)) {
  1299. if (send_supported_codecs(dev))
  1300. return;
  1301. error("hf-client: Could not send BAC command");
  1302. goto failed;
  1303. }
  1304. /* No WBS on remote side. Continue with indicators */
  1305. if (!hfp_hf_register(dev->hf, slc_cind_cb, "+CIND", dev, NULL)) {
  1306. error("hf-client: Could not register for +CIND");
  1307. goto failed;
  1308. }
  1309. if (!hfp_hf_send_command(dev->hf, slc_cind_resp, dev, "AT+CIND=?")) {
  1310. error("hf-client: Could not send AT+CIND command");
  1311. goto failed;
  1312. }
  1313. return;
  1314. failed:
  1315. slc_error(dev);
  1316. }
  1317. static bool create_slc(struct device *dev)
  1318. {
  1319. DBG("");
  1320. if (!hfp_hf_register(dev->hf, slc_brsf_cb, "+BRSF", dev, NULL))
  1321. return false;
  1322. return hfp_hf_send_command(dev->hf, slc_brsf_resp, dev, "AT+BRSF=%u",
  1323. hfp_hf_features);
  1324. }
  1325. static void connect_cb(GIOChannel *chan, GError *err, gpointer user_data)
  1326. {
  1327. struct device *dev = user_data;
  1328. DBG("");
  1329. if (err) {
  1330. error("hf-client: connect failed (%s)", err->message);
  1331. goto failed;
  1332. }
  1333. dev->hf = hfp_hf_new(g_io_channel_unix_get_fd(chan));
  1334. if (!dev->hf) {
  1335. error("hf-client: Could not create hfp io");
  1336. goto failed;
  1337. }
  1338. g_io_channel_set_close_on_unref(chan, FALSE);
  1339. hfp_hf_set_close_on_unref(dev->hf, true);
  1340. hfp_hf_set_disconnect_handler(dev->hf, disconnect_watch, dev, NULL);
  1341. if (!create_slc(dev)) {
  1342. error("hf-client: Could not start SLC creation");
  1343. hfp_hf_disconnect(dev->hf);
  1344. goto failed;
  1345. }
  1346. device_set_state(dev, HAL_HF_CLIENT_CONN_STATE_CONNECTED);
  1347. return;
  1348. failed:
  1349. g_io_channel_shutdown(chan, TRUE, NULL);
  1350. device_destroy(dev);
  1351. }
  1352. static void sdp_hfp_search_cb(sdp_list_t *recs, int err, gpointer data)
  1353. {
  1354. sdp_list_t *protos, *classes;
  1355. struct device *dev = data;
  1356. GError *gerr = NULL;
  1357. GIOChannel *io;
  1358. uuid_t uuid;
  1359. int channel;
  1360. DBG("");
  1361. if (err < 0) {
  1362. error("hf-client: unable to get SDP record: %s",
  1363. strerror(-err));
  1364. goto failed;
  1365. }
  1366. if (!recs || !recs->data) {
  1367. info("hf-client: no HFP SDP records found");
  1368. goto failed;
  1369. }
  1370. if (sdp_get_service_classes(recs->data, &classes) < 0 || !classes) {
  1371. error("hf-client: unable to get service classes from record");
  1372. goto failed;
  1373. }
  1374. /* TODO read remote version? */
  1375. memcpy(&uuid, classes->data, sizeof(uuid));
  1376. sdp_list_free(classes, free);
  1377. if (!sdp_uuid128_to_uuid(&uuid) || uuid.type != SDP_UUID16 ||
  1378. uuid.value.uuid16 != HANDSFREE_AGW_SVCLASS_ID) {
  1379. error("hf-client: invalid service record or not HFP");
  1380. goto failed;
  1381. }
  1382. if (sdp_get_access_protos(recs->data, &protos) < 0) {
  1383. error("hf-client: unable to get access protocols from record");
  1384. sdp_list_free(classes, free);
  1385. goto failed;
  1386. }
  1387. channel = sdp_get_proto_port(protos, RFCOMM_UUID);
  1388. sdp_list_foreach(protos, (sdp_list_func_t) sdp_list_free, NULL);
  1389. sdp_list_free(protos, NULL);
  1390. if (channel <= 0) {
  1391. error("hf-client: unable to get RFCOMM channel from record");
  1392. goto failed;
  1393. }
  1394. io = bt_io_connect(connect_cb, dev, NULL, &gerr,
  1395. BT_IO_OPT_SOURCE_BDADDR, &adapter_addr,
  1396. BT_IO_OPT_DEST_BDADDR, &dev->bdaddr,
  1397. BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
  1398. BT_IO_OPT_CHANNEL, channel,
  1399. BT_IO_OPT_INVALID);
  1400. if (!io) {
  1401. error("hf-client: unable to connect: %s", gerr->message);
  1402. g_error_free(gerr);
  1403. goto failed;
  1404. }
  1405. g_io_channel_unref(io);
  1406. return;
  1407. failed:
  1408. device_destroy(dev);
  1409. }
  1410. static int sdp_search_hfp(struct device *dev)
  1411. {
  1412. uuid_t uuid;
  1413. sdp_uuid16_create(&uuid, HANDSFREE_AGW_SVCLASS_ID);
  1414. return bt_search_service(&adapter_addr, &dev->bdaddr, &uuid,
  1415. sdp_hfp_search_cb, dev, NULL, 0);
  1416. }
  1417. static void handle_connect(const void *buf, uint16_t len)
  1418. {
  1419. struct device *dev;
  1420. const struct hal_cmd_hf_client_connect *cmd = buf;
  1421. uint32_t status;
  1422. bdaddr_t bdaddr;
  1423. char addr[18];
  1424. DBG("");
  1425. android2bdaddr(&cmd->bdaddr, &bdaddr);
  1426. ba2str(&bdaddr, addr);
  1427. DBG("connecting to %s", addr);
  1428. dev = get_device(&bdaddr);
  1429. if (!dev) {
  1430. status = HAL_STATUS_FAILED;
  1431. goto done;
  1432. }
  1433. if (dev->state != HAL_HF_CLIENT_CONN_STATE_DISCONNECTED) {
  1434. status = HAL_STATUS_FAILED;
  1435. goto done;
  1436. }
  1437. if (sdp_search_hfp(dev) < 0) {
  1438. status = HAL_STATUS_FAILED;
  1439. device_destroy(dev);
  1440. goto done;
  1441. }
  1442. device_set_state(dev, HAL_HF_CLIENT_CONN_STATE_CONNECTING);
  1443. status = HAL_STATUS_SUCCESS;
  1444. done:
  1445. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
  1446. HAL_OP_HF_CLIENT_CONNECT, status);
  1447. }
  1448. static void confirm_cb(GIOChannel *chan, gpointer data)
  1449. {
  1450. struct device *dev;
  1451. char address[18];
  1452. bdaddr_t bdaddr;
  1453. GError *err = NULL;
  1454. bt_io_get(chan, &err,
  1455. BT_IO_OPT_DEST, address,
  1456. BT_IO_OPT_DEST_BDADDR, &bdaddr,
  1457. BT_IO_OPT_INVALID);
  1458. if (err) {
  1459. error("hf-client: confirm failed (%s)", err->message);
  1460. g_error_free(err);
  1461. goto drop;
  1462. }
  1463. DBG("Incoming connection from %s", address);
  1464. dev = get_device(&bdaddr);
  1465. if (!dev) {
  1466. error("hf-client: There is other AG connected");
  1467. goto drop;
  1468. }
  1469. if (dev->state != HAL_HF_CLIENT_CONN_STATE_DISCONNECTED) {
  1470. /* TODO: Handle colision */
  1471. error("hf-client: Connections is up or ongoing ?");
  1472. goto drop;
  1473. }
  1474. device_set_state(dev, HAL_HF_CLIENT_CONN_STATE_CONNECTING);
  1475. if (!bt_io_accept(chan, connect_cb, dev, NULL, NULL)) {
  1476. error("hf-client: failed to accept connection");
  1477. device_destroy(dev);
  1478. goto drop;
  1479. }
  1480. return;
  1481. drop:
  1482. g_io_channel_shutdown(chan, TRUE, NULL);
  1483. }
  1484. static const struct ipc_handler cmd_handlers[] = {
  1485. /* HAL_OP_HF_CLIENT_CONNECT */
  1486. { handle_connect, false,
  1487. sizeof(struct hal_cmd_hf_client_connect) },
  1488. /* HAL_OP_HF_CLIENT_DISCONNECT */
  1489. { handle_disconnect, false,
  1490. sizeof(struct hal_cmd_hf_client_disconnect) },
  1491. /* HAL_OP_HF_CLIENT_CONNECT_AUDIO */
  1492. { handle_connect_audio, false,
  1493. sizeof(struct hal_cmd_hf_client_connect_audio) },
  1494. /* HAL_OP_HF_CLIENT_DISCONNECT_AUDIO */
  1495. { handle_disconnect_audio, false,
  1496. sizeof(struct hal_cmd_hf_client_disconnect_audio) },
  1497. /* define HAL_OP_HF_CLIENT_START_VR */
  1498. { handle_start_vr, false, 0 },
  1499. /* define HAL_OP_HF_CLIENT_STOP_VR */
  1500. { handle_stop_vr, false, 0 },
  1501. /* HAL_OP_HF_CLIENT_VOLUME_CONTROL */
  1502. { handle_volume_control, false,
  1503. sizeof(struct hal_cmd_hf_client_volume_control) },
  1504. /* HAL_OP_HF_CLIENT_DIAL */
  1505. { handle_dial, true, sizeof(struct hal_cmd_hf_client_dial) },
  1506. /* HAL_OP_HF_CLIENT_DIAL_MEMORY */
  1507. { handle_dial_memory, false,
  1508. sizeof(struct hal_cmd_hf_client_dial_memory) },
  1509. /* HAL_OP_HF_CLIENT_CALL_ACTION */
  1510. { handle_call_action, false,
  1511. sizeof(struct hal_cmd_hf_client_call_action) },
  1512. /* HAL_OP_HF_CLIENT_QUERY_CURRENT_CALLS */
  1513. { handle_query_current_calls, false, 0 },
  1514. /* HAL_OP_HF_CLIENT_QUERY_OPERATOR_NAME */
  1515. { handle_query_operator_name, false, 0 },
  1516. /* HAL_OP_HF_CLIENT_RETRIEVE_SUBSCR_INFO */
  1517. { handle_retrieve_subscr_info, false, 0 },
  1518. /* HAL_OP_HF_CLIENT_SEND_DTMF */
  1519. { handle_send_dtmf, false,
  1520. sizeof(struct hal_cmd_hf_client_send_dtmf) },
  1521. /* HAL_OP_HF_CLIENT_GET_LAST_VOICE_TAG_NUM */
  1522. { handle_get_last_vc_tag_num, false, 0 },
  1523. };
  1524. static sdp_record_t *hfp_hf_record(void)
  1525. {
  1526. sdp_list_t *svclass_id, *pfseq, *apseq, *root;
  1527. uuid_t root_uuid, svclass_uuid, ga_svclass_uuid;
  1528. uuid_t l2cap_uuid, rfcomm_uuid;
  1529. sdp_profile_desc_t profile;
  1530. sdp_list_t *aproto, *proto[2];
  1531. sdp_record_t *record;
  1532. sdp_data_t *channel, *features;
  1533. uint16_t sdpfeat;
  1534. uint8_t ch = HFP_HF_CHANNEL;
  1535. record = sdp_record_alloc();
  1536. if (!record)
  1537. return NULL;
  1538. sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
  1539. root = sdp_list_append(NULL, &root_uuid);
  1540. sdp_set_browse_groups(record, root);
  1541. sdp_uuid16_create(&svclass_uuid, HANDSFREE_SVCLASS_ID);
  1542. svclass_id = sdp_list_append(NULL, &svclass_uuid);
  1543. sdp_uuid16_create(&ga_svclass_uuid, GENERIC_AUDIO_SVCLASS_ID);
  1544. svclass_id = sdp_list_append(svclass_id, &ga_svclass_uuid);
  1545. sdp_set_service_classes(record, svclass_id);
  1546. sdp_uuid16_create(&profile.uuid, HANDSFREE_PROFILE_ID);
  1547. profile.version = 0x0106;
  1548. pfseq = sdp_list_append(NULL, &profile);
  1549. sdp_set_profile_descs(record, pfseq);
  1550. sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
  1551. proto[0] = sdp_list_append(NULL, &l2cap_uuid);
  1552. apseq = sdp_list_append(NULL, proto[0]);
  1553. sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
  1554. proto[1] = sdp_list_append(NULL, &rfcomm_uuid);
  1555. channel = sdp_data_alloc(SDP_UINT8, &ch);
  1556. proto[1] = sdp_list_append(proto[1], channel);
  1557. apseq = sdp_list_append(apseq, proto[1]);
  1558. /* Codec Negotiation bit in SDP feature is different then in BRSF */
  1559. sdpfeat = hfp_hf_features & 0x0000003F;
  1560. if (hfp_hf_features & HFP_HF_FEAT_CODEC)
  1561. sdpfeat |= 0x00000020;
  1562. else
  1563. sdpfeat &= ~0x00000020;
  1564. features = sdp_data_alloc(SDP_UINT16, &sdpfeat);
  1565. sdp_attr_add(record, SDP_ATTR_SUPPORTED_FEATURES, features);
  1566. aproto = sdp_list_append(NULL, apseq);
  1567. sdp_set_access_protos(record, aproto);
  1568. sdp_set_info_attr(record, "Hands-Free unit", NULL, NULL);
  1569. sdp_data_free(channel);
  1570. sdp_list_free(proto[0], NULL);
  1571. sdp_list_free(proto[1], NULL);
  1572. sdp_list_free(apseq, NULL);
  1573. sdp_list_free(pfseq, NULL);
  1574. sdp_list_free(aproto, NULL);
  1575. sdp_list_free(root, NULL);
  1576. sdp_list_free(svclass_id, NULL);
  1577. return record;
  1578. }
  1579. static bool enable_hf_client(void)
  1580. {
  1581. sdp_record_t *rec;
  1582. GError *err = NULL;
  1583. hfp_hf_server = bt_io_listen(NULL, confirm_cb, NULL, NULL, &err,
  1584. BT_IO_OPT_SOURCE_BDADDR, &adapter_addr,
  1585. BT_IO_OPT_CHANNEL, HFP_HF_CHANNEL,
  1586. BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
  1587. BT_IO_OPT_INVALID);
  1588. if (!hfp_hf_server) {
  1589. error("hf-client: Failed to listen on Handsfree rfcomm: %s",
  1590. err->message);
  1591. g_error_free(err);
  1592. return false;
  1593. }
  1594. hfp_hf_features = HFP_HF_FEATURES;
  1595. rec = hfp_hf_record();
  1596. if (!rec) {
  1597. error("hf-client: Could not create service record");
  1598. goto failed;
  1599. }
  1600. if (bt_adapter_add_record(rec, 0) < 0) {
  1601. error("hf-client: Failed to register service record");
  1602. sdp_record_free(rec);
  1603. goto failed;
  1604. }
  1605. hfp_hf_record_id = rec->handle;
  1606. return true;
  1607. failed:
  1608. g_io_channel_shutdown(hfp_hf_server, TRUE, NULL);
  1609. g_io_channel_unref(hfp_hf_server);
  1610. hfp_hf_server = NULL;
  1611. return false;
  1612. }
  1613. static void cleanup_hfp_hf(void)
  1614. {
  1615. if (hfp_hf_server) {
  1616. g_io_channel_shutdown(hfp_hf_server, TRUE, NULL);
  1617. g_io_channel_unref(hfp_hf_server);
  1618. hfp_hf_server = NULL;
  1619. }
  1620. if (hfp_hf_record_id > 0) {
  1621. bt_adapter_remove_record(hfp_hf_record_id);
  1622. hfp_hf_record_id = 0;
  1623. }
  1624. if (sco) {
  1625. bt_sco_unref(sco);
  1626. sco = NULL;
  1627. }
  1628. }
  1629. static bool confirm_sco_cb(const bdaddr_t *addr, uint16_t *voice_settings)
  1630. {
  1631. struct device *dev;
  1632. DBG("");
  1633. dev = find_device(addr);
  1634. if (!dev || dev->state != HAL_HF_CLIENT_CONN_STATE_SLC_CONNECTED) {
  1635. error("hf-client: No device or SLC not ready");
  1636. return false;
  1637. }
  1638. set_audio_state(dev, HAL_HF_CLIENT_AUDIO_STATE_CONNECTING);
  1639. if (codec_negotiation_supported(dev) &&
  1640. dev->negotiated_codec != CODEC_ID_CVSD)
  1641. *voice_settings = BT_VOICE_TRANSPARENT;
  1642. else
  1643. *voice_settings = BT_VOICE_CVSD_16BIT;
  1644. return true;
  1645. }
  1646. static void connect_sco_cb(enum sco_status status, const bdaddr_t *addr)
  1647. {
  1648. struct device *dev;
  1649. uint8_t audio_state;
  1650. DBG("SCO Status %u", status);
  1651. /* Device shall be there, just sanity check */
  1652. dev = find_device(addr);
  1653. if (!dev) {
  1654. error("hf-client: There is no device?");
  1655. return;
  1656. }
  1657. if (status != SCO_STATUS_OK) {
  1658. audio_state = HAL_HF_CLIENT_AUDIO_STATE_DISCONNECTED;
  1659. goto done;
  1660. }
  1661. if (dev->negotiated_codec == CODEC_ID_MSBC)
  1662. audio_state = HAL_HF_CLIENT_AUDIO_STATE_CONNECTED_MSBC;
  1663. else
  1664. audio_state = HAL_HF_CLIENT_AUDIO_STATE_CONNECTED;
  1665. done:
  1666. set_audio_state(dev, audio_state);
  1667. }
  1668. static void disconnect_sco_cb(const bdaddr_t *addr)
  1669. {
  1670. struct device *dev;
  1671. DBG("");
  1672. dev = find_device(addr);
  1673. if (!dev) {
  1674. error("hf-client: No device");
  1675. return;
  1676. }
  1677. set_audio_state(dev, HAL_HF_CLIENT_AUDIO_STATE_DISCONNECTED);
  1678. }
  1679. bool bt_hf_client_register(struct ipc *ipc, const bdaddr_t *addr)
  1680. {
  1681. DBG("");
  1682. devices = queue_new();
  1683. bacpy(&adapter_addr, addr);
  1684. if (!enable_hf_client())
  1685. goto failed;
  1686. sco = bt_sco_new(addr);
  1687. if (!sco) {
  1688. error("hf-client: Cannot create SCO. HFP AG is in use ?");
  1689. goto failed;
  1690. }
  1691. bt_sco_set_confirm_cb(sco, confirm_sco_cb);
  1692. bt_sco_set_connect_cb(sco, connect_sco_cb);
  1693. bt_sco_set_disconnect_cb(sco, disconnect_sco_cb);
  1694. hal_ipc = ipc;
  1695. ipc_register(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT, cmd_handlers,
  1696. G_N_ELEMENTS(cmd_handlers));
  1697. return true;
  1698. failed:
  1699. cleanup_hfp_hf();
  1700. queue_destroy(devices, free);
  1701. devices = NULL;
  1702. return false;
  1703. }
  1704. void bt_hf_client_unregister(void)
  1705. {
  1706. DBG("");
  1707. cleanup_hfp_hf();
  1708. queue_destroy(devices, (void *) device_destroy);
  1709. devices = NULL;
  1710. ipc_unregister(hal_ipc, HAL_SERVICE_ID_HANDSFREE);
  1711. hal_ipc = NULL;
  1712. }