summaryrefslogtreecommitdiff
path: root/mesh/agent.c
blob: 5058d0d8df0f3c7615828f5c1e48a148ab2b7daf (plain)
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2018-2019  Intel Corporation. All rights reserved.
 *
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <ell/ell.h>

#include "mesh/mesh.h"
#include "mesh/error.h"
#include "mesh/dbus.h"
#include "mesh/agent.h"

typedef enum {
	MESH_AGENT_REQUEST_BLINK,
	MESH_AGENT_REQUEST_BEEP,
	MESH_AGENT_REQUEST_VIBRATE,
	MESH_AGENT_REQUEST_OUT_NUMERIC,
	MESH_AGENT_REQUEST_OUT_ALPHA,
	MESH_AGENT_REQUEST_PUSH,
	MESH_AGENT_REQUEST_TWIST,
	MESH_AGENT_REQUEST_IN_NUMERIC,
	MESH_AGENT_REQUEST_IN_ALPHA,
	MESH_AGENT_REQUEST_STATIC_OOB,
	MESH_AGENT_REQUEST_PRIVATE_KEY,
	MESH_AGENT_REQUEST_PUBLIC_KEY,
	MESH_AGENT_REQUEST_CAPABILITIES,
} agent_request_type_t;

struct agent_request {
	agent_request_type_t type;
	struct l_dbus_message *msg;
	void *cb;
	void *user_data;
};

struct mesh_agent {
	char *path;
	char *owner;
	struct mesh_agent_prov_caps caps;
	struct agent_request *req;
};

struct prov_action {
	const char *action;
	uint16_t output;
	uint16_t input;
	uint8_t size;
};

struct oob_info {
	const char *oob;
	uint16_t mask;
};

static struct prov_action cap_table[] = {
	{"blink", 0x0001, 0x0000, 1},
	{"beep", 0x0002, 0x0000, 1},
	{"vibrate", 0x0004, 0x0000, 1},
	{"out-numeric", 0x0008, 0x0000, 8},
	{"out-alpha", 0x0010, 0x0000, 8},
	{"push", 0x0000, 0x0001, 1},
	{"twist", 0x0000, 0x0002, 1},
	{"in-numeric", 0x0000, 0x0004, 8},
	{"in-alpha", 0x0000, 0x0008, 8}
};

static struct oob_info oob_table[] = {
	{"other", 0x0001},
	{"uri", 0x0002},
	{"machine-code-2d", 0x0004},
	{"barcode", 0x0008},
	{"nfc", 0x0010},
	{"number", 0x0020},
	{"string", 0x0040},
	{"on-box", 0x0800},
	{"in-box", 0x1000},
	{"on-paper", 0x2000},
	{"in-manual", 0x4000},
	{"on-device", 0x8000}
};

static struct l_queue *agents;

static bool simple_match(const void *a, const void *b)
{
	return a == b;
}

static bool parse_prov_caps(struct mesh_agent_prov_caps *caps,
				struct l_dbus_message_iter *property)
{
	struct l_dbus_message_iter iter_caps;
	const char *str;
	uint32_t i;

	if (!l_dbus_message_iter_get_variant(property, "as", &iter_caps))
		return false;

	while (l_dbus_message_iter_next_entry(&iter_caps, &str)) {
		for (i = 0; i < L_ARRAY_SIZE(cap_table); i++) {
			if (strcmp(str, cap_table[i].action))
				continue;

			caps->output_action |= cap_table[i].output;
			if (cap_table[i].output &&
					caps->output_size < cap_table[i].size)
				caps->output_size = cap_table[i].size;

			caps->input_action |= cap_table[i].input;
			if (cap_table[i].input &&
					caps->input_size < cap_table[i].size)
				caps->input_size = cap_table[i].size;

			break;
		}

		if (!strcmp(str, "public-oob"))
			caps->pub_type = 1;
		else if (!strcmp(str, "static-oob"))
			caps->static_type = 1;
	}

	return true;
}

static bool parse_oob_info(struct mesh_agent_prov_caps *caps,
				struct l_dbus_message_iter *property)
{
	struct l_dbus_message_iter iter_oob;
	uint32_t i;
	const char *str;

	if (!l_dbus_message_iter_get_variant(property, "as", &iter_oob))
		return false;

	while (l_dbus_message_iter_next_entry(&iter_oob, &str)) {
		for (i = 0; i < L_ARRAY_SIZE(oob_table); i++) {
			if (strcmp(str, oob_table[i].oob))
				continue;
			caps->oob_info |= oob_table[i].mask;
		}
	}

	return true;
}

static bool parse_properties(struct mesh_agent *agent,
					struct l_dbus_message_iter *properties)
{
	const char *key, *uri_string;
	struct l_dbus_message_iter variant;

	memset(&agent->caps, 0, sizeof(agent->caps));

	while (l_dbus_message_iter_next_entry(properties, &key, &variant)) {
		if (!strcmp(key, "Capabilities")) {
			if (!parse_prov_caps(&agent->caps, &variant))
				return false;
		} else if (!strcmp(key, "URI")) {
			if (!l_dbus_message_iter_get_variant(&variant, "s",
								&uri_string))
				return false;
			/* TODO: compute hash */
		} else if (!strcmp(key, "OutOfBandInfo")) {
			if (!parse_oob_info(&agent->caps, &variant))
				return false;
		}
	}

	return true;
}

static void agent_free(void *agent_data)
{
	struct mesh_agent *agent = agent_data;
	int err;
	mesh_agent_cb_t simple_cb;
	mesh_agent_key_cb_t key_cb;
	mesh_agent_number_cb_t number_cb;

	err = MESH_ERROR_DOES_NOT_EXIST;

	if (agent->req && agent->req->cb) {
		struct agent_request *req = agent->req;

		switch (req->type) {
		case MESH_AGENT_REQUEST_PUSH:
		case MESH_AGENT_REQUEST_TWIST:
		case MESH_AGENT_REQUEST_IN_NUMERIC:
			number_cb = req->cb;
			number_cb(req->user_data, err, 0);
			break;
		case MESH_AGENT_REQUEST_IN_ALPHA:
		case MESH_AGENT_REQUEST_STATIC_OOB:
		case MESH_AGENT_REQUEST_PRIVATE_KEY:
		case MESH_AGENT_REQUEST_PUBLIC_KEY:
			key_cb = req->cb;
			key_cb(req->user_data, err, NULL, 0);
			break;
		case MESH_AGENT_REQUEST_BLINK:
		case MESH_AGENT_REQUEST_BEEP:
		case MESH_AGENT_REQUEST_VIBRATE:
		case MESH_AGENT_REQUEST_OUT_NUMERIC:
		case MESH_AGENT_REQUEST_OUT_ALPHA:
		case MESH_AGENT_REQUEST_CAPABILITIES:
			simple_cb = agent->req->cb;
			simple_cb(req->user_data, err);
		default:
			break;
		}

		l_dbus_message_unref(req->msg);
		l_free(req);
	}

	l_free(agent->path);
	l_free(agent->owner);
	l_free(agent);
}

void mesh_agent_remove(struct mesh_agent *agent)
{
	if (!agent)
		return;

	if (l_queue_remove(agents, agent))
		agent_free(agent);
}

void mesh_agent_cleanup(void)
{
	if (!agents)
		return;

	l_queue_destroy(agents, agent_free);
	agents = NULL;

}

void mesh_agent_init(void)
{
	if (!agents)
		agents = l_queue_new();
}

struct mesh_agent *mesh_agent_create(const char *path, const char *owner,
					struct l_dbus_message_iter *properties)
{
	struct mesh_agent *agent;

	agent = l_new(struct mesh_agent, 1);
	agent->owner = l_strdup(owner);
	agent->path = l_strdup(path);

	if (!parse_properties(agent, properties)) {
		l_free(agent);
		return NULL;
	}

	l_queue_push_tail(agents, agent);

	return agent;
}

struct mesh_agent_prov_caps *mesh_agent_get_caps(struct mesh_agent *agent)
{
	if (!agent || !l_queue_find(agents, simple_match, agent))
		return NULL;

	return &agent->caps;
}

static struct agent_request *create_request(agent_request_type_t type,
						void *cb, void *data)
{
	struct agent_request *req;

	req = l_new(struct agent_request, 1);

	req->type = type;
	req->cb = cb;
	req->user_data = data;

	return req;
}

static int get_reply_error(struct l_dbus_message *reply)
{
	const char *name, *desc;

	if (l_dbus_message_is_error(reply)) {

		l_dbus_message_get_error(reply, &name, &desc);
		l_error("Agent failed (%s), %s", name, desc);
		return MESH_ERROR_FAILED;
	}

	return MESH_ERROR_NONE;
}

static void properties_reply(struct l_dbus_message *reply, void *user_data)
{
	struct mesh_agent *agent = user_data;
	struct agent_request *req;
	mesh_agent_cb_t cb;
	struct l_dbus_message_iter properties;
	int err;

	if (!l_queue_find(agents, simple_match, agent) || !agent->req)
		return;

	req = agent->req;

	err = get_reply_error(reply);

	if (err != MESH_ERROR_NONE)
		goto done;

	if (!l_dbus_message_get_arguments(reply, "a{sv}", &properties)) {
		err = MESH_ERROR_FAILED;
		goto done;
	}

	if (!parse_properties(agent, &properties))
		err = MESH_ERROR_FAILED;

done:
	if (req->cb) {
		cb = req->cb;
		cb(req->user_data, err);
	}

	l_dbus_message_unref(req->msg);
	l_free(req);
	agent->req = NULL;
}

void mesh_agent_refresh(struct mesh_agent *agent, mesh_agent_cb_t cb,
							void *user_data)
{
	struct l_dbus *dbus = dbus_get_bus();
	struct l_dbus_message *msg;
	struct l_dbus_message_builder *builder;

	agent->req = create_request(MESH_AGENT_REQUEST_CAPABILITIES, (void *)cb,
								user_data);

	msg = l_dbus_message_new_method_call(dbus, agent->owner, agent->path,
						L_DBUS_INTERFACE_PROPERTIES,
						"GetAll");

	builder = l_dbus_message_builder_new(msg);
	l_dbus_message_builder_append_basic(builder, 's',
						MESH_PROVISION_AGENT_INTERFACE);
	l_dbus_message_builder_finalize(builder);
	l_dbus_message_builder_destroy(builder);

	l_dbus_send_with_reply(dbus_get_bus(), msg, properties_reply, agent,
									NULL);

	agent->req->msg = l_dbus_message_ref(msg);
}


static void simple_reply(struct l_dbus_message *reply, void *user_data)
{
	struct mesh_agent *agent = user_data;
	struct agent_request *req;
	mesh_agent_cb_t cb;
	int err;

	if (!l_queue_find(agents, simple_match, agent) || !agent->req)
		return;

	req = agent->req;

	err = get_reply_error(reply);

	l_dbus_message_unref(req->msg);

	if (req->cb) {
		cb = req->cb;
		cb(req->user_data, err);
	}

	l_free(req);
	agent->req = NULL;
}

static void numeric_reply(struct l_dbus_message *reply, void *user_data)
{
	struct mesh_agent *agent = user_data;
	struct agent_request *req;
	mesh_agent_number_cb_t cb;
	uint32_t count;
	int err;

	if (!l_queue_find(agents, simple_match, agent) || !agent->req)
		return;

	req = agent->req;

	err = get_reply_error(reply);

	count = 0;

	if (err == MESH_ERROR_NONE) {
		if (!l_dbus_message_get_arguments(reply, "u", &count)) {
			l_error("Failed to retrieve numeric input");
			err = MESH_ERROR_FAILED;
		}
	}

	l_dbus_message_unref(req->msg);

	if (req->cb) {
		cb = req->cb;
		cb(req->user_data, err, count);
	}

	l_free(req);
	agent->req = NULL;
}

static void key_reply(struct l_dbus_message *reply, void *user_data)
{
	struct mesh_agent *agent = user_data;
	struct agent_request *req;
	mesh_agent_key_cb_t cb;
	struct l_dbus_message_iter iter_array;
	uint32_t n = 0, expected_len = 0;
	uint8_t *buf = NULL;
	int err;

	if (!l_queue_find(agents, simple_match, agent) || !agent->req)
		return;

	req = agent->req;

	err = get_reply_error(reply);

	if (err != MESH_ERROR_NONE)
		goto done;

	if (!l_dbus_message_get_arguments(reply, "ay", &iter_array)) {
		l_error("Failed to retrieve key input");
		err = MESH_ERROR_FAILED;
		goto done;
	}

	if (!l_dbus_message_iter_get_fixed_array(&iter_array, &buf, &n)) {
		l_error("Failed to retrieve key input");
		err = MESH_ERROR_FAILED;
		goto done;
	}

	if (req->type == MESH_AGENT_REQUEST_PRIVATE_KEY)
		expected_len = 32;
	else if (req->type == MESH_AGENT_REQUEST_PUBLIC_KEY)
		expected_len = 64;
	else
		expected_len = 16;

	if (n != expected_len) {
		l_error("Bad response length: %u (need %u)", n, expected_len);
		err = MESH_ERROR_FAILED;
		n = 0;
	}

done:
	if (req->cb) {
		cb = req->cb;
		cb(req->user_data, err, buf, n);
	}

	l_dbus_message_unref(req->msg);

	l_free(req);
	agent->req = NULL;
}

static int output_request(struct mesh_agent *agent, const char *action,
					agent_request_type_t type, uint32_t cnt,
					void *cb, void *user_data)
{
	struct l_dbus *dbus = dbus_get_bus();
	struct l_dbus_message *msg;
	struct l_dbus_message_builder *builder;

	if (!l_queue_find(agents, simple_match, agent))
		return MESH_ERROR_DOES_NOT_EXIST;

	if (agent->req)
		return MESH_ERROR_BUSY;

	agent->req = create_request(type, cb, user_data);
	msg = l_dbus_message_new_method_call(dbus, agent->owner, agent->path,
						MESH_PROVISION_AGENT_INTERFACE,
						"DisplayNumeric");

	builder = l_dbus_message_builder_new(msg);
	l_dbus_message_builder_append_basic(builder, 's', action);
	l_dbus_message_builder_append_basic(builder, 'u', &cnt);
	l_dbus_message_builder_finalize(builder);
	l_dbus_message_builder_destroy(builder);

	l_debug("Send DisplayNumeric request to %s %s",
						agent->owner, agent->path);

	l_dbus_send_with_reply(dbus_get_bus(), msg, simple_reply, agent,
									NULL);

	agent->req->msg = l_dbus_message_ref(msg);

	return MESH_ERROR_NONE;
}

static int prompt_input(struct mesh_agent *agent, const char *action,
					agent_request_type_t type, bool numeric,
					void *cb, void *user_data)
{
	struct l_dbus *dbus = dbus_get_bus();
	struct l_dbus_message *msg;
	struct l_dbus_message_builder *builder;
	const char *method_name;
	l_dbus_message_func_t reply_cb;

	if (!l_queue_find(agents, simple_match, agent))
		return MESH_ERROR_DOES_NOT_EXIST;

	if (agent->req)
		return MESH_ERROR_BUSY;

	agent->req = create_request(type, cb, user_data);

	method_name = numeric ? "PromptNumeric" : "PromptStatic";

	msg = l_dbus_message_new_method_call(dbus, agent->owner,
						agent->path,
						MESH_PROVISION_AGENT_INTERFACE,
						method_name);

	builder = l_dbus_message_builder_new(msg);
	l_dbus_message_builder_append_basic(builder, 's', action);
	l_dbus_message_builder_finalize(builder);
	l_dbus_message_builder_destroy(builder);

	l_debug("Send \"%s\" input request to %s %s", action,
						agent->owner, agent->path);

	reply_cb = numeric ? numeric_reply : key_reply;

	l_dbus_send_with_reply(dbus_get_bus(), msg, reply_cb, agent, NULL);

	agent->req->msg = l_dbus_message_ref(msg);

	return MESH_ERROR_NONE;
}

static int request_key(struct mesh_agent *agent,
					agent_request_type_t type,
					void *cb, void *user_data)
{
	struct l_dbus *dbus = dbus_get_bus();
	struct l_dbus_message *msg;
	const char *method_name;

	if (!l_queue_find(agents, simple_match, agent))
		return MESH_ERROR_DOES_NOT_EXIST;

	if (agent->req)
		return MESH_ERROR_BUSY;

	agent->req = create_request(type, cb, user_data);

	method_name = (type == MESH_AGENT_REQUEST_PRIVATE_KEY) ?
						"PrivateKey" : "PublicKey";

	msg = l_dbus_message_new_method_call(dbus, agent->owner,
						agent->path,
						MESH_PROVISION_AGENT_INTERFACE,
						method_name);

	l_dbus_message_set_arguments(msg, "");

	l_debug("Send key request to %s %s", agent->owner, agent->path);

	l_dbus_send_with_reply(dbus_get_bus(), msg, key_reply, agent, NULL);

	agent->req->msg = l_dbus_message_ref(msg);

	return MESH_ERROR_NONE;
}

int mesh_agent_display_string(struct mesh_agent *agent, const char *str,
				mesh_agent_cb_t cb, void *user_data)
{
	struct l_dbus *dbus = dbus_get_bus();
	struct l_dbus_message *msg;
	struct l_dbus_message_builder *builder;

	if (!l_queue_find(agents, simple_match, agent))
		return MESH_ERROR_DOES_NOT_EXIST;

	if (agent->req)
		return MESH_ERROR_BUSY;

	agent->req = create_request(MESH_AGENT_REQUEST_OUT_ALPHA,
								cb, user_data);
	msg = l_dbus_message_new_method_call(dbus, agent->owner, agent->path,
						MESH_PROVISION_AGENT_INTERFACE,
						"DisplayString");

	builder = l_dbus_message_builder_new(msg);
	l_dbus_message_builder_append_basic(builder, 's', str);
	l_dbus_message_builder_finalize(builder);
	l_dbus_message_builder_destroy(builder);

	l_debug("Send DisplayString request to %s %s",
						agent->owner, agent->path);

	l_dbus_send_with_reply(dbus_get_bus(), msg, simple_reply, agent,
									NULL);

	agent->req->msg = l_dbus_message_ref(msg);

	return MESH_ERROR_NONE;

}

int mesh_agent_display_number(struct mesh_agent *agent, bool initiator,
					uint8_t action, uint32_t count,
					mesh_agent_cb_t cb, void *user_data)
{
	const char *str_type;
	agent_request_type_t type;

	type = action;

	if (initiator)
		type = action + MESH_AGENT_REQUEST_PUSH;

	if (type >= L_ARRAY_SIZE(cap_table))
		return MESH_ERROR_INVALID_ARGS;

	str_type = cap_table[type].action;

	return output_request(agent, str_type, type, count, cb, user_data);
}

int mesh_agent_prompt_number(struct mesh_agent *agent, bool initiator,
						uint8_t action,
						mesh_agent_number_cb_t cb,
						void *user_data)
{
	const char *str_type;
	agent_request_type_t type;

	type = action;

	if (!initiator)
		type = action + MESH_AGENT_REQUEST_PUSH;

	if (type >= L_ARRAY_SIZE(cap_table))
		return MESH_ERROR_INVALID_ARGS;

	str_type = cap_table[type].action;

	return prompt_input(agent, str_type, type, true, cb, user_data);
}

int mesh_agent_prompt_alpha(struct mesh_agent *agent, bool initiator,
					mesh_agent_key_cb_t cb, void *user_data)
{
	if (initiator)
		return prompt_input(agent,
				cap_table[MESH_AGENT_REQUEST_OUT_ALPHA].action,
				MESH_AGENT_REQUEST_OUT_ALPHA, false, cb,
				user_data);
	else
		return prompt_input(agent,
				cap_table[MESH_AGENT_REQUEST_IN_ALPHA].action,
				MESH_AGENT_REQUEST_IN_ALPHA, false, cb,
				user_data);
}

int mesh_agent_request_static(struct mesh_agent *agent, mesh_agent_key_cb_t cb,
								void *user_data)
{
	return prompt_input(agent, "static-oob", MESH_AGENT_REQUEST_STATIC_OOB,
							false, cb, user_data);
}

int mesh_agent_request_private_key(struct mesh_agent *agent,
				mesh_agent_key_cb_t cb, void *user_data)
{
	return request_key(agent, MESH_AGENT_REQUEST_PRIVATE_KEY, cb,
								user_data);

}

int mesh_agent_request_public_key(struct mesh_agent *agent,
				mesh_agent_key_cb_t cb, void *user_data)
{
	return request_key(agent, MESH_AGENT_REQUEST_PUBLIC_KEY, cb,
								user_data);
}

void mesh_agent_cancel(struct mesh_agent *agent)
{
	struct l_dbus *dbus = dbus_get_bus();
	struct l_dbus_message *msg;

	if (!l_queue_find(agents, simple_match, agent))
		return;

	msg = l_dbus_message_new_method_call(dbus, agent->owner, agent->path,
						MESH_PROVISION_AGENT_INTERFACE,
						"Cancel");

	l_dbus_message_set_arguments(msg, "");

	l_dbus_send(dbus, msg);
}