Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [paho-dev] Design question - how to best approach background tasks for the embedded clients

Thanks for the info, Frank. I think I have a good idea where to go from here.

Ian

On 05/26/2015 02:17 PM, Frank Pagliughi wrote:
Ian,

For MQTT you would need an RTOS that is "big" enough to support a TCP/IP stack. And if you have that, I think it would be safe to assume that you can use a dedicated thread for the MQTT client. Actually, having threads available for communications tasks is often a primary reason for using an RTOS in the first place.

What you would want to do is make it easy to change/specify the priority that the MQTT thread uses, since each application would want to tweak that value to have the library play nicely with the other threads. And, no matter the RTOS, you want to be frugal with the use of stack space, and have a pretty good idea of how much the library is using on a worst-case basis. Most RTOS' require you to specify the exact amount of stack for each thread, and overrunning that amount is a common, and painful problem on a little board.

Frank

On 05/26/2015 06:29 AM, Ian Craggs wrote:
Hi Richard,

thanks for your reply.

You are correct, yield() would not be needed if a background task were used. I would like to keep the option of not using a background task in case there are operating systems where this has some advantages. But for FreeRTOS, a background task seems the natural approach.

I'll start adding the background task code and see how much complication results (it may be less than I feared). This is the way that the full Posix/Windows Paho C client works in any case.

Ian



On 05/25/2015 05:50 PM, Richard Barry wrote:
Hi Ian,

For ease of use and maintainability reasons it would, in my humble opinion, be best to not only hide the cycle() function, but also the yield() function. That would mean, if yield were called at all, it would be inside the public API functions.

I am still not completely clear as to the purpose of yield(), but suspect if a separate MQTT thread were allowed then yield() would not be required. A background thread would free up processing time by being event driven (important in low power systems that want to sleep), so improve run time resource usage. If it is considered that there is not enough RAM resource for the background thread then consider how much RAM could be saved in each thread using MQTT if the RAM were instead spent in a single MQTT thread. If it were still a problem then there is already a TCP/IP thread, so maybe that could be enhanced to allow user defined extensions - then run the MQTT stuff in there [that last suggestion would not be a quick change!].

I would envisage an application thread using the MQTT thread as a sort of proxy server. The application thread just sends data via the MQTT thread - all the ins and outs of how the data is sent, and QoS level maintained, would be entirely inside the MQTT thread itself. The application thread can sleep until notified by the MQTT thread as to the the success or failure of the send. Likewise when receiving data, the receive can be handled inside the MQTT thread, and then a callback function (or queue, or whatever) used to send received data to application threads that need it.

If you can we suggest one send or receive scenario to concentrate on, and describe the sequence used to implement the scenario that involves calls to yield(), then perhaps I could follow up with a suggestion of how it could possibly be implemented without the yield().

Regards,
Richard.

+ http://www.FreeRTOS.org
Designed for simplicity. More than 113000 downloads in 2014.

+ http://www.FreeRTOS.org/plus
IoT, Trace, Certification, FAT FS, TCP/IP, Training, and more...



On 22/05/2015 12:36, Ian Craggs wrote:
Hello all,

as I've been updating the C "high level" embedded client for FreeRTOS:

(http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt.embedded-c.git/commit/?h=develop)


I've been pondering how to best have background tasks completed.   I
originally envisaged two styles of APIs, similar to the standard Paho C
client libraries, synchronous and asynchronous.  I haven't yet created
the asynchronous version, but that would use a background thread, at the
cost of using more resources.

The "synchronous" or blocking API would be easy to use, and all
processing would take place on the application's thread. Because no
separate threads were involved, this would both simplify implementation and reduce resource usage. To allow messages to be received, there is a
yield() call, which takes as its parameter a time to run. The API also
has a a function (or method) called cycle(), which processes one
incoming MQTT packet.  Yield() just calls cycle() under the covers.  I
didn't originally intend to make cycle() a public function.

The problem with yield() is that it might spend most of its time doing
nothing. An MQTT packet might arrive at the end of the timeout period, and not get processed until the next call to yield(). One solution is
to make cycle() a public function and allow the application to
interleave application with MQTT processing at a fine-grained level.

For FreeRTOS I was considering also a background task to be started. But
this would still have the negative effects of increased resource usage
and complexity as synchronization of the common data structures would be
needed.  So I'm reconsidering this once more, and thinking of leaving
the background thread to an (almost) totally non-blocking API.

I had also been thinking of changing the semantics of yield() in some
way to be more event driven.  But I don't know whether this would buy
any advantages over making cycle() a public function.

Thoughts?

_______________________________________________
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


_______________________________________________
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