Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [paho-dev] PAHO Embedded C library Transport Layer

Hi Veysel,

the way MQTT packets are read is dictated by the format of the headers. 

There is a one byte field which contains the packet type (CONNECT, PUBLISH, etc), then there are between 1 and 4 bytes which contain the length of the rest of the packet, followed by the rest of the packet.

So, the reading process will generally be:

read 1 byte (the header)
read 1 to 4 bytes, in 1 byte chunks,
optionally, read the rest of the packet (some packets have no more data).

The recv function has to be able to handle that sequence of calls, and should return the number of bytes read successfully.  Returning -1 indicates an error.

Does that help?

Ian


On 12/30/2014 12:10 AM, Veysel KARADAG wrote:
Hi Every one ,

I'm trying to implement PAHO embedded C library to custom device ( STM32F405/GPRS module )


My main problem is create custom recv() function , which will use source for buffer.


In PAHO library example, there is a transport_getdata() function. It handle transportation layer. This is tranport_getdata() function.This example uses socket library from which I don't have :(

int transport_getdata(unsigned char<
span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.7142858505249px; vertical-align: baseline; background: transparent;">* buf, int count)
{
    int rc = recv(mysock, buf, count, 0);
    //printf("received %d bytes count %d\n", rc, (int)count);
    return rc;
}

To port library to my project, I need to write my own recv() function like my_recv().

Standartd socket Recv() function description said,

Recv() will return length of message written to buffer from source.

So I try to write my_recv() for replace recv() , ( I don't need socket ID and FLAG)

int  my_recv(char *buf, int len){

    int i=0;

    if (len <= 0 )
        return -1;
    if (len > BUF_MAX)
        len=BUF_MAX - 2;

    for(i=0;i<len;i++){

        *buf=serial_buffer[0];
        *buf++;
        //shift one byte buffer to left
        memmove(serial_buffer, serial_buffer+1
,(BUF_MAX-1)*sizeof(*serial_buffer));
        interrupt_pointer--; // After one byte shift, reduce serial interrupt pointer.
    }
    return i;
}

Handling TCP incoming data, I have a serial/TCP transparent connection over GPRS module.
I used interrupt to read every bytes . All incoming bytes written on serial_buffer.

void USART1_IRQHandler(void){

    int byte;

    if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) // Received characters modify string
    {
            byte= USART_ReceiveData(USART1);

    }

    serial_buffer[interrupt_pointer]=byte;
    interrupt_pointer++;
    if(interrupt_pointer==BUF_MAX){
        interrupt_pointer=0;
        memset(serial_buffer,0,BUF_MAX<
span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13.7142858505249px; vertical-align: baseline; background: transparent;">);
    }

}

For simple test, I send "1234567890" from over TCP socket,

simple test code is

while(strlen(serial_buffer)>0){

    delay_ms(1000);
    printf("%s\r\n",serial_buffer);
    my_recv(buf,3);
    printf("%s\r\n",buf);
    printf("%s\r\n",serial_buffer);
}

My system out is,

1234567890 // All TCP data received
123        // getting 3 bytes from buffer
4567890    // remaining bytes

I'm not sure I understand recv() function well. Is this output is alright.

Couse still I cant handle MQTT package with PAHO library



_______________________________________________
paho-dev mailing list
paho-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/paho-dev

-- 
Ian Craggs                          
icraggs@xxxxxxxxxx                 IBM United Kingdom
Paho Project Lead; Committer on Mosquitto


Back to the top