17 namespace AlignmentSubsystem
20 pGetApproximateMountAlignment(&
MathPlugin::GetApproximateMountAlignment),
22 pSetApproximateMountAlignment(&
MathPlugin::SetApproximateMountAlignment),
23 pTransformCelestialToTelescope(&
MathPlugin::TransformCelestialToTelescope),
24 pTransformTelescopeToCelestial(&
MathPlugin::TransformTelescopeToCelestial),
25 pLoadedMathPlugin(&BuiltInPlugin), LoadedMathPluginHandle(nullptr)
27 memset(&AlignmentSubsystemCurrentMathPlugin, 0,
sizeof(
IText));
33 AlignmentSubsystemMathPlugins.reset(
new ISwitch[MathPluginDisplayNames.size() + 1]);
34 IUFillSwitch(AlignmentSubsystemMathPlugins.get(),
"INBUILT_MATH_PLUGIN",
"Inbuilt Math Plugin",
ISS_ON);
36 for (
int i = 0; i < (int)MathPluginDisplayNames.size(); i++)
38 IUFillSwitch(AlignmentSubsystemMathPlugins.get() + i + 1, MathPluginDisplayNames[i].c_str(),
39 MathPluginDisplayNames[i].c_str(),
ISS_OFF);
42 IUFillSwitchVector(&AlignmentSubsystemMathPluginsV, AlignmentSubsystemMathPlugins.get(),
43 MathPluginDisplayNames.size() + 1, ChildTelescope->
getDeviceName(),
47 int configPlugin = -1;
49 if (configPlugin > 0 && configPlugin < AlignmentSubsystemMathPluginsV.
nsp)
52 AlignmentSubsystemMathPluginsV.
sp[configPlugin].s =
ISS_ON;
53 HandlePluginLoading(ChildTelescope, 0, configPlugin);
62 for (
int i = 0; i < AlignmentSubsystemMathPluginsV.
nsp; i++)
64 if (!strcmp(AlignmentSubsystemMathPluginsV.
sp[i].name, sp->name))
67 HandlePluginLoading(ChildTelescope, 0, i);
75 IUFillSwitch(&AlignmentSubsystemMathPluginInitialise,
"ALIGNMENT_SUBSYSTEM_MATH_PLUGIN_INITIALISE",
"OK",
ISS_OFF);
76 IUFillSwitchVector(&AlignmentSubsystemMathPluginInitialiseV, &AlignmentSubsystemMathPluginInitialise, 1,
77 ChildTelescope->
getDeviceName(),
"ALIGNMENT_SUBSYSTEM_MATH_PLUGIN_INITIALISE",
81 IUFillSwitch(&AlignmentSubsystemActive,
"ALIGNMENT SUBSYSTEM ACTIVE",
"Alignment Subsystem Active",
ISS_OFF);
88 IUFillText(&AlignmentSubsystemCurrentMathPlugin,
"ALIGNMENT_SUBSYSTEM_CURRENT_MATH_PLUGIN",
"Current Math Plugin",
89 AlignmentSubsystemMathPlugins.get()[0].label);
90 IUFillTextVector(&AlignmentSubsystemCurrentMathPluginV, &AlignmentSubsystemCurrentMathPlugin, 1,
91 ChildTelescope->
getDeviceName(),
"ALIGNMENT_SUBSYSTEM_CURRENT_MATH_PLUGIN",
"Current Math Plugin",
99 if (strcmp(name, AlignmentSubsystemCurrentMathPluginV.
name) == 0)
101 AlignmentSubsystemCurrentMathPluginV.
s =
IPS_OK;
102 IUUpdateText(&AlignmentSubsystemCurrentMathPluginV, texts, names, n);
104 if (0 != strcmp(AlignmentSubsystemMathPlugins.get()[0].label, AlignmentSubsystemCurrentMathPlugin.text))
107 if (
nullptr != LoadedMathPluginHandle)
110 Destroy_t *
Destroy = (Destroy_t *)dlsym(LoadedMathPluginHandle,
"Destroy");
114 pLoadedMathPlugin =
nullptr;
115 if (0 == dlclose(LoadedMathPluginHandle))
117 LoadedMathPluginHandle =
nullptr;
122 "MathPluginManagement - dlclose failed on loaded plugin - %s", dlerror());
129 "MathPluginManagement - cannot get Destroy function - %s", dlerror());
134 if (
nullptr != (LoadedMathPluginHandle = dlopen(AlignmentSubsystemCurrentMathPlugin.text, RTLD_NOW)))
137 Create_t *
Create = (Create_t *)dlsym(LoadedMathPluginHandle,
"Create");
140 pLoadedMathPlugin =
Create();
145 for (i = 0; i < (int)MathPluginFiles.size(); i++)
147 if (0 == strcmp(AlignmentSubsystemCurrentMathPlugin.text, MathPluginFiles[i].c_str()))
150 if (i < (
int)MathPluginFiles.size())
153 (AlignmentSubsystemMathPlugins.get() + i + 1)->s =
ISS_ON;
155 IDSetSwitch(&AlignmentSubsystemMathPluginsV,
nullptr);
160 "MathPluginManagement - cannot find %s in list of plugins", MathPluginFiles[i].c_str());
172 AlignmentSubsystemCurrentMathPlugin.text, dlerror());
179 if (
nullptr != LoadedMathPluginHandle)
182 Destroy_t *
Destroy = (Destroy_t *)dlsym(LoadedMathPluginHandle,
"Destroy");
186 pLoadedMathPlugin =
nullptr;
187 if (0 == dlclose(LoadedMathPluginHandle))
189 LoadedMathPluginHandle =
nullptr;
194 "MathPluginManagement - dlclose failed on loaded plugin - %s", dlerror());
201 "MathPluginManagement - cannot get Destroy function - %s", dlerror());
205 pLoadedMathPlugin = &BuiltInPlugin;
207 AlignmentSubsystemMathPlugins.get()->s =
ISS_ON;
209 IDSetSwitch(&AlignmentSubsystemMathPluginsV,
nullptr);
215 char *names[],
int n)
219 if (strcmp(name, AlignmentSubsystemMathPluginsV.
name) == 0)
222 IUUpdateSwitch(&AlignmentSubsystemMathPluginsV, states, names, n);
223 AlignmentSubsystemMathPluginsV.
s =
IPS_OK;
225 HandlePluginLoading(pTelescope, CurrentPlugin, NewPlugin);
227 IDSetSwitch(&AlignmentSubsystemMathPluginsV,
nullptr);
229 else if (strcmp(name, AlignmentSubsystemMathPluginInitialiseV.
name) == 0)
231 AlignmentSubsystemMathPluginInitialiseV.
s =
IPS_OK;
234 IDSetSwitch(&AlignmentSubsystemMathPluginInitialiseV,
nullptr);
239 else if (strcmp(name, AlignmentSubsystemActiveV.
name) == 0)
241 AlignmentSubsystemActiveV.
s =
IPS_OK;
242 if (0 ==
IUUpdateSwitch(&AlignmentSubsystemActiveV, states, names, n))
248 void MathPluginManagement::HandlePluginLoading(
Telescope *pTelescope,
int CurrentPlugin,
int NewPlugin)
250 if (NewPlugin != CurrentPlugin)
255 if (0 != CurrentPlugin)
258 Destroy_t *
Destroy = (Destroy_t *)dlsym(LoadedMathPluginHandle,
"Destroy");
262 pLoadedMathPlugin =
nullptr;
263 if (0 == dlclose(LoadedMathPluginHandle))
265 LoadedMathPluginHandle =
nullptr;
270 "MathPluginManagement - dlclose failed on loaded plugin - %s", dlerror());
277 "MathPluginManagement - cannot get Destroy function - %s", dlerror());
284 std::string PluginPath(MathPluginFiles[NewPlugin - 1]);
285 if (
nullptr != (LoadedMathPluginHandle = dlopen(PluginPath.c_str(), RTLD_NOW)))
288 Create_t *
Create = (Create_t *)dlsym(LoadedMathPluginHandle,
"Create");
291 pLoadedMathPlugin =
Create();
294 IUSaveText(&AlignmentSubsystemCurrentMathPlugin, PluginPath.c_str());
306 PluginPath.c_str(), dlerror());
313 pLoadedMathPlugin = &BuiltInPlugin;
321 AlignmentSubsystemActiveV.
s =
IPS_OK;
339 if (Position.latitude >= 0)
356 return (pLoadedMathPlugin->*pGetApproximateMountAlignment)();
366 (pLoadedMathPlugin->*pSetApproximateMountAlignment)(ApproximateAlignment);
373 if (AlignmentSubsystemActive.s ==
ISS_ON)
374 return (pLoadedMathPlugin->*pTransformCelestialToTelescope)(RightAscension, Declination, JulianOffset,
375 ApparentTelescopeDirectionVector);
383 if (AlignmentSubsystemActive.s ==
ISS_ON)
384 return (pLoadedMathPlugin->*pTransformTelescopeToCelestial)(ApparentTelescopeDirectionVector, RightAscension,
390 void MathPluginManagement::EnumeratePlugins()
392 MathPluginFiles.clear();
393 MathPluginDisplayNames.clear();
394 #ifndef OSX_EMBEDED_MODE
399 char MATH_PLUGINS_DIRECTORY[2048];
400 #if defined(__APPLE__)
401 const char *indiprefix = getenv(
"INDIPREFIX");
403 snprintf(MATH_PLUGINS_DIRECTORY, 2048 - 1,
"%s/Contents/Resources/MathPlugins", indiprefix);
405 snprintf(MATH_PLUGINS_DIRECTORY, 2048 - 1, INDI_MATH_PLUGINS_DIRECTORY);
407 snprintf(MATH_PLUGINS_DIRECTORY, 2048 - 1, INDI_MATH_PLUGINS_DIRECTORY);
410 dp = opendir(MATH_PLUGINS_DIRECTORY);
411 strncat(MATH_PLUGINS_DIRECTORY,
"/", 2);
417 std::string PluginPath(MATH_PLUGINS_DIRECTORY);
423 if (0 == strcmp(de->d_name,
"."))
425 if (0 == strcmp(de->d_name,
".."))
429 PluginPath.append(de->d_name);
430 Handle = dlopen(PluginPath.c_str(), RTLD_NOW);
431 if (
nullptr == Handle)
433 IDLog(
"EnumeratePlugins - cannot load plugin %s error %s\n", PluginPath.c_str(), dlerror());
438 typedef const char *GetDisplayName_t();
439 GetDisplayName_t *GetDisplayNamePtr = (GetDisplayName_t *)dlsym(Handle,
"GetDisplayName");
440 if (
nullptr == GetDisplayNamePtr)
442 IDLog(
"EnumeratePlugins - cannot get plugin %s DisplayName error %s\n", PluginPath.c_str(), dlerror());
445 IDLog(
"EnumeratePlugins - found plugin %s\n", GetDisplayNamePtr());
447 MathPluginDisplayNames.push_back(GetDisplayNamePtr());
448 MathPluginFiles.push_back(PluginPath);
455 IDLog(
"EnumeratePlugins - Failed to open %s error %s\n", MATH_PLUGINS_DIRECTORY, strerror(
errno));
This class provides the driver side API to the in memory alignment database.
bool GetDatabaseReferencePosition(IGeographicCoordinates &Position)
Get the database reference position.
void ProcessSwitchProperties(Telescope *pTelescope, const char *name, ISState *states, char *names[], int n)
Call this function from within the ISNewSwitch processing path. The function will handle any math plu...
bool TransformTelescopeToCelestial(const TelescopeDirectionVector &ApparentTelescopeDirectionVector, double &RightAscension, double &Declination)
TransformTelescopeToCelestial Transforms Mount Coords to Celestial (Sky) Coordinates.
void InitProperties(Telescope *pTelescope)
Initialize alignment math plugin properties. It is recommended to call this function within initPrope...
void SaveConfigProperties(FILE *fp)
Call this function to save persistent math plugin properties. This function should be called from wit...
MathPluginManagement()
Default constructor.
void SetApproximateMountAlignment(MountAlignment_t ApproximateAlignment)
Set the approximate alognment of the mount.
void ProcessTextProperties(Telescope *pTelescope, const char *name, char *texts[], char *names[], int n)
Call this function from within the ISNewText processing path. The function will handle any math plugi...
bool TransformCelestialToTelescope(const double RightAscension, const double Declination, double JulianOffset, TelescopeDirectionVector &ApparentTelescopeDirectionVector)
TransformCelestialToTelescope Transforms Celestial (Sky) Coords to Mount Coordinates.
bool Initialise(InMemoryDatabase *pInMemoryDatabase)
Initialise or re-initialise the math plugin. Re-reading the in memory database as necessary.
MountAlignment_t GetApproximateMountAlignment()
Get the approximate alognment of the mount.
enum INDI::AlignmentSubsystem::MathPluginManagement::MountType MountType_t
void SetAlignmentSubsystemActive(bool enable)
SetAlignmentSubsystemActive Enable or Disable alignment subsystem.
void SetApproximateMountAlignmentFromMountType(MountType_t Type)
Call this function to set the ApproximateMountAlignment property of the current Math Plugin....
Provides alignment subsystem functions to INDI alignment math plugins.
InMemoryDatabase * pInMemoryDatabase
MathPlugin(MountAlignment_t ApproximateAlignment=ZENITH)
Default constructor.
const char * getDeviceName() const
void registerProperty(const INDI::Property &property)
Register the property to be able to observe and update.
void IDLog(const char *fmt,...)
void IUSaveConfigSwitch(FILE *fp, const ISwitchVectorProperty *svp)
Add a switch vector property value to the configuration file.
int IUFindOnSwitchIndex(const ISwitchVectorProperty *svp)
Returns the index of first ON switch it finds in the vector switch property.
void IUResetSwitch(ISwitchVectorProperty *svp)
Reset all switches in a switch vector property to OFF.
void IUFillTextVector(ITextVectorProperty *tvp, IText *tp, int ntp, const char *dev, const char *name, const char *label, const char *group, IPerm p, double timeout, IPState s)
Assign attributes for a text vector property. The vector's auxiliary elements will be set to NULL.
void IUSaveText(IText *tp, const char *newtext)
Function to reliably save new text in a IText.
void IUFillSwitch(ISwitch *sp, const char *name, const char *label, ISState s)
Assign attributes for a switch property. The switch's auxiliary elements will be set to NULL.
void IUFillText(IText *tp, const char *name, const char *label, const char *initialText)
Assign attributes for a text property. The text's auxiliary elements will be set to NULL.
void IUSaveConfigText(FILE *fp, const ITextVectorProperty *tvp)
Add a text vector property value to the configuration file.
void IUFillSwitchVector(ISwitchVectorProperty *svp, ISwitch *sp, int nsp, const char *dev, const char *name, const char *label, const char *group, IPerm p, ISRule r, double timeout, IPState s)
Assign attributes for a switch vector property. The vector's auxiliary elements will be set to NULL.
ISwitch * IUFindSwitch(const ISwitchVectorProperty *svp, const char *name)
Find an ISwitch member in a vector switch property.
int IUUpdateSwitch(ISwitchVectorProperty *svp, ISState *states, char *names[], int n)
Update all switches in a switch vector property.
void IDSetSwitch(const ISwitchVectorProperty *svp, const char *fmt,...)
int IUUpdateText(ITextVectorProperty *tvp, char *texts[], char *names[], int n)
Update all text members in a text vector property.
int IUGetConfigOnSwitchIndex(const char *dev, const char *property, int *index)
IUGetConfigOnSwitchIndex Opens configuration file and reads single switch property to find ON switch ...
#define DEBUGFDEVICE(device, priority, msg,...)
void Destroy(DummyMathPlugin *pPlugin)
DummyMathPlugin * Create()
enum INDI::AlignmentSubsystem::MountAlignment MountAlignment_t
Namespace to encapsulate INDI client, drivers, and mediator classes.
Holds a nomalised direction vector (direction cosines)
Holds the connection type.