Memory Management Improved
[pierogi] / protocols / pirprotocol.cpp
1 #include "pirprotocol.h"
2
3 #include <QMutex>
4 #include <QMetaType>
5
6 #include <time.h>
7 #include <sstream>
8 #include <errno.h>
9 #include "pirexception.h"
10
11 // A flag for communicating with the main thread:
12 extern bool stopRepeatingFlag;
13 extern QMutex stopRepeatingMutex;
14
15 // Total of all running commands
16 extern bool commandInFlight;
17 extern QMutex commandIFMutex;
18
19 // From what I understand (mostly from reading LIRC config files), NEC
20 // protocol based remotes mostly use a frequency of 38000 units and a
21 // duty cycle of 50%.  They'll be set to these defaults here, and overridden
22 // as needed by child classes.
23
24 PIRProtocol::PIRProtocol(
25   QObject *guiObject,
26   unsigned int index,
27   unsigned int gSpace,
28   bool iclflag)
29   : carrierFrequency(38000),
30     dutyCycle(50),
31     isConstantLength(iclflag),
32     gap(gSpace),
33     minimumRepetitions(0),
34     id(index)
35 {
36   qRegisterMetaType<PIRKeyName>("PIRKeyName");
37
38   QObject::connect(
39     guiObject,
40     SIGNAL(buttonPressed(unsigned int, PIRKeyName)),
41     this,
42     SLOT(startSendingCommand(unsigned int, PIRKeyName)),
43     Qt::QueuedConnection);
44
45   QObject::connect(
46     this,
47     SIGNAL(commandFailed(const char *)),
48     guiObject,
49     SLOT(receivedExternalWarning(const char *)),
50     Qt::QueuedConnection);
51 }
52
53
54 void PIRProtocol::addKey(
55   PIRKeyName key,
56   unsigned long command,
57   unsigned int size)
58 {
59   // First, if key already exists, clear it out:
60   PIRKeyBits *pkb = 0;
61   KeycodeCollection::iterator i = keycodes.find(key);
62   if (i != keycodes.end())
63   {
64     pkb = &(i->second);
65     pkb->firstCode.clear();
66   }
67   else
68   {
69     pkb = &(keycodes[key]);
70   }
71
72   appendToBitSeq(pkb->firstCode, command, size);
73 }
74
75
76 void PIRProtocol::addSIRCKey(
77   PIRKeyName key,
78   unsigned int addressData,
79   unsigned int size,
80   unsigned int commandData)
81 {
82   // First, if key already exists, clear it out:
83   PIRKeyBits *pkb = 0;
84   KeycodeCollection::iterator i = keycodes.find(key);
85   if (i != keycodes.end())
86   {
87     pkb = &(i->second);
88     pkb->firstCode.clear();
89     pkb->secondCode.clear();
90     pkb->thirdCode.clear();
91   }
92   else
93   {
94     pkb = &(keycodes[key]);
95   }
96
97   // First, append the address data:
98   appendToBitSeq(pkb->firstCode, addressData, size);
99
100   // Next, the command data.  The size is always 7 bits:
101   appendToBitSeq(pkb->secondCode, commandData, 7);
102 }
103
104
105 void PIRProtocol::addSIRC20Key(
106   PIRKeyName key,
107   unsigned int secondaryAddressData,
108   unsigned int primaryAddressData,
109   unsigned int commandData)
110 {
111   // First, if key already exists, clear it out:
112   PIRKeyBits *pkb = 0;
113   KeycodeCollection::iterator i = keycodes.find(key);
114   if (i != keycodes.end())
115   {
116     pkb = &(i->second);
117     pkb->firstCode.clear();
118     pkb->secondCode.clear();
119     pkb->thirdCode.clear();
120   }
121   else
122   {
123     pkb = &(keycodes[key]);
124   }
125
126   // First, append the secondary address data:
127   appendToBitSeq(pkb->firstCode, secondaryAddressData, 8);
128
129   // Next, the primary address data:
130   appendToBitSeq(pkb->secondCode, primaryAddressData, 5);
131
132   // Next, the command data.  The size is always 7 bits:
133   appendToBitSeq(pkb->thirdCode, commandData, 7);
134 }
135
136
137 void PIRProtocol::addSharpKey(
138   PIRKeyName key,
139   unsigned int addressData,
140   unsigned int commandData)
141 {
142   // First, if key already exists, clear it out:
143   PIRKeyBits *pkb = 0;
144   KeycodeCollection::iterator i = keycodes.find(key);
145   if (i != keycodes.end())
146   {
147     pkb = &(i->second);
148     pkb->firstCode.clear();
149     pkb->secondCode.clear();
150   }
151   else
152   {
153     pkb = &(keycodes[key]);
154   }
155
156   // Sharp commands are all 5 bit address, 8 bit command:
157   appendToBitSeq(pkb->firstCode, addressData, 5);
158   appendToBitSeq(pkb->secondCode, commandData, 8);
159 }
160
161
162 void PIRProtocol::addNECKey(
163   PIRKeyName key,
164   unsigned int addressData,
165   unsigned int commandData)
166 {
167   PIRKeyBits *pkb = 0;
168   KeycodeCollection::iterator i = keycodes.find(key);
169   if (i != keycodes.end())
170   {
171     pkb = &(i->second);
172     pkb->firstCode.clear();
173     pkb->secondCode.clear();
174     pkb->thirdCode.clear();
175     pkb->fourthCode.clear();
176   }
177   else
178   {
179     pkb = &(keycodes[key]);
180   }
181
182   // NEC commands should always be 8 bits address, 8 bits command:
183   appendToBitSeq(pkb->firstCode, addressData, 8);
184   appendToBitSeq(pkb->secondCode, commandData, 8);
185 }
186
187
188 // Most Pioneer keys use the NEC key format, but some are pairs of
189 // NEC keys sent together:
190 void PIRProtocol::addPioneerKey(
191   PIRKeyName key,
192   unsigned int firstAddress,
193   unsigned int firstCommand,
194   unsigned int secondAddress,
195   unsigned int secondCommand)
196 {
197   PIRKeyBits *pkb = 0;
198   KeycodeCollection::iterator i = keycodes.find(key);
199   if (i != keycodes.end())
200   {
201     pkb = &(i->second);
202     pkb->firstCode.clear();
203     pkb->secondCode.clear();
204     pkb->thirdCode.clear();
205     pkb->fourthCode.clear();
206   }
207   else
208   {
209     pkb = &(keycodes[key]);
210   }
211
212   // All four codes should be 8 bits in length:
213   appendToBitSeq(pkb->firstCode, firstAddress, 8);
214   appendToBitSeq(pkb->secondCode, firstCommand, 8);
215   appendToBitSeq(pkb->thirdCode, secondAddress, 8);
216   appendToBitSeq(pkb->fourthCode, secondCommand, 8);
217 }
218
219
220 /*
221 void PIRProtocol::addRCAKey(
222   PIRKeyName key,
223   unsigned int addressData,
224   unsigned int commandData)
225 {
226   PIRKeyBits *pkb = 0;
227   KeycodeCollection::iterator i = keycodes.find(key);
228   if (i != keycodes.end())
229   {
230     pkb = &(i->second);
231     pkb->firstcode.clear();
232     pkb->secondCode.clear();
233   }
234   else
235   {
236     pkb = &(keycodes[key]);
237   }
238
239   // Address is 4 bits, command is 8 bits:
240   appendToBitSeq(pkb->firstCode, addressData, 4);
241   appendToBitSeq(pkb->secondCode, commandData, 8);
242 }
243 */
244
245
246 void PIRProtocol::addKaseikyoKey(
247   PIRKeyName key,
248   unsigned int addressData,
249   unsigned int commandData)
250 {
251   PIRKeyBits *pkb = 0;
252   KeycodeCollection::iterator i = keycodes.find(key);
253   if (i != keycodes.end())
254   {
255     pkb = &(i->second);
256     pkb->firstCode.clear();
257     pkb->secondCode.clear();
258   }
259   else
260   {
261     pkb = &(keycodes[key]);
262   }
263
264   appendToBitSeq(pkb->firstCode, addressData, 12);
265   appendToBitSeq(pkb->secondCode, commandData, 8);
266 }
267
268
269 void PIRProtocol::addDishKey(
270   PIRKeyName key,
271   unsigned int firstCommand,
272   unsigned int secondCommand)
273 {
274   PIRKeyBits *pkb = 0;
275   KeycodeCollection::iterator i = keycodes.find(key);
276   if (i != keycodes.end())
277   {
278     pkb = &(i->second);
279     pkb->firstCode.clear();
280     pkb->secondCode.clear();
281   }
282   else
283   {
284     pkb = &(keycodes[key]);
285   }
286
287   appendToBitSeq(pkb->firstCode, firstCommand, 6);
288   appendToBitSeq(pkb->secondCode, secondCommand, 5);
289 }
290
291
292 void PIRProtocol::setCarrierFrequency(
293   unsigned int freq)
294 {
295   carrierFrequency = freq;
296 }
297
298
299 void PIRProtocol::setDutyCycle(
300   unsigned int dc)
301 {
302   dutyCycle = dc;
303 }
304
305
306 void PIRProtocol::setMinimumRepetitions(
307   unsigned int minrep)
308 {
309   minimumRepetitions = minrep;
310 }
311
312
313 void PIRProtocol::setPreData(
314   unsigned long data,
315   unsigned int bits)
316 {
317   // If the container is not empty, first clear it out:
318   if (!preData.empty())
319   {
320     preData.clear();
321   }
322
323   appendToBitSeq(preData, data, bits);
324 }
325
326
327 void PIRProtocol::setPostData(
328   unsigned long data,
329   unsigned int bits)
330 {
331   // If the container is not empty, first clear it out:
332   if (!postData.empty())
333   {
334     postData.clear();
335   }
336
337   appendToBitSeq(postData, data, bits);
338 }
339
340
341 bool PIRProtocol::isCommandSupported(
342   PIRKeyName command)
343 {
344   return (keycodes.find(command) != keycodes.end());
345 }
346
347
348 void PIRProtocol::appendToBitSeq(
349   CommandSequence &sequence,
350   unsigned int bits,
351   int size)
352 {
353   if (size == 0)
354   {
355     // This is bad, but just return silently for now...
356     return;
357   }
358
359   // For each bit in the char, append a 1 or a 0 into the sequence.
360   // Starting with the largest bit, move forward one bit at a time:
361   unsigned int currentBit = 1 << (size - 1);
362
363   do
364   {
365     if (bits & currentBit)
366     {
367       sequence.push_back(1);
368     }
369     else
370     {
371       sequence.push_back(0);
372     }
373
374     currentBit = currentBit >> 1;
375   }
376   while (currentBit > 0);
377 }
378
379
380 void PIRProtocol::clearRepeatFlag()
381 {
382   QMutexLocker locker(&stopRepeatingMutex);
383   stopRepeatingFlag = false;
384 }
385
386
387 bool PIRProtocol::checkRepeatFlag()
388 {
389   QMutexLocker locker(&stopRepeatingMutex);
390   return stopRepeatingFlag;
391 }
392
393
394 // Note that the following routine blindly sleeps for the amount of time
395 // specified by the LIRC config file.  The extra overhead of processing
396 // each command will mean that repeated commands will overshoot the config
397 // time by some amount.  We could improve accuracy by waiting a little less
398 // than the specified time, if we could get a good handle on how long the
399 // overhead is delaying the command...
400 #define PIEROGI_OVERHEAD_HACK 13260
401
402 void PIRProtocol::sleepUntilRepeat(
403   int commandDuration)
404 {
405   int microseconds;
406
407   // If the LIRC config file specifies the flag "CONST_LENGTH", that means
408   // the "gap" value is the exact amount of time to wait between kicking off
409   // each command.  If not, then the "gap" needs to be added on to the total
410   // time of the previous command to see how long to sleep.
411
412   if (isConstantLength)
413   {
414     microseconds = (gap - commandDuration) - PIEROGI_OVERHEAD_HACK;
415   }
416   else
417   {
418     microseconds = gap - PIEROGI_OVERHEAD_HACK;
419   }
420
421 /*
422   // Don't even bother sleeping if there's only a few microseconds:
423   if (microseconds < 1000)
424   {
425     return;
426   }
427 */
428   // For now, I'm going to enforce a minimum sleep of 10 ms, so that we
429   // don't get runaway commands:
430   if (microseconds < 10000)
431   {
432     microseconds = 10000;
433   }
434
435   timespec sleeptime;
436   sleeptime.tv_sec = 0;
437   sleeptime.tv_nsec = microseconds * 1000;
438
439   timespec remainingtime;
440
441   if (nanosleep(&sleeptime, &remainingtime) == -1)
442   {
443     std::stringstream ss;
444     ss << "Problem while sleeping.\n";
445     ss << "Trying to sleep for: " << microseconds << "\n";
446     ss << "Nanosleep returned error: " << strerror(errno) << "\n";
447     throw PIRException(ss.str());
448   }
449 }
450
451
452 void PIRProtocol::setGapSize(
453   int gapSize,
454   bool iclFlag)
455 {
456   gap = gapSize;
457   isConstantLength = iclFlag;
458 }