Repository navigation
Expand file tree
/
Copy pathshtp.c
More file actions
405 lines (327 loc) · 10.9 KB
/
Copy pathshtp.c
File metadata and controls
405 lines (327 loc) · 10.9 KB
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
/*
* Copyright 2015-21 CEVA, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License and
* any applicable agreements you may have with CEVA, Inc.
* 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.
*/
/*
* Sensor Hub Transport Protocol (SHTP) API
*/
#include "shtp.h"
#include "sh2_err.h"
#include <string.h>
// ------------------------------------------------------------------------
// Private types
#define SHTP_INSTANCES (1) // Number of SHTP devices supported
#define SHTP_MAX_CHANS (8) // Max channels per SHTP device
#define SHTP_HDR_LEN (4)
typedef struct shtp_Channel_s {
uint8_t nextOutSeq;
uint8_t nextInSeq;
shtp_Callback_t *callback;
void *cookie;
} shtp_Channel_t;
// Per-instance data for SHTP
typedef struct shtp_s {
// Associated SHTP HAL
// If 0, this indicates the SHTP instance is available for new opens
sh2_Hal_t *pHal;
// Asynchronous Event callback and it's cookie
shtp_EventCallback_t *eventCallback;
void * eventCookie;
// Transmit support
uint8_t outTransfer[SH2_HAL_MAX_TRANSFER_OUT];
// Receive support
uint16_t inRemaining;
uint8_t inChan;
uint8_t inPayload[SH2_HAL_MAX_PAYLOAD_IN];
uint16_t inCursor;
uint32_t inTimestamp;
uint8_t inTransfer[SH2_HAL_MAX_TRANSFER_IN];
// SHTP Channels
shtp_Channel_t chan[SHTP_MAX_CHANS];
// Stats
uint32_t rxBadChan;
uint32_t rxShortFragments;
uint32_t rxTooLargePayloads;
uint32_t rxInterruptedPayloads;
uint32_t badTxChan;
uint32_t txDiscards;
uint32_t txTooLargePayloads;
} shtp_t;
// ------------------------------------------------------------------------
// Private data
static shtp_t instances[SHTP_INSTANCES];
static bool shtp_initialized = false;
// ------------------------------------------------------------------------
// Private functions
static void shtp_init(void)
{
// Clear pHal pointer in every instance. This marks them as unallocated.
for (int n = 0; n < SHTP_INSTANCES; n++) {
instances[n].pHal = 0;
}
// Set the initialized flag so this doesn't happen again.
shtp_initialized = true;
}
static shtp_t *getInstance(void)
{
for (int n = 0; n < SHTP_INSTANCES; n++) {
if (instances[n].pHal == 0) {
// This instance is free
return &instances[n];
}
}
// Can't give an instance, none are free
return 0;
}
static inline uint16_t min_u16(uint16_t a, uint16_t b)
{
if (a < b) {
return a;
}
else {
return b;
}
}
// Send a cargo as a sequence of transports
static int txProcess(shtp_t *pShtp, uint8_t chan, const uint8_t* pData, uint32_t len)
{
int status = SH2_OK;
bool continuation = false;
uint16_t cursor = 0;
uint16_t remaining;
uint16_t transferLen; // length of transfer, minus the header
uint16_t lenField;
cursor = 0;
remaining = len;
while (remaining > 0) {
// How much data (not header) can we send in next transfer
transferLen = min_u16(remaining, SH2_HAL_MAX_TRANSFER_OUT-SHTP_HDR_LEN);
// Length field will be transferLen + SHTP_HDR_LEN
lenField = transferLen + SHTP_HDR_LEN;
// Put the header in the out buffer
pShtp->outTransfer[0] = lenField & 0xFF;
pShtp->outTransfer[1] = (lenField >> 8) & 0x7F;
if (continuation) {
pShtp->outTransfer[1] |= 0x80;
}
pShtp->outTransfer[2] = chan;
pShtp->outTransfer[3] = pShtp->chan[chan].nextOutSeq++;
// Stage one tranfer in the out buffer
memcpy(pShtp->outTransfer+SHTP_HDR_LEN, pData+cursor, transferLen);
remaining -= transferLen;
cursor += transferLen;
// Transmit (try repeatedly while HAL write returns 0)
status = pShtp->pHal->write(pShtp->pHal, pShtp->outTransfer, lenField);
while (status == 0)
{
shtp_service(pShtp);
status = pShtp->pHal->write(pShtp->pHal, pShtp->outTransfer, lenField);
}
if (status < 0)
{
// Error, throw away this cargo
pShtp->txDiscards++;
return status;
}
// For the rest of this transmission, packets are continuations.
continuation = true;
}
return SH2_OK;
}
static void rxAssemble(shtp_t *pShtp, uint8_t *in, uint16_t len, uint32_t t_us)
{
uint16_t payloadLen;
bool continuation;
uint8_t chan = 0;
uint8_t seq = 0;
// discard invalid short fragments
if (len < SHTP_HDR_LEN) {
pShtp->rxShortFragments++;
if (pShtp->eventCallback) {
pShtp->eventCallback(pShtp->eventCookie, SHTP_SHORT_FRAGMENT);
}
return;
}
// Interpret header fields
payloadLen = (in[0] + (in[1] << 8)) & (~0x8000);
continuation = ((in[1] & 0x80) != 0);
chan = in[2];
seq = in[3];
if (chan >= SHTP_MAX_CHANS) {
// Invalid channel id.
pShtp->rxBadChan++;
if (pShtp->eventCallback) {
pShtp->eventCallback(pShtp->eventCookie, SHTP_BAD_RX_CHAN);
}
return;
}
if (seq != pShtp->chan[chan].nextInSeq){
if (pShtp->eventCallback) {
pShtp->eventCallback(pShtp->eventCookie,
SHTP_BAD_SN);
}
}
if (payloadLen < SHTP_HDR_LEN) {
pShtp->rxShortFragments++;
if (pShtp->eventCallback) {
pShtp->eventCallback(pShtp->eventCookie, SHTP_SHORT_FRAGMENT);
}
return;
}
// Discard earlier assembly in progress if the received data doesn't match it.
if (pShtp->inRemaining) {
// Check this against previously received data.
if (!continuation ||
(chan != pShtp->inChan) ||
(seq != pShtp->chan[chan].nextInSeq) ||
(payloadLen-SHTP_HDR_LEN != pShtp->inRemaining)) {
if (pShtp->eventCallback) {
pShtp->eventCallback(pShtp->eventCookie,
SHTP_BAD_FRAGMENT);
}
// This fragment doesn't fit with previous one, discard earlier data
pShtp->inRemaining = 0;
pShtp->rxInterruptedPayloads++;
if (pShtp->eventCallback) {
pShtp->eventCallback(pShtp->eventCookie, SHTP_INTERRUPTED_PAYLOAD);
}
}
}
// Remember next sequence number we expect for this channel.
pShtp->chan[chan].nextInSeq = seq + 1;
if (pShtp->inRemaining == 0) {
if (payloadLen > sizeof(pShtp->inPayload)) {
// Error: This payload won't fit! Discard it.
pShtp->rxTooLargePayloads++;
if (pShtp->eventCallback) {
pShtp->eventCallback(pShtp->eventCookie, SHTP_TOO_LARGE_PAYLOADS);
}
return;
}
// This represents a new payload
// Store timestamp
pShtp->inTimestamp = t_us;
// Start a new assembly.
pShtp->inCursor = 0;
pShtp->inChan = chan;
}
// Append the new fragment to the payload under construction.
if (len > payloadLen) {
// Only use the valid portion of the transfer
len = payloadLen;
}
memcpy(pShtp->inPayload + pShtp->inCursor, in+SHTP_HDR_LEN, len-SHTP_HDR_LEN);
pShtp->inCursor += len-SHTP_HDR_LEN;
pShtp->inRemaining = payloadLen - len;
// If whole payload received, deliver it to channel listener.
if (pShtp->inRemaining == 0) {
// Call callback if there is one.
if (pShtp->chan[chan].callback != 0) {
pShtp->chan[chan].callback(pShtp->chan[chan].cookie,
pShtp->inPayload, pShtp->inCursor,
pShtp->inTimestamp);
}
}
}
// ------------------------------------------------------------------------
// Public functions
// Takes HAL pointer, returns shtp ID for use in future calls.
// HAL will be opened by this call.
void *shtp_open(sh2_Hal_t *pHal)
{
if (!shtp_initialized) {
// Perform one-time module initialization
shtp_init();
}
// Validate params
if (pHal == 0) {
// Error
return 0;
}
// Find an available instance for this open
shtp_t *pShtp = getInstance();
if (pShtp == 0) {
// No instances available, return error
return 0;
}
// Clear the SHTP instance as a shortcut to initializing all fields
memset(pShtp, 0, sizeof(shtp_t));
// Open HAL
int status = pHal->open(pHal);
if (status != SH2_OK) {
return 0;
}
// Store reference to the HAL
pShtp->pHal = pHal;
return pShtp;
}
// Releases resources associated with this SHTP instance.
// HAL will not be closed.
void shtp_close(void *pInstance)
{
shtp_t *pShtp = (shtp_t *)pInstance;
pShtp->pHal->close(pShtp->pHal);
// Deallocate the SHTP instance.
// (Setting pHal to 0 marks it as free.)
pShtp->pHal = 0;
}
// Register the pointer of the callback function for reporting asynchronous events
void shtp_setEventCallback(void *pInstance,
shtp_EventCallback_t * eventCallback,
void *eventCookie) {
shtp_t *pShtp = (shtp_t *)pInstance;
pShtp->eventCallback = eventCallback;
pShtp->eventCookie = eventCookie;
}
// Register a listener for an SHTP channel
int shtp_listenChan(void *pInstance,
uint8_t channel,
shtp_Callback_t *callback, void * cookie)
{
shtp_t *pShtp = (shtp_t *)pInstance;
// Balk if channel is invalid
if ((channel == 0) || (channel >= SHTP_MAX_CHANS)) {
return SH2_ERR_BAD_PARAM;
}
pShtp->chan[channel].callback = callback;
pShtp->chan[channel].cookie = cookie;
return SH2_OK;
}
// Send an SHTP payload on a particular channel
int shtp_send(void *pInstance,
uint8_t channel,
const uint8_t *payload, uint16_t len)
{
shtp_t *pShtp = (shtp_t *)pInstance;
if (len > SH2_HAL_MAX_PAYLOAD_OUT) {
pShtp->txTooLargePayloads++;
return SH2_ERR_BAD_PARAM;
}
if (channel >= SHTP_MAX_CHANS) {
pShtp->badTxChan++;
return SH2_ERR_BAD_PARAM;
}
return txProcess(pShtp, channel, payload, len);
}
// Check for received data and process it.
void shtp_service(void *pInstance)
{
shtp_t *pShtp = (shtp_t *)pInstance;
uint32_t t_us = 0;
int len = pShtp->pHal->read(pShtp->pHal, pShtp->inTransfer, sizeof(pShtp->inTransfer), &t_us);
if (len > 0) {
rxAssemble(pShtp, pShtp->inTransfer, len, t_us);
}
}