ncm.sh 4.1 KB

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