FirmwareWriteController.m 27.4キロバイト
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
//
//  FirmwareWriteController.m
//  jacket_ios
//
//  Created by ドラッサル 亜嵐 on 2017/06/06.
//  Copyright © 2017年 ドラッサル 亜嵐. All rights reserved.
//

#import "FirmwareWriteController.h"
#import "FirmwareWriteControllerTableViewCell.h"
#import "Operation.h"
#import "DBManager.h"
#import "NSData+NSString.h"

@interface FirmwareWriteController ()

@end

@implementation FirmwareWriteController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    selectionEnabled = 1;
    
    arrResult = [[NSMutableArray alloc] init];
    
    UINib *nib = [UINib nibWithNibName:@"FirmwareWriteControllerTableViewCell" bundle:nil];
    [tblFirmware registerNib:nib forCellReuseIdentifier:@"mycell"];
    tblFirmware.delegate = self;
    tblFirmware.dataSource = self;
    
    [arrResult removeAllObjects];
    
    NSArray *result = [[DBManager sharedInstance] getFirmwareDataList];
    
    for(id key in result) {
        NSString *valueId = [key objectAtIndex:0];
        NSString *valueUuid = [key objectAtIndex:1];
        NSString *valueDeviceType = [key objectAtIndex:2];
        NSString *valueDeviceModel = [key objectAtIndex:3];
        NSString *valueVersion = [key objectAtIndex:4];
        NSString *valueVersionStamp = [key objectAtIndex:5];
        NSString *valueFile = [key objectAtIndex:6];
        
        NSMutableDictionary *newDict = [NSMutableDictionary dictionary];
        [newDict setObject:valueId forKey:@"id"];
        [newDict setObject:valueUuid forKey:@"uuid"];
        [newDict setObject:valueDeviceType forKey:@"deviceType"];
        [newDict setObject:valueDeviceModel forKey:@"deviceModel"];
        [newDict setObject:valueVersion forKey:@"version"];
        [newDict setObject:valueVersionStamp forKey:@"versionStamp"];
        [newDict setObject:valueFile forKey:@"file"];
        
        [arrResult addObject:newDict];
    }
    [tblFirmware reloadData];
}

- (void)viewWillAppear:(BOOL)animated {
    self.lastProtocolDelegate = self.protocol.delegate;
    self.protocol.delegate = self;
}

- (void)viewWillDisappear:(BOOL)animated {
    self.protocol.delegate = self.lastProtocolDelegate;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)cancelButtonClicked:(id)sender {
    [self dismissViewControllerAnimated:YES completion:Nil];
}

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if(selectionEnabled == 1) {
        return indexPath;
    } else {
        return nil;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = [indexPath row];
    NSDictionary *dataRecord = [arrResult objectAtIndex:row];
    
    NSString *filename = [dataRecord valueForKey:@"uuid"];
    
    //selectionEnabled = 0;
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* path = [documentsDirectory stringByAppendingPathComponent:
                      [NSString stringWithFormat: @"/save/%@",filename]];
    firmwareFileStreamSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil] fileSize];
    firmwareFileStream = [[NSInputStream alloc] initWithFileAtPath:path];
    [firmwareFileStream open];
    
    startTime = CACurrentMediaTime();
    firmwareUpdateState = ERASE_PAGE_1;
    //firmwareUpdateState = UPLOAD_DONE;
    firmwareFileStreamPointer = 0;
    firmwareWriteBufferRetry = 0;
    
    [self firmwareWriteTask];
    
    NSLog(@"bin file readout complete");
}

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @[
             [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive
                                                title:@"削除"
                                              handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
                                                  // own delete action
                                                  
                                                  
                                                  // Distructive button tapped.
                                                  NSLog(@"削除の確認");
                                                  
                                                  NSDictionary * selectedData = [arrResult objectAtIndex:indexPath.row];
                                                  NSString *firmwareTitle = [selectedData valueForKey:@"file"];
                                                  
                                                  UIAlertController *actionSheetConfirm = [UIAlertController alertControllerWithTitle:@"削除の確認" message:[NSString stringWithFormat:@"「%@」を削除します。よろしいですか?",firmwareTitle] preferredStyle:UIAlertControllerStyleAlert];
                                                  
                                                  [actionSheetConfirm addAction:[UIAlertAction actionWithTitle:@"削除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *actionConfirm) {
                                                      
                                                      
                                                      // Get the record ID of the selected name and set it to the recordIDToEdit property.
                                                      NSDictionary * selectedData = [arrResult objectAtIndex:indexPath.row];
                                                      
                                                      NSString *recordId = [selectedData valueForKey:@"id"];
                                                      NSLog(@"recordId = %@",recordId);
                                                      //NSString *firmwareFilename = [selectedData valueForKey:@"file"];
                                                      NSString *firmwareFilename = [selectedData valueForKey:@"uuid"];
                                                      NSLog(@"firmwareFilename = %@",firmwareFilename);
                                                      
                                                      
                                                      NSString *docPath = [NSSearchPathForDirectoriesInDomains
                                                                           (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                                                      NSString *docPathFull = [docPath stringByAppendingPathComponent:[NSString stringWithFormat:@"/save/%@",firmwareFilename]];
                                                      //NSURL *docPathFullUrl = [NSURL URLWithString:docPathFull];
                                                      
                                                      
                                                      NSFileManager *fileManager = [NSFileManager defaultManager];
                                                      NSError *error;
                                                      BOOL success = [fileManager removeItemAtPath:docPathFull error:&error];
                                                      if(success) {
                                                          NSLog(@"削除した");
                                                          [[DBManager sharedInstance] deleteFirmwareData:recordId];
                                                          //[self populateDetail];
                                                          //[self.tblResult reloadData];
                                                          [arrResult removeObjectAtIndex:indexPath.row];
                                                          [tblFirmware deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
                                                      } else {
                                                          NSLog(@"削除を失敗した");
                                                          [[DBManager sharedInstance] deleteFirmwareData:recordId];
                                                          //[self populateDetail];
                                                          //[self.tblResult reloadData];
                                                          [arrResult removeObjectAtIndex:indexPath.row];
                                                          [tblFirmware deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
                                                      }
                                                  }]];
                                                  [actionSheetConfirm addAction:[UIAlertAction actionWithTitle:@"キャンセル" style:UIAlertActionStyleDefault handler:^(UIAlertAction *actionConfirm) {
                                                      // Distructive button tapped.
                                                      NSLog(@"キャンセルの確認");
                                                  }]];
                                                  [self presentViewController:actionSheetConfirm animated:YES completion:nil];
                                              }],
             ];
}

- (BOOL)firmwareWriteTask {
    int packetDataSize = 18;
    int readLength = 18;
    unsigned long long firmwareFlashPageSize = 0x8000;
    //unsigned long long firmwareFlashPageSize = 0x100;
    
    switch(firmwareUpdateState) {
        case ERASE_PAGE_1:
            [[self protocol]putData:@"firmwareFlashErase" data:@"1"];
            break;
        case UPLOAD_PAGE_10_ERASE_TEMP:
            [[self protocol]putData:@"firmwareBufferClear" data:nil];
            break;
        case UPLOAD_PAGE_10_BEGIN:
        case UPLOAD_PAGE_11_BEGIN:
        case UPLOAD_PAGE_12_BEGIN:
        case UPLOAD_PAGE_13_BEGIN:
            firmwareFlashPagePointer = firmwareFlashPageSize;
            switch(firmwareUpdateState) {
                case UPLOAD_PAGE_10_BEGIN:
                    firmwareUpdateState = UPLOAD_PAGE_10_CONTINUE;
                    break;
                case UPLOAD_PAGE_11_BEGIN:
                    firmwareUpdateState = UPLOAD_PAGE_11_CONTINUE;
                    break;
                case UPLOAD_PAGE_12_BEGIN:
                    firmwareUpdateState = UPLOAD_PAGE_12_CONTINUE;
                    break;
                case UPLOAD_PAGE_13_BEGIN:
                    firmwareUpdateState = UPLOAD_PAGE_13_CONTINUE;
                    break;
                default:
                    break;
            }
        case UPLOAD_PAGE_10_CONTINUE:
        case UPLOAD_PAGE_11_CONTINUE:
        case UPLOAD_PAGE_12_CONTINUE:
        case UPLOAD_PAGE_13_CONTINUE:
        {
            if([firmwareFileStream hasBytesAvailable]) {
                switch(firmwareUpdateState) {
                    case UPLOAD_PAGE_10_CONTINUE:
                        if((firmwareFileStreamPointer + readLength) > (firmwareFlashPageSize - 1)) {
                            readLength = (int)(firmwareFlashPageSize - firmwareFileStreamPointer);
                        }
                        break;
                    case UPLOAD_PAGE_11_CONTINUE:
                        if((firmwareFileStreamPointer + readLength) > (firmwareFlashPageSize * 2 - 1)) {
                            readLength = (int)(firmwareFlashPageSize * 2 - firmwareFileStreamPointer);
                        }
                        break;
                    case UPLOAD_PAGE_12_CONTINUE:
                        if((firmwareFileStreamPointer + readLength) > (firmwareFlashPageSize * 3 - 1)) {
                            readLength = (int)(firmwareFlashPageSize * 3 - firmwareFileStreamPointer);
                        }
                        break;
                    case UPLOAD_PAGE_13_CONTINUE:
                        if((firmwareFileStreamPointer + readLength) > (firmwareFlashPageSize * 4 - 1)) {
                            readLength = (int)(firmwareFlashPageSize * 4 - firmwareFileStreamPointer);
                        }
                        break;
                    default:
                        break;
                }
                unsigned char outputBuffer[20];
                unsigned char outputBufferData[20];
                for(int i = 0; i < 20; i++) {
                    outputBuffer[i] = 0;
                    outputBufferData[i] = 0;
                }
                NSInteger length = [firmwareFileStream read:outputBufferData maxLength:readLength];
                firmwareFileStreamPointer += length;
                firmwareFlashPagePointer -= length;
                
                
                int indexFrom = 0;
                int indexTo = 0;
                outputBuffer[indexTo++] = 'x';
                uint8_t outputBufferChecksum = 0x00;
                while(indexFrom < length) {
                    outputBufferChecksum ^= outputBufferData[indexFrom];
                    outputBuffer[indexTo++] = outputBufferData[indexFrom++];
                }
                while(indexTo < (packetDataSize + 1)) {
                    outputBufferChecksum ^= 0xFF;
                    outputBuffer[indexTo++] = 0xFF;
                }
                outputBuffer[indexTo++] = outputBufferChecksum;
                
                firmwareWriteBuffer = [NSData dataWithBytes:outputBuffer length:indexTo];
                
                NSUInteger firmwareWriteBufferLength = [firmwareWriteBuffer length];
                Byte *firmwareWriteBufferLengthByteData = (Byte*)malloc(firmwareWriteBufferLength);
                memcpy(firmwareWriteBufferLengthByteData, [firmwareWriteBuffer bytes], firmwareWriteBufferLength);
                NSMutableString *firmwareWriteBufferString = [NSMutableString stringWithCapacity:0];
                for(int i = 0; i < [firmwareWriteBuffer length]; i++) {
                    [firmwareWriteBufferString appendFormat:@"%02x ", firmwareWriteBufferLengthByteData[i]];
                }
                CFTimeInterval elapsedTime = CACurrentMediaTime() - startTime;
                NSLog(@"BLE   SEND(%d) %@  %llu / %llu %.2f%%  %fsec",firmwareWriteBufferRetry,firmwareWriteBufferString,firmwareFileStreamPointer,firmwareFileStreamSize,(float)firmwareFileStreamPointer / (float)firmwareFileStreamSize * 100, elapsedTime);
                lblPercentComplete.text = [NSString stringWithFormat:@"%llu / %llu %.2f%%  %.02f秒",firmwareFileStreamPointer,firmwareFileStreamSize,(float)firmwareFileStreamPointer / (float)firmwareFileStreamSize * 100, elapsedTime];
                
                [self.protocol bleWriteRaw:firmwareWriteBuffer];
            } else if (firmwareFileStreamPointer == firmwareFileStreamSize) {
                switch(firmwareUpdateState) {
                    case UPLOAD_PAGE_10_CONTINUE:
                        firmwareUpdateState = UPLOAD_PAGE_10_END;
                        break;
                    case UPLOAD_PAGE_11_CONTINUE:
                        firmwareUpdateState = UPLOAD_PAGE_11_END;
                        break;
                    case UPLOAD_PAGE_12_CONTINUE:
                        firmwareUpdateState = UPLOAD_PAGE_12_END;
                        break;
                    case UPLOAD_PAGE_13_CONTINUE:
                        firmwareUpdateState = UPLOAD_PAGE_13_END;
                        break;
                    default:
                        break;
                }
                [self firmwareWriteTask];
            } else {
                NSLog(@"Should not get here, something bad bad happened!!!");
                NSLog(@"firmwareFileStreamPointer should never be larger than firmwareFileStreamSize, ever!!!");
            }
            
            //NSData *nsData = [@"fw@" dataUsingEncoding:NSUTF8StringEncoding];
            //[self.protocol putData:@"infoDeviceId" data:nil];
            
            return true;
            break;
        }
        case UPLOAD_PAGE_10_RETRY:
        case UPLOAD_PAGE_11_RETRY:
        case UPLOAD_PAGE_12_RETRY:
        case UPLOAD_PAGE_13_RETRY:
        {
            NSUInteger firmwareWriteBufferLength = [firmwareWriteBuffer length];
            
            Byte *firmwareWriteBufferLengthByteData = (Byte*)malloc(firmwareWriteBufferLength);
            memcpy(firmwareWriteBufferLengthByteData, [firmwareWriteBuffer bytes], firmwareWriteBufferLength);
            NSMutableString *firmwareWriteBufferString = [NSMutableString stringWithCapacity:0];
            for(int i = 0; i < [firmwareWriteBuffer length]; i++) {
                [firmwareWriteBufferString appendFormat:@"%02x ", firmwareWriteBufferLengthByteData[i]];
            }
            CFTimeInterval elapsedTime = CACurrentMediaTime() - startTime;
            NSLog(@"BLE RESEND(%d) %@  %llu / %llu %.2f%%  %fsec",firmwareWriteBufferRetry,firmwareWriteBufferString,firmwareFileStreamPointer,firmwareFileStreamSize,(float)firmwareFileStreamPointer / (float)firmwareFileStreamSize * 100, elapsedTime);
            [self.protocol bleWriteRaw:firmwareWriteBuffer];
            
            //NSData *nsData = [@"fw@" dataUsingEncoding:NSUTF8StringEncoding];
            //[self.protocol putData:@"infoDeviceId" data:nil];
            
            return true;
            break;
        }
        case UPLOAD_PAGE_10_END:
            firmwareUpdateState = WRITE_PAGE_10;
            [self firmwareWriteTask];
            break;
        case WRITE_PAGE_10:
            [[self protocol]putData:@"firmwareFlashWrite" data:@"10"];
            break;
        case WRITE_PAGE_10_DONE:
            if (firmwareFileStreamPointer != firmwareFileStreamSize) {
                firmwareUpdateState = UPLOAD_PAGE_11_ERASE_TEMP;
            } else {
                firmwareUpdateState = UPLOAD_DONE;
            }
            [self firmwareWriteTask];
            break;
        case UPLOAD_PAGE_11_ERASE_TEMP:
            [[self protocol]putData:@"firmwareBufferClear" data:nil];
            break;
        case UPLOAD_PAGE_11_END:
            firmwareUpdateState = WRITE_PAGE_11;
            [self firmwareWriteTask];
            break;
        case WRITE_PAGE_11:
            [[self protocol]putData:@"firmwareFlashWrite" data:@"11"];
            break;
        case WRITE_PAGE_11_DONE:
            if (firmwareFileStreamPointer != firmwareFileStreamSize) {
                firmwareUpdateState = UPLOAD_PAGE_12_ERASE_TEMP;
            } else {
                firmwareUpdateState = UPLOAD_DONE;
            }
            [self firmwareWriteTask];
            break;
        case UPLOAD_PAGE_12_ERASE_TEMP:
            [[self protocol]putData:@"firmwareBufferClear" data:nil];
            break;
        case UPLOAD_PAGE_12_END:
            firmwareUpdateState = WRITE_PAGE_12;
            [self firmwareWriteTask];
            break;
        case WRITE_PAGE_12:
            [[self protocol]putData:@"firmwareFlashWrite" data:@"12"];
            break;
        case WRITE_PAGE_12_DONE:
            if (firmwareFileStreamPointer != firmwareFileStreamSize) {
                firmwareUpdateState = UPLOAD_PAGE_13_ERASE_TEMP;
            } else {
                firmwareUpdateState = UPLOAD_DONE;
            }
            [self firmwareWriteTask];
            break;
        case UPLOAD_PAGE_13_ERASE_TEMP:
            [[self protocol]putData:@"firmwareBufferClear" data:nil];
            break;
        case UPLOAD_PAGE_13_END:
            firmwareUpdateState = WRITE_PAGE_13;
            [self firmwareWriteTask];
            break;
        case WRITE_PAGE_13:
            [[self protocol]putData:@"firmwareFlashWrite" data:@"13"];
            break;
        case WRITE_PAGE_13_DONE:
            firmwareUpdateState = UPLOAD_DONE;
            [self firmwareWriteTask];
            break;
        case UPLOAD_DONE:
            //firmwareUpdateState = EEPROM_WP_OFF;
            //[self firmwareWriteTask];
        {
            CFTimeInterval elapsedTime = CACurrentMediaTime() - startTime;
            NSLog(@"Firmware upload done!  %fsec", elapsedTime);
            break;
        }
        case EEPROM_WP_OFF:
            [[self protocol]putData:@"eepromWriteProtect" data:@"0"];
            break;
        case EEPROM_WP_OFF_DONE:
            firmwareUpdateState = FIRMWARE_IMAGE_SELECT_1;
            [self firmwareWriteTask];
            break;
        case FIRMWARE_IMAGE_SELECT_1:
            [[self protocol]putData:@"firmwareImageSelect" data:@"1"];
            break;
        case FIRMWARE_IMAGE_SELECT_1_DONE:
            firmwareUpdateState = RESET;
            [self firmwareWriteTask];
            break;
        case RESET:
            [[self protocol]putData:@"systemReset" data:nil];
            break;
        case RESET_DONE:
        {
            CFTimeInterval elapsedTime = CACurrentMediaTime() - startTime;
            NSLog(@"Firmware upload done!  %fsec", elapsedTime);
            break;
        }
    }
    return false;
}

- (void)protocolDidGetData:(NSString *)dataType data:(NSString *)dataData {
    if([dataType isEqualToString:@"firmwareFlashErase"]) {
        if([dataData isEqualToString: @"1"]) {
            if(firmwareUpdateState == ERASE_PAGE_1) {
                firmwareUpdateState = UPLOAD_PAGE_10_ERASE_TEMP;
                [self firmwareWriteTask];
            }
        }
    } else if([dataType isEqualToString:@"firmwareBufferClear"]) {
        switch(firmwareUpdateState) {
            case UPLOAD_PAGE_10_ERASE_TEMP:
                firmwareUpdateState = UPLOAD_PAGE_10_BEGIN;
                break;
            case UPLOAD_PAGE_11_ERASE_TEMP:
                firmwareUpdateState = UPLOAD_PAGE_11_BEGIN;
                break;
            case UPLOAD_PAGE_12_ERASE_TEMP:
                firmwareUpdateState = UPLOAD_PAGE_12_BEGIN;
                break;
            case UPLOAD_PAGE_13_ERASE_TEMP:
                firmwareUpdateState = UPLOAD_PAGE_13_BEGIN;
                break;
            default:
                break;
        }
        [self firmwareWriteTask];
    } else if([dataType isEqualToString:@"firmwareBufferWrite"]) {
        if([dataData isEqualToString: @"OK"]) {
            if(firmwareWriteBufferRetry != 0) {
                firmwareWriteBufferRetry = 0;
                switch(firmwareUpdateState) {
                    case UPLOAD_PAGE_10_RETRY:
                        firmwareUpdateState = UPLOAD_PAGE_10_CONTINUE;
                        break;
                    case UPLOAD_PAGE_11_RETRY:
                        firmwareUpdateState = UPLOAD_PAGE_11_CONTINUE;
                        break;
                    case UPLOAD_PAGE_12_RETRY:
                        firmwareUpdateState = UPLOAD_PAGE_12_CONTINUE;
                        break;
                    case UPLOAD_PAGE_13_RETRY:
                        firmwareUpdateState = UPLOAD_PAGE_13_CONTINUE;
                        break;
                    default:
                        break;
                }
            }
            if(firmwareFlashPagePointer == 0) {
                switch(firmwareUpdateState) {
                    case UPLOAD_PAGE_10_CONTINUE:
                        firmwareUpdateState = UPLOAD_PAGE_10_END;
                        break;
                    case UPLOAD_PAGE_11_CONTINUE:
                        firmwareUpdateState = UPLOAD_PAGE_11_END;
                        break;
                    case UPLOAD_PAGE_12_CONTINUE:
                        firmwareUpdateState = UPLOAD_PAGE_12_END;
                        break;
                    case UPLOAD_PAGE_13_CONTINUE:
                        firmwareUpdateState = UPLOAD_PAGE_13_END;
                        break;
                    default:
                        break;
                }
            }
            [self firmwareWriteTask];
        } else {
            if(firmwareWriteBufferRetry < 10) {
                firmwareWriteBufferRetry++;
                switch(firmwareUpdateState) {
                    case UPLOAD_PAGE_10_CONTINUE:
                        firmwareUpdateState = UPLOAD_PAGE_10_RETRY;
                        break;
                    case UPLOAD_PAGE_11_CONTINUE:
                        firmwareUpdateState = UPLOAD_PAGE_11_RETRY;
                        break;
                    case UPLOAD_PAGE_12_CONTINUE:
                        firmwareUpdateState = UPLOAD_PAGE_12_RETRY;
                        break;
                    case UPLOAD_PAGE_13_CONTINUE:
                        firmwareUpdateState = UPLOAD_PAGE_13_RETRY;
                        break;
                    default:
                        break;
                }
                [self firmwareWriteTask];
            } else {
                NSLog(@"BLE RESEND(%d) RETRY EXCEED, GIVE UP",firmwareWriteBufferRetry);
            }
        }
    } else if([dataType isEqualToString:@"firmwareFlashWrite"]) {
        if([dataData isEqualToString:@"10"]) {
            NSLog(@"firmwareFlashWrite 10");
            firmwareUpdateState = WRITE_PAGE_10_DONE;
            [self firmwareWriteTask];
        } else if([dataData isEqualToString:@"11"]) {
            NSLog(@"firmwareFlashWrite 11");
            firmwareUpdateState = WRITE_PAGE_11_DONE;
            [self firmwareWriteTask];
        } else if([dataData isEqualToString:@"12"]) {
            NSLog(@"firmwareFlashWrite 12");
            firmwareUpdateState = WRITE_PAGE_12_DONE;
            [self firmwareWriteTask];
        } else if([dataData isEqualToString:@"13"]) {
            NSLog(@"firmwareFlashWrite 13");
            firmwareUpdateState = WRITE_PAGE_13_DONE;
            [self firmwareWriteTask];
        }
    } else if([dataType isEqualToString:@"firmwareImageSelect"]) {
        if([dataData isEqualToString:@"1"]) {
            NSLog(@"firmwareImageSelect 1");
            firmwareUpdateState = FIRMWARE_IMAGE_SELECT_1_DONE;
            [self firmwareWriteTask];
        }
    } else if([dataType isEqualToString:@"eepromWriteProtect"]) {
        if([dataData isEqualToString:@"0"]) {
            NSLog(@"firmwareImageSelect 0");
            firmwareUpdateState = EEPROM_WP_OFF_DONE;
            [self firmwareWriteTask];
        }
    } else if([dataType isEqualToString:@"systemReset"]) {
        NSLog(@"firmwareImageSelect 1");
        firmwareUpdateState = RESET_DONE;
        [self firmwareWriteTask];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [arrResult count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"mycell";
    FirmwareWriteControllerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    
    NSInteger row = [indexPath row];
    NSDictionary *dataRecord = [arrResult objectAtIndex:row];
    
    // Configure the cell...
    UIFont *newFont = [UIFont fontWithName:@"Arial" size:11.0];
    cell.lblTitle.font = newFont;
    cell.lblTitle.text = [dataRecord valueForKey:@"file"];
    
    newFont = [UIFont fontWithName:@"Arial" size:11.0];
    cell.lblSubtitle.font = newFont;
    cell.lblSubtitle.text = [dataRecord valueForKey:@"uuid"];
    
    return cell;
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end