diff --git a/gex/Client.py b/gex/Client.py index 7a08e69..27d4ecf 100644 --- a/gex/Client.py +++ b/gex/Client.py @@ -152,7 +152,36 @@ class Client: return buffer def bulk_write(self, cs, cmd, bulk, id=None, pld=None): - """ Perform a bulk write. If cs is None, cmd is used as message type """ + """ + Perform a bulk write. If cs is None, cmd is used as message type. + bulk is the data to write. + """ + offer = self.query(cs=cs, cmd=cmd, id=id, pld=pld) + if offer.type != gex.MSG_BULK_WRITE_OFFER: + raise Exception("Bulk write rejected! %s" % offer.data.decode('utf-8')) + + # parse the offer + pp = PayloadParser(offer.data) + max_size = pp.u32() + max_chunk = pp.u32() + + total = len(bulk) + + if max_size < total: + # announce we changed our mind and won't write anything + self.send_raw(type=gex.MSG_BULK_ABORT, id=offer.id) + raise Exception("Bulk write not possible, not enough space (needed %d bytes, max %d)" % (total, max_size)) + + at = 0 + while at < total: + chunklen = min(max_chunk, total - at) + # Send data + rv = self.query_raw(type=gex.MSG_BULK_DATA if chunklen == max_chunk else gex.MSG_BULK_END, + id=offer.id, + pld=bulk[at:at+chunklen]) + if rv.type != gex.MSG_SUCCESS: + raise Exception("Unexpected bulk frame type %d" % rv.type) + at += chunklen diff --git a/main.py b/main.py index daf3bca..c0b432d 100644 --- a/main.py +++ b/main.py @@ -8,6 +8,8 @@ client = gex.Client() buf = client.bulk_read(None, gex.MSG_INI_READ) print(buf.decode('utf-8')) +client.bulk_write(None, gex.MSG_INI_WRITE, buf) + if False: led = gex.Pin(client, 'LED')