#!/bin/bash

#sudo apt-get update && sudo apt-get install libqmi-utils udhcpc

#imposta online
sudo qmicli -d /dev/cdc-wdm0 --dms-set-operating-mode='online'
#sleep 2

#verifica il segnale
sudo qmicli -d /dev/cdc-wdm0 --dms-get-operating-mode
sudo qmicli -d /dev/cdc-wdm0 --nas-get-signal-strength
sudo qmicli -d /dev/cdc-wdm0 --nas-get-home-network
#sleep 2

# Reconfigure the network interface for raw-ip protocol
sudo qmicli -d /dev/cdc-wdm0 -w		# this confirms the name of the network interface, typically wwan0
#sleep 2

sudo ip link set wwan0 down		# change the wwan0 to the one returned above if different
#sleep 2

echo 'Y' | sudo tee /sys/class/net/wwan0/qmi/raw_ip
#sleep 2

sudo ip link set wwan0 up
#sleep 2


# === NOTE VARIE ===

#connect --- qui c'è da aggiungere lo script di check con grep:
#se non c'è la stringa nell'output "[/dev/cdc-wdm0] Network started" e invece c'è "error: couldn't start network: QMI protocol error (14): 'CallFailed'" deve essere riavviato una seconda volta

#NB: SOSTITURE wap.tim.it CON L'APN DELLA VOSTRA RETE

sudo qmicli -p -d /dev/cdc-wdm0 --device-open-net='net-raw-ip|net-no-qos-header' --wds-start-network="apn='wap.tim.it',username='pi',password='PASSWORD,ip-type=4" --client-no-release-cid
#sleep 2


#configure the IP address and the default route with udhcpc
sudo udhcpc -i wwan0
#sleep 2

ip a s wwan0
sleep 2

ip r s



