{"id":36623977,"url":"https://github.com/memfault/mds-bridge","last_synced_at":"2026-01-12T09:29:47.082Z","repository":{"id":325387855,"uuid":"1096839793","full_name":"memfault/mds-bridge","owner":"memfault","description":"A bridge SDK implementing the MDS protocol over multiple transports","archived":false,"fork":false,"pushed_at":"2025-12-11T03:45:04.000Z","size":341,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-12-11T20:17:41.157Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/memfault.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-11-15T03:25:07.000Z","updated_at":"2025-12-11T03:45:07.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/memfault/mds-bridge","commit_stats":null,"previous_names":["memfault/mds-bridge"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/memfault/mds-bridge","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/memfault%2Fmds-bridge","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/memfault%2Fmds-bridge/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/memfault%2Fmds-bridge/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/memfault%2Fmds-bridge/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/memfault","download_url":"https://codeload.github.com/memfault/mds-bridge/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/memfault%2Fmds-bridge/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28337723,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-12T06:09:07.588Z","status":"ssl_error","status_checked_at":"2026-01-12T06:05:18.301Z","response_time":98,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-01-12T09:29:47.024Z","updated_at":"2026-01-12T09:29:47.072Z","avatar_url":"https://github.com/memfault.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MDS Bridge Library\n\nA cross-platform C library implementing the Memfault Diagnostic Service (MDS) protocol with pluggable backend support. The library provides a transport-agnostic protocol layer with built-in HID backend, enabling diagnostic data bridging from embedded devices to gateway applications.\n\n## Features\n\n- **Transport-agnostic MDS protocol**: Pluggable backend architecture supports HID, Serial, BLE, and custom transports\n- **Cross-platform support**: Windows, macOS, and Linux\n- **Built on HIDAPI**: Reliable cross-platform HID communication via built-in HID backend\n- **HTTP chunk uploader**: Built-in libcurl-based uploader for Memfault cloud integration\n- **Simple high-level API**: Automatic device management and initialization\n- **Pure C implementation**: Maximum compatibility and portability\n- **Comprehensive testing**: Full test suite with mocked dependencies (no hardware required)\n- **Language bindings**: Python and Node.js examples with native bindings\n\n## Architecture\n\nThe library uses a layered architecture with pluggable backends:\n\n```\n┌─────────────────────────────────────────────────┐\n│           Application Layer                     │\n│  (Your code, Python/Node.js bindings, etc.)    │\n└─────────────────────────────────────────────────┘\n                      ↓\n┌─────────────────────────────────────────────────┐\n│        MDS Protocol Layer (mds_protocol.h)      │\n│  • Session management                           │\n│  • Device configuration (features, URI, auth)   │\n│  • Stream control (enable/disable)              │\n│  • Packet processing (parse, validate, upload)  │\n│  • Sequence tracking                            │\n└─────────────────────────────────────────────────┘\n                      ↓\n┌─────────────────────────────────────────────────┐\n│      Backend Interface (mds_backend.h)          │\n│  • read(report_id, buffer, timeout)             │\n│  • write(report_id, buffer)                     │\n│  • destroy()                                    │\n└─────────────────────────────────────────────────┘\n           ↓                    ↓\n  ┌────────────────┐   ┌──────────────────┐\n  │  HID Backend   │   │  Custom Backends │\n  │  (built-in)    │   │  (Serial, BLE,   │\n  │                │   │   WebSocket...)  │\n  └────────────────┘   └──────────────────┘\n           ↓\n  ┌────────────────┐\n  │    HIDAPI      │\n  │  (USB HID I/O) │\n  └────────────────┘\n```\n\n### Backend Architecture\n\nThe MDS protocol is completely **transport-agnostic** through the backend interface. Any transport that can provide READ/WRITE operations can be used:\n\n**Built-in HID Backend** (`mds_backend_hid.c`):\n- Implements the backend interface using HIDAPI\n- Maps MDS report IDs to HID GET_FEATURE/SET_FEATURE/READ operations\n- Automatically initialized when using `mds_session_create_hid()`\n\n**Custom Backend Support**:\n- Implement the `mds_backend_ops_t` vtable with read/write/destroy functions\n- Pass your backend to `mds_session_create()` for full protocol support\n- Examples: Serial port, BLE GATT, WebSocket, or event-driven I/O (see Python/Node.js examples)\n\n**Event-driven I/O**:\n- For event-driven transports (node-hid, hidapi in non-blocking mode), you can:\n  - Create a session with NULL backend: `mds_session_create(NULL, \u0026session)`\n  - Use `mds_process_stream_from_bytes()` to process pre-received data\n  - The C library handles parsing, sequence validation, and upload callbacks\n\nThis design enables the same MDS protocol code to work across different transports without modification.\n\n## Building\n\n### Prerequisites\n\n#### HIDAPI\n\n**macOS:**\n```bash\nbrew install hidapi\n```\n\n**Linux (Ubuntu/Debian):**\n```bash\nsudo apt-get install libhidapi-dev\n```\n\n**Windows (using vcpkg - recommended):**\n```cmd\ngit clone https://github.com/Microsoft/vcpkg.git\ncd vcpkg\n.\\bootstrap-vcpkg.bat\n.\\vcpkg install hidapi:x64-windows curl:x64-windows\n.\\vcpkg integrate install\n```\n\nNote: On ARM64 Windows, use `hidapi:arm64-windows curl:arm64-windows` instead.\n\n#### libcurl (for HTTP chunk uploading)\n\n**macOS:**\n```bash\nbrew install curl\n```\n\n**Linux (Ubuntu/Debian):**\n```bash\nsudo apt-get install libcurl4-openssl-dev\n```\n\n**Windows:**\nInstall via vcpkg (see HIDAPI instructions above)\n\n#### CMake\n\n```bash\n# macOS\nbrew install cmake\n\n# Linux\nsudo apt-get install cmake\n\n# Windows\n# Download from https://cmake.org/download/\n```\n\n### Build Instructions\n\n**macOS / Linux:**\n```bash\nmkdir build\ncd build\ncmake ..\nmake\n\n# Optional: Run tests\nmake test\n\n# Optional: Install\nsudo make install\n```\n\n**Windows (using vcpkg):**\n```powershell\nmkdir build\ncd build\ncmake .. -DCMAKE_TOOLCHAIN_FILE=C:\\path\\to\\vcpkg\\scripts\\buildsystems\\vcpkg.cmake\ncmake --build . --config Release\n\n# Optional: Run tests\nctest -C Release\n```\n\nReplace `C:\\path\\to\\vcpkg` with your actual vcpkg installation path.\n\n### Build Options\n\n- `BUILD_SHARED_LIBS`: Build shared libraries (default: ON on Unix, OFF on Windows)\n- `BUILD_EXAMPLES`: Build example programs (default: ON)\n- `BUILD_TESTS`: Build test programs (default: ON)\n- `BUILD_VERBOSE`: Show verbose build output including npm (default: OFF)\n\nExample:\n```bash\n# Unix\ncmake -DBUILD_SHARED_LIBS=OFF -DBUILD_EXAMPLES=ON ..\n\n# Windows\ncmake .. -DBUILD_SHARED_LIBS=ON -DCMAKE_TOOLCHAIN_FILE=C:\\path\\to\\vcpkg\\scripts\\buildsystems\\vcpkg.cmake\n```\n\nNote: Windows defaults to static libraries because DLLs require export declarations. You can override with `-DBUILD_SHARED_LIBS=ON` but the library headers currently lack the necessary `__declspec` annotations.\n\n## Usage\n\n### Quick Start - MDS Protocol over HID\n\nThe primary use case is implementing the Memfault Diagnostic Service (MDS) protocol over USB HID. The library provides a simplified API that handles device management automatically.\n\n```c\n#include \"mds_bridge/mds_protocol.h\"\n\nint main(void) {\n    // Create MDS session (opens HID device and initializes automatically)\n    mds_session_t *session = NULL;\n    int ret = mds_session_create_hid(0x1234, 0x5678, NULL, \u0026session);\n    if (ret != 0) {\n        fprintf(stderr, \"Failed to create MDS session\\n\");\n        return 1;\n    }\n\n    // Read device configuration\n    mds_device_config_t config;\n    mds_read_device_config(session, \u0026config);\n    printf(\"Device ID: %s\\n\", config.device_identifier);\n    printf(\"Data URI: %s\\n\", config.data_uri);\n\n    // Enable streaming\n    mds_stream_enable(session);\n\n    // Process stream packets with automatic upload\n    while (running) {\n        mds_stream_packet_t packet;\n        ret = mds_process_stream(session, \u0026config, 5000, \u0026packet);\n        if (ret == 0) {\n            printf(\"Received chunk: seq=%d, len=%zu\\n\",\n                   packet.sequence, packet.data_len);\n            // Upload callback (if registered) was automatically called\n        }\n    }\n\n    // Cleanup (disables streaming and closes device automatically)\n    mds_session_destroy(session);\n    return 0;\n}\n```\n\n### MDS Protocol Overview\n\nThe MDS protocol uses HID reports to communicate diagnostic data:\n\n**Feature Reports** (Configuration \u0026 Control):\n- `0x01`: Supported features bitmask (read-only)\n- `0x02`: Device identifier string (read-only)\n- `0x03`: Data URI for chunk upload (read-only)\n- `0x04`: Authorization header (read-only, e.g., project key)\n- `0x05`: Stream control (read-write, enable/disable streaming)\n\n**Input Reports** (Device → Host):\n- `0x06`: Stream data packets with diagnostic chunks\n\nEach stream packet includes:\n- **Sequence counter** (5-bit, 0-31, wraps around) for detecting dropped packets\n- **Chunk data payload** (up to 63 bytes per packet)\n\n### MDS API Functions\n\n**Session Management:**\n- `mds_session_create_hid(vid, pid, serial, \u0026session)` - Create session with HID backend\n- `mds_session_create_hid_path(path, \u0026session)` - Create session with HID backend (device path)\n- `mds_session_create(backend, \u0026session)` - Create session with custom backend\n- `mds_session_destroy(session)` - Destroy session and cleanup\n\n**Device Configuration:**\n- `mds_read_device_config(session, \u0026config)` - Read all configuration\n- `mds_get_supported_features(session, \u0026features)` - Get feature bitmask\n- `mds_get_device_identifier(session, buffer, size)` - Get device ID\n- `mds_get_data_uri(session, buffer, size)` - Get upload URI\n- `mds_get_authorization(session, buffer, size)` - Get auth header\n\n**Stream Control:**\n- `mds_stream_enable(session)` - Enable diagnostic data streaming\n- `mds_stream_disable(session)` - Disable streaming\n\n**Data Reception:**\n- `mds_stream_read_packet(session, \u0026packet, timeout_ms)` - Read packet (blocking I/O)\n- `mds_process_stream(session, \u0026config, timeout_ms, \u0026packet)` - Read + validate + upload\n- `mds_process_stream_from_bytes(session, \u0026config, buffer, len, \u0026packet)` - Parse pre-received data\n\n**Chunk Upload:**\n- `mds_set_upload_callback(session, callback, user_data)` - Register upload callback\n\n### Uploading Chunks to Memfault Cloud\n\nThe library supports both custom upload callbacks and a built-in HTTP uploader.\n\n**Option 1: Custom Upload Callback**\n\n```c\nint my_upload_callback(const char *uri, const char *auth_header,\n                       const uint8_t *chunk_data, size_t chunk_len,\n                       void *user_data) {\n    // Parse authorization header (format: \"HeaderName:HeaderValue\")\n    // POST chunk_data to uri with appropriate headers\n    // Content-Type: application/octet-stream\n    return 0; /* Return 0 on success, negative on error */\n}\n\n// Register callback\nmds_set_upload_callback(session, my_upload_callback, my_context);\n\n// Process streams - callback is automatically invoked\nwhile (running) {\n    mds_process_stream(session, \u0026config, 5000, NULL);\n}\n```\n\n**Option 2: Built-in HTTP Uploader**\n\n```c\n#include \"mds_bridge/chunks_uploader.h\"\n\n// Create uploader\nchunks_uploader_t *uploader = chunks_uploader_create();\nchunks_uploader_set_verbose(uploader, true);\n\n// Register built-in uploader\nmds_set_upload_callback(session, chunks_uploader_callback, uploader);\n\n// Process streams - chunks are automatically uploaded\nwhile (running) {\n    mds_process_stream(session, \u0026config, 5000, NULL);\n}\n\n// Get stats\nchunks_upload_stats_t stats;\nchunks_uploader_get_stats(uploader, \u0026stats);\nprintf(\"Uploaded: %zu chunks, %zu bytes\\n\",\n       stats.chunks_uploaded, stats.bytes_uploaded);\n\n// Cleanup\nchunks_uploader_destroy(uploader);\n```\n\n### Device Enumeration\n\nFor applications that need to list/select HID devices:\n\n```c\n#include \"mds_bridge/memfault_hid.h\"\n\n// Initialize HID library\nmemfault_hid_init();\n\n// Enumerate devices\nmemfault_hid_device_info_t *devices = NULL;\nsize_t num_devices = 0;\n\nint ret = memfault_hid_enumerate(0x1234, 0x5678, \u0026devices, \u0026num_devices);\nif (ret == MEMFAULT_HID_SUCCESS) {\n    for (size_t i = 0; i \u003c num_devices; i++) {\n        printf(\"Device: %ls (%04X:%04X)\\n\",\n               devices[i].product,\n               devices[i].vendor_id,\n               devices[i].product_id);\n        printf(\"  Path: %s\\n\", devices[i].path);\n    }\n    memfault_hid_free_device_list(devices);\n}\n\n// Cleanup\nmemfault_hid_exit();\n```\n\n**Note**: When using `mds_session_create_hid()`, the HID library is initialized automatically.\n\n### Custom Backend Example\n\nImplement your own transport by providing the backend vtable:\n\n```c\n#include \"mds_bridge/mds_backend.h\"\n\n// Your backend state\ntypedef struct {\n    int serial_fd;  // Or BLE handle, WebSocket, etc.\n} my_backend_t;\n\n// Backend read operation\nint my_backend_read(void *impl_data, uint8_t report_id,\n                    uint8_t *buffer, size_t length, int timeout_ms) {\n    my_backend_t *backend = impl_data;\n    // Read from your transport\n    return bytes_read;\n}\n\n// Backend write operation\nint my_backend_write(void *impl_data, uint8_t report_id,\n                     const uint8_t *buffer, size_t length) {\n    my_backend_t *backend = impl_data;\n    // Write to your transport\n    return bytes_written;\n}\n\n// Backend cleanup\nvoid my_backend_destroy(void *impl_data) {\n    my_backend_t *backend = impl_data;\n    // Close/cleanup your transport\n    free(backend);\n}\n\n// Create backend\nmds_backend_ops_t ops = {\n    .read = my_backend_read,\n    .write = my_backend_write,\n    .destroy = my_backend_destroy\n};\n\nmy_backend_t *backend_impl = /* initialize your transport */;\n\nmds_backend_t backend = {\n    .ops = \u0026ops,\n    .impl_data = backend_impl\n};\n\n// Create MDS session with your backend\nmds_session_t *session;\nmds_session_create(\u0026backend, \u0026session);\n```\n\n## Examples\n\nThe library includes example programs in C, Python, and Node.js:\n\n### C Examples\n\n- **`mds_gateway`**: Full MDS gateway that uploads diagnostic chunks to Memfault cloud\n- **`mds_monitor`**: Real-time monitor for inspecting MDS stream data\n\n**macOS / Linux:**\n```bash\n# MDS gateway - upload chunks to Memfault cloud\n./build/examples/mds_gateway 2fe3 0007\n\n# MDS gateway - dry-run mode (print chunks without uploading)\n./build/examples/mds_gateway 2fe3 0007 --dry-run\n\n# MDS monitor - display stream data in real-time\n./build/examples/mds_monitor 2fe3 0007\n\n# MDS monitor - interactive device selection\n./build/examples/mds_monitor\n```\n\n**Windows:**\n```powershell\n# MDS gateway - upload chunks to Memfault cloud\n.\\build\\examples\\Release\\mds_gateway.exe 2fe3 0007\n\n# MDS gateway - dry-run mode (print chunks without uploading)\n.\\build\\examples\\Release\\mds_gateway.exe 2fe3 0007 --dry-run\n\n# MDS monitor - display stream data in real-time\n.\\build\\examples\\Release\\mds_monitor.exe 2fe3 0007\n\n# MDS monitor - interactive device selection\n.\\build\\examples\\Release\\mds_monitor.exe\n```\n\n### Python Example\n\nUses ctypes bindings with a custom Python backend:\n\n**macOS / Linux:**\n```bash\ncd build/examples/python\npython3 main.py 2fe3 0007\n```\n\n**Windows:**\n```powershell\ncd build\\examples\\python\npython main.py 2fe3 0007\n```\n\nThe Python example demonstrates:\n- Custom backend implementation bridging hidapi-python with the C library\n- Event-driven I/O using `mds_process_stream_from_bytes()`\n- Upload callback registration from Python\n\n### Node.js Example\n\nUses N-API native addon:\n\n**macOS / Linux:**\n```bash\ncd build/examples/nodejs\nnpm start -- 2fe3 0007\n```\n\n**Windows:**\n```powershell\ncd build\\examples\\nodejs\nnpm start -- 2fe3 0007\n```\n\nThe Node.js example demonstrates:\n- N-API addon for native C bindings\n- Integration with node-hid for HID I/O\n- Event-driven processing with the MDS protocol\n\nSee [examples/README.md](examples/README.md) for detailed documentation.\n\n## API Headers\n\nThe library provides three public headers:\n\n- **`mds_bridge/memfault_hid.h`** - Device enumeration and library initialization\n- **`mds_bridge/mds_protocol.h`** - High-level MDS protocol API\n- **`mds_bridge/mds_backend.h`** - Backend interface for custom transports\n- **`mds_bridge/chunks_uploader.h`** - Built-in HTTP uploader\n\nMost applications only need `mds_protocol.h`.\n\n## Testing\n\nThe library includes comprehensive test suites with mocked dependencies (no hardware or network required):\n\n**macOS / Linux:**\n```bash\ncd build\nmake test\n# Or use ctest directly\nctest --verbose\n```\n\n**Windows:**\n```powershell\ncd build\nctest -C Release --verbose\n\n# Or run individual tests\n.\\test\\Release\\test_hid.exe\n.\\test\\Release\\test_upload.exe\n.\\test\\Release\\test_mds_e2e.exe\n```\n\nOutput:\n```\nRunning tests...\nTest project /path/to/build\n    Start 1: HID_Tests\n1/3 Test #1: HID_Tests ........................   Passed    0.11 sec\n    Start 2: Upload_Tests\n2/3 Test #2: Upload_Tests .....................   Passed    0.01 sec\n    Start 3: MDS_E2E_Test\n3/3 Test #3: MDS_E2E_Test .....................   Passed    0.01 sec\n\n100% tests passed, 0 tests failed out of 3\n```\n\n### Test Suites\n\n- **HID Tests** (`test_hid`): 20 tests covering HID communication and MDS protocol with mock hidapi\n- **Upload Tests** (`test_upload`): 12 tests covering HTTP upload functionality with mock libcurl\n- **E2E Integration Test** (`test_mds_e2e`): Complete gateway workflow test with mocked device and cloud\n\nSee [test/README.md](test/README.md) for detailed testing documentation.\n\n## Platform Notes\n\n### Windows\n\n- Requires Windows 7 or later (Windows 10+ recommended)\n- Visual Studio 2019 or later (for MSVC compiler)\n- CMake 3.15 or later\n- No driver installation required for most HID devices\n- May require administrator privileges for some devices\n- Builds static libraries by default (shared libraries require DLL export annotations)\n- Use vcpkg for dependency management (recommended)\n- Both x64 and ARM64 architectures are supported\n\n### macOS\n\n- Requires macOS 10.9 or later\n- Uses IOKit framework (linked automatically by CMake)\n- No special permissions required for most HID devices\n\n### Linux\n\n- Requires kernel 2.6.39 or later\n- May require udev rules for non-root access\n- Example udev rule (create `/etc/udev/rules.d/99-hidraw-permissions.rules`):\n  ```\n  KERNEL==\"hidraw*\", ATTRS{idVendor}==\"1234\", ATTRS{idProduct}==\"5678\", MODE=\"0666\"\n  ```\n\n## Error Handling\n\nAll API functions that can fail return an error code. Use `memfault_hid_error_string()` to get a human-readable description:\n\n```c\nint ret = mds_session_create_hid(vid, pid, NULL, \u0026session);\nif (ret != 0) {\n    fprintf(stderr, \"Error: %s\\n\", memfault_hid_error_string(ret));\n}\n```\n\n## Contributing\n\nContributions are welcome! Please:\n\n1. Follow the existing code style\n2. Add tests for new features\n3. Update documentation\n4. Ensure cross-platform compatibility\n\n## License\n\nMIT License - See LICENSE file for details\n\n## Support\n\nFor issues, questions, or contributions, please visit:\nhttps://github.com/memfault/mds-bridge\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmemfault%2Fmds-bridge","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmemfault%2Fmds-bridge","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmemfault%2Fmds-bridge/lists"}