I am getting a declaration is incompatible error even if the definitions and declarations of the said functions are the same. Here are the error message below from TI Code Composer Studio after build. Any ideas what could be causing these errors?
Error Message:
'#148 declaration is incompatible with "int32_t exoPal_atoi(char *)" (declared at line 65 of "C:\EK-TM4C129\exosite\exosite_pal.h") exosite_pal.c /getting_started_with_wlan_station_exosite_write/exosite line 509 C/C++ Problem
'#148 declaration is incompatible with "int32_t exoPal_sendingComplete()" (declared at line 62 of "C:\EK-TM4C129\exosite\exosite_pal.h") exosite_pal.c /getting_started_with_wlan_station_exosite_write/exosite line 260 C/C++ Problem
c function declarations at exosite_pal.h
#ifndef EXOSITE_HAL_H
#define EXOSITE_HAL_H
#include <stdint.h>
/*!< This defines the size of the rx buffer in the PAL. This buffer is used
to place incoming data from the modem in.*/
#define RX_BUFFER_SIZE 512
extern char exoPal_rxBuffer[RX_BUFFER_SIZE];
// defines
// functions for export
void exoPal_init();
uint8_t exoPal_setCik(const char * read_buffer);
uint8_t exoPal_getCik(char * read_buffer);
uint8_t exoPal_getModel(char * read_buffer);
uint8_t exoPal_getVendor(char * read_buffer);
uint8_t exoPal_getUuid(char * read_buffer);
uint8_t exoPal_tcpSocketClose();
uint8_t exoPal_tcpSocketOpen();
uint8_t exoPal_socketRead( char * buffer, uint16_t bufSize, uint16_t * responseLength);
uint8_t exoPal_socketWrite( const char * buffer, uint16_t len);
int32_t exoPal_sendingComplete( );
uint8_t exoPal_itoa(int value, char* str, uint8_t radix);
int32_t exoPal_atoi(char* val);
uint16_t exoPal_strlen(const char *s);
uint8_t exoPal_memcpy(char * dst, const char * src, uint16_t length);
char* exoPal_strstr(const char *in, const char *str);
#endif
c function definitions at exosite_pal.c
#include "exosite_pal.h"
#include "simplelink.h"
#include <stdio.h>
#include <stdlib.h>
int32_t exoPal_sendingComplete()
{
return 0;
}
int32_t exoPal_atoi(char* val)
{
return atoi(val);
}
Related
I created a variadic function to parse strings which might have different arguments.
In current case, the incoming string would be "38400,8,N,1\n".
static int parse_response(const char *buff, char *format, ...){
uint16_t retval=0;
va_list arg_ptr;
va_start(arg_ptr, format);
retval = vsscanf((const char*)buff, format, arg_ptr);
va_end(arg_ptr);
return retval;
}
here is the function where I called my vsscanf function.
satel_return_typedef Satel_GetBaudrate(satel_typedef* Satel, uint32_t* Baudrate, uint32_t timeout_ms){
int Len;
Len = printf_SLCommandString(Satel->tx_data_buffer, Satel->max_size, "SL%%B?");
uint8_t DataBits; char Parity; uint8_t StopBit;
if(Len < 0 || Len > (int)Satel->max_size) {
return R_API_ERROR;
}
if(Satel->Platform->Comm_Send(Satel->tx_data_buffer, Len, NULL)<0)
return R_PLATFORM_ERROR;
if(Satel_WaitForCommResponse(Satel,timeout_ms)==R_COMM_RESPONSE_VALUE){
parse_response((char*)Satel->rx_data_buffer,"%d,%d,%c,%d\n", Baudrate, &DataBits, &Parity, &StopBit);
return R_OP_SUCCESSED;
}
return R_OP_ERROR;
}
I know, there are some rules to not fall into undefined behaviour but, My mind confused about which rules are breaking here.
typedef void (*Write_Enamod_Pin)(satel_power_typedef Config);
typedef void (*Write_Service_Pin)(satel_service_typedef Config);
typedef int32_t (*Send_Message)(const uint8_t *data_to_send, uint32_t bytes_to_send, void *custom); /* -1 IO Error*/
typedef int32_t (*Read_Message)(uint8_t *data_to_read, uint32_t bytes_to_read, void *custom); /* -1 IO Error*/
typedef uint32_t (*Get_TickCount)(void);
typedef void (*Delay_ms)(uint32_t milisec);
typedef struct{
Write_Enamod_Pin Power;
Write_Service_Pin Service;
Send_Message Comm_Send;
Read_Message Comm_Read;
Get_TickCount Get_TickCount;
Delay_ms Delay_Ms;
}satel_hw_typedef;
typedef struct{
satel_state_typedef State;
uint8_t* tx_data_buffer;
uint8_t* rx_data_buffer;
uint32_t max_size;
satel_hw_typedef* Platform;
}satel_typedef;
You've got a mismatch of format specifiers.
The %d format specifier expect an int *. In the 3 places you're using it, you're passing a uint32_t *, a uint8_t *, and a uint8_t *. Those mismatches trigger undefined behavior.
You need to use u for an unsigned integer argument for all 3, and you want to use the hh modifier for the uint8_t (i.e. unsigned char) arguments. Also, you want to remove \n from the format string as that prevents reading from stopping until you press some other non-whitespace character after pressing ENTER.
"%u,%hhu,%c,%hhu"
I want to basically process a struct array in a method in a dynamic library, but when I pass the array and print it (in the exact same manner as in my main program) it has different values.
Consider a struct like this:
struct color {
uint8_t b;
uint8_t g;
uint8_t r;
uint8_t a;
}
And the code to print it looks like this:
printf("pos: %p\n", array);
for (i = 0; i < size; i++) {
printf("bgra: %08x\n", ((uint32_t *) array)[i]);
}
Now, what I'm doing in the test program is this:
printf("Table:\n");
print(table, size);
and the output looks like this (as excepted):
pos: 0x7fff5b359530
bgra: 00000000
bgra: ff0000ff
bgra: ff00ffff
But when i execute the same code in a function in the library this is what i get:
pos: 0x7fff5b359530
bgra: 00000008
bgra: 00000030
bgra: 5b3598e0
Now I'm wondering what I'm doing wrong, since i can't see a fault in my code. Also, the values must correlate somehow since, the output is always the same (Except for the address of course).
header.h
#include <stdint.h>
#ifndef __HEADER_H_
#define __HEADER_H_
struct bmpt_color_bgra {
uint8_t b;
uint8_t g;
uint8_t r;
uint8_t a;
};
void print(struct bmpt_color_bgra *table, uint8_t size);
uint8_t *gen(struct bmpt_color_bgra *table, uint8_t size);
#endif
library.c
#include <stdlib.h>
#include <stdio.h>
#include "header.h"
#define EXPORT __attribute__((visibility("default")))
__attribute__((constructor))
static void initializer(void) {
printf("[%s] initializer()\n", __FILE__);
}
__attribute__((destructor))
static void finalizer(void) {
printf("[%s] finalizer()\n", __FILE__);
}
EXPORT
void print(struct bmpt_color_bgra *table, uint8_t size) {
uint8_t i;
printf("pos: %p\n", table);
for (i = 0; i < size; i++) {
printf("bgra: %08x\n", ((uint32_t *) table)[i]);
}
}
EXPORT
uint8_t *gen(struct bmpt_color_bgra *table, uint8_t size) {
printf("table in func:\n");
print(table, size);
}
test.c
#include <stdio.h>
#include <stdlib.h>
#include "header.h"
int main(int argc, char **argv) {
struct bmpt_color_bgra arr[3];
struct bmpt_color_bgra c;
c.b = 0x0;
c.g = 0x0;
c.r = 0x0;
c.a = 0x0;
arr[0] = c;
c.b = 0xff;
c.a = 0xff;
arr[1] = c;
c.r = 0xff;
arr[2] = c;
//the first result (the correct one)
print(arr, 3);
//the second result
gen(arr, 3);
}
This probably comes down to memory alignment of the members within the struct, and the size of the struct itself differing between your program and the dynamic/shared library. You don't mention which compiler you are using, but using different compiler(s) or compiler options for your program and the shared library could cause this effect.
You can preserve binary compatibility between modules by specifying exactly how the members of the struct should be aligned. E.g in GCC you can force how the struct is represented in memory by use of an attribute.
See https://gcc.gnu.org/onlinedocs/gcc-3.3/gcc/Type-Attributes.html for GCC alignment instructions
struct bmpt_color_bgra {
uint8_t b;
uint8_t g;
uint8_t r;
uint8_t a;
} __attribute__ ((packed));
Also take a look at Byte Alignment for integer (or other) types in a uint8_t array for a similar question.
I am not receiving anything in buffer. Wherever I printf my buffer, it is always empty or shows garbage value. Can anyone help?
I defined header, packet and called them in my main, but buffer still shows garbage.
#include <stdint.h>
struct header {
uint16_t f1;
uint16_t f2;
uint32_t f3;
};
struct data {
uint16_t pf1;
uint64_t pf2;
};
#include <arpa/inet.h>
#include <string.h>
#include <stdint.h>
#include "packet.h"
void htonHeader(struct header h, char buffer[8]) {
uint16_t u16;
uint32_t u32;
u16 = htons(h.f1);
memcpy(buffer+0, &u16, 2);
printf("Value of buff is: %hu\n",buffer);
u16 = htons(h.f2);
memcpy(buffer+2, &u16, 2);
u32 = htonl(h.f3);
memcpy(buffer+4, &u32, 4);
}
void htonData(struct data d, char buffer[10]) {
uint16_t u16;
uint32_t u32;
u16 = htons(d.pf1);
memcpy(buffer+0, &u16, 2);
u32 = htonl(d.pf2>>32);
memcpy(buffer+2, &u32, 4);
u32 = htonl(d.pf2);
memcpy(buffer+6,&u32, 4);
}
void HeaderData(struct header h, struct data d, char buffer[18]) {
htonHeader(h, buffer+0);
htonData(d, buffer+8);
printf("buff is: %s\n",buffer);
}
#include <stdio.h>
#include "packet.c"
#include <string.h>
#include<stdlib.h>
int main(){
struct header h;
struct data d;
char buff[18];
//printf("Packet is: %s\n",buff);
printf("Generating Packets..... \n");
h.f1=1;
d.pf1=2;
h.f2=3;
d.pf2=4;
h.f3=5;
HeaderData(h,d,buff);
strcat(buff,buff+8);
printf("Packet is: %s\n",buff);
return 0;
}
The problem is that your printf()s are either syntactically wrong (printf( "%hu", ... ); expects an unsigned short as parameter, but you pass a pointer) or you try to print buff by using "%s" but the content is binary, not text. What you could do instead was doing some kind of hexdump, like:
int i;
for( i=0; i<sizeof( buff ); i++ ) {
printf( "%x ", buff[i] & 0xff );
}
puts( "" ); // terminate the line
Please note, that using sizeof works im main() only, in the other function you've got to determine the buffer size differently.
Besides: because of the binary content of buff, you can't use strcat(). Even if you have made sure that there is a '\0' behind the last value you have copied (I haven't checked if you have), depending on the integer values you copy, there may be another '\0' value before that one and strcat() would overwrite everything form that point on.
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_ */
I have the following code in a header file:
#ifndef BUFFER_H
#define BUFFER_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct c_buff
{
void *buffer; // data buffer
void *buffer_end; // end of data buffer
size_t capacity; // maximum number of items in the buffer
size_t count; // number of items in the buffer
size_t sz; // size of each item in the buffer
void *head; // pointer to head
void *tail; // pointer to tail
};
void cb_init(c_buff *cb, size_t capacity, size_t sz)
{
cb->buffer = malloc(capacity * sz);
if(cb->buffer == NULL) {
// handle error
}
cb->buffer_end = (char *)cb->buffer + capacity * sz;
cb->capacity = capacity;
cb->count = 0;
cb->sz = sz;
cb->head = cb->buffer;
cb->tail = cb->buffer;
}
#endif
And the following c file
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <common.h>
#include <usart.h>
#include <buffer.h>
struct c_buff usart_buffer;
struct c_buff *usart_buffer_ptr;
cb_init(usart_buffer_ptr, USART_BUFFER_SIZE, sizeof(char));
void initUSART(void) {
SETBIT(UCSR0A, UDRE0);
//SETBIT(UCSR0A, U2X0);
SETBIT(UCSR0C, UCSZ01);
SETBIT(UCSR0C, UCSZ00);
UBRR0 = 25;
SETBIT(UCSR0B, RXCIE0);
SETBIT(UCSR0B, TXCIE0);
SETBIT(UCSR0B, RXEN0);
SETBIT(UCSR0B, TXEN0);
}
ISR(USART_RX_vect) {
char data;
data = UDR0;
UDR0 = data;
}
ISR(USART_TX_vect) {
}
When I try to compile this I get an error that points to this line:
cb_init(usart_buffer_ptr, USART_BUFFER_SIZE, sizeof(char));
And it just says "error: expected ')' before numeric constant".
Google tells me it's some kind of preprocessor error. But I don't see how that could be the case.
I'm new to C so I apologize if it is something totally obvious.
You can't have a naked function call at the top level.
cb_init(usart_buffer_ptr, USART_BUFFER_SIZE, sizeof(char));
is a naked function call. Move that inside main().
You can't run function in global scope. It has to be done in main:
int main(int argc, char *argv[] {
cb_init(usart_buffer_ptr, USART_BUFFER_SIZE, sizeof(char));
}
The problem is that you're attempting to execute a method at the file level.
cb_init(usart_buffer_ptr, USART_BUFFER_SIZE, sizeof(char));
The C language only allows declarations / definitions at this level not actual executed statements. This call needs to be moved into a function definition.