bta_mce_act.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /******************************************************************************
  2. *
  3. * Copyright 2014 The Android Open Source Project
  4. * Copyright 2003-2012 Broadcom Corporation
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at:
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. ******************************************************************************/
  19. /******************************************************************************
  20. *
  21. * This file contains action functions for MCE.
  22. *
  23. ******************************************************************************/
  24. #include <arpa/inet.h>
  25. #include <hardware/bluetooth.h>
  26. #include <string.h>
  27. #include "bt_common.h"
  28. #include "bt_types.h"
  29. #include "bta_api.h"
  30. #include "bta_mce_api.h"
  31. #include "bta_mce_int.h"
  32. #include "bta_sys.h"
  33. #include "btm_api.h"
  34. #include "btm_int.h"
  35. #include "sdp_api.h"
  36. #include "utl.h"
  37. using bluetooth::Uuid;
  38. /*****************************************************************************
  39. * Constants
  40. ****************************************************************************/
  41. static const Uuid bta_mce_mas_uuid =
  42. Uuid::From16Bit(UUID_SERVCLASS_MESSAGE_ACCESS);
  43. /*******************************************************************************
  44. *
  45. * Function bta_mce_search_cback
  46. *
  47. * Description Callback from btm after search is completed
  48. *
  49. * Returns void
  50. *
  51. ******************************************************************************/
  52. static void bta_mce_search_cback(uint16_t result, void* user_data) {
  53. tSDP_DISC_REC* p_rec = NULL;
  54. tBTA_MCE_MAS_DISCOVERY_COMP evt_data;
  55. int found = 0;
  56. APPL_TRACE_DEBUG("bta_mce_start_discovery_cback res: 0x%x", result);
  57. bta_mce_cb.sdp_active = BTA_MCE_SDP_ACT_NONE;
  58. if (bta_mce_cb.p_dm_cback == NULL) return;
  59. evt_data.status = BTA_MCE_FAILURE;
  60. evt_data.remote_addr = bta_mce_cb.remote_addr;
  61. evt_data.num_mas = 0;
  62. if (result == SDP_SUCCESS || result == SDP_DB_FULL) {
  63. do {
  64. tSDP_DISC_ATTR* p_attr;
  65. tSDP_PROTOCOL_ELEM pe;
  66. p_rec = SDP_FindServiceUUIDInDb(p_bta_mce_cfg->p_sdp_db, bta_mce_mas_uuid,
  67. p_rec);
  68. APPL_TRACE_DEBUG("p_rec:%p", p_rec);
  69. if (p_rec == NULL) break;
  70. if (!SDP_FindProtocolListElemInRec(p_rec, UUID_PROTOCOL_RFCOMM, &pe))
  71. continue;
  72. evt_data.mas[found].scn = pe.params[0];
  73. p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_SERVICE_NAME);
  74. if (p_attr == NULL) continue;
  75. evt_data.mas[found].p_srv_name = (char*)p_attr->attr_value.v.array;
  76. evt_data.mas[found].srv_name_len =
  77. SDP_DISC_ATTR_LEN(p_attr->attr_len_type);
  78. p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_MAS_INSTANCE_ID);
  79. if (p_attr == NULL) break;
  80. evt_data.mas[found].instance_id = p_attr->attr_value.v.u8;
  81. p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_SUPPORTED_MSG_TYPE);
  82. if (p_attr == NULL) break;
  83. evt_data.mas[found].msg_type = p_attr->attr_value.v.u8;
  84. found++;
  85. } while (p_rec != NULL && found < BTA_MCE_MAX_MAS_INSTANCES);
  86. evt_data.num_mas = found;
  87. evt_data.status = BTA_MCE_SUCCESS;
  88. }
  89. tBTA_MCE bta_mce;
  90. bta_mce.mas_disc_comp = evt_data;
  91. bta_mce_cb.p_dm_cback(BTA_MCE_MAS_DISCOVERY_COMP_EVT, &bta_mce, user_data);
  92. }
  93. /*******************************************************************************
  94. *
  95. * Function bta_mce_enable
  96. *
  97. * Description Initializes the MCE I/F
  98. *
  99. * Returns void
  100. *
  101. ******************************************************************************/
  102. void bta_mce_enable(tBTA_MCE_MSG* p_data) {
  103. tBTA_MCE_STATUS status = BTA_MCE_SUCCESS;
  104. bta_mce_cb.p_dm_cback = p_data->enable.p_cback;
  105. tBTA_MCE bta_mce;
  106. bta_mce.status = status;
  107. bta_mce_cb.p_dm_cback(BTA_MCE_ENABLE_EVT, &bta_mce, NULL);
  108. }
  109. /*******************************************************************************
  110. *
  111. * Function bta_mce_get_remote_mas_instances
  112. *
  113. * Description Discovers MAS instances on remote device
  114. *
  115. * Returns void
  116. *
  117. ******************************************************************************/
  118. void bta_mce_get_remote_mas_instances(tBTA_MCE_MSG* p_data) {
  119. if (p_data == NULL) {
  120. APPL_TRACE_DEBUG("MCE control block handle is null");
  121. return;
  122. }
  123. tBTA_MCE_STATUS status = BTA_MCE_FAILURE;
  124. APPL_TRACE_DEBUG("%s in, sdp_active:%d", __func__, bta_mce_cb.sdp_active);
  125. if (bta_mce_cb.sdp_active != BTA_MCE_SDP_ACT_NONE) {
  126. /* SDP is still in progress */
  127. status = BTA_MCE_BUSY;
  128. if (bta_mce_cb.p_dm_cback) {
  129. tBTA_MCE bta_mce;
  130. bta_mce.status = status;
  131. bta_mce_cb.p_dm_cback(BTA_MCE_MAS_DISCOVERY_COMP_EVT, &bta_mce, NULL);
  132. }
  133. return;
  134. }
  135. bta_mce_cb.sdp_active = BTA_MCE_SDP_ACT_YES;
  136. bta_mce_cb.remote_addr = p_data->get_rmt_mas.bd_addr;
  137. SDP_InitDiscoveryDb(p_bta_mce_cfg->p_sdp_db, p_bta_mce_cfg->sdp_db_size, 1,
  138. &bta_mce_mas_uuid, 0, NULL);
  139. if (!SDP_ServiceSearchAttributeRequest2(p_data->get_rmt_mas.bd_addr,
  140. p_bta_mce_cfg->p_sdp_db,
  141. bta_mce_search_cback, NULL)) {
  142. bta_mce_cb.sdp_active = BTA_MCE_SDP_ACT_NONE;
  143. /* failed to start SDP. report the failure right away */
  144. if (bta_mce_cb.p_dm_cback) {
  145. tBTA_MCE bta_mce;
  146. bta_mce.status = status;
  147. bta_mce_cb.p_dm_cback(BTA_MCE_MAS_DISCOVERY_COMP_EVT, &bta_mce, NULL);
  148. }
  149. }
  150. /*
  151. else report the result when the cback is called
  152. */
  153. }