summaryrefslogtreecommitdiff
path: root/recipes-core/mlinux-scripts/mlinux-scripts-1.2/mlinux-set-apn
blob: ebe8096db7e60096e7f67b7a774c6951b5ab455c (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
#!/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.

noapn_modems=(lvw2 lsp3 cdma)

# Comment out AT+CGDCONT= statement
remove_apn_sedcmd="s/^(OK[[:space:]]+'|#MT[[:space:]]+)(AT\+CGDCONT=[0-9]+[^\']*)'*/#comment by mlinux-set-apn \2/"



function usage {
	echo "Usage: $(basename $0) [--] APN"
        echo "Or  $(basename $0)  -c to clear the APN line"
        echo "-- is requied if the APN begins with -"
	exit 1
}


((clear=0))
if (($# < 1)); then
  usage
fi
if [[ $1 == "--" ]] ; then
  apn=$2
elif [[ $1 == "-c" ]] ; then
  clear=1
elif [[ $1 =~ ^- ]] ; then
  usage
else
  apn=$1
fi


set_apn_sedcmd="s/^(#comment by mlinux-set-apn )*(OK[[:space:]]+'|#MT[[:space:]]+)*AT\+CGDCONT=([0-9]+),\"([^\"]*)\",\"[^\"]*\".*/#MT AT\+CGDCONT=\3,\"\4\",\"${apn}\"/"



getmodem()
{
    radioproduct=$(/usr/sbin/mts-io-sysfs show product-id) # LNA3?
    if (($? != 0)) ; then
        echo "Radio is not ready or not found and cannot determine the type"
        exit 1
    fi
    if [[ $radioproduct =~ [^-]*-([^-]*)- ]] ; then
        echo "${BASH_REMATCH[1]}"
    fi
}
function findItem
{
    local s check="$1"
    shift
    for s ; do [[ "$s" == "$check" ]] && return 0; done
    return 1
}


cd /var/config/ppp/peers
if ((clear != 1)) ; then
    for f in $noapn ; do
        echo "Not allowed to set APN for $f"
    done
    
    for f in *_chat *_chat_non_vz ; do
        if [[ -L $f ]] ; then
            continue
        fi
        [[ $f =~ ([^_]*) ]]
        m=${BASH_REMATCH[1]}
        if findItem "$m" "${noapn_modems[@]}" ; then
            continue
        fi
        
        if sed -r -i "${set_apn_sedcmd}" ${f}; then
            echo "Set APN to \"${apn}\" in ${f}"
            if ! grep -q /usr/libexec/ppp/chat_wrapper ${m} ; then
                sed -i "s?connect '/usr/sbin/chat?connect '/usr/libexec/ppp/chat_wrapper /usr/sbin/chat?" ${m}
            fi
        else
            echo "Failed to set APN in $f_chat"
        fi
    done
else
    for f in *_chat *_chat_non_vz ; do
        if sed -i -r "${remove_apn_sedcmd}" $f ; then
            echo "Commented out APN in $f"
        else
            if [[ $f =~ ([^_]*) ]] ; then
                m=${BASH_REMATCH[1]}
                if ! findItem "$m" "${noapn_modems[@]}" ; then
                    echo "Failed to remove APN setting in $f"
                fi
            fi
        fi
    done
fi

exit 0