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}, \
189 static keyType_t factorySeq[] = {{KEY_POWER, KET_KEYUP, 0, 0}, {KEY_OK, KET_KEYUP, 0, 2000}, \
190 {KEY_RIGHT, KET_KEYUP, 0, 2000}, {KEY_MENU, KET_KEYUP, 0, 2000}, \
191 {KEY_LEFT, KET_KEYUP, 0, 2000}, {KEY_POWER, KET_KEYUP, 0, 0}, \
195 static keyType_t wareHouseSeq[] = {{KEY_POWER, KET_KEYUP, 0, 0}, {KEY_OK, KET_KEYUP, 0, 2000}, \
196 {KEY_RIGHT, KET_KEYUP, 0, 2000}, {KEY_DOWN, KET_KEYUP, 0, 2000}, \
197 {KEY_POWER, KET_KEYUP, 0, 0}, {KEY_NULL, 0, 0, 0}
201 static keyType_t customerSeq[] = {{KEY_POWER, KET_KEYUP, 0, 0}, {KEY_OK, KET_KEYUP, 0, 2000}, \
202 {KED_UNDEFINEDKEY, KET_KEYUP, 0, 0}, {KEY_NULL, 0, 0, 0}
205 keyType_t personalityResetSeq[] = {{KEY_POWER, KET_KEYUP, 0, 0}, {KEY_OK, KET_KEYUP, 0, 2000}, \
206 {KEY_LEFT, KET_KEYUP, 0, 2000}, {KEY_DOWN, KET_KEYUP, 0, 2000}, \
207 {KEY_POWER, KET_KEYUP, 0, 0}, {KEY_NULL, 0, 0, 0}
211 static const keyVariant_t keyVariants[] = {{ KED_POWER, KED_RF_POWER}, {KED_OK, KED_SELECT}};
214 static KeyMap_t Reset_Rules[MAX_RESET_MODE_COUNT] = {{COLD_FACTORY, &coldFactorySeq[0]},{FACTORY, &factorySeq[0]},{WARE_HOUSE, &wareHouseSeq[0]},{CUSTOMER, &customerSeq[0]}, \
215 {PERSONALITY, &personalityResetSeq[0]}
219 static IARM_Bus_PWRMgr_PowerState_t curPwrState = IARM_BUS_PWRMGR_POWERSTATE_STANDBY;
221 static void storeFocus()
226 static void getSystemTime(
long *syst)
228 struct timespec Stime;
229 clock_gettime(CLOCK_MONOTONIC, &Stime);
230 *syst = ((Stime.tv_sec * 1000) + (Stime.tv_nsec / 1000000));
234 static void releaseFocus()
237 LOG(
"Reset: IARM-Release MEDIA Focus for RI Process\n");
238 IARM_BusDaemon_ReleaseOwnership(IARM_BUS_RESOURCE_FOCUS);
245 static void grabFocus()
249 LOG(
"Reset: IARM-Request MEDIA Focus for Reset Process\n");
250 IARM_BusDaemon_RequestOwnership(IARM_BUS_RESOURCE_FOCUS);
254 static void ResetResetState(
void)
257 for( i = 0; i < MAX_RESET_MODE_COUNT; i++ )
259 stateMachineList[i]->expectedKeyIndex = 0;
264 gKeyReleasedTime = 0;
274 if(gResetMonitoredOnIBus)
277 eventData.data.reset_sequence_progress = -1;
279 (
void *)&eventData,
sizeof(eventData));
280 gResetMonitoredOnIBus =
false;
285 void setResetPowerState(IARM_Bus_PWRMgr_PowerState_t newPwrState)
288 curPwrState = newPwrState;
297 LOG(
"Reset: Blinking LEDs\n");
304 for (uint i = 0; i < fpIndicators.
size(); i++)
312 for (uint i = 0; i < fpIndicators.
size(); i++)
319 for (uint i = 0; i < fpIndicators.
size(); i++)
328 LOG(
"An exception caught during FP LED Blink Operation: %s",e.
getMessage().c_str());
333 int checkResetModeSequence(
int keyType,
int keyCode, resetType_t resetMode)
343 pCurrentState = Reset_Rules[resetMode].pData + stateMachineList[resetMode]->expectedKeyIndex;
345 if ( (
true == gWaitState) && (
true != gIsSameKey))
350 pthread_mutex_lock(&tMutexLock);
351 pthread_cond_signal(&tMutexCond);
352 pthread_mutex_unlock(&tMutexLock);
361 if( (pCurrentState->keyMode ==
keyType) && (pCurrentState->Key == keyCode))
363 if(0 < stateMachineList[resetMode]->expectedKeyIndex)
366 if(gLastResetEvent != stateMachineList[resetMode]->expectedKeyIndex)
368 gResetMonitoredOnIBus =
true;
370 eventData.data.reset_sequence_progress = stateMachineList[resetMode]->expectedKeyIndex;
372 (
void *)&eventData,
sizeof(eventData));
374 LOG(
"\n Reset: sending IARM event %d \n", stateMachineList[resetMode]->expectedKeyIndex);
375 gLastResetEvent = stateMachineList[resetMode]->expectedKeyIndex;
379 stateMachineList[resetMode]->expectedKeyIndex++;
384 pNextState = pCurrentState + 1;
385 if ( KEY_NULL == (pNextState)->Key)
388 stateMachineList[resetMode]->expectedKeyIndex = 0;
390 LOG(
"\n Reset: Action took place for: %d \n",resetMode);
400 printf(
"An exception caught while setting fp text : %s",e.
getMessage().c_str());
403 ret = stateMachineList[resetMode]->resetFuction();
407 if(0 != pCurrentState->validTime)
414 getSystemTime(&gCurrentTime);
415 gTimeOutValue = pCurrentState->validTime;
417 LOG(
"Reset: gTimeOutValue: %d- %d ",gTimeOutValue,gCurrentTime);
425 stateMachineList[resetMode]->expectedKeyIndex = 0;
432 int checkResetSequence(
int keyType,
int keyCode)
443 if (keyCode == v->Variant)
451 if (0 != gCurrentTime)
454 getSystemTime(&gKeyPressedTime);
456 if((gKeyPressedTime - gCurrentTime) > gTimeOutValue)
459 LOG(
"\n Reset: Sorry you crossed the time out value %d \n", gTimeOutValue);
472 if (!inResetMode && ((keyCode == KED_POWER) || (keyCode == KED_RF_POWER) || (keyCode == KED_FACTORY)))
475 getSystemTime(&gKeyPressedTime);
484 if (!inResetMode && ((keyCode == KED_POWER) || (keyCode == KED_RF_POWER) || (keyCode == KED_FACTORY)))
487 getSystemTime(&gKeyReleasedTime);
488 if(gKeyPressedTime == 0)
492 if ((keyCode == KED_POWER) || (keyCode == KED_RF_POWER))
494 #ifdef ENABLE_FACTORY_RESET_VIA_FP_POWER_BUTTON
495 if((gKeyReleasedTime - gKeyPressedTime) >= POWER_KEY_HOLD_TIME_FOR_FACTORY_RESET)
498 LOG(
"\n Reset: Factory reset through long-pressing power key\n");
500 processFactoryReset();
505 if ((gKeyReleasedTime - gKeyPressedTime) > POWER_KEY_HOLD_TIME_FOR_REBOOT)
508 LOG(
"\n Reset: Inside reset mode 1\n");
510 PwrMgr_Reset(curPwrState,
true);
516 else if ((gKeyReleasedTime - gKeyPressedTime) >= POWER_KEY_HOLD_TIME_FOR_RESET)
519 LOG(
"\n Reset: Inside reset mode 2\n");
526 getSystemTime(&gCurrentTime);
527 gTimeOutValue = INITIAL_TIME_OUT_VALUE;
534 for( i = 0; i < MAX_RESET_MODE_COUNT; i++ )
536 stateMachineList[i]->expectedKeyIndex++;
539 LOG(
"\n Reset: FP Power key is pressed for 2 secs");
561 LOG(
"\n Reset: Already in reset mode \n");
563 resetType_t resetMode = COLD_FACTORY;
564 checkResetModeSequence(
keyType, keyCode, resetMode);
567 checkResetModeSequence(
keyType, keyCode, resetMode);
568 #ifndef _DISABLE_RESET_SEQUENCE
569 resetMode = WARE_HOUSE;
570 checkResetModeSequence(
keyType, keyCode, resetMode);
572 static bool indicatedDisable =
false;
573 if (!indicatedDisable) {
574 indicatedDisable =
true;
575 LOG(
"Reset sequence is disabled for Warehouse reset");
578 resetMode = CUSTOMER;
579 checkResetModeSequence(
keyType, keyCode, resetMode);
580 resetMode = PERSONALITY;
581 checkResetModeSequence(
keyType, keyCode, resetMode);
592 if(( 0 == stateMachineList[0]->expectedKeyIndex) && (0 == stateMachineList[1]->expectedKeyIndex) && (0 == stateMachineList[2]->expectedKeyIndex) &&
593 (0 == stateMachineList[3]->expectedKeyIndex) && (0 == stateMachineList[4]->expectedKeyIndex))
596 LOG(
"\n Reset: All statemachines in initial state. So reset the state machines\n");
603 IARM_Result_t initReset()
608 pthread_mutex_init (&tMutexLock, NULL);
609 pthread_cond_init (&tMutexCond, NULL);
611 return IARM_RESULT_SUCCESS;
615 IARM_Result_t unInitReset()
619 gInitialized =
false;
620 pthread_mutex_unlock(&tMutexLock);
621 pthread_mutex_destroy(&tMutexLock);
622 pthread_cond_destroy(&tMutexCond);
624 return IARM_RESULT_SUCCESS;
627 void PwrMgr_Reset(IARM_Bus_PWRMgr_PowerState_t newState,
bool isFPKeyPress)
629 LOG(
"\n PWR_Reset: Rebooting box now .....\r\n");
630 if (newState == IARM_BUS_PWRMGR_POWERSTATE_ON) system(
"sh /togglePower");
634 performReboot(
"PowerMgr_Powerreset",
nullptr,
"Rebooting the box due to ECM connectivity Loss...");
638 performReboot(
"PowerMgr_Powerreset",
nullptr,
"Rebooting the box due to Frontpanel power key pressed for 10 sec...");
642 inline static void check_payload(
const char ** input,
const char * default_arg)
644 if((NULL == *input) || (0 == (*input)[0]))
646 *input = default_arg;
651 void performReboot(
const char * requestor,
const char * reboot_reason_custom,
const char * reboot_reason_other)
653 LOG(
"performReboot: Rebooting box now. Requestor: %s. Reboot reason: %s\n", requestor, reboot_reason_custom);
654 const char * default_arg =
"unknown";
656 check_payload(&requestor, default_arg);
657 check_payload(&reboot_reason_custom, default_arg);
658 check_payload(&reboot_reason_other, default_arg);
662 strncpy(eventData.reboot_reason_custom, reboot_reason_custom,
sizeof(eventData.reboot_reason_custom));
663 strncpy(eventData.reboot_reason_other, reboot_reason_other,
sizeof(eventData.reboot_reason_other));
664 strncpy(eventData.requestor, requestor,
sizeof(eventData.requestor));
665 eventData.reboot_reason_custom[
sizeof(eventData.reboot_reason_custom) - 1] =
'\0';
666 eventData.reboot_reason_other[
sizeof(eventData.reboot_reason_other) - 1] =
'\0';
667 eventData.requestor[
sizeof(eventData.requestor) - 1] =
'\0';
676 char * requestor_cpy = strdup(requestor);
677 char * reboot_reason_custom_cpy = strdup(reboot_reason_custom);
678 char * reboot_reason_other_cpy = strdup(reboot_reason_other);
680 std::thread async_reboot_thread([requestor_cpy, reboot_reason_custom_cpy, reboot_reason_other_cpy] () {
681 v_secure_system(
"echo 0 > /opt/.rebootFlag");
683 if(0 == access(
"/rebootNow.sh", F_OK))
685 v_secure_system(
"/rebootNow.sh -s '%s' -r '%s' -o '%s'", requestor_cpy, reboot_reason_custom_cpy, reboot_reason_other_cpy);
689 v_secure_system(
"/lib/rdk/rebootNow.sh -s '%s' -r '%s' -o '%s'", requestor_cpy, reboot_reason_custom_cpy, reboot_reason_other_cpy);
692 free(reboot_reason_custom_cpy);
693 free(reboot_reason_other_cpy);
695 async_reboot_thread.detach();