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