
/*
 * For a detailed description see header file.
 */

#include "LossyCom.h"

#include "VMS_Implementations/VMS_impl/vmalloc.h"

/*
 * Initializes the central exchange structure.
 * Allocates memory to fit the number of endpoints.
 * Returns NULL if an error occurs.
 */
lossyCom__exchange_t* lossyCom__initialize(uint16_t numEndpoints)
{
    lossyCom__exchange_t* exchange;
    
    exchange = VMS_WL__malloc(sizeof(lossyCom__exchange_t));
    if(exchange == NULL)
        return NULL;
    
    exchange->triggerCounter = 0;
    exchange->numEndpoints = numEndpoints;
    exchange->outboxArray = VMS_WL__malloc(sizeof(lossyCom__msg_t)*numEndpoints);
    if(exchange->outboxArray == NULL){
        VMS_WL__free(exchange);
        return NULL;
    }
    
    return exchange;    
}

void lossyCom__initialize_endpoint(lossyCom__endpoint_t* localEndpoint,
                                   lossyCom__exchange_t* centralExchange,
                                   lossyCom__endpointID_t endpointID,
                                   lossyCom__msgHandler* msgHandler)
{
    localEndpoint->localTriggerCopy = 0;
    localEndpoint->endpointID = endpointID;
    localEndpoint->centralExchangePtr = centralExchange;
    localEndpoint->msgHandler = msgHandler;    
}

void inline lossyCom__broadcastMsg(lossyCom__endpoint_t* localEndpoint,
                                   lossyCom__msgBody_t msg)
{
    lossyCom__sendMsg(localEndpoint,
                      BROADCAST_ID,
                      msg);
}

void inline lossyCom__sendMsg(lossyCom__endpoint_t* localEndpoint,
                       lossyCom__endpointID_t receiverEndpointID,
                       lossyCom__msgBody_t msg)
{
    lossyCom__msg_t msgDraft;
    uint16_t triggerCopy;
    
    msgDraft = (0 | msg);
    msgDraft |= ((lossyCom__msg_t)receiverEndpointID << ENDPOINT_ID_SHIFT);
    
    triggerCopy = localEndpoint->centralExchangePtr->triggerCounter +1;
    msgDraft |= ((lossyCom__msg_t)triggerCopy << TRIGGER_SHIFT);
    
    //write msg to central exchange
    localEndpoint->centralExchangePtr->outboxArray[localEndpoint->endpointID] =
            msgDraft;
    
    localEndpoint->centralExchangePtr->triggerCounter = triggerCopy;
}

void inline lossyCom__receiveMsg(lossyCom__endpoint_t* localEndpoint)
{
    uint16_t remoteTriggerCopy;
    lossyCom__endpointID_t senderEndpointID;
    lossyCom__msg_t msgCopy;
    uint16_t msgTrigger;
    lossyCom__msgBody_t msgBody;
    
    senderEndpointID = 0;
    remoteTriggerCopy = localEndpoint->centralExchangePtr->triggerCounter;
    //new message arrived if trigger counter is higher than the last time read
    while(senderEndpointID < localEndpoint->centralExchangePtr->numEndpoints)
    {
        if(senderEndpointID != localEndpoint->endpointID)
        {
            msgCopy = localEndpoint->centralExchangePtr->outboxArray[senderEndpointID];
            msgTrigger = 0xFFFF & (msgCopy >> TRIGGER_SHIFT);
            if(msgTrigger > localEndpoint->localTriggerCopy &&
                    msgTrigger <= remoteTriggerCopy)
            {
                msgBody = 0xFFFFFFFF & msgCopy;
                (*(localEndpoint->msgHandler))(senderEndpointID, msgBody);
            }
        }
        senderEndpointID++;
    }
    //save last parsed msg
    localEndpoint->localTriggerCopy = remoteTriggerCopy;
}