Пример #1
0
int main(void)
{
    uint32_t err_code;
    // Initialize.
    timers_init();
    err_code = bsp_init(BSP_INIT_LED, APP_TIMER_TICKS(100, APP_TIMER_PRESCALER), NULL);
    APP_ERROR_CHECK(err_code);

    uart_init();
    ble_stack_init();

    twi_master_init();

    err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING);
    nrf_delay_ms(200);
    bsp_indication_set(BSP_INDICATE_IDLE);
    nrf_delay_ms(200);
    err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING);

    update_sensor(0);
    start_advertising();
    timers_start();

    // Enter main loop.
    for (;; )
    {
        power_manage();
    }
}
Пример #2
0
int main(void) {
#if defined(NSEC_HARDCODED_BLE_DEVICE_ID)
    sprintf(g_device_id, "%.8s", NSEC_STRINGIFY(NSEC_HARDCODED_BLE_DEVICE_ID));
#else
    sprintf(g_device_id, "NSEC%04X", (uint16_t)(NRF_FICR->DEVICEID[1] % 0xFFFF));
#endif
    g_device_id[9] = '\0';

#ifdef NSEC_CTF_ADD_FLAGS
static volatile char flag1[] = "FLAG-60309301fa5b4a4e990392ead6ac7b5f";
printf("%s", flag1); // Don't optimize out flag1
rot13();
#endif

    /*
     * Initialize base hardware
     */
    log_init();
    power_init();
    softdevice_init();
    timer_init();
    init_WS2812FX();
    ssd1306_init();
    gfx_setTextBackgroundColor(SSD1306_WHITE, SSD1306_BLACK);
    nsec_buttons_init();

    /*
     * Initialize bluetooth stack
     */
    create_ble_device(g_device_id);
    configure_advertising();
    nsec_led_ble_init();
    init_identity_service();
    start_advertising();
    nsec_battery_manager_init();
    nsec_status_bar_init();
    nsec_status_set_name(g_device_id);
    nsec_status_set_badge_class(NSEC_STRINGIFY(NSEC_HARDCODED_BADGE_CLASS));
    nsec_status_set_ble_status(STATUS_BLUETOOTH_ON);

    load_stored_led_settings();

    nsec_intro();
    show_main_menu();

    /*
     * Main loop
     */
    while(true) {
        service_WS2812FX();
        nsec_storage_update();
        power_manage();
    }

    return 0;
}
Пример #3
0
    /** Callback triggered when the ble initialization process has finished */
    void on_init_complete(BLE::InitializationCompleteCallbackContext *params) {
        if (params->error != BLE_ERROR_NONE) {
            printf("Ble initialization failed.");
            return;
        }

        print_mac_address();

        start_advertising();
    }
Пример #4
0
    /** advertise and filter based on known devices */
    virtual void start_after_bonding()
    {
        Gap::PeripheralPrivacyConfiguration_t privacy_configuration = {
            /* use_non_resolvable_random_address */ false,
            Gap::PeripheralPrivacyConfiguration_t::REJECT_NON_RESOLVED_ADDRESS
        };

        _ble.gap().setPeripheralPrivacyConfiguration(&privacy_configuration);

        start_advertising();
    }
Пример #5
0
    /** Callback triggered when the ble initialization process has finished */
    void on_init_complete(BLE::InitializationCompleteCallbackContext *params) {
        if (params->error != BLE_ERROR_NONE) {
            printf("Ble initialization failed.");
            return;
        }

        _led_service = new LEDService(_ble, false);

        _ble.gattServer().onDataWritten(this, &LEDDemo::on_data_written);

        print_mac_address();

        start_advertising();
    }
Пример #6
0
    /** Set up and start advertising accepting anyone */
    virtual void start()
    {
        if (!set_advertising_data()) {
            return;
        }

        Gap::PeripheralPrivacyConfiguration_t privacy_configuration = {
            /* use_non_resolvable_random_address */ false,
            Gap::PeripheralPrivacyConfiguration_t::PERFORM_PAIRING_PROCEDURE
        };

        _ble.gap().setPeripheralPrivacyConfiguration(&privacy_configuration);

        start_advertising();
    };
Пример #7
0
/**
 * @brief Initialize Bluetooth Low Energy and start advertising
 */
void ble_init()
{
    nrf_gpio_cfg_output(PIN_LED_ADVERTISING);
    nrf_gpio_cfg_output(PIN_LED_CONNECTED);
    nrf_gpio_cfg_output(PIN_LED_DATA);

    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, false);

    init_softdevice();
    init_ble_stack();
    init_gap();
    init_services();
    init_characteristics();
    init_advertising();
    init_connection_parameters();
    init_security_parameters();

    start_advertising();
}
Пример #8
0
static void ble_event_handler(ble_evt_t const * p_ble_evt, void * p_context){
    //pm_on_ble_evt(p_ble_evt);
    switch (p_ble_evt->header.evt_id){
        case BLE_GAP_EVT_DISCONNECTED:
            start_advertising();
            break;
        case BLE_GATTS_EVT_SYS_ATTR_MISSING: {
            const uint16_t conn = p_ble_evt->evt.gatts_evt.conn_handle;
            APP_ERROR_CHECK(sd_ble_gatts_sys_attr_set(conn, NULL, 0, 0));
            }
            break;

        case BLE_GAP_EVT_ADV_REPORT: {
            if(_nsec_ble_scan_callback == NULL) {
                break;
            }
            const ble_gap_evt_adv_report_t * rp = &p_ble_evt->evt.gap_evt.params.adv_report;
            int8_t i = 0;
            while((rp->dlen - i) >= 2) {
                const uint8_t len = rp->data[i++] - 1; // The type is included in the length
                const uint8_t type = rp->data[i++];
                const uint8_t * data = &rp->data[i];
                i += len;

                if(type == BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME) {
                    if(len == 8 && data[0] == 'N' && data[1] == 'S' && data[2] == 'E' && data[3] == 'C') {
                        uint16_t other_id = 0;
                        if(sscanf((const char *) &data[4], "%04hx", &other_id) == 1) {
                            _nsec_ble_scan_callback(other_id, rp->peer_addr.addr, rp->rssi);
                        }
                    }
                }
            }
            }
            break;
        case BLE_GATTS_EVT_WRITE:
        {
            const ble_gatts_evt_write_t * event = &p_ble_evt->evt.gatts_evt.params.write;
            if(event->op == BLE_GATTS_OP_EXEC_WRITE_REQ_NOW){
            	on_execute_queued_write_commands();
            }
            else{
            	on_characteristic_write_command_event(event);
            }
            break;
        }
        case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
        {
            uint8_t type = p_ble_evt->evt.gatts_evt.params.authorize_request.type;
            if(type == BLE_GATTS_AUTHORIZE_TYPE_READ){
                const ble_gatts_evt_read_t * event = &p_ble_evt->evt.gatts_evt.params.authorize_request.request.read;
                on_characteristic_read_request_event(event, p_ble_evt->evt.gatts_evt.conn_handle);
            }
            else if(type == BLE_GATTS_AUTHORIZE_TYPE_WRITE){
                const ble_gatts_evt_write_t * event = &p_ble_evt->evt.gatts_evt.params.authorize_request.request.write;
                uint16_t connection_handle = p_ble_evt->evt.gatts_evt.conn_handle;
                switch(event->op){
                	case BLE_GATTS_OP_WRITE_REQ:
                		on_characteristic_write_request_event(event, connection_handle);
                		break;
                	case BLE_GATTS_OP_PREP_WRITE_REQ:
                		on_prepare_write_request(event, connection_handle);
                		break;
                	case BLE_GATTS_OP_EXEC_WRITE_REQ_NOW:
                		on_execute_queued_write_requests(connection_handle);
                		break;
                	default:
                		break;
                }
            }
            break;
        }
        case BLE_EVT_USER_MEM_REQUEST:
        {
            buffer = malloc(LONG_WRITE_MAX_LENGTH);
            ble_user_mem_block_t memory_block;
            memory_block.p_mem = buffer;
            memory_block.len = LONG_WRITE_MAX_LENGTH;
            volatile uint32_t error_code = sd_ble_user_mem_reply(p_ble_evt->evt.common_evt.conn_handle, &memory_block);
            APP_ERROR_CHECK(error_code);
        }
            break;
        case BLE_EVT_USER_MEM_RELEASE:
            free(p_ble_evt->evt.common_evt.params.user_mem_release.mem_block.p_mem);
            buffer = NULL;
            break;
        default:
            break;
    }
}
Пример #9
0
void tester_handle_gap(uint8_t opcode, uint8_t index, uint8_t *data,
		       uint16_t len)
{
	switch (opcode) {
	case GAP_READ_SUPPORTED_COMMANDS:
	case GAP_READ_CONTROLLER_INDEX_LIST:
		if (index != BTP_INDEX_NONE){
			tester_rsp(BTP_SERVICE_ID_GAP, opcode, index,
				   BTP_STATUS_FAILED);
			return;
		}
		break;
	default:
		if (index != CONTROLLER_INDEX){
			tester_rsp(BTP_SERVICE_ID_GAP, opcode, index,
				   BTP_STATUS_FAILED);
			return;
		}
		break;
	}

	switch (opcode) {
	case GAP_READ_SUPPORTED_COMMANDS:
		supported_commands(data, len);
		return;
	case GAP_READ_CONTROLLER_INDEX_LIST:
		controller_index_list(data, len);
		return;
	case GAP_READ_CONTROLLER_INFO:
		controller_info(data, len);
		return;
	case GAP_SET_CONNECTABLE:
		set_connectable(data, len);
		return;
	case GAP_SET_DISCOVERABLE:
		set_discoverable(data, len);
		return;
	case GAP_START_ADVERTISING:
		start_advertising(data, len);
		return;
	case GAP_STOP_ADVERTISING:
		stop_advertising(data, len);
		return;
	case GAP_START_DISCOVERY:
		start_discovery(data, len);
		return;
	case GAP_STOP_DISCOVERY:
		stop_discovery(data, len);
		return;
	case GAP_CONNECT:
		connect(data, len);
		return;
	case GAP_DISCONNECT:
		disconnect(data, len);
		return;
	case GAP_SET_IO_CAP:
		set_io_cap(data, len);
		return;
	case GAP_PAIR:
		pair(data, len);
		return;
	case GAP_PASSKEY_ENTRY:
		passkey_entry(data, len);
		return;
	default:
		tester_rsp(BTP_SERVICE_ID_GAP, opcode, index,
			   BTP_STATUS_UNKNOWN_CMD);
		return;
	}
}