How to use a data structure in a BLE custom characteristic field?

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Does anybody know how to define a data structure in a BLE custom characteristic field?  In the drop-down menu where you select data types, "struct" is listed.  It's unclear to me how to define the structure.  Screen shot attached.

   

For example, my screenshot shows uint8, however, I want to use a structure:

   

struct data

   

{

   

uint8 x;

   

uint16 y;

   

}

0 Likes
1 Solution
Anonymous
Not applicable

I figured out what the Fields entry and associated Type drop-down are doing.  This is simply allocating memory in the form of uint8.  Different data types are listed for convenience so the user doesn't have to do the math.  In the case of structs, arrays, and other data types where it is impossible to know the memory requirements, the user must manually calculate the length and enter it in the Length entry. 

   

In my case, I have a struct, the actual structure is not defined in the BLE configuration, just the memory requirements.  In my structure, the Length is 3.  When you want to send the structure in your code, simply cast it as a uint8 pointer.

   

typedef struct data

   

{

   

uint8 x;

   

uint16 y;

   

}data;

   

 

   

data val;

   

 

   

CYBLE_GATTS_HANDLE_VALUE_NTF_T MyHandle;

   

MyHandle.value.val = (uint8 *) &val;

   

MyHandle.value.len = sizeof(val);

   
        

View solution in original post

0 Likes
2 Replies
Anonymous
Not applicable

I figured out what the Fields entry and associated Type drop-down are doing.  This is simply allocating memory in the form of uint8.  Different data types are listed for convenience so the user doesn't have to do the math.  In the case of structs, arrays, and other data types where it is impossible to know the memory requirements, the user must manually calculate the length and enter it in the Length entry. 

   

In my case, I have a struct, the actual structure is not defined in the BLE configuration, just the memory requirements.  In my structure, the Length is 3.  When you want to send the structure in your code, simply cast it as a uint8 pointer.

   

typedef struct data

   

{

   

uint8 x;

   

uint16 y;

   

}data;

   

 

   

data val;

   

 

   

CYBLE_GATTS_HANDLE_VALUE_NTF_T MyHandle;

   

MyHandle.value.val = (uint8 *) &val;

   

MyHandle.value.len = sizeof(val);

   
        
0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

A struc in memory is in a general sense just a set aside of a given

   

chunk. So pointers can effectively treat it as a chunk of bytes, words,

   

longs, whatever, and operate on it as a whole, rather than specific

   

structure variables.

   

 

   

Thats one way,. for example, a struc can be handled in FLASH or EE

   

when a save/restore operation is called for. Rather than an update on

   

one variable in struc.

   

 

   

Regards, Dana.