Skip to content

Commit 26210a2

Browse files
committed
Converted all fprintf to HAPLogInfo.
1 parent bf8e68f commit 26210a2

File tree

1 file changed

+45
-61
lines changed

1 file changed

+45
-61
lines changed

PAL/Raspi/GRM_Lock.c

+45-61
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ int playAudio(char * filename, float volume) {
186186
device = ao_open_live(default_driver, &format, NULL/*&option*/); // use default audio, setup alsa for dmix and mono!
187187

188188
if (device == NULL) {
189-
fprintf(stderr, "Error opening device.\n");
189+
HAPLogInfo(&kHAPLog_Default, "%s: Error opening device.", __func__);
190190
return 1;
191191
}
192192

@@ -208,7 +208,7 @@ int playAudio(char * filename, float volume) {
208208

209209
if (ao_play(device, (char *) buffer, (uint_32) (read * sizeof(short)))
210210
== 0) {
211-
fprintf(stderr, "ao_play: failed.\n");
211+
HAPLogInfo(&kHAPLog_Default, "%s: ao_play: failed.", __func__);
212212
clean(device, file);
213213
break;
214214
}
@@ -239,27 +239,31 @@ void * playAudioThread(void *ptr) {
239239

240240
gettimeofday(&timeStart, NULL);
241241

242-
if (!playing) {
242+
if (!playing) { // with a mutex, other calls would be kind of queued, this way, they return immediately
243243
playing = true;
244+
244245
if (strcmp((char*) ptr, unlocked) == 0
245-
|| strcmp((char*) ptr, locked) == 0)
246+
|| strcmp((char*) ptr, locked) == 0){
246247
volume = maxVolume; // alway play the "Unlocked" sound at this volume
247-
fprintf(stderr, "volume = %f\n", volume);
248+
}
249+
250+
HAPLogInfo(&kHAPLog_Default, "%s: volume = %u", __func__, (unsigned) (volume * relativeVolume * 100));
251+
248252
playAudio((char*) ptr, volume * relativeVolume); // weighted with global volume
249253
if (strcmp((char*) ptr, unlocked) == 0
250-
|| strcmp((char*) ptr, locked) == 0)
254+
|| strcmp((char*) ptr, locked) == 0){
251255
volume = lowVolume; // after "Unlocked" start over at this volume
252-
256+
}
257+
253258
playing = false;
254259

255260
if (volume < maxVolume)
256-
volume *= 2.0;
261+
volume *= 2.0; // double the volume at every step
257262
else
258-
volume = maxVolume; // max
263+
volume = maxVolume; // but limit to maximum
259264
} else {
260-
fprintf(stderr, "IGNORING - audio still playing\n");
261-
262-
} // with a mutex, other calls would be kind of queued, this way, they return immediately
265+
HAPLogInfo(&kHAPLog_Default, "%s: IGNORING - audio still playing", __func__);
266+
}
263267

264268
return NULL;
265269
}
@@ -268,39 +272,34 @@ void * playAudioThread(void *ptr) {
268272
bool blockBruteForce(long pressTimeMs) { // call only on press (not release)!
269273
bool allow = true;
270274
static int count = 0;
271-
if (pressTimeMs > 30000) // if released for 30s, re-allow unlocking
272-
{
275+
if (pressTimeMs > 30000){ // if released for 30s, re-allow unlocking
273276
allow = true;
274277
count = 0;
275-
fprintf(stderr, "Unlocking re-allowed for 3 attempts!\n");
278+
HAPLogInfo(&kHAPLog_Default, "%s: Unlocking re-allowed for 3 attempts", __func__);
276279
}
277280
count++;
278-
if (count > 3) {
279-
281+
if (count > 3){
280282
allow = false; // after the third (i.e. at the fourth) ring, disallow unlocking the door for some time
281283
count--; // prevent overflow, i.e. go back to 2
282-
fprintf(stderr, "Unlocking disallowed for 30s!\n"); // called after start of pressing, so pressTimeMs is actually a release time
283-
284+
HAPLogInfo(&kHAPLog_Default, "%s: Unlocking disallowed for 30s", __func__); // called after start of pressing, so pressTimeMs is actually a release time
284285
}
285-
286286
return allow;
287287
}
288288

289289
void sendPushNotification(const char * push_message) {
290-
291-
fprintf(stderr, "Push: %s\n", push_message);
290+
HAPLogInfo(&kHAPLog_Default, "%s: DEPRECATED - Push: %s", __func__, push_message);
292291
}
293292

294293
void ringBell2(long pressTimeMs) {
295294

296295
(void) pressTimeMs; // unused, suppress warning
296+
// called after start of pressing, so pressTimeMs is actually a release time
297297

298-
fprintf(stderr, "DINGDONG!\n"); // called after start of pressing, so pressTimeMs is actually a release time
299-
298+
HAPLogInfo(&kHAPLog_Default, "%s: DING-DONG", __func__);
299+
300300
HAPError err = HAPPlatformRunLoopScheduleCallback(ringBell, NULL, 0); // required for IRQ/threads, but not for timers
301301
HAPLogInfo(&kHAPLog_Default, "%s: RING triggered with error = %u", __func__, err);
302302

303-
304303
static struct timeval timeStart, timeEnd;
305304
gettimeofday(&timeEnd, NULL);
306305
long dt = ((timeEnd.tv_sec - timeStart.tv_sec) * 1000
@@ -317,7 +316,7 @@ void ringBell2(long pressTimeMs) {
317316
int result = pthread_create(&audioThread, NULL, playAudioThread,
318317
(void*) doorbell);
319318
if (result) {
320-
fprintf(stderr, "Error - pthread_create(playAudioThread) return code: %d\n", result);
319+
HAPLogInfo(&kHAPLog_Default, "%s: Error - pthread_create(playAudioThread) return code: %d", __func__, result);
321320
} else{
322321
pthread_detach(audioThread);
323322
}
@@ -338,14 +337,12 @@ void puzzleSolved(void){
338337
bool b = block;
339338
pthread_mutex_unlock(&blockMutex);
340339
if (b) {
341-
fprintf(stderr, "LOCKED!\n");
340+
HAPLogInfo(&kHAPLog_Default, "%s: LOCKED!", __func__);
342341
pthread_t audioThread;
343342
int result = pthread_create(&audioThread, NULL, playAudioThread,
344343
(void*) locked);
345344
if (result) {
346-
fprintf(stderr,
347-
"Error - pthread_create(playAudioThread, locked) return code: %d\n",
348-
result);
345+
HAPLogInfo(&kHAPLog_Default, "%s: Error - pthread_create(playAudioThread, locked) return code: %d", __func__, result);
349346
} else{
350347
pthread_detach(audioThread);
351348
}
@@ -355,14 +352,12 @@ void puzzleSolved(void){
355352
logEvent(EVENT_LOCKED);
356353

357354
} else {
358-
fprintf(stderr, "UNLOCKED!\n");
355+
HAPLogInfo(&kHAPLog_Default, "%s: UNLOCKED!", __func__);
359356
pthread_t audioThread;
360357
int result = pthread_create(&audioThread, NULL, playAudioThread,
361358
(void*) unlocked);
362359
if (result) {
363-
fprintf(stderr,
364-
"Error - pthread_create(playAudioThread, unlocked) return code: %d\n",
365-
result);
360+
HAPLogInfo(&kHAPLog_Default, "%s: Error - pthread_create(playAudioThread, unlocked) return code: %d", __func__, result);
366361
} else{
367362
pthread_detach(audioThread);
368363
}
@@ -375,7 +370,7 @@ void puzzleSolved(void){
375370

376371

377372

378-
void decodePattern(bool press) { // bei press (true) und release (false) aufrufen
373+
void decodePattern(bool press) { // call for press (true) and release (false) of bell button
379374
static struct timeval timeStart, timeEnd;
380375

381376

@@ -384,32 +379,25 @@ void decodePattern(bool press) { // bei press (true) und release (false) aufrufe
384379
+ (timeEnd.tv_usec - timeStart.tv_usec) / 1000);
385380
timeStart = timeEnd;
386381

387-
fprintf(stderr, "%s time was = %ld ms = ", !press ? "press" : "release",
382+
HAPLogInfo(&kHAPLog_Default, "%s: %s time was = %ld ms = ", __func__, (!press ? "press" : "release"),
388383
pressTimeMs); // !press, because we lag behind
389-
// NOTE: first value of pressTimeMs will be large negative
384+
385+
// NOTE: first value of pressTimeMs (after startup or long idle time) can be anything (even overflow to negative), but it is ignored in the state machine
390386

391387
/* NEED TO DEBOUNCE! And need to distinguish falling and rising IRQs properly.
392388
* Reading the input at the beginning of the ISR may be too slow, i.e. the level is no longer the one that triggered the IRQ.
393-
* Workaround: use two inputs in parallel, one ofr rising, another one for falling edge detection. */
394-
395-
389+
* Possible workaround: use two inputs in parallel, one for rising, another one for falling edge detection. */
396390

397-
bool isAny = (pressTimeMs <= anyMax) && (pressTimeMs >= anyMin); // nach oben begrenzt, damit kein Zustand ewig stehenbleiben kann! Nach spätestens 10s matched auch Any nicht mehr, d.h. ein press-Event führt zum state RING.
398391

399-
bool isShort = (pressTimeMs <= shortMax) && (pressTimeMs >= shortMin); // less than half a second
400-
bool isLong = (pressTimeMs <= longMax) && (pressTimeMs >= longMin); // about one seconds
401-
bool isVeryLong = (pressTimeMs <= veryLongMax) && (pressTimeMs >= veryLongMin); // about two seconds
402-
403-
404-
fprintf(stderr, "%s \n",
405-
isVeryLong ?
406-
"very long" :
407-
(isLong ? "long" : (isShort ? "short" : "other")));
392+
bool isAny = (pressTimeMs <= anyMax) && (pressTimeMs >= anyMin); // upper limit anyMax ensures that no state is kept forever: when anyMax is exceeded when expecting ANY duration, the next press/release event leads to the RING state
393+
bool isShort = (pressTimeMs <= shortMax) && (pressTimeMs >= shortMin); // e.g. less than half a second
394+
bool isLong = (pressTimeMs <= longMax) && (pressTimeMs >= longMin); // e.g. about one seconds
395+
bool isVeryLong = (pressTimeMs <= veryLongMax) && (pressTimeMs >= veryLongMin); // e.g. about two seconds
396+
397+
HAPLogInfo(&kHAPLog_Default, "%s: %s", __func__, (isVeryLong ? "very long" : (isLong ? "long" : (isShort ? "short" : "other"))));
408398

409399
bool release = !press;
410-
static bool allow = true; // set to false if too many tried in too short a time (prevents brute force attack)
411-
412-
// flexible new variant
400+
static bool allow = true; // set to false if too many tries in too short a time (prevents brute force attack)
413401

414402
bool checks[4]={[CODE_ANY]=isAny, [CODE_SHORT]=isShort,[CODE_LONG]=isLong,[CODE_VERYLONG]=isVeryLong};
415403

@@ -471,23 +459,19 @@ void decodePattern(bool press) { // bei press (true) und release (false) aufrufe
471459
}
472460
}
473461
} else {
474-
fprintf(stderr, "state overflow error"); // this should not ever happen - sizeof(code) must be odd (odd number of press times plus even number of release times)
462+
HAPLogInfo(&kHAPLog_Default, "%s: state overflow error", __func__); // this should not ever happen - sizeof(code) must be odd (odd number of press times plus even number of release times)
475463
numericalState = 0;
476464
}
477-
fprintf(stderr, "state=%d, allow=%d\n", numericalState, allow);
478-
479-
465+
HAPLogInfo(&kHAPLog_Default, "%s: state=%d, allow=%d", __func__, numericalState, allow);
480466
}
481467

482468

483-
484-
485469
void risingISR(void) {
486-
fprintf(stderr, "rising IRQ / button released\n");
470+
HAPLogInfo(&kHAPLog_Default, "%s: rising IRQ / button released", __func__);
487471
}
488472

489473
void fallingISR(void) {
490-
fprintf(stderr, "falling IRQ / button pressed\n");
474+
HAPLogInfo(&kHAPLog_Default, "%s: falling IRQ / button pressed", __func__);
491475
}
492476

493477
/*

0 commit comments

Comments
 (0)