summaryrefslogtreecommitdiff
path: root/common/blob.c
blob: 5e563598a3b787153ff8f5fe80a1c17847cb2c21 (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
/* Copyright 2015 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/* Handle an opaque blob of data */

#include "blob.h"
#include "common.h"
#include "console.h"
#include "printf.h"
#include "queue.h"
#include "task.h"
#include "util.h"

#define CPRINTS(format, args...) cprints(CC_USB, format, ## args)

#define INCOMING_QUEUE_SIZE 100
#define OUTGOING_QUEUE_SIZE 100


static void incoming_add(struct queue_policy const *queue_policy, size_t count)
{
	task_wake(TASK_ID_BLOB);
}

static void incoming_remove(struct queue_policy const *queue_policy,
			    size_t count)
{
	blob_is_ready_for_more_bytes();
}

static struct queue_policy const incoming_policy = {
	.add = incoming_add,
	.remove = incoming_remove,
};

static void outgoing_add(struct queue_policy const *queue_policy, size_t count)
{
	blob_is_ready_to_emit_bytes();
}

static void outgoing_remove(struct queue_policy const *queue_policy,
			    size_t count)
{
	/* we don't care */
}

static struct queue_policy const outgoing_policy = {
	.add = outgoing_add,
	.remove = outgoing_remove,
};

static struct queue const incoming_q = QUEUE(INCOMING_QUEUE_SIZE, uint8_t,
					     incoming_policy);

static struct queue const outgoing_q = QUEUE(OUTGOING_QUEUE_SIZE, uint8_t,
					     outgoing_policy);


/* Call this to send data to the blob-handler */
size_t put_bytes_to_blob(uint8_t *buffer, size_t count)
{
	return QUEUE_ADD_UNITS(&incoming_q, buffer, count);
}

/* Call this to get data back fom the blob-handler */
size_t get_bytes_from_blob(uint8_t *buffer, size_t count)
{
	return QUEUE_REMOVE_UNITS(&outgoing_q, buffer, count);
}

#define WEAK_FUNC(FOO)							\
	void __ ## FOO(void) {}						\
	void FOO(void)							\
		__attribute__((weak, alias(STRINGIFY(CONCAT2(__, FOO)))))

/* Default callbacks for outsiders */
WEAK_FUNC(blob_is_ready_for_more_bytes);
WEAK_FUNC(blob_is_ready_to_emit_bytes);

/* Do the magic */
void blob_task(void)
{
	static uint8_t buf[INCOMING_QUEUE_SIZE];
	size_t count, i;
	task_id_t me = task_get_current();

	while (1) {
		CPRINTS("task %d waiting for events...", me);
		task_wait_event(-1);
		CPRINTS("task %d awakened!", me);

		count = QUEUE_REMOVE_UNITS(&incoming_q, buf, sizeof(buf));

		CPRINTS("task %d gets: count=%d buf=((%s))", me, count, buf);

		/*
		 * Just to have something to test to begin with, we'll
		 * implement "tr a-zA-Z A-Za-z" and return the result.
		 */
		for (i = 0; i < count; i++) {
			char tmp = buf[i];
			if (tmp >= 'a' && tmp <= 'z')
				buf[i] = tmp - ('a' - 'A');
			else if (tmp >= 'A' && tmp <= 'Z')
				buf[i] = tmp + ('a' - 'A');
		}

		count = QUEUE_ADD_UNITS(&outgoing_q, buf, count);
		CPRINTS("task %d puts: count=%d buf=((%s))", me, buf);
	}
}