svn update: 60286 (latest:60286)
[profile/ivi/ecore.git] / src / lib / ecore_ipc / Ecore_Ipc.h
1 #ifndef _ECORE_IPC_H
2 #define _ECORE_IPC_H
3
4 #ifdef EAPI
5 # undef EAPI
6 #endif
7
8 #ifdef _WIN32
9 # ifdef EFL_ECORE_IPC_BUILD
10 #  ifdef DLL_EXPORT
11 #   define EAPI __declspec(dllexport)
12 #  else
13 #   define EAPI
14 #  endif
15 # else
16 #  define EAPI __declspec(dllimport)
17 # endif
18 #else
19 # ifdef __GNUC__
20 #  if __GNUC__ >= 4
21 #   define EAPI __attribute__ ((visibility("default")))
22 #  else
23 #   define EAPI
24 #  endif
25 # else
26 #  define EAPI
27 # endif
28 #endif
29
30 /**
31  * @file Ecore_Ipc.h
32  * @brief Ecore inter-process communication functions.
33  */
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38
39 typedef struct _Ecore_Ipc_Server Ecore_Ipc_Server; /**< An IPC connection handle */
40 typedef struct _Ecore_Ipc_Client Ecore_Ipc_Client; /**< An IPC connection handle */
41
42 /**
43  * Macros used for generic data packing
44  */
45 EAPI unsigned short     _ecore_ipc_swap_16(unsigned short v);
46 EAPI unsigned int       _ecore_ipc_swap_32(unsigned int v);
47 EAPI unsigned long long _ecore_ipc_swap_64(unsigned long long v);
48
49 #ifdef WORDS_BIGENDIAN
50 #define ECORE_IPC_SWAP2NET64(x) _ecore_ipc_swap_64(x)
51 #define ECORE_IPC_SWAP2CPU64(x) _ecore_ipc_swap_64(x)
52 #define ECORE_IPC_SWAP2NET32(x) _ecore_ipc_swap_32(x)
53 #define ECORE_IPC_SWAP2CPU32(x) _ecore_ipc_swap_32(x)
54 #define ECORE_IPC_SWAP2NET16(x) _ecore_ipc_swap_16(x)
55 #define ECORE_IPC_SWAP2CPU16(x) _ecore_ipc_swap_16(x)
56 #define ECORE_IPC_SWAP2NET8(x) (x)
57 #define ECORE_IPC_SWAP2CPU8(x) (x)
58 #else
59 #define ECORE_IPC_SWAP2NET64(x) (x)
60 #define ECORE_IPC_SWAP2CPU64(x) (x)
61 #define ECORE_IPC_SWAP2NET32(x) (x)
62 #define ECORE_IPC_SWAP2CPU32(x) (x)
63 #define ECORE_IPC_SWAP2NET16(x) (x)
64 #define ECORE_IPC_SWAP2CPU16(x) (x)
65 #define ECORE_IPC_SWAP2NET8(x) (x)
66 #define ECORE_IPC_SWAP2CPU8(x) (x)
67 #endif
68
69 /* 1, 2, 4 and 8 byte datatypes */
70 /* unpacking */
71 #define ECORE_IPC_GET64(v)\
72     { \
73         p->v = ECORE_IPC_SWAP2CPU64(*(long long *)(ptr)); \
74         ptr += 8; \
75     }
76 #define ECORE_IPC_GET32(v)\
77     { \
78         p->v = ECORE_IPC_SWAP2CPU32(*(int *)(ptr)); \
79         ptr += 4; \
80     }
81 #define ECORE_IPC_GET16(v)\
82     { \
83         p->v = ECORE_IPC_SWAP2CPU16(*(short *)(ptr)); \
84         ptr += 2; \
85     }
86 #define ECORE_IPC_GET8(v) \
87     { \
88         p->v = ECORE_IPC_SWAP2CPU8(*(char *)(ptr)); \
89         ptr += 1; \
90     }
91 /* packing */
92 #define ECORE_IPC_PUT64(v)\
93     { \
94         *(long long *)(ptr) = ECORE_IPC_SWAP2NET64(p->v); \
95         ptr += 8; \
96     }
97 #define ECORE_IPC_PUT32(v)\
98     { \
99         *(int *)(ptr) = ECORE_IPC_SWAP2NET32(p->v); \
100         ptr += 4; \
101     }
102 #define ECORE_IPC_PUT16(v)\
103     { \
104         *(short *)(ptr) = ECORE_IPC_SWAP2NET16(p->v); \
105         ptr += 2; \
106     }
107 #define ECORE_IPC_PUT8(v) \
108     { \
109         *(char *)(ptr) = ECORE_IPC_SWAP2NET8(p->v); \
110         ptr += 1; \
111     }
112 /* padding data */
113 #define ECORE_IPC_PAD8()   ptr += 1
114 #define ECORE_IPC_PAD16()  ptr += 2
115 #define ECORE_IPC_PAD32()  ptr += 4
116 #define ECORE_IPC_PAD64()  ptr += 8
117
118 /* counting data when encoding lists */
119 #define ECORE_IPC_CNT8()    len += 1
120 #define ECORE_IPC_CNT16()   len += 2
121 #define ECORE_IPC_CNT32()   len += 4
122 #define ECORE_IPC_CNT64()   len += 8
123
124 /* strings */
125 #define ECORE_IPC_CHEKS() if (*((unsigned char *)d + s - 1) != 0) return 0;
126 #define ECORE_IPC_GETS(v) \
127     { \
128         if (ptr < ((unsigned char *)d + s)) \
129             { \
130                 p->v = (char *)ptr; \
131                 ptr += strlen(p->v) + 1; \
132             } \
133     } 
134 #define ECORE_IPC_PUTS(v, l)\
135     { \
136         strcpy((char *)ptr, p->v); \
137         ptr += l + 1; \
138     }
139
140 /* handy to calculate what sized block we need to alloc */
141 #define ECORE_IPC_SLEN(l, v) ((l = strlen(p->v)) + 1)
142 #define ECORE_IPC_CNTS(v)   len += strlen(p->v) + 1
143
144 /* saves typing function headers */
145 #define ECORE_IPC_DEC_STRUCT_PROTO(x) static int x(void *d, int s, void *pp)
146 #define ECORE_IPC_ENC_STRUCT_PROTO(x) static void *x(void *pp, int *s)
147 #define ECORE_IPC_DEC_EINA_LIST_PROTO(x) static Eina_List *x(void *d, int s)
148 #define ECORE_IPC_ENC_EINA_LIST_PROTO(x) static void *x(Eina_List *lp, int *s)
149
150
151 /* decoder setup - saves typing. requires data packet of exact size, or fail */
152 #define ECORE_IPC_DEC_STRUCT_HEAD_EXACT(typ, x) \
153     typ *p; \
154     unsigned char *ptr; \
155     p = (typ *)pp; \
156     if (!d) return 0; if (s != (x)) return 0; \
157     ptr = d;
158 /* decoder setup - saves typing. requires data packet of a minimum size */
159 #define ECORE_IPC_DEC_STRUCT_HEAD_MIN(typ, x) \
160     typ *p; \
161     unsigned char *ptr; \
162     p = (typ *)pp; \
163     if (!d) return 0; if (s < (x)) return 0; \
164     ptr = d;
165 /* footer for the hell of it */
166 #define ECORE_IPC_DEC_STRUCT_FOOT() return 1
167 /* header for encoder - gives native strct type and size of flattened packet */
168 #define ECORE_IPC_ENC_STRUCT_HEAD(typ, sz) \
169     typ *p; \
170     unsigned char *d, *ptr; \
171     int len; \
172     *s = 0; \
173     if(!pp) return NULL; \
174     p = (typ *)pp; \
175     len = sz; \
176     d = malloc(len); \
177     if (!d) return NULL; \
178     *s = len; \
179     ptr = d;
180 /* footer for the hell of it */
181 #define ECORE_IPC_ENC_STRUCT_FOOT() return d
182
183 #define ECORE_IPC_DEC_EINA_LIST_HEAD(typ) \
184     unsigned char *ptr; \
185     Eina_List *l; \
186     typ *p; \
187     l = NULL; \
188     ptr = d; \
189     while(ptr < (unsigned char *)(d + s)) \
190         { \
191             p = malloc(sizeof(typ));
192
193 #define ECORE_IPC_DEC_EINA_LIST_FOOT() \
194             l = eina_list_append(l, p); \
195         } \
196     return l
197 #define ECORE_IPC_ENC_EINA_LIST_HEAD_START(typ) \
198     Eina_List *l; \
199     typ *p; \
200     unsigned char *d, *ptr; \
201     int len; \
202     *s = 0; \
203     len = 0; \
204     for (l = lp; l; l = l->next) \
205       { \
206          p = l->data;
207 #define ECORE_IPC_ENC_EINA_LIST_HEAD_FINISH() \
208       } \
209     d = malloc(len); \
210     if(!d) return NULL; \
211     *s = len; \
212     ptr = d; \
213     for (l = lp; l; l = l->next) \
214       { \
215          p = l->data;
216
217 #define ECORE_IPC_ENC_EINA_LIST_FOOT() \
218       } \
219    return d
220
221 typedef enum _Ecore_Ipc_Type
222 {
223    ECORE_IPC_LOCAL_USER,
224    ECORE_IPC_LOCAL_SYSTEM,
225    ECORE_IPC_REMOTE_SYSTEM,
226    ECORE_IPC_USE_SSL = 16
227 } Ecore_Ipc_Type;
228    
229 typedef struct _Ecore_Ipc_Event_Client_Add  Ecore_Ipc_Event_Client_Add;
230 typedef struct _Ecore_Ipc_Event_Client_Del  Ecore_Ipc_Event_Client_Del;
231 typedef struct _Ecore_Ipc_Event_Server_Add  Ecore_Ipc_Event_Server_Add;
232 typedef struct _Ecore_Ipc_Event_Server_Del  Ecore_Ipc_Event_Server_Del;
233 typedef struct _Ecore_Ipc_Event_Client_Data Ecore_Ipc_Event_Client_Data;
234 typedef struct _Ecore_Ipc_Event_Server_Data Ecore_Ipc_Event_Server_Data;
235
236 struct _Ecore_Ipc_Event_Client_Add
237 {
238    Ecore_Ipc_Client *client;
239 };
240
241 struct _Ecore_Ipc_Event_Client_Del
242 {
243    Ecore_Ipc_Client *client;
244 };
245
246 struct _Ecore_Ipc_Event_Server_Add
247 {
248    Ecore_Ipc_Server *server;
249 };
250
251 struct _Ecore_Ipc_Event_Server_Del
252 {
253    Ecore_Ipc_Server *server;
254 };
255    
256 struct _Ecore_Ipc_Event_Client_Data
257 {
258    Ecore_Ipc_Client *client;
259    /* FIXME: this needs to become an ipc message */
260    int               major;
261    int               minor;
262    int               ref;
263    int               ref_to;
264    int               response;
265    void             *data;
266    int               size;
267 };
268    
269 struct _Ecore_Ipc_Event_Server_Data
270 {
271    Ecore_Ipc_Server *server;
272    /* FIXME: this needs to become an ipc message */
273    int               major;
274    int               minor;
275    int               ref;
276    int               ref_to;
277    int               response;
278    void             *data;
279    int               size;
280 };
281    
282 EAPI extern int ECORE_IPC_EVENT_CLIENT_ADD;
283 EAPI extern int ECORE_IPC_EVENT_CLIENT_DEL;
284 EAPI extern int ECORE_IPC_EVENT_SERVER_ADD;
285 EAPI extern int ECORE_IPC_EVENT_SERVER_DEL;
286 EAPI extern int ECORE_IPC_EVENT_CLIENT_DATA;
287 EAPI extern int ECORE_IPC_EVENT_SERVER_DATA;
288
289 EAPI int               ecore_ipc_init(void);
290 EAPI int               ecore_ipc_shutdown(void);
291
292 /* FIXME: need to add protocol type parameter */
293 EAPI Ecore_Ipc_Server *ecore_ipc_server_add(Ecore_Ipc_Type type, const char *name, int port, const void *data);
294
295 /* FIXME: need to add protocol type parameter */
296 EAPI Ecore_Ipc_Server *ecore_ipc_server_connect(Ecore_Ipc_Type type, char *name, int port, const void *data);
297 EAPI void             *ecore_ipc_server_del(Ecore_Ipc_Server *svr);
298 EAPI void             *ecore_ipc_server_data_get(Ecore_Ipc_Server *svr);
299 EAPI Eina_Bool         ecore_ipc_server_connected_get(Ecore_Ipc_Server *svr);
300 EAPI Eina_List        *ecore_ipc_server_clients_get(Ecore_Ipc_Server *svr);
301 /* FIXME: this needs to become an ipc message */
302 EAPI int               ecore_ipc_server_send(Ecore_Ipc_Server *svr, int major, int minor, int ref, int ref_to, int response, const void *data, int size);
303 EAPI void              ecore_ipc_server_client_limit_set(Ecore_Ipc_Server *svr, int client_limit, char reject_excess_clients);
304 EAPI void              ecore_ipc_server_data_size_max_set(Ecore_Ipc_Server *srv, int size);
305 EAPI int               ecore_ipc_server_data_size_max_get(Ecore_Ipc_Server *srv);
306 EAPI const char       *ecore_ipc_server_ip_get(Ecore_Ipc_Server *svr);
307 EAPI void              ecore_ipc_server_flush(Ecore_Ipc_Server *svr);
308     
309 /* FIXME: this needs to become an ipc message */
310 EAPI int               ecore_ipc_client_send(Ecore_Ipc_Client *cl, int major, int minor, int ref, int ref_to, int response, const void *data, int size);
311 EAPI Ecore_Ipc_Server *ecore_ipc_client_server_get(Ecore_Ipc_Client *cl);
312 EAPI void             *ecore_ipc_client_del(Ecore_Ipc_Client *cl);
313 EAPI void              ecore_ipc_client_data_set(Ecore_Ipc_Client *cl, const void *data);
314 EAPI void             *ecore_ipc_client_data_get(Ecore_Ipc_Client *cl);
315 EAPI void              ecore_ipc_client_data_size_max_set(Ecore_Ipc_Client *cl, int size);
316 EAPI int               ecore_ipc_client_data_size_max_get(Ecore_Ipc_Client *cl);
317 EAPI const char       *ecore_ipc_client_ip_get(Ecore_Ipc_Client *cl);
318 EAPI void              ecore_ipc_client_flush(Ecore_Ipc_Client *cl);
319
320 EAPI int               ecore_ipc_ssl_available_get(void);
321 /* FIXME: need to add a callback to "ok" large ipc messages greater than */
322 /*        a certain size (seurity/DOS attack safety) */
323    
324 #ifdef __cplusplus
325 }
326 #endif
327
328 #endif