Bluetooth: hci_ecc: Convert to new thread API

Convert from a private task to the thread API, also add stack usage
analysis and tune-down the stack size to avoid wasting memory:

On x86:
ecc stack (real size 1280):	unused 80	usage 1136 / 1216 (93 %)

On arm:
ecc stack (real size 1280):	unused 52	usage 1128 / 1180 (95 %)

Change-Id: Iba84a5a9cb5257c1456663adf4952c5c18650f97
Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
This commit is contained in:
Johan Hedberg 2016-11-11 10:48:19 +02:00
commit a14680f7c0

View file

@ -21,6 +21,7 @@
#include <zephyr.h>
#include <atomic.h>
#include <misc/stack.h>
#include <misc/byteorder.h>
#include <tinycrypt/constants.h>
#include <tinycrypt/utils.h>
@ -37,6 +38,8 @@
#define BT_DBG(fmt, ...)
#endif
static BT_STACK_NOINIT(ecc_thread_stack, 1280);
/* based on Core Specification 4.2 Vol 3. Part H 2.3.5.6.1 */
static const uint32_t debug_private_key[8] = {
0xcd3c1abd, 0x5899b8a6, 0xeb40b799, 0x4aff607b, 0xd2103f50, 0x74c9b3e3,
@ -55,7 +58,6 @@ static const uint8_t debug_public_key[64] = {
#endif
static struct k_fifo ecc_queue;
static bool ecc_queue_ready;
static int (*drv_send)(struct net_buf *buf);
static uint32_t private_key[8];
@ -202,24 +204,8 @@ static void emulate_le_generate_dhkey(struct net_buf *buf)
bt_recv(buf);
}
static void ecc_queue_init(void)
static void ecc_thread(void *p1, void *p2, void *p3)
{
unsigned int mask;
mask = irq_lock();
if (!ecc_queue_ready) {
k_fifo_init(&ecc_queue);
ecc_queue_ready = true;
}
irq_unlock(mask);
}
static void ecc_task(void)
{
ecc_queue_init();
while (true) {
struct net_buf *buf;
@ -238,12 +224,12 @@ static void ecc_task(void)
net_buf_unref(buf);
break;
}
stack_analyze("ecc stack", ecc_thread_stack,
sizeof(ecc_thread_stack));
}
}
/* TODO measure required stack size, 1024 is not enough */
DEFINE_TASK(ECC_TASKID, 10, ecc_task, 2048, EXE);
static void clear_ecc_events(struct net_buf *buf)
{
struct bt_hci_cp_le_set_event_mask *cmd;
@ -279,7 +265,11 @@ static int ecc_send(struct net_buf *buf)
void bt_hci_ecc_init(void)
{
ecc_queue_init();
k_fifo_init(&ecc_queue);
k_thread_spawn(ecc_thread_stack, sizeof(ecc_thread_stack),
ecc_thread, NULL, NULL, NULL,
K_PRIO_PREEMPT(10), 0, K_NO_WAIT);
/* set wrapper for driver send function */
drv_send = bt_dev.drv->send;