Skip to content

Instantly share code, notes, and snippets.

@sairon

sairon/longwrite.patch Secret

Created Aug 6, 2019
Embed
What would you like to do?
Espruino long writes
diff --git a/targets/nrf5x/bluetooth.c b/targets/nrf5x/bluetooth.c
index b7a11a7..1163655 100644
--- a/targets/nrf5x/bluetooth.c
+++ b/targets/nrf5x/bluetooth.c
@@ -210,9 +210,63 @@ bool bleHighInterval;
static ble_gap_sec_params_t get_gap_sec_params();
+#define MEM_BUFF_SIZE 128
+uint8_t m_buffer[MEM_BUFF_SIZE];
+ble_user_mem_block_t user_mem_block;
+
// -----------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------
+ret_code_t mem_block_get_attr(ble_user_mem_block_t * mem_buffer,
+ uint16_t * attr_handle,
+ uint8_t * p_mem,
+ uint16_t * p_len)
+{
+ uint16_t i = 0;
+ uint16_t handle = BLE_GATT_HANDLE_INVALID;
+ uint16_t val_len = 0;
+ uint16_t val_offset = 0;
+ uint16_t cur_len = 0;
+
+ do
+ {
+ handle = uint16_decode(&(mem_buffer->p_mem[i]));
+
+ if (handle == BLE_GATT_HANDLE_INVALID)
+ {
+ break;
+ }
+
+ if (*attr_handle == BLE_GATT_HANDLE_INVALID) *attr_handle = handle;
+
+ i += sizeof(uint16_t);
+ val_offset = uint16_decode(&(mem_buffer->p_mem[i]));
+ i += sizeof(uint16_t);
+ val_len = uint16_decode(&(mem_buffer->p_mem[i]));
+ i += sizeof(uint16_t);
+
+ if (handle == *attr_handle)
+ {
+ cur_len = val_offset + val_len;
+ if (cur_len <= *p_len)
+ {
+ memcpy((p_mem + val_offset), &(mem_buffer->p_mem[i]), val_len);
+ }
+ else
+ {
+ return NRF_ERROR_NO_MEM;
+ }
+ }
+
+ i += val_len;
+ }
+ while (i < mem_buffer->len);
+
+ *p_len = cur_len;
+ return NRF_SUCCESS;
+}
+
+
/// Checks for error and reports an exception string if there was one, else 0 if no error
JsVar *jsble_get_error_string(uint32_t err_code) {
if (!err_code) return 0;
@@ -1173,14 +1227,33 @@ static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context) {
break; // BLE_GATTS_EVT_TIMEOUT
case BLE_EVT_USER_MEM_REQUEST:
- err_code = sd_ble_user_mem_reply(p_ble_evt->evt.gattc_evt.conn_handle, NULL);
+ err_code = sd_ble_user_mem_reply(p_ble_evt->evt.gattc_evt.conn_handle, &user_mem_block);
APP_ERROR_CHECK_NOT_URGENT(err_code);
break; // BLE_EVT_USER_MEM_REQUEST
+ case BLE_EVT_USER_MEM_RELEASE:
+ {
+ uint16_t len = MEM_BUFF_SIZE;
+ uint16_t handle = BLE_GATT_HANDLE_INVALID;
+ uint8_t *buf = malloc(len * sizeof(uint8_t));
+
+ if (buf != NULL)
+ {
+ if(mem_block_get_attr(&user_mem_block, &handle, buf, &len) != NRF_SUCCESS) {
+ jsiConsolePrintf("failed to read memblock\n");
+ }
+ jsble_queue_pending_buf(BLEP_WRITE, handle, (char *)buf, len);
+ jsble_peripheral_activity(); // flag that we've been busy
+ free(buf);
+ } else {
+ jsiConsolePrintf("malloc failed\n");
+ }
+ } break; // BLE_EVT_USER_MEM_RELEASE
case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
{
ble_gatts_evt_rw_authorize_request_t req;
ble_gatts_rw_authorize_reply_params_t auth_reply;
+ memset(&auth_reply, 0, sizeof(auth_reply));
req = p_ble_evt->evt.gatts_evt.params.authorize_request;
@@ -1190,23 +1263,12 @@ static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context) {
(req.request.write.op == BLE_GATTS_OP_EXEC_WRITE_REQ_NOW) ||
(req.request.write.op == BLE_GATTS_OP_EXEC_WRITE_REQ_CANCEL))
{
- if (req.type == BLE_GATTS_AUTHORIZE_TYPE_WRITE)
- {
- auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE;
- }
- else
- {
- auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_READ;
- }
+ auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE;
+ auth_reply.params.write.gatt_status = BLE_GATT_STATUS_SUCCESS;
+ auth_reply.params.write.update = 1;
+ auth_reply.params.write.len = req.request.write.len;
+ auth_reply.params.write.offset = req.request.write.offset;
- if (req.request.write.op == BLE_GATTS_OP_EXEC_WRITE_REQ_CANCEL)
- {
- auth_reply.params.write.gatt_status = BLE_GATT_STATUS_SUCCESS;
- }
- else
- {
- auth_reply.params.write.gatt_status = APP_FEATURE_NOT_SUPPORTED;
- }
err_code = sd_ble_gatts_rw_authorize_reply(p_ble_evt->evt.gatts_evt.conn_handle,
&auth_reply);
// This can return an error when connecting to EQ3 CC-RT-BLE
@@ -1734,6 +1796,9 @@ static void gap_params_init() {
ble_gap_conn_params_t gap_conn_params;
ble_gap_conn_sec_mode_t sec_mode;
+ user_mem_block.p_mem = m_buffer;
+ user_mem_block.len = MEM_BUFF_SIZE;
+
char deviceName[BLE_GAP_DEVNAME_MAX_LEN];
#if defined(BLUETOOTH_NAME_PREFIX)
strcpy(deviceName,BLUETOOTH_NAME_PREFIX);
@@ -2544,7 +2609,7 @@ void jsble_set_services(JsVar *data) {
char_md.char_props.read = 1;
if (jsvGetBoolAndUnLock(jsvObjectGetChild(charVar, "writable", 0))) {
char_md.char_props.write = 1;
- char_md.char_props.write_wo_resp = 1;
+ char_md.char_props.write_wo_resp = 0;
}
char_md.p_char_user_desc = NULL;
char_md.p_char_pf = NULL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment