Ejemplo n.º 1
0
bool bacfile_read_ack_stream_data(
    uint32_t instance,
    BACNET_ATOMIC_READ_FILE_DATA * data)
{
    bool found = false;
    FILE *pFile = NULL;
    char *pFilename = NULL;

    pFilename = bacfile_name(instance);
    if (pFilename) {
        found = true;
        pFile = fopen(pFilename, "rb");
        if (pFile) {
            (void) fseek(pFile, data->type.stream.fileStartPosition, SEEK_SET);
            if (fwrite(octetstring_value(&data->fileData),
                    octetstring_length(&data->fileData), 1, pFile) != 1) {
#if PRINT_ENABLED
                fprintf(stderr, "Failed to write to %s (%lu)!\n", pFilename,
                    (unsigned long) instance);
#endif
            }
            fclose(pFile);
        }
    }

    return found;
}
Ejemplo n.º 2
0
static void AtomicReadFileAckHandler(
    uint8_t * service_request,
    uint16_t service_len,
    BACNET_ADDRESS * src,
    BACNET_CONFIRMED_SERVICE_ACK_DATA * service_data)
{
    int len = 0;
    BACNET_ATOMIC_READ_FILE_DATA data;
    FILE *pFile = NULL; /* stream pointer */
    size_t octets_written = 0;

    (void) src; /* FIXME: validate the source address matches */
    len = arf_ack_decode_service_request(service_request, service_len, &data);
    if (len > 0) {
        /* validate the parameters before storing data */
        if ((data.access == FILE_STREAM_ACCESS) &&
            (service_data->invoke_id == Current_Invoke_ID)) {
            if (data.type.stream.fileStartPosition == 0)
                pFile = fopen(Local_File_Name, "wb");
            else
                pFile = fopen(Local_File_Name, "rb+");
            if (pFile) {
                /* is there anything to do with this? data.stream.requestedOctetCount */
                (void) fseek(pFile, data.type.stream.fileStartPosition,
                    SEEK_SET);
                octets_written = fwrite(octetstring_value(&data.fileData), 1,   /* unit to write in bytes - in our case, an octet is one byte */
                    octetstring_length(&data.fileData), pFile);
                if (octets_written != octetstring_length(&data.fileData)) {
                    fprintf(stderr, "Unable to write data to file \"%s\".\n",
                        Local_File_Name);
                } else if (octets_written == 0) {
                    fprintf(stderr, "Received 0 byte octet string!.\n");
                } else {
                    printf("\r%d bytes",
                        (data.type.stream.fileStartPosition + octets_written));
                }
                fflush(pFile);
                fclose(pFile);
            }
            if (data.endOfFile) {
                End_Of_File_Detected = true;
                printf("\r\n");
            }
        }
    }
}
Ejemplo n.º 3
0
void testAtomicWriteFileAccess(
    Test * pTest,
    BACNET_ATOMIC_WRITE_FILE_DATA * data)
{
    BACNET_ATOMIC_WRITE_FILE_DATA test_data = { 0 };
    uint8_t apdu[480] = { 0 };
    int len = 0;
    int apdu_len = 0;
    uint8_t invoke_id = 128;
    uint8_t test_invoke_id = 0;

    len = awf_encode_apdu(&apdu[0], invoke_id, data);
    ct_test(pTest, len != 0);
    apdu_len = len;

    len = awf_decode_apdu(&apdu[0], apdu_len, &test_invoke_id, &test_data);
    ct_test(pTest, len != -1);
    ct_test(pTest, test_data.object_type == data->object_type);
    ct_test(pTest, test_data.object_instance == data->object_instance);
    ct_test(pTest, test_data.access == data->access);
    if (test_data.access == FILE_STREAM_ACCESS) {
        ct_test(pTest,
                test_data.type.stream.fileStartPosition ==
                data->type.stream.fileStartPosition);
    } else if (test_data.access == FILE_RECORD_ACCESS) {
        ct_test(pTest,
                test_data.type.record.fileStartRecord ==
                data->type.record.fileStartRecord);
        ct_test(pTest,
                test_data.type.record.returnedRecordCount ==
                data->type.record.returnedRecordCount);
    }
    ct_test(pTest,
            octetstring_length(&test_data.fileData) ==
            octetstring_length(&data->fileData));
    ct_test(pTest, memcmp(octetstring_value(&test_data.fileData),
                          octetstring_value(&data->fileData),
                          octetstring_length(&test_data.fileData)) == 0);
}
Ejemplo n.º 4
0
void testAtomicReadFileAckAccess(
    Test * pTest,
    BACNET_ATOMIC_READ_FILE_DATA * data)
{
    BACNET_ATOMIC_READ_FILE_DATA test_data = { 0 };
    uint8_t apdu[480] = { 0 };
    int len = 0;
    int apdu_len = 0;
    uint8_t invoke_id = 128;
    uint8_t test_invoke_id = 0;
    unsigned int i = 0;

    len = arf_ack_encode_apdu(&apdu[0], invoke_id, data);
    ct_test(pTest, len != 0);
    apdu_len = len;

    len = arf_ack_decode_apdu(&apdu[0], apdu_len, &test_invoke_id, &test_data);
    ct_test(pTest, len != -1);
    ct_test(pTest, test_data.endOfFile == data->endOfFile);
    ct_test(pTest, test_data.access == data->access);
    if (test_data.access == FILE_STREAM_ACCESS) {
        ct_test(pTest,
            test_data.type.stream.fileStartPosition ==
            data->type.stream.fileStartPosition);
        ct_test(pTest,
            octetstring_length(&test_data.fileData[0]) ==
            octetstring_length(&data->fileData[0]));
        ct_test(pTest, memcmp(octetstring_value(&test_data.fileData[0]),
                octetstring_value(&data->fileData[0]),
                octetstring_length(&test_data.fileData[0])) == 0);
    } else if (test_data.access == FILE_RECORD_ACCESS) {
        ct_test(pTest,
            test_data.type.record.fileStartRecord ==
            data->type.record.fileStartRecord);
        ct_test(pTest,
            test_data.type.record.RecordCount ==
            data->type.record.RecordCount);
        for (i = 0; i < data->type.record.RecordCount; i++) {
            ct_test(pTest,
                octetstring_length(&test_data.fileData[i]) ==
                octetstring_length(&data->fileData[i]));
            ct_test(pTest, memcmp(octetstring_value(&test_data.fileData[i]),
                    octetstring_value(&data->fileData[i]),
                    octetstring_length(&test_data.fileData[i])) == 0);
        }
    }
}
Ejemplo n.º 5
0
static void AtomicReadFileAckHandler(
    uint8_t * service_request,
    uint16_t service_len,
    BACNET_ADDRESS * src,
    BACNET_CONFIRMED_SERVICE_ACK_DATA * service_data)
{
    int len = 0;
    BACNET_ATOMIC_READ_FILE_DATA data;

    if (address_match(&Target_Address, src) &&
        (service_data->invoke_id == Request_Invoke_ID)) {
        len =
            arf_ack_decode_service_request(service_request, service_len,
            &data);
        if (len > 0) {
            /* validate the parameters before storing data */
            if ((data.access == FILE_STREAM_ACCESS) &&
                (service_data->invoke_id == Request_Invoke_ID)) {
                char msg[32];
                uint8_t *pFileData;
                int i;

                sprintf(msg, "EOF=%d,start=%d,", data.endOfFile,
                    data.type.stream.fileStartPosition);
                __LogAnswer(msg, 0);

                pFileData = octetstring_value(&data.fileData);
                for (i = 0; i < octetstring_length(&data.fileData); i++) {
                    sprintf(msg, "%02x ", *pFileData);
                    __LogAnswer(msg, 1);
                    pFileData++;
                }
            } else {
                LogError("Bad stream access reported");
            }
        }
    }
}
Ejemplo n.º 6
0
bool bacfile_write_stream_data(
    BACNET_ATOMIC_WRITE_FILE_DATA * data)
{
    char *pFilename = NULL;
    bool found = false;
    FILE *pFile = NULL;

    pFilename = bacfile_name(data->object_instance);
    if (pFilename) {
        found = true;
        if (data->type.stream.fileStartPosition == 0) {
            /* open the file as a clean slate when starting at 0 */
            pFile = fopen(pFilename, "wb");
        } else if (data->type.stream.fileStartPosition == -1) {
            /* If 'File Start Position' parameter has the special
               value -1, then the write operation shall be treated
               as an append to the current end of file. */
            pFile = fopen(pFilename, "ab+");
        } else {
            /* open for update */
            pFile = fopen(pFilename, "rb+");
        }
        if (pFile) {
            if (data->type.stream.fileStartPosition != -1) {
                (void) fseek(pFile, data->type.stream.fileStartPosition,
                    SEEK_SET);
            }
            if (fwrite(octetstring_value(&data->fileData),
                    octetstring_length(&data->fileData), 1, pFile) != 1) {
                /* do something if it fails? */
            }
            fclose(pFile);
        }
    }

    return found;
}