aboutsummaryrefslogtreecommitdiffstats
path: root/utils/qmi-network
blob: 97ea14d7424d345077ca9af3c04b7800b4a1035d (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/bin/sh

print_usage ()
{
    echo "usage: $0 [DEVICE] [COMMAND]"
}

if [ $# -ne 2 ]; then
    echo "error: missing arguments" 1>&2
    print_usage
    exit 255
fi

DEVICE=$1
COMMAND=$2
STATE_FILE=/tmp/qmi-network-state

save_state ()
{
    KEY=$1
    VAL=$2

    echo "Saving state... ($KEY: $VAL)"

    if [ -f $STATE_FILE ]; then
        PREVIOUS=`cat $STATE_FILE`
        PREVIOUS=`echo "$PREVIOUS" | grep -v $KEY`
        if [ "x$PREVIOUS" != "x" ]; then
            echo $PREVIOUS > $STATE_FILE
        else
            rm $STATE_FILE
        fi
    fi

    if [ "x$VAL" != "x" ]; then
        echo "$KEY=\"$VAL\"" >> $STATE_FILE
    fi
}

load_state ()
{
    if [ -f $STATE_FILE ]; then
        echo "Loading previous state..."
        source $STATE_FILE

        if [ "x$CID" != "x" ]; then
            echo "    Previous CID: $CID"
        fi
        if [ "x$PDH" != "x" ]; then
            echo "    Previous PDH: $PDH"
        fi
    fi
}

clear_state ()
{
    echo "Clearing state..."
    rm $STATE_FILE
}

# qmicli -d /dev/cdc-wdm0 --wds-start-network --client-no-release-cid
# [/dev/cdc-wdm0] Network started
# 	Packet data handle: 3634026241
# [/dev/cdc-wdm0] Client ID not released:
# 	Service: 'wds'
# 	    CID: '80'
start_network ()
{
    if [ "x$CID" != "x" ]; then
        USE_PREVIOUS_CID="--client-cid=$CID"
    fi

    if [ "x$PDH" != "x" ]; then
        echo "error: cannot re-start network, PDH already exists" 1>&2
        exit 3
    fi

    START_NETWORK_CMD="qmicli -d $DEVICE --wds-start-network $USE_PREVIOUS_CID --client-no-release-cid"
    echo "Starting network with '$START_NETWORK_CMD'..."

    if [[ "x$QMIDEBUG" != "x" ]]; then
        START_NETWORK_OUT="\
[/dev/cdc-wdm0] Network started
	Packet data handle: '3634026241'
[/dev/cdc-wdm0] Client ID not released:
	Service: 'wds'
	CID: '80'"
    else
        START_NETWORK_OUT=`$START_NETWORK_CMD`
    fi

    # Save the new CID if we didn't use any before
    if [ "x$CID" = "x" ]; then
        CID=`echo "$START_NETWORK_OUT" | grep CID | sed "s/'//g" | awk 'BEGIN { FS = ":" } ; { print $2 }'`
        if [[ "x$CID" = "x" ]]; then
            echo "error: network start failed, client not allocated" 1>&2
            exit 1
        else
            save_state "CID" $CID
        fi
    fi

    PDH=`echo "$START_NETWORK_OUT" | grep handle | sed "s/'//g" | awk 'BEGIN { FS = ":" } ; { print $2 }'`
    if [[ "x$PDH" = "x" ]]; then
        echo "error: network start failed, no packet data handle" 1>&2
        exit 2
    else
        save_state "PDH" $PDH
    fi

    echo "Network started successfully"
}

# qmicli -d /dev/cdc-wdm0 --wds-stop-network
stop_network ()
{
    if [ "x$CID" = "x" ]; then
        echo "Network already stopped"
    elif [ "x$PDH" = "x" ]; then
        echo "Network already stopped; need to cleanup CID $CID"
        # Cleanup the client
        qmicli -d "$DEVICE" --wds-noop --client-cid="$CID"
    else
        STOP_NETWORK_CMD="qmicli -d $DEVICE --wds-stop-network=$PDH --client-cid=$CID"
        echo "Stopping network with '$STOP_NETWORK_CMD'..."

        if [[ "x$QMIDEBUG" != "x" ]]; then
            STOP_NETWORK_OUT="\
[/dev/cdc-wdm0] Network stopped
"
        else
            STOP_NETWORK_OUT=`$STOP_NETWORK_CMD`
        fi

        echo "Network stopped successfully"
    fi

    clear_state
}

# qmicli -d /dev/cdc-wdm0 --wds-get-packet-service-status
packet_service_status ()
{
    if [ "x$CID" != "x" ]; then
        USE_PREVIOUS_CID="--client-cid=$CID --client-no-release-cid"
    fi

    STATUS_CMD="qmicli -d $DEVICE --wds-get-packet-service-status $USE_PREVIOUS_CID"
    echo "Getting status with '$STATUS_CMD'..."

    if [[ "x$QMIDEBUG" != "x" ]]; then
        STATUS_OUT="\
[/dev/cdc-wdm0] Connection status: 'disconnected'
"
    else
        STATUS_OUT=`$STATUS_CMD`
    fi

    CONN=`echo "$STATUS_OUT" | grep "Connection status" | sed "s/'//g" | awk 'BEGIN { FS = ":" } ; { print $2 }'`
    if [[ "x$CONN" = "x" ]]; then
        echo "error: couldn't get packet service status" 1>&2
        exit 2
    else
        echo "Status: $CONN"
        if [[ "x$CONN" != "xconnected" ]]; then
            exit 64
        fi
    fi
}

# Main

# Load previous state, if any
load_state

# Process commands
case $COMMAND in
    "start")
        start_network
        ;;
    "stop")
        stop_network
        ;;
    "status")
        packet_service_status
        ;;
    *)
        echo "error: unexpected command '$COMMAND'" 1>&2
        print_usage
        exit 255
        ;;
esac

exit 0