Ejemplo n.º 1
0
void
bletest_execute_initiator(void)
{
    int rc;
    uint16_t handle;

    /* 
     * Determine if there is an active connection for the current handle
     * we are trying to create. If so, start looking for the next one
     */
    if (g_bletest_current_conns < BLETEST_CFG_CONCURRENT_CONNS) {
        handle = g_bletest_current_conns + 1;
        if (ble_ll_conn_find_active_conn(handle)) {
            /* Set LED to slower blink rate */
            g_bletest_led_rate = OS_TICKS_PER_SEC;

            /* Set next os time to start the connection update */
            g_next_os_time = 0;

            /* Ask for version information */
            rc = host_hci_cmd_rd_rem_version(handle);
            assert(rc == 0);
            host_hci_outstanding_opcode = 0;

            /* Scanning better be stopped! */
            assert(ble_ll_scan_enabled() == 0);

            /* Add to current connections */
            ++g_bletest_current_conns;

            /* Move to next connection */
            if (g_bletest_current_conns < BLETEST_CFG_CONCURRENT_CONNS) {
                /* restart initiating */
                g_bletest_cur_peer_addr[5] += 1;
                g_dev_addr[5] += 1;
                bletest_init_initiator();
            }
        }
    } else {
        /* Issue a connection parameter update to connection handle 1 */
        if (g_next_os_time == 0) {
            g_next_os_time = os_time_get();
            g_next_os_time += OS_TICKS_PER_SEC * 5;
        } else {
            if (g_next_os_time != 0xffffffff) {
#if 0
                if ((int32_t)(os_time_get() - g_next_os_time) >= 0) {
                    bletest_send_conn_update(1);
                    g_next_os_time = 0xffffffff;
                }
#else
                g_next_os_time = 0xffffffff;
#endif
            }
        }
    }
}
Ejemplo n.º 2
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;
        }
    }
}
Ejemplo n.º 3
0
void
bletest_execute_initiator(void)
{
    int i;
    int rc;
    int8_t rssi;
    uint16_t handle;
    uint8_t new_chan_map[5];

    /*
     * Determine if there is an active connection for the current handle
     * we are trying to create. If so, start looking for the next one
     */
    if (g_bletest_current_conns < BLETEST_CFG_CONCURRENT_CONNS) {
        handle = g_bletest_current_conns + 1;
        if (ble_ll_conn_find_active_conn(handle)) {
            /* Set LED to slower blink rate */
            g_bletest_led_rate = OS_TICKS_PER_SEC;

            /* Ask for version information */
            rc = bletest_hci_rd_rem_version(handle);

            /* Ask for remote used features */
            rc = bletest_hci_le_read_rem_used_feat(handle);

            /* Scanning better be stopped! */
            assert(ble_ll_scan_enabled() == 0);

            /* Add to current connections */
            if (!rc) {
                ++g_bletest_current_conns;

                /* Move to next connection */
                if (g_bletest_current_conns < BLETEST_CFG_CONCURRENT_CONNS) {
                    /* restart initiating */
                    g_bletest_cur_peer_addr[5] += 1;
                    g_dev_addr[5] += 1;
                    bletest_init_initiator();
                }
            }
        } else {
            if (ble_ll_scan_enabled() == 0) {
                bletest_hci_le_create_connection(&g_cc);
            }
        }
    } else {
        if ((int32_t)(os_time_get() - g_next_os_time) >= 0) {
            if ((g_bletest_state == 1) || (g_bletest_state == 3)) {
                for (i = 0; i < g_bletest_current_conns; ++i) {
                    if (ble_ll_conn_find_active_conn(i + 1)) {
                        bletest_hci_le_rd_chanmap(i+1);
                    }
                }
            } else if (g_bletest_state == 2) {
                new_chan_map[0] = 0;
                new_chan_map[1] = 0x3;
                new_chan_map[2] = 0;
                new_chan_map[3] = 0x1F;
                new_chan_map[4] = 0;
                bletest_hci_le_set_host_chan_class(new_chan_map);
            } else if (g_bletest_state == 4) {
#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
                struct hci_start_encrypt hsle;
                for (i = 0; i < g_bletest_current_conns; ++i) {
                    if (ble_ll_conn_find_active_conn(i + 1)) {
                        hsle.connection_handle = i + 1;
                        hsle.encrypted_diversifier = g_bletest_EDIV;
                        hsle.random_number = g_bletest_RAND;
                        swap_buf(hsle.long_term_key, (uint8_t *)g_bletest_LTK,
                                 16);
                        bletest_hci_le_start_encrypt(&hsle);
                    }
                }
#endif
            } else if (g_bletest_state == 8) {
#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
                struct hci_start_encrypt hsle;
                for (i = 0; i < g_bletest_current_conns; ++i) {
                    if (ble_ll_conn_find_active_conn(i + 1)) {
                        hsle.connection_handle = i + 1;
                        hsle.encrypted_diversifier = g_bletest_EDIV;
                        hsle.random_number = ~g_bletest_RAND;
                        swap_buf(hsle.long_term_key, (uint8_t *)g_bletest_LTK,
                                 16);
                        bletest_hci_le_start_encrypt(&hsle);
                    }
                }
#endif
            } else {
                for (i = 0; i < g_bletest_current_conns; ++i) {
                    if (ble_ll_conn_find_active_conn(i + 1)) {
                        ble_hs_hci_util_read_rssi(i+1, &rssi);
                    }
                }
            }

            ++g_bletest_state;
            if (g_bletest_state > 9) {
                g_bletest_state = 9;
            }
            g_next_os_time = os_time_get() + OS_TICKS_PER_SEC * 3;
        }
    }
}
Ejemplo n.º 4
0
/**
 * BLE test task
 *
 * @param arg
 */
void
bletest_task_handler(void *arg)
{
    int rc;
    uint64_t rand64;
    uint64_t event_mask;

    /* 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 the host timer */
    os_callout_init(&g_bletest_timer, &g_bletest_evq, bletest_timer_cb,
                    NULL);

    ble_hs_dbg_set_sync_state(BLE_HS_SYNC_STATE_GOOD);

    /* Send the reset command first */
    rc = bletest_hci_reset_ctlr();
    assert(rc == 0);

#if (BLETEST_CFG_ROLE == BLETEST_ROLE_ADVERTISER)
    /* Initialize the advertiser */
    console_printf("Starting BLE test task as advertiser\n");
#if MYNEWT_VAL(BLE_ANDROID_MULTI_ADV_SUPPORT)
    /* Start up all advertising instances except default one */
    bletest_init_adv_instances();

    /* Start advertising on instance 0 at 0 dbm */
    bletest_init_advertising(0, 0);
#else
    bletest_init_advertising();
#endif
#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

    /* Read unique HW id */
    rc = hal_bsp_hw_id((void *)&g_bletest_hw_id[0], sizeof(g_bletest_hw_id));
    assert(rc == 16);
    console_printf("HW id=%04x%04x%04x%04x\n",
                   (unsigned int)g_bletest_hw_id[0],
                   (unsigned int)g_bletest_hw_id[1],
                   (unsigned int)g_bletest_hw_id[2],
                   (unsigned int)g_bletest_hw_id[3]);

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

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

    /* Read device address */
    rc = bletest_hci_rd_bd_addr();
    assert(rc == 0);

    /* Read local features */
    rc = bletest_hci_rd_local_feat();
    assert(rc == 0);

    /* Read local commands */
    rc = bletest_hci_rd_local_supp_cmd();
    assert(rc == 0);

    /* Read version */
    rc = bletest_hci_rd_local_version();
    assert(rc == 0);

    /* Read supported states */
    rc = bletest_hci_le_read_supp_states();
    assert(rc == 0);

    /* Read maximum data length */
    rc = bletest_hci_le_rd_max_datalen();
    assert(rc == 0);

#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_DATA_LEN_EXT) == 1)
    /* Read suggested data length */
    rc = bletest_hci_le_rd_sugg_datalen();
    assert(rc == 0);

    /* write suggested default data length */
    rc = bletest_hci_le_write_sugg_datalen(BLETEST_CFG_SUGG_DEF_TXOCTETS,
                                           BLETEST_CFG_SUGG_DEF_TXTIME);
    assert(rc == 0);

    /* Read suggested data length */
    rc = bletest_hci_le_rd_sugg_datalen();
    assert(rc == 0);

    /* Set data length (note: we know there is no connection; just a test) */
    rc = bletest_hci_le_set_datalen(0x1234, BLETEST_CFG_SUGG_DEF_TXOCTETS,
                                    BLETEST_CFG_SUGG_DEF_TXTIME);
    assert(rc != 0);
#endif

    /* Encrypt a block */
#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
    rc = bletest_hci_le_encrypt((uint8_t *)g_ble_ll_encrypt_test_key,
                                (uint8_t *)g_ble_ll_encrypt_test_plain_text);
    assert(rc == 0);
#endif

    /* Get a random number */
    rc = ble_hs_hci_util_rand(&rand64, 8);
    assert(rc == 0);

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

    /* Init state */
    g_bletest_state = 0;

    /* Begin advertising if we are an advertiser */
#if (BLETEST_CFG_ROLE == BLETEST_ROLE_ADVERTISER)
#if MYNEWT_VAL(BLE_ANDROID_MULTI_ADV_SUPPORT)
    rc = bletest_hci_le_set_multi_adv_enable(1, 0);
    assert(rc == 0);
#else
    rc = bletest_hci_le_set_adv_enable(1);
    assert(rc == 0);
#endif
#endif

    bletest_timer_cb(NULL);

    while (1) {
        os_eventq_run(&g_bletest_evq);
    }
}