]> granicus.if.org Git - esp-idf/commitdiff
component bt: Added the client demo to the bt project
authorYulong <huangyulong@espressif.com>
Thu, 17 Nov 2016 11:37:56 +0000 (06:37 -0500)
committerYulong <huangyulong@espressif.com>
Thu, 17 Nov 2016 11:37:56 +0000 (06:37 -0500)
14 files changed:
components/bt/bluedroid/api/esp_gap_ble_api.c
components/bt/bluedroid/api/include/esp_gap_ble_api.h
components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c
components/bt/bluedroid/btc/profile/std/include/dis_api.h
examples/06_bluedroid_demos/components/bluedroid_demos/app_client_profiles/battery_c/battery_c.c
examples/06_bluedroid_demos/components/bluedroid_demos/app_project/SampleClientProject.c
examples/09_arch_client/Makefile [new file with mode: 0644]
examples/09_arch_client/README.rst [new file with mode: 0644]
examples/09_arch_client/components/bluedroid_demos/app_core/bt_app_core.c [new file with mode: 0644]
examples/09_arch_client/components/bluedroid_demos/app_project/Arch_SimpleClientProject.c [new file with mode: 0644]
examples/09_arch_client/components/bluedroid_demos/component.mk [new file with mode: 0644]
examples/09_arch_client/components/bluedroid_demos/include/bt_app_common.h [new file with mode: 0644]
examples/09_arch_client/main/component.mk [new file with mode: 0644]
examples/09_arch_client/main/demo_main.c [new file with mode: 0644]

index b7a347b5d30151d8a59babba88bd2abedbcbf0d5..a31e7ff283112851119045a72d33381548b51bdb 100644 (file)
@@ -70,7 +70,7 @@ esp_err_t esp_ble_gap_start_scanning(uint32_t duration)
 
        msg.sig = BTC_SIG_API_CALL;
        msg.pid = BTC_PID_GAP_BLE;
-       msg.act = BTC_GAP_BLE_ACT_SET_SCAN_PARAM;
+       msg.act = BTC_GAP_BLE_ACT_START_SCAN;
        arg.duration = duration;
 
        return (btc_transfer_context(&msg, &arg, sizeof(esp_ble_gap_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
index 6fd356c0d7be9f74e6c81bb7f5637e1e48029c73..84fa4d26017b5ca8bf197d7e55dd6a6c408abd91 100644 (file)
@@ -74,6 +74,24 @@ typedef struct {
        uint8_t                    flag;
 } esp_ble_adv_data_t;
 
+/// Own BD address source of the device
+typedef enum 
+{
+   /// Public Address
+   ESP_PUBLIC_ADDR,
+   /// Provided random address
+   ESP_PROVIDED_RND_ADDR,
+   /// Provided static random address
+   ESP_GEN_STATIC_RND_ADDR,
+   /// Generated resolvable private random address
+   ESP_GEN_RSLV_ADDR,
+   /// Generated non-resolvable private random address
+   ESP_GEN_NON_RSLV_ADDR,
+   /// Provided Reconnection address
+   ESP_PROVIDED_RECON_ADDR,
+}esp_ble_own_addr_src_t;
+
+
 typedef enum {
        BLE_SCAN_TYPE_PASSIVE   =       0x0,
        BLE_SCAN_TYPE_ACTIVE    =       0x1,
index c6e2aa98ccaa7f0f7d5f825216521d01be15ee4f..06172945584808387114ccc6fb6e11afe8a58a78 100644 (file)
@@ -472,7 +472,7 @@ static void btc_search_callback(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data
        }
        case BTA_DM_INQ_CMPL_EVT: {
                param.scan_rst.num_resps = p_data->inq_cmpl.num_resps;
-               LOG_ERROR("%s  BLE observe complete. Num Resp %d", __FUNCTION__,p_data->inq_cmpl.num_resps);
+               LOG_ERROR("%s  BLE observe complete. Num Resp %d\n", __FUNCTION__,p_data->inq_cmpl.num_resps);
                break;
        }
        default:
index 95bf24ac59c7d5cc0e3b8992a0b4c103330ccaa4..5c8d5243fa15f16a43de05198339b82b5b018929 100644 (file)
@@ -28,7 +28,7 @@
 #include "bt_target.h"
 #include "gatt_api.h"
 #include "gattdefs.h"
-#include "esp_gatt_api.h"
+#include "esp_gatts_api.h"
 
 #define DIS_SUCCESS             GATT_SUCCESS
 #define DIS_ILLEGAL_PARAM       GATT_ILLEGAL_PARAMETER
index 9500f77090096379990f44fddc8355f22d7d315b..2f146e967e2bda6ebbbc546016fa1d967cc0ee78 100644 (file)
@@ -31,7 +31,7 @@
 #include "btm_api.h"
 #include "bt_types.h"
 #include "gattc_profile.h"
-#include "esp_gatt_api.h"
+#include "esp_gatts_api.h"
 
 #define BT_BD_ADDR_STR         "%02x:%02x:%02x:%02x:%02x:%02x"
 #define BT_BD_ADDR_HEX(addr)   addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]
index 6f3be9a0f10dea676c4bc2cb530c64f0fc386a07..5eb616a36e88dbb31fac94609998227f07c6e685 100644 (file)
@@ -184,7 +184,7 @@ static void bta_scan_result_callback(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH* p
     char dev_name[32];
     tBTA_GATT_TRANSPORT transport = BTA_GATT_TRANSPORT_LE;
     //char obj_name[] = "Find Me";
-    char obj_name[] = "SimpleBLEPeripheral";
+    char obj_name[] = "SimpleBLEClient";
     uint8_t dev_name_len;
 
     switch (event)
@@ -224,7 +224,7 @@ static void bta_scan_result_callback(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH* p
             LOG_ERROR("connect the device "BT_BD_ADDR_STR", client_if=%d\n", 
                 BT_BD_ADDR_HEX(obj_addr), client_if); 
 
-/* scan complete, start connect*/
+               /* scan complete, start connect*/
             BTA_GATTC_Open(client_if, obj_addr, true, transport); 
         }
         break;
diff --git a/examples/09_arch_client/Makefile b/examples/09_arch_client/Makefile
new file mode 100644 (file)
index 0000000..1e91bbb
--- /dev/null
@@ -0,0 +1,11 @@
+#
+# This is a project Makefile. It is assumed the directory this Makefile resides in is a
+# project subdirectory.
+#
+
+PROJECT_NAME := bluedroid_demos
+
+COMPONENT_ADD_INCLUDEDIRS := components/include
+
+include $(IDF_PATH)/make/project.mk
+
diff --git a/examples/09_arch_client/README.rst b/examples/09_arch_client/README.rst
new file mode 100644 (file)
index 0000000..3b074c7
--- /dev/null
@@ -0,0 +1,10 @@
+ESP-IDF Blufi demo
+=======================
+
+This is the demo for bluetooth config wifi connection to ap.
+
+attentions:
+    1. Please use the BLEDEMO APK
+    2. As the GATTServer start a litte slowly, so Please Wait for a period, then use apk scan the apk util find a random address devices named Espressif_008
+    3. Just a unstable version..
+
diff --git a/examples/09_arch_client/components/bluedroid_demos/app_core/bt_app_core.c b/examples/09_arch_client/components/bluedroid_demos/app_core/bt_app_core.c
new file mode 100644 (file)
index 0000000..68e97fb
--- /dev/null
@@ -0,0 +1,418 @@
+// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include <stdint.h>
+#include <string.h>
+#include <stdbool.h>
+
+#include "fixed_queue.h"
+#include "gki.h"
+#include "bt_defs.h"
+#include "bt_trace.h"
+#include "bt_types.h"
+#include "allocator.h"
+
+#include "bta_api.h"
+#include "bta_gatt_api.h"
+#include "bt_app_common.h"
+
+#include "controller.h"
+//#include "prf_defs.h"
+#include "hash_map.h"
+#include "hash_functions.h"
+#include "alarm.h"
+
+#include "thread.h"
+#include "bt_app_common.h"
+//#include "gattc_profile.h"
+#include "smp_int.h"
+#include "smp_api.h"
+
+extern void gattc_client_test(void);
+
+static fixed_queue_t *bta_app_msg_queue;
+fixed_queue_t *bt_app_general_alarm_queue;
+hash_map_t *bt_app_general_alarm_hash_map;
+pthread_mutex_t bt_app_general_alarm_lock;
+static const size_t BT_APP_GENERAL_ALARM_HASH_MAP_SIZE = 10;
+
+xQueueHandle xBtaApp1Queue;
+xTaskHandle xBtaApp1TaskHandle;
+
+#define BT_APP_TTYPE_MAIN_ENTRY   (1)
+static TIMER_LIST_ENT main_boot_tle;
+
+tSMP_CB smp_cmd;
+
+static void bt_app_context_switched(void *p_msg);
+static void bt_app_send_msg(void *p_msg);
+static void bt_app_task_handler(void *arg);
+static void bta_app_msg_ready(fixed_queue_t *queue);
+static void bt_app_task_shut_down(void);
+
+static void bt_app_general_alarm_ready(fixed_queue_t *queue);
+static void bt_app_general_alarm_process(TIMER_LIST_ENT *p_tle);
+void bt_app_start_timer(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_sec);
+
+//extern void ble_test_conn(void);
+//extern void bt_test_start_inquiry(void);
+extern void ble_server_test(void);
+
+static void bt_app_task_handler(void *arg)
+{
+    BtTaskEvt_t e;
+    UINT8 button_msg[2] = {0x01,0x00};
+    for (;;) {
+        if (pdTRUE == xQueueReceive(xBtaApp1Queue, &e, (portTickType)portMAX_DELAY)) {
+            if (e.sig == 0xff) {
+                fixed_queue_process(bta_app_msg_queue);
+                fixed_queue_process(bt_app_general_alarm_queue);
+            }
+#if (BUT_PROFILE_CFG)
+               //      else if(e.sig == BUTTON_PRESS_EVT){
+               //      LOG_ERROR("button_press_event come in,button_value=%x\n",e.par);
+               //  button_msg[1] = e.par;
+        //  button_msg_notify(2,button_msg);   
+
+
+       //}
+#endif ///BUT_PROFILE_CFG
+
+        }
+    }
+}
+
+static void bt_app_task_post(void)
+{
+     BtTaskEvt_t evt;
+
+     evt.sig = 0xff;
+     evt.par = 0;
+
+     if (xQueueSend(xBtaApp1Queue, &evt, 10/portTICK_RATE_MS) != pdTRUE) {
+         ets_printf("btdm_post failed\n");
+     }
+
+}
+
+
+static void bta_app_msg_ready(fixed_queue_t *queue) {
+    BT_HDR *p_msg;
+    while (!fixed_queue_is_empty(queue)) {
+        p_msg = (BT_HDR *)fixed_queue_dequeue(queue);
+        LOG_ERROR("bta_app_msg_ready, evt: %d\n", p_msg->event);
+        switch (p_msg->event) {
+        case BT_EVT_APP_CONTEXT_SWITCH:
+            bt_app_context_switched(p_msg);
+            break;
+        default:
+            LOG_ERROR("unhandled BT_APP event (%d)\n", p_msg->event & BT_EVT_MASK);
+            break;
+        }
+        GKI_freebuf(p_msg);
+    }
+}
+
+static void bt_app_context_switched(void *p_msg)
+{
+    tBTAPP_CONTEXT_SWITCH_CBACK *p = (tBTAPP_CONTEXT_SWITCH_CBACK *) p_msg;
+    
+    if (p->p_cb)
+        p->p_cb(p->event, p->p_param);
+}
+
+static void bt_app_send_msg(void *p_msg)
+{
+    if (bta_app_msg_queue) {
+        fixed_queue_enqueue(bta_app_msg_queue, p_msg);
+        //ke_event_set(KE_EVENT_BT_APP_TASK);
+        bt_app_task_post();
+    }
+}
+
+bt_status_t bt_app_transfer_context (tBTAPP_CBACK *p_cback, UINT16 event, char* p_params, int param_len, tBTAPP_COPY_CBACK *p_copy_cback)
+{
+    tBTAPP_CONTEXT_SWITCH_CBACK *p_msg;
+
+    LOG_ERROR("btapp_transfer_context evt %d, len %d", event, param_len);
+
+    /* allocate and send message that will be executed in btif context */
+    if ((p_msg = (tBTAPP_CONTEXT_SWITCH_CBACK *) GKI_getbuf(sizeof(tBTAPP_CONTEXT_SWITCH_CBACK) + param_len)) != NULL)
+    {
+        p_msg->hdr.event = BT_EVT_APP_CONTEXT_SWITCH; /* internal event */
+        p_msg->p_cb = p_cback;
+
+        p_msg->event = event;                         /* callback event */
+
+        /* check if caller has provided a copy callback to do the deep copy */
+        if (p_copy_cback)
+        {
+            p_copy_cback(event, p_msg->p_param, p_params);
+        }
+        else if (p_params)
+        {
+            memcpy(p_msg->p_param, p_params, param_len);  /* callback parameter data */
+        }
+
+        bt_app_send_msg(p_msg);
+        return BT_STATUS_SUCCESS;
+    }
+    else
+    {
+        /* let caller deal with a failed allocation */
+        return BT_STATUS_NOMEM;
+    }
+}
+
+void bt_app_task_start_up(void)
+{
+    bta_app_msg_queue = fixed_queue_new(SIZE_MAX);
+    if (bta_app_msg_queue == NULL)
+        goto error_exit;
+    //ke_event_callback_set(KE_EVENT_BT_APP_TASK, &bt_app_task_handler);
+
+    xBtaApp1Queue = xQueueCreate(3, sizeof(BtTaskEvt_t));
+    xTaskCreate(bt_app_task_handler, "BtaApp1T", 8192, NULL, configMAX_PRIORITIES - 3, xBtaApp1TaskHandle);
+
+    fixed_queue_register_dequeue(bta_app_msg_queue, bta_app_msg_ready);
+
+    bt_app_general_alarm_hash_map = hash_map_new(BT_APP_GENERAL_ALARM_HASH_MAP_SIZE,
+            hash_function_pointer, NULL, (data_free_fn)osi_alarm_free, NULL);
+    if (bt_app_general_alarm_hash_map == NULL)
+        goto error_exit;
+
+    pthread_mutex_init(&bt_app_general_alarm_lock, NULL);
+
+    bt_app_general_alarm_queue = fixed_queue_new(SIZE_MAX);
+    if (bt_app_general_alarm_queue == NULL)
+        goto error_exit;
+    fixed_queue_register_dequeue(bt_app_general_alarm_queue, bt_app_general_alarm_ready);
+
+    memset(&main_boot_tle, 0, sizeof(TIMER_LIST_ENT));
+    return;
+
+error_exit:
+    LOG_ERROR("%s Unable to allocate resources for bt_app\n", __func__);
+    bt_app_task_shut_down();
+}
+
+static void bt_app_task_shut_down(void)
+{
+    fixed_queue_unregister_dequeue(bta_app_msg_queue);
+    fixed_queue_free(bta_app_msg_queue, NULL);
+    bta_app_msg_queue = NULL;
+
+    // todo: hash map, pthread_mutex...
+    fixed_queue_unregister_dequeue(bt_app_general_alarm_queue);
+
+    vTaskDelete(xBtaApp1TaskHandle);
+    vQueueDelete(xBtaApp1Queue);
+}
+
+
+static void bt_app_dm_data_copy(uint16_t event, char *dst, char *src)
+{
+    tBTA_DM_SEC *dst_dm_sec = (tBTA_DM_SEC*)dst;
+    tBTA_DM_SEC *src_dm_sec = (tBTA_DM_SEC*)src;
+
+    if (!src_dm_sec)
+        return;
+
+    assert(dst_dm_sec);
+    memcpy(dst_dm_sec, src_dm_sec, sizeof(tBTA_DM_SEC));
+
+    if (event == BTA_DM_BLE_KEY_EVT)
+    {
+        dst_dm_sec->ble_key.p_key_value = osi_malloc(sizeof(tBTM_LE_KEY_VALUE));
+        assert(src_dm_sec->ble_key.p_key_value);
+        assert(dst_dm_sec->ble_key.p_key_value);
+        memcpy(dst_dm_sec->ble_key.p_key_value, src_dm_sec->ble_key.p_key_value, sizeof(tBTM_LE_KEY_VALUE));
+    }
+}
+
+static void bt_app_dm_data_free(uint16_t event, tBTA_DM_SEC *dm_sec)
+{
+    if (event == BTA_DM_BLE_KEY_EVT)
+        osi_free(dm_sec->ble_key.p_key_value);
+}
+
+static void bt_app_dm_upstreams_evt(UINT16 event, char *p_param)
+{
+    tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param;
+    switch (event) {
+    case BTA_DM_ENABLE_EVT: {
+
+//        BTA_DmSetDeviceName("ijiazu");
+
+  
+
+
+        /*set connectable,discoverable, pairable and paired only modes of local device*/
+        tBTA_DM_DISC disc_mode =  BTA_DM_BLE_GENERAL_DISCOVERABLE;
+        tBTA_DM_CONN conn_mode =  BTA_DM_BLE_CONNECTABLE;
+        //BTA_DmSetVisibility(disc_mode, conn_mode, (UINT8)BTA_DM_NON_PAIRABLE, (UINT8)BTA_DM_CONN_ALL);
+
+#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
+        /* Enable local privacy */
+        //BTA_DmBleConfigLocalPrivacy(BLE_LOCAL_PRIVACY_ENABLED);
+        do {
+            const controller_t *controller = controller_get_interface();
+            char bdstr[18];
+            bdaddr_to_string(controller->get_address(), bdstr, sizeof(bdstr));
+            LOG_ERROR("BDA is: %s\n", bdstr);
+        } while (0);
+#endif        
+    }
+       break;
+       case BTA_DM_BLE_SEC_REQ_EVT:
+               
+               smp_cb.local_io_capability = 0x03;              //no input no output
+               smp_cb.loc_oob_flag = 0x00;             //oob data not present
+               smp_cb.loc_auth_req = 0x01;
+               smp_cb.loc_enc_size = 0x10;
+               smp_cb.local_i_key = 0x01;
+               smp_cb.local_r_key = 0x01;              //1101
+               
+               //memcpy(smp_cb.pairing_bda,p_data->ble_req.bd_addr,0x06);
+
+               smp_sm_event(&smp_cb,SMP_PAIRING_REQ_EVT,NULL);
+               //smp_send_cmd(SMP_OPCODE_PAIRING_RSP,&smp_cb);
+               //smp_generate_srand_mrand_confirm(&smp_cb,NULL);
+               //smp_set_state(SMP_STATE_PAIR_REQ_RSP,SMP_BR_PAIRING_REQ_EVT);
+               //BTA_DmConfirm(p_data->ble_req.bd_addr,true);
+                       break;
+       case BTA_DM_BLE_KEY_EVT:
+               if(p_data->ble_key.key_type == BTM_LE_KEY_PENC)
+               {
+                       smp_set_state(SMP_STATE_IDLE);
+               }
+               break;
+       default:
+               break;
+    }
+
+    bt_app_dm_data_free(event, p_data);
+}
+
+static void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC* p_data)
+{
+    LOG_ERROR("bte_dm_evt: %d\n", (uint16_t)event);
+    bt_app_transfer_context(bt_app_dm_upstreams_evt, (uint16_t)event, 
+           (void *)p_data, sizeof(tBTA_DM_SEC), bt_app_dm_data_copy);
+}
+
+void bt_app_init_ok(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param)
+{
+    BTA_EnableBluetooth(bte_dm_evt);
+    vTaskDelay(1000 / portTICK_PERIOD_MS);
+    bt_app_start_timer(&main_boot_tle, BT_APP_TTYPE_MAIN_ENTRY, 8);
+}
+
+/* Alarm timer */
+static void bt_app_general_alarm_cb(void *data) {
+    assert(data != NULL);
+    TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;
+
+    fixed_queue_enqueue(bt_app_general_alarm_queue, p_tle);
+    //ke_event_set(KE_EVENT_BT_APP_TASK);
+    bt_app_task_post();
+}
+
+void bt_app_start_timer(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_sec) {
+   osi_alarm_t *alarm = NULL;
+
+    assert(p_tle != NULL);
+
+    // Get the alarm for the timer list entry.
+    pthread_mutex_lock(&bt_app_general_alarm_lock);
+    if (!hash_map_has_key(bt_app_general_alarm_hash_map, p_tle)) {
+        alarm = osi_alarm_new("bt_app", bt_app_general_alarm_cb, (void *)p_tle, 0);
+        hash_map_set(bt_app_general_alarm_hash_map, p_tle, alarm);
+    }
+    pthread_mutex_unlock(&bt_app_general_alarm_lock);
+    
+       pthread_mutex_lock(&bt_app_general_alarm_lock);
+    alarm = hash_map_get(bt_app_general_alarm_hash_map, p_tle);
+    pthread_mutex_unlock(&bt_app_general_alarm_lock);
+    if (alarm == NULL) {
+        LOG_ERROR("%s Unable to create alarm\n", __func__);
+
+        return;
+    }
+
+    osi_alarm_cancel(alarm);
+
+    p_tle->event = type;
+    // NOTE: This value is in seconds but stored in a ticks field.
+    p_tle->ticks = timeout_sec;
+    p_tle->in_use = TRUE;
+    osi_alarm_set(alarm, (period_ms_t)(timeout_sec * 1000));
+}
+
+void bt_app_stop_timer(TIMER_LIST_ENT *p_tle)
+{
+    assert(p_tle != NULL);
+
+    if (p_tle->in_use == FALSE)
+        return;
+    p_tle->in_use = FALSE;
+
+    // Get the alarm for the timer list entry.
+   osi_alarm_t *alarm = hash_map_get(bt_app_general_alarm_hash_map, p_tle);
+    if (alarm == NULL) {
+        LOG_WARN("%s Unable to find expected alarm in hashmap\n", __func__);
+        return;
+    }
+    osi_alarm_cancel(alarm);
+}
+
+static void bt_app_general_alarm_process(TIMER_LIST_ENT *p_tle)
+{
+    assert(p_tle != NULL);
+    LOG_ERROR("general_alarm_process\n");
+    switch (p_tle->event) {
+    case BT_APP_TTYPE_MAIN_ENTRY:
+        LOG_ERROR("BT_APP main boot**********\n");
+
+      //  ble_test_conn();
+  //      ble_server_test();
+
+  //     bt_test_start_inquiry();
+        /*set connectable,discoverable, pairable and paired only modes of local device*/
+      //  tBTA_DM_DISC disc_mode = BTA_DM_BLE_GENERAL_DISCOVERABLE;
+     //        tBTA_DM_CONN conn_mode = BTA_DM_BLE_CONNECTABLE;
+      //  BTA_DmSetVisibility(disc_mode, conn_mode, (UINT8)BTA_DM_NON_PAIRABLE, (UINT8)BTA_DM_CONN_ALL);
+
+       //gatts_server_test();
+       gattc_client_test();
+        break;
+    }
+
+}
+
+static void bt_app_general_alarm_ready(fixed_queue_t *queue)
+{
+    TIMER_LIST_ENT *p_tle;
+
+    while (!fixed_queue_is_empty(queue)) {
+        p_tle = (TIMER_LIST_ENT *)fixed_queue_dequeue(queue);
+        bt_app_general_alarm_process(p_tle);
+    }
+}
+
+void bt_app_core_start(void) {
+       bt_app_transfer_context(bt_app_init_ok, 0, NULL, 0, NULL);
+}
+
diff --git a/examples/09_arch_client/components/bluedroid_demos/app_project/Arch_SimpleClientProject.c b/examples/09_arch_client/components/bluedroid_demos/app_project/Arch_SimpleClientProject.c
new file mode 100644 (file)
index 0000000..bcf8128
--- /dev/null
@@ -0,0 +1,406 @@
+// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+
+
+/****************************************************************************
+*
+* This file is for gatt client. It can scan ble device, connect one device, 
+*
+****************************************************************************/
+
+#include <stdint.h>
+#include <string.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include "controller.h"
+               
+#include "bt_trace.h"
+#include "bt_types.h"
+#include "btm_api.h"
+#include "bta_api.h"
+#include "bta_gatt_api.h"
+#include "esp_gap_ble_api.h"
+#include "esp_gattc_api.h"
+
+#define BT_BD_ADDR_STR         "%02x:%02x:%02x:%02x:%02x:%02x"
+#define BT_BD_ADDR_HEX(addr)   addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]
+
+tBTA_GATTC_IF client_if;
+BD_ADDR obj_addr;
+static unsigned char BASE_UUID[16] = {
+    0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
+    0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+    };
+
+static esp_ble_scan_params_t ble_scan_params =
+{
+       .scan_type                      = BLE_SCAN_TYPE_ACTIVE,
+       .own_addr_type          = ESP_PUBLIC_ADDR,
+       .scan_filter_policy             = BLE_SCAN_FILTER_ALLOW_ALL,
+       .scan_interval                  = 0x50,
+       .scan_window                    = 0x30
+};
+
+
+static void esp_scan_result_cb(uint32_t event, void *scan_param);
+
+static void esp_gattc_result_cb(uint32_t event, void *gattc_param);
+
+                  
+int arch_uuidType(unsigned char* p_uuid)
+{
+    int i = 0;
+    int match = 0;
+    int all_zero = 1;
+                
+    for(i = 0; i != 16; ++i)
+    {
+        if (i == 12 || i == 13)
+            continue;
+                              
+        if (p_uuid[i] == BASE_UUID[i])
+            ++match;
+                                        
+        if (p_uuid[i] != 0)
+            all_zero = 0;
+    }
+    if (all_zero)
+        return 0;
+    if (match == 12)
+        return LEN_UUID_32;
+    if (match == 14)
+        return LEN_UUID_16;
+    return LEN_UUID_128;
+}
+
+static void btif_to_bta_uuid(tBT_UUID *p_dest, bt_uuid_t *p_src)
+{
+    char *p_byte = (char*)p_src;
+   
+    int i = 0;
+            
+    p_dest->len = arch_uuidType(p_src->uu);
+                  
+    switch (p_dest->len)
+    {
+        case LEN_UUID_16:
+            p_dest->uu.uuid16 = (p_src->uu[13] << 8) + p_src->uu[12];
+            break;
+                                    
+        case LEN_UUID_32:
+            p_dest->uu.uuid32  = (p_src->uu[13] <<  8) + p_src->uu[12];
+            p_dest->uu.uuid32 += (p_src->uu[15] << 24) + (p_src->uu[14] << 16);
+            break;
+                                                   
+        case LEN_UUID_128:
+            for(i = 0; i != 16; ++i)
+                p_dest->uu.uuid128[i] = p_byte[i];
+                break;
+                                                            
+        default:         
+        LOG_ERROR("%s: Unknown UUID length %d!", __FUNCTION__, p_dest->len);
+        break;
+    }
+}
+
+static void esp_scan_result_cb(uint32_t event, void *param)
+{
+       switch(event)
+       {
+               case ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT:
+               {
+                       //the unit of the duration is second
+                       uint32_t duration = 10;
+                       esp_ble_gap_start_scanning(duration);
+                       break;
+               }
+               case ESP_GAP_BLE_SCAN_RESULT_EVT:
+               {
+                       esp_ble_gap_cb_param_t *scan_result = (esp_ble_gap_cb_param_t *)param;
+                       //switch(scan_result->scan_rst.search_evt)
+                       //{
+                       //      case ESP_GAP_SEARCH_DISC_BLE_RES_EVT:
+                       //              LOG_ERROR("ESP_GAP_SEARCH_DISC_BLE_RES_EVT\n");
+                       //      break;
+                       //}
+                       break;
+               }
+               //case :
+               //      break;
+               default:
+                       break;
+       }
+}
+
+
+static void esp_gattc_result_cb(uint32_t event, void *gattc_param)
+{
+       
+}
+
+
+
+/*
+uint16_t get_uuid16(tBT_UUID* p_uuid)
+{
+    if(p_uuid->len == LEN_UUID_16)
+    {
+        return p_uuid->uu.uuid16;
+    }
+    else if(p_uuid->len == LEN_UUID_128)
+    {
+        UINT16 u16;
+        UINT8 *p = &p_uuid->uu.uuid128[LEN_UUID_128 - 4];
+        STREAM_TO_UINT16(u16, p);
+        return u16;
+    }
+    else
+    {
+        return (UINT16)p_uuid->uu.uuid32;
+    }
+}
+
+//fill a GATT ID structure
+void bta_le_fill_16bits_gatt_id(UINT8 inst_id, UINT16 uuid, tBTA_GATT_ID* p_output)
+{
+    p_output->inst_id = inst_id;
+    p_output->uuid.len = LEN_UUID_16;
+    p_output->uuid.uu.uuid16 = uuid;
+}
+
+//fill a service ID structure with a 16 bits service UUID
+void bta_le_fill_16bits_srvc_id(bool is_pri, UINT8 inst_id, UINT16 srvc_uuid, tBTA_GATT_SRVC_ID* p_output)
+{
+    memset((void *)p_output, 0, sizeof(tBTA_GATT_SRVC_ID));
+    p_output->is_primary = is_pri;
+    bta_le_fill_16bits_gatt_id(inst_id, srvc_uuid, &p_output->id);
+}
+
+//fill a char ID structure with a 16 bits char UUID
+void bta_le_fill_16bits_char_id(UINT8 inst_id, UINT16 char_uuid, tBTA_GATT_ID* p_output)
+{
+    memset((void *)p_output, 0, sizeof(tBTA_GATT_ID));
+    bta_le_fill_16bits_gatt_id(inst_id, char_uuid, p_output);
+}
+*/
+/*get remote name*/
+static bool check_remote_name(tBTA_DM_INQ_RES* result, uint8_t* rmt_name, uint8_t* rmt_name_len)
+{
+    uint8_t *p_rmt_name = NULL;
+    uint8_t remote_name_len = 0;
+    
+    if (result->p_eir) {
+        p_rmt_name = BTM_CheckEirData(result->p_eir,
+            BTM_EIR_COMPLETE_LOCAL_NAME_TYPE,
+            &remote_name_len);
+        if (!p_rmt_name)
+            p_rmt_name = BTM_CheckEirData(result->p_eir,
+                BTM_EIR_SHORTENED_LOCAL_NAME_TYPE,
+                &remote_name_len);
+        if (p_rmt_name) {
+            if (remote_name_len > BD_NAME_LEN)
+                remote_name_len = BD_NAME_LEN;
+            if (rmt_name && rmt_name_len) {
+                memcpy(rmt_name, p_rmt_name, remote_name_len);
+                *(rmt_name + remote_name_len) = 0;
+                *rmt_name_len = remote_name_len;
+                }
+            return true;
+        }
+    }
+    return false;
+}
+
+/************************************************************************************
+* * Function        bta_scan_recult_callback
+* *
+* * Description     scan result.it will be called when device scaned a peer device
+* *
+* * Return          NULL
+**************************************************************************************/
+
+#if 0
+static void bta_scan_result_callback(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH* p_data)
+{
+    uint8_t len;
+    BD_ADDR bd_addr;
+    char dev_name[32];
+    tBTA_GATT_TRANSPORT transport = BTA_GATT_TRANSPORT_LE;
+    //char obj_name[] = "Find Me";
+    char obj_name[] = "SimpleBLEClient";
+    uint8_t dev_name_len;
+
+    switch (event)
+    {
+        case BTA_DM_INQ_RES_EVT:
+        {
+            LOG_ERROR("scan result: event=%d, "BT_BD_ADDR_STR", device_type=%d\n",
+                event, BT_BD_ADDR_HEX(p_data->inq_res.bd_addr), p_data->inq_res.device_type); 
+            
+            bdcpy(bd_addr, p_data->inq_res.bd_addr);
+            if (p_data->inq_res.p_eir)
+            {
+                if (BTM_CheckEirData(p_data->inq_res.p_eir, BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &len))
+                {
+                    p_data->inq_res.remt_name_not_required = TRUE;
+                }
+            }
+
+            if(check_remote_name(&(p_data->inq_res), dev_name, &dev_name_len))
+            {
+                LOG_ERROR("scan device name len=%d, name = %s\n", dev_name_len, dev_name);
+            }
+
+            if(strcmp(dev_name, obj_name) == 0)
+            {
+                bdcpy(obj_addr, bd_addr);
+                LOG_ERROR("find the device, obj_addr="BT_BD_ADDR_STR"\n", BT_BD_ADDR_HEX(obj_addr));
+//                BTA_GATTC_Open(client_if, obj_addr, true, transport);
+            }
+        }
+        break;
+
+        case BTA_DM_INQ_CMPL_EVT:
+        {
+            LOG_ERROR("%s-BLE observe complete. Num Resp %d\n", __FUNCTION__, p_data->inq_cmpl.num_resps);
+
+            LOG_ERROR("connect the device "BT_BD_ADDR_STR", client_if=%d\n", 
+                BT_BD_ADDR_HEX(obj_addr), client_if); 
+
+               /* scan complete, start connect*/
+            BTA_GATTC_Open(client_if, obj_addr, true, transport); 
+        }
+        break;
+
+        default:
+        LOG_ERROR("%s : unknown event 0x%x", __FUNCTION__, event);
+    }
+}
+
+#endif 
+
+/************************************************************************************
+* * Function        bta_scan_param_setup_cback
+* *
+* * Description     set scan param callback.it will be called after setting scan parameter
+* *
+* * Return          NULL
+**************************************************************************************/
+/*
+static void bta_scan_param_setup_cback(tGATT_IF c_client_if, tBTM_STATUS status)
+{
+    client_if = c_client_if;
+    LOG_ERROR("\nset scan params complete: status=%d, client_if=%d\n", status, client_if);
+    
+    BTA_DmBleObserve(true, 8, bta_scan_result_callback);
+}*/
+
+/************************************************************************************
+* * Function        bta_gattc_callback
+* *
+* * Description     app register callback
+* *
+* * Return          NULL
+**************************************************************************************/
+#if 0
+static void bta_gattc_callback(tBTA_GATTC_EVT event, tBTA_GATTC* p_data)
+{
+    switch (event)
+    {   
+        case BTA_GATTC_REG_EVT:
+        {  
+            tBTA_GATT_STATUS  status = p_data->reg_oper.status;
+            client_if = p_data->reg_oper.client_if;
+            LOG_ERROR("%s:register complete: event=%d, status=%d, client_if=%d\n", __FUNCTION__, event, status, client_if);
+            UINT8 scan_interval = 0x50;
+            UINT8 scan_window = 0x30;
+            tBLE_SCAN_MODE scan_mode = BTM_BLE_SCAN_MODE_ACTI;
+            
+            bac_register();
+            /*register complete,set scan parameter*/
+            BTA_DmSetBleScanParams(client_if, scan_interval, scan_window, scan_mode,
+                bta_scan_param_setup_cback);
+  
+        }
+        break;
+        
+        /*connect callback*/
+        case BTA_GATTC_OPEN_EVT:
+        {
+            
+            LOG_ERROR("\n%s:device is connected "BT_BD_ADDR_STR", client_if=%d, status=%d, connect_id=%d\n", 
+                            __FUNCTION__, BT_BD_ADDR_HEX(p_data->open.remote_bda), p_data->open.client_if,
+                             p_data->open.status, p_data->open.conn_id);
+            /*return whether the remote device is currently connected*/
+            int is_connected = BTA_DmGetConnectionState(p_data->open.remote_bda);
+            LOG_ERROR("is_connected=%d\n",is_connected);
+            /*get the energy info of the controller*/
+
+            /*read battery level*/
+            int conn_id = p_data->open.conn_id;
+
+        }
+        break;
+               
+        default:
+        LOG_ERROR("%s:unknown event: %d\n", __FUNCTION__, event);
+    }
+
+}
+
+#endif 
+
+/************************************************************************************
+* * Function        ble_client_appRegister
+* *
+* * Description     app register function
+* *
+* * Return          NULL
+**************************************************************************************/
+void ble_client_appRegister(void)
+{
+    
+    bt_uuid_t uuid;
+    tBT_UUID t_uuid;
+    memcpy(&uuid, BASE_UUID, sizeof(bt_uuid_t));
+    btif_to_bta_uuid(&t_uuid, &uuid);
+    esp_err_t status;
+    LOG_ERROR("register application\n");
+    //BTA_GATTC_AppRegister(&t_uuid, bta_gattc_callback);
+
+    //register the scan callback function to the gap moudule
+    if((status = esp_ble_gap_register_callback(esp_scan_result_cb)) == ESP_OK){
+               esp_ble_gap_set_scan_params(&ble_scan_params);
+       }else{
+               LOG_ERROR("gap register error, error code = %x\n",status);      
+       }       
+
+   //register the callback function to the gattc module
+   if ((status = esp_ble_gattc_register_callback(esp_gattc_result_cb)) != ESP_OK){\r
+       LOG_ERROR("gattc register error, error code = %x\n",status);            
+   }   
+
+
+
+   
+
+}
+
+void gattc_client_test(void)
+{
+    BTM_SetTraceLevel(BT_TRACE_LEVEL_DEBUG);
+
+    ble_client_appRegister();
+}
diff --git a/examples/09_arch_client/components/bluedroid_demos/component.mk b/examples/09_arch_client/components/bluedroid_demos/component.mk
new file mode 100644 (file)
index 0000000..81ba2de
--- /dev/null
@@ -0,0 +1,17 @@
+#
+# Main Makefile. This is basically the same as a component makefile.
+#
+# This Makefile should, at the very least, just include $(SDK_PATH)/make/component_common.mk. By default, 
+# this will take the sources in the src/ directory, compile them and link them into 
+# lib(subdirectory_name).a in the build directory. This behaviour is entirely configurable,
+# please read the ESP-IDF documents if you need to do this.
+#
+
+COMPONENT_SRCDIRS :=   \
+                       app_core                                \
+                       app_project                             \
+
+CFLAGS += -Wno-error=unused-label -Wno-error=return-type -Wno-error=missing-braces -Wno-error=pointer-sign -Wno-error=parentheses -I./include
+
+
+include $(IDF_PATH)/make/component_common.mk
diff --git a/examples/09_arch_client/components/bluedroid_demos/include/bt_app_common.h b/examples/09_arch_client/components/bluedroid_demos/include/bt_app_common.h
new file mode 100644 (file)
index 0000000..501bfcc
--- /dev/null
@@ -0,0 +1,30 @@
+#ifndef __BT_APP_COMMON_H__
+#define __BT_APP_COMMON_H__
+
+#include <stdint.h>
+#include "osi.h"
+#include "bt_common_types.h"
+#include "bt_defs.h"
+    
+/* BT APP Events */
+#define BT_EVT_APP                     (0xB000)
+#define BT_EVT_APP_CONTEXT_SWITCH      (0x0001 | BT_EVT_APP)
+
+typedef void (tBTAPP_CBACK) (uint16_t event, char *p_param);
+typedef void (tBTAPP_COPY_CBACK) (uint16_t event, char *p_dest, char *p_src);
+
+typedef struct
+{
+    BT_HDR               hdr;
+    tBTAPP_CBACK*       p_cb;    /* context switch callback */
+
+    /* parameters passed to callback */
+    UINT16               event;   /* message event id */
+    char                 p_param[0]; /* parameter area needs to be last */
+} tBTAPP_CONTEXT_SWITCH_CBACK;
+
+bt_status_t bt_app_transfer_context (tBTAPP_CBACK *p_cback, UINT16 event, char* p_params, int param_len, tBTAPP_COPY_CBACK *p_copy_cback);
+
+void bt_app_task_start_up(void);
+
+#endif /* __BT_APP_COMMON_H__ */
diff --git a/examples/09_arch_client/main/component.mk b/examples/09_arch_client/main/component.mk
new file mode 100644 (file)
index 0000000..24356f2
--- /dev/null
@@ -0,0 +1,10 @@
+#
+# Main Makefile. This is basically the same as a component makefile.
+#
+# This Makefile should, at the very least, just include $(SDK_PATH)/make/component_common.mk. By default, 
+# this will take the sources in the src/ directory, compile them and link them into 
+# lib(subdirectory_name).a in the build directory. This behaviour is entirely configurable,
+# please read the ESP-IDF documents if you need to do this.
+#
+
+include $(IDF_PATH)/make/component_common.mk
diff --git a/examples/09_arch_client/main/demo_main.c b/examples/09_arch_client/main/demo_main.c
new file mode 100644 (file)
index 0000000..a128c1f
--- /dev/null
@@ -0,0 +1,43 @@
+// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include "bt.h"
+#include "freertos/FreeRTOS.h"
+#include "freertos/task.h"
+#include "string.h"
+//#include "bt_app_common.h"
+
+extern void bte_main_boot_entry(void *);
+extern void bt_app_task_start_up(void);
+extern void bt_app_core_start(void);
+
+void pingTask(void *pvParameters)
+{
+    while (1) {
+        vTaskDelay(1000 / portTICK_PERIOD_MS);
+        //printf("ping\n");
+    }
+}
+
+void app_main()
+{
+    bt_controller_init();
+    xTaskCreatePinnedToCore(&pingTask, "pingTask", 2048, NULL, 5, NULL, 0);
+    bt_app_task_start_up();
+    bte_main_boot_entry(bt_app_core_start);
+}
+