$Date: 2017/12/18 07:19:06 $
内蔵無線WAN機能を使って網へ自動接続するためのサンプル Lua スクリプトです。
APN 設定リストに利用する可能性のある SIM カードの接続先設定情報を登録しておけば、
SIM カードを挿入してルーターを起動することによりリストの設定を順番に試して自動的に網に接続されます。
以降の例ではUSBメモリに上記ファイルを保存した場合について記載します。
microSDカードに保存した場合は config.txt と auto_wwan.lua 中のファイルパスが記載されている箇所を変更するだけで利用可能です。
また、ルーターの内蔵フラッシュROM (RTFS) に config.txt の内容を save し、その他のファイルを RTFS の
任意のパスにコピーすることでも利用可能です。この場合も config.txt と auto_wwan.lua 中のファイルパスを
上記のファイルをコピーしたパスに変更してください。
NTTドコモ及びそのMVNO事業者以外のSIMカードを利用するには、NVR700W のファームウェアを Rev.15.00.10 以降にし、 内蔵無線WANモジュールファームウェアの更新が必要になります。
schedule at 1 startup * lua usb1:/auto_wwan.lua console character en.ascii console info on ip route default gateway pdp wan1 ip lan1 address 192.168.100.1/24 ip wan1 address pdp ip wan1 nat descriptor 1 wan1 bind wwan 1 wwan select 1 wwan enable 1 nat descriptor type 1 masquerade nat descriptor address outer 1 primary syslog debug on dhcp service server dhcp server rfc2131 compliant except remain-silent dhcp scope 1 192.168.100.2-192.168.100.191/24 dns server pdp wan1 wwan-module syslog on
先頭の schedule コマンドで、起動時に Lua スクリプトを実行しています。 その他の部分は全ての SIM カードに共通の設定です。
-- ************************************* -- APN関連コマンド設定関数 -- ************************************* function SetApn(op, ap, id, pw, au, mnc ) -- =============================== -- ルータコンフィグの書き換え -- =============================== print("# --- Trying ", op," ---") rtn, str = rt.command("wwan select 1") if (not rtn) then print(str) print("*** Command Error ***") print(" Terminating Lua script...") os.exit(0) end cmd = string.format("wwan access-point name %s", ap) rtn, str = rt.command(cmd) if (not rtn) then print(str) print("*** Command Error ***") print(" Terminating Lua script...") os.exit(0) end cmd = string.format("wwan auth myname %s %s", id, pw) rtn, str = rt.command(cmd) if (not rtn) then print(str) print("*** Command Error ***") print(" Terminating Lua script...") os.exit(0) end cmd = string.format("wwan auth accept %s", au) rtn, str = rt.command(cmd) if (not rtn) then print(str) print("*** Command Error ***") print(" Terminating Lua script...") os.exit(0) end if mnc == "50" then rtn, str = rt.command("wwan radio access technology lte") if (not rtn) then print(str) print("*** Command Error ***") print(" Terminating Lua script...") os.exit(0) end end -- =============================== -- モジュールの起動 -- =============================== rtn, str = rt.command("show status wwan-module") power = string.match(str, "Module power%:%s+(%u+)") print("Module power status :", power) if power == "OFF" then rtn, str = rt.command("wwan-module use on") -- モジュールの起動を待つ print("# --- Please wait until wwan-module started.---") pattern = "Initialization completed" cnt, array = rt.syslogwatch(pattern, 1, 120) if cnt == 0 then print("*** WWAN module initialization Error ***") print(" Terminating Lua script...") os.exit(0) end end -- =============================== -- 接続が成功すればtrueを返す -- =============================== rt.sleep(2) rtn, str = rt.command("connect wan1") pattern = "Connected" cnt, array = rt.syslogwatch(pattern, 1, 5) if cnt == 1 then return true end if (not rtn) then print(str) print("*** Connect command Error ***") print(" Terminating Lua script...") os.exit(0) end rtn, str = rt.command("wwan-module use off") rt.sleep(2) return false end -- ################################################################## -- -- SIM自動接続 メイン処理 -- -- ################################################################## -- ************************************* -- SIMキャリアの確認 -- ************************************* -- =============================== -- モジュールの起動を待つ -- =============================== rtn, str = rt.command("wwan-module use on") print("# --- Please wait until getting MNC from SIM.---") pattern = "Initialization completed" cnt, array = rt.syslogwatch(pattern, 1, 30) if cnt == 0 then print("*** WWAN module initialization Error ***") print(" Terminating Lua script...") os.exit(0) end -- =============================== -- SIMのIMSIを確認 -- =============================== rtn, str = rt.command("execute at-command wwan-module at+cimi") mnc = string.match(str, "440(%d%d)") print("SIM MNC =", mnc) -- =============================== -- モジュール電源OFF -- =============================== rtn, str = rt.command("wwan-module use off") -- ************************************* -- APNの設定 -- ************************************* apn_tbl = { op = "hoge", apn = "hoge.jp", uid = "usr", pwd = "yyy", auth = "chap" } -- =============================== -- 設定ファイルのオープン -- =============================== if mnc == "10" then fname = "usb1:/APN_docomo.json" elseif mnc == "20" then fname = "usb1:/APN_softbank.json" else fname = "usb1:/APN_kddi.json" end hdl = io.open(fname) if hdl then print("# Open APN-list file.") else print("# *** File open error ***") print(" Terminating Lua script...") os.exit(0) end -- =============================== -- ファイルから1行ずつ読む -- =============================== for line in hdl:lines() do delim = string.match(line, "%s-%p+") dquot = string.match(delim, "\"") if dquot then key = string.match(line, "\"(%a+)\"") if key ~= "ApnTable" then data = string.match(line, "\"%a+\"%s-%:%s-\"(%w+%p-%a-%p-%a-%p-%a-%p-%a-%p-%w+)\"") if key then else msg = string.format("# *** file format error(%s : key name) ***", fname) print(msg) break end if data then else msg = string.format("# *** file format error(%s : value) ***", fname) print(msg) break end -- =============================== -- 接続パラメータの設定 -- =============================== if key == "operator" then apn_tbl["op"] = data elseif key == "apn" then apn_tbl["apn"] = data elseif key == "userid" then apn_tbl["uid"] = data elseif key == "passwd" then apn_tbl["pwd"] = data elseif key == "authtype" then apn_tbl["auth"] = data result = SetApn(apn_tbl.op, apn_tbl.apn, apn_tbl.uid, apn_tbl.pwd, apn_tbl.auth, mnc ) if result then break end end end end end if hdl then io.close(hdl) print("# Closed APN-list file.") end
“SIM 自動接続 メイン処理”のコメント以下がスクリプトのメイン処理になります。
スクリプトでは以下のような処理を行っています。
3キャリアごとの APN 設定リストの例を以下に示します。使用するSIMカードに応じて適宜変更してください。 利用する可能性が高いSIMカードの設定は上位に記述した方が接続までの時間が早くなります。
{ "ApnTable": [ { "operator": "docomo", "apn" : "mopera.net", "userid" : "mopera", "passwd" : "mopera", "authtype": "chap" } , { "operator": "通信サービス名 or 通信事業社名(任意)", "apn" : "Access point name", "userid" : "ユーザーID", "passwd" : "パスワード", "authtype": "認証方式(pap/chap)" } : : , { "operator": "biglobe", "apn" : "biglobe.jp", "userid" : "user", "passwd" : "0000", "authtype": "chap" } ] }
{ "ApnTable": [ { "operator": "kddi", "apn" : "au.au-net.ne.jp", "userid" : "user@au.au-net.ne.jp", "passwd" : "au", "authtype": "chap" } , { "operator": "通信サービス名 or 通信事業社名(任意)", "apn" : "Access point name", "userid" : "ユーザーID", "passwd" : "パスワード", "authtype": "認証方式(pap/chap)" } : : , { "operator": "mineo-a", "apn" : "mineo.jp", "userid" : "mineo@k-opti.com", "passwd" : "mineo", "authtype": "chap" } ] }
{ "ApnTable": [ { "operator": "softbank-4g", "apn" : "plus.4g", "userid" : "plus", "passwd" : "4g", "authtype": "chap" } , { "operator": "通信サービス名 or 通信事業社名(任意)", "apn" : "Access point name", "userid" : "ユーザーID", "passwd" : "パスワード", "authtype": "認証方式(pap/chap)" } : : , { "operator": "ymobile", "apn" : "plus.acs.jp", "userid" : "ym", "passwd" : "ym", "authtype": "chap" } ] }