Couchbase Lite on ESP32

I’m interested in running Couchbase Lite on an ESP32 device. I found some commits that look like Fleece can be compiled to run on the ESP32, but no evidence that Couchbase Lite can be run on there. I’m hoping it’s possible, especially now with the stable C API.

The problem you run into with these kinds of things is the lack of an operating system and sometimes even the lack of a filesystem and/or network capabilities. The implementation of Couchbase Lite assumes underneath that there is a libc and c++ runtime capable of supporting the operations it needs. If that’s not there then it won’t compile and I don’t know what we are supposed to do as a database if we have no filesystem other than perhaps implement an in-memory database, which is not likely to go over well on a device with low memory. I’ll ping the person who actually did the ESP32 investigation though.

I’ve done some work with ESP32. It’s a nice platform, with a fairly complete C++ std library, but getting CBL to run on it would take a lot of work. Off the top of my head:

  • We use SQLite, which is generally too big for embedded systems. I’m not aware of anyone getting it running on ESP32, although it might be possible with the newer SoCs that support more RAM and flash.
  • The networking code in CBL-C uses POSIX socket APIs, which I don’t think ESP-IDF implements(?)
  • Likewise, it uses mbedTLS; I don’t know if that’s supported on ESP32.

Those aren’t insurmountable problems, but I suspect most of the work would be dealing with large numbers of small compatibility problems that you always run into when porting a big C/C++ codebase to a new platform.

I found a repo building SQLite for ESP: GitHub - siara-cc/esp32_arduino_sqlite3_lib: Sqlite3 Arduino library for ESP32

It looks like std::filesystem is not supported on ESP32, but I guess it’s not needed if we can leverage SQLite.

Regarding sockets, it looks like they do support POSIX (BSD) sockets: lwIP - ESP32 - — ESP-IDF Programming Guide v5.2.3 documentation

And they also have a version of Mbed TLS: Mbed TLS - ESP32 - — ESP-IDF Programming Guide v5.2.3 documentation

The hard part is not just using mbedtls, but combining the fork we have with the fork they have. That’s probably where a lot of the hard stuff will come in.

Hm, the SQLite repo is for the Arduino SDK, which is kind of the “easy” or “compatibility” mode on ESP32. The real SDK is ESP-IDF, and I think any port of LiteCore would need to use that. It would probably not be too hard to adapt that SQLite code to use IDF.