aboutsummaryrefslogtreecommitdiff
path: root/src/optmanager.cpp
blob: 19b814e7516da41bff13b71d603403f6d1747a26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
    Copyright (C) 2014 BARRATERO Laurent                                   

    AeroUp is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by 
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    Aeroup is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
*/

/*
 *       Filename:  optmanager.cpp
 *
 *    Description:  Manages command line options & sub options
 *                  To do : make space possible on command line 
 *                  for sub option would be cool more cool
 *
 *          ToDo :  Verify that the value for setSerial is not out of range
 *                  for the time being, the value is set to max.
 *                  we could test string before conversion.
 *
 *        Version:  0.3.2
 *        Created:  23/12/2013 23:31:46
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  BARATTERO Laurent, laurentba<at>larueluberlu.net
 *   Organization:  La rue Luberlu
 */

#include "optmanager.hpp"

extern bool VERBOSE_AERO;

using namespace std;


/*
 *       Class:  OptManager
 *      Method:  OptManager :: start
 * Description:  Order Processing
 */
  void 
OptManager::start()
{
  // Start command
  for (vector<string>::iterator iter = startList.begin();
                      iter != startList.end(); ++iter)
  {
    sendStart(*iter);
    if (VERBOSE_AERO)
      cout << "Start command sent on : " << *iter << endl;
  }

  // Color Command
  for (vector<optCol>::iterator iter = colorList.begin();
                      iter != colorList.end(); ++iter)
  {
    if ( (*iter).port != "\0")
    {
      sendColor( (*iter).rgb[RED],
                 (*iter).rgb[GREEN],
                 (*iter).rgb[BLUE],
                 (*iter).port);
    }
    else
    {
      cout << "missing port name argument in color command" << endl;
      exit (EXIT_FAILURE);
    }

    if(VERBOSE_AERO)
    {
      cout << "Color command (c" << dec 
           << "," << static_cast<int>((*iter).rgb[RED])   
           << "," << static_cast<int>((*iter).rgb[GREEN])
           << "," << static_cast<int>((*iter).rgb[BLUE])
           << ") sent on port \"" << (*iter).port << "\"" 
           << endl;

    }
  }

  // Test command
  for ( vector<string>::iterator iter = testList.begin();
                      iter != testList.end(); ++iter)
  {
    sendTest(*iter);
    if (VERBOSE_AERO)
      cout << "Test command sent on : " << *iter << endl;
  }

  // Upload Glo file
  for (vector<optUp>::iterator iter = upGloList.begin();
                      iter != upGloList.end(); ++iter)
  {
    sendUpGlo((*iter).fileName, (*iter).port);
  }

  // Get serial number
  for (vector<string>::iterator iter = getSerialList.begin();
                      iter != getSerialList.end(); ++iter)
  {
    sendGetSerial(*iter);
  }

  // Symlink
  for (vector<string>::iterator iter = symlinkList.begin();
                      iter != symlinkList.end(); ++iter)
  {
    sendSymlink(*iter);
  }

  // Set serial number
  for (vector<optSerial>::iterator iter = setSerialList.begin();
                      iter != setSerialList.end(); ++iter)
  {
    sendSetSerial((*iter).i_serialNumber,(*iter).port);
  }

  // Verify Glo file
  for ( vector<string>::iterator iter = verifyList.begin();
                      iter != verifyList.end(); ++iter)
  {
    sendVerify(*iter);
    if (VERBOSE_AERO)
      cout << "Process verification on file " << *iter << endl;
  }

}	/* -----  end of method OptManager::start  ----- */


/*
 *                           .::SEND COMMAND::.
 * all the functionalities functionality proposed by the command line interface  
 */

/*
 *      Method:  OptManager :: sendTest
 */
  void
OptManager::sendTest( std::string  port_name )
{
  Ultimate prop( port_name );
  prop.test();

}

/*
 *      Method:  OptManager :: sendStart
 */
  void 
OptManager::sendStart(string port_name)
{
          Ultimate prop(port_name);
          prop.start();
}

/*
 *      Method:  OptManager :: sendColor
 */
  void 
OptManager::sendColor( uint8_t red, uint8_t green, uint8_t blue, string port_name)
{
          Ultimate prop(port_name);
          prop.color(red, green, blue);
}

/*
 *      Method:  OptManager :: sendGetSerial
 */
  void 
OptManager::sendGetSerial( std::string port_name)
{
  Ultimate prop(port_name);
  cout << "Serial number is : 0x" << prop.getSerial() << endl;
          
}

/*
 *      Method:  OptManager :: sendSymlink
 */
  void 
OptManager::sendSymlink( std::string port_name)
{
  string sym_out;
  Ultimate prop(port_name);
  string serial_number = prop.getSerial();
  sym_out = Symlink::getSym(serial_number);
  cout << sym_out << endl;

          
}
/*
 *      Method:  OptManager :: sendSetSerial
 */
  void
OptManager::sendSetSerial(uint32_t i_serialNumber, string port)
{
  uint32_t ser_nb;
  Ultimate prop(port);
  ser_nb = prop.setSerialUlt( i_serialNumber);

  if (VERBOSE_AERO)
  {
    cout << "Serial number 0x" << hex << ser_nb
         << " sent" << endl;
  }

}

/*
 *      Method:  OptManager :: sendVerify
 */
  void
OptManager::sendVerify(string fileName)
{
  GloInterface gloInt;
  gloInt.verifyFile(fileName);
}
/*
 *      Method:  OptManager :: sendUpGlo
 */
  void 
OptManager::sendUpGlo(string fileName, string port_name)
{
  Ultimate prop(port_name);
  if( prop.uploadGlo(fileName))
  {
    cout << "Upload " << fileName << " OK" << endl;
  }
}

/* ---- End of SEND COMMAND ---- */ 


/*
 *        .::SUB COMMAND::.
 */
 
/*
 *       Class:  OptManager
 *      Method:  OptManager :: subRoutine
 * Description:  Getopt sub routine switch
 */
  void
OptManager::subRoutine(const short int FLAG )
{
  switch(FLAG)
  {
    case FLAG_COLOR:
      subRoutineColor();
      break;

    case FLAG_UP_GLOC:
    case FLAG_UP_GLO:  
      subRoutineUp(FLAG);
      break;

    case FLAG_SET_SER:
      subRoutineSetSerial();
      break;
  }
}

/*
 *       Class:  OptManager
 *      Method:  OptManager :: subRoutineSetSerial
 * Description:  Getopt sub routine for Set Serial
 */
  void
OptManager::subRoutineSetSerial()
{
  optSerial  tmpSerialNb; 
  subopts = optarg;
  tmpSerialNb.i_serialNumber = 0;
  uint32_t tmp_int;
  for (int i = 0; *subopts !='\0'; i++)
  {
    getsubopt(&subopts, token, &value);
    switch (i)
    {
      case 0:
        tmp_int =isValidSerialNumber(value);
        if (tmp_int != 1)
          tmpSerialNb.i_serialNumber = tmp_int; 
        else
          cerr << "error without throw" << endl;
        break;

      case 1:
        tmpSerialNb.port = value; 
        break;

      default:
        cout << "too many args in set serial command" << endl;
        break;
    }
  }
  setSerialList.push_back(tmpSerialNb); 
}


/*
 *       Class:  OptManager
 *      Method:  OptManager :: subRoutineUp
 * Description:  Getopt sub routine for Upload Glo & GloC
 */
  void
OptManager::subRoutineUp(const short int FLAG )
{
  optUp  tmpOpt; 

  subopts = optarg;
  for (int i = 0; *subopts !='\0'; i++)
  {
    getsubopt(&subopts, token, &value);
    switch (i)
    {
      case 0:
        tmpOpt.fileName = value;
        break;

      case 1:
        tmpOpt.port = value;
        break;

      default:
        cout << "too many args in upload command" << endl;
        break;
    }
  }

  switch (FLAG)
  {
    case FLAG_UP_GLO:
      upGloList.push_back(tmpOpt);
      break;
    case FLAG_UP_GLOC:
      upGloCList.push_back(tmpOpt); 
      break;
  }
}

/*
 *       Class:  OptManager
 *      Method:  OptManager :: subRoutineColor
 * Description:  Getopt sub routine for color command
 */
  void
OptManager::subRoutineColor()
{
  optCol tmpcol;
  uint8_t intensity[3] = {0}  ;
  int tmp_int;
  subopts = optarg;
  for (int i = 0; *subopts !='\0'; i++)
  {
    getsubopt(&subopts, token, &value);
    switch (i)
    {
      
      // for all color verify the validity of values
      case RED: case GREEN: case BLUE:
        tmp_int = isValid0xFF(value);
        if (tmp_int == -1)
        {
          cerr << "ERROR : non valid color for color command c " 
               << value << endl;
          exit (EXIT_FAILURE);
        }
        intensity[i] = ( char)tmp_int;
        break;

      case PORT_NAME:
        for (int index = 0 ; index<3; index++)
          tmpcol.rgb[index] = intensity[index];
        tmpcol.port = value;
        colorList.push_back(tmpcol);
        break;

      default:
        cerr << "Error, too many args for -c" << endl;
        exit (EXIT_FAILURE);
        break;
    }
  }
}		/* -----  end of method OptManager::subRoutineColor  ----- */


/*
 *       Class:  OptManager
 *      Method:  OptManager :: isValid0xFF
 * Description:  verify if value can become 1 byte(0-255) 
 */
  int 
OptManager::isValid0xFF(char * value)
{
  int hexval = strtol(value,(char**)NULL,0); 
  if (hexval < 256 && hexval > -1)
    return hexval;
  return -1;
}

/*
 *       Class:  OptManager
 *      Method:  OptManager :: isValidSerialNumber
 * Description:  test with dec, hex & bin notation 
 */
  uint32_t 
OptManager::isValidSerialNumber(string strNb)
{
  uint32_t i_ser_nb; 

  if (isDecNotation( strNb)) 
  {
    std::stringstream ss( strNb);
    ss >> dec >> i_ser_nb;
  }
  else if(isHexNotation( strNb))
  {
    std::stringstream ss( strNb.substr(2));
    ss >> hex >> i_ser_nb;
  }
  else
  {
    throw std::string( "Bad serial number" );
  }

  return i_ser_nb;
}

/*
 *       Class:  OptManager
 *      Method:  OptManager :: isHexNotation
 * Description:  is hex notation
 */
  bool
OptManager::isHexNotation(std::string const& s)
{
  return s.compare(0, 2, "0x") == 0
      && s.size() > 2
      && s.find_first_not_of("0123456789abcdefABCDEF", 2) == std::string::npos;
}


/*
 *       Class:  OptManager
 *      Method:  OptManager :: isDecNotation
 * Description:  is dec notation
 */
  bool
OptManager::isDecNotation(const std::string& s)
{
    std::string::const_iterator it = s.begin();
    while (it != s.end() && std::isdigit(*it)) ++it;
    return !s.empty() && it == s.end();
}