ncm.sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 ifname
  12. proto_config_add_string apn
  13. proto_config_add_string auth
  14. proto_config_add_string username
  15. proto_config_add_string password
  16. proto_config_add_string pincode
  17. proto_config_add_string delay
  18. proto_config_add_string mode
  19. proto_config_add_string pdptype
  20. proto_config_add_int profile
  21. proto_config_add_defaults
  22. }
  23. proto_ncm_setup() {
  24. local interface="$1"
  25. local manufacturer initialize setmode connect finalize devname devpath ifpath
  26. local device ifname apn auth username password pincode delay mode pdptype profile $PROTO_DEFAULT_OPTIONS
  27. json_get_vars device ifname apn auth username password pincode delay mode pdptype profile $PROTO_DEFAULT_OPTIONS
  28. local context_type
  29. [ "$metric" = "" ] && metric="0"
  30. [ -n "$profile" ] || profile=1
  31. pdptype=$(echo "$pdptype" | awk '{print toupper($0)}')
  32. [ "$pdptype" = "IP" -o "$pdptype" = "IPV6" -o "$pdptype" = "IPV4V6" ] || pdptype="IP"
  33. [ "$pdptype" = "IPV4V6" ] && context_type=3
  34. [ -z "$context_type" -a "$pdptype" = "IPV6" ] && context_type=2
  35. [ -n "$context_type" ] || context_type=1
  36. [ -n "$ctl_device" ] && device=$ctl_device
  37. [ -n "$device" ] || {
  38. echo "No control device specified"
  39. proto_notify_error "$interface" NO_DEVICE
  40. proto_set_available "$interface" 0
  41. return 1
  42. }
  43. device="$(readlink -f $device)"
  44. [ -e "$device" ] || {
  45. echo "Control device not valid"
  46. proto_set_available "$interface" 0
  47. return 1
  48. }
  49. [ -z "$ifname" ] && {
  50. devname="$(basename "$device")"
  51. case "$devname" in
  52. 'ttyACM'*)
  53. devpath="$(readlink -f /sys/class/tty/$devname/device)"
  54. ifpath="$devpath/../*/net"
  55. ;;
  56. 'tty'*)
  57. devpath="$(readlink -f /sys/class/tty/$devname/device)"
  58. ifpath="$devpath/../../*/net"
  59. ;;
  60. *)
  61. devpath="$(readlink -f /sys/class/usbmisc/$devname/device/)"
  62. ifpath="$devpath/net"
  63. ;;
  64. esac
  65. ifname="$(ls $(ls -1 -d $ifpath | head -n 1))"
  66. }
  67. [ -n "$ifname" ] || {
  68. echo "The interface could not be found."
  69. proto_notify_error "$interface" NO_IFACE
  70. proto_set_available "$interface" 0
  71. return 1
  72. }
  73. start=$(date +%s)
  74. while true; do
  75. manufacturer=$(gcom -d "$device" -s /etc/gcom/getcardinfo.gcom | awk 'NF && $0 !~ /AT\+CGMI/ { sub(/\+CGMI: /,""); print tolower($1); exit; }')
  76. [ "$manufacturer" = "error" ] && {
  77. manufacturer=""
  78. }
  79. [ -n "$manufacturer" ] && {
  80. break
  81. }
  82. [ -z "$delay" ] && {
  83. break
  84. }
  85. sleep 1
  86. elapsed=$(($(date +%s) - start))
  87. [ "$elapsed" -gt "$delay" ] && {
  88. break
  89. }
  90. done
  91. [ -z "$manufacturer" ] && {
  92. echo "Failed to get modem information"
  93. proto_notify_error "$interface" GETINFO_FAILED
  94. return 1
  95. }
  96. json_load "$(cat /etc/gcom/ncm.json)"
  97. json_select "$manufacturer"
  98. [ $? -ne 0 ] && {
  99. echo "Unsupported modem"
  100. proto_notify_error "$interface" UNSUPPORTED_MODEM
  101. proto_set_available "$interface" 0
  102. return 1
  103. }
  104. json_get_values initialize initialize
  105. for i in $initialize; do
  106. eval COMMAND="$i" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
  107. echo "Failed to initialize modem"
  108. proto_notify_error "$interface" INITIALIZE_FAILED
  109. return 1
  110. }
  111. done
  112. [ -n "$pincode" ] && {
  113. PINCODE="$pincode" gcom -d "$device" -s /etc/gcom/setpin.gcom || {
  114. echo "Unable to verify PIN"
  115. proto_notify_error "$interface" PIN_FAILED
  116. proto_block_restart "$interface"
  117. return 1
  118. }
  119. }
  120. json_get_values configure configure
  121. echo "Configuring modem"
  122. for i in $configure; do
  123. eval COMMAND="$i" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
  124. echo "Failed to configure modem"
  125. proto_notify_error "$interface" CONFIGURE_FAILED
  126. return 1
  127. }
  128. done
  129. [ -n "$mode" ] && {
  130. json_select modes
  131. json_get_var setmode "$mode"
  132. [ -n "$setmode" ] && {
  133. echo "Setting mode"
  134. eval COMMAND="$setmode" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
  135. echo "Failed to set operating mode"
  136. proto_notify_error "$interface" SETMODE_FAILED
  137. return 1
  138. }
  139. }
  140. json_select ..
  141. }
  142. echo "Starting network $interface"
  143. json_get_vars connect
  144. [ -n "$connect" ] && {
  145. echo "Connecting modem"
  146. eval COMMAND="$connect" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
  147. echo "Failed to connect"
  148. proto_notify_error "$interface" CONNECT_FAILED
  149. return 1
  150. }
  151. }
  152. json_get_vars finalize
  153. echo "Setting up $ifname"
  154. proto_init_update "$ifname" 1
  155. proto_add_data
  156. json_add_string "manufacturer" "$manufacturer"
  157. proto_close_data
  158. proto_send_update "$interface"
  159. local zone="$(fw3 -q network "$interface" 2>/dev/null)"
  160. [ "$pdptype" = "IP" -o "$pdptype" = "IPV4V6" ] && {
  161. json_init
  162. json_add_string name "${interface}_4"
  163. json_add_string ifname "@$interface"
  164. json_add_string proto "dhcp"
  165. proto_add_dynamic_defaults
  166. [ -n "$zone" ] && {
  167. json_add_string zone "$zone"
  168. }
  169. json_close_object
  170. ubus call network add_dynamic "$(json_dump)"
  171. }
  172. [ "$pdptype" = "IPV6" -o "$pdptype" = "IPV4V6" ] && {
  173. json_init
  174. json_add_string name "${interface}_6"
  175. json_add_string ifname "@$interface"
  176. json_add_string proto "dhcpv6"
  177. json_add_string extendprefix 1
  178. proto_add_dynamic_defaults
  179. [ -n "$zone" ] && {
  180. json_add_string zone "$zone"
  181. }
  182. json_close_object
  183. ubus call network add_dynamic "$(json_dump)"
  184. }
  185. [ -n "$finalize" ] && {
  186. eval COMMAND="$finalize" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
  187. echo "Failed to configure modem"
  188. proto_notify_error "$interface" FINALIZE_FAILED
  189. return 1
  190. }
  191. }
  192. }
  193. proto_ncm_teardown() {
  194. local interface="$1"
  195. local manufacturer disconnect
  196. local device profile
  197. json_get_vars device profile
  198. [ -n "$ctl_device" ] && device=$ctl_device
  199. [ -n "$device" ] || {
  200. echo "No control device specified"
  201. proto_notify_error "$interface" NO_DEVICE
  202. proto_set_available "$interface" 0
  203. return 1
  204. }
  205. device="$(readlink -f $device)"
  206. [ -e "$device" ] || {
  207. echo "Control device not valid"
  208. proto_set_available "$interface" 0
  209. return 1
  210. }
  211. [ -n "$profile" ] || profile=1
  212. echo "Stopping network $interface"
  213. json_load "$(ubus call network.interface.$interface status)"
  214. json_select data
  215. json_get_vars manufacturer
  216. [ $? -ne 0 -o -z "$manufacturer" ] && {
  217. # Fallback to direct detect, for proper handle device replug.
  218. manufacturer=$(gcom -d "$device" -s /etc/gcom/getcardinfo.gcom | awk 'NF && $0 !~ /AT\+CGMI/ { sub(/\+CGMI: /,""); print tolower($1); exit; }')
  219. [ $? -ne 0 -o -z "$manufacturer" ] && {
  220. echo "Failed to get modem information"
  221. proto_notify_error "$interface" GETINFO_FAILED
  222. return 1
  223. }
  224. json_add_string "manufacturer" "$manufacturer"
  225. }
  226. json_load "$(cat /etc/gcom/ncm.json)"
  227. json_select "$manufacturer" || {
  228. echo "Unsupported modem"
  229. proto_notify_error "$interface" UNSUPPORTED_MODEM
  230. return 1
  231. }
  232. json_get_vars disconnect
  233. [ -n "$disconnect" ] && {
  234. eval COMMAND="$disconnect" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
  235. echo "Failed to disconnect"
  236. proto_notify_error "$interface" DISCONNECT_FAILED
  237. return 1
  238. }
  239. }
  240. proto_init_update "*" 0
  241. proto_send_update "$interface"
  242. }
  243. [ -n "$INCLUDE_ONLY" ] || {
  244. add_protocol ncm
  245. }