# HG changeset patch # User Merten Sach # Date 1331233560 -3600 # Node ID 826448e34e80a5396d5e98bc336219c291d2618a # Parent 29d8b41926f0583d82028c3cf5d069fd26e67219 added data to message handler diff -r 29d8b41926f0 -r 826448e34e80 LossyCom.c --- a/LossyCom.c Wed Mar 07 19:46:37 2012 +0100 +++ b/LossyCom.c Thu Mar 08 20:06:00 2012 +0100 @@ -31,17 +31,28 @@ return exchange; } +/* + * Connects the local endpoint to the central exchange structure. + * This registers a message handler that handles all the incomming messages. + * Also an endpointID is set this ID has to be between 0 and total number of + * endpoints-1 and the number has to be unique. + */ void lossyCom__initialize_endpoint(lossyCom__endpoint_t* localEndpoint, lossyCom__exchange_t* centralExchange, lossyCom__endpointID_t endpointID, - lossyCom__msgHandler* msgHandler) + lossyCom__msgHandler msgHandler, + void* msgHandlerData) { localEndpoint->localTriggerCopy = 0; localEndpoint->endpointID = endpointID; - localEndpoint->centralExchangePtr = centralExchange; - localEndpoint->msgHandler = msgHandler; + localEndpoint->centralExchange = centralExchange; + localEndpoint->msgHandler = msgHandler; + localEndpoint->msgHandlerData = msgHandlerData; } +/* + * This broadcasts a message to all connected receivers + */ void inline lossyCom__broadcastMsg(lossyCom__endpoint_t* localEndpoint, lossyCom__msgBody_t msg) { @@ -50,6 +61,10 @@ msg); } +/* + * This sends a message another endpoint. Again it is not guaranteed that the + * message is received. But in most cases it will. + */ void inline lossyCom__sendMsg(lossyCom__endpoint_t* localEndpoint, lossyCom__endpointID_t receiverEndpointID, lossyCom__msgBody_t msg) @@ -57,45 +72,52 @@ lossyCom__msg_t msgDraft; uint16_t triggerCopy; + //build message msgDraft = (0 | msg); msgDraft |= ((lossyCom__msg_t)receiverEndpointID << ENDPOINT_ID_SHIFT); - triggerCopy = localEndpoint->centralExchangePtr->triggerCounter +1; + triggerCopy = localEndpoint->centralExchange->triggerCounter +1; msgDraft |= ((lossyCom__msg_t)triggerCopy << TRIGGER_SHIFT); //write msg to central exchange - localEndpoint->centralExchangePtr->outboxArray[localEndpoint->endpointID] = + localEndpoint->centralExchange->outboxArray[localEndpoint->endpointID] = msgDraft; - localEndpoint->centralExchangePtr->triggerCounter = triggerCopy; + localEndpoint->centralExchange->triggerCounter = triggerCopy; } void inline lossyCom__receiveMsg(lossyCom__endpoint_t* localEndpoint) { - uint16_t remoteTriggerCopy; + uint16_t currentTriggerCopy; lossyCom__endpointID_t senderEndpointID; lossyCom__msg_t msgCopy; uint16_t msgTrigger; lossyCom__msgBody_t msgBody; senderEndpointID = 0; - remoteTriggerCopy = localEndpoint->centralExchangePtr->triggerCounter; + currentTriggerCopy = localEndpoint->centralExchange->triggerCounter; + //new message arrived if trigger counter is higher than the last time read - while(senderEndpointID < localEndpoint->centralExchangePtr->numEndpoints) + while(senderEndpointID < localEndpoint->centralExchange->numEndpoints) { if(senderEndpointID != localEndpoint->endpointID) { - msgCopy = localEndpoint->centralExchangePtr->outboxArray[senderEndpointID]; + msgCopy = localEndpoint->centralExchange->outboxArray[senderEndpointID]; msgTrigger = 0xFFFF & (msgCopy >> TRIGGER_SHIFT); + // check if the message is new (msg trigger > archived trigger) + // and already valid (msgTrigger <= currentTriggerCopy) if(msgTrigger > localEndpoint->localTriggerCopy && - msgTrigger <= remoteTriggerCopy) + msgTrigger <= currentTriggerCopy) { + //let the message handler parse the message msgBody = 0xFFFFFFFF & msgCopy; - (*(localEndpoint->msgHandler))(senderEndpointID, msgBody); + (*(localEndpoint->msgHandler))(senderEndpointID, + msgBody, + localEndpoint->msgHandlerData); } } senderEndpointID++; } //save last parsed msg - localEndpoint->localTriggerCopy = remoteTriggerCopy; + localEndpoint->localTriggerCopy = currentTriggerCopy; } \ No newline at end of file diff -r 29d8b41926f0 -r 826448e34e80 LossyCom.h --- a/LossyCom.h Wed Mar 07 19:46:37 2012 +0100 +++ b/LossyCom.h Thu Mar 08 20:06:00 2012 +0100 @@ -21,23 +21,27 @@ #include - +/*********************************** + * General Defines + ***********************************/ +//never use this number as a endpoint! +#define BROADCAST_ID 65535 /*********************************** * Type Definitions ***********************************/ -#define BROADCAST_ID 65535 -#define ENDPOINT_ID_SHIFT 32 -#define TRIGGER_SHIFT 48 - /* * A message consists of the - * 16bit saved trigger value when the message was sent + * 16bit saved trigger value +1 when the message was sent * 16bit receiver identifier, this is the index of the slot in the array * 65536 is the broadcast identifier * 32bit message body, the message format is defined by the endpoints + * + * | 16bit trigger value | 16bit receiverID | 32bit message body | */ +#define ENDPOINT_ID_SHIFT 32 +#define TRIGGER_SHIFT 48 typedef uint64_t lossyCom__msg_t; typedef uint32_t lossyCom__msgBody_t; typedef uint16_t lossyCom__endpointID_t; @@ -47,7 +51,7 @@ * Example: * void handler_func(lossyCom__endpointID_t senderID, lossyCom__msgBody_t msg); */ -typedef void (*lossyCom__msgHandler) (lossyCom__endpointID_t, lossyCom__msgBody_t); +typedef void (*lossyCom__msgHandler) (lossyCom__endpointID_t, lossyCom__msgBody_t, void*); /* * Central communication structure. @@ -64,8 +68,9 @@ typedef struct { uint16_t localTriggerCopy; lossyCom__endpointID_t endpointID; - lossyCom__exchange_t* centralExchangePtr; - lossyCom__msgHandler* msgHandler; + lossyCom__exchange_t* centralExchange; + lossyCom__msgHandler msgHandler; + void* msgHandlerData; } lossyCom__endpoint_t; /*********************************** @@ -77,7 +82,8 @@ void lossyCom__initialize_endpoint(lossyCom__endpoint_t* localEndpoint, lossyCom__exchange_t* exchange, lossyCom__endpointID_t endpointID, - lossyCom__msgHandler* msgHandler); + lossyCom__msgHandler msgHandler, + void* msgHandlerData); void lossyCom__broadcastMsg(lossyCom__endpoint_t* localEndpoint, lossyCom__msgBody_t msg);