//
//  RTKOTAProfile.h
//  RTKOTASDK
//
//  Created by jerome_gu on 2019/4/16.
//  Copyright © 2019 Realtek. All rights reserved.
//

#ifdef RTK_SDK_IS_STATIC_LIBRARY
#import "libRTKLEFoundation.h"
#import "RTKOTAPeripheral.h"
#import "RTKMultiDFUPeripheral.h"
#else
#import <RTKLEFoundation/RTKLEFoundation.h>
#import <RTKOTASDK/RTKOTAPeripheral.h>
#import <RTKOTASDK/RTKMultiDFUPeripheral.h>
#endif


NS_ASSUME_NONNULL_BEGIN


typedef NS_ENUM(uint8_t, RTKOTAAdvbud) {
    RTKOTAAdvbudSingle,
    RTKOTAAdvbudPrimary,
    RTKOTAAdvbudSecondary,
};


/**
 * A RTKOTAProfile object used for manage peripherals that are about to be upgraded.
 *
 * @discussion A RTKOTAProfile is a concrete subclass of RTKLEProfile, with APIs to create RTKOTAPeripheral instance and return RTKDFUPeripheral instances.
 *
 * To upgrade a remote device, you typically have a known CBPeripheral object represents a remote device to upgrade, call -instantiatePeripheralWithCBPeripheral: with the known CBPeripheral to get an RTKOTAPeripheral object. Then call -translatePeripheral:toDFUPeripheralWithCompletion: or -DFUPeripheralOfOTAPeripheral: to get an RTKDFUPeripheral object for start upgrade. Before call any methods, RTKOTAPeripheral object or RTKDFUPeriperahl object should be connected with RTKOTAProfile.
 */
@interface RTKOTAProfile : RTKLEProfile

// Instantiate OTAPeripheral from a known CBPeripheral object.
// You typically already have a CBPeripheral to upgrade within your CBCentralManager.

- (nullable RTKOTAPeripheral*)OTAPeripheralFromCBPeripheral:(CBPeripheral *)peripheral DEPRECATED_MSG_ATTRIBUTE("use -instantiatePeripheralWithCBPeripheral: instead");

/**
 * Create and return a RTKDFUPeripheral object for silent upgrade.
 *
 * @note If a non-nil RTKDFUPeripheral object is return , you still need to call <code>-[RTKOTAProfile connectTo:]</code> to start connection with it, before you can start upgrade procedure.
 *
 * @returns A non-nil RTKDFUPeripheral object is returned if the peripheral support silent upgrade, otherwise nil is returned.
 */
- (nullable RTKDFUPeripheral*)DFUPeripheralOfOTAPeripheral:(RTKOTAPeripheral *)peripheral;


/// Synonymous with <code>-connectTo:</code>
- (void)connectToDFUPeripheral:(RTKDFUPeripheral *)peripheral;


/**
 * Make the remote device transition to a dedicated upgrade running mode for normal upgrade.
 *
 * @discussion This is a asynchronous method. When the transition complete, the handler parameter get called with a RTKDFUPeripheral object.
 *
 * @note When handler get called with a RTKDFUPeripheral object., you still need to call <code>-[RTKOTAProfile connectTo:]</code> to start connection with it, before you can start upgrade procedure.
 */
- (void)translatePeripheral:(RTKOTAPeripheral*)peripheral toDFUPeripheralWithCompletion:(void(^)(BOOL success, NSError *_Nullable err, RTKDFUPeripheral *_Nullable peripheral))handler;


/**
 * Scan for companion peripheral of the passed in peripheral object.
 *
 * @note Only used for RWS upgrade.
 */
- (void)scanCompanionPeripheralOf:(RTKOTAPeripheral *)peripheral withCompletionHandler:(void(^)(BOOL success, NSError*_Nullable err, RTKOTAPeripheral *_Nullable targetPeripheral))handler;

@end



@interface RTKOTAProfile (Protect)

/* Protected */
- (void)_scanDFUPerpheralOf:(RTKOTAPeripheral*)peripheral withCompletion:(void(^)(BOOL success, NSError *_Nullable err, RTKDFUPeripheral *_Nullable peripheral))handler;

@end


NS_ASSUME_NONNULL_END