Skip to content

Commit

Permalink
Refactor find_ddci_for_pmt() somewhat so it's more clear what's going on
Browse files Browse the repository at this point in the history
No changes to the logic, just better logging and
  • Loading branch information
Jalle19 committed Jun 24, 2024
1 parent 1509979 commit 259f423
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 22 deletions.
67 changes: 46 additions & 21 deletions src/ddci.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,36 +288,57 @@ int create_channel_for_pmt(Sddci_channel *c, SPMT *pmt) {
return 0;
}

/**
* Find the DDCI device that should be used to handle the specified PMT. The
* first suitable device is returned. If a suitable device is found but the
* device is still initializing, -1 is returned. If no suitable device is found,
* -2 is returned.
*/
int find_ddci_for_pmt(Sddci_channel *c, SPMT *pmt) {
int ddid = -100;
int retry = 0;
int ddid = -100; // -100 means we didn't find a suitable device

int i = 0;
for (i = 0; i < c->ddcis; i++) {
ddid = c->ddci[i].ddci;
ddci_device_t *d = get_ddci(ddid);
int candidate = c->ddci[i].ddci;
ddci_device_t *d = get_ddci(candidate);
if (!d) {
LOG("DDID %d not enabled", ddid);
LOG("%s: DDCI %d not enabled, skipping", __FUNCTION__, candidate);
continue;
}
if (is_ca_initializing(ddid)) {
LOG("DD %d is initializing", ddid);
retry = 1;

} else {
if (d && d->channels < d->max_channels)
break;
else
LOG("DDCI %d cannot be used for PMT %d, pid %d (used "
"channels "
"%d max %d)",
ddid, pmt->id, pmt->pid, d ? d->channels : -1,
d ? d->max_channels : -1);
if (d->channels >= d->max_channels) {
LOG("%s: DDCI %d cannot be used for PMT %d, pid %d, sid, %d (used "
"channels "
"%d max %d), skipping",
__FUNCTION__, candidate, pmt->id, pmt->pid, pmt->sid,
d ? d->channels : -1, d ? d->max_channels : -1);
continue;
}
ddid = -100;

ddid = candidate;
break;
}
if (retry)

if (ddid == -100) {
// Distinguish between otherwise not found and deliberately not assigned
if (c->ddcis == 0) {
LOG("%s: no suitable DDCI found for PMT %d (sid %d): not mapped to "
"any DDCI "
"device in ddci.conf",
__FUNCTION__, pmt->id, pmt->sid);
} else {
LOG("%s: no suitable DDCI found for PMT %d (sid %d)", __FUNCTION__,
pmt->id, pmt->sid);
}

return -TABLES_RESULT_ERROR_NORETRY;
} else if (is_ca_initializing(ddid)) {
LOG("%s: DDCI %d is initializing, retrying", __FUNCTION__, ddid);
return -TABLES_RESULT_ERROR_RETRY;
return ddid;
} else {
LOG("%s: Using DDCI %d", __FUNCTION__, ddid);
return ddid;
}
}

int is_pmt_running(SPMT *pmt) {
Expand Down Expand Up @@ -380,10 +401,14 @@ int ddci_process_pmt(adapter *ad, SPMT *pmt) {
"Could not allocate channel");
}

// Determine which DDCI should handle this PMT
if (ddid == -1)
ddid = find_ddci_for_pmt(channel, pmt);
// Negative return values are used to distinguish from valid return values (>= 0)
if (ddid == -TABLES_RESULT_ERROR_RETRY)
return -ddid;
return TABLES_RESULT_ERROR_RETRY;
else if (ddid == -TABLES_RESULT_ERROR_NORETRY)
return TABLES_RESULT_ERROR_NORETRY;

d = get_ddci(ddid);
if (!d) {
Expand Down
14 changes: 13 additions & 1 deletion tests/test_ddci.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ int test_channels() {

int test_add_del_pmt() {
int i;
SPMT *pmt0, *pmt1, *pmt2, *pmt3;
SPMT *pmt0, *pmt1, *pmt2, *pmt3, *pmt4;
ddci_device_t d0, d1;
ca_device_t ca0, ca1;
adapter ad, a0, a1;
Expand All @@ -142,6 +142,7 @@ int test_add_del_pmt() {
pmt1 = create_pmt(8, 200, 201, 202, 0x100, 0x500);
pmt2 = create_pmt(8, 300, 301, 302, 0x500, 0x100);
pmt3 = create_pmt(8, 400, 401, 402, 0x600, 0x601);
pmt4 = create_pmt(8, 500, 501, 502, 0x500, 0x500);
memset(&d0, 0, sizeof(d0));
memset(&d1, 0, sizeof(d1));
memset(&d0.pmt, -1, sizeof(d0.pmt));
Expand All @@ -155,6 +156,14 @@ int test_add_del_pmt() {
create_hash_table(&d1.mapping, 30);
create_hash_table(&channels, 30);

// Save a channel for pmt4, we'll check that it is not assigned to
// any DDCI adapter even though its CAID matches etc.
Sddci_channel c4;
memset(&c4, 0, sizeof(c4));
c4.sid = pmt4->sid;
c4.ddcis = 0;
setItem(&channels, c4.sid, &c4, sizeof(c4));

int dvbca_id = add_ca(&dvbca, 0xFFFFFFFF);
// DD 0 - 0x100, DD 1 - 0x500
add_caid_mask(dvbca_id, 0, 0x100, 0xFFFF);
Expand All @@ -177,6 +186,9 @@ int test_add_del_pmt() {
ASSERT(ddci_process_pmt(&ad, pmt3) == TABLES_RESULT_ERROR_NORETRY,
"DDCI ready, expected no retry");

ASSERT(ddci_process_pmt(&ad, pmt4) == TABLES_RESULT_ERROR_NORETRY,
"Channel not assigned to any DDCI adapter, expected no retry");

// One matching channel
ASSERT(ddci_process_pmt(&ad, pmt0) == TABLES_RESULT_OK,
"DDCI matching DD 0");
Expand Down

0 comments on commit 259f423

Please sign in to comment.