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 2023/01/29 09:59:59 UTC

[mynewt-nimble] branch master updated (fcf16680 -> bc1662c9)

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 fcf16680 ci: Make check style output more readable
     new 8be739d0 nimble/phy: Rework phy encryption API
     new 20198fae nimble/phy: Add API to set ifs for tx-tx transition
     new bc1662c9 nimble/phy/nrf53: Clean state on phy disable

The 3 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:
 nimble/controller/include/controller/ble_phy.h | 15 ++++---
 nimble/controller/src/ble_ll_conn.c            | 46 ++++++++++----------
 nimble/drivers/dialog_cmac/src/ble_phy.c       | 28 ++++++------
 nimble/drivers/native/src/ble_phy.c            | 20 +++------
 nimble/drivers/nrf51/src/ble_phy.c             | 28 +++++-------
 nimble/drivers/nrf5x/src/ble_phy.c             | 59 +++++++++++++++++---------
 6 files changed, 102 insertions(+), 94 deletions(-)


[mynewt-nimble] 02/03: nimble/phy: Add API to set ifs for tx-tx transition

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 20198fae319842e445cfb24cc5038ccb877be41e
Author: Andrzej Kaczmarek <an...@codecoup.pl>
AuthorDate: Sat Jan 21 00:49:50 2023 +0100

    nimble/phy: Add API to set ifs for tx-tx transition
    
    TX-TX transition is kind of special since we'll use it e.g. for ISO to
    achieve proper spacing between subevents thus we want to be able to
    control time between start of PDUs.
---
 nimble/controller/include/controller/ble_phy.h |  4 ++++
 nimble/drivers/nrf5x/src/ble_phy.c             | 28 ++++++++++++++++++++++++--
 2 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/nimble/controller/include/controller/ble_phy.h b/nimble/controller/include/controller/ble_phy.h
index 9b3a57b9..902ac034 100644
--- a/nimble/controller/include/controller/ble_phy.h
+++ b/nimble/controller/include/controller/ble_phy.h
@@ -92,6 +92,10 @@ int ble_phy_setchan(uint8_t chan, uint32_t access_addr, uint32_t crcinit);
 void ble_phy_tifs_set(uint16_t tifs);
 #endif
 
+/* Set T_ifs for tx-tx transitions. Anchor is 0 for start of previous PDU,
+ * non-zero for end of PDU */
+void ble_phy_tifs_txtx_set(uint16_t usecs, uint8_t anchor);
+
 /* Set transmit start time */
 int ble_phy_tx_set_start_time(uint32_t cputime, uint8_t rem_usecs);
 
diff --git a/nimble/drivers/nrf5x/src/ble_phy.c b/nimble/drivers/nrf5x/src/ble_phy.c
index d4fe0a51..3cdae5c5 100644
--- a/nimble/drivers/nrf5x/src/ble_phy.c
+++ b/nimble/drivers/nrf5x/src/ble_phy.c
@@ -68,6 +68,8 @@
 extern void tm_tick(void);
 #endif
 
+#include <controller/ble_ll_pdu.h>
+
 /*
  * NOTE: This code uses a couple of PPI channels so care should be taken when
  *       using PPI somewhere else.
@@ -153,6 +155,9 @@ struct ble_phy_obj
 #if MYNEWT_VAL(BLE_PHY_VARIABLE_TIFS)
     uint16_t tifs;
 #endif
+
+    uint16_t txtx_time_us;
+    uint8_t txtx_time_anchor;
 };
 static struct ble_phy_obj g_ble_phy_data;
 
@@ -1092,8 +1097,20 @@ ble_phy_tx_end_isr(void)
          *       case we still rxd something, so perhaps we could check it here
          */
     } else if (transition == BLE_PHY_TRANSITION_TX_TX) {
-        /* Schedule TX exactly T_IFS after TX end captured in CC[2] */
-        tx_time = NRF_TIMER0->CC[2] + tifs;
+        if (g_ble_phy_data.txtx_time_anchor) {
+            /* Schedule next TX relative to current TX end. TX end timestamp is
+             * captured in CC[2].
+             */
+            tx_time = NRF_TIMER0->CC[2] + g_ble_phy_data.txtx_time_us;
+        } else {
+            /* Schedule next TX relative to current TX start. AA timestamp is
+             * captured in CC[1], we need to adjust for sync word to get TX
+             * start.
+             */
+            tx_time = NRF_TIMER0->CC[1] - ble_ll_pdu_syncword_us(tx_phy_mode) +
+                      g_ble_phy_data.txtx_time_us;
+        }
+
         /* Adjust for delay between EVENT_END and actual TX end time */
         tx_time += g_ble_phy_t_txenddelay[tx_phy_mode];
 
@@ -2228,3 +2245,10 @@ ble_phy_rfclk_disable(void)
     nrf_clock_task_trigger(NRF_CLOCK, NRF_CLOCK_TASK_HFCLKSTOP);
 #endif
 }
+
+void
+ble_phy_tifs_txtx_set(uint16_t usecs, uint8_t anchor)
+{
+    g_ble_phy_data.txtx_time_us = usecs;
+    g_ble_phy_data.txtx_time_anchor = anchor;
+}


[mynewt-nimble] 01/03: nimble/phy: Rework phy encryption API

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 8be739d0c313d4bc1d5996f77ef680d17ab86661
Author: Andrzej Kaczmarek <an...@codecoup.pl>
AuthorDate: Fri Jan 27 13:32:58 2023 +0100

    nimble/phy: Rework phy encryption API
    
    This splits encryption API into smaller functions. We start encryption
    with a common key, they have to set IV (which can be different for each
    subevent) and also counter/dir_bit.
---
 nimble/controller/include/controller/ble_phy.h | 11 +++---
 nimble/controller/src/ble_ll_conn.c            | 46 ++++++++++++--------------
 nimble/drivers/dialog_cmac/src/ble_phy.c       | 28 +++++++++-------
 nimble/drivers/native/src/ble_phy.c            | 20 ++++-------
 nimble/drivers/nrf51/src/ble_phy.c             | 28 ++++++----------
 nimble/drivers/nrf5x/src/ble_phy.c             | 29 ++++++----------
 6 files changed, 70 insertions(+), 92 deletions(-)

diff --git a/nimble/controller/include/controller/ble_phy.h b/nimble/controller/include/controller/ble_phy.h
index 35ef87ed..9b3a57b9 100644
--- a/nimble/controller/include/controller/ble_phy.h
+++ b/nimble/controller/include/controller/ble_phy.h
@@ -160,15 +160,14 @@ uint8_t ble_phy_max_data_pdu_pyld(void);
 uint32_t ble_phy_access_addr_get(void);
 
 /* Enable encryption */
-void ble_phy_encrypt_enable(uint64_t pkt_counter, uint8_t *iv, uint8_t *key,
-                            uint8_t is_central);
-
+void ble_phy_encrypt_enable(const uint8_t *key);
+/* Set encryption IV */
+void ble_phy_encrypt_iv_set(const uint8_t *iv);
+/* Set encryption packet counter and direction bit */
+void ble_phy_encrypt_counter_set(uint64_t counter, uint8_t dir_bit);
 /* Disable encryption */
 void ble_phy_encrypt_disable(void);
 
-/* Set the packet counters and dir used by LE encyption */
-void ble_phy_encrypt_set_pkt_cntr(uint64_t pkt_counter, int dir);
-
 /* Enable phy resolving list */
 void ble_phy_resolv_list_enable(void);
 
diff --git a/nimble/controller/src/ble_ll_conn.c b/nimble/controller/src/ble_ll_conn.c
index 379a59a7..5b88d3ef 100644
--- a/nimble/controller/src/ble_ll_conn.c
+++ b/nimble/controller/src/ble_ll_conn.c
@@ -859,10 +859,10 @@ ble_ll_conn_start_rx_encrypt(void *arg)
 
     connsm = (struct ble_ll_conn_sm *)arg;
     CONN_F_ENCRYPTED(connsm) = 1;
-    ble_phy_encrypt_enable(connsm->enc_data.rx_pkt_cntr,
-                           connsm->enc_data.iv,
-                           connsm->enc_data.enc_block.cipher_text,
-                           !CONN_IS_CENTRAL(connsm));
+    ble_phy_encrypt_enable(connsm->enc_data.enc_block.cipher_text);
+    ble_phy_encrypt_iv_set(connsm->enc_data.iv);
+    ble_phy_encrypt_counter_set(connsm->enc_data.rx_pkt_cntr,
+                                !CONN_IS_CENTRAL(connsm));
 }
 
 #if MYNEWT_VAL(BLE_LL_ROLE_PERIPHERAL)
@@ -905,8 +905,8 @@ ble_ll_conn_continue_rx_encrypt(void *arg)
     struct ble_ll_conn_sm *connsm;
 
     connsm = (struct ble_ll_conn_sm *)arg;
-    ble_phy_encrypt_set_pkt_cntr(connsm->enc_data.rx_pkt_cntr,
-                                 !CONN_IS_CENTRAL(connsm));
+    ble_phy_encrypt_counter_set(connsm->enc_data.rx_pkt_cntr,
+                                !CONN_IS_CENTRAL(connsm));
 }
 #endif
 
@@ -1362,10 +1362,10 @@ conn_tx_pdu:
          */
         CONN_F_ENCRYPTED(connsm) = 1;
         connsm->enc_data.tx_encrypted = 1;
-        ble_phy_encrypt_enable(connsm->enc_data.tx_pkt_cntr,
-                               connsm->enc_data.iv,
-                               connsm->enc_data.enc_block.cipher_text,
-                               CONN_IS_CENTRAL(connsm));
+        ble_phy_encrypt_enable(connsm->enc_data.enc_block.cipher_text);
+        ble_phy_encrypt_iv_set(connsm->enc_data.iv);
+        ble_phy_encrypt_counter_set(connsm->enc_data.tx_pkt_cntr,
+                                    CONN_IS_CENTRAL(connsm));
     } else if (is_ctrl && (opcode == BLE_LL_CTRL_START_ENC_REQ)) {
         /*
          * Only the peripheral sends this and it gets sent unencrypted but
@@ -1398,10 +1398,10 @@ conn_tx_pdu:
         case BLE_LL_CONN_ROLE_PERIPHERAL:
             CONN_F_ENCRYPTED(connsm) = 1;
             connsm->enc_data.tx_encrypted = 1;
-            ble_phy_encrypt_enable(connsm->enc_data.tx_pkt_cntr,
-                                   connsm->enc_data.iv,
-                                   connsm->enc_data.enc_block.cipher_text,
-                                   CONN_IS_CENTRAL(connsm));
+            ble_phy_encrypt_enable(connsm->enc_data.enc_block.cipher_text);
+            ble_phy_encrypt_iv_set(connsm->enc_data.iv);
+            ble_phy_encrypt_counter_set(connsm->enc_data.tx_pkt_cntr,
+                                        CONN_IS_CENTRAL(connsm));
             if (txend_func == NULL) {
                 txend_func = ble_ll_conn_start_rx_unencrypt;
             } else {
@@ -1417,8 +1417,8 @@ conn_tx_pdu:
         /* If encrypted set packet counter */
         if (CONN_F_ENCRYPTED(connsm)) {
             connsm->enc_data.tx_encrypted = 1;
-            ble_phy_encrypt_set_pkt_cntr(connsm->enc_data.tx_pkt_cntr,
-                                         CONN_IS_CENTRAL(connsm));
+            ble_phy_encrypt_counter_set(connsm->enc_data.tx_pkt_cntr,
+                                        CONN_IS_CENTRAL(connsm));
             if (txend_func == NULL) {
                 txend_func = ble_ll_conn_continue_rx_encrypt;
             }
@@ -1526,10 +1526,9 @@ ble_ll_conn_event_start_cb(struct ble_ll_sched_item *sch)
         if (!rc) {
 #if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
             if (CONN_F_ENCRYPTED(connsm)) {
-                ble_phy_encrypt_enable(connsm->enc_data.tx_pkt_cntr,
-                                       connsm->enc_data.iv,
-                                       connsm->enc_data.enc_block.cipher_text,
-                                       1);
+                ble_phy_encrypt_enable(connsm->enc_data.enc_block.cipher_text);
+                ble_phy_encrypt_iv_set(connsm->enc_data.iv);
+                ble_phy_encrypt_counter_set(connsm->enc_data.tx_pkt_cntr, 1);
             } else {
                 ble_phy_encrypt_disable();
             }
@@ -1551,10 +1550,9 @@ ble_ll_conn_event_start_cb(struct ble_ll_sched_item *sch)
     case BLE_LL_CONN_ROLE_PERIPHERAL:
 #if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
         if (CONN_F_ENCRYPTED(connsm)) {
-            ble_phy_encrypt_enable(connsm->enc_data.rx_pkt_cntr,
-                                   connsm->enc_data.iv,
-                                   connsm->enc_data.enc_block.cipher_text,
-                                   1);
+            ble_phy_encrypt_enable(connsm->enc_data.enc_block.cipher_text);
+            ble_phy_encrypt_iv_set(connsm->enc_data.iv);
+            ble_phy_encrypt_counter_set(connsm->enc_data.rx_pkt_cntr, 1);
         } else {
             ble_phy_encrypt_disable();
         }
diff --git a/nimble/drivers/dialog_cmac/src/ble_phy.c b/nimble/drivers/dialog_cmac/src/ble_phy.c
index b7e4818c..a1ba3061 100644
--- a/nimble/drivers/dialog_cmac/src/ble_phy.c
+++ b/nimble/drivers/dialog_cmac/src/ble_phy.c
@@ -1598,19 +1598,12 @@ ble_phy_rx_enc_start(uint8_t len)
 }
 
 void
-ble_phy_encrypt_enable(uint64_t pkt_counter, uint8_t *iv, uint8_t *key,
-                       uint8_t is_master)
+ble_phy_encrypt_enable(const uint8_t *key)
 {
     struct ble_phy_encrypt_obj *enc;
 
     enc = &g_ble_phy_encrypt_data;
     memcpy(enc->key, key, 16);
-    memcpy(&enc->b0[6], iv, 8);
-    put_le32(&enc->b0[1], pkt_counter);
-    enc->b0[5] = is_master ? 0x80 : 0;
-    memcpy(&enc->ai[6], iv, 8);
-    put_le32(&enc->ai[1], pkt_counter);
-    enc->ai[5] = enc->b0[5];
 
     g_ble_phy_data.phy_encrypted = 1;
 
@@ -1631,14 +1624,25 @@ ble_phy_encrypt_enable(uint64_t pkt_counter, uint8_t *iv, uint8_t *key,
 }
 
 void
-ble_phy_encrypt_set_pkt_cntr(uint64_t pkt_counter, int dir)
+ble_phy_encrypt_iv_set(const uint8_t *iv)
+{
+    struct ble_phy_encrypt_obj *enc;
+
+    enc = &g_ble_phy_encrypt_data;
+    memcpy(&enc->b0[6], iv, 8);
+    memcpy(&enc->ai[6], iv, 8);
+}
+
+
+void
+ble_phy_encrypt_counter_set(uint64_t counter, uint8_t dir_bit)
 {
     struct ble_phy_encrypt_obj *enc;
 
     enc = &g_ble_phy_encrypt_data;
-    put_le32(&enc->b0[1], pkt_counter);
-    enc->b0[5] = dir ? 0x80 : 0;
-    put_le32(&enc->ai[1], pkt_counter);
+    put_le32(&enc->b0[1], counter);
+    enc->b0[5] = dir_bit ? 0x80 : 0;
+    put_le32(&enc->ai[1], counter);
     enc->ai[5] = enc->b0[5];
 
     CMAC->CM_CRYPTO_CTRL_REG = CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_SW_REQ_PBUF_CLR_Msk;
diff --git a/nimble/drivers/native/src/ble_phy.c b/nimble/drivers/native/src/ble_phy.c
index 410b9ce6..7635621c 100644
--- a/nimble/drivers/native/src/ble_phy.c
+++ b/nimble/drivers/native/src/ble_phy.c
@@ -342,24 +342,18 @@ ble_phy_restart_rx(void)
 }
 
 #if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
-/**
- * Called to enable encryption at the PHY. Note that this state will persist
- * in the PHY; in other words, if you call this function you have to call
- * disable so that future PHY transmits/receives will not be encrypted.
- *
- * @param pkt_counter
- * @param iv
- * @param key
- * @param is_master
- */
 void
-ble_phy_encrypt_enable(uint64_t pkt_counter, uint8_t *iv, uint8_t *key,
-                       uint8_t is_master)
+ble_phy_encrypt_enable(const uint8_t *key)
+{
+}
+
+void
+ble_phy_encrypt_iv_set(const uint8_t *iv)
 {
 }
 
 void
-ble_phy_encrypt_set_pkt_cntr(uint64_t pkt_counter, int dir)
+ble_phy_encrypt_counter_set(uint64_t counter, uint8_t dir_bit)
 {
 }
 
diff --git a/nimble/drivers/nrf51/src/ble_phy.c b/nimble/drivers/nrf51/src/ble_phy.c
index cfcfb7ec..f35c7781 100644
--- a/nimble/drivers/nrf51/src/ble_phy.c
+++ b/nimble/drivers/nrf51/src/ble_phy.c
@@ -983,24 +983,10 @@ ble_phy_rx(void)
 }
 
 #if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
-/**
- * Called to enable encryption at the PHY. Note that this state will persist
- * in the PHY; in other words, if you call this function you have to call
- * disable so that future PHY transmits/receives will not be encrypted.
- *
- * @param pkt_counter
- * @param iv
- * @param key
- * @param is_master
- */
 void
-ble_phy_encrypt_enable(uint64_t pkt_counter, uint8_t *iv, uint8_t *key,
-                       uint8_t is_master)
+ble_phy_encrypt_enable(const uint8_t *key)
 {
     memcpy(g_nrf_ccm_data.key, key, 16);
-    g_nrf_ccm_data.pkt_counter = pkt_counter;
-    memcpy(g_nrf_ccm_data.iv, iv, 8);
-    g_nrf_ccm_data.dir_bit = is_master;
     g_ble_phy_data.phy_encrypted = 1;
 
     /* Encryption uses LFLEN=5, S1LEN = 3. */
@@ -1014,10 +1000,16 @@ ble_phy_encrypt_enable(uint64_t pkt_counter, uint8_t *iv, uint8_t *key,
 }
 
 void
-ble_phy_encrypt_set_pkt_cntr(uint64_t pkt_counter, int dir)
+ble_phy_encrypt_iv_set(const uint8_t *iv)
+{
+    memcpy(g_nrf_ccm_data.iv, iv, 8);
+}
+
+void
+ble_phy_encrypt_counter_set(uint64_t counter, uint8_t dir_bit)
 {
-    g_nrf_ccm_data.pkt_counter = pkt_counter;
-    g_nrf_ccm_data.dir_bit = dir;
+    g_nrf_ccm_data.pkt_counter = counter;
+    g_nrf_ccm_data.dir_bit = dir_bit;
 }
 
 void
diff --git a/nimble/drivers/nrf5x/src/ble_phy.c b/nimble/drivers/nrf5x/src/ble_phy.c
index 0663eba7..d4fe0a51 100644
--- a/nimble/drivers/nrf5x/src/ble_phy.c
+++ b/nimble/drivers/nrf5x/src/ble_phy.c
@@ -1676,35 +1676,26 @@ ble_phy_rx(void)
 }
 
 #if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
-/**
- * Called to enable encryption at the PHY. Note that this state will persist
- * in the PHY; in other words, if you call this function you have to call
- * disable so that future PHY transmits/receives will not be encrypted.
- *
- * @param pkt_counter
- * @param iv
- * @param key
- * @param is_master
- */
 void
-ble_phy_encrypt_enable(uint64_t pkt_counter, uint8_t *iv, uint8_t *key,
-                       uint8_t is_master)
+ble_phy_encrypt_enable(const uint8_t *key)
 {
     memcpy(g_nrf_ccm_data.key, key, 16);
-    g_nrf_ccm_data.pkt_counter = pkt_counter;
-    memcpy(g_nrf_ccm_data.iv, iv, 8);
-    g_nrf_ccm_data.dir_bit = is_master;
     g_ble_phy_data.phy_encrypted = 1;
-    /* Enable the module (AAR cannot be on while CCM on) */
     NRF_AAR->ENABLE = AAR_ENABLE_ENABLE_Disabled;
     NRF_CCM->ENABLE = CCM_ENABLE_ENABLE_Enabled;
 }
 
 void
-ble_phy_encrypt_set_pkt_cntr(uint64_t pkt_counter, int dir)
+ble_phy_encrypt_iv_set(const uint8_t *iv)
+{
+    memcpy(g_nrf_ccm_data.iv, iv, 8);
+}
+
+void
+ble_phy_encrypt_counter_set(uint64_t counter, uint8_t dir_bit)
 {
-    g_nrf_ccm_data.pkt_counter = pkt_counter;
-    g_nrf_ccm_data.dir_bit = dir;
+    g_nrf_ccm_data.pkt_counter = counter;
+    g_nrf_ccm_data.dir_bit = dir_bit;
 }
 
 void


[mynewt-nimble] 03/03: nimble/phy/nrf53: Clean state on phy disable

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 bc1662c95f7d83dfe2925b327a3bba94d58928d0
Author: Andrzej Kaczmarek <an...@codecoup.pl>
AuthorDate: Fri Jan 27 11:33:07 2023 +0100

    nimble/phy/nrf53: Clean state on phy disable
---
 nimble/drivers/nrf5x/src/ble_phy.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/nimble/drivers/nrf5x/src/ble_phy.c b/nimble/drivers/nrf5x/src/ble_phy.c
index 3cdae5c5..1e5fc5c3 100644
--- a/nimble/drivers/nrf5x/src/ble_phy.c
+++ b/nimble/drivers/nrf5x/src/ble_phy.c
@@ -2126,6 +2126,8 @@ ble_phy_disable(void)
     ble_phy_stop_usec_timer();
     ble_phy_disable_irq_and_ppi();
 
+    g_ble_phy_data.phy_transition_late = 0;
+
 #if PHY_USE_FEM
     phy_fem_disable();
 #endif