# charlie0129/batt architecture

> Architecture diagram of the GitHub repository charlie0129/batt, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/charlie0129/batt
- Repository: https://github.com/charlie0129/batt

## Overview

##### Main System Components

The system should be represented as a client-server architecture with the following key components:

a) Client Component:
- Command-line interface
- Configuration handling
- Client-side communication

b) Daemon Component:
- Battery monitoring service
- SMC (System Management Controller) interface
- Sleep/Wake handler
- Configuration management

##### Key Architectural Elements

a) IPC Communication:
- Unix Domain Socket connection between client and daemon
- Command message passing system

b) Hardware Integration Layer:
- SMC interface (written in C)
- IOKit integration for sleep/wake notifications

**[User Interface Layer]**

- ▼
**[Client Component]──────Unix Socket─────▶[Daemon Component]**

- ▼
- [Hardware Interface Layer]

- ▼
- [MacBook Hardware]

##### Component Details

a) Client Component (represented in blue):
- Command parser (cmd.go)
- Configuration handler (conf.go)
- Client communication (client.go)

b) Daemon Component (represented in green):
- Main daemon service (daemon.go)
- Event loop handler (loop.go)
- Request handler (handler.go)

c) Hardware Interface Layer (represented in orange):
- SMC package (smc/*.go)
- C-based hooks (hook.c, hook.h)
- Battery status monitoring
- Power adapter control
- MagSafe LED control

##### Data Flow Indicators

- Command flow from client to daemon
- Status updates from daemon to client
- Hardware interaction flow
- Configuration data flow

##### Implementation Details

- Go-based main components
- C-based hardware interfaces
- Configuration storage
- Log management (/tmp/batt.log)

##### Additional Notes

Annotations for:
- Security boundaries (root vs non-root access)
- Persistent storage locations
- Critical system interfaces
- Error handling paths

This system design should effectively communicate both the high-level architecture and the critical low-level interactions that make this battery management tool work.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph UI["User Interface Layer"]
        CLI["Command Line Interface"]
    end

    subgraph Client["Client Component"]
        CmdParser["Command Parser"]:::client
        ConfHandler["Configuration Handler"]:::client
        ClientComm["Client Communication"]:::client
        InstallHandler["Installation Handler"]:::client
        VersionMgr["Version Manager"]:::client
    end

    subgraph Daemon["Daemon Component"]
        DaemonSvc["Main Daemon Service"]:::daemon
        EventLoop["Event Loop Handler"]:::daemon
        ReqHandler["Request Handler"]:::daemon
        SleepWake["Sleep/Wake Handler"]:::daemon
    end

    subgraph Hardware["Hardware Interface Layer"]
        SMCInterface["SMC Interface"]:::hardware
        BatteryMgr["Battery Management"]:::hardware
        PowerAdapter["Power Adapter Control"]:::hardware
        MagSafeLED["MagSafe LED Control"]:::hardware
        ACPower["AC Power Management"]:::hardware
        HWHooks["Hardware Hooks"]:::hardware
    end

    subgraph System["MacBook Hardware"]
        Battery[("Battery")]:::system
        SMC[("SMC")]:::system
        PowerMgmt[("Power Management")]:::system
    end

    %% Connections
    CLI --> CmdParser
    CmdParser --> ClientComm
    ConfHandler --> ClientComm
    ClientComm <--> |"Unix Socket"| DaemonSvc
    DaemonSvc --> EventLoop
    EventLoop --> ReqHandler
    ReqHandler --> SMCInterface
    SleepWake --> EventLoop
    
    SMCInterface --> BatteryMgr
    SMCInterface --> PowerAdapter
    SMCInterface --> MagSafeLED
    SMCInterface --> ACPower
    HWHooks --> SMC
    
    BatteryMgr --> Battery
    PowerAdapter --> PowerMgmt
    ACPower --> PowerMgmt
    
    %% Click Events
    click CmdParser "https://github.com/charlie0129/batt/blob/master/cmd.go"
    click ConfHandler "https://github.com/charlie0129/batt/blob/master/conf.go"
    click ClientComm "https://github.com/charlie0129/batt/blob/master/client.go"
    click InstallHandler "https://github.com/charlie0129/batt/blob/master/install.go"
    click VersionMgr "https://github.com/charlie0129/batt/blob/master/pkg/version/version.go"
    click DaemonSvc "https://github.com/charlie0129/batt/blob/master/daemon.go"
    click EventLoop "https://github.com/charlie0129/batt/blob/master/loop.go"
    click ReqHandler "https://github.com/charlie0129/batt/blob/master/handler.go"
    click SleepWake "https://github.com/charlie0129/batt/blob/master/sleepcallback.go"
    click SMCInterface "https://github.com/charlie0129/batt/blob/master/smc/smc.go"
    click BatteryMgr "https://github.com/charlie0129/batt/blob/master/smc/battery.go"
    click PowerAdapter "https://github.com/charlie0129/batt/blob/master/smc/adapter.go"
    click MagSafeLED "https://github.com/charlie0129/batt/blob/master/smc/magsafe.go"
    click ACPower "https://github.com/charlie0129/batt/blob/master/smc/acpower.go"
    click HWHooks "https://github.com/charlie0129/batt/blob/master/hook.c"

    %% Styles
    classDef client fill:#a8d1f0,stroke:#333,stroke-width:2px;
    classDef daemon fill:#90EE90,stroke:#333,stroke-width:2px;
    classDef hardware fill:#FFA500,stroke:#333,stroke-width:2px;
    classDef system fill:#D3D3D3,stroke:#333,stroke-width:2px;

    %% Legend
    subgraph Legend
        Client_L["Client Component"]:::client
        Daemon_L["Daemon Component"]:::daemon
        Hardware_L["Hardware Interface"]:::hardware
        System_L["System Component"]:::system
    end
```
