opc source code
This commit is contained in:
68
third_party/open62541/examples/common.h
vendored
Normal file
68
third_party/open62541/examples/common.h
vendored
Normal file
@@ -0,0 +1,68 @@
|
||||
/* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
|
||||
* See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
|
||||
|
||||
#include <open62541/types.h>
|
||||
#include <open62541/types_generated_handling.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
/* sleep_ms */
|
||||
#ifdef _WIN32
|
||||
# include <synchapi.h>
|
||||
# define sleep_ms(ms) Sleep(ms)
|
||||
#else
|
||||
# include <unistd.h>
|
||||
# define sleep_ms(ms) usleep(ms * 1000)
|
||||
#endif
|
||||
|
||||
/* loadFile parses the certificate file.
|
||||
*
|
||||
* @param path specifies the file name given in argv[]
|
||||
* @return Returns the file content after parsing */
|
||||
static UA_INLINE UA_ByteString
|
||||
loadFile(const char *const path) {
|
||||
UA_ByteString fileContents = UA_STRING_NULL;
|
||||
|
||||
/* Open the file */
|
||||
FILE *fp = fopen(path, "rb");
|
||||
if(!fp) {
|
||||
errno = 0; /* We read errno also from the tcp layer... */
|
||||
return fileContents;
|
||||
}
|
||||
|
||||
/* Get the file length, allocate the data and read */
|
||||
fseek(fp, 0, SEEK_END);
|
||||
fileContents.length = (size_t)ftell(fp);
|
||||
fileContents.data = (UA_Byte *)UA_malloc(fileContents.length * sizeof(UA_Byte));
|
||||
if(fileContents.data) {
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
size_t read = fread(fileContents.data, sizeof(UA_Byte), fileContents.length, fp);
|
||||
if(read != fileContents.length)
|
||||
UA_ByteString_clear(&fileContents);
|
||||
} else {
|
||||
fileContents.length = 0;
|
||||
}
|
||||
fclose(fp);
|
||||
|
||||
return fileContents;
|
||||
}
|
||||
|
||||
static UA_INLINE UA_StatusCode
|
||||
writeFile(const char* const path, const UA_ByteString buffer) {
|
||||
FILE *fp = NULL;
|
||||
|
||||
fp = fopen(path, "wb");
|
||||
if(fp == NULL)
|
||||
return UA_STATUSCODE_BADINTERNALERROR;
|
||||
|
||||
for(UA_UInt32 bufIndex = 0; bufIndex < buffer.length; bufIndex++) {
|
||||
int retVal = fputc(buffer.data[bufIndex], fp);
|
||||
if(retVal == EOF) {
|
||||
fclose(fp);
|
||||
return UA_STATUSCODE_BADINTERNALERROR;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
return UA_STATUSCODE_GOOD;
|
||||
}
|
||||
Reference in New Issue
Block a user