Example #1
0
void test_Private_Transfer_Error(
    Test * pTest)
{
    uint8_t apdu[480] = { 0 };
    int len = 0;
    int apdu_len = 0;
    uint8_t invoke_id = 128;
    uint8_t test_invoke_id = 0;
    BACNET_ERROR_CLASS error_class = ERROR_CLASS_RESOURCES;
    BACNET_ERROR_CODE error_code = ERROR_CODE_OPERATIONAL_PROBLEM;
    BACNET_ERROR_CLASS test_error_class = 0;
    BACNET_ERROR_CODE test_error_code = 0;
    BACNET_PRIVATE_TRANSFER_DATA private_data;
    BACNET_PRIVATE_TRANSFER_DATA test_data;
    uint8_t test_value[480] = { 0 };
    int private_data_len = 0;
    char private_data_chunk[33] = { "00112233445566778899AABBCCDDEEFF" };
    BACNET_APPLICATION_DATA_VALUE data_value;
    BACNET_APPLICATION_DATA_VALUE test_data_value;
    bool status = false;

    private_data.vendorID = BACNET_VENDOR_ID;
    private_data.serviceNumber = 1;

    status =
        bacapp_parse_application_data(BACNET_APPLICATION_TAG_OCTET_STRING,
        &private_data_chunk[0], &data_value);
    ct_test(pTest, status == true);
    private_data_len =
        bacapp_encode_application_data(&test_value[0], &data_value);
    private_data.serviceParameters = &test_value[0];
    private_data.serviceParametersLen = private_data_len;

    len =
        ptransfer_error_encode_apdu(&apdu[0], invoke_id, error_class,
        error_code, &private_data);
    ct_test(pTest, len != 0);
    ct_test(pTest, len != -1);
    apdu_len = len;
    len =
        ptransfer_error_decode_apdu(&apdu[0], apdu_len, &test_invoke_id,
        &test_error_class, &test_error_code, &test_data);
    ct_test(pTest, len != -1);
    ct_test(pTest, test_invoke_id == invoke_id);
    ct_test(pTest, test_data.vendorID == private_data.vendorID);
    ct_test(pTest, test_data.serviceNumber == private_data.serviceNumber);
    ct_test(pTest, test_error_class == error_class);
    ct_test(pTest, test_error_code == error_code);
    ct_test(pTest,
        test_data.serviceParametersLen == private_data.serviceParametersLen);
    len =
        bacapp_decode_application_data(test_data.serviceParameters,
        test_data.serviceParametersLen, &test_data_value);
    ct_test(pTest, bacapp_same_value(&data_value, &test_data_value) == true);
}
Example #2
0
void test_Private_Transfer_Request(
    Test * pTest)
{
    uint8_t apdu[480] = { 0 };
    uint8_t test_value[480] = { 0 };
    int len = 0;
    int apdu_len = 0;
    uint8_t invoke_id = 128;
    uint8_t test_invoke_id = 0;
    int private_data_len = 0;
    char private_data_chunk[33] = { "00112233445566778899AABBCCDDEEFF" };
    BACNET_APPLICATION_DATA_VALUE data_value = { 0 };
    BACNET_APPLICATION_DATA_VALUE test_data_value = { 0 };
    BACNET_PRIVATE_TRANSFER_DATA private_data = { 0 };
    BACNET_PRIVATE_TRANSFER_DATA test_data = { 0 };
    bool status = false;

    private_data.vendorID = BACNET_VENDOR_ID;
    private_data.serviceNumber = 1;

    status =
        bacapp_parse_application_data(BACNET_APPLICATION_TAG_OCTET_STRING,
        &private_data_chunk[0], &data_value);
    ct_test(pTest, status == true);
    private_data_len =
        bacapp_encode_application_data(&test_value[0], &data_value);
    private_data.serviceParameters = &test_value[0];
    private_data.serviceParametersLen = private_data_len;

    len = ptransfer_encode_apdu(&apdu[0], invoke_id, &private_data);
    ct_test(pTest, len != 0);
    apdu_len = len;
    len =
        ptransfer_decode_apdu(&apdu[0], apdu_len, &test_invoke_id, &test_data);
    ct_test(pTest, len != -1);
    ct_test(pTest, test_data.vendorID == private_data.vendorID);
    ct_test(pTest, test_data.serviceNumber == private_data.serviceNumber);
    ct_test(pTest,
        test_data.serviceParametersLen == private_data.serviceParametersLen);
    len =
        bacapp_decode_application_data(test_data.serviceParameters,
        test_data.serviceParametersLen, &test_data_value);
    ct_test(pTest, bacapp_same_value(&data_value, &test_data_value) == true);

    return;
}
Example #3
0
void test_Private_Transfer_Ack(
    Test * pTest)
{
    uint8_t apdu[480] = { 0 };
    int len = 0;
    int apdu_len = 0;
    uint8_t invoke_id = 128;
    uint8_t test_invoke_id = 0;
    BACNET_PRIVATE_TRANSFER_DATA private_data;
    BACNET_PRIVATE_TRANSFER_DATA test_data;
    uint8_t test_value[480] = { 0 };
    int private_data_len = 0;
    uint8_t private_data_chunk[32] = { "I Love You, Patricia!" };
    BACNET_APPLICATION_DATA_VALUE data_value;
    BACNET_APPLICATION_DATA_VALUE test_data_value;

    private_data.vendorID = BACNET_VENDOR_ID;
    private_data.serviceNumber = 1;

    bacapp_parse_application_data(BACNET_APPLICATION_TAG_OCTET_STRING,
        &private_data_chunk[0], &data_value);
    private_data_len =
        bacapp_encode_application_data(&test_value[0], &data_value);

    private_data.serviceParameters = &test_value[0];
    private_data.serviceParametersLen = private_data_len;


    len = ptransfer_ack_encode_apdu(&apdu[0], invoke_id, &private_data);
    ct_test(pTest, len != 0);
    ct_test(pTest, len != -1);
    apdu_len = len;
    len =
        ptransfer_ack_decode_apdu(&apdu[0], apdu_len, &test_invoke_id,
        &test_data);
    ct_test(pTest, len != -1);
    ct_test(pTest, test_invoke_id == invoke_id);
    ct_test(pTest, test_data.vendorID == private_data.vendorID);
    ct_test(pTest, test_data.serviceNumber == private_data.serviceNumber);
    ct_test(pTest,
        test_data.serviceParametersLen == private_data.serviceParametersLen);
    len =
        bacapp_decode_application_data(test_data.serviceParameters,
        test_data.serviceParametersLen, &test_data_value);
    ct_test(pTest, bacapp_same_value(&data_value, &test_data_value) == true);
}
Example #4
0
void test_Unconfirmed_Private_Transfer_Request(
    Test * pTest)
{
    uint8_t apdu[480] = { 0 };
    uint8_t test_value[480] = { 0 };
    int len = 0;
    int apdu_len = 0;
    int private_data_len = 0;
    char private_data_chunk[32] = { "I Love You, Patricia!" };
    BACNET_APPLICATION_DATA_VALUE data_value;
    BACNET_APPLICATION_DATA_VALUE test_data_value;
    BACNET_PRIVATE_TRANSFER_DATA private_data;
    BACNET_PRIVATE_TRANSFER_DATA test_data;
    bool status = false;

    private_data.vendorID = BACNET_VENDOR_ID;
    private_data.serviceNumber = 1;

    status =
        bacapp_parse_application_data(BACNET_APPLICATION_TAG_CHARACTER_STRING,
        &private_data_chunk[0], &data_value);
    ct_test(pTest, status == true);
    private_data_len =
        bacapp_encode_application_data(&test_value[0], &data_value);
    private_data.serviceParameters = &test_value[0];
    private_data.serviceParametersLen = private_data_len;

    len = uptransfer_encode_apdu(&apdu[0], &private_data);
    ct_test(pTest, len != 0);
    apdu_len = len;
    len = uptransfer_decode_apdu(&apdu[0], apdu_len, &test_data);
    ct_test(pTest, len != -1);
    ct_test(pTest, test_data.vendorID == private_data.vendorID);
    ct_test(pTest, test_data.serviceNumber == private_data.serviceNumber);
    ct_test(pTest,
        test_data.serviceParametersLen == private_data.serviceParametersLen);
    len =
        bacapp_decode_application_data(test_data.serviceParameters,
        test_data.serviceParametersLen, &test_data_value);
    ct_test(pTest, bacapp_same_value(&data_value, &test_data_value) == true);

    return;
}
Example #5
0
int BacnetWriteProperty(
    int deviceInstanceNumber,
    int objectType,
    int objectInstanceNumber,
    int objectProperty,
    int objectPriority,
    int objectIndex,
    const char *tag,
    const char *value)
{
    char msg[MAX_ERROR_STRING];
    int isFailure = 1;

    if (!isWritePropertyHandlerRegistered) {
        /* handle the ack coming back */
        apdu_set_confirmed_simple_ack_handler(SERVICE_CONFIRMED_WRITE_PROPERTY,
            My_Write_Property_SimpleAck_Handler);

        /* handle any errors coming back */
        apdu_set_error_handler(SERVICE_CONFIRMED_WRITE_PROPERTY,
            My_Error_Handler);

        /* indicate that handlers are now registered */
        isWritePropertyHandlerRegistered = true;
    }

    if (objectIndex == -1) {
        objectIndex = BACNET_ARRAY_ALL;
    }
    /* Loop for eary exit; */
    do {
        /* Handle the tag/value pair */
        uint8_t context_tag = 0;
        BACNET_APPLICATION_TAG property_tag;
        BACNET_APPLICATION_DATA_VALUE propertyValue;

        if (toupper(tag[0]) == 'C') {
            context_tag = strtol(&tag[1], NULL, 0);
            propertyValue.context_tag = context_tag;
            propertyValue.context_specific = true;
        } else {
            propertyValue.context_specific = false;
        }
        property_tag = strtol(tag, NULL, 0);

        if (property_tag >= MAX_BACNET_APPLICATION_TAG) {
            sprintf(msg, "Error: tag=%u - it must be less than %u",
                property_tag, MAX_BACNET_APPLICATION_TAG);
            LogError(msg);
            break;
        }
        if (!bacapp_parse_application_data(property_tag, value,
                &propertyValue)) {
            sprintf(msg, "Error: unable to parse the tag value");
            LogError(msg);
            break;
        }
        propertyValue.next = NULL;

        /* Send out the message */
        Request_Invoke_ID =
            Send_Write_Property_Request(deviceInstanceNumber, objectType,
            objectInstanceNumber, objectProperty, &propertyValue,
            objectPriority, objectIndex);
        Wait_For_Answer_Or_Timeout(100, waitAnswer);

        /* If we get here, then there were no explicit failures. However, there */
        /* could have been implicit failures. Let's look at those also. */
        isFailure = Error_Detected;
    } while (false);

    /* Clean up after ourselves. */
    Error_Detected = false;
    return isFailure;
}
Example #6
0
int main(
    int argc,
    char *argv[])
{
    BACNET_ADDRESS src = {
        0
    };  /* address where message came from */
    uint16_t pdu_len = 0;
    unsigned timeout = 10;      /* milliseconds */
    unsigned max_apdu = 0;
    time_t elapsed_seconds = 0;
    time_t last_seconds = 0;
    time_t current_seconds = 0;
    time_t timeout_seconds = 0;
    time_t delta_seconds = 0;
    bool found = false;
    char *filename = NULL;
    char *value_string = NULL;
    bool status = false;
    int args_remaining = 0, tag_value_arg = 0, i = 0;
    BACNET_APPLICATION_TAG property_tag;
    uint8_t context_tag = 0;
    BACNET_PRIVATE_TRANSFER_DATA private_data = { 0 };
    int len = 0;
    bool sent_message = false;

    if (argc < 6) {
        filename = filename_remove_path(argv[0]);
        printf("Usage: %s <device-instance|broadcast|dnet=> vendor-id"
            " service-number tag value [tag value...]\r\n", filename);
        if ((argc > 1) && (strcmp(argv[1], "--help") == 0)) {
            printf("device-instance:\r\n"
                "BACnet Device Object Instance number that you are\r\n"
                "trying to communicate to.  This number will be used\r\n"
                "to try and bind with the device using Who-Is and\r\n"
                "I-Am services.  For example, if you were transferring to\r\n"
                "Device Object 123, the device-instance would be 123.\r\n"
                "For Global Broadcast, use the word 'broadcast'.\r\n"
                "For Local Broadcast to a particular DNET n, use 'dnet=n'.\r\n"
                "\r\n" "vendor_id:\r\n"
                "the unique vendor identification code for the type of\r\n"
                "vendor proprietary service to be performed.\r\n" "\r\n"
                "service-number (Unsigned32):\r\n"
                "the desired proprietary service to be performed.\r\n" "\r\n"
                "tag:\r\n" "Tag is the integer value of the enumeration \r\n"
                "BACNET_APPLICATION_TAG in bacenum.h.\r\n"
                "It is the data type of the value that you are sending.\r\n"
                "For example, if you were transfering a REAL value, you would \r\n"
                "use a tag of 4.\r\n"
                "Context tags are created using two tags in a row.\r\n"
                "The context tag is preceded by a C.  Ctag tag.\r\n"
                "C2 4 creates a context 2 tagged REAL.\r\n" "\r\n" "value:\r\n"
                "The value is an ASCII representation of some type of data\r\n"
                "that you are transfering.\r\n"
                "It is encoded using the tag information provided.\r\n"
                "For example, if you were transferring a REAL value of 100.0,\r\n"
                "you would use 100.0 as the value.\r\n"
                "If you were transferring an object identifier for Device 123,\r\n"
                "you would use 8:123 as the value.\r\n" "\r\n" "Example:\r\n"
                "If you want to transfer a REAL value of 1.1 to service 23 of \r\n"
                "vendor 260 in Device 99, you could send the following command:\r\n"
                "%s 99 260 23 4 1.1\r\n", filename);
        }
        return 0;
    }
    /* decode the command line parameters */
    if (strcmp(argv[1], "broadcast") == 0) {
        Target_Broadcast = true;
        Target_DNET = BACNET_BROADCAST_NETWORK;
    } else if (strncmp(argv[1], "dnet=", 5) == 0) {
        Target_Broadcast = true;
        Target_DNET = strtol(&argv[1][5], NULL, 0);
    } else {
        Target_Device_Object_Instance = strtol(argv[1], NULL, 0);
    }
    Target_Vendor_Identifier = strtol(argv[2], NULL, 0);
    Target_Service_Number = strtol(argv[3], NULL, 0);
    if ((!Target_Broadcast) &&
        (Target_Device_Object_Instance > BACNET_MAX_INSTANCE)) {
        fprintf(stderr, "device-instance=%u - it must be less than %u\r\n",
            Target_Device_Object_Instance, BACNET_MAX_INSTANCE);
        return 1;
    }
    args_remaining = (argc - (6 - 2));
    for (i = 0; i < MAX_PROPERTY_VALUES; i++) {
        tag_value_arg = (6 - 2) + (i * 2);
        /* special case for context tagged values */
        if (toupper(argv[tag_value_arg][0]) == 'C') {
            context_tag = strtol(&argv[tag_value_arg][1], NULL, 0);
            tag_value_arg++;
            args_remaining--;
            Target_Object_Property_Value[i].context_tag = context_tag;
            Target_Object_Property_Value[i].context_specific = true;
        } else {
            Target_Object_Property_Value[i].context_specific = false;
        }
        property_tag = strtol(argv[tag_value_arg], NULL, 0);
        args_remaining--;
        if (args_remaining <= 0) {
            fprintf(stderr, "Error: not enough tag-value pairs\r\n");
            return 1;
        }
        value_string = argv[tag_value_arg + 1];
        args_remaining--;
        /* printf("tag[%d]=%u value[%d]=%s\r\n",
           i, property_tag, i, value_string); */
        if (property_tag >= MAX_BACNET_APPLICATION_TAG) {
            fprintf(stderr, "Error: tag=%u - it must be less than %u\r\n",
                property_tag, MAX_BACNET_APPLICATION_TAG);
            return 1;
        }
        status =
            bacapp_parse_application_data(property_tag, value_string,
            &Target_Object_Property_Value[i]);
        if (!status) {
            /* FIXME: show the expected entry format for the tag */
            fprintf(stderr, "Error: unable to parse the tag value\r\n");
            return 1;
        }
        Target_Object_Property_Value[i].next = NULL;
        if (i > 0) {
            Target_Object_Property_Value[i - 1].next =
                &Target_Object_Property_Value[i];
        }
        if (args_remaining <= 0) {
            break;
        }
    }
    if (args_remaining > 0) {
        fprintf(stderr, "Error: Exceeded %d tag-value pairs.\r\n",
            MAX_PROPERTY_VALUES);
        return 1;
    }
    /* setup my info */
    Device_Set_Object_Instance_Number(BACNET_MAX_INSTANCE);
    address_init();
    Init_Service_Handlers();
    dlenv_init();
    atexit(datalink_cleanup);
    /* configure the timeout values */
    last_seconds = time(NULL);
    if (Target_Broadcast) {
        datalink_get_broadcast_address(&Target_Address);
        Target_Address.net = Target_DNET;
        found = true;
        timeout_seconds = 0;
    } else {
        timeout_seconds = (apdu_timeout() / 1000) * apdu_retries();
        /* try to bind with the device */
        found =
            address_bind_request(Target_Device_Object_Instance, &max_apdu,
            &Target_Address);
        if (!found) {
            Send_WhoIs(Target_Device_Object_Instance,
                Target_Device_Object_Instance);
        }
    }
    /* loop forever */
    for (;;) {
        /* increment timer - exit if timed out */
        current_seconds = time(NULL);
        /* at least one second has passed */
        if (current_seconds != last_seconds) {
            /* increment timer - exit if timed out */
            delta_seconds = current_seconds - last_seconds;
            elapsed_seconds += delta_seconds;
            tsm_timer_milliseconds(((current_seconds - last_seconds) * 1000));
        }
        if (Error_Detected)
            break;
        /* wait until the device is bound, or timeout and quit */
        if (!found) {
            found =
                address_bind_request(Target_Device_Object_Instance, &max_apdu,
                &Target_Address);
        }
        if (!sent_message) {
            if (found) {
                len =
                    bacapp_encode_data(&Service_Parameters[0],
                    &Target_Object_Property_Value[0]);
                private_data.serviceParameters = &Service_Parameters[0];
                private_data.serviceParametersLen = len;
                private_data.vendorID = Target_Vendor_Identifier;
                private_data.serviceNumber = Target_Service_Number;
                Send_UnconfirmedPrivateTransfer(&Target_Address,
                    &private_data);
                printf("Sent PrivateTransfer.");
                if (timeout_seconds) {
                    printf(" Waiting %u seconds.\r\n",
                        (unsigned) (timeout_seconds - elapsed_seconds));
                } else {
                    printf("\r\n");
                }
                sent_message = true;
            } else {
                if (elapsed_seconds > timeout_seconds) {
                    printf("\rError: APDU Timeout!\r\n");
                    Error_Detected = true;
                    break;
                }
            }
        }
        /* returns 0 bytes on timeout */
        pdu_len = datalink_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout);
        /* process */
        if (pdu_len) {
            npdu_handler(&src, &Rx_Buf[0], pdu_len);
        }
        if (Error_Detected) {
            break;
        }
        /* unconfirmed - so just wait until our timeout value */
        if (elapsed_seconds > timeout_seconds) {
            break;
        }
        /* keep track of time for next check */
        last_seconds = current_seconds;
    }

    if (Error_Detected)
        return 1;
    return 0;
}
Example #7
0
int write_bacnet_obj(bacnet_device_t *device, int obj_index, char *value)
{
    uint8_t rx_buf[MAX_MPDU] = {0};
    BACNET_ADDRESS src = {
        0
    };  /* address where message came from */
    uint16_t pdu_len = 0;
    unsigned timeout = 500;     /* milliseconds */
    unsigned max_apdu = 0;
    time_t elapsed_seconds = 0;
    time_t last_seconds = 0;
    time_t current_seconds = 0;
    time_t timeout_seconds = 0;
    int status = 0, i;

    // Set Property Tag
    BACNET_APPLICATION_TAG property_tag;
    if (device -> obj_types[obj_index] == OBJECT_BINARY_VALUE)
        property_tag = BACNET_APPLICATION_TAG_ENUMERATED;
    else
        property_tag = BACNET_APPLICATION_TAG_REAL;
    BACNET_APPLICATION_DATA_VALUE target_value;

    for (i = 0; i < MAX_PROPERTY_VALUES; i++) {
        target_value.context_specific = false;

        if (property_tag >= MAX_BACNET_APPLICATION_TAG) {
            fprintf(stderr, "Error: tag=%u - it must be less than %u\r\n",
                    property_tag, MAX_BACNET_APPLICATION_TAG);
            return 1;
        }
        status =
            bacapp_parse_application_data(property_tag, value,
                                          &target_value);
        if (!status) {
            printf("Error: unable to parse the tag value\r\n");
            return 1;
        }
        target_value.next = NULL;
        break;
    }

    /* configure the timeout values */
    last_seconds = time(NULL);
    timeout_seconds = (apdu_timeout() / 1000) * apdu_retries();

    /* try to bind with the device */
    found =
        address_bind_request(device -> instance, &max_apdu, &target_address);

    if (!found) {
        Send_WhoIs(device -> instance, device -> instance);
    }
    /* loop forever */
    request_invoke_id = 0;
    while (1) {
        /* increment timer - exit if timed out */
        current_seconds = time(NULL);

        /* at least one second has passed */
        if (current_seconds != last_seconds)
            tsm_timer_milliseconds((uint16_t) ((current_seconds -
                                                last_seconds) * 1000));
        if (error_detected)
            break;
        /* wait until the device is bound, or timeout and quit */
        if (!found) {
            found =
                address_bind_request(device -> instance, &max_apdu,
                                     &target_address);
        }
        if (found) {
            if (request_invoke_id == 0) {
                printf("request invoke set\n");
                write_acked = 0;
                request_invoke_id =
                    Send_Write_Property_Request(device -> instance,
                                                device -> obj_types[obj_index], device -> obj_ids[obj_index],
                                                PROP_PRESENT_VALUE, &target_value,
                                                MESSAGE_PRIORITY_NORMAL, -1);
            } else if (tsm_invoke_id_free(request_invoke_id))
                break;
            else if (tsm_invoke_id_failed(request_invoke_id)) {
                fprintf(stderr, "\rError: TSM Timeout!\r\n");
                tsm_free_invoke_id(request_invoke_id);
                error_detected = true;
                break;
            }
        } else {
            /* increment timer - exit if timed out */
            elapsed_seconds += (current_seconds - last_seconds);
            if (elapsed_seconds > timeout_seconds) {
                error_detected = true;
                printf("\rError: APDU Timeout!\r\n");
                break;
            }
        }

        /* returns 0 bytes on timeout */
        pdu_len = datalink_receive(&src, &rx_buf[0], MAX_MPDU, timeout);

        /* process */
        if (pdu_len) {
            npdu_handler(&src, &rx_buf[0], pdu_len);
        }

        /* keep track of time for next check */
        last_seconds = current_seconds;
    }
    if (error_detected)
    {
        error_detected = 0;
        return 1;
    }
    return 0;
}
Example #8
0
int main(
    int argc,
    char *argv[])
{
    char *value_string = NULL;
    bool status = false;
    BACNET_COV_DATA cov_data;
    BACNET_PROPERTY_VALUE value_list;
    uint8_t tag;

    if (argc < 7) {
        /* note: priority 16 and 0 should produce the same end results... */
        printf("Usage: %s pid device-id object-type object-instance "
            "time property tag value [priority] [index]\r\n" "\r\n" "pid:\r\n"
            "Process Identifier for this broadcast.\r\n" "\r\n"
            "device-id:\r\n"
            "The Initiating BACnet Device Object Instance number.\r\n" "\r\n"
            "object-type:\r\n"
            "The monitored object type is the integer value of the\r\n"
            "enumeration BACNET_OBJECT_TYPE in bacenum.h.  For example,\r\n"
            "if you were monitoring Analog Output 2, the object-type\r\n"
            "would be 1.\r\n" "\r\n" "object-instance:\r\n"
            "The monitored object instance number.\r\n" "\r\n" "time:\r\n"
            "The subscription time remaining is conveyed in seconds.\r\n"
            "\r\n" "property:\r\n"
            "The property is an integer value of the enumeration \r\n"
            "BACNET_PROPERTY_ID in bacenum.h. For example, if you were\r\n"
            "monitoring the Present Value property, you would use 85\r\n"
            "as the property.\r\n" "\r\n" "tag:\r\n"
            "Tag is the integer value of the enumeration BACNET_APPLICATION_TAG \r\n"
            "in bacenum.h.  It is the data type of the value that you are\r\n"
            "monitoring.  For example, if you were monitoring a REAL value, you would \r\n"
            "use a tag of 4." "\r\n" "value:\r\n"
            "The value is an ASCII representation of some type of data that you\r\n"
            "are monitoring.  It is encoded using the tag information provided.  For\r\n"
            "example, if you were writing a REAL value of 100.0, you would use \r\n"
            "100.0 as the value.\r\n" "\r\n" "[priority]:\r\n"
            "This optional parameter is used for reporting the priority of the\r\n"
            "value. If no priority is given, none is sent, and the BACnet \r\n"
            "standard requires that the value is reported at the lowest \r\n"
            "priority (16) if the object property supports priorities.\r\n"
            "\r\n" "[index]\r\n"
            "This optional integer parameter is the index number of an array.\r\n"
            "If the property is an array, individual elements can be reported.\r\n"
            "\r\n" "Here is a brief overview of BACnet property and tags:\r\n"
            "Certain properties are expected to be written with certain \r\n"
            "application tags, so you probably need to know which ones to use\r\n"
            "with each property of each object.  It is almost safe to say that\r\n"
            "given a property and an object and a table, the tag could be looked\r\n"
            "up automatically.  There may be a few exceptions to this, such as\r\n"
            "the Any property type in the schedule object and the Present Value\r\n"
            "accepting REAL, BOOLEAN, NULL, etc.  Perhaps it would be simpler for\r\n"
            "the demo to use this kind of table - but I also wanted to be able\r\n"
            "to do negative testing by passing the wrong tag and have the server\r\n"
            "return a reject message.\r\n" "\r\n" "Example:\r\n"
            "If you want generate an unconfirmed COV,\r\n"
            "you could send the following command:\r\n"
            "%s 1 2 3 4 5 85 4 100.0\r\n"
            "where 1=pid, 2=device-id, 3=AV, 4=object-id, 5=time,\r\n"
            "85=Present-Value, 4=REAL, 100.0=value\r\n",
            filename_remove_path(argv[0]), filename_remove_path(argv[0]));
        return 0;
    }
    /* decode the command line parameters */
    cov_data.subscriberProcessIdentifier = strtol(argv[1], NULL, 0);
    cov_data.initiatingDeviceIdentifier = strtol(argv[2], NULL, 0);
    cov_data.monitoredObjectIdentifier.type = strtol(argv[3], NULL, 0);
    cov_data.monitoredObjectIdentifier.instance = strtol(argv[4], NULL, 0);
    cov_data.timeRemaining = strtol(argv[5], NULL, 0);
    cov_data.listOfValues = &value_list;
    value_list.next = NULL;
    value_list.propertyIdentifier = strtol(argv[6], NULL, 0);
    tag = strtol(argv[7], NULL, 0);
    value_string = argv[8];
    /* optional priority */
    if (argc > 9)
        value_list.priority = strtol(argv[9], NULL, 0);
    else
        value_list.priority = BACNET_NO_PRIORITY;
    /* optional index */
    if (argc > 10)
        value_list.propertyArrayIndex = strtol(argv[10], NULL, 0);
    else
        value_list.propertyArrayIndex = BACNET_ARRAY_ALL;

    if (cov_data.initiatingDeviceIdentifier >= BACNET_MAX_INSTANCE) {
        fprintf(stderr, "device-instance=%u - it must be less than %u\r\n",
            cov_data.initiatingDeviceIdentifier, BACNET_MAX_INSTANCE);
        return 1;
    }
    if (cov_data.monitoredObjectIdentifier.type >= MAX_BACNET_OBJECT_TYPE) {
        fprintf(stderr, "object-type=%u - it must be less than %u\r\n",
            cov_data.monitoredObjectIdentifier.type, MAX_BACNET_OBJECT_TYPE);
        return 1;
    }
    if (cov_data.monitoredObjectIdentifier.instance > BACNET_MAX_INSTANCE) {
        fprintf(stderr, "object-instance=%u - it must be less than %u\r\n",
            cov_data.monitoredObjectIdentifier.instance,
            BACNET_MAX_INSTANCE + 1);
        return 1;
    }
    if (cov_data.listOfValues->propertyIdentifier > MAX_BACNET_PROPERTY_ID) {
        fprintf(stderr, "object-type=%u - it must be less than %u\r\n",
            cov_data.listOfValues->propertyIdentifier,
            MAX_BACNET_PROPERTY_ID + 1);
        return 1;
    }
    if (tag >= MAX_BACNET_APPLICATION_TAG) {
        fprintf(stderr, "tag=%u - it must be less than %u\r\n", tag,
            MAX_BACNET_APPLICATION_TAG);
        return 1;
    }
    status =
        bacapp_parse_application_data(tag, value_string,
        &cov_data.listOfValues->value);
    if (!status) {
        /* FIXME: show the expected entry format for the tag */
        fprintf(stderr, "unable to parse the tag value\r\n");
        return 1;
    }
    /* setup my info */
    Device_Set_Object_Instance_Number(BACNET_MAX_INSTANCE);
    Init_Service_Handlers();
    dlenv_init();
    atexit(datalink_cleanup);
    Send_UCOV_Notify(&Handler_Transmit_Buffer[0], &cov_data);

    return 0;
}
Example #9
0
int main(
    int argc,
    char *argv[])
{
    BACNET_ADDRESS src = {
        0
    };  /* address where message came from */
    uint16_t pdu_len = 0;
    unsigned timeout = 100;     /* milliseconds */
    unsigned max_apdu = 0;
    time_t elapsed_seconds = 0;
    time_t last_seconds = 0;
    time_t current_seconds = 0;
    time_t timeout_seconds = 0;
    bool found = false;
    char *value_string = NULL;
    bool status = false;
    int args_remaining = 0, tag_value_arg = 0, i = 0;
    BACNET_APPLICATION_TAG property_tag;
    uint8_t context_tag = 0;
    int argi = 0;

    /* print help if requested */
    for (argi = 1; argi < argc; argi++) {
        if (strcmp(argv[argi], "--help") == 0) {
            print_usage(filename_remove_path(argv[0]));
            print_help(filename_remove_path(argv[0]));
            return 0;
        }
        if (strcmp(argv[argi], "--version") == 0) {
            printf("%s %s\n",
                filename_remove_path(argv[0]),
                BACNET_VERSION_TEXT);
            printf("Copyright (C) 2014 by Steve Karg\n"
                "This is free software; see the source for copying conditions.\n"
                "There is NO warranty; not even for MERCHANTABILITY or\n"
                "FITNESS FOR A PARTICULAR PURPOSE.\n");
            return 0;
        }
    }
    if (argc < 9) {
        print_usage(filename_remove_path(argv[0]));
        return 0;
    }
    /* decode the command line parameters */
    Target_Device_Object_Instance = strtol(argv[1], NULL, 0);
    Target_Object_Type = strtol(argv[2], NULL, 0);
    Target_Object_Instance = strtol(argv[3], NULL, 0);
    Target_Object_Property = strtol(argv[4], NULL, 0);
    Target_Object_Property_Priority = (uint8_t) strtol(argv[5], NULL, 0);
    Target_Object_Property_Index = strtol(argv[6], NULL, 0);
    if (Target_Object_Property_Index == -1)
        Target_Object_Property_Index = BACNET_ARRAY_ALL;
    if (Target_Device_Object_Instance > BACNET_MAX_INSTANCE) {
        fprintf(stderr, "device-instance=%u - it must be less than %u\n",
            Target_Device_Object_Instance, BACNET_MAX_INSTANCE + 1);
        return 1;
    }
    if (Target_Object_Type > MAX_BACNET_OBJECT_TYPE) {
        fprintf(stderr, "object-type=%u - it must be less than %u\n",
            Target_Object_Type, MAX_BACNET_OBJECT_TYPE + 1);
        return 1;
    }
    if (Target_Object_Instance > BACNET_MAX_INSTANCE) {
        fprintf(stderr, "object-instance=%u - it must be less than %u\n",
            Target_Object_Instance, BACNET_MAX_INSTANCE + 1);
        return 1;
    }
    if (Target_Object_Property > MAX_BACNET_PROPERTY_ID) {
        fprintf(stderr, "property=%u - it must be less than %u\n",
            Target_Object_Property, MAX_BACNET_PROPERTY_ID + 1);
        return 1;
    }
    args_remaining = (argc - 7);
    for (i = 0; i < MAX_PROPERTY_VALUES; i++) {
        tag_value_arg = 7 + (i * 2);
        /* special case for context tagged values */
        if (toupper(argv[tag_value_arg][0]) == 'C') {
            context_tag = (uint8_t) strtol(&argv[tag_value_arg][1], NULL, 0);
            tag_value_arg++;
            args_remaining--;
            Target_Object_Property_Value[i].context_tag = context_tag;
            Target_Object_Property_Value[i].context_specific = true;
        } else {
            Target_Object_Property_Value[i].context_specific = false;
        }
        property_tag = strtol(argv[tag_value_arg], NULL, 0);
        args_remaining--;
        if (args_remaining <= 0) {
            fprintf(stderr, "Error: not enough tag-value pairs\n");
            return 1;
        }
        value_string = argv[tag_value_arg + 1];
        args_remaining--;
        /* printf("tag[%d]=%u value[%d]=%s\n",
           i, property_tag, i, value_string); */
        if (property_tag >= MAX_BACNET_APPLICATION_TAG) {
            fprintf(stderr, "Error: tag=%u - it must be less than %u\n",
                property_tag, MAX_BACNET_APPLICATION_TAG);
            return 1;
        }
        status =
            bacapp_parse_application_data(property_tag, value_string,
            &Target_Object_Property_Value[i]);
        if (!status) {
            /* FIXME: show the expected entry format for the tag */
            fprintf(stderr, "Error: unable to parse the tag value\n");
            return 1;
        }
        Target_Object_Property_Value[i].next = NULL;
        if (i > 0) {
            Target_Object_Property_Value[i - 1].next =
                &Target_Object_Property_Value[i];
        }
        if (args_remaining <= 0) {
            break;
        }
    }
    if (args_remaining > 0) {
        fprintf(stderr, "Error: Exceeded %d tag-value pairs.\n",
            MAX_PROPERTY_VALUES);
        return 1;
    }
    /* setup my info */
    Device_Set_Object_Instance_Number(BACNET_MAX_INSTANCE);
    address_init();
    Init_Service_Handlers();
    dlenv_init();
    atexit(datalink_cleanup);
    /* configure the timeout values */
    last_seconds = time(NULL);
    timeout_seconds = (apdu_timeout() / 1000) * apdu_retries();
    /* try to bind with the device */
    found =
        address_bind_request(Target_Device_Object_Instance, &max_apdu,
        &Target_Address);
    if (!found) {
        Send_WhoIs(Target_Device_Object_Instance,
            Target_Device_Object_Instance);
    }
    /* loop forever */
    for (;;) {
        /* increment timer - exit if timed out */
        current_seconds = time(NULL);

        /* at least one second has passed */
        if (current_seconds != last_seconds)
            tsm_timer_milliseconds((uint16_t) ((current_seconds -
                        last_seconds) * 1000));
        if (Error_Detected)
            break;
        /* wait until the device is bound, or timeout and quit */
        if (!found) {
            found =
                address_bind_request(Target_Device_Object_Instance, &max_apdu,
                &Target_Address);
        }
        if (found) {
            if (Request_Invoke_ID == 0) {
                Request_Invoke_ID =
                    Send_Write_Property_Request(Target_Device_Object_Instance,
                    Target_Object_Type, Target_Object_Instance,
                    Target_Object_Property, &Target_Object_Property_Value[0],
                    Target_Object_Property_Priority,
                    Target_Object_Property_Index);
            } else if (tsm_invoke_id_free(Request_Invoke_ID))
                break;
            else if (tsm_invoke_id_failed(Request_Invoke_ID)) {
                fprintf(stderr, "\rError: TSM Timeout!\n");
                tsm_free_invoke_id(Request_Invoke_ID);
                Error_Detected = true;
                /* try again or abort? */
                break;
            }
        } else {
            /* increment timer - exit if timed out */
            elapsed_seconds += (current_seconds - last_seconds);
            if (elapsed_seconds > timeout_seconds) {
                Error_Detected = true;
                printf("\rError: APDU Timeout!\n");
                break;
            }
        }

        /* returns 0 bytes on timeout */
        pdu_len = datalink_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout);

        /* process */
        if (pdu_len) {
            npdu_handler(&src, &Rx_Buf[0], pdu_len);
        }

        /* keep track of time for next check */
        last_seconds = current_seconds;
    }
    if (Error_Detected)
        return 1;
    return 0;
}