Instrument Neutral Distributed Interface INDI  1.9.5
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  ISwitchVectorProperty *pPlugins = MathPlugins->getSwitch();
27 
28  for (int i = 0; i < pPlugins->nsp; i++)
29  AvailableMathPlugins.emplace_back(std::string(pPlugins->sp[i].label));
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  ISwitchVectorProperty *pPlugins = MathPlugins->getSwitch();
85 
86  int i;
87  for (i = 0; i < pPlugins->nsp; i++)
88  {
89  if (0 == strcmp(MathPluginName.c_str(), pPlugins->sp[i].label))
90  break;
91  }
92  if (i >= pPlugins->nsp)
93  return false;
94 
95  IUResetSwitch(pPlugins);
96  pPlugins->sp[i].s = ISS_ON;
97  SetDriverBusy();
98  BaseClient->sendNewSwitch(pPlugins);
99  WaitForDriverCompletion();
100  if (IPS_OK != pPlugins->s)
101  {
102  IDLog("SelectMathPlugin - Bad MathPlugins switch state %s\n", pstateStr(pPlugins->s));
103  return false;
104  }
105  return true;
106 }
107 
109 {
110  // Wait for driver to initialise if neccessary
111  WaitForDriverCompletion();
112 
113  ISwitchVectorProperty *pPluginInitialise = PluginInitialise->getSwitch();
114 
115  IUResetSwitch(pPluginInitialise);
116  pPluginInitialise->sp[0].s = ISS_ON;
117  SetDriverBusy();
118  BaseClient->sendNewSwitch(pPluginInitialise);
119  WaitForDriverCompletion();
120  if (IPS_OK != pPluginInitialise->s)
121  {
122  IDLog("ReInitialiseMathPlugin - Bad PluginInitialise switch state %s\n", pstateStr(pPluginInitialise->s));
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
INDI::Property::getName
const char * getName() const
Definition: indiproperty.cpp:289
IPS_OK
@ IPS_OK
Definition: indiapi.h:161
INDI::AlignmentSubsystem::ClientAPIForMathPluginManagement::SelectMathPlugin
bool SelectMathPlugin(const std::string &MathPluginName)
Selects, loads and initialises the named math plugin.
Definition: ClientAPIForMathPluginManagement.cpp:79
INDI::AlignmentSubsystem::ClientAPIForMathPluginManagement::ReInitialiseMathPlugin
bool ReInitialiseMathPlugin()
Re-initialises the current math plugin.
Definition: ClientAPIForMathPluginManagement.cpp:108
INDI::AlignmentSubsystem::ClientAPIForMathPluginManagement::EnumerateMathPlugins
bool EnumerateMathPlugins(MathPluginsList &AvailableMathPlugins)
Return a list of the names of the available math plugins.
Definition: ClientAPIForMathPluginManagement.cpp:19
IUResetSwitch
void IUResetSwitch(ISwitchVectorProperty *svp)
Reset all switches in a switch vector property to OFF.
Definition: indicom.c:1442
IDLog
void void void void void IDLog(const char *msg,...) ATTRIBUTE_FORMAT_PRINTF(1
Function Drivers call to log a message locally.
INDI::Property
Provides generic container for INDI properties.
Definition: indiproperty.h:43
ClientAPIForMathPluginManagement.h
INDI::AlignmentSubsystem::ClientAPIForMathPluginManagement::ProcessNewProperty
void ProcessNewProperty(INDI::Property *PropertyPointer)
Process new property message from driver. This routine should be called from within the newProperty h...
Definition: ClientAPIForMathPluginManagement.cpp:44
pstateStr
const char * pstateStr(IPState s)
Definition: indicom.c:1215
INDI::AlignmentSubsystem::ClientAPIForMathPluginManagement::ProcessNewSwitch
void ProcessNewSwitch(ISwitchVectorProperty *SwitchVectorProperty)
Process new switch message from driver. This routine should be called from within the newSwitch handl...
Definition: ClientAPIForMathPluginManagement.cpp:65
_ISwitchVectorProperty::nsp
int nsp
Definition: indiapi.h:386
IPS_BUSY
@ IPS_BUSY
Definition: indiapi.h:162
INDI::BaseClient
Class to provide basic client functionality.
Definition: baseclient.h:54
INDI::BaseClient::sendNewSwitch
void sendNewSwitch(ISwitchVectorProperty *pp)
Send new Switch command to server.
Definition: baseclient.cpp:1000
_ISwitchVectorProperty::s
IPState s
Definition: indiapi.h:382
INDI
Namespace to encapsulate INDI client, drivers, and mediator classes.
Definition: AlignmentSubsystemForClients.cpp:11
INDI::AlignmentSubsystem::ClientAPIForMathPluginManagement::ProcessNewDevice
void ProcessNewDevice(INDI::BaseDevice *DevicePointer)
Process new device message from driver. This routine should be called from within the newDevice handl...
Definition: ClientAPIForMathPluginManagement.cpp:39
INDI::AlignmentSubsystem::ClientAPIForMathPluginManagement::Initialise
void Initialise(INDI::BaseClient *BaseClient)
Intialise the API.
Definition: ClientAPIForMathPluginManagement.cpp:34
_ISwitchVectorProperty::sp
ISwitch * sp
Definition: indiapi.h:384
INDI::BaseDevice
Class to provide basic INDI device functionality.
Definition: basedevice.h:45
INDI::Property::getSwitch
INDI::PropertyView< ISwitch > * getSwitch() const
Definition: indiproperty.cpp:411
INDI::AlignmentSubsystem::ClientAPIForMathPluginManagement::MathPluginsList
std::vector< std::string > MathPluginsList
Definition: ClientAPIForMathPluginManagement.h:33
_ISwitchVectorProperty
Switch vector property descriptor.
Definition: indiapi.h:365
_ISwitchVectorProperty::name
char name[MAXINDINAME]
Definition: indiapi.h:370
ISS_ON
@ ISS_ON
Definition: indiapi.h:151