aboutsummaryrefslogtreecommitdiff
path: root/lib/pbdrv/pb-write.c
blob: 1a3f24e9e26580c29baeb134a245474bd66dba55 (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
#include <mpack.h>

#include "pb-write.h"

typedef struct {
	mpack_writer_t writer;
	pbdrv_buf_t buf;
} pbdrv_writer_t;

static void pbdrv_write_init(pbdrv_writer_t * writer) {
	mpack_writer_init_growable(&writer->writer, &writer->buf.data, &writer->buf.size);
}

static pbdrv_buf_t pbdrv_write_finish(pbdrv_writer_t * writer) {
	if (mpack_writer_destroy(&writer->writer) != mpack_ok) {
		writer->buf.data = NULL;
		writer->buf.size = 0;
	}
	return writer->buf;
}

static void pbdrv_write_msg_header(pbdrv_writer_t * writer, pb_msg_header_t header) {
	mpack_write_u8(&writer->writer, header.type);
	// TODO: if pbdrv is compiled under the pbdrv-mod target, place
	// PBDRV_MOD_ADDR in this field
	mpack_write_u16(&writer->writer, header.sender);
}

pbdrv_buf_t pbdrv_write_cmd_req_read(pb_cmd_req_read_t data) {
	data.header.type = PB_CMD_REQ_READ;
	pbdrv_writer_t writer;
	pbdrv_write_init(&writer);

	pbdrv_write_msg_header(&writer, data.header);
	mpack_write_u8(&writer.writer, data.propid);

	return pbdrv_write_finish(&writer);
}

pbdrv_buf_t pbdrv_write_cmd_res_read(pb_cmd_res_read_t data) {
	data.header.type = PB_CMD_RES_READ;
	pbdrv_writer_t writer;
	pbdrv_write_init(&writer);

	pbdrv_write_msg_header(&writer, data.header);
	mpack_write_u8(&writer.writer, data.propid);
	mpack_write_bin(&writer.writer, (char *) data.value, data._value_size);

	return pbdrv_write_finish(&writer);
}

pbdrv_buf_t pbdrv_write_cmd_req_write(pb_cmd_req_write_t data) {
	data.header.type = PB_CMD_REQ_WRITE;
	pbdrv_writer_t writer;
	pbdrv_write_init(&writer);

	pbdrv_write_msg_header(&writer, data.header);
	mpack_write_u8(&writer.writer, data.propid);
	mpack_write_bin(&writer.writer, (char *) data.value, data._value_size);

	return pbdrv_write_finish(&writer);
}

pbdrv_buf_t pbdrv_write_cmd_req_state(pb_cmd_req_state_t data) {
	data.header.type = PB_CMD_REQ_STATE;
	pbdrv_writer_t writer;
	pbdrv_write_init(&writer);

	pbdrv_write_msg_header(&writer, data.header);
	mpack_write_u8(&writer.writer, data.state);

	return pbdrv_write_finish(&writer);
}

pbdrv_buf_t pbdrv_write_cmd_res_state(pb_cmd_res_state_t data) {
	data.header.type = PB_CMD_RES_STATE;
	pbdrv_writer_t writer;
	pbdrv_write_init(&writer);

	pbdrv_write_msg_header(&writer, data.header);
	mpack_write_u8(&writer.writer, data.state);

	return pbdrv_write_finish(&writer);
}

pbdrv_buf_t pbdrv_write_cmd_req_set_state(pb_cmd_req_set_state_t data) {
	data.header.type = PB_CMD_REQ_SET_STATE;
	pbdrv_writer_t writer;
	pbdrv_write_init(&writer);

	pbdrv_write_msg_header(&writer, data.header);
	mpack_write_u8(&writer.writer, data.state);

	return pbdrv_write_finish(&writer);
}

pbdrv_buf_t pbdrv_write_cmd_magic(pb_cmd_magic_t data) {
	data.header.type = PB_CMD_MAGIC;
	pbdrv_writer_t writer;
	pbdrv_write_init(&writer);

	pbdrv_write_msg_header(&writer, data.header);
	mpack_write_bin(&writer.writer, data.magic, data._magic_size);

	return pbdrv_write_finish(&writer);
}