Skip to content
This repository has been archived by the owner on Apr 11, 2023. It is now read-only.

Add support for building the UIHH format AGPS file after download #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions huami_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import urllib
import argparse
import getpass
import zipfile
import zlib
import requests

from rich.console import Console
Expand All @@ -39,6 +41,9 @@

import urls

def encode_uint32(value) -> bytes:
return bytes([value & 0xff]) + bytes([(value >> 8) & 0xff]) + bytes([(value >> 16) & 0xff]) + bytes([(value >> 24) & 0xff]);

class HuamiAmazfit:
"""Base class for logging in and receiving auth keys and GPS packs"""
def __init__(self, method="amazfit", email=None, password=None):
Expand Down Expand Up @@ -262,6 +267,32 @@ def get_gps_data(self) -> None:
with open(agps_file_names[pack_idx], 'wb') as gps_file:
shutil.copyfileobj(request.raw, gps_file)

def build_gps_uihh(self) -> None:
print("Building gps_uihh.bin")
d = {'gps_alm.bin':0x05, 'gln_alm.bin':0x0f, 'lle_bds.lle':0x86, 'lle_gps.lle':0x87, 'lle_glo.lle':0x88, 'lle_gal.lle':0x89, 'lle_qzss.lle':0x8a}
cep_archive = zipfile.ZipFile('cep_7days.zip', 'r')
lle_archive = zipfile.ZipFile('lle_1week.zip', 'r')
f = open('gps_uihh.bin', 'wb')
content = bytes()
filecontent = bytes()
fileheader = bytes()

for key, value in d.items():
if value >= 0x86:
filecontent = lle_archive.read(key)
else:
filecontent = cep_archive.read(key)

fileheader = bytes([1]) + bytes([value]) + encode_uint32(len(filecontent)) + encode_uint32(zlib.crc32(filecontent) & 0xffffffff)
content += fileheader + filecontent

header = b'UIHH' + bytes([0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]) + encode_uint32(zlib.crc32(content) & 0xffffffff) + \
bytes([0x00, 0x00, 0x00, 0x00, 0x00, 0x00]) + encode_uint32(len(content)) + bytes([0x00, 0x00, 0x00, 0x00, 0x00, 0x00])

content = header + content
f.write(content)
f.close()

def logout(self) -> None:
"""Log out from the current account"""
logout_url = urls.URLS['logout']
Expand Down Expand Up @@ -378,6 +409,7 @@ def logout(self) -> None:

if args.gps or args.all:
device.get_gps_data()
device.build_gps_uihh()

if args.no_logout:
print("\nNo logout!")
Expand Down