/**
 * @file mac_orphan.c
 *
 * @brief Implements orphan scan related functionalities on the parent side.
 *
 * $Id: mac_orphan.c 21503 2010-04-13 08:08:15Z sschneid $
 *
 * @author    Atmel Corporation: http://www.atmel.com
 * @author    Support email: avr@atmel.com
 */
/*
 * Copyright (c) 2009, Atmel Corporation All rights reserved.
 *
 * Licensed under Atmel's Limited License Agreement --> EULA.txt
 */

/* === Includes ============================================================ */

#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include "pal.h"
#include "return_val.h"
#include "bmm.h"
#include "qmm.h"
#include "tal.h"
#include "ieee_const.h"
#include "mac_msg_const.h"
#include "mac_api.h"
#include "mac_msg_types.h"
#include "mac_data_structures.h"
#include "stack_config.h"
#include "mac_internal.h"
#include "mac.h"
#include "mac_build_config.h"

/* === Macros  ============================================================= */


/* === Globals ============================================================= */


/* === Prototypes ========================================================== */


/* === Implementation ====================================================== */

#if (MAC_ORPHAN_INDICATION_RESPONSE == 1)
/**
 * @brief Handles an orphan notification
 *
 * This function processes an incoming orphan notification command.
 * A PAN coordinator gets to this function through a TAL data indication
 * message.
 *
 * @param msg Frame reception buffer
 */
void mac_process_orphan_notification(buffer_t *msg)
{
    mlme_orphan_ind_t *moi = (mlme_orphan_ind_t *)BMM_BUFFER_POINTER(msg);

    moi->cmdcode = MLME_ORPHAN_INDICATION;
    ADDR_COPY_DST_SRC_64(moi->OrphanAddress, mac_parse_data.src_addr.long_address);

    /* Append the MLME orphan indication message to MAC-NHLE queue */
    qmm_queue_append(&mac_nhle_q, msg);
}
#endif /* MAC_ORPHAN_INDICATION_RESPONSE */



#if (MAC_ORPHAN_INDICATION_RESPONSE == 1)
/**
 * @brief Implements the MLME-ORPHAN.response
 *
 * The MLME-ORPHAN.response primitive allows the next higher layer
 * of a coordinator to respond to the MLME-ORPHAN.indication primitive.
 * The MLME-ORPHAN.response primitive is generated by the next higher
 * layer and issued to its MLME when it reaches a decision about whether
 * the orphaned device indicated in the MLME-ORPHAN.indication primitive
 * is associated.
 *
 * @param m Pointer to the message.
 */
void mlme_orphan_response(uint8_t *m)
{
    bool transmission_status = mac_tx_coord_realignment_command(ORPHANREALIGNMENT,
                                                           (buffer_t *)m,
                                                           tal_pib_PANId,
                                                           tal_pib_CurrentChannel,
                                                           tal_pib_CurrentPage);

    if (!transmission_status)
    {
        mac_mlme_comm_status(MAC_CHANNEL_ACCESS_FAILURE,
                             (buffer_t *)m);
    }
}
#endif /* MAC_ORPHAN_INDICATION_RESPONSE */

/* EOF */
