简体   繁体   中英

Copying char* values through Queues and threads on MBED OS

I am trying to implement some RTOS threads on Arm MBED OS over a K64F board. I am parting from the RTOS examples and I have succesfully run and communicated different threads using Queues. I am having problems when copying char* values from one struct to another to get a message from one queue to another. I believe I am misunderstanding something and that my problem is related to pointers and memory handling but I am not able to get through it.

I have defined diferent queues to send data to various threads. I have also created a basic data structure containing everything I need to go among these threads. In this struct I have a char* variable (rHostAddr) containing the remote host address that requested a service.

MemoryPool<cMsg, 16> AMPool;
Queue<cMsg, 16> AMQueue;
MemoryPool<cMsg, 16> ioLedPool;
Queue<cMsg, 16> ioLedQueue;

typedef struct{
    ...

    char* rHostAddr;
    ...
} cMsg;

In the Main Thread I am creating this data structure and putting it in the first queue (AMQueue).

--- Main Thread ---
cMsg *message = AMPool.alloc();

char* rcvaddrs = "111.111.111.111";
message->rHostAddr = "111.111.111.111";

rcvaddrs = (char*)addr.get_ip_address();
message->rHostAddr = rcvaddrs;

AMQueue.put(message);

On the Thread 1 I wait for a message to arrive and on certain conditions I copy the whole structure to a new one created from the corresponding pool and insert it on a new queue (ioLedQueue).

--- Thread 1 ---

cMsg *msg;
cMsg *ledm = ioLedPool.alloc();
osEvent evt = AMQueue.get();
msg = (cMsg*)evt.value.p;

msg.rHostAddr = ledm.rHostAddr;
printf("\t -- Host 1 -- %s\n\r", ledm->rHostAddr);
ioLedQueue.put(ledm);

On the Thread 2 I get the message structure and the data .

--- Thread 2 ---

cMsg *msg;
osEvent evt = ioLedQueue.get();
msg = (cMsg*)evt.value.p;
printf("\t -- Host 2  -- %s\n\r", msg->rHostAddr);

On this stage rHostAddr is empty. I can see the value on the printf "Host 1" but not in the "Host 2"

I believe (if I am not wrong) that the problem comes from assigning with = operand, as I am copying the address, not the value, and it is lost when first pool memory is freed. I have tried copying the value with memcpy, strcpy and even my own char by char but system hangs when calling this methods.

How can I copy the value through this queues?

I move it here as the correct answer was written as a comment. Converting the value to a array of chars was the way to go, so the string data is part of the struct.

char rHostAddr[40];

Now the assignation can be done with srtcpy method and it is passed through all the process correctly:

char* rcvaddrs = (char*)addr.get_ip_address();

strcpy(message->rHostAddr,rcvaddrs);

Take a look at this solution from ARM mbed:

https://github.com/ARMmbed/mbed-events

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM