/**
 * Send a LE command from the host to the controller.
 * 
 * @param ocf 
 * @param len 
 * @param cmddata 
 * 
 * @return int 
 */
static int
host_hci_le_cmd_send(uint16_t ocf, uint8_t len, void *cmddata)
{
    int rc;
    rc = host_hci_cmd_send(BLE_HCI_OGF_LE, ocf, len, cmddata);
    return rc;
}
/**
 * Read the transmit power level used for LE advertising channel packets.
 *
 * @return int
 */
int
host_hci_cmd_read_adv_pwr(void)
{
    int rc;

    rc = host_hci_cmd_send(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RD_ADV_CHAN_TXPWR, 0,
                           NULL);
    return rc;
}
int
host_hci_cmd_le_create_conn_cancel(void)
{
    int rc;

    rc = host_hci_cmd_send(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_CREATE_CONN_CANCEL,
                           0, NULL);
    return rc;
}
/**
 * Reset the controller and link manager.
 * 
 * @return int 
 */
int
host_hci_cmd_reset(void)
{
    int rc;

    rc = host_hci_cmd_send(BLE_HCI_OGF_CTLR_BASEBAND, BLE_HCI_OCF_CB_RESET, 0,
                           NULL);
    return rc;
}
int
host_hci_cmd_rd_local_version(void)
{
    int rc;

    rc = host_hci_cmd_send(BLE_HCI_OGF_INFO_PARAMS,
                           BLE_HCI_OCF_IP_RD_LOCAL_VER, 0, NULL);
    return rc;
}
/**
 * Read the RSSI for a given connection handle
 * 
 * NOTE: OGF=0x05 OCF=0x0005 
 *  
 * @param handle 
 * 
 * @return int 
 */
int
host_hci_cmd_read_rssi(uint16_t handle)
{
    int rc;
    uint8_t cmd[sizeof(uint16_t)];

    htole16(cmd, handle);
    rc = host_hci_cmd_send(BLE_HCI_OGF_STATUS_PARAMS, BLE_HCI_OCF_RD_RSSI, 
                           sizeof(uint16_t), cmd);
    return rc;
}
int
host_hci_cmd_rd_rem_version(uint16_t handle)
{
    int rc;
    uint8_t cmd[sizeof(uint16_t)];

    htole16(cmd, handle);
    rc = host_hci_cmd_send(BLE_HCI_OGF_LINK_CTRL,
                           BLE_HCI_OCF_RD_REM_VER_INFO, sizeof(uint16_t), cmd);
    return rc;
}
int
host_hci_cmd_set_event_mask(uint64_t event_mask)
{
    int rc;
    uint8_t cmd[BLE_HCI_SET_EVENT_MASK_LEN];

    htole64(cmd, event_mask);
    rc = host_hci_cmd_send(BLE_HCI_OGF_CTLR_BASEBAND,
                           BLE_HCI_OCF_CB_SET_EVENT_MASK,
                           BLE_HCI_SET_EVENT_MASK_LEN,
                           cmd);
    return rc;
}
int
host_hci_cmd_disconnect(uint16_t handle, uint8_t reason)
{
    int rc;
    uint8_t cmd[BLE_HCI_DISCONNECT_CMD_LEN];

    htole16(cmd, handle);
    cmd[2] = reason;
    rc = host_hci_cmd_send(BLE_HCI_OGF_LINK_CTRL,
                           BLE_HCI_OCF_DISCONNECT_CMD,
                           BLE_HCI_DISCONNECT_CMD_LEN,
                           cmd);
    return rc;
}
Esempio n. 10
0
/**
 * BLE test task 
 * 
 * @param arg 
 */
void
bletest_task_handler(void *arg)
{
    int rc;
    uint64_t event_mask;
    struct os_event *ev;
    struct os_callout_func *cf;

    /* Set LED blink rate */
    g_bletest_led_rate = OS_TICKS_PER_SEC / 20;

    /* Wait one second before starting test task */
    os_time_delay(OS_TICKS_PER_SEC);

    /* Initialize eventq */
    os_eventq_init(&g_bletest_evq);

    /* Initialize the host timer */
    os_callout_func_init(&g_bletest_timer, &g_bletest_evq, bletest_timer_cb,
                         NULL);

    /* Send the reset command first */
    rc = host_hci_cmd_send(BLE_HCI_OGF_CTLR_BASEBAND, BLE_HCI_OCF_CB_RESET,
                           0, NULL);
    assert(rc == 0);
    host_hci_outstanding_opcode = 0;

#if (BLETEST_CFG_ROLE == BLETEST_ROLE_ADVERTISER)
    /* Initialize the advertiser */
    console_printf("Starting BLE test task as advertiser\n");
    bletest_init_advertising();
#endif

#if (BLETEST_CFG_ROLE == BLETEST_ROLE_SCANNER)
    /* Initialize the scanner */
    console_printf("Starting BLE test task as scanner\n");
    bletest_init_scanner();
#endif

#if (BLETEST_CFG_ROLE == BLETEST_ROLE_INITIATOR)
    /* Initialize the scanner */
    console_printf("Starting BLE test task as initiator\n");
    bletest_init_initiator();
#endif

    /* Set the event mask we want to display */
    event_mask = 0x7FF;
    rc = host_hci_cmd_le_set_event_mask(event_mask);
    assert(rc == 0);
    host_hci_outstanding_opcode = 0;

    /* Turn on all events */
    event_mask = 0xffffffffffffffff;
    rc = host_hci_cmd_set_event_mask(event_mask);
    assert(rc == 0);
    host_hci_outstanding_opcode = 0;

    /* Turn on all events */
    rc = host_hci_cmd_rd_local_version();
    assert(rc == 0);
    host_hci_outstanding_opcode = 0;

    /* Wait some time before starting */
    os_time_delay(OS_TICKS_PER_SEC);

    /* Init bletest variables */
    g_bletest_state = 0;
    g_next_os_time = os_time_get();

    /* Begin advertising if we are an advertiser */
#if (BLETEST_CFG_ROLE == BLETEST_ROLE_ADVERTISER)
    rc = host_hci_cmd_le_set_adv_enable(1);
    assert(rc == 0);
    host_hci_outstanding_opcode = 0;
#endif

    bletest_timer_cb(NULL);

    while (1) {
        ev = os_eventq_get(&g_bletest_evq);
        switch (ev->ev_type) {
        case OS_EVENT_T_TIMER:
            cf = (struct os_callout_func *)ev;
            assert(cf->cf_func);
            cf->cf_func(cf->cf_arg);
            break;
        default:
            assert(0);
            break;
        }
    }
}