forked from bhks/core-alljoyn-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajs_msgloop.c
499 lines (466 loc) · 15.3 KB
/
ajs_msgloop.c
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
/**
* @file
*/
/******************************************************************************
* Copyright AllSeen Alliance. All rights reserved.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
******************************************************************************/
#include "ajs.h"
#include "ajs_util.h"
#include "ajs_services.h"
#include "ajs_debugger.h"
#include <aj_msg_priv.h>
#include <aj_link_timeout.h>
#include <alljoyn/controlpanel/ControlPanelService.h>
static uint8_t IsPropAccessor(AJ_Message* msg)
{
if (strcmp(msg->iface, AJ_PropertiesIface[0] + 1) == 0) {
return msg->msgId & 0xFF;
} else {
return AJS_NOT_ACCESSOR;
}
}
static AJ_Status SessionBindReply(duk_context* ctx, AJ_Message* msg)
{
AJ_Status status;
uint32_t result;
uint16_t port;
uint8_t ldstate;
status = AJ_UnmarshalArgs(msg, "uq", &result, &port);
if (status != AJ_OK) {
return status;
}
if (port != AJS_APP_PORT) {
AJ_ResetArgs(msg);
#if !defined(AJS_CONSOLE_LOCKDOWN)
status = AJS_GetLockdownState(&ldstate);
if (status == AJ_OK && ldstate == AJS_CONSOLE_UNLOCKED) {
status = AJS_ConsoleMsgHandler(ctx, msg);
}
#endif
if (status == AJ_ERR_NO_MATCH) {
status = AJS_ServicesMsgHandler(msg);
if (status == AJ_ERR_NO_MATCH) {
status = AJ_OK;
}
}
}
return status;
}
static AJ_Status SessionDispatcher(duk_context* ctx, AJ_Message* msg)
{
AJ_Status status;
uint32_t sessionId;
uint16_t port;
char* joiner;
uint8_t ldstate;
status = AJ_UnmarshalArgs(msg, "qus", &port, &sessionId, &joiner);
if (status != AJ_OK) {
return status;
}
/*
* Automatically accept sessions requests to the application port
*/
if (port == AJS_APP_PORT) {
return AJS_HandleAcceptSession(ctx, msg, port, sessionId, joiner);
}
/*
* See if this is the control panel port
*/
if (AJCPS_CheckSessionAccepted(port, sessionId, joiner)) {
return AJ_BusReplyAcceptSession(msg, TRUE);
}
status = AJ_ResetArgs(msg);
if (status != AJ_OK) {
return status;
}
/*
* JavaScript doesn't accept/reject session so the session is either for the console or perhaps
* a service if the services bind their own ports.
*/
#if !defined(AJS_CONSOLE_LOCKDOWN)
status = AJS_GetLockdownState(&ldstate);
if (status == AJ_OK && ldstate == AJS_CONSOLE_UNLOCKED) {
status = AJS_ConsoleMsgHandler(ctx, msg);
}
#endif
if (status == AJ_ERR_NO_MATCH) {
status = AJS_ServicesMsgHandler(msg);
}
if (status == AJ_ERR_NO_MATCH) {
AJ_ErrPrintf(("SessionDispatcher rejecting attempt to join unbound port %d\n", port));
status = AJ_BusReplyAcceptSession(msg, FALSE);
}
return status;
}
static AJ_Status HandleMessage(duk_context* ctx, duk_idx_t ajIdx, AJ_Message* msg)
{
duk_idx_t msgIdx;
uint8_t accessor = AJS_NOT_ACCESSOR;
const char* func;
AJ_Status status;
uint8_t ldstate;
#if !defined(AJS_CONSOLE_LOCKDOWN)
status = AJS_GetLockdownState(&ldstate);
if (status == AJ_OK && ldstate == AJS_CONSOLE_UNLOCKED) {
status = AJS_ConsoleMsgHandler(ctx, msg);
if (status != AJ_ERR_NO_MATCH) {
if (status != AJ_OK) {
AJ_WarnPrintf(("AJS_ConsoleMsgHandler returned %s\n", AJ_StatusText(status)));
}
return status;
}
}
#endif
/*
* JOIN_SESSION replies are handled in the AllJoyn.js layer and don't get passed to JavaScript
*/
if (msg->msgId == AJ_REPLY_ID(AJ_METHOD_JOIN_SESSION)) {
return AJS_HandleJoinSessionReply(ctx, msg);
}
/*
* Nothing more to do if the AllJoyn module was not loaded
*/
if (ajIdx < 0) {
return AJ_OK;
}
/*
* Let the bases services layer take a look at the message
*/
status = AJS_ServicesMsgHandler(msg);
if (status != AJ_ERR_NO_MATCH) {
if (status != AJ_OK) {
AJ_WarnPrintf(("AJS_ServicesMsgHandler returned %s\n", AJ_StatusText(status)));
}
return status;
}
/*
* Push the appropriate callback function onto the duktape stack
*/
if (msg->hdr->msgType == AJ_MSG_SIGNAL) {
/*
* About announcements and found name signal get special handling
*/
if (msg->msgId == AJ_SIGNAL_ABOUT_ANNOUNCE) {
return AJS_AboutAnnouncement(ctx, msg);
}
if (msg->msgId == AJ_SIGNAL_FOUND_ADV_NAME) {
return AJS_FoundAdvertisedName(ctx, msg);
}
if ((msg->msgId == AJ_SIGNAL_SESSION_LOST) || (msg->msgId == AJ_SIGNAL_SESSION_LOST_WITH_REASON)) {
if (AJS_DebuggerIsAttached()) {
msg = AJS_CloneAndCloseMessage(ctx, msg);
}
return AJS_SessionLost(ctx, msg);
}
func = "onSignal";
duk_get_prop_string(ctx, ajIdx, func);
} else if (msg->hdr->msgType == AJ_MSG_METHOD_CALL) {
accessor = IsPropAccessor(msg);
switch (accessor) {
case AJS_NOT_ACCESSOR:
func = "onMethodCall";
break;
case AJ_PROP_GET:
func = "onPropGet";
break;
case AJ_PROP_SET:
func = "onPropSet";
break;
case AJ_PROP_GET_ALL:
func = "onPropGetAll";
break;
default:
return AJ_ERR_INVALID;
}
duk_get_prop_string(ctx, ajIdx, func);
} else {
func = "onReply";
AJS_GetGlobalStashObject(ctx, func);
if (duk_is_object(ctx, -1)) {
duk_get_prop_index(ctx, -1, msg->replySerial);
duk_swap_top(ctx, -2);
/*
* Clear the onReply entry
*/
duk_del_prop_index(ctx, -1, msg->replySerial);
duk_pop(ctx);
}
}
/*
* Skip if there is no function to call.
*/
if (!duk_is_function(ctx, -1)) {
if (msg->hdr->msgType == AJ_MSG_METHOD_CALL) {
AJ_Message error;
AJ_WarnPrintf(("%s: not registered - rejecting message\n", func));
AJ_MarshalErrorMsg(msg, &error, AJ_ErrRejected);
status = AJ_DeliverMsg(&error);
} else {
AJ_WarnPrintf(("%s: not registered - ignoring message\n", func));
status = AJ_OK;
}
duk_pop(ctx);
return status;
}
/*
* Opens up a stack entry above the function
*/
duk_dup_top(ctx);
msgIdx = AJS_UnmarshalMessage(ctx, msg, accessor);
AJ_ASSERT(msgIdx == (ajIdx + 3));
/*
* Save the message object on the stack
*/
duk_copy(ctx, msgIdx, -3);
/*
* Special case for GET prop so we can get the signature for marshalling the result
*/
if (accessor == AJS_NOT_ACCESSOR) {
status = AJS_UnmarshalMsgArgs(ctx, msg);
} else {
status = AJS_UnmarshalPropArgs(ctx, msg, accessor, msgIdx);
}
if (status == AJ_OK) {
duk_idx_t numArgs = duk_get_top(ctx) - msgIdx - 1;
/*
* If attached, the debugger will begin to unmarshal a message when the
* method handler is called, therefore it must be cloned-and-closed now.
*/
if (AJS_DebuggerIsAttached()) {
msg = AJS_CloneAndCloseMessage(ctx, msg);
}
if (duk_pcall_method(ctx, numArgs) != DUK_EXEC_SUCCESS) {
const char* err = duk_safe_to_string(ctx, -1);
AJ_ErrPrintf(("%s: %s\n", func, err));
/*
* Generate an error reply if this was a method call
*/
if (msg->hdr->msgType == AJ_MSG_METHOD_CALL) {
duk_push_c_lightfunc(ctx, AJS_MethodCallError, 1, 0, 0);
duk_insert(ctx, -3);
(void)duk_pcall_method(ctx, 1);
}
}
}
/*
* Cleanup stack back to the AJ object
*/
duk_set_top(ctx, ajIdx + 1);
return status;
}
static AJS_DEFERRED_OP deferredOp = AJS_OP_NONE;
void AJS_DeferredOperation(duk_context* ctx, AJS_DEFERRED_OP op)
{
deferredOp = op;
}
static AJ_Status DoDeferredOperation(duk_context* ctx)
{
AJ_Status status = AJ_ERR_RESTART;
switch (deferredOp) {
case AJS_OP_OFFBOARD:
AJOBS_ClearInfo();
AJS_DetachAllJoyn(AJS_GetBusAttachment(), AJ_ERR_RESTART);
break;
case AJS_OP_FACTORY_RESET:
AJS_FactoryReset();
break;
default:
status = AJ_OK;
break;
}
deferredOp = AJS_OP_NONE;
return status;
}
AJ_Status AJS_MessageLoop(duk_context* ctx, AJ_BusAttachment* aj, duk_idx_t ajIdx)
{
AJ_Status status = AJ_OK;
AJ_Message msg;
AJ_Time timerClock;
uint32_t linkTO;
uint32_t msgTO = 0x7FFFFFFF;
duk_idx_t top = duk_get_top_index(ctx);
uint8_t ldstate;
AJ_InfoPrintf(("AJS_MessageLoop top=%d\n", (int)top));
deferredOp = AJS_OP_NONE;
if (ajIdx >= 0) {
/*
* Read the link timeout property from the config set
*/
duk_get_prop_string(ctx, -1, "config");
duk_get_prop_string(ctx, -1, "linkTimeout");
linkTO = duk_get_int(ctx, -1);
duk_pop_2(ctx);
AJ_SetBusLinkTimeout(aj, linkTO);
}
/*
* timer clock must be initialized
*/
AJ_InitTimer(&timerClock);
AJ_ASSERT(duk_get_top_index(ctx) == top);
/*
* Initialize About we can start sending announcements
*/
AJ_AboutInit(aj, AJS_APP_PORT);
while (status == AJ_OK) {
/*
* Check we are cleaning up the duktape stack correctly.
*/
if (duk_get_top_index(ctx) != top) {
AJ_ErrPrintf(("!!!AJS_MessageLoop top=%d expected %d\n", (int)duk_get_top_index(ctx), (int)top));
AJS_DumpStack(ctx);
AJ_ASSERT(duk_get_top_index(ctx) == top);
}
AJS_SetWatchdogTimer(AJS_DEFAULT_WATCHDOG_TIMEOUT);
/*
* Pinned items (strings/buffers) are only valid while running script
*/
AJS_ClearPins(ctx);
/*
* Check if there are any pending I/O operations to perform.
*/
status = AJS_ServiceIO(ctx);
if (status != AJ_OK) {
AJ_ErrPrintf(("Error servicing I/O functions\n"));
break;
}
/*
* Services the internal and timeout timers and updates the timeout value for any new
* timers that have been registered since this function was last called.
*/
status = AJS_RunTimers(ctx, &timerClock, &msgTO);
if (status != AJ_OK) {
AJ_ErrPrintf(("Error servicing timer functions\n"));
break;
}
/*
* Do any announcing required
*/
status = AJS_GetLockdownState(&ldstate);
if (status == AJ_OK && ldstate == AJS_CONSOLE_UNLOCKED) {
status = AJ_AboutAnnounce(aj);
}
if (status != AJ_OK) {
break;
}
/*
* This special wildcard allows us to unmarshal signals with any source path
*/
AJS_SetObjectPath("!");
/*
* Block until a message is received, the timeout expires, or the operation is interrupted.
*/
status = AJ_UnmarshalMsg(aj, &msg, msgTO);
if (status != AJ_OK) {
if ((status == AJ_ERR_INTERRUPTED) || (status == AJ_ERR_TIMEOUT)) {
status = AJ_OK;
continue;
}
if (status == AJ_ERR_NO_MATCH) {
status = AJ_OK;
}
AJ_CloseMsg(&msg);
continue;
}
switch (msg.msgId) {
case 0:
/* If a message was parsed but is unrecognized and should be ignored */
break;
/* Introspection messages */
case AJ_METHOD_PING:
case AJ_METHOD_BUS_PING:
case AJ_METHOD_GET_MACHINE_ID:
case AJ_METHOD_INTROSPECT:
case AJ_METHOD_GET_DESCRIPTION_LANG:
case AJ_METHOD_INTROSPECT_WITH_DESC:
/* About messages */
case AJ_METHOD_ABOUT_GET_PROP:
case AJ_METHOD_ABOUT_SET_PROP:
case AJ_METHOD_ABOUT_GET_ABOUT_DATA:
case AJ_METHOD_ABOUT_GET_OBJECT_DESCRIPTION:
case AJ_METHOD_ABOUT_ICON_GET_PROP:
case AJ_METHOD_ABOUT_ICON_SET_PROP:
case AJ_METHOD_ABOUT_ICON_GET_URL:
case AJ_METHOD_ABOUT_ICON_GET_CONTENT:
/* Authentication messages and replies */
case AJ_METHOD_EXCHANGE_GUIDS:
case AJ_REPLY_ID(AJ_METHOD_EXCHANGE_GUIDS):
case AJ_METHOD_EXCHANGE_SUITES:
case AJ_REPLY_ID(AJ_METHOD_EXCHANGE_SUITES):
case AJ_METHOD_AUTH_CHALLENGE:
case AJ_REPLY_ID(AJ_METHOD_AUTH_CHALLENGE):
case AJ_METHOD_GEN_SESSION_KEY:
case AJ_REPLY_ID(AJ_METHOD_GEN_SESSION_KEY):
case AJ_METHOD_EXCHANGE_GROUP_KEYS:
case AJ_REPLY_ID(AJ_METHOD_EXCHANGE_GROUP_KEYS):
case AJ_METHOD_KEY_EXCHANGE:
case AJ_REPLY_ID(AJ_METHOD_KEY_EXCHANGE):
case AJ_METHOD_KEY_AUTHENTICATION:
case AJ_REPLY_ID(AJ_METHOD_KEY_AUTHENTICATION):
/* Replies the app ignores */
case AJ_REPLY_ID(AJ_METHOD_ADD_MATCH):
case AJ_REPLY_ID(AJ_METHOD_REMOVE_MATCH):
case AJ_REPLY_ID(AJ_METHOD_PING):
case AJ_REPLY_ID(AJ_METHOD_BUS_PING):
/* Signals the app ignores */
case AJ_SIGNAL_PROBE_ACK:
case AJ_SIGNAL_PROBE_REQ:
case AJ_SIGNAL_NAME_OWNER_CHANGED:
status = AJ_BusHandleBusMessage(&msg);
break;
case AJ_METHOD_ACCEPT_SESSION:
status = SessionDispatcher(ctx, &msg);
break;
case AJ_REPLY_ID(AJ_METHOD_BIND_SESSION_PORT):
status = SessionBindReply(ctx, &msg);
break;
default:
status = HandleMessage(ctx, ajIdx, &msg);
break;
}
/*
* Free message resources
*/
AJ_CloseMsg(&msg);
/*
* Decide which messages should cause us to exit
*/
switch (status) {
case AJ_OK:
break;
case AJ_ERR_READ:
case AJ_ERR_WRITE:
case AJ_ERR_RESTART:
case AJ_ERR_RESTART_APP:
break;
default:
AJ_ErrPrintf(("Got error %s - continuing anyway\n", AJ_StatusText(status)));
status = AJ_OK;
}
/*
* Let link monitor know we are getting messages
*/
AJ_NotifyLinkActive();
/*
* Perform any deferred operations. These are operations such as factory reset that cannot
* be cleanly performed from inside duktape.
*/
if (status == AJ_OK) {
status = DoDeferredOperation(ctx);
}
}
AJS_ClearPins(ctx);
AJS_ClearWatchdogTimer();
return status;
}