aboutsummaryrefslogtreecommitdiff
path: root/nixos/modules/turris-omnialeds.nix
blob: 8063eb6fdf3930a452c036a3e17ed68b01ee758f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.turris.omnialeds;

  ledConfig = {name, trigger ? "none", netdevName ? null, color}: {
    enabled = mkOption {
      type = types.bool;
      default = true;
      description = "If LED should be enabled at all";
    };
    brightness = mkOption {
      type = types.ints.u8;
      default = 255;
      description = "Set brightness intensity";
    };
    color = let 
      clr = c: mkOption {
        type = types.ints.u8;
        default = color.${c};
        description = "Set intensity for the color ${c}";
      };
    in {
      red = clr "red";
      green = clr "green";
      blue = clr "blue";
    };
    trigger = mkOption {
      type = types.str;
      default = trigger;
      description = "Trigger for the LED";
    };
    # netdev trigger
    netdevName = mkOption {
      type = with types; nullOr str;
      default = netdevName;
      description = ''
        Name of the network device to trigger when trigger is set to `netdev`
      '';
    };
  };
  lanConfig = i: ledConfig {
    name = "lan${toString i}";
    trigger = "netdev";
    netdevName = "lan${toString i}";
    color = {
      red = 255; green = 255; blue = 0;
    };
  };
  wlanConfig = i: ledConfig {
    name = "wlan${toString i}";
    color = {
      red = 0; green = 255; blue = 255;
    };
  };
  indicatorConfig = i: ledConfig {
    name = "indicator${toString i}";
    color = {
      red = 0; green = 255; blue = 255;
    };
  };

  ledSetup = sysname: lcfg: ''
    echo ${toString lcfg.brightness} > /sys/class/leds/rgb:${sysname}/brightness
    echo ${toString lcfg.color.red} ${toString lcfg.color.green} ${toString lcfg.color.blue} > /sys/class/leds/rgb:${sysname}/multi_intensity
    echo ${lcfg.trigger} > /sys/class/leds/rgb:${sysname}/trigger
    ${optionalString (lcfg.trigger == "netdev") "echo '${lcfg.netdevName}' > /sys/class/leds/rgb:${sysname}/device_name"}
  '';

in {

  options = {
    turris.omnialeds = {
      enabled = mkOption {
        type = types.bool;
        default = true;
        description = "If Omnia LEDs setup should be enabled or not.";
      };
      brightness = mkOption {
        type = with types; nullOr (ints.between 0 100);
        default = null;
        description = "Global brightness (overrides brightness set by the front button)";
      };
      power = ledConfig {
        name = "power";
        trigger = "heartbeat";
        color = {
          red = 0; green = 255; blue = 0;
        };
      };
      wan = ledConfig {
        name = "wan";
        trigger = "netdev";
        netdevName = "eth2";
        color = {
          red = 0; green = 255; blue = 0;
        };
      };
      lan0 = lanConfig 0;
      lan1 = lanConfig 1;
      lan2 = lanConfig 2;
      lan3 = lanConfig 3;
      lan4 = lanConfig 4;
      wlan1 = wlanConfig 1;
      wlan2 = wlanConfig 2;
      wlan3 = wlanConfig 3;
      indicator1 = indicatorConfig 1;
      indicator2 = indicatorConfig 2;
      extraCommands = mkOption {
        type = types.lines;
        default = "";
        description = "Extra commands executed to setup LEDs";
      };
    };
  };

  config = mkIf (config.turris.board == "omnia" && cfg.enabled) {

    # TODO modprobe triggers only if required
    boot.kernelModules = [
      "ledtrig_tty" "ledtrig_activity" "ledtrig_pattern" "ledtrig_netdev"
      "ledtrig_usbport"
    ];

    systemd.services."omnia-leds" = {
      script = ''
        ${optionalString (cfg.brightness != null)
          "echo ${toString cfg.brightness} > /sys/devices/platform/soc/soc:internal-regs/f1011000.i2c/i2c-0/i2c-1/1-002b/brightness"}
        ${ledSetup "power" cfg.power}
        ${ledSetup "wan" cfg.wan}
        ${ledSetup "lan-0" cfg.lan0}
        ${ledSetup "lan-1" cfg.lan1}
        ${ledSetup "lan-2" cfg.lan2}
        ${ledSetup "lan-3" cfg.lan3}
        ${ledSetup "lan-4" cfg.lan4}
        ${ledSetup "wlan-1" cfg.wlan1}
        ${ledSetup "wlan-2" cfg.wlan2}
        ${ledSetup "wlan-3" cfg.wlan3}
        ${ledSetup "indicator-1" cfg.indicator1}
        ${ledSetup "indicator-2" cfg.indicator2}

        # Extra commands
        ${cfg.extraCommands}
      '';
      wantedBy = [ "multi-user.target" ];
    };

  };
}