常用的应用层整型编解码函数

来源:互联网 发布:嘉兴nit软件培训 编辑:程序博客网 时间:2024/05/29 17:41
int encode_unsigned16(
    uint8_t * apdu,
    uint16_t value)
{
    apdu[0] = (uint8_t) ((value & 0xff00) >> 8);
    apdu[1] = (uint8_t) (value & 0x00ff);

    return 2;
}

int decode_unsigned16(
    uint8_t * apdu,
    uint16_t * value)
{
    if (value) {
        *value = (uint16_t) ((((uint16_t) apdu[0]) << 8) & 0xff00);
        *value |= ((uint16_t) (((uint16_t) apdu[1]) & 0x00ff));
    }

    return 2;
}

int encode_unsigned24(
    uint8_t * apdu,
    uint32_t value)
{
    apdu[0] = (uint8_t) ((value & 0xff0000) >> 16);
    apdu[1] = (uint8_t) ((value & 0x00ff00) >> 8);
    apdu[2] = (uint8_t) (value & 0x0000ff);

    return 3;
}

int decode_unsigned24(
    uint8_t * apdu,
    uint32_t * value)
{
    if (value) {
        *value = ((uint32_t) ((((uint32_t) apdu[0]) << 16) & 0x00ff0000));
        *value |= (uint32_t) ((((uint32_t) apdu[1]) << 8) & 0x0000ff00);
        *value |= ((uint32_t) (((uint32_t) apdu[2]) & 0x000000ff));
    }

    return 3;
}

int encode_unsigned32(
    uint8_t * apdu,
    uint32_t value)
{
    apdu[0] = (uint8_t) ((value & 0xff000000) >> 24);
    apdu[1] = (uint8_t) ((value & 0x00ff0000) >> 16);
    apdu[2] = (uint8_t) ((value & 0x0000ff00) >> 8);
    apdu[3] = (uint8_t) (value & 0x000000ff);

    return 4;
}

int decode_unsigned32(
    uint8_t * apdu,
    uint32_t * value)
{
    if (value) {
        *value = ((uint32_t) ((((uint32_t) apdu[0]) << 24) & 0xff000000));
        *value |= ((uint32_t) ((((uint32_t) apdu[1]) << 16) & 0x00ff0000));
        *value |= ((uint32_t) ((((uint32_t) apdu[2]) << 8) & 0x0000ff00));
        *value |= ((uint32_t) (((uint32_t) apdu[3]) & 0x000000ff));
    }

    return 4;
}

#if BACNET_USE_SIGNED
int encode_signed8(
    uint8_t * apdu,
    int8_t value)
{
    apdu[0] = (uint8_t) value;

    return 1;
}

int decode_signed8(
    uint8_t * apdu,
    int32_t * value)
{
    if (value) {
        /* negative - bit 7 is set */
        if (apdu[0] & 0x80)
            *value = 0xFFFFFF00;
        else
            *value = 0;
        *value |= ((int32_t) (((int32_t) apdu[0]) & 0x000000ff));
    }

    return 1;
}

int encode_signed16(
    uint8_t * apdu,
    int16_t value)
{
    apdu[0] = (uint8_t) ((value & 0xff00) >> 8);
    apdu[1] = (uint8_t) (value & 0x00ff);

    return 2;
}

int decode_signed16(
    uint8_t * apdu,
    int32_t * value)
{
    if (value) {
        /* negative - bit 7 is set */
        if (apdu[0] & 0x80)
            *value = 0xFFFF0000;
        else
            *value = 0;
        *value |= ((int32_t) ((((int32_t) apdu[0]) << 8) & 0x0000ff00));
        *value |= ((int32_t) (((int32_t) apdu[1]) & 0x000000ff));
    }

    return 2;
}

int encode_signed24(
    uint8_t * apdu,
    int32_t value)
{
    apdu[0] = (uint8_t) ((value & 0xff0000) >> 16);
    apdu[1] = (uint8_t) ((value & 0x00ff00) >> 8);
    apdu[2] = (uint8_t) (value & 0x0000ff);

    return 3;
}

int decode_signed24(
    uint8_t * apdu,
    int32_t * value)
{
    if (value) {
        /* negative - bit 7 is set */
        if (apdu[0] & 0x80)
            *value = 0xFF000000;
        else
            *value = 0;
        *value |= ((int32_t) ((((int32_t) apdu[0]) << 16) & 0x00ff0000));
        *value |= ((int32_t) ((((int32_t) apdu[1]) << 8) & 0x0000ff00));
        *value |= ((int32_t) (((int32_t) apdu[2]) & 0x000000ff));
    }

    return 3;
}

int encode_signed32(
    uint8_t * apdu,
    int32_t value)
{
    apdu[0] = (uint8_t) ((value & 0xff000000) >> 24);
    apdu[1] = (uint8_t) ((value & 0x00ff0000) >> 16);
    apdu[2] = (uint8_t) ((value & 0x0000ff00) >> 8);
    apdu[3] = (uint8_t) (value & 0x000000ff);

    return 4;
}

int decode_signed32(
    uint8_t * apdu,
    int32_t * value)
{
    if (value) {
        *value = ((int32_t) ((((int32_t) apdu[0]) << 24) & 0xff000000));
        *value |= ((int32_t) ((((int32_t) apdu[1]) << 16) & 0x00ff0000));
        *value |= ((int32_t) ((((int32_t) apdu[2]) << 8) & 0x0000ff00));
        *value |= ((int32_t) (((int32_t) apdu[3]) & 0x000000ff));
    }

    return 4;
}