Tizen 2.1 base
[framework/uifw/ecore.git] / src / lib / ecore_ipc / Ecore_Ipc.h
1 #ifndef _ECORE_IPC_H
2 #define _ECORE_IPC_H
3
4 #include <Eina.h>
5
6 #ifdef EAPI
7 # undef EAPI
8 #endif
9
10 #ifdef _WIN32
11 # ifdef EFL_ECORE_IPC_BUILD
12 #  ifdef DLL_EXPORT
13 #   define EAPI __declspec(dllexport)
14 #  else
15 #   define EAPI
16 #  endif
17 # else
18 #  define EAPI __declspec(dllimport)
19 # endif
20 #else
21 # ifdef __GNUC__
22 #  if __GNUC__ >= 4
23 #   define EAPI __attribute__ ((visibility("default")))
24 #  else
25 #   define EAPI
26 #  endif
27 # else
28 #  define EAPI
29 # endif
30 #endif
31
32 /**
33  * @file Ecore_Ipc.h
34  * @brief Ecore inter-process communication functions.
35  */
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 typedef struct _Ecore_Ipc_Server Ecore_Ipc_Server; /**< An IPC connection handle */
42 typedef struct _Ecore_Ipc_Client Ecore_Ipc_Client; /**< An IPC connection handle */
43
44 EAPI unsigned short     _ecore_ipc_swap_16(unsigned short v);
45 EAPI unsigned int       _ecore_ipc_swap_32(unsigned int v);
46 EAPI unsigned long long _ecore_ipc_swap_64(unsigned long long v);
47
48 #ifdef WORDS_BIGENDIAN
49 #define ECORE_IPC_SWAP2NET64(x) _ecore_ipc_swap_64(x)
50 #define ECORE_IPC_SWAP2CPU64(x) _ecore_ipc_swap_64(x)
51 #define ECORE_IPC_SWAP2NET32(x) _ecore_ipc_swap_32(x)
52 #define ECORE_IPC_SWAP2CPU32(x) _ecore_ipc_swap_32(x)
53 #define ECORE_IPC_SWAP2NET16(x) _ecore_ipc_swap_16(x)
54 #define ECORE_IPC_SWAP2CPU16(x) _ecore_ipc_swap_16(x)
55 #define ECORE_IPC_SWAP2NET8(x) (x)
56 #define ECORE_IPC_SWAP2CPU8(x) (x)
57 #else
58 #define ECORE_IPC_SWAP2NET64(x) (x)
59 #define ECORE_IPC_SWAP2CPU64(x) (x)
60 #define ECORE_IPC_SWAP2NET32(x) (x)
61 #define ECORE_IPC_SWAP2CPU32(x) (x)
62 #define ECORE_IPC_SWAP2NET16(x) (x)
63 #define ECORE_IPC_SWAP2CPU16(x) (x)
64 #define ECORE_IPC_SWAP2NET8(x) (x)
65 #define ECORE_IPC_SWAP2CPU8(x) (x)
66 #endif
67
68 /* 1, 2, 4 and 8 byte datatypes */
69 /* unpacking */
70 #define ECORE_IPC_GET64(v)\
71     { \
72         p->v = ECORE_IPC_SWAP2CPU64(*(long long *)(ptr)); \
73         ptr += 8; \
74     }
75 #define ECORE_IPC_GET32(v)\
76     { \
77         p->v = ECORE_IPC_SWAP2CPU32(*(int *)(ptr)); \
78         ptr += 4; \
79     }
80 #define ECORE_IPC_GET16(v)\
81     { \
82         p->v = ECORE_IPC_SWAP2CPU16(*(short *)(ptr)); \
83         ptr += 2; \
84     }
85 #define ECORE_IPC_GET8(v) \
86     { \
87         p->v = ECORE_IPC_SWAP2CPU8(*(char *)(ptr)); \
88         ptr += 1; \
89     }
90 /* packing */
91 #define ECORE_IPC_PUT64(v)\
92     { \
93         *(long long *)(ptr) = ECORE_IPC_SWAP2NET64(p->v); \
94         ptr += 8; \
95     }
96 #define ECORE_IPC_PUT32(v)\
97     { \
98         *(int *)(ptr) = ECORE_IPC_SWAP2NET32(p->v); \
99         ptr += 4; \
100     }
101 #define ECORE_IPC_PUT16(v)\
102     { \
103         *(short *)(ptr) = ECORE_IPC_SWAP2NET16(p->v); \
104         ptr += 2; \
105     }
106 #define ECORE_IPC_PUT8(v) \
107     { \
108         *(char *)(ptr) = ECORE_IPC_SWAP2NET8(p->v); \
109         ptr += 1; \
110     }
111 /* padding data */
112 #define ECORE_IPC_PAD8()   ptr += 1
113 #define ECORE_IPC_PAD16()  ptr += 2
114 #define ECORE_IPC_PAD32()  ptr += 4
115 #define ECORE_IPC_PAD64()  ptr += 8
116
117 /* counting data when encoding lists */
118 #define ECORE_IPC_CNT8()    len += 1
119 #define ECORE_IPC_CNT16()   len += 2
120 #define ECORE_IPC_CNT32()   len += 4
121 #define ECORE_IPC_CNT64()   len += 8
122
123 /* strings */
124 #define ECORE_IPC_CHEKS() if (*((unsigned char *)d + s - 1) != 0) return 0;
125 #define ECORE_IPC_GETS(v) \
126     { \
127         if (ptr < ((unsigned char *)d + s)) \
128             { \
129                 p->v = (char *)ptr; \
130                 ptr += strlen(p->v) + 1; \
131             } \
132     } 
133 #define ECORE_IPC_PUTS(v, l)\
134     { \
135         strcpy((char *)ptr, p->v); \
136         ptr += l + 1; \
137     }
138
139 /* handy to calculate what sized block we need to alloc */
140 #define ECORE_IPC_SLEN(l, v) ((l = strlen(p->v)) + 1)
141 #define ECORE_IPC_CNTS(v)   len += strlen(p->v) + 1
142
143 /* saves typing function headers */
144 #define ECORE_IPC_DEC_STRUCT_PROTO(x) static int x(void *d, int s, void *pp)
145 #define ECORE_IPC_ENC_STRUCT_PROTO(x) static void *x(void *pp, int *s)
146 #define ECORE_IPC_DEC_EINA_LIST_PROTO(x) static Eina_List *x(void *d, int s)
147 #define ECORE_IPC_ENC_EINA_LIST_PROTO(x) static void *x(Eina_List *lp, int *s)
148
149
150 /* decoder setup - saves typing. requires data packet of exact size, or fail */
151 #define ECORE_IPC_DEC_STRUCT_HEAD_EXACT(typ, x) \
152     typ *p; \
153     unsigned char *ptr; \
154     p = (typ *)pp; \
155     if (!d) return 0; if (s != (x)) return 0; \
156     ptr = d;
157 /* decoder setup - saves typing. requires data packet of a minimum size */
158 #define ECORE_IPC_DEC_STRUCT_HEAD_MIN(typ, x) \
159     typ *p; \
160     unsigned char *ptr; \
161     p = (typ *)pp; \
162     if (!d) return 0; if (s < (x)) return 0; \
163     ptr = d;
164 /* footer for the hell of it */
165 #define ECORE_IPC_DEC_STRUCT_FOOT() return 1
166 /* header for encoder - gives native strct type and size of flattened packet */
167 #define ECORE_IPC_ENC_STRUCT_HEAD(typ, sz) \
168     typ *p; \
169     unsigned char *d, *ptr; \
170     int len; \
171     *s = 0; \
172     if(!pp) return NULL; \
173     p = (typ *)pp; \
174     len = sz; \
175     d = malloc(len); \
176     if (!d) return NULL; \
177     *s = len; \
178     ptr = d;
179 /* footer for the hell of it */
180 #define ECORE_IPC_ENC_STRUCT_FOOT() return d
181
182 #define ECORE_IPC_DEC_EINA_LIST_HEAD(typ) \
183     unsigned char *ptr; \
184     Eina_List *l; \
185     typ *p; \
186     l = NULL; \
187     ptr = d; \
188     while(ptr < (unsigned char *)(d + s)) \
189         { \
190             p = malloc(sizeof(typ));
191
192 #define ECORE_IPC_DEC_EINA_LIST_FOOT() \
193             l = eina_list_append(l, p); \
194         } \
195     return l
196 #define ECORE_IPC_ENC_EINA_LIST_HEAD_START(typ) \
197     Eina_List *l; \
198     typ *p; \
199     unsigned char *d, *ptr; \
200     int len; \
201     *s = 0; \
202     len = 0; \
203     for (l = lp; l; l = l->next) \
204       { \
205          p = l->data;
206 #define ECORE_IPC_ENC_EINA_LIST_HEAD_FINISH() \
207       } \
208     d = malloc(len); \
209     if(!d) return NULL; \
210     *s = len; \
211     ptr = d; \
212     for (l = lp; l; l = l->next) \
213       { \
214          p = l->data;
215
216 #define ECORE_IPC_ENC_EINA_LIST_FOOT() \
217       } \
218    return d
219
220 typedef enum _Ecore_Ipc_Type
221 {
222    ECORE_IPC_LOCAL_USER,
223    ECORE_IPC_LOCAL_SYSTEM,
224    ECORE_IPC_REMOTE_SYSTEM,
225    ECORE_IPC_USE_SSL = (1 << 4),
226    ECORE_IPC_NO_PROXY = (1 << 5)
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