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 } } } }
static void bletest_execute_advertiser(void) { int i,j; int rc; uint16_t handle; uint16_t pktlen; struct os_mbuf *om; /* See if we should start advertising again */ 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; /* advertising better be stopped! */ assert(ble_ll_adv_enabled() == 0); /* Send the remote used features command */ rc = host_hci_cmd_le_read_rem_used_feat(handle); host_hci_outstanding_opcode = 0; assert(rc == 0); /* Send the remote used features command */ rc = host_hci_cmd_rd_rem_version(handle); host_hci_outstanding_opcode = 0; assert(rc == 0); /* set conn update time */ g_bletest_conn_upd_time = os_time_get() + (OS_TICKS_PER_SEC * 5); g_bletest_start_update = 1; /* 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_advertising(); rc = host_hci_cmd_le_set_adv_enable(1); host_hci_outstanding_opcode = 0; assert(rc == 0); } } } #if 0 if (g_bletest_start_update) { if ((int32_t)(os_time_get() - g_bletest_conn_upd_time) >= 0) { bletest_send_conn_update(1); g_bletest_start_update = 0; } } #endif /* See if it is time to hand a data packet to the connection */ if ((int32_t)(os_time_get() - g_next_os_time) >= 0) { if (g_bletest_current_conns) { for (i = 0; i < g_bletest_current_conns; ++i) { if ((g_last_handle_used == 0) || (g_last_handle_used > g_bletest_current_conns)) { g_last_handle_used = 1; } handle = g_last_handle_used; if (ble_ll_conn_find_active_conn(handle)) { om = bletest_get_packet(); if (om) { /* set payload length */ pktlen = BLETEST_PKT_SIZE; om->om_len = BLETEST_PKT_SIZE + 4; /* Put the HCI header in the mbuf */ htole16(om->om_data, handle); htole16(om->om_data + 2, om->om_len); /* Place L2CAP header in packet */ htole16(om->om_data + 4, pktlen); om->om_data[6] = 0; om->om_data[7] = 0; /* Fill with incrementing pattern (starting from 1) */ for (j = 0; j < pktlen; ++j) { om->om_data[8 + j] = (uint8_t)(j + 1); } /* Add length */ om->om_len += 4; OS_MBUF_PKTHDR(om)->omp_len = om->om_len; ble_hci_transport_host_acl_data_send(om); /* Increment last handle used */ ++g_last_handle_used; } } } } g_next_os_time += OS_TICKS_PER_SEC; } }
void bletest_execute(void) { int rc; int i; uint16_t pktlen; uint16_t handle; struct os_mbuf *om; struct ble_ll_conn_sm *connsm; handle = 1; if ((int32_t)(os_time_get() - g_next_os_time) >= 0) { if (g_bletest_state == 0) { rc = host_hci_cmd_le_set_adv_enable(1); host_hci_outstanding_opcode = 0; assert(rc == 0); g_bletest_state = 1; } else if (g_bletest_state == 1) { /* See if handle 1 has been created. If so, send packets */ connsm = ble_ll_conn_find_active_conn(handle); if (connsm) { /* Set connection end time */ g_bletest_conn_end = os_time_get() + (OS_TICKS_PER_SEC * (60 * 15)); g_bletest_state = 2; } } else if (g_bletest_state == 2) { if ((int32_t)(os_time_get() - g_bletest_conn_end) >= 0) { g_bletest_state = 3; host_hci_cmd_disconnect(handle, BLE_ERR_REM_USER_CONN_TERM); } else { om = bletest_get_packet(); if (om) { /* set payload length */ pktlen = BLETEST_PKT_SIZE; om->om_len = BLETEST_PKT_SIZE + 4; /* Put the HCI header in the mbuf */ htole16(om->om_data, handle); htole16(om->om_data + 2, om->om_len); /* Place L2CAP header in packet */ htole16(om->om_data + 4, pktlen); om->om_data[6] = 0; om->om_data[7] = 0; /* Fill with incrementing pattern (starting from 1) */ for (i = 0; i < pktlen; ++i) { om->om_data[8 + i] = (uint8_t)(i + 1); } /* Add length */ om->om_len += 4; OS_MBUF_PKTHDR(om)->omp_len = om->om_len; ble_hci_transport_host_acl_data_send(om); } g_next_os_time += OS_TICKS_PER_SEC / 10; return; } } else if (g_bletest_state == 3) { /* We should be waiting for disconnect */ connsm = ble_ll_conn_find_active_conn(handle); if (!connsm) { /* Set to 0 if you want to restart advertising */ //g_bletest_state = 0; g_bletest_state = 4; } } g_next_os_time += OS_TICKS_PER_SEC; } }
static void bletest_execute_advertiser(void) { int i; #if (BLETEST_CONCURRENT_CONN_TEST == 1) #if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1) uint16_t mask; uint16_t reply_handle; #endif #endif int rc; uint16_t handle; struct os_mbuf *om; #if (BLETEST_THROUGHPUT_TEST == 1) os_sr_t sr; uint16_t completed_pkts; #endif /* See if we should start advertising again */ 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; #if (BLETEST_THROUGHPUT_TEST == 1) /* Set next os time to 10 seconds after 1st connection */ if (g_next_os_time == 0) { g_next_os_time = os_time_get() + (10 * OS_TICKS_PER_SEC); g_bletest_handle = handle; } #endif /* advertising better be stopped! */ assert(ble_ll_adv_enabled() == 0); /* Send the remote used features command */ rc = bletest_hci_le_read_rem_used_feat(handle); if (rc) { return; } /* Send the remote read version command */ rc = bletest_hci_rd_rem_version(handle); if (rc) { return; } /* set conn update time */ g_bletest_conn_upd_time = os_time_get() + (OS_TICKS_PER_SEC * 5); g_bletest_start_update = 1; /* 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; #if MYNEWT_VAL(BLE_ANDROID_MULTI_ADV_SUPPORT) bletest_init_advertising(0,0); bletest_hci_le_set_multi_adv_enable(1, 0); #else bletest_init_advertising(); bletest_hci_le_set_adv_enable(1); #endif } } else { /* If we failed to start advertising we should keep trying */ if (ble_ll_adv_enabled() == 0) { #if MYNEWT_VAL(BLE_ANDROID_MULTI_ADV_SUPPORT) bletest_hci_le_set_multi_adv_enable(1, 0); #else bletest_hci_le_set_adv_enable(1); #endif } } } #if 0 if (g_bletest_start_update) { if ((int32_t)(os_time_get() - g_bletest_conn_upd_time) >= 0) { bletest_send_conn_update(1); g_bletest_start_update = 0; } } #endif #if (BLETEST_CONCURRENT_CONN_TEST == 1) /* See if it is time to hand a data packet to the connection */ if ((int32_t)(os_time_get() - g_next_os_time) >= 0) { #if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1) /* Do we need to send a LTK reply? */ mask = 1; reply_handle = 1; while (g_bletest_ltk_reply_handle && mask) { if (g_bletest_ltk_reply_handle & mask) { bletest_send_ltk_req_reply(reply_handle); //bletest_send_ltk_req_neg_reply(reply_handle); g_bletest_ltk_reply_handle &= ~mask; } ++reply_handle; mask <<= 1; } #endif if (g_bletest_current_conns) { for (i = 0; i < g_bletest_current_conns; ++i) { if ((g_last_handle_used == 0) || (g_last_handle_used > g_bletest_current_conns)) { g_last_handle_used = 1; } handle = g_last_handle_used; if (ble_ll_conn_find_active_conn(handle)) { om = bletest_send_packet(handle); if (om) { /* Increment last handle used */ ++g_last_handle_used; } } else { ++g_last_handle_used; } } } g_next_os_time = os_time_get() + OS_TICKS_PER_SEC; } #endif #if (BLETEST_THROUGHPUT_TEST == 1) /* Nothing to do if no connections */ if (!g_bletest_current_conns) { return; } /* See if it is time to start throughput testing */ if ((int32_t)(os_time_get() - g_next_os_time) >= 0) { /* Keep window full */ OS_ENTER_CRITICAL(sr); completed_pkts = g_bletest_completed_pkts; g_bletest_completed_pkts = 0; OS_EXIT_CRITICAL(sr); assert(g_bletest_outstanding_pkts >= completed_pkts); g_bletest_outstanding_pkts -= completed_pkts; while (g_bletest_outstanding_pkts < 20) { om = bletest_send_packet(g_bletest_handle); if (om) { ++g_bletest_outstanding_pkts; } } } #endif /* XXX: throughput test */ }
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; } } }