1 /*
2  * Copyright Runtime.io 2018. All rights reserved.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <string.h>
8 #include "net/buf.h"
9 #include "mgmt/mcumgr/buf.h"
10 #include <tinycbor/compilersupport_p.h>
11 
12 NET_BUF_POOL_DEFINE(pkt_pool, CONFIG_MCUMGR_BUF_COUNT, CONFIG_MCUMGR_BUF_SIZE,
13 		    CONFIG_MCUMGR_BUF_USER_DATA_SIZE, NULL);
14 
15 struct net_buf *
mcumgr_buf_alloc(void)16 mcumgr_buf_alloc(void)
17 {
18 	return net_buf_alloc(&pkt_pool, K_NO_WAIT);
19 }
20 
21 void
mcumgr_buf_free(struct net_buf * nb)22 mcumgr_buf_free(struct net_buf *nb)
23 {
24 	net_buf_unref(nb);
25 }
26 
27 static uint8_t
cbor_nb_reader_get8(struct cbor_decoder_reader * d,int offset)28 cbor_nb_reader_get8(struct cbor_decoder_reader *d, int offset)
29 {
30 	struct cbor_nb_reader *cnr;
31 
32 	cnr = (struct cbor_nb_reader *) d;
33 
34 	if (offset < 0 || offset >= cnr->nb->len) {
35 		return UINT8_MAX;
36 	}
37 
38 	return cnr->nb->data[offset];
39 }
40 
41 static uint16_t
cbor_nb_reader_get16(struct cbor_decoder_reader * d,int offset)42 cbor_nb_reader_get16(struct cbor_decoder_reader *d, int offset)
43 {
44 	struct cbor_nb_reader *cnr;
45 	uint16_t val;
46 
47 	cnr = (struct cbor_nb_reader *) d;
48 
49 	if (offset < 0 || offset > cnr->nb->len - (int)sizeof(val)) {
50 		return UINT16_MAX;
51 	}
52 
53 	memcpy(&val, cnr->nb->data + offset, sizeof(val));
54 	return cbor_ntohs(val);
55 }
56 
57 static uint32_t
cbor_nb_reader_get32(struct cbor_decoder_reader * d,int offset)58 cbor_nb_reader_get32(struct cbor_decoder_reader *d, int offset)
59 {
60 	struct cbor_nb_reader *cnr;
61 	uint32_t val;
62 
63 	cnr = (struct cbor_nb_reader *) d;
64 
65 	if (offset < 0 || offset > cnr->nb->len - (int)sizeof(val)) {
66 		return UINT32_MAX;
67 	}
68 
69 	memcpy(&val, cnr->nb->data + offset, sizeof(val));
70 	return cbor_ntohl(val);
71 }
72 
73 static uint64_t
cbor_nb_reader_get64(struct cbor_decoder_reader * d,int offset)74 cbor_nb_reader_get64(struct cbor_decoder_reader *d, int offset)
75 {
76 	struct cbor_nb_reader *cnr;
77 	uint64_t val;
78 
79 	cnr = (struct cbor_nb_reader *) d;
80 
81 	if (offset < 0 || offset > cnr->nb->len - (int)sizeof(val)) {
82 		return UINT64_MAX;
83 	}
84 
85 	memcpy(&val, cnr->nb->data + offset, sizeof(val));
86 	return cbor_ntohll(val);
87 }
88 
89 static uintptr_t
cbor_nb_reader_cmp(struct cbor_decoder_reader * d,char * buf,int offset,size_t len)90 cbor_nb_reader_cmp(struct cbor_decoder_reader *d, char *buf, int offset,
91 		   size_t len)
92 {
93 	struct cbor_nb_reader *cnr;
94 
95 	cnr = (struct cbor_nb_reader *) d;
96 
97 	if (offset < 0 || offset > cnr->nb->len - (int)len) {
98 		return -1;
99 	}
100 
101 	return memcmp(cnr->nb->data + offset, buf, len);
102 }
103 
104 static uintptr_t
cbor_nb_reader_cpy(struct cbor_decoder_reader * d,char * dst,int offset,size_t len)105 cbor_nb_reader_cpy(struct cbor_decoder_reader *d, char *dst, int offset,
106 		   size_t len)
107 {
108 	struct cbor_nb_reader *cnr;
109 
110 	cnr = (struct cbor_nb_reader *) d;
111 
112 	if (offset < 0 || offset > cnr->nb->len - (int)len) {
113 		return -1;
114 	}
115 
116 	return (uintptr_t)memcpy(dst, cnr->nb->data + offset, len);
117 }
118 
119 void
cbor_nb_reader_init(struct cbor_nb_reader * cnr,struct net_buf * nb)120 cbor_nb_reader_init(struct cbor_nb_reader *cnr,
121 		    struct net_buf *nb)
122 {
123 	cnr->r.get8 = &cbor_nb_reader_get8;
124 	cnr->r.get16 = &cbor_nb_reader_get16;
125 	cnr->r.get32 = &cbor_nb_reader_get32;
126 	cnr->r.get64 = &cbor_nb_reader_get64;
127 	cnr->r.cmp = &cbor_nb_reader_cmp;
128 	cnr->r.cpy = &cbor_nb_reader_cpy;
129 
130 	cnr->nb = nb;
131 	cnr->r.message_size = nb->len;
132 }
133 
134 static int
cbor_nb_write(struct cbor_encoder_writer * writer,const char * data,int len)135 cbor_nb_write(struct cbor_encoder_writer *writer, const char *data, int len)
136 {
137 	struct cbor_nb_writer *cnw;
138 
139 	cnw = (struct cbor_nb_writer *) writer;
140 	if (len > net_buf_tailroom(cnw->nb)) {
141 		return CborErrorOutOfMemory;
142 	}
143 
144 	net_buf_add_mem(cnw->nb, data, len);
145 	cnw->enc.bytes_written += len;
146 
147 	return CborNoError;
148 }
149 
150 void
cbor_nb_writer_init(struct cbor_nb_writer * cnw,struct net_buf * nb)151 cbor_nb_writer_init(struct cbor_nb_writer *cnw, struct net_buf *nb)
152 {
153 	cnw->nb = nb;
154 	cnw->enc.bytes_written = 0;
155 	cnw->enc.write = &cbor_nb_write;
156 }
157