FTLab Wireless Smart Geiger — Part 6 — Implementing a client

Previous posts:

With all those findings, it’s time to start an implementation of a Linux client for the FTLab Wireless Smart Geiger.

Implementation #

At work, I’m mostly using Python, so I started with a spike in Python. But I also added a Rust branch to compare.

The device on which I plan to run this client is already swapping because it’s short on RAM. And Python uses much more memory than Rust, so I decided to keep the Rust implementation. Also, type safety and avoiding primitive obsession (more on that below) are easier in Rust. Rust being generally faster was not a criteria here, since most of the time will be spent in the Bluetooth communication.

This is my first really vibe-coded application. It’s written in Rust, which I have a very basic knowledge of, so I can’t really verify what the AI generated by reading the code.

So how can I ensure the produced code is somewhat correct? Well, that’s where decades of software engineering come in. So let’s use some of that.

Superpowers #

Let’s not reinvent the wheel. There are plugins and skills for AI agents that already bring some software engineering practices. One of those tools is “superpowers”.

It implements a base workflow like this:

Note that the various harnesses don’t follow it the same way. From my experience, OpenCode with superpowers applies it almost systematically… sometimes it wants to brainstorm for a change that will touch one line. On the other hand, Claude Code tends to use superpowers’ skills much less often and needs to be reminded to do it.

Behavior-driven development #

Behavior-driven development (BDD) is a practice aiming to ensure that the software meets the expected behavior. The idea is to have the behavior described in natural language, usually in a domain-specific language (DSL). Typically, the feature is explained with a sentence like “As an X, I want Y so that Z.” and the acceptance criteria are described with scenarios. Those scenarios being in the form “Given X, When Y, Then Z.”

The next step is to have tools so those scenarios can be automated. The most well-known tools are Gherkin, which is a language for writing human-readable specifications and Cucumber, which runs automated tests based on Gherkin scenarios.

I like this approach, but I always find it hard to make it readable enough for the non-technical stakeholders. So I wanted to try something else.

What I would like is to be able to generate a sort of user manual for the tool. And at the same time, explain how the device works. That’s where Varar comes in.

Varar is an open-source tool to make the specifications executable. It has been created by the same person who created Cucumber. It’s a somewhat looser version which seems better suited to what I want to do. Note that it’s also stricter, in a different way, by having a lock file, so any change in the specification can be detected. And it has been written so it can also be used with AI agents.

I have seen many applications for which you could not say what they were doing and more importantly, why they were doing what they were doing. So many applications have specifications, but they don’t match what was implemented; and/or the specification stayed the same while the application evolved. BDD helps to address those issues.

And when you have that, AI agents can read the specifications and “understand” the purpose of the application. Preventing them from going too far off track.

Testing #

So we have executable specifications… but to execute what? I think the canonical way to use it is to run it against the “core” implementation of the application. Where the business logic lives.

But I want my specs to also be a manual for the users. It would be nice to have the console usage of the tool in it, so basically use the specs for end-to-end testing.

End-to-end and domain testing #

So I decided to have the same specifications document for both. Basically, I have the manual with the tool usage examples, like this:

1
2
3
4
5
6
$ bsg-001 read --address AA:BB:CC:DD:EE:FF
Status: measuring
Measuring time: 0 d 00:12:18
Average dose rate: 0.05 µSv/h
One-minute dose rate: 0.50 µSv/h (1 pulse)
Battery: 40%

Where the command (line 1) tells Varar what to execute, and the other lines are the expected output.

By default, those examples run against fake Bluetooth devices, so I don’t need a real device nearby to run the tests. It also makes it possible to test situations that would be difficult to reproduce with the real one.

Of course, if the fake device behaves differently from the real one, the tests can pass while the application is wrong. So there is also an option to run the examples that can be reproduced against a real BSG-001. Some things still need to be checked by hand, like whether a new name is kept after a power cycle.

And similarly, some tests run against the domain code. Which in this case is mostly about defining the various functions of the device and the associated command payload:

The tool takes the content of the reply to a query as shown.
The last column says why.

| query | reply                      | content              | why                                                                                                      |
| ----- | -------------------------- | -------------------- | -------------------------------------------------------------------------------------------------------- |
| 31    | 31 0b 04 41 42 43 44 07 01 | 04 41 42 43 44 07 01 | Everything after byte 1 is kept; the feature will read `04 41 42 43 44` and ignore the leftover `07 01`. |
| 31    | 31 00 04 41 42 43 44       | 04 41 42 43 44       | Byte 1 says `00`, but the content is there and is kept.                                                  |
| 31    | 21 0b 0a 01                | rejected             | Byte 0 is `21`, so this reply answers another query.                                                     |
| 31    | 31                         | rejected             | The reply stops before byte 1, so it is not a complete reply.                                            |
| 31    |                            | rejected             | The reply is empty.                                                                                      |

From an architectural point of view (more on that below), it can seem strange that the protocol is domain logic and not in an adapter. But given this project is about knowing the device’s protocol itself, and it’s code only (pure logic, no I/O), that’s fine here.

Unit testing #

So we defined the behavior of the application. Then we have unit tests.

And the idea is to write unit tests first, see them fail, then write code that makes them pass. And when that’s done, refactor the code to make it cleaner.

As seen before, this practice can be added through a skill. In this case, it’s brought for free by superpowers.

Architecture #

Hexagonal architecture #

The idea of hexagonal architecture is to keep the application logic independent of the outside world. The core defines what it needs through interfaces (the ports), and adapters implement those interfaces for the actual infrastructure.

Here, the application is split into 3 parts:

The dependencies go in one direction: adapters → application → domain. So the application doesn’t need to know about the Bluetooth library, and the domain doesn’t need to know about the command line.

And for the tests, the real Bluetooth adapter can be replaced by a fake. The same goes for the clock, so a test doesn’t depend on the time at which it runs.

Design principles #

There are also a few principles I want the code to follow. For example, KISS (Keep It Simple, Stupid): keep the design as simple as possible. And YAGNI (You Aren’t Gonna Need It): don’t add features or abstractions for needs that may never come.

Another one I like is avoiding primitive obsession. The idea is to use types that represent concepts of the domain instead of passing strings or numbers everywhere. For example, a device address has its own type, which can check that the address is valid.

But there is a balance to find here. Creating a type for every string or number doesn’t necessarily make the code simpler. So the project instructions ask the agent to use domain types when they have a purpose, not just for the sake of it.

Those principles can’t be checked automatically. They still need some judgment.

Tooling #

The previous steps are useful to guide the AI agent in what it generates. But then, we can use the same tools as we used before (when writing code by hand) to check some aspects of the quality.

Preferably, those tools should be fast, so we can apply the “shift left” principle, meaning that defects are detected as early as possible.

And those tools should be executed by the AI agent, so it can correct itself before handing over the code.

For this project, I have:

The Rust compiler also checks the code, and the project forbids unsafe Rust. That doesn’t mean the application can’t have bugs, but it removes some possibilities.

Checking the architecture #

Having an architecture is nice, but it’s also easy for a dependency to go in the wrong direction. Especially when an AI agent is adding code.

The layers are separate crates in a Cargo workspace. Each crate declares its dependencies, and the compiler won’t let it use a crate it doesn’t depend on.

Then cargo-deny checks those dependencies against an allow list: the domain doesn’t depend on any other crate, and the application only depends on the domain. Both are also no_std crates, so they can’t use the standard library to access things like the clock, files or the network.

That checks the dependency boundaries, not whether the code inside each layer is well-designed. For example, nothing prevents the agent from putting some protocol logic in the Bluetooth adapter instead of the domain, or from passing a plain string where a domain type would make sense. That part, like the design principles, still needs someone to review the code. Or that check can be part of the review done by the agent.

Running the checks #

mise installs the tools and provides tasks to run the checks. The tool versions and Rust dependencies are recorded in lockfiles, so the checks don’t silently pick up a different dependency version.

The agent instructions should tell the agent to run those checks in its development process. And if it doesn’t, pre-commit will.

pre-commit adds git hooks before a commit (for most of those checks), and some hooks before a push (for checks that don’t need to run as often, like the dependency audit).

Then the continuous integration (CI, implemented with GitHub Actions) runs the checks and the tests on every push. On the main branch, it also publishes the manual with the results of the executable examples, including failures.

There are also a few precautions for the dependencies themselves. Dependabot proposes updates for Rust dependencies and GitHub Actions, and new releases have a 3-day cooldown before being used. The actions are pinned to commits instead of just version tags. These are mostly to help with reproducibility and reduce the impact of supply chain attacks.

All of this doesn’t guarantee that the code does what I want. The specifications can be wrong, incomplete, or the tests can miss something. But at least I have more than the AI telling me that everything works.

Result #

The implementation code is available on GitHub.

And the manual (generated from Varar results) is published here.

It can discover devices, read the live measurement, check the logging state, download the records (as text or CSV), start and stop runs, and rename the device.

Memory #

So, was Rust worth it for the memory? I measured the peak memory (RSS) of each command with /usr/bin/time on my computer (not on the target device), against the Python spike:

CommandRustPython
Read the current measurement (3 runs)4.7–5.0 MB28.4–29.4 MB
Download the records (2 runs)4.9 MB29.1–29.2 MB
Scan for devices5.0 MB29.5 MB

So about 6 times less memory, with a 4.3 MiB binary on disk. The device was recording and held 4 records when I tested, so the download is probably still mostly the cost of connecting.

Tests #

The manual has 163 executable examples, all passing against the fake devices.

Against the real device, which was recording and held a few records:

The split depends on the device’s state: in another state (stopped, or without records), some of the examples that ran are skipped and some of the skipped ones run instead.

Besides the examples, there are 67 ordinary Rust tests.

The tests cover 80 % of the lines overall (coverage report), measured against the fake devices:

LayerLines coveredNote
Domain77–100 %protocol.rs is the weakest file, with 14 lines not covered
Application67–100 %discover.rs at 67 % and error.rs at 74 %
CLI adapter94 %
Bluetooth adapters23–49 %expected, as they only run against the real device

Coverage only says which lines are run by the tests, not that the tests check the right thing. So it’s more of an indication than a proof.

Size #

Lines of code, not counting blank and comment lines:

PartLines
Domain (no_std, no dependencies)~633
Application (no_std, depends on the domain only)268
Adapters (command line and Bluetooth)~640
Production total~1 540
Domain unit tests284
Adapter unit tests52
Varar step bindings790
Fake devices and report support869
Other integration tests337
Test total~2 330
Manual (Markdown)1 168

So there are about 1.5 lines of test code per line of production code, plus the manual. And the domain is the biggest layer, which is where the protocol knowledge should live.


The result is good enough so far. I have reached my first goal: manage the BSG-001 from my Linux machine.

The next goal is to integrate it into my home automation system running Gladys Assistant.

Comments Add one by emailing me.