Skip to content

Commit 6a71977

Browse files
committed
lte/alt1250: Refactor alt1250_regevtcb
Refactor alt1250_regevtcb to improve readability.
1 parent 196521d commit 6a71977

File tree

1 file changed

+74
-55
lines changed

1 file changed

+74
-55
lines changed

lte/alt1250/callback_handlers/alt1250_evt.c

Lines changed: 74 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
#define EVTTASK_NAME "lteevt_task"
6060
#define LAPIEVT_QNAME "/lapievt"
6161

62+
#define clr_evtcb(info) reg_evtcb(info, 0, NULL)
63+
#define search_free_evtcb() search_evtcb(0)
64+
6265
/****************************************************************************
6366
* Private Function Prototypes
6467
****************************************************************************/
@@ -1692,6 +1695,73 @@ static uint64_t alt1250_evt_search(uint32_t cmdid)
16921695
return evtbitmap;
16931696
}
16941697

1698+
/****************************************************************************
1699+
* Name: reg_evtcb
1700+
****************************************************************************/
1701+
1702+
static int reg_evtcb(struct cbinfo_s *info, uint32_t cmdid, FAR void *cb)
1703+
{
1704+
if (info == NULL)
1705+
{
1706+
return ERROR;
1707+
}
1708+
1709+
info->cmdid = cmdid;
1710+
info->cb = cb;
1711+
1712+
return OK;
1713+
}
1714+
1715+
/****************************************************************************
1716+
* Name: search_evtcb
1717+
****************************************************************************/
1718+
1719+
static struct cbinfo_s *search_evtcb(uint32_t cmdid)
1720+
{
1721+
int i;
1722+
1723+
for (i = 0; i < ARRAY_SZ(g_cbtable); i++)
1724+
{
1725+
if (g_cbtable[i].cmdid == cmdid)
1726+
{
1727+
return &g_cbtable[i];
1728+
}
1729+
}
1730+
1731+
return NULL;
1732+
}
1733+
1734+
/****************************************************************************
1735+
* Name: register_evtcb
1736+
****************************************************************************/
1737+
1738+
static int register_evtcb(uint32_t cmdid, FAR void *cb)
1739+
{
1740+
if (search_evtcb(cmdid) == NULL)
1741+
{
1742+
if (reg_evtcb(search_free_evtcb(), cmdid, cb) == ERROR)
1743+
{
1744+
return -EBUSY;
1745+
}
1746+
1747+
return OK;
1748+
}
1749+
else
1750+
{
1751+
return IS_REPORT_API(cmdid) ? -EALREADY : -EINPROGRESS;
1752+
}
1753+
}
1754+
1755+
/****************************************************************************
1756+
* Name: clear_evtcb
1757+
****************************************************************************/
1758+
1759+
static int clear_evtcb(uint32_t cmdid)
1760+
{
1761+
clr_evtcb(search_evtcb(cmdid));
1762+
return OK;
1763+
}
1764+
16951765
/****************************************************************************
16961766
* Public Functions
16971767
****************************************************************************/
@@ -1723,68 +1793,17 @@ int alt1250_evtdatadestroy(void)
17231793

17241794
int alt1250_regevtcb(uint32_t cmdid, FAR void *cb)
17251795
{
1726-
int ret = OK;
1727-
int i;
1728-
bool is_clear = (cb == NULL);
1729-
int myidx = -1;
1730-
int freeidx = -1;
1796+
int ret;
17311797

17321798
sem_wait(&g_cbtablelock);
17331799

1734-
for (i = 0; i < ARRAY_SZ(g_cbtable); i++)
1735-
{
1736-
if (g_cbtable[i].cmdid == 0)
1737-
{
1738-
freeidx = i;
1739-
}
1740-
1741-
if (g_cbtable[i].cmdid == cmdid)
1742-
{
1743-
myidx = i;
1744-
break;
1745-
}
1746-
}
1747-
1748-
if (!is_clear)
1800+
if (cb == NULL)
17491801
{
1750-
/* Found my ID */
1751-
1752-
if (myidx != -1)
1753-
{
1754-
if (IS_REPORT_API(cmdid))
1755-
{
1756-
ret = -EALREADY;
1757-
}
1758-
else
1759-
{
1760-
ret = -EINPROGRESS;
1761-
}
1762-
}
1763-
1764-
/* No free index at table? */
1765-
1766-
else if (freeidx == -1)
1767-
{
1768-
ret = -EBUSY;
1769-
}
1770-
1771-
/* Not found my ID, but found a free index. */
1772-
1773-
else
1774-
{
1775-
g_cbtable[freeidx].cmdid = cmdid;
1776-
g_cbtable[freeidx].cb = cb;
1777-
}
1802+
ret = clear_evtcb(cmdid);
17781803
}
17791804
else
17801805
{
1781-
/* Found my ID */
1782-
1783-
if (myidx != -1)
1784-
{
1785-
g_cbtable[myidx].cmdid = 0;
1786-
g_cbtable[myidx].cb = NULL;
1787-
}
1806+
ret = register_evtcb(cmdid, cb);
17881807
}
17891808

17901809
sem_post(&g_cbtablelock);

0 commit comments

Comments
 (0)