mbim.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #!/bin/sh
  2. [ -n "$INCLUDE_ONLY" ] || {
  3. . /lib/functions.sh
  4. . ../netifd-proto.sh
  5. init_proto "$@"
  6. }
  7. #DBG=-v
  8. proto_mbim_init_config() {
  9. available=1
  10. no_device=1
  11. proto_config_add_string "device:device"
  12. proto_config_add_string apn
  13. proto_config_add_string pincode
  14. proto_config_add_string delay
  15. proto_config_add_string auth
  16. proto_config_add_string username
  17. proto_config_add_string password
  18. proto_config_add_defaults
  19. }
  20. _proto_mbim_setup() {
  21. local interface="$1"
  22. local tid=2
  23. local ret
  24. local device apn pincode delay $PROTO_DEFAULT_OPTIONS
  25. json_get_vars device apn pincode delay auth username password $PROTO_DEFAULT_OPTIONS
  26. [ -n "$ctl_device" ] && device=$ctl_device
  27. [ -n "$device" ] || {
  28. echo "mbim[$$]" "No control device specified"
  29. proto_notify_error "$interface" NO_DEVICE
  30. proto_set_available "$interface" 0
  31. return 1
  32. }
  33. [ -c "$device" ] || {
  34. echo "mbim[$$]" "The specified control device does not exist"
  35. proto_notify_error "$interface" NO_DEVICE
  36. proto_set_available "$interface" 0
  37. return 1
  38. }
  39. devname="$(basename "$device")"
  40. devpath="$(readlink -f /sys/class/usbmisc/$devname/device/)"
  41. ifname="$( ls "$devpath"/net )"
  42. [ -n "$ifname" ] || {
  43. echo "mbim[$$]" "Failed to find matching interface"
  44. proto_notify_error "$interface" NO_IFNAME
  45. proto_set_available "$interface" 0
  46. return 1
  47. }
  48. [ -n "$apn" ] || {
  49. echo "mbim[$$]" "No APN specified"
  50. proto_notify_error "$interface" NO_APN
  51. return 1
  52. }
  53. [ -n "$delay" ] && sleep "$delay"
  54. echo "mbim[$$]" "Reading capabilities"
  55. umbim $DBG -n -d $device caps || {
  56. echo "mbim[$$]" "Failed to read modem caps"
  57. proto_notify_error "$interface" PIN_FAILED
  58. return 1
  59. }
  60. tid=$((tid + 1))
  61. [ "$pincode" ] && {
  62. echo "mbim[$$]" "Sending pin"
  63. umbim $DBG -n -t $tid -d $device unlock "$pincode" || {
  64. echo "mbim[$$]" "Unable to verify PIN"
  65. proto_notify_error "$interface" PIN_FAILED
  66. proto_block_restart "$interface"
  67. return 1
  68. }
  69. }
  70. tid=$((tid + 1))
  71. echo "mbim[$$]" "Checking pin"
  72. umbim $DBG -n -t $tid -d $device pinstate || {
  73. echo "mbim[$$]" "PIN required"
  74. proto_notify_error "$interface" PIN_FAILED
  75. proto_block_restart "$interface"
  76. return 1
  77. }
  78. tid=$((tid + 1))
  79. echo "mbim[$$]" "Checking subscriber"
  80. umbim $DBG -n -t $tid -d $device subscriber || {
  81. echo "mbim[$$]" "Subscriber init failed"
  82. proto_notify_error "$interface" NO_SUBSCRIBER
  83. return 1
  84. }
  85. tid=$((tid + 1))
  86. echo "mbim[$$]" "Register with network"
  87. umbim $DBG -n -t $tid -d $device registration || {
  88. echo "mbim[$$]" "Subscriber registration failed"
  89. proto_notify_error "$interface" NO_REGISTRATION
  90. return 1
  91. }
  92. tid=$((tid + 1))
  93. echo "mbim[$$]" "Attach to network"
  94. umbim $DBG -n -t $tid -d $device attach || {
  95. echo "mbim[$$]" "Failed to attach to network"
  96. proto_notify_error "$interface" ATTACH_FAILED
  97. return 1
  98. }
  99. tid=$((tid + 1))
  100. echo "mbim[$$]" "Connect to network"
  101. while ! umbim $DBG -n -t $tid -d $device connect "$apn" "$auth" "$username" "$password"; do
  102. tid=$((tid + 1))
  103. sleep 1;
  104. done
  105. tid=$((tid + 1))
  106. uci_set_state network $interface tid "$tid"
  107. echo "mbim[$$]" "Connected, starting DHCP"
  108. proto_init_update "$ifname" 1
  109. proto_send_update "$interface"
  110. json_init
  111. json_add_string name "${interface}_4"
  112. json_add_string ifname "@$interface"
  113. json_add_string proto "dhcp"
  114. proto_add_dynamic_defaults
  115. json_close_object
  116. ubus call network add_dynamic "$(json_dump)"
  117. json_init
  118. json_add_string name "${interface}_6"
  119. json_add_string ifname "@$interface"
  120. json_add_string proto "dhcpv6"
  121. json_add_string extendprefix 1
  122. proto_add_dynamic_defaults
  123. ubus call network add_dynamic "$(json_dump)"
  124. }
  125. proto_mbim_setup() {
  126. local ret
  127. _proto_mbim_setup $@
  128. ret=$?
  129. [ "$ret" = 0 ] || {
  130. logger "mbim bringup failed, retry in 15s"
  131. sleep 15
  132. }
  133. return $rt
  134. }
  135. proto_mbim_teardown() {
  136. local interface="$1"
  137. local device
  138. json_get_vars device
  139. local tid=$(uci_get_state network $interface tid)
  140. [ -n "$ctl_device" ] && device=$ctl_device
  141. echo "mbim[$$]" "Stopping network"
  142. [ -n "$tid" ] && {
  143. umbim $DBG -t$tid -d "$device" disconnect
  144. uci_revert_state network $interface tid
  145. }
  146. proto_init_update "*" 0
  147. proto_send_update "$interface"
  148. }
  149. [ -n "$INCLUDE_ONLY" ] || add_protocol mbim