ncm.sh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #!/bin/sh
  2. [ -n "$INCLUDE_ONLY" ] || {
  3. . /lib/functions.sh
  4. . ../netifd-proto.sh
  5. init_proto "$@"
  6. }
  7. proto_ncm_init_config() {
  8. no_device=1
  9. available=1
  10. proto_config_add_string "device:device"
  11. proto_config_add_string apn
  12. proto_config_add_string auth
  13. proto_config_add_string username
  14. proto_config_add_string password
  15. proto_config_add_string pincode
  16. proto_config_add_string delay
  17. proto_config_add_string mode
  18. proto_config_add_string pdptype
  19. proto_config_add_int profile
  20. proto_config_add_defaults
  21. }
  22. proto_ncm_setup() {
  23. local interface="$1"
  24. local manufacturer initialize setmode connect finalize ifname devname devpath
  25. local device apn auth username password pincode delay mode pdptype profile $PROTO_DEFAULT_OPTIONS
  26. json_get_vars device apn auth username password pincode delay mode pdptype profile $PROTO_DEFAULT_OPTIONS
  27. [ "$metric" = "" ] && metric="0"
  28. [ -n "$profile" ] || profile=1
  29. pdptype=`echo "$pdptype" | awk '{print toupper($0)}'`
  30. [ "$pdptype" = "IP" -o "$pdptype" = "IPV6" -o "$pdptype" = "IPV4V6" ] || pdptype="IP"
  31. [ -n "$ctl_device" ] && device=$ctl_device
  32. [ -n "$device" ] || {
  33. echo "No control device specified"
  34. proto_notify_error "$interface" NO_DEVICE
  35. proto_set_available "$interface" 0
  36. return 1
  37. }
  38. device="$(readlink -f $device)"
  39. [ -e "$device" ] || {
  40. echo "Control device not valid"
  41. proto_set_available "$interface" 0
  42. return 1
  43. }
  44. devname="$(basename "$device")"
  45. case "$devname" in
  46. 'tty'*)
  47. devpath="$(readlink -f /sys/class/tty/$devname/device)"
  48. ifname="$( ls "$devpath"/../../*/net )"
  49. ;;
  50. *)
  51. devpath="$(readlink -f /sys/class/usbmisc/$devname/device/)"
  52. ifname="$( ls "$devpath"/net )"
  53. ;;
  54. esac
  55. [ -n "$ifname" ] || {
  56. echo "The interface could not be found."
  57. proto_notify_error "$interface" NO_IFACE
  58. proto_set_available "$interface" 0
  59. return 1
  60. }
  61. [ -n "$delay" ] && sleep "$delay"
  62. manufacturer=`gcom -d "$device" -s /etc/gcom/getcardinfo.gcom | awk 'NF && $0 !~ /AT\+CGMI/ { sub(/\+CGMI: /,""); print tolower($1); exit; }'`
  63. [ $? -ne 0 ] && {
  64. echo "Failed to get modem information"
  65. proto_notify_error "$interface" GETINFO_FAILED
  66. return 1
  67. }
  68. json_load "$(cat /etc/gcom/ncm.json)"
  69. json_select "$manufacturer"
  70. [ $? -ne 0 ] && {
  71. echo "Unsupported modem"
  72. proto_notify_error "$interface" UNSUPPORTED_MODEM
  73. proto_set_available "$interface" 0
  74. return 1
  75. }
  76. json_get_values initialize initialize
  77. for i in $initialize; do
  78. eval COMMAND="$i" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
  79. echo "Failed to initialize modem"
  80. proto_notify_error "$interface" INITIALIZE_FAILED
  81. return 1
  82. }
  83. done
  84. [ -n "$pincode" ] && {
  85. PINCODE="$pincode" gcom -d "$device" -s /etc/gcom/setpin.gcom || {
  86. echo "Unable to verify PIN"
  87. proto_notify_error "$interface" PIN_FAILED
  88. proto_block_restart "$interface"
  89. return 1
  90. }
  91. }
  92. json_get_values configure configure
  93. echo "Configuring modem"
  94. for i in $configure; do
  95. eval COMMAND="$i" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
  96. echo "Failed to configure modem"
  97. proto_notify_error "$interface" CONFIGURE_FAILED
  98. return 1
  99. }
  100. done
  101. [ -n "$mode" ] && {
  102. json_select modes
  103. json_get_var setmode "$mode"
  104. echo "Setting mode"
  105. eval COMMAND="$setmode" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
  106. echo "Failed to set operating mode"
  107. proto_notify_error "$interface" SETMODE_FAILED
  108. return 1
  109. }
  110. json_select ..
  111. }
  112. echo "Starting network $interface"
  113. json_get_vars connect
  114. echo "Connecting modem"
  115. eval COMMAND="$connect" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
  116. echo "Failed to connect"
  117. proto_notify_error "$interface" CONNECT_FAILED
  118. return 1
  119. }
  120. json_get_vars finalize
  121. echo "Setting up $ifname"
  122. proto_init_update "$ifname" 1
  123. proto_add_data
  124. json_add_string "manufacturer" "$manufacturer"
  125. proto_close_data
  126. proto_send_update "$interface"
  127. [ "$pdptype" = "IP" -o "$pdptype" = "IPV4V6" ] && {
  128. json_init
  129. json_add_string name "${interface}_4"
  130. json_add_string ifname "@$interface"
  131. json_add_string proto "dhcp"
  132. proto_add_dynamic_defaults
  133. ubus call network add_dynamic "$(json_dump)"
  134. }
  135. [ "$pdptype" = "IPV6" -o "$pdptype" = "IPV4V6" ] && {
  136. json_init
  137. json_add_string name "${interface}_6"
  138. json_add_string ifname "@$interface"
  139. json_add_string proto "dhcpv6"
  140. json_add_string extendprefix 1
  141. proto_add_dynamic_defaults
  142. ubus call network add_dynamic "$(json_dump)"
  143. }
  144. [ -n "$finalize" ] && {
  145. eval COMMAND="$finalize" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
  146. echo "Failed to configure modem"
  147. proto_notify_error "$interface" FINALIZE_FAILED
  148. return 1
  149. }
  150. }
  151. }
  152. proto_ncm_teardown() {
  153. local interface="$1"
  154. local manufacturer disconnect
  155. local device profile
  156. json_get_vars device profile
  157. [ -n "$ctl_device" ] && device=$ctl_device
  158. [ -n "$profile" ] || profile=1
  159. echo "Stopping network $interface"
  160. json_load "$(ubus call network.interface.$interface status)"
  161. json_select data
  162. json_get_vars manufacturer
  163. json_load "$(cat /etc/gcom/ncm.json)"
  164. json_select "$manufacturer" || {
  165. echo "Unsupported modem"
  166. proto_notify_error "$interface" UNSUPPORTED_MODEM
  167. return 1
  168. }
  169. json_get_vars disconnect
  170. eval COMMAND="$disconnect" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
  171. echo "Failed to disconnect"
  172. proto_notify_error "$interface" DISCONNECT_FAILED
  173. return 1
  174. }
  175. proto_init_update "*" 0
  176. proto_send_update "$interface"
  177. }
  178. [ -n "$INCLUDE_ONLY" ] || {
  179. add_protocol ncm
  180. }