Skip to content

Commit 96f5fbe

Browse files
committed
[wip] Add GATT Ranging Service
1 parent 6c30f96 commit 96f5fbe

File tree

12 files changed

+3885
-74
lines changed

12 files changed

+3885
-74
lines changed

nimble/host/include/host/ble_cs.h

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,86 @@
2323
#define H_BLE_CS_
2424
#include "syscfg/syscfg.h"
2525

26+
#define BLE_HS_CS_MODE0 (0)
27+
#define BLE_HS_CS_MODE1 (1)
28+
#define BLE_HS_CS_MODE2 (2)
29+
#define BLE_HS_CS_MODE3 (3)
30+
#define BLE_HS_CS_MODE_UNUSED (0xff)
31+
#define BLE_HS_CS_SUBMODE_TYPE BLE_HS_CS_MODE_UNUSED
32+
33+
#define BLE_HS_CS_ROLE_INITIATOR (0)
34+
#define BLE_HS_CS_ROLE_REFLECTOR (1)
35+
2636
#define BLE_CS_EVENT_CS_PROCEDURE_COMPLETE (0)
37+
#define BLE_CS_EVENT_CS_STEP_DATA (1)
38+
39+
#define BLE_HS_CS_TOA_TOD_NOT_AVAILABLE (0x00008000)
40+
#define BLE_HS_CS_N_AP_MAX (4)
41+
42+
struct ble_cs_mode0_result {
43+
uint16_t measured_freq_offset;
44+
uint8_t packet_quality;
45+
uint8_t packet_rssi;
46+
uint8_t packet_antenna;
47+
};
48+
49+
struct ble_cs_mode1_result {
50+
uint32_t packet_pct1;
51+
uint32_t packet_pct2;
52+
int16_t toa_tod;
53+
uint8_t packet_quality;
54+
uint8_t packet_nadm;
55+
uint8_t packet_rssi;
56+
uint8_t packet_antenna;
57+
};
58+
59+
struct ble_cs_mode2_result {
60+
uint32_t tone_pct[BLE_HS_CS_N_AP_MAX + 1];
61+
uint8_t tone_quality_ind[BLE_HS_CS_N_AP_MAX + 1];
62+
uint8_t antenna_paths[4];
63+
uint8_t antenna_path_permutation_id;
64+
};
65+
66+
struct ble_cs_mode3_result {
67+
uint32_t packet_pct1;
68+
uint32_t packet_pct2;
69+
uint32_t tone_pct[BLE_HS_CS_N_AP_MAX + 1];
70+
uint8_t tone_quality_ind[BLE_HS_CS_N_AP_MAX + 1];
71+
uint8_t antenna_paths[4];
72+
int16_t toa_tod;
73+
uint8_t antenna_path_permutation_id;
74+
uint8_t packet_quality;
75+
uint8_t packet_nadm;
76+
uint8_t packet_rssi;
77+
uint8_t packet_antenna;
78+
};
2779

2880
struct ble_cs_event {
81+
uint16_t conn_handle;
82+
uint8_t status;
2983
uint8_t type;
30-
union
31-
{
32-
struct
33-
{
34-
uint16_t conn_handle;
35-
uint8_t status;
36-
} procedure_complete;
84+
union {
85+
struct {
86+
uint8_t role;
87+
uint8_t mode;
88+
uint8_t *data;
89+
} step_data;
3790
};
38-
3991
};
4092

4193
typedef int ble_cs_event_fn(struct ble_cs_event *event, void *arg);
4294

43-
struct ble_cs_initiator_procedure_start_params {
95+
struct ble_cs_procedure_start_params {
4496
uint16_t conn_handle;
45-
ble_cs_event_fn *cb;
46-
void *cb_arg;
4797
};
4898

49-
struct ble_cs_reflector_setup_params {
99+
struct ble_cs_setup_params {
100+
uint16_t conn_handle;
50101
ble_cs_event_fn *cb;
51102
void *cb_arg;
52103
};
53104

54-
int ble_cs_initiator_procedure_start(const struct ble_cs_initiator_procedure_start_params *params);
55-
int ble_cs_initiator_procedure_terminate(uint16_t conn_handle);
56-
int ble_cs_reflector_setup(struct ble_cs_reflector_setup_params *params);
105+
int ble_cs_procedure_start(const struct ble_cs_procedure_start_params *params);
106+
int ble_cs_procedure_terminate(uint16_t conn_handle);
107+
int ble_cs_setup(struct ble_cs_setup_params *params);
57108
#endif
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#ifndef H_BLE_PEER_
21+
#define H_BLE_PEER_
22+
23+
#include "os/mynewt.h"
24+
#ifdef __cplusplus
25+
extern "C" {
26+
#endif
27+
28+
/** Peer. */
29+
struct ble_peer_dsc {
30+
SLIST_ENTRY(ble_peer_dsc) next;
31+
struct ble_gatt_dsc dsc;
32+
};
33+
SLIST_HEAD(ble_peer_dsc_list, ble_peer_dsc);
34+
35+
struct ble_peer_chr {
36+
SLIST_ENTRY(ble_peer_chr) next;
37+
struct ble_gatt_chr chr;
38+
39+
struct ble_peer_dsc_list dscs;
40+
};
41+
SLIST_HEAD(ble_peer_chr_list, ble_peer_chr);
42+
43+
struct ble_peer_svc {
44+
SLIST_ENTRY(ble_peer_svc) next;
45+
struct ble_gatt_svc svc;
46+
47+
struct ble_peer_chr_list chrs;
48+
};
49+
SLIST_HEAD(ble_peer_svc_list, ble_peer_svc);
50+
51+
struct ble_peer;
52+
typedef void ble_peer_disc_fn(const struct ble_peer *peer, int status, void *arg);
53+
54+
struct ble_peer {
55+
SLIST_ENTRY(ble_peer) next;
56+
57+
uint16_t conn_handle;
58+
59+
/** List of discovered GATT services. */
60+
struct ble_peer_svc_list svcs;
61+
62+
/** Keeps track of where we are in the service discovery process. */
63+
uint16_t disc_prev_chr_val;
64+
struct ble_peer_svc *cur_svc;
65+
66+
/** Callback that gets executed when service discovery completes. */
67+
ble_peer_disc_fn *disc_cb;
68+
void *disc_cb_arg;
69+
};
70+
71+
struct ble_peer *ble_peer_find(uint16_t conn_handle);
72+
int ble_peer_disc_all(uint16_t conn_handle, ble_peer_disc_fn *disc_cb, void *disc_cb_arg);
73+
const struct ble_peer_dsc *ble_peer_dsc_find_uuid(const struct ble_peer *peer,
74+
const ble_uuid_t *svc_uuid, const ble_uuid_t *chr_uuid, const ble_uuid_t *dsc_uuid);
75+
const struct ble_peer_chr *ble_peer_chr_find_uuid(const struct ble_peer *peer,
76+
const ble_uuid_t *svc_uuid, const ble_uuid_t *chr_uuid);
77+
const struct ble_peer_svc *ble_peer_svc_find_uuid(const struct ble_peer *peer, const ble_uuid_t *uuid);
78+
int ble_peer_delete(uint16_t conn_handle);
79+
int ble_peer_add(uint16_t conn_handle);
80+
81+
#ifdef __cplusplus
82+
}
83+
#endif
84+
85+
#endif

nimble/host/pkg.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,9 @@ pkg.req_apis:
5454

5555
pkg.down.BLE_HS_STOP_ON_SHUTDOWN:
5656
ble_hs_shutdown: 200
57+
58+
pkg.init.'BLE_PEER':
59+
ble_peer_init: $after:ble_transport_hs_init
60+
61+
pkg.init.'BLE_CHANNEL_SOUNDING':
62+
ble_cs_init: $after:ble_transport_hs_init
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#ifndef H_BLE_SVC_RAS_
21+
#define H_BLE_SVC_RAS_
22+
23+
#include <inttypes.h>
24+
#include "host/ble_cs.h"
25+
26+
#ifdef __cplusplus
27+
extern "C" {
28+
#endif
29+
30+
#define BLE_CONN_HANDLE_INVALID 0xffff
31+
32+
#define BLE_SVC_RAS_SVC_RANGING_SERVICE_UUID 0x185B
33+
#define BLE_SVC_RAS_CHR_RAS_FEATURES_UUID 0x2C14
34+
#define BLE_SVC_RAS_CHR_REAL_TIME_RANGING_DATA_UUID 0x2C15
35+
#define BLE_SVC_RAS_CHR_ON_DEMAND_RANGING_DATA_UUID 0x2C16
36+
#define BLE_SVC_RAS_CHR_RAS_CONTROL_POINT_UUID 0x2C17
37+
#define BLE_SVC_RAS_CHR_RANGING_DATA_READY_UUID 0x2C18
38+
#define BLE_SVC_RAS_CHR_RANGING_DATA_OVERWRITTEN_UUID 0x2C19
39+
40+
#define BLE_SVC_RAS_FILTER_MODE0_POSITION (0)
41+
#define BLE_SVC_RAS_FILTER_MODE1_POSITION (BLE_SVC_RAS_FILTER_MODE0_POSITION << 14)
42+
#define BLE_SVC_RAS_FILTER_MODE2_POSITION (BLE_SVC_RAS_FILTER_MODE1_POSITION << 14)
43+
#define BLE_SVC_RAS_FILTER_MODE3_POSITION (BLE_SVC_RAS_FILTER_MODE3_POSITION << 14)
44+
45+
#define BLE_SVC_RAS_FILTER_MODE0_MASK (0xF)
46+
#define BLE_SVC_RAS_FILTER_MODE1_MASK (0x7F)
47+
#define BLE_SVC_RAS_FILTER_MODE2_MASK (0x7F)
48+
#define BLE_SVC_RAS_FILTER_MODE3_MASK (0x3FFF)
49+
50+
#define BLE_SVC_RAS_FILTER_MODE0_PACKET_QUALITY (0)
51+
#define BLE_SVC_RAS_FILTER_MODE0_PACKET_RSSI (1)
52+
#define BLE_SVC_RAS_FILTER_MODE0_PACKET_ANTENNA (2)
53+
#define BLE_SVC_RAS_FILTER_MODE0_MEASURED_FREQ_OFFSET (3)
54+
55+
#define BLE_SVC_RAS_FILTER_MODE1_PACKET_QUALITY (0)
56+
#define BLE_SVC_RAS_FILTER_MODE1_PACKET_NADM (1)
57+
#define BLE_SVC_RAS_FILTER_MODE1_PACKET_RSSI (2)
58+
#define BLE_SVC_RAS_FILTER_MODE1_TOD_TOA (3)
59+
#define BLE_SVC_RAS_FILTER_MODE1_PACKET_ANTENNA (4)
60+
#define BLE_SVC_RAS_FILTER_MODE1_PACKET_PCT1 (5)
61+
#define BLE_SVC_RAS_FILTER_MODE1_PACKET_PCT2 (6)
62+
63+
#define BLE_SVC_RAS_FILTER_MODE2_ANTENNA_PERMUTATION_ID (0)
64+
#define BLE_SVC_RAS_FILTER_MODE2_TONE_PCT (1)
65+
#define BLE_SVC_RAS_FILTER_MODE2_TONE_QUALITY (2)
66+
#define BLE_SVC_RAS_FILTER_MODE2_ANTENNA_PATH_1 (3)
67+
#define BLE_SVC_RAS_FILTER_MODE2_ANTENNA_PATH_2 (4)
68+
#define BLE_SVC_RAS_FILTER_MODE2_ANTENNA_PATH_3 (5)
69+
#define BLE_SVC_RAS_FILTER_MODE2_ANTENNA_PATH_4 (6)
70+
71+
#define BLE_SVC_RAS_FILTER_MODE3_PACKET_QUALITY (0)
72+
#define BLE_SVC_RAS_FILTER_MODE3_PACKET_NADM (1)
73+
#define BLE_SVC_RAS_FILTER_MODE3_PACKET_RSSI (2)
74+
#define BLE_SVC_RAS_FILTER_MODE3_TOD_TOA (3)
75+
#define BLE_SVC_RAS_FILTER_MODE3_PACKET_ANTENNA (4)
76+
#define BLE_SVC_RAS_FILTER_MODE3_PACKET_PCT1 (5)
77+
#define BLE_SVC_RAS_FILTER_MODE3_PACKET_PCT2 (6)
78+
#define BLE_SVC_RAS_FILTER_MODE3_ANTENNA_PERMUTATION_ID (7)
79+
#define BLE_SVC_RAS_FILTER_MODE3_TONE_PCT (8)
80+
#define BLE_SVC_RAS_FILTER_MODE3_TONE_QUALITY (9)
81+
#define BLE_SVC_RAS_FILTER_MODE3_ANTENNA_PATH_1 (10)
82+
#define BLE_SVC_RAS_FILTER_MODE3_ANTENNA_PATH_2 (11)
83+
#define BLE_SVC_RAS_FILTER_MODE3_ANTENNA_PATH_3 (12)
84+
#define BLE_SVC_RAS_FILTER_MODE3_ANTENNA_PATH_4 (13)
85+
86+
#define BLE_SVC_RAS_CP_CMD_GET_RANGING_DATA (0x00)
87+
#define BLE_SVC_RAS_CP_CMD_ACK_RANGING_DATA (0x01)
88+
#define BLE_SVC_RAS_CP_CMD_RETRIEVE_LOST_SEGMENT (0x02)
89+
#define BLE_SVC_RAS_CP_CMD_ABORT_OPERATION (0x03)
90+
#define BLE_SVC_RAS_CP_CMD_SET_FILTER (0x04)
91+
92+
#define BLE_SVC_RAS_CP_RSP_COMPLETE_RANGING_DATA (0x00)
93+
#define BLE_SVC_RAS_CP_RSP_COMPLETE_LOST_SEGMENT (0x01)
94+
#define BLE_SVC_RAS_CP_RSP_RESPONSE_CODE (0x02)
95+
96+
#define BLE_SVC_RAS_CP_RSPCODE_SUCCESS (0x01)
97+
#define BLE_SVC_RAS_CP_RSPCODE_OP_CODE_NOT_SUPPORTED (0x02)
98+
#define BLE_SVC_RAS_CP_RSPCODE_INVALID_PARAMETER (0x03)
99+
#define BLE_SVC_RAS_CP_RSPCODE_SUCCESS_PERSISTED (0x04)
100+
#define BLE_SVC_RAS_CP_RSPCODE_ABORT_UNSUCCESSFUL (0x05)
101+
#define BLE_SVC_RAS_CP_RSPCODE_PROCEDURE_NOT_COMPLETED (0x06)
102+
#define BLE_SVC_RAS_CP_RSPCODE_SERVER_BUSY (0x07)
103+
#define BLE_SVC_RAS_CP_RSPCODE_NO_RECORDS_FOUND (0x08)
104+
105+
#define BLE_SVC_RAS_MODE_REAL_TIME (0)
106+
#define BLE_SVC_RAS_MODE_ON_DEMAND (1)
107+
108+
#if MYNEWT_VAL(BLE_SVC_RAS_SERVER)
109+
void ble_svc_ras_init(void);
110+
int ble_svc_ras_ranging_data_body_init(uint16_t conn_handle, uint16_t procedure_counter, uint8_t config_id,
111+
uint8_t tx_power, uint8_t antenna_paths_mask);
112+
int ble_svc_ras_ranging_subevent_init(uint16_t conn_handle, uint16_t start_acl_conn_event, uint16_t frequency_compensation,
113+
uint8_t ranging_done_status, uint8_t subevent_done_status,
114+
uint8_t ranging_abort_reason, uint8_t subevent_abort_reason,
115+
uint8_t reference_power_level, uint8_t number_of_steps_reported);
116+
int ble_svc_ras_ranging_subevent_update_status(uint16_t conn_handle, uint8_t number_of_steps_reported,
117+
uint8_t ranging_done_status, uint8_t subevent_done_status,
118+
uint8_t ranging_abort_reason, uint8_t subevent_abort_reason);
119+
int ble_svc_ras_add_step_mode(uint16_t conn_handle, uint8_t mode, uint8_t status);
120+
int ble_svc_ras_add_mode0_result(struct ble_cs_mode0_result *result, uint16_t conn_handle, uint8_t local_role);
121+
int ble_svc_ras_add_mode1_result(struct ble_cs_mode1_result *result, uint16_t conn_handle, uint8_t rtt_pct_included);
122+
int ble_svc_ras_add_mode2_result(struct ble_cs_mode2_result *result, uint16_t conn_handle, uint8_t n_ap);
123+
int ble_svc_ras_add_mode3_result(struct ble_cs_mode3_result *result, uint16_t conn_handle, uint8_t n_ap, uint8_t rtt_pct_included);
124+
int ble_svc_ras_ranging_data_ready(uint16_t conn_handle);
125+
#endif
126+
127+
#if MYNEWT_VAL(BLE_SVC_RAS_CLIENT)
128+
struct ble_svc_ras_clt_ranging_header {
129+
uint16_t ranging_counter;
130+
uint8_t config_id;
131+
uint8_t tx_power;
132+
uint8_t antenna_paths_mask;
133+
};
134+
135+
struct ble_svc_ras_clt_subevent_header {
136+
uint16_t start_acl_conn_event;
137+
uint16_t frequency_compensation;
138+
uint8_t done_status;
139+
uint8_t abort_reason;
140+
uint8_t reference_power_level;
141+
uint8_t number_of_steps_reported;
142+
};
143+
144+
typedef void ble_svc_ras_clt_subscribe_cb(uint16_t conn_handle);
145+
typedef void ble_svc_ras_clt_step_data_received_cb(void *data, uint16_t conn_handle, uint8_t step_mode);
146+
int ble_svc_ras_clt_config_set(uint16_t conn_handle, uint8_t rtt_pct_included, uint8_t n_ap, uint8_t local_role);
147+
int ble_svc_ras_clt_subscribe(ble_svc_ras_clt_subscribe_cb *subscribe_cb,
148+
ble_svc_ras_clt_step_data_received_cb *step_data_cb,
149+
uint16_t conn_handle, uint8_t mode);
150+
#endif
151+
#ifdef __cplusplus
152+
}
153+
#endif
154+
155+
#endif

nimble/host/services/ras/pkg.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
20+
pkg.name: nimble/host/services/ras
21+
pkg.description: Implements the RAS Service.
22+
pkg.author: "Apache Mynewt <[email protected]>"
23+
pkg.homepage: "http://mynewt.apache.org/"
24+
pkg.keywords:
25+
- ble
26+
- bluetooth
27+
- nimble
28+
- ras
29+
30+
pkg.deps:
31+
- nimble/host
32+
33+
pkg.init.'BLE_SVC_RAS_SERVER':
34+
ble_svc_ras_init: 'MYNEWT_VAL(BLE_SVC_RAS_SYSINIT_STAGE)'
35+
36+
pkg.init.'BLE_SVC_RAS_CLIENT':
37+
ble_svc_ras_clt_init: 'MYNEWT_VAL(BLE_SVC_RAS_SYSINIT_STAGE)'
38+
39+
pkg.source_files.'BLE_SVC_RAS_SERVER':
40+
- "src/ble_svc_ras.c"
41+
42+
pkg.source_files.'BLE_SVC_RAS_CLIENT':
43+
- "src/ble_svc_ras_clt.c"

0 commit comments

Comments
 (0)