diff --git a/host/achiles.nix b/host/achiles.nix index 6fa34fa..435a706 100644 --- a/host/achiles.nix +++ b/host/achiles.nix @@ -48,7 +48,7 @@ in services.wwan = { enable = true; apn = "gprs.swisscom.ch"; - networkInterface = "wwp0s19u1u3i12"; + networkInterface = routerConfig.wanInterface; }; # Use the GRUB 2 boot loader. diff --git a/role/networking/wwan.nix b/role/networking/wwan.nix index e015053..3cf31cd 100644 --- a/role/networking/wwan.nix +++ b/role/networking/wwan.nix @@ -1,16 +1,29 @@ # Based on https://github.com/jgillich/nixos/blob/master/services/ppp.nix # Tipps and tricks under https://www.hackster.io/munoz0raul/how-to-use-gsm-3g-4g-in-embedded-linux-systems-9047cf#toc-configuring-the-ppp-files-5 -# TODO: http://www.embeddedpi.com/documentation/3g-4g-modems/raspberry-pi-sierra-wireless-mc7455-modem-raw-ip-qmi-interface-setup { config, lib, pkgs, ... }: with lib; let cfg = config.services.wwan; + mbim-ip-configured = pkgs.writeScriptBin "mbim-ip-configured" ('' #!${pkgs.stdenv.shell} MBIM_INTERFACE=${cfg.mbimInterface} '' + (readFile ./wwan/mbim-ip.bash)); + + mbim-check-status = pkgs.writeScriptBin "mbim-check-status" '' + #!${pkgs.stdenv.shell} + if ! systemctl is-active --quiet wwan.service; then + # Skip check if wwan is not running + exit 0 + fi + + if ! mbim-network ${cfg.mbimInterface} status | grep -q "Status: activated"; then + echo "WWAN device is currently in disabled state, triggering restart." + systemctl restart wwan.service + fi + ''; in { options.services.wwan = { @@ -70,17 +83,36 @@ in }; config = mkIf cfg.enable { - systemd.services."wwan" = { - description = "WWAN connectivity"; - wantedBy = [ "network.target" ]; - bindsTo = [ "network-addresses-${cfg.networkInterface}.service" ]; - path = [ pkgs.libmbim pkgs.iproute ]; + systemd.services = { + "wwan" = { + description = "WWAN connectivity"; + wantedBy = [ "network.target" ]; + bindsTo = [ "network-addresses-${cfg.networkInterface}.service" ]; + path = with pkgs; [ libmbim iproute ]; - serviceConfig = { - ExecStart = "${mbim-ip-configured}/bin/mbim-ip-configured start ${cfg.networkInterface}"; - ExecStop = "${mbim-ip-configured}/bin/mbim-ip-configured stop ${cfg.networkInterface}"; + serviceConfig = { + ExecStart = "${mbim-ip-configured}/bin/mbim-ip-configured start ${cfg.networkInterface}"; + ExecStop = "${mbim-ip-configured}/bin/mbim-ip-configured stop ${cfg.networkInterface}"; - RemainAfterExit = true; + RemainAfterExit = true; + }; + }; + "wwan-check" = { + description = "Check WWAN connectivity and restart if disabled"; + path = with pkgs; [ libmbim ]; + serviceConfig = { + Type = "oneshot"; + ExecStart = "${mbim-check-status}/bin/mbim-check-status"; + }; + }; + }; + systemd.timers."wwan-check" = { + description = "WWAN connectivity check"; + wantedBy = [ "timers.target" ]; + timerConfig = { + Unit = "wwan-check"; + OnBootSec="2m"; + OnUnitActiveSec="1m"; }; };