async Task _WriteValueAsync(ReadOnlyMemory <byte> data, GattWriteOption option)
        {
            if (option == GattWriteOption.WriteWithoutResponse)
            {
                var(fd, mtu) = await proxy.AcquireWriteAsync(new Dictionary <string, object>());

                // do low level file I/O in background task
                await Task.Run(() => {
                    try {
                        using (var dataHandle = data.Pin()) {
                            unsafe {
                                var written = 0;
                                while (written < data.Length)
                                {
                                    var ret = write((int)fd.DangerousGetHandle(), dataHandle.Pointer, (UIntPtr)data.Length);
                                    if ((int)ret == -1)
                                    {
                                        var err = Marshal.GetLastWin32Error();
                                        if (err == EINTR || err == EAGAIN)
                                        {
                                            continue;
                                        }
                                        // TODO: marshal other errors to more specific exceptions
                                        throw new Exception($"IO error: {Marshal.PtrToStringAnsi(strerror(err))}");
                                    }
                                    written += (int)ret;
                                }
                            }
                        }
                    }
                    finally {
                        fd.Close();
                    }
                });
            }
            else
            {
                if (data.Length <= 20)
                {
                    await proxy.WriteValueAsync(data.ToArray(), new Dictionary <string, object>());
                }
                else
                {
                    var options = new Dictionary <string, object> {
                        { "offset", (ushort)0 }
                    };
                    for (ushort i = 0; i < data.Length; i += 20)
                    {
                        options["offset"] = i;
                        await proxy.WriteValueAsync(data.Slice(i, Math.Min(data.Length - i, 20)).ToArray(), options);
                    }
                }
            }
        }
Beispiel #2
0
 public Task WriteValueAsync(byte[] value)
 {
     return(_gattCharacteristic1Proxy.WriteValueAsync(value, new Dictionary <string, object>()));
 }