ble_error_t BlueNRGGattClient::read(Gap::Handle_t connHandle, GattAttribute::Handle_t attributeHandle, uint16_t offset) const
{
  /* avoid compiler warnings about unused variables */
  (void)offset;

  tBleStatus ret;
  
  BlueNRGGattClient *gattc = const_cast<BlueNRGGattClient*>(this);
  
  gattc->_currentState = GATT_READ_CHAR;
  
  // Save the attribute_handle not provided by evt_att_read_resp    
  gattc->readCBParams.handle = attributeHandle;
  
  // FIXME: We need to wait for a while before starting a read
  // due to BlueNRG process queue handling
  Clock_Wait(100);

  ret = aci_gatt_read_charac_val(connHandle, attributeHandle);
  
  if(ret == BLE_STATUS_SUCCESS) {
    return BLE_ERROR_NONE;
  }
  switch (ret) {
  case BLE_STATUS_BUSY:
    return BLE_STACK_BUSY;
  default:
    return BLE_ERROR_INVALID_STATE;
  }
}
Example #2
0
/*host creat connection*/
tBleStatus ble_host_connect(tBDAddr bdaddr)
{

    tBleStatus ret;
    if(set_connectable == TRUE) {
        printf("Client Create Connection\n");
        BSP_LED_On(LED0);
        /*
          Scan_Interval, Scan_Window, Peer_Address_Type, Peer_Address, Own_Address_Type, Conn_Interval_Min,
          Conn_Interval_Max, Conn_Latency, Supervision_Timeout, Conn_Len_Min, Conn_Len_Max
          */
        ret = aci_gap_create_connection(SCAN_P, SCAN_L, PUBLIC_ADDR, bdaddr, PUBLIC_ADDR, CONN_P1, CONN_P2, 0,
                                        SUPERV_TIMEOUT, CONN_L1 , CONN_L2);
        if (ret != 0) {
            printf("Error while starting connection.\n");
            Clock_Wait(100);
        }
        printf("connection init\n\r");

    }
    set_connectable = FALSE;
    /*discover device*/
    ble_host_discover_char();
    
    return ret;
}
Example #3
0
/**
 * @brief  Make the device connectable
 * @param  None 
 * @retval None
 */
void Make_Connection(void)
{  
  tBleStatus ret;
  
  
  if(BLE_Role == CLIENT) {
    
    PRINTF("Client Create Connection\n");
    tBDAddr bdaddr = {0xaa, 0x00, 0x00, 0xE1, 0x80, 0x02};
    //tBDAddr bdaddr = {0xfd, 0x00, 0x25, 0xec, 0x02, 0x04}; //BT address for HRM test
    
    BSP_LED_On(LED2); //To indicate the start of the connection and discovery phase
    
    // Intial value for connection interval 9
    /*
    Scan_Interval, Scan_Window, Peer_Address_Type, Peer_Address, Own_Address_Type, Conn_Interval_Min, 
    Conn_Interval_Max, Conn_Latency, Supervision_Timeout, Conn_Len_Min, Conn_Len_Max    
    */
    ret = aci_gap_create_connection(SCAN_P, SCAN_L, PUBLIC_ADDR, bdaddr, PUBLIC_ADDR, CONN_P1, CONN_P2, 0, 
      SUPERV_TIMEOUT, CONN_L1 , CONN_L2);

    //ret = aci_gap_create_connection(0x4000, 0x4000, PUBLIC_ADDR, bdaddr, PUBLIC_ADDR, 40, 40, 0, 600, 2000 , 2000);
    
    if (ret != 0){
      PRINTF("Error while starting connection.\n");
      Clock_Wait(100);
    }
    
  } else  {
    
    const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,'B','l','u','e','N','R','G','_','C','h','a','t'};
    
    /* disable scan response */
    hci_le_set_scan_resp_data(0,NULL);
    
    PRINTF("General Discoverable Mode ");
    /*
    Advertising_Event_Type, Adv_Interval_Min, Adv_Interval_Max, Address_Type, Adv_Filter_Policy,
    Local_Name_Length, Local_Name, Service_Uuid_Length, Service_Uuid_List, Slave_Conn_Interval_Min,
    Slave_Conn_Interval_Max
    */
    ret = aci_gap_set_discoverable(ADV_IND, 0, 0, PUBLIC_ADDR, NO_WHITE_LIST_USE,
                                   13, local_name, 0, NULL, 0, 0);
    PRINTF("%d\n",ret);
  }
}
void ble_host_connect(tBDAddr bdaddr)
{
    if(host_connect_init_flag == FALSE) {
        tBleStatus ret;
        printf("Client Create Connection\n");
        BSP_LED_On(LED0);
        /*
          scan_interval, scan_window, peer_address_type, peer_address, own_address_type, conn_interval_min,
          conn_interval_max, conn_latency, supervision_timeout, conn_len_min, conn_len_max
          */
        ret = aci_gap_create_connection(SCAN_P, SCAN_L, RANDOM_ADDR, bdaddr, PUBLIC_ADDR, CONN_P1, CONN_P2, 0,
                                        SUPERV_TIMEOUT, CONN_L1 , CONN_L2);
        if (ret != 0) {
            printf("Error while starting connection.\n");
            Clock_Wait(100);
        } else {
            host_connect_init_flag = TRUE;
        }
        printf("connection init\n\r");
    }
}
int main(void)
{
    //int ret;
    
    NVIC_SetVectorTable(NVIC_VectTab_FLASH,VECTOR_TABLE_BASE_ADDRESS);
    
    /* Identify the BlueNRG platform */
    SdkEvalIdentification();

    RCC_Configuration();
    /* Basic button init function for handling application jumping */
    Configure_Button();
 
#if 0 /* TBR */
    PWR_PVDCmd(DISABLE);
    
    /* Disable FLASH during Sleep  */
    FLASH_SLEEPPowerDownCmd(ENABLE);
    
    /* Enable Ultra low power mode */
    PWR_UltraLowPowerCmd(ENABLE);
    
    PWR_FastWakeUpCmd(DISABLE);
#endif 
    
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
    
    Clock_Init();
    
    HCI_Init();
 
    /* Init SPI interface */
    SdkEvalSpiInit(SPI_MODE_EXTI);
    /* Reset BlueNRG SPI interface */
    BlueNRG_RST();
    
    /* Init leds */
    SdkEvalLedInit(LED1);
    SdkEvalLedInit(LED2);

    {
        tHalUint8 bdaddr[] = {0x12, 0x34, 0x00, 0xE1, 0x80, 0x02};

        aci_hal_write_config_data(CONFIG_DATA_PUBADDR_OFFSET, CONFIG_DATA_PUBADDR_LEN,
                                        bdaddr);
    }
    
    aci_gatt_init();    
    
    {
        uint16_t service_handle, dev_name_char_handle, appearance_char_handle;
        aci_gap_init(1, &service_handle, &dev_name_char_handle, &appearance_char_handle);        
    }
    
#if 0/* TBR */
    aci_gap_set_auth_requirement(MITM_PROTECTION_REQUIRED,
                                       OOB_AUTH_DATA_ABSENT,
                                       NULL,
                                       7,
                                       16,
                                       USE_FIXED_PIN_FOR_PAIRING,
                                       123456,
                                       BONDING);
#endif 
    
    //PRINTF("BLE Stack Initialized.\n");
    
#ifdef ST_OTA_BTL
    /* Add OTA bootloader service */
    Add_Btl_Service();
#endif
    
    /* -2 dBm output power */
    aci_hal_set_tx_power_level(1,4);
    
    while(1)
    {
#ifdef ST_OTA_BTL
      static tClockTime startTime = 0;

      if (Clock_Time() - startTime >led_blinking_rate)
      {    
        /* LED D1 is toggling on OTA_Service Manager */
        SdkEvalLedToggle(LED1);     
        startTime = Clock_Time();
      }
#endif /* end ST_OTA_BTL */

        HCI_Process();
        
        if(set_connectable){
            setConnectable();
            set_connectable = 0;
        }
        
      /* Use button to switch to the basic Reset Manager */
      if (GPIO_ReadInputDataBit(ButtonPort,ButtonPin) == RESET)
      {
        /* Add delay to avoid conlict with DFU activation */
        Clock_Wait(2000);
        
        /* Check if an application has been loaded previously through OTA service
           manager */
        if (*((uint32_t*) NEW_APP_MEM_INFO)!= 0) 
          /* Service Manager will jump to the Application previously loaded at
           address  APPLICATION_JUMP_ADDRESS */
          Switch_To_OTA_Service_Manager_Application(APPLICATION_JUMP_ADDRESS);
      }
    }
}