forked from apache/nuttx-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnxscope_pser.c
293 lines (225 loc) · 7.94 KB
/
nxscope_pser.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
/****************************************************************************
* apps/logging/nxscope/nxscope_pser.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <assert.h>
#include <debug.h>
#include <endian.h>
#include <errno.h>
#include <string.h>
#include <nuttx/crc16.h>
#include <logging/nxscope/nxscope.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define NXSCOPE_HDR_LEN (sizeof(struct nxscope_hdr_s))
#define NXSCOPE_DATA_START (NXSCOPE_HDR_LEN)
#define NXSCOPE_BASEFRAME_LEN (NXSCOPE_HDR_LEN + NXSCOPE_CRC_LEN)
#define NXSCOPE_HDR_SOF (0x55)
#define NXSCOPE_CRC_LEN (sizeof(uint16_t))
/****************************************************************************
* Private Type Definition
****************************************************************************/
/* Nxscope serial protocol frame:
* +--------------------+------------+-------------+
* | HDR [4B] | frame data | footer [2B] |
* +-----+---------+----+------------+-------------+
* | SOF | len [1] | id | frame data | crc16 [2] |
* +-----+----+----+----+------------+------+------+
* | 0 | 1 | 2 | 3 | ... | n+1 | n+2 |
* +-----+----+----+----+------------+------+------+
*
* [1] - always little-endian
* [2] - always big-endian
*/
/* Nxscope header */
begin_packed_struct struct nxscope_hdr_s
{
uint8_t sof; /* SOF */
uint16_t len; /* Frame length */
uint8_t id; /* Frame ID */
} end_packed_struct;
/* Nxscope footer */
begin_packed_struct struct nxscope_footer_s
{
uint16_t crc16; /* check sum (see nxscope_frame_final()) */
} end_packed_struct;
/****************************************************************************
* Private Function Protototypes
****************************************************************************/
static int nxscope_frame_get(FAR struct nxscope_proto_s *p,
FAR uint8_t *buff, size_t len,
FAR struct nxscope_frame_s *frame);
static int nxscope_frame_final(FAR struct nxscope_proto_s *p,
uint8_t id,
FAR uint8_t *buff, FAR size_t *len);
/****************************************************************************
* Public Data
****************************************************************************/
static struct nxscope_proto_ops_s g_nxscope_proto_ser_ops =
{
nxscope_frame_get,
nxscope_frame_final,
};
static struct nxscope_proto_s g_nxscope_proto_ser =
{
false,
NULL,
&g_nxscope_proto_ser_ops,
(size_t)NXSCOPE_HDR_LEN,
(size_t)NXSCOPE_CRC_LEN
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: nxscope_hdr_fill
****************************************************************************/
static void nxscope_hdr_fill(FAR uint8_t *buff, uint8_t id, uint16_t len)
{
FAR struct nxscope_hdr_s *hdr = NULL;
DEBUGASSERT(buff);
hdr = (FAR struct nxscope_hdr_s *)buff;
hdr->sof = NXSCOPE_HDR_SOF;
/* Length always as little endian */
hdr->len = htole16(len);
hdr->id = id;
}
/****************************************************************************
* Name: nxscope_frame_get
****************************************************************************/
static int nxscope_frame_get(FAR struct nxscope_proto_s *p,
FAR uint8_t *buff, size_t len,
FAR struct nxscope_frame_s *frame)
{
FAR struct nxscope_hdr_s *hdr = NULL;
uint16_t crc = 0;
int ret = OK;
int i = 0;
DEBUGASSERT(p);
DEBUGASSERT(buff);
DEBUGASSERT(frame);
UNUSED(p);
/* Find SOF */
for (i = 0; i < len - NXSCOPE_HDR_LEN; i++)
{
hdr = (FAR struct nxscope_hdr_s *)&buff[i];
/* Verify SOF */
if (hdr->sof == NXSCOPE_HDR_SOF)
{
break;
}
}
if (hdr->sof != NXSCOPE_HDR_SOF)
{
ret = -EINVAL;
goto errout;
}
/* Verify len - always little-endian */
hdr->len = htole16(hdr->len);
if ((len - i) < hdr->len)
{
ret = -EINVAL;
goto errout;
}
/* Verify crc16 for the whole frame */
crc = crc16(&buff[i], hdr->len);
if (crc != 0)
{
_err("ERROR: invalid crc16 %d\n", crc);
ret = -EINVAL;
goto errout;
}
/* Return the frame data */
frame->id = hdr->id;
frame->data = &buff[i + NXSCOPE_DATA_START];
frame->drop = hdr->len + i;
frame->dlen = hdr->len - NXSCOPE_BASEFRAME_LEN;
errout:
return ret;
}
/****************************************************************************
* Name: nxscope_frame_final
****************************************************************************/
static int nxscope_frame_final(FAR struct nxscope_proto_s *p,
uint8_t id,
FAR uint8_t *buff, FAR size_t *len)
{
uint16_t crc = 0;
int ret = OK;
DEBUGASSERT(p);
DEBUGASSERT(buff);
DEBUGASSERT(len);
if (*len <= NXSCOPE_HDR_LEN)
{
/* No data */
ret = -ENODATA;
goto errout;
}
/* Fill hdr */
nxscope_hdr_fill(buff, id, *len + NXSCOPE_CRC_LEN);
/* Add crc16 - always as big-endian.
*
* crc16 xmodem:
* polynominal = 0x1021
* initial value = 0x0000
* final xor value = 0x0000
*/
crc = crc16(buff, *len);
#ifdef CONFIG_ENDIAN_BIG
buff[(*len)++] = (crc >> 0) & 0xff;
buff[(*len)++] = (crc >> 8) & 0xff;
#else
buff[(*len)++] = (crc >> 8) & 0xff;
buff[(*len)++] = (crc >> 0) & 0xff;
#endif
errout:
return ret;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: nxscope_proto_ser_init
****************************************************************************/
int nxscope_proto_ser_init(FAR struct nxscope_proto_s *proto, FAR void *cfg)
{
DEBUGASSERT(proto);
/* cfg argument not used, but keept here for compatibility with
* future protocol implementations.
*/
UNUSED(cfg);
/* Just copy protocol data */
memcpy(proto, &g_nxscope_proto_ser, sizeof(struct nxscope_proto_s));
/* Initialized */
proto->initialized = true;
return OK;
}
/****************************************************************************
* Name: nxscope_proto_ser_deinit
****************************************************************************/
void nxscope_proto_ser_deinit(FAR struct nxscope_proto_s *proto)
{
DEBUGASSERT(proto);
/* Nothings here */
UNUSED(proto);
}