From fa9856a79f67fe677340f5d21dd29da30647c1f5 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 1 Sep 2015 12:26:56 +0100 Subject: works_quickly --- ble.c | 91 +++++++++++++++++++++++++++++------------------- ble.h | 5 +++ dfu.c | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- prototypes.h | 4 +++ 4 files changed, 174 insertions(+), 37 deletions(-) diff --git a/ble.c b/ble.c index 8e8c6c8..7bf7231 100644 --- a/ble.c +++ b/ble.c @@ -429,6 +429,23 @@ notify_cb (uint16_t value_handle, const uint8_t * value, int i; BLE *ble = user_data; + + + if ((value_handle == ble->cp_handle) && (length == 5) + && (value[0] == OP_CODE_PKT_RCPT_NOTIF)) + { + uint32_t u32; + memcpy (&u32, &value[1], 4); + ble->notify_pkts = u32; + if (ble->notify_waiting_for_pkts) + mainloop_quit (); + ble->notify_waiting_for_pkts = 0; + + return; + } + + + printf ("Handle Value Not/Ind: 0x%04x - ", value_handle); if (length == 0) @@ -444,21 +461,18 @@ notify_cb (uint16_t value_handle, const uint8_t * value, printf ("\n"); - if (value_handle != ble->cp_handle) - return; - if (length != 3) - return; - if (value[0] != OP_CODE_RESPONSE) - return; - if (value[1] != ble->notify_waiting_for_op) - return; + if ((value_handle == ble->cp_handle) && (length == 3) + && (value[0] == OP_CODE_RESPONSE) + && (value[1] == ble->notify_waiting_for_op)) + { + ble->notify_code = value[2]; + printf ("Got response => 0x%02x\n", value[2]); + mainloop_quit (); + } - ble->notify_code = value[2]; - printf ("Got response => 0x%02x\n", value[2]); - mainloop_quit (); } @@ -593,32 +607,12 @@ ble_send_data (BLE * ble, uint8_t * buf, size_t len) int ble_send_data_noresp (BLE * ble, uint8_t * buf, size_t len) { - size_t mtu = 16, tx; - - printf ("Sending data:\n"); - - - hexdump (buf, (len < 64) ? len : 64); - - - while (len) + if (!bt_gatt_client_write_without_response + (ble->gatt, ble->data_handle, false, buf, len)) { - - printf ("%u bytes left\n", (unsigned) len); - - tx = (len > mtu) ? mtu : len; - - if (!bt_gatt_client_write_without_response - (ble->gatt, ble->data_handle, false, buf, tx)) - { - printf ("Failed to initiate write procedure\n"); - return EXIT_FAILURE; - } - - len -= tx; - buf += tx; + printf ("Failed to initiate write procedure\n"); + return EXIT_FAILURE; } - return EXIT_SUCCESS; } @@ -642,3 +636,30 @@ ble_wait_run (BLE * ble) return ble->notify_code; } + +void +ble_notify_pkts_start (BLE * ble) +{ + ble->notify_waiting_for_pkts = 1; +} + + +void +ble_notify_pkts_stop (BLE * ble) +{ + ble->notify_waiting_for_pkts = 0; +} + +size_t +ble_notify_get_pkts (BLE * ble) +{ + size_t ret = 0; + + mainloop_run (); + + if (!ble->notify_waiting_for_pkts) + ret = ble->notify_pkts; + ble->notify_waiting_for_pkts = 1; + + return ret; +} diff --git a/ble.h b/ble.h index b59bf90..b4e6c9d 100644 --- a/ble.h +++ b/ble.h @@ -20,6 +20,11 @@ struct ble int notify_waiting_for_op; int notify_code; + + + size_t notify_pkts; + int notify_waiting_for_pkts; + }; typedef struct ble BLE; diff --git a/dfu.c b/dfu.c index 6dd835e..3d22133 100644 --- a/dfu.c +++ b/dfu.c @@ -1,6 +1,97 @@ #include "project.h" #include "dfu.h" +static int +send_data_quickly (BLE * b, uint8_t * d, size_t sz, int pkts) +{ + char bs[64]; + size_t mtu = 16; + size_t off = 0; + size_t tx, rx; + int sent; + + ble_notify_pkts_start (b); + + memset (bs, '\b', sizeof (bs)); + + while (off != sz) + { + fwrite (bs, 1, sizeof (bs), stdout); + printf ("%6u/%6u bytes", (unsigned) off, (unsigned) sz); + fflush (stdout); + + tx = (sz - off); + if (tx > mtu) + tx = mtu; + + if (ble_send_data_noresp (b, d + off, tx)) + { + printf ("\nFailed\n"); + ble_notify_pkts_stop (b); + return EXIT_FAILURE; + } + + off += tx; + sent++; + + if (sent == pkts) + { + rx = ble_notify_get_pkts (b); + + if (rx != off) + { + printf ("\nFailed -Lost a packet\n"); + ble_notify_pkts_stop (b); + return EXIT_FAILURE; + } + sent = 0; + } + + } + + ble_notify_pkts_stop (b); + + printf ("\nDone\n"); + return EXIT_SUCCESS; + +} + + +static int +send_data_slowly (BLE * b, uint8_t * d, size_t sz) +{ + char bs[64]; + size_t mtu = 16; + size_t off = 0; + size_t tx; + + memset (bs, '\b', sizeof (bs)); + + while (off != sz) + { + fwrite (bs, 1, sizeof (bs), stdout); + printf ("%6u/%6u bytes", (unsigned) off, (unsigned) sz); + fflush (stdout); + + tx = (sz - off); + if (tx > mtu) + tx = mtu; + + if (ble_send_data (b, d + off, tx)) + { + printf ("\nFailed\n"); + return EXIT_FAILURE; + } + + off += tx; + } + + + printf ("\nDone\n"); + return EXIT_SUCCESS; + +} + void dfu (const char *bdaddr, const char *type, const char *version, uint8_t * dat, size_t dat_sz, uint8_t * bin, size_t bin_sz) @@ -69,9 +160,11 @@ dfu (const char *bdaddr, const char *type, const char *version, uint8_t * dat, break; +#if 1 // ble_wait_setup(b,OP_CODE_PKT_RCPT_NOTIF_REQ); +// buf[0] = OP_CODE_PKT_RCPT_NOTIF_REQ; - u16 = 0x10; + u16 = 0x40; memcpy (&buf[1], &u16, 2); if (ble_send_cp (b, buf, 3)) @@ -80,19 +173,33 @@ dfu (const char *bdaddr, const char *type, const char *version, uint8_t * dat, // if (ble_wait_run(b)!=BLE_DFU_RESP_VAL_SUCCESS) // break; + ble_wait_setup (b, OP_CODE_RECEIVE_FW); + + buf[0] = OP_CODE_RECEIVE_FW; + if (ble_send_cp (b, buf, 1)) + break; + + if (send_data_quickly (b, bin, bin_sz, u16) != EXIT_SUCCESS) + break; + if (ble_wait_run (b) != BLE_DFU_RESP_VAL_SUCCESS) + break; +#else + ble_wait_setup (b, OP_CODE_RECEIVE_FW); buf[0] = OP_CODE_RECEIVE_FW; if (ble_send_cp (b, buf, 1)) break; - if (ble_send_data (b, bin, bin_sz)) + if (send_data_slowly (b, bin, bin_sz) != EXIT_SUCCESS) break; + if (ble_wait_run (b) != BLE_DFU_RESP_VAL_SUCCESS) break; +#endif ble_wait_setup (b, OP_CODE_VALIDATE); buf[0] = OP_CODE_VALIDATE; diff --git a/prototypes.h b/prototypes.h index 570f4f7..e16dc69 100644 --- a/prototypes.h +++ b/prototypes.h @@ -15,8 +15,12 @@ extern BLE *ble_open (const char *bdaddr); extern int ble_register_notify (BLE * ble); extern int ble_send_cp (BLE * ble, uint8_t * buf, size_t len); extern int ble_send_data (BLE * ble, uint8_t * buf, size_t len); +extern int ble_send_data_noresp (BLE * ble, uint8_t * buf, size_t len); extern void ble_wait_setup (BLE * ble, uint8_t op); extern int ble_wait_run (BLE * ble); +extern void ble_notify_pkts_start (BLE * ble); +extern void ble_notify_pkts_stop (BLE * ble); +extern size_t ble_notify_get_pkts (BLE * ble); /* manifest.c */ extern json_object *_json_object_object_get (json_object * obj, const char *name); -- cgit v1.2.3