]> granicus.if.org Git - esp-idf/blob - examples/08_bt_sdp/components/bluedroid_demos/app_project/SampleBtSdp.c
component/bt: legacy bt API provide
[esp-idf] / examples / 08_bt_sdp / components / bluedroid_demos / app_project / SampleBtSdp.c
1 #include <stdint.h>
2 #include <stdbool.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6
7 #include "freertos/FreeRTOS.h"
8 #include "freertos/task.h"
9
10 #include "bt_app_common.h"
11 #include "btif_stack_manager.h"
12 #include "btif_sdp.h"
13 #include "bt_gap_api.h"
14
15 #include "bta_api.h"
16
17 typedef enum {
18     BT_APP_EVT_STACK_ON,
19     BT_APP_EVT_STACK_OFF,
20     BT_APP_EVT
21 } tBT_APP_EVT;
22
23 typedef union {
24     uint32_t dummy;
25 } tBT_APP_EVT_DATA;
26
27 static void bt_stack_state_changed(bt_state_t state);
28 static int bt_sdp_add_record(void);
29 static void bt_sdp_search_complete(bt_status_t status, bt_bdaddr_t *bd_addr, uint8_t* uuid, int num_records, bluetooth_sdp_record *records);
30
31 // static bt_bdaddr_t peer_bd_addr = {{0x00, 0x1b, 0xdc, 0x08, 0x0f, 0xe7}};
32 static bt_bdaddr_t peer_bd_addr = {{0xfc, 0x3f, 0x7c, 0xf1, 0x2c, 0x78}};
33
34 /* root browse
35 static const uint8_t target_uuid[16] = { 0x00, 0x00, 0x10, 0x02, 0x00, 0x00, 0x10, 0x00,
36                                          0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB };
37 */
38
39 /* UUID_MAP_MAS */
40 static const uint8_t  target_uuid[] = {0x00, 0x00, 0x11, 0x32, 0x00, 0x00, 0x10, 0x00,
41                                         0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB};
42
43 /* UUID AUDIO Source */
44 /*
45 static const uint8_t  target_uuid[] = {0x00, 0x00, 0x11, 0x0A, 0x00, 0x00, 0x10, 0x00,
46                                         0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB};
47 */
48
49 static bt_callbacks_t bt_callbacks = {
50     bt_stack_state_changed
51 };
52
53 static btsdp_callbacks_t btsdp_callbacks = {
54     bt_sdp_search_complete
55 };
56     
57 static void bt_app_stack_evt(UINT16 event, char *p_param)
58 {
59     switch (event) {
60     case BT_APP_EVT_STACK_ON: {
61         char *dev_name = "SDP_SERVER_CLIENT";
62         BTM_SetTraceLevel(BT_TRACE_LEVEL_DEBUG);
63         BTA_DmSetDeviceName(dev_name);
64     
65         esp_bt_gap_set_scan_mode(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
66         BTIF_SdpInit(&btsdp_callbacks);
67
68         vTaskDelay(1000 / portTICK_PERIOD_MS);
69         bt_sdp_add_record();
70
71         vTaskDelay(20000/portTICK_PERIOD_MS);
72         BTIF_SdpSearch(&peer_bd_addr, target_uuid);
73     }
74         break;
75     default:
76         break;
77     }
78 }
79
80 static void bt_stack_evt(tBT_APP_EVT event, tBT_APP_EVT_DATA *p_data)
81 {
82     LOG_ERROR("bt_stack_evt: %d\n", (uint16_t)event);
83     bt_app_transfer_context(bt_app_stack_evt, (uint16_t)event, 
84            (void *)p_data, sizeof(tBT_APP_EVT_DATA), NULL);
85 }
86
87 static void bt_stack_state_changed(bt_state_t state)
88 {
89     if (state == BT_STATE_ON) {
90         bt_stack_evt(BT_APP_EVT_STACK_ON, NULL);
91     }
92 }
93
94 static int bt_sdp_add_record(void)
95 {
96     int handle;
97     bluetooth_sdp_sap_record sap_svr;
98     memset (&sap_svr, 0, sizeof(bluetooth_sdp_sap_record));
99     
100     sap_svr.hdr.type = SDP_TYPE_SAP_SERVER;
101     sap_svr.hdr.rfcomm_channel_number = 2;
102     sap_svr.hdr.service_name = "SIM ACCESS";
103     sap_svr.hdr.service_name_length = 10;
104     sap_svr.hdr.profile_version = 0x0100;
105
106     BTIF_SdpCreateRecord((bluetooth_sdp_record *)(&sap_svr), &handle);
107     return handle;
108 }
109
110 static void bt_sdp_search_complete(bt_status_t status, bt_bdaddr_t *bd_addr, uint8_t* uuid, int num_records, bluetooth_sdp_record *records)
111 {
112     uint8_t *addr = bd_addr->address;
113     bluetooth_sdp_hdr_overlay *p = &records->mas.hdr;
114     LOG_ERROR("sdp search cmpl: st %d, bd_addr: %02x:%02x:%02x:%02x:%02x:%02x, records %d\n",
115               status, addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], num_records);
116     if (p->service_name_length > 0) {
117         LOG_ERROR("service name: %s\n", p->service_name);
118     }
119     LOG_ERROR("rfc_chl_num %d, l2cap_psm %d, version %02x\n",
120               p->rfcomm_channel_number, p->l2cap_psm, p->profile_version);
121 #if 0
122     uint8_t *addr = bd_addr->address;
123     bluetooth_sdp_hdr_overlay *p = &records->hdr;
124     LOG_ERROR("sdp search cmpl: st %d, bd_addr: %02x:%02x:%02x:%02x:%02x:%02x, records %d, len:%d\n",
125               status, addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], num_records, p->user1_ptr_len);
126     if (p->service_name_length > 0) {
127         LOG_ERROR("service name: %s\n", p->service_name);
128     }
129 #endif
130 }
131
132 void app_main_entry(void)
133 {
134     bt_status_t stat;
135     stat = BTIF_InitStack(&bt_callbacks);
136     if (stat == BT_STATUS_SUCCESS) {
137         BTIF_EnableStack();
138     }
139 }
140