forked from vk2tds/libosdp_arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
osdp_file.c
368 lines (305 loc) · 8.24 KB
/
osdp_file.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
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
/*
* Copyright (c) 2021 Siddharth Chandrasekaran <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* The following define is just to cheat IDEs; normally it is provided by build
* system and this file is not complied if not configured.
*/
#ifndef CONFIG_OSDP_FILE
#define CONFIG_OSDP_FILE
#endif
//vk2tds
#define OSDP_EXPORT
#define OSDP_NO_EXPORT
#include <stdlib.h>
#include "osdp_file.h"
static inline void file_state_reset(struct osdp_file *f)
{
f->flags = 0;
f->offset = 0;
f->length = 0;
f->errors = 0;
f->size = 0;
f->state = OSDP_FILE_IDLE;
f->file_id = 0;
f->cancel_req = false;
}
static inline bool file_tx_in_progress(struct osdp_file *f)
{
return f && f->state == OSDP_FILE_INPROG;
}
/* --- Sender CMD/RESP Handers --- */
int osdp_file_cmd_tx_build(struct osdp_pd *pd, uint8_t *buf, int max_len)
{
int buf_available;
struct osdp_cmd_file_xfer *p = (struct osdp_cmd_file_xfer *)buf;
struct osdp_file *f = TO_FILE(pd);
if (f == NULL) {
LOG_ERR("TX_Build: File ops not registered!");
return -1;
}
if (f->state != OSDP_FILE_INPROG) {
LOG_ERR("TX_Build: File transfer is not in progress!");
return -1;
}
if ((size_t)max_len <= sizeof(struct osdp_cmd_file_xfer)) {
LOG_ERR("TX_Build: insufficient space need:%zu have:%d",
sizeof(struct osdp_cmd_file_xfer), max_len);
return -1;
}
/**
* OSDP File module is a bit different than the rest of LibOSDP: it
* tries to greedily consume all available packet space. We need to
* account for the bytes that phy layer would add and then account for
* the overhead due to encryption if a secure channel is active. For
* now 16 is choosen based on crude observation.
*
* TODO: Try to add smarts here later.
*/
buf_available = max_len - sizeof(struct osdp_cmd_file_xfer) - 16;
p->type = f->file_id;
p->offset = f->offset;
p->size = f->size;
f->length = f->ops.read(f->ops.arg, p->data, buf_available, p->offset);
if (f->length < 0) {
LOG_ERR("TX_Build: user read failed! rc:%d len:%d off:%d",
f->length, buf_available, p->offset);
f->errors++;
f->length = 0;
return -1;
}
if (f->length == 0) {
LOG_WRN("TX_Build: Read 0 length chunk; Aborting transfer!");
file_state_reset(f);
return -1;
}
p->length = f->length;
return sizeof(struct osdp_cmd_file_xfer) + f->length;
}
int osdp_file_cmd_stat_decode(struct osdp_pd *pd, uint8_t *buf, int len)
{
struct osdp_file *f = TO_FILE(pd);
struct osdp_cmd_file_stat *p = (struct osdp_cmd_file_stat *)buf;
if (f == NULL) {
LOG_ERR("Stat_Decode: File ops not registered!");
return -1;
}
if (f->state != OSDP_FILE_INPROG) {
LOG_ERR("Stat_Decode: File transfer is not in progress!");
return -1;
}
if ((size_t)len < sizeof(struct osdp_cmd_file_stat)) {
LOG_ERR("Stat_Decode: invalid decode len:%d exp:%zu",
len, sizeof(struct osdp_cmd_file_stat));
return -1;
}
if (p->status == 0) {
f->offset += f->length;
f->errors = 0;
} else {
f->errors++;
}
f->length = 0;
assert(f->offset <= f->size);
if (f->offset == f->size) { /* EOF */
if (f->ops.close(f->ops.arg) < 0) {
LOG_ERR("Stat_Decode: Close failed!");
return -1;
}
f->state = OSDP_FILE_DONE;
LOG_INF("Stat_Decode: File transfer complete");
}
return 0;
}
/* --- Receiver CMD/RESP Handler --- */
int osdp_file_cmd_tx_decode(struct osdp_pd *pd, uint8_t *buf, int len)
{
int rc;
struct osdp_file *f = TO_FILE(pd);
struct osdp_cmd_file_xfer *p = (struct osdp_cmd_file_xfer *)buf;
struct osdp_cmd cmd;
if (f == NULL) {
LOG_ERR("TX_Decode: File ops not registered!");
return -1;
}
if (f->state == OSDP_FILE_IDLE || f->state == OSDP_FILE_DONE) {
if (pd->command_callback) {
/**
* Notify app of this command and make sure
* we can proceed
*/
cmd.id = OSDP_CMD_FILE_TX;
cmd.file_tx.flags = f->flags;
cmd.file_tx.id = p->type;
rc = pd->command_callback(pd->command_callback_arg, &cmd);
if (rc < 0)
return -1;
}
/* new file write request */
int size = (int)p->size;
if (f->ops.open(f->ops.arg, p->type, &size) < 0) {
LOG_ERR("TX_Decode: Open failed! fd:%d", p->type);
return -1;
}
LOG_INF("TX_Decode: Starting file transfer");
file_state_reset(f);
f->file_id = p->type;
f->size = size;
f->state = OSDP_FILE_INPROG;
}
if (f->state != OSDP_FILE_INPROG) {
LOG_ERR("TX_Decode: File transfer is not in progress!");
return -1;
}
if ((size_t)len <= sizeof(struct osdp_cmd_file_xfer)) {
LOG_ERR("TX_Decode: invalid decode len:%d exp>=%zu",
len, sizeof(struct osdp_cmd_file_xfer));
return -1;
}
f->length = f->ops.write(f->ops.arg, p->data, p->length, p->offset);
if (f->length != p->length) {
LOG_ERR("TX_Decode: user write failed! rc:%d len:%d off:%d",
f->length, p->length, p->offset);
f->errors++;
return -1;
}
return 0;
}
int osdp_file_cmd_stat_build(struct osdp_pd *pd, uint8_t *buf, int max_len)
{
struct osdp_cmd_file_stat *p = (struct osdp_cmd_file_stat *)buf;
struct osdp_file *f = TO_FILE(pd);
if (f == NULL) {
LOG_ERR("Stat_Build: File ops not registered!");
return -1;
}
if (f->state != OSDP_FILE_INPROG) {
LOG_ERR("Stat_Build: File transfer is not in progress!");
return -1;
}
if ((size_t)max_len < sizeof(struct osdp_cmd_file_stat)) {
LOG_ERR("Stat_Build: insufficient space need:%zu have:%d",
sizeof(struct osdp_cmd_file_stat), max_len);
return -1;
}
if (f->length > 0) {
p->status = 0;
f->errors = 0;
f->offset += f->length;
} else {
p->status = -1;
}
p->rx_size = 0;
p->control = 0;
p->delay = 0;
f->length = 0;
assert(f->offset <= f->size);
if (f->offset == f->size) { /* EOF */
if (f->ops.close(f->ops.arg) < 0) {
LOG_ERR("Stat_Build: Close failed!");
return -1;
}
f->state = OSDP_FILE_DONE;
LOG_INF("TX_Decode: File receive complete");
}
return sizeof(struct osdp_cmd_file_stat);
}
/* --- State Management --- */
void osdp_file_tx_abort(struct osdp_pd *pd)
{
struct osdp_file *f = TO_FILE(pd);
if (file_tx_in_progress(f)) {
f->ops.close(f->ops.arg);
file_state_reset(f);
}
}
int osdp_get_file_tx_state(struct osdp_pd *pd)
{
struct osdp_file *f = TO_FILE(pd);
if (!f || f->state == OSDP_FILE_IDLE || f->state == OSDP_FILE_DONE) {
return OSDP_FILE_TX_STATE_IDLE;
}
if (f->errors > OSDP_FILE_ERROR_RETRY_MAX || f->cancel_req) {
LOG_ERR("Aborting transfer of file fd:%d", f->file_id);
osdp_file_tx_abort(pd);
return OSDP_FILE_TX_STATE_ERROR;
}
return OSDP_FILE_TX_STATE_PENDING;
}
/**
* Entry point based on command OSDP_CMD_FILE to kick off a new file transfer.
*/
int osdp_file_tx_command(struct osdp_pd *pd, int file_id, uint32_t flags)
{
int size = 0;
struct osdp_file *f = TO_FILE(pd);
if (f == NULL) {
LOG_ERR("TX_init: File ops not registered!");
return -1;
}
if (file_tx_in_progress(f)) {
if (flags & OSDP_CMD_FILE_TX_FLAG_CANCEL) {
if (file_id == f->file_id) {
f->cancel_req = true;
return 0;
}
LOG_ERR("TX_init: invalid cancel request; no such tx!");
return -1;
}
LOG_ERR("TX_init: A file tx is already in progress");
return -1;
}
if (flags & OSDP_CMD_FILE_TX_FLAG_CANCEL) {
LOG_ERR("TX_init: invalid cancel request");
return -1;
}
if (f->ops.open(f->ops.arg, file_id, &size) < 0) {
LOG_ERR("TX_init: Open failed! fd:%d", file_id);
return -1;
}
if (size <= 0) {
LOG_ERR("TX_init: Invalid file size %d", size);
return -1;
}
LOG_INF("TX_init: Starting file transfer of size: %d", size);
file_state_reset(f);
f->flags = flags;
f->file_id = file_id;
f->size = size;
f->state = OSDP_FILE_INPROG;
return 0;
}
/* --- Exported Methods --- */
OSDP_EXPORT
int osdp_file_register_ops(osdp_t *ctx, int pd_idx,
const struct osdp_file_ops *ops)
{
input_check(ctx, pd_idx);
struct osdp_pd *pd = osdp_to_pd(ctx, pd_idx);
if (!pd->file) {
pd->file = calloc(1, sizeof(struct osdp_file));
if (pd->file == NULL) {
LOG_PRINT("Failed to alloc struct osdp_file");
return -1;
}
}
memcpy(&pd->file->ops, ops, sizeof(struct osdp_file_ops));
file_state_reset(pd->file);
return 0;
}
OSDP_EXPORT
int osdp_get_file_tx_status(const osdp_t *ctx, int pd_idx,
int *size, int *offset)
{
input_check(ctx, pd_idx);
struct osdp_file *f = TO_FILE(osdp_to_pd(ctx, pd_idx));
if (f->state != OSDP_FILE_INPROG && f->state != OSDP_FILE_DONE) {
LOG_PRINT("File TX not in progress");
return -1;
}
*size = f->size;
*offset = f->offset;
return 0;
}