1 Reply Latest reply on Sep 3, 2019 11:05 PM by PY_21

    Using multiple fields in one characteristic

    user_1528391

      I need to send/receive data over BLE between my PSoC device and a mobile phone. There are multiple different parameters to transfer and the data length is bigger than 512 bytes.

       

      So, instead of creating different characteristics for each parameter, I wanted to write a packet communication protocol on one characteristic. In this packet communication protocol, the sender will send the data and also say which parameter this data belongs.

       

      I added multiple fields in my characteristic like below:

       

       

      I also created a struct just like this characteristic fields, which is also below:

       

       

       

      #pragma pack(push, 1)
      typedef struct{
      
           uint8 start;
           uint8 cmdId;
           uint8 dataLen;
           uint8 data[500];
           uint8 checksum;
           uint8 end;
      
      }pck_t;
      #pragma pack(pop)
      
      pck_t packet;
      

       

       

      Then when I need to write the attribute value, I will reference my struct by casting it to uint8 like below:

       

      CommCharHandle.value.val = (uint8 *)&packet;
      CommCharHandle.value.len = sizeof(pck_t);
      
      err_code = CyBle_GattsWriteAttributeValue(&CommCharHandle, 0, &cyBle_connHandle, CYBLE_GATT_DB_LOCALLY_INITIATED);
      

       

       

      Do you think this is a good method? I saw this method (multiple field usage) in the Cypress's BLE Bootloader Module. But I couldn't find how they used these fields in the source codes. There is not any structure definition. Maybe they just defined these fields to help the developer to understand the packet easily? I don't know.

       

      And the real question is:

       

      Does Cypress Bootloader always send 500 bytes of data in each packet? Because if the data length changes, the position of the checksum and end bytes will shift. How to handle them? I mean, if I receive a packet in which the data length is not exactly 500, I can not mass copy the received packet to my structure?

       

      If the data length is always 500, why do they added the 'Data Length' byte in the fields? And how do they handle the last packet that does not actually necessarily contain 500 bytes of data? Do you append zeros to it?

       

      Message was edited by: Abdullah Ç─▒nar