micropython-lib icon indicating copy to clipboard operation
micropython-lib copied to clipboard

umqtt.simple using ssl: check_msg() throws NotImplementedError

Open stlehmann opened this issue 8 years ago • 9 comments

The check_msg() function uses socket.setblocking() to check for pending messages without blocking. While working fine with normal sockets this leads to an NotImplementedError when used with a ssl connection.

To create a ssl connection the ussl.wrap_socket() function is used which returns a ussl._SSLSocket object. For the _SSLSocket class there exists only a method stub for setblocking() that throws a NotImplementedError. So basically the _SSLSocket.setblocking() method needs to be implemented in the ussl library for this to work.

stlehmann avatar Apr 21 '17 09:04 stlehmann

So basically the _SSLSocket.setblocking() method needs to be implemented in the ussl library for this to work.

No, of course no. You're staying in you car ahead of big-big pit on the street with a warning sign in front of it. And now you're saying that to pass the street you need to remove this warning sign.

pfalcon avatar Apr 21 '17 10:04 pfalcon

Ok, I might have missed something here but can't see what. What is the sign warning me from?

stlehmann avatar Apr 21 '17 11:04 stlehmann

That non-blocking I/O support for SSL sockets is not implemented, and it's pretty complex thing ("big-big pit"), or otherwise it would have been implemented long ago.

pfalcon avatar Apr 21 '17 11:04 pfalcon

I see...so this is a more complex topic... But is there a workaround? I need to send regular ping requests to the mosquitto server in order to keep the connection alive. I can't do that with a blocking socket. At the moment all I can do is drop the ssl encryption and use a normal socket.

stlehmann avatar Apr 21 '17 11:04 stlehmann

MrLeeh could you fork a new thread and send pings? I'm very new at this so apologies if that sounds like an ignorant question, but it seems to me that would work.

SlowBro904 avatar Jul 24 '17 01:07 SlowBro904

@SlowBro904 with full-grown Python this would be possible I guess. But afaik there is no threading implemented in Micropython, yet.

stlehmann avatar Jul 24 '17 05:07 stlehmann

Threading is usable in Pycom's uPy, and I have successfully flashed that to a standard WROOM-32. Theirs is open source.

(It's not completely usable in that state, one must also alter the pins in a csv file and recompile, but I wanted to see it done as a proof of concept. Also their threading is not complete, with no way to exit a thread from the outside. I hacked a workaround using a global variable as a flag and it works.)

SlowBro904 avatar Jul 24 '17 09:07 SlowBro904

Oh, and I found this. You /might/ have threading in your implementation after all. Go try it. https://forum.micropython.org/viewtopic.php?t=1864

SlowBro904 avatar Jul 24 '17 09:07 SlowBro904

As a workaround, check_msg might be modified to use polling:

    def check_msg(self, timeout):
        poller = uselect.poll()
        poller.register(self.sock, uselect.POLLIN)
        res = poller.poll(timeout)  # time in milliseconds
        if not res:
            return None
        return self.wait_msg()

chtinnes avatar Oct 22 '19 20:10 chtinnes

mqtt.simple should work with SSL for many years now. If there are issues, please re-open with details on how to reproduce.

jonnor avatar Aug 25 '24 12:08 jonnor