/** * Generates the Ri byte used in the confirm message. On success, the byte is * written to the supplied procedure object. */ static int ble_sm_sc_gen_ri(struct ble_sm_proc *proc) { int byte; int bit; int rc; switch (proc->pair_alg) { case BLE_SM_PAIR_ALG_JW: case BLE_SM_PAIR_ALG_NUMCMP: proc->ri = 0; return 0; case BLE_SM_PAIR_ALG_PASSKEY: BLE_HS_DBG_ASSERT(proc->passkey_bits_exchanged < BLE_SM_SC_PASSKEY_BITS); byte = proc->passkey_bits_exchanged / 8; bit = proc->passkey_bits_exchanged % 8; proc->ri = 0x80 | !!(proc->tk[byte] & (1 << bit)); proc->passkey_bits_exchanged++; return 0; case BLE_SM_PAIR_ALG_OOB: rc = ble_hs_hci_util_rand(&proc->ri, 1); return rc; default: BLE_HS_DBG_ASSERT(0); return BLE_HS_EUNKNOWN; } }
/** * Generates a new random address. This function does not configure the device * with the new address; the caller can use the address in subsequent * operations. * * @param nrpa The type of random address to generate: * 0: static * 1: non-resolvable private * @param out_addr On success, the generated address gets written * here. * * @return 0 on success; nonzero on failure. */ int ble_hs_id_gen_rnd(int nrpa, uint8_t *out_addr) { int rc; rc = ble_hs_hci_util_rand(out_addr, 6); if (rc != 0) { return rc; } if (nrpa) { out_addr[5] &= ~0xc0; } else { out_addr[5] |= 0xc0; } return 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); } }