diff --git a/examples/bluetooth/nimble/blecent/main/main.c b/examples/bluetooth/nimble/blecent/main/main.c index 8e4efe6095..d6b72eedc4 100644 --- a/examples/bluetooth/nimble/blecent/main/main.c +++ b/examples/bluetooth/nimble/blecent/main/main.c @@ -34,6 +34,10 @@ static uint8_t peer_addr[6]; void ble_store_config_init(void); +#if MYNEWT_VAL(BLE_POWER_CONTROL) +static struct ble_gap_event_listener power_control_event_listener; +#endif + /** * Application callback. Called when the attempt to subscribe to notifications * for the ANS Unread Alert Status characteristic has completed. @@ -413,6 +417,55 @@ blecent_connect_if_interesting(void *disc) } } +#if MYNEWT_VAL(BLE_POWER_CONTROL) +static void blecent_power_control(uint16_t conn_handle) +{ + int rc; + + rc = ble_gap_read_remote_transmit_power_level(conn_handle, 0x01 ); // Attempting on LE 1M phy + assert (rc == 0); + + rc = ble_gap_set_transmit_power_reporting_enable(conn_handle, 0x01, 0x01); + assert (rc == 0); + + rc = ble_gap_set_path_loss_reporting_param(conn_handle, 60, 10, 30, 10, 2 ); //demo values + assert (rc == 0); + + rc = ble_gap_set_path_loss_reporting_enable(conn_handle, 0x01); + assert (rc == 0); +} + +static int +blecent_gap_power_event(struct ble_gap_event *event, void *arg) +{ + + switch(event->type) { + case BLE_GAP_EVENT_TRANSMIT_POWER: + MODLOG_DFLT(INFO, "Transmit power event : status=%d conn_handle=%d reason=%d " + "phy=%d power_level=%x power_level_flag=%d delta=%d", + event->transmit_power.status, + event->transmit_power.conn_handle, + event->transmit_power.reason, + event->transmit_power.phy, + event->transmit_power.transmit_power_level, + event->transmit_power.transmit_power_level_flag, + event->transmit_power.delta); + return 0; + + case BLE_GAP_EVENT_PATHLOSS_THRESHOLD: + MODLOG_DFLT(INFO, "Pathloss threshold event : conn_handle=%d current path loss=%d " + "zone_entered =%d", + event->pathloss_threshold.conn_handle, + event->pathloss_threshold.current_path_loss, + event->pathloss_threshold.zone_entered); + return 0; + + default: + return 0; + } +} +#endif + /** * The nimble host executes this callback when a GAP event occurs. The * application associates a GAP event callback with each connection that is @@ -467,14 +520,38 @@ blecent_gap_event(struct ble_gap_event *event, void *arg) return 0; } - /* Perform service discovery. */ +#if MYNEWT_VAL(BLE_POWER_CONTROL) + blecent_power_control(event->connect.conn_handle); + + ble_gap_event_listener_register(&power_control_event_listener, + blecent_gap_power_event, NULL); +#endif + +#if CONFIG_EXAMPLE_ENCRYPTION + /** Initiate security - It will perform + * Pairing (Exchange keys) + * Bonding (Store keys) + * Encryption (Enable encryption) + * Will invoke event BLE_GAP_EVENT_ENC_CHANGE + **/ + rc = ble_gap_security_initiate(event->connect.conn_handle); + if (rc != 0) { + MODLOG_DFLT(INFO, "Security could not be initiated, rc = %d\n", rc); + return ble_gap_terminate(event->connect.conn_handle, + BLE_ERR_REM_USER_CONN_TERM); + } else { + MODLOG_DFLT(INFO, "Connection secured\n"); + } +#else + /* Perform service discovery */ rc = peer_disc_all(event->connect.conn_handle, blecent_on_disc_complete, NULL); if (rc != 0) { MODLOG_DFLT(ERROR, "Failed to discover services; rc=%d\n", rc); return 0; } - } else { +#endif + } else { /* Connection attempt failed; resume scanning. */ MODLOG_DFLT(ERROR, "Error: Connection failed; status=%d\n", event->connect.status);