Skip to content

Commit dad97d7

Browse files
committed
LibOSDP: Decouple OSDP_FLAG_XXX and PD_FLAG_XXX
LibOSDP has two kinds of flags: 1. CP/PD init-time flags set by the application 2. Runtime flags which are set by LibOSDP to indicate state Both of these were collected into pd->flags with a 16 bit reservation for each. The runtime flags have now grown past 16 flags which conflicts with the init-time flags (which occupy the upper 16 bits). To avoid this problem, this patch collects the init-time flags into a runtime flag. This approach also prevents the application layer from setting random flags which might affect the way LibOSDP behaves in weird ways. Related-to: #258 Signed-off-by: Siddharth Chandrasekaran <sidcha.dev@gmail.com>
1 parent 49cf3f7 commit dad97d7

File tree

3 files changed

+84
-14
lines changed

3 files changed

+84
-14
lines changed

src/osdp_common.h

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,14 @@ union osdp_ephemeral_data {
275275
#define PD_FLAG_PKT_BROADCAST BIT(14) /* this packet was addressed to 0x7F */
276276
#define PD_FLAG_CP_USE_CRC BIT(15) /* CP uses CRC-16 instead of checksum */
277277

278+
/* PD Init flags */
279+
#define PD_FLAG_ENFORCE_SECURE BIT(24) /* See: OSDP_FLAG_ENFORCE_SECURE */
280+
#define PD_FLAG_INSTALL_MODE BIT(25) /* See: OSDP_FLAG_INSTALL_MODE */
281+
#define PD_FLAG_IGNORE_USR BIT(26) /* See: OSDP_FLAG_IGNORE_USR */
282+
#define PD_FLAG_ENABLE_NOTIF BIT(27) /* See: OSDP_FLAG_ENABLE_NOTIFICATION */
283+
#define PD_FLAG_CAPTURE_PKT BIT(28) /* See: OSDP_FLAG_CAPTURE_PACKETS */
284+
#define PD_FLAG_ALLOW_EMPTY_EDB BIT(29) /* See: OSDP_FLAG_ALLOW_EMPTY_ENCRYPTED_DATA_BLOCK */
285+
278286
/* CP event requests; used with make_request() and check_request() */
279287
#define CP_REQ_RESTART_SC 0x00000001
280288
#define CP_REQ_EVENT_SEND 0x00000002
@@ -581,40 +589,40 @@ static inline bool test_request(struct osdp_pd *pd, uint32_t req) {
581589
}
582590

583591
static inline bool is_capture_enabled(struct osdp_pd *pd) {
584-
return (ISSET_FLAG(pd, OSDP_FLAG_CAPTURE_PACKETS) &&
592+
return (ISSET_FLAG(pd, PD_FLAG_CAPTURE_PKT) &&
585593
(IS_ENABLED(OPT_OSDP_PACKET_TRACE) ||
586594
IS_ENABLED(OPT_OSDP_DATA_TRACE)));
587595
}
588596

589597
static inline bool is_data_trace_enabled(struct osdp_pd *pd) {
590-
return (ISSET_FLAG(pd, OSDP_FLAG_CAPTURE_PACKETS) &&
598+
return (ISSET_FLAG(pd, PD_FLAG_CAPTURE_PKT) &&
591599
IS_ENABLED(OPT_OSDP_DATA_TRACE));
592600
}
593601

594602
static inline bool is_packet_trace_enabled(struct osdp_pd *pd) {
595-
return (ISSET_FLAG(pd, OSDP_FLAG_CAPTURE_PACKETS) &&
603+
return (ISSET_FLAG(pd, PD_FLAG_CAPTURE_PKT) &&
596604
IS_ENABLED(OPT_OSDP_PACKET_TRACE));
597605
}
598606

599607
static inline bool sc_allow_empty_encrypted_data_block(struct osdp_pd *pd) {
600-
return ISSET_FLAG(pd, OSDP_FLAG_ALLOW_EMPTY_ENCRYPTED_DATA_BLOCK);
608+
return ISSET_FLAG(pd, PD_FLAG_ALLOW_EMPTY_EDB);
601609
}
602610

603611
static inline bool is_enforce_secure(struct osdp_pd *pd)
604612
{
605-
return ISSET_FLAG(pd, OSDP_FLAG_ENFORCE_SECURE);
613+
return ISSET_FLAG(pd, PD_FLAG_ENFORCE_SECURE);
606614
}
607615

608616
static inline bool is_notifications_enabled(struct osdp_pd *pd) {
609-
return ISSET_FLAG(pd, OSDP_FLAG_ENABLE_NOTIFICATION);
617+
return ISSET_FLAG(pd, PD_FLAG_ENABLE_NOTIF);
610618
}
611619

612620
static inline bool is_ignore_unsolicited_messages(struct osdp_pd *pd) {
613-
return ISSET_FLAG(pd, OSDP_FLAG_IGN_UNSOLICITED);
621+
return ISSET_FLAG(pd, PD_FLAG_IGNORE_USR);
614622
}
615623

616624
static inline bool is_install_mode(struct osdp_pd *pd) {
617-
return ISSET_FLAG(pd, OSDP_FLAG_INSTALL_MODE);
625+
return ISSET_FLAG(pd, PD_FLAG_INSTALL_MODE);
618626
}
619627

620628
#endif /* _OSDP_COMMON_H_ */

src/osdp_cp.c

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,6 +1493,28 @@ static int cp_detect_connection_topology(struct osdp *ctx)
14931493
return 0;
14941494
}
14951495

1496+
static void cp_collect_init_flags(struct osdp_pd *pd, int flags)
1497+
{
1498+
if (flags & OSDP_FLAG_ENFORCE_SECURE) {
1499+
SET_FLAG(pd, PD_FLAG_ENFORCE_SECURE);
1500+
}
1501+
if (flags & OSDP_FLAG_INSTALL_MODE) {
1502+
SET_FLAG(pd, PD_FLAG_INSTALL_MODE);
1503+
}
1504+
if (flags & OSDP_FLAG_IGN_UNSOLICITED) {
1505+
SET_FLAG(pd, PD_FLAG_IGNORE_USR);
1506+
}
1507+
if (flags & OSDP_FLAG_ENABLE_NOTIFICATION) {
1508+
SET_FLAG(pd, PD_FLAG_ENABLE_NOTIF);
1509+
}
1510+
if (flags & OSDP_FLAG_CAPTURE_PACKETS) {
1511+
SET_FLAG(pd, PD_FLAG_CAPTURE_PKT);
1512+
}
1513+
if (flags & OSDP_FLAG_ALLOW_EMPTY_ENCRYPTED_DATA_BLOCK) {
1514+
SET_FLAG(pd, PD_FLAG_ALLOW_EMPTY_EDB);
1515+
}
1516+
}
1517+
14961518
static int cp_add_pd(struct osdp *ctx, int num_pd, const osdp_pd_info_t *info_list)
14971519
{
14981520
int i, old_num_pd;
@@ -1527,8 +1549,9 @@ static int cp_add_pd(struct osdp *ctx, int num_pd, const osdp_pd_info_t *info_li
15271549
}
15281550
pd->baud_rate = info->baud_rate;
15291551
pd->address = info->address;
1530-
pd->flags = info->flags;
1552+
pd->flags = 0;
15311553
pd->seq_number = -1;
1554+
cp_collect_init_flags(pd, info->flags);
15321555
SET_FLAG(pd, PD_FLAG_SC_DISABLED);
15331556
/* Default to CRC-16 until we know PD capabilities */
15341557
SET_FLAG(pd, PD_FLAG_CP_USE_CRC);
@@ -1732,15 +1755,37 @@ int osdp_cp_modify_flag(osdp_t *ctx, int pd_idx, uint32_t flags, bool do_set)
17321755
const uint32_t all_flags = (
17331756
OSDP_FLAG_ENFORCE_SECURE |
17341757
OSDP_FLAG_INSTALL_MODE |
1735-
OSDP_FLAG_IGN_UNSOLICITED
1758+
OSDP_FLAG_IGN_UNSOLICITED |
1759+
OSDP_FLAG_ENABLE_NOTIFICATION |
1760+
OSDP_FLAG_ALLOW_EMPTY_ENCRYPTED_DATA_BLOCK
17361761
);
17371762
struct osdp_pd *pd = osdp_to_pd(ctx, pd_idx);
1763+
uint32_t pd_flags = 0;
17381764

17391765
if (flags & ~all_flags) {
17401766
return -1;
17411767
}
17421768

1743-
do_set ? SET_FLAG(pd, flags) : CLEAR_FLAG(pd, flags);
1769+
if (flags & OSDP_FLAG_ENFORCE_SECURE) {
1770+
pd_flags |= PD_FLAG_ENFORCE_SECURE;
1771+
}
1772+
if (flags & OSDP_FLAG_INSTALL_MODE) {
1773+
pd_flags |= PD_FLAG_INSTALL_MODE;
1774+
}
1775+
if (flags & OSDP_FLAG_IGN_UNSOLICITED) {
1776+
pd_flags |= PD_FLAG_IGNORE_USR;
1777+
}
1778+
if (flags & OSDP_FLAG_ENABLE_NOTIFICATION) {
1779+
pd_flags |= PD_FLAG_ENABLE_NOTIF;
1780+
}
1781+
if (flags & OSDP_FLAG_CAPTURE_PACKETS) {
1782+
pd_flags |= PD_FLAG_CAPTURE_PKT;
1783+
}
1784+
if (flags & OSDP_FLAG_ALLOW_EMPTY_ENCRYPTED_DATA_BLOCK) {
1785+
pd_flags |= PD_FLAG_ALLOW_EMPTY_EDB;
1786+
}
1787+
1788+
do_set ? SET_FLAG(pd, pd_flags) : CLEAR_FLAG(pd, pd_flags);
17441789
return 0;
17451790
}
17461791

src/osdp_pd.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,7 @@ static void osdp_pd_update(struct osdp_pd *pd)
10961096
if (pd->cmd_id == CMD_KEYSET && pd->reply_id == REPLY_ACK) {
10971097
memcpy(pd->sc.scbk, pd->ephemeral_data, 16);
10981098
CLEAR_FLAG(pd, PD_FLAG_SC_USE_SCBKD);
1099-
CLEAR_FLAG(pd, OSDP_FLAG_INSTALL_MODE);
1099+
CLEAR_FLAG(pd, PD_FLAG_INSTALL_MODE);
11001100
sc_deactivate(pd);
11011101
} else if (pd->cmd_id == CMD_COMSET &&
11021102
pd->reply_id == REPLY_COM) {
@@ -1154,6 +1154,22 @@ static void osdp_pd_set_attributes(struct osdp_pd *pd,
11541154
}
11551155
}
11561156

1157+
static void pd_collect_init_flags(struct osdp_pd *pd, uint32_t flags)
1158+
{
1159+
if (flags & OSDP_FLAG_ENFORCE_SECURE) {
1160+
SET_FLAG(pd, PD_FLAG_ENFORCE_SECURE);
1161+
}
1162+
if (flags & OSDP_FLAG_INSTALL_MODE) {
1163+
SET_FLAG(pd, PD_FLAG_INSTALL_MODE);
1164+
}
1165+
if (flags & OSDP_FLAG_CAPTURE_PACKETS) {
1166+
SET_FLAG(pd, PD_FLAG_CAPTURE_PKT);
1167+
}
1168+
if (flags & OSDP_FLAG_ALLOW_EMPTY_ENCRYPTED_DATA_BLOCK) {
1169+
SET_FLAG(pd, PD_FLAG_ALLOW_EMPTY_EDB);
1170+
}
1171+
}
1172+
11571173
/* --- Exported Methods --- */
11581174

11591175
osdp_t *osdp_pd_setup(const osdp_pd_info_t *info)
@@ -1199,10 +1215,11 @@ osdp_t *osdp_pd_setup(const osdp_pd_info_t *info)
11991215
}
12001216
pd->baud_rate = info->baud_rate;
12011217
pd->address = info->address;
1202-
pd->flags = info->flags;
1218+
pd->flags = 0;
12031219
pd->seq_number = -1;
12041220
memcpy(&pd->channel, &info->channel, sizeof(struct osdp_channel));
12051221

1222+
pd_collect_init_flags(pd, info->flags);
12061223
logger_get_default(&pd->logger);
12071224
snprintf(name, sizeof(name), "OSDP: PD-%d", pd->address);
12081225
logger_set_name(&pd->logger, name);
@@ -1217,7 +1234,7 @@ osdp_t *osdp_pd_setup(const osdp_pd_info_t *info)
12171234
goto error;
12181235
}
12191236
LOG_WRN("SCBK not provided. PD is in INSTALL_MODE");
1220-
SET_FLAG(pd, OSDP_FLAG_INSTALL_MODE);
1237+
SET_FLAG(pd, PD_FLAG_INSTALL_MODE);
12211238
} else {
12221239
memcpy(pd->sc.scbk, info->scbk, 16);
12231240
}

0 commit comments

Comments
 (0)