#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void confirm_close(void);
static long fileSize(FILE *f);
static void terminate(const char *format, ...);
int main(void)
{
char* ipaddress = "192.168.1.1";
unsigned short portno = 8006;
uint32_t serverAPIVersion;
size_t blobSize = 0, size = 0;
char *fileName = "config.gen";
FILE *fd = NULL;
printf("Connecting to %s:%u\n", ipaddress, portno);
returnCode =
GHSConnect(ipaddress, portno, &connection, &serverAPIVersion);
{
terminate("Failed: client has API version %u, while mainframe has API version %u.\n",
}
{
terminate("Failed: return code is %d.\n", returnCode);
}
printf("Connected\n");
printf("\nCopying Boot settings to Active settings\n");
{
terminate("Failed on GHSApplyPersistedSettings: return code is %d.\n", returnCode);
}
printf("\nCopying Active settings to Boot settings\n");
{
terminate("Failed on GHSPersistCurrentSettings: return code is %d.\n", returnCode);
}
printf("\nSaving GEN Mainframe settings to disk\n");
{
terminate("Failed on GHSGetCurrentSettings: return code is %d.\n", returnCode);
}
fd = fopen(fileName, "w");
if (!fd)
{
terminate("Failed on opening file '%s': %s.\n", fileName, strerror(errno));
}
size = fwrite(blob, 1, blobSize, fd);
if (size < blobSize)
{
terminate("Failed on writing to file '%s': only %d bytes of %d written.\n", fileName, size, blobSize);
}
fclose(fd);
printf("\nLoading GEN Mainframe settings from disk\n");
fd = fopen(fileName, "r");
if (!fd)
{
terminate("Failed on opening file '%s': %s.\n", fileName, strerror(errno));
}
blobSize = fileSize(fd);
confBlob = malloc(blobSize);
if (!confBlob)
{
terminate("Failed on allocating memory for configuration\n");
}
size = fread(confBlob, 1, blobSize, fd);
if (size < blobSize)
{
terminate("Failed on reading from file '%s': only %d bytes of %d read.\n", fileName, size, blobSize);
}
fclose(fd);
{
terminate("Failed on GHSSetCurrentSettings: return code is %d.\n", returnCode);
}
free(confBlob);
confBlob = NULL;
{
terminate("Failed on GHSFreeSettings: return code is %d.\n", returnCode);
}
{
terminate("Failed on GHSDisconnect: return code is %d.\n", returnCode);
}
printf("\nDisconnected\n");
confirm_close();
return EXIT_SUCCESS;
}
void confirm_close(void)
{
printf("Press ENTER to close this example program.\n");
(void)getchar();
}
long fileSize(FILE *f)
{
long seek = 0, prevSeek = ftell(f);
fseek(f, 0, SEEK_END);
seek = ftell(f);
fseek(f, prevSeek, SEEK_SET);
return seek;
}
void terminate(const char *format, ...)
{
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
confirm_close();
exit(EXIT_FAILURE);
}