mirror of
https://github.com/juanjiant-bit/JIANT.git
synced 2026-12-05 20:34:27 +01:00
V10.35: BLUETOOTH AIR, the radio as a proximity sensor without a phone
MENU > SYSTEM > BLUETOOTH gets a third value, AIR: the radio scans passively instead of advertising, and the RSSI of every good packet heard around feeds PROX (its page, FAR / NEAR, the MS_PROX source). Optimist's scanner (fm1-base 4039a8b, GPL-3.0 OR MPL-2.0) at the minimal central level 3 in the Bluetooth segment (56 KB of 72); the RSSI ring 8 deep and a 1-slot report ring keep the pool at 8256 B. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KMHk6yH4XbP1xxHYKCo9Mh
This commit is contained in:
12 files changed
+3694
-1432
No files matched your search
@@ -0,0 +1,340 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-only OR MPL-2.0 */
|
||||
/* The host's side of the central role (BLE_CENTRAL; docs/BLE-STACK.md §9.2, docs/BLE-DEVICES-DESIGN.md §4): one connection out to a
|
||||
* BLE-MIDI peripheral, from the initiator to "ready" (MIDI both ways):
|
||||
* ble_central_connect initiate (ble_ll_connect) to the peer's AdvA; connected as master -> the GATT client
|
||||
* (ble_gattc.c) starts, and with the peer's bond the link is encrypted at once (Apple's
|
||||
* peripherals want it: QA1831), so the BLE-MIDI characteristic is found and subscribed;
|
||||
* security the peripheral's SMP Security Request, an Insufficient Authentication / Encryption answer,
|
||||
* or a bond the peripheral lost (LL_REJECT with PIN or Key Missing) -> encrypt with the bond,
|
||||
* else pair as initiator (ble_smp_init.c); the GATT request that asked goes again once the
|
||||
* link is encrypted (or the pairing done and our keys sent). Refused again after a Just Works
|
||||
* pairing (an iPhone app's MIDI characteristic): paired again with MITM on the same link
|
||||
* (the passkey; the LL pauses the encryption and restarts it with the new key); a peer that
|
||||
* refuses that pairing before any passkey is shown -> BLE_CF_NEED_MITM (the firmware makes a
|
||||
* new link with BLE_PEER_MITM);
|
||||
* its requests an L2CAP Connection Parameter Update Request is answered and carried out as an
|
||||
* LL_CONNECTION_UPDATE_IND (ble_ll_conn_update);
|
||||
* the end a failure (no BLE-MIDI service, pairing failed, authentication refused, a GATT error or
|
||||
* timeout) leaves the link; ble_central_fail / _code say why until the next attempt.
|
||||
* RPA resolution (ah, Core Vol 3 Part H 2.2.2) for finding LAST behind a private address: ble_rpa_resolve.
|
||||
* The levels (ble_cfg.h): BLE_CENTRAL 1 all of the above; 3 (no BLE_CEN_PAIR) no security at all: whatever asks for it
|
||||
* (the Security Request, Insufficient Authentication / Encryption) leaves the link with BLE_CF_NEED_PAIR
|
||||
* (ble_central_no_pairing). */
|
||||
#include "ble.h"
|
||||
#include "ble_host.h"
|
||||
#include "ble_ll.h"
|
||||
#include "ble_aes.h"
|
||||
#include "ble_util.h"
|
||||
#include "ble_diag.h"
|
||||
|
||||
static struct {
|
||||
uint8_t st, fail, code; /* BLE_CS_*; BLE_CF_* and its code (the last attempt) */
|
||||
#if BLE_CEN_PAIR
|
||||
uint8_t enc_tried, pairing, encrypted; /* the bond's LTK tried; a pairing of ours runs; the link encrypted */
|
||||
uint8_t mitm, auth; /* pair with a passkey (the peer needs it); the link's key is
|
||||
* authenticated */
|
||||
#endif
|
||||
uint8_t leave; /* bcen_fail: leave once the TX queue is empty (or 1 s) */
|
||||
#if BLE_CEN_PAIR
|
||||
uint8_t repair; /* 1: pairing again with MITM on this encrypted link, 2: done */
|
||||
uint8_t prompted; /* a pairing of ours started on the link (a prompt on a phone) */
|
||||
uint8_t gatt_after_keys; /* the GATT request goes again once our keys have gone */
|
||||
uint8_t late_due; /* a Just Works pairing ended on the link: iOS may still send its
|
||||
* Pairing Failed for it (blell-dev3 / dev4: 3 events after our keys) */
|
||||
#endif
|
||||
uint32_t leave_t;
|
||||
struct ble_peer peer;
|
||||
} bcen;
|
||||
|
||||
BLE_API uint8_t ble_central_state(void) { return bcen.st; }
|
||||
BLE_API uint8_t ble_central_fail(void) { return bcen.fail; }
|
||||
BLE_API uint8_t ble_central_code(void) { return bcen.code; }
|
||||
|
||||
BLE_API int ble_central_connect(const struct ble_peer *p)
|
||||
{
|
||||
if (!ble_ll_connect(p->addr, p->addr_rand))
|
||||
return 0;
|
||||
#if BLE_CEN_PAIR
|
||||
bcen.prompted = 0;
|
||||
#endif
|
||||
bcen.peer = *p;
|
||||
bcen.st = BLE_CS_CONNECTING;
|
||||
bcen.fail = BLE_CF_NONE;
|
||||
bcen.code = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
BLE_API void ble_central_cancel(void)
|
||||
{
|
||||
if (ble_ll_initiating()) {
|
||||
ble_ll_connect_cancel();
|
||||
bcen.st = BLE_CS_IDLE;
|
||||
} else if (ble_ll_central())
|
||||
ble_ll_disconnect(BLE_ERR_REMOTE_USER); /* (ble_central_disconnected when it is gone) */
|
||||
}
|
||||
|
||||
/* leave the link for this reason (it is kept until the next attempt), once what is queued has gone (a Pairing Failed
|
||||
* of ours: ble_ll_disconnect drops what was not sent), at the next event */
|
||||
static void bcen_fail(uint8_t why, uint8_t code)
|
||||
{
|
||||
if (bcen.fail == BLE_CF_NONE) {
|
||||
bcen.fail = why;
|
||||
bcen.code = code;
|
||||
}
|
||||
if (!bcen.leave) {
|
||||
bcen.leave = 1;
|
||||
bcen.leave_t = ble_hw_time_us();
|
||||
}
|
||||
}
|
||||
|
||||
BLE_API void ble_central_connected(void)
|
||||
{
|
||||
if (!ble_ll_central())
|
||||
return;
|
||||
bcen.st = BLE_CS_SETUP;
|
||||
#if BLE_CEN_PAIR
|
||||
bcen.enc_tried = bcen.pairing = bcen.encrypted = bcen.leave = bcen.auth = 0;
|
||||
bcen.repair = bcen.prompted = bcen.gatt_after_keys = bcen.late_due = 0;
|
||||
bcen.mitm = (uint8_t)(bcen.peer.sec & BLE_PEER_MITM ? 1u : 0u);
|
||||
#else
|
||||
bcen.leave = 0;
|
||||
#endif
|
||||
ble_gattc_start();
|
||||
#if BLE_CEN_PAIR
|
||||
if (bcen.peer.bonded) { /* a bonded peer: encrypted before it has to ask */
|
||||
bcen.enc_tried = 1;
|
||||
ble_ll_start_enc(bcen.peer.ltk, bcen.peer.rand, bcen.peer.ediv);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
BLE_API void ble_central_disconnected(uint8_t reason)
|
||||
{
|
||||
if (bcen.st == BLE_CS_SETUP || bcen.st == BLE_CS_READY) {
|
||||
if (bcen.fail == BLE_CF_NONE) { /* gone without a reason of ours: lost (it left, or out of range); */
|
||||
bcen.fail = BLE_CF_LOST; /* it left with MIC Failure / Key Missing while our encryption with */
|
||||
bcen.code = reason; /* the bond started: the bond is no good to it (KEY) */
|
||||
#if BLE_CEN_PAIR
|
||||
if (bcen.peer.bonded && bcen.enc_tried && !bcen.encrypted && !bcen.pairing &&
|
||||
(reason == BLE_ERR_MIC_FAILURE || reason == BLE_ERR_PIN_KEY_MISSING))
|
||||
bcen.fail = BLE_CF_KEY;
|
||||
#endif
|
||||
}
|
||||
bcen.st = BLE_CS_IDLE;
|
||||
}
|
||||
ble_gattc_reset();
|
||||
}
|
||||
|
||||
#if BLE_CEN_PAIR
|
||||
static void bcen_pair(void)
|
||||
{
|
||||
bcen.pairing = 1;
|
||||
bcen.prompted = 1;
|
||||
ble_smp_pair(bcen.mitm);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* security is needed (the peripheral asked, or refused a GATT request with this ATT error; 0: a Security Request):
|
||||
* the bond's key first, else a pairing (Just Works unless the peer is known to need a passkey). Encrypted already and
|
||||
* refused with Insufficient Authentication by a key without MITM protection: the peer needs a passkey. It is asked for
|
||||
* on this link (Core Vol 3 Part H 2.4: a pairing on an encrypted link; the STK replaces the key through the LL's
|
||||
* encryption pause, Vol 6 Part B 5.1.3.2), once: no disconnection, no new connection to establish, the phone's user
|
||||
* sees the passkey prompt as with a new link. 1: something started (wait for it), 0: nothing left to try */
|
||||
#if BLE_CEN_PAIR
|
||||
static int bcen_secure(uint8_t code)
|
||||
{
|
||||
if (bcen.pairing)
|
||||
return 1;
|
||||
if (bcen.encrypted) {
|
||||
if (code == 0x05u && !bcen.auth && !bcen.mitm && !bcen.repair) {
|
||||
BLE_DG(ble_dgc.cen_need_mitm++);
|
||||
BLE_DG(ble_dgc.si_repair++);
|
||||
bcen.mitm = 1;
|
||||
bcen.repair = 1;
|
||||
bcen_pair();
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if (bcen.peer.bonded) {
|
||||
if (!bcen.enc_tried) {
|
||||
bcen.enc_tried = 1;
|
||||
ble_ll_start_enc(bcen.peer.ltk, bcen.peer.rand, bcen.peer.ediv);
|
||||
}
|
||||
return 1; /* (on its way; refused: ble_host_enc_failed pairs) */
|
||||
}
|
||||
bcen_pair();
|
||||
return 1;
|
||||
}
|
||||
|
||||
BLE_API int ble_central_pairing(void) { return bcen.pairing; }
|
||||
BLE_API int ble_central_prompted(void) { return bcen.prompted; }
|
||||
BLE_API int ble_central_mitm(void) { return bcen.mitm; }
|
||||
#else
|
||||
/* BLE_CENTRAL 3: the peripheral wants security (0: its Security Request, answered Pairing Not Supported by ble_smp.c;
|
||||
* else the ATT error it refused a GATT request with): this level has none, the link is left (NEEDS PAIRING, held by
|
||||
* the firmware until the user acts) */
|
||||
BLE_API void ble_central_no_pairing(uint8_t code)
|
||||
{
|
||||
BLE_DG(ble_dgc.si_sec_req_rx += code == 0u);
|
||||
bcen_fail(BLE_CF_NEED_PAIR, code);
|
||||
}
|
||||
|
||||
static int bcen_secure(uint8_t code)
|
||||
{
|
||||
ble_central_no_pairing(code);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* (ble_gattc.c) */
|
||||
static void ble_central_gattc_ready(void)
|
||||
{
|
||||
bcen.st = BLE_CS_READY;
|
||||
ble_app_state();
|
||||
}
|
||||
|
||||
static void ble_central_gattc_fail(uint8_t why, uint8_t code)
|
||||
{
|
||||
if (why == BLE_CF_NO_MIDI)
|
||||
BLE_DG(ble_dgc.gc_no_midi++);
|
||||
bcen_fail(why, code);
|
||||
}
|
||||
|
||||
static int ble_central_gattc_auth(uint8_t code)
|
||||
{
|
||||
return bcen_secure(code);
|
||||
}
|
||||
|
||||
#if BLE_CEN_PAIR
|
||||
/* (ble_smp_init.c) a Security Request: its AuthReq's MITM bit asks for a passkey */
|
||||
static void ble_central_sec_req(uint8_t auth_req)
|
||||
{
|
||||
if (auth_req & 0x04u)
|
||||
bcen.mitm = 1;
|
||||
if (!bcen_secure(0))
|
||||
ble_gattc_retry();
|
||||
}
|
||||
|
||||
static void ble_central_encrypted(void) /* encrypted with the bond's LTK */
|
||||
{
|
||||
bcen.encrypted = 1;
|
||||
bcen.auth = (uint8_t)(bcen.peer.sec & BLE_PEER_AUTH ? 1u : 0u);
|
||||
if (!bcen.pairing)
|
||||
ble_gattc_retry();
|
||||
}
|
||||
|
||||
/* the pairing ended: ok, or failed with reason; auth: the keys are authenticated (ok), a passkey was shown (failed) */
|
||||
static void ble_central_paired(int ok, uint8_t reason, uint8_t auth)
|
||||
{
|
||||
bcen.pairing = 0;
|
||||
if (!ok) { /* (Authentication Requirements, either way: AUTH) */
|
||||
if (bcen.repair == 1 && !auth && reason != 0x03u) {
|
||||
BLE_DG(ble_dgc.si_repair_fallback++); /* (refused on this link before any passkey: a new link) */
|
||||
bcen_fail(BLE_CF_NEED_MITM, reason);
|
||||
return;
|
||||
}
|
||||
bcen_fail(reason == 0x03u ? BLE_CF_AUTH : BLE_CF_PAIRING, reason);
|
||||
return;
|
||||
}
|
||||
if (bcen.repair == 1)
|
||||
bcen.repair = 2;
|
||||
bcen.late_due = (uint8_t)!auth;
|
||||
bcen.encrypted = 1; /* (with the STK, the keys exchanged) */
|
||||
bcen.auth = auth;
|
||||
bcen.gatt_after_keys = 1; /* (in a later event than our keys: ble_central_event) */
|
||||
}
|
||||
|
||||
/* (ble_smp_init.c) a Pairing Failed from the peripheral, at: 0 no pairing of ours runs, 1 ours waits for its Pairing
|
||||
* Response, 2 ours is further on -> 1: late (counted, ignored): with none of ours running; or iOS's Unspecified Reason
|
||||
* for the Just Works pairing that ended on this link (it came 3 events after our keys, blell-dev3 / dev4) arriving
|
||||
* after our Pairing Request with MITM went (its Pairing Response is still awaited). 0: it ends the pairing that runs */
|
||||
static int ble_central_late_fail(uint8_t reason, int at)
|
||||
{
|
||||
int late = bcen.late_due && reason == 0x08u;
|
||||
if (at == 2)
|
||||
return 0;
|
||||
if (late)
|
||||
bcen.late_due = 0;
|
||||
return at == 0 || late;
|
||||
}
|
||||
|
||||
/* as master, our LL_ENC_REQ was refused: the peripheral lost the bond (Key Missing) -> pair afresh */
|
||||
BLE_API void ble_host_enc_failed(uint8_t err)
|
||||
{
|
||||
if (!ble_ll_central())
|
||||
return;
|
||||
if (bcen.pairing) { /* (the STK refused: the pairing is over) */
|
||||
bcen.pairing = 0;
|
||||
bcen_fail(BLE_CF_PAIRING, err);
|
||||
return;
|
||||
}
|
||||
bcen.peer.bonded = 0;
|
||||
bcen_pair();
|
||||
}
|
||||
#endif
|
||||
|
||||
BLE_API void ble_central_event(void)
|
||||
{
|
||||
uint32_t now;
|
||||
if (!ble_ll_central())
|
||||
return;
|
||||
now = ble_hw_time_us();
|
||||
if (bcen.leave) {
|
||||
if (ble_ll_tx_room() + 4u >= BLE_LL_TX_RING || now - bcen.leave_t > 1000000u) {
|
||||
bcen.leave = 0;
|
||||
ble_ll_disconnect(BLE_ERR_REMOTE_USER);
|
||||
}
|
||||
return;
|
||||
}
|
||||
ble_gattc_event(now);
|
||||
#if BLE_CEN_PAIR
|
||||
if (bcen.gatt_after_keys && ble_ll_tx_room() + 4u >= BLE_LL_TX_RING) {
|
||||
bcen.gatt_after_keys = 0; /* (our keys handed to the engine: the request again; after the
|
||||
* GATT timeout check, whose `now` is older than this request) */
|
||||
ble_gattc_retry();
|
||||
}
|
||||
if (bcen.repair == 1 && smp_init_rsp_late(now)) { /* no answer to the pairing on this link: a new link */
|
||||
bcen.pairing = 0;
|
||||
BLE_DG(ble_dgc.si_repair_fallback++);
|
||||
bcen_fail(BLE_CF_NEED_MITM, 0xFF);
|
||||
} else if (smp_init_timed_out(now)) {
|
||||
bcen.pairing = 0;
|
||||
bcen_fail(BLE_CF_PAIRING, 0xFF);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/* an L2CAP Connection Parameter Update Request (Core Vol 3 Part A 4.20): 0 accepted (carried out with an
|
||||
* LL_CONNECTION_UPDATE_IND at an instant), 1 rejected */
|
||||
BLE_API uint8_t ble_central_sig(const uint8_t *d, uint16_t n)
|
||||
{
|
||||
uint16_t imin, imax, lat, to;
|
||||
BLE_DG(ble_dgc.m_l2_upd_rx++);
|
||||
if (n < 8u)
|
||||
return 1;
|
||||
imin = ble_rd16(d), imax = ble_rd16(d + 2), lat = ble_rd16(d + 4), to = ble_rd16(d + 6);
|
||||
if (imin < 6u || imin > imax || imax > 3200u || lat > 499u || to < 10u || to > 3200u ||
|
||||
(uint32_t)to * 4u <= (1u + lat) * imax)
|
||||
return 1;
|
||||
ble_ll_conn_update(imin, imax, to); /* (later when a procedure of ours runs: kept) */
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if BLE_CEN_PAIR
|
||||
/* ah(k, r) = e(k, 0^104 || r) mod 2^24 against the hash in an RPA's low 24 bits (top two bits 01) */
|
||||
BLE_API int ble_rpa_resolve(const uint8_t irk[16], const uint8_t addr[6])
|
||||
{
|
||||
uint8_t k[16], x[16], i;
|
||||
if ((addr[5] >> 6) != 1u)
|
||||
return 0;
|
||||
for (i = 0; i < 16u; i++)
|
||||
k[i] = irk[15 - i]; /* (e() takes its key most significant octet first) */
|
||||
ble_zero(x, 13);
|
||||
x[13] = addr[5]; /* prand, most significant octet first */
|
||||
x[14] = addr[4];
|
||||
x[15] = addr[3];
|
||||
ble_aes128(k, x, x);
|
||||
return x[15] == addr[0] && x[14] == addr[1] && x[13] == addr[2];
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,341 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-only OR MPL-2.0 */
|
||||
/* The ATT client of the central role (BLE_CENTRAL; Core Specification Vol 3 Part F / G), finding a peripheral's
|
||||
* BLE-MIDI characteristic and turning its notifications on (MIDI Association, "Specification for MIDI over Bluetooth
|
||||
* Low Energy" 1.0):
|
||||
* 1. Exchange MTU (ours BLE_ATT_MTU_MAX; the smaller one is used both ways, ble_att_set_mtu);
|
||||
* 2. Find By Type Value: the primary service with the BLE-MIDI UUID -> its handle range (a server that does not
|
||||
* support it: Read By Group Type over all primary services instead);
|
||||
* 3. Read By Type 0x2803 in that range -> the MIDI I/O characteristic (its 128-bit UUID) and where it ends (the next
|
||||
* declaration, or the service's end);
|
||||
* 4. Find Information after its value -> its Client Characteristic Configuration descriptor (0x2902);
|
||||
* 5. Write Request CCCD = 0x0001 (notifications) -> ready: notifications on the value are MIDI in (ble_att.c's
|
||||
* decoder), MIDI out goes as Write Without Response (ble_att.c att_midi_out).
|
||||
* One request outstanding, the 30 s transaction timeout. An Error Response asking for security (Insufficient
|
||||
* Authentication 0x05, Insufficient Encryption 0x0F, Insufficient Encryption Key Size 0x0C) hands over to the host
|
||||
* (ble_central.c: encrypt with the bond, else pair) and the same request is sent again once the link is encrypted. */
|
||||
#include "ble.h"
|
||||
#include "ble_host.h"
|
||||
#include "ble_ll.h"
|
||||
#include "ble_util.h"
|
||||
#include "ble_diag.h"
|
||||
|
||||
enum { G_IDLE, G_MTU, G_SVC, G_SVC_GRP, G_CHR, G_DSC, G_CCCD, G_READY, G_FAIL };
|
||||
enum { GE_AUTHEN = 0x05, GE_NOT_SUPP = 0x06, GE_KEY_SIZE = 0x0C, GE_NOT_FOUND = 0x0A, GE_ENC = 0x0F };
|
||||
|
||||
/* the MIDI I/O characteristic 7772E5DB-3868-4112-A1A9-F2669D106BF3, least significant octet first */
|
||||
static const uint8_t GC_UUID_MIDI_IO[16] = {0xF3, 0x6B, 0x10, 0x9D, 0x66, 0xF2, 0xA9, 0xA1,
|
||||
0x12, 0x41, 0x68, 0x38, 0xDB, 0xE5, 0x72, 0x77};
|
||||
|
||||
/* (ble_central.c) */
|
||||
static void ble_central_gattc_ready(void);
|
||||
static void ble_central_gattc_fail(uint8_t why, uint8_t code);
|
||||
static int ble_central_gattc_auth(uint8_t code);
|
||||
|
||||
static struct {
|
||||
uint8_t st, wait_sec, due; /* G_*; a request waits for the link's security; one waits for room */
|
||||
uint8_t req[23], req_n; /* the request outstanding (sent again after security) */
|
||||
uint32_t t_req; /* when it went (the 30 s timeout) */
|
||||
uint16_t svc_s, svc_e, val, end, cccd, next; /* the service, the value, the characteristic's end, its CCCD, the
|
||||
* next handle to look from */
|
||||
} bgc;
|
||||
|
||||
static void gc_send(void)
|
||||
{
|
||||
if (!ble_ll_send(L2CAP_CID_ATT, bgc.req, bgc.req_n)) {
|
||||
bgc.due = 1; /* no room now: the next event */
|
||||
return;
|
||||
}
|
||||
bgc.due = 0;
|
||||
bgc.t_req = ble_hw_time_us();
|
||||
}
|
||||
|
||||
static void gc_req_range(uint8_t op, uint16_t s, uint16_t e, uint16_t type)
|
||||
{
|
||||
bgc.req[0] = op;
|
||||
ble_wr16(bgc.req + 1, s);
|
||||
ble_wr16(bgc.req + 3, e);
|
||||
bgc.req_n = 5;
|
||||
if (type) {
|
||||
ble_wr16(bgc.req + 5, type);
|
||||
bgc.req_n = 7;
|
||||
}
|
||||
gc_send();
|
||||
}
|
||||
|
||||
static void gc_fail(uint8_t why, uint8_t code)
|
||||
{
|
||||
bgc.st = G_FAIL;
|
||||
BLE_DG(ble_dgc.gc_state = G_FAIL);
|
||||
ble_central_gattc_fail(why, code);
|
||||
}
|
||||
|
||||
static void gc_step(uint8_t st)
|
||||
{
|
||||
bgc.st = st;
|
||||
BLE_DG(ble_dgc.gc_state = st);
|
||||
}
|
||||
|
||||
static void gc_find_service(void) /* Find By Type Value: primary service = the BLE-MIDI UUID */
|
||||
{
|
||||
gc_step(G_SVC);
|
||||
bgc.req[0] = 0x06;
|
||||
ble_wr16(bgc.req + 1, 1);
|
||||
ble_wr16(bgc.req + 3, 0xFFFF);
|
||||
ble_wr16(bgc.req + 5, 0x2800);
|
||||
ble_cpy(bgc.req + 7, BLE_UUID_MIDI_SVC, 16);
|
||||
bgc.req_n = 23;
|
||||
gc_send();
|
||||
}
|
||||
|
||||
static void gc_chars(uint16_t from)
|
||||
{
|
||||
gc_step(G_CHR);
|
||||
gc_req_range(0x08, from, bgc.svc_e, 0x2803);
|
||||
}
|
||||
|
||||
static void gc_descs(uint16_t from)
|
||||
{
|
||||
if (from > bgc.end) {
|
||||
gc_fail(BLE_CF_NO_MIDI, 0); /* the MIDI I/O characteristic has no CCCD */
|
||||
return;
|
||||
}
|
||||
gc_step(G_DSC);
|
||||
gc_req_range(0x04, from, bgc.end, 0);
|
||||
}
|
||||
|
||||
static void gc_subscribe(void)
|
||||
{
|
||||
gc_step(G_CCCD);
|
||||
bgc.req[0] = 0x12; /* Write Request */
|
||||
ble_wr16(bgc.req + 1, bgc.cccd);
|
||||
bgc.req[3] = 0x01; /* notifications */
|
||||
bgc.req[4] = 0x00;
|
||||
bgc.req_n = 5;
|
||||
gc_send();
|
||||
}
|
||||
|
||||
static void gc_service(uint16_t s, uint16_t e)
|
||||
{
|
||||
bgc.svc_s = s;
|
||||
bgc.svc_e = e;
|
||||
BLE_DG(ble_dgc.gc_svc_s = s);
|
||||
BLE_DG(ble_dgc.gc_svc_e = e);
|
||||
bgc.val = 0;
|
||||
gc_chars(s);
|
||||
}
|
||||
|
||||
/* Read By Type Response (0x2803 declarations): handle, properties, value handle, UUID */
|
||||
static void gc_chars_rsp(const uint8_t *p, uint16_t n)
|
||||
{
|
||||
uint16_t i, el = p[1], last = 0;
|
||||
if (el < 7u)
|
||||
return;
|
||||
for (i = 2; i + el <= n; i = (uint16_t)(i + el)) {
|
||||
uint16_t decl = ble_rd16(p + i), v = ble_rd16(p + i + 3);
|
||||
last = decl;
|
||||
if (bgc.val && decl > bgc.val) { /* the declaration after ours: our characteristic ends before it */
|
||||
bgc.end = (uint16_t)(decl - 1u);
|
||||
gc_descs((uint16_t)(bgc.val + 1u));
|
||||
return;
|
||||
}
|
||||
if (!bgc.val && el == 21u && ble_eq(p + i + 5, GC_UUID_MIDI_IO, 16) && (p[i + 2] & 0x10u)) {
|
||||
bgc.val = v; /* (notify among its properties) */
|
||||
BLE_DG(ble_dgc.gc_val = v);
|
||||
}
|
||||
}
|
||||
if (last >= bgc.svc_e || !last) { /* the service's end */
|
||||
if (!bgc.val) {
|
||||
gc_fail(BLE_CF_NO_MIDI, 0);
|
||||
return;
|
||||
}
|
||||
bgc.end = bgc.svc_e;
|
||||
gc_descs((uint16_t)(bgc.val + 1u));
|
||||
return;
|
||||
}
|
||||
gc_chars((uint16_t)(last + 1u)); /* more declarations in the service */
|
||||
}
|
||||
|
||||
/* Find Information Response: handle + UUID pairs (format 1: 16-bit, 2: 128-bit) */
|
||||
static void gc_descs_rsp(const uint8_t *p, uint16_t n)
|
||||
{
|
||||
uint16_t i, el = p[1] == 1u ? 4u : 18u, last = 0;
|
||||
for (i = 2; i + el <= n; i = (uint16_t)(i + el)) {
|
||||
last = ble_rd16(p + i);
|
||||
if (el == 4u && ble_rd16(p + i + 2) == 0x2902u) {
|
||||
bgc.cccd = last;
|
||||
BLE_DG(ble_dgc.gc_cccd = last);
|
||||
gc_subscribe();
|
||||
return;
|
||||
}
|
||||
}
|
||||
gc_descs((uint16_t)(last + 1u));
|
||||
}
|
||||
|
||||
/* Read By Group Type Response (primary services; the fallback): handle, end, UUID */
|
||||
static void gc_groups_rsp(const uint8_t *p, uint16_t n)
|
||||
{
|
||||
uint16_t i, el = p[1], last = 0;
|
||||
for (i = 2; i + el <= n; i = (uint16_t)(i + el)) {
|
||||
last = ble_rd16(p + i + 2);
|
||||
if (el == 20u && ble_eq(p + i + 4, BLE_UUID_MIDI_SVC, 16)) {
|
||||
gc_service(ble_rd16(p + i), last);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (!last || last == 0xFFFFu) {
|
||||
gc_fail(BLE_CF_NO_MIDI, 0);
|
||||
return;
|
||||
}
|
||||
gc_req_range(0x10, (uint16_t)(last + 1u), 0xFFFF, 0x2800);
|
||||
}
|
||||
|
||||
static void gc_error(const uint8_t *p)
|
||||
{
|
||||
uint8_t code = p[4];
|
||||
BLE_DG(ble_dgc.gc_errs++);
|
||||
BLE_DG(ble_dgc.gc_last_err_op = p[1]);
|
||||
BLE_DG(ble_dgc.gc_last_err_h = ble_rd16(p + 2));
|
||||
BLE_DG(ble_dgc.gc_last_err = code);
|
||||
if (code == GE_AUTHEN || code == GE_ENC || code == GE_KEY_SIZE) {
|
||||
BLE_DG(ble_dgc.gc_auth_errs++);
|
||||
if (ble_central_gattc_auth(code))
|
||||
bgc.wait_sec = 1; /* sent again once encrypted (ble_gattc_retry) */
|
||||
else
|
||||
gc_fail(BLE_CF_AUTH, code);
|
||||
return;
|
||||
}
|
||||
switch (bgc.st) {
|
||||
case G_SVC:
|
||||
if (code == GE_NOT_SUPP) { /* no Find By Type Value: walk the primary services */
|
||||
gc_step(G_SVC_GRP);
|
||||
gc_req_range(0x10, 1, 0xFFFF, 0x2800);
|
||||
} else
|
||||
gc_fail(code == GE_NOT_FOUND ? BLE_CF_NO_MIDI : BLE_CF_GATT, code);
|
||||
return;
|
||||
case G_SVC_GRP:
|
||||
gc_fail(code == GE_NOT_FOUND ? BLE_CF_NO_MIDI : BLE_CF_GATT, code);
|
||||
return;
|
||||
case G_CHR:
|
||||
if (code == GE_NOT_FOUND && bgc.val) { /* no declaration after ours: it runs to the service's end */
|
||||
bgc.end = bgc.svc_e;
|
||||
gc_descs((uint16_t)(bgc.val + 1u));
|
||||
} else
|
||||
gc_fail(code == GE_NOT_FOUND ? BLE_CF_NO_MIDI : BLE_CF_GATT, code);
|
||||
return;
|
||||
case G_DSC:
|
||||
gc_fail(code == GE_NOT_FOUND ? BLE_CF_NO_MIDI : BLE_CF_GATT, code);
|
||||
return;
|
||||
default:
|
||||
gc_fail(BLE_CF_GATT, code);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
BLE_API void ble_gattc_reset(void)
|
||||
{
|
||||
ble_zero((uint8_t *)&bgc, sizeof bgc);
|
||||
}
|
||||
|
||||
/* a link as central is up: MTU first */
|
||||
BLE_API void ble_gattc_start(void)
|
||||
{
|
||||
ble_gattc_reset();
|
||||
BLE_DG(ble_dgc.gc_starts++);
|
||||
gc_step(G_MTU);
|
||||
bgc.req[0] = 0x02;
|
||||
ble_wr16(bgc.req + 1, BLE_ATT_MTU_MAX);
|
||||
bgc.req_n = 3;
|
||||
gc_send();
|
||||
}
|
||||
|
||||
/* the link is encrypted now: the request that asked for it again */
|
||||
BLE_API void ble_gattc_retry(void)
|
||||
{
|
||||
if (!bgc.wait_sec)
|
||||
return;
|
||||
bgc.wait_sec = 0;
|
||||
BLE_DG(ble_dgc.gc_retries++);
|
||||
gc_send();
|
||||
}
|
||||
|
||||
BLE_API uint16_t ble_gattc_midi(void) { return bgc.st == G_READY ? bgc.val : 0u; }
|
||||
|
||||
/* an ATT PDU from the peripheral's server: 1 taken (a response to us, a notification or an indication), 0 not ours
|
||||
* (a request from its client to our server: ble_att.c) */
|
||||
BLE_API int ble_gattc_rx(const uint8_t *p, uint16_t n)
|
||||
{
|
||||
uint8_t op = p[0];
|
||||
if (op == 0x1Bu || op == 0x1Du) { /* Handle Value Notification / Indication */
|
||||
if (op == 0x1Du) {
|
||||
static const uint8_t conf = 0x1E;
|
||||
ble_ll_send(L2CAP_CID_ATT, &conf, 1);
|
||||
BLE_DG(ble_dgc.gc_ind_rx++);
|
||||
} else
|
||||
BLE_DG(ble_dgc.gc_ntf_rx++);
|
||||
if (n >= 3u && bgc.st == G_READY && ble_rd16(p + 1) == bgc.val)
|
||||
ble_att_midi_packet(p + 3, (uint16_t)(n - 3u));
|
||||
return 1;
|
||||
}
|
||||
if (op != 0x01u && (!(op & 1u) || op > 0x19u))
|
||||
return 0; /* requests and commands: the server's */
|
||||
if (bgc.st == G_IDLE || bgc.st == G_READY || bgc.st == G_FAIL || bgc.wait_sec)
|
||||
return 1; /* (nothing of ours outstanding) */
|
||||
bgc.t_req = 0;
|
||||
if (op == 0x01u) {
|
||||
if (n == 5u && p[1] == bgc.req[0])
|
||||
gc_error(p);
|
||||
return 1;
|
||||
}
|
||||
if (op != (uint8_t)(bgc.req[0] + 1u))
|
||||
return 1; /* (not the answer to ours) */
|
||||
switch (bgc.st) {
|
||||
case G_MTU:
|
||||
if (n == 3u) {
|
||||
uint16_t m = ble_rd16(p + 1);
|
||||
ble_att_set_mtu(m < 23u ? 23u : m);
|
||||
BLE_DG(ble_dgc.gc_mtu = ble_att_mtu());
|
||||
}
|
||||
gc_find_service();
|
||||
return 1;
|
||||
case G_SVC:
|
||||
if (n >= 5u)
|
||||
gc_service(ble_rd16(p + 1), ble_rd16(p + 3));
|
||||
else
|
||||
gc_fail(BLE_CF_GATT, 0);
|
||||
return 1;
|
||||
case G_SVC_GRP:
|
||||
if (n >= 2u)
|
||||
gc_groups_rsp(p, n);
|
||||
return 1;
|
||||
case G_CHR:
|
||||
if (n >= 2u)
|
||||
gc_chars_rsp(p, n);
|
||||
return 1;
|
||||
case G_DSC:
|
||||
if (n >= 2u)
|
||||
gc_descs_rsp(p, n);
|
||||
return 1;
|
||||
case G_CCCD: /* Write Response: subscribed */
|
||||
gc_step(G_READY);
|
||||
BLE_DG(ble_dgc.gc_subscribed++);
|
||||
ble_central_gattc_ready();
|
||||
return 1;
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* each connection event: a request waiting for room; the 30 s timeout */
|
||||
BLE_API void ble_gattc_event(uint32_t now)
|
||||
{
|
||||
if (bgc.st == G_IDLE || bgc.st == G_READY || bgc.st == G_FAIL || bgc.wait_sec)
|
||||
return;
|
||||
if (bgc.due) {
|
||||
gc_send();
|
||||
return;
|
||||
}
|
||||
if (bgc.t_req && now - bgc.t_req > BLE_GATTC_TIMEOUT_US) {
|
||||
BLE_DG(ble_dgc.gc_timeouts++);
|
||||
gc_fail(BLE_CF_GATT, 0xFF);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,474 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-only OR MPL-2.0 */
|
||||
/* The WL82 driver's central role (BLE_CENTRAL; docs/BLE-HW-FACTS.md §21), part of ble_hw_wl82.c (included there: it
|
||||
* shares the control block, drv and the helpers): scanning (state 1, §21.2), initiating (state 3, §21.3) and the
|
||||
* switch to the master of the connection (state 6, §21.3 / §21.4). The master's connection then runs on the
|
||||
* peripheral's code (the RX rule of §8, the TX rule of §8.2, the event service) with the differences of §21.4 there
|
||||
* (drv.master). Clean room: the fact sheet, the Core Specification; no vendor code. */
|
||||
|
||||
/* ------------------------------------------------------------------------------------------- scanning --- */
|
||||
|
||||
/* HW §21.2 in the vendor's order: one link (link 0) in state 1. The engine sends the SCAN_REQ by itself in T_IFS after
|
||||
* an ADV_IND / ADV_SCAN_IND while FORMAT bit8 is clear (§21.1 [I, strong]); both TX buffers hold that SCAN_REQ with
|
||||
* six zero octets where AdvA goes, which the engine must fill in (C1). Software moves the channel 37 -> 38 -> 39 in
|
||||
* each event interrupt (the value written is for the next window, C12) and keeps the backoff. No column read (op 2)
|
||||
* on this path, as on the advertising one. */
|
||||
#define HW_SCAN_WIN2 50u /* WINCNTL2 while scanning (HW §21.2 step 4) */
|
||||
|
||||
static void hw_scan_backoff_reset(void)
|
||||
{
|
||||
drv.bo_upper = 1;
|
||||
drv.bo_count = 1;
|
||||
drv.bo_sent = drv.bo_succ = drv.bo_fail = 0;
|
||||
}
|
||||
|
||||
BLE_API void ble_hw_scan_start(const struct ble_hw_scan *s)
|
||||
{
|
||||
uint32_t iv = s->interval, win = s->window, b, i;
|
||||
hw_link_open();
|
||||
cb_rfprio(17u); /* 1 */
|
||||
fm1_ble_col_wr(HW_LINK, 8, 0); /* 2: advDelay off */
|
||||
if (win + 4u > iv) { /* 3: the vendor's adjustment */
|
||||
if (iv > 4u)
|
||||
win = iv - 4u;
|
||||
else
|
||||
iv = 5u, win = 1u;
|
||||
}
|
||||
cb_window(win * 625u); /* 4 */
|
||||
CB->wincntl2 = HW_SCAN_WIN2;
|
||||
fm1_ble_col_wr(HW_LINK, 1, iv & 0xFFFFu);
|
||||
fm1_ble_col_wr(HW_LINK, 15, iv >> 16); /* bit15 = 0 for scanning (its meaning: C8) */
|
||||
CB->filtercntl &= (uint16_t)~8u; /* 5: filter policy 0 (no whitelist) */
|
||||
drv.scan_active = s->active ? 1u : 0u;
|
||||
CB->format = (uint16_t)(drv.scan_active ? 0u : 0x100u); /* 6: bit8 0 = SCAN_REQ allowed */
|
||||
fm1_ble_col_wr(HW_LINK, 6, 0x2100u | 37u); /* 7: channel 37 first */
|
||||
CB->rxptr[0] = BB_OFF(bb.rx[0].buf + HW_SWHDR); /* 8: two RX buffers (264 octets of room) */
|
||||
CB->rxptr[1] = BB_OFF(bb.rx[1].buf + HW_SWHDR);
|
||||
CB->rxbufcntl[0] &= (uint8_t)~1u;
|
||||
CB->rxbufcntl[1] &= (uint8_t)~1u;
|
||||
hw_rx_wipe(0);
|
||||
hw_rx_wipe(1);
|
||||
for (b = 0; b < 2u; b++) { /* 9: both TX buffers hold the SCAN_REQ */
|
||||
uint8_t *p = bb.tx[b].buf + HW_SWHDR;
|
||||
for (i = 0; i < 6u; i++) {
|
||||
p[i] = s->own[i]; /* ScanA */
|
||||
p[6u + i] = 0; /* AdvA: the engine's (C1) */
|
||||
}
|
||||
CB->txahdr[b] = (uint16_t)(3u | (uint32_t)(s->own_rand & 1u) << 4);
|
||||
CB->txdhdr[b] = (uint16_t)(12u << 8);
|
||||
}
|
||||
CB->txtog = 0;
|
||||
fm1_ble_sync();
|
||||
fm1_ble_col_wr(HW_LINK, 2, 0x1000u); /* 10: state 1 */
|
||||
CB->optcntl &= (uint16_t)~0x200u; /* 11 */
|
||||
CB->optcntl |= 0xC00u;
|
||||
hw_scan_backoff_reset(); /* 12 */
|
||||
drv.scan_ch = 37u;
|
||||
drv.scan_evts = 0;
|
||||
fm1_ble_sync();
|
||||
fm1_ble_link_irqs_on(HW_LINK); /* 13: the only open link: start at once */
|
||||
fm1_ble_col_wr(HW_LINK, 7, 0);
|
||||
fm1_ble_col_wr(HW_LINK, 0, 0);
|
||||
fm1_ble_col_wr(HW_LINK, 14, 0);
|
||||
fm1_ble_col_wr(HW_LINK, 0, 0);
|
||||
fm1_ble_col_wr(HW_LINK, 14, 0x8000u);
|
||||
drv.state = HW_SCAN;
|
||||
drv.gen++;
|
||||
BLE_DG(ble_dgs.starts++);
|
||||
BLE_DG(ble_dgs.ch = 37u);
|
||||
}
|
||||
|
||||
BLE_API void ble_hw_scan_stop(void)
|
||||
{
|
||||
hw_stop(BDS_SCAN, HW_STOP_WAIT_US); /* HW §21.2 Stop: column 14 = 0, interrupts off, idle */
|
||||
BLE_DG(ble_dgs.stops++);
|
||||
drv.state = HW_OFF;
|
||||
drv.gen++;
|
||||
}
|
||||
|
||||
/* Core Vol 6 Part B 4.4.3.2 as the vendor keeps it (HW §21.2): each ADV_IND / ADV_SCAN_IND counts the backoff down; at
|
||||
* 0 a SCAN_REQ went out (the engine answered that packet): if the one before got no SCAN_RSP, a failure (two in a row:
|
||||
* upperLimit doubles, at most 256); the count is drawn again in 1..upperLimit. A SCAN_RSP is a success (two in a row:
|
||||
* upperLimit halves). FORMAT bit8 = 0 only while the count is 1: the engine's SCAN_REQ armed for the next one. */
|
||||
static void hw_scan_backoff(uint8_t type)
|
||||
{
|
||||
uint8_t r;
|
||||
if (!drv.scan_active)
|
||||
return;
|
||||
if (type == 0x4u) {
|
||||
if (drv.bo_sent) {
|
||||
drv.bo_sent = 0;
|
||||
drv.bo_fail = 0;
|
||||
if (++drv.bo_succ >= 2u) {
|
||||
drv.bo_succ = 0;
|
||||
drv.bo_upper = (uint16_t)(drv.bo_upper > 1u ? drv.bo_upper / 2u : 1u);
|
||||
}
|
||||
BLE_DG(ble_dgs.rsp_ok++);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (type != 0x0u && type != 0x6u)
|
||||
return;
|
||||
if (--drv.bo_count == 0) {
|
||||
if (drv.bo_sent) {
|
||||
drv.bo_succ = 0;
|
||||
BLE_DG(ble_dgs.req_fail++);
|
||||
if (++drv.bo_fail >= 2u) {
|
||||
drv.bo_fail = 0;
|
||||
drv.bo_upper = (uint16_t)(drv.bo_upper < 256u ? drv.bo_upper * 2u : 256u);
|
||||
}
|
||||
}
|
||||
drv.bo_sent = 1;
|
||||
BLE_DG(ble_dgs.req_armed++);
|
||||
ble_hw_rand(&r, 1);
|
||||
drv.bo_count = (uint8_t)(1u + r % drv.bo_upper); /* (upper 256: 1..256 as 1..255 + 0 -> a uint8 of 1..255) */
|
||||
if (!drv.bo_count)
|
||||
drv.bo_count = 1;
|
||||
#if BLE_DIAG
|
||||
if (drv.bo_upper > ble_dgs.upper_max)
|
||||
ble_dgs.upper_max = drv.bo_upper;
|
||||
#endif
|
||||
}
|
||||
CB->format = (uint16_t)((CB->format & ~0x100u) | (drv.bo_count == 1u ? 0u : 0x100u));
|
||||
}
|
||||
|
||||
/* one report out of RX buffer b: header from RXAHDR / RXDHDR, status RXSTAT, RSSI2, the channel LASTCHMAP (HW §21.2) */
|
||||
static void hw_scan_take(uint32_t b)
|
||||
{
|
||||
uint16_t ah = CB->rxahdr[b], dh = CB->rxdhdr[b], st = CB->rxstat[b], rssi = CB->rssi[2];
|
||||
uint8_t n = (uint8_t)(dh >> 8), type = (uint8_t)(ah & 0x0Fu), ch = (uint8_t)CB->lastchmap, *pdu;
|
||||
CB->rxahdr[b] = CB->rxdhdr[b] = CB->rxstat[b] = 0; /* (a buffer found again by RXTOG alone is a new packet) */
|
||||
BLE_DG(ble_dgs.last_ahdr = ah);
|
||||
BLE_DG(ble_dgs.last_dhdr = dh);
|
||||
BLE_DG(ble_dgs.last_rssi = rssi);
|
||||
BLE_DG(ble_dgs.last_ch = ch);
|
||||
if ((st & 0xFu) != 1u) {
|
||||
BLE_DG(ble_dgs.rx_bad_stat++);
|
||||
return;
|
||||
}
|
||||
if (n < 6u || n > 37u) {
|
||||
BLE_DG(ble_dgs.rx_bad_len++);
|
||||
return;
|
||||
}
|
||||
#ifdef BLE_HW_RX_RSSI
|
||||
BLE_HW_RX_RSSI(CB->rssi, b); /* (JIANT V10.35 AIR: every good packet heard, PROX) */
|
||||
#endif
|
||||
if (type == 0x3u || type == 0x5u)
|
||||
return; /* SCAN_REQ / CONNECT_IND: dropped in state 1 */
|
||||
pdu = &bb.rx[b].buf[HW_SWHDR - 2u];
|
||||
pdu[0] = (uint8_t)ah; /* Core layout: type, ChSel, TxAdd, RxAdd */
|
||||
pdu[1] = n;
|
||||
hw_scan_backoff(type);
|
||||
ble_ll_hw_adv_report(pdu, (uint8_t)(n + 2u), rssi, ch);
|
||||
}
|
||||
|
||||
/* the RX interrupt while scanning: the connection path of HW §8 (§21.2): RXTOG read until stable, the RXTOG buffer
|
||||
* then the other, each if RXBUFnCNTL bit0 = 1, cleared after it. Whether the engine sets bit0 while scanning is C2;
|
||||
* when neither has it, the buffer RXTOG moved past is taken if its header was written since it was last taken (the
|
||||
* advertising rule the FM-1 showed, §8.1). Counted: blell scan rxf_cntl / rxf_tog / rxf_none. */
|
||||
static void hw_rx_scan(void)
|
||||
{
|
||||
uint32_t t1, t2, k, took = 0, tries = 0;
|
||||
uint8_t c0, c1;
|
||||
do {
|
||||
t1 = CB->rxtog & 1u;
|
||||
c0 = CB->rxbufcntl[0];
|
||||
c1 = CB->rxbufcntl[1];
|
||||
t2 = CB->rxtog & 1u;
|
||||
} while (t1 != t2 && ++tries < 4u);
|
||||
BLE_DG(ble_dgs.last_cntl = (uint8_t)((c0 & 1u) | (c1 & 1u) << 1));
|
||||
BLE_DG(ble_dgs.last_tog = (uint8_t)t2);
|
||||
for (k = 0; k < 2u; k++) {
|
||||
uint32_t b = t2 ^ k;
|
||||
if ((b ? c1 : c0) & 1u) {
|
||||
BLE_DG(ble_dgs.rxf_cntl++);
|
||||
hw_scan_take(b);
|
||||
CB->rxbufcntl[b] &= (uint8_t)~1u;
|
||||
took++;
|
||||
}
|
||||
}
|
||||
if (!took && (CB->rxdhdr[t2 ^ 1u] || CB->rxstat[t2 ^ 1u])) {
|
||||
BLE_DG(ble_dgs.rxf_tog++);
|
||||
hw_scan_take(t2 ^ 1u);
|
||||
took++;
|
||||
}
|
||||
if (!took)
|
||||
BLE_DG(ble_dgs.rxf_none++);
|
||||
}
|
||||
|
||||
/* the event interrupt while scanning: the next window's channel (HW §21.2 "Each event"), RFPRIO 17, 26 every 6th */
|
||||
static void hw_scan_event(void)
|
||||
{
|
||||
fm1_ble_col_wr(HW_LINK, 6, 0x2100u | drv.scan_ch);
|
||||
drv.scan_ch = (uint8_t)(drv.scan_ch >= 39u ? 37u : drv.scan_ch + 1u);
|
||||
cb_rfprio(++drv.scan_evts % 6u ? 17u : 26u);
|
||||
BLE_DG(ble_dgs.events++);
|
||||
BLE_DG(ble_dgs.ch = drv.scan_ch);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------------------------------- initiating --- */
|
||||
|
||||
/* HW §21.3 in the vendor's order: link 0 in state 3, our CONNECT_IND complete in both TX buffers before the link
|
||||
* starts; the one target in WHITELIST0 / TARGETADR with FILTERCNTL bit0 (entry valid), bit4 (CONNECT_IND only to it)
|
||||
* and bit8 (its type); the engine sends the CONNECT_IND T_IFS after the target's ADV_IND by itself (C3) and leaves
|
||||
* initiating (column 9 = 1). The windows and channels as scanning (the event interrupt moves the channel). No column
|
||||
* read (op 2) on this path. */
|
||||
static void hw_adr(volatile uint16_t *r, const uint8_t a[6]) /* an address into three little-endian halves */
|
||||
{
|
||||
r[0] = (uint16_t)(a[0] | a[1] << 8);
|
||||
r[1] = (uint16_t)(a[2] | a[3] << 8);
|
||||
r[2] = (uint16_t)(a[4] | a[5] << 8);
|
||||
}
|
||||
|
||||
BLE_API void ble_hw_init_start(const struct ble_hw_init *in)
|
||||
{
|
||||
const uint8_t *p = in->cind;
|
||||
uint32_t iv = in->interval, win = in->window, b, i;
|
||||
hw_cpy(drv.init_own, p + 2, 6);
|
||||
hw_cpy(drv.init_peer, p + 8, 6);
|
||||
drv.init_own_rand = (uint8_t)(p[0] >> 6 & 1u);
|
||||
drv.init_peer_rand = (uint8_t)(p[0] >> 7 & 1u);
|
||||
drv.ic = in->conn;
|
||||
hw_link_open();
|
||||
cb_rfprio(26u); /* 1 */
|
||||
fm1_ble_col_wr(HW_LINK, 8, 0); /* 2: as scanning (§21.2 steps 3-4), column 6 = 37 */
|
||||
if (win + 4u > iv) {
|
||||
if (iv > 4u)
|
||||
win = iv - 4u;
|
||||
else
|
||||
iv = 5u, win = 1u;
|
||||
}
|
||||
cb_window(win * 625u);
|
||||
CB->wincntl2 = HW_SCAN_WIN2;
|
||||
fm1_ble_col_wr(HW_LINK, 1, iv & 0xFFFFu);
|
||||
fm1_ble_col_wr(HW_LINK, 15, iv >> 16);
|
||||
fm1_ble_col_wr(HW_LINK, 6, 0x2100u | 37u);
|
||||
hw_adr(CB->localadr, drv.init_own); /* 3: our address, its match on */
|
||||
CB->format = 0x0008u;
|
||||
CB->optcntl &= (uint16_t)~0x10u;
|
||||
hw_adr(CB->whitelist, drv.init_peer); /* 4: the target */
|
||||
CB->filtercntl = (uint16_t)((CB->filtercntl & 0x7Eu) | 1u | (uint32_t)drv.init_peer_rand << 8);
|
||||
CB->filtercntl |= 0x10u;
|
||||
CB->optcntl &= (uint16_t)~0x08u;
|
||||
hw_adr(CB->targetadr, drv.init_peer);
|
||||
CB->rxptr[0] = BB_OFF(bb.rx[0].buf + HW_SWHDR); /* 5: two RX buffers */
|
||||
CB->rxptr[1] = BB_OFF(bb.rx[1].buf + HW_SWHDR);
|
||||
CB->rxbufcntl[0] &= (uint8_t)~1u;
|
||||
CB->rxbufcntl[1] &= (uint8_t)~1u;
|
||||
hw_rx_wipe(0);
|
||||
hw_rx_wipe(1);
|
||||
for (b = 0; b < 2u; b++) { /* 6: both TX buffers the CONNECT_IND */
|
||||
uint8_t *q = bb.tx[b].buf + HW_SWHDR;
|
||||
for (i = 0; i < 34u; i++)
|
||||
q[i] = p[2 + i];
|
||||
CB->txahdr[b] = (uint16_t)(5u | (uint32_t)drv.init_own_rand << 4 | (uint32_t)drv.init_peer_rand << 5);
|
||||
CB->txdhdr[b] = (uint16_t)(34u << 8);
|
||||
}
|
||||
CB->txtog = 0;
|
||||
fm1_ble_sync();
|
||||
fm1_ble_col_wr(HW_LINK, 2, 0x3000u); /* 7: state 3 */
|
||||
drv.scan_ch = 37u; /* (the first event writes 37 again, as scanning) */
|
||||
drv.scan_evts = 0;
|
||||
drv.init_hit = 0;
|
||||
drv.master = 0;
|
||||
fm1_ble_sync();
|
||||
fm1_ble_link_irqs_on(HW_LINK); /* 8: the only open link: start at once */
|
||||
fm1_ble_col_wr(HW_LINK, 7, 0);
|
||||
fm1_ble_col_wr(HW_LINK, 0, 0);
|
||||
fm1_ble_col_wr(HW_LINK, 14, 0);
|
||||
fm1_ble_col_wr(HW_LINK, 0, 0);
|
||||
fm1_ble_col_wr(HW_LINK, 14, 0x8000u);
|
||||
fm1_ble_col_wr(HW_LINK, 9, 1); /* "init end": the engine leaves state 3 after it */
|
||||
drv.state = HW_INIT;
|
||||
drv.gen++;
|
||||
drv.init_t0 = fm1_ticks(); /* (after the start: a packet heard ends later than this) */
|
||||
BLE_DG(ble_dgc.init_last_ch = 37u);
|
||||
}
|
||||
|
||||
BLE_API void ble_hw_init_stop(void)
|
||||
{
|
||||
hw_stop(BDS_INIT, HW_STOP_WAIT_US); /* as any link: column 14 = 0, interrupts off, idle */
|
||||
drv.state = HW_OFF;
|
||||
drv.gen++;
|
||||
}
|
||||
|
||||
/* one state-3 report in buffer b: the target's ADV_IND (AdvA, TxAdd) or an ADV_DIRECT_IND from it to us (TargetA,
|
||||
* RxAdd) marks the hit; the engine has sent our CONNECT_IND after it (§21.3 "Reception"); anything else is dropped.
|
||||
* A report that ends sooner after ble_hw_init_start than its own airtime ((1 + 4 + 2 + n + 3) x 8 us at 1 Mb/s) was
|
||||
* in the air before the engine listened for the target: the scan's packet that was being received when the pick
|
||||
* stopped it. No CONNECT_IND follows it, so it is no hit: taken as one, the event IRQ at the window's end switched to
|
||||
* the master with nothing sent (state 6 with no link, never left; the "mac" pick of tests/ble_emu_central_test.py
|
||||
* when the pick landed during the Mac's ADV_IND: c4_rx_to_evt 37.9 ms, a whole window, where a real hit is 0.4 ms) */
|
||||
static int hw_init_stale(uint32_t n)
|
||||
{
|
||||
return (fm1_ticks() - drv.init_t0) / FM1_TICKS_PER_US < (10u + n) * 8u;
|
||||
}
|
||||
static void hw_init_take(uint32_t b)
|
||||
{
|
||||
uint16_t ah = CB->rxahdr[b], dh = CB->rxdhdr[b], st = CB->rxstat[b];
|
||||
const uint8_t *p = bb.rx[b].buf + HW_SWHDR;
|
||||
uint8_t n = (uint8_t)(dh >> 8), type = (uint8_t)(ah & 0x0Fu);
|
||||
CB->rxahdr[b] = CB->rxdhdr[b] = CB->rxstat[b] = 0;
|
||||
BLE_DG(ble_dgc.init_last_ahdr = ah);
|
||||
BLE_DG(ble_dgc.init_last_dhdr = dh);
|
||||
if ((st & 0xFu) != 1u || n < 6u || n > 37u) {
|
||||
BLE_DG(ble_dgc.init_rx_bad++);
|
||||
return;
|
||||
}
|
||||
if (hw_init_stale(n)) {
|
||||
BLE_DG(ble_dgc.init_rx_stale++);
|
||||
return;
|
||||
}
|
||||
if ((type == 0x0u || (type == 0x1u && n == 12u && ble_eq(p + 6, drv.init_own, 6) &&
|
||||
(uint8_t)(ah >> 7 & 1u) == drv.init_own_rand)) &&
|
||||
ble_eq(p, drv.init_peer, 6) && (uint8_t)(ah >> 6 & 1u) == drv.init_peer_rand) {
|
||||
if (!drv.init_hit) {
|
||||
drv.init_hit = 1;
|
||||
drv.init_t_hit = fm1_ticks();
|
||||
BLE_DG(ble_dgc.init_chsel = (uint8_t)(ah >> 5 & 1u));
|
||||
ble_diag_ev(BDE_INIT_HIT, ah);
|
||||
}
|
||||
BLE_DG(ble_dgc.init_rx_target++);
|
||||
} else
|
||||
BLE_DG(ble_dgc.init_rx_other++);
|
||||
}
|
||||
|
||||
/* the RX interrupt in state 3: the vendor reads the buffer other than RXTOG's without RXBUFnCNTL (the advertising
|
||||
* path, §21.3); each buffer is taken by bit0 if the engine sets it, else by RXTOG having moved past a written header */
|
||||
static void hw_rx_init(void)
|
||||
{
|
||||
uint32_t t = CB->rxtog & 1u, k, took = 0;
|
||||
BLE_DG(ble_dgc.init_rx++);
|
||||
for (k = 0; k < 2u; k++)
|
||||
if (CB->rxbufcntl[k] & 1u) {
|
||||
BLE_DG(ble_dgc.init_rxf_cntl++);
|
||||
hw_init_take(k);
|
||||
CB->rxbufcntl[k] &= (uint8_t)~1u;
|
||||
took++;
|
||||
}
|
||||
if (!took && (CB->rxdhdr[t ^ 1u] || CB->rxstat[t ^ 1u])) {
|
||||
BLE_DG(ble_dgc.init_rxf_tog++);
|
||||
hw_init_take(t ^ 1u);
|
||||
}
|
||||
}
|
||||
|
||||
/* The first anchor (HW §21.3 "First anchor", C5): the anchor counter runs 2 x WinOffset + 4 slots from its start, which
|
||||
* puts the first master packet WinOffset x 1.25 ms + 2.5 ms after this set-up. The peripheral listens for it only in the
|
||||
* transmit window, WinSize (2) x 1.25 ms long, 1.25 ms + WinOffset x 1.25 ms after the CONNECT_IND's end; the CONNECT_IND
|
||||
* ends about 0.5 ms after the target's ADV_IND that the hit is (T_IFS + its 352 us), so the packet lands
|
||||
* (set-up - hit) + 0.75 ms into the 2.5 ms window. On the FM-1 the event interrupt that runs this set-up came 0.2 and
|
||||
* 0.5 ms after the hit in the connections that were made, 1.9 and 4.8 ms in the two that failed with 0x3E (blell-dev3,
|
||||
* dev4: c4_rx_to_evt_*): the packet after the window, never heard. So the counter is shortened by the slots the set-up
|
||||
* came late (whole slots past HW_ANCHOR_AIM_US), which keeps the packet 0.6..1.4 ms into the window; it never goes
|
||||
* under HW_ANCHOR_MIN_SLOTS (later than that the window is gone: the link fails to be established, 0x3E, and the host
|
||||
* tries again). Counted: m_anchor_adj (slots, last / max), m_anchor_late. */
|
||||
#define HW_ANCHOR_AIM_US 500u
|
||||
#define HW_ANCHOR_MIN_SLOTS 3u
|
||||
|
||||
static uint32_t hw_first_anchor(const struct ble_hw_conn *c) /* -> the anchor counter's column 0 value */
|
||||
{
|
||||
uint32_t n = 2u * c->win_offset + 4u, late = (fm1_ticks() - drv.init_t_hit) / FM1_TICKS_PER_US, adj = 0;
|
||||
if (late > 625u)
|
||||
adj = (late - HW_ANCHOR_AIM_US + 624u) / 625u;
|
||||
if (adj + HW_ANCHOR_MIN_SLOTS > n) {
|
||||
adj = n - HW_ANCHOR_MIN_SLOTS;
|
||||
BLE_DG(ble_dgc.m_anchor_late++);
|
||||
}
|
||||
#if BLE_DIAG
|
||||
ble_dgc.m_setup_us = late;
|
||||
ble_dgc.m_anchor_adj = (uint8_t)adj;
|
||||
if (adj > ble_dgc.m_anchor_adj_max)
|
||||
ble_dgc.m_anchor_adj_max = (uint8_t)adj;
|
||||
#endif
|
||||
return n - adj - 1u; /* (the counter fires one slot after the value) */
|
||||
}
|
||||
|
||||
/* the master's set-up (§21.3 "Switch to master", the vendor's order), in the event interrupt after the hit */
|
||||
static void hw_master_setup(void)
|
||||
{
|
||||
const struct ble_hw_conn *c = &drv.ic;
|
||||
uint32_t a, k;
|
||||
cb_rfprio(28u); /* 1: as the slave's (§7) */
|
||||
CB->anchor = 0x8000u;
|
||||
CB->txtog &= (uint16_t)~2u;
|
||||
CB->rxtog = 0;
|
||||
CB->bdaddr[0] = (uint16_t)c->aa;
|
||||
CB->bdaddr[1] = (uint16_t)(c->aa >> 16);
|
||||
CB->crcword[0] = (uint16_t)c->crc_init;
|
||||
CB->crcword[1] = (uint16_t)(c->crc_init >> 16 & 0xFFu);
|
||||
CB->optcntl |= 4u;
|
||||
CB->optcntl &= (uint16_t)~0x200u;
|
||||
CB->optcntl |= 0xC00u;
|
||||
CB->optcntl |= 0x1000u;
|
||||
CB->intframe = (uint16_t)((CB->intframe & ~0x30u) | 0x10u);
|
||||
CB->txbufcntl[0] |= 1u; /* both TX buffers empty (the only 1-writes, §8.2) */
|
||||
CB->txbufcntl[1] |= 1u;
|
||||
CB->anchor = (uint16_t)((CB->anchor & 0x8000u) | 4u);
|
||||
fm1_ble_col_wr(HW_LINK, 5, 0);
|
||||
fm1_ble_col_wr(HW_LINK, 4, 0); /* 2: master only */
|
||||
fm1_ble_col_wr(HW_LINK, 7, 0);
|
||||
fm1_ble_col_wr(HW_LINK, 0, 0);
|
||||
fm1_ble_col_wr(HW_LINK, 14, 0);
|
||||
a = hw_first_anchor(c); /* 2 x WinOffset + 4 slots, less the set-up's lateness */
|
||||
for (k = 0; k < 2u; k++) { /* (written twice with the same value on 1M) */
|
||||
fm1_ble_col_wr(HW_LINK, 0, a & 0xFFFFu);
|
||||
fm1_ble_col_wr(HW_LINK, 14, 0x8000u | a >> 16);
|
||||
}
|
||||
fm1_ble_col_wr(HW_LINK, 2, 0x6000u); /* state 6, latency 0 */
|
||||
fm1_ble_col_wr(HW_LINK, 8, 0); /* 3: the shared tail */
|
||||
fm1_ble_col_wr(HW_LINK, 3, 0);
|
||||
CB->evtcount = 0;
|
||||
cb_window((uint32_t)c->win_size * 1250u + 1250u);
|
||||
CB->wincntl2 = HW_WIN_NORMAL;
|
||||
fm1_ble_col_wr(HW_LINK, 1, 2u * c->interval);
|
||||
fm1_ble_col_wr(HW_LINK, 15, 0x8000u | (2u * c->interval) >> 16);
|
||||
cb_channels(c->chm);
|
||||
fm1_ble_col_wr(HW_LINK, 6, 0x8000u | (uint32_t)c->hop << 8 | c->hop);
|
||||
CB->txahdr[0] = CB->txahdr[1] = 0;
|
||||
CB->txdhdr[CB->txtog & 1u] = 1u; /* the TXTOG buffer: LLID 1, bit2 0; the other bit2 1 */
|
||||
CB->txdhdr[(CB->txtog & 1u) ^ 1u] = 5u;
|
||||
fm1_ble_sync();
|
||||
drv.state = HW_CONN;
|
||||
drv.master = 1;
|
||||
drv.gen++;
|
||||
drv.rx_next = 0;
|
||||
drv.rx_sn = 0;
|
||||
drv.rx_seen = drv.rx_any = 0;
|
||||
drv.tx_rec[0] = drv.tx_rec[1] = 0; /* no PDU recorded in either buffer */
|
||||
drv.tx_md[0] = drv.tx_md[1] = 0;
|
||||
drv.upd = 0;
|
||||
drv.win_wide = 1; /* the first window, until the first packet */
|
||||
drv.wide_from = 0;
|
||||
drv.interval = c->interval;
|
||||
drv.sca = c->sca;
|
||||
drv.last_evt = 0xFFFFu;
|
||||
ble_hw_stat.connects++;
|
||||
hwd.conn_t0 = fm1_ticks();
|
||||
hwd.first_rx = hwd.c3_seen = 0;
|
||||
#if BLE_DIAG
|
||||
{
|
||||
uint32_t us = (hwd.conn_t0 - drv.init_t_hit) / FM1_TICKS_PER_US;
|
||||
ble_dgc.c4_rx_to_evt_us = us;
|
||||
if (us > ble_dgc.c4_rx_to_evt_max)
|
||||
ble_dgc.c4_rx_to_evt_max = us;
|
||||
ble_dgc.master_starts++;
|
||||
ble_dgc.m_first_rx_evt = ble_dgc.m_first_evt = 0xFFFFu;
|
||||
ble_dgc.m_first_rx_us = 0;
|
||||
}
|
||||
#endif
|
||||
ble_ll_hw_master_start();
|
||||
}
|
||||
|
||||
/* the event interrupt in state 3: after the hit, the master; else the next window's channel, RFPRIO 26 (30 every 6th:
|
||||
* §21.6) */
|
||||
static void hw_init_event(void)
|
||||
{
|
||||
BLE_DG(ble_dgc.init_events++);
|
||||
if (drv.init_hit) {
|
||||
hw_master_setup();
|
||||
return;
|
||||
}
|
||||
fm1_ble_col_wr(HW_LINK, 6, 0x2100u | drv.scan_ch);
|
||||
BLE_DG(ble_dgc.init_last_ch = drv.scan_ch);
|
||||
drv.scan_ch = (uint8_t)(drv.scan_ch >= 39u ? 37u : drv.scan_ch + 1u);
|
||||
cb_rfprio(++drv.scan_evts % 6u ? 26u : 30u);
|
||||
}
|
||||
@@ -0,0 +1,536 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-only OR MPL-2.0 */
|
||||
/* The link layer's central role (BLE_CENTRAL; Core Specification Vol 6 Part B, docs/BLE-STACK.md §9.1, HW §21),
|
||||
* part of ble_ll.c (included there: it shares bll and the TX queues):
|
||||
* - scanning for the DEVICES list: the raw reports' ring between the RX interrupt and the main loop (4.4.3);
|
||||
* - initiating: our CONNECT_IND built before the engine starts (2.3.3.1; the access address by 2.1.2, a random
|
||||
* CRCInit, WinSize 2, WinOffset in [Interval / 2, Interval - 1], Hop 5..16, all 37 channels), the engine sends it
|
||||
* to the one target (HW §21.3), the driver turns the link into the master (ble_ll_hw_master_start);
|
||||
* - the master's side of the control procedures (5.1): we start the version exchange, the feature exchange, the
|
||||
* encryption (LL_ENC_REQ with a bond's or a pairing's key) and the data length; we answer the peripheral's
|
||||
* feature exchange, its connection parameters request (with an LL_CONNECTION_UPDATE_IND at an instant 8..11
|
||||
* events ahead, HW §21.4), its PHY request (no change: 1M only); connection and channel map updates we start
|
||||
* (ble_ll_conn_update, ble_ll_chmap_update). The rest (TX / RX, acknowledgement, supervision, termination) is
|
||||
* the peripheral's code: the engine's TX and RX rules are the same in states 6 and 7 (HW §21.4). */
|
||||
|
||||
/* (defined further down ble_ll.c) */
|
||||
static void ll_features(uint8_t op, uint8_t peer);
|
||||
static void ll_lengths(const uint8_t *d);
|
||||
static void ll_our_lengths(uint8_t *d);
|
||||
static int ll_version_send(void);
|
||||
static void ll_rx_ctrl(const uint8_t *p, uint8_t n);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------- scanning --- */
|
||||
|
||||
#define LL_REP_MAX (2u + 37u)
|
||||
static struct {
|
||||
uint8_t pdu[BLE_SCAN_RING][LL_REP_MAX], len[BLE_SCAN_RING];
|
||||
uint16_t rssi[BLE_SCAN_RING];
|
||||
volatile uint32_t w, r; /* free-running: w the RX interrupt's, r the main loop's */
|
||||
} bscan;
|
||||
|
||||
static void ll_scan_start(void)
|
||||
{
|
||||
struct ble_hw_scan s;
|
||||
s.interval = BLE_SCAN_INTERVAL;
|
||||
s.window = BLE_SCAN_WINDOW;
|
||||
s.active = BLE_SCAN_ACTIVE;
|
||||
s.own = bll.addr;
|
||||
s.own_rand = bll.addr_rand;
|
||||
bll.state = LL_SCAN;
|
||||
ble_hw_scan_start(&s);
|
||||
}
|
||||
|
||||
BLE_API int ble_ll_scanning(void) { return bll.state == LL_SCAN; }
|
||||
|
||||
/* RX interrupt: only ADV_IND and SCAN_RSP can make a list entry (ble_scan.c); the rest is dropped here */
|
||||
BLE_API void ble_ll_hw_adv_report(const uint8_t *pdu, uint8_t len, uint16_t rssi, uint8_t ch)
|
||||
{
|
||||
uint32_t i, t = pdu[0] & 0x0Fu;
|
||||
(void)ch;
|
||||
if (bll.state != LL_SCAN || len < 8u || len > LL_REP_MAX)
|
||||
return;
|
||||
if (t != 0x0u && t != 0x4u) {
|
||||
BLE_DG(ble_dgs.rep_other++);
|
||||
return;
|
||||
}
|
||||
if (bscan.w - bscan.r >= BLE_SCAN_RING) {
|
||||
BLE_DG(ble_dgs.ring_full++);
|
||||
return;
|
||||
}
|
||||
if (t == 0x0u)
|
||||
BLE_DG(ble_dgs.rep_adv_ind++);
|
||||
else
|
||||
BLE_DG(ble_dgs.rep_scan_rsp++);
|
||||
i = bscan.w & (BLE_SCAN_RING - 1u);
|
||||
ble_cpy(bscan.pdu[i], pdu, len);
|
||||
bscan.len[i] = len;
|
||||
bscan.rssi[i] = rssi;
|
||||
BLE_BARRIER(); /* the slot before the index */
|
||||
bscan.w++;
|
||||
}
|
||||
|
||||
BLE_API uint8_t ble_ll_scan_take(uint8_t *pdu, uint16_t *rssi)
|
||||
{
|
||||
uint32_t i;
|
||||
uint8_t n;
|
||||
if (bscan.r == bscan.w)
|
||||
return 0;
|
||||
BLE_BARRIER(); /* the index before the slot */
|
||||
i = bscan.r & (BLE_SCAN_RING - 1u);
|
||||
n = bscan.len[i];
|
||||
ble_cpy(pdu, bscan.pdu[i], n);
|
||||
*rssi = bscan.rssi[i];
|
||||
BLE_BARRIER();
|
||||
bscan.r++;
|
||||
return n;
|
||||
}
|
||||
|
||||
BLE_API void ble_ll_scan(int on)
|
||||
{
|
||||
bll.scan_want = on ? 1u : 0u;
|
||||
if (on && bll.enabled && bll.state == LL_ADV) {
|
||||
ble_hw_adv_stop(); /* one link: advertising stops, the scan takes the link */
|
||||
ll_scan_start();
|
||||
} else if (on && bll.enabled && bll.state == LL_OFF)
|
||||
ll_scan_start();
|
||||
else if (!on && bll.state == LL_SCAN) {
|
||||
ble_hw_scan_stop();
|
||||
bll.state = LL_OFF;
|
||||
if (bll.enabled)
|
||||
ll_adv_start();
|
||||
} /* (initiating or connected: only the wish is kept) */
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------------------------- initiating --- */
|
||||
|
||||
static struct {
|
||||
uint8_t cind[2 + 34]; /* our CONNECT_IND, as the engine sends it */
|
||||
struct ble_hw_conn c; /* its LLData */
|
||||
uint8_t peer[6], peer_rand; /* the target (its AdvA as heard: an RPA stays an RPA) */
|
||||
uint8_t upd_want; /* the peripheral asked for these: an update when we are free */
|
||||
uint16_t upd_interval, upd_timeout;
|
||||
#if BLE_CEN_PAIR
|
||||
uint8_t enc_want, ltk[16], rand[8], skdm[8], ivm[4]; /* ble_ll_start_enc: LL_ENC_REQ when we are free */
|
||||
uint16_t ediv;
|
||||
uint8_t pausing; /* our LL_PAUSE_ENC_REQ is out (a new key on an encrypted link) */
|
||||
uint8_t held[27], held_n; /* the peripheral's control PDU held while our encryption starts */
|
||||
#endif
|
||||
} llc;
|
||||
|
||||
static uint32_t llc_rand32(void)
|
||||
{
|
||||
uint8_t b[4];
|
||||
ble_hw_rand(b, 4);
|
||||
return ble_rd32(b);
|
||||
}
|
||||
|
||||
BLE_API int ble_ll_initiating(void) { return bll.state == LL_INIT; }
|
||||
BLE_API int ble_ll_central(void) { return bll.state == LL_CONN && bll.central; }
|
||||
|
||||
/* our connection's parameters (HW §21.3, ble_cfg.h BLE_CENTRAL_*), and the CONNECT_IND that carries them */
|
||||
static void llc_cind(const uint8_t peer[6], uint8_t peer_rand)
|
||||
{
|
||||
static const uint8_t ALL[5] = {0xFF, 0xFF, 0xFF, 0xFF, 0x1F};
|
||||
struct ble_hw_conn *c = &llc.c;
|
||||
uint8_t *p = llc.cind;
|
||||
uint32_t r, k, iv = BLE_CENTRAL_INTERVAL;
|
||||
for (k = 1, c->aa = llc_rand32(); !ble_aa_valid(c->aa); k++) /* the Core's rules (2.1.2), in ble_prim.c */
|
||||
c->aa = llc_rand32() ^ k * 0x9E3779B1u; /* (stirred: a generator stuck on one value still ends) */
|
||||
c->crc_init = llc_rand32() & 0xFFFFFFu; /* random, as the Core asks (the vendor keeps 0x1983AE) */
|
||||
r = llc_rand32();
|
||||
c->win_size = BLE_CENTRAL_WINSIZE;
|
||||
c->win_offset = (uint16_t)(iv / 2u + r % (iv - iv / 2u)); /* [Interval / 2, Interval - 1], as the vendor */
|
||||
c->interval = (uint16_t)iv;
|
||||
c->latency = 0;
|
||||
c->timeout = BLE_CENTRAL_TIMEOUT;
|
||||
ble_cpy(c->chm, ALL, 5);
|
||||
c->hop = (uint8_t)(5u + (r >> 8) % 12u); /* 5..16 */
|
||||
c->sca = BLE_CENTRAL_SCA;
|
||||
p[0] = (uint8_t)(0x05u | bll.addr_rand << 6 | (peer_rand & 1u) << 7); /* CONNECT_IND, TxAdd, RxAdd, ChSel 0 */
|
||||
p[1] = 34;
|
||||
ble_cpy(p + 2, bll.addr, 6); /* InitA */
|
||||
ble_cpy(p + 8, peer, 6); /* AdvA */
|
||||
p[14] = (uint8_t)c->aa, p[15] = (uint8_t)(c->aa >> 8), p[16] = (uint8_t)(c->aa >> 16), p[17] = (uint8_t)(c->aa >> 24);
|
||||
p[18] = (uint8_t)c->crc_init, p[19] = (uint8_t)(c->crc_init >> 8), p[20] = (uint8_t)(c->crc_init >> 16);
|
||||
p[21] = c->win_size;
|
||||
ble_wr16(p + 22, c->win_offset);
|
||||
ble_wr16(p + 24, c->interval);
|
||||
ble_wr16(p + 26, c->latency);
|
||||
ble_wr16(p + 28, c->timeout);
|
||||
ble_cpy(p + 30, c->chm, 5);
|
||||
p[35] = (uint8_t)(c->hop | c->sca << 5);
|
||||
}
|
||||
|
||||
/* start initiating to peer (its AdvA and TxAdd): advertising or scanning stops (one link). 0: not now (off, or a
|
||||
* connection is up) */
|
||||
BLE_API int ble_ll_connect(const uint8_t peer[6], uint8_t peer_rand)
|
||||
{
|
||||
struct ble_hw_init in;
|
||||
if (!bll.enabled || bll.state == LL_CONN)
|
||||
return 0;
|
||||
if (bll.state == LL_ADV)
|
||||
ble_hw_adv_stop();
|
||||
else if (bll.state == LL_SCAN)
|
||||
ble_hw_scan_stop();
|
||||
else if (bll.state == LL_INIT)
|
||||
ble_hw_init_stop();
|
||||
llc_cind(peer, peer_rand);
|
||||
ble_cpy(llc.peer, peer, 6);
|
||||
llc.peer_rand = (uint8_t)(peer_rand & 1u);
|
||||
in.interval = BLE_INIT_INTERVAL;
|
||||
in.window = BLE_INIT_WINDOW;
|
||||
in.cind = llc.cind;
|
||||
in.conn = llc.c;
|
||||
bll.state = LL_INIT;
|
||||
BLE_DG(ble_dgc.connects++);
|
||||
ble_diag_ev(BDE_INIT_START, llc.c.win_offset);
|
||||
ble_hw_init_start(&in);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* stop initiating (the caller's timeout, NONE picked): advertising or the scan again */
|
||||
BLE_API void ble_ll_connect_cancel(void)
|
||||
{
|
||||
if (bll.state != LL_INIT)
|
||||
return;
|
||||
ble_hw_init_stop();
|
||||
bll.state = LL_OFF;
|
||||
BLE_DG(ble_dgc.cancels++);
|
||||
ll_idle();
|
||||
}
|
||||
|
||||
/* the driver: the engine sent our CONNECT_IND and the link is the master now (event counter 0 is the first anchor) */
|
||||
BLE_API void ble_ll_hw_master_start(void)
|
||||
{
|
||||
if (bll.state != LL_INIT)
|
||||
return;
|
||||
ll_conn_begin(&llc.c, llc.peer, llc.peer_rand, 1);
|
||||
llc.upd_want = 0;
|
||||
#if BLE_CEN_PAIR
|
||||
llc.enc_want = llc.pausing = llc.held_n = 0;
|
||||
#endif
|
||||
ble_diag_ev(BDE_MASTER, llc.c.interval);
|
||||
ble_host_connected();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------- the master's procedures --- */
|
||||
|
||||
/* an LL_CONNECTION_UPDATE_IND from us: WinSize 1, WinOffset = Interval / 2 and the instant 8..11 events ahead (the
|
||||
* vendor's plain case, HW §21.4); the driver writes the master's values at instant - 1 */
|
||||
static int llc_update(uint16_t interval, uint16_t timeout, uint32_t now)
|
||||
{
|
||||
uint8_t d[11];
|
||||
uint16_t instant = (uint16_t)(bll.last_evt + 8u + llc_rand32() % 4u);
|
||||
if (bll.lproc != P_NONE || bll.upd_pending || bll.chm_pending)
|
||||
return 0;
|
||||
d[0] = 1;
|
||||
ble_wr16(d + 1, interval / 2u);
|
||||
ble_wr16(d + 3, interval);
|
||||
ble_wr16(d + 5, 0);
|
||||
ble_wr16(d + 7, timeout);
|
||||
ble_wr16(d + 9, instant);
|
||||
if (!ll_ctrl(LL_CONNECTION_UPDATE_IND, d, 11))
|
||||
return 0;
|
||||
bll.upd.win_size = 1;
|
||||
bll.upd.win_offset = (uint16_t)(interval / 2u);
|
||||
bll.upd.interval = interval;
|
||||
bll.upd.latency = 0;
|
||||
bll.upd.timeout = timeout;
|
||||
bll.upd.instant = instant;
|
||||
bll.upd_pending = 1;
|
||||
bll.lproc = P_UPD; /* (done at the instant: ble_ll_hw_event_end) */
|
||||
bll.lproc_t = now;
|
||||
ble_hw_conn_update(&bll.upd);
|
||||
BLE_DG(ble_dgc.m_upd_tx++);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* as master: move to a new interval in [imin, imax] (ours when it fits) with this supervision timeout -> 1 started,
|
||||
* 0 not valid or not now (another procedure: kept for later when it came from the peripheral) */
|
||||
BLE_API int ble_ll_conn_update(uint16_t imin, uint16_t imax, uint16_t timeout)
|
||||
{
|
||||
uint16_t iv = (uint16_t)(BLE_CENTRAL_INTERVAL < imin ? imin : BLE_CENTRAL_INTERVAL > imax ? imax : BLE_CENTRAL_INTERVAL);
|
||||
if (!ble_ll_central() || imin < 6u || imin > imax || !ll_params_ok(iv, 0, timeout))
|
||||
return 0;
|
||||
if (llc_update(iv, timeout, ble_hw_time_us()))
|
||||
return 1;
|
||||
llc.upd_want = 1; /* busy: when the current procedure is over */
|
||||
llc.upd_interval = iv;
|
||||
llc.upd_timeout = timeout;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* as master: a new channel map from the event counter + 6..9 on (HW §21.4) -> 1 started */
|
||||
BLE_API int ble_ll_chmap_update(const uint8_t chm[5])
|
||||
{
|
||||
struct ble_chmap m;
|
||||
uint8_t d[7];
|
||||
uint16_t instant = (uint16_t)(bll.last_evt + 7u + llc_rand32() % 4u);
|
||||
if (!ble_ll_central() || bll.lproc != P_NONE || bll.upd_pending || bll.chm_pending)
|
||||
return 0;
|
||||
ble_cpy(d, chm, 5);
|
||||
d[4] &= 0x1Fu;
|
||||
if (!ble_chmap_set(&m, d))
|
||||
return 0;
|
||||
ble_wr16(d + 5, instant);
|
||||
if (!ll_ctrl(LL_CHANNEL_MAP_IND, d, 7))
|
||||
return 0;
|
||||
ble_cpy(bll.new_chm, d, 5);
|
||||
bll.chm_instant = instant;
|
||||
bll.chm_pending = 1;
|
||||
bll.lproc = P_UPD;
|
||||
bll.lproc_t = ble_hw_time_us();
|
||||
ble_hw_chmap_update(bll.new_chm, instant);
|
||||
BLE_DG(ble_dgc.m_chm_tx++);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#if BLE_CEN_PAIR
|
||||
/* as master: encrypt the link with ltk (a bond's, EDIV / Rand as the peripheral handed them out; or a pairing's STK,
|
||||
* EDIV 0 / Rand 0), at the first moment no other procedure of ours runs. On a link encrypted already (a pairing
|
||||
* again, with MITM, on it) the encryption is paused first (5.1.3.2), then started with the new
|
||||
* key. Done:
|
||||
* ble_host_encrypted(); refused: ble_host_enc_failed() */
|
||||
BLE_API void ble_ll_start_enc(const uint8_t ltk[16], const uint8_t rand[8], uint16_t ediv)
|
||||
{
|
||||
if (!ble_ll_central())
|
||||
return;
|
||||
ble_cpy(llc.ltk, ltk, 16);
|
||||
ble_cpy(llc.rand, rand, 8);
|
||||
llc.ediv = ediv;
|
||||
llc.enc_want = 1;
|
||||
}
|
||||
|
||||
static int llc_enc_req(uint32_t now)
|
||||
{
|
||||
uint8_t d[22];
|
||||
ble_cpy(d, llc.rand, 8);
|
||||
ble_wr16(d + 8, llc.ediv);
|
||||
ble_hw_rand(llc.skdm, 8);
|
||||
ble_hw_rand(llc.ivm, 4);
|
||||
ble_cpy(d + 10, llc.skdm, 8);
|
||||
ble_cpy(d + 18, llc.ivm, 4);
|
||||
if (!ll_ctrl(LL_ENC_REQ, d, 22))
|
||||
return 0;
|
||||
llc.enc_want = 0;
|
||||
bll.enc_rx = bll.enc_tx = 0;
|
||||
bll.tx_paused = 1; /* no data until the encryption runs (5.1.3.1) */
|
||||
bll.lproc = P_ENC;
|
||||
bll.lproc_t = now;
|
||||
BLE_DG(ble_dgc.m_enc_req_tx++);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* the encryption pause (Core Vol 6 Part B 5.1.3.2), the master's side: our LL_PAUSE_ENC_REQ (encrypted), the
|
||||
* peripheral's LL_PAUSE_ENC_RSP (encrypted), then ours unencrypted and both directions plain; the encryption start
|
||||
* with the new key follows at once (LL_ENC_REQ). No data meanwhile */
|
||||
static int llc_pause_req(uint32_t now)
|
||||
{
|
||||
if (!ll_ctrl(LL_PAUSE_ENC_REQ, llc.ivm, 0))
|
||||
return 0;
|
||||
llc.enc_want = 0;
|
||||
llc.pausing = 1;
|
||||
bll.tx_paused = 1;
|
||||
bll.lproc = P_ENC;
|
||||
bll.lproc_t = now;
|
||||
BLE_DG(ble_dgc.m_pause_tx++);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void llc_enc_failed(uint8_t err);
|
||||
|
||||
static void llc_pause_rsp(void)
|
||||
{
|
||||
uint8_t d[1];
|
||||
llc.pausing = 0;
|
||||
BLE_DG(ble_dgc.m_pause_rsp_rx++);
|
||||
bll.enc_rx = bll.enc_tx = 0; /* (ours goes unencrypted, and all after it) */
|
||||
if (!ll_ctrl(LL_PAUSE_ENC_RSP, d, 0) || !llc_enc_req(ble_hw_time_us()))
|
||||
llc_enc_failed(BLE_ERR_UNSPECIFIED);
|
||||
}
|
||||
|
||||
/* LL_ENC_RSP: SK = e(LTK, SKDs || SKDm), IV = IVm || IVs; directionBit 1 for what we send */
|
||||
static void llc_enc_rsp(const uint8_t *p)
|
||||
{
|
||||
uint8_t skd[16], i;
|
||||
for (i = 0; i < 8u; i++) { /* most significant octet first */
|
||||
skd[i] = p[1 + 7 - i];
|
||||
skd[8 + i] = llc.skdm[7 - i];
|
||||
}
|
||||
for (i = 0; i < 16u; i++)
|
||||
bll.ccm_tx.key[i] = llc.ltk[15 - i];
|
||||
ble_aes128(bll.ccm_tx.key, skd, bll.ccm_tx.key);
|
||||
ble_cpy(bll.ccm_tx.iv, llc.ivm, 4);
|
||||
ble_cpy(bll.ccm_tx.iv + 4, p + 9, 4);
|
||||
bll.ccm_tx.ctr = 0;
|
||||
bll.ccm_tx.ctr_hi = 0;
|
||||
bll.ccm_rx = bll.ccm_tx;
|
||||
bll.ccm_tx.dir = 1;
|
||||
bll.ccm_rx.dir = 0;
|
||||
BLE_DG(ble_dgc.m_enc_rsp_rx++);
|
||||
}
|
||||
|
||||
static void llc_enc_failed(uint8_t err)
|
||||
{
|
||||
bll.lproc = P_NONE;
|
||||
bll.tx_paused = 0;
|
||||
if (llc.pausing)
|
||||
llc.pausing = 0; /* (the pause refused: still encrypted with the old key) */
|
||||
else
|
||||
bll.enc_rx = bll.enc_tx = 0;
|
||||
BLE_DG(ble_dgc.m_enc_rej++);
|
||||
BLE_DG(ble_dgc.m_enc_rej_err = err);
|
||||
ble_host_enc_failed(err);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* our procedures as master, one at a time once the link is up: the version exchange, the feature exchange, the
|
||||
* encryption the host asked for, an update the peripheral asked for, the data length */
|
||||
static void llc_start_procs(uint32_t now)
|
||||
{
|
||||
uint8_t d[8];
|
||||
#if BLE_CEN_PAIR
|
||||
if (llc.held_n) { /* the peripheral's procedure held during our encryption start */
|
||||
d[0] = llc.held_n;
|
||||
llc.held_n = 0;
|
||||
ll_rx_ctrl(llc.held, d[0]);
|
||||
}
|
||||
#endif
|
||||
if (!bll.ver_sent) {
|
||||
if (ll_version_send()) {
|
||||
bll.lproc = P_VER;
|
||||
bll.lproc_t = now;
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!bll.feat_known) {
|
||||
ble_zero(d, 8);
|
||||
d[0] = (uint8_t)LL_OUR_FEAT;
|
||||
if (ll_ctrl(LL_FEATURE_REQ, d, 8)) {
|
||||
bll.lproc = P_FEAT;
|
||||
bll.lproc_t = now;
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (BLE_LL_MAX_OCTETS > 27u && !bll.len_done && (bll.peer_feat & BLE_FEAT_DLE)) {
|
||||
ll_our_lengths(d); /* (before the encryption: else the iPhone sends its own
|
||||
* LL_LENGTH_REQ across our LL_ENC_REQ, blell-dev6) */
|
||||
if (ll_ctrl(LL_LENGTH_REQ, d, 8)) {
|
||||
bll.lproc = P_LEN;
|
||||
bll.lproc_t = now;
|
||||
}
|
||||
return;
|
||||
}
|
||||
#if BLE_CEN_PAIR
|
||||
if (llc.enc_want) {
|
||||
if (bll.enc_tx)
|
||||
llc_pause_req(now); /* a new key on an encrypted link: paused first */
|
||||
else
|
||||
llc_enc_req(now);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
if (llc.upd_want && llc_update(llc.upd_interval, llc.upd_timeout, now))
|
||||
llc.upd_want = 0;
|
||||
}
|
||||
|
||||
/* a control PDU from the peripheral that the master handles otherwise -> 1 handled (else ble_ll.c's own code). While
|
||||
* our encryption starts (from our LL_ENC_REQ or LL_PAUSE_ENC_REQ to its LL_START_ENC_RSP) we send nothing but that
|
||||
* procedure's PDUs (Core Vol 6 Part B 5.1.3.1): a procedure the peripheral starts meanwhile is held, answered once the
|
||||
* link is encrypted (the iPhone left with MIC Failure 0x3D when our LL_LENGTH_RSP came then, blell-dev6) */
|
||||
static int llc_rx_ctrl(uint8_t op, const uint8_t *p, uint8_t n)
|
||||
{
|
||||
uint8_t d[8];
|
||||
#if BLE_CEN_PAIR
|
||||
if (bll.lproc == P_ENC && op != LL_ENC_RSP && op != LL_START_ENC_REQ && op != LL_START_ENC_RSP &&
|
||||
op != LL_PAUSE_ENC_RSP && op != LL_REJECT_IND && op != LL_REJECT_EXT_IND && op != LL_UNKNOWN_RSP &&
|
||||
op != LL_TERMINATE_IND && n <= sizeof llc.held) {
|
||||
ble_cpy(llc.held, p, n);
|
||||
llc.held_n = n;
|
||||
BLE_DG(ble_dgc.m_held++);
|
||||
return 1;
|
||||
}
|
||||
#else
|
||||
(void)n;
|
||||
#endif
|
||||
switch (op) {
|
||||
case LL_CONNECTION_UPDATE_IND: /* a master's PDUs: a peripheral never sends them */
|
||||
case LL_CHANNEL_MAP_IND:
|
||||
case LL_ENC_REQ:
|
||||
case LL_PHY_UPDATE_IND:
|
||||
case LL_FEATURE_REQ:
|
||||
ll_unknown(op);
|
||||
return 1;
|
||||
case LL_VERSION_IND:
|
||||
BLE_DG(ble_dgc.m_ver_rx++);
|
||||
return 0;
|
||||
case LL_FEATURE_RSP:
|
||||
BLE_DG(ble_dgc.m_feat_rsp++);
|
||||
return 0;
|
||||
case LL_LENGTH_RSP:
|
||||
BLE_DG(ble_dgc.m_len_done++);
|
||||
return 0;
|
||||
case LL_PERIPHERAL_FEATURE_REQ: /* the peripheral starts the feature exchange */
|
||||
ll_features(LL_FEATURE_RSP, p[1]);
|
||||
return 1;
|
||||
case LL_PHY_REQ: /* 1M only: LL_PHY_UPDATE_IND with no change (5.1.10) */
|
||||
BLE_DG(ble_dgc.m_phy_req_rx++);
|
||||
ble_zero(d, 4);
|
||||
ll_ctrl(LL_PHY_UPDATE_IND, d, 4);
|
||||
return 1;
|
||||
case LL_CONNECTION_PARAM_REQ: /* answered with our LL_CONNECTION_UPDATE_IND (5.1.7.2) */
|
||||
BLE_DG(ble_dgc.m_param_req_rx++);
|
||||
if (!ll_params_ok(ble_rd16(p + 3), ble_rd16(p + 5), ble_rd16(p + 7)) || ble_rd16(p + 1) > ble_rd16(p + 3) ||
|
||||
ble_rd16(p + 1) < 6u) {
|
||||
ll_reject(op, BLE_ERR_INVALID_LL_PARAMS);
|
||||
return 1;
|
||||
}
|
||||
ble_ll_conn_update(ble_rd16(p + 1), ble_rd16(p + 3), ble_rd16(p + 7));
|
||||
return 1;
|
||||
case LL_CONNECTION_PARAM_RSP: /* (we never ask) */
|
||||
return 1;
|
||||
#if BLE_CEN_PAIR
|
||||
case LL_ENC_RSP:
|
||||
if (bll.lproc == P_ENC)
|
||||
llc_enc_rsp(p);
|
||||
return 1;
|
||||
case LL_PAUSE_ENC_RSP: /* (encrypted): ours plain, then the start with the new key */
|
||||
if (bll.lproc == P_ENC && llc.pausing)
|
||||
llc_pause_rsp();
|
||||
return 1;
|
||||
case LL_START_ENC_REQ: /* (sent plain; our answer and everything after: encrypted) */
|
||||
if (bll.lproc != P_ENC)
|
||||
return 1;
|
||||
BLE_DG(ble_dgc.m_start_enc_rx++);
|
||||
bll.enc_rx = bll.enc_tx = 1;
|
||||
ll_ctrl(LL_START_ENC_RSP, d, 0);
|
||||
return 1;
|
||||
case LL_START_ENC_RSP:
|
||||
if (bll.lproc != P_ENC || !bll.enc_rx)
|
||||
return 1;
|
||||
bll.lproc = P_NONE;
|
||||
bll.tx_paused = 0;
|
||||
BLE_DG(ble_dg.enc_on_n++);
|
||||
BLE_DG(ble_dgc.m_enc_on++);
|
||||
ble_host_encrypted();
|
||||
return 1;
|
||||
case LL_REJECT_IND:
|
||||
if (bll.lproc != P_ENC)
|
||||
return 0;
|
||||
llc_enc_failed(p[1]);
|
||||
return 1;
|
||||
case LL_REJECT_EXT_IND:
|
||||
if (bll.lproc != P_ENC || (p[1] != LL_ENC_REQ && p[1] != LL_PAUSE_ENC_REQ))
|
||||
return 0;
|
||||
llc_enc_failed(p[2]);
|
||||
return 1;
|
||||
case LL_UNKNOWN_RSP:
|
||||
if (bll.lproc != P_ENC || (p[1] != LL_ENC_REQ && p[1] != LL_PAUSE_ENC_REQ))
|
||||
return 0;
|
||||
llc_enc_failed(BLE_ERR_UNSUPP_REMOTE);
|
||||
return 1;
|
||||
#endif
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-only OR MPL-2.0 */
|
||||
/* The AD parser and the found-devices table (ble_scan.h). Written from the Core Specification Supplement Part A
|
||||
* (AD types 0x01, 0x06, 0x07, 0x08, 0x09) and Core Vol 6 Part B 2.3 (advertising PDUs). No vendor code. */
|
||||
#include "ble_scan.h"
|
||||
#include "ble_host.h" /* BLE_UUID_MIDI_SVC */
|
||||
#include "ble_util.h"
|
||||
|
||||
enum { SC_ADV_IND = 0, SC_SCAN_RSP = 4 };
|
||||
|
||||
BLE_API void ble_ad_parse(const uint8_t *ad, uint32_t n, struct ble_ad *out)
|
||||
{
|
||||
uint32_t i = 0;
|
||||
ble_zero((uint8_t *)out, sizeof *out);
|
||||
while (i < n) {
|
||||
uint32_t len = ad[i], k;
|
||||
const uint8_t *d = ad + i + 2;
|
||||
if (!len)
|
||||
break; /* the significant part ends (the rest is padding) */
|
||||
if (i + 1u + len > n) {
|
||||
out->bad = 1;
|
||||
break;
|
||||
}
|
||||
switch (ad[i + 1]) {
|
||||
case 0x01:
|
||||
if (len >= 2u) {
|
||||
out->flags = d[0];
|
||||
out->has_flags = 1;
|
||||
}
|
||||
break;
|
||||
case 0x06: /* incomplete / complete list of 128-bit service UUIDs */
|
||||
case 0x07:
|
||||
for (k = 0; k + 16u <= len - 1u; k += 16u)
|
||||
if (ble_eq(d + k, BLE_UUID_MIDI_SVC, 16))
|
||||
out->midi = 1;
|
||||
break;
|
||||
case 0x08:
|
||||
case 0x09:
|
||||
if (out->name_type != 0x09u) { /* the complete name wins over a shortened one */
|
||||
out->name_type = ad[i + 1];
|
||||
out->name = d;
|
||||
out->name_len = (uint8_t)(len - 1u);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
i += 1u + len;
|
||||
}
|
||||
}
|
||||
|
||||
/* a name as the screen's Latin-1 font shows it: ASCII as it is, a two-octet UTF-8 Latin-1 character decoded, anything
|
||||
* else '?'; at most BLE_NAME_MAX characters, terminated */
|
||||
static void sc_name(char *dst, const uint8_t *s, uint32_t n)
|
||||
{
|
||||
uint32_t i = 0, o = 0;
|
||||
while (i < n && o < BLE_NAME_MAX) {
|
||||
uint8_t c = s[i++];
|
||||
if (c >= 0x20u && c < 0x7Fu)
|
||||
dst[o++] = (char)c;
|
||||
else if ((c == 0xC2u || c == 0xC3u) && i < n && (s[i] & 0xC0u) == 0x80u) {
|
||||
uint8_t l = (uint8_t)((c & 3u) << 6 | (s[i++] & 0x3Fu));
|
||||
dst[o++] = (char)(l >= 0xA0u ? l : '?');
|
||||
} else if (c >= 0x20u || c == 0) {
|
||||
if (c == 0)
|
||||
break; /* (a padded name) */
|
||||
dst[o++] = '?';
|
||||
while (i < n && (s[i] & 0xC0u) == 0x80u)
|
||||
i++; /* (the rest of that UTF-8 character) */
|
||||
}
|
||||
}
|
||||
dst[o] = 0;
|
||||
}
|
||||
|
||||
BLE_API void ble_scan_clear(struct ble_scan_tab *t)
|
||||
{
|
||||
uint32_t gen = t->gen;
|
||||
ble_zero((uint8_t *)t, sizeof *t);
|
||||
t->gen = gen + 1u;
|
||||
}
|
||||
|
||||
static int sc_find(const struct ble_scan_tab *t, const uint8_t *a, uint8_t rnd)
|
||||
{
|
||||
uint32_t i;
|
||||
for (i = 0; i < BLE_SCAN_N; i++)
|
||||
if (t->e[i].used && t->e[i].addr_rand == rnd && ble_eq(t->e[i].addr, a, 6))
|
||||
return (int)i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* a free slot, else the oldest unconfirmed one; a BLE-MIDI device may also take the slot of the listed device heard
|
||||
* least recently when that one is not heard for 2 s. -1: none */
|
||||
static int sc_slot(const struct ble_scan_tab *t, int midi, uint32_t now_ms)
|
||||
{
|
||||
uint32_t i;
|
||||
int un = -1, old = -1;
|
||||
for (i = 0; i < BLE_SCAN_N; i++) {
|
||||
const struct ble_found *e = &t->e[i];
|
||||
if (!e->used)
|
||||
return (int)i;
|
||||
if (!e->midi && (un < 0 || e->seen_ms < t->e[un].seen_ms))
|
||||
un = (int)i;
|
||||
if (e->midi && (old < 0 || e->seen_ms < t->e[old].seen_ms))
|
||||
old = (int)i;
|
||||
}
|
||||
if (un >= 0)
|
||||
return un;
|
||||
if (midi && old >= 0 && now_ms - t->e[old].seen_ms > 2000u)
|
||||
return old;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void sc_listed(struct ble_scan_tab *t, struct ble_found *e)
|
||||
{
|
||||
e->midi = 1;
|
||||
e->order = ++t->next_order; /* new ones go to the bottom of the list */
|
||||
t->gen++;
|
||||
}
|
||||
|
||||
BLE_API void ble_scan_add(struct ble_scan_tab *t, const uint8_t *pdu, uint32_t len, uint16_t rssi, uint32_t now_ms)
|
||||
{
|
||||
struct ble_ad ad;
|
||||
struct ble_found *e;
|
||||
uint8_t type, rnd;
|
||||
int i;
|
||||
t->reports++;
|
||||
if (len < 8u || pdu[1] < 6u || pdu[1] > 37u || len < 2u + pdu[1]) {
|
||||
t->bad_ad++;
|
||||
return;
|
||||
}
|
||||
type = (uint8_t)(pdu[0] & 0x0Fu);
|
||||
rnd = (uint8_t)(pdu[0] >> 6 & 1u); /* TxAdd */
|
||||
if (type != SC_ADV_IND && type != SC_SCAN_RSP) {
|
||||
t->ignored_type++; /* not connectable (or directed elsewhere): never a list entry */
|
||||
return;
|
||||
}
|
||||
ble_ad_parse(pdu + 8, pdu[1] - 6u, &ad);
|
||||
if (ad.bad)
|
||||
t->bad_ad++;
|
||||
i = sc_find(t, pdu + 2, rnd);
|
||||
if (i < 0) {
|
||||
if (type != SC_ADV_IND)
|
||||
return; /* a scan response is only news about an advertiser already seen */
|
||||
i = sc_slot(t, ad.midi, now_ms);
|
||||
if (i < 0) {
|
||||
t->dropped_full++;
|
||||
return;
|
||||
}
|
||||
e = &t->e[i];
|
||||
if (e->used && e->midi)
|
||||
t->gen++; /* (a listed one gave its place) */
|
||||
ble_zero((uint8_t *)e, sizeof *e);
|
||||
e->used = 1;
|
||||
e->addr_rand = rnd;
|
||||
ble_cpy(e->addr, pdu + 2, 6);
|
||||
}
|
||||
e = &t->e[i];
|
||||
t->kept++;
|
||||
e->seen_ms = now_ms;
|
||||
if (rssi)
|
||||
e->rssi = rssi;
|
||||
if (e->hits < 255u)
|
||||
e->hits++;
|
||||
if (ad.name_type && (ad.name_type == 0x09u || e->name_type != 0x09u)) {
|
||||
char nm[BLE_NAME_MAX + 1u];
|
||||
uint32_t k;
|
||||
sc_name(nm, ad.name, ad.name_len);
|
||||
for (k = 0; k <= BLE_NAME_MAX && nm[k] == e->name[k] && nm[k]; k++)
|
||||
;
|
||||
if (nm[k] != e->name[k]) {
|
||||
ble_cpy((uint8_t *)e->name, (const uint8_t *)nm, sizeof nm);
|
||||
if (e->midi)
|
||||
t->gen++;
|
||||
}
|
||||
e->name_type = ad.name_type;
|
||||
}
|
||||
e->kind = (uint8_t)(BLE_KIND_MIDI | (ble_eq((const uint8_t *)e->name, (const uint8_t *)"Optimist ", 9) ||
|
||||
ble_eq((const uint8_t *)e->name, (const uint8_t *)"FM-1 ", 5) ?
|
||||
BLE_KIND_FM1 : 0u)); /* (our name; "FM-1 " is that of the builds before 2026-10-16) */
|
||||
if (ad.midi && !e->midi)
|
||||
sc_listed(t, e);
|
||||
}
|
||||
|
||||
BLE_API void ble_scan_age(struct ble_scan_tab *t, uint32_t now_ms)
|
||||
{
|
||||
uint32_t i;
|
||||
for (i = 0; i < BLE_SCAN_N; i++) {
|
||||
struct ble_found *e = &t->e[i];
|
||||
if (!e->used)
|
||||
continue;
|
||||
if (now_ms - e->seen_ms > (e->midi ? BLE_SCAN_AGE_MS : BLE_SCAN_AGE_UNCONF_MS)) {
|
||||
if (e->midi)
|
||||
t->gen++;
|
||||
e->used = 0;
|
||||
continue;
|
||||
}
|
||||
e->hits = (uint8_t)((e->hits + 1u) / 2u);
|
||||
}
|
||||
}
|
||||
|
||||
BLE_API uint32_t ble_scan_list(const struct ble_scan_tab *t, uint8_t idx[BLE_SCAN_N])
|
||||
{
|
||||
uint32_t n = 0, i, k;
|
||||
for (i = 0; i < BLE_SCAN_N; i++)
|
||||
if (t->e[i].used && t->e[i].midi) {
|
||||
for (k = n; k > 0 && t->e[idx[k - 1u]].order > t->e[i].order; k--)
|
||||
idx[k] = idx[k - 1u]; /* (insertion by first-listed order) */
|
||||
idx[k] = (uint8_t)i;
|
||||
n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
BLE_API uint32_t ble_scan_strength(uint16_t rssi)
|
||||
{
|
||||
/* RSSI dBm = 21 - (RSSI2 & 63) - T[(RSSI2 >> 8) & 63] (HW §3 0x138) with T not transcribed: a bigger fine part
|
||||
* or gain index is taken to mean a weaker signal [I] */
|
||||
return 255u - ((rssi & 63u) + (rssi >> 8 & 63u));
|
||||
}
|
||||
|
||||
static uint32_t sc_metric(const struct ble_scan_tab *t, uint32_t i, int by_rssi)
|
||||
{
|
||||
return by_rssi ? ble_scan_strength(t->e[i].rssi) : t->e[i].hits;
|
||||
}
|
||||
|
||||
BLE_API uint32_t ble_scan_bars(const struct ble_scan_tab *t, uint32_t i)
|
||||
{
|
||||
uint8_t idx[BLE_SCAN_N];
|
||||
uint32_t n = ble_scan_list(t, idx), k, lo = 0xFFFFFFFFu, hi = 0, m;
|
||||
int by_rssi = 1;
|
||||
if (i >= BLE_SCAN_N || !t->e[i].used || !t->e[i].midi)
|
||||
return 0;
|
||||
for (k = 0; k < n; k++)
|
||||
if (!t->e[idx[k]].rssi)
|
||||
by_rssi = 0; /* any without an RSSI word: all by how often they were heard */
|
||||
for (k = 0; k < n; k++) {
|
||||
m = sc_metric(t, idx[k], by_rssi);
|
||||
lo = m < lo ? m : lo;
|
||||
hi = m > hi ? m : hi;
|
||||
}
|
||||
if (hi == lo)
|
||||
return 3;
|
||||
m = sc_metric(t, i, by_rssi);
|
||||
return 1u + (2u * (m - lo) + (hi - lo) / 2u) / (hi - lo);
|
||||
}
|
||||
@@ -0,0 +1,309 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-only OR MPL-2.0 */
|
||||
/* The Security Manager as initiator (BLE_CENTRAL 1, BLE_CEN_PAIR: we are the master of the link; Core Specification Vol 3 Part H),
|
||||
* part of ble_smp.c (included there: it shares bsmp, smp_send, smp_c1, smp_cut). LE legacy pairing with bonding, and
|
||||
* only when the peripheral asks (a Security Request, Insufficient Authentication / Encryption: ble_central.c). Our
|
||||
* Pairing Request is always DisplayOnly, bonding + MITM (2.3.5.3, Table 2.8); the responder's IO capability picks:
|
||||
* passkey entry a responder that can type (KeyboardOnly / KeyboardDisplay: a Mac, a phone) gets a passkey we show
|
||||
* (0..999999, random, TK = the passkey); its user types it. The keys are authenticated (MITM
|
||||
* protection, security mode 1 level 3, Vol 3 Part C 10.2.1). Asked in the first pairing, not after
|
||||
* a Just Works one: macOS drops an alert that comes right after another (2026-10-18, the window
|
||||
* vanished before the passkey could be typed);
|
||||
* Just Works a responder that cannot type (another FM-1, most controllers): TK = 0, nothing on either screen
|
||||
* of ours. Unless the host knows the peer needs MITM (mitm 1): then we refuse with Authentication
|
||||
* Requirements (0x03) instead of a pairing it will refuse anyway.
|
||||
* Neither side asks for Secure Connections, so legacy pairing is used:
|
||||
* Pairing Request -> the responder's Pairing Response (a key size under 7 fails) -> our Mconfirm = c1(TK, Mrand, ...)
|
||||
* -> its Sconfirm (with a passkey: once its user typed it) -> our Mrand -> its Srand, checked against Sconfirm ->
|
||||
* STK = s1(TK, Srand, Mrand) cut to the key size -> the LL encryption with the STK started by us (EDIV 0, Rand 0) ->
|
||||
* phase 3: the responder's keys first (its LTK, EDIV, Rand: the bond; its IRK and identity address: finding it again
|
||||
* behind a resolvable private address), then ours (an LTK, EDIV and Rand we never use: a peripheral cannot start
|
||||
* encryption; our IRK (smp_own_irk) and our static identity address) -> the keys to the firmware (ble_app_central_keys, with
|
||||
* BLE_KEYS_AUTH after a passkey) and the host told (ble_central_*).
|
||||
* The 30 s SMP timeout (3.4, from our last command: the user types within it) ends the link. A Pairing Failed either
|
||||
* way ends the pairing (the host decides). Context: the BLE interrupts' (the passkey is read by the main loop). */
|
||||
|
||||
enum { I_RSP = 10, I_CONFIRM, I_RANDOM, I_ENC, I_KEYS };
|
||||
enum { IO_DISPLAY_ONLY = 0x00, IO_KEYBOARD_ONLY = 0x02, IO_KEYBOARD_DISPLAY = 0x04 };
|
||||
|
||||
/* (ble_central.c) */
|
||||
static void ble_central_paired(int ok, uint8_t reason, uint8_t auth);
|
||||
static void ble_central_sec_req(uint8_t auth_req);
|
||||
static void ble_central_encrypted(void);
|
||||
static int ble_central_late_fail(uint8_t reason, int at);
|
||||
|
||||
static int smp_init_busy(void) { return bsmp.init && bsmp.st >= I_RSP; }
|
||||
|
||||
static void smp_init_send(const uint8_t *p, uint16_t n)
|
||||
{
|
||||
smp_send(p, n);
|
||||
bsmp.t0 = ble_hw_time_us(); /* (3.4: the timer restarts with each command queued) */
|
||||
}
|
||||
|
||||
static void smp_init_fail(uint8_t why, int send)
|
||||
{
|
||||
if (send)
|
||||
smp_fail(why);
|
||||
bsmp.st = S_IDLE;
|
||||
smp_passkey = BLE_NO_PASSKEY;
|
||||
BLE_DG(ble_dgc.si_last_fail = why);
|
||||
if (send)
|
||||
BLE_DG(ble_dgc.si_fail_tx++);
|
||||
ble_central_paired(0, why, bsmp.auth); /* (auth: a passkey was shown) */
|
||||
}
|
||||
|
||||
/* start a pairing as initiator (the host: a Security Request, an authentication error, a bond the peer lost); mitm:
|
||||
* the peer needs an authenticated link (Just Works refused), else a passkey only if it can type */
|
||||
static void ble_smp_pair(int mitm)
|
||||
{
|
||||
uint8_t *r = bsmp.preq;
|
||||
if (smp_init_busy())
|
||||
return;
|
||||
ble_smp_reset();
|
||||
bsmp.init = 1;
|
||||
bsmp.mitm = (uint8_t)(mitm != 0);
|
||||
r[0] = SMP_PAIR_REQ;
|
||||
r[1] = IO_DISPLAY_ONLY; /* IO capability: a display (the FM-1's screen) */
|
||||
r[2] = 0x00; /* no OOB data */
|
||||
r[3] = SMP_AUTH_BOND | SMP_AUTH_MITM; /* no Secure Connections, no keypress */
|
||||
r[4] = 16; /* our largest key size */
|
||||
r[5] = SMP_KD_ENC | SMP_KD_ID; /* ours to them (initiator key distribution) */
|
||||
r[6] = SMP_KD_ENC | SMP_KD_ID; /* theirs to us (responder key distribution) */
|
||||
smp_init_send(r, 7);
|
||||
bsmp.st = I_RSP;
|
||||
BLE_DG(ble_dgc.si_pair_req++);
|
||||
if (mitm)
|
||||
BLE_DG(ble_dgc.si_mitm_req++);
|
||||
}
|
||||
|
||||
/* the method (2.3.5.1, Table 2.8 with us DisplayOnly and MITM asked): passkey entry when the responder can type */
|
||||
static int smp_init_passkey_method(const uint8_t *rsp)
|
||||
{
|
||||
return rsp[1] == IO_KEYBOARD_ONLY || rsp[1] == IO_KEYBOARD_DISPLAY;
|
||||
}
|
||||
|
||||
static void smp_init_rsp(const uint8_t *p)
|
||||
{
|
||||
uint8_t c[17], k[4];
|
||||
int pk = smp_init_passkey_method(p);
|
||||
BLE_DG(ble_dgc.si_rsp_io = p[1]);
|
||||
BLE_DG(ble_dgc.si_rsp_auth = p[3]);
|
||||
if (p[4] < 7u || p[4] > 16u) {
|
||||
smp_init_fail(SMP_E_KEY_SIZE, 1);
|
||||
return;
|
||||
}
|
||||
BLE_DG(ble_dgc.si_pair_rsp++);
|
||||
if (bsmp.mitm && !pk) {
|
||||
smp_init_fail(SMP_E_AUTH_REQ, 1); /* (Just Works would not satisfy it: refused at once) */
|
||||
return;
|
||||
}
|
||||
ble_cpy(bsmp.pres, p, 7);
|
||||
bsmp.key_size = p[4];
|
||||
bsmp.bond = (p[3] & 3u) == SMP_AUTH_BOND;
|
||||
bsmp.ours = (uint8_t)(p[5] & bsmp.preq[5]); /* what the responder agreed to take from us / give us */
|
||||
bsmp.theirs = (uint8_t)(p[6] & bsmp.preq[6]);
|
||||
if (pk) { /* passkey entry: we display, its user types (TK = the passkey) */
|
||||
ble_hw_rand(k, 4);
|
||||
smp_passkey = ((uint32_t)k[0] | (uint32_t)k[1] << 8 | (uint32_t)k[2] << 16 | (uint32_t)k[3] << 24) % 1000000u;
|
||||
ble_smp_passkey_tk(smp_passkey, bsmp.tk);
|
||||
bsmp.auth = 1;
|
||||
BLE_DG(ble_dgc.si_passkey++);
|
||||
}
|
||||
ble_hw_rand(bsmp.mrand, 16);
|
||||
c[0] = SMP_CONFIRM;
|
||||
smp_c1(bsmp.mrand, c + 1);
|
||||
smp_init_send(c, 17);
|
||||
bsmp.st = I_CONFIRM;
|
||||
}
|
||||
|
||||
static void smp_init_confirm(const uint8_t *p)
|
||||
{
|
||||
uint8_t c[17];
|
||||
ble_cpy(bsmp.mconf, p + 1, 16); /* (the responder's Sconfirm: its user typed the passkey) */
|
||||
c[0] = SMP_RANDOM;
|
||||
ble_cpy(c + 1, bsmp.mrand, 16);
|
||||
smp_init_send(c, 17);
|
||||
bsmp.st = I_RANDOM;
|
||||
}
|
||||
|
||||
static void smp_init_random(const uint8_t *p)
|
||||
{
|
||||
static const uint8_t zero[8] = {0};
|
||||
uint8_t c[16];
|
||||
smp_c1(p + 1, c);
|
||||
if (!ble_eq(c, bsmp.mconf, 16)) { /* (a passkey typed wrong ends here) */
|
||||
smp_init_fail(SMP_E_CONFIRM, 1);
|
||||
return;
|
||||
}
|
||||
smp_passkey = BLE_NO_PASSKEY;
|
||||
BLE_DG(ble_dgc.si_confirm_ok++);
|
||||
ble_smp_s1(bsmp.tk, p + 1, bsmp.mrand, bsmp.stk); /* STK = s1(TK, Srand, Mrand) */
|
||||
smp_cut(bsmp.stk);
|
||||
bsmp.st = I_ENC;
|
||||
BLE_DG(ble_dgc.si_stk_enc++);
|
||||
ble_ll_start_enc(bsmp.stk, zero, 0);
|
||||
}
|
||||
|
||||
/* phase 3, after theirs: our keys as agreed (none of them is ever used by us: a peripheral cannot start encryption,
|
||||
* and we never use a private address), then done */
|
||||
static void smp_init_our_keys(void)
|
||||
{
|
||||
uint8_t e[17], m[11];
|
||||
if (bsmp.ours & SMP_KD_ENC) {
|
||||
e[0] = SMP_ENC_INFO;
|
||||
ble_hw_rand(e + 1, 16);
|
||||
smp_cut(e + 1);
|
||||
m[0] = SMP_MASTER_ID;
|
||||
ble_hw_rand(m + 1, 10);
|
||||
smp_send(e, 17);
|
||||
smp_send(m, 11);
|
||||
BLE_DG(ble_dgc.si_keys_tx += 2);
|
||||
}
|
||||
if (bsmp.ours & SMP_KD_ID) {
|
||||
uint8_t own[6], peer[6], t = ble_ll_addrs(own, peer);
|
||||
e[0] = SMP_ID_INFO;
|
||||
smp_own_irk(e + 1);
|
||||
m[0] = SMP_ID_ADDR;
|
||||
m[1] = (uint8_t)(t & 1u);
|
||||
ble_cpy(m + 2, own, 6);
|
||||
smp_send(e, 17);
|
||||
smp_send(m, 8);
|
||||
BLE_DG(ble_dgc.si_keys_tx += 2);
|
||||
}
|
||||
bsmp.ours = 0;
|
||||
bsmp.st = S_IDLE;
|
||||
BLE_DG(ble_dgc.si_done++);
|
||||
if (bsmp.auth)
|
||||
BLE_DG(ble_dgc.si_auth_done++);
|
||||
if (bsmp.bond && bsmp.keys.has) {
|
||||
if (bsmp.auth)
|
||||
bsmp.keys.has |= BLE_KEYS_AUTH;
|
||||
ble_app_central_keys(&bsmp.keys);
|
||||
}
|
||||
ble_central_paired(1, 0, bsmp.auth);
|
||||
}
|
||||
|
||||
static void smp_init_keys_if_done(void)
|
||||
{
|
||||
if (bsmp.st == I_KEYS && !(bsmp.theirs & (SMP_KD_ENC | SMP_KD_ID | SMP_KD_SIGN)))
|
||||
smp_init_our_keys();
|
||||
}
|
||||
|
||||
/* the link encrypted (ble_host_encrypted, as master): with our STK -> the key distribution; with a bond's LTK -> the
|
||||
* host carries on */
|
||||
static void smp_init_encrypted(void)
|
||||
{
|
||||
if (bsmp.init && bsmp.st == I_ENC) {
|
||||
bsmp.st = I_KEYS;
|
||||
smp_init_keys_if_done();
|
||||
return;
|
||||
}
|
||||
BLE_DG(ble_dgc.si_ltk_enc++);
|
||||
ble_central_encrypted();
|
||||
}
|
||||
|
||||
static void smp_init_their_key(const uint8_t *p)
|
||||
{
|
||||
uint8_t op = p[0];
|
||||
BLE_DG(ble_dgc.si_keys_rx++);
|
||||
switch (op) {
|
||||
case SMP_ENC_INFO:
|
||||
ble_cpy(bsmp.keys.ltk, p + 1, 16);
|
||||
return;
|
||||
case SMP_MASTER_ID:
|
||||
bsmp.keys.ediv = ble_rd16(p + 1);
|
||||
ble_cpy(bsmp.keys.rand, p + 3, 8);
|
||||
bsmp.keys.has |= BLE_KEYS_LTK;
|
||||
bsmp.theirs &= (uint8_t)~SMP_KD_ENC;
|
||||
break;
|
||||
case SMP_ID_INFO:
|
||||
ble_cpy(bsmp.keys.irk, p + 1, 16);
|
||||
return;
|
||||
case SMP_ID_ADDR:
|
||||
ble_cpy(bsmp.keys.id, p + 2, 6);
|
||||
bsmp.keys.id_rand = (uint8_t)(p[1] & 1u);
|
||||
bsmp.keys.has |= BLE_KEYS_ID;
|
||||
bsmp.theirs &= (uint8_t)~SMP_KD_ID;
|
||||
break;
|
||||
default: /* (Signing Information: not kept) */
|
||||
bsmp.theirs &= (uint8_t)~SMP_KD_SIGN;
|
||||
break;
|
||||
}
|
||||
smp_init_keys_if_done();
|
||||
}
|
||||
|
||||
/* an SMP PDU from the responder */
|
||||
static void smp_init_rx(const uint8_t *p, uint16_t n)
|
||||
{
|
||||
static const uint8_t LEN[12] = {0, 0, 7, 17, 17, 2, 17, 11, 17, 8, 17, 2};
|
||||
uint8_t op = p[0], want;
|
||||
if (op >= sizeof LEN || !LEN[op]) { /* a Pairing Request (a responder never sends it), SC's PDUs */
|
||||
smp_fail(SMP_E_CMD);
|
||||
return;
|
||||
}
|
||||
if (n != LEN[op]) {
|
||||
if (smp_init_busy())
|
||||
smp_init_fail(SMP_E_INVALID, 1);
|
||||
return;
|
||||
}
|
||||
switch (op) {
|
||||
case SMP_SEC_REQ: /* the peripheral asks for security: encrypt with the bond, or pair */
|
||||
BLE_DG(ble_dgc.si_sec_req_rx++);
|
||||
if (!smp_init_busy())
|
||||
ble_central_sec_req(p[1]);
|
||||
return;
|
||||
case SMP_FAILED:
|
||||
BLE_DG(ble_dgc.si_fail_rx++);
|
||||
BLE_DG(ble_dgc.si_last_fail = p[1]);
|
||||
if (ble_central_late_fail(p[1], !smp_init_busy() ? 0 : bsmp.st == I_RSP ? 1 : 2))
|
||||
BLE_DG(ble_dgc.si_fail_late++); /* (after the pairing: iOS did so after a Just Works one, §13.9) */
|
||||
else if (smp_init_busy())
|
||||
smp_init_fail(p[1], 0);
|
||||
return;
|
||||
case SMP_PAIR_RSP:
|
||||
case SMP_CONFIRM:
|
||||
case SMP_RANDOM:
|
||||
want = op == SMP_PAIR_RSP ? I_RSP : op == SMP_CONFIRM ? I_CONFIRM : I_RANDOM;
|
||||
if (!bsmp.init || bsmp.st != want) {
|
||||
if (smp_init_busy())
|
||||
smp_init_fail(SMP_E_UNSPEC, 1);
|
||||
return;
|
||||
}
|
||||
if (op == SMP_PAIR_RSP)
|
||||
smp_init_rsp(p);
|
||||
else if (op == SMP_CONFIRM)
|
||||
smp_init_confirm(p);
|
||||
else
|
||||
smp_init_random(p);
|
||||
return;
|
||||
default: /* the responder's keys (phase 3, encrypted) */
|
||||
if (bsmp.init && bsmp.st == I_KEYS && ble_ll_encrypted())
|
||||
smp_init_their_key(p);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* a pairing request on an encrypted link (ble_central.c: with MITM after Just Works) left unanswered this long: the
|
||||
* pairing is dropped (the host makes a new link for it) -> 1 */
|
||||
#define SMP_RSP_LATE_US 5000000u
|
||||
static int smp_init_rsp_late(uint32_t now)
|
||||
{
|
||||
if (!bsmp.init || bsmp.st != I_RSP || now - bsmp.t0 <= SMP_RSP_LATE_US)
|
||||
return 0;
|
||||
bsmp.st = S_IDLE;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* the 30 s SMP timeout (3.4): no more SMP on this link; the host ends it */
|
||||
static int smp_init_timed_out(uint32_t now)
|
||||
{
|
||||
if (!smp_init_busy() || now - bsmp.t0 <= BLE_GATTC_TIMEOUT_US)
|
||||
return 0;
|
||||
bsmp.st = S_IDLE;
|
||||
smp_passkey = BLE_NO_PASSKEY;
|
||||
return 1;
|
||||
}
|
||||
|
||||
BLE_API uint32_t ble_central_passkey(void)
|
||||
{
|
||||
return bsmp.init && smp_init_busy() ? smp_passkey : BLE_NO_PASSKEY;
|
||||
}
|
||||
+18
-10
@@ -18,8 +18,8 @@
|
||||
* bt_seg_start(addr) the first ON: the vectors (IRQ 45 / 29 at priority 2, below the audio and TIMER5), the radio's
|
||||
* start-up and calibration, the stack, advertising. The caller has the interrupts off, the
|
||||
* audio buffer silent, the second core parked
|
||||
* bt_seg_enable(on) advertising on again / off (and any link closed); the radio stays set up until power-off
|
||||
* bt_seg_status() bit 0 a central connected, bit 1 its MIDI notifications on, [15:8] connections since start
|
||||
* bt_seg_enable(on) advertising on again / off (and any link closed); (V10.35) 2: AIR, scanning only; the radio stays set up until power-off
|
||||
* bt_seg_status() bit 0 a central connected, bit 1 its MIDI notifications on, (V10.35) bit 2 AIR scanning, [15:8] connections since start
|
||||
* bt_seg_rssi(w, rd) (V10.14) the RSSI words of the good packets received since the last call (PROX)
|
||||
* (V10.13) MIDI both ways (bt_midi.h): what a central writes, decoded by the stack (ble_midi.c), goes into the app's MIDI
|
||||
* input ring as USB's does (bt_midi_accept, then the app's bt_midi_rx: src/bt.c); MIDI OUT's copy (the app's bt_oq, filled
|
||||
@@ -39,6 +39,10 @@
|
||||
#define BLE_DIAG 0 /* (no counters, no console: as Melodee) */
|
||||
#define FELUCCA_BLE_BOND 1 /* (V10.32) Just Works pairing with bonding, LL encryption: a central that asks
|
||||
* to pair (Windows, some Macs and Androids) pairs, no PIN */
|
||||
#define FELUCCA_BLE_CENTRAL 3 /* (V10.35) AIR: the scanner alone (no connecting out): every advertisement's
|
||||
* RSSI to PROX (bs_rssi), nothing listed */
|
||||
#define BLE_SCAN_RING 1u /* (the list unused: AIR reads bs_rssi) */
|
||||
#define BLE_SCAN_ACTIVE 0 /* (listens only: nothing sent) */
|
||||
#define BLE_LTK_DERIVE 1 /* .. the bond's key made again from EDIV / Rand (ble_smp.c ble_smp_ltk_of):
|
||||
* nothing kept, every bonded central back after a power-off */
|
||||
#include "fm1_cc.h"
|
||||
@@ -47,13 +51,13 @@
|
||||
#include "fm1_guard.h"
|
||||
/* (V10.14) PROX: the RSSI word of every good packet on the link (ble_hw_wl82.c hw_rx_service, the RX interrupt: one a
|
||||
* connection event, 89..133 a second), kept in a ring the app reads (bt_seg_rssi; src/bt.c filters it) */
|
||||
static volatile uint16_t bs_rq[16][4];
|
||||
static volatile uint16_t bs_rq[8][4]; /* (V10.35: 8, was 16: the pool for AIR's scanner) */
|
||||
static volatile uint32_t bs_rw;
|
||||
/* (V10.18) all four RSSI words of the packet (RSSI2 / RSSI3 by the buffer first, as V10.14 read it, then the other, then
|
||||
* RSSI0, RSSI1): which one follows the distance is found by the app (src/bt.c bt_prox_tick) */
|
||||
static void bs_rssi(const volatile uint16_t *r, uint32_t b)
|
||||
{
|
||||
volatile uint16_t *q = bs_rq[bs_rw & 15u];
|
||||
volatile uint16_t *q = bs_rq[bs_rw & 7u];
|
||||
q[0] = r[2u + (b & 1u)];
|
||||
q[1] = r[3u - (b & 1u)];
|
||||
q[2] = r[0];
|
||||
@@ -150,15 +154,19 @@ void bt_seg_enable(int on)
|
||||
{
|
||||
fm1_ble_irqs_hold(1);
|
||||
fm1_ble_step(on ? FM1_BLE_STEP_ENABLE : FM1_BLE_STEP_SET_OFF);
|
||||
ble_enable(on);
|
||||
if (!on)
|
||||
if (on != 1)
|
||||
ble_enable(0); /* (V10.35) off, or AIR: the advertising / the scan stops first */
|
||||
ble_ll_scan(on == 2); /* AIR (2): the scanner, nothing advertised */
|
||||
ble_enable(on != 0);
|
||||
if (on != 1)
|
||||
bs_linked = bt_oq.on = 0;
|
||||
fm1_ble_irqs_hold(0);
|
||||
}
|
||||
|
||||
uint32_t bt_seg_status(void)
|
||||
{
|
||||
return (uint32_t)ble_connected() | (uint32_t)ble_midi_ready() << 1 | (uint32_t)bs_conns << 8;
|
||||
return (uint32_t)ble_connected() | (uint32_t)ble_midi_ready() << 1 | (uint32_t)ble_ll_scanning() << 2 |
|
||||
(uint32_t)bs_conns << 8;
|
||||
}
|
||||
|
||||
/* (V10.19) PROX's diagnostics: [15:0] the packets the RSSI hook saw, [31:16] the good packets the link received */
|
||||
@@ -169,11 +177,11 @@ uint32_t bt_seg_rssi(uint16_t *w, uint32_t *rd)
|
||||
{
|
||||
uint32_t e = bs_rw, n = e - *rd, i, j;
|
||||
RING_PUBLISH();
|
||||
if (n > 16u)
|
||||
n = 16u;
|
||||
if (n > 8u)
|
||||
n = 8u;
|
||||
for (i = 0; i < n; i++)
|
||||
for (j = 0; j < 4u; j++)
|
||||
w[4u * i + j] = bs_rq[(e - n + i) & 15u][j];
|
||||
w[4u * i + j] = bs_rq[(e - n + i) & 7u][j];
|
||||
*rd = e;
|
||||
return n;
|
||||
}
|
||||
+11
-5
@@ -64,7 +64,9 @@ BTAPP_FN void bt_midi_rx(uint32_t pkt)
|
||||
fm1_irq_on();
|
||||
}
|
||||
#endif
|
||||
static uint32_t bt_get(void) { return BT_ROW[0] == BT_MARK && (BT_ROW[1] & 1u); }
|
||||
/* 0 OFF, 1 ON, (V10.35) 2 AIR: [25] bit 0 the radio on, bit 1 AIR (the scanner: no MIDI, every advertisement heard
|
||||
* around feeds PROX) */
|
||||
static uint32_t bt_get(void) { return BT_ROW[0] == BT_MARK && (BT_ROW[1] & 1u) ? 1u + (BT_ROW[1] >> 1 & 1u) : 0u; }
|
||||
static void bt_set(uint32_t on)
|
||||
{
|
||||
uint8_t *r = BT_ROW;
|
||||
@@ -72,13 +74,13 @@ static void bt_set(uint32_t on)
|
||||
memset(r, 0, 8);
|
||||
r[0] = BT_MARK;
|
||||
}
|
||||
r[1] = (uint8_t)((r[1] & ~1u) | (on & 1u));
|
||||
r[1] = (uint8_t)((r[1] & ~3u) | (on ? 1u | (on == 2u) << 1 : 0u));
|
||||
}
|
||||
static void bt_start(void)
|
||||
{
|
||||
int rc;
|
||||
if (bt_up == BT_RUN) {
|
||||
bt_hw_enable(1);
|
||||
bt_hw_enable((int)bt_get());
|
||||
bt_crumb = BT_CR_RUN;
|
||||
return;
|
||||
}
|
||||
@@ -87,6 +89,8 @@ static void bt_start(void)
|
||||
bt_crumb = BT_CR_START;
|
||||
rc = bt_hw_start(BT_ROW + 2);
|
||||
bt_crumb = rc ? 0u : BT_CR_RUN;
|
||||
if (!rc && bt_get() == 2u)
|
||||
bt_hw_enable(2); /* (V10.35) AIR: the scanner instead of the advertising */
|
||||
bt_up = (uint8_t)(rc == 2 ? BT_NOSEG : rc ? BT_FAIL : BT_RUN);
|
||||
if (rc == 1) { /* (the start-up returned unfinished: OFF, not again at power-on) */
|
||||
bt_set(0);
|
||||
@@ -116,7 +120,9 @@ static const char *bt_vname(uint32_t v)
|
||||
return "OFF";
|
||||
if (bt_up != BT_RUN)
|
||||
return bt_up == BT_NOSEG ? "NO SEG" : bt_up == BT_FAIL ? "FAILED" : "ON";
|
||||
str_cpy(bt_val, st & 1u ? "CONN" : "ADV", sizeof bt_val);
|
||||
str_cpy(bt_val, v == 2u ? "AIR" : st & 1u ? "CONN" : "ADV", sizeof bt_val);
|
||||
if (v == 2u)
|
||||
return bt_val;
|
||||
if (n) {
|
||||
str_cpy(bt_val + str_len(bt_val), " ", 2);
|
||||
fmt_int(bt_val + str_len(bt_val), (int32_t)n);
|
||||
@@ -229,7 +235,7 @@ BTAPP_FN static void bt_prox_tick(void)
|
||||
{
|
||||
uint16_t w[64];
|
||||
uint32_t n = bt_hw_rssi(w, &bp.rd);
|
||||
bt_prox_feed(w, n, bt_get() && (bt_hw_status() & 1u), fm1_ms);
|
||||
bt_prox_feed(w, n, bt_get() && (bt_hw_status() & 5u), fm1_ms); /* (a central's link, or AIR's scan) */
|
||||
}
|
||||
static uint32_t bt_prox_ok(void) { return bt_up == BT_RUN && bt_get(); } /* (the PROX page opens: ui_menu.c) */
|
||||
static void bt_tick(void) /* main loop: the saved ON, once the music is stopped */
|
||||
|
||||
@@ -41,7 +41,7 @@
|
||||
#define FELUCCA_ID "FM-1_900" /* package identity (build.py: the .fwsc marker string) */
|
||||
#endif
|
||||
#ifndef FELUCCA_VERSION
|
||||
#define FELUCCA_VERSION "V10.34" /* (2026-11-02) V10.34: DRUM-X kit HIP HOP (a user's ask: boom bap, a round kick with a knock, a fat cracking snare, a tight clap, dusty band-passed hats, a woody rim, a shaker in the BELL's place) and the pattern BOOM BAP it suggests (SEQ > PATTERNS 14). (2026-11-02) V10.33: the MIDI clock steady. Out (MIDI OUT CLOCK / ALL): each of the 24 pulses at its own sample, sent at its own time (USB: usb.c ep1_tx holds it to its TIMER4 tick; Bluetooth: stamped with it), no longer a burst each 2.9 ms render, and no rounding in the pulse length (a follower read 105.1 for 104). In (CLK USB / TRS, Bluetooth too): the tempo over two beats, smoothed, a whole BPM held within 0.6 (it was six pulses raw: 103 105 104..), the interpolation at the steady tempo. (2026-11-02) V10.32: Bluetooth MIDI pairs: Just Works (no PIN) with bonding and the link encrypted when a central asks (Windows, some Macs and Androids refused "Pairing Not Supported" before); the bond's key made again from EDIV / Rand (bt/ble ble_smp_ltk_of), nothing stored, every bonded computer back after a power-off; Optimist's AES / CCM (ble_aes.c, fm1-base 4039a8b) in the Bluetooth segment (51 KB of its 72). (2026-11-02) V10.31: EUCLID reversible: the sequence it took is kept whole (the last track taken, 1.5 KB in RAMTEXT's room: notes, chords, velocities, components, micro), put back when its settings are at rest again (VARY and MODES 0, its own rhythm) or HITS / DENSITY go to 0; another track's taken since: its rhythm and notes come back from what EUCLID took. SCL > SCENE kept with the project (FUNC 'S' 'C': ON and the six roles; none: OFF, all KEEP). (2026-11-02) V10.30: flash by hand: a parameter card in one place (pf_column: eleven cards of ui_draw.c), "a/n" in one place (fmt_frac: eight); every screen pixel for pixel as before (9250 renders hashed): ~0.6 KB more free. (2026-11-02) V10.29: flash: the files added since 0.9.7 (EUCLID, SCENE, LIVE, MIDI LEARN, the song player, PERFORM, PLAYHEADS, SLICER) and the engines' main-loop side built for size (tools/size_fns.py; what the interrupts reach keeps -Os): ~1.8 KB more free, the sound as it was. (2026-11-02) V10.28: EUCLID starts at 0 (no HITS, no DENSITY, MODES and VARY 0: built from nothing); a sequence the track has (recorded, edited, a song's; not a factory pattern) is transformed: its rhythm and holds until HITS STEPS or ROTATE are turned, its notes always (a kit: its lanes); VARY in two halves: up to 50 the one cycle moved, each value its own (one hit at least), above 50 the four turns A A' A'' B. (2026-11-02) V10.27: testers' fixes: the CHORD page no longer writes VOICING LEAD's last chord (a key played another inversion after the page was shown: chord_peek); MENU: KNOB k edits its card's row (all four edited the selected row); TAPE WOW wobbles like a warped record (a smoothed random drift, ~+-45 cents at 100; its noise source was stuck at 0: the hiss and drift were dead); slower: LFO RATE 8BAR 16BAR, PLAYHEADS 1/8 1/16, STRUM past 64 up to ~1 s. (2026-11-02) V10.26: SCENE starts with every track KEEP (OFF, as before); OCT+ on SCENE dices the roles (each role one track at most: never two doing the same, never two playing chords), OCT- all KEEP; turned ON with none set, it dices; a role picked by hand that another track has sends that one to KEEP. (2026-11-02) V10.25: SEQ > STRETCH's NUDGE in two modes: OCT- the whole sequence a step (blue, as before), OCT+ the cursor step's MICRO timing (red, 64ths of a step; PRESETS moves the cursor), MICRO back on a knob (a tester's ask). (2026-11-02) V10.24: the user's picks from the redesign mock: SEQ > EUCLID the lanes as concentric rings (heat colours, VARY's turns under the ring); SEQ > MUTATE the anchor above, the pattern playing below (changed steps lit), an arc to the next mutation; SAVE > PRESETS two columns (the engines, their presets), OCT+ keeps the sound browsed to, OCT- brings back the one before; SAVE > TOOLS by scope (KNOB 1 TRACK SOUND SONG SYSTEM, KNOB 2 the action), OCT+ held a second does it (no dialog); SCL > SCENE, the multitimbral mode: one key plays every track given a role (KEEP CHORD ARP BASS LEAD; the kit keeps its sequence), REC records each into its own steps. (2026-11-01) V10.23: STRING FDBK heard (its BLOOM level was applied twice, -15 dB); EXCITE into / out of FDBK keeps each side's EDIT 2 knobs (GAIN no longer starts at STRETCH's 0), FDBK's defaults audible. (2026-11-01) V10.22: testers' asks: ENV > ENV 2 (a second ADSR, MOD SRC ENV2, any DEST), FILTER TYPE LP24 (4 poles), ANALOG WAVE PLS3 (three pulses, SHAPE the width). (2026-11-01) V10.21: EUCLID's lane rows named right (they were one off: ALL's name on BD's row); the ring shows one cycle (the lane's, ALL's, the melody's) and the turn playing, its playhead going round; PROX's page says the phone, not the hand. (2026-11-01) V10.20: PROX smooth: the RSSI reading fixed on word 0 as the level (V10.18's widest range picked a noisy byte: a random sample anLine truncated
|
||||
#define FELUCCA_VERSION "V10.35" /* (2026-11-02) V10.35: MENU > SYSTEM > BLUETOOTH AIR, an experiment: the radio listens instead of advertising (passive scan, nothing sent, no MIDI) and every Bluetooth packet heard around (phones, watches, headphones, TVs) feeds PROX: a body near the antenna shadows them, PROX's page and the MS_PROX source as with a phone (Optimist's scanner, fm1-base 4039a8b, minimal central level 3, in the segment: 56 KB of 72). (2026-11-02) V10.34: DRUM-X kit HIP HOP (a user's ask: boom bap, a round kick with a knock, a fat cracking snare, a tight clap, dusty band-passed hats, a woody rim, a shaker in the BELL's place) and the pattern BOOM BAP it suggests (SEQ > PATTERNS 14). (2026-11-02) V10.33: the MIDI clock steady. Out (MIDI OUT CLOCK / ALL): each of the 24 pulses at its own sample, sent at its own time (USB: usb.c ep1_tx holds it to its TIMER4 tick; Bluetooth: stamped with it), no longer a burst each 2.9 ms render, and no rounding in the pulse length (a follower read 105.1 for 104). In (CLK USB / TRS, Bluetooth too): the tempo over two beats, smoothed, a whole BPM held within 0.6 (it was six pulses raw: 103 105 104..), the interpolation at the steady tempo. (2026-11-02) V10.32: Bluetooth MIDI pairs: Just Works (no PIN) with bonding and the link encrypted when a central asks (Windows, some Macs and Androids refused "Pairing Not Supported" before); the bond's key made again from EDIV / Rand (bt/ble ble_smp_ltk_of), nothing stored, every bonded computer back after a power-off; Optimist's AES / CCM (ble_aes.c, fm1-base 4039a8b) in the Bluetooth segment (51 KB of its 72). (2026-11-02) V10.31: EUCLID reversible: the sequence it took is kept whole (the last track taken, 1.5 KB in RAMTEXT's room: notes, chords, velocities, components, micro), put back when its settings are at rest again (VARY and MODES 0, its own rhythm) or HITS / DENSITY go to 0; another track's taken since: its rhythm and notes come back from what EUCLID took. SCL > SCENE kept with the project (FUNC 'S' 'C': ON and the six roles; none: OFF, all KEEP). (2026-11-02) V10.30: flash by hand: a parameter card in one place (pf_column: eleven cards of ui_draw.c), "a/n" in one place (fmt_frac: eight); every screen pixel for pixel as before (9250 renders hashed): ~0.6 KB more free. (2026-11-02) V10.29: flash: the files added since 0.9.7 (EUCLID, SCENE, LIVE, MIDI LEARN, the song player, PERFORM, PLAYHEADS, SLICER) and the engines' main-loop side built for size (tools/size_fns.py; what the interrupts reach keeps -Os): ~1.8 KB more free, the sound as it was. (2026-11-02) V10.28: EUCLID starts at 0 (no HITS, no DENSITY, MODES and VARY 0: built from nothing); a sequence the track has (recorded, edited, a song's; not a factory pattern) is transformed: its rhythm and holds until HITS STEPS or ROTATE are turned, its notes always (a kit: its lanes); VARY in two halves: up to 50 the one cycle moved, each value its own (one hit at least), above 50 the four turns A A' A'' B. (2026-11-02) V10.27: testers' fixes: the CHORD page no longer writes VOICING LEAD's last chord (a key played another inversion after the page was shown: chord_peek); MENU: KNOB k edits its card's row (all four edited the selected row); TAPE WOW wobbles like a warped record (a smoothed random drift, ~+-45 cents at 100; its noise source was stuck at 0: the hiss and drift were dead); slower: LFO RATE 8BAR 16BAR, PLAYHEADS 1/8 1/16, STRUM past 64 up to ~1 s. (2026-11-02) V10.26: SCENE starts with every track KEEP (OFF, as before); OCT+ on SCENE dices the roles (each role one track at most: never two doing the same, never two playing chords), OCT- all KEEP; turned ON with none set, it dices; a role picked by hand that another track has sends that one to KEEP. (2026-11-02) V10.25: SEQ > STRETCH's NUDGE in two modes: OCT- the whole sequence a step (blue, as before), OCT+ the cursor step's MICRO timing (red, 64ths of a step; PRESETS moves the cursor), MICRO back on a knob (a tester's ask). (2026-11-02) V10.24: the user's picks from the redesign mock: SEQ > EUCLID the lanes as concentric rings (heat colours, VARY's turns under the ring); SEQ > MUTATE the anchor above, the pattern playing below (changed steps lit), an arc to the next mutation; SAVE > PRESETS two columns (the engines, their presets), OCT+ keeps the sound browsed to, OCT- brings back the one before; SAVE > TOOLS by scope (KNOB 1 TRACK SOUND SONG SYSTEM, KNOB 2 the action), OCT+ held a second does it (no dialog); SCL > SCENE, the multitimbral mode: one key plays every track given a role (KEEP CHORD ARP BASS LEAD; the kit keeps its sequence), REC records each into its own steps. (2026-11-01) V10.23: STRING FDBK heard (its BLOOM level was applied twice, -15 dB); EXCITE into / out of FDBK keeps each side's EDIT 2 knobs (GAIN no longer starts at STRETCH's 0), FDBK's defaults audible. (2026-11-01) V10.22: testers' asks: ENV > ENV 2 (a second ADSR, MOD SRC ENV2, any DEST), FILTER TYPE LP24 (4 poles), ANALOG WAVE PLS3 (threLine truncated
|
||||
#endif
|
||||
#if FELUCCA_OTA && !FELUCCA_FLASH
|
||||
#error "FELUCCA_OTA needs FELUCCA_FLASH"
|
||||
|
||||
@@ -160,7 +160,7 @@ static const menu_flag_t *menu_flag(uint32_t row)
|
||||
/* a row's value as 0..menu_n(row) - 1 in the order the menu steps it (LEDS: OFF DIM LO DIM HI INV, LEDS_MENU) */
|
||||
static uint32_t menu_n(uint32_t row)
|
||||
{
|
||||
return row == MI_COLOR ? NPALETTES : row == MI_SKIN ? SKIN_N : row == MI_LOWCUT || (row >= MI_CLICK && row <= MI_COUNTIN) || row == MI_LOAD ? 3u :
|
||||
return row == MI_COLOR ? NPALETTES : row == MI_SKIN ? SKIN_N : row == MI_LOWCUT || (row >= MI_CLICK && row <= MI_COUNTIN) || row == MI_LOAD || row == MI_BT ? 3u :
|
||||
row == MI_HOLD || row == MI_MIDIOUT || row == MI_LEARN ? 4u : row == MI_MIDIMAP ? MLM_N : row == MI_USB ? 6u : row == MI_SERIAL ? 5u : row == MI_LEDS ? LEDS_COUNT : row == MI_SCROFF ? NELEM(SCROFF_N) : 2u;
|
||||
}
|
||||
static uint32_t menu_get(uint32_t row)
|
||||
@@ -267,7 +267,7 @@ static void menu_put(uint32_t row, uint32_t v)
|
||||
case MI_LEARN: ml_set(v); break; /* (p1: armed, or cleared, at the MENU's close; not saved) */
|
||||
case MI_CPU: cpu_put(0, v & 1u); break; /* (p1: at the next power-on, main.c) */
|
||||
case MI_CLOCK: cpu_put(1, v & 1u); break; /* (0.9.9-stab: at the next power-on, main.c cpu_clock) */
|
||||
case MI_BT: bt_put(v & 1u); break; /* (V10.12: at once, bt.c) */
|
||||
case MI_BT: bt_put(v % 3u); break; /* (V10.12: at once, bt.c; V10.35 2 AIR) */
|
||||
}
|
||||
}
|
||||
/* the menu's step, any of KNOB 1..4 or OCT+ (s > 0) / OCT- (s < 0): the next / previous value, stopping at the ends;
|
||||
|
||||
+6
-2
@@ -2882,7 +2882,7 @@ static int test_menu_tabs(void)
|
||||
memset(&favorites.factory[6][24], 0, 8);
|
||||
bt_up = 0;
|
||||
ok = MI_TAB[MI_BT] == MTAB_SYSTEM && MI_BT < MI_VALUES && str_eq(MI_NAME[MI_BT], "BLUETOOTH") && menu_get(MI_BT) == 0u &&
|
||||
str_eq(menu_vname(MI_BT, 0), "OFF") && menu_n(MI_BT) == 2u;
|
||||
str_eq(menu_vname(MI_BT, 0), "OFF") && menu_n(MI_BT) == 3u;
|
||||
song.playing = 1;
|
||||
menu_put(MI_BT, 1u);
|
||||
ok &= menu_get(MI_BT) == 0u && msg_is("STOP FIRST");
|
||||
@@ -2890,6 +2890,10 @@ static int test_menu_tabs(void)
|
||||
menu_put(MI_BT, 1u);
|
||||
ok &= menu_get(MI_BT) == 1u && favorites.factory[6][24] == BT_MARK && (favorites.factory[6][25] & 1u) &&
|
||||
str_eq(menu_vname(MI_BT, 1), "NO SEG") && bt_crumb == 0u;
|
||||
menu_put(MI_BT, 2u); /* (V10.35) AIR: its own bit, ON kept with it */
|
||||
ok &= menu_get(MI_BT) == 2u && (favorites.factory[6][25] & 3u) == 3u;
|
||||
menu_put(MI_BT, 1u);
|
||||
ok &= menu_get(MI_BT) == 1u && (favorites.factory[6][25] & 3u) == 1u;
|
||||
menu_put(MI_BT, 0u);
|
||||
ok &= menu_get(MI_BT) == 0u && str_eq(menu_vname(MI_BT, 0), "OFF");
|
||||
menu_put(MI_BT, 1u);
|
||||
@@ -2906,7 +2910,7 @@ static int test_menu_tabs(void)
|
||||
memset(&favorites.factory[6][24], 0, 8);
|
||||
bt_up = 0;
|
||||
bad += check("V10.12 MENU > SYSTEM > BLUETOOTH: OFF by default, STOP FIRST while playing, ON saved with its mark (NO SEG "
|
||||
"without the segment), OFF; the boot guard after a reset in the start or a crash while it ran", ok);
|
||||
"without the segment), V10.35 AIR its own bit, OFF; the boot guard after a reset in the start or a crash while it ran", ok);
|
||||
|
||||
/* (V10.13) ABOUT's texts kept in the segment (ui_menu.c ABOUT_PROJECT, ABOUT_CREDITS): each line a kind octet
|
||||
* (1..12) and a text, the list ended; every credit and the licences there (the host: all in one image) */
|
||||
|
||||
+1412
-1412
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user