ncm.sh 7.2 KB

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