Skip to content

FidelityFramework/BAREWire

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

35 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

BAREWire

Type-safe binary encoding and zero-copy memory operations for the Fidelity Framework.

License: Apache 2.0 License: Commercial

🚧 Under Active Development 🚧
This project is in early development and not intended for production use.

Overview

BAREWire implements the BARE (Binary Application Record Encoding) protocol with F#'s type system providing compile-time safety. It enables zero-copy operations, structured memory access, and efficient inter-process communication - all without runtime overhead.

Key Characteristics

  • Zero Dependencies: Pure F# implementation (FSharp.UMX for phantom types)
  • Type Safety: Leverages units of measure for compile-time memory safety
  • Zero-Copy Operations: Direct memory access without intermediate allocations
  • Schema-Driven: Type-safe DSL for defining binary data structures
  • Modular Design: Use only the components you need

The Fidelity Framework

BAREWire is part of the Fidelity native F# compilation ecosystem:

Project Role
Firefly AOT compiler: F# β†’ PSG β†’ MLIR β†’ Native binary
FNCS F# Native Compiler Services (intrinsics, native types)
BAREWire Binary encoding, memory mapping, zero-copy IPC
Farscape C/C++ header parsing for native library bindings
XParsec Parser combinators powering PSG traversal and header parsing

The name "Fidelity" reflects the framework's core mission: preserving type and memory safety from source code through compilation to native execution.

Dual-Target Architecture

BAREWire supports two compilation targets:

Target Use Case Available Modules
Firefly Native desktop/embedded applications All modules
Fable WREN stack WebSocket IPC Encoding modules only

WREN Stack Integration

The WREN Stack (WebView + Reactive + Embedded + Native) uses BAREWire for type-safe communication between the native Firefly backend and the Fable/JavaScript frontend:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                 WREN Stack Application                   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  Frontend (Fable)  β”‚      Backend (Firefly)             β”‚
β”‚  Partas.Solid UI   β”‚      Native Application Logic      β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚         WebSocket + BAREWire Binary Protocol            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Module Availability by Target

Dual-Target (Firefly + Fable):

  • BAREWire.Core.Buffer - Sequential write buffer
  • BAREWire.Core.Binary - Byte conversion utilities
  • BAREWire.Encoding.* - BARE encoding/decoding

Firefly-Only:

  • BAREWire.Core.Memory<'T,'region> - Capability-based memory
  • BAREWire.Memory.* - Region, View, SafeMemory
  • BAREWire.Core.Capability - Lifetime markers

Core Concepts

BAREWire provides four interconnected capabilities:

1. Schema Definition

Define binary data structures with a type-safe DSL:

open BAREWire.Schema.DSL

let messageSchema =
    schema "Message"
    |> withType "UserId" string
    |> withType "Timestamp" int64
    |> withType "Content" string
    |> withType "Message" (struct' [
        field "sender" (userType "UserId")
        field "timestamp" (userType "Timestamp")
        field "content" (userType "Content")
    ])
    |> validate
    |> Result.get

2. Binary Encoding/Decoding

Convert between F# values and compact binary representations:

open BAREWire.Encoding.Codec

type Message = {
    Sender: string
    Timestamp: int64
    Content: string
}

// Encode to bytes
let encoded = encode messageSchema message buffer

// Decode from bytes
let decoded = decode<Message> messageSchema memory

3. Memory Mapping

Access structured data in memory without copying:

open BAREWire.Memory.Region
open BAREWire.Memory.View

// Create a typed memory region
let region = create<Message, heap> data

// Create a view for field access
let view = View.create<Message, heap> region messageSchema

// Read fields directly from memory
let sender = View.getField<Message, string, heap> view ["sender"]

4. Inter-Process Communication

Share typed data between processes:

open BAREWire.IPC.SharedMemory

// Process 1: Create shared region
let shared = create<Message> "channel" size messageSchema

// Process 2: Open existing region
let received = open'<Message> "channel" messageSchema

Type Safety with Units of Measure

BAREWire uses F#'s units of measure and FSharp.UMX phantom types to prevent memory errors at compile time:

open FSharp.UMX
open BAREWire.Core

// Memory regions are typed
[<Measure>] type heap
[<Measure>] type stack
[<Measure>] type shared

// Offsets and sizes are dimensioned
let offset = 16<offset>
let size = 1024<bytes>

// Type system prevents mixing incompatible memory
let heapMem: Memory<Message, heap> = ...
let stackMem: Memory<Message, stack> = ...
// Cannot accidentally mix these - compile error!

Hardware Integration

For embedded targets, BAREWire provides Peripheral Descriptors that capture memory-mapped hardware layouts:

type PeripheralDescriptor = {
    Name: string                          // "GPIO"
    Instances: Map<string, unativeint>    // GPIOA β†’ 0x48000000
    Layout: PeripheralLayout
    MemoryRegion: MemoryRegionKind        // Peripheral | SRAM | Flash
}

type FieldDescriptor = {
    Name: string                          // "ODR", "BSRR"
    Offset: int                           // Byte offset from base
    Type: RegisterType
    Access: AccessKind                    // ReadOnly | WriteOnly | ReadWrite
}

Farscape generates these descriptors from C/C++ headers (like CMSIS HAL), and Firefly's Alex component uses them to emit correct memory-mapped access code with proper volatile semantics.

Schema Compatibility

BAREWire includes tools for evolving schemas safely:

open BAREWire.Schema.Analysis

match checkCompatibility oldSchema newSchema with
| FullyCompatible ->
    printfn "Schemas are fully compatible"
| BackwardCompatible ->
    printfn "New schema can read old data"
| ForwardCompatible ->
    printfn "Old schema can read new data"
| Incompatible reasons ->
    printfn "Breaking changes: %A" reasons

Architecture

src/
β”œβ”€β”€ Core/           # Fundamental types and operations
β”‚   β”œβ”€β”€ Binary.fs   # Binary conversion (Dual-target βœ“)
β”‚   β”œβ”€β”€ Memory.fs   # Buffer (Dual-target βœ“) + Memory<'T,'region> (Firefly)
β”‚   β”œβ”€β”€ Types.fs    # Core type definitions and measures
β”‚   β”œβ”€β”€ Utf8.fs     # UTF-8 encoding/decoding (Dual-target βœ“)
β”‚   └── Capability.fs # Lifetime markers (Firefly only)
β”œβ”€β”€ Encoding/       # BARE protocol implementation (Dual-target βœ“)
β”‚   β”œβ”€β”€ Codec.fs    # Combined encode/decode
β”‚   β”œβ”€β”€ Decoder.fs  # Decoding primitives
β”‚   └── Encoder.fs  # Encoding primitives
β”œβ”€β”€ Memory/         # Memory mapping and views (Firefly only)
β”‚   β”œβ”€β”€ Region.fs   # Memory region operations
β”‚   β”œβ”€β”€ View.fs     # Typed field access
β”‚   └── Mapping.fs  # Memory mapping functions
β”œβ”€β”€ IPC/            # Inter-process communication (Firefly only)
β”‚   β”œβ”€β”€ SharedMemory.fs  # Shared memory regions
β”‚   β”œβ”€β”€ MessageQueue.fs  # Message queues
β”‚   └── NamedPipe.fs     # Named pipes
β”œβ”€β”€ Network/        # Network protocol support
β”‚   β”œβ”€β”€ Protocol.fs # Message passing primitives
β”‚   β”œβ”€β”€ Transport.fs # Transport abstractions
β”‚   └── Frame.fs    # Wire frame format
└── Schema/         # Schema definition and validation
    β”œβ”€β”€ DSL.fs      # Schema definition language
    β”œβ”€β”€ Definition.fs # Schema type definitions
    β”œβ”€β”€ Validation.fs # Schema validation
    └── Analysis.fs   # Compatibility checking

Legend: Modules marked "(Dual-target βœ“)" work on both Firefly and Fable. All others are Firefly-only.

Performance

BAREWire is designed for high-performance scenarios:

  • Zero-copy: Read structured data directly from memory/network buffers
  • No allocations: Encoding/decoding can work with pre-allocated buffers
  • Compact format: BARE encoding is smaller than JSON, MessagePack, or Protobuf
  • Type-safe without overhead: All safety checks happen at compile time

Installation

For .NET projects:

dotnet add package BAREWire

For Fidelity/Firefly projects, BAREWire is included as source files via the project configuration.

Development Status

BAREWire is under active development. Current focus:

  • Core encoding/decoding for primitive and composite types
  • Memory region abstractions
  • Shared memory IPC
  • Peripheral descriptor support for embedded targets

License

BAREWire is dual-licensed under both the Apache License 2.0 and a Commercial License.

Open Source License

For open source projects, academic use, non-commercial applications, and internal tools, use BAREWire under the Apache License 2.0.

Commercial License

A Commercial License is required for incorporating BAREWire into commercial products or services. See Commercial.md for details.

Patent Notice

BAREWire includes technology covered by U.S. Patent Application No. 63/786,247 "System and Method for Zero-Copy Inter-Process Communication Using BARE Protocol". See PATENTS.md for licensing details.

Contributing

Contributions are welcome! By submitting a pull request, you agree to license your contributions under the same dual license terms.

Acknowledgments

  • BARE Protocol: The binary encoding specification
  • FSharp.UMX: Phantom types for units of measure
  • Firefly Team: For the native compilation infrastructure

About

Implementation of the BARE protocol in F# for memory management, IPC and network comms

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages