GEN DAQ API  4.0
Controlling GEN Series tethered Mainframes
examples/mainframe-settings.c
/*
* Copyright (C) 2009 HBM Netherlands B.V.
* Schutweg 15a
* 5145 NP Waalwijk
* The Netherlands
* http://www.hbm.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "GEN_DAQ_API.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;
GHSConnectionHandle connection = 0;
GHSReturnValue returnCode;
uint32_t serverAPIVersion;
GHSSettingsPtr blob = NULL, confBlob = NULL;
size_t blobSize = 0, size = 0;
char *fileName = "config.gen";
FILE *fd = NULL;
printf("Connecting to %s:%u\n", ipaddress, portno);
/* Connect to the mainframe "localhost" is also possible. */
returnCode = GHSConnect(ipaddress, portno, &connection, &serverAPIVersion);
if (returnCode == GHSReturnValue_APIMismatch)
{
terminate("Failed: client has API version %u, while mainframe has API version %u.\n",
GHSGetClientAPIVersion(), serverAPIVersion);
}
else if (returnCode != GHSReturnValue_OK)
{
terminate("Failed: return code is %d.\n", returnCode);
}
printf("Connected\n");
/*Copy Boot settings to Active settings*/
printf("\nCopying Boot settings to Active settings\n");
returnCode = GHSApplyPersistedSettings(connection);
if (returnCode != GHSReturnValue_OK)
{
terminate("Failed on GHSApplyPersistedSettings: return code is %d.\n", returnCode);
}
/*Copy Active settings to Boot settings*/
printf("\nCopying Active settings to Boot settings\n");
returnCode = GHSPersistCurrentSettings(connection);
if (returnCode != GHSReturnValue_OK)
{
terminate("Failed on GHSPersistCurrentSettings: return code is %d.\n", returnCode);
}
/*Save GEN Mainframe settings to disk*/
printf("\nSaving GEN Mainframe settings to disk\n");
returnCode = GHSGetCurrentSettings(connection, &blob, &blobSize);
if (returnCode != GHSReturnValue_OK)
{
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);
/*Load GEN Mainframe settings from disk*/
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);
returnCode = GHSSetCurrentSettings(connection, blob, blobSize);
if (returnCode != GHSReturnValue_OK)
{
terminate("Failed on GHSSetCurrentSettings: return code is %d.\n", returnCode);
}
free(confBlob);
confBlob = NULL;
/* Free allocated resources. */
returnCode = GHSFreeSettings(blob);
if (returnCode != GHSReturnValue_OK)
{
terminate("Failed on GHSFreeSettings: return code is %d.\n", returnCode);
}
/* Disconnect from the mainframe (this enables Perception or another GHS API client to connect). */
returnCode = GHSDisconnect(connection);
if (returnCode != GHSReturnValue_OK)
{
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);
}