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 Ian ,

I think my problem is creating robust and stable  my_recv() function same as recv()  in "<sys/socket.h>"

Transport layer is not part of PAHO library. Its based on hw or developers source. 

Thanks your reply it's help me to understand MQTT package content. Happy new year to you and everyone ..

Also I want to share more information about my works, any suggestions will help. 

Triying to connecting broker . (I print out first  32 byte of buffers with HEX format. ) 
 
MQTT Connection Package to BROKER. ( char buf[512] )
10 0E 00 04 4D 51 54 54 04 02 00 14 00 02 6D 65 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

MQTT CONNACK Package from BROKER. ( char serial_buf[512] )
20 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
 
20  CONNACK Header
02  Renaming lenght
00  Reserved Value
00  Connection Accept.

Everything looks fine, 

Next step is ,trying  to read MQTT package from serial buffer for PAHO library.

I'm using  MQTTPacket_read() function, 

MQTTPacket_read()  calls   transport_getdata()  and transport_getdata() calls my_recv() function.
MQTTPacket_read()  calls  multiple times  transport_getdata() function.

Serial buffer, before call MQTTPacket_read() 
20 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Mqtt buffer, before call MQTTPacket_read()
10 0E 00 04 4D 51 54 54 04 02 00 14 00 02 6D 65 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

Serial buffer, after my_recv(buf,1) ,
02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Mqtt Buffer, after my_recv(buf,1) ,
20 0E 00 04 4D 51 54 54 04 02 00 14 00 02 6D 65 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

I have doubts about my_recv() function ,
Should I need to add one more byte"EoF" MQTT buffer. After moved n bytes from serial buffer. I can add 0x00. 
Maybe I have clear MQTT buffer before all read package? It will be same as move 0x00 end of package.

Sometime I got package error, Sometimes I read package. 

I'm  digging code with debug  and finding  problem. 

Best regards, 

Date: Tue, 30 Dec 2014 17:53:09 +0000
From: icraggs@xxxxxxxxxxxxxxxxxxxxxxx
To: paho-dev@xxxxxxxxxxx
Subject: 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


_______________________________________________ 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

Back to the top