SORACOM AK-020の設定

AK-020をRaspberry Pi, Ubuntu などで利用しようとしたら公式に掲載されているwvdialでは安定して利用することができませんでした。
最終的にpppconfigを利用するようにしたら、ある程度安定して動作していますので、そのスクリプトです。
動作確認は、UbuntuとRaspberry Piで行っています。

2021.3.16:
LINUX 5.10では正しく動作しなくなっていたので修正しました。

## 1. パッケージのインストール

```
sudo apt-get install eject pppconfig
```

## 2. PPPの設定
### 2-1. pppconfigによる設定

```bash
pppconfig
```
実行するとこのような画面が開きます。

![pppconfig.png](https://qiita-image-store.s3.amazonaws.com/0/136933/9d1af90e-ad34-8cd6-1c96-68b57af63dd4.png)

順番に次の通り設定していきます。

| 項目 | 設定値 |
|-----|--------|
| Provider | soracom |
| DNS | Dynamic |
| authorization | PAP |
| Your ISP | sora |
| password | sora |
|modem speed | 460800 |
| Pulse/Tone | Tone |
| Phone Number | *99***1# |
| Choose Modem Config Method | No |
| Manually Select Modem Port | /dev/modem |

### 2-2. 手動で編集
最後の３行に以下の３行を追加します。
usepeerdns
persist
replacedefaultroute

```config:/etc/ppp/peers/soracom
# This optionfile was generated by pppconfig 2.3.18. 
# 
#
hide-password 
noauth
connect "/usr/sbin/chat -v -f /etc/chatscripts/soracom"
/dev/modem
460800
defaultroute
noipdefault 
user "sora"
remotename soracom
ipparam soracom

usepeerdns
persist
replacedefaultroute
```
### 2-3. ATコマンドの編集
ATコマンドが記載されているファイルを以下のように変更します。

```config:/etc/chatscripts/soracom 
# This chatfile was generated by pppconfig 2.3.18.
# Please do not delete any of the comments.  Pppconfig needs them.
# 
# ispauth PAP
# abortstring
ABORT BUSY ABORT 'NO CARRIER' ABORT VOICE ABORT 'NO DIALTONE' ABORT 'NO DIAL TONE' ABORT 'NO ANSWER' ABORT DELAYED
# modeminit
'' ATH
# ispnumber
OK AT+CFUN=1
OK ATZ
OK AT+CGDCONT=1,"IP","soracom.io"
OK-AT-OK "ATD*99***1#"
# ispconnect
CONNECT \d\c
# prelogin

# ispname
# isppassword
# postlogin

# end of pppconfig stuff
```

## 3. rules.dの登録
### 3-1. モデムの再登録
USBドングルを挿入したらCD-ROMとして認識されるUSBドングルをイジェクトして、USBシリアルデバイスとして再認識させます。

```/etc/udev/rules.d/40-ak-020.rules
ACTION=="add", ATTRS{idVendor}=="15eb", ATTRS{idProduct}=="a403", RUN+="/usr/sbin/usb_modeswitch --std-eject --default-vendor 0x15ed --default-product 0xa403 --target-product 0x15eb --target-product 0x7d03"
ACTION=="add", ATTRS{idVendor}=="15eb", ATTRS{idProduct}=="7d0e", RUN+="/sbin/modprobe usbserial vendor=0x15eb product=0x7d0e"
```
### 3-2. モデムデバイスの変更とネットワークの起動スクリプト
/dev/ttyUSBx として認識されるUSBデバイスを/dev/modemとして認識させて、ネットワークデバイスの起動を行う部分です。

```:/etc/udev/rules.d/50-soracom.rules
KERNEL=="ttyUSB*", ATTRS{../idVendor}=="15eb", ATTRS{../idProduct}=="7d0e", ATTRS{bNumEndpoints}=="03", ATTRS{bInterfaceNumber}=="02", SYMLINK+="modem",ENV{SYSTEMD_WANTS}="ifup@wwan0.service"
```

## 3-3. ネットワークの起動とPPPを結びつけます
wwan0のデバイスが挿入されるとppp接続でsoracomに接続を行います。

```/etc/network/interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback

allow-hotplug wwan0
iface wwan0 inet ppp
    provider soracom

```

# 動作確認

syslogの結果を機能別に分割しました。

## 1. USBデバイスの認識と、CD-ROMのイジェクト部分
この辺は3-1の１行目による動作です。

```shell:/var/log/syslog
Nov 20 11:36:34 MacBookAir kernel: [10630.361741] usb 1-1: new high-speed USB device number 21 using xhci_hcd
Nov 20 11:36:34 MacBookAir kernel: [10630.510912] usb 1-1: New USB device found, idVendor=15eb, idProduct=a403
Nov 20 11:36:34 MacBookAir kernel: [10630.510918] usb 1-1: New USB device strings: Mfr=2, Product=3, SerialNumber=4
Nov 20 11:36:34 MacBookAir kernel: [10630.510923] usb 1-1: Product: AK-020 
Nov 20 11:36:34 MacBookAir kernel: [10630.510926] usb 1-1: Manufacturer: ABIT Corporation
Nov 20 11:36:34 MacBookAir kernel: [10630.510930] usb 1-1: SerialNumber: 536957701071820
Nov 20 11:36:34 MacBookAir kernel: [10630.511752] usb-storage 1-1:1.0: USB Mass Storage device detected
Nov 20 11:36:34 MacBookAir kernel: [10630.511998] scsi host2: usb-storage 1-1:1.0
Nov 20 11:36:34 MacBookAir mtp-probe: checking bus 1, device 21: "/sys/devices/pci0000:00/0000:00:14.0/usb1/1-1"
Nov 20 11:36:34 MacBookAir mtp-probe: bus: 1, device: 21 was not an MTP device
Nov 20 11:36:34 MacBookAir systemd-udevd[9461]: Process '/usr/bin/eject /dev/sr0' failed with exit code 1.
Nov 20 11:36:35 MacBookAir kernel: [10631.530227] scsi 2:0:0:0: CD-ROM            HSPA_USB SCSI CD-ROM       622 PQ: 0 ANSI: 0 CCS
Nov 20 11:36:35 MacBookAir kernel: [10631.531402] sr 2:0:0:0: [sr0] scsi3-mmc drive: 0x/0x caddy
Nov 20 11:36:35 MacBookAir kernel: [10631.531770] sr 2:0:0:0: Attached scsi CD-ROM sr0
Nov 20 11:36:35 MacBookAir kernel: [10631.532004] sr 2:0:0:0: Attached scsi generic sg2 type 5
Nov 20 11:36:36 MacBookAir kernel: [10632.369980] usb 1-1: reset high-speed USB device number 21 using xhci_hcd
Nov 20 11:36:36 MacBookAir kernel: [10632.517754] usb 1-1: device firmware changed
Nov 20 11:36:36 MacBookAir kernel: [10632.517929] usb 1-1: USB disconnect, device number 21
Nov 20 11:36:34 MacBookAir systemd-udevd[9461]: message repeated 3 times: [ Process '/usr/bin/eject /dev/sr0' failed with exit code 1.]
Nov 20 11:36:36 MacBookAir systemd-udevd[9473]: Process '/usr/bin/eject /dev/sr0' failed with exit code 1.
Nov 20 11:36:36 MacBookAir systemd-udevd[9472]: Process '/usr/bin/eject /dev/sr0' failed with exit code 1.
Nov 20 11:36:36 MacBookAir systemd-udevd[9476]: Process '/usr/bin/eject /dev/sr0' failed with exit code 1.
Nov 20 11:36:36 MacBookAir systemd-udevd[9477]: Process '/usr/bin/eject /dev/sr0' failed with exit code 1.
Nov 20 11:36:36 MacBookAir kernel: [10632.705640] usb 1-1: new high-speed USB device number 22 using xhci_hcd
```
## usbserialデバイスとしてUSBデバイスを再登録

ここも3-1の２行目のスクリプトです。

```shell:/var/log/syslog
Nov 20 11:36:37 MacBookAir kernel: [10632.857444] usb 1-1: New USB device found, idVendor=15eb, idProduct=7d0e
Nov 20 11:36:37 MacBookAir kernel: [10632.857450] usb 1-1: New USB device strings: Mfr=9, Product=10, SerialNumber=0
Nov 20 11:36:37 MacBookAir kernel: [10632.857455] usb 1-1: Product: AK-020 
Nov 20 11:36:37 MacBookAir kernel: [10632.857459] usb 1-1: Manufacturer: ABIT Corporation
Nov 20 11:36:37 MacBookAir kernel: [10632.859330] usbserial_generic 1-1:1.0: The "generic" usb-serial driver is only for testing and one-off prototypes.
Nov 20 11:36:37 MacBookAir kernel: [10632.859337] usbserial_generic 1-1:1.0: Tell linux-usb@vger.kernel.org to add your device to a proper driver.
Nov 20 11:36:37 MacBookAir kernel: [10632.859341] usbserial_generic 1-1:1.0: device has no bulk endpoints
Nov 20 11:36:37 MacBookAir kernel: [10632.882760] cdc_mbim 1-1:1.0: cdc-wdm2: USB WDM device
Nov 20 11:36:37 MacBookAir kernel: [10632.883245] cdc_mbim 1-1:1.0 wwan0: register 'cdc_mbim' at usb-0000:00:14.0-1, CDC MBIM, 2a:41:3c:1c:53:f1
Nov 20 11:36:37 MacBookAir kernel: [10632.884206] usbserial_generic 1-1:1.2: The "generic" usb-serial driver is only for testing and one-off prototypes.
Nov 20 11:36:37 MacBookAir kernel: [10632.884210] usbserial_generic 1-1:1.2: Tell linux-usb@vger.kernel.org to add your device to a proper driver.
Nov 20 11:36:37 MacBookAir kernel: [10632.884215] usbserial_generic 1-1:1.2: generic converter detected
Nov 20 11:36:37 MacBookAir kernel: [10632.884405] usb 1-1: generic converter now attached to ttyUSB0
Nov 20 11:36:37 MacBookAir kernel: [10632.885099] usbserial_generic 1-1:1.3: The "generic" usb-serial driver is only for testing and one-off prototypes.
Nov 20 11:36:37 MacBookAir kernel: [10632.885102] usbserial_generic 1-1:1.3: Tell linux-usb@vger.kernel.org to add your device to a proper driver.
Nov 20 11:36:37 MacBookAir kernel: [10632.885106] usbserial_generic 1-1:1.3: generic converter detected
Nov 20 11:36:37 MacBookAir kernel: [10632.885252] usb 1-1: generic converter now attached to ttyUSB1
Nov 20 11:36:37 MacBookAir kernel: [10632.886182] usbserial_generic 1-1:1.4: The "generic" usb-serial driver is only for testing and one-off prototypes.
Nov 20 11:36:37 MacBookAir kernel: [10632.886188] usbserial_generic 1-1:1.4: Tell linux-usb@vger.kernel.org to add your device to a proper driver.
Nov 20 11:36:37 MacBookAir kernel: [10632.886194] usbserial_generic 1-1:1.4: generic converter detected
Nov 20 11:36:37 MacBookAir kernel: [10632.886635] usb 1-1: generic converter now attached to ttyUSB2
Nov 20 11:36:37 MacBookAir kernel: [10632.887481] usbserial_generic 1-1:1.5: The "generic" usb-serial driver is only for testing and one-off prototypes.
Nov 20 11:36:37 MacBookAir kernel: [10632.887487] usbserial_generic 1-1:1.5: Tell linux-usb@vger.kernel.org to add your device to a proper driver.
Nov 20 11:36:37 MacBookAir kernel: [10632.887493] usbserial_generic 1-1:1.5: generic converter detected
Nov 20 11:36:37 MacBookAir kernel: [10632.887887] usb 1-1: generic converter now attached to ttyUSB3
Nov 20 11:36:37 MacBookAir kernel: [10632.888604] usb-storage 1-1:1.6: USB Mass Storage device detected
Nov 20 11:36:37 MacBookAir kernel: [10632.890883] scsi host2: usb-storage 1-1:1.6
Nov 20 11:36:37 MacBookAir systemd-udevd[9473]: Process '/usr/bin/eject /dev/sr0' failed with exit code 1.
Nov 20 11:36:37 MacBookAir mtp-probe: checking bus 1, device 22: "/sys/devices/pci0000:00/0000:00:14.0/usb1/1-1"
Nov 20 11:36:37 MacBookAir mtp-probe: bus: 1, device: 22 was not an MTP device
```
## wwan0の起動
この辺りから3-2のスクリプトになります。

```/var/log/syslog
Nov 20 11:36:37 MacBookAir kernel: [10632.967249] cdc_mbim 1-1:1.0 wwp0s20u1: renamed from wwan0
Nov 20 11:36:37 MacBookAir NetworkManager[810]: <info>  [1542681397.2765] devices added (path: /sys/devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/net/wwp0s20u1, iface: wwp0s20u1)
Nov 20 11:36:37 MacBookAir NetworkManager[810]: <info>  [1542681397.2765] device added (path: /sys/devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/net/wwp0s20u1, iface: wwp0s20u1): no ifupdown configuration found.
Nov 20 11:36:38 MacBookAir kernel: [10633.899131] scsi 2:0:0:0: Direct-Access     HSPA_USB SCSI CD-ROM       622 PQ: 0 ANSI: 0 CCS
Nov 20 11:36:38 MacBookAir kernel: [10633.899975] sd 2:0:0:0: Attached scsi generic sg2 type 0
Nov 20 11:36:38 MacBookAir kernel: [10633.917894] sd 2:0:0:0: [sdc] 0 512-byte logical blocks: (0 B/0 B)
Nov 20 11:36:38 MacBookAir kernel: [10633.917897] sd 2:0:0:0: [sdc] 0-byte physical blocks
Nov 20 11:36:38 MacBookAir kernel: [10633.918245] sd 2:0:0:0: [sdc] Test WP failed, assume Write Enabled
Nov 20 11:36:38 MacBookAir kernel: [10633.918606] sd 2:0:0:0: [sdc] Asking for cache data failed
Nov 20 11:36:38 MacBookAir kernel: [10633.918612] sd 2:0:0:0: [sdc] Assuming drive cache: write through
Nov 20 11:36:38 MacBookAir kernel: [10633.919091] sd 2:0:0:0: [sdc] Attached SCSI removable disk
```
## wwan0としてネットワークを起動する

3-3.でwwan0とpppconfigの接続部分がうまくできていないと、この辺りで止まります。
また、初期起動時はpppを自動で起動してくれない可能性があるので、
その時はを実行してみてください。

```
sudo pon soracom
```

そして、ここから下は「2-3」のスクリプトが動いています。
最後にIPアドレスが表示されたら、インターネットに接続されたということです。

```shell:/var/log/syslog
Nov 20 11:36:51 MacBookAir pppd[9582]: pppd 2.4.7 started by root, uid 0
Nov 20 11:36:52 MacBookAir chat[9584]: abort on (BUSY)
Nov 20 11:36:52 MacBookAir chat[9584]: abort on (NO CARRIER)
Nov 20 11:36:52 MacBookAir chat[9584]: abort on (VOICE)
Nov 20 11:36:52 MacBookAir chat[9584]: abort on (NO DIALTONE)
Nov 20 11:36:52 MacBookAir chat[9584]: abort on (NO DIAL TONE)
Nov 20 11:36:52 MacBookAir chat[9584]: abort on (NO ANSWER)
Nov 20 11:36:52 MacBookAir chat[9584]: abort on (DELAYED)
Nov 20 11:36:52 MacBookAir chat[9584]: send (ATH^M)
Nov 20 11:36:52 MacBookAir chat[9584]: expect (OK)
Nov 20 11:36:52 MacBookAir chat[9584]: ATH^M^M
Nov 20 11:36:52 MacBookAir chat[9584]: OK
Nov 20 11:36:52 MacBookAir chat[9584]:  -- got it
Nov 20 11:36:52 MacBookAir chat[9584]: send (AT+CFUN=1^M)
Nov 20 11:36:52 MacBookAir chat[9584]: expect (OK)
Nov 20 11:36:52 MacBookAir chat[9584]: ^M
Nov 20 11:36:52 MacBookAir chat[9584]: AT+CFUN=1^M^M
Nov 20 11:36:52 MacBookAir chat[9584]: OK
Nov 20 11:36:52 MacBookAir chat[9584]:  -- got it
Nov 20 11:36:52 MacBookAir chat[9584]: send (ATZ^M)
Nov 20 11:36:52 MacBookAir chat[9584]: expect (OK)
Nov 20 11:36:52 MacBookAir chat[9584]: ^M
Nov 20 11:36:52 MacBookAir chat[9584]: ATZ^M^M
Nov 20 11:36:52 MacBookAir chat[9584]: OK
Nov 20 11:36:52 MacBookAir chat[9584]:  -- got it
Nov 20 11:36:52 MacBookAir chat[9584]: send (AT+CGDCONT=1,"IP","soracom.io"^M)
Nov 20 11:36:52 MacBookAir chat[9584]: expect (OK)
Nov 20 11:36:52 MacBookAir chat[9584]: ^M
Nov 20 11:36:52 MacBookAir chat[9584]: AT+CGDCONT=1,"IP","soracom.io"^M^M
Nov 20 11:36:52 MacBookAir chat[9584]: OK
Nov 20 11:36:52 MacBookAir chat[9584]:  -- got it
Nov 20 11:36:52 MacBookAir chat[9584]: send (ATD*99#^M)
Nov 20 11:36:52 MacBookAir chat[9584]: expect (CONNECT)
Nov 20 11:36:52 MacBookAir chat[9584]: ^M
Nov 20 11:36:52 MacBookAir chat[9584]: ATD*99#^M^M
Nov 20 11:36:52 MacBookAir chat[9584]: OK^M
Nov 20 11:36:52 MacBookAir chat[9584]: ^M
Nov 20 11:36:52 MacBookAir chat[9584]: CONNECT
Nov 20 11:36:52 MacBookAir chat[9584]:  -- got it
Nov 20 11:36:52 MacBookAir chat[9584]: send (\d)
Nov 20 11:36:53 MacBookAir pppd[9582]: Serial connection established.
Nov 20 11:36:53 MacBookAir pppd[9582]: Using interface ppp0
Nov 20 11:36:53 MacBookAir pppd[9582]: Connect: ppp0 <--> /dev/modem
Nov 20 11:36:53 MacBookAir NetworkManager[810]: nm_device_get_device_type: assertion 'NM_IS_DEVICE (self)' failed
Nov 20 11:36:53 MacBookAir NetworkManager[810]: <info>  [1542681413.7705] manager: (ppp0): new Generic device (/org/freedesktop/NetworkManager/Devices/4)
Nov 20 11:36:53 MacBookAir NetworkManager[810]: <info>  [1542681413.7814] devices added (path: /sys/devices/virtual/net/ppp0, iface: ppp0)
Nov 20 11:36:53 MacBookAir NetworkManager[810]: <info>  [1542681413.7815] device added (path: /sys/devices/virtual/net/ppp0, iface: ppp0): no ifupdown configuration found.
Nov 20 11:36:56 MacBookAir pppd[9582]: PAP authentication succeeded
Nov 20 11:36:59 MacBookAir pppd[9582]: Could not determine remote IP address: defaulting to xx.xx.xx.xx
Nov 20 11:36:59 MacBookAir pppd[9582]: local  IP address xx.xx.xx.xx
Nov 20 11:36:59 MacBookAir pppd[9582]: remote IP address xx.xx.xx.xx
Nov 20 11:36:59 MacBookAir pppd[9582]: primary   DNS address xx.xx.xx.xx
Nov 20 11:36:59 MacBookAir pppd[9582]: secondary DNS address xx.xx.xx.xx
Nov 20 11:37:01 MacBookAir ntpd[1046]: Listen normally on 8 ppp0 10.182.142.21:123
Nov 20 11:37:01 MacBookAir ntpd[1046]: new interface(s) found: waking up resolver
```