Developer guide v0.2.1

Quick Start.

Build cadb, connect to a test device, and run your first shell command. Then wire the same C API into your own application.

C11CMake ≥ 3.16No adb server required~10 minutes

00 / Overview

What you'll build

You'll compile the cadb library and its example program, connect directly to an Android Debug Bridge daemon (adbd) over TCP, then run a shell command. The complete example owns the TCP socket and releases every resource on exit.

Start with an unauthenticated development endpoint. The zero-dependency sample deliberately does not implement RSA AUTH, TLS, wireless pairing, or device discovery. Later sections explain how the host app supplies those platform features.

01 / Requirements

Set up your build environment

  • A C11 compiler: Clang, GCC, or MSVC.
  • CMake 3.16 or newer.
  • A reachable Android device or emulator with an ADB TCP endpoint.
  • For secured devices, an application-provided signing callback; see Add authentication.

The built-in network backend supports POSIX platforms such as macOS, Linux, Android, and iOS. Other platforms can supply a custom cadb_net_ops_t transport.

02 / Build

Build and check cadb

From a cadb checkout, configure the library, examples, and tests, then compile and run the test suite:

SHELL

Configure, build, test

cmake -S . -B build \
  -DCMAKE_BUILD_TYPE=Release \
  -DCADB_BUILD_TESTS=ON \
  -DCADB_BUILD_EXAMPLES=ON
cmake --build build --parallel
ctest --test-dir build --output-on-failure

The example executable is built at build/examples/cadb_minimal_connect. The CMake options and sanitizer configuration are documented in the build guide.

03 / Device

Prepare a test endpoint

Use an emulator or a test device configured to accept an unauthenticated ADB TCP connection. Enter its reachable IP address and port in the command below; port 5555 is common for classic ADB-over-TCP setups.

Use a trusted development network. An unauthenticated ADB endpoint grants remote shell access. Do not expose it to the public internet. Production and wireless-debugging connections should use AUTH and a TLS-capable transport.

The address 192.0.2.10 below is reserved for documentation. Replace it with the test device's actual reachable address.

04 / First connection

Run a shell command

Pass the device IP, port, and optional shell command to the sample. If you omit the command, it queries the Android model:

SHELL

Run the built-in example

./build/examples/cadb_minimal_connect \
  192.0.2.10 5555 \
  "getprop ro.product.model"

On success, the sample prints the command output. On connection or protocol failure, it prints the cadb error name and numeric code. The full source is examples/minimal_connect.c.

05 / Application integration

Link cadb from your CMake project

Fetch a reviewed, immutable cadb commit and link your target against cadb::cadb. This example pins the commit currently tagged v0.2.1:

CMAKE

FetchContent

include(FetchContent)

FetchContent_Declare(cadb
    GIT_REPOSITORY https://github.com/weenas/cadb.git
    GIT_TAG        bc8fa7580acb48dfae3af814e0891f557cda1684
)
FetchContent_MakeAvailable(cadb)

add_executable(device_tool main.c)
target_link_libraries(device_tool PRIVATE cadb::cadb)

Set your target's C standard to C11 if your project doesn't already do so. For a local checkout, use add_subdirectory(path/to/cadb cadb) instead. Do not depend on the moving main branch.

Connect and execute Shell v2

The code below is a compact, complete example for a test device that does not require AUTH. It reports transport errors separately from the remote command's exit status.

C11

main.c

#include <cadb/cadb.h>
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    cadb_net_t *net = NULL;
    cadb_t *adb = NULL;
    cadb_socket_t socket = CADB_INVALID_SOCKET;
    cadb_net_config_t net_config = { .default_timeout_ms = 5000u };
    cadb_endpoint_t endpoint = {
        .host = "192.0.2.10",
        .port = 5555u
    };
    cadb_config_t adb_config = {0};
    uint8_t out[4096], err_out[1024];
    cadb_shell_v2_result_t result = { .exit_code = -1 };
    cadb_err_t err = CADB_OK;
    int exit_status = EXIT_FAILURE;

    err = cadb_net_create(&net_config, &net);
    if (err != CADB_OK) goto done;
    err = cadb_net_connect(net, &endpoint, 5000u, NULL, &socket);
    if (err != CADB_OK) goto done;

    adb_config.net = net;
    adb_config.socket_handle = socket;
    adb_config.timeout_ms = 5000u;
    err = cadb_create(&adb_config, &adb);
    if (err != CADB_OK) goto done;
    err = cadb_connect(adb, NULL);
    if (err != CADB_OK) goto done;

    err = cadb_exec_shell_v2(adb, "getprop ro.product.model",
                             out, sizeof(out), err_out, sizeof(err_out),
                             &result, NULL);
    if (err != CADB_OK) goto done;

    fwrite(out, 1u, result.stdout_length, stdout);
    fwrite(err_out, 1u, result.stderr_length, stderr);
    exit_status = result.exit_code == 0 ? EXIT_SUCCESS : EXIT_FAILURE;

done:
    if (err != CADB_OK) {
        fprintf(stderr, "cadb: %s (%d)\n", cadb_err_str(err), (int)err);
    }
    cadb_destroy(adb);
    if (socket != CADB_INVALID_SOCKET) cadb_net_close(net, socket);
    cadb_net_destroy(net);
    return exit_status;
}

  • Ownership: cadb_t borrows the network object and socket handle. Destroy the session, close the caller-owned socket, then destroy the network object.
  • Shell status: a completed protocol exchange can return CADB_OK even when the remote command failed. Check result.exit_code.
  • Session use: serialize calls and close every stream before destroying the session.

06 / Authentication

Add ADB RSA authentication

cadb does not create or store private keys. The application provides two callbacks through cadb_auth_provider_t: one signs the challenge, and one returns the ADB-encoded public key.

C11

Attach platform-owned key callbacks

cadb_auth_provider_t auth = {
    .context = platform_key_store,
    .sign = platform_sign_adb_digest,
    .get_public_key = platform_copy_adb_public_key,
};

cadb_config_t config = {
    .net = net,
    .socket_handle = socket,
    .timeout_ms = 5000u,
    .auth = auth,
};

The callback names above are application functions you implement. On Apple platforms, keep the key in Keychain/SecKey; on Android, use Android Keystore. Keep auth.context alive until cadb_destroy().

Sign the challenge as supplied. Classic ADB AUTH provides a 20-byte SHA-1 digest. Return an RSA PKCS#1 v1.5 DigestInfo(SHA-1) signature without hashing the challenge a second time. See adb_cli.c for an OpenSSL example.

07 / Platform transport

Supply TLS and cancellation policy

For wireless debugging or a platform-owned socket, provide a complete cadb_net_ops_t transport. Your adapter owns socket creation and TLS policy; cadb calls it through the stable transport interface.

  • A connect callback may return a socket that is already TLS-connected after the host performs pairing and peer validation.
  • If ADB requests an STLS upgrade, implement the optional start_tls callback and validate the peer certificate there.
  • Every callback must honor its timeout and cancellation token. Send and receive callbacks may complete partially but must report actual progress.
  • Create one cadb_cancel_t per operation. Trigger it from the UI if needed, wait for the worker to return, then destroy the token.

Device discovery, wireless pairing, and trust prompts belong to native platform APIs such as Network.framework or Android's network service APIs. cadb does not implement those flows.

Read the full mobile transport guide

08 / Troubleshooting

Common connection issues

Connection refused or timed out

Check the device IP, port, routing, and firewall. The built-in POSIX backend accepts numeric addresses; it does not resolve hostnames. Confirm that adbd is listening on the selected TCP port.

Authentication failed

Verify that the signing callback handles the 20-byte challenge directly and that the public-key callback returns the ADB wire-format key. Keep the matching key identity in the platform store.

Command reports failure but cadb returned CADB_OK

CADB_OK means the shell protocol exchange completed. Check cadb_shell_v2_result_t.exit_code for the remote command's status.

CADB_ERR_NOT_SUPPORTED

The current build or transport does not implement the requested backend operation. For TLS, provide a TLS-connected handle or an appropriate start_tls callback.

09 / Continue

Go further with cadb