summaryrefslogtreecommitdiff
path: root/recipes-core/mlinux-scripts/mlinux-scripts-1.2/mlinux-switch-apn
blob: 535ab17c946f2736411f64f7ed8f8fa617748c1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/bash

# Copyright (C) 2014, 2017 Multi-Tech Systems

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

lna3_chat_file[0]=/etc/ppp/peers/lna3_chat_non_vz
lna3_chat_file[1]=/etc/ppp/peers/lna3_chat_vz
lna3_chat_link=/etc/ppp/peers/lna3_chat

function setchat {
  fw=$1
  silent=$2
  if ((${#lna3_chat_file[$fw]} == 0)) ; then
    >&2 echo "Invalid firmware SKU $fw"
    return 1
  else
    canon_src=$(readlink -f "${lna3_chat_file[$fw]}")
    canon_dest=$(readlink -f "${lna3_chat_link}")
    if [[ ${canon_src} != ${canon_dest} ]] ; then
        if ((silent == 0)) ; then
            echo "Setting chat script to ${lna3_chat_file[$fw]}"
        fi
        ln -sf "${lna3_chat_file[$fw]}" "${lna3_chat_link}"
    fi
  fi
}

function usage {
    >&2 echo "$(basename $0) [firmware image SKU]"
    >&2 echo "The firmware image SKU is optional."
    >&2 echo "If not specified, the image SKU will be determined"
    >&2 echo "from the SIM, and automatically switched with the"
    >&2 echo "APN.  Current valid SKU's are 0 and 1"
    >&2 echo "Only LE910-NA1 is supported on product-ids containing string -LNA3-"
    exit 1
}

function not_ready {
	>&2 echo "Radio is not ready"
	>&2 echo "Try executing mlinux-switch-apn once the radio is ready"
	exit 1
}
productid=$(/usr/sbin/mts-io-sysfs show product-id)
if ! [[ $productid =~ -LNA3- ]] ; then
  echo "Firmware switch is supported only on LE910-NA1 models with product-id containing -LNA3-."
  usage
  exit 1
fi

if (($# > 0)) ; then
    fw="$1"
    if [[ fw =~ ^[0-9]+$ ]] ; then
        if ((fw < 0)) || ((fw > 1)) ; then
            usage
        fi
    else
        if ((fw > 1))  || ((fw < 0)) ; then
            >&2 echo "Invalid firmware SKU value"
            usage
        fi
    fi
    echo "Switching chat script to firmware SKU $fw"
    setchat $fw 0
    exit $?
else
    if ! /usr/sbin/mlinux-cell-radio-ready ; then
        >&2 echo Radio is not ready
        >&2 echo Try again later
        exit 1
    fi
    MODEL=$(radio-query --model)
    if (($? != 0)) ; then
        >&2echo "Assume no radio on this device."
        usage
        exit 1
    fi
    if [[ $MODEL != LE910-NA1 ]] ; then
        >&2 echo "Firmware switch is supported only on LE910-NA1 models with product-id containing -LNA3-."
        usage
    fi
    
    if  fwreply=$(/usr/sbin/mlinux-switch-cell-fw -1); then
        if [[ $fwreply =~ Cellular[[:space:]]+radio[[:space:]]+firmware[[:space:]]+has[[:space:]]+(already[[:space:]]+)been[[:space:]]+switched[[:space:]]+to[[:space:]]+([^[:space:]]*)[[:space:]]+image\. ]] ; then
          fw=${BASH_REMATCH[2]}
          setchat $fw 0
        else
	  fw=$(radio-query --active-firmware)
          if (($? == 0)) ; then
                setchat $fw 0
          fi
      fi
    fi
fi