NSInputStream *is = [NSInputStream inputStreamWithURL:self.updateFile];
uint8_t buffer[OTA_MAX_BUFFER]; // 20B buffer
NSInteger bytesRead;
NSInteger totalBytesRead = 0;
[is open];
while((bytesRead = [is read:buffer maxLength:OTA_MAX_BUFFER])) {
totalBytesRead += bytesRead;
if(totalBytesRead > OTA_MAX_DATA) {
[self writeUpdateControl:ATPeripheralUpdateControlCancel];
[self failUpdateWithError:ATPeripheralUpdateErrorDataTooLarge];
return;
}
[self.peripheral writeValue:[NSData dataWithBytes:buffer length:bytesRead] forCharacteristic:self.updateDataCharacteristic type:CBCharacteristicWriteWithoutResponse];
}
[is close];
if(totalBytesRead == 0) {
[self writeUpdateControl:ATPeripheralUpdateControlCancel];
[self failUpdateWithError:ATPeripheralUpdateErrorCouldNotReadFile];
return;
}
#if logDebug
NSLog(@"Wrote OTA data, %ld bytes", (long)totalBytesRead);
#endif