mbim.sh 4.0 KB

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