Skip to content

Instantly share code, notes, and snippets.

@ilobmirt
Forked from alastair-dm/load_agps_data.py
Last active June 21, 2021 02:03
Show Gist options
  • Save ilobmirt/85fa9fb8b12090681236fc069f4be5b7 to your computer and use it in GitHub Desktop.
Save ilobmirt/85fa9fb8b12090681236fc069f4be5b7 to your computer and use it in GitHub Desktop.
Proof of concept AGPS data loading for PinePhone
#!/usr/bin/python3
#
# load_agps_data
#
# proof of concept loading of agps data to quectel modem via AT commands
from datetime import datetime, timezone
import requests
import serial
at_port = '/dev/ttyUSB2' # ttyUSB2 is the command port on mobian - think they renamed via udev on PmOS
agps_url = 'http://xtrapath1.izatcloud.net/xtra3gr.bin'
agps_file_location='UFS'
agps_file_name = 'xtra3gr.bin'
def sendCmd(cmdstring):
print(cmdstring)
ser.write(f"{cmdstring}\r\n".encode('ascii'))
res = []
while True:
line = ser.readline()
if (line == b''):
break
else:
res.append(line)
print(res)
return res
def setTime():
now = datetime.utcnow().replace(tzinfo=timezone.utc).strftime('%Y/%m/%d,%H:%M:%S')
return sendCmd(f"AT+QGPSXTRATIME=0,\"{now}\"")
# is ModemManager running? Won't work if it's already got the port
print('open serial port')
ser = serial.Serial(at_port, timeout=1)
print('Check modem will talk to us')
res = sendCmd('AT')
# FIXME - expect response OK - really ought to check this and other responses
## is GPS turned on? If so the later stuff won't work so we'll have to turn it off
print('Is GPS turned on?')
res = sendCmd('AT+QGPS?')
print('Turn off GPS')
res = sendCmd('AT+QGPSEND') # this ought to be sent only if the gps was on...
print('Is AGPS enabled?')
res = sendCmd('AT+QGPSXTRA?')
print('Precautionary enabling of AGPS')
res = sendCmd('AT+QGPSXTRA=1')
print('Is valid AGPS data already loaded?')
res = sendCmd('AT+QGPSXTRADATA?')
# FIXME: check the returned stuff
# actually this check seems unnecessary as the returned value is alway the same
# so doesn't help (and doesn't match the docs)
# fetch http://xtrapath[1-3].izatcloud.net/xtra(2).bin and upload
print('delete previous AGPS data file')
res = sendCmd("AT+QFDEL=\"{}:*\"".format(agps_file_location))
res = sendCmd("AT+QFLST=\"{}:*\"".format(agps_file_location))
print('upload new AGPS data')
# FIXME: check res == CONNECT and don't send if it's not
res = sendCmd('AT+QHTTPCFG="requestheader",0')
res = sendCmd("AT+QHTTPURL={},60".format(len(agps_url)))
ser.write("{}".format(agps_url).encode('ascii'))
res = []
while True:
line = ser.readline()
if (line == b''):
break
else:
res.append(line)
print(res)
res = sendCmd("AT+QHTTPGET=80")
res = sendCmd('AT+QHTTPREADFILE="{}:{}",80'.format(agps_file_location,agps_file_name))
#FIXME: calculate agps file checksum and check that it matches the response
# we'll assume here that local system time is kept accurate either by NTP or
# phone network time, so we don't need to fetch SNTP time
print('set current UTC time using local system time')
res = setTime()
print('set AGPS data to file we uploaded')
res = sendCmd('AT+QGPSXTRADATA="{}"'.format(agps_file_name))
print('what does it day about data validity now?')
res = sendCmd('AT+QGPSXTRADATA?')
print('NOTE: it\'s given us the same response as before, despite having new data uploaded')
print('enable gps')
res = sendCmd('AT+QGPS=1')
print('close serial port')
if ser.is_open:
ser.close()
@ilobmirt
Copy link
Author

Rather then download locally, then upload it to the modem. These modifications have the modem download directly from the source to its own memory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment