File: MathPluginManagement.cpp

package info (click to toggle)
indi 2.1.9%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 15,888 kB
  • sloc: cpp: 217,447; ansic: 31,363; xml: 1,195; sh: 311; makefile: 13
file content (461 lines) | stat: -rw-r--r-- 19,112 bytes parent folder | download | duplicates (4)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*!
 * \file MathPluginManagement.cpp
 *
 * \author Roger James
 * \date 13th November 2013
 *
 */

#include "MathPluginManagement.h"

#include <dirent.h>
#include <dlfcn.h>
#include <cerrno>

namespace INDI
{
namespace AlignmentSubsystem
{
MathPluginManagement::MathPluginManagement() : CurrentInMemoryDatabase(nullptr),
    pGetApproximateMountAlignment(&MathPlugin::GetApproximateMountAlignment),
    pInitialise(&MathPlugin::Initialise),
    pSetApproximateMountAlignment(&MathPlugin::SetApproximateMountAlignment),
    pTransformCelestialToTelescope(&MathPlugin::TransformCelestialToTelescope),
    pTransformTelescopeToCelestial(&MathPlugin::TransformTelescopeToCelestial),
    pLoadedMathPlugin(&BuiltInPlugin), LoadedMathPluginHandle(nullptr)
{
    memset(&AlignmentSubsystemCurrentMathPlugin, 0, sizeof(IText));
}

void MathPluginManagement::InitProperties(Telescope *ChildTelescope)
{
    EnumeratePlugins();
    AlignmentSubsystemMathPlugins.reset(new ISwitch[MathPluginDisplayNames.size() + 1]);
    IUFillSwitch(AlignmentSubsystemMathPlugins.get(), "INBUILT_MATH_PLUGIN", "Inbuilt Math Plugin", ISS_ON);

    for (int i = 0; i < (int)MathPluginDisplayNames.size(); i++)
    {
        IUFillSwitch(AlignmentSubsystemMathPlugins.get() + i + 1, MathPluginDisplayNames[i].c_str(),
                     MathPluginDisplayNames[i].c_str(), ISS_OFF);
    }

    IUFillSwitchVector(&AlignmentSubsystemMathPluginsV, AlignmentSubsystemMathPlugins.get(),
                       MathPluginDisplayNames.size() + 1, ChildTelescope->getDeviceName(),
                       "ALIGNMENT_SUBSYSTEM_MATH_PLUGINS", "Math Plugins", ALIGNMENT_TAB, IP_RW, ISR_1OFMANY, 60,
                       IPS_IDLE);

    int configPlugin = -1;
    IUGetConfigOnSwitchIndex(ChildTelescope->getDeviceName(),  "ALIGNMENT_SUBSYSTEM_MATH_PLUGINS", &configPlugin);
    if (configPlugin > 0 && configPlugin < AlignmentSubsystemMathPluginsV.nsp)
    {
        IUResetSwitch(&AlignmentSubsystemMathPluginsV);
        AlignmentSubsystemMathPluginsV.sp[configPlugin].s = ISS_ON;
        HandlePluginLoading(ChildTelescope, 0, configPlugin);
    }
    // Select nearest if available
    else
    {
        ISwitch *sp = IUFindSwitch(&AlignmentSubsystemMathPluginsV, "Nearest Math Plugin");
        if (sp)
        {
            IUResetSwitch(&AlignmentSubsystemMathPluginsV);
            for (int i = 0; i < AlignmentSubsystemMathPluginsV.nsp; i++)
            {
                if (!strcmp(AlignmentSubsystemMathPluginsV.sp[i].name, sp->name))
                {
                    sp->s = ISS_ON;
                    HandlePluginLoading(ChildTelescope, 0, i);
                    break;
                }
            }
        }
    }
    ChildTelescope->registerProperty(&AlignmentSubsystemMathPluginsV);

    IUFillSwitch(&AlignmentSubsystemMathPluginInitialise, "ALIGNMENT_SUBSYSTEM_MATH_PLUGIN_INITIALISE", "OK", ISS_OFF);
    IUFillSwitchVector(&AlignmentSubsystemMathPluginInitialiseV, &AlignmentSubsystemMathPluginInitialise, 1,
                       ChildTelescope->getDeviceName(), "ALIGNMENT_SUBSYSTEM_MATH_PLUGIN_INITIALISE",
                       "(Re)Initialise Plugin", ALIGNMENT_TAB, IP_RW, ISR_ATMOST1, 60, IPS_IDLE);
    ChildTelescope->registerProperty(&AlignmentSubsystemMathPluginInitialiseV);

    IUFillSwitch(&AlignmentSubsystemActive, "ALIGNMENT SUBSYSTEM ACTIVE", "Alignment Subsystem Active", ISS_OFF);
    IUFillSwitchVector(&AlignmentSubsystemActiveV, &AlignmentSubsystemActive, 1, ChildTelescope->getDeviceName(),
                       "ALIGNMENT_SUBSYSTEM_ACTIVE", "Activate alignment subsystem", ALIGNMENT_TAB, IP_RW, ISR_ATMOST1,
                       60, IPS_IDLE);
    ChildTelescope->registerProperty(&AlignmentSubsystemActiveV);

    // The following property is used for configuration purposes only and is not exposed to the client.
    IUFillText(&AlignmentSubsystemCurrentMathPlugin, "ALIGNMENT_SUBSYSTEM_CURRENT_MATH_PLUGIN", "Current Math Plugin",
               AlignmentSubsystemMathPlugins.get()[0].label);
    IUFillTextVector(&AlignmentSubsystemCurrentMathPluginV, &AlignmentSubsystemCurrentMathPlugin, 1,
                     ChildTelescope->getDeviceName(), "ALIGNMENT_SUBSYSTEM_CURRENT_MATH_PLUGIN", "Current Math Plugin",
                     ALIGNMENT_TAB, IP_RO, 60, IPS_IDLE);
}

void MathPluginManagement::ProcessTextProperties(Telescope *pTelescope, const char *name, char *texts[], char *names[],
        int n)
{
    //DEBUGFDEVICE(pTelescope->getDeviceName(), INDI::Logger::DBG_DEBUG, "ProcessTextProperties - name(%s)", name);
    if (strcmp(name, AlignmentSubsystemCurrentMathPluginV.name) == 0)
    {
        AlignmentSubsystemCurrentMathPluginV.s = IPS_OK;
        IUUpdateText(&AlignmentSubsystemCurrentMathPluginV, texts, names, n);

        if (0 != strcmp(AlignmentSubsystemMathPlugins.get()[0].label, AlignmentSubsystemCurrentMathPlugin.text))
        {
            // Unload old plugin if required
            if (nullptr != LoadedMathPluginHandle)
            {
                typedef void Destroy_t(MathPlugin *);
                Destroy_t *Destroy = (Destroy_t *)dlsym(LoadedMathPluginHandle, "Destroy");
                if (nullptr != Destroy)
                {
                    Destroy(pLoadedMathPlugin);
                    pLoadedMathPlugin = nullptr;
                    if (0 == dlclose(LoadedMathPluginHandle))
                    {
                        LoadedMathPluginHandle = nullptr;
                    }
                    else
                    {
                        DEBUGFDEVICE(pTelescope->getDeviceName(), INDI::Logger::DBG_ERROR,
                                     "MathPluginManagement - dlclose failed on loaded plugin - %s", dlerror());
                        AlignmentSubsystemMathPluginsV.s = IPS_ALERT;
                    }
                }
                else
                {
                    DEBUGFDEVICE(pTelescope->getDeviceName(), INDI::Logger::DBG_ERROR,
                                 "MathPluginManagement - cannot get Destroy function - %s", dlerror());
                    AlignmentSubsystemMathPluginsV.s = IPS_ALERT;
                }
            }
            // It is not the built in so try to load it
            if (nullptr != (LoadedMathPluginHandle = dlopen(AlignmentSubsystemCurrentMathPlugin.text, RTLD_NOW)))
            {
                typedef MathPlugin *Create_t();
                Create_t *Create = (Create_t *)dlsym(LoadedMathPluginHandle, "Create");
                if (nullptr != Create)
                {
                    pLoadedMathPlugin = Create();

                    // TODO - Update the client to reflect the new plugin
                    int i = 0;

                    for (i = 0; i < (int)MathPluginFiles.size(); i++)
                    {
                        if (0 == strcmp(AlignmentSubsystemCurrentMathPlugin.text, MathPluginFiles[i].c_str()))
                            break;
                    }
                    if (i < (int)MathPluginFiles.size())
                    {
                        IUResetSwitch(&AlignmentSubsystemMathPluginsV);
                        (AlignmentSubsystemMathPlugins.get() + i + 1)->s = ISS_ON;
                        //  Update client
                        IDSetSwitch(&AlignmentSubsystemMathPluginsV, nullptr);
                    }
                    else
                    {
                        DEBUGFDEVICE(pTelescope->getDeviceName(), INDI::Logger::DBG_WARNING,
                                     "MathPluginManagement - cannot find %s in list of plugins", MathPluginFiles[i].c_str());
                    }
                }
                else
                {
                    DEBUGFDEVICE(pTelescope->getDeviceName(), INDI::Logger::DBG_ERROR, "MathPluginManagement - cannot get Create function - %s",
                                 dlerror());
                }
            }
            else
            {
                DEBUGFDEVICE(pTelescope->getDeviceName(), INDI::Logger::DBG_ERROR, "MathPluginManagement - cannot load plugin %s error %s",
                             AlignmentSubsystemCurrentMathPlugin.text, dlerror());
            }
        }
        else
        {
            // It is the inbuilt plugin
            // Unload old plugin if required
            if (nullptr != LoadedMathPluginHandle)
            {
                typedef void Destroy_t(MathPlugin *);
                Destroy_t *Destroy = (Destroy_t *)dlsym(LoadedMathPluginHandle, "Destroy");
                if (nullptr != Destroy)
                {
                    Destroy(pLoadedMathPlugin);
                    pLoadedMathPlugin = nullptr;
                    if (0 == dlclose(LoadedMathPluginHandle))
                    {
                        LoadedMathPluginHandle = nullptr;
                    }
                    else
                    {
                        DEBUGFDEVICE(pTelescope->getDeviceName(), INDI::Logger::DBG_ERROR,
                                     "MathPluginManagement - dlclose failed on loaded plugin - %s", dlerror());
                        AlignmentSubsystemMathPluginsV.s = IPS_ALERT;
                    }
                }
                else
                {
                    DEBUGFDEVICE(pTelescope->getDeviceName(), INDI::Logger::DBG_ERROR,
                                 "MathPluginManagement - cannot get Destroy function - %s", dlerror());
                    AlignmentSubsystemMathPluginsV.s = IPS_ALERT;
                }
            }
            pLoadedMathPlugin = &BuiltInPlugin;
            IUResetSwitch(&AlignmentSubsystemMathPluginsV);
            AlignmentSubsystemMathPlugins.get()->s = ISS_ON;
            //  Update client
            IDSetSwitch(&AlignmentSubsystemMathPluginsV, nullptr);
        }
    }
}

void MathPluginManagement::ProcessSwitchProperties(Telescope *pTelescope, const char *name, ISState *states,
        char *names[], int n)
{
    //DEBUGFDEVICE(pTelescope->getDeviceName(), INDI::Logger::DBG_DEBUG, "ProcessSwitchProperties - name(%s)", name);
    INDI_UNUSED(pTelescope);
    if (strcmp(name, AlignmentSubsystemMathPluginsV.name) == 0)
    {
        int CurrentPlugin = IUFindOnSwitchIndex(&AlignmentSubsystemMathPluginsV);
        IUUpdateSwitch(&AlignmentSubsystemMathPluginsV, states, names, n);
        AlignmentSubsystemMathPluginsV.s = IPS_OK; // Assume OK for the time being
        int NewPlugin = IUFindOnSwitchIndex(&AlignmentSubsystemMathPluginsV);
        HandlePluginLoading(pTelescope, CurrentPlugin, NewPlugin);
        //  Update client
        IDSetSwitch(&AlignmentSubsystemMathPluginsV, nullptr);
    }
    else if (strcmp(name, AlignmentSubsystemMathPluginInitialiseV.name) == 0)
    {
        AlignmentSubsystemMathPluginInitialiseV.s = IPS_OK;
        IUResetSwitch(&AlignmentSubsystemMathPluginInitialiseV);
        //  Update client display
        IDSetSwitch(&AlignmentSubsystemMathPluginInitialiseV, nullptr);

        // Initialise or reinitialise the current math plugin
        Initialise(CurrentInMemoryDatabase);
    }
    else if (strcmp(name, AlignmentSubsystemActiveV.name) == 0)
    {
        AlignmentSubsystemActiveV.s = IPS_OK;
        if (0 == IUUpdateSwitch(&AlignmentSubsystemActiveV, states, names, n))
            //  Update client
            IDSetSwitch(&AlignmentSubsystemActiveV, nullptr);
    }
}

void MathPluginManagement::HandlePluginLoading(Telescope *pTelescope, int CurrentPlugin, int NewPlugin)
{
    if (NewPlugin != CurrentPlugin)
    {
        MountAlignment_t currentMountAlignment = GetApproximateMountAlignment();
        // New plugin requested
        // Unload old plugin if required
        if (0 != CurrentPlugin)
        {
            typedef void Destroy_t(MathPlugin *);
            Destroy_t *Destroy = (Destroy_t *)dlsym(LoadedMathPluginHandle, "Destroy");
            if (nullptr != Destroy)
            {
                Destroy(pLoadedMathPlugin);
                pLoadedMathPlugin = nullptr;
                if (0 == dlclose(LoadedMathPluginHandle))
                {
                    LoadedMathPluginHandle = nullptr;
                }
                else
                {
                    DEBUGFDEVICE(pTelescope->getDeviceName(), INDI::Logger::DBG_ERROR,
                                 "MathPluginManagement - dlclose failed on loaded plugin - %s", dlerror());
                    AlignmentSubsystemMathPluginsV.s = IPS_ALERT;
                }
            }
            else
            {
                DEBUGFDEVICE(pTelescope->getDeviceName(), INDI::Logger::DBG_ERROR,
                             "MathPluginManagement - cannot get Destroy function - %s", dlerror());
                AlignmentSubsystemMathPluginsV.s = IPS_ALERT;
            }
        }
        // Load the requested plugin if required
        if (0 != NewPlugin)
        {
            std::string PluginPath(MathPluginFiles[NewPlugin - 1]);
            if (nullptr != (LoadedMathPluginHandle = dlopen(PluginPath.c_str(), RTLD_NOW)))
            {
                typedef MathPlugin *Create_t();
                Create_t *Create = (Create_t *)dlsym(LoadedMathPluginHandle, "Create");
                if (nullptr != Create)
                {
                    pLoadedMathPlugin = Create();
                    SetApproximateMountAlignment(currentMountAlignment);
                    Initialise(CurrentInMemoryDatabase);
                    IUSaveText(&AlignmentSubsystemCurrentMathPlugin, PluginPath.c_str());
                }
                else
                {
                    DEBUGFDEVICE(pTelescope->getDeviceName(), INDI::Logger::DBG_ERROR, "MathPluginManagement - cannot get Create function - %s",
                                 dlerror());
                    AlignmentSubsystemMathPluginsV.s = IPS_ALERT;
                }
            }
            else
            {
                DEBUGFDEVICE(pTelescope->getDeviceName(), INDI::Logger::DBG_ERROR, "MathPluginManagement - cannot load plugin %s error %s",
                             PluginPath.c_str(), dlerror());
                AlignmentSubsystemMathPluginsV.s = IPS_ALERT;
            }
        }
        else
        {
            // It is in built plugin just set up the pointers
            pLoadedMathPlugin = &BuiltInPlugin;
        }
    }
}

void MathPluginManagement::SetAlignmentSubsystemActive(bool enable)
{
    AlignmentSubsystemActive.s  = enable ? ISS_ON : ISS_OFF;
    AlignmentSubsystemActiveV.s = IPS_OK;
    IDSetSwitch(&AlignmentSubsystemActiveV, nullptr);
}

void MathPluginManagement::SaveConfigProperties(FILE *fp)
{
    IUSaveConfigText(fp, &AlignmentSubsystemCurrentMathPluginV);
    IUSaveConfigSwitch(fp, &AlignmentSubsystemMathPluginsV);
    IUSaveConfigSwitch(fp, &AlignmentSubsystemActiveV);
}

void MathPluginManagement::SetApproximateMountAlignmentFromMountType(MountType_t Type)
{
    if (EQUATORIAL == Type)
    {
        IGeographicCoordinates Position { 0, 0, 0 };
        if (CurrentInMemoryDatabase->GetDatabaseReferencePosition(Position))
        {
            if (Position.latitude >= 0)
                SetApproximateMountAlignment(NORTH_CELESTIAL_POLE);
            else
                SetApproximateMountAlignment(SOUTH_CELESTIAL_POLE);
        }
        // If no position found, assume northern hemisphere.
        else
            SetApproximateMountAlignment(NORTH_CELESTIAL_POLE);
    }
    else
        SetApproximateMountAlignment(ZENITH);
}

// These must match the function signatures in MathPlugin

MountAlignment_t MathPluginManagement::GetApproximateMountAlignment()
{
    return (pLoadedMathPlugin->*pGetApproximateMountAlignment)();
}

bool MathPluginManagement::Initialise(InMemoryDatabase *pInMemoryDatabase)
{
    return (pLoadedMathPlugin->*pInitialise)(pInMemoryDatabase);
}

void MathPluginManagement::SetApproximateMountAlignment(MountAlignment_t ApproximateAlignment)
{
    (pLoadedMathPlugin->*pSetApproximateMountAlignment)(ApproximateAlignment);
}

bool MathPluginManagement::TransformCelestialToTelescope(const double RightAscension, const double Declination,
        double JulianOffset,
        TelescopeDirectionVector &ApparentTelescopeDirectionVector)
{
    if (AlignmentSubsystemActive.s == ISS_ON)
        return (pLoadedMathPlugin->*pTransformCelestialToTelescope)(RightAscension, Declination, JulianOffset,
                ApparentTelescopeDirectionVector);
    else
        return false;
}

bool MathPluginManagement::TransformTelescopeToCelestial(
    const TelescopeDirectionVector &ApparentTelescopeDirectionVector, double &RightAscension, double &Declination)
{
    if (AlignmentSubsystemActive.s == ISS_ON)
        return (pLoadedMathPlugin->*pTransformTelescopeToCelestial)(ApparentTelescopeDirectionVector, RightAscension,
                Declination);
    else
        return false;
}

void MathPluginManagement::EnumeratePlugins()
{
    MathPluginFiles.clear();
    MathPluginDisplayNames.clear();
#ifndef OSX_EMBEDED_MODE
    dirent *de;
    DIR *dp;

    errno = 0;
    char MATH_PLUGINS_DIRECTORY[2048];
#if defined(__APPLE__)
    const char *indiprefix = getenv("INDIPREFIX");
    if (indiprefix)
        snprintf(MATH_PLUGINS_DIRECTORY, 2048 - 1, "%s/Contents/Resources/MathPlugins", indiprefix);
    else
        snprintf(MATH_PLUGINS_DIRECTORY, 2048 - 1, INDI_MATH_PLUGINS_DIRECTORY);
#else
    snprintf(MATH_PLUGINS_DIRECTORY, 2048 - 1, INDI_MATH_PLUGINS_DIRECTORY);
#endif

    dp    = opendir(MATH_PLUGINS_DIRECTORY);
    strncat(MATH_PLUGINS_DIRECTORY, "/", 2);
    if (dp)
    {
        while (true)
        {
            void *Handle;
            std::string PluginPath(MATH_PLUGINS_DIRECTORY);

            errno = 0;
            de    = readdir(dp);
            if (de == nullptr)
                break;
            if (0 == strcmp(de->d_name, "."))
                continue;
            if (0 == strcmp(de->d_name, ".."))
                continue;

            // Try to load the plugin
            PluginPath.append(de->d_name);
            Handle = dlopen(PluginPath.c_str(), RTLD_NOW);
            if (nullptr == Handle)
            {
                IDLog("EnumeratePlugins - cannot load plugin %s error %s\n", PluginPath.c_str(), dlerror());
                continue;
            }

            // Try to get the plugin display name
            typedef const char *GetDisplayName_t();
            GetDisplayName_t *GetDisplayNamePtr = (GetDisplayName_t *)dlsym(Handle, "GetDisplayName");
            if (nullptr == GetDisplayNamePtr)
            {
                IDLog("EnumeratePlugins - cannot get plugin %s DisplayName error %s\n", PluginPath.c_str(), dlerror());
                continue;
            }
            IDLog("EnumeratePlugins - found plugin %s\n", GetDisplayNamePtr());

            MathPluginDisplayNames.push_back(GetDisplayNamePtr());
            MathPluginFiles.push_back(PluginPath);
            dlclose(Handle);
        }
        closedir(dp);
    }
    else
    {
        IDLog("EnumeratePlugins - Failed to open %s error %s\n", MATH_PLUGINS_DIRECTORY, strerror(errno));
    }
#endif
}

} // namespace AlignmentSubsystem
} // namespace INDI