1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| /**
* @file HostLib_rtw.c
* @brief Helper for C clients of the HostLib library.
* Copyright 2007-2013 The MathWorks, Inc.
*/
#include <string.h>
#include <stdio.h>
#include "HostLib_rtw.h"
/* Open the library and initialize the function pointers */
void CreateHostLibrary(const char *libName, void *hl)
{
HostLibrary *hostLib = (HostLibrary*)hl;
*hostLib->errorMessage = '\0';
memset(hostLib, 0, sizeof(HostLibrary));
#if defined(_WIN32)
hostLib->library = LoadLibrary(libName);
if(hostLib->library == NULL)
sprintf(hostLib->errorMessage, "Could not open library: %s.\n", libName);
else {
hostLib->libCreate = GetProcAddress(hostLib->library, "libCreate");
hostLib->libStart = GetProcAddress(hostLib->library, "libStart");
hostLib->libReset = GetProcAddress(hostLib->library, "libReset");
hostLib->libUpdate = GetProcAddress(hostLib->library, "libUpdate");
hostLib->libOutputs = GetProcAddress(hostLib->library, "libOutputs");
hostLib->libTerminate = GetProcAddress(hostLib->library, "libTerminate");
hostLib->libDestroy = GetProcAddress(hostLib->library, "libDestroy");
}
#elif defined(_VXWORKS_)
hostLib->library = dlopen(libName, RTLD_NOW);
if(hostLib->library == NULL) {
sprintf(hostLib->errorMessage, "Could not open library: %s", libName);
}
else {
hostLib->libCreate = dlsym(hostLib->library, "libCreate");
hostLib->libStart = dlsym(hostLib->library, "libStart");
hostLib->libReset = dlsym(hostLib->library, "libReset");
hostLib->libUpdate = dlsym(hostLib->library, "libUpdate");
hostLib->libOutputs = dlsym(hostLib->library, "libOutputs");
hostLib->libTerminate = dlsym(hostLib->library, "libTerminate");
hostLib->libDestroy = dlsym(hostLib->library, "libDestroy");
}
#else
hostLib->library = dlopen(libName, RTLD_NOW | RTLD_LOCAL);
if(hostLib->library == NULL) {
sprintf(hostLib->errorMessage, "Could not open library: %s", libName);
}
else {
hostLib->libCreate = dlsym(hostLib->library, "libCreate");
hostLib->libStart = dlsym(hostLib->library, "libStart");
hostLib->libReset = dlsym(hostLib->library, "libReset");
hostLib->libUpdate = dlsym(hostLib->library, "libUpdate");
hostLib->libOutputs = dlsym(hostLib->library, "libOutputs");
hostLib->libTerminate = dlsym(hostLib->library, "libTerminate");
hostLib->libDestroy = dlsym(hostLib->library, "libDestroy");
}
#endif
if(!*hostLib->errorMessage &&
(!hostLib->libCreate ||
!hostLib->libStart ||
!hostLib->libUpdate ||
!hostLib->libOutputs ||
!hostLib->libTerminate ||
!hostLib->libDestroy)
)
{
sprintf(hostLib->errorMessage, "Could not determine function entry points in %s", libName);
}
if(*hostLib->errorMessage) {
if (hostLib->library) DestroyHostLibrary(hostLib);
fprintf(stderr,"%s",hostLib->errorMessage);
}
}
/* Close the library */
void DestroyHostLibrary(void *hl)
{
HostLibrary *hostLib = (HostLibrary*)hl;
if(hostLib->library) {
#if defined(_WIN32)
FreeLibrary(hostLib->library);
#else
dlclose(hostLib->library);
#endif
/* Clear the structure (except for any possible error message). */
memset(hostLib, 0, sizeof(HostLibrary) - MAX_ERR_MSG_LEN);
}
}
void LibStart(void *hl)
{
HostLibrary *hostLib = (HostLibrary*)hl;
if(hostLib->instance)
(MAKE_FCN_PTR(pFnLibStart,hostLib->libStart))(hostLib->instance, hostLib->errorMessage);
}
void LibReset(void *hl)
{
HostLibrary *hostLib = (HostLibrary*)hl;
if(hostLib->instance && hostLib->libReset)
(MAKE_FCN_PTR(pFnLibReset,hostLib->libReset))(hostLib->instance, hostLib->errorMessage);
}
void LibTerminate(void *hl)
{
HostLibrary *hostLib = (HostLibrary*)hl;
if(hostLib->instance)
(MAKE_FCN_PTR(pFnLibTerminate,hostLib->libTerminate))(hostLib->instance, hostLib->errorMessage);
}
void LibDestroy(void *hl, int type)
{
HostLibrary *hostLib = (HostLibrary*)hl;
if(hostLib->instance)
{
(MAKE_FCN_PTR(pFnLibDestroy,hostLib->libDestroy))(hostLib->instance, hostLib->errorMessage, type);
hostLib->instance = NULL;
}
}
int LibError(void *hl)
{
HostLibrary *hostLib = (HostLibrary*)hl;
return hostLib->errorMessage[0] != '\0';
}
void PrintError(char *message)
{
/* Print, and then reset, the error message */
printf("Error: %s\n", message);
message[0] = '\0';
}
void PrintWarning(char *message)
{
/* Print, and then reset, the warning message */
printf("Warning: %s\n", message);
message[0] = '\0';
}
char * GetErrorBuffer(void *hl)
{
HostLibrary *hostLib = (HostLibrary*)hl;
return hostLib->errorMessage;
}
void * GetNullPointer(void)
{
return NULL;
} |
Partager