forked from vk2tds/libosdp_arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosdp_common.c
297 lines (255 loc) · 6.49 KB
/
osdp_common.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
/*
* Copyright (c) 2019-2021 Siddharth Chandrasekaran <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/
#define _GNU_SOURCE /* See feature_test_macros(7) */
#define CONFIG_DISABLE_PRETTY_LOGGING TRUE
unsigned long millis(void); // vk2tds
#include <stdarg.h>
#include <stdlib.h>
#ifndef CONFIG_DISABLE_PRETTY_LOGGING
#include <unistd.h>
#endif
//vk2tds
//#include <sys/time.h>
#define OSDP_EXPORT
#define OSDP_NO_EXPORT
#include "osdp_common.h"
uint16_t crc16_itu_t(uint16_t seed, const uint8_t *src, size_t len)
{
for (; len > 0; len--) {
seed = (seed >> 8U) | (seed << 8U);
seed ^= *src++;
seed ^= (seed & 0xffU) >> 4U;
seed ^= seed << 12U;
seed ^= (seed & 0xffU) << 5U;
}
return seed;
}
uint16_t osdp_compute_crc16(const uint8_t *buf, size_t len)
{
return crc16_itu_t(0x1D0F, buf, len);
}
int64_t osdp_millis_now(void)
{
//vk2tds
return millis();
}
int64_t osdp_millis_since(int64_t last)
{
return osdp_millis_now() - last;
}
const char *osdp_cmd_name(int cmd_id)
{
const char *name;
static const char * const names[] = {
[CMD_POLL - CMD_POLL] = "POLL",
[CMD_ID - CMD_POLL] = "ID",
[CMD_CAP - CMD_POLL] = "CAP",
[CMD_LSTAT - CMD_POLL] = "LSTAT",
[CMD_ISTAT - CMD_POLL] = "ISTAT",
[CMD_OSTAT - CMD_POLL] = "OSTAT",
[CMD_RSTAT - CMD_POLL] = "RSTAT",
[CMD_OUT - CMD_POLL] = "OUT",
[CMD_LED - CMD_POLL] = "LED",
[CMD_BUZ - CMD_POLL] = "BUZ",
[CMD_TEXT - CMD_POLL] = "TEXT",
[CMD_RMODE - CMD_POLL] = "RMODE",
[CMD_TDSET - CMD_POLL] = "TDSET",
[CMD_COMSET - CMD_POLL] = "COMSET",
[CMD_BIOREAD - CMD_POLL] = "BIOREAD",
[CMD_BIOMATCH - CMD_POLL] = "BIOMATCH",
[CMD_KEYSET - CMD_POLL] = "KEYSET",
[CMD_CHLNG - CMD_POLL] = "CHLNG",
[CMD_SCRYPT - CMD_POLL] = "SCRYPT",
[CMD_ACURXSIZE - CMD_POLL] = "ACURXSIZE",
[CMD_FILETRANSFER - CMD_POLL] = "FILETRANSFER",
[CMD_MFG - CMD_POLL] = "MFG",
[CMD_XWR - CMD_POLL] = "XWR",
[CMD_ABORT - CMD_POLL] = "ABORT",
[CMD_PIVDATA - CMD_POLL] = "PIVDATA",
[CMD_CRAUTH - CMD_POLL] = "CRAUTH",
[CMD_GENAUTH - CMD_POLL] = "GENAUTH",
[CMD_KEEPACTIVE - CMD_POLL] = "KEEPACTIVE",
};
if (cmd_id < CMD_POLL || cmd_id > CMD_KEEPACTIVE) {
return "INVALID";
}
name = names[cmd_id - CMD_POLL];
if (name[0] == '\0') {
return "UNKNOWN";
}
return name;
}
const char *osdp_reply_name(int reply_id)
{
const char *name;
static const char * const names[] = {
[REPLY_ACK - REPLY_ACK] = "ACK",
[REPLY_NAK - REPLY_ACK] = "NAK",
[REPLY_PDID - REPLY_ACK] = "PDID",
[REPLY_PDCAP - REPLY_ACK] = "PDCAP",
[REPLY_LSTATR - REPLY_ACK] = "LSTATR",
[REPLY_ISTATR - REPLY_ACK] = "ISTATR",
[REPLY_OSTATR - REPLY_ACK] = "OSTATR",
[REPLY_RSTATR - REPLY_ACK] = "RSTATR",
[REPLY_RAW - REPLY_ACK] = "RAW",
[REPLY_FMT - REPLY_ACK] = "FMT",
[REPLY_KEYPPAD - REPLY_ACK] = "KEYPPAD",
[REPLY_COM - REPLY_ACK] = "COM",
[REPLY_BIOREADR - REPLY_ACK] = "BIOREADR",
[REPLY_BIOMATCHR - REPLY_ACK] = "BIOMATCHR",
[REPLY_CCRYPT - REPLY_ACK] = "CCRYPT",
[REPLY_RMAC_I - REPLY_ACK] = "RMAC_I",
[REPLY_FTSTAT - REPLY_ACK] = "FTSTAT",
[REPLY_MFGREP - REPLY_ACK] = "MFGREP",
[REPLY_BUSY - REPLY_ACK] = "BUSY",
[REPLY_PIVDATAR - REPLY_ACK] = "PIVDATA",
[REPLY_CRAUTHR - REPLY_ACK] = "CRAUTH",
[REPLY_MFGSTATR - REPLY_ACK] = "MFGSTATR",
[REPLY_MFGERRR - REPLY_ACK] = "MFGERR",
[REPLY_XRD - REPLY_ACK] = "XRD",
};
if (reply_id < REPLY_ACK || reply_id > REPLY_XRD) {
return "INVALID";
}
name = names[reply_id - REPLY_ACK];
if (!name) {
return "UNKNOWN";
}
return name;
}
void osdp_keyset_complete(struct osdp_pd *pd)
{
cp_keyset_complete(pd, true);
}
int osdp_rb_push(struct osdp_rb *p, uint8_t data)
{
size_t next;
next = p->head + 1;
if (next >= sizeof(p->buffer))
next = 0;
if (next == p->tail)
return -1;
p->buffer[p->head] = data;
p->head = next;
return 0;
}
int osdp_rb_push_buf(struct osdp_rb *p, uint8_t *buf, int len)
{
int i;
for (i = 0; i < len; i++) {
if (osdp_rb_push(p, buf[i])) {
break;
}
}
return i;
}
int osdp_rb_pop(struct osdp_rb *p, uint8_t *data)
{
size_t next;
if (p->head == p->tail)
return -1;
next = p->tail + 1;
if (next >= sizeof(p->buffer))
next = 0;
*data = p->buffer[p->tail];
p->tail = next;
return 0;
}
int osdp_rb_pop_buf(struct osdp_rb *p, uint8_t *buf, int max_len)
{
int i;
for (i = 0; i < max_len; i++) {
if (osdp_rb_pop(p, buf + i)) {
break;
}
}
return i;
}
/* --- Exported Methods --- */
OSDP_EXPORT
void osdp_logger_init(const char *name, int log_level,
osdp_log_puts_fn_t log_fn)
{
logger_t ctx;
FILE *file = NULL;
int flags = LOGGER_FLAG_NONE;
#ifdef CONFIG_DISABLE_PRETTY_LOGGING
flags |= LOGGER_FLAG_NO_COLORS;
#endif
if (!log_fn)
file = stderr;
logger_init(&ctx, log_level, name, REPO_ROOT, log_fn, file, NULL, flags);
logger_set_default(&ctx); /* Mark this config as logging default */
}
OSDP_EXPORT
void osdp_set_log_callback(osdp_log_callback_fn_t cb)
{
logger_t ctx;
int flags = LOGGER_FLAG_NONE;
logger_init(&ctx, 0, NULL, REPO_ROOT, NULL, NULL, cb, flags);
logger_set_default(&ctx); /* Mark this config as logging default */
}
OSDP_EXPORT
const char *osdp_get_version()
{
return PROJECT_VERSION;
}
OSDP_EXPORT
const char *osdp_get_source_info()
{
if (strnlen(GIT_TAG, 8) > 0) {
return GIT_BRANCH " (" GIT_TAG ")";
} else if (strnlen(GIT_REV, 8) > 0) {
return GIT_BRANCH " (" GIT_REV GIT_DIFF ")";
} else {
return GIT_BRANCH;
}
}
OSDP_EXPORT
void osdp_get_sc_status_mask(const osdp_t *ctx, uint8_t *bitmask)
{
input_check(ctx);
int i, pos;
uint8_t *mask = bitmask;
struct osdp_pd *pd;
*mask = 0;
for (i = 0; i < NUM_PD(ctx); i++) {
pos = i & 0x07;
if (i && pos == 0) {
mask++;
*mask = 0;
}
pd = osdp_to_pd(ctx, i);
if (ISSET_FLAG(pd, PD_FLAG_SC_ACTIVE) &&
!ISSET_FLAG(pd, PD_FLAG_SC_USE_SCBKD)) {
*mask |= 1 << pos;
}
}
}
OSDP_EXPORT
void osdp_get_status_mask(const osdp_t *ctx, uint8_t *bitmask)
{
input_check(ctx);
int i, pos;
uint8_t *mask = bitmask;
struct osdp_pd *pd = osdp_to_pd(ctx, 0);
if (ISSET_FLAG(pd, PD_FLAG_PD_MODE)) {
*mask = osdp_millis_since(pd->tstamp) < OSDP_PD_ONLINE_TOUT_MS;
return;
}
*mask = 0;
for (i = 0; i < NUM_PD(ctx); i++) {
pos = i & 0x07;
if (i && pos == 0) {
mask++;
*mask = 0;
}
pd = osdp_to_pd(ctx, i);
if (pd->state == OSDP_CP_STATE_ONLINE) {
*mask |= 1 << pos;
}
}
}