Instrument Neutral Distributed Interface INDI  2.0.2
ClientAPIForMathPluginManagement.cpp
Go to the documentation of this file.
1 
10 
11 #include <cstring>
12 
13 namespace INDI
14 {
15 namespace AlignmentSubsystem
16 {
17 // Public methods
18 
20 {
21  // Wait for driver to initialise if neccessary
22  WaitForDriverCompletion();
23 
24  AvailableMathPlugins.clear();
25 
26  auto pPlugins = MathPlugins->getSwitch();
27 
28  for (int i = 0; i < pPlugins->count(); i++)
29  AvailableMathPlugins.emplace_back(std::string(pPlugins->at(i)->getLabel()));
30 
31  return true;
32 }
33 
35 {
36  ClientAPIForMathPluginManagement::BaseClient = BaseClient;
37 }
38 
40 {
41  Device = DevicePointer;
42 }
43 
45 {
46  bool GotOneOfMine = true;
47 
48  if (strcmp(PropertyPointer->getName(), "ALIGNMENT_SUBSYSTEM_MATH_PLUGINS") == 0)
49  MathPlugins = PropertyPointer;
50  else if (strcmp(PropertyPointer->getName(), "ALIGNMENT_SUBSYSTEM_MATH_PLUGIN_INITIALISE") == 0)
51  PluginInitialise = PropertyPointer;
52  else
53  GotOneOfMine = false;
54 
55  // Tell the client when all the database proeprties have been set up
56  if (GotOneOfMine && (nullptr != MathPlugins) && (nullptr != PluginInitialise))
57  {
58  // The DriverActionComplete state variable is initialised to false
59  // So I need to call this to set it to true and signal anyone
60  // waiting for the driver to initialise etc.
61  SignalDriverCompletion();
62  }
63 }
64 
66 {
67  if (strcmp(SwitchVectorProperty->name, "ALIGNMENT_SUBSYSTEM_MATH_PLUGINS") == 0)
68  {
69  if (IPS_BUSY != SwitchVectorProperty->s)
70  SignalDriverCompletion();
71  }
72  else if (strcmp(SwitchVectorProperty->name, "ALIGNMENT_SUBSYSTEM_MATH_PLUGIN_INITIALISE") == 0)
73  {
74  if (IPS_BUSY != SwitchVectorProperty->s)
75  SignalDriverCompletion();
76  }
77 }
78 
79 bool ClientAPIForMathPluginManagement::SelectMathPlugin(const std::string &MathPluginName)
80 {
81  // Wait for driver to initialise if neccessary
82  WaitForDriverCompletion();
83 
84  auto pPlugins = MathPlugins->getSwitch();
85 
86  int i;
87  for (i = 0; i < pPlugins->count(); i++)
88  {
89  if (0 == strcmp(MathPluginName.c_str(), pPlugins->at(i)->getLabel()))
90  break;
91  }
92  if (i >= pPlugins->count())
93  return false;
94 
95  pPlugins->reset();
96  pPlugins->at(i)->setState(ISS_ON);
97  SetDriverBusy();
98  BaseClient->sendNewSwitch(pPlugins);
99  WaitForDriverCompletion();
100  if (IPS_OK != pPlugins->getState())
101  {
102  IDLog("SelectMathPlugin - Bad MathPlugins switch state %s\n", pPlugins->getStateAsString());
103  return false;
104  }
105  return true;
106 }
107 
109 {
110  // Wait for driver to initialise if neccessary
111  WaitForDriverCompletion();
112 
113  auto pPluginInitialise = PluginInitialise->getSwitch();
114 
115  pPluginInitialise->reset();
116  pPluginInitialise->at(0)->setState(ISS_ON);
117  SetDriverBusy();
118  BaseClient->sendNewSwitch(pPluginInitialise);
119  WaitForDriverCompletion();
120  if (IPS_OK != pPluginInitialise->getState())
121  {
122  IDLog("ReInitialiseMathPlugin - Bad PluginInitialise switch state %s\n", pPluginInitialise->getStateAsString());
123  return false;
124  }
125  return true;
126 }
127 
128 // Private methods
129 
130 bool ClientAPIForMathPluginManagement::SetDriverBusy()
131 {
132  int ReturnCode = pthread_mutex_lock(&DriverActionCompleteMutex);
133 
134  if (ReturnCode != 0)
135  return false;
136  DriverActionComplete = false;
137  IDLog("SetDriverBusy\n");
138  ReturnCode = pthread_mutex_unlock(&DriverActionCompleteMutex);
139  return ReturnCode == 0;
140 }
141 
142 bool ClientAPIForMathPluginManagement::SignalDriverCompletion()
143 {
144  int ReturnCode = pthread_mutex_lock(&DriverActionCompleteMutex);
145 
146  if (ReturnCode != 0)
147  return false;
148  DriverActionComplete = true;
149  ReturnCode = pthread_cond_signal(&DriverActionCompleteCondition);
150  if (ReturnCode != 0)
151  {
152  ReturnCode = pthread_mutex_unlock(&DriverActionCompleteMutex);
153  return false;
154  }
155  IDLog("SignalDriverCompletion\n");
156  ReturnCode = pthread_mutex_unlock(&DriverActionCompleteMutex);
157  return ReturnCode == 0;
158 }
159 
160 bool ClientAPIForMathPluginManagement::WaitForDriverCompletion()
161 {
162  int ReturnCode = pthread_mutex_lock(&DriverActionCompleteMutex);
163 
164  while (!DriverActionComplete)
165  {
166  IDLog("WaitForDriverCompletion - Waiting\n");
167  ReturnCode = pthread_cond_wait(&DriverActionCompleteCondition, &DriverActionCompleteMutex);
168  IDLog("WaitForDriverCompletion - Back from wait ReturnCode = %d\n", ReturnCode);
169  if (ReturnCode != 0)
170  {
171  ReturnCode = pthread_mutex_unlock(&DriverActionCompleteMutex);
172  return false;
173  }
174  }
175  IDLog("WaitForDriverCompletion - Finished waiting\n");
176  ReturnCode = pthread_mutex_unlock(&DriverActionCompleteMutex);
177  return ReturnCode == 0;
178 }
179 
180 } // namespace AlignmentSubsystem
181 } // namespace INDI
void sendNewSwitch(INDI::Property pp)
Send new Switch command to server.
bool EnumerateMathPlugins(MathPluginsList &AvailableMathPlugins)
Return a list of the names of the available math plugins.
void ProcessNewDevice(INDI::BaseDevice *DevicePointer)
Process new device message from driver. This routine should be called from within the newDevice handl...
void ProcessNewProperty(INDI::Property *PropertyPointer)
Process new property message from driver. This routine should be called from within the newProperty h...
void Initialise(INDI::BaseClient *BaseClient)
Intialise the API.
void ProcessNewSwitch(ISwitchVectorProperty *SwitchVectorProperty)
Process new switch message from driver. This routine should be called from within the newSwitch handl...
bool SelectMathPlugin(const std::string &MathPluginName)
Selects, loads and initialises the named math plugin.
Class to provide basic client functionality.
Definition: baseclient.h:52
Class to provide basic INDI device functionality.
Definition: basedevice.h:52
Provides generic container for INDI properties.
Definition: indiproperty.h:48
INDI::PropertyViewSwitch * getSwitch() const
const char * getName() const
@ ISS_ON
Definition: indiapi.h:152
@ IPS_BUSY
Definition: indiapi.h:163
@ IPS_OK
Definition: indiapi.h:162
void IDLog(const char *fmt,...)
Definition: indicom.c:316
Namespace to encapsulate INDI client, drivers, and mediator classes.
Switch vector property descriptor.
Definition: indiapi.h:367
char name[MAXINDINAME]
Definition: indiapi.h:371