| 1 | #include <stdlib.h>
|
|---|
| 2 | #include <string.h>
|
|---|
| 3 | #include <errno.h>
|
|---|
| 4 | #include <zlib.h>
|
|---|
| 5 |
|
|---|
| 6 | #include "defaultdriver.h"
|
|---|
| 7 | #include "indicom.h"
|
|---|
| 8 | #include "base64.h"
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 | INDI::DefaultDriver::DefaultDriver()
|
|---|
| 12 | {
|
|---|
| 13 | pDebug = false;
|
|---|
| 14 | pSimulation = false;
|
|---|
| 15 | }
|
|---|
| 16 |
|
|---|
| 17 | bool INDI::DefaultDriver::loadConfig()
|
|---|
| 18 | {
|
|---|
| 19 | char errmsg[MAXRBUF];
|
|---|
| 20 | bool pResult = false;
|
|---|
| 21 |
|
|---|
| 22 | pResult = IUReadConfig(NULL, deviceID, errmsg) == 0 ? true : false;
|
|---|
| 23 |
|
|---|
| 24 | if (pResult)
|
|---|
| 25 | IDMessage(deviceID, "Configuration successfully loaded.");
|
|---|
| 26 |
|
|---|
| 27 | IUSaveDefaultConfig(NULL, NULL, deviceID);
|
|---|
| 28 |
|
|---|
| 29 | return pResult;
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | bool INDI::DefaultDriver::saveConfig()
|
|---|
| 33 | {
|
|---|
| 34 | std::vector<pOrder>::const_iterator orderi;
|
|---|
| 35 | ISwitchVectorProperty *svp=NULL;
|
|---|
| 36 | char errmsg[MAXRBUF];
|
|---|
| 37 | FILE *fp = NULL;
|
|---|
| 38 |
|
|---|
| 39 | fp = IUGetConfigFP(NULL, deviceID, errmsg);
|
|---|
| 40 |
|
|---|
| 41 | if (fp == NULL)
|
|---|
| 42 | {
|
|---|
| 43 | IDMessage(deviceID, "Error saving configuration. %s", errmsg);
|
|---|
| 44 | return false;
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | IUSaveConfigTag(fp, 0);
|
|---|
| 48 |
|
|---|
| 49 | for (orderi = pAll.begin(); orderi != pAll.end(); orderi++)
|
|---|
| 50 | {
|
|---|
| 51 | switch ( (*orderi).type)
|
|---|
| 52 | {
|
|---|
| 53 | case INDI_NUMBER:
|
|---|
| 54 | IUSaveConfigNumber(fp, static_cast<INumberVectorProperty *> ((*orderi).p));
|
|---|
| 55 | break;
|
|---|
| 56 | case INDI_TEXT:
|
|---|
| 57 | IUSaveConfigText(fp, static_cast<ITextVectorProperty *> ((*orderi).p));
|
|---|
| 58 | break;
|
|---|
| 59 | case INDI_SWITCH:
|
|---|
| 60 | svp = static_cast<ISwitchVectorProperty *> ((*orderi).p);
|
|---|
| 61 | /* Never save CONNECTION property. Don't save switches with no switches on if the rule is one of many */
|
|---|
| 62 | if (!strcmp(svp->name, "CONNECTION") || (svp->r == ISR_1OFMANY && !IUFindOnSwitch(svp)))
|
|---|
| 63 | continue;
|
|---|
| 64 | IUSaveConfigSwitch(fp, svp);
|
|---|
| 65 | break;
|
|---|
| 66 | case INDI_BLOB:
|
|---|
| 67 | IUSaveConfigBLOB(fp, static_cast<IBLOBVectorProperty *> ((*orderi).p));
|
|---|
| 68 | break;
|
|---|
| 69 | }
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | IUSaveConfigTag(fp, 1);
|
|---|
| 73 |
|
|---|
| 74 | fclose(fp);
|
|---|
| 75 |
|
|---|
| 76 | IUSaveDefaultConfig(NULL, NULL, deviceID);
|
|---|
| 77 |
|
|---|
| 78 | IDMessage(deviceID, "Configuration successfully saved.");
|
|---|
| 79 |
|
|---|
| 80 | return true;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | bool INDI::DefaultDriver::loadDefaultConfig()
|
|---|
| 84 | {
|
|---|
| 85 | char configDefaultFileName[MAXRBUF];
|
|---|
| 86 | char errmsg[MAXRBUF];
|
|---|
| 87 | bool pResult = false;
|
|---|
| 88 |
|
|---|
| 89 | if (getenv("INDICONFIG"))
|
|---|
| 90 | snprintf(configDefaultFileName, MAXRBUF, "%s.default", getenv("INDICONFIG"));
|
|---|
| 91 | else
|
|---|
| 92 | snprintf(configDefaultFileName, MAXRBUF, "%s/.indi/%s_config.xml.default", getenv("HOME"), deviceID);
|
|---|
| 93 |
|
|---|
| 94 | if (pDebug)
|
|---|
| 95 | IDLog("Requesting to load default config with: %s\n", configDefaultFileName);
|
|---|
| 96 |
|
|---|
| 97 | pResult = IUReadConfig(configDefaultFileName, deviceID, errmsg) == 0 ? true : false;
|
|---|
| 98 |
|
|---|
| 99 | if (pResult)
|
|---|
| 100 | IDMessage(deviceID, "Default configuration loaded.");
|
|---|
| 101 | else
|
|---|
| 102 | IDMessage(deviceID, "Error loading default configuraiton. %s", errmsg);
|
|---|
| 103 |
|
|---|
| 104 | return pResult;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | bool INDI::DefaultDriver::ISNewSwitch (const char *dev, const char *name, ISState *states, char *names[], int n)
|
|---|
| 108 | {
|
|---|
| 109 |
|
|---|
| 110 | // ignore if not ours //
|
|---|
| 111 | if (strcmp (dev, deviceID))
|
|---|
| 112 | return false;
|
|---|
| 113 |
|
|---|
| 114 | ISwitchVectorProperty *svp = getSwitch(name);
|
|---|
| 115 |
|
|---|
| 116 | if (!svp)
|
|---|
| 117 | return false;
|
|---|
| 118 |
|
|---|
| 119 | if (!strcmp(svp->name, "DEBUG"))
|
|---|
| 120 | {
|
|---|
| 121 | IUUpdateSwitch(svp, states, names, n);
|
|---|
| 122 | ISwitch *sp = IUFindOnSwitch(svp);
|
|---|
| 123 | if (!sp)
|
|---|
| 124 | return false;
|
|---|
| 125 |
|
|---|
| 126 | if (!strcmp(sp->name, "ENABLE"))
|
|---|
| 127 | setDebug(true);
|
|---|
| 128 | else
|
|---|
| 129 | setDebug(false);
|
|---|
| 130 | return true;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | if (!strcmp(svp->name, "SIMULATION"))
|
|---|
| 134 | {
|
|---|
| 135 | IUUpdateSwitch(svp, states, names, n);
|
|---|
| 136 | ISwitch *sp = IUFindOnSwitch(svp);
|
|---|
| 137 | if (!sp)
|
|---|
| 138 | return false;
|
|---|
| 139 |
|
|---|
| 140 | if (!strcmp(sp->name, "ENABLE"))
|
|---|
| 141 | setSimulation(true);
|
|---|
| 142 | else
|
|---|
| 143 | setSimulation(false);
|
|---|
| 144 | return true;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | if (!strcmp(svp->name, "CONFIG_PROCESS"))
|
|---|
| 148 | {
|
|---|
| 149 | IUUpdateSwitch(svp, states, names, n);
|
|---|
| 150 | ISwitch *sp = IUFindOnSwitch(svp);
|
|---|
| 151 | IUResetSwitch(svp);
|
|---|
| 152 | bool pResult = false;
|
|---|
| 153 | if (!sp)
|
|---|
| 154 | return false;
|
|---|
| 155 |
|
|---|
| 156 | if (!strcmp(sp->name, "CONFIG_LOAD"))
|
|---|
| 157 | pResult = loadConfig();
|
|---|
| 158 | else if (!strcmp(sp->name, "CONFIG_SAVE"))
|
|---|
| 159 | pResult = saveConfig();
|
|---|
| 160 | else if (!strcmp(sp->name, "CONFIG_DEFAULT"))
|
|---|
| 161 | pResult = loadDefaultConfig();
|
|---|
| 162 |
|
|---|
| 163 | if (pResult)
|
|---|
| 164 | svp->s = IPS_OK;
|
|---|
| 165 | else
|
|---|
| 166 | svp->s = IPS_ALERT;
|
|---|
| 167 |
|
|---|
| 168 | IDSetSwitch(svp, NULL);
|
|---|
| 169 | return true;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | return false;
|
|---|
| 173 |
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | void INDI::DefaultDriver::addAuxControls()
|
|---|
| 177 | {
|
|---|
| 178 | /**************************************************************************/
|
|---|
| 179 | DebugSP = getSwitch("DEBUG");
|
|---|
| 180 | if (!DebugSP)
|
|---|
| 181 | {
|
|---|
| 182 | DebugSP = new ISwitchVectorProperty;
|
|---|
| 183 | IUFillSwitch(&DebugS[0], "ENABLE", "Enable", ISS_OFF);
|
|---|
| 184 | IUFillSwitch(&DebugS[1], "DISABLE", "Disable", ISS_ON);
|
|---|
| 185 | IUFillSwitchVector(DebugSP, DebugS, NARRAY(DebugS), deviceID, "DEBUG", "Debug", "Options", IP_RW, ISR_1OFMANY, 0, IPS_IDLE);
|
|---|
| 186 | pSwitches.push_back(DebugSP);
|
|---|
| 187 | pOrder debo = {INDI_SWITCH, DebugSP};
|
|---|
| 188 | pAll.push_back(debo);
|
|---|
| 189 | }
|
|---|
| 190 | else
|
|---|
| 191 | {
|
|---|
| 192 | ISwitch *sp = IUFindSwitch(DebugSP, "ENABLE");
|
|---|
| 193 | if (sp)
|
|---|
| 194 | if (sp->s == ISS_ON)
|
|---|
| 195 | pDebug = true;
|
|---|
| 196 | }
|
|---|
| 197 | /**************************************************************************/
|
|---|
| 198 |
|
|---|
| 199 | /**************************************************************************/
|
|---|
| 200 | SimulationSP = getSwitch("SIMULATION");
|
|---|
| 201 | if (!SimulationSP)
|
|---|
| 202 | {
|
|---|
| 203 | SimulationSP = new ISwitchVectorProperty;
|
|---|
| 204 | IUFillSwitch(&SimulationS[0], "ENABLE", "Enable", ISS_OFF);
|
|---|
| 205 | IUFillSwitch(&SimulationS[1], "DISABLE", "Disable", ISS_ON);
|
|---|
| 206 | IUFillSwitchVector(SimulationSP, SimulationS, NARRAY(SimulationS), deviceID, "SIMULATION", "Simulation", "Options", IP_RW, ISR_1OFMANY, 0, IPS_IDLE);
|
|---|
| 207 | pSwitches.push_back(SimulationSP);
|
|---|
| 208 | pOrder simo = {INDI_SWITCH, SimulationSP};
|
|---|
| 209 | pAll.push_back(simo);
|
|---|
| 210 | }
|
|---|
| 211 | else
|
|---|
| 212 | {
|
|---|
| 213 | ISwitch *sp = IUFindSwitch(SimulationSP, "ENABLE");
|
|---|
| 214 | if (sp)
|
|---|
| 215 | if (sp->s == ISS_ON)
|
|---|
| 216 | pSimulation = true;
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | /**************************************************************************/
|
|---|
| 220 | ConfigProcessSP = getSwitch("CONFIG_PROCESS");
|
|---|
| 221 | if (!ConfigProcessSP)
|
|---|
| 222 | {
|
|---|
| 223 | ConfigProcessSP = new ISwitchVectorProperty;
|
|---|
| 224 | IUFillSwitch(&ConfigProcessS[0], "CONFIG_LOAD", "Load", ISS_OFF);
|
|---|
| 225 | IUFillSwitch(&ConfigProcessS[1], "CONFIG_SAVE", "Save", ISS_OFF);
|
|---|
| 226 | IUFillSwitch(&ConfigProcessS[2], "CONFIG_DEFAULT", "Default", ISS_OFF);
|
|---|
| 227 | IUFillSwitchVector(ConfigProcessSP, ConfigProcessS, NARRAY(ConfigProcessS), deviceID, "CONFIG_PROCESS", "Configuration", "Options", IP_RW, ISR_1OFMANY, 0, IPS_IDLE);
|
|---|
| 228 | pSwitches.push_back(ConfigProcessSP);
|
|---|
| 229 | pOrder cpono = {INDI_SWITCH, ConfigProcessSP};
|
|---|
| 230 | pAll.push_back(cpono);
|
|---|
| 231 | }
|
|---|
| 232 | /**************************************************************************/
|
|---|
| 233 |
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | void INDI::DefaultDriver::setDebug(bool enable)
|
|---|
| 237 | {
|
|---|
| 238 | if (pDebug == enable)
|
|---|
| 239 | {
|
|---|
| 240 | DebugSP->s = IPS_OK;
|
|---|
| 241 | IDSetSwitch(DebugSP, NULL);
|
|---|
| 242 | return;
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | if (!DebugSP)
|
|---|
| 246 | return;
|
|---|
| 247 |
|
|---|
| 248 | IUResetSwitch(DebugSP);
|
|---|
| 249 |
|
|---|
| 250 | if (enable)
|
|---|
| 251 | {
|
|---|
| 252 | ISwitch *sp = IUFindSwitch(DebugSP, "ENABLE");
|
|---|
| 253 | if (sp)
|
|---|
| 254 | {
|
|---|
| 255 | sp->s = ISS_ON;
|
|---|
| 256 | IDMessage(deviceID, "Debug is enabled.");
|
|---|
| 257 | }
|
|---|
| 258 | }
|
|---|
| 259 | else
|
|---|
| 260 | {
|
|---|
| 261 | ISwitch *sp = IUFindSwitch(DebugSP, "DISABLE");
|
|---|
| 262 | if (sp)
|
|---|
| 263 | {
|
|---|
| 264 | sp->s = ISS_ON;
|
|---|
| 265 | IDMessage(deviceID, "Debug is disabled.");
|
|---|
| 266 | }
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | pDebug = enable;
|
|---|
| 270 | DebugSP->s = IPS_OK;
|
|---|
| 271 | IDSetSwitch(DebugSP, NULL);
|
|---|
| 272 |
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | void INDI::DefaultDriver::setSimulation(bool enable)
|
|---|
| 276 | {
|
|---|
| 277 | if (pSimulation == enable)
|
|---|
| 278 | {
|
|---|
| 279 | SimulationSP->s = IPS_OK;
|
|---|
| 280 | IDSetSwitch(SimulationSP, NULL);
|
|---|
| 281 | return;
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | if (!SimulationSP)
|
|---|
| 285 | return;
|
|---|
| 286 |
|
|---|
| 287 | IUResetSwitch(SimulationSP);
|
|---|
| 288 |
|
|---|
| 289 | if (enable)
|
|---|
| 290 | {
|
|---|
| 291 | ISwitch *sp = IUFindSwitch(SimulationSP, "ENABLE");
|
|---|
| 292 | if (sp)
|
|---|
| 293 | {
|
|---|
| 294 | IDMessage(deviceID, "Simulation is enabled.");
|
|---|
| 295 | sp->s = ISS_ON;
|
|---|
| 296 | }
|
|---|
| 297 | }
|
|---|
| 298 | else
|
|---|
| 299 | {
|
|---|
| 300 | ISwitch *sp = IUFindSwitch(SimulationSP, "DISABLE");
|
|---|
| 301 | if (sp)
|
|---|
| 302 | {
|
|---|
| 303 | sp->s = ISS_ON;
|
|---|
| 304 | IDMessage(deviceID, "Simulation is disabled.");
|
|---|
| 305 | }
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | pSimulation = enable;
|
|---|
| 309 | SimulationSP->s = IPS_OK;
|
|---|
| 310 | IDSetSwitch(SimulationSP, NULL);
|
|---|
| 311 |
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | bool INDI::DefaultDriver::isDebug()
|
|---|
| 315 | {
|
|---|
| 316 | return pDebug;
|
|---|
| 317 | }
|
|---|
| 318 |
|
|---|
| 319 | bool INDI::DefaultDriver::isSimulation()
|
|---|
| 320 | {
|
|---|
| 321 | return pSimulation;
|
|---|
| 322 | }
|
|---|
| 323 |
|
|---|
| 324 | void INDI::DefaultDriver::ISGetProperties (const char *dev)
|
|---|
| 325 | {
|
|---|
| 326 | std::vector<pOrder>::const_iterator orderi;
|
|---|
| 327 |
|
|---|
| 328 | for (orderi = pAll.begin(); orderi != pAll.end(); orderi++)
|
|---|
| 329 | {
|
|---|
| 330 | switch ( (*orderi).type)
|
|---|
| 331 | {
|
|---|
| 332 | case INDI_NUMBER:
|
|---|
| 333 | IDDefNumber(static_cast<INumberVectorProperty *>((*orderi).p) , NULL);
|
|---|
| 334 | break;
|
|---|
| 335 | case INDI_TEXT:
|
|---|
| 336 | IDDefText(static_cast<ITextVectorProperty *>((*orderi).p) , NULL);
|
|---|
| 337 | break;
|
|---|
| 338 | case INDI_SWITCH:
|
|---|
| 339 | IDDefSwitch(static_cast<ISwitchVectorProperty *>((*orderi).p) , NULL);
|
|---|
| 340 | break;
|
|---|
| 341 | case INDI_LIGHT:
|
|---|
| 342 | IDDefLight(static_cast<ILightVectorProperty *>((*orderi).p) , NULL);
|
|---|
| 343 | break;
|
|---|
| 344 | case INDI_BLOB:
|
|---|
| 345 | IDDefBLOB(static_cast<IBLOBVectorProperty *>((*orderi).p) , NULL);
|
|---|
| 346 | break;
|
|---|
| 347 | }
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 | void INDI::DefaultDriver::resetProperties()
|
|---|
| 353 | {
|
|---|
| 354 | std::vector<INumberVectorProperty *>::const_iterator numi;
|
|---|
| 355 | std::vector<ISwitchVectorProperty *>::const_iterator switchi;
|
|---|
| 356 | std::vector<ITextVectorProperty *>::const_iterator texti;
|
|---|
| 357 | std::vector<ILightVectorProperty *>::const_iterator lighti;
|
|---|
| 358 | std::vector<IBLOBVectorProperty *>::const_iterator blobi;
|
|---|
| 359 |
|
|---|
| 360 | for ( numi = pNumbers.begin(); numi != pNumbers.end(); numi++)
|
|---|
| 361 | {
|
|---|
| 362 | (*numi)->s = IPS_IDLE;
|
|---|
| 363 | IDSetNumber( (*numi), NULL);
|
|---|
| 364 | }
|
|---|
| 365 |
|
|---|
| 366 | for ( switchi = pSwitches.begin(); switchi != pSwitches.end(); switchi++)
|
|---|
| 367 | {
|
|---|
| 368 | (*switchi)->s = IPS_IDLE;
|
|---|
| 369 | IDSetSwitch( (*switchi), NULL);
|
|---|
| 370 | }
|
|---|
| 371 |
|
|---|
| 372 | for ( texti = pTexts.begin(); texti != pTexts.end(); texti++)
|
|---|
| 373 | {
|
|---|
| 374 | (*texti)->s = IPS_IDLE;
|
|---|
| 375 | IDSetText( (*texti), NULL);
|
|---|
| 376 | }
|
|---|
| 377 |
|
|---|
| 378 | for ( lighti = pLights.begin(); lighti != pLights.end(); lighti++)
|
|---|
| 379 | {
|
|---|
| 380 | (*lighti)->s = IPS_IDLE;
|
|---|
| 381 | IDSetLight( (*lighti), NULL);
|
|---|
| 382 | }
|
|---|
| 383 |
|
|---|
| 384 | for ( blobi = pBlobs.begin(); blobi != pBlobs.end(); blobi++)
|
|---|
| 385 | {
|
|---|
| 386 | (*blobi)->s = IPS_IDLE;
|
|---|
| 387 | IDSetBLOB( (*blobi), NULL);
|
|---|
| 388 | }
|
|---|
| 389 | }
|
|---|