/*******************************************************************************
**
** Function         smp_encrypt_data
**
** Description      This function is called to generate passkey.
**
** Returns          void
**
*******************************************************************************/
BOOLEAN smp_encrypt_data (UINT8 *key, UINT8 key_len,
                          UINT8 *plain_text, UINT8 pt_len,
                          tSMP_ENC *p_out)
{
    aes_context     ctx;
    UINT8           *p_start = NULL;
    UINT8           *p = NULL;
    UINT8           *p_rev_data = NULL;    /* input data in big endilan format */
    UINT8           *p_rev_key = NULL;     /* input key in big endilan format */
    UINT8           *p_rev_output = NULL;  /* encrypted output in big endilan format */

    SMP_TRACE_DEBUG0 ("smp_encrypt_data");
    if ( (p_out == NULL ) || (key_len != SMP_ENCRYT_KEY_SIZE) )
    {
        BTM_TRACE_ERROR0 ("smp_encrypt_data Failed");
        return(FALSE);
    }

    if ((p_start = (UINT8 *)GKI_getbuf((SMP_ENCRYT_DATA_SIZE*4))) == NULL)
    {
        BTM_TRACE_ERROR0 ("smp_encrypt_data Failed unable to allocate buffer");
        return(FALSE);
    }

    if (pt_len > SMP_ENCRYT_DATA_SIZE)
        pt_len = SMP_ENCRYT_DATA_SIZE;

    memset(p_start, 0, SMP_ENCRYT_DATA_SIZE * 4);
    p = p_start;
    ARRAY_TO_STREAM (p, plain_text, pt_len); /* byte 0 to byte 15 */
    p_rev_data = p = p_start + SMP_ENCRYT_DATA_SIZE; /* start at byte 16 */
    REVERSE_ARRAY_TO_STREAM (p, p_start, SMP_ENCRYT_DATA_SIZE);  /* byte 16 to byte 31 */
    p_rev_key = p; /* start at byte 32 */
    REVERSE_ARRAY_TO_STREAM (p, key, SMP_ENCRYT_KEY_SIZE); /* byte 32 to byte 47 */

    smp_debug_print_nbyte_little_endian(key, (const UINT8 *)"Key", SMP_ENCRYT_KEY_SIZE);
    smp_debug_print_nbyte_little_endian(p_start, (const UINT8 *)"Plain text", SMP_ENCRYT_DATA_SIZE);
    p_rev_output = p;
    aes_set_key(p_rev_key, SMP_ENCRYT_KEY_SIZE, &ctx);
    aes_encrypt(p_rev_data, p, &ctx);  /* outputs in byte 48 to byte 63 */

    p = p_out->param_buf;
    REVERSE_ARRAY_TO_STREAM (p, p_rev_output, SMP_ENCRYT_DATA_SIZE);
    smp_debug_print_nbyte_little_endian(p_out->param_buf, (const UINT8 *)"Encrypted text", SMP_ENCRYT_KEY_SIZE);

    p_out->param_len = SMP_ENCRYT_KEY_SIZE;
    p_out->status = HCI_SUCCESS;
    p_out->opcode =  HCI_BLE_ENCRYPT;

    GKI_freebuf(p_start);

    return(TRUE);
}
/*******************************************************************************
**
** Function         btm_enq_wl_dev_operation
**
** Description      enqueue the pending whitelist device operation(loading or removing).
*******************************************************************************/
void btm_enq_wl_dev_operation(BOOLEAN to_add, BD_ADDR bd_addr, UINT8 attr)
{
    tBTM_BLE_WL_OP *p_dev_op = btm_cb.ble_ctr_cb.wl_op_q;
    UINT8   i = 0;

    for (i = 0; i < BTM_BLE_MAX_BG_CONN_DEV_NUM; i ++, p_dev_op ++)
    {
        if (p_dev_op->in_use && !memcmp(p_dev_op->bd_addr, bd_addr, BD_ADDR_LEN))
        {
            p_dev_op->to_add = to_add;
            p_dev_op->attr = attr;
            return;
        }
        else if (!p_dev_op->in_use)
            break;
    }
    if (i != BTM_BLE_MAX_BG_CONN_DEV_NUM)
    {
        p_dev_op->in_use = TRUE;
        p_dev_op->to_add = to_add;
        p_dev_op->attr  = attr;
        memcpy(p_dev_op->bd_addr, bd_addr, BD_ADDR_LEN);
    }
    else
    {
        BTM_TRACE_ERROR0("max pending WL operation reached, discard");
    }
    return;
}
/*******************************************************************************
**
** Function         btm_ble_initiate_select_conn
**
** Description      This function is to start/stop selective connection procedure.
**
** Parameters       start: TRUE to start; FALSE to stop.
**                  p_select_cback: callback function to return application
**                                  selection.
**
** Returns          BOOLEAN: selective connectino procedure is started.
**
*******************************************************************************/
void btm_ble_initiate_select_conn(BD_ADDR bda)
{
    BTM_TRACE_EVENT0 ("btm_ble_initiate_select_conn");

    /* use direct connection procedure to initiate connection */
    if (!L2CA_ConnectFixedChnl(L2CAP_ATT_CID, bda))
    {
        BTM_TRACE_ERROR0("btm_ble_initiate_select_conn failed");
    }
}
Exemplo n.º 4
0
/*******************************************************************************
**
** Function         btm_ble_initiate_select_conn
**
** Description      This function is to start/stop selective connection procedure.
**
** Parameters       start: TRUE to start; FALSE to stop.
**                  p_select_cback: callback function to return application
**                                  selection.
**
** Returns          BOOLEAN: selective connectino procedure is started.
**
*******************************************************************************/
void btm_ble_initiate_select_conn(BD_ADDR bda)
{
    UINT8   addr_type;
    BTM_TRACE_EVENT0 ("btm_ble_initiate_select_conn");
    addr_type = btm_ble_map_bda_to_conn_bda(bda);

    /* use direct connection procedure to initiate connection */
    if (!L2CA_ConnectFixedChnl(L2CAP_ATT_CID, bda))
    {
        BTM_TRACE_ERROR0("btm_ble_initiate_select_conn failed");
    }
}
int start_work_thread(void)
{
    adpcm_rec_cb.running = 1;

    if(adpcm_rec_cb.tid > 0)
    {
        BTM_TRACE_WARNING0("the pthread is already start,can't start again");
        return -1;
    }

    if (pthread_create(&adpcm_rec_cb.tid, (const pthread_attr_t *) NULL, (void*)work_task, NULL) < 0)
    {
        BTM_TRACE_ERROR0("uipc_thread_create pthread_create failed");
        return -1;
    }

    return 0;
}
Exemplo n.º 6
0
/*******************************************************************************
**
** Function         btm_ble_start_select_conn
**
** Description      This function is to start/stop selective connection procedure.
**
** Parameters       start: TRUE to start; FALSE to stop.
**                  p_select_cback: callback function to return application
**                                  selection.
**
** Returns          BOOLEAN: selective connectino procedure is started.
**
*******************************************************************************/
BOOLEAN btm_ble_start_select_conn(BOOLEAN start,tBTM_BLE_SEL_CBACK   *p_select_cback)
{
    tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
    UINT16 scan_int, scan_win;

    BTM_TRACE_EVENT0 ("btm_ble_start_select_conn");

    scan_int = (p_cb->scan_int == BTM_BLE_CONN_PARAM_UNDEF) ? BTM_BLE_CONN_EST_SCAN_INT : p_cb->scan_int;
    scan_win = (p_cb->scan_win == BTM_BLE_CONN_PARAM_UNDEF) ? BTM_BLE_CONN_EST_SCAN_WIND : p_cb->scan_win;

    if (start)
    {
        if (!btm_cb.btm_inq_vars.inq_active)
        {
            btm_cb.ble_ctr_cb.p_select_cback = p_select_cback;

            btm_update_scanner_filter_policy(SP_ADV_WL);

            if (!btsnd_hcic_ble_set_scan_params(BTM_BLE_SCAN_MODE_PASS,  /* use passive scan by default */
                                                scan_int, /* scan interval */
                                                scan_win,    /* scan window */
                                                BLE_ADDR_PUBLIC,         /* own device, DUMO always use public */
                                                SP_ADV_WL)              /* process advertising packets only from devices in the White List */
                )
                return FALSE;

            if (p_cb->inq_var.adv_mode == BTM_BLE_ADV_ENABLE)
            {
                BTM_TRACE_ERROR0("peripheral device cannot initiate a selective connection");
                return FALSE;
            }
            else if (p_cb->bg_conn_dev_num > 0 && btm_ble_count_unconn_dev_in_whitelist() > 0 )
            {

                if (!btsnd_hcic_ble_set_scan_enable(TRUE, TRUE)) /* duplicate filtering enabled */
                    return FALSE;

                /* mark up inquiry status flag */
                btm_cb.btm_inq_vars.inq_active = TRUE;
                btm_cb.ble_ctr_cb.inq_var.proc_mode = BTM_BLE_SELECT_SCAN;

                p_cb->bg_conn_state = BLE_BG_CONN_ACTIVE;

            }
        }
        else
        {
            BTM_TRACE_ERROR0("scan active, can not start selective connection procedure");
            return FALSE;
        }
    }
    else /* disable selective connection mode */
    {
        p_cb->p_select_cback = NULL;
        btm_cb.btm_inq_vars.inq_active = FALSE;
        btm_cb.ble_ctr_cb.inq_var.proc_mode = BTM_BLE_INQUIRY_NONE;

        btm_update_scanner_filter_policy(SP_ADV_ALL);

        /* stop scanning */
        if (p_cb->bg_conn_dev_num > 0)
        {
            if (!btsnd_hcic_ble_set_scan_enable(FALSE, TRUE)) /* duplicate filtering enabled */
                return FALSE;
        }
    }
    return TRUE;
}
static void work_task(void *arg)
{
    BTM_TRACE_WARNING0("WORK THREAD START");
    while (1)
    {
        VOHOG_LOCK();
        if(adpcm_rec_cb.running)
        {
           if(adpcm_rec_cb.channel_status)
           {
                if(!isEmpty())
                {
                    if(getDATAfrombuffer(tempData,ADPCM_VOICE_DATA_BLOCK_SIZE))
                    {
                        if(UIPC_RTKBT_VR_Send_noblock(UIPC_CH_ID_RTKBT_VR_AUDIO, 0, tempData, ADPCM_VOICE_DATA_BLOCK_SIZE))
                        {
                            BTM_TRACE_DEBUG0("send data sucess");

                            adpcm_voice_data_buffer.ridx ++;
                            adpcm_voice_data_buffer.ridx %= ADPCM_VOICE_DATA_BUFFER_SIZE;

                        } else {
                            BTM_TRACE_ERROR0("send data failed");
                        }
                   }

                }
           }
        } else {
            if(adpcm_rec_cb.channel_status)
            {
                BTM_TRACE_WARNING0("close thread: channel is connected");
                if(isEmpty())
                {
                    BTM_TRACE_WARNING0("the buffer is empty, close socket");
                    resetBuffer();
                    UIPC_Close(UIPC_CH_ID_RTKBT_VR_AUDIO);
                    VOHOG_UNLOCK();
                    break;
                }  else {
                    BTM_TRACE_WARNING0("the buffer is not empty, send data");
                    if(getDATAfrombuffer(tempData,ADPCM_VOICE_DATA_BLOCK_SIZE))
                    {
                        if(UIPC_RTKBT_VR_Send_noblock(UIPC_CH_ID_RTKBT_VR_AUDIO, 0, tempData, ADPCM_VOICE_DATA_BLOCK_SIZE))
                        {
                            BTM_TRACE_DEBUG0("send data sucess");

                            adpcm_voice_data_buffer.ridx ++;
                            adpcm_voice_data_buffer.ridx %= ADPCM_VOICE_DATA_BUFFER_SIZE;

                        } else {
                            BTM_TRACE_ERROR0("send data failed,close socket");
                            resetBuffer();
                            UIPC_Close(UIPC_CH_ID_RTKBT_VR_AUDIO);
                            VOHOG_UNLOCK();
                            break;
                        }
                   }

                }
            }else {
                BTM_TRACE_WARNING0("close thread: the socket is not connected");
                VOHOG_UNLOCK();
                break;
            }

        }
        VOHOG_UNLOCK();
        usleep(1000);
    }
    adpcm_rec_cb.tid = 0;
    BTM_TRACE_WARNING0("VOICE THREAD DONE");
}