Skip to content

Commit

Permalink
Simplify GATTS API
Browse files Browse the repository at this point in the history
Remove offset and trans_id.
Stack is expected to deal with long reads/writes, app doesn't need to worry.
  • Loading branch information
rojer committed Dec 6, 2021
1 parent c223f66 commit 0f37448
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 28 deletions.
5 changes: 0 additions & 5 deletions include/mgos_bt_gatts.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,14 @@ struct mgos_bt_gatts_read_arg {
struct mgos_bt_uuid char_uuid;
struct mgos_bt_uuid desc_uuid;
uint16_t handle;
uint32_t trans_id;
uint16_t offset;
};

struct mgos_bt_gatts_write_arg {
struct mgos_bt_uuid svc_uuid;
struct mgos_bt_uuid char_uuid;
struct mgos_bt_uuid desc_uuid;
uint16_t handle;
uint32_t trans_id;
uint16_t offset;
struct mg_str data;
bool need_rsp;
};

struct mgos_bt_gatts_notify_mode_arg {
Expand Down
13 changes: 3 additions & 10 deletions src/esp32/esp32_bt_gatts.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,9 @@ static int esp32_bt_gatts_mtu_event(uint16_t conn_id,
uint16_t mtu, void *arg) {
struct esp32_bt_gatts_connection_entry *ce = arg;
LOG(LL_DEBUG, ("MTU_FN %d st %d mtu %d", conn_id, err->status, mtu));
// Peer may not support MTU exchange procedure.
// Peer may not support MTU exchange procedure, proceed anyway.
if (err->status == 0) {
// ce->gc.mtu = mtu;
ce->gc.mtu = mtu;
}
esp32_bt_gatts_create_sessions(ce);
return 0;
Expand Down Expand Up @@ -374,8 +374,7 @@ int esp32_bt_gatts_event(const struct ble_gap_event *ev, void *arg) {
break;
}
ce->gc.conn_id = conn_id;
// ce->gc.mtu = ble_att_mtu(conn_id);
ce->gc.mtu = 1024; // mtu;
ce->gc.mtu = ble_att_mtu(conn_id);
esp32_bt_addr_to_mgos(&cd.peer_ota_addr, &ce->gc.addr);
SLIST_INIT(&ce->pending_nm);
STAILQ_INIT(&ce->pending_inds);
Expand Down Expand Up @@ -608,8 +607,6 @@ static int esp32_gatts_attr_access_cb(uint16_t ch, uint16_t ah,
struct mgos_bt_gatts_read_arg rarg = {
.svc_uuid = sse->se->uuid,
.handle = ah,
.trans_id = 0,
.offset = 0,
};
struct esp32_bt_service_attr_info *cai = arg;
if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) {
Expand Down Expand Up @@ -648,9 +645,6 @@ static int esp32_gatts_attr_access_cb(uint16_t ch, uint16_t ah,
.svc_uuid = sse->se->uuid,
.handle = ah,
.data = data,
.trans_id = 0,
.offset = 0,
.need_rsp = true,
};
struct esp32_bt_service_attr_info *cai = arg;
if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
Expand Down Expand Up @@ -911,7 +905,6 @@ void mgos_bt_gatts_notify_uuid(struct mgos_bt_gatts_conn *gsc,
}

bool esp32_bt_gatts_start(void) {
LOG(LL_INFO, ("GATTS start, synced? %d", ble_hs_synced()));
return esp32_bt_register_services();
}

Expand Down
1 change: 0 additions & 1 deletion src/mgos_bt_gatts.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ static enum mgos_bt_gatt_status mgos_bt_gatts_read_n(
return MGOS_BT_GATT_STATUS_REQUEST_NOT_SUPPORTED;
}
struct mgos_bt_gatts_read_arg *arg = ev_arg;
if (arg->offset != 0) return MGOS_BT_GATT_STATUS_INVALID_OFFSET;
mgos_bt_gatts_send_resp_data(c, arg, mg_mk_str_n((void *) &handler_arg, n));
(void) c;
(void) ev_arg;
Expand Down
8 changes: 0 additions & 8 deletions src/mgos_bt_js.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,6 @@ static const struct mjs_c_struct_member gatts_read_arg_def[] = {
MJS_STRUCT_FIELD_TYPE_CUSTOM, bt_uuid_to_str},
{"handle", offsetof(struct mgos_bt_gatts_read_arg, handle),
MJS_STRUCT_FIELD_TYPE_UINT16, NULL},
{"transId", offsetof(struct mgos_bt_gatts_read_arg, trans_id),
MJS_STRUCT_FIELD_TYPE_INT, NULL},
{"offset", offsetof(struct mgos_bt_gatts_read_arg, offset),
MJS_STRUCT_FIELD_TYPE_UINT16, NULL},
{NULL},
};

Expand All @@ -216,10 +212,6 @@ static const struct mjs_c_struct_member gatts_write_arg_def[] = {
MJS_STRUCT_FIELD_TYPE_CUSTOM, bt_uuid_to_str},
{"handle", offsetof(struct mgos_bt_gatts_write_arg, handle),
MJS_STRUCT_FIELD_TYPE_UINT16, NULL},
{"transId", offsetof(struct mgos_bt_gatts_write_arg, trans_id),
MJS_STRUCT_FIELD_TYPE_INT, NULL},
{"offset", offsetof(struct mgos_bt_gatts_write_arg, offset),
MJS_STRUCT_FIELD_TYPE_UINT16, NULL},
{"data", offsetof(struct mgos_bt_gatts_write_arg, data),
MJS_STRUCT_FIELD_TYPE_MG_STR, NULL},
{NULL},
Expand Down
12 changes: 8 additions & 4 deletions tools/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,19 @@ def call(self, method, params=None, resp=True):
self._tx_ctl_char.write(struct.pack(">I", reqLen), withResponse=True)
logging.debug(f"Sending request...")
self._data_char.write(reqJSON.encode("ascii"), withResponse=True)
prev_frame_len = 0
while True:
frame_len = struct.unpack(">I", self._rx_ctl_char.read())
frame_len = struct.unpack(">I", self._rx_ctl_char.read())[0]
logging.debug(f"RX frame len: {frame_len}")
if frame_len == 0:
time.sleep(0.1)
continue
frame = json.loads(self._data_char.read())
logging.debug(f"RX Frame data: {frame}")
frame_data, n_chunks = b"", 0
while len(frame_data) < frame_len:
chunk = self._data_char.read()
frame_data += chunk
n_chunks += 1
logging.debug(f"RX Frame data (rec'd in {n_chunks} chunks): {frame_data}")
frame = json.loads(frame_data)
if frame.get("id", 0) != req["id"]:
continue
if "result" in frame:
Expand Down

0 comments on commit 0f37448

Please sign in to comment.