ncm.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 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. [ -n "$mode" ] && {
  93. json_select modes
  94. json_get_var setmode "$mode"
  95. eval COMMAND="$setmode" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
  96. echo "Failed to set operating mode"
  97. proto_notify_error "$interface" SETMODE_FAILED
  98. return 1
  99. }
  100. json_select ..
  101. }
  102. echo "Starting network $interface"
  103. json_get_vars connect
  104. eval COMMAND="$connect" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
  105. echo "Failed to connect"
  106. proto_notify_error "$interface" CONNECT_FAILED
  107. return 1
  108. }
  109. echo "Setting up $ifname"
  110. proto_init_update "$ifname" 1
  111. proto_add_data
  112. json_add_string "manufacturer" "$manufacturer"
  113. proto_close_data
  114. proto_send_update "$interface"
  115. [ "$pdptype" = "IP" -o "$pdptype" = "IPV4V6" ] && {
  116. json_init
  117. json_add_string name "${interface}_4"
  118. json_add_string ifname "@$interface"
  119. json_add_string proto "dhcp"
  120. proto_add_dynamic_defaults
  121. ubus call network add_dynamic "$(json_dump)"
  122. }
  123. [ "$pdptype" = "IPV6" -o "$pdptype" = "IPV4V6" ] && {
  124. json_init
  125. json_add_string name "${interface}_6"
  126. json_add_string ifname "@$interface"
  127. json_add_string proto "dhcpv6"
  128. json_add_string extendprefix 1
  129. proto_add_dynamic_defaults
  130. ubus call network add_dynamic "$(json_dump)"
  131. }
  132. }
  133. proto_ncm_teardown() {
  134. local interface="$1"
  135. local manufacturer disconnect
  136. local device profile
  137. json_get_vars device profile
  138. [ -n "$ctl_device" ] && device=$ctl_device
  139. [ -n "$profile" ] || profile=1
  140. echo "Stopping network $interface"
  141. json_load "$(ubus call network.interface.$interface status)"
  142. json_select data
  143. json_get_vars manufacturer
  144. json_load "$(cat /etc/gcom/ncm.json)"
  145. json_select "$manufacturer" || {
  146. echo "Unsupported modem"
  147. proto_notify_error "$interface" UNSUPPORTED_MODEM
  148. return 1
  149. }
  150. json_get_vars disconnect
  151. eval COMMAND="$disconnect" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
  152. echo "Failed to disconnect"
  153. proto_notify_error "$interface" DISCONNECT_FAILED
  154. return 1
  155. }
  156. proto_init_update "*" 0
  157. proto_send_update "$interface"
  158. }
  159. [ -n "$INCLUDE_ONLY" ] || {
  160. add_protocol ncm
  161. }