Martin
Martin

Reputation: 2863

How to put the correct data type into function for Byte_t *Data?

I am developing for BLE Send/Receive data. I have some issue for data type change.

The type of the BLE Send Data function is like the following:

typedef unsigned char Byte_t;                   /* Generic 8 bit Container.   */
static void SPPLEDataIndicationEvent(DeviceInfo_t *DeviceInfo, unsigned int DataLength, Byte_t *Data)

I add a char array and set the data like the following:

#define ReservedCode                                   0x01
#define Sensor_TypeCode                                0xEA
static Byte_t         *SensorData;
SensorData[0]=ReservedCode;             
SensorData[1]=ReservedCode;           
SensorData[2]=Sensor_TypeCode;      
SensorData[3]=Sensor_TypeCode;              
SensorData[4]=Sensor_TypeCode;             

And I have try the following , but it seems did not working. The BLE App did not receive any data. (I am sure the DeviceInfo is no problem here.)

SPPLEDataIndicationEvent(DeviceInfo, sizeof(SensorData), SensorData);

How to put the correct data into SPPLEDataIndicationEvent function ?

Upvotes: 1

Views: 103

Answers (3)

LPs
LPs

Reputation: 16213

Your pointer is pointing to undefined address. It is Undefined Behavior

You must init your pointer

Byte_t  *SensorData = malloc(requested_size);
if (SensorData != NULL)
{
    // your stuff
}
free(SensorData);

Or declare it as an array

static Byte_t SensorData[requested_size];

Upvotes: 2

CWLiu
CWLiu

Reputation: 4043

I think you should add following line to allocate memory for your SensorData array:

SensorData=malloc(sizeof(Byte_t)*5);

Or you would get "segmentation fault" while you try to access SensorData[].

Upvotes: 0

Leandros
Leandros

Reputation: 16825

SensorData is just a pointer, it hold's no value. You need to actually allocate memory for it.

Changing the definition of SensorData to:

static Byte_t SensorData[5];

will fix your problem.

A wonder your program did run at all, you are accessing data you're not supposed to access.

Upvotes: 0

Related Questions