semuconsulting / pyspartn

Python library for parsing SPARTN protocol messages.
BSD 3-Clause "New" or "Revised" License
7 stars 3 forks source link

SPARTN-1X-OCB-GLO bad decryption #17

Closed jonathanmuller closed 7 months ago

jonathanmuller commented 7 months ago

Decryption of SPARTN-1X-OCB-GLO doesn't seem to be correct. The output result looks random (not a single field looks correct)

It looks to be related to the ambiguity resolution as SPARTN-1X-OCB-GLO are the only messages with 16 bits GNSS time tag defined in GLONASS epoch (rather than being 32 bits with no ambiguity or defined in GPS epoch)

Ideally a parameter of one of the function in the chain should specify if GPS time needs to be used or GLONASS I'm not sure how to solve it cleanly for now but I'll think about it and try to come with a proposal (or maybe @semuadmin has an idea on how to do that properly)

image

def convert_timetag(timetag16: int, basedate: datetime = datetime.now()) -> int:
    ...
    ...
    base16 = datetime(basedate.year, basedate.month, basedate.day, 0, 0, 0)
    secs = timetag16
    if basedate.hour >= 12:
        secs += 43200
    time16 = base16 + timedelta(seconds=secs)
    return date2timetag(time16)

Code to reproduce :

import datetime

from pyspartn import SPARTNReader, SPARTNMessage

msgs = []

key = "930d847b779b126863c8b3b2766ae7cc"

# msgs.append(b's\x00\x14h\x04\x120[\x11\x08(\x18\x14&\x98\x9e|\xdaz\x14\x01\xc0\xa5HS\xbc\xdb\xba\xc7\xcd\x83VuQ\xa5O\xc6\xc8]G\xe8\xc6t\xbcX\xd79\xe1*6\xa9\xfe\xc2')
# msgs.append(b's\x00\x0e\xec\x10\x1d [\x1e\xc8\xd6_\xf6q\xb8\x94\xdfja\x7fLh\x94\x89#G9\xbd%\xdfi\x15B\xc6\xe6y\xb3V\t\x04D)')
msgs.append(
    b's\x02\xb8j\x18\xd7)+p[\x10\x88\x04\xcf\xe7\xb3\xec\xf2l\xc1\xd9J\xd2qj\xc4\x8fW[\\\x08\x87\xe4\xa0\xb1\x1c\xb3\x96\xfc\xdci\x07\xf2\xf5\'5\t\x7f\xd0.\xe42\x0c\x0b\xb8\xd9r|\x8b\xcf\xd8c\x02\xe8t~\xfayqJHVU\xb9D\x1b\x02\xe1\xedK\xb6%p\xf1Z\xc2y+$M\xf9\xad\xa1?\xcb\xde6\xc0\x99\x02)\x7f\xde\xb1\xc85\xc7\xab\xc0x\x9f(\xc3K\xe66\xa0c\\4d\xf6\xb0\xaf-\x10x\x06\xa7\xadGl4\xec\xe6_\xa5;\xde\x12\xb4_\xdd\x10\x83J\xfb\xf6!\xf5]\xeaR\x08\x06)\x9fg\x03${\x10\xefVR+\xa7d=\x8d\x97U\xe7\x88\xc7\x01&\xccD#j\x19d\x87\x92">\xcb\xdaOET\xbfL\xfc\x8bQ{\xca\xb9\x89\xae\xe0\xd6\x7f\xcf\x154\n\xe7\x87Bau\xb5\xf3&\xcb\x8dp\xbbi\xcck\x99\xf4\xdfq\xd5\xa4\x85U\xb8\xb0NE\x8eO\x1a\xc9\xf6\xf3\x95\xcc\xc7\xe0\xa5~\x97\r\xca\x954.\x0e\x0c5\xfc\x011\xdbc\x8d\x1e\x8d\x84>\x07\x0cL\x87\xc9\xec\xa5\xecBo\xce\xc13:\xcd\x83\x824~\xd05\xa3\xcaL\xb1M\xc3d\xba\xe0ob\xceC\xf3\xa8Vw\xaakJ\xd8dT\x83D\x03g\xad\x04\xb0s\x90!BJ\xa8zh!\xa5\xe1\xffiYb\xf3\x16\x18\xbd\xca,\xe8v\xff\xf6gLq\xeb\xbf\xe0\x02c\xf0\xa8\xf9\xd3\x11\x07\x9f\x1a\xc3\xd4\x867\x89\xf1\x0e\xe6\x99\xf3\x06\x87\x1e-m\xd4\x9a\xf1\xec\r\xf2\xb4P8')
msgs.append(
    b's\x02\x9d\xeb\x18\xd7)+p[\x11\x08P\xe3T\xd7\xbe5\xd9Q\x86\x95\xc2\xe3x\xbdK\x8aF)\xdf\xe7\xe3\xf7H\x076G\xab\xa9:\xa1\xc0\xd5;\xd2}t\xbc\x80\xe5\x88\xc0\xa7\xba7\xb93\xc0\xc7\xab\xfd,@\xe5\x9f\xff6\xae\xac\xe8-,U(\xdf[\xe4\x0c\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\xac\xf9\xe4\xf4\xe4\xdbU\x13\xb7\xc5\xdc\x9b\xca\xf3\xee\x0b!\x9aI\x1bP}\xc2\xcd+\x9b\x96{\x91\xcbL\x84T\xdb\t,\xcf\xa7\x9f\x93\x9b\x8d\x93\xb99\xbb\xd2\\;\xb5x\xc7\x9f%F\x95wzX\x01\x1d\x80\x15\xe0|Ix%\xe8\x90\xe3ps\x1f\x8e\x90+\xa5\xbfXB(\x85\xde\xb1\xffY\xe0N\xc2~\xb1\x01\xed\xd2\xe6\x00\x9c]\x9f%\x84\xcbB\xbf\x89\xb4\xda\x8b\xf6/\xaa\t\xc5\xb5\xe2&\x1e\xcdk\xe2r\x11\xf0\x0cz\x9b\xa9v\x99\xe0~\x8d\xf5W\xde\xcf\n\xac{\x88\x0e')
msgs.append(
    b's\x02\xc8\xee8\xd7\'\xda\x10[\x10\x08<uA)]O\xe24\x95\xbd)\x05\x9e)\x02M\x16\xb2\xdd\xbe\x1c\xce\x15\x0b\x03\xc2\xb1\x8e"\xcd\xd1\xce0\x8di}\xc5$\x8ezd\xc6\x83]QO\xbf\xf44\x95\nv\xb0\x16\xec\xc1\xc3\xc0\x9d\x95\xdc\x9cR\x07l\xda\xc8\x05{\x18|\x11\x81\xdc\x1c\x84\x14\x91\x87\xba\xbc\x05L\xa4\xaa\xed\x8d\rsfqOh\xd7\x17b\x92w\x13\x959\x9cT\x8d\xd7\x0f\xd4\xb0/\x8a\xcfH\xe9\xce\xd0\xd3\x0e\x85R\xb9N7\x18Q\xddK\xb2z\xe5\xc5\xef\x82\x06\x9b\x94\x0c\xa8Sl\xe6\xb0QN\xe7\xd0Y\xab\x8f\x03\xa9n \r\x0f`\xe3\xfd*\xcbN\xe3\x08\x08\x0c\xb5\xb7h:\x06\xe1\x98\xc6At\xdah\xcd\xd7JC\xe9\x0f\xca\xd8\x96\xcc\xd08\xdb\xabw\xa4\xa7+c\xff\x8b\x056\x98\xf1\xca\x19l\x04V!p\xe2\xf3K\xaf\xe2b\xae\x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Z\xd3X[\xdd\x05\xe8Q\x96\x01q\xdb\xd1\x8cgI\xe1~5Y\xd8\x82\x9b#\xaa\xe6\xe9\x01\xf9\x0f\x15f)\xee\x03_s\xdb\xdeWh\xa9;\xbeC\xf4\xa2\xb5U\xe9\xf5\r\x1f,q\x8co\x9a\xef\x7f\xc9\x8e\xf7\xbe\xb6Rg\xe3\x99\xa3J\xf8.\x94]U\x9d\x1f\xa9F:\x93\xca\xce)\x92hF\xa9[!\x08\xf6\xce\xdfO`\xfe'\xad5\xa33\xda\x8aq\x80\x92\xdb\x06r\xc4S:\r\x13d$\xa8\x1e\xc8b\xbb\xed\xd9\xfaC\xb3\xee\xc0~<\xaa\x044Q\xca!>\xfe>\x84E\x8a\xf4*\x19\xb9X\xe9\x9c\x1b{\x95\x13\x1b$v\x045hy21\xa8D\x06\x9f7\x7f\xbf\x1b\x95\xd8\xea]-\xc7mM[\x85*\x10\xfc\xff\x1d\x87s\x82k\x92\xf02\xb6\xd0\xb7\xe5\xe4*\x92\xb1R\x90\xb4\xc3\x07\r\xda-$\xe2\xd4:\xe7\x95\x03\x02\x16/\x82o\x8ag\xc6\xdbz\xe8")
msgs.append(b"s\x02'`8\xd7'\xda\x10[\x10\xc8\xb8\xf0\x1b\xe6\xba\xc9:\x1a\xb6{\xae$cf\\k3\xed\xec\xac\xc4T\xad{\xbe:/7\x0b\x89zHa\x14\x91\xdfJ\xc3(\\\xad\x14\xc5I\x9d|\x8c\xc6\xd1\x97\x0fW\xd34\xa7\xf8\xcb\x1a\x1c\x0c6wT7:g\xfdineJ\x18\xb0\x8c\xb3\x96\x92u$\xf3\xd9")
msgs.append(
    b's\x02\xeb\xe5(\xd7\'\xda\x80[\x10\x08\x84\xc4\xf4\xb5r\x17\xb9e\x82\x9c#J\xe7|;\xce;NZ\x93\xf3\x80\x9c\xc7s\x1dG\xdc\xde\xfc\x92j\xe4-R\xb7\xcb\xac\x14\xff\x04`_xI\xdd\xb72\x06SC{c\x9e_R\x8e\x17\xf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x84\x06lbH\x07bX1\xf0\xcda1/\xc0?\xa2\xb0-\xe3\xfd\xed\x01\xb15Z\\\x97\xea\x1f\x8a\x9d\x08\xa8wO"\x95\xfdJ\xbb}EC\x0c\x99\xf1\xf3\xfa\xbe,1\xf2\xd8\x01Rf/\xa2\xf5x\x8e\x90\xc8B?\x15\xe7\x95\xe6\x1b?\x10\xc4\xdb\x01f\xf6t\x03\xc2\x1a\xb5\xf6CX\xfa\xc0A\xfe\xc47-\xe86\xeb\x9b\xe1\xb9\xa7\xf4\xf0\xcd\x91\x86\xf3\x1c\xb9\x9a\xde\x0b<\xce\x02\x12\x13\x8e\xddV\xa1_\xe4\xcc\xb9\t\xb6\x7f\x01\x83|\xf4\xc6\xb8uso\xa8\x05K\x08')
msgs.append(b"s\x02+\xef(\xd7'\xda\x80[\x11\x08N(pwa\x85\xab\x9a90y\xf6+3\x88C\xb8\x97^|\xba\xdb5\xa1\xba\x81\xb6$Z\x94\xd6\xdfE\r>8\xc7\xd2U:\x1b}z\xb9|$\xd7\x8bs>I\x01i\xe4\x8eL}\x06\xe8\x99\xff\xc0h\xd5\xee\xc6\xb7\xeaAb{\x13\x8dc\xa0N\x1c\x05\x8f\x08a?\xa9g9i(\t\xc5&")
msgs.append(
    b"s\x00}\xe9\x08\xd7'\xda\x80[\x11\x88\x97\xfa%m\xeb\r\x9evnH<\xe0\xca\xa9a@\xef\xb8\xd9E\xd4\xban\xd5\xd5\x9b\xce\xe8\x11\xf0&2\x83A\x0e\x8aR\x14)\x14\xe8R\xa8+\xa4\xe3\x05B+\xc5\xe3\xd6/\x13\x84\xb7!\x98\xaecu\xb2j!kq\x82\xb6\xf2I\xec\xfc\xca\x9b\xb1\x8e\xf8\xc6\xbeC`\xe0\xa8p`Qg\xa2\x01Q\xfb\xbb\x8d\xdf\xa6\x9e\xa2i1m\x15XD\xbd\xb7\x87\x0en\xa8\xaa|\t\ro\xe6\x18  \x8a\xafQ\xd1\xeb\x9erJ\x98\x07#\x11\xa1\xec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xa2\xc9h\xd1\xa5\xf5spN\x1cc\x8aZx}\xa9X\x8c\xe4S\xe0\xe2f7\x97')
msgs.append(
    b"s\x00_\xeb(\xd7'\xda\x80[\x1fH\xa9\x81\xe5>2`~E\xa3O\xf7\x9b\x08R)Z\x96\xccI\xb0\xd0\xb4\xd4C\x8d\xcb\xcb\xadDx\x18\xcc\x94\xdbG\xce\x95\x8e\xef\xd9\xff\x1c\xef\xdd\xcf\xe8\xe7\x8fT\x80\xb5\x99\xc3\x10\x07=\x93\x82\xc9\xa4\x82\xbe)\xbc\xe6,\x16\x9e\xa5\xb4`N\x92\r2x1\x85\x82\x99M\xdd\x83\xec\xf3\xe8ov\x0c\xe5!\xcb\x85d\x8di\xe4Q\x1d\xcaE))\x871\x81\xc5\xbd\xbfc<\x0f\x82\xe0\x8b\x81\xd2\xc8T0\xfaz'\xdb\xf0\xc0\xa8\x8c\x13x\x93\x88$\x9c\xe7;\xfd\xa8\xe5t\xeb~Mc\x1aY\x80d\xcd_\xf1y\x94\xbd\xe5\xc6\xb6\xfd\x14i\xb6\x1e\xc0\x14\x96$]\xe1(\xe1\xb7v\x02$?\x9bd\xef\x8d\xc9\x12\xff\x99j3\x95U\xf5\x8e1p$\xa9\xd9")
msgs.append(b's\x00\x14h\x04\x12\xa8[\x11\xc8V\xf4^R\xac\xaf\xc7.Q\t\x82\x9c\xa3\xc2\x02H\xca\xb1J\xc4\x18\xc6A:\x90qZ-\x08\xee\xf8\xa8\x93\xc3\xfa\xcb\xdf\x9a\xa9\x0e\xaa\x9e\xf1')
msgs.append(b's\x00\x0e\xec\x10\x1d\x98[\x1f\x88r^\\\xff\xf6\x1a\xf8\x97\xf0)u"\xb1\x04\xe0\xb8\x1b\xbeu\x8d\xcao{\x01sUn\xc1\xc1b\x05I')
msgs.append(b"s\x00\x14h\x04\x12\xd0[\x12\x08&\x9b\nOi\x90\xe3\xccKp\x99\xc7w9\xd4\x8cY\xed'\xd2\xcb\x97`\xa4mT\xb5S\xbe&\xac-\x00u\x19\x1d\xcch\x96\x87\xcat\xcc")

msgs.append(b"s\x00\x0e\xec\x10\x1d\xc0[\x1f\xc8'e\x0f\xfc\xc4\x14z\x96\xc4h\x14\xe1\xec=\x05\x03\xc9\xa1h\xab\xc9\x99w\xbfN\x1fh\xee\xad\xb4\xf7\x1d")
msgs.append(b's\x00\x0c\xed4\x12`[\x17\xc8\x1c\\\x8d}\xf2\xafp=\xc9\x02\xf2J\xab\xa2\xd2Pc\x11\x00\r\xac\x16\xec\xd6\xd4j\x95?')
msgs.append(b"s\x00\x11c$\x12\xd0[\x1f\xc8\xd8O\x9e\x0f\x04X\xe7N7!\x9c\xa6g\x8c7\xbf[\t\x1c'\x99k\x954\xb8\xe8jR\x9dmBG\xd8\x07\x05\xd5K")

for msg in msgs:
    try:
        parsed = SPARTNReader.parse(msg, decode=True, key=key, basedate=datetime.datetime.utcfromtimestamp(1713519000))
        # print(parsed._decode)
        parsed_typed: SPARTNMessage = parsed
        print(f"{parsed_typed.identity}")
    except Exception as e:
        print(e)
semuadmin commented 7 months ago

Hi again @jonathanmuller

So - yes - if the decryption is incorrect the entire payload will be junk - there's no middle ground there!

Are you able to ascertain if this issue only affects SPARTN-1X-OCB-GLO messages with 16-bit timetags (timeTagtype = 0), or does it also affect other GNSS types (e.g. OCB-GPS or OCB-GAL) with 16-bit timetags?

Are you able to confirm whether SPARTN-1X-OCB-GLO messages with timeTagtype = 1 (32-bit timetag) are being decrypted correctly?

This is puzzling because, based on the SPARTN sources I've been using (Thingstream PointPerfect MQTT and NTRIP services), all SPARTN-1X-GAD (global area definition) messages have 16-bit timetags (timeTagtype = 0) but these are definitely being decrypted correctly provided I use the correct key and basedate. I can verify this against exemplary output - see for example the gad_plot_map.png illustration. If I deliberately provide an invalid basedate, the GAD output is - as expected - gibberish.

The fact that GAD messags with timeTagtype = 0 are decrypted correctly suggests that the basic IV (Initialisation Vector) and decryption algorithms in pyspartn are working as expected as long as the key and basedate values are correct.

Unfortunately I don't have access to exemplary output for OCB or HPAC messages so it's much more difficult to ascertain whether they are in fact being decoded correctly. There's no explicit encryption validation CRC or hash, for example (understandably, as this is good cryptographic practice). One avenue I can use is to compare them with contemporary output from the Thingstream PointPerfect NTRIP service, which outputs unencrypted SPARTN messages (eaf=0).

Few suggestions just to discount any obvious hiccups (I'm sure you've checked these but just to confirm):

  1. I'm assuming the key you're using is valid, but it might be worth double checking it hasn't expired.
  2. What are using as the basedate? If a datetime value, what value are you using and how have you obtained that?
  3. Have you tried using a 32-bit timetag from the same datastream in place of a datetime value as the basedate (pyspartn will accept either) - i.e. the gnssTimeTag value from a contemporaneous message with timeTagtype = 1 (maybe an HPAC message)?
  4. Have you tried using the spartn_mqtt_client.py or spartn_ntrip_client.py examples to obtain a 'real time' SPARTN data stream, by way of comparison?

In essence, can we discount the possibility that this is simply an issue of incorrect decryption parameters (key and basedate)?

jonathanmuller commented 7 months ago

I can confirm it's only SPARTN-1X-OCB-GLO messages. In the same stream (60s of data), all HPAC, GAD and OCB messages are correctly decrypted, except the SPARTN-1X-OCB-GLO

1) Key looks valid as all other messages are decrypted correctly 2) I'm using basedate=datetime.datetime.utcfromtimestamp(1713519000))with this epoch being the time I started the mqtt client 3) I'll try that, but as all the other messages of the stream are correctly parsed I'm relatively confident the base time is correct 4) Yes, I'm using convert_timetag and copy/paste the messages for convenience

I'm relatively sure that's it's only a decryption problem for GLONASS ambiguity on 16 bits timestamp (for SPARTN-1X-OCB-GLO). I'll double check with HPAC-GLO decryption just to make sure.

Keeping you updated

semuadmin commented 7 months ago

I'll review the latest SPARTN protocol specification again to see if there's anything I may have missed with regard to special treatment for GLO 16-bit decryption, but as I recall it's fairly vague around the whole timetag ambiguity area. The IV and timetag conversion algorithms used by pyspartn were arrived at after lengthy discussions on the u-blox community forum.

jonathanmuller commented 7 months ago

I just tried with a fresh stream : datetime.datetime(2024, 4, 22, 7, 35, 0) SPARTN-1X-OCB-GLO decryption worked without trouble ( I only launched the test on few minutes of data) That leaves 2 main solution :

I'll try again with a stream recorded few minutes before and after this critical time switch that is midday UTC+0

jonathanmuller commented 7 months ago

That seems to be it : to get a correct parsing (or at least what seems to be valid) of SPARTN-1X-OCB-GLO I need to set a datetime 3h in the future to account for conversion between GPS UTC+0 and Moscow UTC+3. Obviously it works most of the day without trouble unless I record a stream between 12h and 15h. Just for completeness I'll try after 15h to see if parsing works again because the shift is "resolved".

There also seem to be a problem with datetime counting leap seconds while GPS doesn't, but as it's only 18s shift it's really hard to reproduce.

Let me know how I can help you and if you would like me to try open a PR for those 16 bits resolution ambiguity time shift.

EDIT : confirmed, parsing after 15h (UTC+0) works

semuadmin commented 7 months ago

That seems to be it : to get a correct parsing (or at least what seems to be valid) of SPARTN-1X-OCB-GLO I need to set a datetime 3h in the future to account for conversion between GPS UTC+0 and Moscow UTC+3. Obviously it works most of the day without trouble unless I record a stream between 12h and 15h. Just for completeness I'll try after 15h to see if parsing works again because the shift is "resolved".

There also seem to be a problem with datetime counting leap seconds while GPS doesn't, but as it's only 18s shift it's really hard to reproduce.

Let me know how I can help you and if you would like me to try open a PR for those 16 bits resolution ambiguity time shift.

EDIT : confirmed, parsing after 15h (UTC+0) works

I can't fault your logic, but this feels incredibly counter-intuitive to me. Should a comparable UTC+n adjustment apply to, say, BEI or QZS constellations based on their respective base timezones?

Can I clarify a couple of points:

  1. What specific criteria are you using to confirm that the HPAC or OCB payloads are in fact being decrypted correctly? Do you have exemplary data to validate the output against?
  2. Have you tried using, as a basedate, a 32-bit timetag from an OCB-GLO message in the same datastream - i.e. use the 32-bit timetag from an OCB-GLO message with timeTagtype = 1 to decrypt one with a 16-bit timetag (timeTagtype = 0), without making any further adjustment based on UTC? If so, does it work?

I'm kind of in two minds about whether to implement a code fix for this UTC basedate adjustment, or simply add an advisory in the README. If we're to add an automated UTC adjustment, I'd like to see it tested against all possible GNSS constellation types (i.e. OCB-GPS, OCB-GAL, OCB-GLO, OCB-BEI and OCB-QZS messages with timeTagtype = 0). If we can establish it definitely works in all cases, very happy to consider a PR.

semuadmin commented 7 months ago

FYI As an aside, I've added a further fix in RC 0.3.1 to use the correct Area Count iteration value. I realised the attribute SF030 Area Count represents (area count - 1), not (area count) as previously handled. This will affect HPAC and GAD payloads but not OCB, and it has no direct bearing on the conversation above. I presume it was done this way so as to accommodate up to 32 iterations in a 5-bit field, but AFAICS it's not explicitly stated in the SPARTN specification.

semuadmin commented 7 months ago

FYI As an aside, I've added a further fix in RC 0.3.1 to use the correct Area Count iteration value. I realised the attribute SF030 Area Count represents (area count - 1), not (area count) as previously handled.

As part of this change, I've added a protected attribute _padding to all SPARTNMessage objects. This contains the number of redundant bits added to the payload in order to byte-align the payload with the exact number of bytes specified in the transport layer payload length attribute nData i.e. (actual content length in bits + msg._padding = msg.nData * 8). If the payload has been successfully decrypted and decoded, this value should always be >=0, <=8, so checking the value provides a rough-and-ready (but by no means definitive) indication of successful decryption.

jonathanmuller commented 7 months ago
1. What specific criteria are you using to confirm that the HPAC or OCB payloads are in fact being decrypted correctly? Do you have exemplary data to validate the output against?

2. Have you tried using, as a `basedate`, a 32-bit timetag from an OCB-GLO message in the same datastream - i.e. use the 32-bit timetag from an OCB-GLO message with `timeTagtype` = 1 to decrypt one with a 16-bit timetag (`timeTagtype` = 0), without making any further adjustment based on UTC? If so, does it work?
  1. I'm verifying the message is fully parsed (between 0 and 7 bits left and not trying to read above the message limit), as well as that all the Do Not Use (DNU) flags are set to 0, and that the Solution issue of update (SIOU) follow the other messages.
  2. That is a really clever idea and would have saved time if I though about it earlier, nice one ! Here are the timestamp in a continuous stream (few seconds of data) :

As you can see, GLO = GPS time + 3600*3 - 18 However, it also seems there is a 14 second difference between BeiDou and GPS ... : BeiDou = GPS + 14

I'll edit my message/re-comment when I have more details

Message Name 32 Bit Timestamp 16 Bit Timestamp
SPARTN-1X-GAD 5430
SPARTN-1X-GAD 5430
SPARTN-1X-HPAC-GPS 451488630
SPARTN-1X-HPAC-GPS 451488630
SPARTN-1X-HPAC-GPS 451488630
SPARTN-1X-HPAC-GPS 451488630
SPARTN-1X-HPAC-GPS 451488630
SPARTN-1X-HPAC-GLO 451499412
SPARTN-1X-HPAC-GLO 451499412
SPARTN-1X-HPAC-GLO 451499412
SPARTN-1X-HPAC-GLO 451499412
SPARTN-1X-HPAC-GLO 451499412
SPARTN-1X-HPAC-BEI 451488616
SPARTN-1X-HPAC-BEI 451488616
SPARTN-1X-HPAC-BEI 451488616
SPARTN-1X-HPAC-BEI 451488616
SPARTN-1X-HPAC-BEI 451488616
SPARTN-1X-HPAC-GAL 451488630
SPARTN-1X-HPAC-GAL 451488630
SPARTN-1X-HPAC-GAL 451488630
SPARTN-1X-HPAC-GAL 451488630
SPARTN-1X-HPAC-GAL 451488630
SPARTN-1X-OCB-GPS 451488630
SPARTN-1X-OCB-GLO 451499412
SPARTN-1X-OCB-BEI 451488616
SPARTN-1X-OCB-GAL 451488630
SPARTN-1X-OCB-GPS 5435
SPARTN-1X-OCB-GLO 16217
SPARTN-1X-OCB-BEI 5421
SPARTN-1X-OCB-GAL 5435
SPARTN-1X-OCB-GPS 5440
SPARTN-1X-OCB-GLO 16222
SPARTN-1X-OCB-BEI 5426
SPARTN-1X-OCB-GAL 5440
semuadmin commented 7 months ago

👍

1. I'm verifying the message is fully parsed (between 0 and 7 bits left and not trying to read above the message limit), as well as that all the Do Not Use (DNU) flags are set to 0, and that the Solution issue of update (SIOU) follow the other messages.

This is essentially what I've been doing as well in debug mode - hopefully the new _padding attribute makes this a bit easier.

semuadmin commented 7 months ago

As you can see, GLO = GPS time + 3600*3 - 18 However, it also seems there is a 14 second difference between BeiDou and GPS ... : BeiDou = GPS + 14

Yeah different GNSS constellations follow different leap second adjustment rules (I have an old private GitHub branch which was intended to address this for historical logs - I may dust it off sometime). It's not normally a major issue in practice but it's clearly significant in the context of using (reasonably) accurate timestamps to derive cryptographic initialisation vectors (IV). I understand that it's good practice to use 'unpredictable' values in IV derivations, but I really wish u-blox hadn't chosen basedate given all the potential confusion around UTC and leap second adjustments. I suspect their intention was for end users to always use a 32-bit timetag from the same datastream, but this is geared towards 'real time' embedded applications.

semuadmin commented 7 months ago

FYI see https://github.com/semuconsulting/pyspartn/pull/19 - not sure this is a complete fix, but it should work OK provided there are sufficient unambiguous 32-bit timetags in the incoming datastream.

There is still value in applying an automatic UTC (and ideally leap second) shift to any 'basedate' input argument to cater for situations where there is no available 32-bit timetag in the incoming datastream. The only other option I can see is to have separate 'basedate' arguments for each msgSubtype (i.e. GNSS constellation), which feels awfully clunky.

If you have the time, give this branch a go and let me know how you get on. Feedback and corrections welcome.

jonathanmuller commented 7 months ago

I was thinking about doing it simpler (but your PR is obviously a great addition !) Given a close enough date-time and an ambiguous 16 bits time :

That way, as long as the provided date-time is +/- 6h accurate, the resolution should work, regardless of GNSS time difference (even the +3h of Glonass)

I'll try to open a PR as soon as I can find some time

semuadmin commented 7 months ago

That would also work.

I think collating 32-bit timetags from the data stream for use as basedates for 16-bit timetags is a solid policy and - I suspect - u-blox's intention for implementation. But the method for providing a 'backup' basedate for when there is no 32-bit timetag available (yet) is up for grabs. Happy to consider alternative approaches.

The basedate provided to SPARTNMessage for an individual message must obviously correspond to that specific message.

I think I'll merge the PR for RC 0.3.2 sooner rather than later as it includes a bunch of other enhancements I've had on the pile for a while now (iincluded automatic conversion of float values and a bunch of enumerations), but we can look to enhance further in RC 0.3.3.

semuadmin commented 7 months ago

PR has automatically closed this but feel free to re-open, or raise another issue/PR, if you have a better approach.

I've opened a Discussion for v0.3.2-beta so feel free to contribute to that - https://github.com/semuconsulting/pyspartn/discussions/20