Build msgpack for micropython

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
bokolob
Posts: 11
Joined: Tue Mar 09, 2021 7:03 pm

Build msgpack for micropython

Post by bokolob » Wed Mar 10, 2021 4:59 pm

Hello.

I'm a newbie in python and especially micropython worlds, so I need some help :) Could somebody give me a clue how I could build msgpack (https://github.com/msgpack/msgpack-python). Maybe somebody had done it already. Or maybe there is an instruction (not about this library but general one)?

I know, there is a pure-python library, but I want to try binary one.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Build msgpack for micropython

Post by jimmo » Thu Mar 11, 2021 5:01 am

bokolob wrote:
Wed Mar 10, 2021 4:59 pm
I'm a newbie in python and especially micropython worlds, so I need some help Could somebody give me a clue how I could build msgpack (https://github.com/msgpack/msgpack-python). Maybe somebody had done it already. Or maybe there is an instruction (not about this library but general one)?
Unfortunately this is not a simple process as the way CPython bindings work is very different to MicroPython bindings.

The starting point for extending MicroPython in C would be http://docs.micropython.org/en/latest/d ... dules.html

bokolob
Posts: 11
Joined: Tue Mar 09, 2021 7:03 pm

Re: Build msgpack for micropython

Post by bokolob » Thu Mar 11, 2021 5:11 am

Thanks.
Is there some examples of iterating through an object content in C ? How to walk trough all the fields recursively ?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Build msgpack for micropython

Post by jimmo » Thu Mar 11, 2021 5:51 am

bokolob wrote:
Thu Mar 11, 2021 5:11 am
Is there some examples of iterating through an object content in C ? How to walk trough all the fields recursively ?
I wish I could just say "look at how the JSON serialiser works" -- https://github.com/micropython/micropyt ... modujson.c

Unfortunately, it's a bit complicated because the JSON serialiser is part of the core... you'll see the key line in modujson.c is:

Code: Select all

mp_obj_print_helper(&print, obj, PRINT_JSON);
But you can follow the implementation of print and the various places that PRINT_JSON is used and get some hints.

However, in general the way to walk an object is via iterable:

Code: Select all

mp_obj_t iter = mp_getiter(iterable, NULL);
mp_obj_t item;
while ((item = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
    // do something with item
}

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Build msgpack for micropython

Post by pythoncoder » Thu Mar 11, 2021 8:22 am

I appreciate this doesn't answer your question, but this doc describes the four serialisation protocols which do work with MicroPython. Alas msgpack is not one of them.
Peter Hinch
Index to my micropython libraries.

bokolob
Posts: 11
Joined: Tue Mar 09, 2021 7:03 pm

Re: Build msgpack for micropython

Post by bokolob » Thu Mar 11, 2021 10:48 am

I need some serialiser that is efficient for the cpu (to consume less battery) and the result "size" (to consume less network). About that four serializers - json is fast, but its result is longer, other libraries are pure python, and I suppose that they are quite slow..

bokolob
Posts: 11
Joined: Tue Mar 09, 2021 7:03 pm

Re: Build msgpack for micropython

Post by bokolob » Thu Mar 11, 2021 11:23 am

Isn't it a good ide to add other serialisers into core? At the same level as json is? They can be disabled with preprocessor directives.
Msgpack has C-library that could be used.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Build msgpack for micropython

Post by stijn » Thu Mar 11, 2021 1:47 pm

See https://github.com/micropython/micropython/issues/4241
In short: the fallback implementation of msgpack is pure Python and almost works out of the box

Code: Select all

Isn't it a good ide to add other serialisers into core? 
There are just too many of them to choose. Also see e.g. https://github.com/micropython/micropython/issues/5597

bokolob
Posts: 11
Joined: Tue Mar 09, 2021 7:03 pm

Re: Build msgpack for micropython

Post by bokolob » Thu Mar 11, 2021 2:15 pm

I know about that library, but I suppose it would be too slow and inefficient for IO-heavy applications (on microcontroller). And in my case every uA of battery is crucial.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Build msgpack for micropython

Post by stijn » Fri Mar 12, 2021 9:08 am

bokolob wrote:
Thu Mar 11, 2021 2:15 pm
I suppose it would be too slow.
That is understandable, but unless you actually measure it you don't know for sure.

Post Reply