5 Replies Latest reply on Jan 2, 2018 7:50 AM by e.pratt_1639216

    Need a little help with two functions

    decoburn_1926091

      Of the two following functions the first one works fine, the second seems to hang until I cycle power.  I'd be grateful if someone could explain what I'm doing wrong.

       

      Thanks,

      Dennis

       

       


      void UpdateGattDB_Sol_Timeout()
      {
          CYBLE_GATTS_HANDLE_VALUE_NTF_T  tempHandle;
          tempHandle.attrHandle = CYBLE_SOLENOID_READ_SOLENOID_TIMEOUT_CHAR_HANDLE;
          tempHandle.value.val[0] = (uint8)  (SolTOTime >> 8); 
          tempHandle.value.val[1] =  (uint8) (SolTOTime & 0x00FF);
          tempHandle.value.len = 2; 
          CyBle_GattsWriteAttributeValue(&tempHandle,0,&cyBle_connHandle,CYBLE_GATT_DB_LOCALLY_INITIATED );      
      }

       

       

       

       

      void UpdateGattDB_Sol_State()
      {
          CYBLE_GATTS_HANDLE_VALUE_NTF_T  tempHandle;
          tempHandle.attrHandle = CYBLE_SOLENOID_READ_SOLENOID_STATE_CHAR_HANDLE;
          tempHandle.value.val[0] = (uint8) SolenoidState; 
          tempHandle.value.len = 1; 
          CyBle_GattsWriteAttributeValue(&tempHandle,0,&cyBle_connHandle,CYBLE_GATT_DB_LOCALLY_INITIATED );


      }

       

       

       

        • 1. Re: Need a little help with two functions
          e.pratt_1639216

          If your attribute is defined as 2 bytes in length, then you are probably getting an error from the GattsWriteAttributeValue() command; Check the response to be sure, but otherwise for an easy fix:

          Change the len to 2, and assign a 0 to the byte you don't use.

          • 2. Re: Need a little help with two functions
            user_15163247

            try in this way:

            void UpdateGattDB_Sol_Timeout()
            {
                 uint8 newvalue[2];   

                 CYBLE_GATTS_HANDLE_VALUE_NTF_T  tempHandle;
                tempHandle.attrHandle = CYBLE_SOLENOID_READ_SOLENOID_TIMEOUT_CHAR_HANDLE;

                 newvalue[0]= (uint8)  (SolTOTime >> 8); 
                newvalue [1] =  (uint8) (SolTOTime & 0x00FF);

                 tempHandle.value.val = newvalue;
                tempHandle.value.len = 2; 
                CyBle_GattsWriteAttributeValue(&tempHandle,0,&cyBle_connHandle,CYBLE_GATT_DB_LOCALLY_INITIATED );      
            }

            void UpdateGattDB_Sol_State()
            {
                uint8 newvalue[2];

                 CYBLE_GATTS_HANDLE_VALUE_NTF_T  tempHandle;
                tempHandle.attrHandle = CYBLE_SOLENOID_READ_SOLENOID_STATE_CHAR_HANDLE;
                newvalue[0]= (uint8) SolenoidState; 
                tempHandle.value.val=newvalue;

                 tempHandle.value.len = 1; 
                CyBle_GattsWriteAttributeValue(&tempHandle,0,&cyBle_connHandle,CYBLE_GATT_DB_LOCALLY_INITIATED );


            }

            • 3. Re: Need a little help with two functions
              decoburn_1926091

              That fixed it, but I don't understand why it works and my code didn't?

               

              I'm also not clear on exactly how the function CyBle_GattsWriteAttributeValue does anything based on looking at it's code.

               

              Oh well,  it works and I'm grateful for your help.

               

              Thanks,

              Dennis

               

               

               

               

               

               

               

               

              • 4. Re: Need a little help with two functions
                decoburn_1926091

                Actually, I tried the fix you suggested and it didn't really work.  But thanks, anyway.

                • 5. Re: Need a little help with two functions
                  e.pratt_1639216

                  tempHandle.value.val is a pointer to an array containing data of length tempHandle.value.len.

                  As @user_15163247 stated in his code above, you need to declare space for an array and then pass the pointer to that array in the CYBLE_GATTS_HANDLE_VALUE_NTF_T struct. It could be you got lucky with the incorrect location/pointer dereferencing when the first function ran, but not the second one.

                  Here is the relevant documentation from the PSoC Creator:

                   

                  typedef struct

                  {

                      /** Pointer to the value to be packed */

                      uint8*   val;

                   

                   

                      /** Length of Value to be packed */

                      uint16   len;

                   

                   

                      /** Out Parameter Indicating Actual Length Packed and sent over the air. Actual length

                         can be less than or equal to the 'len' parameter value. This provides information

                         to application that what is the actual length of data that is transmitted over the

                         air. Each GATT procedures defines different length of data that can be transmitted

                         over the air. If application sends more than that, all data may not transmitted over air.*/

                      uint16   actualLen;

                   

                   

                  }CYBLE_GATT_VALUE_T;

                  (The pointer addressing/dereferencing is due to the code being written in C/C++, as other higher level coding languages such as java, python, VB don't use pointers as much.)

                  The answer to this pointers - How come an array's address is equal to its value in C? - Stack Overflow  post might be useful in understanding the problem that occurred.

                  And here is some examples for doing it the right way:

                  https://www.programiz.com/c-programming/c-arrays?utm_expid=66600334-4.VfQskolZTeyX_ldg7j0KXQ.0&utm_referrer=https%3A%2F%…

                  Arrays in C

                  https://denniskubes.com/2012/08/17/basics-of-memory-addresses-in-c/