uint32_t conn_mw_ble_gap_tx_power_set(uint8_t const * const p_rx_buf, uint32_t rx_buf_len, uint8_t * const p_tx_buf, uint32_t * const p_tx_buf_len) { SER_ASSERT_NOT_NULL(p_rx_buf); SER_ASSERT_NOT_NULL(p_tx_buf); SER_ASSERT_NOT_NULL(p_tx_buf_len); int8_t tx_power; uint32_t err_code = NRF_SUCCESS; uint32_t sd_err_code; #if NRF_SD_BLE_API_VERSION > 5 uint8_t role; uint16_t handle; err_code = ble_gap_tx_power_set_req_dec(p_rx_buf, rx_buf_len, &role, &handle, &tx_power); SER_ASSERT(err_code == NRF_SUCCESS, err_code); sd_err_code = sd_ble_gap_tx_power_set(role, handle, tx_power); #else err_code = ble_gap_tx_power_set_req_dec(p_rx_buf, rx_buf_len, &tx_power); SER_ASSERT(err_code == NRF_SUCCESS, err_code); sd_err_code = sd_ble_gap_tx_power_set(tx_power); #endif err_code = ble_gap_tx_power_set_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len); SER_ASSERT(err_code == NRF_SUCCESS, err_code); return err_code; }
/**@brief Function for the GAP initialization. * * @details This function sets up all the necessary GAP (Generic Access Profile) parameters of the * device including the device name, appearance, and the preferred connection parameters. */ static void gap_params_init(void) { uint32_t err_code; ble_gap_conn_params_t gap_conn_params; ble_gap_conn_sec_mode_t sec_mode; ble_gap_addr_t device_mac; #ifdef RANDOM_MAC device_mac.addr_type = BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE; err_code = sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_AUTO, &device_mac); #else uint8_t mac_addr[] = {0x00,0x19,0x25,0x39,0x54,0x95}; device_mac.addr_type = BLE_GAP_ADDR_TYPE_PUBLIC; memcpy(device_mac.addr, mac_addr,6); err_code = sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &device_mac); APP_ERROR_CHECK(err_code); #endif BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); err_code = sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)DEVICE_NAME, strlen(DEVICE_NAME)); APP_ERROR_CHECK(err_code); err_code = sd_ble_gap_appearance_set(BLE_APPEARANCE_GENERIC_TAG); APP_ERROR_CHECK(err_code); memset(&gap_conn_params, 0, sizeof(gap_conn_params)); gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL; gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL; gap_conn_params.slave_latency = SLAVE_LATENCY; gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT; err_code = sd_ble_gap_ppcp_set(&gap_conn_params); APP_ERROR_CHECK(err_code); err_code = sd_ble_gap_tx_power_set(4); APP_ERROR_CHECK(err_code); }
/**@brief Function for the GAP initialization. * * @details This function sets up all the necessary GAP (Generic Access Profile) parameters of the * device including the device name, appearance, and the preferred connection parameters. */ static void gap_params_init(void) { uint32_t err_code; ble_gap_conn_params_t gap_conn_params; ble_gap_conn_sec_mode_t sec_mode; BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); err_code = sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)DEVICE_NAME, strlen(DEVICE_NAME)); APP_ERROR_CHECK(err_code); err_code = sd_ble_gap_appearance_set(BLE_APPEARANCE_GENERIC_KEYRING); APP_ERROR_CHECK(err_code); memset(&gap_conn_params, 0, sizeof(gap_conn_params)); gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL; gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL; gap_conn_params.slave_latency = SLAVE_LATENCY; gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT; err_code = sd_ble_gap_ppcp_set(&gap_conn_params); APP_ERROR_CHECK(err_code); err_code = sd_ble_gap_tx_power_set(TX_POWER_LEVEL); APP_ERROR_CHECK(err_code); }
static void gap_params_init(void) { uint32_t err_code; ble_gap_conn_sec_mode_t sec_mode; // Set the power. Using really low (-30) doesn't really work sd_ble_gap_tx_power_set(4); // Configure something about the gap // Make the connection open cause yea BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); // Set the name of the device so its easier to find err_code = sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)DEVICE_NAME, strlen(DEVICE_NAME)); APP_ERROR_CHECK(err_code); // Set an appearance; we don't use this //err_code = sd_ble_gap_appearance_set(BLE_APPEARANCE_GENERIC_WATCH); //APP_ERROR_CHECK(err_code); // memset(&gap_conn_params, 0, sizeof(gap_conn_params)); // gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL; // gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL; // gap_conn_params.slave_latency = SLAVE_LATENCY; // gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT; // err_code = sd_ble_gap_ppcp_set(&gap_conn_params); // APP_ERROR_CHECK(err_code); }
//////////////////////////////////////////////////////////////////////////////// // // GAP Initialization static void gap_params_init(void) { // Create the scan response name memset(_scanResponse, 0, sizeof(_scanResponse)); memcpy(_scanResponse, SCAN_RESPONSE_BASE, strlen(SCAN_RESPONSE_BASE)); memcpy(_scanResponse + strlen(SCAN_RESPONSE_BASE), STR(BUILD_NUM), 4); uint32_t err_code; ble_gap_conn_params_t gap_conn_params; ble_gap_conn_sec_mode_t sec_mode; BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); err_code = sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)_scanResponse, strlen(_scanResponse)); APP_ERROR_CHECK(err_code); err_code = sd_ble_gap_appearance_set(BLE_APPEARANCE_UNKNOWN); APP_ERROR_CHECK(err_code); err_code = sd_ble_gap_tx_power_set(4); APP_ERROR_CHECK(err_code); memset(&gap_conn_params, 0, sizeof(gap_conn_params)); gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL; gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL; gap_conn_params.slave_latency = SLAVE_LATENCY; gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT; err_code = sd_ble_gap_ppcp_set(&gap_conn_params); APP_ERROR_CHECK(err_code); }
// gap name/appearance/connection parameters static void gap_params_init (void) { uint32_t err_code; ble_gap_conn_sec_mode_t sec_mode; ble_gap_conn_params_t gap_conn_params; // Full strength signal sd_ble_gap_tx_power_set(4); // Let anyone connect and set the name given the platform BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); err_code = sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)"BEES", strlen("BEES")); //(const uint8_t *)DEVICE_NAME, strlen(DEVICE_NAME)); APP_ERROR_CHECK(err_code); // Not sure what this is useful for, but why not set it err_code = sd_ble_gap_appearance_set(BLE_APPEARANCE_GENERIC_COMPUTER); APP_ERROR_CHECK(err_code); // Specify parameters for a connection memset(&gap_conn_params, 0, sizeof(gap_conn_params)); gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL; gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL; gap_conn_params.slave_latency = SLAVE_LATENCY; gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT; err_code = sd_ble_gap_ppcp_set(&gap_conn_params); APP_ERROR_CHECK(err_code); }
/**@brief Set the radio's transmit power. * * @param[in] tx_power Radio transmit power in dBm (accepted values are -40, -30, -20, -16, -12, -8, -4, 0, and 4 dBm). * * @note -40 dBm will not actually give -40 dBm, but will instead be remapped to -30 dBm. */ void ble_device_set_tx_power( int8_t tx_power) { uint32_t err_code; err_code = sd_ble_gap_tx_power_set(tx_power); APP_ERROR_CHECK(err_code); }
/**@brief Function for the GAP initialization. * * @details This function sets up all the necessary GAP (Generic Access Profile) parameters of the * device including the device name, appearance, and the preferred connection parameters. */ static void gap_params_init(void) { uint32_t err_code; ble_gap_conn_params_t gap_conn_params; ble_gap_conn_sec_mode_t sec_mode; BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); err_code = sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)DEVICE_NAME, strlen(DEVICE_NAME)); APP_ERROR_CHECK(err_code); err_code = sd_ble_gap_appearance_set(BLE_APPEARANCE_UNKNOWN); APP_ERROR_CHECK(err_code); memset(&gap_conn_params, 0, sizeof(gap_conn_params)); gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL; gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL; gap_conn_params.slave_latency = SLAVE_LATENCY; gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT; err_code = sd_ble_gap_ppcp_set(&gap_conn_params); APP_ERROR_CHECK(err_code); err_code = sd_ble_gap_tx_power_set(INITIAL_TX_POWER_LEVEL); APP_ERROR_CHECK(err_code); ble_opt_t ble_opt; uint8_t passkey[6] = { 0 }; // Fetch pin from UICR uint8_t val = (*((uint32_t *)UICR_PIN_ADDRESS)) & 0x000000FF; passkey[0] = (val == 0xFF) ? 0x31 : val; val = ((*((uint32_t *)UICR_PIN_ADDRESS)) & 0x0000FF00) >> 8; passkey[1] = (val == 0xFF) ? 0x32 : val; val = ((*((uint32_t *)UICR_PIN_ADDRESS)) & 0x00FF0000) >> 16; passkey[2] = (val == 0xFF) ? 0x33 : val; val = ((*((uint32_t *)UICR_PIN_ADDRESS)) & 0xFF000000) >> 24; passkey[3] = (val == 0xFF) ? 0x34 : val; val = (*((uint32_t *)(UICR_PIN_ADDRESS+4))) & 0x000000FF; passkey[4] = (val == 0xFF) ? 0x35 : val; val = ((*((uint32_t *)(UICR_PIN_ADDRESS+4))) & 0x0000FF00) >> 8; passkey[5] = (val == 0xFF) ? 0x36 : val; ble_opt.gap_opt.passkey.p_passkey = passkey; err_code = sd_ble_opt_set(BLE_GAP_OPT_PASSKEY, &ble_opt); APP_ERROR_CHECK(err_code); }
error_t btle_gap_init(void) { ble_gap_conn_params_t gap_conn_params = {0}; gap_conn_params.min_conn_interval = msec_to_1_25msec( CFG_GAP_CONNECTION_MIN_INTERVAL_MS); // in 1.25ms units gap_conn_params.max_conn_interval = msec_to_1_25msec( CFG_GAP_CONNECTION_MAX_INTERVAL_MS); // in 1.25ms unit gap_conn_params.slave_latency = CFG_GAP_CONNECTION_SLAVE_LATENCY; gap_conn_params.conn_sup_timeout = CFG_GAP_CONNECTION_SUPERVISION_TIMEOUT_MS / 10; // in 10ms unit ble_gap_conn_sec_mode_t sec_mode; BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); // no security is needed // ASSERT_STATUS( sd_ble_gap_device_name_set(&sec_mode, //_modify // (const uint8_t *) // CFG_GAP_LOCAL_NAME, // strlen(CFG_GAP_LOCAL_NAME))); // ASSERT_STATUS( sd_ble_gap_appearance_set(CFG_GAP_APPEARANCE)); // ASSERT_STATUS( sd_ble_gap_ppcp_set(&gap_conn_params)); // ASSERT_STATUS( sd_ble_gap_tx_power_set(CFG_BLE_TX_POWER_LEVEL)); nrf_err_check( sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *) CFG_GAP_LOCAL_NAME, strlen(CFG_GAP_LOCAL_NAME)) ); nrf_err_check( sd_ble_gap_appearance_set(CFG_GAP_APPEARANCE) ); nrf_err_check( sd_ble_gap_ppcp_set(&gap_conn_params) ); nrf_err_check( sd_ble_gap_tx_power_set(CFG_BLE_TX_POWER_LEVEL) ); /* Connection Parameters */ enum { FIRST_UPDATE_DELAY = APP_TIMER_TICKS(5000, CFG_TIMER_PRESCALER), NEXT_UPDATE_DELAY = APP_TIMER_TICKS(5000, CFG_TIMER_PRESCALER), MAX_UPDATE_COUNT = 3 }; ble_conn_params_init_t cp_init = {0}; cp_init.p_conn_params = NULL; cp_init.first_conn_params_update_delay = FIRST_UPDATE_DELAY; cp_init.next_conn_params_update_delay = NEXT_UPDATE_DELAY; cp_init.max_conn_params_update_count = MAX_UPDATE_COUNT; cp_init.start_on_notify_cccd_handle = BLE_GATT_HANDLE_INVALID; cp_init.disconnect_on_fail = true; cp_init.evt_handler = NULL; cp_init.error_handler = error_callback; //ASSERT_STATUS ( ble_conn_params_init(&cp_init)); //_modify nrf_err_check( ble_conn_params_init(&cp_init) ); return ERROR_NONE; }
/* (Valid values are -40, -20, -16, -12, -8, -4, 0, 4) */ ble_error_t nRF5xGap::setTxPower(int8_t txPower) { unsigned rc; if ((rc = sd_ble_gap_tx_power_set(txPower)) != NRF_SUCCESS) { switch (rc) { case NRF_ERROR_BUSY: return BLE_STACK_BUSY; case NRF_ERROR_INVALID_PARAM: default: return BLE_ERROR_PARAM_OUT_OF_RANGE; } } return BLE_ERROR_NONE; }
/**@brief Function handling events from @ref es_adv_timing.c. * * @param[in] p_evt Advertisement timing event. */ static void adv_timing_callback(const es_adv_timing_evt_t * p_evt) { ret_code_t err_code; ble_gap_adv_params_t non_connectable_adv_params; const es_slot_reg_t * p_reg = es_slot_get_registry(); // As new advertisement data will be loaded, stop advertising. err_code = sd_ble_gap_adv_stop(); if (err_code != NRF_ERROR_INVALID_STATE) { APP_ERROR_CHECK(err_code); } // If a non-eTLM frame is to be advertised. if (p_evt->evt_id == ES_ADV_TIMING_EVT_ADV_SLOT) { err_code = sd_ble_gap_tx_power_set(p_reg->slots[p_evt->slot_no].radio_tx_pwr); APP_ERROR_CHECK(err_code); es_adv_frame_fill_non_connectable_adv_data(p_evt->slot_no, false); } // If an eTLM frame is to be advertised else if (p_evt->evt_id == ES_ADV_TIMING_EVT_ADV_ETLM) { err_code = sd_ble_gap_tx_power_set(p_reg->slots[p_reg->tlm_slot].radio_tx_pwr); APP_ERROR_CHECK(err_code); es_adv_frame_fill_non_connectable_adv_data(p_evt->slot_no, true); } invoke_callback(ES_ADV_EVT_NON_CONN_ADV); get_adv_params(&non_connectable_adv_params, true, m_remain_connectable); adv_start(&non_connectable_adv_params); }
/**@brief Function for handling events coming from the S132 SoftDevice * @param[in] slot Slot index * @param[out] p_eddystone_data_array buffer array to which the adv data is retrieved */ static void fetch_adv_data_from_slot( uint8_t slot, uint8_array_t * p_eddystone_data_array ) { eddystone_adv_slot_params_t eddystone_adv_slot_params; eddystone_adv_slot_params_get(slot, &eddystone_adv_slot_params); sd_ble_gap_tx_power_set(eddystone_adv_slot_params.radio_tx_pwr); switch (eddystone_adv_slot_params.frame_type) { case EDDYSTONE_FRAME_TYPE_UID: p_eddystone_data_array->p_data = (uint8_t *) &(eddystone_adv_slot_params.p_adv_frame->uid); p_eddystone_data_array->size = sizeof(eddystone_uid_frame_t); break; case EDDYSTONE_FRAME_TYPE_URL: p_eddystone_data_array->p_data = (uint8_t *) &(eddystone_adv_slot_params.p_adv_frame->url); p_eddystone_data_array->size = eddystone_adv_slot_params.url_frame_length; break; case EDDYSTONE_FRAME_TYPE_TLM: //If there are EIDs, broadcast eTLM, else just TLM if (eddystone_adv_slot_num_of_current_eids(NULL, NULL) != 0) { //The current EIK slot that is paired with the current eTLM slot uint8_t eik_pair_slot = m_etlm_adv_counter.eid_positions[m_etlm_adv_counter.eid_slot_counter]; eddystone_tlm_manager_etlm_get(eik_pair_slot, &(eddystone_adv_slot_params.p_adv_frame->etlm)); p_eddystone_data_array->p_data = (uint8_t *) &(eddystone_adv_slot_params.p_adv_frame->etlm); p_eddystone_data_array->size = sizeof(eddystone_etlm_frame_t); } //Just plain TLM else { eddystone_tlm_manager_tlm_get(&(eddystone_adv_slot_params.p_adv_frame->tlm)); p_eddystone_data_array->p_data = (uint8_t *) &(eddystone_adv_slot_params.p_adv_frame->tlm); p_eddystone_data_array->size = sizeof(eddystone_tlm_frame_t); } break; case EDDYSTONE_FRAME_TYPE_EID: p_eddystone_data_array->p_data = (uint8_t *) &(eddystone_adv_slot_params.p_adv_frame->eid); p_eddystone_data_array->size = sizeof(eddystone_eid_frame_t); break; default: APP_ERROR_CHECK(NRF_ERROR_INVALID_DATA); //Should never happen! break; } }
/**@brief Function for the GAP initialization. * * @details This function sets up all the necessary GAP (Generic Access Profile) parameters of the * device including the device name, appearance, and the preferred connection parameters. */ static void gap_params_init(void) { uint32_t err_code; ble_gap_conn_params_t gap_conn_params; ble_gap_conn_sec_mode_t sec_mode; BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); err_code = sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)DEVICE_NAME, strlen(DEVICE_NAME)); if (err_code == NRF_SUCCESS) debug_printf("Device name set!\r\n"); else { debug_printf("Ooops.. Something went wrong with setting the name..\r\n"); APP_ERROR_CHECK(err_code); } err_code = sd_ble_gap_appearance_set(BLE_APPEARANCE_UNKNOWN); if (err_code == NRF_SUCCESS) debug_printf("Appearance set!\r\n"); else { debug_printf("Ooops.. Something is wrong with setting the appearrance..\r\n"); APP_ERROR_CHECK(err_code); } memset(&gap_conn_params, 0, sizeof(gap_conn_params)); gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL; gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL; gap_conn_params.slave_latency = SLAVE_LATENCY; gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT; err_code = sd_ble_gap_ppcp_set(&gap_conn_params); if (err_code == NRF_SUCCESS) debug_printf("Connection parameters set!\r\n"); else { debug_printf("Ooops.. Something is wrong with setting connection parameters..\r\n"); APP_ERROR_CHECK(err_code); } sd_ble_gap_tx_power_set(4); }
/**@brief Start advertising. */ static void advertising_start(void) { uint32_t err_code; ble_gap_adv_params_t adv_params; // Start advertising memset(&adv_params, 0, sizeof(adv_params)); adv_params.type = BLE_GAP_ADV_TYPE_ADV_IND; adv_params.p_peer_addr = NULL; adv_params.fp = BLE_GAP_ADV_FP_ANY; adv_params.interval = APP_ADV_INTERVAL; adv_params.timeout = APP_ADV_TIMEOUT_IN_SECONDS; //tx_power Radio transmit power in dBm (accepted values are -40, -30, -20, -16, -12, -8, -4, 0, and 4 dBm) err_code = sd_ble_gap_tx_power_set(OUTPUT_POWER); APP_ERROR_CHECK(err_code); err_code = sd_ble_gap_adv_start(&adv_params); APP_ERROR_CHECK(err_code); }
/**@brief Function for the GAP initialization. * * @details This function sets up all the necessary GAP (Generic Access Profile) parameters of the * device including the device name, appearance, and the preferred connection parameters. */ void gap_params_init(void) { uint32_t err_code = NRF_SUCCESS; ble_gap_conn_params_t gap_conn_params; ble_gap_conn_sec_mode_t sec_mode; BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); sd_ble_gap_tx_power_set(gap_tx_power); /* err_code = sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)DEVICE_NAME, strlen(DEVICE_NAME)); */ APP_ERROR_CHECK(err_code); memset(&gap_conn_params, 0, sizeof(gap_conn_params)); gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL; gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL; gap_conn_params.slave_latency = SLAVE_LATENCY; gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT; err_code = sd_ble_gap_ppcp_set(&gap_conn_params); APP_ERROR_CHECK(err_code); ble_gap_addr_t gap_address; memset(&gap_address, 0, sizeof(gap_address)); //TODO: BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE & IRK gap_address.addr_type = BLE_GAP_ADDR_TYPE_PUBLIC; for (uint8_t i = 0; i < BLE_GAP_ADDR_LEN; i++) //Only last byte differs. { gap_address.addr[BLE_GAP_ADDR_LEN - 1 - i] = l_device_address[i]; } //TODO: BLE_GAP_ADDR_CYCLE_MODE_AUTO err_code = sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &gap_address); APP_ERROR_CHECK(err_code);// Check for errors }
bool nRF51822::setTxPower(int txPower) { if (txPower <= -40) { txPower = -40; } else if (txPower <= -30) { txPower = -30; } else if (txPower <= -20) { txPower = -20; } else if (txPower <= -16) { txPower = -16; } else if (txPower <= -12) { txPower = -12; } else if (txPower <= -8) { txPower = -8; } else if (txPower <= -4) { txPower = -4; } else if (txPower <= 0) { txPower = 0; } else { txPower = 4; } return (sd_ble_gap_tx_power_set(txPower) == NRF_SUCCESS); }
uint32_t conn_mw_ble_gap_tx_power_set(uint8_t const * const p_rx_buf, uint32_t rx_buf_len, uint8_t * const p_tx_buf, uint32_t * const p_tx_buf_len) { SER_ASSERT_NOT_NULL(p_rx_buf); SER_ASSERT_NOT_NULL(p_tx_buf); SER_ASSERT_NOT_NULL(p_tx_buf_len); int8_t tx_power; uint32_t err_code = NRF_SUCCESS; uint32_t sd_err_code; err_code = ble_gap_tx_power_set_req_dec(p_rx_buf, rx_buf_len, &tx_power); SER_ASSERT(err_code == NRF_SUCCESS, err_code); sd_err_code = sd_ble_gap_tx_power_set(tx_power); err_code = ble_gap_tx_power_set_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len); SER_ASSERT(err_code == NRF_SUCCESS, err_code); return err_code; }
void nRF51822::begin(unsigned char advertisementDataType, unsigned char advertisementDataLength, const unsigned char* advertisementData, unsigned char scanDataType, unsigned char scanDataLength, const unsigned char* scanData, BLELocalAttribute** localAttributes, unsigned char numLocalAttributes, BLERemoteAttribute** remoteAttributes, unsigned char numRemoteAttributes) { #ifdef __RFduino__ sd_softdevice_enable(NRF_CLOCK_LFCLKSRC_SYNTH_250_PPM, NULL); #else sd_softdevice_enable(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, NULL); // sd_nvic_EnableIRQ(SWI2_IRQn); #endif #ifdef NRF51_S130 ble_enable_params_t enableParams = { .gatts_enable_params = { .service_changed = true } }; sd_ble_enable(&enableParams); #endif #ifdef NRF_51822_DEBUG ble_version_t version; sd_ble_version_get(&version); Serial.print(F("version = ")); Serial.print(version.version_number); Serial.print(F(" ")); Serial.print(version.company_id); Serial.print(F(" ")); Serial.print(version.subversion_number); Serial.println(); #endif ble_gap_conn_params_t gap_conn_params; gap_conn_params.min_conn_interval = 40; // in 1.25ms units gap_conn_params.max_conn_interval = 80; // in 1.25ms unit gap_conn_params.slave_latency = 0; gap_conn_params.conn_sup_timeout = 4000 / 10; // in 10ms unit sd_ble_gap_ppcp_set(&gap_conn_params); sd_ble_gap_tx_power_set(0); unsigned char srData[31]; unsigned char srDataLen = 0; this->_advDataLen = 0; // flags this->_advData[this->_advDataLen + 0] = 2; this->_advData[this->_advDataLen + 1] = 0x01; this->_advData[this->_advDataLen + 2] = 0x06; this->_advDataLen += 3; if (advertisementDataType && advertisementDataLength && advertisementData) { this->_advData[this->_advDataLen + 0] = advertisementDataLength + 1; this->_advData[this->_advDataLen + 1] = advertisementDataType; this->_advDataLen += 2; memcpy(&this->_advData[this->_advDataLen], advertisementData, advertisementDataLength); this->_advDataLen += advertisementDataLength; } if (scanDataType && scanDataLength && scanData) { srData[0] = scanDataLength + 1; srData[1] = scanDataType; memcpy(&srData[2], scanData, scanDataLength); srDataLen = 2 + scanDataLength; } sd_ble_gap_adv_data_set(this->_advData, this->_advDataLen, srData, srDataLen); sd_ble_gap_appearance_set(0); for (int i = 0; i < numLocalAttributes; i++) { BLELocalAttribute *localAttribute = localAttributes[i]; if (localAttribute->type() == BLETypeCharacteristic) { this->_numLocalCharacteristics++; } } this->_numLocalCharacteristics -= 3; // 0x2a00, 0x2a01, 0x2a05 this->_localCharacteristicInfo = (struct localCharacteristicInfo*)malloc(sizeof(struct localCharacteristicInfo) * this->_numLocalCharacteristics); unsigned char localCharacteristicIndex = 0; uint16_t handle = 0; BLEService *lastService = NULL; for (int i = 0; i < numLocalAttributes; i++) { BLELocalAttribute *localAttribute = localAttributes[i]; BLEUuid uuid = BLEUuid(localAttribute->uuid()); const unsigned char* uuidData = uuid.data(); unsigned char value[255]; ble_uuid_t nordicUUID; if (uuid.length() == 2) { nordicUUID.uuid = (uuidData[1] << 8) | uuidData[0]; nordicUUID.type = BLE_UUID_TYPE_BLE; } else { unsigned char uuidDataTemp[16]; memcpy(&uuidDataTemp, uuidData, sizeof(uuidDataTemp)); nordicUUID.uuid = (uuidData[13] << 8) | uuidData[12]; uuidDataTemp[13] = 0; uuidDataTemp[12] = 0; sd_ble_uuid_vs_add((ble_uuid128_t*)&uuidDataTemp, &nordicUUID.type); } if (localAttribute->type() == BLETypeService) { BLEService *service = (BLEService *)localAttribute; if (strcmp(service->uuid(), "1800") == 0 || strcmp(service->uuid(), "1801") == 0) { continue; // skip } sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &nordicUUID, &handle); lastService = service; } else if (localAttribute->type() == BLETypeCharacteristic) { BLECharacteristic *characteristic = (BLECharacteristic *)localAttribute; if (strcmp(characteristic->uuid(), "2a00") == 0) { ble_gap_conn_sec_mode_t secMode; BLE_GAP_CONN_SEC_MODE_SET_OPEN(&secMode); // no security is needed sd_ble_gap_device_name_set(&secMode, characteristic->value(), characteristic->valueLength()); } else if (strcmp(characteristic->uuid(), "2a01") == 0) { const uint16_t *appearance = (const uint16_t*)characteristic->value(); sd_ble_gap_appearance_set(*appearance); } else if (strcmp(characteristic->uuid(), "2a05") == 0) { // do nothing } else { uint8_t properties = characteristic->properties() & 0xfe; uint16_t valueLength = characteristic->valueLength(); this->_localCharacteristicInfo[localCharacteristicIndex].characteristic = characteristic; this->_localCharacteristicInfo[localCharacteristicIndex].notifySubscribed = false; this->_localCharacteristicInfo[localCharacteristicIndex].indicateSubscribed = false; this->_localCharacteristicInfo[localCharacteristicIndex].service = lastService; ble_gatts_char_md_t characteristicMetaData; ble_gatts_attr_md_t clientCharacteristicConfigurationMetaData; ble_gatts_attr_t characteristicValueAttribute; ble_gatts_attr_md_t characteristicValueAttributeMetaData; memset(&characteristicMetaData, 0, sizeof(characteristicMetaData)); memcpy(&characteristicMetaData.char_props, &properties, 1); characteristicMetaData.p_char_user_desc = NULL; characteristicMetaData.p_char_pf = NULL; characteristicMetaData.p_user_desc_md = NULL; characteristicMetaData.p_cccd_md = NULL; characteristicMetaData.p_sccd_md = NULL; if (properties & (BLENotify | BLEIndicate)) { memset(&clientCharacteristicConfigurationMetaData, 0, sizeof(clientCharacteristicConfigurationMetaData)); BLE_GAP_CONN_SEC_MODE_SET_OPEN(&clientCharacteristicConfigurationMetaData.read_perm); BLE_GAP_CONN_SEC_MODE_SET_OPEN(&clientCharacteristicConfigurationMetaData.write_perm); clientCharacteristicConfigurationMetaData.vloc = BLE_GATTS_VLOC_STACK; characteristicMetaData.p_cccd_md = &clientCharacteristicConfigurationMetaData; } memset(&characteristicValueAttributeMetaData, 0, sizeof(characteristicValueAttributeMetaData)); if (properties & (BLERead | BLENotify | BLEIndicate)) { if (this->_bondStore) { BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&characteristicValueAttributeMetaData.read_perm); } else { BLE_GAP_CONN_SEC_MODE_SET_OPEN(&characteristicValueAttributeMetaData.read_perm); } } if (properties & (BLEWriteWithoutResponse | BLEWrite)) { if (this->_bondStore) { BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&characteristicValueAttributeMetaData.write_perm); } else { BLE_GAP_CONN_SEC_MODE_SET_OPEN(&characteristicValueAttributeMetaData.write_perm); } } characteristicValueAttributeMetaData.vloc = BLE_GATTS_VLOC_STACK; characteristicValueAttributeMetaData.rd_auth = 0; characteristicValueAttributeMetaData.wr_auth = 0; characteristicValueAttributeMetaData.vlen = !characteristic->fixedLength(); for (int j = (i + 1); j < numLocalAttributes; j++) { localAttribute = localAttributes[j]; if (localAttribute->type() != BLETypeDescriptor) { break; } BLEDescriptor *descriptor = (BLEDescriptor *)localAttribute; if (strcmp(descriptor->uuid(), "2901") == 0) { characteristicMetaData.p_char_user_desc = (uint8_t*)descriptor->value(); characteristicMetaData.char_user_desc_max_size = descriptor->valueLength(); characteristicMetaData.char_user_desc_size = descriptor->valueLength(); } else if (strcmp(descriptor->uuid(), "2904") == 0) { characteristicMetaData.p_char_pf = (ble_gatts_char_pf_t *)descriptor->value(); } } memset(&characteristicValueAttribute, 0, sizeof(characteristicValueAttribute)); characteristicValueAttribute.p_uuid = &nordicUUID; characteristicValueAttribute.p_attr_md = &characteristicValueAttributeMetaData; characteristicValueAttribute.init_len = valueLength; characteristicValueAttribute.init_offs = 0; characteristicValueAttribute.max_len = characteristic->valueSize(); characteristicValueAttribute.p_value = NULL; sd_ble_gatts_characteristic_add(BLE_GATT_HANDLE_INVALID, &characteristicMetaData, &characteristicValueAttribute, &this->_localCharacteristicInfo[localCharacteristicIndex].handles); if (valueLength) { for (int j = 0; j < valueLength; j++) { value[j] = (*characteristic)[j]; } sd_ble_gatts_value_set(this->_localCharacteristicInfo[localCharacteristicIndex].handles.value_handle, 0, &valueLength, value); } localCharacteristicIndex++; } } else if (localAttribute->type() == BLETypeDescriptor) { BLEDescriptor *descriptor = (BLEDescriptor *)localAttribute; if (strcmp(descriptor->uuid(), "2901") == 0 || strcmp(descriptor->uuid(), "2902") == 0 || strcmp(descriptor->uuid(), "2903") == 0 || strcmp(descriptor->uuid(), "2904") == 0) { continue; // skip } uint16_t valueLength = descriptor->valueLength(); ble_gatts_attr_t descriptorAttribute; ble_gatts_attr_md_t descriptorMetaData; memset(&descriptorAttribute, 0, sizeof(descriptorAttribute)); memset(&descriptorMetaData, 0, sizeof(descriptorMetaData)); descriptorMetaData.vloc = BLE_GATTS_VLOC_STACK; descriptorMetaData.vlen = (valueLength == descriptor->valueLength()) ? 0 : 1; if (this->_bondStore) { BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&descriptorMetaData.read_perm); } else { BLE_GAP_CONN_SEC_MODE_SET_OPEN(&descriptorMetaData.read_perm); } descriptorAttribute.p_uuid = &nordicUUID; descriptorAttribute.p_attr_md = &descriptorMetaData; descriptorAttribute.init_len = valueLength; descriptorAttribute.max_len = descriptor->valueLength(); descriptorAttribute.p_value = NULL; sd_ble_gatts_descriptor_add(BLE_GATT_HANDLE_INVALID, &descriptorAttribute, &handle); if (valueLength) { for (int j = 0; j < valueLength; j++) { value[j] = (*descriptor)[j]; } sd_ble_gatts_value_set(handle, 0, &valueLength, value); } } } if ( numRemoteAttributes > 0) { numRemoteAttributes -= 2; // 0x1801, 0x2a05 } for (int i = 0; i < numRemoteAttributes; i++) { BLERemoteAttribute *remoteAttribute = remoteAttributes[i]; if (remoteAttribute->type() == BLETypeService) { this->_numRemoteServices++; } else if (remoteAttribute->type() == BLETypeCharacteristic) { this->_numRemoteCharacteristics++; } } this->_remoteServiceInfo = (struct remoteServiceInfo*)malloc(sizeof(struct remoteServiceInfo) * this->_numRemoteServices); this->_remoteCharacteristicInfo = (struct remoteCharacteristicInfo*)malloc(sizeof(struct remoteCharacteristicInfo) * this->_numRemoteCharacteristics); BLERemoteService *lastRemoteService = NULL; unsigned char remoteServiceIndex = 0; unsigned char remoteCharacteristicIndex = 0; for (int i = 0; i < numRemoteAttributes; i++) { BLERemoteAttribute *remoteAttribute = remoteAttributes[i]; BLEUuid uuid = BLEUuid(remoteAttribute->uuid()); const unsigned char* uuidData = uuid.data(); ble_uuid_t nordicUUID; if (uuid.length() == 2) { nordicUUID.uuid = (uuidData[1] << 8) | uuidData[0]; nordicUUID.type = BLE_UUID_TYPE_BLE; } else { unsigned char uuidDataTemp[16]; memcpy(&uuidDataTemp, uuidData, sizeof(uuidDataTemp)); nordicUUID.uuid = (uuidData[13] << 8) | uuidData[12]; uuidDataTemp[13] = 0; uuidDataTemp[12] = 0; sd_ble_uuid_vs_add((ble_uuid128_t*)&uuidDataTemp, &nordicUUID.type); } if (remoteAttribute->type() == BLETypeService) { this->_remoteServiceInfo[remoteServiceIndex].service = lastRemoteService = (BLERemoteService *)remoteAttribute; this->_remoteServiceInfo[remoteServiceIndex].uuid = nordicUUID; memset(&this->_remoteServiceInfo[remoteServiceIndex].handlesRange, 0, sizeof(this->_remoteServiceInfo[remoteServiceIndex].handlesRange)); remoteServiceIndex++; } else if (remoteAttribute->type() == BLETypeCharacteristic) { this->_remoteCharacteristicInfo[remoteCharacteristicIndex].characteristic = (BLERemoteCharacteristic *)remoteAttribute; this->_remoteCharacteristicInfo[remoteCharacteristicIndex].service = lastRemoteService; this->_remoteCharacteristicInfo[remoteCharacteristicIndex].uuid = nordicUUID; memset(&this->_remoteCharacteristicInfo[remoteCharacteristicIndex].properties, 0, sizeof(this->_remoteCharacteristicInfo[remoteCharacteristicIndex].properties)); this->_remoteCharacteristicInfo[remoteCharacteristicIndex].valueHandle = 0; remoteCharacteristicIndex++; } } if (this->_bondStore && this->_bondStore->hasData()) { #ifdef NRF_51822_DEBUG Serial.println(F("Restoring bond data")); #endif #ifdef NRF51_S130 this->_bondStore->getData(this->_bondData, 0, sizeof(this->_bondData)); #else this->_bondStore->getData(this->_authStatusBuffer, 0, sizeof(this->_authStatusBuffer)); #endif } this->startAdvertising(); #ifdef __RFduino__ RFduinoBLE_enabled = 1; #endif }
/*JSON{ "type" : "staticmethod", "class" : "NRF", "name" : "setTxPower", "generate" : "jswrap_nrf_bluetooth_setTxPower", "params" : [ ["power","int","Transmit power. Accepted values are -40, -30, -20, -16, -12, -8, -4, 0, and 4 dBm. Others will give an error code."] ] } Set the BLE radio transmit power. The default TX power is 0 dBm. */ void jswrap_nrf_bluetooth_setTxPower(JsVarInt pwr) { uint32_t err_code; err_code = sd_ble_gap_tx_power_set(pwr); if (err_code) jsExceptionHere(JSET_ERROR, "Got BLE error code %d", err_code); }
/**@snippet [Handling the data received over BLE] */ static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length) { uint32_t err_code; if (length==1) //< Control Command { switch(p_data[0]) { /** Radio transmit power in dBm * (accepted values are -40, -30, -20, -16, -12, -8, -4, 0, and 4 dBm). */ case '1': err_code = ble_nus_string_send(p_nus, (uint8_t *) "Power set to -40", 16); APP_ERROR_CHECK(err_code); gap_tx_power = -40; err_code = sd_ble_gap_tx_power_set(gap_tx_power); APP_ERROR_CHECK(err_code); break; case '2': err_code = ble_nus_string_send(p_nus, (uint8_t *) "Power set to -20", 16); APP_ERROR_CHECK(err_code); gap_tx_power = -20; err_code = sd_ble_gap_tx_power_set(gap_tx_power); APP_ERROR_CHECK(err_code); break; case '3': err_code = ble_nus_string_send(p_nus, (uint8_t *) "Power set to 0", 14); APP_ERROR_CHECK(err_code); gap_tx_power = 0; err_code = sd_ble_gap_tx_power_set(gap_tx_power); APP_ERROR_CHECK(err_code); break; case 'c': ; storage_clear(); err_code = ble_nus_string_send(p_nus, (uint8_t *) "Storage cleared.", 16); APP_ERROR_CHECK(err_code); break; case 'l': ; char str_l[17]; sprintf(str_l, "Toggled lock to %d", storage_toggle_lock()); ble_nus_string_send(p_nus, (uint8_t *) str_l, 18); break; case 'r': store_read_init(); read_store_data(m_nus_data, &m_data_length); nrf_delay_ms(MAX_CONN_INTERVAL * 2); m_file_in_transit = true; err_code = ble_nus_string_send(p_nus, (uint8_t *) "***DATA TRANSFER***", 19); APP_ERROR_CHECK(err_code); break; case 't': ; char str_t[20]; uint8_t c; uint32_t ts = NRF_RTC1->COUNTER; c = sprintf(str_t, "ts:%lu epoch:%d", ts, timer_epoch); err_code = ble_nus_string_send(p_nus, (uint8_t *) str_t, c); APP_ERROR_CHECK(err_code); break; } } __LOG("NUSCON:%s", p_data); }