-
Notifications
You must be signed in to change notification settings - Fork 0
/
lunaservice.h
572 lines (427 loc) · 13.9 KB
/
lunaservice.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#include <stdio.h>
#include <stdbool.h>
#include <glib.h>
#include <json.h>
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#define NOGDI
#include <winsock2.h>
#else
#include <sys/select.h>
#endif
#include <lunaservice-errors.h>
#ifndef _LUNASERVICE_H_
#define _LUNASERVICE_H_
#ifdef __cplusplus
extern "C" {
#endif
/**
* @mainpage
*
* <h1>LunaService</h1>
*
* <em>Example client usage:</em>
*
@code
bool retVal;
LSError lserror;
LSErrorInit(&lserror);
LSHandle *serviceHandle;
retVal = LSRegister(NULL, &serviceHandle, &lserror);
if (!retVal) goto error;
retVal = LSCall(serviceHandle, "luna://com.palm.contacts/category/listContacts",
"{ \"json payload\" }", listContactsHandler, user_data, &token, &lserror);
if (!retVal) goto error;
LSGmainAttach(serviceHandle, gmainLoop, &lserror);
g_main_loop_run(gmainLoop);
@endcode
<em>Example service usage.</em>
@code
// callback
static bool
listContacts(LSHandle *sh, LSMessage *message)
{
LSMessage *reply = NULL;
bool retVal;
LSError lserror;
LSErrorInit(&lserror);
retVal = LSMessageReply(sh, message, "{ JSON REPLY PAYLOAD }", &lserror);
if (!retVal)
{
LSErrorPrint(&lserror, stderr);
LSErrorFree(&lserror);
}
return retVal;
}
static LSMethod ipcMethods[] = {
{ "listContacts", listContacts },
{ },
};
...
// Service registration thread
bool retVal;
LSError lserror;
LSErrorInit(&lserror);
LSHandle *serviceHandle;
retVal = LSRegister("com.palm.contacts", &serviceHandle, &lserror);
if (!retVal) goto error;
retVal = LSRegisterCategory(serviceHandle, "/category", ipcMethods, NULL, NULL, &lserror);
if (!retVal) goto error;
retVal = LSGmainAttach(serviceHandle, gmainLoop, &lserror);
if (!retVal) goto error;
g_main_loop_run(gmainLoop);
@endcode
<em>Storing a message for replying in another thread.</em>
@code
Queue messageQueue;
...
static bool
listContacts(LSHandle *sh, LSMessage *message)
{
bool retVal;
LSError lserror;
LSErrorInit(&lserror);
LSMessageRef(message);
queue(messageQueue, message);
}
...
void
SomeOtherThread()
{
LSError lserror;
LSErrorInit(&lserror);
LSMessage *message = dequeue(messageQueue);
...
if (!LSMessageReply(sh, message, "{PAYLOAD IN JSON}", lserror))
{
LSErrorPrint(&lserror);
LSErrorFree(&lserror);
}
....
}
@endcode
<em>Example run loop via select. See LSCustomSelectExample() for
latest example.</em>
@code
bool retVal;
do
{
int nfd = -1;
fd_set rdfds, wrfds, exfds;
FD_ZERO(&rdfds);
FD_ZERO(&wrfds);
FD_ZERO(&exfds);
retVal = LSCustomGetFds(sh, &nfd, &rdfds, &wrfds, &exfds, lserror);
if (!retVal) return -1;
int ret = select(nfd, &rdfds, &wrfds, &exfds, NULL);
if (ret < 0)
{
perror("select");
break;
}
// Pull incoming bytes off socket and push outgoing bytes onto it.
retVal = LSCustomSendRecvBytes(sh, &rdfds, &wrfds, &exfds, lserror);
if (!retVal)
{
break;
}
// Transmit byte and Dispatch incomming at most 1 message
retVal = LSCustomDispatchMessage(sh, NULL, lserror);
if (!retVal)
{
break;
}
} while (true);
@endcode
<em>Example run loop via select if you want to handle messages directly.</em>
@code
while (serviceRunning)
{
fd_set rdfds, wrfds, exfds;
FD_ZERO(&rdfds);
FD_ZERO(&wrfds);
FD_ZERO(&exfds);
LSGetFd(serviceHandle, &maxfd, &rdfds, &wrfds, &exfds, &lserror);
ret = select(maxfd, &rdfds, &wrfds, &exfds, NULL);
if (ret > 0)
{
LSMessage *message;
char *reply = NULL;
LSMessageFetch(serviceHandle, &message, &lserror);
if (strcmp(LSMessageGetName(message), "listContacts"))
{
char *payload;
payload = LSMessageGetPayload(message);
...
reply = "{ JSON PAYLOAD }";
LSMessageReply(serviceHandle, message, reply, &lserror);
}
}
}
@endcode
*/
/**
* @addtogroup LunaService
* @{
*/
typedef unsigned long LSMessageToken;
/**
* @brief Invalid token number.
*
* This is seen if you do LSMessageGetResponseToken() on a message that is not
* a reply. It is also a good neutral value to initialize an array of
* unitialized message tokens.
*/
#define LSMESSAGE_TOKEN_INVALID 0
/**
* @brief Error object which contains information about first
* error since it was initialized via LSErrorInit.
*/
struct LSError {
int error_code; /**< public error code */
char *message; /**< public error message */
const char *file; /**< file in which error happened. */
int line; /**< line on which error happened. */
const char *func; /**< function on which error happened. */
void *padding; /**< Reserved for future use */
unsigned long magic; /**< use as cookie to detect invalid LSErrors */
};
typedef struct LSError LSError;
/**
* @brief Handle to service.
*/
typedef struct LSHandle LSHandle;
/**
* @brief Handle to public service.
*/
typedef struct LSPalmService LSPalmService;
/**
* @brief Message object.
*/
typedef struct LSMessage LSMessage;
/**
* Table registration of callbacks.
*/
/**
* @brief Type for method callbacks.
*
* @param *LSMethodFunction
* @param sh
* @param msg
*
* @retval true if message successfully processed.
* @retval false if some error occurred and you would like the callback to
* be called again later.
*/
typedef bool (*LSMethodFunction) (LSHandle *sh, LSMessage *msg, void *category_context);
/**
* @brief Type for property get callback.
*
* @param *LSPropertyGetFunction
* @param sh
* @param msg
*
* @retval Same as LSMethodFunction()
*/
typedef bool (*LSPropertyGetFunction) (LSHandle *sh, LSMessage *msg, void *category_context);
/**
* @brief Type for property set callback.
*
* @param *LSPropertySetFunction
* @param sh
* @param msg
*
* @retval Same as LSMethodFunction()
*/
typedef bool (*LSPropertySetFunction) (LSHandle *sh, LSMessage *msg, void *category_context);
/**
* @brief Method flags
*/
typedef enum {
LUNA_METHOD_FLAG_DEPRECATED = (1 << 0),
} LSMethodFlags;
/**
* @brief Signal flags
*/
typedef enum {
LUNA_SIGNAL_FLAG_DEPRECATED = (1 << 0),
} LSSignalFlags;
/**
* @brief Property flags
*/
typedef enum {
LUNA_PROPERTY_FLAG_DEPRECATED = (1 << 0),
} LSPropertyFlags;
typedef struct {
const char *name; /**< Method name */
LSMethodFunction function; /**< Method function */
LSMethodFlags flags; /**< Method flags */
} LSMethod;
typedef struct {
const char *name; /**<Signal name */
LSSignalFlags flags; /**<Signal flags */
} LSSignal;
typedef struct {
const char *name; /**<Property name */
const char *type; /**<Property value type */
LSPropertyGetFunction get; /**<Property get function */
LSPropertySetFunction set; /**<Property set function */
LSPropertyFlags flags; /**<Property flags */
} LSProperty;
/* LSError exception style functions */
bool LSErrorInit(LSError *error);
void LSErrorFree(LSError *error);
bool LSErrorIsSet(LSError *lserror);
void LSErrorPrint(LSError *lserror, FILE *out);
/* Luna Service general functions */
bool LSRegister(const char *name, LSHandle **sh,
LSError *lserror);
bool LSRegisterPubPriv(const char *name, LSHandle **sh,
bool public_bus,
LSError *lserror);
typedef void (*LSDisconnectHandler)(LSHandle *sh, void *user_data);
bool LSSetDisconnectHandler(LSHandle *sh, LSDisconnectHandler disconnect_handler,
void *user_data, LSError *lserror);
bool LSRegisterCategory(LSHandle *sh, const char *category,
LSMethod *methods,
LSSignal *signals,
LSProperty *properties, LSError *lserror);
bool LSRegisterCategoryAppend(LSHandle *sh, const char *category,
LSMethod *methods,
LSSignal *signals,
LSError *lserror);
bool LSCategorySetData(LSHandle *sh, const char *category,
void *user_data, LSError *lserror);
bool LSUnregister(LSHandle *service, LSError *lserror);
const char * LSHandleGetName(LSHandle *sh);
/* Palm Services */
bool LSRegisterPalmService(const char *name,
LSPalmService **ret_palm_service,
LSError *lserror);
bool LSUnregisterPalmService(LSPalmService *psh, LSError *lserror);
bool LSPalmServiceRegisterCategory(LSPalmService *psh,
const char *category,
LSMethod *methods_public, LSMethod *methods_private,
LSSignal *signals, void *category_user_data, LSError *lserror);
LSHandle * LSPalmServiceGetPrivateConnection(LSPalmService *psh);
LSHandle * LSPalmServiceGetPublicConnection(LSPalmService *psh);
/* LSMessage (Luna Service Message) functions */
LSHandle * LSMessageGetConnection(LSMessage *message);
bool LSMessageIsPublic(LSPalmService *psh, LSMessage *message);
void LSMessageRef(LSMessage *message);
void LSMessageUnref(LSMessage *message);
bool LSMessagePrint(LSMessage *lmsg, FILE *out);
const char * LSMessageGetUniqueToken(LSMessage *message);
const char * LSMessageGetKind(LSMessage *message);
const char * LSMessageGetApplicationID(LSMessage *message);
const char * LSMessageGetSender(LSMessage *message);
const char * LSMessageGetCategory(LSMessage *message);
const char * LSMessageGetMethod(LSMessage *message);
const char * LSMessageGetPayload(LSMessage *message);
json_t * LSMessageGetPayloadJSON(LSMessage *message);
bool LSMessageIsSubscription(LSMessage *lsmgs);
LSMessageToken LSMessageGetToken(LSMessage *call);
LSMessageToken LSMessageGetResponseToken(LSMessage *reply);
bool LSMessageRespond(LSMessage *message, const char *reply_payload,
LSError *lserror);
bool LSMessageReply(LSHandle *sh, LSMessage *lsmsg, const char *replyPayload,
LSError *lserror);
bool LSMessageReturn(LSHandle *sh, LSMessage *message, const char *replyPayload,
LSError *error);
/* Mainloop integration functions. */
bool LSGmainAttach(LSHandle *sh, GMainLoop *mainLoop, LSError *lserror);
bool LSGmainAttachPalmService(LSPalmService *psh,
GMainLoop *mainLoop, LSError *lserror);
bool LSGmainSetPriority(LSHandle *sh, int priority, LSError *lserror);
bool LSGmainSetPriorityPalmService(LSPalmService *psh, int priority, LSError *lserror);
/* @} END OF LunaService */
/**
* @addtogroup LunaServiceClient
* @{
*/
/**
* @brief Function callback to be called when serviceName connects/disconnects.
*
* @param sh service handle
* @param serviceName name of service that was brought up/down.
* @param connected service was brought up if true.
*
* @retval
*/
typedef bool (*LSServerStatusFunc) (LSHandle *sh, const char *serviceName,
bool connected,
void *ctx);
/**
* @brief Callback function called on incomming message.
*
* @param sh service handle
* @param reply reply message
* @param void * context
*
* @retval true if message is handled.
*/
typedef bool (*LSFilterFunc) (LSHandle *sh, LSMessage *reply, void *ctx);
bool LSCall(LSHandle *sh, const char *uri, const char *payload,
LSFilterFunc callback, void *user_data,
LSMessageToken *ret_token, LSError *lserror);
bool LSCallOneReply(LSHandle *sh, const char *uri, const char *payload,
LSFilterFunc callback, void *ctx,
LSMessageToken *ret_token, LSError *lserror);
bool LSCallFromApplication(LSHandle *sh, const char *uri, const char *payload,
const char *applicationID,
LSFilterFunc callback, void *ctx,
LSMessageToken *ret_token, LSError *lserror);
bool LSCallFromApplicationOneReply(
LSHandle *sh, const char *uri, const char *payload,
const char *applicationID,
LSFilterFunc callback, void *ctx,
LSMessageToken *ret_token, LSError *lserror);
bool LSCallCancel(LSHandle *sh, LSMessageToken token, LSError *lserror);
/* @} END OF LunaServiceClient */
/**
* @addtogroup LunaServiceSubscription
* @{
*/
typedef struct LSSubscriptionIter LSSubscriptionIter;
bool LSSubscriptionProcess (LSHandle *sh, LSMessage *message, bool *subscribed,
LSError *lserror);
bool LSSubscriptionSetCancelFunction(LSHandle *sh,
LSFilterFunc cancelFunction,
void *ctx, LSError *lserror);
bool LSSubscriptionAdd(LSHandle *sh, const char *key,
LSMessage *message, LSError *lserror);
bool LSSubscriptionAcquire(LSHandle *sh, const char *key,
LSSubscriptionIter **ret_iter, LSError *lserror);
void LSSubscriptionRelease(LSSubscriptionIter *iter);
bool LSSubscriptionHasNext(LSSubscriptionIter *iter);
LSMessage *LSSubscriptionNext(LSSubscriptionIter *iter);
void LSSubscriptionRemove(LSSubscriptionIter *iter);
bool LSSubscriptionReply(LSHandle *sh, const char *key,
const char *payload, LSError *lserror);
bool LSSubscriptionRespond(LSPalmService *psh, const char *key,
const char *payload, LSError *lserror);
bool LSSubscriptionPost(LSHandle *sh, const char *category,
const char *method,
const char *payload, LSError *lserror);
/* @} END OF LunaServiceSubscription */
/**
* @addtogroup LunaServiceSignals
* @{
*/
bool LSSignalSend(LSHandle *sh, const char *uri, const char *payload,
LSError *lserror);
bool LSSignalSendNoTypecheck(LSHandle *sh,
const char *uri, const char *payload, LSError *lserror);
bool LSSignalCall(LSHandle *sh,
const char *category, const char *methodName,
LSFilterFunc filterFunc, void *ctx,
LSMessageToken *ret_token,
LSError *lserror);
bool LSSignalCallCancel(LSHandle *sh, LSMessageToken token, LSError *lserror);
bool LSRegisterServerStatus(LSHandle *sh, const char *serviceName,
LSServerStatusFunc func, void *ctx, LSError *lserror);
/* @} END OF LunaServiceSignals */
#ifdef __cplusplus
} // extern "C"
#endif
#endif //_LUNASERVICE_H_