view LossyCom.h @ 1:826448e34e80

added data to message handler
author Merten Sach <msach@mailbox.tu-berlin.de>
date Thu, 08 Mar 2012 20:06:00 +0100
parents 29d8b41926f0
children b6dd31dbab8c
line source
1 /*
2 * This describes a best effort communication channel. Mainly designed for
3 * communication between Core-Controllers, so they can inform each other about
4 * their status and pass work around. This is to avoid idling cores that occupy
5 * the Masterlock.
6 *
7 * The communication entities poll a central trigger counter that increases when
8 * there are new messages. However it's not guaranteed that the increase of the
9 * counter reflects the number of new messages.
10 *
11 * The messages consist of a single 64bit word so it can be written in one
12 * operation and therefore avoiding inconsistent messages.
13 *
14 * The messages are handled with a handler function provided by the receiver
15 * when acquiring the communication endpoint.
16 *
17 * The message passing is handled by a central struct that is used w/o any lock.
18 * It consists of a array messages for the number of communicating entities. The
19 * number of entities is fixed at initialization.
20 */
22 #include <stdint.h>
24 /***********************************
25 * General Defines
26 ***********************************/
27 //never use this number as a endpoint!
28 #define BROADCAST_ID 65535
30 /***********************************
31 * Type Definitions
32 ***********************************/
34 /*
35 * A message consists of the
36 * 16bit saved trigger value +1 when the message was sent
37 * 16bit receiver identifier, this is the index of the slot in the array
38 * 65536 is the broadcast identifier
39 * 32bit message body, the message format is defined by the endpoints
40 *
41 * | 16bit trigger value | 16bit receiverID | 32bit message body |
42 */
43 #define ENDPOINT_ID_SHIFT 32
44 #define TRIGGER_SHIFT 48
45 typedef uint64_t lossyCom__msg_t;
46 typedef uint32_t lossyCom__msgBody_t;
47 typedef uint16_t lossyCom__endpointID_t;
49 /*
50 * Message Handler that has two arguments the sender endpoint ID and the Msg
51 * Example:
52 * void handler_func(lossyCom__endpointID_t senderID, lossyCom__msgBody_t msg);
53 */
54 typedef void (*lossyCom__msgHandler) (lossyCom__endpointID_t, lossyCom__msgBody_t, void*);
56 /*
57 * Central communication structure.
58 */
59 typedef struct{
60 uint16_t triggerCounter;
61 uint16_t numEndpoints;
62 lossyCom__msg_t* outboxArray;
63 }lossyCom__exchange_t;
65 /*
66 * Endpoint data structure.
67 */
68 typedef struct {
69 uint16_t localTriggerCopy;
70 lossyCom__endpointID_t endpointID;
71 lossyCom__exchange_t* centralExchange;
72 lossyCom__msgHandler msgHandler;
73 void* msgHandlerData;
74 } lossyCom__endpoint_t;
76 /***********************************
77 * Function Declarations
78 ***********************************/
80 lossyCom__exchange_t* lossyCom__initialize(uint16_t numEndpoints);
82 void lossyCom__initialize_endpoint(lossyCom__endpoint_t* localEndpoint,
83 lossyCom__exchange_t* exchange,
84 lossyCom__endpointID_t endpointID,
85 lossyCom__msgHandler msgHandler,
86 void* msgHandlerData);
88 void lossyCom__broadcastMsg(lossyCom__endpoint_t* localEndpoint,
89 lossyCom__msgBody_t msg);
91 void lossyCom__sendMsg(lossyCom__endpoint_t* localEndpoint,
92 lossyCom__endpointID_t receiverEndpointID,
93 lossyCom__msgBody_t msg);
95 void lossyCom__receiveMsg(lossyCom__endpoint_t* localEndpoint);