src/ncp/ncp_base.cpp
otError NcpBase::HandlePropertySet_SPINEL_PROP_HOST_POWER_STATE(uint8_t aHeader)
{
    uint8_t powerState;
    otError error = OT_ERROR_NONE;

    error = mDecoder.ReadUint8(powerState);

    if (error == OT_ERROR_NONE)
    {
        switch (powerState)
        {
        case SPINEL_HOST_POWER_STATE_OFFLINE:
        case SPINEL_HOST_POWER_STATE_DEEP_SLEEP:
        case SPINEL_HOST_POWER_STATE_LOW_POWER:
        case SPINEL_HOST_POWER_STATE_ONLINE:
            // Adopt the requested power state.
            mHostPowerState = static_cast<spinel_host_power_state_t>(powerState);
            break;

        case SPINEL_HOST_POWER_STATE_RESERVED:
            // Per the specification, treat this as synonymous with SPINEL_HOST_POWER_STATE_DEEP_SLEEP.
            mHostPowerState = SPINEL_HOST_POWER_STATE_DEEP_SLEEP;
            break;

        default:
            // Per the specification, treat unrecognized values as synonymous with SPINEL_HOST_POWER_STATE_LOW_POWER.
            mHostPowerState = SPINEL_HOST_POWER_STATE_LOW_POWER;
            break;
        }

        mHostPowerStateHeader = 0;

        error = WritePropertyValueIsFrame(aHeader, SPINEL_PROP_HOST_POWER_STATE);

        if (mHostPowerState != SPINEL_HOST_POWER_STATE_ONLINE)
        {
            if (error == OT_ERROR_NONE)
            {
                mHostPowerReplyFrameTag = GetLastOutboundFrameTag();
            }
            else
            {
                mHostPowerReplyFrameTag = Spinel::Buffer::kInvalidTag;
            }

            mHostPowerStateInProgress = true;
        }

        if (error != OT_ERROR_NONE)
        {
            mHostPowerStateHeader = aHeader;

            // The reply will be queued when buffer space becomes available
            // in the NCP tx buffer so we return `success` to avoid sending a
            // NOMEM status for the same tid through `mDroppedReplyTid` list.

            error = OT_ERROR_NONE;
        }
    }
    else
    {
        error = WriteLastStatusFrame(aHeader, ThreadErrorToSpinelStatus(error));
    }

    return error;
}
// Setting `HOST_POWER_STATE` is treated and implemented differently from other
// handlers as it requires two special behaviors (a) the response frame for the
// set operation should be tracked and only when it is delivered we can assume
// that host is sleep (b) the response is critical so if there is no spinel
// buffer to prepare the response, the current spinel header is saved to
// prepare and send the response as soon as buffer space becomes available.
Setting `HOST_POWER_STATE` is treated and implemented differently from other
handlers as it requires two special behaviors (a) the response frame for the
set operation should be tracked and only when it is delivered we can assume
that host is sleep (b) the response is critical so if there is no spinel
buffer to prepare the response, the current spinel header is saved to
prepare and send the response as soon as buffer space becomes available.
Setting ` HOST_POWER_STATE ` is treated and implemented differently from other handlers as it requires two special behaviors ( a ) the response frame for the set operation should be tracked and only when it is delivered we can assume that host is sleep ( b ) the response is critical so if there is no spinel buffer to prepare the response the current spinel header is saved to prepare and send the response as soon as buffer space becomes available .
otError NcpBase::HandlePropertySet_SPINEL_PROP_HOST_POWER_STATE(uint8_t aHeader)
{
    uint8_t powerState;
    otError error = OT_ERROR_NONE;
    error = mDecoder.ReadUint8(powerState);
    if (error == OT_ERROR_NONE)
    {
        switch (powerState)
        {
        case SPINEL_HOST_POWER_STATE_OFFLINE:
        case SPINEL_HOST_POWER_STATE_DEEP_SLEEP:
        case SPINEL_HOST_POWER_STATE_LOW_POWER:
        case SPINEL_HOST_POWER_STATE_ONLINE:
            mHostPowerState = static_cast<spinel_host_power_state_t>(powerState);
            break;
        case SPINEL_HOST_POWER_STATE_RESERVED:
            mHostPowerState = SPINEL_HOST_POWER_STATE_DEEP_SLEEP;
            break;
        default:
            mHostPowerState = SPINEL_HOST_POWER_STATE_LOW_POWER;
            break;
        }
        mHostPowerStateHeader = 0;
        error = WritePropertyValueIsFrame(aHeader, SPINEL_PROP_HOST_POWER_STATE);
        if (mHostPowerState != SPINEL_HOST_POWER_STATE_ONLINE)
        {
            if (error == OT_ERROR_NONE)
            {
                mHostPowerReplyFrameTag = GetLastOutboundFrameTag();
            }
            else
            {
                mHostPowerReplyFrameTag = Spinel::Buffer::kInvalidTag;
            }
            mHostPowerStateInProgress = true;
        }
        if (error != OT_ERROR_NONE)
        {
            mHostPowerStateHeader = aHeader;
            error = OT_ERROR_NONE;
        }
    }
    else
    {
        error = WriteLastStatusFrame(aHeader, ThreadErrorToSpinelStatus(error));
    }
    return error;
}
otError NcpBase :: HandlePropertySet_SPINEL_PROP_HOST_POWER_STATE ( uint8_t aHeader ) { uint8_t powerState ; otError error = OT_ERROR_NONE ; error = mDecoder . ReadUint8 ( powerState ) ; if ( error == OT_ERROR_NONE ) { switch ( powerState ) { case SPINEL_HOST_POWER_STATE_OFFLINE : case SPINEL_HOST_POWER_STATE_DEEP_SLEEP : case SPINEL_HOST_POWER_STATE_LOW_POWER : case SPINEL_HOST_POWER_STATE_ONLINE : mHostPowerState = static_cast < spinel_host_power_state_t > ( powerState ) ; break ; case SPINEL_HOST_POWER_STATE_RESERVED : mHostPowerState = SPINEL_HOST_POWER_STATE_DEEP_SLEEP ; break ; default : mHostPowerState = SPINEL_HOST_POWER_STATE_LOW_POWER ; break ; } mHostPowerStateHeader = 0 ; error = WritePropertyValueIsFrame ( aHeader , SPINEL_PROP_HOST_POWER_STATE ) ; if ( mHostPowerState != SPINEL_HOST_POWER_STATE_ONLINE ) { if ( error == OT_ERROR_NONE ) { mHostPowerReplyFrameTag = GetLastOutboundFrameTag ( ) ; } else { mHostPowerReplyFrameTag = Spinel :: Buffer :: kInvalidTag ; } mHostPowerStateInProgress = true ; } if ( error != OT_ERROR_NONE ) { mHostPowerStateHeader = aHeader ; error = OT_ERROR_NONE ; } } else { error = WriteLastStatusFrame ( aHeader , ThreadErrorToSpinelStatus ( error ) ) ; } return error ; }
Setting `HOST_POWER_STATE` is treated and implemented differently from other
handlers as it requires two special behaviors (a) the response frame for the
set operation should be tracked and only when it is delivered we can assume
that host is sleep (b) the response is critical so if there is no spinel
buffer to prepare the response, the current spinel header is saved to
prepare and send the response as soon as buffer space becomes available.
Setting ` HOST_POWER_STATE ` is treated and implemented differently from other handlers as it requires two special behaviors ( a ) the response frame for the set operation should be tracked and only when it is delivered we can assume that host is sleep ( b ) the response is critical so if there is no spinel buffer to prepare the response the current spinel header is saved to prepare and send the response as soon as buffer space becomes available .
// Adopt the requested power state. // Per the specification, treat this as synonymous with SPINEL_HOST_POWER_STATE_DEEP_SLEEP. // Per the specification, treat unrecognized values as synonymous with SPINEL_HOST_POWER_STATE_LOW_POWER. // The reply will be queued when buffer space becomes available // in the NCP tx buffer so we return `success` to avoid sending a // NOMEM status for the same tid through `mDroppedReplyTid` list.