Skip to content

Architecture

Latisha. edited this page Dec 27, 2024 · 1 revision

Architecture

System Components

LibDC-Swift is organized into three main layers:

1. Swift Layer (LibDCSwift)

Core Swift components providing high-level APIs:

graph TB
    A[BLEManager] --> B[CoreBluetoothManager]
    C[DiveLogRetriever] --> D[DiveDataViewModel]
    E[GenericParser] --> F[DiveData Models]
    G[DeviceConfiguration] --> H[Device Management]
Loading

Key Components:

  • BLEManager: Bluetooth operations and device management
  • DiveLogRetriever: Coordinate dive log downloads
  • GenericParser: Parse dive data into Swift models
  • DeviceConfiguration: Handle device setup and identification

2. Bridge Layer (LibDCBridge)

Objective-C/C bridge connecting Swift and libdivecomputer:

graph LR
    A[BLEBridge.m] --> B[configuredc.c]
    B --> C[libdivecomputer]
    D[Swift] --> A
Loading

Components:

  • BLEBridge: Objective-C interface for BLE operations
  • configuredc: C implementation of device protocols
  • Bridge headers for Swift/C interop

3. Core Layer (Clibdivecomputer)

Native C library integration:

graph TB
    A[libdivecomputer] --> B[Device Communication]
    A --> C[Data Parsing]
    A --> D[Protocol Handling]
Loading

Data Flow

Device Discovery and Connection

sequenceDiagram
    participant App
    participant BLEManager
    participant Bridge
    participant Device
    
    App->>BLEManager: startScanning()
    BLEManager->>Device: Discover
    Device-->>BLEManager: Found
    App->>BLEManager: connect()
    BLEManager->>Bridge: openBLEDevice()
    Bridge->>Device: Establish Connection
    Device-->>App: Connected
Loading

Dive Log Retrieval

sequenceDiagram
    participant App
    participant DiveLogRetriever
    participant Parser
    participant Device
    
    App->>DiveLogRetriever: retrieveDiveLogs()
    DiveLogRetriever->>Device: Request Logs
    Device-->>DiveLogRetriever: Raw Data
    DiveLogRetriever->>Parser: Parse Data
    Parser-->>App: DiveData Models
Loading

Class Hierarchy

classDiagram
    class CoreBluetoothManager {
        +shared: CoreBluetoothManager
        +startScanning()
        +stopScanning()
        +connect()
    }
    
    class DiveLogRetriever {
        +retrieveDiveLogs()
        -handleCallback()
        -processData()
    }
    
    class GenericParser {
        +parseDiveData()
        -parseProfile()
        -parseEvents()
    }
    
    class DiveData {
        +number: Int
        +datetime: Date
        +profile: [DiveProfilePoint]
    }
    
    CoreBluetoothManager --> DiveLogRetriever
    DiveLogRetriever --> GenericParser
    GenericParser --> DiveData
Loading

Dependencies

External Dependencies

  • libdivecomputer: Core dive computer communication
  • CoreBluetooth: iOS/macOS Bluetooth functionality
  • Swift Standard Library: Foundation, Combine

Internal Module Dependencies

graph TD
    A[LibDCSwift] --> B[LibDCBridge]
    B --> C[Clibdivecomputer]
    A --> D[CoreBluetooth]
    A --> E[Combine]
Loading

Memory Management

Swift/C Bridging

  • Proper memory management between Swift and C
  • Manual memory management in C layer
  • ARC in Swift layer
  • Bridge cleanup and deallocation

Resource Handling

// Example of proper resource management
class DeviceHandler {
    private var devicePtr: UnsafeMutablePointer<device_data_t>?
    
    deinit {
        if let ptr = devicePtr {
            ptr.deallocate()
        }
    }
}

Error Handling

Error Propagation

graph TD
    A[C Error] --> B[Bridge Layer]
    B --> C[Swift Error]
    C --> D[User Interface]
Loading

Error Types

  • Device Communication Errors
  • Parse Errors
  • BLE Connection Errors
  • Resource Errors

Thread Safety

Concurrent Operations

  • BLE operations on dedicated queue
  • Parse operations on background queue
  • UI updates on main queue
  • Thread-safe data structures

Configuration System

Device Configuration

struct DeviceConfig {
    let family: DeviceFamily
    let model: UInt32
    let capabilities: DeviceCapabilities
}

Runtime Configuration

  • Logging levels
  • Timeout values
  • Retry policies
  • Buffer sizes

Performance Considerations

Memory Usage

  • Efficient buffer management
  • Proper resource cleanup
  • Lazy loading where appropriate

Processing Efficiency

  • Batch processing of dive data
  • Incremental updates using fingerprints
  • Optimized parsing algorithms