static struct os_mbuf * bletest_send_packet(uint16_t handle) { int j; uint8_t val; struct os_mbuf *om; uint16_t pktlen; om = bletest_get_packet(); if (om) { /* set payload length */ #if BLETEST_THROUGHPUT_TEST pktlen = BLETEST_PKT_SIZE; #else #if (BLETEST_CFG_RAND_PKT_SIZE == 1) pktlen = rand() % (BLETEST_MAX_PKT_SIZE + 1); #else pktlen = BLETEST_PKT_SIZE; #endif #endif /* Put the HCI header in the mbuf */ put_le16(om->om_data, handle); put_le16(om->om_data + 2, pktlen + 4); /* Place L2CAP header in packet */ put_le16(om->om_data + 4, pktlen); om->om_data[6] = 0; om->om_data[7] = 0; om->om_len = 8; OS_MBUF_PKTHDR(om)->omp_len = 8; /* Fill with incrementing pattern (starting from 1) */ for (j = 0; j < pktlen; ++j) { val = j + 1; os_mbuf_append(om, &val, 1); } /* Transmit it */ ble_hci_trans_hs_acl_tx(om); } return om; }
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; } }