liminix

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

huawei-e3372.nix (2235B)


      1 {
      2   liminix,
      3   usb-modeswitch,
      4   ppp,
      5   lib,
      6   svc,
      7   uevent-watch,
      8 }:
      9 {
     10   apn,
     11   username,
     12   password,
     13   authType,
     14 }:
     15 let
     16   inherit (liminix.services) oneshot;
     17   authTypeNum = if authType == "pap" then "1" else "2";
     18   chat = lib.escapeShellArgs [
     19     # Your usb modem thing might present as a tty that you run PPP
     20     # over, or as a network device ("ndis" or "ncm"). The latter
     21     # kind is to be preferred, at least in principle, because it's
     22     # faster.  This initialization sequence works for the Huawei
     23     # E3372, and took much swearing: the error messages are *awful*
     24     ""
     25     "AT"
     26     "OK"
     27     "ATZ"
     28     # create PDP context
     29     "OK"
     30     "AT+CGDCONT=1,\"IP\",\"${apn}\""
     31     # activate PDP context
     32     "OK"
     33     "AT+CGACT=1,1"
     34     # setup username and password per requirements of sim provider.
     35     # (caret is special to chat, so needs escaping in AT commands)
     36     "OK"
     37     "AT\\^AUTHDATA=1,${authTypeNum},\"\",\"${password}\",\"${username}\""
     38     # start the thing (I am choosing to read this as "NDIS DialUP")
     39     "OK"
     40     "AT\\^NDISDUP=1,1"
     41     "OK"
     42   ];
     43   modeswitch = oneshot rec {
     44     name = "modem-modeswitch";
     45     controller = (
     46       svc.uevent-rule.build {
     47         serviceName = name;
     48         terms = {
     49           devtype = "usb_device";
     50           product = "12d1/14fe/102";
     51         };
     52       }
     53     );
     54     up = ''
     55       ${usb-modeswitch}/bin/usb_modeswitch -v 12d1 -p 14fe --huawei-new-mode
     56     '';
     57   };
     58   atz = oneshot rec {
     59     name = "modem-atz";
     60     # atz does not depend on modeswitch because modeswitch service
     61     # is only running when the stick is in the wrong mode
     62     dependencies = [ modeswitch.controller ];
     63     buildInputs = [ modeswitch ];
     64     controller = (
     65       svc.uevent-rule.build {
     66         serviceName = name;
     67         terms = {
     68           subsystem = "tty";
     69           attrs = {
     70             idVendor = "12d1";
     71             idProduct = "1506";
     72           };
     73         };
     74         symlink = "/dev/modem";
     75       }
     76     );
     77     up = ''
     78       ls -l /dev/modem
     79       test -L /dev/modem || exit 1
     80       ${ppp}/bin/chat -s -v ${chat}  0<>/dev/modem 1>&0
     81     '';
     82     down = "${ppp}/bin/chat -v '' ATZ OK  0<>/dev/modem 1>&0";
     83   };
     84 
     85 in
     86 svc.network.link.build {
     87   ifname = "wwan0";
     88   dependencies = [ atz ];
     89 }