Skip to content

CanBusService Class

Default ICanBusService: attaches once to ICanBus.FrameObserved and fans each observed CanFrameView out to every registered Subscription (arc42 §5.3 "Multi-Protokoll-Demux", ADR-5), and implements SendConfirmed(CanFrame, Nullable<TimeSpan>, CancellationToken) (arc42 §6.3, ADR-7; FR-RAW-030..034) on the same single ICanBus.FrameObserved subscription.

public sealed class CanBusService : CanKit.Pro.RawCan.ICanBusService, System.IDisposable

Inheritance Object → CanBusService

Implements ICanBusService, IDisposable

Constructors

CanBusService(ICanBus) Constructor

Creates a service that demultiplexes bus. Attaches to the bus's ICanBus.FrameObserved event immediately.

public CanBusService(CanKit.Abstractions.API.Can.ICanBus bus);

Parameters

bus ICanBus

Fields

CanBusService.DefaultBufferCapacity Field

Default per-subscription bounded buffer capacity when none is specified.

public const int DefaultBufferCapacity = 1024;

Field Value

Int32

CanBusService.DefaultConfirmTimeout Field

Default SendConfirmed(CanFrame, Nullable<TimeSpan>, CancellationToken) echo-wait timeout when none is specified (FR-RAW-034).

public static readonly TimeSpan DefaultConfirmTimeout;

Field Value

TimeSpan

Properties

CanBusService.Bus Property

The underlying bus this service demultiplexes.

public CanKit.Abstractions.API.Can.ICanBus Bus { get; }

Implements Bus

Property Value

ICanBus

CanBusService.SubscriptionCount Property

Number of currently registered (not yet disposed) subscriptions. Primarily for diagnostics/tests: after disposing every subscription it returns to zero, proving no registry entries leak (FR-RAW-012).

public int SubscriptionCount { get; }

Implements SubscriptionCount

Property Value

Int32

Methods

CanBusService.FindOverlappingFilterSubscriptions() Method

Diagnostic: finds every pair of currently registered, still-undisposed CanIdFilter-based subscriptions whose ID spaces overlap, and the range of CAN IDs each pair shares (FR-RAW-041, "Should") -- helps catch misconfiguration when multiple protocol instances were meant to have disjoint ID ranges but don't. Subscriptions registered via the generic Subscribe(Func<CanFrameEvent,bool>, Nullable<int>, bool) predicate overload are opaque and are not analyzable, so they are skipped.

public System.Collections.Generic.IReadOnlyList<CanKit.Pro.RawCan.FilterOverlap> FindOverlappingFilterSubscriptions();

Implements FindOverlappingFilterSubscriptions()

Returns

IReadOnlyList<FilterOverlap>
One FilterOverlap per overlapping pair, each naming the two subscriptions and the ID range on which they collide. Empty when no two filters share ID space.

CanBusService.SendConfirmed(CanFrame, Nullable<TimeSpan>, CancellationToken) Method

Sends frame and asynchronously confirms it was actually sent, using a uniform abstraction regardless of whether the underlying bus has hardware TX echo enabled (arc42 §6.3, ADR-7; FR-RAW-030). When the bus both declares Echo and has WorkMode == ChannelWorkMode.Echo configured, confirmation comes from an actually-matched echo frame (FR-RAW-031, including correct FIFO matching of multiple concurrent byte-identical sends — no cross-matching or crash); otherwise it is a documented approximation based on driver acceptance (IsApproximated, FR-RAW-032). Never hangs: timeout, bus-off, and outright rejection all resolve the returned task within bounded time (FR-RAW-033) — see TxConfirmation for exactly how.

public System.Threading.Tasks.Task<CanKit.Pro.RawCan.TxConfirmation> SendConfirmed(CanKit.Abstractions.API.Can.Definitions.CanFrame frame, System.Nullable<System.TimeSpan> timeout=null, System.Threading.CancellationToken cancellationToken=default(System.Threading.CancellationToken));

Parameters

frame CanFrame

The frame to send. As with ICanBus.Transmit(CanFrame@), the caller remains the owner (TX-lease) and is responsible for disposing it after this call returns/completes — ICanBusService never disposes it.

timeout Nullable<TimeSpan>

Maximum time to wait for an echo before failing with Timeout (FR-RAW-034); null uses DefaultConfirmTimeout. Ignored on the approximated path (driver acceptance is synchronous/immediate). Must be positive.

cancellationToken CancellationToken

Caller-supplied cancellation; cancels the returned task per standard .NET convention, distinct from the domain-level Timeout outcome.

Implements SendConfirmed(CanFrame, Nullable<TimeSpan>, CancellationToken)

Returns

Task<TxConfirmation>

CanBusService.Subscribe(CanIdFilter, Nullable<int>, bool) Method

Registers a subscription using the allocation-free ID-range/mask fast path (FR-RAW-010/013).

public CanKit.Pro.RawCan.ISubscription Subscribe(CanKit.Pro.RawCan.CanIdFilter filter, System.Nullable<int> bufferCapacity=null, bool includeEcho=false);

Parameters

filter CanIdFilter

ID-range or acceptance-code/mask filter.

bufferCapacity Nullable<Int32>

Bounded buffer capacity for this subscription; null uses DefaultBufferCapacity.

includeEcho Boolean

Whether this subscription also receives the local host's own transmit echoes; false by default. See the predicate overload above.

Implements Subscribe(CanIdFilter, Nullable<int>, bool)

Returns

ISubscription

CanBusService.Subscribe(Func<CanFrameEvent,bool>, Nullable<int>, bool) Method

Registers a subscription that receives every frame for which predicate returns true; a null predicate accepts all frames (FR-RAW-010).

public CanKit.Pro.RawCan.ISubscription Subscribe(System.Func<CanKit.Pro.RawCan.CanFrameEvent,bool>? predicate=null, System.Nullable<int> bufferCapacity=null, bool includeEcho=false);

Parameters

predicate Func<CanFrameEvent,Boolean>

Per-frame filter, or null to accept all frames. Runs on the bus's dispatch thread before the payload is copied, so the Frame it inspects aliases the adapter's RX lease and must not be retained beyond the call. It is never offered an echo unless includeEcho is set.

bufferCapacity Nullable<Int32>

Bounded buffer capacity for this subscription; null uses DefaultBufferCapacity. When the buffer is full the oldest buffered frame is dropped so dispatch never blocks (FR-RAW-011).

includeEcho Boolean

Whether this subscription also receives the local host's own transmit echoes. Defaults to false: a protocol layer that sees its own transmissions come back as if they were peer traffic misbehaves in ways that are tedious to diagnose, so opting in is a decision the caller makes deliberately (see the remarks below).

Implements Subscribe(Func<CanFrameEvent,bool>, Nullable<int>, bool)

Returns

ISubscription

Events

CanBusService.BackgroundExceptionOccurred Event

Raised when a caller-supplied subscription filter predicate throws during dispatch (FR-RAW-023-style fault channel). The failing frame is isolated to that subscription (delivery to the other subscriptions continues), and the exception is surfaced here instead of being silently swallowed. Invoked synchronously on the bus's dispatch thread, so handlers must return quickly and must not call back into the service.

public event EventHandler<Exception>? BackgroundExceptionOccurred;

Implements BackgroundExceptionOccurred

Event Type

EventHandler<Exception>