Exemple #1
0
        /// <summary>
        /// Write data to data point characteristic
        /// </summary>
        /// <param name="crc32"></param>
        /// <param name="file"></param>
        /// <param name="device"></param>
        /// <param name="MTU"></param>
        /// <param name="transferBytes"></param>
        /// <param name="remoteOffset"></param>
        /// <returns></returns>
        private async Task <int> TransferData(IGattCharacteristic packetPoint, CRC32 crc, Stream file, /*int prn = -1,*/ int offsetStart = 0, int offsetEnd = -1, int MTU = 20)
        {
            const int RESERVED_BYTES = 3;
            int       bytesWritten   = 0;

            try
            {
                int MAX_READ_BYTES = MTU - RESERVED_BYTES;
                Debug.WriteLineIf(LogLevelDebug, String.Format("Start of data transfer flen: {0}", file.Length));
                file.Seek(offsetStart, SeekOrigin.Begin);

                byte[] data = new byte[MAX_READ_BYTES];

                for (int packetsSent = 0, offset = offsetStart; ;)
                {
                    int bytesToRead = MAX_READ_BYTES;
                    if (offsetEnd > -1)
                    {
                        int endOffsetDiff = offsetEnd - offset;
                        if (endOffsetDiff <= 0)
                        {
                            break;
                        }
                        bytesToRead = Math.Min(bytesToRead, endOffsetDiff);
                    }
                    Debug.WriteLineIf(LogLevelDebug, String.Format("Reading {0} bytes from file stream", bytesToRead));
                    int size = file.Read(data, 0, bytesToRead);
                    if (size <= 0)
                    {
                        break;
                    }
                    offset += size;
                    byte[] pending;

                    if (size < MAX_READ_BYTES)
                    {
                        pending = new byte[size];
                        Array.Copy(data, pending, size);
                    }
                    else
                    {
                        pending = data;
                    }

                    // Specific for iOS, if no timeout, then sending happens to be blocked
                    await Task.Delay(1);

                    await packetPoint.WriteWithoutResponse(pending).Timeout(OperationTimeout);

                    packetsSent++;
                    bytesWritten += pending.Length;

                    crc.Update(pending);
                    Debug.WriteLineIf(LogLevelDebug, String.Format("### CRC32 Step value, crc: {0} offset:{2} size: {1} packetLocalNo:{3}", crc.Value, pending.Length, offset, packetsSent));
                }
                Debug.WriteLineIf(LogLevelDebug, String.Format("End of data transfer"));
            }
            catch (Exception ex)
            {
                Debug.WriteLineIf(LogLevelDebug, String.Format("Error while transfering data"));
                Debug.Write(ex);
            }
            return(bytesWritten);
        }