No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.m 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // main.m
  3. // BlueTerm
  4. //
  5. // Created by Thomas Buck on 29.05.17.
  6. // Copyright © 2017 Thomas Buck. All rights reserved.
  7. // xythobuz@xythobuz.de
  8. //
  9. // Heavily based on:
  10. // https://gist.github.com/brayden-morris-303/09a738ed9c83a7d14c82
  11. //
  12. // This utility connects to a common Bluetooth 4.0 BLE UART bridge
  13. // and logs all received data to the local filesystem.
  14. // This has been developed for use as a Betaflight blackbox replacement.
  15. #import <Foundation/Foundation.h>
  16. #import <CoreBluetooth/CoreBluetooth.h>
  17. // ---------------------------------------------------------------------------------------
  18. // BLE UART device name that is searched for
  19. static NSString * const kDeviceName = @"xyCopter";
  20. // BLE Service & Characteristic UUIDs for the UART data stream
  21. static NSString * const kUARTServiceUUID = @"ffe0";
  22. static NSString * const kUARTCharacteristicUUID = @"ffe1";
  23. // Default log file path, if no command line argument is given
  24. static NSString * const kDefaultFilePath = @"~/BlueTerm.log";
  25. // ---------------------------------------------------------------------------------------
  26. @interface BlueTerm : NSObject <CBCentralManagerDelegate, CBPeripheralDelegate>
  27. @property (strong, nonatomic) CBCentralManager *centralManager;
  28. @property (strong, nonatomic) CBPeripheral *discoveredPeripheral;
  29. @property (assign) BOOL shouldRun;
  30. @property (strong, nonatomic) NSFileHandle *fileHandle;
  31. @end
  32. @implementation BlueTerm
  33. - (id)init {
  34. if (self = [super init]) {
  35. self.shouldRun = false;
  36. self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
  37. return self;
  38. }
  39. return nil;
  40. }
  41. - (void)centralManagerDidUpdateState:(CBCentralManager *)central {
  42. if (!self.shouldRun) return;
  43. // You should test all scenarios
  44. if (central.state != CBCentralManagerStatePoweredOn) {
  45. NSLog(@"Unknown BLE state: %ld", (long)central.state);
  46. return;
  47. }
  48. if (central.state == CBCentralManagerStatePoweredOn) {
  49. // Scan for devices
  50. [self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:kUARTServiceUUID]] options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
  51. NSLog(@"Scanning started...");
  52. }
  53. }
  54. - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
  55. NSLog(@"Discovered %@ at %@", peripheral.name, RSSI);
  56. if (([peripheral.name isEqualToString:kDeviceName]) && (self.discoveredPeripheral != peripheral)) {
  57. // Save a local copy of the peripheral, so CoreBluetooth doesn't get rid of it
  58. self.discoveredPeripheral = peripheral;
  59. // And connect
  60. NSLog(@"Connecting to peripheral %@", peripheral);
  61. [self.centralManager connectPeripheral:peripheral options:nil];
  62. }
  63. }
  64. - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
  65. NSLog(@"Connected");
  66. [_centralManager stopScan];
  67. NSLog(@"Scanning stopped");
  68. peripheral.delegate = self;
  69. [peripheral discoverServices:@[[CBUUID UUIDWithString:kUARTServiceUUID]]];
  70. }
  71. - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
  72. if (error) {
  73. NSLog(@"Error: %@", error);
  74. return;
  75. }
  76. for (CBService *service in peripheral.services) {
  77. NSLog(@"Discovered service: %@", service.UUID);
  78. if ([service.UUID isEqual:[CBUUID UUIDWithString:kUARTServiceUUID]]) {
  79. NSLog(@"This is our service. Discovering characteristics...");
  80. [peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:kUARTCharacteristicUUID]] forService:service];
  81. }
  82. }
  83. }
  84. - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
  85. if (error) {
  86. return;
  87. }
  88. for (CBCharacteristic *characteristic in service.characteristics) {
  89. NSLog(@"Discovered characteristic: %@", characteristic.UUID);
  90. if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:kUARTCharacteristicUUID]]) {
  91. NSLog(@"This is our characteristic! Subscribing...");
  92. [peripheral setNotifyValue:YES forCharacteristic:characteristic];
  93. }
  94. }
  95. }
  96. - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
  97. if (error) {
  98. return;
  99. }
  100. static time_t lastTime = 0;
  101. time_t now = time(NULL);
  102. if ((now - lastTime) > 0) {
  103. NSString *valueString = [[[[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding] stringByReplacingOccurrencesOfString:@"\n" withString:@""] stringByReplacingOccurrencesOfString:@"\r" withString:@""];
  104. NSLog(@"Received more data (excerpt: \"%@\")", valueString);
  105. lastTime = now;
  106. }
  107. [self.fileHandle writeData:characteristic.value];
  108. }
  109. - (BOOL)initializeTargetFile:(int)argc withParams:(const char **)argv {
  110. NSString *filePath;
  111. if (argc == 1) {
  112. // Use hard-coded default path
  113. filePath = [kDefaultFilePath stringByExpandingTildeInPath];
  114. } else if (argc == 2) {
  115. // Use given path
  116. filePath = [[NSString stringWithCString:argv[1] encoding:NSUTF8StringEncoding] stringByExpandingTildeInPath];
  117. } else {
  118. NSLog(@"Usage:\n\t%@ [/path/to/log]", [NSString stringWithCString:argv[0] encoding:NSUTF8StringEncoding]);
  119. return false;
  120. }
  121. [[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
  122. self.fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
  123. if (self.fileHandle) {
  124. NSLog(@"Writing received data to: \"%@\"!", filePath);
  125. NSLog(@"File has been overwritten (if it existed before)!");
  126. [self.fileHandle seekToEndOfFile];
  127. } else {
  128. NSLog(@"Error opening logfile: \"%@\"!", filePath);
  129. return false;
  130. }
  131. self.shouldRun = true;
  132. return true;
  133. }
  134. - (void)cleanup {
  135. if (self.discoveredPeripheral) {
  136. if ([self.discoveredPeripheral state] != CBPeripheralStateDisconnected) {
  137. NSLog(@"Disconnecting from device...");
  138. [self.centralManager cancelPeripheralConnection:self.discoveredPeripheral];
  139. }
  140. }
  141. if (self.fileHandle) {
  142. NSLog(@"Closing logfile...");
  143. [self.fileHandle closeFile];
  144. self.fileHandle = nil;
  145. }
  146. exit(0);
  147. }
  148. @end
  149. BlueTerm *bt;
  150. void signalHandler(int sig) {
  151. [bt cleanup];
  152. }
  153. int main(int argc, const char * argv[]) {
  154. @autoreleasepool {
  155. NSLog(@"Initializing BlueTerm...");
  156. if (signal(SIGINT, signalHandler) == SIG_ERR) {
  157. NSLog(@"Error, can't catch SIGINT...");
  158. return 1;
  159. }
  160. bt = [[BlueTerm alloc] init];
  161. if (![bt initializeTargetFile:argc withParams:argv]) {
  162. return 2;
  163. }
  164. [[NSRunLoop currentRunLoop] run];
  165. }
  166. return 0;
  167. }