summaryrefslogtreecommitdiff
path: root/recipes-core/mlinux-scripts/mlinux-scripts-1.2/mlinux-switch-apn
blob: 689b0a481626902b0c7b0eab0611b2cbbd3e6ca6 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/bin/bash

# Copyright (C) 2014, 2017, 2019 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.

MTMODEMS=(LNA3 L4N1)
MODEMS=(LE910-NA1 LE910C4-NF)

# See if string is in array of strings
# parm 1 String
# parm 2 Arrays of strings
function findItem {
    local s check="$1"
    shift
    for s ; do 
        [[ "$s" == "$check" ]] && return 0;
    done
    return 1
}

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, 1 and 2"
    exit 1
}

function setchat {
    m=$1
    m=${m,,}
    fw=$2
    silent=$3
    case $fw in
        0)
            ending="non_vz"
            ;;
        1)
            ending="vz"
            ;;
        2)
            # Currently L4N1 only
            ending="non_vz"
            ;;
        *)
            >&2 echo 'Invalid firmware number $fw'
            usage
            exit 1
            ;;
    esac
    source="/etc/ppp/peers/${m}_chat_${ending}"
    target="/etc/ppp/peers/${m}_chat"
    canon_src=$(readlink -f "${source}")
    canon_target=$(readlink -f "${target}")
    if [[ ${canon_src} != ${canon_target} ]] ; then
        if ((silent == 0)) ; then
            echo "Setting chat script to ${source}"
        fi
        ln -sf "${source}" "${target}"
    fi
}

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 =~ [^-]*-([^-]*)- ]] ; then
    mymtmodem="${BASH_REMATCH[1],,}"
else
    >&2 echo "Cannot find the modem in the $productid string"
    exit 1
fi

if ! findItem "${mymtmodem^^}" "${MTMODEMS[@]}"  ; then
  echo "Firmware switch is not supported on ${mymtmodem}."
  usage
  exit 1
fi

if (($# > 0)) ; then
    fw="$1"
    if [[ $fw =~ ^[0-9]+$ ]] ; then
	echo fw is $fw
        if ((fw < 0)) || ((fw > 2)) ; then
            usage
        fi
    else
        >&2 echo "Invalid firmware SKU value"
        usage
    fi
    echo "Switching chat script to firmware SKU $fw"
    setchat $mymtmodem $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 "No radio on this device."
        usage
        exit 1
    fi
    if ! findItem $MODEL "${MODEMS[@]}" ; then
        >&2 echo "Firmware switch is supported only on ${MODEMS[@]} and this modem is $MODEL"
        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 "${mymtmodem}" $fw 0
        else
	  fw=$(radio-query --active-firmware)
          if (($? == 0)) ; then
              setchat "${mymtmodem}" $fw 0
          fi
      fi
    fi
fi