ncm.sh 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 -o -z "$manufacturer" ] && {
  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. [ -n "$setmode" ] && {
  105. echo "Setting mode"
  106. eval COMMAND="$setmode" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
  107. echo "Failed to set operating mode"
  108. proto_notify_error "$interface" SETMODE_FAILED
  109. return 1
  110. }
  111. }
  112. json_select ..
  113. }
  114. echo "Starting network $interface"
  115. json_get_vars connect
  116. [ -n "$connect" ] && {
  117. echo "Connecting modem"
  118. eval COMMAND="$connect" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
  119. echo "Failed to connect"
  120. proto_notify_error "$interface" CONNECT_FAILED
  121. return 1
  122. }
  123. }
  124. json_get_vars finalize
  125. echo "Setting up $ifname"
  126. proto_init_update "$ifname" 1
  127. proto_add_data
  128. json_add_string "manufacturer" "$manufacturer"
  129. proto_close_data
  130. proto_send_update "$interface"
  131. local zone="$(fw3 -q network "$interface" 2>/dev/null)"
  132. [ "$pdptype" = "IP" -o "$pdptype" = "IPV4V6" ] && {
  133. json_init
  134. json_add_string name "${interface}_4"
  135. json_add_string ifname "@$interface"
  136. json_add_string proto "dhcp"
  137. proto_add_dynamic_defaults
  138. [ -n "$zone" ] && {
  139. json_add_string zone "$zone"
  140. }
  141. json_close_object
  142. ubus call network add_dynamic "$(json_dump)"
  143. }
  144. [ "$pdptype" = "IPV6" -o "$pdptype" = "IPV4V6" ] && {
  145. json_init
  146. json_add_string name "${interface}_6"
  147. json_add_string ifname "@$interface"
  148. json_add_string proto "dhcpv6"
  149. json_add_string extendprefix 1
  150. proto_add_dynamic_defaults
  151. [ -n "$zone" ] && {
  152. json_add_string zone "$zone"
  153. }
  154. json_close_object
  155. ubus call network add_dynamic "$(json_dump)"
  156. }
  157. [ -n "$finalize" ] && {
  158. eval COMMAND="$finalize" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
  159. echo "Failed to configure modem"
  160. proto_notify_error "$interface" FINALIZE_FAILED
  161. return 1
  162. }
  163. }
  164. }
  165. proto_ncm_teardown() {
  166. local interface="$1"
  167. local manufacturer disconnect
  168. local device profile
  169. json_get_vars device profile
  170. [ -n "$ctl_device" ] && device=$ctl_device
  171. [ -n "$device" ] || {
  172. echo "No control device specified"
  173. proto_notify_error "$interface" NO_DEVICE
  174. proto_set_available "$interface" 0
  175. return 1
  176. }
  177. device="$(readlink -f $device)"
  178. [ -e "$device" ] || {
  179. echo "Control device not valid"
  180. proto_set_available "$interface" 0
  181. return 1
  182. }
  183. [ -n "$profile" ] || profile=1
  184. echo "Stopping network $interface"
  185. json_load "$(ubus call network.interface.$interface status)"
  186. json_select data
  187. json_get_vars manufacturer
  188. [ $? -ne 0 -o -z "$manufacturer" ] && {
  189. # Fallback to direct detect, for proper handle device replug.
  190. manufacturer=$(gcom -d "$device" -s /etc/gcom/getcardinfo.gcom | awk 'NF && $0 !~ /AT\+CGMI/ { sub(/\+CGMI: /,""); print tolower($1); exit; }')
  191. [ $? -ne 0 -o -z "$manufacturer" ] && {
  192. echo "Failed to get modem information"
  193. proto_notify_error "$interface" GETINFO_FAILED
  194. return 1
  195. }
  196. json_add_string "manufacturer" "$manufacturer"
  197. }
  198. json_load "$(cat /etc/gcom/ncm.json)"
  199. json_select "$manufacturer" || {
  200. echo "Unsupported modem"
  201. proto_notify_error "$interface" UNSUPPORTED_MODEM
  202. return 1
  203. }
  204. json_get_vars disconnect
  205. [ -n "$disconnect" ] && {
  206. eval COMMAND="$disconnect" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
  207. echo "Failed to disconnect"
  208. proto_notify_error "$interface" DISCONNECT_FAILED
  209. return 1
  210. }
  211. }
  212. proto_init_update "*" 0
  213. proto_send_update "$interface"
  214. }
  215. [ -n "$INCLUDE_ONLY" ] || {
  216. add_protocol ncm
  217. }