ncm.sh 4.4 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_boolean ipv6
  20. }
  21. proto_ncm_setup() {
  22. local interface="$1"
  23. local manufacturer initialize setmode connect ifname devname devpath
  24. local device apn auth username password pincode delay mode pdptype ipv6
  25. json_get_vars device apn auth username password pincode delay mode pdptype ipv6
  26. if [ "$ipv6" = 0 ]; then
  27. ipv6=""
  28. else
  29. ipv6=1
  30. fi
  31. [ -z "$pdptype" ] && {
  32. if [ -n "$ipv6" ]; then
  33. pdptype="IPV4V6"
  34. else
  35. pdptype="IP"
  36. fi
  37. }
  38. [ -n "$ctl_device" ] && device=$ctl_device
  39. [ -n "$device" ] || {
  40. echo "No control device specified"
  41. proto_notify_error "$interface" NO_DEVICE
  42. proto_set_available "$interface" 0
  43. return 1
  44. }
  45. [ -e "$device" ] || {
  46. echo "Control device not valid"
  47. proto_set_available "$interface" 0
  48. return 1
  49. }
  50. [ -n "$apn" ] || {
  51. echo "No APN specified"
  52. proto_notify_error "$interface" NO_APN
  53. return 1
  54. }
  55. devname="$(basename "$device")"
  56. case "$devname" in
  57. 'tty'*)
  58. devpath="$(readlink -f /sys/class/tty/$devname/device)"
  59. ifname="$( ls "$devpath"/../../*/net )"
  60. ;;
  61. *)
  62. devpath="$(readlink -f /sys/class/usbmisc/$devname/device/)"
  63. ifname="$( ls "$devpath"/net )"
  64. ;;
  65. esac
  66. [ -n "$ifname" ] || {
  67. echo "The interface could not be found."
  68. proto_notify_error "$interface" NO_IFACE
  69. proto_set_available "$interface" 0
  70. return 1
  71. }
  72. [ -n "$delay" ] && sleep "$delay"
  73. manufacturer=`gcom -d "$device" -s /etc/gcom/getcardinfo.gcom | awk '/Manufacturer/ { print tolower($2) }'`
  74. [ $? -ne 0 ] && {
  75. echo "Failed to get modem information"
  76. proto_notify_error "$interface" GETINFO_FAILED
  77. return 1
  78. }
  79. json_load "$(cat /etc/gcom/ncm.json)"
  80. json_select "$manufacturer"
  81. [ $? -ne 0 ] && {
  82. echo "Unsupported modem"
  83. proto_notify_error "$interface" UNSUPPORTED_MODEM
  84. proto_set_available "$interface" 0
  85. return 1
  86. }
  87. json_get_values initialize initialize
  88. for i in $initialize; do
  89. eval COMMAND="$i" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
  90. echo "Failed to initialize modem"
  91. proto_notify_error "$interface" INITIALIZE_FAILED
  92. return 1
  93. }
  94. done
  95. [ -n "$pincode" ] && {
  96. PINCODE="$pincode" gcom -d "$device" -s /etc/gcom/setpin.gcom || {
  97. echo "Unable to verify PIN"
  98. proto_notify_error "$interface" PIN_FAILED
  99. proto_block_restart "$interface"
  100. return 1
  101. }
  102. }
  103. [ -n "$mode" ] && {
  104. json_select modes
  105. json_get_var setmode "$mode"
  106. 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. json_select ..
  112. }
  113. json_get_vars connect
  114. eval COMMAND="$connect" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
  115. echo "Failed to connect"
  116. proto_notify_error "$interface" CONNECT_FAILED
  117. return 1
  118. }
  119. echo "Connected, starting DHCP on $ifname"
  120. proto_init_update "$ifname" 1
  121. proto_send_update "$interface"
  122. json_init
  123. json_add_string name "${interface}_4"
  124. json_add_string ifname "@$interface"
  125. json_add_string proto "dhcp"
  126. ubus call network add_dynamic "$(json_dump)"
  127. [ -n "$ipv6" ] && {
  128. json_init
  129. json_add_string name "${interface}_6"
  130. json_add_string ifname "@$interface"
  131. json_add_string proto "dhcpv6"
  132. json_add_string extendprefix 1
  133. ubus call network add_dynamic "$(json_dump)"
  134. }
  135. }
  136. proto_ncm_teardown() {
  137. local interface="$1"
  138. local manufacturer disconnect
  139. local device
  140. json_get_vars device
  141. echo "Stopping network"
  142. manufacturer=`gcom -d "$device" -s /etc/gcom/getcardinfo.gcom | awk '/Manufacturer/ { print tolower($2) }'`
  143. [ $? -ne 0 ] && {
  144. echo "Failed to get modem information"
  145. proto_notify_error "$interface" GETINFO_FAILED
  146. return 1
  147. }
  148. json_load "$(cat /etc/gcom/ncm.json)"
  149. json_select "$manufacturer" || {
  150. echo "Unsupported modem"
  151. proto_notify_error "$interface" UNSUPPORTED_MODEM
  152. return 1
  153. }
  154. json_get_vars disconnect
  155. COMMAND="$disconnect" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
  156. echo "Failed to disconnect"
  157. proto_notify_error "$interface" DISCONNECT_FAILED
  158. return 1
  159. }
  160. proto_init_update "*" 0
  161. proto_send_update "$interface"
  162. }
  163. [ -n "$INCLUDE_ONLY" ] || {
  164. add_protocol ncm
  165. }