35 #include <sys/types.h>
37 #include <secure_wrapper.h>
40 #include "comcastIrKeyCodes.h"
43 #include "plat_power.h"
44 #include "libIBusDaemon.h"
45 #include "resetModes.h"
47 #include "exception.hpp"
50 #include "pwrlogger.h"
54 static pthread_mutex_t tMutexLock;
55 static pthread_cond_t tMutexCond;
56 static long gKeyPressedTime = 0;
57 static long gKeyReleasedTime = 0;
58 static long gCurrentTime = 0;
59 static int gTimeOutValue = 0;
60 static bool inResetMode =
false;
61 static bool gResetMonitoredOnIBus =
false;
62 static unsigned int gLastResetEvent = 0;
63 static bool gInitialized =
false;
65 static bool isRIFocus =
false;
66 static bool isXREFocus =
false;
67 static bool grabedFocus =
false;
68 static int modeIndexStatus = 0;
72 static bool gWaitState =
false;
74 static bool gResetTimer =
true;
77 static bool gIsSameKey =
false;
78 static bool gKeyAllowed[] = {
true,
true,
true,
true};
101 typedef enum keyValue
106 KEY_RIGHT=KED_ARROWRIGHT,
107 KEY_LEFT=KED_ARROWLEFT,
110 KEY_DOWN=KED_ARROWDOWN
134 int expectedKeyIndex;
135 int (*resetFuction) (void);
140 static stateMachine_t coldFactory = {ENDED, 0, processColdFactoryReset};
142 static stateMachine_t wareHouse = {ENDED, 0, processWareHouseReset};
143 static stateMachine_t customer = {ENDED, 0, processCustomerReset};
144 static stateMachine_t personality = {ENDED, 0, processPersonalityReset};
149 stateMachine_t * stateMachineList[] = {&coldFactory, &factory, &wareHouse, &customer, &personality};
152 typedef enum resetType
161 static const char *resetString [5] = {
"CLDINIT",
"FR RST",
"WH RST",
"CUST",
"PERS"};
171 #define INITIAL_TIME_OUT_VALUE 10000
172 #define MAX_RESET_MODE_COUNT 5
174 #define KEY_COMBINATION_TIMEOUT 2000
175 #define POWER_KEY_HOLD_TIME_FOR_REBOOT 10000
176 #define POWER_KEY_HOLD_TIME_FOR_RESET 2000
177 #define POWER_KEY_HOLD_TIME_FOR_FACTORY_RESET 30000
183 static keyType_t coldFactorySeq[] = {{KEY_POWER, KET_KEYUP, 0, 0}, {KEY_OK, KET_KEYUP, 0, 2000}, \
184 {KEY_RIGHT, KET_KEYUP, 0, 2000}, {KEY_MENU, KET_KEYUP, 0, 2000}, \
185 {KEY_RIGHT, KET_KEYUP, 0, 2000}, {KEY_POWER, KET_KEYUP, 0, 0}, \
186 {KEY_NULL, 0, 0, 0}};
188 static keyType_t factorySeq[] = {{KEY_POWER, KET_KEYUP, 0, 0}, {KEY_OK, KET_KEYUP, 0, 2000}, \
189 {KEY_RIGHT, KET_KEYUP, 0, 2000}, {KEY_MENU, KET_KEYUP, 0, 2000}, \
190 {KEY_LEFT, KET_KEYUP, 0, 2000}, {KEY_POWER, KET_KEYUP, 0, 0}, \
191 {KEY_NULL, 0, 0, 0}};
193 static keyType_t wareHouseSeq[] = {{KEY_POWER, KET_KEYUP, 0, 0}, {KEY_OK, KET_KEYUP, 0, 2000}, \
194 {KEY_RIGHT, KET_KEYUP, 0, 2000}, {KEY_DOWN, KET_KEYUP, 0, 2000}, \
195 {KEY_POWER, KET_KEYUP, 0, 0}, {KEY_NULL, 0, 0, 0}};
198 static keyType_t customerSeq[] = {{KEY_POWER, KET_KEYUP, 0, 0}, {KEY_OK, KET_KEYUP, 0, 2000}, \
199 {KED_UNDEFINEDKEY, KET_KEYUP, 0, 0}, {KEY_NULL, 0, 0, 0}};
201 keyType_t personalityResetSeq[] = {{KEY_POWER, KET_KEYUP, 0, 0}, {KEY_OK, KET_KEYUP, 0, 2000}, \
202 {KEY_LEFT, KET_KEYUP, 0, 2000}, {KEY_DOWN, KET_KEYUP, 0, 2000}, \
203 {KEY_POWER, KET_KEYUP, 0, 0}, {KEY_NULL, 0, 0, 0}};
206 static const keyVariant_t keyVariants[] = {{ KED_POWER, KED_RF_POWER}, {KED_OK, KED_SELECT}};
209 static KeyMap_t Reset_Rules[MAX_RESET_MODE_COUNT] = {{COLD_FACTORY, &coldFactorySeq[0]},{FACTORY, &factorySeq[0]},{WARE_HOUSE, &wareHouseSeq[0]},{CUSTOMER, &customerSeq[0]}, \
210 {PERSONALITY, &personalityResetSeq[0]}};
213 static IARM_Bus_PWRMgr_PowerState_t curPwrState = IARM_BUS_PWRMGR_POWERSTATE_STANDBY;
215 static void storeFocus()
220 static void getSystemTime(
long *syst)
222 struct timespec Stime;
223 clock_gettime(CLOCK_MONOTONIC, &Stime);
224 *syst = ((Stime.tv_sec * 1000) + (Stime.tv_nsec / 1000000));
228 static void releaseFocus()
230 __TIMESTAMP();LOG(
"Reset: IARM-Release MEDIA Focus for RI Process\n");
231 IARM_BusDaemon_ReleaseOwnership(IARM_BUS_RESOURCE_FOCUS);
238 static void grabFocus()
241 __TIMESTAMP();LOG(
"Reset: IARM-Request MEDIA Focus for Reset Process\n");
242 IARM_BusDaemon_RequestOwnership(IARM_BUS_RESOURCE_FOCUS);
246 static void ResetResetState(
void)
249 for( i = 0; i < MAX_RESET_MODE_COUNT; i++ )
251 stateMachineList[i]->expectedKeyIndex = 0;
256 gKeyReleasedTime = 0;
266 if(gResetMonitoredOnIBus)
269 eventData.data.reset_sequence_progress = -1;
271 (
void *)&eventData,
sizeof(eventData));
272 gResetMonitoredOnIBus =
false;
277 void setResetPowerState(IARM_Bus_PWRMgr_PowerState_t newPwrState)
280 curPwrState = newPwrState;
288 __TIMESTAMP();LOG(
"Reset: Blinking LEDs\n");
295 for (uint i = 0; i < fpIndicators.
size(); i++)
303 for (uint i = 0; i < fpIndicators.
size(); i++)
310 for (uint i = 0; i < fpIndicators.
size(); i++)
318 __TIMESTAMP();LOG(
"An exception caught during FP LED Blink Operation: %s",e.
getMessage().c_str());
323 int checkResetModeSequence(
int keyType,
int keyCode, resetType_t resetMode)
333 pCurrentState = Reset_Rules[resetMode].pData + stateMachineList[resetMode]->expectedKeyIndex;
335 if ( (
true == gWaitState) && (
true != gIsSameKey))
340 pthread_mutex_lock(&tMutexLock);
341 pthread_cond_signal(&tMutexCond);
342 pthread_mutex_unlock(&tMutexLock);
351 if( (pCurrentState->keyMode ==
keyType) && (pCurrentState->Key == keyCode))
353 if(0 < stateMachineList[resetMode]->expectedKeyIndex)
356 if(gLastResetEvent != stateMachineList[resetMode]->expectedKeyIndex)
358 gResetMonitoredOnIBus =
true;
360 eventData.data.reset_sequence_progress = stateMachineList[resetMode]->expectedKeyIndex;
362 (
void *)&eventData,
sizeof(eventData));
363 __TIMESTAMP();LOG(
"\n Reset: sending IARM event %d \n", stateMachineList[resetMode]->expectedKeyIndex);
364 gLastResetEvent = stateMachineList[resetMode]->expectedKeyIndex;
368 stateMachineList[resetMode]->expectedKeyIndex++;
373 pNextState = pCurrentState + 1;
374 if ( KEY_NULL == (pNextState)->Key)
377 stateMachineList[resetMode]->expectedKeyIndex = 0;
378 __TIMESTAMP();LOG(
"\n Reset: Action took place for: %d \n",resetMode);
388 printf(
"An exception caught while setting fp text : %s",e.
getMessage().c_str());
391 ret = stateMachineList[resetMode]->resetFuction();
395 if(0 != pCurrentState->validTime)
402 getSystemTime(&gCurrentTime);
403 gTimeOutValue = pCurrentState->validTime;
404 __TIMESTAMP();LOG(
"Reset: gTimeOutValue: %d- %d ",gTimeOutValue,gCurrentTime);
412 stateMachineList[resetMode]->expectedKeyIndex = 0;
419 int checkResetSequence(
int keyType,
int keyCode)
430 if (keyCode == v->Variant)
438 if (0 != gCurrentTime)
441 getSystemTime(&gKeyPressedTime);
443 if((gKeyPressedTime - gCurrentTime) > gTimeOutValue)
445 __TIMESTAMP(); LOG(
"\n Reset: Sorry you crossed the time out value %d \n", gTimeOutValue);
458 if (!inResetMode && ((keyCode == KED_POWER) || (keyCode == KED_RF_POWER) || (keyCode == KED_FACTORY)))
461 getSystemTime(&gKeyPressedTime);
470 if (!inResetMode && ((keyCode == KED_POWER) || (keyCode == KED_RF_POWER) || (keyCode == KED_FACTORY)))
473 getSystemTime(&gKeyReleasedTime);
474 if(gKeyPressedTime == 0)
478 if ((keyCode == KED_POWER) || (keyCode == KED_RF_POWER))
480 #ifdef ENABLE_FACTORY_RESET_VIA_FP_POWER_BUTTON
481 if((gKeyReleasedTime - gKeyPressedTime) >= POWER_KEY_HOLD_TIME_FOR_FACTORY_RESET)
483 __TIMESTAMP();LOG(
"\n Reset: User Factory reset through long-pressing power key\n");
485 processUserFactoryReset();
490 if ((gKeyReleasedTime - gKeyPressedTime) > POWER_KEY_HOLD_TIME_FOR_REBOOT)
492 __TIMESTAMP();LOG(
"\n Reset: Inside reset mode 1\n");
494 PwrMgr_Reset(curPwrState,
true);
500 else if ((gKeyReleasedTime - gKeyPressedTime) >= POWER_KEY_HOLD_TIME_FOR_RESET)
502 __TIMESTAMP();LOG(
"\n Reset: Inside reset mode 2\n");
509 getSystemTime(&gCurrentTime);
510 gTimeOutValue = INITIAL_TIME_OUT_VALUE;
517 for( i = 0; i < MAX_RESET_MODE_COUNT; i++ )
519 stateMachineList[i]->expectedKeyIndex++;
521 __TIMESTAMP();LOG(
"\n Reset: FP Power key is pressed for 2 secs");
542 __TIMESTAMP();LOG(
"\n Reset: Already in reset mode \n");
544 resetType_t resetMode = COLD_FACTORY;
545 checkResetModeSequence(
keyType, keyCode, resetMode);
548 checkResetModeSequence(
keyType, keyCode, resetMode);
549 #ifndef _DISABLE_RESET_SEQUENCE
550 resetMode = WARE_HOUSE;
551 checkResetModeSequence(
keyType, keyCode, resetMode);
553 static bool indicatedDisable =
false;
554 if (!indicatedDisable) {
555 indicatedDisable =
true;
556 LOG(
"Reset sequence is disabled for Warehouse reset");
559 resetMode = CUSTOMER;
560 checkResetModeSequence(
keyType, keyCode, resetMode);
561 resetMode = PERSONALITY;
562 checkResetModeSequence(
keyType, keyCode, resetMode);
573 if(( 0 == stateMachineList[0]->expectedKeyIndex) && (0 == stateMachineList[1]->expectedKeyIndex) && (0 == stateMachineList[2]->expectedKeyIndex) &&
574 (0 == stateMachineList[3]->expectedKeyIndex) && (0 == stateMachineList[4]->expectedKeyIndex))
576 __TIMESTAMP();LOG(
"\n Reset: All statemachines in initial state. So reset the state machines\n");
583 IARM_Result_t initReset()
588 pthread_mutex_init (&tMutexLock, NULL);
589 pthread_cond_init (&tMutexCond, NULL);
591 return IARM_RESULT_SUCCESS;
595 IARM_Result_t unInitReset()
599 gInitialized =
false;
600 pthread_mutex_unlock(&tMutexLock);
601 pthread_mutex_destroy(&tMutexLock);
602 pthread_cond_destroy(&tMutexCond);
604 return IARM_RESULT_SUCCESS;
607 void PwrMgr_Reset(IARM_Bus_PWRMgr_PowerState_t newState,
bool isFPKeyPress)
609 LOG(
"\n PWR_Reset: Rebooting box now .....\r\n");
610 if (newState == IARM_BUS_PWRMGR_POWERSTATE_ON) system(
"sh /togglePower");
614 performReboot(
"PowerMgr_Powerreset",
nullptr,
"Rebooting the box due to ECM connectivity Loss...");
618 performReboot(
"PowerMgr_Powerreset",
nullptr,
"Rebooting the box due to Frontpanel power key pressed for 10 sec...");
622 inline static void check_payload(
const char ** input,
const char * default_arg)
624 if((NULL == *input) || (0 == (*input)[0]))
626 *input = default_arg;
631 void performReboot(
const char * requestor,
const char * reboot_reason_custom,
const char * reboot_reason_other)
633 LOG(
"performReboot: Rebooting box now. Requestor: %s. Reboot reason: %s\n", requestor, reboot_reason_custom);
634 const char * default_arg =
"unknown";
636 check_payload(&requestor, default_arg);
637 check_payload(&reboot_reason_custom, default_arg);
638 check_payload(&reboot_reason_other, default_arg);
642 strncpy(eventData.reboot_reason_custom, reboot_reason_custom,
sizeof(eventData.reboot_reason_custom));
643 strncpy(eventData.reboot_reason_other, reboot_reason_other,
sizeof(eventData.reboot_reason_other));
644 strncpy(eventData.requestor, requestor,
sizeof(eventData.requestor));
645 eventData.reboot_reason_custom[
sizeof(eventData.reboot_reason_custom) - 1] =
'\0';
646 eventData.reboot_reason_other[
sizeof(eventData.reboot_reason_other) - 1] =
'\0';
647 eventData.requestor[
sizeof(eventData.requestor) - 1] =
'\0';
656 char * requestor_cpy = strdup(requestor);
657 char * reboot_reason_custom_cpy = strdup(reboot_reason_custom);
658 char * reboot_reason_other_cpy = strdup(reboot_reason_other);
660 std::thread async_reboot_thread([requestor_cpy, reboot_reason_custom_cpy, reboot_reason_other_cpy] () {
661 v_secure_system(
"echo 0 > /opt/.rebootFlag");
663 if(0 == access(
"/rebootNow.sh", F_OK))
665 v_secure_system(
"/rebootNow.sh -s '%s' -r '%s' -o '%s'", requestor_cpy, reboot_reason_custom_cpy, reboot_reason_other_cpy);
669 v_secure_system(
"/lib/rdk/rebootNow.sh -s '%s' -r '%s' -o '%s'", requestor_cpy, reboot_reason_custom_cpy, reboot_reason_other_cpy);
672 free(reboot_reason_custom_cpy);
673 free(reboot_reason_other_cpy);
675 async_reboot_thread.detach();