Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decoding datetime objects - error on some devices #103

Open
Afroboltski opened this issue May 17, 2022 · 0 comments
Open

Decoding datetime objects - error on some devices #103

Afroboltski opened this issue May 17, 2022 · 0 comments

Comments

@Afroboltski
Copy link

In Serialize/ASN1.cs:

The following method:

public static int decode_bacnet_time(byte[] buffer, int offset, out DateTime btime)
{
    int hour = buffer[offset + 0];
    int min = buffer[offset + 1];
    int sec = buffer[offset + 2];
    int hundredths = buffer[offset + 3];
    if (hour == 0xFF && min == 0xFF && sec == 0xFF && hundredths == 0xFF)
    {
        btime = new DateTime(1, 1, 1);
    }
    else
    {
        if (hundredths > 100) hundredths = 0; // sometimes set to 255
        btime = new DateTime(1, 1, 1, hour, min, sec, hundredths * 10);
    }
    return 4;
}

could be re-written with the following change, to be more device-agnostic as to when a particular value is deemed to be garbage or not:

public static int decode_bacnet_time(byte[] buffer, int offset, out DateTime btime)
{
    int hour = buffer[offset + 0];
    int min = buffer[offset + 1];
    int sec = buffer[offset + 2];
    int hundredths = buffer[offset + 3];
    if (hour > 23 || min > 59 || sec > 59) // This conditional check has been changed
        btime = new DateTime(1, 1, 1);
    else
    {
        if (hundredths > 100) hundredths = 0;   // sometimes set to 255
        btime = new DateTime(1, 1, 1, hour, min, sec, hundredths * 10);
    }
    return 4;
}

I found that when using the former version in a make-shift BACnet server I rigged up, it would crash (my exception handling was non-existent at the time) every time it tried to decode a date from a particular range of Siemens BACnet devices. The crash was to do with invalid values in the DateTime constructor. The invalid values were able to bypass the following check:
if (hour == 0xFF && min == 0xFF && sec == 0xFF && hundredths == 0xFF)
because the panel sends it's "garbage" value as:

hour == 0xFF;
min == 0xFF;
sec == 0xFF;
hundredths == 0x00;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant