Ejemplo n.º 1
0
static void Init_Service_Handlers(
    void)
{
    Device_Init(NULL);
    /* we need to handle who-is
       to support dynamic device binding to us */
    apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is);
    /* handle i-am to support binding to other devices */
    apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, handler_i_am_bind);
    /* set the handler for all the services we don't implement
       It is required to send the proper reject message... */
    apdu_set_unrecognized_service_handler_handler
        (handler_unrecognized_service);
    /* we must implement read property - it's required! */
    apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY,
        handler_read_property);

    apdu_set_confirmed_handler(SERVICE_CONFIRMED_PRIVATE_TRANSFER,
        handler_conf_private_trans);
    /* handle the data coming back from confirmed requests */
    apdu_set_confirmed_ack_handler(SERVICE_CONFIRMED_READ_PROPERTY,
        handler_read_property_ack);

    apdu_set_confirmed_ack_handler(SERVICE_CONFIRMED_PRIVATE_TRANSFER,
        handler_conf_private_trans_ack);

    /* handle any errors coming back */
    apdu_set_error_handler(SERVICE_CONFIRMED_READ_PROPERTY, MyErrorHandler);
    apdu_set_error_handler(SERVICE_CONFIRMED_PRIVATE_TRANSFER, MyErrorHandler);
    apdu_set_abort_handler(MyAbortHandler);
    apdu_set_reject_handler(MyRejectHandler);
}
Ejemplo n.º 2
0
int BacnetAtomicReadFile(
    int deviceInstanceNumber,
    int fileInstanceNumber,
    int startOffset,
    int numBytes)
{
    if (!isAtomicReadFileHandlerRegistered) {
        /* handle the data coming back from confirmed requests */
        apdu_set_confirmed_ack_handler(SERVICE_CONFIRMED_ATOMIC_READ_FILE,
            AtomicReadFileAckHandler);

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

        /* indicate that handlers are now registered */
        isAtomicReadFileHandlerRegistered = true;
    }
    /* Send the message out */
    Request_Invoke_ID =
        Send_Atomic_Read_File_Stream(deviceInstanceNumber, fileInstanceNumber,
        startOffset, numBytes);
    Wait_For_Answer_Or_Timeout(100, waitAnswer);

    int isFailure = Error_Detected;
    Error_Detected = 0;
    return isFailure;
}
Ejemplo n.º 3
0
int BacnetReadProperty(
    int deviceInstanceNumber,
    int objectType,
    int objectInstanceNumber,
    int objectProperty,
    int objectIndex)
{
    if (!isReadPropertyHandlerRegistered) {
        /* handle the data coming back from confirmed requests */
        apdu_set_confirmed_ack_handler(SERVICE_CONFIRMED_READ_PROPERTY,
            My_Read_Property_Ack_Handler);

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

        /* indicate that handlers are now registered */
        isReadPropertyHandlerRegistered = true;
    }
    /* Send the message out */
    Request_Invoke_ID =
        Send_Read_Property_Request(deviceInstanceNumber, objectType,
        objectInstanceNumber, objectProperty, objectIndex);
    Wait_For_Answer_Or_Timeout(100, waitAnswer);

    int isFailure = Error_Detected;
    Error_Detected = 0;
    return isFailure;
}
Ejemplo n.º 4
0
static void Init_Service_Handlers(
    void)
{
    Device_Init(NULL);
    /* we need to handle who-is
       to support dynamic device binding to us */
    apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is);
    /* handle i-am to support binding to other devices */
    apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, handler_i_am_bind);
    /* set the handler for all the services we don't implement
       It is required to send the proper reject message... */
    apdu_set_unrecognized_service_handler_handler
        (handler_unrecognized_service);
    /* we must implement read property - it's required! */
    apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY,
        handler_read_property);
    /* handle communication so we can shutup when asked */
    apdu_set_confirmed_handler(SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL,
        handler_device_communication_control);
    /* handle the ack coming back */
    apdu_set_confirmed_simple_ack_handler
        (SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL,
        MyDeviceCommunicationControlSimpleAckHandler);
    /* handle any errors coming back */
    apdu_set_error_handler(SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL,
        MyErrorHandler);
    apdu_set_abort_handler(MyAbortHandler);
    apdu_set_reject_handler(MyRejectHandler);
}
Ejemplo n.º 5
0
static void Init_Service_Handlers(
    void)
{
    Device_Init(NULL);
    /* we need to handle who-is
       to support dynamic device binding to us */
    apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is);
    /* handle i-am to support binding to other devices */
    apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, LocalIAmHandler);
    /* set the handler for all the services we don't implement
       It is required to send the proper reject message... */
    apdu_set_unrecognized_service_handler_handler
        (handler_unrecognized_service);
    /* we must implement read property - it's required! */
    apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY,
        handler_read_property);
    /* handle the data coming back from confirmed requests */
    apdu_set_confirmed_ack_handler(SERVICE_CONFIRMED_ATOMIC_READ_FILE,
        AtomicReadFileAckHandler);
    /* handle any errors coming back */
    apdu_set_error_handler(SERVICE_CONFIRMED_ATOMIC_READ_FILE,
        Atomic_Read_File_Error_Handler);
    apdu_set_abort_handler(MyAbortHandler);
    apdu_set_reject_handler(MyRejectHandler);
}
Ejemplo n.º 6
0
int BacnetAtomicWriteFile(
    int deviceInstanceNumber,
    int fileInstanceNumber,
    int blockStartAddr,
    int blockNumBytes,
    char *nibbleBuffer)
{
    BACNET_OCTET_STRING fileData;
    int i, nibble;
    uint8_t byteValue;
    unsigned char nibbleValue;

    if (!isAtomicWriteFileHandlerRegistered) {
        /* handle any errors coming back */
        apdu_set_error_handler(SERVICE_CONFIRMED_ATOMIC_WRITE_FILE,
            My_Error_Handler);

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

    for (i = 0; i < blockNumBytes; i++) {
        byteValue = 0;
        for (nibble = 0; nibble < 2; nibble++) {
            nibbleValue = toupper(nibbleBuffer[i * 2 + nibble]);
            if ((nibbleValue >= '0') && (nibbleValue <= '9')) {
                byteValue += (nibbleValue - '0') << (4 * (1 - nibble));
            } else if ((nibbleValue >= 'A') && (nibbleValue <= 'F')) {
                byteValue += (nibbleValue - 'A' + 10) << (4 * (1 - nibble));
            } else {
                LogError("Bad data in buffer.");
            }
        }
        fileData.value[i] = byteValue;
    }
    octetstring_truncate(&fileData, blockNumBytes);

    /* Send out the message and wait for answer */
    if (!Error_Detected) {
        Request_Invoke_ID =
            Send_Atomic_Write_File_Stream(deviceInstanceNumber,
            fileInstanceNumber, blockStartAddr, &fileData);
        Wait_For_Answer_Or_Timeout(100, waitAnswer);
    }

    int isFailure = Error_Detected;
    Error_Detected = 0;
    return isFailure;
}
Ejemplo n.º 7
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;
}
Ejemplo n.º 8
0
int BacnetReadPropertyMultiple(
    int deviceInstanceNumber,
    ...)
{
    /* Get the variable argument list from the stack */
    Inline_Stack_Vars;
    int rpmIndex = 1;
    BACNET_READ_ACCESS_DATA *rpm_object =
        calloc(1, sizeof(BACNET_READ_ACCESS_DATA));
    BACNET_READ_ACCESS_DATA *Read_Access_Data = rpm_object;
    BACNET_PROPERTY_REFERENCE *rpm_property;
    uint8_t buffer[MAX_PDU] = { 0 };

    while (rpmIndex < Inline_Stack_Items) {
        SV *pSV = Inline_Stack_Item(rpmIndex++);

        /* Make sure the argument is an Array Reference */
        if (SvTYPE(SvRV(pSV)) != SVt_PVAV) {
            LogError("Argument is not an Array reference");
            break;
        }
        /* Make sure we can access the memory */
        if (rpm_object) {
            rpm_object->listOfProperties = NULL;
        } else {
            LogError("Memory Allocation Issue");
            break;
        }

        AV *pAV = (AV *) SvRV(pSV);
        SV **ppSV;

        /* The 0th argument is the object type */
        ppSV = av_fetch(pAV, 0, 0);
        if (ppSV) {
            rpm_object->object_type = SvIV(*ppSV);
        } else {
            LogError("Problem parsing the Array of arguments");
            break;
        }

        /* The 1st argument is the object instance */
        ppSV = av_fetch(pAV, 1, 0);
        if (ppSV) {
            rpm_object->object_instance = SvIV(*ppSV);
        } else {
            LogError("Problem parsing the Array of arguments");
            break;
        }

        /* The 2nd argument is the property type */
        ppSV = av_fetch(pAV, 2, 0);
        if (ppSV) {
            rpm_property = calloc(1, sizeof(BACNET_PROPERTY_REFERENCE));
            rpm_object->listOfProperties = rpm_property;
            if (rpm_property) {
                rpm_property->propertyIdentifier = SvIV(*ppSV);
            } else {
                LogError("Memory allocation error");
                break;
            }
        } else {
            LogError("Problem parsing the Array of arguments");
            break;
        }

        /* The 3rd argument is the property index */
        ppSV = av_fetch(pAV, 3, 0);
        if (ppSV) {
            rpm_property->propertyArrayIndex = SvIV(*ppSV);
        } else {
            LogError("Problem parsing the Array of arguments");
            break;
        }

        /* Advance to the next RPM index */
        if (rpmIndex < Inline_Stack_Items) {
            rpm_object->next = calloc(1, sizeof(BACNET_READ_ACCESS_DATA));
            rpm_object = rpm_object->next;
        } else {
            rpm_object->next = NULL;
        }
    }

    if (!isReadPropertyMultipleHandlerRegistered) {
        /* handle the data coming back from confirmed requests */
        apdu_set_confirmed_ack_handler(SERVICE_CONFIRMED_READ_PROP_MULTIPLE,
            My_Read_Property_Multiple_Ack_Handler);

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

        /* indicate that handlers are now registered */
        isReadPropertyMultipleHandlerRegistered = true;
    }
    /* Send the message out */
    if (!Error_Detected) {
        Request_Invoke_ID =
            Send_Read_Property_Multiple_Request(&buffer[0], sizeof(buffer),
            deviceInstanceNumber, Read_Access_Data);
        Wait_For_Answer_Or_Timeout(100, waitAnswer);
    }
    /* Clean up allocated memory */
    BACNET_READ_ACCESS_DATA *old_rpm_object;
    BACNET_PROPERTY_REFERENCE *old_rpm_property;

    rpm_object = Read_Access_Data;
    old_rpm_object = rpm_object;
    while (rpm_object) {
        rpm_property = rpm_object->listOfProperties;
        while (rpm_property) {
            old_rpm_property = rpm_property;
            rpm_property = rpm_property->next;
            free(old_rpm_property);
        }
        old_rpm_object = rpm_object;
        rpm_object = rpm_object->next;
        free(old_rpm_object);
    }

    /* Process the return value */
    int isFailure = Error_Detected;
    Error_Detected = 0;
    return isFailure;
}