static uint32_t uuid_list_encode(const ble_advdata_uuid_list_t * p_uuid_list, uint8_t adv_type_16, uint8_t adv_type_128, uint8_t * p_encoded_data, uint16_t * p_offset, uint16_t max_size) { uint32_t err_code; // Encode 16 bit UUIDs. err_code = uuid_list_sized_encode(p_uuid_list, adv_type_16, sizeof(uint16_le_t), p_encoded_data, p_offset, max_size); VERIFY_SUCCESS(err_code); // Encode 128 bit UUIDs. err_code = uuid_list_sized_encode(p_uuid_list, adv_type_128, sizeof(ble_uuid128_t), p_encoded_data, p_offset, max_size); VERIFY_SUCCESS(err_code); return NRF_SUCCESS; }
uint32_t ant_search_init(ant_search_config_t const * p_config) { uint32_t err_code; if (p_config->low_priority_timeout == ANT_LOW_PRIORITY_SEARCH_DISABLE && p_config->high_priority_timeout == ANT_HIGH_PRIORITY_SEARCH_DISABLE) { return NRF_ERROR_INVALID_PARAM; } err_code = sd_ant_search_channel_priority_set(p_config->channel_number, p_config->search_priority); VERIFY_SUCCESS(err_code); err_code = sd_ant_search_waveform_set(p_config->channel_number, p_config->waveform); VERIFY_SUCCESS(err_code); err_code = sd_ant_channel_rx_search_timeout_set(p_config->channel_number, p_config->high_priority_timeout); VERIFY_SUCCESS(err_code); err_code = sd_ant_channel_low_priority_rx_search_timeout_set(p_config->channel_number, p_config->low_priority_timeout); VERIFY_SUCCESS(err_code); err_code = sd_ant_active_search_sharing_cycles_set(p_config->channel_number, p_config->search_sharing_cycles); return err_code; }
uint32_t ble_lbs_init(ble_lbs_t * p_lbs, const ble_lbs_init_t * p_lbs_init) { uint32_t err_code; ble_uuid_t ble_uuid; // Initialize service structure. p_lbs->conn_handle = BLE_CONN_HANDLE_INVALID; p_lbs->led_write_handler = p_lbs_init->led_write_handler; // Add service. ble_uuid128_t base_uuid = {LBS_UUID_BASE}; err_code = sd_ble_uuid_vs_add(&base_uuid, &p_lbs->uuid_type); VERIFY_SUCCESS(err_code); ble_uuid.type = p_lbs->uuid_type; ble_uuid.uuid = LBS_UUID_SERVICE; err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_lbs->service_handle); VERIFY_SUCCESS(err_code); // Add characteristics. err_code = button_char_add(p_lbs, p_lbs_init); VERIFY_SUCCESS(err_code); err_code = led_char_add(p_lbs, p_lbs_init); VERIFY_SUCCESS(err_code); return NRF_SUCCESS; }
/**@brief Function for starting advertising. */ static uint32_t advertising_start(void) { uint32_t err_code; ble_gap_adv_params_t adv_params; if ((m_flags & DFU_BLE_FLAG_IS_ADVERTISING) != 0) { return NRF_SUCCESS; } // Initialize advertising parameters (used when starting advertising). memset(&adv_params, 0, sizeof(adv_params)); err_code = advertising_init(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); VERIFY_SUCCESS(err_code); 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; err_code = sd_ble_gap_adv_start(&adv_params); VERIFY_SUCCESS(err_code); dfu_set_status(DFUS_ADVERTISING_START); m_flags |= DFU_BLE_FLAG_IS_ADVERTISING; return NRF_SUCCESS; }
static uint32_t ble_stack_init(bool init_softdevice) { uint32_t err_code; nrf_clock_lf_cfg_t clock_lf_cfg = NRF_CLOCK_LFCLKSRC; if (init_softdevice) { err_code = nrf_dfu_mbr_init_sd(); VERIFY_SUCCESS(err_code); } NRF_LOG_INFO("vector table: 0x%08x\r\n", BOOTLOADER_START_ADDR); err_code = sd_softdevice_vector_table_base_set(BOOTLOADER_START_ADDR); VERIFY_SUCCESS(err_code); SOFTDEVICE_HANDLER_APPSH_INIT(&clock_lf_cfg, true); ble_enable_params_t ble_enable_params; // Only one connection as a central is used when performing dfu. err_code = softdevice_enable_get_default_config(1, 1, &ble_enable_params); VERIFY_SUCCESS(err_code); #if (NRF_SD_BLE_API_VERSION == 3) ble_enable_params.gatt_enable_params.att_mtu = NRF_BLE_MAX_MTU_SIZE; #endif // Enable BLE stack. err_code = softdevice_enable(&ble_enable_params); return err_code; }
/**@brief Function for the GAP initialization. * * @details This function will setup all the necessary GAP (Generic Access Profile) parameters of * the device. It also sets the permissions and appearance. */ static uint32_t gap_params_init(void) { uint32_t err_code; ble_gap_conn_params_t gap_conn_params = {0}; ble_gap_conn_sec_mode_t sec_mode; BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); err_code = gap_address_change(); VERIFY_SUCCESS(err_code); err_code = sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)DEVICE_NAME, strlen(DEVICE_NAME)); VERIFY_SUCCESS(err_code); 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); return err_code; }
static uint32_t gap_address_change(void) { uint32_t err_code; ble_gap_addr_t addr; #ifdef NRF51 err_code = sd_ble_gap_address_get(&addr); #elif NRF52 err_code = sd_ble_gap_addr_get(&addr); #else #endif VERIFY_SUCCESS(err_code); // Increase the BLE address by one when advertising openly. addr.addr[0] += 1; #ifdef NRF51 err_code = sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &addr); #elif NRF52 err_code = sd_ble_gap_addr_set(&addr); #else #endif VERIFY_SUCCESS(err_code); return NRF_SUCCESS; }
uint32_t esb_init( void ) { uint32_t err_code; uint8_t base_addr_0[4] = {0xE7, 0xE7, 0xE7, 0xE7}; uint8_t base_addr_1[4] = {0xC2, 0xC2, 0xC2, 0xC2}; uint8_t addr_prefix[8] = {0xE7, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8 }; nrf_esb_config_t nrf_esb_config = NRF_ESB_DEFAULT_CONFIG; nrf_esb_config.protocol = NRF_ESB_PROTOCOL_ESB_DPL; nrf_esb_config.retransmit_delay = 600; nrf_esb_config.bitrate = NRF_ESB_BITRATE_2MBPS; nrf_esb_config.event_handler = nrf_esb_event_handler; nrf_esb_config.mode = NRF_ESB_MODE_PTX; nrf_esb_config.selective_auto_ack = false; err_code = nrf_esb_init(&nrf_esb_config); VERIFY_SUCCESS(err_code); err_code = nrf_esb_set_base_address_0(base_addr_0); VERIFY_SUCCESS(err_code); err_code = nrf_esb_set_base_address_1(base_addr_1); VERIFY_SUCCESS(err_code); err_code = nrf_esb_set_prefixes(addr_prefix, 8); VERIFY_SUCCESS(err_code); return err_code; }
uint32_t ble_dfu_transport_close(void) { uint32_t err_code = NRF_SUCCESS; if ((m_flags & DFU_BLE_FLAG_TEAR_DOWN_IN_PROGRESS) != 0) { return NRF_SUCCESS; } m_flags |= DFU_BLE_FLAG_TEAR_DOWN_IN_PROGRESS; NRF_LOG_INFO("Waiting for buffers to be cleared before disconnect\r\n"); nrf_delay_ms(MAX_CONN_INTERVAL_MS*4); NRF_LOG_INFO("Disconnecting\r\n"); if (m_conn_handle != BLE_CONN_HANDLE_INVALID) { // Disconnect from peer. err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); VERIFY_SUCCESS(err_code); } else if ((m_flags & DFU_BLE_FLAG_IS_ADVERTISING) != 0) { // If not connected, then the device will be advertising. Hence stop the advertising. err_code = advertising_stop(); VERIFY_SUCCESS(err_code); } // Stop the timer, disregard the result. (void)ble_conn_params_stop(); return err_code; }
static uint32_t dfu_sd_img_block_swap(uint32_t * src, uint32_t * dst, uint32_t len, uint32_t block_size) { // It is neccesarry to swap the new SoftDevice in 3 rounds to ensure correct copy of data // and verifucation of data in case power reset occurs during write to flash. // To ensure the robustness of swapping the images are compared backwards till start of // image swap. If the back is identical everything is swapped. uint32_t err_code = dfu_compare_block(src, dst, len); if (err_code == NRF_SUCCESS) { return err_code; } if ((uint32_t)dst > SOFTDEVICE_REGION_START) { err_code = dfu_sd_img_block_swap((uint32_t *)((uint32_t)src - block_size), (uint32_t *)((uint32_t)dst - block_size), block_size, block_size); VERIFY_SUCCESS(err_code); } err_code = dfu_copy_sd(src, dst, len); VERIFY_SUCCESS(err_code); return dfu_compare_block(src, dst, len); }
static OCStackApplicationResult AmsMgrAclReqCallback(void *ctx, OCDoHandle handle, OCClientResponse * clientResponse) { OC_LOG_V(INFO, TAG, "%s Begin", __func__ ); (void)handle; PEContext_t *context = (PEContext_t *) ctx; SRMAccessResponse_t rsps; if (!ctx || !clientResponse || !clientResponse->payload|| (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type) || (clientResponse->result != OC_STACK_OK)) { SRMSendResponse(ACCESS_DENIED_AMS_SERVICE_ERROR); goto exit; } if (context->state != AWAITING_AMS_RESPONSE) { OC_LOG_V(ERROR, TAG, "%s Invalid State ", __func__); context->retVal = ACCESS_DENIED_AMS_SERVICE_ERROR; SRMSendResponse(context->retVal); return OC_STACK_DELETE_TRANSACTION; } // Verify before installing ACL if the ID of the sender of this ACL is an AMS //service that this device trusts. rsps = ACCESS_DENIED; if((UUID_LENGTH == clientResponse->identity.id_length) && memcmp(context->amsMgrContext->amsDeviceId.id, clientResponse->identity.id, sizeof(context->amsMgrContext->amsDeviceId.id)) == 0) { OCStackResult ret = InstallNewACL(((OCSecurityPayload*)clientResponse->payload)->securityData); VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR); OC_LOG_V(INFO, TAG, "%s : Calling checkPermission", __func__); rsps = CheckPermission(context, &context->subject, context->resource, context->permission); VERIFY_SUCCESS(TAG, (true == IsAccessGranted(rsps)), ERROR); OC_LOG_V(INFO, TAG, "%sAccess granted, Calling SRMCallCARequestHandler", __func__); context->retVal = ACCESS_GRANTED; SRMSendResponse(context->retVal); return OC_STACK_DELETE_TRANSACTION; } exit: context->retVal = ACCESS_DENIED_AMS_SERVICE_ERROR; SRMSendResponse(context->retVal); FreeCARequestInfo(context->amsMgrContext->requestInfo); OICFree(context->amsMgrContext->endpoint); return OC_STACK_DELETE_TRANSACTION; }
uint32_t app_uart_init(const app_uart_comm_params_t * p_comm_params, app_uart_buffers_t * p_buffers, app_uart_event_handler_t event_handler, app_irq_priority_t irq_priority) { uint32_t err_code; m_event_handler = event_handler; if (p_buffers == NULL) { return NRF_ERROR_INVALID_PARAM; } // Configure buffer RX buffer. err_code = app_fifo_init(&m_rx_fifo, p_buffers->rx_buf, p_buffers->rx_buf_size); VERIFY_SUCCESS(err_code); // Configure buffer TX buffer. err_code = app_fifo_init(&m_tx_fifo, p_buffers->tx_buf, p_buffers->tx_buf_size); VERIFY_SUCCESS(err_code); nrf_drv_uart_config_t config = NRF_DRV_UART_DEFAULT_CONFIG; config.baudrate = (nrf_uart_baudrate_t)p_comm_params->baud_rate; config.hwfc = (p_comm_params->flow_control == APP_UART_FLOW_CONTROL_DISABLED) ? NRF_UART_HWFC_DISABLED : NRF_UART_HWFC_ENABLED; config.interrupt_priority = irq_priority; config.parity = p_comm_params->use_parity ? NRF_UART_PARITY_INCLUDED : NRF_UART_PARITY_EXCLUDED; config.pselcts = p_comm_params->cts_pin_no; config.pselrts = p_comm_params->rts_pin_no; config.pselrxd = p_comm_params->rx_pin_no; config.pseltxd = p_comm_params->tx_pin_no; err_code = nrf_drv_uart_init(&app_uart_inst, &config, uart_event_handler); VERIFY_SUCCESS(err_code); m_rx_ovf = false; // Turn on receiver if RX pin is connected if (p_comm_params->rx_pin_no != UART_PIN_DISCONNECTED) { #ifdef UARTE_PRESENT if (!config.use_easy_dma) #endif { nrf_drv_uart_rx_enable(&app_uart_inst); } return nrf_drv_uart_rx(&app_uart_inst, rx_buffer,1); } else { return NRF_SUCCESS; } }
uint32_t app_uart_flush(void) { uint32_t err_code; err_code = app_fifo_flush(&m_rx_fifo); VERIFY_SUCCESS(err_code); err_code = app_fifo_flush(&m_tx_fifo); VERIFY_SUCCESS(err_code); return NRF_SUCCESS; }
uint32_t ble_dfu_init(ble_dfu_t * p_dfu, ble_dfu_init_t * p_dfu_init) { if ((p_dfu == NULL) || (p_dfu_init == NULL) || (p_dfu_init->evt_handler == NULL)) { return NRF_ERROR_NULL; } p_dfu->conn_handle = BLE_CONN_HANDLE_INVALID; ble_uuid_t service_uuid; uint32_t err_code; const ble_uuid128_t base_uuid128 = { { 0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15, 0xDE, 0xEF, 0x12, 0x12, 0x00, 0x00, 0x00, 0x00 } }; service_uuid.uuid = BLE_DFU_SERVICE_UUID; err_code = sd_ble_uuid_vs_add(&base_uuid128, &(service_uuid.type)); VERIFY_SUCCESS(err_code); err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &service_uuid, &(p_dfu->service_handle)); VERIFY_SUCCESS(err_code); p_dfu->uuid_type = service_uuid.type; err_code = dfu_pkt_char_add(p_dfu); VERIFY_SUCCESS(err_code); err_code = dfu_ctrl_pt_add(p_dfu); VERIFY_SUCCESS(err_code); err_code = dfu_rev_char_add(p_dfu, p_dfu_init); VERIFY_SUCCESS(err_code); p_dfu->evt_handler = p_dfu_init->evt_handler; if (p_dfu_init->error_handler != NULL) { p_dfu->error_handler = p_dfu_init->error_handler; } m_is_dfu_service_initialized = true; return NRF_SUCCESS; }
uint32_t ble_ancs_c_request_attrs(ble_ancs_c_t * p_ancs, const ble_ancs_c_evt_notif_t * p_notif) { uint32_t err_code; err_code = ble_ancs_verify_notification_format(p_notif); VERIFY_SUCCESS(err_code); err_code = ble_ancs_get_notif_attrs(p_ancs, p_notif->notif_uid); p_ancs->parse_state = COMMAND_ID_AND_NOTIF_UID; VERIFY_SUCCESS(err_code); return NRF_SUCCESS; }
uint32_t dfu_sd_image_swap(void) { bootloader_settings_t boot_settings; bootloader_settings_get(&boot_settings); if (boot_settings.sd_image_size == 0) { return NRF_SUCCESS; } if ((SOFTDEVICE_REGION_START + boot_settings.sd_image_size) > boot_settings.sd_image_start) { uint32_t err_code; uint32_t sd_start = SOFTDEVICE_REGION_START; uint32_t block_size = (boot_settings.sd_image_start - sd_start) / 2; uint32_t image_end = boot_settings.sd_image_start + boot_settings.sd_image_size; uint32_t img_block_start = boot_settings.sd_image_start + 2 * block_size; uint32_t sd_block_start = sd_start + 2 * block_size; if (SD_SIZE_GET(MBR_SIZE) < boot_settings.sd_image_size) { // This will clear a page thus ensuring the old image is invalidated before swapping. err_code = dfu_copy_sd((uint32_t *)(sd_start + block_size), (uint32_t *)(sd_start + block_size), sizeof(uint32_t)); VERIFY_SUCCESS(err_code); err_code = dfu_copy_sd((uint32_t *)sd_start, (uint32_t *)sd_start, sizeof(uint32_t)); VERIFY_SUCCESS(err_code); } return dfu_sd_img_block_swap((uint32_t *)img_block_start, (uint32_t *)sd_block_start, image_end - img_block_start, block_size); } else { if (boot_settings.sd_image_size != 0) { return dfu_copy_sd((uint32_t *)boot_settings.sd_image_start, (uint32_t *)SOFTDEVICE_REGION_START, boot_settings.sd_image_size); } } return NRF_SUCCESS; }
static uint32_t appearance_encode(uint8_t * p_encoded_data, uint16_t * p_offset, uint16_t max_size) { uint32_t err_code; uint16_t appearance; // Check for buffer overflow. if (((*p_offset) + AD_TYPE_APPEARANCE_SIZE) > max_size) { return NRF_ERROR_DATA_SIZE; } // Get GAP appearance field. err_code = sd_ble_gap_appearance_get(&appearance); VERIFY_SUCCESS(err_code); // Encode Length, AD Type and Appearance. p_encoded_data[*p_offset] = (uint8_t)(ADV_AD_TYPE_FIELD_SIZE + AD_TYPE_APPEARANCE_DATA_SIZE); *p_offset += ADV_LENGTH_FIELD_SIZE; p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_APPEARANCE; *p_offset += ADV_AD_TYPE_FIELD_SIZE; *p_offset += uint16_encode(appearance, &p_encoded_data[*p_offset]); return NRF_SUCCESS; }
uint32_t ble_lbs_c_init(ble_lbs_c_t * p_ble_lbs_c, ble_lbs_c_init_t * p_ble_lbs_c_init) { uint32_t err_code; ble_uuid_t lbs_uuid; ble_uuid128_t lbs_base_uuid = {LBS_UUID_BASE}; VERIFY_PARAM_NOT_NULL(p_ble_lbs_c); VERIFY_PARAM_NOT_NULL(p_ble_lbs_c_init); VERIFY_PARAM_NOT_NULL(p_ble_lbs_c_init->evt_handler); p_ble_lbs_c->peer_lbs_db.button_cccd_handle = BLE_GATT_HANDLE_INVALID; p_ble_lbs_c->peer_lbs_db.button_handle = BLE_GATT_HANDLE_INVALID; p_ble_lbs_c->peer_lbs_db.led_handle = BLE_GATT_HANDLE_INVALID; p_ble_lbs_c->conn_handle = BLE_CONN_HANDLE_INVALID; p_ble_lbs_c->evt_handler = p_ble_lbs_c_init->evt_handler; err_code = sd_ble_uuid_vs_add(&lbs_base_uuid, &p_ble_lbs_c->uuid_type); if (err_code != NRF_SUCCESS) { return err_code; } VERIFY_SUCCESS(err_code); lbs_uuid.type = p_ble_lbs_c->uuid_type; lbs_uuid.uuid = LBS_UUID_SERVICE; return ble_db_discovery_evt_register(&lbs_uuid); }
uint32_t ble_nus_c_init(ble_nus_c_t * p_ble_nus_c, ble_nus_c_init_t * p_ble_nus_c_init) { uint32_t err_code; ble_uuid_t uart_uuid; ble_uuid128_t nus_base_uuid = NUS_BASE_UUID; VERIFY_PARAM_NOT_NULL(p_ble_nus_c); VERIFY_PARAM_NOT_NULL(p_ble_nus_c_init); err_code = sd_ble_uuid_vs_add(&nus_base_uuid, &p_ble_nus_c->uuid_type); VERIFY_SUCCESS(err_code); uart_uuid.type = p_ble_nus_c->uuid_type; uart_uuid.uuid = BLE_UUID_NUS_SERVICE; // save the pointer to the ble_uart_c_t struct locally mp_ble_nus_c = p_ble_nus_c; p_ble_nus_c->conn_handle = BLE_CONN_HANDLE_INVALID; p_ble_nus_c->evt_handler = p_ble_nus_c_init->evt_handler; p_ble_nus_c->nus_rx_handle = BLE_GATT_HANDLE_INVALID; p_ble_nus_c->nus_tx_handle = BLE_GATT_HANDLE_INVALID; return ble_db_discovery_evt_register(&uart_uuid, db_discover_evt_handler); }
uint32_t app_uart_init(const app_uart_comm_params_t * p_comm_params, app_uart_buffers_t * p_buffers, app_uart_event_handler_t event_handler, app_irq_priority_t irq_priority) { nrf_drv_uart_config_t config = NRF_DRV_UART_DEFAULT_CONFIG; config.baudrate = (nrf_uart_baudrate_t)p_comm_params->baud_rate; config.hwfc = (p_comm_params->flow_control == APP_UART_FLOW_CONTROL_DISABLED) ? NRF_UART_HWFC_DISABLED : NRF_UART_HWFC_ENABLED; config.interrupt_priority = irq_priority; config.parity = p_comm_params->use_parity ? NRF_UART_PARITY_INCLUDED : NRF_UART_PARITY_EXCLUDED; config.pselcts = p_comm_params->cts_pin_no; config.pselrts = p_comm_params->rts_pin_no; config.pselrxd = p_comm_params->rx_pin_no; config.pseltxd = p_comm_params->tx_pin_no; m_event_handler = event_handler; rx_done = false; uint32_t err_code = nrf_drv_uart_init(&app_uart_inst, &config, uart_event_handler); VERIFY_SUCCESS(err_code); // Turn on receiver if RX pin is connected if (p_comm_params->rx_pin_no != UART_PIN_DISCONNECTED) { return nrf_drv_uart_rx(&app_uart_inst, rx_buffer,1); } else { return NRF_SUCCESS; } }
static uint32_t conn_int_encode(const ble_advdata_conn_int_t * p_conn_int, uint8_t * p_encoded_data, uint16_t * p_offset, uint16_t max_size) { uint32_t err_code; // Check for buffer overflow. if (((*p_offset) + AD_TYPE_CONN_INT_SIZE) > max_size) { return NRF_ERROR_DATA_SIZE; } // Check parameters. err_code = conn_int_check(p_conn_int); VERIFY_SUCCESS(err_code); // Encode Length and AD Type. p_encoded_data[*p_offset] = (uint8_t)(ADV_AD_TYPE_FIELD_SIZE + AD_TYPE_CONN_INT_DATA_SIZE); *p_offset += ADV_LENGTH_FIELD_SIZE; p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_SLAVE_CONNECTION_INTERVAL_RANGE; *p_offset += ADV_AD_TYPE_FIELD_SIZE; // Encode Minimum and Maximum Connection Intervals. *p_offset += uint16_encode(p_conn_int->min_conn_interval, &p_encoded_data[*p_offset]); *p_offset += uint16_encode(p_conn_int->max_conn_interval, &p_encoded_data[*p_offset]); return NRF_SUCCESS; }
ret_code_t nrf_pwr_mgmt_init(uint32_t ticks_per_1s) { NRF_LOG_INFO("Init\r\n"); DEBUG_PINS_INIT(); SLEEP_INIT(); #if NRF_PWR_MGMT_CONFIG_CPU_USAGE_MONITOR_ENABLED m_max_cpu_usage = 0; m_ticks_sleeping = 0; m_ticks_last = 0; #endif // NRF_PWR_MGMT_CONFIG_CPU_USAGE_MONITOR_ENABLED #if NRF_PWR_MGMT_CONFIG_STANDBY_TIMEOUT_ENABLED m_standby_counter = 0; #endif // NRF_PWR_MGMT_CONFIG_STANDBY_TIMEOUT_ENABLED m_sysoff_guard = false; m_next_handler = 0; #if APP_TIMER_REQUIRED ret_code_t ret_code = app_timer_create(&m_pwr_mgmt_timer, APP_TIMER_MODE_REPEATED, nrf_pwr_mgmt_timeout_handler); VERIFY_SUCCESS(ret_code); return app_timer_start(m_pwr_mgmt_timer, ticks_per_1s, NULL); #else return NRF_SUCCESS; #endif // APP_TIMER_REQUIRED }
static ret_code_t cc310_backend_uninit(void) { #if defined(NRF_CRYPTO_RNG_AUTO_INIT_ENABLED) && (NRF_CRYPTO_RNG_AUTO_INIT_ENABLED == 1) uint32_t ret_val; ret_val = nrf_crypto_rng_init(NULL, NULL); VERIFY_SUCCESS(ret_val); #elif defined(NRF_CRYPTO_RNG_AUTO_INIT_ENABLED) && (NRF_CRYPTO_RNG_AUTO_INIT_ENABLED == 0) // Do nothing #else #warning NRF_CRYPTO_RNG_AUTO_INIT_ENABLED define not found in sdk_config.h (Is the sdk_config.h valid?). #endif // NRF_CRYPTO_RNG_AUTO_INIT_ENABLED // Initialize the CC310 HW to do shutdown. NRF_CRYPTOCELL->ENABLE = 1; SaSi_LibFini(); // Shut down CC310 after shutdown. NRF_CRYPTOCELL->ENABLE = 0; return NRF_SUCCESS; }
ret_code_t ant_stack_encryption_config(ant_encrypt_stack_settings_t const * const p_crypto_set) { ret_code_t err_code; for ( uint32_t i = 0; i < p_crypto_set->key_number; i++) { err_code = sd_ant_crypto_key_set(i, p_crypto_set->pp_key[i]); VERIFY_SUCCESS(err_code); } if (p_crypto_set->p_adv_burst_config != NULL) { err_code = ant_enc_advance_burs_config_apply(p_crypto_set->p_adv_burst_config); VERIFY_SUCCESS(err_code); } // subcomands LUT for @ref sd_ant_crypto_info_set calls const uint8_t set_enc_info_param_lut[] = { ENCRYPTION_INFO_SET_CRYPTO_ID, ENCRYPTION_INFO_SET_CUSTOM_USER_DATA, ENCRYPTION_INFO_SET_RNG_SEED }; for ( uint32_t i = 0; i < sizeof(set_enc_info_param_lut); i++) { if ( p_crypto_set->info.pp_array[i] != NULL) { err_code = sd_ant_crypto_info_set(set_enc_info_param_lut[i], p_crypto_set->info.pp_array[i]); VERIFY_SUCCESS(err_code); } } #ifdef ANT_ENCRYPT_NEGOTIATION_SLAVE_ENABLED // all ANT channels have unsupported slave encryption tracking (even master's channel) ant_channel_encryp_negotiation_slave_init(); #endif m_ant_enc_evt_handler = NULL; m_stack_encryption_configured = true; return NRF_SUCCESS; }
uint32_t ble_advdata_set(const ble_advdata_t * p_advdata, const ble_advdata_t * p_srdata) { uint32_t err_code; uint16_t len_advdata = BLE_GAP_ADV_MAX_SIZE; uint16_t len_srdata = BLE_GAP_ADV_MAX_SIZE; uint8_t encoded_advdata[BLE_GAP_ADV_MAX_SIZE]; uint8_t encoded_srdata[BLE_GAP_ADV_MAX_SIZE]; uint8_t * p_encoded_advdata; uint8_t * p_encoded_srdata; // Encode advertising data (if supplied). if (p_advdata != NULL) { err_code = advdata_check(p_advdata); VERIFY_SUCCESS(err_code); err_code = adv_data_encode(p_advdata, encoded_advdata, &len_advdata); VERIFY_SUCCESS(err_code); p_encoded_advdata = encoded_advdata; } else { p_encoded_advdata = NULL; len_advdata = 0; } // Encode scan response data (if supplied). if (p_srdata != NULL) { err_code = srdata_check(p_srdata); VERIFY_SUCCESS(err_code); err_code = adv_data_encode(p_srdata, encoded_srdata, &len_srdata); VERIFY_SUCCESS(err_code); p_encoded_srdata = encoded_srdata; } else { p_encoded_srdata = NULL; len_srdata = 0; } // Pass encoded advertising data and/or scan response data to the stack. return sd_ble_gap_adv_data_set(p_encoded_advdata, len_advdata, p_encoded_srdata, len_srdata); }
uint32_t ble_dfu_init(ble_dfu_t * p_dfu, const ble_dfu_init_t * p_dfu_init) { uint32_t err_code; ble_uuid_t ble_uuid; ble_uuid128_t nus_base_uuid = BLE_DFU_BASE_UUID; VERIFY_PARAM_NOT_NULL(p_dfu); VERIFY_PARAM_NOT_NULL(p_dfu_init); p_m_dfu = p_dfu; // TODO: find a nicer solution to this // Initialize the service structure. p_dfu->conn_handle = BLE_CONN_HANDLE_INVALID; p_dfu->evt_handler = p_dfu_init->evt_handler; p_dfu->is_waiting_for_disconnection = false; p_dfu->is_ctrlpt_notification_enabled = false; /**@snippet [Adding proprietary Service to S110 SoftDevice] */ // Add a custom base UUID. err_code = sd_ble_uuid_vs_add(&nus_base_uuid, &p_dfu->uuid_type); VERIFY_SUCCESS(err_code); ble_uuid.type = p_dfu->uuid_type; ble_uuid.uuid = BLE_UUID_DFU_SERVICE; // Add the service. err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_dfu->service_handle); /**@snippet [Adding proprietary Service to S110 SoftDevice] */ VERIFY_SUCCESS(err_code); // Add the RX Characteristic. err_code = rx_char_add(p_dfu, p_dfu_init); VERIFY_SUCCESS(err_code); err_code = nrf_dfu_flash_init(true); VERIFY_SUCCESS(err_code); nrf_dfu_settings_init(); return NRF_SUCCESS; }
uint32_t ble_ancs_c_init(ble_ancs_c_t * p_ancs, const ble_ancs_c_init_t * p_ancs_init) { uint32_t err_code; //Verify that the parameters needed for to initialize this instance of ANCS are not NULL. VERIFY_PARAM_NOT_NULL(p_ancs); VERIFY_PARAM_NOT_NULL(p_ancs_init); VERIFY_PARAM_NOT_NULL(p_ancs_init->evt_handler); p_ancs->parse_state = COMMAND_ID_AND_NOTIF_UID; p_ancs->p_data_dest = NULL; p_ancs->current_attr_index = 0; p_ancs->evt_handler = p_ancs_init->evt_handler; p_ancs->error_handler = p_ancs_init->error_handler; p_ancs->conn_handle = BLE_CONN_HANDLE_INVALID; p_ancs->service.data_source_cccd.uuid.uuid = BLE_UUID_DESCRIPTOR_CLIENT_CHAR_CONFIG; p_ancs->service.notif_source_cccd.uuid.uuid = BLE_UUID_DESCRIPTOR_CLIENT_CHAR_CONFIG; // Make sure instance of service is clear. GATT handles inside the service and characteristics are set to @ref BLE_GATT_HANDLE_INVALID. memset(&p_ancs->service, 0, sizeof(ble_ancs_c_service_t)); memset(m_tx_buffer, 0, TX_BUFFER_SIZE); // Assign UUID types. err_code = sd_ble_uuid_vs_add(&ble_ancs_base_uuid128, &p_ancs->service.service.uuid.type); VERIFY_SUCCESS(err_code); err_code = sd_ble_uuid_vs_add(&ble_ancs_cp_base_uuid128, &p_ancs->service.control_point_char.uuid.type); VERIFY_SUCCESS(err_code); err_code = sd_ble_uuid_vs_add(&ble_ancs_ns_base_uuid128, &p_ancs->service.notif_source_char.uuid.type); VERIFY_SUCCESS(err_code); err_code = sd_ble_uuid_vs_add(&ble_ancs_ds_base_uuid128, &p_ancs->service.data_source_char.uuid.type); VERIFY_SUCCESS(err_code); // Assign UUID to the service. p_ancs->service.service.uuid.uuid = ANCS_UUID_SERVICE; p_ancs->service.service.uuid.type = p_ancs->service.service.uuid.type; return ble_db_discovery_evt_register(&p_ancs->service.service.uuid); }
ret_code_t nfc_t4t_file_control_tlv_parse(nfc_t4t_tlv_block_t * p_file_control_tlv, uint8_t * p_raw_data, uint16_t * p_len) { ret_code_t err_code; uint8_t * p_offset = p_raw_data; if (*p_len < TLV_MIN_TL_FIELD_LEN) { return NRF_ERROR_INVALID_LENGTH; } memset(p_file_control_tlv, 0, sizeof(nfc_t4t_tlv_block_t)); // Handle type field of TLV block. p_file_control_tlv->type = *p_offset; p_offset += TLV_TYPE_FIELD_LEN; // Handle length field of TLV block. if (*p_offset == TLV_LEN_LONG_FORMAT_TOKEN) { if (*p_len < TLV_MIN_LONG_FORMAT_TL_FIELD_LEN) { return NRF_ERROR_INVALID_LENGTH; } p_file_control_tlv->length = uint16_big_decode(p_offset + TLV_LEN_LONG_FORMAT_TOKEN_SIZE); p_offset += TLV_LEN_LONG_FIELD_LEN; if (p_file_control_tlv->length < TLV_LEN_LONG_FORMAT_MIN_VALUE) { return NRF_ERROR_INVALID_DATA; } } else { p_file_control_tlv->length = *p_offset; p_offset += TLV_LEN_SHORT_FIELD_LEN; } // Calculate the total TLV block size. uint16_t tlv_block_len = (p_offset - p_raw_data) + p_file_control_tlv->length; if (*p_len < tlv_block_len) { return NRF_ERROR_INVALID_LENGTH; } *p_len = tlv_block_len; // Validate if type and length fields contain values supported by Type 4 Tag. err_code = nfc_t4t_file_control_tl_validate(p_file_control_tlv); VERIFY_SUCCESS(err_code); // Handle value field of TLV block. err_code = nfc_t4t_file_control_value_parse(p_file_control_tlv, p_offset); return err_code; }
char * BinToPstatJSON(const OicSecPstat_t * pstat, const bool isIncResName) { if(NULL == pstat) { return NULL; } cJSON *jsonPstat = NULL; char *jsonStr = NULL; cJSON *jsonSmArray = NULL; char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(((OicUuid_t*) 0)->id)) + 1] = {}; uint32_t outLen = 0; B64Result b64Ret = B64_OK; cJSON *jsonRoot = NULL; if(isIncResName) { jsonRoot = cJSON_CreateObject(); VERIFY_NON_NULL(TAG, jsonRoot, INFO); cJSON_AddItemToObject(jsonRoot, OIC_JSON_PSTAT_NAME, jsonPstat = cJSON_CreateObject()); } else { jsonPstat = cJSON_CreateObject(); jsonRoot = jsonPstat; } VERIFY_NON_NULL(TAG, jsonPstat, INFO); cJSON_AddBoolToObject(jsonPstat, OIC_JSON_ISOP_NAME, pstat->isOp); b64Ret = b64Encode(pstat->deviceID.id, sizeof(pstat->deviceID.id), base64Buff, sizeof(base64Buff), &outLen); VERIFY_SUCCESS(TAG, b64Ret == B64_OK, ERROR); cJSON_AddStringToObject(jsonPstat, OIC_JSON_DEVICE_ID_NAME, base64Buff); cJSON_AddNumberToObject(jsonPstat, OIC_JSON_COMMIT_HASH_NAME, pstat->commitHash); cJSON_AddNumberToObject(jsonPstat, OIC_JSON_CM_NAME, (int)pstat->cm); cJSON_AddNumberToObject(jsonPstat, OIC_JSON_TM_NAME, (int)pstat->tm); cJSON_AddNumberToObject(jsonPstat, OIC_JSON_OM_NAME, (int)pstat->om); cJSON_AddItemToObject(jsonPstat, OIC_JSON_SM_NAME, jsonSmArray = cJSON_CreateArray()); VERIFY_NON_NULL(TAG, jsonSmArray, INFO); for (size_t i = 0; i < pstat->smLen; i++) { cJSON_AddItemToArray(jsonSmArray, cJSON_CreateNumber((int )pstat->sm[i])); } jsonStr = cJSON_Print(jsonRoot); exit: if (jsonRoot) { cJSON_Delete(jsonRoot); } return jsonStr; }
uint32_t ble_dfu_buttonless_bootloader_start_finalize(void) { uint32_t err_code; NRF_LOG_DEBUG("In ble_dfu_buttonless_bootloader_start_finalize\r\n"); err_code = sd_power_gpregret_clr(0, 0xffffffff); VERIFY_SUCCESS(err_code); err_code = sd_power_gpregret_set(0, BOOTLOADER_DFU_START); VERIFY_SUCCESS(err_code); // Indicate that the Secure DFU bootloader will be entered m_dfu.evt_handler(BLE_DFU_EVT_BOOTLOADER_ENTER); // Signal that DFU mode is to be enter to the power management module nrf_pwr_mgmt_shutdown(NRF_PWR_MGMT_SHUTDOWN_GOTO_DFU); return NRF_SUCCESS; }