CropPath 开发者

全部指南

C++ SDK

可用

基于 C 设备 SDK 的 C++ 封装。

构建在 C SDK 之上、仅含头文件的 C++17 封装,用于以 C++ 编写的设备固件和网关软件。所有被签名和发送的字节仍由 C SDK 生成,因此 C++ 设备的帧就是 C SDK 的帧。封装增加了拥有所有权的类型、取代 C 函数指针的 C++ 接口,以及 Status / Result<T> 形式的错误;与 C SDK 不同,它会进行堆分配。启用或关闭异常(-fno-exceptions,ESP-IDF 的默认设置)时都能使用。它构建并发送设备信封,不是 HTTP API 的客户端。

从本门户的“下载”页面(/developers/downloads)下载两个源代码包:croppath-cpp-0.3.0.tar.gz(C++ 封装)和 croppath-c-0.3.0.tar.gz(它所链接的 C SDK),以及它们的 .sha256 文件,核对 SHA-256 后按下方命令解压。CropPath 只通过本门户分发 SDK,不通过公共软件包仓库。把 C SDK 的核心文件(如使用 MbedtlsKey,还需要它的 mbedTLS 后端,要求 mbedTLS 3.6 或更高版本,即 ESP-IDF 5.3 或更高版本)以及两个 include 目录加入构建,使用 C++17 或更高版本。

for f in croppath-cpp-0.3.0.tar.gz croppath-c-0.3.0.tar.gz; do
  curl -fLO https://txng.theio.vn/developers/downloads/sdk/$f && curl -fLO https://txng.theio.vn/developers/downloads/sdk/$f.sha256 && sha256sum -c $f.sha256 && tar xzf $f
done                                            # macOS: shasum -a 256 -c instead of sha256sum -c
#include "agritrace/agritrace.hpp"
#include "agritrace/agritrace_mbedtls.hpp"

using namespace agritrace;

MbedtlsKey key(platform_rng, nullptr);   // at_rng_fn of the platform
// key.load(private_key) from secure storage, or key.generate() on first boot and register key.public_key()

ClientConfig cfg;
cfg.tenant = "coop-1";
cfg.site = "ph-1";
cfg.device_id = "knk-1";
cfg.profile = "vn.agritrace.durian-knocker@1.3.0";
cfg.kid = "knk-1-key";
cfg.signer = &key;                       // not owned: must outlive the client
cfg.storage = &storage;                  // a Storage, e.g. CStorage over the C port of your flash
cfg.transport = [&](std::string_view route, std::string_view frame) -> Status {
    int http = post("/v1/ingest/" + std::string(route), frame);
    if ((http >= 200 && http < 300) || http == 409) return {};       // stored, or already stored
    if (http == 0 || http == 408 || http == 429 || http >= 500) return Errc::retry;
    return Errc::rejected;
};
Result<Client> client = Client::open(cfg);
if (!client) return client.status();     // or client.value(), which throws agritrace::Error

Message m;
m.kind(Kind::event)
    .event_type("inspect")
    .context("lot", lot_id)
    .reading(Reading("produce.maturity_index", Value::decimal(825, 1)).unit("%"));
Result<Emitted> e = client->emit(m);     // signed and stored in the outbox; nothing is sent yet
Result<Flushed> f = client->flush();     // f->retry_later: the link failed, the frame stays queued

您的 Signer、Storage、传输、时钟和 on_rejected 可以抛出异常:异常绝不会穿过 C 代码;C SDK 收到失败码并保持状态一致,C 调用返回后异常被原样重新抛出。Client 不是线程安全的。尚未验证:使用设备工具链(ESP-IDF、STM32Cube)的构建和 32 位构建;测试在 64 位 Linux 主机上运行。

API 参考中的相关内容