pan_utils.cc 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /******************************************************************************
  2. *
  3. * Copyright 1999-2012 Broadcom Corporation
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at:
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. ******************************************************************************/
  18. /*****************************************************************************
  19. *
  20. * This file contains main functions to support PAN profile
  21. * commands and events.
  22. *
  23. *****************************************************************************/
  24. #include <base/logging.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include "bnep_api.h"
  28. #include "bt_common.h"
  29. #include "btm_api.h"
  30. #include "hcidefs.h"
  31. #include "l2c_api.h"
  32. #include "pan_api.h"
  33. #include "pan_int.h"
  34. #include "sdp_api.h"
  35. #include "sdpdefs.h"
  36. static const uint8_t pan_proto_elem_data[] = {
  37. 0x35, 0x18, /* data element sequence of length 0x18 bytes */
  38. 0x35, 0x06, /* data element sequence for L2CAP descriptor */
  39. 0x19, 0x01, 0x00, /* UUID for L2CAP - 0x0100 */
  40. 0x09, 0x00, 0x0F, /* PSM for BNEP - 0x000F */
  41. 0x35, 0x0E, /* data element seqence for BNEP descriptor */
  42. 0x19, 0x00, 0x0F, /* UUID for BNEP - 0x000F */
  43. 0x09, 0x01,
  44. 0x00, /* BNEP specific parameter 0 -- Version of BNEP = version 1 = 0x0001
  45. */
  46. 0x35,
  47. 0x06, /* BNEP specific parameter 1 -- Supported network packet type list */
  48. 0x09, 0x08, 0x00, /* network packet type IPv4 = 0x0800 */
  49. 0x09, 0x08, 0x06 /* network packet type ARP = 0x0806 */
  50. };
  51. /*******************************************************************************
  52. *
  53. * Function pan_register_with_sdp
  54. *
  55. * Description
  56. *
  57. * Returns
  58. *
  59. ******************************************************************************/
  60. uint32_t pan_register_with_sdp(uint16_t uuid, uint8_t sec_mask,
  61. const char* p_name, const char* p_desc) {
  62. uint32_t sdp_handle;
  63. uint16_t browse_list = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP;
  64. uint16_t security = 0;
  65. uint32_t proto_len = (uint32_t)pan_proto_elem_data[1];
  66. /* Create a record */
  67. sdp_handle = SDP_CreateRecord();
  68. if (sdp_handle == 0) {
  69. PAN_TRACE_ERROR("PAN_SetRole - could not create SDP record");
  70. return 0;
  71. }
  72. /* Service Class ID List */
  73. SDP_AddServiceClassIdList(sdp_handle, 1, &uuid);
  74. /* Add protocol element sequence from the constant string */
  75. SDP_AddAttribute(sdp_handle, ATTR_ID_PROTOCOL_DESC_LIST,
  76. DATA_ELE_SEQ_DESC_TYPE, proto_len,
  77. (uint8_t*)(pan_proto_elem_data + 2));
  78. /* Language base */
  79. SDP_AddLanguageBaseAttrIDList(sdp_handle, LANG_ID_CODE_ENGLISH,
  80. LANG_ID_CHAR_ENCODE_UTF8, LANGUAGE_BASE_ID);
  81. /* Profile descriptor list */
  82. SDP_AddProfileDescriptorList(sdp_handle, uuid, PAN_PROFILE_VERSION);
  83. /* Service Name */
  84. SDP_AddAttribute(sdp_handle, ATTR_ID_SERVICE_NAME, TEXT_STR_DESC_TYPE,
  85. (uint8_t)(strlen(p_name) + 1), (uint8_t*)p_name);
  86. /* Service description */
  87. SDP_AddAttribute(sdp_handle, ATTR_ID_SERVICE_DESCRIPTION, TEXT_STR_DESC_TYPE,
  88. (uint8_t)(strlen(p_desc) + 1), (uint8_t*)p_desc);
  89. /* Security description */
  90. if (sec_mask) {
  91. UINT16_TO_BE_FIELD(&security, 0x0001);
  92. }
  93. SDP_AddAttribute(sdp_handle, ATTR_ID_SECURITY_DESCRIPTION, UINT_DESC_TYPE, 2,
  94. (uint8_t*)&security);
  95. #if (PAN_SUPPORTS_ROLE_NAP == TRUE)
  96. if (uuid == UUID_SERVCLASS_NAP) {
  97. uint16_t NetAccessType = 0x0005; /* Ethernet */
  98. uint32_t NetAccessRate = 0x0001312D0; /* 10Mb/sec */
  99. uint8_t array[10], *p;
  100. /* Net access type. */
  101. p = array;
  102. UINT16_TO_BE_STREAM(p, NetAccessType);
  103. SDP_AddAttribute(sdp_handle, ATTR_ID_NET_ACCESS_TYPE, UINT_DESC_TYPE, 2,
  104. array);
  105. /* Net access rate. */
  106. p = array;
  107. UINT32_TO_BE_STREAM(p, NetAccessRate);
  108. SDP_AddAttribute(sdp_handle, ATTR_ID_MAX_NET_ACCESS_RATE, UINT_DESC_TYPE, 4,
  109. array);
  110. /* Register with Security Manager for the specific security level */
  111. if ((!BTM_SetSecurityLevel(true, p_name, BTM_SEC_SERVICE_BNEP_NAP, sec_mask,
  112. BT_PSM_BNEP, BTM_SEC_PROTO_BNEP,
  113. UUID_SERVCLASS_NAP)) ||
  114. (!BTM_SetSecurityLevel(false, p_name, BTM_SEC_SERVICE_BNEP_NAP,
  115. sec_mask, BT_PSM_BNEP, BTM_SEC_PROTO_BNEP,
  116. UUID_SERVCLASS_NAP))) {
  117. PAN_TRACE_ERROR("PAN Security Registration failed for PANU");
  118. }
  119. }
  120. #endif
  121. #if (PAN_SUPPORTS_ROLE_GN == TRUE)
  122. if (uuid == UUID_SERVCLASS_GN) {
  123. if ((!BTM_SetSecurityLevel(true, p_name, BTM_SEC_SERVICE_BNEP_GN, sec_mask,
  124. BT_PSM_BNEP, BTM_SEC_PROTO_BNEP,
  125. UUID_SERVCLASS_GN)) ||
  126. (!BTM_SetSecurityLevel(false, p_name, BTM_SEC_SERVICE_BNEP_GN, sec_mask,
  127. BT_PSM_BNEP, BTM_SEC_PROTO_BNEP,
  128. UUID_SERVCLASS_GN))) {
  129. PAN_TRACE_ERROR("PAN Security Registration failed for GN");
  130. }
  131. }
  132. #endif
  133. #if (PAN_SUPPORTS_ROLE_PANU == TRUE)
  134. if (uuid == UUID_SERVCLASS_PANU) {
  135. if ((!BTM_SetSecurityLevel(true, p_name, BTM_SEC_SERVICE_BNEP_PANU,
  136. sec_mask, BT_PSM_BNEP, BTM_SEC_PROTO_BNEP,
  137. UUID_SERVCLASS_PANU)) ||
  138. (!BTM_SetSecurityLevel(false, p_name, BTM_SEC_SERVICE_BNEP_PANU,
  139. sec_mask, BT_PSM_BNEP, BTM_SEC_PROTO_BNEP,
  140. UUID_SERVCLASS_PANU))) {
  141. PAN_TRACE_ERROR("PAN Security Registration failed for PANU");
  142. }
  143. }
  144. #endif
  145. /* Make the service browsable */
  146. SDP_AddUuidSequence(sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse_list);
  147. return sdp_handle;
  148. }
  149. /*******************************************************************************
  150. *
  151. * Function pan_allocate_pcb
  152. *
  153. * Description
  154. *
  155. * Returns
  156. *
  157. ******************************************************************************/
  158. tPAN_CONN* pan_allocate_pcb(const RawAddress& p_bda, uint16_t handle) {
  159. uint16_t i;
  160. for (i = 0; i < MAX_PAN_CONNS; i++) {
  161. if (pan_cb.pcb[i].con_state != PAN_STATE_IDLE &&
  162. pan_cb.pcb[i].handle == handle)
  163. return NULL;
  164. }
  165. for (i = 0; i < MAX_PAN_CONNS; i++) {
  166. if (pan_cb.pcb[i].con_state != PAN_STATE_IDLE &&
  167. pan_cb.pcb[i].rem_bda == p_bda)
  168. return NULL;
  169. }
  170. for (i = 0; i < MAX_PAN_CONNS; i++) {
  171. if (pan_cb.pcb[i].con_state == PAN_STATE_IDLE) {
  172. memset(&(pan_cb.pcb[i]), 0, sizeof(tPAN_CONN));
  173. pan_cb.pcb[i].rem_bda = p_bda;
  174. pan_cb.pcb[i].handle = handle;
  175. return &(pan_cb.pcb[i]);
  176. }
  177. }
  178. return NULL;
  179. }
  180. /*******************************************************************************
  181. *
  182. * Function pan_get_pcb_by_handle
  183. *
  184. * Description
  185. *
  186. * Returns
  187. *
  188. ******************************************************************************/
  189. tPAN_CONN* pan_get_pcb_by_handle(uint16_t handle) {
  190. uint16_t i;
  191. for (i = 0; i < MAX_PAN_CONNS; i++) {
  192. if (pan_cb.pcb[i].con_state != PAN_STATE_IDLE &&
  193. pan_cb.pcb[i].handle == handle)
  194. return &(pan_cb.pcb[i]);
  195. }
  196. return NULL;
  197. }
  198. /*******************************************************************************
  199. *
  200. * Function pan_get_pcb_by_addr
  201. *
  202. * Description
  203. *
  204. * Returns
  205. *
  206. ******************************************************************************/
  207. tPAN_CONN* pan_get_pcb_by_addr(const RawAddress& p_bda) {
  208. uint16_t i;
  209. for (i = 0; i < MAX_PAN_CONNS; i++) {
  210. if (pan_cb.pcb[i].con_state == PAN_STATE_IDLE) continue;
  211. if (pan_cb.pcb[i].rem_bda == p_bda) return &(pan_cb.pcb[i]);
  212. /*
  213. if (pan_cb.pcb[i].mfilter_present &&
  214. p_bda == pan_cb.pcb[i].multi_cast_bridge)
  215. return &(pan_cb.pcb[i]);
  216. */
  217. }
  218. return NULL;
  219. }
  220. /*******************************************************************************
  221. *
  222. * Function pan_close_all_connections
  223. *
  224. * Description
  225. *
  226. * Returns void
  227. *
  228. ******************************************************************************/
  229. void pan_close_all_connections(void) {
  230. uint16_t i;
  231. for (i = 0; i < MAX_PAN_CONNS; i++) {
  232. if (pan_cb.pcb[i].con_state != PAN_STATE_IDLE) {
  233. BNEP_Disconnect(pan_cb.pcb[i].handle);
  234. pan_cb.pcb[i].con_state = PAN_STATE_IDLE;
  235. }
  236. }
  237. pan_cb.active_role = PAN_ROLE_INACTIVE;
  238. pan_cb.num_conns = 0;
  239. return;
  240. }
  241. /*******************************************************************************
  242. *
  243. * Function pan_release_pcb
  244. *
  245. * Description This function releases a PCB.
  246. *
  247. * Returns void
  248. *
  249. ******************************************************************************/
  250. void pan_release_pcb(tPAN_CONN* p_pcb) {
  251. /* Drop any response pointer we may be holding */
  252. memset(p_pcb, 0, sizeof(tPAN_CONN));
  253. p_pcb->con_state = PAN_STATE_IDLE;
  254. }
  255. /*******************************************************************************
  256. *
  257. * Function pan_dump_status
  258. *
  259. * Description This function dumps the pan control block and connection
  260. * blocks information
  261. *
  262. * Returns none
  263. *
  264. ******************************************************************************/
  265. void pan_dump_status(void) {
  266. #if (PAN_SUPPORTS_DEBUG_DUMP == TRUE)
  267. uint16_t i;
  268. tPAN_CONN* p_pcb;
  269. PAN_TRACE_DEBUG("PAN role %x, active role %d, num_conns %d", pan_cb.role,
  270. pan_cb.active_role, pan_cb.num_conns);
  271. for (i = 0, p_pcb = pan_cb.pcb; i < MAX_PAN_CONNS; i++, p_pcb++) {
  272. VLOG(1) << +i << " state:" << p_pcb->con_state
  273. << ", handle:" << p_pcb->handle << ", src" << p_pcb->src_uuid
  274. << ", BD:" << p_pcb->rem_bda;
  275. }
  276. #endif
  277. }