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

Commit

Permalink
Add support for building the UIHH format AGPS file after download
Browse files Browse the repository at this point in the history
  • Loading branch information
piggz committed May 16, 2021
1 parent c6c0d79 commit d96ce0e
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions huami_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,18 @@
import argparse
import getpass
import requests
import zipfile
import zlib

from rich.console import Console
from rich.table import Table
from rich import box

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

0 comments on commit d96ce0e

Please sign in to comment.