You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by an...@apache.org on 2022/03/10 11:52:51 UTC

[mynewt-nimble] branch master updated (808ee41 -> 899c4fa)

This is an automated email from the ASF dual-hosted git repository.

andk pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-nimble.git.


    from 808ee41  nimble/ll: Add HCI Reset to supported commands
     new 6faa402  nimble/ll: Optimize num used channels calculation
     new 2162896  nimble/ll: Optimize CSA2 perm for Thumb2
     new 742bf62  nimble/ll: Refactor CSA2 prng a bit
     new d9c921a  nimble/ll: Add generic helpers to calculate/remap chan_idx for CSA2
     new ad36fd8  nimble/ll: Use common helper to calculate chan_idx for dci
     new 4e69f78  nimble/ll: Rename util to calculate common CSA2 dci
     new 899c4fa  nimble/ll: Add unit test for CSA2 (ISO)

The 7 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../controller/include/controller/ble_ll_utils.h   |  12 +-
 nimble/controller/src/ble_ll_adv.c                 |  24 +--
 nimble/controller/src/ble_ll_conn.c                |   4 +-
 nimble/controller/src/ble_ll_sync.c                |  12 +-
 nimble/controller/src/ble_ll_utils.c               | 226 ++++++++++++++++-----
 nimble/controller/test/src/ble_ll_csa2_test.c      | 172 ++++++++++++++++
 6 files changed, 375 insertions(+), 75 deletions(-)

[mynewt-nimble] 02/07: nimble/ll: Optimize CSA2 perm for Thumb2

Posted by an...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

andk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-nimble.git

commit 21628969837beb9fa36f90817da0c00542a9cf1d
Author: Andrzej Kaczmarek <an...@codecoup.pl>
AuthorDate: Mon Feb 28 11:19:57 2022 +0100

    nimble/ll: Optimize CSA2 perm for Thumb2
    
    Thumb2 has dedicated instructions for reversing bit and byte order so
    CSA2 permutation can be computed in 2 simple instructions.
    
    Also using uint32 for parameters means we skip zero-extending value
    every second instruction.
---
 nimble/controller/src/ble_ll_utils.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/nimble/controller/src/ble_ll_utils.c b/nimble/controller/src/ble_ll_utils.c
index c312626..7b3a863 100644
--- a/nimble/controller/src/ble_ll_utils.c
+++ b/nimble/controller/src/ble_ll_utils.c
@@ -202,10 +202,22 @@ ble_ll_utils_calc_num_used_chans(const uint8_t *chan_map)
 }
 
 #if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CSA2)
-static uint16_t
-ble_ll_utils_csa2_perm(uint16_t in)
+#if __thumb2__
+static inline uint32_t
+ble_ll_utils_csa2_perm(uint32_t val)
 {
-    uint16_t out = 0;
+    __asm__ volatile (".syntax unified      \n"
+                      "rbit %[val], %[val]  \n"
+                      "rev %[val], %[val]   \n"
+                      : [val] "+r" (val));
+
+    return val;
+}
+#else
+static uint32_t
+ble_ll_utils_csa2_perm(uint32_t in)
+{
+    uint32_t out = 0;
     int i;
 
     for (i = 0; i < 8; i++) {
@@ -218,6 +230,7 @@ ble_ll_utils_csa2_perm(uint16_t in)
 
     return out;
 }
+#endif
 
 static uint16_t
 ble_ll_utils_csa2_prng(uint16_t counter, uint16_t ch_id)

[mynewt-nimble] 05/07: nimble/ll: Use common helper to calculate chan_idx for dci

Posted by an...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

andk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-nimble.git

commit ad36fd84d50df3d859c6ebaca0dd1f746b51fa98
Author: Andrzej Kaczmarek <an...@codecoup.pl>
AuthorDate: Mon Feb 28 11:49:14 2022 +0100

    nimble/ll: Use common helper to calculate chan_idx for dci
---
 .../controller/include/controller/ble_ll_utils.h   |  6 +++
 nimble/controller/src/ble_ll_utils.c               | 63 ++++++++++++++++------
 2 files changed, 54 insertions(+), 15 deletions(-)

diff --git a/nimble/controller/include/controller/ble_ll_utils.h b/nimble/controller/include/controller/ble_ll_utils.h
index 48d76a6..12d14ed 100644
--- a/nimble/controller/include/controller/ble_ll_utils.h
+++ b/nimble/controller/include/controller/ble_ll_utils.h
@@ -23,6 +23,12 @@ uint32_t ble_ll_utils_calc_access_addr(void);
 uint8_t ble_ll_utils_remapped_channel(uint8_t remap_index, const uint8_t *chanmap);
 uint8_t ble_ll_utils_calc_dci_csa2(uint16_t event_cntr, uint16_t channel_id,
                                    uint8_t num_used_chans, const uint8_t *chanmap);
+uint16_t ble_ll_utils_dci_iso_event(uint16_t counter, uint16_t chan_id,
+                                    uint16_t *prn_sub_lu, uint8_t num_used_chans,
+                                    const uint8_t *chan_map, uint16_t *remap_idx);
+uint16_t ble_ll_utils_dci_iso_subevent(uint16_t chan_id, uint16_t *prn_sub_lu,
+                                       uint8_t num_used_chans, const uint8_t *chan_map,
+                                       uint16_t *remap_idx);
 uint8_t ble_ll_utils_calc_num_used_chans(const uint8_t *chan_map);
 uint32_t ble_ll_utils_calc_window_widening(uint32_t anchor_point,
                                            uint32_t last_anchor_point,
diff --git a/nimble/controller/src/ble_ll_utils.c b/nimble/controller/src/ble_ll_utils.c
index c2edb0d..1b28576 100644
--- a/nimble/controller/src/ble_ll_utils.c
+++ b/nimble/controller/src/ble_ll_utils.c
@@ -342,28 +342,61 @@ uint8_t
 ble_ll_utils_calc_dci_csa2(uint16_t event_cntr, uint16_t channel_id,
                            uint8_t num_used_chans, const uint8_t *chanmap)
 {
-    uint16_t channel_unmapped;
-    uint8_t remap_index;
-
     uint16_t prn_e;
-    uint8_t bitpos;
+    uint16_t chan_idx;
+    uint16_t remap_idx;
 
     prn_e = ble_ll_utils_csa2_prng(event_cntr, channel_id);
 
-    channel_unmapped = prn_e % 37;
+    chan_idx = ble_ll_utils_csa2_calc_chan_idx(prn_e, num_used_chans, chanmap,
+                                               &remap_idx);
 
-    /*
-     * If unmapped channel is the channel index of a used channel it is used
-     * as channel index.
-     */
-    bitpos = 1 << (channel_unmapped & 0x07);
-    if (chanmap[channel_unmapped >> 3] & bitpos) {
-        return channel_unmapped;
-    }
+    return chan_idx;
+}
+
+uint16_t
+ble_ll_utils_dci_iso_event(uint16_t counter, uint16_t chan_id,
+                           uint16_t *prn_sub_lu, uint8_t num_used_chans,
+                           const uint8_t *chan_map, uint16_t *remap_idx)
+{
+    uint16_t prn_s;
+    uint16_t prn_e;
+    uint16_t chan_idx;
+
+    prn_s = ble_ll_utils_csa2_prn_s(counter, chan_id);
+    prn_e = prn_s ^ chan_id;
 
-    remap_index = (num_used_chans * prn_e) / 0x10000;
+    *prn_sub_lu = prn_s;
 
-    return ble_ll_utils_remapped_channel(remap_index, chanmap);
+    chan_idx = ble_ll_utils_csa2_calc_chan_idx(prn_e, num_used_chans, chan_map,
+                                               remap_idx);
+
+    return chan_idx;
+}
+
+uint16_t
+ble_ll_utils_dci_iso_subevent(uint16_t chan_id, uint16_t *prn_sub_lu,
+                              uint8_t num_used_chans, const uint8_t *chan_map,
+                              uint16_t *remap_idx)
+{
+    uint16_t prn_sub_se;
+    uint16_t chan_idx;
+    uint16_t d;
+
+    *prn_sub_lu = ble_ll_utils_csa2_perm(*prn_sub_lu);
+    *prn_sub_lu = ble_ll_utils_csa2_mam(*prn_sub_lu, chan_id);
+    prn_sub_se = *prn_sub_lu ^ chan_id;
+
+    /* Core 5.3, Vol 6, Part B, 4.5.8.3.6 (enjoy!) */
+    /* TODO optimize this somehow */
+    d = max(1, max(min(3, num_used_chans - 5),
+                   min(11, (num_used_chans - 10) / 2)));
+    *remap_idx = (*remap_idx + d + prn_sub_se *
+                  (num_used_chans - 2 * d + 1) / 65536) % num_used_chans;
+
+    chan_idx = ble_ll_utils_csa2_remap2chan(*remap_idx, chan_map);
+
+    return chan_idx;
 }
 #endif
 

[mynewt-nimble] 04/07: nimble/ll: Add generic helpers to calculate/remap chan_idx for CSA2

Posted by an...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

andk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-nimble.git

commit d9c921a81527fe2787ab71a7b6364ae30ae44c5a
Author: Andrzej Kaczmarek <an...@codecoup.pl>
AuthorDate: Mon Feb 28 11:42:12 2022 +0100

    nimble/ll: Add generic helpers to calculate/remap chan_idx for CSA2
    
    For events we calculate unmapped chan_idx from prn_e value and then
    remap it if necessary.
    
    For subevents we only need to calculate index of chan_idx on used
    channels list.
---
 nimble/controller/src/ble_ll_utils.c | 69 ++++++++++++++++++++++++++++++++++++
 1 file changed, 69 insertions(+)

diff --git a/nimble/controller/src/ble_ll_utils.c b/nimble/controller/src/ble_ll_utils.c
index ee25f83..c2edb0d 100644
--- a/nimble/controller/src/ble_ll_utils.c
+++ b/nimble/controller/src/ble_ll_utils.c
@@ -269,6 +269,75 @@ ble_ll_utils_csa2_prng(uint16_t counter, uint16_t ch_id)
     return prn_e;
 }
 
+/* Find remap_idx for given chan_idx */
+static uint16_t
+ble_ll_utils_csa2_chan2remap(uint16_t chan_idx, const uint8_t *chan_map)
+{
+    uint16_t remap_idx = 0;
+    uint32_t u32 = 0;
+    unsigned idx;
+
+    for (idx = 0; idx < 37; idx++) {
+        if ((idx % 8) == 0) {
+            u32 = chan_map[idx / 8];
+        }
+        if (u32 & 1) {
+            if (idx == chan_idx) {
+                return remap_idx;
+            }
+            remap_idx++;
+        }
+        u32 >>= 1;
+    }
+
+    BLE_LL_ASSERT(0);
+
+    return 0;
+}
+
+/* Find chan_idx at given remap_idx */
+static uint16_t
+ble_ll_utils_csa2_remap2chan(uint16_t remap_idx, const uint8_t *chan_map)
+{
+    uint32_t u32 = 0;
+    unsigned idx;
+
+    for (idx = 0; idx < 37; idx++) {
+        if ((idx % 8) == 0) {
+            u32 = chan_map[idx / 8];
+        }
+        if (u32 & 1) {
+            if (!remap_idx) {
+                return idx;
+            }
+            remap_idx--;
+        }
+        u32 >>= 1;
+    }
+
+    BLE_LL_ASSERT(0);
+
+    return 0;
+}
+
+static uint16_t
+ble_ll_utils_csa2_calc_chan_idx(uint16_t prn_e, uint8_t num_used_chans,
+                                const uint8_t *chanm_map, uint16_t *remap_idx)
+{
+    uint16_t chan_idx;
+
+    chan_idx = prn_e % 37;
+    if (chanm_map[chan_idx / 8] & (1 << (chan_idx % 8))) {
+        *remap_idx = ble_ll_utils_csa2_chan2remap(chan_idx, chanm_map);
+        return chan_idx;
+    }
+
+    *remap_idx = (num_used_chans * prn_e) / 65536;
+    chan_idx = ble_ll_utils_csa2_remap2chan(*remap_idx, chanm_map);
+
+    return chan_idx;
+}
+
 uint8_t
 ble_ll_utils_calc_dci_csa2(uint16_t event_cntr, uint16_t channel_id,
                            uint8_t num_used_chans, const uint8_t *chanmap)

[mynewt-nimble] 07/07: nimble/ll: Add unit test for CSA2 (ISO)

Posted by an...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

andk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-nimble.git

commit 899c4fa880f6d7379d9a45edf1921537d2a139a4
Author: Andrzej Kaczmarek <an...@codecoup.pl>
AuthorDate: Mon Feb 28 10:20:57 2022 +0100

    nimble/ll: Add unit test for CSA2 (ISO)
---
 nimble/controller/test/src/ble_ll_csa2_test.c | 172 ++++++++++++++++++++++++++
 1 file changed, 172 insertions(+)

diff --git a/nimble/controller/test/src/ble_ll_csa2_test.c b/nimble/controller/test/src/ble_ll_csa2_test.c
index 5261eb5..566500e 100644
--- a/nimble/controller/test/src/ble_ll_csa2_test.c
+++ b/nimble/controller/test/src/ble_ll_csa2_test.c
@@ -22,6 +22,7 @@
 #include "testutil/testutil.h"
 #include "controller/ble_ll_test.h"
 #include "controller/ble_ll_conn.h"
+#include "controller/ble_ll_utils.h"
 #include "ble_ll_csa2_test.h"
 
 TEST_CASE_SELF(ble_ll_csa2_test_1)
@@ -108,8 +109,179 @@ TEST_CASE_SELF(ble_ll_csa2_test_2)
     TEST_ASSERT(rc == 34);
 }
 
+TEST_CASE_SELF(ble_ll_csa2_test_3)
+{
+    uint8_t chan_map[5];
+    uint8_t num_used_chans;
+    uint16_t chan_id;
+    uint16_t prn_sub_lu;
+    uint16_t chan_idx;
+    uint16_t remap_idx;
+
+    /* Sample data: Core 5.3, Vol 6, Part C, 3.1 */
+    chan_map[0] = 0xff;
+    chan_map[1] = 0xff;
+    chan_map[2] = 0xff;
+    chan_map[3] = 0xff;
+    chan_map[4] = 0x1f;
+    num_used_chans = ble_ll_utils_calc_num_used_chans(chan_map);
+    TEST_ASSERT(num_used_chans == 37);
+    chan_id = 0x305f;
+
+    chan_idx = ble_ll_utils_dci_iso_event(0, chan_id, &prn_sub_lu, num_used_chans, chan_map, &remap_idx);
+    TEST_ASSERT((prn_sub_lu ^ chan_id) == 56857);
+    TEST_ASSERT(chan_idx == 25);
+    TEST_ASSERT(remap_idx == 25);
+
+    chan_idx = ble_ll_utils_dci_iso_subevent(chan_id, &prn_sub_lu, num_used_chans, chan_map, &remap_idx);
+    TEST_ASSERT((prn_sub_lu ^ chan_id) == 11710);
+    TEST_ASSERT(chan_idx == 1);
+    TEST_ASSERT(remap_idx == 1);
+
+    chan_idx = ble_ll_utils_dci_iso_subevent(chan_id, &prn_sub_lu, num_used_chans, chan_map, &remap_idx);
+    TEST_ASSERT((prn_sub_lu ^ chan_id) == 16649);
+    TEST_ASSERT(chan_idx == 16);
+    TEST_ASSERT(remap_idx == 16);
+
+    chan_idx = ble_ll_utils_dci_iso_subevent(chan_id, &prn_sub_lu, num_used_chans, chan_map, &remap_idx);
+    TEST_ASSERT((prn_sub_lu ^ chan_id) == 38198);
+    TEST_ASSERT(chan_idx == 36);
+    TEST_ASSERT(remap_idx == 36);
+
+    chan_idx = ble_ll_utils_dci_iso_event(1, chan_id, &prn_sub_lu, num_used_chans, chan_map, &remap_idx);
+    TEST_ASSERT((prn_sub_lu ^ chan_id) == 1685);
+    TEST_ASSERT(chan_idx == 20);
+    TEST_ASSERT(remap_idx == 20);
+
+    chan_idx = ble_ll_utils_dci_iso_subevent(chan_id, &prn_sub_lu, num_used_chans, chan_map, &remap_idx);
+    TEST_ASSERT((prn_sub_lu ^ chan_id) == 20925);
+    TEST_ASSERT(chan_idx == 36);
+    TEST_ASSERT(remap_idx == 36);
+
+    chan_idx = ble_ll_utils_dci_iso_subevent(chan_id, &prn_sub_lu, num_used_chans, chan_map, &remap_idx);
+    TEST_ASSERT((prn_sub_lu ^ chan_id) == 11081);
+    TEST_ASSERT(chan_idx == 12);
+    TEST_ASSERT(remap_idx == 12);
+
+    chan_idx = ble_ll_utils_dci_iso_subevent(chan_id, &prn_sub_lu, num_used_chans, chan_map, &remap_idx);
+    TEST_ASSERT((prn_sub_lu ^ chan_id) == 48920);
+    TEST_ASSERT(chan_idx == 34);
+    TEST_ASSERT(remap_idx == 34);
+
+    chan_idx = ble_ll_utils_dci_iso_event(2, chan_id, &prn_sub_lu, num_used_chans, chan_map, &remap_idx);
+    TEST_ASSERT((prn_sub_lu ^ chan_id) == 38301);
+    TEST_ASSERT(chan_idx == 6);
+    TEST_ASSERT(remap_idx == 6);
+
+    chan_idx = ble_ll_utils_dci_iso_subevent(chan_id, &prn_sub_lu, num_used_chans, chan_map, &remap_idx);
+    TEST_ASSERT((prn_sub_lu ^ chan_id) == 6541);
+    TEST_ASSERT(chan_idx == 18);
+    TEST_ASSERT(remap_idx == 18);
+
+    chan_idx = ble_ll_utils_dci_iso_subevent(chan_id, &prn_sub_lu, num_used_chans, chan_map, &remap_idx);
+    TEST_ASSERT((prn_sub_lu ^ chan_id) == 14597);
+    TEST_ASSERT(chan_idx == 32);
+    TEST_ASSERT(remap_idx == 32);
+
+    chan_idx = ble_ll_utils_dci_iso_subevent(chan_id, &prn_sub_lu, num_used_chans, chan_map, &remap_idx);
+    TEST_ASSERT((prn_sub_lu ^ chan_id) == 62982);
+    TEST_ASSERT(chan_idx == 21);
+    TEST_ASSERT(remap_idx == 21);
+
+    chan_idx = ble_ll_utils_dci_iso_event(3, chan_id, &prn_sub_lu, num_used_chans, chan_map, &remap_idx);
+    TEST_ASSERT((prn_sub_lu ^ chan_id) == 27475);
+    TEST_ASSERT(chan_idx == 21);
+    TEST_ASSERT(remap_idx == 21);
+
+    chan_idx = ble_ll_utils_dci_iso_subevent(chan_id, &prn_sub_lu, num_used_chans, chan_map, &remap_idx);
+    TEST_ASSERT((prn_sub_lu ^ chan_id) == 40400);
+    TEST_ASSERT(chan_idx == 4);
+    TEST_ASSERT(remap_idx == 4);
+
+    chan_idx = ble_ll_utils_dci_iso_subevent(chan_id, &prn_sub_lu, num_used_chans, chan_map, &remap_idx);
+    TEST_ASSERT((prn_sub_lu ^ chan_id) == 30015);
+    TEST_ASSERT(chan_idx == 22);
+    TEST_ASSERT(remap_idx == 22);
+
+    chan_idx = ble_ll_utils_dci_iso_subevent(chan_id, &prn_sub_lu, num_used_chans, chan_map, &remap_idx);
+    TEST_ASSERT((prn_sub_lu ^ chan_id) == 49818);
+    TEST_ASSERT(chan_idx == 8);
+    TEST_ASSERT(remap_idx == 8);
+
+    /* Sample data: Core 5.3, Vol 6, Part C, 3.2 */
+    chan_map[0] = 0x00;
+    chan_map[1] = 0x06;
+    chan_map[2] = 0xe0;
+    chan_map[3] = 0x00;
+    chan_map[4] = 0x1e;
+    num_used_chans = ble_ll_utils_calc_num_used_chans(chan_map);
+    TEST_ASSERT(num_used_chans == 9);
+    chan_id = 0x305f;
+
+    chan_idx = ble_ll_utils_dci_iso_event(6, chan_id, &prn_sub_lu, num_used_chans, chan_map, &remap_idx);
+    TEST_ASSERT((prn_sub_lu ^ chan_id) == 10975);
+    TEST_ASSERT(chan_idx == 23);
+    TEST_ASSERT(remap_idx == 4);
+
+    chan_idx = ble_ll_utils_dci_iso_subevent(chan_id, &prn_sub_lu, num_used_chans, chan_map, &remap_idx);
+    TEST_ASSERT((prn_sub_lu ^ chan_id) == 14383);
+    TEST_ASSERT(chan_idx == 35);
+    TEST_ASSERT(remap_idx == 7);
+
+    chan_idx = ble_ll_utils_dci_iso_subevent(chan_id, &prn_sub_lu, num_used_chans, chan_map, &remap_idx);
+    TEST_ASSERT((prn_sub_lu ^ chan_id) == 28946);
+    TEST_ASSERT(chan_idx == 21);
+    TEST_ASSERT(remap_idx == 2);
+
+    chan_idx = ble_ll_utils_dci_iso_subevent(chan_id, &prn_sub_lu, num_used_chans, chan_map, &remap_idx);
+    TEST_ASSERT((prn_sub_lu ^ chan_id) == 61038);
+    TEST_ASSERT(chan_idx == 36);
+    TEST_ASSERT(remap_idx == 8);
+
+    chan_idx = ble_ll_utils_dci_iso_event(7, chan_id, &prn_sub_lu, num_used_chans, chan_map, &remap_idx);
+    TEST_ASSERT((prn_sub_lu ^ chan_id) == 5490);
+    TEST_ASSERT(chan_idx == 9);
+    TEST_ASSERT(remap_idx == 0);
+
+    chan_idx = ble_ll_utils_dci_iso_subevent(chan_id, &prn_sub_lu, num_used_chans, chan_map, &remap_idx);
+    TEST_ASSERT((prn_sub_lu ^ chan_id) == 4108);
+    TEST_ASSERT(chan_idx == 22);
+    TEST_ASSERT(remap_idx == 3);
+
+    chan_idx = ble_ll_utils_dci_iso_subevent(chan_id, &prn_sub_lu, num_used_chans, chan_map, &remap_idx);
+    TEST_ASSERT((prn_sub_lu ^ chan_id) == 45462);
+    TEST_ASSERT(chan_idx == 36);
+    TEST_ASSERT(remap_idx == 8);
+
+    chan_idx = ble_ll_utils_dci_iso_subevent(chan_id, &prn_sub_lu, num_used_chans, chan_map, &remap_idx);
+    TEST_ASSERT((prn_sub_lu ^ chan_id) == 64381);
+    TEST_ASSERT(chan_idx == 33);
+    TEST_ASSERT(remap_idx == 5);
+
+    chan_idx = ble_ll_utils_dci_iso_event(8, chan_id, &prn_sub_lu, num_used_chans, chan_map, &remap_idx);
+    TEST_ASSERT((prn_sub_lu ^ chan_id) == 46970);
+    TEST_ASSERT(chan_idx == 34);
+    TEST_ASSERT(remap_idx == 6);
+
+    chan_idx = ble_ll_utils_dci_iso_subevent(chan_id, &prn_sub_lu, num_used_chans, chan_map, &remap_idx);
+    TEST_ASSERT((prn_sub_lu ^ chan_id) == 7196);
+    TEST_ASSERT(chan_idx == 9);
+    TEST_ASSERT(remap_idx == 0);
+
+    chan_idx = ble_ll_utils_dci_iso_subevent(chan_id, &prn_sub_lu, num_used_chans, chan_map, &remap_idx);
+    TEST_ASSERT((prn_sub_lu ^ chan_id) == 33054);
+    TEST_ASSERT(chan_idx == 33);
+    TEST_ASSERT(remap_idx == 5);
+
+    chan_idx = ble_ll_utils_dci_iso_subevent(chan_id, &prn_sub_lu, num_used_chans, chan_map, &remap_idx);
+    TEST_ASSERT((prn_sub_lu ^ chan_id) == 42590);
+    TEST_ASSERT(chan_idx == 10);
+    TEST_ASSERT(remap_idx == 1);
+}
+
 TEST_SUITE(ble_ll_csa2_test_suite)
 {
     ble_ll_csa2_test_1();
     ble_ll_csa2_test_2();
+    ble_ll_csa2_test_3();
 }

[mynewt-nimble] 03/07: nimble/ll: Refactor CSA2 prng a bit

Posted by an...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

andk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-nimble.git

commit 742bf62ea8f4a32a5c32440c13aee9c11f54e909
Author: Andrzej Kaczmarek <an...@codecoup.pl>
AuthorDate: Mon Feb 28 11:37:25 2022 +0100

    nimble/ll: Refactor CSA2 prng a bit
    
    Prng calculates prn_e, but we also need prn_s (for iso subevents) which
    is genreated before final xor in prng. Add helper to calculate prn_s
    and make prng use it.
    
    Also split mam to separate helper just to make it look nicer.
---
 nimble/controller/src/ble_ll_utils.c | 36 ++++++++++++++++++++++++++----------
 1 file changed, 26 insertions(+), 10 deletions(-)

diff --git a/nimble/controller/src/ble_ll_utils.c b/nimble/controller/src/ble_ll_utils.c
index 7b3a863..ee25f83 100644
--- a/nimble/controller/src/ble_ll_utils.c
+++ b/nimble/controller/src/ble_ll_utils.c
@@ -232,23 +232,39 @@ ble_ll_utils_csa2_perm(uint32_t in)
 }
 #endif
 
+static inline uint32_t
+ble_ll_utils_csa2_mam(uint32_t a, uint32_t b)
+{
+    return (17 * a + b) % 65536;
+}
+
 static uint16_t
-ble_ll_utils_csa2_prng(uint16_t counter, uint16_t ch_id)
+ble_ll_utils_csa2_prn_s(uint16_t counter, uint16_t ch_id)
 {
-    uint16_t prn_e;
+    uint32_t prn_s;
+
+    prn_s = counter ^ ch_id;
 
-    prn_e = counter ^ ch_id;
+    prn_s = ble_ll_utils_csa2_perm(prn_s);
+    prn_s = ble_ll_utils_csa2_mam(prn_s, ch_id);
 
-    prn_e = ble_ll_utils_csa2_perm(prn_e);
-    prn_e = (prn_e * 17) + ch_id;
+    prn_s = ble_ll_utils_csa2_perm(prn_s);
+    prn_s = ble_ll_utils_csa2_mam(prn_s, ch_id);
 
-    prn_e = ble_ll_utils_csa2_perm(prn_e);
-    prn_e = (prn_e * 17) + ch_id;
+    prn_s = ble_ll_utils_csa2_perm(prn_s);
+    prn_s = ble_ll_utils_csa2_mam(prn_s, ch_id);
 
-    prn_e = ble_ll_utils_csa2_perm(prn_e);
-    prn_e = (prn_e * 17) + ch_id;
+    return prn_s;
+}
+
+static uint16_t
+ble_ll_utils_csa2_prng(uint16_t counter, uint16_t ch_id)
+{
+    uint16_t prn_s;
+    uint16_t prn_e;
 
-    prn_e = prn_e ^ ch_id;
+    prn_s = ble_ll_utils_csa2_prn_s(counter, ch_id);
+    prn_e = prn_s ^ ch_id;
 
     return prn_e;
 }

[mynewt-nimble] 06/07: nimble/ll: Rename util to calculate common CSA2 dci

Posted by an...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

andk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-nimble.git

commit 4e69f781b597fccfe1f87a28cc0d7f8fd43f6322
Author: Andrzej Kaczmarek <an...@codecoup.pl>
AuthorDate: Mon Feb 28 11:52:47 2022 +0100

    nimble/ll: Rename util to calculate common CSA2 dci
    
    Just to have it consistent with iso utils.
---
 .../controller/include/controller/ble_ll_utils.h   |  4 ++--
 nimble/controller/src/ble_ll_adv.c                 | 24 +++++++++++-----------
 nimble/controller/src/ble_ll_conn.c                |  4 ++--
 nimble/controller/src/ble_ll_sync.c                | 12 +++++------
 nimble/controller/src/ble_ll_utils.c               |  8 ++++----
 5 files changed, 26 insertions(+), 26 deletions(-)

diff --git a/nimble/controller/include/controller/ble_ll_utils.h b/nimble/controller/include/controller/ble_ll_utils.h
index 12d14ed..ae8abfb 100644
--- a/nimble/controller/include/controller/ble_ll_utils.h
+++ b/nimble/controller/include/controller/ble_ll_utils.h
@@ -21,8 +21,8 @@
 
 uint32_t ble_ll_utils_calc_access_addr(void);
 uint8_t ble_ll_utils_remapped_channel(uint8_t remap_index, const uint8_t *chanmap);
-uint8_t ble_ll_utils_calc_dci_csa2(uint16_t event_cntr, uint16_t channel_id,
-                                   uint8_t num_used_chans, const uint8_t *chanmap);
+uint8_t ble_ll_utils_dci_csa2(uint16_t counter, uint16_t chan_id,
+                              uint8_t num_used_chans, const uint8_t *chan_map);
 uint16_t ble_ll_utils_dci_iso_event(uint16_t counter, uint16_t chan_id,
                                     uint16_t *prn_sub_lu, uint8_t num_used_chans,
                                     const uint8_t *chan_map, uint16_t *remap_idx);
diff --git a/nimble/controller/src/ble_ll_adv.c b/nimble/controller/src/ble_ll_adv.c
index 070c89c..bc04c8f 100644
--- a/nimble/controller/src/ble_ll_adv.c
+++ b/nimble/controller/src/ble_ll_adv.c
@@ -1371,10 +1371,10 @@ ble_ll_adv_aux_calculate(struct ble_ll_adv_sm *advsm,
     aux->ext_hdr = 0;
 
 #if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CSA2)
-    aux->chan = ble_ll_utils_calc_dci_csa2(advsm->event_cntr++,
-                                           advsm->channel_id,
-                                           g_ble_ll_conn_params.num_used_chans,
-                                           g_ble_ll_conn_params.central_chan_map);
+    aux->chan = ble_ll_utils_dci_csa2(advsm->event_cntr++,
+                                      advsm->channel_id,
+                                      g_ble_ll_conn_params.num_used_chans,
+                                      g_ble_ll_conn_params.central_chan_map);
 #else
     aux->chan = ble_ll_utils_remapped_channel(ble_ll_rand() % BLE_PHY_NUM_DATA_CHANS,
                                               g_ble_ll_conn_params.central_chan_map);
@@ -2316,10 +2316,10 @@ ble_ll_adv_periodic_schedule_first(struct ble_ll_adv_sm *advsm,
      * Preincrement event counter as we later send this in PDU so make sure
      * same values are used
      */
-    chan = ble_ll_utils_calc_dci_csa2(++advsm->periodic_event_cntr,
-                                      advsm->periodic_channel_id,
-                                      advsm->periodic_num_used_chans,
-                                      advsm->periodic_chanmap);
+    chan = ble_ll_utils_dci_csa2(++advsm->periodic_event_cntr,
+                                 advsm->periodic_channel_id,
+                                 advsm->periodic_num_used_chans,
+                                 advsm->periodic_chanmap);
 
     ble_ll_adv_sync_calculate(advsm, sync, 0, chan);
 
@@ -2405,10 +2405,10 @@ ble_ll_adv_periodic_schedule_next(struct ble_ll_adv_sm *advsm)
     BLE_LL_ASSERT(rem_sync_data_len > 0);
 
     /* we use separate counter for chaining */
-    chan = ble_ll_utils_calc_dci_csa2(advsm->periodic_chain_event_cntr++,
-                                      advsm->periodic_channel_id,
-                                      advsm->periodic_num_used_chans,
-                                      advsm->periodic_chanmap);
+    chan = ble_ll_utils_dci_csa2(advsm->periodic_chain_event_cntr++,
+                                 advsm->periodic_channel_id,
+                                 advsm->periodic_num_used_chans,
+                                 advsm->periodic_chanmap);
 
     ble_ll_adv_sync_calculate(advsm, sync_next, next_sync_data_offset, chan);
     max_usecs = ble_ll_pdu_tx_time_get(sync_next->payload_len, advsm->sec_phy);
diff --git a/nimble/controller/src/ble_ll_conn.c b/nimble/controller/src/ble_ll_conn.c
index f3d903d..c41c35b 100644
--- a/nimble/controller/src/ble_ll_conn.c
+++ b/nimble/controller/src/ble_ll_conn.c
@@ -747,8 +747,8 @@ ble_ll_conn_calc_dci(struct ble_ll_conn_sm *conn, uint16_t latency)
 
 #if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CSA2)
     if (CONN_F_CSA2_SUPP(conn)) {
-        return ble_ll_utils_calc_dci_csa2(conn->event_cntr, conn->channel_id,
-                                          conn->num_used_chans, conn->chanmap);
+        return ble_ll_utils_dci_csa2(conn->event_cntr, conn->channel_id,
+                                     conn->num_used_chans, conn->chanmap);
     }
 #endif
 
diff --git a/nimble/controller/src/ble_ll_sync.c b/nimble/controller/src/ble_ll_sync.c
index e5ef0ef..43f328e 100644
--- a/nimble/controller/src/ble_ll_sync.c
+++ b/nimble/controller/src/ble_ll_sync.c
@@ -1178,8 +1178,8 @@ ble_ll_sync_next_event(struct ble_ll_sync_sm *sm, uint32_t cur_ww_adjust)
     }
 
     /* Calculate channel index of next event */
-    sm->chan_index = ble_ll_utils_calc_dci_csa2(sm->event_cntr, sm->channel_id,
-                                                sm->num_used_chans, sm->chanmap);
+    sm->chan_index = ble_ll_utils_dci_csa2(sm->event_cntr, sm->channel_id,
+                                           sm->num_used_chans, sm->chanmap);
 
     cur_ww = ble_ll_utils_calc_window_widening(sm->anchor_point,
                                                sm->last_anchor_point,
@@ -1409,8 +1409,8 @@ ble_ll_sync_info_event(struct ble_ll_scan_addr_data *addrd,
     sm->window_widening = BLE_LL_JITTER_USECS;
 
     /* Calculate channel index of first event */
-    sm->chan_index = ble_ll_utils_calc_dci_csa2(sm->event_cntr, sm->channel_id,
-                                                sm->num_used_chans, sm->chanmap);
+    sm->chan_index = ble_ll_utils_dci_csa2(sm->event_cntr, sm->channel_id,
+                                           sm->num_used_chans, sm->chanmap);
 
     if (ble_ll_sched_sync(&sm->sch, rxhdr->beg_cputime, rxhdr->rem_usecs,
                           offset, sm->phy_mode)) {
@@ -1957,8 +1957,8 @@ ble_ll_sync_periodic_ind(struct ble_ll_conn_sm *connsm,
     sm->phy_mode = phy_mode;
 
     /* Calculate channel index of first event */
-    sm->chan_index = ble_ll_utils_calc_dci_csa2(sm->event_cntr, sm->channel_id,
-                                                sm->num_used_chans, sm->chanmap);
+    sm->chan_index = ble_ll_utils_dci_csa2(sm->event_cntr, sm->channel_id,
+                                           sm->num_used_chans, sm->chanmap);
 
     /* get anchor for specified conn event */
     conn_event_count = get_le16(sync_ind + 20);
diff --git a/nimble/controller/src/ble_ll_utils.c b/nimble/controller/src/ble_ll_utils.c
index 1b28576..a2507ad 100644
--- a/nimble/controller/src/ble_ll_utils.c
+++ b/nimble/controller/src/ble_ll_utils.c
@@ -339,16 +339,16 @@ ble_ll_utils_csa2_calc_chan_idx(uint16_t prn_e, uint8_t num_used_chans,
 }
 
 uint8_t
-ble_ll_utils_calc_dci_csa2(uint16_t event_cntr, uint16_t channel_id,
-                           uint8_t num_used_chans, const uint8_t *chanmap)
+ble_ll_utils_dci_csa2(uint16_t counter, uint16_t chan_id,
+                      uint8_t num_used_chans, const uint8_t *chan_map)
 {
     uint16_t prn_e;
     uint16_t chan_idx;
     uint16_t remap_idx;
 
-    prn_e = ble_ll_utils_csa2_prng(event_cntr, channel_id);
+    prn_e = ble_ll_utils_csa2_prng(counter, chan_id);
 
-    chan_idx = ble_ll_utils_csa2_calc_chan_idx(prn_e, num_used_chans, chanmap,
+    chan_idx = ble_ll_utils_csa2_calc_chan_idx(prn_e, num_used_chans, chan_map,
                                                &remap_idx);
 
     return chan_idx;

[mynewt-nimble] 01/07: nimble/ll: Optimize num used channels calculation

Posted by an...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

andk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-nimble.git

commit 6faa4027dea1d1ed58e344a7eba938ca308242e1
Author: Andrzej Kaczmarek <an...@codecoup.pl>
AuthorDate: Mon Feb 28 11:05:06 2022 +0100

    nimble/ll: Optimize num used channels calculation
    
    Using single loop and uint32 should be more efficient than nested loops
    with uint8.
---
 .../controller/include/controller/ble_ll_utils.h   |  2 +-
 nimble/controller/src/ble_ll_utils.c               | 37 ++++++++--------------
 2 files changed, 15 insertions(+), 24 deletions(-)

diff --git a/nimble/controller/include/controller/ble_ll_utils.h b/nimble/controller/include/controller/ble_ll_utils.h
index e0b1646..48d76a6 100644
--- a/nimble/controller/include/controller/ble_ll_utils.h
+++ b/nimble/controller/include/controller/ble_ll_utils.h
@@ -23,7 +23,7 @@ uint32_t ble_ll_utils_calc_access_addr(void);
 uint8_t ble_ll_utils_remapped_channel(uint8_t remap_index, const uint8_t *chanmap);
 uint8_t ble_ll_utils_calc_dci_csa2(uint16_t event_cntr, uint16_t channel_id,
                                    uint8_t num_used_chans, const uint8_t *chanmap);
-uint8_t ble_ll_utils_calc_num_used_chans(const uint8_t *chanmap);
+uint8_t ble_ll_utils_calc_num_used_chans(const uint8_t *chan_map);
 uint32_t ble_ll_utils_calc_window_widening(uint32_t anchor_point,
                                            uint32_t last_anchor_point,
                                            uint8_t central_sca);
diff --git a/nimble/controller/src/ble_ll_utils.c b/nimble/controller/src/ble_ll_utils.c
index 66ca3e2..c312626 100644
--- a/nimble/controller/src/ble_ll_utils.c
+++ b/nimble/controller/src/ble_ll_utils.c
@@ -182,32 +182,23 @@ ble_ll_utils_remapped_channel(uint8_t remap_index, const uint8_t *chanmap)
 }
 
 uint8_t
-ble_ll_utils_calc_num_used_chans(const uint8_t *chmap)
+ble_ll_utils_calc_num_used_chans(const uint8_t *chan_map)
 {
-    int i;
-    int j;
-    uint8_t mask;
-    uint8_t chanbyte;
-    uint8_t used_channels;
-
-    used_channels = 0;
-    for (i = 0; i < BLE_LL_CHMAP_LEN; ++i) {
-        chanbyte = chmap[i];
-        if (chanbyte) {
-            if (chanbyte == 0xff) {
-                used_channels += 8;
-            } else {
-                mask = 0x01;
-                for (j = 0; j < 8; ++j) {
-                    if (chanbyte & mask) {
-                        ++used_channels;
-                    }
-                    mask <<= 1;
-                }
-            }
+    uint32_t u32 = 0;
+    uint32_t num_used_chans = 0;
+    unsigned idx;
+
+    for (idx = 0; idx < 37; idx++) {
+        if ((idx % 8) == 0) {
+            u32 = chan_map[idx / 8];
         }
+        if (u32 & 1) {
+            num_used_chans++;
+        }
+        u32 >>= 1;
     }
-    return used_channels;
+
+    return num_used_chans;
 }
 
 #if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CSA2)