ncm.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. local zone="$(fw3 -q network "$interface" 2>/dev/null)"
  128. [ "$pdptype" = "IP" -o "$pdptype" = "IPV4V6" ] && {
  129. json_init
  130. json_add_string name "${interface}_4"
  131. json_add_string ifname "@$interface"
  132. json_add_string proto "dhcp"
  133. proto_add_dynamic_defaults
  134. [ -n "$zone" ] && {
  135. json_add_string zone "$zone"
  136. }
  137. json_close_object
  138. ubus call network add_dynamic "$(json_dump)"
  139. }
  140. [ "$pdptype" = "IPV6" -o "$pdptype" = "IPV4V6" ] && {
  141. json_init
  142. json_add_string name "${interface}_6"
  143. json_add_string ifname "@$interface"
  144. json_add_string proto "dhcpv6"
  145. json_add_string extendprefix 1
  146. proto_add_dynamic_defaults
  147. [ -n "$zone" ] && {
  148. json_add_string zone "$zone"
  149. }
  150. json_close_object
  151. ubus call network add_dynamic "$(json_dump)"
  152. }
  153. [ -n "$finalize" ] && {
  154. eval COMMAND="$finalize" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
  155. echo "Failed to configure modem"
  156. proto_notify_error "$interface" FINALIZE_FAILED
  157. return 1
  158. }
  159. }
  160. }
  161. proto_ncm_teardown() {
  162. local interface="$1"
  163. local manufacturer disconnect
  164. local device profile
  165. json_get_vars device profile
  166. [ -n "$ctl_device" ] && device=$ctl_device
  167. [ -n "$profile" ] || profile=1
  168. echo "Stopping network $interface"
  169. json_load "$(ubus call network.interface.$interface status)"
  170. json_select data
  171. json_get_vars manufacturer
  172. json_load "$(cat /etc/gcom/ncm.json)"
  173. json_select "$manufacturer" || {
  174. echo "Unsupported modem"
  175. proto_notify_error "$interface" UNSUPPORTED_MODEM
  176. return 1
  177. }
  178. json_get_vars disconnect
  179. eval COMMAND="$disconnect" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
  180. echo "Failed to disconnect"
  181. proto_notify_error "$interface" DISCONNECT_FAILED
  182. return 1
  183. }
  184. proto_init_update "*" 0
  185. proto_send_update "$interface"
  186. }
  187. [ -n "$INCLUDE_ONLY" ] || {
  188. add_protocol ncm
  189. }