Exemplo n.º 1
0
/* decode the service request only */
int whohas_decode_service_request(
    uint8_t * apdu,
    unsigned apdu_len,
    BACNET_WHO_HAS_DATA * data)
{
    int len = 0;
    uint8_t tag_number = 0;
    uint32_t len_value = 0;
    uint32_t decoded_value = 0; /* for decoding */
    uint16_t decoded_type = 0;  /* for decoding */

    if (apdu_len && data) {
        /* optional limits - must be used as a pair */
        if (decode_is_context_tag(&apdu[len], 0)) {
            len +=
                decode_tag_number_and_value(&apdu[len], &tag_number,
                &len_value);
            len += decode_unsigned(&apdu[len], len_value, &decoded_value);
            if (decoded_value <= BACNET_MAX_INSTANCE)
                data->low_limit = decoded_value;
            if (!decode_is_context_tag(&apdu[len], 1))
                return -1;
            len +=
                decode_tag_number_and_value(&apdu[len], &tag_number,
                &len_value);
            len += decode_unsigned(&apdu[len], len_value, &decoded_value);
            if (decoded_value <= BACNET_MAX_INSTANCE)
                data->high_limit = decoded_value;
        } else {
            data->low_limit = -1;
            data->high_limit = -1;
        }
        /* object id */
        if (decode_is_context_tag(&apdu[len], 2)) {
            data->object_name = false;
            len +=
                decode_tag_number_and_value(&apdu[len], &tag_number,
                &len_value);
            len +=
                decode_object_id(&apdu[len], &decoded_type,
                &data->object.identifier.instance);
            data->object.identifier.type = decoded_type;
        }
        /* object name */
        else if (decode_is_context_tag(&apdu[len], 3)) {
            data->object_name = true;
            len +=
                decode_tag_number_and_value(&apdu[len], &tag_number,
                &len_value);
            len +=
                decode_character_string(&apdu[len], len_value,
                &data->object.name);
        }
        /* missing required parameters */
        else
            return -1;
    }

    return len;
}
Exemplo n.º 2
0
/* decode the service request only */
int ihave_decode_service_request(
    uint8_t * apdu,
    unsigned apdu_len,
    BACNET_I_HAVE_DATA * data)
{
    int len = 0;
    uint8_t tag_number = 0;
    uint32_t len_value = 0;
    uint16_t decoded_type = 0;  /* for decoding */

    if (apdu_len && data) {
        /* deviceIdentifier */
        len +=
            decode_tag_number_and_value(&apdu[len], &tag_number, &len_value);
        if (tag_number == BACNET_APPLICATION_TAG_OBJECT_ID) {
            len +=
                decode_object_id(&apdu[len], &decoded_type,
                &data->device_id.instance);
            data->device_id.type = decoded_type;
        } else
            return -1;
        /* objectIdentifier */
        len +=
            decode_tag_number_and_value(&apdu[len], &tag_number, &len_value);
        if (tag_number == BACNET_APPLICATION_TAG_OBJECT_ID) {
            len +=
                decode_object_id(&apdu[len], &decoded_type,
                &data->object_id.instance);
            data->object_id.type = decoded_type;
        } else
            return -1;
        /* objectName */
        len +=
            decode_tag_number_and_value(&apdu[len], &tag_number, &len_value);
        if (tag_number == BACNET_APPLICATION_TAG_CHARACTER_STRING) {
            len +=
                decode_character_string(&apdu[len], len_value,
                &data->object_name);
        } else
            return -1;
    } else
        return -1;

    return len;
}
Exemplo n.º 3
0
/* decode the service request only */
int rd_decode_service_request(
    uint8_t * apdu,
    unsigned apdu_len,
    BACNET_REINITIALIZED_STATE * state,
    BACNET_CHARACTER_STRING * password)
{
    unsigned len = 0;
    uint8_t tag_number = 0;
    uint32_t len_value_type = 0;
    uint32_t value = 0;

    /* check for value pointers */
    if (apdu_len) {
        /* Tag 0: reinitializedStateOfDevice */
        if (!decode_is_context_tag(&apdu[len], 0))
            return -1;
        len +=
            decode_tag_number_and_value(&apdu[len], &tag_number,
            &len_value_type);
        len += decode_enumerated(&apdu[len], len_value_type, &value);
        if (state)
            *state = (BACNET_REINITIALIZED_STATE) value;
        /* Tag 1: password - optional */
        if (len < apdu_len) {
            if (!decode_is_context_tag(&apdu[len], 1))
                return -1;
            len +=
                decode_tag_number_and_value(&apdu[len], &tag_number,
                &len_value_type);
            len +=
                decode_character_string(&apdu[len], len_value_type, password);
        }
    }

    return (int) len;
}
Exemplo n.º 4
0
void DecodeBlock(
    char cBlockNum,
    uint8_t * pData)
{
    int iLen;
    uint32_t ulTemp;
    int tag_len;
    uint8_t tag_number;
    uint32_t len_value_type;
    BACNET_CHARACTER_STRING bsName;
    DATABLOCK Response;

    iLen = 0;

    if (cBlockNum >= MY_MAX_BLOCK)
        return;

    tag_len =
        decode_tag_number_and_value(&pData[iLen], &tag_number,
        &len_value_type);
    iLen += tag_len;
    if (tag_number != BACNET_APPLICATION_TAG_UNSIGNED_INT)
        return;

    iLen += decode_unsigned(&pData[iLen], len_value_type, &ulTemp);
    Response.cMyByte1 = (char) ulTemp;

    tag_len =
        decode_tag_number_and_value(&pData[iLen], &tag_number,
        &len_value_type);
    iLen += tag_len;
    if (tag_number != BACNET_APPLICATION_TAG_UNSIGNED_INT)
        return;

    iLen += decode_unsigned(&pData[iLen], len_value_type, &ulTemp);
    Response.cMyByte2 = (char) ulTemp;

    tag_len =
        decode_tag_number_and_value(&pData[iLen], &tag_number,
        &len_value_type);
    iLen += tag_len;
    if (tag_number != BACNET_APPLICATION_TAG_REAL)
        return;

    iLen += decode_real(&pData[iLen], &Response.fMyReal);

    tag_len =
        decode_tag_number_and_value(&pData[iLen], &tag_number,
        &len_value_type);
    iLen += tag_len;
    if (tag_number != BACNET_APPLICATION_TAG_CHARACTER_STRING)
        return;

    iLen += decode_character_string(&pData[iLen], len_value_type, &bsName);
    strncpy(Response.sMyString, characterstring_value(&bsName), MY_MAX_STR);
    Response.sMyString[MY_MAX_STR] = '\0';      /* Make sure it is nul terminated */

    printf("Private Transfer Read Block Response\n");
    printf("Data Block: %d\n", (int) cBlockNum);
    printf("  First Byte  : %d\n", (int) Response.cMyByte1);
    printf("  Second Byte : %d\n", (int) Response.cMyByte2);
    printf("  Real        : %f\n", Response.fMyReal);
    printf("  String      : %s\n\n", Response.sMyString);
}
Exemplo n.º 5
0
static void ProcessPT(
    BACNET_PRIVATE_TRANSFER_DATA * data)
{
    int iLen;   /* Index to current location in data */
    char cBlockNumber;
    uint32_t ulTemp;
    int tag_len;
    uint8_t tag_number;
    uint32_t len_value_type;
    BACNET_CHARACTER_STRING bsTemp;

    iLen = 0;

    /* Decode the block number */
    tag_len =
        decode_tag_number_and_value(&data->serviceParameters[iLen],
        &tag_number, &len_value_type);
    iLen += tag_len;
    if (tag_number != BACNET_APPLICATION_TAG_UNSIGNED_INT) {
        /* Bail out early if wrong type */
        /* and signal unexpected error */
        data->serviceParametersLen = 0;
        return;
    }

    iLen +=
        decode_unsigned(&data->serviceParameters[iLen], len_value_type,
        &ulTemp);
    cBlockNumber = (char) ulTemp;
    if (cBlockNumber < MY_MAX_BLOCK) {
        if (data->serviceNumber == MY_SVC_READ) {
            /*  Read Response is an unsigned int with
               0 for success or a non 0 error code
               For a successful read the 0 success
               code is followed by the block number
               and then the block contents which
               consist of 2 unsigned ints (in 0 to 255
               range as they are really chars) a single
               precision real and a string which
               will be up to 32 chars + a nul */

            iLen = 0;

            /* Signal success */
            iLen += encode_application_unsigned(&IOBufferPT[iLen], MY_ERR_OK);
            /* Followed by the block number */
            iLen +=
                encode_application_unsigned(&IOBufferPT[iLen], cBlockNumber);
            /* And Then the block contents */
            iLen +=
                encode_application_unsigned(&IOBufferPT[iLen],
                MyData[(int8_t) cBlockNumber].cMyByte1);
            iLen +=
                encode_application_unsigned(&IOBufferPT[iLen],
                MyData[(int8_t) cBlockNumber].cMyByte2);
            iLen +=
                encode_application_real(&IOBufferPT[iLen],
                MyData[(int8_t) cBlockNumber].fMyReal);
            characterstring_init_ansi(&bsTemp,
                (char *) MyData[(int8_t) cBlockNumber].sMyString);
            iLen +=
                encode_application_character_string(&IOBufferPT[iLen],
                &bsTemp);
        } else {
            /* Write operation */
            /*  Write block consists of the block number
               followed by the block contents as
               described above for the read operation.
               The returned result is an unsigned
               response which is 0 for success and
               a non 0 error code otherwise. */

            tag_len =
                decode_tag_number_and_value(&data->serviceParameters[iLen],
                &tag_number, &len_value_type);
            iLen += tag_len;
            if (tag_number != BACNET_APPLICATION_TAG_UNSIGNED_INT) {
                data->serviceParametersLen = 0;
                return;
            }
            iLen +=
                decode_unsigned(&data->serviceParameters[iLen], len_value_type,
                &ulTemp);
            MyData[(int8_t) cBlockNumber].cMyByte1 = (char) ulTemp;

            tag_len =
                decode_tag_number_and_value(&data->serviceParameters[iLen],
                &tag_number, &len_value_type);
            iLen += tag_len;
            if (tag_number != BACNET_APPLICATION_TAG_UNSIGNED_INT) {
                data->serviceParametersLen = 0;
                return;
            }
            iLen +=
                decode_unsigned(&data->serviceParameters[iLen], len_value_type,
                &ulTemp);
            MyData[(int8_t) cBlockNumber].cMyByte2 = (char) ulTemp;

            tag_len =
                decode_tag_number_and_value(&data->serviceParameters[iLen],
                &tag_number, &len_value_type);
            iLen += tag_len;
            if (tag_number != BACNET_APPLICATION_TAG_REAL) {
                data->serviceParametersLen = 0;
                return;
            }
            iLen +=
                decode_real(&data->serviceParameters[iLen],
                &MyData[(int8_t) cBlockNumber].fMyReal);

            tag_len =
                decode_tag_number_and_value(&data->serviceParameters[iLen],
                &tag_number, &len_value_type);
            iLen += tag_len;
            if (tag_number != BACNET_APPLICATION_TAG_CHARACTER_STRING) {
                data->serviceParametersLen = 0;
                return;
            }
            decode_character_string(&data->serviceParameters[iLen],
                len_value_type, &bsTemp);
            /* Only copy as much as we can accept */
            strncpy((char *) MyData[(int8_t) cBlockNumber].sMyString,
                characterstring_value(&bsTemp), MY_MAX_STR);
            /* Make sure it is nul terminated */
            MyData[(int8_t) cBlockNumber].sMyString[MY_MAX_STR] = '\0';
            /* Signal success */
            iLen = encode_application_unsigned(&IOBufferPT[0], MY_ERR_OK);
        }
    } else {
        /* Signal bad index */
        iLen = encode_application_unsigned(&IOBufferPT[0], MY_ERR_BAD_INDEX);
    }
    data->serviceParametersLen = iLen;
    data->serviceParameters = IOBufferPT;
}