~ubuntu-branches/ubuntu/utopic/knutclient/utopic

« back to all changes in this revision

Viewing changes to knutclient/knutnet.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Quette
  • Date: 2008-06-19 11:09:50 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20080619110950-izi2q2vd0dhwlpym
Tags: 0.9.4-1
* New upstream release
* Merge back Ubuntu changes
* debian/knutclient.menu: update section to fix lintian warnings
* debian/watch: update the pattern for matching new releases (Closes: #471247)
* debian/control:
  - move Homepage to the field and update the URL
  - update Standards-Version to 3.8.0
  - update debhelper Build-Depends to 5.0.51
  - add automake and autoconf to Build-Depends
  - update NUT URL
* debian/rules:
  - fix lintian warning debian-rules-ignores-make-clean-error
  - add a commented call to dh_icons

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
                          knutnet.cpp  -  description
3
 
                             -------------------
4
 
    begin                : Ne led 12 2003
5
 
    copyright            : (C) 2003 by Daniel Prynych
6
 
    email                : Daniel.Prynych@alo.cz
7
 
 ***************************************************************************/
8
 
 
9
 
/***************************************************************************
10
 
 *                                                                         *
11
 
 *   This program is free software; you can redistribute it and/or modify  *
12
 
 *   it under the terms of the GNU General Public License as published by  *
13
 
 *   the Free Software Foundation; either version 2 of the License, or     *
14
 
 *   (at your option) any later version.                                   *
15
 
 *                                                                         *
16
 
 ***************************************************************************/
17
 
 
18
 
#include "knutnet.h"
19
 
 
20
 
#include <qtextstream.h>
21
 
#include <qstring.h>
22
 
#include <qcstring.h>
23
 
 
24
 
 
25
 
 
26
 
#include <sys/socket.h>
27
 
#include <sys/types.h>
28
 
#include <sys/time.h>
29
 
#include <arpa/inet.h>
30
 
#include <netdb.h>
31
 
#include <netinet/in.h>
32
 
#include <unistd.h>
33
 
#include <errno.h>
34
 
#include <fcntl.h>
35
 
 
36
 
 
37
 
/****** KONSTANTY ***********/
38
 
 
39
 
const int rBufferLen = 1024;
40
 
 
41
 
 
42
 
 
43
 
KNutNet::KNutNet (const QString netAddress, const QString netName, const bool netProtocol, const unsigned short port, unsigned int countRepeat, const unsigned  int delay, QObject *parent, const char *name) : QObject(parent,name), protocol(netProtocol) {
44
 
 
45
 
  state = NotConnected;
46
 
  numberVars = 0;
47
 
  numberRWVars = 0;
48
 
  numberIComms = 0;
49
 
  upsStatusVar = 0;
50
 
  nutProtocol = 0;
51
 
  nutVariables = 0;
52
 
  switchToTCP = false;
53
 
  
54
 
  description = false; // popis neni natazen
55
 
 
56
 
  upsAddress = netAddress;
57
 
  upsName = netName;
58
 
            
59
 
  if (upsAddress.isEmpty()) {
60
 
    error = NullAddress; // jestlize addresa neexistuje koncime
61
 
    state = ConnectError;
62
 
    return;
63
 
    }
64
 
  else {
65
 
    error = NoError;
66
 
    // zjistime IP adresu - vynulujeme strukturu
67
 
    bzero ((char *) &upsAddrServer, sizeof (upsAddrServer));
68
 
    upsAddrServer.sin_family = AF_INET;
69
 
    //nastavime port
70
 
    upsAddrServer.sin_port = htons(port);
71
 
    QCString netAddress = upsAddress.local8Bit();
72
 
    long returnAddress;
73
 
 
74
 
    if ((returnAddress = inet_addr(netAddress.data())) == -1) {
75
 
      // neni to teckova adresa zkusime jmenou
76
 
      struct hostent *phe; 
77
 
      if ((phe = gethostbyname(netAddress.data())) != 0L)
78
 
        bcopy(phe->h_addr,(char *)&upsAddrServer.sin_addr,phe->h_length); // adresa nalezena - prevedeme adresu
79
 
      else {
80
 
        error = NoSuchHost;
81
 
        state = ConnectError;
82
 
        return;
83
 
        }
84
 
      }
85
 
    else upsAddrServer.sin_addr.s_addr = (unsigned long)returnAddress; // prevedeme adresu
86
 
    // v upsAddrServer.sin_addr.s_addr mame ulozenou prevedenou adresu
87
 
 
88
 
    // spojime se se serverem a vytvorime socket
89
 
    if (protocol) socAddr = socket (AF_INET,SOCK_STREAM,0);
90
 
    else socAddr = socket (AF_INET,SOCK_DGRAM,0);
91
 
    if (socket < 0) {
92
 
      error=SocketErr;
93
 
      state = ConnectError;
94
 
      return;
95
 
      }
96
 
    // zkusime nekolikrat pripojit
97
 
    countRepeat++; // maximalni pocet cyklu je pocet opakovani + 1
98
 
    int retVal;
99
 
 
100
 
    retVal = makeConnect(countRepeat, delay);
101
 
 
102
 
    if (retVal) {
103
 
      close(socAddr);
104
 
      error=CantConnect;
105
 
      state = ConnectError;
106
 
      return;
107
 
      }
108
 
    int n = version (countRepeat, delay);
109
 
 
110
 
    if (n) {
111
 
    // pokud server pro pouziti UDP neodpovi zkusime jeste TCP
112
 
      if (!protocol) {
113
 
        close(socAddr);
114
 
        socAddr = socket (AF_INET,SOCK_STREAM,0);
115
 
        countRepeat++; // maximalni pocet cyklu je pocet opakovani + 1
116
 
        int retVal;
117
 
 
118
 
        retVal = makeConnect(countRepeat, delay);
119
 
 
120
 
        if (retVal) {
121
 
          close(socAddr);
122
 
          error=CantConnect;
123
 
          state = ConnectError;
124
 
          return;
125
 
          }
126
 
        protocol = !protocol;
127
 
        switchToTCP = true;
128
 
        // promena error je nastavena v procedure version
129
 
        int n = version (countRepeat, delay);
130
 
        if (n) {
131
 
          close(socAddr);
132
 
          state = ConnectError;
133
 
          return;
134
 
          }
135
 
        }
136
 
      else {
137
 
        close(socAddr);
138
 
        state = ConnectError;
139
 
        return;
140
 
        }
141
 
      }
142
 
 
143
 
    if ((nutProtocol == 0) || (nutVariables == 0)) { // ansfer from upsd (VER) is unknown
144
 
      close(socAddr);
145
 
      error=NutProtocolVarErr;
146
 
      state = ConnectError;
147
 
      return;
148
 
      }
149
 
 
150
 
    if ((nutProtocol == 2) && !protocol) { //ups > 1.3 doesn't support udp
151
 
      close(socAddr);
152
 
      socAddr = socket (AF_INET,SOCK_STREAM,0);
153
 
      countRepeat++; // maximalni pocet cyklu je pocet opakovani + 1
154
 
      int retVal;
155
 
 
156
 
      retVal = makeConnect(countRepeat, delay);
157
 
 
158
 
      if (retVal) {
159
 
        close(socAddr);
160
 
        error=CantConnect;
161
 
        state = ConnectError;
162
 
        return;
163
 
        }
164
 
      protocol = !protocol;  
165
 
      switchToTCP = true;  
166
 
      }
167
 
 
168
 
    if ((nutProtocol == 2) && upsName.isEmpty()) {
169
 
      if ((error =KNutNet::getFirstUpsName (upsName))) { // reading first upsName
170
 
        close(socAddr);
171
 
        state = ConnectError;
172
 
        return;
173
 
        }
174
 
      }
175
 
    }
176
 
  state=Connected;
177
 
  netMutex=false;
178
 
  }
179
 
 
180
 
 
181
 
KNutNet::~KNutNet(){
182
 
  QCString  inBuffer;
183
 
  QString  outBuffer;
184
 
 
185
 
  if (state == Connected) {
186
 
    inBuffer="LOGOUT\n";
187
 
    getUpsData (inBuffer,outBuffer);
188
 
    }
189
 
      
190
 
  deleteVars();
191
 
  }
192
 
 
193
 
  
194
 
int KNutNet::getError ( void ) { return error; }
195
 
 
196
 
int KNutNet::getState ( void ){ return state; }
197
 
 
198
 
bool KNutNet::isDescription ( void ){ return description; }
199
 
      
200
 
int KNutNet::getNutProtocol ( void ) { return nutProtocol; }
201
 
 
202
 
int KNutNet::getNutVariables ( void ) { return nutVariables; }
203
 
 
204
 
bool KNutNet::switchedToTCP ( void ) { return switchToTCP; }
205
 
 
206
 
 
207
 
int KNutNet::getUpsVars ( void) {
208
 
  error=0;
209
 
  if (state == Connected) {
210
 
 
211
 
    // init vars and lists
212
 
    numberVars = 0;
213
 
    numberRWVars = 0;
214
 
    numberIComms = 0;
215
 
    deleteVars();
216
 
    switch (nutProtocol) {
217
 
      case 1:
218
 
        return getUpsVars1();
219
 
      break;
220
 
      case 2:
221
 
        return getUpsVars2();
222
 
      break;
223
 
      default:
224
 
      return 0;  
225
 
      }  
226
 
    }
227
 
  else {
228
 
    return NotConnection;
229
 
    }
230
 
  }
231
 
 
232
 
 
233
 
int KNutNet::getUpsValues (const bool allVars ) {// allVars = true vse; allVars = false jen activate
234
 
 
235
 
  error=0; // vynulujeme chyby
236
 
  if (state == Connected) {
237
 
    // init vars and lists
238
 
    switch (nutProtocol) {
239
 
      case 1:
240
 
        return getUpsValues1(allVars);
241
 
      break;
242
 
      case 2:
243
 
        return getUpsValues2(allVars);
244
 
      break;
245
 
      default:
246
 
      return 0;
247
 
      }
248
 
    }
249
 
  else {
250
 
    return NotConnection;
251
 
    }
252
 
  }  
253
 
 
254
 
 
255
 
 
256
 
int KNutNet::getDescription (bool always) {
257
 
 
258
 
  error=0; // vynulujeme chyby
259
 
  if (state == Connected) {
260
 
    switch (nutProtocol) {
261
 
      case 1:
262
 
        return getDescription1 (always);
263
 
      break;
264
 
      case 2:
265
 
        return getDescription2 (always);
266
 
      break;
267
 
      default:
268
 
        return 0;
269
 
      }
270
 
    }  
271
 
  else return NotConnection;
272
 
  }
273
 
 
274
 
 
275
 
int KNutNet::instantCommand (const QString command, const QString userName, const QString password, const bool onlyCommand) {
276
 
  error=0; // vynulujeme chyby
277
 
  if (state == Connected) {
278
 
// tuto cast pouzijem az uz nebudeme podporovat starsi verze KDE
279
 
//    if (netMutex.locked()) {
280
 
//      // proces bezi pockame na dokonceni
281
 
//      netMutex.lock(); // cekame na volny pristup
282
 
//      }
283
 
//    netMutex.lock();
284
 
//
285
 
    if (netMutex) while (netMutex) {};
286
 
    netMutex = true;
287
 
    QString outBuffer;
288
 
 
289
 
    // funkce sendComm sama mastavi promenou error
290
 
    if (onlyCommand) {
291
 
      if (!(error = sendComm("INSTCMD", command, "",true))) {
292
 
        netMutex=false;
293
 
        return 0;
294
 
        }
295
 
      else { netMutex=false; return error;}
296
 
      }
297
 
    else {
298
 
      if (!(error = sendComm ("USERNAME", userName, ""))) {
299
 
        if (!(error = sendComm("PASSWORD", password, ""))) {
300
 
          if (!(error = sendComm("INSTCMD", command, "",true))) {
301
 
            netMutex=false;
302
 
            return 0;
303
 
            }
304
 
          else { netMutex=false; return error;}
305
 
          } //username
306
 
        else { netMutex=false; return error;}
307
 
        }
308
 
      else { netMutex=false; return error;}
309
 
      }
310
 
    }
311
 
  else return NotConnection;
312
 
  }
313
 
 
314
 
 
315
 
int KNutNet::setVariable (const QString variable, const QString value, const QString userName, const QString password, const bool onlyVariable) {
316
 
  error=0; // vynulujeme chyby
317
 
  if (state == Connected) {
318
 
// tuto cast pouzijem az uz nebudeme podporovat starsi verze KDE
319
 
//    if (netMutex.locked()) {
320
 
//      // proces bezi pockame na dokonceni
321
 
//      netMutex.lock(); // cekame na volny pristup
322
 
//      }
323
 
//    netMutex.lock();
324
 
//
325
 
    if (netMutex) while (netMutex) {};
326
 
    netMutex = true;
327
 
    QString outBuffer;
328
 
    QString setCommand;
329
 
 
330
 
    switch (nutProtocol) {
331
 
      case 1: setCommand = "SET";
332
 
         break;
333
 
      case 2: setCommand = "SET VAR";
334
 
      }
335
 
    if (onlyVariable) {
336
 
      if (!(error = sendComm(setCommand, variable, value, true))) {
337
 
        netMutex=false;
338
 
        return 0;
339
 
        }
340
 
      else { netMutex=false; return error;}
341
 
      }
342
 
    else {
343
 
      if (!(error = sendComm ("USERNAME", userName, ""))) {
344
 
        if (!(error = sendComm("PASSWORD", password, ""))) {
345
 
          if (!(error = sendComm(setCommand, variable, value, true))) {
346
 
            netMutex=false;
347
 
            return 0;
348
 
            }
349
 
          else { netMutex=false; return error;}
350
 
          } //username
351
 
        else { netMutex=false; return error;}
352
 
        }
353
 
      else { netMutex=false; return error;}
354
 
      }
355
 
    }
356
 
  else return NotConnection;
357
 
  }
358
 
 
359
 
 
360
 
int KNutNet::readNumberVars (typeOfVar typVar) {
361
 
//  Vraci pocet promenych
362
 
  error=0;
363
 
  if (state == Connected) {
364
 
    switch (typVar) {
365
 
      case AllVars:
366
 
        return numberVars;
367
 
        break;
368
 
      case ROVars:
369
 
        return numberVars - numberRWVars;
370
 
        break;
371
 
      case RWVars:
372
 
        return numberRWVars;
373
 
        break;
374
 
      default:
375
 
        return -1;
376
 
      }
377
 
    }
378
 
  else return -1;
379
 
  }
380
 
 
381
 
int KNutNet::readNumberComms (void) {
382
 
//  Vraci pocet prikazu
383
 
  error=0;
384
 
  if (state == Connected) return numberIComms;
385
 
  else return -1;
386
 
  }
387
 
 
388
 
 
389
 
int KNutNet::readIComm (const int seqNumber, struct upsIComm& iComm) {
390
 
  error =0;
391
 
  if (state == Connected) {
392
 
  #if defined  (KDE_VERSION_MAJOR)
393
 
     #if KDE_VERSION_MAJOR >= 3
394
 
    if ((seqNumber < 1) || ((unsigned int)seqNumber > listIComms.size())) {
395
 
     #else
396
 
    if ((seqNumber < 1) || ((unsigned int)seqNumber > listIComms.count())) {
397
 
     #endif
398
 
   #else
399
 
    if ((seqNumber < 1) || ((unsigned int)seqNumber > listIComms.count())) {
400
 
  #endif
401
 
      error=CommNotFind;
402
 
      return error;
403
 
      }
404
 
    iComm = listIComms[seqNumber-1];
405
 
    return 0;
406
 
    }
407
 
  else return NotConnection;
408
 
  }  
409
 
 
410
 
 
411
 
int KNutNet::readVars (const QString name, struct upsVar& allVar) {
412
 
  error =0;
413
 
  if (state == Connected) {
414
 
  #if defined  (KDE_VERSION_MAJOR)
415
 
    #if KDE_VERSION_MAJOR >= 3
416
 
      QValueVector<upsVar>::const_iterator it;
417
 
    #else
418
 
      QValueList<upsVar>::ConstIterator it;
419
 
    #endif
420
 
  #else
421
 
    QValueList<upsVar>::ConstIterator it;
422
 
  #endif
423
 
    for (it = listVars.begin(); it != listVars.end(); it++) {
424
 
       if ((*it).upsVarName == name) {
425
 
         allVar = (*it);
426
 
         return 0;
427
 
         }
428
 
       }
429
 
     error=VarNotFind;
430
 
     return error;
431
 
    }
432
 
  else return NotConnection;
433
 
  }
434
 
 
435
 
  int KNutNet::readVars (const int seqNumber, struct upsVar& allVar, const typeOfVar typVar) {
436
 
  error =0;
437
 
  if (state == Connected) {
438
 
    int n = 1;
439
 
    #if defined  (KDE_VERSION_MAJOR)
440
 
      #if KDE_VERSION_MAJOR >= 3
441
 
        QValueVector<upsVar>::const_iterator it;
442
 
      #else
443
 
        QValueList<upsVar>::ConstIterator it;
444
 
      #endif
445
 
    #else
446
 
      QValueList<upsVar>::ConstIterator it;
447
 
    #endif
448
 
 
449
 
    for (it = listVars.begin(); it != listVars.end(); it++) {
450
 
      if ((typVar== AllVars) || ((typVar==ROVars) && ((*it).upsVarType)) ||
451
 
        ((typVar==RWVars) && (!(*it).upsVarType))) {
452
 
        if (n == seqNumber) {
453
 
          allVar = (*it);
454
 
          return 0;
455
 
          }
456
 
        n++;
457
 
        }
458
 
      }
459
 
    error=VarNotFind;
460
 
    return error;
461
 
    }
462
 
  else return NotConnection;
463
 
  }  
464
 
 
465
 
 
466
 
QString KNutNet::readStringVar (const QString name) {
467
 
  error =0;
468
 
  if (state == Connected) {
469
 
  #if defined  (KDE_VERSION_MAJOR)
470
 
    #if KDE_VERSION_MAJOR >= 3
471
 
      QValueVector<upsVar>::const_iterator it;
472
 
    #else
473
 
      QValueList<upsVar>::ConstIterator it;
474
 
    #endif
475
 
  #else
476
 
    QValueList<upsVar>::ConstIterator it;
477
 
  #endif
478
 
 
479
 
 
480
 
    for (it = listVars.begin(); it != listVars.end(); it++) {
481
 
      if ((*it).upsVarName == name) return (*it).upsValue;
482
 
      }
483
 
    error = VarNotFind;
484
 
    return 0l;  
485
 
    }
486
 
  else return 0l;
487
 
}
488
 
 
489
 
 
490
 
QString KNutNet::readEnumValueVar (const QString name, const int valueNumber) {
491
 
  error =0;
492
 
  if (state == Connected) {
493
 
    #if defined  (KDE_VERSION_MAJOR)
494
 
      #if KDE_VERSION_MAJOR >= 3
495
 
        QValueVector<upsVar>::const_iterator it;
496
 
      #else
497
 
        QValueList<upsVar>::ConstIterator it;
498
 
      #endif
499
 
    #else
500
 
      QValueList<upsVar>::ConstIterator it;
501
 
    #endif
502
 
 
503
 
 
504
 
    for (it = listVars.begin(); it != listVars.end(); it++) {
505
 
      if ((*it).upsVarName == name) {
506
 
        if ((valueNumber < 1) || (valueNumber > (*it).upsVarMax)) {
507
 
          error=EnumValueNotFind;
508
 
          return 0l;
509
 
          }
510
 
        // vratime hodnotu
511
 
        return (*(*it).upsEnumValues)[valueNumber-1];
512
 
        }
513
 
      }
514
 
    error = VarNotFind;
515
 
    return 0l;
516
 
    }
517
 
  else return 0l;
518
 
  }
519
 
 
520
 
 
521
 
int KNutNet::readStatus(void) {
522
 
  error=0;
523
 
  return upsStatusVar;
524
 
  }
525
 
 
526
 
  
527
 
int KNutNet::setActivate ( const QString name ) {
528
 
  return activate (name,true);
529
 
  }
530
 
 
531
 
 
532
 
int KNutNet::unSetActivate ( const QString name ) {
533
 
  return activate (name,false);
534
 
  }
535
 
 
536
 
 
537
 
int KNutNet::unSetActivateAll ( void ) {
538
 
  return activateAll (false);
539
 
  }
540
 
                  
541
 
 
542
 
int KNutNet::existName ( const QString name ) {
543
 
  error =0;
544
 
  if (state == Connected) {
545
 
    #if defined  (KDE_VERSION_MAJOR)
546
 
      #if KDE_VERSION_MAJOR >= 3
547
 
        QValueVector<upsVar>::const_iterator it;
548
 
      #else
549
 
        QValueList<upsVar>::ConstIterator it;
550
 
      #endif
551
 
    #else
552
 
      QValueList<upsVar>::ConstIterator it;
553
 
    #endif
554
 
    for (it = listVars.begin(); it != listVars.end(); it++) {
555
 
      if ((*it).upsVarName == name) return 0;
556
 
      }
557
 
    error = VarNotFind;
558
 
    return error;
559
 
    }
560
 
  else return NotConnection;
561
 
  }
562
 
 
563
 
/*************************************************************************/  
564
 
/*                                                                       */
565
 
/*                           PRIVATE FUNCTIONS                           */
566
 
/*                                                                       */
567
 
/*************************************************************************/
568
 
 
569
 
int KNutNet::makeConnect (int countRepeat, const int delay) {
570
 
int retVal;
571
 
 
572
 
  do {
573
 
    // pouzijem connect
574
 
    if (!( retVal = ::connect (socAddr,(struct sockaddr *)&upsAddrServer,sizeof(upsAddrServer)))) break;
575
 
      countRepeat--;
576
 
      if (countRepeat) sleep (delay);
577
 
      }
578
 
    while ((countRepeat));
579
 
  return retVal;
580
 
  }
581
 
 
582
 
 
583
 
int KNutNet::version (int countRepeat, const int delay) {
584
 
  QCString  inBuffer;
585
 
  QString  outBuffer;
586
 
 
587
 
  error =0;
588
 
  do {
589
 
    inBuffer="VER\n";
590
 
    if (!(error=getUpsData (inBuffer,outBuffer))) {
591
 
      outBuffer.stripWhiteSpace ();
592
 
 
593
 
      if (outBuffer.length() > 0) {
594
 
        outBuffer = outBuffer.mid(outBuffer.find("upsd")+4);
595
 
        int n = outBuffer.find(".");
596
 
        if (n > 0) {
597
 
          QString firstNumber= outBuffer.left(n);
598
 
          QString secondNumber= outBuffer.mid(n+1);
599
 
          int n = secondNumber.find(".");
600
 
          if (n > 0) {
601
 
            bool ok;
602
 
            secondNumber = secondNumber.left(n);
603
 
            int secondInt = secondNumber.toInt(&ok);
604
 
            if (!ok) {error = UnknownFormatVer; return error;}
605
 
            int firstInt = firstNumber.toInt(&ok);
606
 
            if (!ok) {error = UnknownFormatVer; return error;}
607
 
            if ((firstInt == 0) || ((firstInt == 1)  && (secondInt < 3))) {
608
 
              nutProtocol =1;
609
 
              nutVariables =1;
610
 
              }
611
 
            else {
612
 
              nutProtocol =2;
613
 
              nutVariables =2;
614
 
              }
615
 
            return 0;
616
 
            }
617
 
          else error = UnknownFormatVer;
618
 
          }
619
 
        else error = UnknownFormatVer;
620
 
        }
621
 
      error = UnknownAnswer;
622
 
      return error;
623
 
      }
624
 
    countRepeat--;
625
 
    if (countRepeat) sleep (delay);
626
 
    }
627
 
  while ((countRepeat));
628
 
  return error;
629
 
  }
630
 
 
631
 
  
632
 
 
633
 
int KNutNet::getFirstUpsName (QString& firstUpsName) {
634
 
  QCString  inBuffer;
635
 
  QString  outBuffer;
636
 
  QString  varOutBuffer;
637
 
  int lenString;
638
 
  
639
 
  error=0;
640
 
  inBuffer = "LIST UPS\n";
641
 
  if (!(error=getUpsData (inBuffer,outBuffer,"END LIST UPS"))) {
642
 
 
643
 
    QString inLine, upsNameRet, varName, value;
644
 
    int key, typeValue;
645
 
    bool beginList = false;
646
 
    QTextIStream varInputStream(&outBuffer);
647
 
    while (!(inLine = varInputStream.readLine()).isNull()) {
648
 
      key = parseLine (inLine, upsNameRet, varName, value, typeValue, lenString);
649
 
      switch (key) {
650
 
        case BEGIN_LIST_UPS: {
651
 
          beginList = true;;
652
 
          break;
653
 
          }
654
 
        case UPS:
655
 
         if (beginList ) {
656
 
           firstUpsName = upsNameRet;
657
 
           return 0;
658
 
           }
659
 
        case END_LIST_UPS:
660
 
          firstUpsName = "";
661
 
            error=NoUpsHere;
662
 
            return error;
663
 
        case ERR:
664
 
          error = upsTranslateError(value);
665
 
          return error;
666
 
        default:
667
 
          error=UnknownAnswer;
668
 
          return error;
669
 
        }
670
 
      }// end while
671
 
    }
672
 
  else {error=NoData; return error; }
673
 
  return 0;
674
 
  }
675
 
 
676
 
 
677
 
 
678
 
int KNutNet::setKey (const QString line) {
679
 
  line.stripWhiteSpace();
680
 
  if (line == "OK") return OK;
681
 
  if (line == "ERR") return ERR;
682
 
  if (line == "BEGIN") return BEGIN;
683
 
  if (line == "END") return END;
684
 
  if (line == "VAR") return VAR;
685
 
  if (line == "TYPE") return TYPE;
686
 
  if (line == "DESC") return DESC;
687
 
  if (line == "CMDDESC") return CMDDESC;
688
 
  if (line == "UPSDESC") return CMDDESC;
689
 
  if (line == "UPS") return UPS;
690
 
  if (line == "LIST") return LIST;
691
 
  if (line == "RW") return RW;
692
 
  if (line == "CMD") return CMD;
693
 
  if (line == "ENUM") return ENUM;
694
 
  if (line == "STARTTLS") return STARTTLS;
695
 
  return NONE;
696
 
  }
697
 
 
698
 
QString KNutNet::parseValue (const QString line) {
699
 
  line.stripWhiteSpace();
700
 
  if (line.isEmpty()) return "";
701
 
  if (line[0] != "\"") {
702
 
    if ((posChar=line.find(' ')) == -1) return line;
703
 
    else return line.left(posChar+1);
704
 
    }
705
 
  else {
706
 
    int len = line.length();
707
 
    QString outString = "";
708
 
    for (int i=1; i < len; i++) {
709
 
      if ( line[i] == "\\") {
710
 
        if ((i+1) <len) {
711
 
          if ((line[i+1] == "\"") || (line[i+1] == "\"")) {
712
 
            outString +=line[i+1];
713
 
            i++;
714
 
            }
715
 
          else i +=2;  
716
 
          }
717
 
        outString +=line[i];
718
 
        continue;
719
 
        }
720
 
      if (line [i] == "\"") {
721
 
        return outString;
722
 
        }
723
 
      outString += line[i];    
724
 
      }
725
 
    return outString;
726
 
    }
727
 
  return "";
728
 
  }
729
 
 
730
 
int KNutNet::parseTypeValue (QString line, int& lenString ) {
731
 
  int ret=0;
732
 
  QString word;
733
 
 
734
 
  lenString = 0;
735
 
  if (line.isEmpty()) return 0;
736
 
  QTextIStream inputStream(&line);
737
 
  while (!inputStream.atEnd()) {
738
 
    inputStream >> word;
739
 
    if (word == "RW") ret += RW_FLAG;
740
 
    if (word == "ENUM") ret += ENUM_FLAG;
741
 
    if (word.find("STRING:") != -1) {
742
 
      word = word.mid(7);
743
 
      ret += STRING_FLAG;
744
 
      lenString = word.toInt();
745
 
      }
746
 
    }
747
 
  return ret;
748
 
  }
749
 
 
750
 
        
751
 
int KNutNet::parseLine(QString& line, QString& upsName ,QString& varName, QString& value, int& typeValue, int& lenString) {
752
 
  int posChar;
753
 
  QString word1, word2, word3;
754
 
  int key1, key2, key3;
755
 
        
756
 
  line = line.stripWhiteSpace();
757
 
 
758
 
  upsName = "";
759
 
  varName = "";
760
 
  value = "";
761
 
  typeValue =0;
762
 
  
763
 
  if (line.isEmpty()) return NONE;
764
 
  
765
 
  if ((posChar=line.find(' ')) == -1) {
766
 
    if (setKey(line) == OK) return OK;
767
 
    else return NONE;
768
 
    }
769
 
  else {
770
 
    key1 = setKey( line.left(posChar));
771
 
    line = line.mid(posChar+1);
772
 
 
773
 
    switch (key1) {
774
 
 
775
 
      case NONE:
776
 
        return key1;
777
 
      case OK:
778
 
        return key1;
779
 
      case ERR:
780
 
        value = parseValue(line);
781
 
        return key1;
782
 
      default:
783
 
        if ((posChar=line.find(' ')) == -1) return NONE;
784
 
        word2 = line.left(posChar);
785
 
        key2 = setKey( word2);
786
 
        line = line.mid(posChar+1);
787
 
 
788
 
        switch (key1) {
789
 
          case BEGIN:
790
 
            if ((key2 == LIST) && (setKey(line) == UPS)) return BEGIN_LIST_UPS;
791
 
          break;
792
 
          case END:
793
 
            if ((key2 == LIST) && (setKey(line) == UPS)) return END_LIST_UPS;
794
 
          break;
795
 
          case UPS:
796
 
            upsName = word2;
797
 
            value = parseValue(line);
798
 
            return key1;
799
 
          case CMD:
800
 
            upsName = word2;
801
 
            varName = parseValue(line);
802
 
            return key1;
803
 
          }
804
 
 
805
 
        if ((posChar=line.find(' ')) == -1) return NONE;
806
 
 
807
 
        word3 = line.left(posChar);
808
 
        key3 = setKey( word3);
809
 
        line = line.mid(posChar+1);
810
 
    
811
 
        switch (key1) {
812
 
          case VAR:
813
 
            upsName = word2;
814
 
            varName = word3;
815
 
            value = parseValue(line);
816
 
            return VAR;
817
 
          case TYPE:
818
 
            upsName = word2;
819
 
            varName = word3;
820
 
            typeValue = parseTypeValue(line,lenString);
821
 
            return TYPE;
822
 
          case ENUM:
823
 
            upsName = word2;
824
 
            varName = word3;
825
 
            value = parseValue(line);
826
 
            return ENUM;
827
 
          case DESC:
828
 
            upsName = word2;
829
 
            varName = word3;
830
 
            value = parseValue(line);
831
 
            return DESC;
832
 
          case CMDDESC:
833
 
            upsName = word2;
834
 
            varName = word3;
835
 
            value = parseValue(line);
836
 
            return CMDDESC;
837
 
          case BEGIN:
838
 
            if (key2 != LIST) return NONE;
839
 
            if (key3 == VAR) {
840
 
              upsName=line.stripWhiteSpace();
841
 
              return BEGIN_LIST_VAR;
842
 
              }
843
 
            if (key3 == RW) {
844
 
              upsName=line.stripWhiteSpace();
845
 
              return BEGIN_LIST_RW;
846
 
              }
847
 
            if (key3 == CMD) {
848
 
              upsName=line.stripWhiteSpace();
849
 
              return BEGIN_LIST_CMD;
850
 
              }
851
 
            if (key3 == ENUM) {
852
 
              if ((posChar=line.find(' ')) == -1) return NONE;
853
 
              upsName = line.left(posChar);
854
 
              line=line.mid(posChar+1);
855
 
              varName = line.stripWhiteSpace();
856
 
              return BEGIN_LIST_ENUM;
857
 
              }
858
 
            return NONE;  
859
 
          case END:
860
 
            if (key2 != LIST) return NONE;
861
 
            if (key3 == VAR) {
862
 
              upsName=line.stripWhiteSpace();
863
 
              return END_LIST_VAR;
864
 
              }
865
 
            if (key3 == RW) {
866
 
              upsName=line.stripWhiteSpace();
867
 
              return END_LIST_RW;
868
 
              }
869
 
            if (key3 == CMD) {
870
 
              upsName=line.stripWhiteSpace();
871
 
              return END_LIST_CMD;
872
 
              }
873
 
            if (key3 == ENUM) {
874
 
              if ((posChar=line.find(' ')) == -1) return NONE;
875
 
              upsName = line.left(posChar);
876
 
              line=line.mid(posChar+1);
877
 
              varName = line.stripWhiteSpace();
878
 
              return END_LIST_ENUM;
879
 
              }
880
 
            return NONE;
881
 
          default:
882
 
            return NONE;
883
 
          }  
884
 
      }
885
 
    }  
886
 
  }    
887
 
 
888
 
 
889
 
int KNutNet::newDescription (const QCString  inBuffer, QString& upsDescription) {
890
 
  QString  outBuffer;
891
 
 
892
 
  if (!(error=getUpsData (inBuffer,outBuffer))) {
893
 
    outBuffer.stripWhiteSpace ();
894
 
    QString word1, word2, word3;
895
 
 
896
 
    if ((posChar = outBuffer.find(' ')) == -1)
897
 
      word1 = outBuffer;
898
 
    else {
899
 
      word1 = outBuffer.left(posChar);
900
 
      word2 = outBuffer.mid(posChar+1);
901
 
      }
902
 
    if (word1 == "DESC") {
903
 
      if ((posChar = word2.find('\"')) == -1) word3="";
904
 
      else
905
 
        {
906
 
        word3 = word2.mid(posChar+1);
907
 
        if ((posChar = word3.find('\"')) != -1) word3=word3.left(posChar);
908
 
        }
909
 
      if (!word3.isEmpty()) upsDescription=word3;
910
 
      }
911
 
    else {
912
 
      if (word1 == "ERR" ) return upsTranslateError(word2);
913
 
      else return UnknownAnswer;
914
 
      }
915
 
    }
916
 
  else return error;
917
 
  return 0;
918
 
  }
919
 
 
920
 
 
921
 
  
922
 
 
923
 
int KNutNet::getUpsData (const QCString sBuffer, QString& rBuffer, const QCString endString) {
924
 
 
925
 
  //rbuffer_len a sbuffer_len udavaji maximalni velikost bufferu
926
 
  int selectReturn; // navratova hodnota selectu
927
 
  struct timeval upsTimeout; // struktura pro cas cekani na ukazatel
928
 
  QCString  recvBuffer(rBufferLen);
929
 
  fd_set fdUps;
930
 
 
931
 
 // clean buffer
932
 
 
933
 
// nastavime vsechny ukazatele na nulu
934
 
  FD_ZERO (&fdUps);
935
 
// nastavime reakci na nas socket
936
 
  FD_SET (socAddr,&fdUps);
937
 
  upsTimeout.tv_sec=0;
938
 
  upsTimeout.tv_usec=0;
939
 
  int sr;
940
 
  int n = 0;
941
 
  while ((sr = select (socAddr+1,(fd_set *)&fdUps,(fd_set *)0L,(fd_set *)0L,&upsTimeout)) >0)  { 
942
 
    recv (socAddr,recvBuffer.data(),rBufferLen, 0);
943
 
  if (recvBuffer.length() == 0) break;
944
 
  if (n > 20) break; else n++;
945
 
   // obslouzime dve varianty: -1 data neprisla a 0 delka dat je nule
946
 
   // Nikdy necteme vice nez 20 polozek
947
 
  }
948
 
 
949
 
  if (send(socAddr,sBuffer.data(),sBuffer.length(),0) < 0 ) return SendErr;
950
 
 
951
 
  rBuffer=""; // vycistime buffer;
952
 
  
953
 
  int numberOfEINTR = 0;
954
 
  do {
955
 
    // nastavime vsechny ukazatele na nulu
956
 
    FD_ZERO (&fdUps);
957
 
    // nastavime reakci na nas socket
958
 
    FD_SET (socAddr,&fdUps);
959
 
    // nastavime timeout
960
 
    upsTimeout.tv_sec=upsGetTimeout1;
961
 
    upsTimeout.tv_usec=0;
962
 
    if ((selectReturn = select (socAddr+1,(fd_set *)&fdUps,(fd_set *)0L,(fd_set *)0L,&upsTimeout)) <0 )
963
 
      {
964
 
      if ((errno == EINTR) && (numberOfEINTR < 10)) {
965
 
        sleep (1);
966
 
        numberOfEINTR++;
967
 
        }
968
 
      else return SelectErr;
969
 
      }
970
 
    }
971
 
  while (selectReturn < 0);
972
 
  
973
 
  if (( ! protocol ) && ( ! selectReturn )) {
974
 
     // zajima nas selectReturn = 0 zaporne hodnoty jsme uz odfiltrovali
975
 
    // posleme jeste jeden paket
976
 
    if (send (socAddr,sBuffer.data(),sBuffer.length(), 0)<0) return SendErr;
977
 
    int numberOfEINTR = 0;
978
 
    do {
979
 
      // nastavime vsechny ukazatele na nulu
980
 
      FD_ZERO (&fdUps);
981
 
      // nastavime reakci na nas socket
982
 
      FD_SET (socAddr,&fdUps);
983
 
      // nastavime timeout
984
 
      upsTimeout.tv_sec=upsGetTimeout2;
985
 
      upsTimeout.tv_usec=0;
986
 
      if ((selectReturn = select (socAddr+1,(fd_set *)&fdUps,(fd_set *)0L,(fd_set *)0L,&upsTimeout)) <0 )
987
 
        {
988
 
        if ((errno == EINTR) && (numberOfEINTR < 10)) {
989
 
          sleep (1);
990
 
          numberOfEINTR++;
991
 
          }
992
 
        else return SelectErr;
993
 
        }
994
 
      }
995
 
    while (selectReturn < 0);
996
 
    } // end if ((  ! protocol........
997
 
  if ( !selectReturn) return NoServerData;  // opet neprisli zadna data
998
 
  else {
999
 
    bool endRecv = false;
1000
 
    do {
1001
 
      recvBuffer.fill('\0',rBufferLen); // vyplnime string mezarami
1002
 
      if (recv (socAddr,recvBuffer.data(),rBufferLen, 0) <0 ) return RecvErr;
1003
 
      rBuffer.append(recvBuffer.data()); // pripojime
1004
 
      if (endString.isEmpty()) endRecv = false;
1005
 
      else {
1006
 
        if (recvBuffer.findRev(endString) == -1)  {
1007
 
          // nastavime vsechny ukazatele na nulu
1008
 
          FD_ZERO (&fdUps);
1009
 
          // nastavime reakci na nas socket
1010
 
          FD_SET (socAddr,&fdUps);
1011
 
          // nastavime timeout
1012
 
          upsTimeout.tv_sec=upsGetTimeout3;
1013
 
          upsTimeout.tv_usec=0;
1014
 
          selectReturn = select (socAddr+1,(fd_set *)&fdUps,(fd_set *)0L,(fd_set *)0L,&upsTimeout);
1015
 
          if (selectReturn < 0) return SelectErr;
1016
 
          else {
1017
 
            if (selectReturn==0) endRecv=false; //select vraci 0 kdyz nic neprislo
1018
 
            else endRecv=true;
1019
 
            }
1020
 
          }
1021
 
        else endRecv = false; // nalezen ocekavany konec
1022
 
        }
1023
 
      } while (endRecv);
1024
 
    }
1025
 
  // clean buffer
1026
 
  // 
1027
 
// nastavime vsechny ukazatele na nulu
1028
 
//  FD_ZERO (&fdUps);
1029
 
// nastavime reakci na nas socket
1030
 
//  FD_SET (socAddr,&fdUps);
1031
 
//  upsTimeout.tv_sec=0;
1032
 
//  upsTimeout.tv_usec=0;
1033
 
//  while (select (socAddr+1,(fd_set *)&fdUps,(fd_set *)0L,(fd_set *)0L,&upsTimeout) >0)  { 
1034
 
//    recv (socAddr,recvBuffer.data(),rBufferLen, 0);
1035
 
//    }
1036
 
  return 0;   // vse OK
1037
 
  }
1038
 
 
1039
 
  // definice setRWVVars
1040
 
  #if defined  (KDE_VERSION_MAJOR)
1041
 
    #if KDE_VERSION_MAJOR >= 3
1042
 
    void KNutNet::setRWVars (const QString varName, const bool valueType, const int varMax, QValueVector<QString>* enumValues) {
1043
 
    #else
1044
 
    void KNutNet::setRWVars (const QString varName, const bool valueType, const int varMax, QValueList<QString>* enumValues) {
1045
 
    #endif
1046
 
  #else
1047
 
    void KNutNet::setRWVars (const QString varName, const bool valueType, const int varMax, QValueList<QString>* enumValues) {
1048
 
  #endif
1049
 
  #if defined  (KDE_VERSION_MAJOR)
1050
 
    #if KDE_VERSION_MAJOR >= 3
1051
 
      QValueVector<upsVar>::iterator it;
1052
 
    #else
1053
 
      QValueList<upsVar>::Iterator it;
1054
 
    #endif
1055
 
  #else
1056
 
    QValueList<upsVar>::Iterator it;
1057
 
  #endif
1058
 
   for (it = listVars.begin(); it != listVars.end(); it++) {
1059
 
     if ((*it).upsVarName == varName) {
1060
 
       (*it).upsVarMax = varMax;
1061
 
       (*it).upsValueType=valueType;
1062
 
       (*it).upsEnumValues=enumValues;
1063
 
       (*it).upsVarType=false;
1064
 
       break;
1065
 
       }
1066
 
    }
1067
 
  }
1068
 
 
1069
 
void KNutNet::upsSetType (struct upsVar& uVar, const QString name, const QString value) {
1070
 
  if (((nutVariables = 2) && (name == "STATUS")) || ((nutVariables = 2) && (name == "ups.status")))
1071
 
    uVar.upsVarActivate=true;
1072
 
  else uVar.upsVarActivate=false;
1073
 
  uVar.upsVarName = name;
1074
 
  uVar.upsVarType=true;
1075
 
  uVar.upsValueType=true;
1076
 
  uVar.upsVarMax=0;
1077
 
  uVar.upsValue=value;
1078
 
  uVar.upsDescription="";
1079
 
  uVar.upsEnumValues=0L;
1080
 
}
1081
 
 
1082
 
 
1083
 
void KNutNet::deleteVars (void) {
1084
 
  #if defined  (KDE_VERSION_MAJOR)
1085
 
    #if KDE_VERSION_MAJOR >= 3
1086
 
      QValueVector<upsVar>::iterator it;
1087
 
    #else
1088
 
      QValueList<upsVar>::Iterator it;
1089
 
    #endif
1090
 
  #else
1091
 
    QValueList<upsVar>::Iterator it;
1092
 
  #endif
1093
 
  for (it = listVars.begin(); it != listVars.end(); it++) {
1094
 
    if ((*it).upsEnumValues != 0) {
1095
 
      (*it).upsEnumValues->clear();
1096
 
      delete (*it).upsEnumValues;
1097
 
      }
1098
 
    }
1099
 
  listVars.clear();
1100
 
  listIComms.clear();     
1101
 
  }
1102
 
 
1103
 
void KNutNet::genStatusFlags (QString value) {
1104
 
  upsStatusVar = 0;
1105
 
  QTextIStream inputStream(&value);
1106
 
  QString word;
1107
 
  while (!inputStream.atEnd()) {
1108
 
    inputStream >> word;
1109
 
    if (word == "OFF") upsStatusVar +=OFF;
1110
 
    if (word == "OL") upsStatusVar +=OL;
1111
 
    if (word == "OB") upsStatusVar +=OB;
1112
 
    if (word == "LB") upsStatusVar +=LB;
1113
 
    if (word == "CAL") upsStatusVar +=CAL;
1114
 
    if (word == "TRIM") upsStatusVar +=TRIM;
1115
 
    if (word == "BOOST") upsStatusVar +=BOOST;
1116
 
    if (word == "OVER") upsStatusVar +=OVER;
1117
 
    if (word == "RB") upsStatusVar +=RB;
1118
 
    //OFF     -  1 ups je vypnuta
1119
 
    //OL      -  2 ups je bezi na sit
1120
 
    //OB      -  4 ups bezi na baterie
1121
 
    //LB      -  8 baterie je vybyta (pokud je zaroven OB dojde k shutdownu)
1122
 
    //CAL     - 16 je spustena kalibrace
1123
 
    //OVER    -128 ups je pretizena
1124
 
    //RB      -256 ups pozaduje vymenu baterie
1125
 
    }
1126
 
  }  
1127
 
 
1128
 
 
1129
 
int KNutNet::getDescription1 (bool always) {
1130
 
 
1131
 
  if (always || (! description)) {
1132
 
// tuto cast pouzijem az uz nebudeme podporovat starsi verze KDE
1133
 
//    if (netMutex.locked()) {
1134
 
//      // proces bezi pockame na dokonceni
1135
 
//      // nema vyznam ho znovu cist
1136
 
//      netMutex.lock(); // cekame na volny pristup
1137
 
//      netMutex.unlock();
1138
 
//      return 0;
1139
 
//      }
1140
 
//    netMutex.lock();
1141
 
//
1142
 
    if (netMutex) while (netMutex) {};
1143
 
    netMutex = true;
1144
 
    QCString  inBuffer;
1145
 
    QString  outBuffer;
1146
 
 
1147
 
    #if defined  (KDE_VERSION_MAJOR)
1148
 
      #if KDE_VERSION_MAJOR >= 3
1149
 
         QValueVector<upsVar>::iterator itv;
1150
 
         QValueVector<upsIComm>::iterator itc;
1151
 
      #else
1152
 
         QValueList<upsVar>::Iterator itv;
1153
 
         QValueList<upsIComm>::Iterator itc;
1154
 
      #endif
1155
 
    #else
1156
 
       QValueList<upsVar>::Iterator itv;
1157
 
       QValueList<upsIComm>::Iterator itc;
1158
 
    #endif
1159
 
     for (itv = listVars.begin(); itv != listVars.end(); itv++) {
1160
 
      //nacteme promenou
1161
 
      inBuffer = "VARDESC " + (*itv).upsVarName + "\n";
1162
 
      if ((error = newDescription (inBuffer, (*itv).upsDescription))) {
1163
 
        //netMutex.unlock();
1164
 
        netMutex=false;
1165
 
        return error;
1166
 
        }
1167
 
      }
1168
 
    for (itc = listIComms.begin(); itc != listIComms.end(); itc++) {
1169
 
      //nacteme promenou
1170
 
      inBuffer = "INSTCMDDESC "+(*itc).upsCommName+"\n";
1171
 
      if ((error = newDescription (inBuffer, (*itc).upsDescription))) {
1172
 
        //netMutex.unlock();
1173
 
        netMutex=false;
1174
 
        return error;
1175
 
        }
1176
 
      }
1177
 
    netMutex=false;
1178
 
    description = true;
1179
 
    }
1180
 
  return 0;
1181
 
  }
1182
 
 
1183
 
 
1184
 
 
1185
 
int KNutNet::getDescription2 (bool always) {
1186
 
 
1187
 
  if (always || (! description)) {
1188
 
// tuto cast pouzijem az uz nebudeme podporovat starsi verze KDE
1189
 
//    if (netMutex.locked()) {
1190
 
//      // proces bezi pockame na dokonceni
1191
 
//      // nema vyznam ho znovu cist
1192
 
//      netMutex.lock(); // cekame na volny pristup
1193
 
//      netMutex.unlock();
1194
 
//      return 0;
1195
 
//      }
1196
 
//    netMutex.lock();
1197
 
//
1198
 
    if (netMutex) while (netMutex) {};
1199
 
    netMutex = true;
1200
 
    QCString  inBuffer;
1201
 
    QString  outBuffer;
1202
 
    QString upsNameRet;
1203
 
    QString varName;
1204
 
    QString value;
1205
 
    int typeValue;
1206
 
    int lenString;
1207
 
 
1208
 
    #if defined  (KDE_VERSION_MAJOR)
1209
 
      #if KDE_VERSION_MAJOR >= 3
1210
 
         QValueVector<upsVar>::iterator itv;
1211
 
         QValueVector<upsIComm>::iterator itc;
1212
 
      #else
1213
 
         QValueList<upsVar>::Iterator itv;
1214
 
         QValueList<upsIComm>::Iterator itc;
1215
 
      #endif
1216
 
    #else
1217
 
       QValueList<upsVar>::Iterator itv;
1218
 
       QValueList<upsIComm>::Iterator itc;
1219
 
    #endif
1220
 
     for (itv = listVars.begin(); itv != listVars.end(); itv++) {
1221
 
      //nacteme promenou
1222
 
      inBuffer = "GET DESC " + upsName + " " + (*itv).upsVarName+"\n";
1223
 
 
1224
 
      if (!(error=getUpsData (inBuffer,outBuffer))) {
1225
 
        outBuffer.stripWhiteSpace ();
1226
 
        if ( !outBuffer.isEmpty() ) {
1227
 
          int key = parseLine(outBuffer, upsNameRet, varName, value, typeValue, lenString);
1228
 
          switch (key) {
1229
 
            case DESC:
1230
 
              (*itv).upsDescription = value;
1231
 
               break;
1232
 
            case ERR:
1233
 
              error=upsTranslateError(value);
1234
 
              //netMutex.unlock();
1235
 
              netMutex=false;
1236
 
              return error;
1237
 
            default:
1238
 
              error=UnknownAnswer;
1239
 
              //netMutex.unlock();
1240
 
              netMutex=false;
1241
 
              return error;
1242
 
            }
1243
 
          }
1244
 
        else {error = NoData; netMutex=false; return error;}
1245
 
        }
1246
 
      else {netMutex=false; return error;}
1247
 
      }
1248
 
    for (itc = listIComms.begin(); itc != listIComms.end(); itc++) {
1249
 
      //nacteme promenou
1250
 
      inBuffer = "GET CMDDESC " + upsName + " " + (*itc).upsCommName+"\n";
1251
 
      if (!(error=getUpsData (inBuffer,outBuffer))) {
1252
 
        outBuffer.stripWhiteSpace ();
1253
 
        if ( !outBuffer.isEmpty() ) {
1254
 
          int key = parseLine(outBuffer, upsNameRet, varName, value, typeValue, lenString);
1255
 
          switch (key) {
1256
 
            case CMDDESC:
1257
 
              (*itc).upsDescription = value;
1258
 
               break;
1259
 
            case ERR:
1260
 
              error=upsTranslateError(value);
1261
 
              //netMutex.unlock();
1262
 
              netMutex=false;
1263
 
              return error;
1264
 
            default:
1265
 
              error=UnknownAnswer;
1266
 
              //netMutex.unlock();
1267
 
              netMutex=false;
1268
 
              return error;
1269
 
            }
1270
 
          }
1271
 
        else {error = NoData; netMutex=false; return error;}
1272
 
        }
1273
 
      else {netMutex=false; return error;}
1274
 
      }
1275
 
    netMutex=false;
1276
 
    description = true;
1277
 
    }
1278
 
  return 0;
1279
 
  }
1280
 
 
1281
 
 
1282
 
 
1283
 
int KNutNet::getUpsVars1 ( void) {
1284
 
// getUpsVars nacte promene a jejich typ
1285
 
// vraci kod chyby nebo 0 pokud je vse v poradku
1286
 
  QCString  inBuffer;
1287
 
  QString  outBuffer;
1288
 
  QString  varOutBuffer;
1289
 
  struct upsVar uVars;
1290
 
  upsIComm uIComm;
1291
 
 
1292
 
  // init vars and lists
1293
 
  if (!upsName.isEmpty()) { // pokud upsName neni je vzdy nastaveno na null
1294
 
    inBuffer="LISTVARS ";
1295
 
    inBuffer.append(upsName);
1296
 
    inBuffer.append("\n");
1297
 
    }
1298
 
  else inBuffer ="LISTVARS\n";
1299
 
 
1300
 
  if (!(error=getUpsData (inBuffer,outBuffer))) {
1301
 
    outBuffer.stripWhiteSpace ();
1302
 
    if (outBuffer.length() > 0) {
1303
 
    // precteme data
1304
 
      QTextIStream inputStream(&outBuffer);
1305
 
      QString word;
1306
 
      int count =1 ;
1307
 
      while (!inputStream.atEnd()) {
1308
 
        inputStream >> word;
1309
 
        switch (count) {
1310
 
          case 1:
1311
 
          // vyndame prvni polozku
1312
 
          if (word == "VARS") count++;
1313
 
          else {
1314
 
            if (word == "ERR") {
1315
 
              inputStream >> word;
1316
 
              error = upsTranslateError(word);
1317
 
              }
1318
 
            else error=UnknownAnswer;
1319
 
            return error;
1320
 
            }
1321
 
          break;
1322
 
        case 2:
1323
 
          count++;
1324
 
          if ((word.length() > 0) && word.startsWith("@")) break;
1325
 
          default:
1326
 
          // pridame polozku do tabulky
1327
 
          upsSetType(uVars, word);
1328
 
           #if defined  (KDE_VERSION_MAJOR)
1329
 
            #if KDE_VERSION_MAJOR >= 3
1330
 
              listVars.push_back(uVars);
1331
 
            #else
1332
 
              listVars.append(uVars);
1333
 
            #endif
1334
 
          #else
1335
 
            listVars.append(uVars);
1336
 
          #endif
1337
 
          numberVars++;
1338
 
          break;
1339
 
          }
1340
 
        }
1341
 
      }
1342
 
    else { error=NoData; return error; }
1343
 
    }
1344
 
  else return error;
1345
 
 
1346
 
  if ((error = getUpsValues1(true))) return error;
1347
 
  
1348
 
  // *********** RW VARIABLES
1349
 
  if (!upsName.isEmpty()) { // pokud upsName neni je vzdy nastaveno na null
1350
 
    inBuffer="LISTRW ";
1351
 
    inBuffer.append(upsName);
1352
 
    inBuffer.append("\n");
1353
 
    }
1354
 
  else inBuffer ="LISTRW\n";
1355
 
 
1356
 
  if (!(error=getUpsData (inBuffer,outBuffer))) {
1357
 
    outBuffer.stripWhiteSpace();
1358
 
    if (!outBuffer.isEmpty()) {
1359
 
    // precteme data
1360
 
      QTextIStream inputStream(&outBuffer);
1361
 
      QString word;
1362
 
      int count =1 ;
1363
 
      bool valueType;
1364
 
      int varMax;
1365
 
      while (!inputStream.atEnd()) {
1366
 
        inputStream >> word;
1367
 
        switch (count) {
1368
 
          case 1:
1369
 
          // vyndame prvni polozku
1370
 
           if (word == "RW") count++;
1371
 
           else {
1372
 
             if (word == "ERR") {
1373
 
               inputStream >> word;
1374
 
               error = upsTranslateError(word);
1375
 
               }
1376
 
             else error=UnknownAnswer;
1377
 
             return error;
1378
 
             }
1379
 
          break;
1380
 
          case 2:
1381
 
          count++;
1382
 
          if ((word.length() > 0) && word.startsWith("@")) break;
1383
 
          default:
1384
 
          // Zpracujeme polozku v tabulce
1385
 
          // zjistime informaci o promene
1386
 
          inBuffer="VARTYPE ";
1387
 
          inBuffer.append(word);
1388
 
          if (!upsName.isEmpty()) {
1389
 
            inBuffer.append("@"); // pokud upsName neni je vzdy nastaveno na null
1390
 
            inBuffer.append(upsName);
1391
 
            }
1392
 
          inBuffer.append("\n");
1393
 
          if (!(error=getUpsData (inBuffer,varOutBuffer))) {
1394
 
            varOutBuffer.stripWhiteSpace();
1395
 
            if ( !outBuffer.isEmpty() ) {
1396
 
              QTextIStream varInputStream(&varOutBuffer);
1397
 
              QString word1, word2, word3;
1398
 
              varInputStream >> word1;
1399
 
              varInputStream >> word2;
1400
 
              varInputStream >> word3;
1401
 
              if (word1 == "TYPE") {
1402
 
                if ((word2.isEmpty()) || (word3.isEmpty())) {
1403
 
                  error=UnknownFormat;
1404
 
                  return error;
1405
 
                  }
1406
 
                if (word2 == "ENUM") valueType=false;
1407
 
                else {
1408
 
                  if (word2 =="STRING") valueType=true;
1409
 
                  else {
1410
 
                    error=UnknownFormat;
1411
 
                    return error;
1412
 
                    }
1413
 
                  }
1414
 
                bool ok = true;
1415
 
                varMax=word3.toInt(&ok);
1416
 
                if (!ok) {error=UnknownFormat; return error;};
1417
 
                } // word1 = Type
1418
 
              else {
1419
 
                if (word1=="ERR") {
1420
 
                  error = upsTranslateError(word2);
1421
 
                  return error;
1422
 
                  }
1423
 
                else {
1424
 
                  error = UnknownAnswer;
1425
 
                  return error;
1426
 
                  }
1427
 
                }
1428
 
              }
1429
 
            else { error=NoData; return error; }
1430
 
            }
1431
 
          else return error;
1432
 
          // ********** ENUM / STRING ******************
1433
 
 
1434
 
         #if defined  (KDE_VERSION_MAJOR)
1435
 
           #if KDE_VERSION_MAJOR >= 3
1436
 
             QValueVector<QString>* enumString=0;
1437
 
           #else
1438
 
             QValueList<QString>* enumString=0;
1439
 
           #endif
1440
 
         #else
1441
 
            QValueList<QString>* enumString=0;
1442
 
         #endif
1443
 
 
1444
 
          if (!valueType) {
1445
 
          // nacteme enum hodnoty
1446
 
            inBuffer="ENUM ";
1447
 
            inBuffer.append(word);
1448
 
            // pokud existuje pridame jmeno UPS-ky
1449
 
            if (!upsName.isEmpty()) {
1450
 
              inBuffer.append("@");
1451
 
              inBuffer.append(upsName);
1452
 
              }
1453
 
            inBuffer.append("\n");
1454
 
            if (!(error=getUpsData (inBuffer,varOutBuffer,"END\n"))) {
1455
 
//               varOutBuffer.stripWhiteSpace();  nemuze provest protoze bychom si odstranili konce radek
1456
 
              if (!varOutBuffer.isEmpty()) {
1457
 
                QString inLine, word1, word2;
1458
 
                QTextIStream varInputStream(&varOutBuffer);
1459
 
                int inCountLine=1;
1460
 
                while (!(inLine = varInputStream.readLine()).isNull()) {
1461
 
                  inLine.stripWhiteSpace();
1462
 
 
1463
 
                  if ((posChar=inLine.find(' ')) == -1) word1=inLine;
1464
 
                  else {
1465
 
                    word1=inLine.left(posChar);
1466
 
                    inLine = inLine.mid(posChar+1);
1467
 
                    }
1468
 
                  if (word1.isEmpty()) { error = UnknownFormat; return error; }
1469
 
                  if (inCountLine == 1) {
1470
 
                  if ((posChar=inLine.find(' ')) == -1) word2=inLine;
1471
 
                  else word2=inLine.left(posChar);
1472
 
                    if (word1 =="ENUM") {
1473
 
                      if (word2 != word) { error = UnknownFormat; return error; }
1474
 
                      varMax=0;
1475
 
                      inCountLine++;
1476
 
                      #if defined  (KDE_VERSION_MAJOR)
1477
 
                        #if KDE_VERSION_MAJOR >= 3
1478
 
                           enumString = new QValueVector<QString>;
1479
 
                        #else
1480
 
                           enumString = new QValueList<QString>;
1481
 
                        #endif
1482
 
                      #else
1483
 
                        enumString = new QValueList<QString>;
1484
 
                      #endif
1485
 
                      enumString->clear();
1486
 
                      }
1487
 
                    else {
1488
 
                      if (word1=="ERR")
1489
 
                        error=upsTranslateError(word2); // prevede chybu na jeji kod
1490
 
                      else error=UnknownAnswer;
1491
 
                      return error;
1492
 
                      }
1493
 
                    }
1494
 
                  else {
1495
 
                    if (word1 == "END") break;
1496
 
                    if (word1 != "OPTION") { error = UnknownFormat; return error; }
1497
 
                    if ((posChar = inLine.find('\"')) == -1) word2 = "";
1498
 
                    else {
1499
 
                      word2 = inLine.mid(posChar+1);
1500
 
                      if ((posChar=word2.find('\"')) != -1) word2 = word2.left(posChar);
1501
 
                      }
1502
 
                      #if defined  (KDE_VERSION_MAJOR)
1503
 
                        #if KDE_VERSION_MAJOR >= 3
1504
 
                          enumString->push_back(word2);
1505
 
                        #else
1506
 
                          enumString->append(word2);
1507
 
                        #endif
1508
 
                      #else
1509
 
                        enumString->append(word2);
1510
 
                      #endif
1511
 
                    varMax++;
1512
 
                    }
1513
 
                  } // end while
1514
 
                } // buffer is not empty
1515
 
              else { error = NoData ; return error; }
1516
 
              } // getUpsData
1517
 
            else return error;
1518
 
            } // type of var
1519
 
          else  enumString = 0l;
1520
 
          // nacteme stringovou promenou
1521
 
          // ulozime promene
1522
 
          setRWVars (word,valueType,varMax,enumString);
1523
 
          numberRWVars++;
1524
 
          break;
1525
 
          }
1526
 
        } // end  while (!inputStream.atEnd())
1527
 
      }
1528
 
    else { error = NoData ; return error; }
1529
 
    }
1530
 
  else return error;
1531
 
 
1532
 
  // *************** INSTANT COMMAND
1533
 
  if (!upsName.isEmpty()) { // pokud upsName neni je vzdy nastaveno na null
1534
 
    inBuffer="LISTINSTCMD ";
1535
 
    inBuffer.append(upsName);
1536
 
    inBuffer.append("\n");
1537
 
    }
1538
 
  else inBuffer ="LISTINSTCMD\n";
1539
 
 
1540
 
  if (!(error=getUpsData (inBuffer,outBuffer))) {
1541
 
    outBuffer.stripWhiteSpace ();
1542
 
    if ( !outBuffer.isEmpty() ) {
1543
 
      // precteme data
1544
 
      QTextIStream inputStream(&outBuffer);
1545
 
      QString word;
1546
 
      int count =1 ;
1547
 
      while (!inputStream.atEnd()) {
1548
 
        inputStream >> word;
1549
 
        switch (count) {
1550
 
          case 1:
1551
 
          // vyndame prvni polozku
1552
 
           if (word == "INSTCMDS") count++;
1553
 
           else {
1554
 
             if (word == "ERR") {
1555
 
               inputStream >> word;
1556
 
               error = upsTranslateError(word);
1557
 
               }
1558
 
             else error=UnknownAnswer;
1559
 
             return error;
1560
 
             }
1561
 
          break;
1562
 
          case 2:
1563
 
          count++;
1564
 
          if ((word.length() > 0) && word.startsWith("@")) break;
1565
 
          default:
1566
 
          // Zpracujeme polozku v tabulky
1567
 
          uIComm.upsCommName=word;
1568
 
          uIComm.upsDescription="";
1569
 
          #if defined  (KDE_VERSION_MAJOR)
1570
 
            #if KDE_VERSION_MAJOR >= 3
1571
 
          listIComms.push_back(uIComm);
1572
 
            #else
1573
 
          listIComms.append(uIComm);
1574
 
            #endif
1575
 
          #else
1576
 
          listIComms.append(uIComm);
1577
 
          #endif
1578
 
          numberIComms++;
1579
 
          break;
1580
 
          }
1581
 
        }
1582
 
      }
1583
 
    else { error=NoData; return error; }
1584
 
    }
1585
 
  else return error;
1586
 
  return 0;
1587
 
  }
1588
 
 
1589
 
 
1590
 
 
1591
 
int KNutNet::getUpsVars2 ( void) {
1592
 
  QCString  inBuffer;
1593
 
  QString  outBuffer;
1594
 
  QString  varOutBuffer;
1595
 
  struct upsVar uVars;
1596
 
  upsIComm uIComm;
1597
 
  int key;
1598
 
  int typeValue;
1599
 
  int lenString;
1600
 
  int varMax=0;
1601
 
 
1602
 
  #if defined  (KDE_VERSION_MAJOR)
1603
 
    #if KDE_VERSION_MAJOR >= 3
1604
 
      QValueVector<QString>* enumString=0;
1605
 
    #else
1606
 
      QValueList<QString>* enumString=0;
1607
 
    #endif
1608
 
   #else
1609
 
     QValueList<QString>* enumString=0;
1610
 
   #endif
1611
 
  inBuffer = "LIST VAR " + upsName + "\n";
1612
 
  if (!(error=getUpsData (inBuffer,outBuffer,"END LIST VAR"))) {
1613
 
    QString inLine, upsNameRet, varName, varNameRet, value;
1614
 
    bool beginList = false;
1615
 
    QTextIStream varInputStream(&outBuffer);
1616
 
    while (!(inLine = varInputStream.readLine()).isNull()) {
1617
 
      key = parseLine (inLine, upsNameRet, varName, value, typeValue, lenString);
1618
 
      switch (key) {
1619
 
        case BEGIN_LIST_VAR:
1620
 
          if (upsNameRet == upsName) beginList = true;
1621
 
        break;
1622
 
        case VAR:
1623
 
         if (beginList ) {
1624
 
           upsSetType(uVars, varName, value);
1625
 
 
1626
 
           if (varName == "ups.status") genStatusFlags(value);
1627
 
           inBuffer = "GET TYPE " + upsName + " " + varName +"\n";
1628
 
           if (!(error=getUpsData (inBuffer,varOutBuffer))) {
1629
 
             key = parseLine (varOutBuffer, upsNameRet, varNameRet, value, typeValue, lenString);
1630
 
             switch (key) {
1631
 
               case TYPE:
1632
 
                if (typeValue & RW_FLAG) {
1633
 
                  uVars.upsVarMax = 0;
1634
 
                  uVars.upsEnumValues=0L;
1635
 
                  if (typeValue & ENUM_FLAG) {
1636
 
                    uVars.upsValueType=false;
1637
 
                    // nacteme enumValues
1638
 
                    inBuffer = "LIST ENUM " + upsName + " " + varName +"\n";
1639
 
                    if (!(error=getUpsData (inBuffer,varOutBuffer,"END LIST ENUM"))) {
1640
 
                      bool beginEnumList = false;
1641
 
                      QTextIStream varInputStream(&varOutBuffer);
1642
 
                      while (!(inLine = varInputStream.readLine()).isNull()) {
1643
 
                        key = parseLine (inLine, upsNameRet, varName, value, typeValue, lenString);
1644
 
                        switch (key) {
1645
 
                          case BEGIN_LIST_ENUM:
1646
 
                            beginEnumList = true;
1647
 
                            varMax=0;
1648
 
                            #if defined  (KDE_VERSION_MAJOR)
1649
 
                              #if KDE_VERSION_MAJOR >= 3
1650
 
                                 enumString = new QValueVector<QString>;
1651
 
                              #else
1652
 
                                enumString = new QValueList<QString>;
1653
 
                              #endif
1654
 
                            #else
1655
 
                              enumString = new QValueList<QString>;
1656
 
                            #endif
1657
 
                            enumString->clear();
1658
 
                            uVars.upsEnumValues=enumString;
1659
 
                          break;
1660
 
                          case ENUM:
1661
 
                            if (beginList ) {
1662
 
                              #if defined  (KDE_VERSION_MAJOR)
1663
 
                                #if KDE_VERSION_MAJOR >= 3
1664
 
                                  enumString->push_back(value);
1665
 
                                #else
1666
 
                                   enumString->append(value);
1667
 
                                #endif
1668
 
                              #else
1669
 
                                enumString->append(value);
1670
 
                              #endif
1671
 
                              varMax++;
1672
 
                              }
1673
 
                          break;
1674
 
                          case ERR:
1675
 
                            error = upsTranslateError(value);
1676
 
                          return error;
1677
 
                          default:
1678
 
                            error=UnknownAnswer;
1679
 
                          return error;
1680
 
                          case END_LIST_ENUM:
1681
 
                          uVars.upsVarMax=varMax;
1682
 
                          break;
1683
 
                          }
1684
 
                        }
1685
 
                      }
1686
 
                    }
1687
 
                  else {
1688
 
                    // Vse co nenini ENUM je STRING
1689
 
                    uVars.upsValueType=true;
1690
 
                    uVars.upsVarMax=lenString;
1691
 
                    }
1692
 
                  uVars.upsVarType=false;
1693
 
                  numberRWVars++;
1694
 
                  }
1695
 
                 break;
1696
 
               case ERR:
1697
 
                 error = upsTranslateError(value);
1698
 
                 return error;
1699
 
               default:
1700
 
                 error=UnknownAnswer;
1701
 
                 return error;
1702
 
               }
1703
 
             }
1704
 
           else { return error; }
1705
 
           #if defined  (KDE_VERSION_MAJOR)
1706
 
             #if KDE_VERSION_MAJOR >= 3
1707
 
               listVars.push_back(uVars);
1708
 
             #else
1709
 
               listVars.append(uVars);
1710
 
             #endif
1711
 
           #else
1712
 
             listVars.append(uVars);
1713
 
           #endif
1714
 
           numberVars++;
1715
 
           }
1716
 
          break;
1717
 
        case END_LIST_VAR:
1718
 
          break;
1719
 
        case ERR:
1720
 
          error = upsTranslateError(value);
1721
 
          return error;
1722
 
        default:
1723
 
          error=UnknownAnswer;
1724
 
          return error;
1725
 
        } // end switch
1726
 
      } // end while
1727
 
    }
1728
 
  else { return error; }
1729
 
  // INSTANT COMMANDS
1730
 
 
1731
 
  inBuffer = "LIST CMD " + upsName + "\n";
1732
 
  if (!(error=getUpsData (inBuffer,outBuffer,"END LIST CMD"))) {
1733
 
 
1734
 
    QString inLine, upsNameRet, varName, value;
1735
 
    int key, typeValue;
1736
 
    bool beginList = false;
1737
 
    QTextIStream varInputStream(&outBuffer);
1738
 
    while (!(inLine = varInputStream.readLine()).isNull()) {
1739
 
      key = parseLine (inLine, upsNameRet, varName, value, typeValue, lenString);
1740
 
      switch (key) {
1741
 
        case BEGIN_LIST_CMD: {
1742
 
          if (upsNameRet == upsName) beginList = true;
1743
 
          break;
1744
 
          }
1745
 
        case CMD:
1746
 
         if (beginList ) {
1747
 
           // Zpracujeme polozku v tabulky
1748
 
           uIComm.upsCommName=varName;
1749
 
           uIComm.upsDescription="";
1750
 
           #if defined  (KDE_VERSION_MAJOR)
1751
 
             #if KDE_VERSION_MAJOR >= 3
1752
 
               listIComms.push_back(uIComm);
1753
 
             #else
1754
 
               listIComms.append(uIComm);
1755
 
             #endif
1756
 
           #else
1757
 
             listIComms.append(uIComm);
1758
 
           #endif
1759
 
           numberIComms++;
1760
 
           }
1761
 
         break;
1762
 
        case END_LIST_CMD:
1763
 
          break;
1764
 
        case ERR:
1765
 
          error = upsTranslateError(value);
1766
 
          return error;
1767
 
        default:
1768
 
          error=UnknownAnswer;
1769
 
          return error;
1770
 
        } // end switch
1771
 
      } // end while
1772
 
    }
1773
 
  else { return error; }
1774
 
  return 0;
1775
 
  }
1776
 
 
1777
 
 
1778
 
 
1779
 
int KNutNet::getUpsValues1 (const bool allVars ) {// allVars = true vse; allVars = false jen activate
1780
 
  // Nacte hodnotu promenych
1781
 
// tuto cast pouzijem az uz nebudeme podporovat starsi verze KDE
1782
 
//    if (netMutex.locked()) {
1783
 
//      // proces bezi pockame na dokonceni
1784
 
//      // nema vyznam ho znovu cist
1785
 
//      netMutex.lock(); // cekame na volny pristup
1786
 
//      netMutex.unlock();
1787
 
//      return 0;
1788
 
//      }
1789
 
//    netMutex.lock();
1790
 
//
1791
 
    if (netMutex) while (netMutex) {};
1792
 
    netMutex = true;
1793
 
    QCString  inBuffer;
1794
 
    QString  outBuffer;
1795
 
    #if defined  (KDE_VERSION_MAJOR)
1796
 
      #if KDE_VERSION_MAJOR >= 3
1797
 
         QValueVector<upsVar>::iterator it;
1798
 
      #else
1799
 
        QValueList<upsVar>::Iterator it;
1800
 
      #endif
1801
 
    #else
1802
 
      QValueList<upsVar>::Iterator it;
1803
 
    #endif
1804
 
    emit getVarDesc (numberVars, 0);
1805
 
 
1806
 
    int numberVar  = 0;
1807
 
    for (it = listVars.begin(); it != listVars.end(); it++) {
1808
 
      numberVar++;
1809
 
      if ((allVars) || ((*it).upsVarActivate)) {
1810
 
        //nacteme promenou
1811
 
        inBuffer = "REQ ";
1812
 
        inBuffer.append((*it).upsVarName);
1813
 
        if (!upsName.isEmpty()) { // pokud upsName neni je vzdy nastaveno na null
1814
 
          inBuffer.append("@");
1815
 
          inBuffer.append(upsName);
1816
 
          }
1817
 
        inBuffer.append("\n");
1818
 
        if (!(error=getUpsData (inBuffer,outBuffer))) {
1819
 
          outBuffer.stripWhiteSpace ();
1820
 
 
1821
 
          emit getVarDesc (numberVars, numberVar);
1822
 
 
1823
 
          if ( !outBuffer.isEmpty() ) {
1824
 
            QTextIStream inputStream(&outBuffer);
1825
 
            QString word1, word2, word3 ;
1826
 
            inputStream >> word1;
1827
 
            inputStream >> word2;
1828
 
            if (inputStream.atEnd()) { // neexistuje treti cast VALUE
1829
 
              if (word1 == "ERR") {
1830
 
                // vracena chyba
1831
 
                error=upsTranslateError(word2);
1832
 
                //netMutex.unlock();
1833
 
                netMutex=false;
1834
 
                return error;
1835
 
                }
1836
 
              else {
1837
 
                error=UnknownFormat;
1838
 
                //netMutex.unlock();
1839
 
                netMutex=false;
1840
 
                return error;
1841
 
                }
1842
 
              }
1843
 
            inputStream >> word3;
1844
 
            if (word1 == "ANS") {
1845
 
              // odpoved
1846
 
              // odstranime pripadne jmeno upsky
1847
 
              if ((posChar = word2.find('@')) != -1) word2 = word2.left(posChar);
1848
 
              if ( word2 != (*it).upsVarName ) {
1849
 
                error=UnknownAnswer;
1850
 
                //netMutex.unlock();
1851
 
                netMutex=false;
1852
 
                return error;// neni to odpoved na spravnou promenou
1853
 
                }
1854
 
              // test pro starsi format modulu vraceli chybu v hodnote promene
1855
 
              if ((error=upsOldTranslateError(word3))) {
1856
 
                //netMutex.unlock();
1857
 
                netMutex=false;
1858
 
                return error;
1859
 
                }
1860
 
              // zalozime hodnotu do vectoru;
1861
 
              (*it).upsValue=word3;
1862
 
              if ((*it).upsVarName == "STATUS") {
1863
 
                // dogenerujeme dalsi slova pro status
1864
 
                word3 += inputStream.readLine();
1865
 
                genStatusFlags(word3);
1866
 
                }
1867
 
              }
1868
 
            else {
1869
 
              // neznama odpoved - neni ANS ani ERR
1870
 
              error=UnknownAnswer;
1871
 
              //netMutex.unlock();
1872
 
              netMutex=false;
1873
 
              return error;
1874
 
              }
1875
 
            }
1876
 
          else {netMutex=false; error=NoData; return error; }
1877
 
          }
1878
 
        else { netMutex=false; return error;}
1879
 
        }
1880
 
      }
1881
 
    //netMutex.unlock();
1882
 
    netMutex=false;
1883
 
    return 0;
1884
 
  }
1885
 
 
1886
 
 
1887
 
 
1888
 
int KNutNet::getUpsValues2 (const bool allVars ) {// allVars = true vse; allVars = false jen activate
1889
 
  QString varName;
1890
 
  QString upsNameRet;
1891
 
  QString value;
1892
 
  int typeValue;
1893
 
  int lenString;
1894
 
  
1895
 
  // Nacte hodnotu promenych
1896
 
// tuto cast pouzijem az uz nebudeme podporovat starsi verze KDE
1897
 
//    if (netMutex.locked()) {
1898
 
//      // proces bezi pockame na dokonceni
1899
 
//      // nema vyznam ho znovu cist
1900
 
//      netMutex.lock(); // cekame na volny pristup
1901
 
//      netMutex.unlock();
1902
 
//      return 0;
1903
 
//      }
1904
 
//    netMutex.lock();
1905
 
//
1906
 
  if (netMutex) while (netMutex) {};
1907
 
  netMutex = true;
1908
 
  QCString  inBuffer;
1909
 
  QString  outBuffer;
1910
 
  #if defined  (KDE_VERSION_MAJOR)
1911
 
    #if KDE_VERSION_MAJOR >= 3
1912
 
       QValueVector<upsVar>::iterator it;
1913
 
    #else
1914
 
      QValueList<upsVar>::Iterator it;
1915
 
    #endif
1916
 
  #else
1917
 
    QValueList<upsVar>::Iterator it;
1918
 
  #endif
1919
 
  emit getVarDesc (numberVars, 0);
1920
 
   int numberVar  = 0;
1921
 
  for (it = listVars.begin(); it != listVars.end(); it++) {
1922
 
    numberVar++;
1923
 
    if ((allVars) || ((*it).upsVarActivate)) {
1924
 
      //nacteme promenou
1925
 
      inBuffer = "GET VAR "+upsName+" "+(*it).upsVarName+"\n";
1926
 
      if (!(error=getUpsData (inBuffer,outBuffer))) {
1927
 
        outBuffer.stripWhiteSpace ();
1928
 
        emit getVarDesc (numberVars, numberVar);
1929
 
        if ( !outBuffer.isEmpty() ) {
1930
 
          int key = parseLine(outBuffer, upsNameRet, varName, value, typeValue, lenString);
1931
 
          switch (key) {
1932
 
            case VAR:
1933
 
              if ( varName != (*it).upsVarName ) {
1934
 
                error=UnknownAnswer;
1935
 
                //netMutex.unlock();
1936
 
                netMutex=false;
1937
 
                return error;// neni to odpoved na spravnou promenou
1938
 
                }
1939
 
              (*it).upsValue=value;
1940
 
              if ((((*it).upsVarName == "STATUS") && (nutVariables == 1)) || (((*it).upsVarName == "ups.status") && (nutVariables == 2))) {
1941
 
                // dogenerujeme dalsi slova pro status
1942
 
                genStatusFlags(value);
1943
 
                }
1944
 
              break;  
1945
 
            case ERR:
1946
 
              error=upsTranslateError(value);
1947
 
              //netMutex.unlock();
1948
 
              netMutex=false;
1949
 
              return error;
1950
 
            default:
1951
 
              error=UnknownAnswer;
1952
 
              //netMutex.unlock();
1953
 
              netMutex=false;
1954
 
              return error;
1955
 
            }
1956
 
          }
1957
 
        else {netMutex=false; error=NoData; return error; }
1958
 
        }
1959
 
      else { netMutex=false; return error;}
1960
 
      }
1961
 
    }
1962
 
  //netMutex.unlock();
1963
 
  netMutex=false;
1964
 
  return 0;
1965
 
  }
1966
 
 
1967
 
 
1968
 
int KNutNet::sendComm (const QString command, const QString arg1, const QString arg2, const bool useUpsName) {
1969
 
  int localError;
1970
 
// nenastavi promenou error ale nenuluje ji
1971
 
  QCString inBuffer;
1972
 
  QString outBuffer;
1973
 
 
1974
 
  switch (nutProtocol) {
1975
 
    case 1:
1976
 
      inBuffer=command + " " + arg1;
1977
 
      if (useUpsName && (!upsName.isEmpty())) // pokud upsName neni je vzdy prazdne
1978
 
        inBuffer += "@"+upsName;
1979
 
   if (!arg2.isEmpty()) inBuffer += " " + arg2;
1980
 
    break;
1981
 
    case 2:
1982
 
    inBuffer = command + " ";
1983
 
    if (useUpsName) inBuffer += upsName + " ";
1984
 
    inBuffer.append(arg1);
1985
 
   if (!arg2.isEmpty()) inBuffer += " \"" + arg2 +"\"";
1986
 
   break;
1987
 
    }
1988
 
  inBuffer.append("\n");
1989
 
  
1990
 
  if (!(localError=getUpsData (inBuffer,outBuffer))) {
1991
 
    outBuffer.stripWhiteSpace ();
1992
 
    QTextIStream inputStream(&outBuffer);
1993
 
    QString word1, word2;
1994
 
    inputStream >> word1;
1995
 
    inputStream >> word2;
1996
 
    if (word1.isEmpty()) return UnknownAnswer; // zadna odpoved
1997
 
    if (word1 != "OK") {
1998
 
      // neni rovno OK
1999
 
      if (word1 == "ERR") return upsTranslateError (word2);
2000
 
      else return UnknownAnswer;
2001
 
      }
2002
 
    else return 0;
2003
 
    }
2004
 
  else return localError;
2005
 
  }
2006
 
  
2007
 
 
2008
 
int KNutNet::activate ( const QString name, const bool setActivate  ) {
2009
 
  error =0;
2010
 
  if (state == Connected) {
2011
 
  #if defined  (KDE_VERSION_MAJOR)
2012
 
    #if KDE_VERSION_MAJOR >= 3
2013
 
      QValueVector<upsVar>::iterator it;
2014
 
    #else
2015
 
      QValueList<upsVar>::Iterator it;
2016
 
    #endif
2017
 
  #else
2018
 
    QValueList<upsVar>::Iterator it;
2019
 
  #endif
2020
 
    for (it = listVars.begin(); it != listVars.end(); it++) {
2021
 
      if ((*it).upsVarName == name) {
2022
 
        (*it).upsVarActivate = setActivate;
2023
 
        return 0;
2024
 
        }
2025
 
      }
2026
 
    error = VarNotFind;
2027
 
    return error;
2028
 
    }
2029
 
  else return NotConnection;
2030
 
  }
2031
 
 
2032
 
int KNutNet::activateAll ( const bool setActivate  ) {
2033
 
  error =0;
2034
 
  if (state == Connected) {
2035
 
  #if defined  (KDE_VERSION_MAJOR)
2036
 
    #if KDE_VERSION_MAJOR >= 3
2037
 
      QValueVector<upsVar>::iterator it;
2038
 
    #else
2039
 
      QValueList<upsVar>::Iterator it;
2040
 
    #endif
2041
 
  #else
2042
 
    QValueList<upsVar>::Iterator it;
2043
 
  #endif
2044
 
    for (it = listVars.begin(); it != listVars.end(); it++) {
2045
 
      (*it).upsVarActivate = setActivate;
2046
 
      }
2047
 
    return 0;
2048
 
    }
2049
 
  else return NotConnection;
2050
 
  }
2051
 
 
2052
 
  
2053
 
int KNutNet::upsTranslateError (const QString string) {
2054
 
  if (string == "VAR-NOT-SUPPORTED") return VarNotSupported;
2055
 
  if (string == "VAR-UNKNOWN") return VarUnknown;
2056
 
 
2057
 
  if (string == "ACCESS-DENIED") return AccessDenied;
2058
 
  if (string == "PASSWORD-REQUIRED") return PasswordRequired;
2059
 
  if (string == "PASSWORD-INCORRECT") return PasswordIncorrect;
2060
 
  if (string == "UNKNOWN-UPS") return UnknownUps;
2061
 
  if (string == "ALREADY-LOGGED_IN") return AlreadyLoggedIn;
2062
 
  if (string == "ALREADY-SET-PASSWORD") return AlreadySetPassword;
2063
 
  if (string == "UNKNOWN-COMMAND") return UnknownCommand;
2064
 
 
2065
 
  if (string == "UNKNOWN-INSTCMD") return UnknownInstCmd;
2066
 
  if (string == "CMD-NOT-SUPPORTED") return CmdNotSupported;
2067
 
 
2068
 
  if (string == "INVALID-VALUE") return InvalidValue;
2069
 
 
2070
 
  if (string == "NO-RESPONSE") return NoResponse;
2071
 
  if (string == "UNKNOWN-REPLY") return UnknownReply;
2072
 
  if (string == "NOT-IMPLEMENTED") return NotImplemented;
2073
 
  if (string == "COMMAND-FAILED") return CommandFailed;
2074
 
 
2075
 
  if (string == "MISSING-ARGUMENT") return MissingArgument;
2076
 
  if (string == "DATA-STALE") return DataStale;
2077
 
  if (string == "UNKNOWN-TYPE") return UnknownType;
2078
 
 
2079
 
  if (string == "DRIVER-NOT-CONNECTED") return DriverNotConnected;
2080
 
 
2081
 
  if (string == "") return UnknownFormat;
2082
 
  
2083
 
  return UnknownErr;
2084
 
  }
2085
 
 
2086
 
int KNutNet::upsOldTranslateError (const QString string) {
2087
 
  if (string == "NOT-SUPPORTED") return VarNotSupported;
2088
 
  if (string == "UNKNOWN") return VarUnknown;
2089
 
  if (string == "DATA-STALE") return DataStale;
2090
 
  return 0; // nebylo nalezeno
2091
 
  }
2092
 
 
2093
 
 
2094
 
#include "knutnet.moc"