japaric-archived / photon-hal

Low level Rust bindings to particle's HAL (Hardware Abstraction Layer)
Apache License 2.0
9 stars 8 forks source link

Support TCP connections #17

Open dbrgn opened 7 years ago

dbrgn commented 7 years ago

I started porting the TCPClient, but kind of got stuck in the complexity of the issue.

Is this the correct direction / approach?

japaric commented 7 years ago

Where and how did you get stuck?

I personally would start by directly calling the socket API until I can stablish a connection to some server while verifying that all the socket calls don't return errors and figuring out what arguments make sense to use with the socket API. Then I would continue to do some basic I/O through the socket. Only after I have understood how the socket API works would I try to create a Rust version of the TCPClient API. But just putting up a minimal connection example that directly uses the socket API would be a great start!

dbrgn commented 7 years ago

This is their library: https://github.com/spark/firmware/blob/develop/wiring/src/spark_wiring_tcpclient.cpp I wanted to port it to Rust (as directly as possible for now, without making it idiomatic).

That code looks pretty straightforward, the tricky part (for me) is the network / wifi stuff: https://github.com/spark/firmware/blob/develop/wiring/src/spark_wiring_wifi.cpp I can't find the place anymore, but somehow I found a rabbit hole that led to calls to the WICED library, which is pretty big. Maybe bindings to that could be generated with bindgen?

japaric commented 7 years ago

You could try to directly bind to their C++ firmware library for the TCPClient stuff. Bindgen supports C++ libraries but I haven't used it like that before so I can't provide further advice on that.

dbrgn commented 7 years ago

Ok, initial attempt with current bindgen:

$ cd particle-firmware
$ bindgen --use-core --ctypes-prefix ::ctypes \
    firmware/wiring/inc/spark_wiring_tcpclient.h -- \
    -I firmware/system/inc/ \
    -I firmware/hal/inc/ \
    -I firmware/services/inc/ \
    -I firmware/hal/shared/ \
    -x c++ -std c++14
thread 'main' panicked at 'TranslationUnit::parse failed',
/checkout/src/libcore/option.rs:794
note: Run with `RUST_BACKTRACE=1` for a backtrace.

Any idea?

japaric commented 7 years ago

No idea. The error message is not particularly helpful. The documentation says that you will likely need to whitelist some std stuff to make this work but doesn't suggest what should be whitelisted (everything's whitelisted by default). You'll probably have more luck asking around on IRC or looking for a project that creates C++ bindings to see how it does it.