2 Replies Latest reply on Mar 3, 2017 11:39 PM by user_569551

    How to read the status....


      I have realized the BTLE device (attached the code). I need to send back 2 responses (a) one for status of a switch (b) and Timeout (by default it is 5 secs). 

      How do I do this? 
      a. When the device starts (it should update the switch status - and timeout value which is default). 
      b. How do I update when the value changes - I need to modify the switch status when the position is changed and when the timeout changed (this value is set by the central - so I would be getting CYBLE_EVT_GATTS_WRITE_REQ event) 

      With best regards, 


      My main.c looks like as below....


      unsigned char BTState;
      unsigned char LockOpenDuration = 5; //5 secs the lock is open
      uint8 UpperByte, LowerByte;
      uint16 WriteVal;
      unsigned char applicationPower;
      CYBLE_API_RESULT_T apiResult;


      void OpenTheLock(void)
          SCB_1_UartPutString("Opening the lock\n\r");
          //LED_2_Write (1); //Turn off the blue LED if on...
          LED_2_Write (1);
          CyDelay(LockOpenDuration * 1000);
          LED_2_Write (0);
          /* if you are done with everything and ready to go to sleep,
          then set it up to go to sleep. Update the code inside if() specific
          to your application*/
          applicationPower = SLEEP;
          /* if you are done with everything and ready to go to deepsleep,
          then set it up to go to deepsleep. Update the code inside if() specific
          to your application*/
          if (1)
          applicationPower = DEEPSLEEP;


      void AppCallBack (uint32 event, void * eventParam)
      /* Structure to store data written by Client */
              case CYBLE_EVT_STACK_ON:
              /* This event is generated when BLE stack is ON */
              /* BLE stack is on. Start BLE advertisement */
              //In the final version this will depend on Switch to turn on the advertisement or not
              /* C8. Get the configured clock parameters for BLE subsystem */
              /* C8. Set the device sleep-clock accuracy (SCA) based on the tuned ppm
              of the WCO */
              clockConfig.bleLlSca = CYBLE_LL_SCA_000_TO_020_PPM;
              /* C8. Set the clock parameter of BLESS with updated values */
              /* Put the device into discoverable mode so that a Central device can
              connect to it. */
              apiResult = CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST);
              CY_SET_XTND_REG32((void CYFAR *)(CYREG_BLE_BLERD_BB_XO_CAPTRIM), 0x0000BCBC);
              /* This event is generated at GAP disconnection. */
              /* Restart BLE advertisement */
              //In the final version this will depend on Switch to turn on the advertisement or not
              case CYBLE_EVT_GATTS_WRITE_REQ:
              /* This event is generated when the connected Central */
              /* device sends a Write request. */
              /* The parameter ‘eventParam’ contains the data written */
              /* Extract the Write data sent by Client */
              wrReqParam = (CYBLE_GATTS_WRITE_REQ_PARAM_T *) eventParam;
              UpperByte = *((uint8 *)(wrReqParam->handleValPair.value.val));
              SCB_1_UartPutString("Upper Byte = 0x"); PrintChar(UpperByte);SCB_1_UartPutString("\n\r");
              LowerByte = *(((uint8 *)(wrReqParam->handleValPair.value.val))+1);
              SCB_1_UartPutString("Lower byte = 0x"); PrintChar(LowerByte); SCB_1_UartPutString("\n\r");


              //if( CYBLE_BT_LOCK_SERVICE_HANDLE == *(uint8 *)(wrReqParam->handleValPair.value.val))
              if (UpperByte == 0x0AA)
                  /* Open the lock */
              else if (UpperByte == 0x55) //This is where I am setting the duration
                  /* Set the durarion */
                  LockOpenDuration = LowerByte;
                  WriteVal = UpperByte << 8; WriteVal = WriteVal | LowerByte;
                  SCB_1_UartPutString("WriteVal = 0x"); 
                  PrintChar(WriteVal>>8); //&0x00ff); 
                  PrintChar(WriteVal); //&0xff);
                  CyBle_GattsWriteAttributeValue (&(wrReqParam->handleValPair),0, &(wrReqParam->connHandle), WriteVal);
                  SCB_1_UartPutString("Wrong data written. Check your data\n\r");


              /* Send the response to the write request received. */


      int main()
      /* Variable declarations for low power*/
      CYBLE_LP_MODE_T lpMode;
      CYBLE_BLESS_STATE_T blessState;
      uint8 interruptStatus;    
          /* Enable global interrupts */
          CyGlobalIntEnable;  /* Uncomment this line to enable global interrupts. */


          /* Place your initialization/startup code here (e.g. MyInst_Start()) */

          /* For these C1, C2 etc Cx states - refer to AN92584 */
          /* Designing for Low Power and Estimating Battery Life for BLE Applications */
          /* C1. Stop the ILO to reduce current consumption */
          /* C2. Configure the divider values for the ECO, so that a 3-MHz ECO divided
      c   lock can be provided to the CPU in Sleep mode */
          /* Start the BLE Component and register the generic event handler */
          apiResult = CyBle_Start(AppCallBack);


          /* Wait for BLE Component to initialize */
          while (CyBle_GetState() == CYBLE_STATE_INITIALIZING)
          applicationPower = ACTIVE;
          /* main while loop of the application */    
              /* Process all pending BLE events in the stack */


              //Is below code needed?
              applicationPower = DEEPSLEEP; 


              /* C3. Call the function that manages the BLESS power modes */
              /*C4. Run your application specific codehere */
              //??? Nothing I (Phani)  believe as of now...
              //if(applicationPower == ACTIVE)
              //    RunApplication();
              /*C6. Manage Application power mode */
              /*C7. Manage System power mode */

        • 1. Re: How to read the status....

          Hi Phani,


          a) Use the macro "CYBLE_GATT_DB_ATTR_SET_GEN_VALUE(handle,src,length)" to set the value of an attribute, where


          'Handle' is the handle of the attribute, 'src' is the pointer to the data and 'length' is the length of the data.




          b) You can use this macro whenever you want to update the data of the attribute but do not use this before the stack is on. With respect to the value written by the central, you are handling it correctly except that you are not checking for the handle(you have commented it out).


          What is the exact issue you are facing?

          • 2. Re: How to read the status....

            Hi Sujay,


            Yes, this CYBLE_GATT_DB_ATTR_SET_GEN_VALUE(handle,src,length) helped and works fine. Thanks a lot.


            With best regards,