Skip to content

Commit

Permalink
Merge branch 'library/sample_app' into 'master'
Browse files Browse the repository at this point in the history
ESPProvision Sample App and Library Update

See merge request idf/esp-idf-provisioning-ios!10
  • Loading branch information
shahpiyushv committed Jun 30, 2020
2 parents f0ee7e5 + 5266b9f commit 1329f25
Show file tree
Hide file tree
Showing 138 changed files with 4,948 additions and 34 deletions.
4 changes: 2 additions & 2 deletions ESPProvision.podspec
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Pod::Spec.new do |spec|

spec.name = "ESPProvision"
spec.version = "2.0.1"
spec.version = "2.0.2"
spec.summary = "ESP-IDF provisioning in Swift"
spec.description = "It provides mechanism to provide network credentials and/or custom data to an ESP32 or ESP32-S2 devices"
spec.description = "It provides mechanism to provide network credentials and/or custom data to an ESP32, ESP32-S2 or ESP8266 devices"
spec.homepage = "https://github.com/espressif/esp-idf-provisioning-ios"

spec.license = { :type => 'Apache License, Version 2.0',
Expand Down
2 changes: 1 addition & 1 deletion ESPProvision/Crypto/ESPSecurity1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class ESPSecurity1: ESPCodeable {
ESPLog.log("Step0 response processed.")
clientVerify = verifyBytes
} catch {
print(error)
ESPLog.log(error.localizedDescription)
}
}

Expand Down
53 changes: 32 additions & 21 deletions ESPProvision/ESPDevice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ public class ESPDevice {
var session:ESPSession!
/// Name of device.
var deviceName: String
/// Security implementation.
var security: ESPSecurity
/// Mode of transport.
var transport: ESPTransport
/// BLE transport layer.
var espBleTransport: ESPBleTransport!
/// SoftAp transport layer.
Expand All @@ -76,6 +72,10 @@ public class ESPDevice {
var bleConnectionStatusHandler: ((ESPSessionStatus) -> Void)?
/// List of capabilities of a device.
var capabilities: [String]?
/// Security implementation.
public var security: ESPSecurity
/// Mode of transport.
public var transport: ESPTransport
/// Delegate of `ESPDevice` object.
public var delegate:ESPDeviceConnectionDelegate?
/// Security layer of device.
Expand Down Expand Up @@ -342,7 +342,14 @@ public class ESPDevice {
}

/// Disconnect `ESPDevice`.
func disconnect() {
public func disconnect() {
ESPLog.log("Disconnecting device..")
switch transport {
case .ble:
espBleTransport.disconnect()
default:
NEHotspotConfigurationManager.shared.removeConfiguration(forSSID: self.name)
}

}

Expand All @@ -367,7 +374,7 @@ public class ESPDevice {
/// - Parameter completionHandler: The completion handler that is called when session is initalised
/// Parameter of block include status of session.
private func initialiseSession(completionHandler: @escaping (ESPSessionStatus) -> Void) {
print("Initialise session")
ESPLog.log("Initialise session")

if let capability = self.capabilities, capability.contains(ESPConstants.noSecCapability) {
if security != .unsecure {
Expand All @@ -390,17 +397,17 @@ public class ESPDevice {
pop = self.proofOfPossession ?? ""
}
}
print("Initialise session security 1")
ESPLog.log("Initialise session security 1")
securityLayer = ESPSecurity1(proofOfPossession: pop)
case .unsecure:
print("Initialise session security 0")
ESPLog.log("Initialise session security 0")
securityLayer = ESPSecurity0()
}
initSession(completionHandler: completionHandler)
}

private func initSession(completionHandler: @escaping (ESPSessionStatus) -> Void) {
print("Init session")
ESPLog.log("Init session")
switch transport {
case .ble:
session = ESPSession(transport: espBleTransport, security: securityLayer)
Expand All @@ -409,13 +416,13 @@ public class ESPDevice {
}
session.initialize(response: nil) { error in
guard error == nil else {
print("Init session error")
print("Error in establishing session \(error.debugDescription)")
ESPLog.log("Init session error")
ESPLog.log("Error in establishing session \(error.debugDescription)")
self.connectionStatus = .failedToConnect(.sessionInitError)
completionHandler(self.connectionStatus)
return
}
print("Init session success")
ESPLog.log("Init session success")
self.connectionStatus = .connected
completionHandler(.connected)
}
Expand All @@ -427,7 +434,7 @@ public class ESPDevice {
private func getDeviceVersionInfo(completionHandler: @escaping (ESPSessionStatus) -> Void) {
switch transport {
case .ble:
print("Get Device Version Info")
ESPLog.log("Get Device Version Info")
espBleTransport.SendConfigData(path: espBleTransport.utility.versionPath, data: Data("ESP".utf8)) { response, error in
self.processVersionInfoResponse(response: response, error: error, completionHandler: completionHandler)
}
Expand All @@ -445,16 +452,16 @@ public class ESPDevice {
/// - error: Error encountered if any.
/// - completionHandler: Invoked when error is encountered while processing version information.
private func processVersionInfoResponse(response: Data?, error: Error?, completionHandler: @escaping (ESPSessionStatus) -> Void) {
print("Process version info start")
ESPLog.log("Process version info start")
guard error == nil else {
print("Process version info error")
ESPLog.log("Process version info error")
self.connectionStatus = .failedToConnect(.versionInfoError(error!))
completionHandler(self.connectionStatus)
return
}
do {
if let result = try JSONSerialization.jsonObject(with: response!, options: .mutableContainers) as? NSDictionary {
print("Process version info success")
ESPLog.log("Process version info success")
switch transport {
case .ble:
self.espBleTransport.utility.deviceVersionInfo = result
Expand All @@ -463,13 +470,17 @@ public class ESPDevice {
}
if let prov = result[ESPConstants.provKey] as? NSDictionary, let capabilities = prov[ESPConstants.capabilitiesKey] as? [String] {
self.capabilities = capabilities
self.initialiseSession(completionHandler: completionHandler)
DispatchQueue.main.async {
self.initialiseSession(completionHandler: completionHandler)
}
}
}
} catch {
print("Process version info catch")
self.initialiseSession(completionHandler: completionHandler)
print(error)
ESPLog.log("Process version info catch")
DispatchQueue.main.async {
self.initialiseSession(completionHandler: completionHandler)
}
ESPLog.log(error.localizedDescription)
}
}

Expand Down Expand Up @@ -518,7 +529,7 @@ extension ESPDevice: ESPScanWifiListProtocol {

extension ESPDevice: ESPBLEStatusDelegate {
func peripheralConnected() {
print("Peripheral Connected")
ESPLog.log("Peripheral Connected")
self.getDeviceVersionInfo(completionHandler: bleConnectionStatusHandler!)
}

Expand Down
4 changes: 2 additions & 2 deletions ESPProvision/ESPProvision.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class ESPProvision {
let configResponse = try Espressif_WiFiConfigPayload(serializedData: decryptedResponse)
responseStatus = configResponse.respGetStatus.status
} catch {
print(error)
ESPLog.log(error.localizedDescription)
}
return responseStatus
}
Expand All @@ -216,7 +216,7 @@ class ESPProvision {
let configResponse = try Espressif_WiFiConfigPayload(serializedData: decryptedResponse)
responseStatus = configResponse.respApplyConfig.status
} catch {
print(error)
ESPLog.log(error.localizedDescription)
}
return responseStatus
}
Expand Down
14 changes: 11 additions & 3 deletions ESPProvision/ESPProvisionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ public class ESPProvisionManager: NSObject, AVCaptureMetadataOutputObjectsDelega

}

/// Search for `ESPDevice` using bluetooth scan.
/// SoftAp search is not yet supported in iOS
/// Scan for `ESPDevice` using QR code.
///
/// - Parameters:
/// - scanView: Camera preview layer will be added as subview of this `UIView` parameter.
Expand All @@ -116,6 +115,15 @@ public class ESPProvisionManager: NSObject, AVCaptureMetadataOutputObjectsDelega
}
}

/// Stop camera session that is capturing QR code. Call this method when your `Scan View` goes out of scope.
///
public func stopScan() {
ESPLog.log("Stopping Camera Session..")
if self.captureSession != nil {
self.captureSession.stopRunning()
}
}

/// Refresh device list with current transport and security settings.
///
/// - Parameter completionHandler: The completion handler is called when refresh is completed. Result
Expand Down Expand Up @@ -260,7 +268,7 @@ public class ESPProvisionManager: NSObject, AVCaptureMetadataOutputObjectsDelega
self.searchCompletionHandler = nil
self.scanCompletionHandler = completionHandler
self.security = security
espBleTransport = ESPBleTransport(scanTimeout: 5.0, deviceNamePrefix: deviceName, proofOfPossession: proofOfPossession ?? "")
espBleTransport = ESPBleTransport(scanTimeout: 5.0, deviceNamePrefix: deviceName, proofOfPossession: proofOfPossession)
espBleTransport.scan(delegate: self)
default:
let newDevice = ESPDevice(name: deviceName, security: security, transport: transport,proofOfPossession: proofOfPossession, softAPPassword: softAPPassword)
Expand Down
2 changes: 1 addition & 1 deletion ESPProvision/ESPSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class ESPSession {
ESPLog.log("Initializing the session handshake to establish a secure session with the device.")
do {
let request = try securityLayerPrivate.getNextRequestInSession(data: response)
print("session intialize")
ESPLog.log("session intialize")
if let request = request {
transportLayerPrivate.SendSessionData(data: request) { responseData, error in

Expand Down
2 changes: 1 addition & 1 deletion ESPProvision/Transport/ESPBleTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class ESPBleTransport: NSObject, ESPCommunicable {
/// - Parameters:
/// - deviceNamePrefix: Device name prefix.
/// - scanTimeout: Timeout in seconds for which BLE scan should happen.
init(scanTimeout: TimeInterval, deviceNamePrefix: String, proofOfPossession:String = "") {
init(scanTimeout: TimeInterval, deviceNamePrefix: String, proofOfPossession:String? = nil) {
ESPLog.log("Initalising BLE transport class with scan timeout \(scanTimeout)")
self.scanTimeout = scanTimeout
self.deviceNamePrefix = deviceNamePrefix
Expand Down
Loading

0 comments on commit 1329f25

Please sign in to comment.