/** * \callgraph *\brief Initialize the blocks of flash memory used for reading/writing. *\details Uses Nordic's pstorage software abstraction/API to cleanly access flash when the SoftDevice (for BLE) * is also running. */ void ladybug_flash_init() { SEGGER_RTT_WriteString(0,"==> IN ladybug_flash_init\n"); pstorage_module_param_t pstorage_param; //Used when registering with pstorage pstorage_handle_t handle; //used to access the chunk-o-flash requested when registering //First thing is to initialize pstorage uint32_t err_code = pstorage_init(); //if initialization was not successful, the error routine will be called and the code won't proceed. APP_ERROR_CHECK(err_code); //Next is to register amount of flash needed. The smallest amount that can be requested is 16 bytes. I'm requesting //32 bytes because this is the number of bytes needed for plant_info. Two blocks are used...one for plant info and one for hydro values. I must make a //block request for the larger amount of bytes pstorage_param.block_size = BLOCK_SIZE; //request three blocks - one will be for pH, one for plant_info, and one for device name pstorage_param.block_count = 3; //assign a callback so know when a command has finished. pstorage_param.cb = ladybug_flash_handler; err_code = pstorage_register(&pstorage_param, &handle); APP_ERROR_CHECK(err_code); //Then get the handles to the blocks of flash pstorage_block_identifier_get(&handle, 0, &m_block_calibration_store_handle); pstorage_block_identifier_get(&handle,1,&m_block_plantInfo_store_handle); pstorage_block_identifier_get(&handle,2,&m_block_device_name_store_handle); // Create the timer. This will be called before a Flash activity is requested to avoid forever hanging. create_timer(); }
Storage::Storage() { u32 err; //Register with Terminal Terminal::AddTerminalCommandListener(this); //Initialize queue for queueing store and load tasks taskQueue = new SimpleQueue(taskBuffer, TASK_BUFFER_LENGTH); //Initialize pstorage library pstorage_module_param_t param; u8 num_blocks = STORAGE_BLOCK_NUMBER; bufferedOperationInProgress = false; param.block_size = STORAGE_BLOCK_SIZE; //Multiple of 4 and divisor of page size (pagesize is usually 1024) in case total size is bigger than page size param.block_count = num_blocks; param.cb = PstorageEventHandler; err = pstorage_init(); APP_ERROR_CHECK(err); //Register a number of blocks if (pstorage_register(¶m, &handle) != NRF_SUCCESS){ logt("STORAGE", "Could not register storage"); } for (u32 i = 0; i < num_blocks; ++i){ pstorage_block_identifier_get(&handle, i, &block_handles[i]); } }
/**@brief Function for the Device Manager initialization. */ static void device_manager_init(void) { uint32_t err_code; dm_init_param_t init_data; dm_application_param_t register_param; // Initialize persistent storage module. err_code = pstorage_init(); APP_ERROR_CHECK(err_code); // Clear all bonded centrals if the "delete all bonds" button is pushed. err_code = bsp_button_is_pressed(BOND_DELETE_ALL_WAKEUP_BUTTON_ID,&(init_data.clear_persistent_data)); APP_ERROR_CHECK(err_code); err_code = dm_init(&init_data); APP_ERROR_CHECK(err_code); memset(®ister_param.sec_param, 0, sizeof(ble_gap_sec_params_t)); register_param.sec_param.timeout = SEC_PARAM_TIMEOUT; register_param.sec_param.bond = SEC_PARAM_BOND; register_param.sec_param.mitm = SEC_PARAM_MITM; register_param.sec_param.io_caps = SEC_PARAM_IO_CAPABILITIES; register_param.sec_param.oob = SEC_PARAM_OOB; register_param.sec_param.min_key_size = SEC_PARAM_MIN_KEY_SIZE; register_param.sec_param.max_key_size = SEC_PARAM_MAX_KEY_SIZE; register_param.evt_handler = device_manager_evt_handler; register_param.service_type = DM_PROTOCOL_CNTXT_GATT_SRVR_ID; err_code = dm_register(&m_app_handle, ®ister_param); APP_ERROR_CHECK(err_code); }
uint32_t bootloader_dfu_start(void) { uint32_t err_code = NRF_SUCCESS; pstorage_module_param_t storage_params; storage_params.cb = pstorage_callback_handler; storage_params.block_size = sizeof(bootloader_settings_t); storage_params.block_count = 1; err_code = pstorage_init(); if (err_code != NRF_SUCCESS) { return err_code; } err_code = pstorage_register(&storage_params, &m_bootsettings_handle); if (err_code != NRF_SUCCESS) { return err_code; } // Clear swap if banked update is used. err_code = dfu_init(); if (err_code != NRF_SUCCESS) { return err_code; } err_code = dfu_transport_update_start(); wait_for_events(); return err_code; }
/**@brief Function for the Device Manager initialization. */ static void device_manager_init(void) { uint32_t err_code; dm_init_param_t init_data; dm_application_param_t register_param; // Initialize persistent storage module. err_code = pstorage_init(); APP_ERROR_CHECK(err_code); // Clear all bonded centrals if the Bonds Delete button is pushed. init_data.clear_persistent_data = true;//(nrf_gpio_pin_read(BOND_DELETE_ALL_BUTTON_ID) == 0); err_code = dm_init(&init_data); APP_ERROR_CHECK(err_code); memset(®ister_param.sec_param, 0, sizeof(ble_gap_sec_params_t)); //register_param.sec_param.timeout = SEC_PARAM_TIMEOUT; register_param.sec_param.bond = SEC_PARAM_BOND; register_param.sec_param.mitm = SEC_PARAM_MITM; register_param.sec_param.io_caps = SEC_PARAM_IO_CAPABILITIES; register_param.sec_param.oob = SEC_PARAM_OOB; register_param.sec_param.min_key_size = SEC_PARAM_MIN_KEY_SIZE; register_param.sec_param.max_key_size = SEC_PARAM_MAX_KEY_SIZE; register_param.evt_handler = device_manager_evt_handler; register_param.service_type = DM_PROTOCOL_CNTXT_NONE; err_code = dm_register(&g_AppHandle, ®ister_param); APP_ERROR_CHECK(err_code); }
/**@brief Function for the Device Manager initialization. * * @param[in] erase_bonds Indicates whether bonding information should be cleared from * persistent storage during initialization of the Device Manager. */ static void device_manager_init(bool erase_bonds) { uint32_t err_code; dm_init_param_t init_param = {.clear_persistent_data = erase_bonds}; dm_application_param_t register_param; // Initialize persistent storage module. err_code = pstorage_init(); APP_ERROR_CHECK(err_code); err_code = dm_init(&init_param); APP_ERROR_CHECK(err_code); memset(®ister_param.sec_param, 0, sizeof(ble_gap_sec_params_t)); register_param.sec_param.bond = SEC_PARAM_BOND; register_param.sec_param.mitm = SEC_PARAM_MITM; register_param.sec_param.io_caps = SEC_PARAM_IO_CAPABILITIES; register_param.sec_param.oob = SEC_PARAM_OOB; register_param.sec_param.min_key_size = SEC_PARAM_MIN_KEY_SIZE; register_param.sec_param.max_key_size = SEC_PARAM_MAX_KEY_SIZE; register_param.evt_handler = device_manager_evt_handler; register_param.service_type = DM_PROTOCOL_CNTXT_GATT_SRVR_ID; err_code = dm_register(&m_app_handle, ®ister_param); APP_ERROR_CHECK(err_code); app_bond_init(&m_app_bond_table); for(uint8_t i = 0; i < DEVICE_MANAGER_MAX_BONDS; i++) { APP_LOG("[APP][ID: %d], Application context : %08X\r\n",m_app_bond_table.device_id[i],(unsigned int) m_app_bond_table.app_bond_cnt[i]); } }
/* Platform-specific implementation for persistence on the nRF5x. Based on the * pstorage module provided by the Nordic SDK. */ bool loadURIBeaconConfigParams(EddystoneService::EddystoneParams_t *paramsP) { static bool pstorageInitied = false; if (!pstorageInitied) { pstorage_init(); static pstorage_module_param_t pstorageParams = { .cb = pstorageNotificationCallback, .block_size = sizeof(PersistentParams_t), .block_count = 1 }; pstorage_register(&pstorageParams, &pstorageHandle); pstorageInitied = true; } PersistentParams_t persistentParams; if ((pstorage_load(reinterpret_cast<uint8_t *>(&persistentParams), &pstorageHandle, sizeof(PersistentParams_t), 0) != NRF_SUCCESS) || (persistentParams.persistenceSignature != PersistentParams_t::MAGIC)) { // On failure zero out and let the service reset to defaults memset(paramsP, 0, sizeof(EddystoneService::EddystoneParams_t)); return false; } memcpy(paramsP, &persistentParams.params, sizeof(EddystoneService::EddystoneParams_t)); persistenceSignature = persistentParams.persistenceSignature; return true; }
/**@brief Function for the Device Manager initialization. * * @param[in] erase_bonds Indicates whether bonding information should be cleared from * persistent storage during initialization of the Device Manager. */ static void device_manager_init(bool erase_bonds) { uint32_t err_code; dm_init_param_t init_param = {.clear_persistent_data = erase_bonds}; dm_application_param_t register_param; err_code = pstorage_init(); APP_ERROR_CHECK(err_code); err_code = dm_init(&init_param); APP_ERROR_CHECK(err_code); memset(®ister_param.sec_param, 0, sizeof (ble_gap_sec_params_t)); // Event handler to be registered with the module. register_param.evt_handler = device_manager_event_handler; // Service or protocol context for device manager to load, store and apply on behalf of application. // Here set to client as application is a GATT client. register_param.service_type = DM_PROTOCOL_CNTXT_GATT_CLI_ID; // Secuirty parameters to be used for security procedures. register_param.sec_param.bond = SEC_PARAM_BOND; register_param.sec_param.mitm = SEC_PARAM_MITM; register_param.sec_param.io_caps = SEC_PARAM_IO_CAPABILITIES; register_param.sec_param.oob = SEC_PARAM_OOB; register_param.sec_param.min_key_size = SEC_PARAM_MIN_KEY_SIZE; register_param.sec_param.max_key_size = SEC_PARAM_MAX_KEY_SIZE; register_param.sec_param.kdist_periph.enc = 1; register_param.sec_param.kdist_periph.id = 1; err_code = dm_register(&m_dm_app_id, ®ister_param); APP_ERROR_CHECK(err_code); }
/**@brief Function for the Device Manager initialization. * * @param[in] erase_bonds Indicates whether bonding information should be cleared from * persistent storage during initialization of the Device Manager. */ static void device_manager_init(bool erase_bonds) { uint32_t err_code; dm_init_param_t init_param = {.clear_persistent_data = erase_bonds}; dm_application_param_t register_param; // Initialize persistent storage module. err_code = pstorage_init(); APP_ERROR_CHECK(err_code); err_code = dm_init(&init_param); APP_ERROR_CHECK(err_code); memset(®ister_param.sec_param, 0, sizeof(ble_gap_sec_params_t)); register_param.sec_param.bond = SEC_PARAM_BOND; register_param.sec_param.mitm = SEC_PARAM_MITM; register_param.sec_param.io_caps = SEC_PARAM_IO_CAPABILITIES; register_param.sec_param.oob = SEC_PARAM_OOB; register_param.sec_param.min_key_size = SEC_PARAM_MIN_KEY_SIZE; register_param.sec_param.max_key_size = SEC_PARAM_MAX_KEY_SIZE; register_param.evt_handler = device_manager_evt_handler; register_param.service_type = DM_PROTOCOL_CNTXT_GATT_SRVR_ID; err_code = dm_register(&m_app_handle, ®ister_param); APP_ERROR_CHECK(err_code); }
H_U32 _StorageOpen(void) { H_U32 err_code; // Initialize persistent storage module. err_code = pstorage_init(); APP_ERROR_CHECK(err_code); return err_code; }
/**@brief Function for application main entry. */ int main(void) { uint32_t err_code; // Initialize app_trace_init(); leds_init(); #ifdef COMMISSIONING_ENABLED err_code = pstorage_init(); APP_ERROR_CHECK(err_code); #endif // COMMISSIONING_ENABLED timers_init(); buttons_init(); static ipv6_medium_init_params_t ipv6_medium_init_params; memset(&ipv6_medium_init_params, 0x00, sizeof(ipv6_medium_init_params)); ipv6_medium_init_params.ipv6_medium_evt_handler = on_ipv6_medium_evt; ipv6_medium_init_params.ipv6_medium_error_handler = on_ipv6_medium_error; ipv6_medium_init_params.use_scheduler = false; #ifdef COMMISSIONING_ENABLED ipv6_medium_init_params.commissioning_id_mode_cb = commissioning_id_mode_cb; ipv6_medium_init_params.commissioning_power_off_cb = commissioning_power_off_cb; #endif // COMMISSIONING_ENABLED err_code = ipv6_medium_init(&ipv6_medium_init_params, \ IPV6_MEDIUM_ID_BLE, \ &m_ipv6_medium); APP_ERROR_CHECK(err_code); eui48_t ipv6_medium_eui48; err_code = ipv6_medium_eui48_get(m_ipv6_medium.ipv6_medium_instance_id, \ &ipv6_medium_eui48); ipv6_medium_eui48.identifier[EUI_48_SIZE - 1] = 0x00; err_code = ipv6_medium_eui48_set(m_ipv6_medium.ipv6_medium_instance_id, \ &ipv6_medium_eui48); APP_ERROR_CHECK(err_code); ip_stack_init(); ip_stack_timer_init(); dns_client_init(); APPL_LOG("\r\n"); APPL_LOG("[APPL]: Init complete.\r\n"); // Start execution connectable_mode_enter(); // Enter main loop for (;;) { power_manage(); } }
/** * @brief Function for application main entry. */ int main(void) { uint32_t err_code; //Initialize. app_trace_init(); leds_init(); timers_init(); iot_timer_init(); #ifdef COMMISSIONING_ENABLED err_code = pstorage_init(); APP_ERROR_CHECK(err_code); buttons_init(); #endif // COMMISSIONING_ENABLED static ipv6_medium_init_params_t ipv6_medium_init_params; memset(&ipv6_medium_init_params, 0x00, sizeof(ipv6_medium_init_params)); ipv6_medium_init_params.ipv6_medium_evt_handler = on_ipv6_medium_evt; ipv6_medium_init_params.ipv6_medium_error_handler = on_ipv6_medium_error; ipv6_medium_init_params.use_scheduler = false; #ifdef COMMISSIONING_ENABLED ipv6_medium_init_params.commissioning_id_mode_cb = commissioning_id_mode_cb; ipv6_medium_init_params.commissioning_power_off_cb = commissioning_power_off_cb; #endif // COMMISSIONING_ENABLED err_code = ipv6_medium_init(&ipv6_medium_init_params, \ IPV6_MEDIUM_ID_BLE, \ &m_ipv6_medium); APP_ERROR_CHECK(err_code); eui48_t ipv6_medium_eui48; err_code = ipv6_medium_eui48_get(m_ipv6_medium.ipv6_medium_instance_id, \ &ipv6_medium_eui48); ipv6_medium_eui48.identifier[EUI_48_SIZE - 1] = 0x00; err_code = ipv6_medium_eui48_set(m_ipv6_medium.ipv6_medium_instance_id, \ &ipv6_medium_eui48); APP_ERROR_CHECK(err_code); ip_stack_init(); udp_port_setup(); //Start execution. connectable_mode_enter(); //Enter main loop. for (;;) { //Sleep waiting for an application event. err_code = sd_app_evt_wait(); APP_ERROR_CHECK(err_code); } }
/**@brief Function for initializing the Device Manager. * * @details Device manager is initialized here. */ static void device_manager_init(void) { dm_init_param_t init_param; uint32_t err_code; err_code = pstorage_init(); APP_ERROR_CHECK(err_code); err_code = dm_init(&init_param); APP_ERROR_CHECK(err_code); }
uint32_t bootloader_init(void) { uint32_t err_code; pstorage_module_param_t storage_params = {.cb = pstorage_callback_handler}; err_code = pstorage_init(); VERIFY_SUCCESS(err_code); m_bootsettings_handle.block_id = BOOTLOADER_SETTINGS_ADDRESS; err_code = pstorage_register(&storage_params, &m_bootsettings_handle); return err_code; }
/**@brief Function for the Device Manager initialization. * * @param[in] erase_bonds Indicates whether bonding information should be cleared from * persistent storage during initialization of the Device Manager. */ static void device_manager_init(bool erase_bonds) { uint32_t err_code; dm_init_param_t init_param = {.clear_persistent_data = erase_bonds}; dm_application_param_t register_param; // Initialize persistent storage module. err_code = pstorage_init(); if (err_code == NRF_SUCCESS) debug_printf("Persistent storage module is ready!\r\n"); else { debug_printf("Ooops.. Something went wrong with initialising the PS module..\r\n"); APP_ERROR_CHECK(err_code); } err_code = dm_init(&init_param); if (err_code == NRF_SUCCESS) debug_printf("DM initialised!\r\n"); else { debug_printf("Ooops.. Something went wrong with initialising the DM..\r\n"); APP_ERROR_CHECK(err_code); } memset(®ister_param.sec_param, 0, sizeof(ble_gap_sec_params_t)); register_param.sec_param.bond = SEC_PARAM_BOND; register_param.sec_param.mitm = SEC_PARAM_MITM; register_param.sec_param.io_caps = SEC_PARAM_IO_CAPABILITIES; register_param.sec_param.oob = SEC_PARAM_OOB; register_param.sec_param.min_key_size = SEC_PARAM_MIN_KEY_SIZE; register_param.sec_param.max_key_size = SEC_PARAM_MAX_KEY_SIZE; register_param.evt_handler = device_manager_evt_handler; register_param.service_type = DM_PROTOCOL_CNTXT_GATT_SRVR_ID; err_code = dm_register(&m_app_handle, ®ister_param); if (err_code == NRF_SUCCESS) debug_printf("Registered parameters for DM!\r\n"); else { debug_printf("Ooops.. Something went wrong with registering the parameters for the DM..\r\n"); APP_ERROR_CHECK(err_code); } }
/**@brief Function for initializing the Device Manager. * * @details Device manager is initialized here. */ static void device_manager_init(void) { dm_application_param_t param; dm_init_param_t init_param; uint32_t err_code; err_code = pstorage_init(); APP_ERROR_CHECK(err_code); init_param.clear_persistent_data = false; #ifdef BOND_DELETE_ALL_BUTTON_PIN // Clear all bonded devices if user requests to. init_param.clear_persistent_data = ((nrf_gpio_pin_read(BOND_DELETE_ALL_BUTTON_PIN) == 0)? true: false); #endif err_code = dm_init(&init_param); APP_ERROR_CHECK(err_code); memset(¶m.sec_param, 0, sizeof (ble_gap_sec_params_t)); // Event handler to be registered with the module. param.evt_handler = device_manager_event_handler; // Service or protocol context for device manager to load, store and apply on behalf of application. // Here set to client as application is a GATT client. param.service_type = DM_PROTOCOL_CNTXT_GATT_CLI_ID; // Secuirty parameters to be used for security procedures. param.sec_param.bond = SEC_PARAM_BOND; param.sec_param.mitm = SEC_PARAM_MITM; param.sec_param.io_caps = SEC_PARAM_IO_CAPABILITIES; param.sec_param.oob = SEC_PARAM_OOB; param.sec_param.min_key_size = SEC_PARAM_MIN_KEY_SIZE; param.sec_param.max_key_size = SEC_PARAM_MAX_KEY_SIZE; param.sec_param.kdist_periph.enc = 1; param.sec_param.kdist_periph.id = 1; err_code = dm_register(&m_dm_app_id,¶m); APP_ERROR_CHECK(err_code); }
uint32_t bootloader_init(void) { uint32_t err_code; pstorage_module_param_t storage_params; storage_params.cb = pstorage_callback_handler; storage_params.block_size = BOOTLOADER_SETTINGS_FLASH_BLOCK_SIZE; storage_params.block_count = BOOTLOADER_SETTINGS_FLASH_BLOCK_COUNT; err_code = pstorage_init(); if (err_code != NRF_SUCCESS) { return err_code; } err_code = pstorage_register(&storage_params, &m_bootsettings_handle); m_delay_applied = false; return err_code; }
bool pstorage_driver_init() { if (pstorage_init() != NRF_SUCCESS) { return false; } // Configure pstorage driver. if(!pstorage_driver_cfg(0x20)) { return false; } // Read SensorID from p_storage if(!init_global((uint8_t*)&sensor_bridge.sensorID, (uint8_t*)&DEFAULT_SENSOR_ID, sizeof(sensor_bridge.sensorID))) { return false; } // Read SensorBeaconFrequency from p_storage if(!init_global((uint8_t*)&sensor_bridge.beaconFrequency, (uint8_t*)&DEFAULT_SENSOR_BEACON_FREQUENCY, sizeof(sensor_bridge.beaconFrequency))) { return false; } // Read Config from p_storage if(!init_global((uint8_t*)&sensor_bridge.config, (uint8_t*)&DEFAULT_SENSOR_CONFIG, sizeof(sensor_bridge.config))) { return false; } // Read Passkey from p_storage if(!init_global((uint8_t*)sensor_bridge.passkey, (uint8_t*)DEFAULT_SENSOR_PASSKEY, sizeof(sensor_bridge.passkey))) { return false; } // Read mitm_req_flag from p_storage if(!init_global((uint8_t*)&sensor_bridge.mitm_req_flag, (uint8_t*)&DEFAULT_MITM_REQ_FLAG, sizeof(sensor_bridge.mitm_req_flag))) { return false; } return true; }
*/ static void bond_manager_init(void) { uint32_t err_code; ble_bondmngr_init_t bond_init_data; bool bonds_delete; // Initialize persistent storage module. err_code = pstorage_init(); APP_ERROR_CHECK(err_code); // Clear all bonded centrals if the Bonds Delete button is pushed bonds_delete = (nrf_gpio_pin_read(BONDMNGR_DELETE_BUTTON_PIN_NO) == 0); // Initialize the Bond Manager bond_init_data.evt_handler = NULL; bond_init_data.error_handler = bond_manager_error_handler; bond_init_data.bonds_delete = bonds_delete; err_code = ble_bondmngr_init(&bond_init_data); APP_ERROR_CHECK(err_code);
/**@brief Function for initializing the Bond Manager. */ static void bond_manager_init(void) { uint32_t err_code; ble_bondmngr_init_t bond_init_data; bool bonds_delete; err_code = pstorage_init(); APP_ERROR_CHECK(err_code); // Clear all bonded masters if the Bonds Delete button is pushed. err_code = app_button_is_pushed(BONDMNGR_DELETE_BUTTON_PIN_NO, &bonds_delete); APP_ERROR_CHECK(err_code); // Initialize the Bond Manager. bond_init_data.evt_handler = on_bond_mgmr_evt; bond_init_data.error_handler = bond_manager_error_handler; bond_init_data.bonds_delete = bonds_delete; err_code = ble_bondmngr_init(&bond_init_data); APP_ERROR_CHECK(err_code); }
static error_t bond_manager_init(void) { ble_bondmngr_init_t bond_para = {0}; ASSERT_STATUS ( pstorage_init()); bond_para.flash_page_num_bond = CFG_BLE_BOND_FLASH_PAGE_BOND; bond_para.flash_page_num_sys_attr = CFG_BLE_BOND_FLASH_PAGE_SYS_ATTR; //bond_para.bonds_delete = boardButtonCheck(CFG_BLE_BOND_DELETE_BUTTON_NUM) ; bond_para.evt_handler = NULL; bond_para.error_handler = service_error_callback; ASSERT_STATUS( ble_bondmngr_init( &bond_para )); /* Init radio active/inactive notification to flash (to only perform flashing when the radio is inactive) */ // ASSERT_STATUS( ble_radio_notification_init(NRF_APP_PRIORITY_HIGH, // NRF_RADIO_NOTIFICATION_DISTANCE_4560US, // ble_flash_on_radio_active_evt) ); return ERROR_NONE; }
*/ static void bond_manager_init(void) { uint32_t err_code; ble_bondmngr_init_t bond_init_data; bool bonds_delete; // Initialize persistent storage module. err_code = pstorage_init(); APP_ERROR_CHECK(err_code); // Clear all bonded centrals if the Bonds Delete button is pushed err_code = app_button_is_pushed(BONDMNGR_DELETE_BUTTON_PIN_NO, &bonds_delete); APP_ERROR_CHECK(err_code); // Initialize the Bond Manager bond_init_data.flash_page_num_bond = FLASH_PAGE_BOND; bond_init_data.flash_page_num_sys_attr = FLASH_PAGE_SYS_ATTR; bond_init_data.evt_handler = NULL; bond_init_data.error_handler = bond_manager_error_handler; bond_init_data.bonds_delete = bonds_delete; err_code = ble_bondmngr_init(&bond_init_data); APP_ERROR_CHECK(err_code);
void simble_adv_start(void) { struct ble_gap_advdata advdata = { .length = 0 }; struct ble_gap_ad_flags flags = { .payload_length = 1, .type = BLE_GAP_AD_TYPE_FLAGS, .flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE, }; simble_add_advdata(&flags, &advdata); struct ble_gap_ad_name name; uint16_t namelen = sizeof(advdata); if (sd_ble_gap_device_name_get(name.name, &namelen) != NRF_SUCCESS) namelen = 0; name.type = BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME; name.payload_length = namelen; simble_add_advdata(&name, &advdata); sd_ble_gap_adv_data_set(advdata.data, advdata.length, NULL, 0); ble_gap_adv_params_t adv_params = { .type = BLE_GAP_ADV_TYPE_ADV_IND, .fp = BLE_GAP_ADV_FP_ANY, .interval = 0x400, }; sd_ble_gap_adv_start(&adv_params); onboard_led(ONBOARD_LED_ON); } static void simble_srv_tx_init(void) { uint16_t srv_handle; ble_uuid_t srv_uuid = {.type = BLE_UUID_TYPE_BLE, .uuid = 0x1804}; sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &srv_uuid, &srv_handle); ble_gatts_char_handles_t chr_handles; ble_gatts_char_md_t char_meta = {.char_props = {.read = 1}}; ble_uuid_t chr_uuid = {.type = BLE_UUID_TYPE_BLE, .uuid = 0x2a07}; ble_gatts_attr_md_t chr_attr_meta = { .vloc = BLE_GATTS_VLOC_STACK, }; BLE_GAP_CONN_SEC_MODE_SET_OPEN(&chr_attr_meta.read_perm); BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&chr_attr_meta.write_perm); uint8_t tx_val = 0; ble_gatts_attr_t chr_attr = { .p_uuid = &chr_uuid, .p_attr_md = &chr_attr_meta, .init_offs = 0, .init_len = sizeof(tx_val), .max_len = sizeof(tx_val), .p_value = &tx_val, }; sd_ble_gatts_characteristic_add(srv_handle, &char_meta, &chr_attr, &chr_handles); } /* DM requires a `app_error_handler` */ void __attribute__((weak)) app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name) { app_trace_log("[simble]: err: %d, line: %d, file: %s\r\n", error_code, line_num, p_file_name); for (;;) { /* NOTHING */ } } void simble_init(const char *name) { app_trace_init(); sd_softdevice_enable(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, NULL); /* XXX assertion handler */ ble_enable_params_t ble_params = { #if defined(SD120) .gap_enable_params = { .role = BLE_GAP_ROLE_PERIPH, }, #endif .gatts_enable_params = { .service_changed = 1, }, }; sd_ble_enable(&ble_params); ble_gap_conn_sec_mode_t mode; BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&mode); sd_ble_gap_device_name_set(&mode, (const uint8_t *)name, strlen(name)); // flash storage pstorage_init(); simble_srv_tx_init(); } uint8_t simble_get_vendor_uuid_class(void) { ble_uuid128_t vendorid = {{0x13,0xb0,0xa0,0x71,0xfe,0x62,0x4c,0x01,0xaa,0x4d,0xd8,0x03,0,0,0x0b,0xd0}}; static uint8_t vendor_type = BLE_UUID_TYPE_UNKNOWN; if (vendor_type != BLE_UUID_TYPE_UNKNOWN) return (vendor_type); sd_ble_uuid_vs_add(&vendorid, &vendor_type); return (vendor_type); } void simble_srv_register(struct service_desc *s) { s->next = services; services = s; sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &s->uuid, &s->handle); for (int i = 0; i < s->char_count; ++i) { struct char_desc *c = &s->chars[i]; ble_gatts_attr_md_t cccd_md; memset(&cccd_md, 0, sizeof(cccd_md)); BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm); cccd_md.vloc = BLE_GATTS_VLOC_STACK; int have_write = c->write_cb != NULL; ble_gatts_char_md_t char_meta = { .char_props = { .broadcast = 0, .read = 1, .write_wo_resp = have_write, .write = have_write, .notify = c->notify, .indicate = c->indicate, .auth_signed_wr = have_write, }, .p_char_user_desc = (uint8_t *)c->desc, .char_user_desc_size = strlen(c->desc), .char_user_desc_max_size = strlen(c->desc), .p_char_pf = c->format.format != 0 ? &c->format : NULL, .p_cccd_md = (c->notify || c->indicate) ? &cccd_md : NULL, }; ble_gatts_attr_md_t chr_attr_meta = { .vlen = 1, .vloc = BLE_GATTS_VLOC_STACK, .rd_auth = 1, .wr_auth = 1, }; BLE_GAP_CONN_SEC_MODE_SET_OPEN(&chr_attr_meta.read_perm); if (have_write) BLE_GAP_CONN_SEC_MODE_SET_OPEN(&chr_attr_meta.write_perm); else BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&chr_attr_meta.write_perm); ble_gatts_attr_t chr_attr = { .p_uuid = &c->uuid, .p_attr_md = &chr_attr_meta, .init_offs = 0, .init_len = 0, .max_len = c->length, }; sd_ble_gatts_characteristic_add(s->handle, &char_meta, &chr_attr, &c->handles); } } void simble_srv_init(struct service_desc *s, uint8_t type, uint16_t id) { *s = (struct service_desc){ .uuid = {.type = type, .uuid = id}, .char_count = 0, }; }; void simble_srv_char_add(struct service_desc *s, struct char_desc *c, uint8_t type, uint16_t id, const char *desc, uint16_t length) { *c = (struct char_desc){ .uuid = { .type = type, .uuid = id }, .desc = desc, .length = length, };
/**@brief Function for application main entry. */ int main(void) { uint32_t err_code; // Initialize app_trace_init(); leds_init(); timers_init(); #ifdef COMMISSIONING_ENABLED err_code = pstorage_init(); APP_ERROR_CHECK(err_code); buttons_init(); #endif // COMMISSIONING_ENABLED static ipv6_medium_init_params_t ipv6_medium_init_params; memset(&ipv6_medium_init_params, 0x00, sizeof(ipv6_medium_init_params)); ipv6_medium_init_params.ipv6_medium_evt_handler = on_ipv6_medium_evt; ipv6_medium_init_params.ipv6_medium_error_handler = on_ipv6_medium_error; ipv6_medium_init_params.use_scheduler = false; #ifdef COMMISSIONING_ENABLED ipv6_medium_init_params.commissioning_id_mode_cb = commissioning_id_mode_cb; ipv6_medium_init_params.commissioning_power_off_cb = commissioning_power_off_cb; #endif // COMMISSIONING_ENABLED err_code = ipv6_medium_init(&ipv6_medium_init_params, \ IPV6_MEDIUM_ID_BLE, \ &m_ipv6_medium); APP_ERROR_CHECK(err_code); eui48_t ipv6_medium_eui48; err_code = ipv6_medium_eui48_get(m_ipv6_medium.ipv6_medium_instance_id, \ &ipv6_medium_eui48); // Hardcode EUI_48. uint8_t mac[EUI_48_SIZE] = {0xD9, 0xDA, 0x27, 0x2F, 0x6D, 0x00}; memcpy(&ipv6_medium_eui48.identifier, mac, EUI_48_SIZE); //ipv6_medium_eui48.identifier[EUI_48_SIZE - 1] = 0x00; err_code = ipv6_medium_eui48_set(m_ipv6_medium.ipv6_medium_instance_id, \ &ipv6_medium_eui48); APP_ERROR_CHECK(err_code); ip_stack_init(); coap_port_t local_port_list[COAP_PORT_COUNT] = { {.port_number = LOCAL_PORT_NUM} }; coap_transport_init_t port_list; port_list.p_port_table = &local_port_list[0]; err_code = coap_init(17, &port_list); APP_ERROR_CHECK(err_code); err_code = coap_error_handler_register(coap_error_handler); APP_ERROR_CHECK(err_code); coap_endpoints_init(); iot_timer_init(); APPL_LOG("\r\n"); APPL_LOG("[APPL]: Init complete.\r\n"); // Start execution connectable_mode_enter(); // Enter main loop for (;;) { power_manage(); } }
ble_error_t btle_initializeSecurity(bool enableBonding, bool requireMITM, SecurityManager::SecurityIOCapabilities_t iocaps, const SecurityManager::Passkey_t passkey) { /* guard against multiple initializations */ if (initialized) { return BLE_ERROR_NONE; } if (pstorage_init() != NRF_SUCCESS) { return BLE_ERROR_UNSPECIFIED; } ret_code_t rc; if (passkey) { ble_opt_t opts; opts.gap_opt.passkey.p_passkey = const_cast<uint8_t *>(passkey); if ((rc = sd_ble_opt_set(BLE_GAP_OPT_PASSKEY, &opts)) != NRF_SUCCESS) { switch (rc) { case BLE_ERROR_INVALID_CONN_HANDLE: case NRF_ERROR_INVALID_ADDR: case NRF_ERROR_INVALID_PARAM: default: return BLE_ERROR_INVALID_PARAM; case NRF_ERROR_INVALID_STATE: return BLE_ERROR_INVALID_STATE; case NRF_ERROR_BUSY: return BLE_STACK_BUSY; } } } dm_init_param_t dm_init_param = { .clear_persistent_data = false /* Set to true in case the module should clear all persistent data. */ }; if (dm_init(&dm_init_param) != NRF_SUCCESS) { return BLE_ERROR_UNSPECIFIED; } // update default security parameters with function call parameters securityParameters.bond = enableBonding; securityParameters.mitm = requireMITM; securityParameters.io_caps = iocaps; const dm_application_param_t dm_param = { .evt_handler = dm_handler, .service_type = DM_PROTOCOL_CNTXT_GATT_CLI_ID, .sec_param = securityParameters }; if ((rc = dm_register(&applicationInstance, &dm_param)) != NRF_SUCCESS) { switch (rc) { case NRF_ERROR_INVALID_STATE: return BLE_ERROR_INVALID_STATE; case NRF_ERROR_NO_MEM: return BLE_ERROR_NO_MEM; default: return BLE_ERROR_UNSPECIFIED; } } initialized = true; return BLE_ERROR_NONE; } ble_error_t btle_purgeAllBondingState(void) { ret_code_t rc; if ((rc = dm_device_delete_all(&applicationInstance)) == NRF_SUCCESS) { return BLE_ERROR_NONE; } switch (rc) { case NRF_ERROR_INVALID_STATE: return BLE_ERROR_INVALID_STATE; case NRF_ERROR_NO_MEM: return BLE_ERROR_NO_MEM; default: return BLE_ERROR_UNSPECIFIED; } }