I am using PIC18F4550, and following the example given in https://www.electronicwings.com/pic/pic18f4550-usart, the program works because when i send a character via bluetooth (HC05) it received it and then transmit the same character, the problem is, that is done in the interrupt USART function and if i try to compare the character received in the main code for example
if(data == 'a')
The condition never met, but if i do that in the same interrupt fnction, it does recognize the character.
I am using MPLAB X v5.2 with XC8 compiler. The problem is not the bluetooth or the app used because i have used it before with arduino.
MAIN CODE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pic18f4550.h>
#include "Configuration_Header_File.h"
#include "LCD_16x2_8-bit_Header_File.h"
void USART_Init(int);
#define F_CPU 8000000/64
char data;
void __interrupt(low_priority) ISR(void)
{
while(RCIF==0);
data=RCREG;
LCD_Char(data);
while(TXIF==0);
if(data=='b')
TXREG='b';
if(data=='a')
TXREG='a';
}
void main()
{
OSCCON=0x72;
MSdelay(10);
LCD_Init();
USART_Init(9600);
LCD_String_xy(1,0,"Receive");
LCD_Command(0xC0);
while(1){
if(data=='c') /* THIS IS WHAT IS WRONG */
TXREG='c';
}
}
void USART_Init(int baud_rate)
{
float temp;
TRISC6=0; /*Make Tx pin as output*/
TRISC7=1; /*Make Rx pin as input*/
temp=(((float)(F_CPU)/(float)baud_rate)-1);
SPBRG=(int)temp; /*baud rate=9600 SPBRG=(F_CPU /(64*9600))-1*/
TXSTA=0x20; /*TX enable; */
RCSTA=0x90; /*RX enanle and serial port enable*/
INTCONbits.GIE=1; /*Enable Global Interrupt */
INTCONbits.PEIE=1; /*Enable Peripheral Interrupt */
PIE1bits.RCIE=1; /*Enable Receive Interrupt*/
PIE1bits.TXIE=1; /*Enable Transmit Interrupt*/
}
As you can see in the code, the conditions inside the interrupt function works, because if i send an 'a' via bluettoth (APP), an 'a' is what i received in the same APP, and same with a 'b', but, when i received a 'c', nothing happens. So i guess that the problem is how is declare the variable "data" or something related to that variable, i have not tried to make it global but i am pretty sure there have to be a better solution. Maybe the content in "data" is erased when the interrupt is done.
I expect the "data" content to be available in the main code but i do not know why it is only recognize in the USART interrupt function.
I would appreciate all your help.
You need to capture the received data in a FIFO, or other buffering solution for use in the main 'thread'.
The idea is to keep buffering input bytes continously from your ISR and while grabbing the input at your leisure from the main loop.
Here's an example of a simple COM port solution... I have not tested it, but it shows the minimal steps needed for an echo application.
char rx_buffer[16];
char tx_buffer[16];
_fifo_t rx_fifo; // need 2 buffers, one for sending, and one for receiving.
_fifo_t tx_fifo;
void __interrupt(low_priority) ISR(void)
{
// never wait for a flag to becom set or reset within an ISR. the ISR will be
// called automatically when the
while (RCIF)
{
data=RCREG;
LCD_Char(data); // you should considfer moving this out o the ISR.
FifoPutByte(&rx_fifo, data); // check for overruns is handled by the fifo module.
}
while (TXIE && TXIF && !FifoEmpty(&tx_fifo))
TXREG = FifoGetByte(&tx_fifo);
if (FifoEmpty(&tx_fifo)) // if no more data to send, disable TX interrupt.
TXIE = 0;
}
int COM_Transmit(char c)
{
if (!FifoPutByte(&tx_fifo, c))
return 0; // buffer is full!
TXIE = 1; // enable UART TX interrupts
// now comes the tricky part, you need to 'prime' the transmission of bytes
// in the transmit fifo. This operation MUST be done
// with interrupts disabled.
if (TXIF) // can only be true if TX interrupts are not kicking in.
{
PEIE = 0; // disable peripheral interrupts
TXREG = FifoGetByte(&tx_fifo); // clears TXIF
PEIE = 1; // enable peripheral inrerrupts
// ISR will be called when UART is done sending the byte in TXREG.
}
}
void main()
{
/* initialization code... */
FifoInit(&rx_fifo, rx_buffer, sizeof(rx_buffer));
FifoInit(&tx_fifo, tx_buffer, sizeof(tx_buffer));
while(1)
{
// simple UART echo code
while (!FifoEmpty(&rx_fifo) && !FifoFull(&tx_fifo))
COM_Transmit(FifoGetByte(&rx_fifo));
/* doing the other stuff that your controller must do may take some time */
/* You must make sure that rx_buffer is large enough to capture any bytes */
/* that could be received during this delay, at 9600 bauds, count 1ms per char */
MSdelay(15); // for example... A larger processing time will warrant larger buffer size for rx_fifo.
}
}
include file:
#ifndef _fifo_h
#define _fifo_h
/* *****************************************************************************
*
* file: fifo.h
*
* Simple byte fifo definitions.
*
******************************************************************************/
#define FIFO_DEBUG 0
typedef struct _fifo_t
{
volatile unsigned char* pHead; // head of data to extract.
volatile unsigned char* pTail; // tail of data for insertion.
unsigned char* pStart; // start of buffer.
unsigned char* pEnd; // end of buffer.
volatile unsigned int nOverruns; // overrun counter, in bytes.
volatile unsigned int nUnderruns; // underrun counter, in bytes.
#ifdef FIFO_DEBUG
volatile unsigned int nWaterMark; // Water mark indicating the maximum number of bytes ever held.
#endif
} fifo_t;
void FifoInit(fifo_t* fifo, unsigned char* buffer, unsigned int len);
void FifoReset(fifo_t* fifo);
int FifoEmpty(fifo_t* fifo);
unsigned int FifoGetFreeSpace(fifo_t* fifo);
// returns nuèmber of bytes available for GET op
unsigned int FifoGetCount(fifo_t* fifo);
// returns total buffer length
unsigned int FifoGetSize(fifo_t* fifo);
// returns the number of bytes in overrun
unsigned int FifoPut(fifo_t* fifo, const void* data, unsigned int len);
// return the number of bytes copied to data buffer.
unsigned int FifoGet(fifo_t* fifo, void* data, unsigned int len);
// returns negative value if fifo was empty
int FifoGetByte(fifo_t* fifo);
// returns non zetro value on success.
int FifoPutByte(fifo_t* fifo, unsigned char c);
// returns a contiguous buffer inside fifo, the length of contiguous part is returned in availLen.
// caller is responsible for advancing get/put pointer when the data is consumed.
unsigned char* FifoGetContiguousPutBuffer(fifo_t* fifo, unsigned int reqLen, unsigned int* availLen);
unsigned char* FifoGetContiguousGetBuffer(fifo_t* fifo, unsigned int reqLen, unsigned int* availLen);
// - peeks at data inside fifo, with optional offset from GET pointer,
// the length of contiguous part is returned in availLen.
// - buffer must be large enough to hold reqLen bytes.
// - the value returned is either a direct pointer to fifo buffer, if the data is contuguous, or buffer.
// if the data was copied to it, or NULL if the data is not avialable in buffer.
// - caller is responsible for advancing get/put pointer when the data is consumed.
void* FifoPeekBuffer(fifo_t* fifo, unsigned int offset, unsigned int reqLen, void* buffer);
// - Modifies a single byte at offset from GET pointer.
// - returns non-zero if successful.
char FifoPoke(fifo_t* fifo, unsigned int offset, unsigned char c);
// NOTE: interrupts should be disabled around calls to this function.
void FifoSetCount(fifo_t* fifo, unsigned int count);
unsigned int FifoAdvancePutPtr(fifo_t* fifo, unsigned int len);
unsigned int FifoAdvanceGetPtr(fifo_t* fifo, unsigned int len);
#endif
.c module:
/* *****************************************************************************
*
* file: fifo.c
*
* Simple byte fifo definitions.
*
******************************************************************************/
#include <string.h> // memcpy()
#include "fifo.h"
void FifoInit(fifo_t* fifo, unsigned char* buffer, unsigned int len)
{
fifo->pStart = (unsigned char*)(buffer);
fifo->pEnd = (unsigned char*)((buffer) + (len));
fifo->pHead = (unsigned char*)(buffer);
fifo->pTail = (unsigned char*)(buffer);
fifo->nOverruns = 0;
#ifdef FIFO_DEBUG
fifo->nWaterMark = 0;
#endif
}
void FifoReset(fifo_t* fifo)
{
fifo->pHead = fifo->pStart;
fifo->pTail = fifo->pStart;
fifo->nOverruns = 0;
}
int FifoEmpty(fifo_t* fifo)
{
return (fifo->pHead == fifo->pTail);
}
unsigned int FifoGetFreeSpace(fifo_t* fifo)
{
return ((fifo->pEnd - fifo->pStart) - FifoGetCount(fifo)) - 1;
}
unsigned int FifoGetCount(fifo_t* fifo)
{
unsigned char* pHead, * pTail;
unsigned int nResult;
pHead = (unsigned char*)fifo->pHead;
pTail = (unsigned char*)fifo->pTail;
if (pTail >= pHead)
{
nResult = pTail - pHead;
}
else
{
nResult = (pTail - fifo->pStart) + (fifo->pEnd - pHead);
}
return nResult;
}
unsigned int FifoGetSize(fifo_t* fifo)
{
return fifo->pEnd - fifo->pStart;
}
unsigned int FifoPut(fifo_t* fifo, const void* data, unsigned int len)
{
unsigned int nMax, nLeft;
unsigned char* pTail, * pHead;
const unsigned char* p;
nLeft = (len);
p = (const unsigned char*)data;
while(nLeft)
{
pHead = (unsigned char*)fifo->pHead;
if (pHead > fifo->pTail)
{
nMax = (pHead - fifo->pTail) - 1;
}
else
{
nMax = fifo->pEnd - fifo->pTail;
}
if (nMax > nLeft)
{
nMax = nLeft;
}
if (!nMax)
{
fifo->nOverruns += nLeft;
break;
}
memcpy((void*)fifo->pTail, p, nMax);
pTail = (unsigned char*)fifo->pTail + nMax;
if (pTail >= fifo->pEnd)
{
pTail = fifo->pStart;
}
fifo->pTail = pTail;
p += nMax;
nLeft -= nMax;
}
#ifdef FIFO_DEBUG
nMax = FifoGetCount(fifo);
if (nMax > fifo->nWaterMark)
{
fifo->nWaterMark = nMax;
}
#endif
return nLeft;
}
unsigned int FifoGet(fifo_t* fifo, void* data, unsigned int len)
{
unsigned int nMax, nLeft;
unsigned char* p, * pTail, * pHead;
nLeft = (len);
p = (unsigned char*)(data);
while(nLeft)
{
pTail = (unsigned char*)fifo->pTail;
if (pTail >= fifo->pHead)
{
nMax = pTail - fifo->pHead;
}
else
{
nMax = fifo->pEnd - fifo->pHead;
}
if (!nMax)
{
break;
}
if (nMax > nLeft)
{
nMax = nLeft;
}
memcpy(p, (void*)fifo->pHead, nMax);
pHead = (unsigned char*)fifo->pHead + nMax;
if (pHead >= fifo->pEnd)
{
pHead = fifo->pStart;
}
fifo->pHead = pHead;
p += nMax;
nLeft -= nMax;
}
return len - nLeft;
}
int FifoPutByte(fifo_t* fifo, unsigned char c)
{
int result = 0;
if (FifoGetFreeSpace(fifo))
{
unsigned char* pTail = (unsigned char*)fifo->pTail;
*pTail++ = c;
if (pTail >= fifo->pEnd)
{
pTail = fifo->pStart;
}
fifo->pTail = pTail;
result = 1;
}
return result;
}
int FifoGetByte(fifo_t* fifo)
{
int result = -1;
if (fifo->pHead != fifo->pTail)
{
unsigned char* pHead = (unsigned char*)fifo->pHead;
result = *(fifo->pHead);
if (++pHead >= fifo->pEnd)
{
pHead = fifo->pStart;
}
fifo->pHead = pHead;
}
return result;
}
unsigned char* FifoGetContiguousPutBuffer(fifo_t* fifo, unsigned int reqLen, unsigned int* availLen)
{
unsigned int nMax;
unsigned char* pHead;
pHead = (unsigned char*)fifo->pHead;
if (pHead > fifo->pTail)
{
nMax = (pHead - fifo->pTail) - 1;
}
else
{
nMax = fifo->pEnd - fifo->pTail;
if (pHead == fifo->pStart)
{
--nMax;
}
}
if (nMax > reqLen)
{
nMax = reqLen;
}
*availLen = nMax;
return (unsigned char*)fifo->pTail;
}
unsigned char* FifoGetContiguousGetBuffer(fifo_t* fifo, unsigned int reqLen, unsigned int* availLen)
{
unsigned int nMax;
unsigned char* pTail;
pTail = (unsigned char*)fifo->pTail;
if (pTail >= fifo->pHead)
{
nMax = pTail - fifo->pHead;
}
else
{
nMax = fifo->pEnd - fifo->pHead;
}
if (nMax > reqLen)
{
nMax = reqLen;
}
*availLen = nMax;
return (unsigned char*)fifo->pHead;
}
void* FifoPeekBuffer(fifo_t* fifo, unsigned int offset, unsigned int reqLen, void* buffer)
{
void* result;
result = NULL;
if ((reqLen + offset) <= FifoGetCount(fifo))
{
unsigned char* pHead, * pTail;
pHead = (unsigned char*)fifo->pHead + offset;
if (pHead >= fifo->pEnd)
{
pHead -= (fifo->pEnd - fifo->pStart);
}
pTail = pHead + reqLen;
if (pTail <= fifo->pEnd)
{
result = pHead;
}
else
{
// need to copy, as requested data wraps around
unsigned char* p;
unsigned int n;
p = (unsigned char*)buffer;
n = fifo->pEnd - pHead;
memcpy(p, pHead, n);
memcpy(p + n, fifo->pStart, reqLen - n);
result = buffer;
}
}
return result;
}
char FifoPoke(fifo_t* fifo, unsigned int offset, unsigned char c)
{
unsigned char* p;
char result = 0;
if (offset < FifoGetCount(fifo))
{
p = (unsigned char*)fifo->pHead + offset;
if (p >= fifo->pEnd)
{
p -= (fifo->pEnd - fifo->pStart);
}
*p = c;
result = 1;
}
return result;
}
unsigned int FifoAdvancePutPtr(fifo_t* fifo, unsigned int len)
{
unsigned int n;
unsigned char* pTail;
n = FifoGetFreeSpace(fifo);
if (len > n)
{
fifo->nOverruns += (len - n);
len = n;
}
pTail = (unsigned char*)fifo->pTail + len;
while (pTail >= fifo->pEnd)
{
pTail -= (fifo->pEnd - fifo->pStart);
}
fifo->pTail = pTail;
#ifdef FIFO_DEBUG
n = FifoGetCount(fifo);
if (n > fifo->nWaterMark)
{
fifo->nWaterMark = n;
}
#endif
return len;
}
unsigned int FifoAdvanceGetPtr(fifo_t* fifo, unsigned int len)
{
unsigned int n;
unsigned char* pHead;
n = FifoGetCount(fifo);
if (len > n)
{
fifo->nUnderruns += (len - n);
len = n;
}
pHead = (unsigned char*)fifo->pHead + len;
while (pHead >= fifo->pEnd)
{
pHead -= (fifo->pEnd - fifo->pStart);
}
fifo->pHead = pHead;
return len;
}
void FifoSetCount(fifo_t* fifo, unsigned int count)
{
unsigned int n;
unsigned char* pTail;
n = FifoGetSize(fifo);
if (count > n)
{
fifo->nOverruns += (count - n);
count = n;
}
pTail = (unsigned char*)fifo->pHead + count;
while (pTail >= fifo->pEnd)
{
pTail -= (fifo->pEnd - fifo->pStart);
}
fifo->pTail = pTail;
#ifdef FIFO_DEBUG
n = FifoGetCount(fifo);
if (n > fifo->nWaterMark)
{
fifo->nWaterMark = n;
}
#endif
}
Note that this fifo struct is not the most efficient for a small PIC, but it should work fine nonetheless. Writing your own FIFO is a very good exercise. The one above is taken from production code, I think I last used it on an STM32. It's also been used in production on dsPIC and on Arduino.
Related
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
#include <math.h>
#include <time.h>
#define RAND(lower,upper) (rand()%(upper-lower+1))+lower
int power(int base, int exp)
{
int result=1;
while (exp != 0)
{
result *= base;
--exp;
}
return result;
}
int isKthBitSet(int n, int k)//from right, 1<=k<=n
{
int new_num = n >> (k - 1);
// if it results to '1' then bit is set,
// else it results to '0' bit is unset
return (new_num & 1);
}
struct Astructure{
uint16_t bitmap;
uint32_t a;
uint32_t b;
char str[10];
uint16_t d;
}__attribute__((packed));
int main()
{
struct Astructure abitmap;
abitmap.bitmap = 0;
char buffer[(sizeof(struct Astructure))];
abitmap.a = 52;
abitmap.b = 16;
char c[10]={"ramya"};
strcpy(abitmap.str, c);
abitmap.d = 59;
char ch;
srand(time(0));
for(uint8_t position =1;position<5;position++)
{
int random10 = RAND(0,1);
if(random10==1)
{
int value = power(2,position-1);
abitmap.bitmap = abitmap.bitmap | value;
}
}
memcpy(buffer, (char *)&abitmap.bitmap, sizeof(abitmap.bitmap)+1);
uint8_t temp4 = isKthBitSet(abitmap.bitmap,4);
uint8_t temp3 = isKthBitSet(abitmap.bitmap,3);
uint8_t temp2 = isKthBitSet(abitmap.bitmap,2);
uint8_t temp1 = isKthBitSet(abitmap.bitmap,1);
uint8_t previousLength = sizeof(abitmap.bitmap);
if(temp4){
//add a to buffer
memcpy(buffer + previousLength, (void *)&abitmap.a, sizeof(abitmap.a)+1);
previousLength += sizeof(abitmap.a);
}
if(temp3){
//add b to buffer
memcpy(buffer + previousLength, (void *)&abitmap.b, sizeof(abitmap.b)+1);
previousLength += sizeof(abitmap.b);
}
if(temp2){
//add c to buffer
memcpy(buffer + previousLength, (void *)&abitmap.str, sizeof(abitmap.str)+1);
previousLength += sizeof(abitmap.str);
}
if(temp1){
//add d to buffer
memcpy(buffer + previousLength, (char *)&abitmap.d, sizeof(abitmap.d)+1);
previousLength += sizeof(abitmap.d);
}
//SHOW BUFFER
previousLength = sizeof(abitmap.bitmap);
uint32_t a;
uint32_t b;
char str[10];
uint16_t d;
if(temp4){
memcpy(&a , (void *) buffer+previousLength , sizeof(abitmap.a)+1);//
printf("a = %d\t",a);
previousLength += sizeof(a);
}
if(temp3){
memcpy(&b , (void *) buffer+previousLength , sizeof(abitmap.b)+1);
printf("b = %d\t",b);
previousLength += sizeof(b);
}
if(temp2){
memcpy(str , (void *) buffer+previousLength , sizeof(abitmap.str)+1);//memccpy
printf("string = %s\t",str);
previousLength += sizeof(str);
}
if(temp1){
memcpy(&d , (void *) buffer+previousLength , sizeof(abitmap.d)+1);
printf("d = %d\t",d);
previousLength += sizeof(d);
}
printf("\n");
}
I trying to copy some variables to my buffer. The variables I want to add in the buffer is picked at random. Here I gave the size of my buffer before hand to the size of my structure. But as per my program I may not be adding all my variables in the buffer. So its a waste.
So How am I supposed to declare my buffer here.
if my replace this
char buffer[(sizeof(struct Astructure))];
to
char *buffer;
this
I get below error.
a = 52 string = ramya
*** stack smashing detected ***: terminated
Aborted (core dumped)
I dont want to waste the size of my buffer in here, giving the whole size of structure. so what do i do.
If you want to allocate only the required memory:
typedef struct
{
size_t length;
unsigned char data[];
}buff_t;
buff_t *addtobuffer(buff_t *buff, const void *data, size_t size)
{
size_t pos = buff ? buff -> length : 0;
buff = realloc(buff, pos + size + sizeof(*buff));
if(buff)
{
memcpy(buff -> data + pos, data, size);
buff -> length = pos + size;
}
return buff;
}
and example usage:
buff_t *buffer = NULL;
/* ... */
if(temp4)
{
buff_t *tmp = addtobuffer(buffer, &abitmap.a, sizeof(abitmap.a)+1);
if(!tmp) { /* error handling */}
buffer = tmp;
/*...*/
}
First of all I found similar question here. But I didn't understand how this should work:
int data = rand();
char* tosend = (char*)&data;
int remaining = sizeof(data);
I want to send size of file which I store into int variable. And I use next code for this:
Server side:
struct stat st;
stat("file", &st);
int file_size = st.st_size;
write(client_fd, &file_size, sizeof(file_size));
Client side:
int file_size;
read(server_fd, &file_size, sizeof(int));
And it works fine. But in documentation of write says that:
Note that a successful write() may transfer fewer than count
bytes
And same for read:
It is not an error if this number is smaller than the number of
bytes requested
If I'd send buffer of chars, which are 1 byte long each. I would just add offset to the buffer and store each next of bytes in buffer. Like this:
int write_all(int fd, void* buffer, int count) {
int offset = 0, b = 0, status;
while ((status = write(fd, buffer + offset, count)) != count) {
if (status == -1) {
/error check/
return -1;
}
count -= status;
offset += status;
b += status;
}
b += status;
return b;
}
But how to make the same thing for variable?
The example you refer to is not great because it gives no consideration of the integer byte order - and you have no guarantee that the byte order on the receiving host will be the same as on the host you are sending from.
My suggestion would be to
choose a byte order - LSB first (little endian) or MSB first (big endian), and then
allocate a char or uint8_t buffer of the relevant size (e.g. 2 bytes for a 16-bit integer, 4 bytes for a 32-bit integer) and store the integer there byte by byte
use your existing write_all() function to send it.
Big-endian is maybe more typical for network data, so it might be better to use that. Then, if you want to send a 32-bit integer ...
void send_u32(int client_fd, int32_t n) {
uint8_t buf[4];
// Big-endian byte order ...
for (int i=0; i<4; i++) {
buf[i] = n >> ((3-i)*8);
}
int res = write_all(client_fd, buf, 4);
}
For 16-bit integer just use a 2-byte buffer.
You will need to #include <stdint.h> for above.
P.S. st_size in struct stat is of type off_t - which I think might be OS-dependent. Maybe someone who knows more about POSIX can shed some light on that. (You can probably forget about using a 16-bit integer anyway - as I expect it will always be at least a 32-bit integer, except maybe on some embedded systems?.)
Your write_all() implementation is faulty. It will fail on short writes.
Consider these:
size_t write_some(const int desc, const void *buffer, const size_t min_length, const size_t max_length)
{
const char *ptr = (const char *)buffer;
const char *const need = (const char *)buffer + min_length;
const char *const end = (const char *)buffer + max_length;
while (ptr < need) {
ssize_t len = write(desc, ptr, (size_t)(end - ptr));
if (len > 0) {
ptr += len;
} else
if (len != -1) {
errno = EIO;
return (size_t)(ptr - (const char *)buffer);
} else {
/* errno set by write() */
return (size_t)(ptr - (const char *)buffer);
}
}
errno = 0;
return (size_t)(ptr - (const char *)buffer);
}
size_t read_some(const int desc, void *buffer, const size_t min_length, const size_t max_length)
{
char *const end = (char *)buffer + max_length;
char *const need = (char *)buffer + min_length;
char *ptr = (char *)buffer;
while (ptr < need) {
ssize_t len = read(desc, ptr, (size_t)(end - ptr));
if (len > 0) {
ptr += len;
} else
if (len == 0) {
errno = 0;
return (size_t)(ptr - (char *)buffer);
} else
if (len != -1) {
errno = EIO;
return (size_t)(ptr - (char *)buffer);
} else {
/* errno set by read() */
return (size_t)(ptr - (char *)buffer);
}
}
errno = 0;
return (size_t)(ptr - (char *)buffer);
}
They both always set errno, to zero if successful, errno otherwise, and return the actual number of bytes sent/received.
If you receive a stream (using descriptor desc) of packets starting with four-byte big-endian (network-endian) length fields, the length including the four-byte length field itself (so that its minimum valid value is 4), you can use the following:
static inline size_t packet_size4(const void *const from)
{
const unsigned char *src = (const unsigned char *)from;
return ((size_t)(src[0]) << 24)
| ((size_t)(src[1]) << 16)
| ((size_t)(src[2]) << 8)
| src[3];
}
int handle_all_packets(int desc, unsigned char *buffer, size_t maxlen)
{
size_t have = 0;
size_t len = 0;
while (1) {
if (have < 4) {
have += read_some(desc, buffer + have, 4 - have, maxlen - have);
if (have < 4)
return errno;
len = packet_size4(buffer);
if (len < 4)
return errno = EBADMSG;
if (len > maxlen)
return errno = EOVERFLOW;
}
if (have < len) {
have += read_some(desc, buffer + have, len - have, maxlen - have);
if (errno)
return errno;
if (have < len)
return errno = EIO;
}
/* Process len-byte packet in buffer. */
if (have > len) {
memmove(buffer, buffer + len, have - len);
have -= len;
len = 0;
} else {
have = 0;
len = 0;
}
}
}
If there were no errors, it will return 0, otherwise it returns a nonzero errno error code.
To send such a packet, you can use the following:
static inline void set_packet_size4(unsigned char *dst, size_t size)
{
dst[0] = (size >> 24) & 255;
dst[1] = (size >> 16) & 255;
dst[2] = (size >> 8) & 255;
dst[3] = size & 255;
}
int send_packet(const int desc, const void *data, const size_t len)
{
size_t n;
{
unsigned char buf[4];
set_packet_size4(buf, len + 4);
n = write_some(desc, buf, 4, 4);
if (errno)
return errno;
if (n != 4)
return errno = EIO;
}
n = write_some(desc, data, len, len);
if (errno)
return errno;
if (n != len)
return errno = EIO;
return 0;
}
If the packet is written correctly, the function returns 0, otherwise it returns a nonzero errno error code.
There is actually a standardized network byte order (which happens to be big endian), and standard functions to convert from host to network and vice versa:
htons() Host to Network Short
htonl() Host to Network Long
ntohl() Network to Host Long
ntohs() Network to Host Short
Use these for a portable way to transfer binary integers over the network. You might also look at writen() for really, really trying to write n bytes, although that function comes with its own set of pitfalls.
Both write and read return the number of bytes sent or received respectively. Don't start processing your data before you have received enough bytes to do so.
In more detail: TCP/IP will try to pass on the data it has as quickly as possible. Therefore if data is split between packets, your read may return before the expected number of bytes has been received, this may allow you to get a head start on data processing instead of waiting.
I am seeing a potential overflow: streamBuffer is a struct object (part of the FreeRTOS lib) and upon executing the following line in OutputToSerial(), I see streamBuffer.xHead's value is set to an extremely large value even though it's not being modified at the time.
LONG_TO_STR(strData, txStr);
Note that I didn't have any issues when I called nRF24_ReadReg() multiple times before.
Also, often times I see that printf doesn't print the entire text that's being printed (prior to calling the time when I see a potential overflow) - instead misses on some chars.
Any way to get a better understanding of the cause? I don't see any hardfaults or anything to look in the registers...
For a reference, the following is the struct's definition:
typedef struct StreamBufferDef_t /*lint !e9058 Style convention uses tag. */
{
volatile size_t xTail; /* Index to the next item to read within the buffer. */
volatile size_t xHead; /* Index to the next item to write within the buffer. */
size_t xLength; /* The length of the buffer pointed to by pucBuffer. */
size_t xTriggerLevelBytes; /* The number of bytes that must be in the stream buffer before a task that is waiting for data is unblocked. */
volatile TaskHandle_t xTaskWaitingToReceive; /* Holds the handle of a task waiting for data, or NULL if no tasks are waiting. */
volatile TaskHandle_t xTaskWaitingToSend; /* Holds the handle of a task waiting to send data to a message buffer that is full. */
uint8_t *pucBuffer; /* Points to the buffer itself - that is - the RAM that stores the data passed through the buffer. */
uint8_t ucFlags;
#if ( configUSE_TRACE_FACILITY == 1 )
UBaseType_t uxStreamBufferNumber; /* Used for tracing purposes. */
#endif
} StreamBuffer_t;
// file.c
#define PRI_UINT64_C_Val(value) ((unsigned long) (value>>32)), ((unsigned long)value)
#define LONG_TO_STR(STR, LONG_VAL) (sprintf(STR, "%lx%lx", PRI_UINT64_C_Val(LONG_VAL)))
unsigned long long concatData(uint8_t *arr, uint8_t size)
{
long long unsigned value = 0;
for (uint8_t i = 0; i < size; i++)
{
value <<= 8;
value |= arr[i];
}
return value;
}
void nRF24_ReadReg(nrfl2401 *nrf, uint8_t reg, const uint8_t rxSize, uint8_t *rxBuffer, char *text)
{
uint8_t txBuffer[1] = {0};
uint8_t spiRxSize = rxSize;
if (reg <= nRF24_CMD_W_REG)
{
txBuffer[0] = nRF24_CMD_R_REG | (reg & nRF24_R_W_MASK);
spiRxSize++;
}
else
{
txBuffer[0] = reg;
}
nRF24_SendCommand(nrf, txBuffer, rxBuffer, spiRxSize);
OutputToSerial(txBuffer, rxBuffer, spiRxSize, text);
}
void OutputToSerial(uint8_t *writeBuffer, uint8_t *readBuffer, uint8_t size, char *text)
{
char strData[100] = {0}, rxStrData[100] = {0};
long long unsigned txStr = concatData(writeBuffer, size);
long long unsigned rxStr = concatData(readBuffer, size);
LONG_TO_STR(strData, txStr); // POTENTIAL ERROR.....!
LONG_TO_STR(rxStrData, rxStr);
char outputMsg[60] = {0};
strcpy(outputMsg, text);
strcat(outputMsg, ": 0x%s ----------- 0x%s\n");
printf (outputMsg, strData, rxStrData);
}
// main.c
StreamBufferHandle_t streamBuffer;
Perhaps other issues, yet the LONG_TO_STR(x) is simply a mess.
Consider the value 0x123400005678 would print as "12345678". That ref code is broken.
Yes its too bad code has long long yet no "%llx". Easy enough to re-write it all into a clean function.
//#define PRI_UINT64_C_Val(value) ((unsigned long) (value>>32)), ((unsigned long)value)
//#define LONG_TO_STR(STR, LONG_VAL) (sprintf(STR, "%lx%lx", PRI_UINT64_C_Val(LONG_VAL)))
#include <stdio.h>
#include <string.h>
#include <limits.h>
// Good for base [2...16]
void ullong_to_string(char *dest, unsigned long long x, int base) {
char buf[sizeof x * CHAR_BIT + 1]; // Worst case size
char *p = &buf[sizeof buf - 1]; // set to last element
*p = '\0';
do {
p--;
*p = "0123456789ABCDEF"[x % (unsigned) base];
x /= (unsigned) base;
} while (x);
strcpy(dest, p);
}
int main(void) {
char buf[100];
ullong_to_string(buf, 0x123400005678, 16); puts(buf);
ullong_to_string(buf, 0, 16); puts(buf);
ullong_to_string(buf, ULLONG_MAX, 16); puts(buf);
ullong_to_string(buf, ULLONG_MAX, 10); puts(buf);
return 0;
}
Output
123400005678
0
FFFFFFFFFFFFFFFF
18446744073709551615
First I will explain what I want to program. I want to open a directory and get all the files names and their number (in the directory). I want to allocate certain amount of memory for the number of files I have found, and a certain amount of memory for their names. I will give an example.
Lets say we have got a directory named dir and that directory has 4 files in it. The 4 files are named as follows: lalo, camo, mara, sarw.
I want my program to work as follows
Go into dir and while finding files keep allocating memory in order to store them (like a table of strings where I don't know the exact size because I don't know how many files I will find). I am storing the filenames character by character and I keep allocating memory as needed, every time I read one char I am realocating memory and incrementing by 1. Same logic I follow to the files number where I keep incrementing the number of files and reallocating memory as needed. A table like this (pointers) is what I imagine filenames[numberOfFiles][stringCharacter].
I get a segmentation fault in function GetFiles(...) on the second loop here : *(*(entries+*number)+size) = dp->d_name[size];
int main()
{
int number,i;
char *mainDirectory = NULL,**names = NULL;
printf("Give the movies directory: ");
mainDirectory = ReadMainDirectory();
if(GetFiles(mainDirectory,&names,&number) != 0)
{
system("PAUSE");
return 1;
}
system("PAUSE");
return 0;
}
char* ReadMainDirectory()
{
char *dir,c;
int size = 1;
dir = (char*)malloc(size+1);
dir[size-1] = '\0';
while((c = getchar()) != '\n')
{
dir[size-1] = c;
size++;
dir = (char*)realloc(dir,size+1);
dir[size-1] = '\0';
}
return dir;
}
int GetFiles(char *dir,char ***names,int *number)
{
struct dirent *dp;
DIR *fd;
int size;
char **entries = NULL;
if ((fd = opendir(dir)) == NULL)
{
printf("Can't open directory %s!\n",dir);
return 0;
}
*number = 0;
size = 0;
entries = (char**)malloc((*number+1) * sizeof(char*));
*entries = (char*)malloc((size+1) * sizeof(char));
while ((dp = readdir(fd)) != NULL)
{
if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
{
continue;
}
size = 0;
while(dp->d_name[size] != '\0')
{
*(*(entries+*number)+size) = dp->d_name[size];
size++;
*(entries+*number) = (char*)realloc(entries[*number],(size+1) * sizeof(char));
}
entries[*number][size] = '\0';
printf("%s\n",entries[*number]);
(*number)++;
entries = (char**)realloc(entries,(*number+1) * sizeof(char*));
}
*names = entries;
closedir(fd);
return 1;
}
int GetStringSize(char *string)
{
int size = 0;
while(string[size] != '\0')
{
size++;
}
return size;
}
int StringEndsWith(char* string,char* extension)
{
return !strcmp(string + strlen(string) - strlen(extension), extension);
}
A proper implementation of getfiles (gets all the files in a directory in a dynamic array) and provides a deallocator:
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
/* returns a NULL terminated array of files in directory */
char **getfilelist(const char *dirname) {
DIR *dp;
char **entries = NULL;
struct dirent *entry;
off_t index = 0;
dp = opendir(dirname);
if (!dp) {
perror(dirname);
return NULL;
}
while((entry = readdir(dp)) != NULL) {
/* increase entries array size by one pointer-size */
entries = realloc(entries, (1+index)*sizeof(char *));
/* strdup returns a newly allocated duplicate of a string that
* you must free yourself
*/
entries[index] = strdup(entry->d_name);
index++;
}
/* need one more entry for NULL termination */
entries = realloc(entries, (1+index)*sizeof(char *));
entries[index] = NULL;
return entries;
}
void putfilelist(char **entries) {
char **p;
if(!entries)
return;
for(p = entries; *p !=NULL ; p++) {
free(*p); /* free all the strdups */
}
free(entries); /* free the array of pointers */
}
An example of how you could use it:
int main(int argc, char **argv) {
char **p, **entries;
char *dir = (argc == 1 ? "." : argv[1]);
entries = getfilelist(dir);
for (p = entries; p && *p; p++) {
printf("%s\n", *p);
}
putfilelist(entries);
}
Updated solution,
In order to implement your solution without using any of the library code, and keep it to system calls, here is an example that implements everything that the above example does, without relying on higher-level library calls.
Note
The bottom functions are the same as the above example.
/*
* A partially low-level implementation using a direct system calls
* and internal memory management
*
* This is an opinionated (linux only) implementation of OP
*
* linux headers are only included for some of the constants and
* where it would be trivial to re-implement system calls (e.g. open, write etc.)
* which I am too lazy to do in this example.
*
*/
#define NDEBUG
#include <stdarg.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <linux/fcntl.h>
#include <linux/unistd.h>
/* replace this macro with your own error handling messages */
#define handle_error(msg) \
do { my_fdputs(2, msg); my_exit(-127); } while (0)
#if !defined(NDEBUG)
#define assert(x) do { int __rv = (x); if(!__rv) { my_fdprintf(2, "assertion failed %s\n", #x); my_exit(__rv); } } while(0)
#else
#define assert(x) do {} while(0)
#endif
/* returns a NULL terminated array of files in directory */
/* low-level list builder using getdents */
void my_exit(int x)
{
syscall(SYS_exit_group, x);
}
/* a trivial malloc / realloc / free */
/* simple linked list memory accounting */
struct memblock {
struct memblock *next;
size_t size;
int free; /* flag */
int magic; /* canary value for debugging */
};
#define METASIZE sizeof(struct memblock)
void *global_base = NULL;
struct memblock *find_free_block(struct memblock **last, size_t size)
{
struct memblock *current = global_base;
while(current && !(current->free && current->size >= size)) {
*last = current;
current = current->next;
}
return current;
}
/*
* instead of using sbrk, we should really use mmap on newer
* linux kernels and do better accounting, however this is a
* simple example to get you started
*/
struct memblock *request_space(struct memblock *last, size_t size)
{
struct memblock *block;
void *request;
block = sbrk(0); /* get current program break */
request = sbrk(size + METASIZE);
assert((void *)block == request);
if(request == (void *)-1) {
return NULL;
}
if(last) {
last->next = block;
}
block->size = size;
block->next = NULL;
block->free = 0;
block->magic = 0x12345678;
return block;
}
struct memblock *get_memblock_ptr(void *ptr)
{
return (struct memblock *)ptr - 1;
}
/* a simple memcpy, can be optimized by taking alignment into account */
void *my_memcpy(void *dst, void *src, size_t len)
{
size_t i;
char *d = dst;
const char *s = src;
struct memblock *bd, *bs;
bd = get_memblock_ptr(dst);
for(i = 0; i < len; i++) {
d[i] = s[i];
}
return dst;
}
/* now to implement malloc */
void *my_malloc(size_t size)
{
struct memblock *block;
if(size == 0)
return NULL;
if(!global_base) {
block = request_space(NULL, size);
if(!block)
return NULL;
global_base = block;
}
else {
struct memblock *last = global_base;
block = find_free_block(&last, size);
if(!block) {
block = request_space(last, size);
if(!block) {
return NULL;
}
}
else {
block->free = 0;
block->magic = 0x77777777;
}
}
return (block+1);
}
void my_free(void *ptr)
{
struct memblock *block;
if (!ptr)
return;
block = get_memblock_ptr(ptr);
assert(block->free == 0);
assert(block->magic == 0x77777777 || block->magic == 0x12345678);
block->free = 1;
block->magic = 0x55555555;
}
void *my_realloc(void *ptr, size_t size)
{
struct memblock *block;
void *newptr;
if(!ptr)
return my_malloc(size);
block = get_memblock_ptr(ptr);
if(block->size >= size)
return ptr;
newptr = my_malloc(size);
if(!newptr) {
return NULL;
}
my_memcpy(newptr, ptr, block->size);
my_free(ptr);
return newptr;
}
/* trivial string functions */
size_t my_strlen(const char *src) {
size_t len = 0;
while(src[len])
len++;
return len;
}
char *my_strdup(const char *src)
{
char *dst;
char *p;
size_t len = 0, i;
len = my_strlen(src);
dst = my_malloc(1+len);
if(!dst)
return NULL;
for(i = 0; i < len; i++) {
dst[i] = src[i];
}
dst[i] = 0;
return dst;
}
/* trivial output functions */
my_fdputs(int fd, const char *str)
{
return write(fd, str, my_strlen(str));
}
int my_fdputc(int fd, char c)
{
return write(fd, &c, sizeof(char));
}
/* a very limited implementation of printf */
int my_fdvprintf(int fd, const char *fmt, va_list ap)
{
const char *p;
int count = 0;
for(p = fmt; p && *p; p++ ) {
if(*p == '%') {
p++;
switch(*p) {
case 's':
count += my_fdputs(fd, va_arg(ap, char *));
break;
case '%':
count += my_fdputc(fd, '%');
break;
default:
#ifndef NDEBUG
my_fdputs(2, "warning: unimplemented printf format specifier %");
my_fdputc(2, *p);
my_fdputc(2, '\n');
#endif
break;
}
}
else {
my_fdputc(fd, *p);
}
}
return count;
}
int my_fdprintf(int fd, const char *fmt, ...)
{
int rv;
va_list ap;
va_start(ap, fmt);
rv = my_fdvprintf(fd, fmt, ap);
va_end(ap);
return rv;
}
/* wrapper to linux getdents directory entry call */
/* linux dirent structure */
struct linux_dirent {
long d_ino;
off_t d_off;
unsigned short d_reclen;
char d_name[];
};
/* system call wrapper */
int getdents(int fd, void *buf, size_t bufsize)
{
return syscall(SYS_getdents, fd, buf, bufsize);
}
/* reimplement getfilelist using our getdents */
#define BUF_SIZE 1024
char **getfilelist(const char *dirname) {
int fd, nread;
char **entries = NULL;
off_t index = 0;
char buf[BUF_SIZE];
struct linux_dirent *d;
int bpos;
/* O_DIRECTORY since kernel 2.1 */
fd = open(dirname, O_DIRECTORY|O_RDONLY);
if (fd < 0) {
handle_error(dirname);
}
for(;;) {
nread = getdents(fd, buf, BUF_SIZE);
if (nread == -1)
handle_error("getdents");
if (nread == 0)
break;
for (bpos = 0; bpos < nread;) {
d = (struct linux_dirent *) (buf + bpos);
entries = my_realloc(entries, (1+index) * sizeof(char *));
entries[index++] = my_strdup(d->d_name);
bpos += d->d_reclen;
}
}
/* need one more entry for NULL termination */
entries = my_realloc(entries, (1+index)*sizeof(char *));
entries[index] = NULL;
close(fd);
return entries;
}
void putfilelist(char **entries) {
char **p;
if(!entries)
return;
for(p = entries; *p !=NULL ; p++) {
my_free(*p); /* free all the strdups */
}
my_free(entries); /* free the array of pointers */
}
int main(int argc, char **argv) {
char **p, **entries;
char *dir = (argc == 1 ? "." : argv[1]);
entries = getfilelist(dir);
for (p = entries; p && *p; p++) {
my_fdprintf(1, "%s\n", *p);
}
putfilelist(entries);
}
Hope you enjoy
How do I work with msgpack_pack_raw and msgpack_pack_raw_body to send an unsigned char array to more importantly, how to retrieve (unpack) it?
What I have done is as follows:
msgpack_sbuffer* buffer = msgpack_sbuffer_new();
msgpack_packer* pk = msgpack_packer_new(buffer, msgpack_sbuffer_write);
msgpack_sbuffer_clear(buffer);
msgpack_pack_array(pk, 10);
unsigned char a[10] = "0123456789";
msgpack_pack_raw(pk, 10);
msgpack_pack_raw_body(pk,a,10);
and in the receiver part I have:
msgpack_unpacked msg;
msgpack_unpacked_init(&msg);
msgpack_unpack_next(&msg, buffer->data, buffer->size, NULL);
msgpack_object obj = msg.data;
msgpack_object* p = obj.via.array.ptr;
int length = (*p).via.raw.size;
IDPRINT(length);
unsigned char* b = (unsigned char*) malloc(length);
memcpy(b,(*p).via.raw.ptr,length);
But it throws seg fault when executing "int length = (*p).via.raw.size;".
Any idea why?
Any idea why?
This is because msgpack_pack_array(pk, 10); is not required here, since you pack your data as a raw buffer of a given size. In other words msgpack_pack_raw and msgpack_pack_raw_body are sufficient.
At unpack time, you must access its fields as follow:
length: obj.via.raw.size
data: obj.via.raw.ptr
see: msgpack_object_raw in object.h.
Here's a recap of how to proceed:
#include <stdio.h>
#include <assert.h>
#include <msgpack.h>
int main(void) {
unsigned char a[10] = "0123456789";
char *buf = NULL;
int size;
/* -- PACK -- */
msgpack_sbuffer sbuf;
msgpack_sbuffer_init(&sbuf);
msgpack_packer pck;
msgpack_packer_init(&pck, &sbuf, msgpack_sbuffer_write);
msgpack_pack_raw(&pck, 10);
msgpack_pack_raw_body(&pck, a, 10);
size = sbuf.size;
buf = malloc(sbuf.size);
memcpy(buf, sbuf.data, sbuf.size);
msgpack_sbuffer_destroy(&sbuf);
/* -- UNPACK -- */
unsigned char *b = NULL;
int bsize = -1;
msgpack_unpacked msg;
msgpack_unpacked_init(&msg);
if (msgpack_unpack_next(&msg, buf, size, NULL)) {
msgpack_object root = msg.data;
if (root.type == MSGPACK_OBJECT_RAW) {
bsize = root.via.raw.size;
b = malloc(bsize);
memcpy(b, root.via.raw.ptr, bsize);
}
}
/* -- CHECK -- */
assert(bsize == 10);
assert(b != NULL);
for (int i = 0; i < bsize; i++)
assert(b[i] == a[i]);
printf("ok\n");
free(buf);
free(b);
return 0;
}