bool AiaDeleteAlert()

in ports/Storage/src/aia_storage_config.c [310:387]


bool AiaDeleteAlert( const char* alertToken, size_t alertTokenLen )
{
    if( !alertToken )
    {
        AiaLogError( "Null alertToken" );
        return false;
    }
    if( alertTokenLen != AIA_ALERT_TOKEN_CHARS )
    {
        AiaLogError( "Invalid alert token length" );
        return false;
    }

    size_t allAlertsBytes = AiaGetAlertsSize();
    size_t bytePosition;
    bool deletingExistingAlert = false;

    /**
     * Load all alerts from persistent storage.
     */
    uint8_t* allAlertsBuffer = AiaCalloc( 1, allAlertsBytes );
    if( !allAlertsBuffer )
    {
        AiaLogError( "AiaCalloc failed, bytes=%zu.", allAlertsBytes );
        return false;
    }

    if( !AiaLoadAlerts( allAlertsBuffer, allAlertsBytes ) )
    {
        AiaLogError( "AiaLoadBlob failed" );
        AiaFree( allAlertsBuffer );
        return false;
    }

    /** Go through the tokens to find the first empty or matching one */
    for( bytePosition = 0; bytePosition < allAlertsBytes;
         bytePosition += AIA_SIZE_OF_ALERT_IN_BYTES )
    {
        if( '\0' == allAlertsBuffer[ bytePosition ] )
        {
            /** Found an empty token */
            break;
        }
        else
        {
            /** Check if this token matches with what we are trying to delete */
            if( !strncmp( (const char*)allAlertsBuffer + bytePosition,
                          alertToken, alertTokenLen ) )
            {
                uint8_t* moveDst = allAlertsBuffer + bytePosition;
                uint8_t* moveSrc =
                    allAlertsBuffer + bytePosition + AIA_SIZE_OF_ALERT_IN_BYTES;
                size_t moveBytes =
                    allAlertsBytes -
                    ( bytePosition + AIA_SIZE_OF_ALERT_IN_BYTES );
                memmove( moveDst, moveSrc, moveBytes );
                deletingExistingAlert = true;
                break;
            }
        }
    }

    /** Store the new blob in persistent storage */
    size_t storeSize =
        ( deletingExistingAlert ? allAlertsBytes - AIA_SIZE_OF_ALERT_IN_BYTES
                                : allAlertsBytes );
    /** Store the new blob */
    if( !AiaStoreBlob( AIA_ALL_ALERTS_STORAGE_KEY_V0, allAlertsBuffer,
                       storeSize ) )
    {
        AiaLogError( "AiaStoreBlob failed" );
        AiaFree( allAlertsBuffer );
        return false;
    }

    AiaFree( allAlertsBuffer );
    return true;
}