Prefix ll with ble_. This includes filenames
Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/commit/36623dd5 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/36623dd5 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/36623dd5 Branch: refs/heads/master Commit: 36623dd5f5cdb07f60f72e3b7d995cc854b87b4e Parents: a666bf8 Author: Willam San Filippo <w...@micosa.io> Authored: Fri Oct 30 15:20:57 2015 -0700 Committer: Willam San Filippo <w...@micosa.io> Committed: Fri Oct 30 15:20:57 2015 -0700 ---------------------------------------------------------------------- .../controller/include/controller/ble_ll.h | 437 +++++++++ .../controller/include/controller/ble_ll_adv.h | 153 ++++ .../include/controller/ble_ll_sched.h | 63 ++ net/nimble/controller/include/controller/ll.h | 437 --------- .../controller/include/controller/ll_adv.h | 153 ---- .../controller/include/controller/ll_sched.h | 63 -- net/nimble/controller/src/ble_ll.c | 655 +++++++++++++ net/nimble/controller/src/ble_ll_adv.c | 912 ++++++++++++++++++ net/nimble/controller/src/ble_ll_hci.c | 336 +++++++ net/nimble/controller/src/ble_ll_scan.c | 22 +- net/nimble/controller/src/ble_ll_sched.c | 237 +++++ net/nimble/controller/src/ll.c | 661 ------------- net/nimble/controller/src/ll_adv.c | 916 ------------------- net/nimble/controller/src/ll_hci.c | 336 ------- net/nimble/controller/src/ll_sched.c | 222 ----- net/nimble/controller/src/phy.c | 6 +- project/bletest/src/main.c | 4 +- 17 files changed, 2809 insertions(+), 2804 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/36623dd5/net/nimble/controller/include/controller/ble_ll.h ---------------------------------------------------------------------- diff --git a/net/nimble/controller/include/controller/ble_ll.h b/net/nimble/controller/include/controller/ble_ll.h new file mode 100644 index 0000000..6d8504c --- /dev/null +++ b/net/nimble/controller/include/controller/ble_ll.h @@ -0,0 +1,437 @@ +/** + * Copyright (c) 2015 Stack Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef H_BLE_LL_ +#define H_BLE_LL_ + +/* XXX: Not sure this should go here, but whatever */ +/* + * ble ll global parameters + * + * NOTES: + * 1) Controller should not change value of supported max tx and rx time and + * octets. + */ +struct ble_ll_global_params +{ + int conn_init_max_tx_octets; + int conn_init_max_tx_time; + int supp_max_tx_octets; + int supp_max_tx_time; + int supp_max_rx_octets; + int supp_max_rx_time; +}; + +struct ble_ll_obj +{ + uint8_t ll_state; + struct os_eventq ll_evq; + struct ble_ll_global_params ll_params; + struct os_event ll_rx_pkt_ev; + STAILQ_HEAD(ll_rxpkt_qh, os_mbuf_pkthdr) ll_rx_pkt_q; +}; + +struct ble_ll_stats +{ + uint32_t rx_crc_ok; + uint32_t rx_crc_fail; + uint32_t rx_bytes; + uint32_t rx_adv_ind; + uint32_t rx_adv_direct_ind; + uint32_t rx_adv_nonconn_ind; + uint32_t rx_scan_reqs; + uint32_t rx_scan_rsps; + uint32_t rx_connect_reqs; + uint32_t rx_scan_ind; + uint32_t rx_unk_pdu; + uint32_t rx_malformed_pkts; + uint32_t hci_cmds; + uint32_t hci_cmd_errs; + uint32_t hci_events_sent; +}; + +extern struct ble_ll_stats g_ble_ll_stats; +extern struct ble_ll_obj g_ble_ll_data; + +/* States */ +#define BLE_LL_STATE_STANDBY (0) +#define BLE_LL_STATE_ADV (1) +#define BLE_LL_STATE_SCANNING (2) +#define BLE_LL_STATE_INITITATING (3) +#define BLE_LL_STATE_CONNECT (4) + +/* BLE LL Task Events */ +#define BLE_LL_EVENT_HCI_CMD (OS_EVENT_T_PERUSER) +#define BLE_LL_EVENT_ADV_TXDONE (OS_EVENT_T_PERUSER + 1) +#define BLE_LL_EVENT_RX_PKT_IN (OS_EVENT_T_PERUSER + 2) +#define BLE_LL_EVENT_SCAN_WIN_END (OS_EVENT_T_PERUSER + 3) + +/* LL Features */ +#define BLE_LL_FEAT_LE_ENCRYPTION (0x01) +#define BLE_LL_FEAT_CONN_PARM_REQ (0x02) +#define BLE_LL_FEAT_EXTENDED_REJ (0x04) +#define BLE_LL_FEAT_SLAVE_INIT (0x08) +#define BLE_LL_FEAT_LE_PING (0x10) +#define BLE_LL_FEAT_DATA_LEN_EXT (0x20) +#define BLE_LL_FEAT_LL_PRIVACY (0x40) +#define BLE_LL_FEAT_EXT_SCAN_FILT (0x80) + +/* LL timing */ +#define BLE_LL_IFS (150) /* usecs */ +#define BLE_CLOCK_DRIFT_ACTIVE (50) /* +/- ppm */ +#define BLE_CLOCK_DRIFT_SLEEP (500) /* +/- ppm */ + +/* + * BLE LL device address. Note that element 0 of the array is the LSB and + * is sent over the air first. Byte 5 is the MSB and is the last one sent over + * the air. + */ +#define BLE_DEV_ADDR_LEN (6) /* bytes */ + +struct ble_dev_addr +{ + uint8_t u8[BLE_DEV_ADDR_LEN]; +}; + +#define BLE_IS_DEV_ADDR_STATIC(addr) ((addr->u8[5] & 0xc0) == 0xc0) +#define BLE_IS_DEV_ADDR_RESOLVABLE(addr) ((addr->u8[5] & 0xc0) == 0x40) +#define BLE_IS_DEV_ADDR_UNRESOLVABLE(addr) ((addr->u8[5] & 0xc0) == 0x00) + +/* + * LL packet format + * + * -> Preamble (1 byte) + * -> Access Address (4 bytes) + * -> PDU (2 to 257 octets) + * -> CRC (3 bytes) + */ +#define BLE_LL_PREAMBLE_LEN (1) +#define BLE_LL_ACC_ADDR_LEN (4) +#define BLE_LL_CRC_LEN (3) +#define BLE_LL_OVERHEAD_LEN \ + (BLE_LL_CRC_LEN + BLE_LL_ACC_ADDR_LEN + BLE_LL_PREAMBLE_LEN) +#define BLE_LL_PDU_HDR_LEN (2) +#define BLE_LL_MIN_PDU_LEN (BLE_LL_PDU_HDR_LEN) +#define BLE_LL_MAX_PDU_LEN (257) +#define BLE_LL_CRCINIT_ADV (0x555555) + +/* Access address for advertising channels */ +#define BLE_ACCESS_ADDR_ADV (0x8E89BED6) + +/* + * Data Channel format + * + * -> Header (2 bytes. + * -> LSB contains llid, nesn, sn and md + * -> MSB contains length (8 bits) + * -> Payload (0 to 251) + * -> MIC (0 or 4 bytes) + */ +#define BLE_LL_DATA_HDR_LLID_MASK (0x03) +#define BLE_LL_DATA_HDR_NESN_MASK (0x04) +#define BLE_LL_DATA_HDR_SN_MASK (0x08) +#define BLE_LL_DATA_HDR_MD_MASK (0x10) +#define BLE_LL_DATA_HDR_RSRVD_MASK (0xE0) + +/* LLID definitions */ +#define BLE_LL_LLID_RSRVD (0) +#define BLE_LL_LLID_DATA_FRAG (1) +#define BLE_LL_LLID_DATA_START (2) +#define BLE_LL_LLID_CTRL (3) + +/* + * LL CTRL PDU format + * -> Opcode (1 byte) + * -> Ctrl data (0 - 26 bytes) + */ +#define BLE_LL_CTRL_CONN_UPDATE_REQ (0) +#define BLE_LL_CTRL_CHANNEL_MAP_REQ (1) +#define BLE_LL_CTRL_TERMINATE_IND (2) +#define BLE_LL_CTRL_ENC_REQ (3) +#define BLE_LL_CTRL_ENC_RSP (4) +#define BLE_LL_CTRL_START_ENC_REQ (5) +#define BLE_LL_CTRL_START_ENC_RSP (6) +#define BLE_LL_CTRL_UNKNOWN_RSP (7) +#define BLE_LL_CTRL_FEATURE_REQ (8) +#define BLE_LL_CTRL_FEATURE_RSP (9) +#define BLE_LL_CTRL_PAUSE_ENC_REQ (10) +#define BLE_LL_CTRL_PAUSE_ENC_RSP (11) +#define BLE_LL_CTRL_VERSION_IND (12) +#define BLE_LL_CTRL_REJECT_IND (13) +#define BLE_LL_CTRL_SLAVE_FEATURE_REQ (14) +#define BLE_LL_CTRL_CONN_PARM_REQ (15) +#define BLE_LL_CTRL_CONN_PARM_RSP (16) +#define BLE_LL_CTRL_REJECT_IND_EXT (17) +#define BLE_LL_CTRL_PING_REQ (18) +#define BLE_LL_CTRL_PING_RSP (19) +#define BLE_LL_CTRL_LENGTH_REQ (20) +#define BLE_LL_CTRL_LENGTH_RSP (21) + +/* LL control connection update request */ +struct ble_ll_conn_upd_req +{ + uint8_t winsize; + uint16_t winoffset; + uint16_t interval; + uint16_t latency; + uint16_t timeout; + uint16_t instant; +}; + +#define BLE_LL_CTRL_CONN_UPD_REQ_LEN (11) + +/* LL control channel map request */ +struct ble_ll_chan_map_req +{ + uint8_t chmap[5]; + uint16_t instant; +}; + +#define BLE_LL_CTRL_CHAN_MAP_LEN (7) + +/* + * LL control terminate ind + * -> error code (1 byte) + */ +#define BLE_LL_CTRL_TERMINATE_IND_LEN (1) + +/* LL control enc req */ +struct ble_ll_enc_req +{ + uint8_t rand[8]; + uint16_t ediv; + uint8_t skdm[8]; + uint32_t ivm; +}; + +#define BLE_LL_CTRL_ENC_REQ_LEN (22) + +/* LL control enc rsp */ +struct ble_ll_enc_rsp +{ + uint8_t skds[8]; + uint32_t ivs; +}; + +#define BLE_LL_CTRL_ENC_RSP_LEN (12) + +/* LL control start enc req and start enc rsp have no data */ +#define BLE_LL_CTRL_START_ENC_LEN (0) + +/* + * LL control unknown response + * -> 1 byte which contains the unknown or un-supported opcode. + */ +#define BLE_LL_CTRL_UNK_RSP_LEN (1) + +/* + * LL control feature req and LL control feature rsp + * -> 8 bytes of data containing features supported by device. + */ +#define BLE_LL_CTRL_FEATURE_LEN (8) + +/* LL control pause enc req and pause enc rsp have no data */ +#define BLE_LL_CTRL_PAUSE_ENC_LEN (0) + +/* + * LL control version ind + * -> version (1 byte): + * Contains the version number of the bluetooth controller specification. + * -> comp_id (2 bytes) + * Contains the company identifier of the manufacturer of the controller. + * -> sub_ver_num: Contains a unique value for implementation or revision of + * the bluetooth controller. + */ +struct ble_ll_version_ind +{ + uint8_t ble_ctrlr_ver; + uint16_t company_id; + uint16_t sub_ver_num; +}; + +#define BLE_LL_CTRL_VERSION_IND_LEN (5) + +/* + * LL control reject ind + * -> error code (1 byte): contains reason why request was rejected. + */ +#define BLE_LL_CTRL_REJ_IND_LEN (1) + +/* + * LL control slave feature req + * -> 8 bytes of data containing features supported by device. + */ +#define BLE_LL_CTRL_SLAVE_FEATURE_REQ_LEN (8) + +/* LL control connection param req and connection param rsp */ +struct ble_ll_conn_params +{ + uint16_t interval_min; + uint16_t interval_max; + uint16_t latency; + uint16_t timeout; + uint16_t pref_periodicity; + uint16_t ref_conn_event_cnt; + uint16_t offset0; + uint16_t offset1; + uint16_t offset2; + uint16_t offset3; + uint16_t offset4; + uint16_t offset5; +}; + +#define BLE_LL_CTRL_CONN_PARAMS_LEN (24) + +/* LL control reject ind ext */ +struct ble_ll_reject_ind_ext +{ + uint8_t reject_opcode; + uint8_t err_code; +}; + +#define BLE_LL_CTRL_REJECT_IND_EXT_LEN (2) + +/* LL control ping req and ping rsp (contain no data) */ +#define BLE_LL_CTRL_PING_LEN (0) + +/* + * LL control length req and length rsp + * -> max_rx_bytes (2 bytes): defines connMaxRxOctets. Range 27 to 251 + * -> max_rx_time (2 bytes): defines connMaxRxTime. Range 328 to 2120 usecs. + * -> max_tx_bytes (2 bytes): defines connMaxTxOctets. Range 27 to 251 + * -> max_tx_time (2 bytes): defines connMaxTxTime. Range 328 to 2120 usecs. + */ +struct ble_ll_len_req +{ + uint16_t max_rx_bytes; + uint16_t max_rx_time; + uint16_t max_tx_bytes; + uint16_t max_tx_time; +}; + +#define BLE_LL_CTRL_LENGTH_REQ_LEN (8) + +/* + * CONNECT_REQ + * -> InitA (6 bytes) + * -> AdvA (6 bytes) + * -> LLData (22 bytes) + * -> Access address (4 bytes) + * -> CRC init (3 bytes) + * -> WinSize (1 byte) + * -> WinOffset (2 bytes) + * -> Interval (2 bytes) + * -> Latency (2 bytes) + * -> Timeout (2 bytes) + * -> Channel Map (5 bytes) + * -> Hop Increment (5 bits) + * -> SCA (3 bits) + * + * InitA is the initiators public (TxAdd=0) or random (TxAdd=1) address. + * AdvaA is the advertisers public (RxAdd=0) or random (RxAdd=1) address. + * LLData contains connection request data. + * aa: Link Layer's access address + * crc_init: The CRC initialization value used for CRC calculation. + * winsize: The transmit window size = winsize * 1.25 msecs + * winoffset: The transmit window offset = winoffset * 1.25 msecs + * interval: The connection interval = interval * 1.25 msecs. + * latency: connection slave latency = latency + * timeout: Connection supervision timeout = timeout * 10 msecs. + * chanmap: contains channel mapping indicating used and unused data + * channels. Only bits that are 1 are usable. LSB is channel 0. + * hop_inc: Hop increment used for frequency hopping. Random value in + * range of 5 to 16. + */ +#define BLE_CONNECT_REQ_LEN (34) + +struct ble_conn_req_data +{ + uint32_t aa; + uint8_t crc_init[3]; + uint8_t winsize; + uint16_t winoffset; + uint16_t interval; + uint16_t latency; + uint16_t timeout; + uint8_t chanmap[5]; + uint8_t hop_inc; + uint8_t master_sca; +}; + +#define BLE_CONN_REQ_HOP_MASK (0x1F) +#define BLE_CONN_REQ_SCA_MASK (0xE0) + +#define BLE_MASTER_SCA_251_500_PPM (0) +#define BLE_MASTER_SCA_151_250_PPM (1) +#define BLE_MASTER_SCA_101_150_PPM (2) +#define BLE_MASTER_SCA_76_100_PPM (3) +#define BLE_MASTER_SCA_51_75_PPM (4) +#define BLE_MASTER_SCA_31_50_PPM (5) +#define BLE_MASTER_SCA_21_30_PPM (6) +#define BLE_MASTER_SCA_0_20_PPM (7) + +/* + * Initiator filter policy + * + * Determines how the initiator's Link Layer processes advertisements. + * + * LIST: process connectable advertisements only from devices in white list. + * SINGLE: do not use white list; process connectable advertisements from + * a single specific device specified by the host. + */ +enum ble_ll_init_filt_policy +{ + BLE_LL_INIT_FILT_LIST = 0, + BLE_LL_INIT_FILT_SINGLE, +}; + +/*--- External API ---*/ +/* Initialize the Link Layer */ +int ble_ll_init(void); + +/* 'Boolean' function returning true if address is a valid random address */ +int ble_ll_is_valid_random_addr(uint8_t *addr); + +/* Calculate the amount of time a pdu of 'len' bytes will take to transmit */ +uint16_t ble_ll_pdu_tx_time_get(uint16_t len); + +/* Boolean returning true if device is on whitelist */ +int ble_ll_is_on_whitelist(uint8_t *addr, int addr_type); + +/* Is this address a resolvable private address? */ +int ble_ll_is_resolvable_priv_addr(uint8_t *addr); + +/* Is 'addr' our device address? 'addr_type' is public (0) or random (!=0) */ +int ble_ll_is_our_devaddr(uint8_t *addr, int addr_type); + +/*--- PHY interfaces ---*/ +/* Called by the PHY when a packet has started */ +int ble_ll_rx_start(struct os_mbuf *rxpdu); + +/* Called by the PHY when a packet reception ends */ +int ble_ll_rx_end(struct os_mbuf *rxpdu, uint8_t crcok); + +/*--- Controller API ---*/ +/* Set the link layer state */ +void ble_ll_state_set(int ll_state); + +/* Send an event to LL task */ +void ble_ll_event_send(struct os_event *ev); + +/* Set random address */ +int ble_ll_set_random_addr(uint8_t *addr); + +#endif /* H_LL_ */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/36623dd5/net/nimble/controller/include/controller/ble_ll_adv.h ---------------------------------------------------------------------- diff --git a/net/nimble/controller/include/controller/ble_ll_adv.h b/net/nimble/controller/include/controller/ble_ll_adv.h new file mode 100644 index 0000000..15f8d25 --- /dev/null +++ b/net/nimble/controller/include/controller/ble_ll_adv.h @@ -0,0 +1,153 @@ +/** + * Copyright (c) 2015 Stack Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef H_BLE_LL_ADV_ +#define H_BLE_LL_ADV_ + +/* + * ADV event timing + * T_advEvent = advInterval + advDelay + * + * advInterval: increments of 625 usecs + * advDelay: RAND[0, 10] msecs + * + */ +#define BLE_LL_ADV_ITVL (625) /* usecs */ +#define BLE_LL_ADV_ITVL_MIN (32) /* units */ +#define BLE_LL_ADV_ITVL_MAX (16384) /* units */ +#define BLE_LL_ADV_ITVL_MS_MIN (20) /* msecs */ +#define BLE_LL_ADV_ITVL_MS_MAX (10240) /* msecs */ +#define BLE_LL_ADV_ITVL_SCAN_MIN (160) /* units */ +#define BLE_LL_ADV_ITVL_SCAN_MS_MIN (100) /* msecs */ +#define BLE_LL_ADV_ITVL_NONCONN_MIN (160) /* units */ +#define BLE_LL_ADV_ITVL_NONCONN_MS_MIN (100) /* msecs */ +#define BLE_LL_ADV_DELAY_MS_MIN (0) /* msecs */ +#define BLE_LL_ADV_DELAY_MS_MAX (10) /* msecs */ +#define BLE_LL_ADV_PDU_ITVL_LD_MS_MAX (10) /* msecs */ +#define BLE_LL_ADV_PDU_ITVL_HD_MS_MAX (3750) /* usecs */ +#define BLE_LL_ADV_STATE_HD_MAX (1280) /* msecs */ + +/* + * Advertising PDU format: + * -> 2 byte header + * -> LSB contains pdu type, txadd and rxadd bits. + * -> MSB contains length (6 bits). + * -> Payload + */ +#define BLE_ADV_PDU_HDR_TYPE_MASK (0x0F) +#define BLE_ADV_PDU_HDR_TXADD_MASK (0x40) +#define BLE_ADV_PDU_HDR_RXADD_MASK (0x80) +#define BLE_ADV_PDU_HDR_LEN_MASK (0x3F) + +/* Advertising channel PDU types */ +#define BLE_ADV_PDU_TYPE_ADV_IND (0) +#define BLE_ADV_PDU_TYPE_ADV_DIRECT_IND (1) +#define BLE_ADV_PDU_TYPE_ADV_NONCONN_IND (2) +#define BLE_ADV_PDU_TYPE_SCAN_REQ (3) +#define BLE_ADV_PDU_TYPE_SCAN_RSP (4) +#define BLE_ADV_PDU_TYPE_CONNECT_REQ (5) +#define BLE_ADV_PDU_TYPE_ADV_SCAN_IND (6) + +/* + * TxAdd and RxAdd bit definitions. A 0 is a public address; a 1 is a + * random address. + */ +#define BLE_ADV_PDU_HDR_TXADD_RAND (0x40) +#define BLE_ADV_PDU_HDR_RXADD_RAND (0x80) + +/* Maximum advertisement data length */ +#define BLE_ADV_DATA_MAX_LEN (31) + +/* + * ADV_IND + * -> AdvA (6 bytes) + * -> AdvData (0 - 31 bytes) + * + * The advertising address (AdvA) is a public address (TxAdd=0) or random + * address (TxAdd = 1) + */ +#define BLE_ADV_IND_MIN_LEN (6) +#define BLE_ADV_IND_MAX_LEN (37) + +/* + * ADV_DIRECT_IND + * -> AdvA (6 bytes) + * -> InitA (6 bytes) + * + * AdvA is the advertisers public address (TxAdd=0) or random address + * (TxAdd = 1). + * + * InitA is the initiators public or random address. This is the address + * to which this packet is addressed. + * + */ +#define BLE_ADV_DIRECT_IND_LEN (12) + +/* + * ADV_NONCONN_IND + * -> AdvA (6 bytes) + * -> AdvData (0 - 31 bytes) + * + * The advertising address (AdvA) is a public address (TxAdd=0) or random + * address (TxAdd = 1) + * + */ +#define BLE_ADV_NONCONN_IND_MIN_LEN (6) +#define BLE_ADV_NONCONN_IND_MAX_LEN (37) + +/* + * ADV_SCAN_IND + * -> AdvA (6 bytes) + * -> AdvData (0 - 31 bytes) + * + * The advertising address (AdvA) is a public address (TxAdd=0) or random + * address (TxAdd = 1) + * + */ +#define BLE_ADV_SCAN_IND_MIN_LEN (6) +#define BLE_ADV_SCAN_IND_MAX_LEN (37) + +/*---- HCI ----*/ +/* Start an advertiser */ +int ble_ll_adv_start_req(uint8_t adv_chanmask, uint8_t adv_type, + uint8_t *init_addr, uint16_t adv_itvl, void *handle); + +/* Start or stop advertising */ +int ble_ll_adv_set_enable(uint8_t *cmd); + +/* Set advertising data */ +int ble_ll_adv_set_adv_data(uint8_t *cmd, uint8_t len); + +/* Set scan response data */ +int ble_ll_adv_set_scan_rsp_data(uint8_t *cmd, uint8_t len); + +/* Set advertising parameters */ +int ble_ll_adv_set_adv_params(uint8_t *cmd); + +/* Read advertising channel power */ +int ble_ll_adv_read_txpwr(uint8_t *rspbuf); + +/*---- API used by BLE LL ----*/ +/* Called when advertising tx done event posted to LL task */ +void ble_ll_adv_tx_done_proc(void *arg); + +/* Called to initialize advertising functionality. */ +void ble_ll_adv_init(void); + +/* Called when a scan request has been received. */ +int ble_ll_adv_rx_scan_req(uint8_t *rxbuf); + +#endif /* H_LL_ADV_ */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/36623dd5/net/nimble/controller/include/controller/ble_ll_sched.h ---------------------------------------------------------------------- diff --git a/net/nimble/controller/include/controller/ble_ll_sched.h b/net/nimble/controller/include/controller/ble_ll_sched.h new file mode 100644 index 0000000..ba2bd20 --- /dev/null +++ b/net/nimble/controller/include/controller/ble_ll_sched.h @@ -0,0 +1,63 @@ +/** + * Copyright (c) 2015 Stack Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef H_BLE_LL_SCHED_ +#define H_BLE_LL_SCHED_ + +/* BLE scheduler errors */ +#define BLE_LL_SCHED_ERR_OVERLAP (1) + +/* Types of scheduler events */ +#define BLE_LL_SCHED_TYPE_ADV (0) +#define BLE_LL_SCHED_TYPE_SCAN (1) +#define BLE_LL_SCHED_TYPE_TX (2) +#define BLE_LL_SCHED_TYPE_RX (3) + +/* Return values for schedule callback. */ +#define BLE_LL_SCHED_STATE_RUNNING (0) +#define BLE_LL_SCHED_STATE_DONE (1) + +/* Callback function */ +struct ble_ll_sched_item; +typedef int (*sched_cb_func)(struct ble_ll_sched_item *sch); + +struct ble_ll_sched_item +{ + int sched_type; + uint32_t start_time; + uint32_t end_time; + uint32_t next_wakeup; + void *cb_arg; + sched_cb_func sched_cb; + TAILQ_ENTRY(ble_ll_sched_item) link; +}; + +/* Add an item to the schedule */ +int ble_ll_sched_add(struct ble_ll_sched_item *sch); + +/* Remove item(s) from schedule */ +int ble_ll_sched_rmv(uint8_t sched_type); + +/* Initialize the scheduler */ +int ble_ll_sched_init(void); + +/* Get a schedule item */ +struct ble_ll_sched_item *ble_ll_sched_get_item(void); + +/* Free a schedule item */ +void ble_ll_sched_free_item(struct ble_ll_sched_item *sch); + +#endif /* H_LL_SCHED_ */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/36623dd5/net/nimble/controller/include/controller/ll.h ---------------------------------------------------------------------- diff --git a/net/nimble/controller/include/controller/ll.h b/net/nimble/controller/include/controller/ll.h deleted file mode 100644 index 7fbf8db..0000000 --- a/net/nimble/controller/include/controller/ll.h +++ /dev/null @@ -1,437 +0,0 @@ -/** - * Copyright (c) 2015 Stack Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef H_LL_ -#define H_LL_ - -/* XXX: Not sure this should go here, but whatever */ -/* - * ble ll global parameters - * - * NOTES: - * 1) Controller should not change value of supported max tx and rx time and - * octets. - */ -struct ll_global_params -{ - int conn_init_max_tx_octets; - int conn_init_max_tx_time; - int supp_max_tx_octets; - int supp_max_tx_time; - int supp_max_rx_octets; - int supp_max_rx_time; -}; - -struct ll_obj -{ - uint8_t ll_state; - struct os_eventq ll_evq; - struct ll_global_params ll_params; - struct os_event ll_rx_pkt_ev; - STAILQ_HEAD(ll_rxpkt_qh, os_mbuf_pkthdr) ll_rx_pkt_q; -}; - -struct ll_stats -{ - uint32_t rx_crc_ok; - uint32_t rx_crc_fail; - uint32_t rx_bytes; - uint32_t rx_adv_ind; - uint32_t rx_adv_direct_ind; - uint32_t rx_adv_nonconn_ind; - uint32_t rx_scan_reqs; - uint32_t rx_scan_rsps; - uint32_t rx_connect_reqs; - uint32_t rx_scan_ind; - uint32_t rx_unk_pdu; - uint32_t rx_malformed_pkts; - uint32_t hci_cmds; - uint32_t hci_cmd_errs; - uint32_t hci_events_sent; -}; - -extern struct ll_stats g_ll_stats; -extern struct ll_obj g_ll_data; - -/* States */ -#define BLE_LL_STATE_STANDBY (0) -#define BLE_LL_STATE_ADV (1) -#define BLE_LL_STATE_SCANNING (2) -#define BLE_LL_STATE_INITITATING (3) -#define BLE_LL_STATE_CONNECT (4) - -/* BLE LL Task Events */ -#define BLE_LL_EVENT_HCI_CMD (OS_EVENT_T_PERUSER) -#define BLE_LL_EVENT_ADV_TXDONE (OS_EVENT_T_PERUSER + 1) -#define BLE_LL_EVENT_RX_PKT_IN (OS_EVENT_T_PERUSER + 2) -#define BLE_LL_EVENT_SCAN_WIN_END (OS_EVENT_T_PERUSER + 3) - -/* LL Features */ -#define BLE_LL_FEAT_LE_ENCRYPTION (0x01) -#define BLE_LL_FEAT_CONN_PARM_REQ (0x02) -#define BLE_LL_FEAT_EXTENDED_REJ (0x04) -#define BLE_LL_FEAT_SLAVE_INIT (0x08) -#define BLE_LL_FEAT_LE_PING (0x10) -#define BLE_LL_FEAT_DATA_LEN_EXT (0x20) -#define BLE_LL_FEAT_LL_PRIVACY (0x40) -#define BLE_LL_FEAT_EXT_SCAN_FILT (0x80) - -/* LL timing */ -#define BLE_LL_IFS (150) /* usecs */ -#define BLE_CLOCK_DRIFT_ACTIVE (50) /* +/- ppm */ -#define BLE_CLOCK_DRIFT_SLEEP (500) /* +/- ppm */ - -/* - * BLE LL device address. Note that element 0 of the array is the LSB and - * is sent over the air first. Byte 5 is the MSB and is the last one sent over - * the air. - */ -#define BLE_DEV_ADDR_LEN (6) /* bytes */ - -struct ble_dev_addr -{ - uint8_t u8[BLE_DEV_ADDR_LEN]; -}; - -#define BLE_IS_DEV_ADDR_STATIC(addr) ((addr->u8[5] & 0xc0) == 0xc0) -#define BLE_IS_DEV_ADDR_RESOLVABLE(addr) ((addr->u8[5] & 0xc0) == 0x40) -#define BLE_IS_DEV_ADDR_UNRESOLVABLE(addr) ((addr->u8[5] & 0xc0) == 0x00) - -/* - * LL packet format - * - * -> Preamble (1 byte) - * -> Access Address (4 bytes) - * -> PDU (2 to 257 octets) - * -> CRC (3 bytes) - */ -#define BLE_LL_PREAMBLE_LEN (1) -#define BLE_LL_ACC_ADDR_LEN (4) -#define BLE_LL_CRC_LEN (3) -#define BLE_LL_OVERHEAD_LEN \ - (BLE_LL_CRC_LEN + BLE_LL_ACC_ADDR_LEN + BLE_LL_PREAMBLE_LEN) -#define BLE_LL_PDU_HDR_LEN (2) -#define BLE_LL_MIN_PDU_LEN (BLE_LL_PDU_HDR_LEN) -#define BLE_LL_MAX_PDU_LEN (257) -#define BLE_LL_CRCINIT_ADV (0x555555) - -/* Access address for advertising channels */ -#define BLE_ACCESS_ADDR_ADV (0x8E89BED6) - -/* - * Data Channel format - * - * -> Header (2 bytes. - * -> LSB contains llid, nesn, sn and md - * -> MSB contains length (8 bits) - * -> Payload (0 to 251) - * -> MIC (0 or 4 bytes) - */ -#define BLE_LL_DATA_HDR_LLID_MASK (0x03) -#define BLE_LL_DATA_HDR_NESN_MASK (0x04) -#define BLE_LL_DATA_HDR_SN_MASK (0x08) -#define BLE_LL_DATA_HDR_MD_MASK (0x10) -#define BLE_LL_DATA_HDR_RSRVD_MASK (0xE0) - -/* LLID definitions */ -#define BLE_LL_LLID_RSRVD (0) -#define BLE_LL_LLID_DATA_FRAG (1) -#define BLE_LL_LLID_DATA_START (2) -#define BLE_LL_LLID_CTRL (3) - -/* - * LL CTRL PDU format - * -> Opcode (1 byte) - * -> Ctrl data (0 - 26 bytes) - */ -#define BLE_LL_CTRL_CONN_UPDATE_REQ (0) -#define BLE_LL_CTRL_CHANNEL_MAP_REQ (1) -#define BLE_LL_CTRL_TERMINATE_IND (2) -#define BLE_LL_CTRL_ENC_REQ (3) -#define BLE_LL_CTRL_ENC_RSP (4) -#define BLE_LL_CTRL_START_ENC_REQ (5) -#define BLE_LL_CTRL_START_ENC_RSP (6) -#define BLE_LL_CTRL_UNKNOWN_RSP (7) -#define BLE_LL_CTRL_FEATURE_REQ (8) -#define BLE_LL_CTRL_FEATURE_RSP (9) -#define BLE_LL_CTRL_PAUSE_ENC_REQ (10) -#define BLE_LL_CTRL_PAUSE_ENC_RSP (11) -#define BLE_LL_CTRL_VERSION_IND (12) -#define BLE_LL_CTRL_REJECT_IND (13) -#define BLE_LL_CTRL_SLAVE_FEATURE_REQ (14) -#define BLE_LL_CTRL_CONN_PARM_REQ (15) -#define BLE_LL_CTRL_CONN_PARM_RSP (16) -#define BLE_LL_CTRL_REJECT_IND_EXT (17) -#define BLE_LL_CTRL_PING_REQ (18) -#define BLE_LL_CTRL_PING_RSP (19) -#define BLE_LL_CTRL_LENGTH_REQ (20) -#define BLE_LL_CTRL_LENGTH_RSP (21) - -/* LL control connection update request */ -struct ll_conn_upd_req -{ - uint8_t winsize; - uint16_t winoffset; - uint16_t interval; - uint16_t latency; - uint16_t timeout; - uint16_t instant; -}; - -#define BLE_LL_CTRL_CONN_UPD_REQ_LEN (11) - -/* LL control channel map request */ -struct ll_chan_map_req -{ - uint8_t chmap[5]; - uint16_t instant; -}; - -#define BLE_LL_CTRL_CHAN_MAP_LEN (7) - -/* - * LL control terminate ind - * -> error code (1 byte) - */ -#define BLE_LL_CTRL_TERMINATE_IND_LEN (1) - -/* LL control enc req */ -struct ll_enc_req -{ - uint8_t rand[8]; - uint16_t ediv; - uint8_t skdm[8]; - uint32_t ivm; -}; - -#define BLE_LL_CTRL_ENC_REQ_LEN (22) - -/* LL control enc rsp */ -struct ll_enc_rsp -{ - uint8_t skds[8]; - uint32_t ivs; -}; - -#define BLE_LL_CTRL_ENC_RSP_LEN (12) - -/* LL control start enc req and start enc rsp have no data */ -#define BLE_LL_CTRL_START_ENC_LEN (0) - -/* - * LL control unknown response - * -> 1 byte which contains the unknown or un-supported opcode. - */ -#define BLE_LL_CTRL_UNK_RSP_LEN (1) - -/* - * LL control feature req and LL control feature rsp - * -> 8 bytes of data containing features supported by device. - */ -#define BLE_LL_CTRL_FEATURE_LEN (8) - -/* LL control pause enc req and pause enc rsp have no data */ -#define BLE_LL_CTRL_PAUSE_ENC_LEN (0) - -/* - * LL control version ind - * -> version (1 byte): - * Contains the version number of the bluetooth controller specification. - * -> comp_id (2 bytes) - * Contains the company identifier of the manufacturer of the controller. - * -> sub_ver_num: Contains a unique value for implementation or revision of - * the bluetooth controller. - */ -struct ll_version_ind -{ - uint8_t ble_ctrlr_ver; - uint16_t company_id; - uint16_t sub_ver_num; -}; - -#define BLE_LL_CTRL_VERSION_IND_LEN (5) - -/* - * LL control reject ind - * -> error code (1 byte): contains reason why request was rejected. - */ -#define BLE_LL_CTRL_REJ_IND_LEN (1) - -/* - * LL control slave feature req - * -> 8 bytes of data containing features supported by device. - */ -#define BLE_LL_CTRL_SLAVE_FEATURE_REQ_LEN (8) - -/* LL control connection param req and connection param rsp */ -struct ll_conn_params -{ - uint16_t interval_min; - uint16_t interval_max; - uint16_t latency; - uint16_t timeout; - uint16_t pref_periodicity; - uint16_t ref_conn_event_cnt; - uint16_t offset0; - uint16_t offset1; - uint16_t offset2; - uint16_t offset3; - uint16_t offset4; - uint16_t offset5; -}; - -#define BLE_LL_CTRL_CONN_PARAMS_LEN (24) - -/* LL control reject ind ext */ -struct ll_reject_ind_ext -{ - uint8_t reject_opcode; - uint8_t err_code; -}; - -#define BLE_LL_CTRL_REJECT_IND_EXT_LEN (2) - -/* LL control ping req and ping rsp (contain no data) */ -#define BLE_LL_CTRL_PING_LEN (0) - -/* - * LL control length req and length rsp - * -> max_rx_bytes (2 bytes): defines connMaxRxOctets. Range 27 to 251 - * -> max_rx_time (2 bytes): defines connMaxRxTime. Range 328 to 2120 usecs. - * -> max_tx_bytes (2 bytes): defines connMaxTxOctets. Range 27 to 251 - * -> max_tx_time (2 bytes): defines connMaxTxTime. Range 328 to 2120 usecs. - */ -struct ll_len_req -{ - uint16_t max_rx_bytes; - uint16_t max_rx_time; - uint16_t max_tx_bytes; - uint16_t max_tx_time; -}; - -#define BLE_LL_CTRL_LENGTH_REQ_LEN (8) - -/* - * CONNECT_REQ - * -> InitA (6 bytes) - * -> AdvA (6 bytes) - * -> LLData (22 bytes) - * -> Access address (4 bytes) - * -> CRC init (3 bytes) - * -> WinSize (1 byte) - * -> WinOffset (2 bytes) - * -> Interval (2 bytes) - * -> Latency (2 bytes) - * -> Timeout (2 bytes) - * -> Channel Map (5 bytes) - * -> Hop Increment (5 bits) - * -> SCA (3 bits) - * - * InitA is the initiators public (TxAdd=0) or random (TxAdd=1) address. - * AdvaA is the advertisers public (RxAdd=0) or random (RxAdd=1) address. - * LLData contains connection request data. - * aa: Link Layer's access address - * crc_init: The CRC initialization value used for CRC calculation. - * winsize: The transmit window size = winsize * 1.25 msecs - * winoffset: The transmit window offset = winoffset * 1.25 msecs - * interval: The connection interval = interval * 1.25 msecs. - * latency: connection slave latency = latency - * timeout: Connection supervision timeout = timeout * 10 msecs. - * chanmap: contains channel mapping indicating used and unused data - * channels. Only bits that are 1 are usable. LSB is channel 0. - * hop_inc: Hop increment used for frequency hopping. Random value in - * range of 5 to 16. - */ -#define BLE_CONNECT_REQ_LEN (34) - -struct ble_conn_req_data -{ - uint32_t aa; - uint8_t crc_init[3]; - uint8_t winsize; - uint16_t winoffset; - uint16_t interval; - uint16_t latency; - uint16_t timeout; - uint8_t chanmap[5]; - uint8_t hop_inc; - uint8_t master_sca; -}; - -#define BLE_CONN_REQ_HOP_MASK (0x1F) -#define BLE_CONN_REQ_SCA_MASK (0xE0) - -#define BLE_MASTER_SCA_251_500_PPM (0) -#define BLE_MASTER_SCA_151_250_PPM (1) -#define BLE_MASTER_SCA_101_150_PPM (2) -#define BLE_MASTER_SCA_76_100_PPM (3) -#define BLE_MASTER_SCA_51_75_PPM (4) -#define BLE_MASTER_SCA_31_50_PPM (5) -#define BLE_MASTER_SCA_21_30_PPM (6) -#define BLE_MASTER_SCA_0_20_PPM (7) - -/* - * Initiator filter policy - * - * Determines how the initiator's Link Layer processes advertisements. - * - * LIST: process connectable advertisements only from devices in white list. - * SINGLE: do not use white list; process connectable advertisements from - * a single specific device specified by the host. - */ -enum ll_init_filt_policy -{ - BLE_LL_INIT_FILT_LIST = 0, - BLE_LL_INIT_FILT_SINGLE, -}; - -/*--- External API ---*/ -/* Initialize the Link Layer */ -int ll_init(void); - -/* 'Boolean' function returning true if address is a valid random address */ -int ble_ll_is_valid_random_addr(uint8_t *addr); - -/* Calculate the amount of time a pdu of 'len' bytes will take to transmit */ -uint16_t ll_pdu_tx_time_get(uint16_t len); - -/* Boolean returning true if device is on whitelist */ -int ble_ll_is_on_whitelist(uint8_t *addr, int addr_type); - -/* Is this address a resolvable private address? */ -int ble_ll_is_resolvable_priv_addr(uint8_t *addr); - -/* Is 'addr' our device address? 'addr_type' is public (0) or random (!=0) */ -int ble_ll_is_our_devaddr(uint8_t *addr, int addr_type); - -/*--- PHY interfaces ---*/ -/* Called by the PHY when a packet has started */ -int ll_rx_start(struct os_mbuf *rxpdu); - -/* Called by the PHY when a packet reception ends */ -int ll_rx_end(struct os_mbuf *rxpdu, uint8_t crcok); - -/*--- Controller API ---*/ -/* Set the link layer state */ -void ble_ll_state_set(int ll_state); - -/* Send an event to LL task */ -void ble_ll_event_send(struct os_event *ev); - -/* Set random address */ -int ble_ll_set_random_addr(uint8_t *addr); - -#endif /* H_LL_ */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/36623dd5/net/nimble/controller/include/controller/ll_adv.h ---------------------------------------------------------------------- diff --git a/net/nimble/controller/include/controller/ll_adv.h b/net/nimble/controller/include/controller/ll_adv.h deleted file mode 100644 index 0f0a8b7..0000000 --- a/net/nimble/controller/include/controller/ll_adv.h +++ /dev/null @@ -1,153 +0,0 @@ -/** - * Copyright (c) 2015 Stack Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef H_LL_ADV_ -#define H_LL_ADV_ - -/* - * ADV event timing - * T_advEvent = advInterval + advDelay - * - * advInterval: increments of 625 usecs - * advDelay: RAND[0, 10] msecs - * - */ -#define BLE_LL_ADV_ITVL (625) /* usecs */ -#define BLE_LL_ADV_ITVL_MIN (32) /* units */ -#define BLE_LL_ADV_ITVL_MAX (16384) /* units */ -#define BLE_LL_ADV_ITVL_MS_MIN (20) /* msecs */ -#define BLE_LL_ADV_ITVL_MS_MAX (10240) /* msecs */ -#define BLE_LL_ADV_ITVL_SCAN_MIN (160) /* units */ -#define BLE_LL_ADV_ITVL_SCAN_MS_MIN (100) /* msecs */ -#define BLE_LL_ADV_ITVL_NONCONN_MIN (160) /* units */ -#define BLE_LL_ADV_ITVL_NONCONN_MS_MIN (100) /* msecs */ -#define BLE_LL_ADV_DELAY_MS_MIN (0) /* msecs */ -#define BLE_LL_ADV_DELAY_MS_MAX (10) /* msecs */ -#define BLE_LL_ADV_PDU_ITVL_LD_MS_MAX (10) /* msecs */ -#define BLE_LL_ADV_PDU_ITVL_HD_MS_MAX (3750) /* usecs */ -#define BLE_LL_ADV_STATE_HD_MAX (1280) /* msecs */ - -/* - * Advertising PDU format: - * -> 2 byte header - * -> LSB contains pdu type, txadd and rxadd bits. - * -> MSB contains length (6 bits). - * -> Payload - */ -#define BLE_ADV_PDU_HDR_TYPE_MASK (0x0F) -#define BLE_ADV_PDU_HDR_TXADD_MASK (0x40) -#define BLE_ADV_PDU_HDR_RXADD_MASK (0x80) -#define BLE_ADV_PDU_HDR_LEN_MASK (0x3F) - -/* Advertising channel PDU types */ -#define BLE_ADV_PDU_TYPE_ADV_IND (0) -#define BLE_ADV_PDU_TYPE_ADV_DIRECT_IND (1) -#define BLE_ADV_PDU_TYPE_ADV_NONCONN_IND (2) -#define BLE_ADV_PDU_TYPE_SCAN_REQ (3) -#define BLE_ADV_PDU_TYPE_SCAN_RSP (4) -#define BLE_ADV_PDU_TYPE_CONNECT_REQ (5) -#define BLE_ADV_PDU_TYPE_ADV_SCAN_IND (6) - -/* - * TxAdd and RxAdd bit definitions. A 0 is a public address; a 1 is a - * random address. - */ -#define BLE_ADV_PDU_HDR_TXADD_RAND (0x40) -#define BLE_ADV_PDU_HDR_RXADD_RAND (0x80) - -/* Maximum advertisement data length */ -#define BLE_ADV_DATA_MAX_LEN (31) - -/* - * ADV_IND - * -> AdvA (6 bytes) - * -> AdvData (0 - 31 bytes) - * - * The advertising address (AdvA) is a public address (TxAdd=0) or random - * address (TxAdd = 1) - */ -#define BLE_ADV_IND_MIN_LEN (6) -#define BLE_ADV_IND_MAX_LEN (37) - -/* - * ADV_DIRECT_IND - * -> AdvA (6 bytes) - * -> InitA (6 bytes) - * - * AdvA is the advertisers public address (TxAdd=0) or random address - * (TxAdd = 1). - * - * InitA is the initiators public or random address. This is the address - * to which this packet is addressed. - * - */ -#define BLE_ADV_DIRECT_IND_LEN (12) - -/* - * ADV_NONCONN_IND - * -> AdvA (6 bytes) - * -> AdvData (0 - 31 bytes) - * - * The advertising address (AdvA) is a public address (TxAdd=0) or random - * address (TxAdd = 1) - * - */ -#define BLE_ADV_NONCONN_IND_MIN_LEN (6) -#define BLE_ADV_NONCONN_IND_MAX_LEN (37) - -/* - * ADV_SCAN_IND - * -> AdvA (6 bytes) - * -> AdvData (0 - 31 bytes) - * - * The advertising address (AdvA) is a public address (TxAdd=0) or random - * address (TxAdd = 1) - * - */ -#define BLE_ADV_SCAN_IND_MIN_LEN (6) -#define BLE_ADV_SCAN_IND_MAX_LEN (37) - -/*---- HCI ----*/ -/* Start an advertiser */ -int ll_adv_start_req(uint8_t adv_chanmask, uint8_t adv_type, uint8_t *init_addr, - uint16_t adv_itvl, void *handle); - -/* Start or stop advertising */ -int ll_adv_set_enable(uint8_t *cmd); - -/* Set advertising data */ -int ll_adv_set_adv_data(uint8_t *cmd, uint8_t len); - -/* Set scan response data */ -int ll_adv_set_scan_rsp_data(uint8_t *cmd, uint8_t len); - -/* Set advertising parameters */ -int ll_adv_set_adv_params(uint8_t *cmd); - -/* Read advertising channel power */ -int ll_adv_read_txpwr(uint8_t *rspbuf); - -/*---- API used by BLE LL ----*/ -/* Called when advertising tx done event posted to LL task */ -void ll_adv_tx_done_proc(void *arg); - -/* Called to initialize advertising functionality. */ -void ll_adv_init(void); - -/* Called when a scan request has been received. */ -int ble_ll_adv_rx_scan_req(uint8_t *rxbuf); - -#endif /* H_LL_ADV_ */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/36623dd5/net/nimble/controller/include/controller/ll_sched.h ---------------------------------------------------------------------- diff --git a/net/nimble/controller/include/controller/ll_sched.h b/net/nimble/controller/include/controller/ll_sched.h deleted file mode 100644 index 22be5f5..0000000 --- a/net/nimble/controller/include/controller/ll_sched.h +++ /dev/null @@ -1,63 +0,0 @@ -/** - * Copyright (c) 2015 Stack Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef H_LL_SCHED_ -#define H_LL_SCHED_ - -/* BLE scheduler errors */ -#define BLE_LL_SCHED_ERR_OVERLAP (1) - -/* Types of scheduler events */ -#define BLE_LL_SCHED_TYPE_ADV (0) -#define BLE_LL_SCHED_TYPE_SCAN (1) -#define BLE_LL_SCHED_TYPE_TX (2) -#define BLE_LL_SCHED_TYPE_RX (3) - -/* Return values for schedule callback. */ -#define BLE_LL_SCHED_STATE_RUNNING (0) -#define BLE_LL_SCHED_STATE_DONE (1) - -/* Callback function */ -struct ll_sched_item; -typedef int (*sched_cb_func)(struct ll_sched_item *sch); - -struct ll_sched_item -{ - int sched_type; - uint32_t start_time; - uint32_t end_time; - uint32_t next_wakeup; - void *cb_arg; - sched_cb_func sched_cb; - TAILQ_ENTRY(ll_sched_item) link; -}; - -/* Add an item to the schedule */ -int ll_sched_add(struct ll_sched_item *sch); - -/* Remove item(s) from schedule */ -int ll_sched_rmv(uint8_t sched_type); - -/* Initialize the scheduler */ -int ll_sched_init(void); - -/* Get a schedule item */ -struct ll_sched_item *ll_sched_get_item(void); - -/* Free a schedule item */ -void ll_sched_free_item(struct ll_sched_item *sch); - -#endif /* H_LL_SCHED_ */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/36623dd5/net/nimble/controller/src/ble_ll.c ---------------------------------------------------------------------- diff --git a/net/nimble/controller/src/ble_ll.c b/net/nimble/controller/src/ble_ll.c new file mode 100644 index 0000000..2c50a09 --- /dev/null +++ b/net/nimble/controller/src/ble_ll.c @@ -0,0 +1,655 @@ +/** + * Copyright (c) 2015 Runtime Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include <stdint.h> +#include <assert.h> +#include <string.h> +#include "os/os.h" +#include "nimble/ble.h" +#include "controller/phy.h" +#include "controller/ble_ll.h" +#include "controller/ble_ll_adv.h" +#include "controller/ble_ll_sched.h" +#include "controller/ll_scan.h" +#include "controller/ll_hci.h" + +/* XXX: use the sanity task! */ + +/* Connection related define */ +#define BLE_LL_CONN_INIT_MAX_REMOTE_OCTETS (27) +#define BLE_LL_CONN_INIT_MAX_REMOTE_TIME (238) + +/* The global BLE LL data object */ +struct ble_ll_obj g_ble_ll_data; + +/* Global link layer statistics */ +struct ble_ll_stats g_ble_ll_stats; + +/* The BLE LL task data structure */ +#define BLE_LL_TASK_PRI (OS_TASK_PRI_HIGHEST) +#define BLE_LL_STACK_SIZE (128) +struct os_task g_ble_ll_task; +os_stack_t g_ble_ll_stack[BLE_LL_STACK_SIZE]; + +/* XXX: this is just temporary; used to calculate the channel index */ +struct ble_ll_sm_connection +{ + /* Data channel index for connection */ + uint8_t unmapped_chan; + uint8_t last_unmapped_chan; + uint8_t num_used_channels; + + /* Flow control */ + uint8_t tx_seq; + uint8_t next_exp_seq; + + /* Parameters kept by the link-layer per connection */ + uint8_t max_tx_octets; + uint8_t max_rx_octets; + uint8_t max_tx_time; + uint8_t max_rx_time; + uint8_t remote_max_tx_octets; + uint8_t remote_max_rx_octets; + uint8_t remote_max_tx_time; + uint8_t remote_max_rx_time; + uint8_t effective_max_tx_octets; + uint8_t effective_max_rx_octets; + uint8_t effective_max_tx_time; + uint8_t effective_max_rx_time; + + /* The connection request data */ + struct ble_conn_req_data req_data; +}; + +uint8_t +ble_ll_next_data_channel(struct ble_ll_sm_connection *cnxn) +{ + int i; + int j; + uint8_t curchan; + uint8_t remap_index; + uint8_t bitpos; + uint8_t cntr; + uint8_t mask; + uint8_t usable_chans; + + /* Get next un mapped channel */ + curchan = (cnxn->last_unmapped_chan + cnxn->req_data.hop_inc) % + BLE_PHY_NUM_DATA_CHANS; + + /* Set the current unmapped channel */ + cnxn->unmapped_chan = curchan; + + /* Is this a valid channel? */ + bitpos = 1 << (curchan & 0x07); + if ((cnxn->req_data.chanmap[curchan >> 3] & bitpos) == 0) { + + /* Calculate remap index */ + remap_index = curchan % cnxn->num_used_channels; + + /* Iterate through channel map to find this channel */ + cntr = 0; + for (i = 0; i < 5; i++) { + usable_chans = cnxn->req_data.chanmap[i]; + if (usable_chans != 0) { + mask = 0x01; + for (j = 0; j < 8; j++) { + if (usable_chans & mask) { + if (cntr == remap_index) { + return cntr; + } + ++cntr; + } + mask <<= 1; + } + } + } + } + + return curchan; +} + +/* Called when a connection gets initialized */ +int +ble_init_conn_sm(struct ble_ll_sm_connection *cnxn) +{ + cnxn->max_tx_time = g_ble_ll_data.ll_params.conn_init_max_tx_time; + cnxn->max_rx_time = g_ble_ll_data.ll_params.supp_max_rx_time; + cnxn->max_tx_octets = g_ble_ll_data.ll_params.conn_init_max_tx_octets; + cnxn->max_rx_octets = g_ble_ll_data.ll_params.supp_max_rx_octets; + cnxn->remote_max_rx_octets = BLE_LL_CONN_INIT_MAX_REMOTE_OCTETS; + cnxn->remote_max_tx_octets = BLE_LL_CONN_INIT_MAX_REMOTE_OCTETS; + cnxn->remote_max_rx_time = BLE_LL_CONN_INIT_MAX_REMOTE_TIME; + cnxn->remote_max_tx_time = BLE_LL_CONN_INIT_MAX_REMOTE_TIME; + + return 0; +} + +static void +ble_ll_count_rx_pkts(uint8_t pdu_type) +{ + /* Count received packet types */ + switch (pdu_type) { + case BLE_ADV_PDU_TYPE_ADV_IND: + ++g_ble_ll_stats.rx_adv_ind; + break; + case BLE_ADV_PDU_TYPE_ADV_DIRECT_IND: + /* XXX: Do I want to count these if they are not for me? */ + ++g_ble_ll_stats.rx_adv_direct_ind; + break; + case BLE_ADV_PDU_TYPE_ADV_NONCONN_IND: + ++g_ble_ll_stats.rx_adv_nonconn_ind; + break; + case BLE_ADV_PDU_TYPE_SCAN_REQ: + /* XXX: Do I want to count these if they are not for me? */ + ++g_ble_ll_stats.rx_scan_reqs; + break; + case BLE_ADV_PDU_TYPE_SCAN_RSP: + ++g_ble_ll_stats.rx_scan_rsps; + break; + case BLE_ADV_PDU_TYPE_CONNECT_REQ: + /* XXX: Do I want to count these if they are not for me? */ + ++g_ble_ll_stats.rx_connect_reqs; + break; + case BLE_ADV_PDU_TYPE_ADV_SCAN_IND: + ++g_ble_ll_stats.rx_scan_ind; + break; + default: + ++g_ble_ll_stats.rx_unk_pdu; + break; + } +} + + +int +ble_ll_is_on_whitelist(uint8_t *addr, int addr_type) +{ + /* XXX: implement this */ + return 1; +} + +int +ble_ll_is_resolvable_priv_addr(uint8_t *addr) +{ + /* XXX: implement this */ + return 0; +} + +/* Checks to see that the device is a valid random address */ +int +ble_ll_is_valid_random_addr(uint8_t *addr) +{ + int i; + int rc; + uint16_t sum; + uint8_t addr_type; + + /* Make sure all bits are neither one nor zero */ + sum = 0; + for (i = 0; i < (BLE_DEV_ADDR_LEN -1); ++i) { + sum += addr[i]; + } + sum += addr[5] & 0x3f; + + if ((sum == 0) || (sum == ((5*255) + 0x3f))) { + return 0; + } + + /* Get the upper two bits of the address */ + rc = 1; + addr_type = addr[5] & 0xc0; + if (addr_type == 0xc0) { + /* Static random address. No other checks needed */ + } else if (addr_type == 0x40) { + /* Resolvable */ + sum = addr[3] + addr[4] + (addr[5] & 0x3f); + if ((sum == 0) || (sum == (255 + 255 + 0x3f))) { + rc = 0; + } + } else if (addr_type == 0) { + /* non-resolvable. Cant be equal to public */ + if (!memcmp(g_dev_addr, addr, BLE_DEV_ADDR_LEN)) { + rc = 0; + } + } else { + /* Invalid upper two bits */ + rc = 0; + } + + return rc; +} + +/** + * Called from the HCI command parser when the set random address command + * is received. + * + * Context: Link Layer task (HCI command parser) + * + * @param addr Pointer to address + * + * @return int 0: success + */ +int +ble_ll_set_random_addr(uint8_t *addr) +{ + int rc; + + rc = BLE_ERR_INV_HCI_CMD_PARMS; + if (ble_ll_is_valid_random_addr(addr)) { + memcpy(g_random_addr, addr, BLE_DEV_ADDR_LEN); + rc = BLE_ERR_SUCCESS; + } + + return rc; +} + +int +ble_ll_is_our_devaddr(uint8_t *addr, int addr_type) +{ + int rc; + uint8_t *our_addr; + + rc = 0; + if (addr_type) { + our_addr = g_dev_addr; + } else { + our_addr = g_random_addr; + } + + rc = 0; + if (!memcmp(our_addr, g_random_addr, BLE_DEV_ADDR_LEN)) { + rc = 1; + } + + return rc; +} + +/** + * ll pdu tx time get + * + * Returns the number of usecs it will take to transmit a PDU of length 'len' + * bytes. Each byte takes 8 usecs. + * + * @param len The number of PDU bytes to transmit + * + * @return uint16_t The number of usecs it will take to transmit a PDU of + * length 'len' bytes. + */ +uint16_t +ble_ll_pdu_tx_time_get(uint16_t len) +{ + len += BLE_LL_OVERHEAD_LEN; + len = len << 3; + return len; +} + +/** + * ll rx pkt in proc + * + * Process received packet from PHY + * + * Callers: LL task. + * + */ +void +ble_ll_rx_pkt_in_proc(void) +{ + os_sr_t sr; + uint8_t pdu_type; + uint8_t *rxbuf; + struct os_mbuf_pkthdr *pkthdr; + struct ble_mbuf_hdr *ble_hdr; + struct os_mbuf *m; + + /* Drain all packets off the queue */ + while (STAILQ_FIRST(&g_ble_ll_data.ll_rx_pkt_q)) { + /* Get mbuf pointer from packet header pointer */ + pkthdr = STAILQ_FIRST(&g_ble_ll_data.ll_rx_pkt_q); + m = (struct os_mbuf *)((uint8_t *)pkthdr - sizeof(struct os_mbuf)); + + /* Remove from queue */ + OS_ENTER_CRITICAL(sr); + STAILQ_REMOVE_HEAD(&g_ble_ll_data.ll_rx_pkt_q, omp_next); + OS_EXIT_CRITICAL(sr); + + /* XXX: need to check if this is an adv channel or data channel */ + + /* Count statistics */ + rxbuf = m->om_data; + pdu_type = rxbuf[0] & BLE_ADV_PDU_HDR_TYPE_MASK; + ble_hdr = BLE_MBUF_HDR_PTR(m); + if (ble_hdr->crcok) { + /* The total bytes count the PDU header and PDU payload */ + g_ble_ll_stats.rx_bytes += pkthdr->omp_len; + ++g_ble_ll_stats.rx_crc_ok; + ble_ll_count_rx_pkts(pdu_type); + } else { + ++g_ble_ll_stats.rx_crc_fail; + } + + /* + * XXX: The reason I dont bail earlier on bad CRC is that + * there may be some connection stuff I need to do with a packet + * that has failed the CRC. + */ + + /* Process the PDU */ + switch (g_ble_ll_data.ll_state) { + case BLE_LL_STATE_ADV: + /* XXX: implement this */ + break; + case BLE_LL_STATE_SCANNING: + if (ble_hdr->crcok) { + ble_ll_scan_rx_pdu_proc(pdu_type, rxbuf, ble_hdr->rssi); + } + + /* We need to re-enable the PHY if we are in idle state */ + if (ble_phy_state_get() == BLE_PHY_STATE_IDLE) { + /* XXX: If this returns error, we will need to attempt to + re-start scanning! */ + ble_phy_rx(); + } + break; + default: + /* XXX: implement */ + assert(0); + break; + } + + /* XXX: Free the mbuf for now */ + os_mbuf_free(&g_mbuf_pool, m); + } +} + +/** + * Called to put a packet on the Link Layer receive packet queue. + * + * @param rxpdu Pointer to received PDU + */ +void +ble_ll_rx_pdu_in(struct os_mbuf *rxpdu) +{ + struct os_mbuf_pkthdr *pkthdr; + + pkthdr = OS_MBUF_PKTHDR(rxpdu); + STAILQ_INSERT_TAIL(&g_ble_ll_data.ll_rx_pkt_q, pkthdr, omp_next); + os_eventq_put(&g_ble_ll_data.ll_evq, &g_ble_ll_data.ll_rx_pkt_ev); +} + +/** + * Called upon start of received PDU + * + * @param rxpdu + * + * @return int + * < 0: A frame we dont want to receive. + * = 0: Continue to receive frame. Dont go from rx to tx + * > 1: Continue to receive frame and go from rx to idle when done + */ +int +ble_ll_rx_start(struct os_mbuf *rxpdu) +{ + int rc; + uint8_t pdu_type; + uint8_t *rxbuf; + + /* XXX: need to check if this is an adv channel or data channel */ + rxbuf = rxpdu->om_data; + pdu_type = rxbuf[0] & BLE_ADV_PDU_HDR_TYPE_MASK; + + switch (g_ble_ll_data.ll_state) { + case BLE_LL_STATE_ADV: + /* If we get a scan request we must tell the phy to go from rx to tx */ + if (pdu_type == BLE_ADV_PDU_TYPE_SCAN_REQ) { + rc = 1; + } else if (pdu_type == BLE_ADV_PDU_TYPE_CONNECT_REQ) { + rc = 0; + } else { + /* This is a frame we dont want. Just abort it */ + rc = -1; + } + break; + case BLE_LL_STATE_SCANNING: + rc = ble_ll_scan_rx_pdu_start(pdu_type, rxpdu); + break; + default: + /* XXX: should we really assert here? What to do... */ + rc = -1; + assert(0); + break; + } + + return rc; +} + +/** + * Called by the PHY when a receive packet has ended. + * + * NOTE: Called from interrupt context! + * + * @param rxbuf + * + * @return int + * < 0: Disable the phy after reception. + * == 0: Success. Do not disable the PHY. + * > 0: Do not disable PHY as that has already been done. + */ +int +ble_ll_rx_end(struct os_mbuf *rxpdu, uint8_t crcok) +{ + int rc; + int badpkt; + uint8_t pdu_type; + uint8_t len; + uint16_t mblen; + uint8_t *rxbuf; + + /* Set the rx buffer pointer to the start of the received data */ + rxbuf = rxpdu->om_data; + + /* XXX: need to check if this is an adv channel or data channel */ + pdu_type = rxbuf[0] & BLE_ADV_PDU_HDR_TYPE_MASK; + len = rxbuf[1] & BLE_ADV_PDU_HDR_LEN_MASK; + + /* XXX: Should I do this at LL task context? */ + /* If the CRC checks, make sure lengths check! */ + badpkt = 0; + if (crcok) { + switch (pdu_type) { + case BLE_ADV_PDU_TYPE_SCAN_REQ: + case BLE_ADV_PDU_TYPE_ADV_DIRECT_IND: + if (len != BLE_SCAN_REQ_LEN) { + badpkt = 1; + } + break; + case BLE_ADV_PDU_TYPE_SCAN_RSP: + case BLE_ADV_PDU_TYPE_ADV_IND: + case BLE_ADV_PDU_TYPE_ADV_SCAN_IND: + case BLE_ADV_PDU_TYPE_ADV_NONCONN_IND: + if ((len < BLE_DEV_ADDR_LEN) || (len > BLE_ADV_SCAN_IND_MAX_LEN)) { + badpkt = 1; + } + break; + case BLE_ADV_PDU_TYPE_CONNECT_REQ: + if (len != BLE_CONNECT_REQ_LEN) { + badpkt = 1; + } + break; + default: + badpkt = 1; + break; + } + } + + /* If this is a malformed packet, just kill it here */ + if (badpkt) { + ++g_ble_ll_stats.rx_malformed_pkts; + os_mbuf_free(&g_mbuf_pool, rxpdu); + return -1; + } + + /* Setup the mbuf */ + mblen = len + BLE_LL_PDU_HDR_LEN; + OS_MBUF_PKTHDR(rxpdu)->omp_len = mblen; + rxpdu->om_len = mblen; + + rc = -1; + switch (g_ble_ll_data.ll_state) { + case BLE_LL_STATE_ADV: + /* If we get a scan request*/ + if (pdu_type == BLE_ADV_PDU_TYPE_SCAN_REQ) { + /* Just bail if CRC is not good */ + if (crcok) { + rc = ble_ll_adv_rx_scan_req(rxbuf); + if (rc) { + /* XXX: One thing left to reconcile here. We have + * the advertisement schedule element still running. + * How to deal with the end of the advertising event? + * Need to figure that out. + */ + } + } + } else { + if (pdu_type == BLE_ADV_PDU_TYPE_CONNECT_REQ) { + rc = 0; + /* XXX: deal with this */ + } + } + break; + case BLE_LL_STATE_SCANNING: + if (crcok) { + /* + * NOTE: If this returns a positive number there was an error but + * there is no need to disable the PHY on return as that was + * done already. + */ + rc = ble_ll_scan_rx_pdu_end(rxbuf); + } + break; + default: + assert(0); + break; + } + + /* Hand packet up to higher layer */ + ble_ll_rx_pdu_in(rxpdu); + + return rc; +} + +void +ble_ll_task(void *arg) +{ + struct os_event *ev; + + /* Init ble phy */ + ble_phy_init(); + + /* Set output power to 1mW (0 dBm) */ + ble_phy_txpwr_set(0); + + /* Wait for an event */ + while (1) { + ev = os_eventq_get(&g_ble_ll_data.ll_evq); + switch (ev->ev_type) { + case OS_EVENT_T_TIMER: + break; + case BLE_LL_EVENT_HCI_CMD: + /* Process HCI command */ + ble_ll_hci_cmd_proc(ev); + break; + case BLE_LL_EVENT_ADV_TXDONE: + ble_ll_adv_tx_done_proc(ev->ev_arg); + break; + case BLE_LL_EVENT_SCAN_WIN_END: + ble_ll_scan_win_end_proc(ev->ev_arg); + break; + case BLE_LL_EVENT_RX_PKT_IN: + ble_ll_rx_pkt_in_proc(); + break; + default: + assert(0); + break; + } + + /* XXX: we can possibly take any finished schedule items and + free them here. Have a queue for them. */ + } +} + +/** + * ble ll state set + * + * Called to set the current link layer state. + * + * Context: Interrupt and Link Layer task + * + * @param ll_state + */ +void +ble_ll_state_set(int ll_state) +{ + g_ble_ll_data.ll_state = ll_state; +} + +/** + * ble ll event send + * + * Send an event to the Link Layer task + * + * @param ev Event to add to the Link Layer event queue. + */ +void +ble_ll_event_send(struct os_event *ev) +{ + os_eventq_put(&g_ble_ll_data.ll_evq, ev); +} + +/** + * Initialize the Link Layer. Should be called only once + * + * @return int + */ +int +ble_ll_init(void) +{ + /* Initialize the receive queue */ + STAILQ_INIT(&g_ble_ll_data.ll_rx_pkt_q); + + /* Initialize eventq */ + os_eventq_init(&g_ble_ll_data.ll_evq); + + /* Initialize receive packet (from phy) event */ + g_ble_ll_data.ll_rx_pkt_ev.ev_type = BLE_LL_EVENT_RX_PKT_IN; + + /* Initialize LL HCI */ + ble_ll_hci_init(); + + /* Init the scheduler */ + ble_ll_sched_init(); + + /* Initialize advertiser */ + ble_ll_adv_init(); + + /* Initialize a scanner */ + ble_ll_scan_init(); + + /* Initialize the LL task */ + os_task_init(&g_ble_ll_task, "ble_ll", ble_ll_task, NULL, BLE_LL_TASK_PRI, + OS_WAIT_FOREVER, g_ble_ll_stack, BLE_LL_STACK_SIZE); + + return 0; +} +