I'm writing a code to decode a specific, all function seem to be working fine with correct decoding result but I'm struggling to get the data into tydef struct
I'm define var like this
typedef struct{
unsigned char utctime[8];
unsigned char Lat[11];
unsigned char LatDirect;
unsigned char Status;
unsigned char Long[11];
unsigned char LongDirect;
//unsigned float speed;
unsigned char date[6];
}StrGPSDATA;
unsigned char datatestRealword1[100] = " *** TEST DATA HERE *** ";
Here is pseudo code.
unsigned char parseByDelimiter(char *inputData, const char delims, int index, unsigned int maxIndexSize){
unsigned char valOut[maxDataOut] = {0};
// Parse/Split value //
return valOut; // Debugging show correct result
}
void RMCDecode(unsigned char *dataIn, unsigned int sizeIn){
unsigned char dataExt[60] = {0};
// do something to get value to //
parseByDelimiter(dataExt, ',', 1, 60); // Send decoded value to parse
}
void main(void){
RMCDecode(datatestRealword1, 100);
}
A debugger report a correct value
But after when I tried
GPSDATARMC.utctime = parseByDelimiter(RMC_DATA, ',', 0, 10);
I gave an error incompatible data type, I don't know how to write value into my typedef. Anyone got an idea?
Thank you
Related
The following task is from an exam I took in engineering school (mechanical engineering):
You get an array unsigned char buffer[128]; used to read data from a source byte by byte, containing data of the structure:
struct Pixel {
unsigned char x;
unsigned char y;
unsigned char greyValue;
};
The task is: Create an instance of a pixel and copy the data content from the header of the buffer using "memcpy".
My aproach does not seem to work:
#include <stdio.h>
#include <string.h>
struct Pixel {
unsigned char x;
unsigned char y;
unsigned char greyValue;
};
int main()
{
unsigned char buffer[128] = {2,4,44};
struct Pixel singlePixel;
memcpy(singlePixel, buffer, 3);
printf("singlePixel.x = %d\n", singlePixel.x);
printf("singlePixel.y = %d\n", singlePixel.y);
printf("singlePixel.greyValue = %d\n", singlePixel.greyValue);
return 0;
}
I would expect that singlePixel.x = 2, singlePixel.y = 4 and singlePixel.greyValue = 44.
When debugging I get the error: incompatible type for argument 1 of ‘memcpy’
I'm also not at all sure if my approach is up to the task, since I don't understand exactly how this should works with the buffer...
I have a function which is performing some read operation from Sockets using udp,
the function is like this
int Read_packet(int CSPL_Socket, void *buf,int datalen)
{
int numb_bytes=0;
numb_bytes=read(CSPL_Socket, buf, datalen);
if(numb_bytes<0)
{
CSPL_SOK_ERROR(-6);
close(CSPL_Socket);
exit(1);
return -1;
}
else
{
//printf("Count %d\n",numb_bytes);
}
return 0;
}
When i call this from Eclipse ide, it shows a warning as shown below
But i dont see any performance issues here , everything is working good, how can i remove this warning ?
This is my definition for pkt
typedef struct UDP_Packet {
unsigned short Year; // year
unsigned char Month; // months
unsigned char Day; // day
unsigned char Hour; // hour
unsigned char Minute; // minute
unsigned char Seconds; // seconds
unsigned short Milliseconds; // milliseconds
unsigned char SeqNo; // packet sequence no
unsigned short CommandCode; // packet type
unsigned char DestSubSysID; // Destination sub system id (0 to 255)
unsigned char DestNodeID; // Destination node id
unsigned char SrcSubSysID; // Source sub system id
unsigned char SrcNodeID; // Source node id
unsigned short DataSize; // Data size in bytes
unsigned char AckSel; // select acknowledgment option
unsigned char AckID; // ID for ACK
unsigned char DataFlag; // Flag indicating single part(0) or multipart data (1)
unsigned char MessageID; // unique message ID
} UDP_Packet;
UDP_Packet pkt;
This is simple code; I am using this code snippet in my project and its working fine.
My question if it is the best way or otherwise What is the best way to set the bit fields using loops in C?
#define NUM_OF_MAX_SENSOR 10
typedef float SENSOR_VAL ;
typedef struct
{
unsigned char bit0:1;
unsigned char bit1:1;
unsigned char bit2:1;
unsigned char bit3:1;
unsigned char bit4:1;
unsigned char bit5:1;
unsigned char bit6:1;
unsigned char bit7:1;
}BYTE_FIELD;
typedef struct
{
BYTE_FIELD funP1;
BYTE_FIELD funP2;
}LOG_DATA;
LOG_DATA xlog;
typedef struct
{
unsigned char ok:1;
unsigned char open:1;
unsigned char shrt:1;
unsigned char unused:5;
}SENSOR_FLAGS;
typedef struct
{
SENSOR_FLAGS flag;
SENSOR_VAL val;
}SENSOR_DATA;
typedef union
{
SENSOR_DATA sensor[NUM_OF_MAX_SENSOR];
unsigned char buff[sizeof(SENSOR_DATA) * NUM_OF_MAX_SENSOR];
}SENSOR;
SENSOR sensor;
These are the structures I used and these are the max details I can provide.
void UpdateLogData()
{
int i = 0;
unsigned char *funPAddr =(unsigned char *) &xlog.funP1;
for(i=0;i<8;i++)
*(funPAddr) |= sensor.sensor[i].flag.ok << i;
printf("0x%x",*(funPAddr));
}
//main
int main()
{
sensor.sensor[0].flag.ok = 1;
sensor.sensor[4].flag.ok = 1;
sensor.sensor[5].flag.ok = 1;
sensor.sensor[6].flag.ok = 1;
sensor.sensor[7].flag.ok = 1;
UpdateLogData();
}
I want to define a structure that contains a data array and some pointers to the same data array. So I defined the structure and then I initialize the pointer like this:
typedef struct {
unsigned char data[MAX_PACKET_DATA];
unsigned char* sector1;
unsigned char* sector2;
unsigned char* sector3;
} Packet;
[...]
NGSCTransmittingDataPacket packet
[...]
packet->sector1 = packet.data + SECTOR1_OFFSET;
packet->sector2 = packet.data + SECTOR2_OFFSET;
packet->sector2 = packet.data + SECTOR3_OFFSET;
Can I initialize the pointer directly inside the typedef struct definition? If I write
typedef struct {
unsigned char data[MAX_PACKET_DATA];
unsigned char* sector1 = data + SECTOR1_OFFSET;
unsigned char* sector2 = data + SECTOR2_OFFSET;
unsigned char* sector3 = data + SECTOR3_OFFSET;
} Packet;
the compiler gives me error.
Any solution?
you cannot do that, C won't allow it. But in your example you could do that equivalent:
typedef struct {
unsigned char data[SECTOR1_OFFSET];
unsigned char sector1[SECTOR2_OFFSET-SECTOR1_OFFSET];
unsigned char sector2[SECTOR3_OFFSET-SECTOR2_OFFSET];
unsigned char sector3[MAX_PACKET_DATA-SECTOR3_OFFSET];
} Packet;
There's no padding since all members are char arrays.
You normally cannot go further SECTOR1_OFFSET-1 for an index of data, but there it would work (or create an union with a MAX_PACKET_DATA-length array if you want to make it cleaner, because some compilers could complain if you're accessing data with a bigger index)
Example with an union of 2 anonymous structures:
#include <stdio.h>
#define SECTOR1_OFFSET 20
#define SECTOR2_OFFSET 50
#define SECTOR3_OFFSET 80
#define MAX_PACKET_DATA 100
typedef union
{
struct
{
unsigned char sector0[SECTOR1_OFFSET];
unsigned char sector1[SECTOR2_OFFSET-SECTOR1_OFFSET];
unsigned char sector2[SECTOR3_OFFSET-SECTOR2_OFFSET];
unsigned char sector3[MAX_PACKET_DATA-SECTOR3_OFFSET];
};
struct
{
unsigned char data[MAX_PACKET_DATA];
};
} Packet;
int main()
{
Packet p;
p.data[SECTOR3_OFFSET] = 'a';
p.data[SECTOR3_OFFSET+1] = 'z';
p.data[SECTOR3_OFFSET+2] = '\0';
printf("sector 3 %s\n",p.sector3);
return 0;
}
result:
sector 3 az
I found an implementation of AES for a project I'm doing.
However when i integrate it I'm getting the following errors during the build.
In file included from ff.h:26:0,
from disp.h:4,
from main.c:14:
aes.h:14:3: error: conflicting types for 'AesCtx'
aes.h:14:3: note: previous declaration of 'AesCtx' was here
aes.h:28:5: error: conflicting types for 'AesCtxIni'
aes.h:28:5: note: previous declaration of 'AesCtxIni' was here
aes.h:29:5: error: conflicting types for 'AesEncrypt'
aes.h:29:5: note: previous declaration of 'AesEncrypt' was here
aes.h:30:5: error: conflicting types for 'AesDecrypt'
aes.h:30:5: note: previous declaration of 'AesDecrypt' was here
The header file itself is:
// AES context structure
typedef struct {
unsigned int Ek[60];
unsigned int Dk[60];
unsigned int Iv[4];
unsigned char Nr;
unsigned char Mode;
} AesCtx;
// key length in bytes
#define KEY128 16
#define KEY192 24
#define KEY256 32
// block size in bytes
#define BLOCKSZ 16
// mode
#define EBC 0
#define CBC 1
// AES API function prototype
int AesCtxIni(AesCtx *pCtx, unsigned char *pIV, unsigned char *pKey, unsigned int KeyLen, unsigned char Mode);
int AesEncrypt(AesCtx *pCtx, unsigned char *pData, unsigned char *pCipher, unsigned int DataLen);
int AesDecrypt(AesCtx *pCtx, unsigned char *pCipher, unsigned char *pData, unsigned int CipherLen);
and then the respective C file uses.
int AesCtxIni(AesCtx *pCtx, unsigned char *pIV, unsigned char *pKey, unsigned int KeyLen, unsigned char Mode)
{
// Cut out code for brevity
}
int AesEncrypt(AesCtx *pCtx, unsigned char *pData, unsigned char *pCipher, unsigned int DataLen)
{
// Cut out code for brevity
}
int AesDecrypt(AesCtx *pCtx, unsigned char *pCipher, unsigned char *pData, unsigned int CipherLen)
{
// Cut out code for brevity
}
I know these errors usually occur because either the function hasn't been pre-declared or because it's slightly different to it's declaration, but I can't see a difference.
Any ideas?
What compiler are you using? My best guess that it's trying to say aes.h is being #included twice. Try adding header guards at the beginning and end of the aes.h:
#ifndef AES_H_
#define AES_H_
typedef struct {
...
int AesDecrypt(AesCtx *pCtx, unsigned char *pCipher, unsigned char *pData, unsigned int CipherLen);
#endif /* !AES_H_ */