#include "include.h"
#include "ble_service.h"
#include "ble_user_service.h"
#include "ble_hid_service.h"
#include "ble_fota_service.h"


///////////////////////////////////////////////////////////////////////////
const uint8_t adv_data_const[] = {
    // Flags general discoverable, BR/EDR not supported
    0x02, 0x01, 0x06,
    // Manufacturer Specific Data
    0x06, 0xff, 0x06, 0x00, 0x03, 0x00, 0x80,
    // Incomplete List of 16-bit Service Class UUIDs
    0x03, 0x02, 0x12, 0x18,
    // Appearance: Mouse
    0x03, 0x19, 0xc2, 0x03
};

const uint8_t scan_data_const[] = {
    // Name
    0x08, 0x09, 'B', 'L', 'E', '-', 'B', 'O', 'X',
};


u32 ble_get_scan_data(u8 *scan_buf, u32 buf_size)
{
    memset(scan_buf, 0, buf_size);
    u32 data_len = sizeof(scan_data_const);
    memcpy(scan_buf, scan_data_const, data_len);

    //读取BLE配置的蓝牙名称
    int len;
    len = strlen(xcfg_cb.le_name);
    if (len > 0) {
        memcpy(&scan_buf[2], xcfg_cb.le_name, len);
        data_len = 2 + len;
        scan_buf[0] = len + 1;
    }
    return data_len;
}

u32 ble_get_adv_data(u8 *adv_buf, u32 buf_size)
{
    memset(adv_buf, 0, buf_size);
    u32 data_len = sizeof(adv_data_const);
    memcpy(adv_buf, adv_data_const, data_len);

#if 1
    //读取BLE配置的蓝牙名称
    int len;
    len = strlen(xcfg_cb.le_name);
    if (len > 0 && (data_len + len + 2) <= 31) {
        memcpy(&adv_buf[data_len+2], xcfg_cb.le_name, len);
        adv_buf[data_len+0] = len + 1;
        adv_buf[data_len+1] = 0x09;
        data_len += 2 + len;
    }
#endif

    return data_len;
}


const uint8_t profile_data[] =
{
    // 0x0001 PRIMARY_SERVICE-FF12
    0x0a, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x28, 0x12, 0xff, 
    // 0x0002 CHARACTERISTIC-FF15 - READ | WRITE_WITHOUT_RESPONSE | DYNAMIC
    0x0d, 0x00, 0x02, 0x00, 0x02, 0x00, 0x03, 0x28, 0x06, 0x03, 0x00, 0x15, 0xff, 
    // 0x0003 VALUE CHARACTERISTIC-FF15 - READ | WRITE_WITHOUT_RESPONSE | DYNAMIC
    // READ_ANYBODY, WRITE_ANYBODY
    0x08, 0x00, 0x06, 0x01, 0x03, 0x00, 0x15, 0xff, 
    // 0x0004 CHARACTERISTIC-FF14 - NOTIFY
    0x0d, 0x00, 0x02, 0x00, 0x04, 0x00, 0x03, 0x28, 0x10, 0x05, 0x00, 0x14, 0xff, 
    // 0x0005 VALUE CHARACTERISTIC-FF14 - NOTIFY -''
    // 
    0x08, 0x00, 0x00, 0x00, 0x05, 0x00, 0x14, 0xff, 
    // 0x0006 CLIENT_CHARACTERISTIC_CONFIGURATION
    // READ_ANYBODY, WRITE_ANYBODY
    0x0a, 0x00, 0x0e, 0x01, 0x06, 0x00, 0x02, 0x29, 0x00, 0x00, 

    // 0x0007 PRIMARY_SERVICE-GAP_SERVICE
    0x0a, 0x00, 0x02, 0x00, 0x07, 0x00, 0x00, 0x28, 0x00, 0x18, 
    // 0x0008 CHARACTERISTIC-GAP_DEVICE_NAME - READ | DYNAMIC
    0x0d, 0x00, 0x02, 0x00, 0x08, 0x00, 0x03, 0x28, 0x02, 0x09, 0x00, 0x00, 0x2a, 
    // 0x0009 VALUE CHARACTERISTIC-GAP_DEVICE_NAME - READ | DYNAMIC
    // READ_ANYBODY
    0x08, 0x00, 0x02, 0x01, 0x09, 0x00, 0x00, 0x2a, 
    // 0x000a CHARACTERISTIC-GAP_APPEARANCE - READ
    0x0d, 0x00, 0x02, 0x00, 0x0a, 0x00, 0x03, 0x28, 0x02, 0x0b, 0x00, 0x01, 0x2a, 
    // 0x000b VALUE CHARACTERISTIC-GAP_APPEARANCE - READ -'c2 03'
    // READ_ANYBODY
    0x0a, 0x00, 0x02, 0x00, 0x0b, 0x00, 0x01, 0x2a, 0xc2, 0x03, 

    // 0x000c PRIMARY_SERVICE-GATT_SERVICE
    0x0a, 0x00, 0x02, 0x00, 0x0c, 0x00, 0x00, 0x28, 0x01, 0x18, 
    // 0x000d CHARACTERISTIC-GATT_SERVICE_CHANGED - INDICATE
    0x0d, 0x00, 0x02, 0x00, 0x0d, 0x00, 0x03, 0x28, 0x20, 0x0e, 0x00, 0x05, 0x2a, 
    // 0x000e VALUE CHARACTERISTIC-GATT_SERVICE_CHANGED - INDICATE -''
    // 
    0x08, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x05, 0x2a, 
    // 0x000f CLIENT_CHARACTERISTIC_CONFIGURATION
    // READ_ANYBODY, WRITE_ANYBODY
    0x0a, 0x00, 0x0e, 0x01, 0x0f, 0x00, 0x02, 0x29, 0x00, 0x00, 

    // 0x0010 PRIMARY_SERVICE-1812
    0x0a, 0x00, 0x02, 0x00, 0x10, 0x00, 0x00, 0x28, 0x12, 0x18, 
    // 0x0011 CHARACTERISTIC-2a4a - READ
    0x0d, 0x00, 0x02, 0x00, 0x11, 0x00, 0x03, 0x28, 0x02, 0x12, 0x00, 0x4a, 0x2a, 
    // 0x0012 VALUE CHARACTERISTIC-2a4a - READ -'11 01 00 00'
    // READ_ANYBODY
    0x0c, 0x00, 0x02, 0x00, 0x12, 0x00, 0x4a, 0x2a, 0x11, 0x01, 0x00, 0x00, 
    // 0x0013 CHARACTERISTIC-2a4b - READ | ENCRYPTION_KEY_LEAST_SIZE_16 | DYNAMIC
    0x0d, 0x00, 0x02, 0x00, 0x13, 0x00, 0x03, 0x28, 0x02, 0x14, 0x00, 0x4b, 0x2a, 
    // 0x0014 VALUE CHARACTERISTIC-2a4b - READ | ENCRYPTION_KEY_LEAST_SIZE_16 | DYNAMIC
    // READ_ANYBODY, ENCRYPTION_KEY_SIZE=16
    0x08, 0x00, 0x02, 0xf1, 0x14, 0x00, 0x4b, 0x2a, 
    // 0x0015 CHARACTERISTIC-2a4c - WRITE_WITHOUT_RESPONSE | DYNAMIC
    0x0d, 0x00, 0x02, 0x00, 0x15, 0x00, 0x03, 0x28, 0x04, 0x16, 0x00, 0x4c, 0x2a, 
    // 0x0016 VALUE CHARACTERISTIC-2a4c - WRITE_WITHOUT_RESPONSE | DYNAMIC
    // WRITE_ANYBODY
    0x08, 0x00, 0x04, 0x01, 0x16, 0x00, 0x4c, 0x2a, 
    // 0x0017 CHARACTERISTIC-2a4d - READ | NOTIFY | DYNAMIC
    0x0d, 0x00, 0x02, 0x00, 0x17, 0x00, 0x03, 0x28, 0x12, 0x18, 0x00, 0x4d, 0x2a, 
    // 0x0018 VALUE CHARACTERISTIC-2a4d - READ | NOTIFY | DYNAMIC
    // READ_ANYBODY
    0x08, 0x00, 0x02, 0x01, 0x18, 0x00, 0x4d, 0x2a, 
    // 0x0019 CLIENT_CHARACTERISTIC_CONFIGURATION
    // READ_ANYBODY, WRITE_ANYBODY
    0x0a, 0x00, 0x0e, 0x01, 0x19, 0x00, 0x02, 0x29, 0x00, 0x00, 
    // 0x001a REPORT_REFERENCE-READ-1-1
    0x0a, 0x00, 0x02, 0x00, 0x1a, 0x00, 0x08, 0x29, 0x01, 0x01, 
    // 0x001b CHARACTERISTIC-2a4d - READ | NOTIFY | DYNAMIC
    0x0d, 0x00, 0x02, 0x00, 0x1b, 0x00, 0x03, 0x28, 0x12, 0x1c, 0x00, 0x4d, 0x2a, 
    // 0x001c VALUE CHARACTERISTIC-2a4d - READ | NOTIFY | DYNAMIC
    // READ_ANYBODY
    0x08, 0x00, 0x02, 0x01, 0x1c, 0x00, 0x4d, 0x2a, 
    // 0x001d CLIENT_CHARACTERISTIC_CONFIGURATION
    // READ_ANYBODY, WRITE_ANYBODY
    0x0a, 0x00, 0x0e, 0x01, 0x1d, 0x00, 0x02, 0x29, 0x00, 0x00, 
    // 0x001e REPORT_REFERENCE-READ-2-1
    0x0a, 0x00, 0x02, 0x00, 0x1e, 0x00, 0x08, 0x29, 0x02, 0x01, 
    // 0x001f CHARACTERISTIC-2a4d - READ | WRITE | WRITE_WITHOUT_RESPONSE | DYNAMIC
    0x0d, 0x00, 0x02, 0x00, 0x1f, 0x00, 0x03, 0x28, 0x0e, 0x20, 0x00, 0x4d, 0x2a, 
    // 0x0020 VALUE CHARACTERISTIC-2a4d - READ | WRITE | WRITE_WITHOUT_RESPONSE | DYNAMIC
    // READ_ANYBODY, WRITE_ANYBODY
    0x08, 0x00, 0x0e, 0x01, 0x20, 0x00, 0x4d, 0x2a, 
    // 0x0021 REPORT_REFERENCE-READ-2-2
    0x0a, 0x00, 0x02, 0x00, 0x21, 0x00, 0x08, 0x29, 0x02, 0x02, 
    // 0x0022 CHARACTERISTIC-2a4d - READ | NOTIFY | DYNAMIC
    0x0d, 0x00, 0x02, 0x00, 0x22, 0x00, 0x03, 0x28, 0x12, 0x23, 0x00, 0x4d, 0x2a, 
    // 0x0023 VALUE CHARACTERISTIC-2a4d - READ | NOTIFY | DYNAMIC
    // READ_ANYBODY
    0x08, 0x00, 0x02, 0x01, 0x23, 0x00, 0x4d, 0x2a, 
    // 0x0024 CLIENT_CHARACTERISTIC_CONFIGURATION
    // READ_ANYBODY, WRITE_ANYBODY
    0x0a, 0x00, 0x0e, 0x01, 0x24, 0x00, 0x02, 0x29, 0x00, 0x00, 
    // 0x0025 REPORT_REFERENCE-READ-3-1
    0x0a, 0x00, 0x02, 0x00, 0x25, 0x00, 0x08, 0x29, 0x03, 0x01, 

    // END
    0x00, 0x00, 
}; // total size 198 bytes 


//----------------------------------------------------------------------------
//
const u8 *ble_get_profile_data(void)
{
    return profile_data;
}

void ble_service_init(void)
{
    printf("ble_service_init\n");

    ble_user_service_init();
    ble_hid_service_init();
#if (AB_FOT_CHANNEL & AB_FOT_CHANNEL_BLE)
    ble_fota_service_init();
#endif
}
