void VideoStream::push_data(uint32_t mm_time, uint32_t length, uint8_t* data)
{
    maintenance();
    uint32_t frame_slot = alloc_frame_slot();
    _frames[frame_slot].compressed_data = new uint8_t[length];
    memcpy(_frames[frame_slot].compressed_data, data, length);
    _frames[frame_slot].compressed_data_size = length;
    _frames[frame_slot].mm_time = mm_time ? mm_time : 1;
    maintenance();
}
void VideoStream::handle_update_mark(uint64_t update_mark)
{
    if (!_update_mark || update_mark < _update_mark) {
        return;
    }
    _update_mark = 0;
    maintenance();
}
/*
 *
 * Function: run
 * Description:  
 * Input: 
 * OutPut:
 *      
 * Return:
 * Other:
 *
 */
int CommandServer::run( void )
{
   int iSize;

   if( initShareMemory() == -1 )
   {
      return -1;
   }

   int iRet = makeListener( &m_pstListen, m_pstRoot, m_pstConfig->m_iListenPort,
                            APR_INET, SOCK_STREAM );
   if( iRet == -1 )
   {
      return -1;
   }

   maintenance();

   return 0;
}
void
ApplicationImpl::start()
{
    mDatabase->upgradeToCurrentSchema();

    if (mPersistentState->getState(PersistentState::kForceSCPOnNextLaunch) ==
        "true")
    {
        mConfig.FORCE_SCP = true;
    }

    if (mConfig.NETWORK_PASSPHRASE.empty())
    {
        throw std::invalid_argument("NETWORK_PASSPHRASE not configured");
    }
    if (mConfig.QUORUM_SET.threshold == 0)
    {
        throw std::invalid_argument("Quorum not configured");
    }
    if (!mHerder->isQuorumSetSane(mConfig.QUORUM_SET, !mConfig.UNSAFE_QUORUM))
    {
        std::string err("Invalid QUORUM_SET: duplicate entry or bad threshold "
                        "(should be between ");
        if (mConfig.UNSAFE_QUORUM)
        {
            err = err + "1";
        }
        else
        {
            err = err + "51";
        }
        err = err + " and 100)";
        throw std::invalid_argument(err);
    }

    bool done = false;
    mLedgerManager->loadLastKnownLedger(
        [this, &done](asio::error_code const& ec)
        {
            if (ec)
            {
                throw std::runtime_error("Unable to restore last-known ledger state");
            }

            // restores the SCP state before starting overlay
            mHerder->restoreSCPState();
            // perform maintenance tasks if configured to do so
            // for now, we only perform it when CATCHUP_COMPLETE is not set
            if (mConfig.MAINTENANCE_ON_STARTUP && !mConfig.CATCHUP_COMPLETE)
            {
                maintenance();
            }
            mOverlayManager->start();
            auto npub = mHistoryManager->publishQueuedHistory();
            if (npub != 0)
            {
                CLOG(INFO, "Ledger") << "Restarted publishing " << npub
                                     << " queued snapshots";
            }
            if (mConfig.FORCE_SCP)
            {
                std::string flagClearedMsg = "";
                if (mPersistentState->getState(
                        PersistentState::kForceSCPOnNextLaunch) == "true")
                {
                    flagClearedMsg = " (`force scp` flag cleared in the db)";
                    mPersistentState->setState(
                        PersistentState::kForceSCPOnNextLaunch, "false");
                }

                LOG(INFO) << "* ";
                LOG(INFO) << "* Force-starting scp from the current db state."
                          << flagClearedMsg;
                LOG(INFO) << "* ";

                mHerder->bootstrap();
            }
            done = true;
        });

    while (!done)
    {
        mVirtualClock.crank(true);
    }
}
/*
 * State Machine Procedure
 */
void sm_run(void) {

    /* 
     * STANDBY STATE 
     * The system starts by this state and wait command to Turn on (b_On = 1)
     */
    if (us_State == 1U) 
    {
        /* If system is ON */
        if (b_On == 1U) 
        {
            /* Change State to IBIT */
            us_State = 2U;
        }
        /* If emergency active */
        if (b_Emergency == 1U) 
        {
            /* Show Emergency Alert */
            /* (" --EMERGENCY ACTIVATED-- ") */
        }
    }
        /* 
         * The system check the state
         */
    else 
    {
        /* 
         * OPERATIONAL STATE
         */
        if (us_State == 7U) 
        {
            /* If operational ON */
            if (b_Operational == 1U) 
            {
                /* Call OPERATIONAL state */
                operational(b_Fail);
            } 
            else 
            {
                /* Call to Close Panel */
                shutdown(b_Fail);

                /* If fail is active */
                if (b_Fail == 1U) 
                {
                    /* Return to Fail state */
                    us_State = 3U;
                } 
                else 
                {
                    /* Return to READY state */
                    us_State = 4U;
                }
            }

            /* Call Health Monitoring
             * return: state if changed */
            us_State = powerMonitoring(us_State);
        }
            /*
             * BIT STATE 
             */
        else if (us_State == 2U) 
        {
            /* Call Built-in Test */
            us_State = bit();

            /* Call Health Monitoring
             * return: state if changed */
            us_State = powerMonitoring(us_State);
        }
            /*
             * FAIL STATE 
             */
        else if (us_State == 3U) 
        {
            /* Alert system to FAIL */
            b_Fail = 1U;

            /* If system is ON */
            if (b_On == 0U) 
            {
                /*Change State to STANDBY*/
                us_State = 1U;
            }

            if (b_Operational == 1U) 
            {
                /* Change State to OPERATIONAL REDUCED */
                us_State = 7U;

                /* Initialize operational variables */
                operational_init();
            }

            /* Call Health Monitoring
             * return: state if changed */
            us_State = powerMonitoring(us_State);
        }
            /*
             * READY STATE 
             */
        else if (us_State == 4U) 
        {
            /* If system is ON */
            if (b_On == 0U) 
            {
                /*Change State to STANDBY*/
                us_State = 1U;
            }

            if (b_Maintenance == 1U) 
            {
                /*Change State to MAINTENANCE*/
                us_State = 6U;
            }

            if (b_Operational == 1U) 
            {
                /*Change State to Operational*/
                us_State = 7U;

                /* Initialize operational variables */
                operational_init();
            }

            /* Call Health Monitoring
             * return: state if changed */
            us_State = powerMonitoring(us_State);
        }
            /*
             * MAINTENANCE STATE 
             */
        else if (us_State == 6U) 
        {
            /* If maintenance active */
            if (b_Maintenance == 0U) 
            {
                /* Call to Close Panel */
                shutdown(b_Fail);

                /* Change State to READY */
                us_State = 4U;
            } 
            else {
                /* State MAINTENANCE */
                maintenance();
            }

            /* Call Health Monitoring
             * return: state if changed */
            us_State = powerMonitoring(us_State);
        } else 
        {
            /*
             * EMERGENCY STATE 
             */
            if (us_State == 9U) 
            {
                /* System Turn OFF */
                b_On = 0U;

                /* Set FAIL Alert to OFF */
                b_Fail = 0U;

                /* Change state to STANDBY */
                us_State = 1U;

                /* Emergency Active */
                b_Emergency = 1U;

                /* Operational OFF */
                b_Operational = 0U;

                /* Shutdown System Safely */
                shutdown(b_Fail);
            }
        }
    }
}