Skip to content

Commit

Permalink
Merge branch 'features/amazon_sign_in' into versions/avs-ble
Browse files Browse the repository at this point in the history
Contain changes of ssdp discovery branch also
  • Loading branch information
vikas-chandra-mnnit committed Jun 13, 2019
2 parents 2341700 + f749316 commit 3c18983
Show file tree
Hide file tree
Showing 21 changed files with 1,004 additions and 24 deletions.
122 changes: 111 additions & 11 deletions EspressifProvision/EspressifProvision.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
"images" : [
{
"idiom" : "universal",
"filename" : "wifi_symbol.png",
"filename" : "vertical_RGB_color_darktext.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "vertical_RGB_color_darktext-1.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "vertical_RGB_color_darktext-2.png",
"scale" : "3x"
}
],
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
312 changes: 306 additions & 6 deletions EspressifProvision/EspressifProvision/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions EspressifProvision/EspressifProvision/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<plist version="1.0">
<dict>
<key>APIKey</key>
<string></string>
<string>eyJhbGciOiJSU0EtU0hBMjU2IiwidmVyIjoiMSJ9.eyJ2ZXIiOiIzIiwiZW5kcG9pbnRzIjp7ImF1dGh6IjoiaHR0cHM6Ly93d3cuYW1hem9uLmNvbS9hcC9vYSIsInRva2VuRXhjaGFuZ2UiOiJodHRwczovL2FwaS5hbWF6b24uY29tL2F1dGgvbzIvdG9rZW4ifSwiY2xpZW50SWQiOiJhbXpuMS5hcHBsaWNhdGlvbi1v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key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key>
<string>Int BLE AVS</string>
<string>Int-BLE-AVS-Prov</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
Expand All @@ -31,22 +31,22 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<string>1.1</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>com.espressif.avs.provisioning.ble</string>
<string>com.espressif.intprovbleavs</string>
<key>CFBundleURLSchemes</key>
<array>
<string>amzn-com.espressif.avs.provisioning.ble</string>
<string>amzn-com.espressif.intprovbleavs</string>
</array>
</dict>
</array>
<key>CFBundleVersion</key>
<string>1</string>
<string>2</string>
<key>CodeVerifier</key>
<string>abcd1234</string>
<key>LSRequiresIPhoneOS</key>
Expand Down
23 changes: 23 additions & 0 deletions EspressifProvision/EspressifProvision/Model/AlexaDevice.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// AlexaDevice.swift
// EspressifProvision
//
// Created by Vikas Chandra on 27/05/19.
// Copyright © 2019 Espressif. All rights reserved.
//

import Foundation

class AlexaDevice: NSObject {
var modelNumber: String?
var hostAddress: String?
var status: String?
var softwareVersion: String?
var friendlyname: String?
var uuid: String?

init(hostAddr: String) {
hostAddress = hostAddr
friendlyname = nil
}
}
132 changes: 132 additions & 0 deletions EspressifProvision/EspressifProvision/SSDPClient/SSDPDiscovery.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import Foundation
import Socket

// MARK: Protocols

/// Delegate for service discovery
public protocol SSDPDiscoveryDelegate {
/// Tells the delegate a requested service has been discovered.
func ssdpDiscovery(_ discovery: SSDPDiscovery, didDiscoverService service: SSDPService)

/// Tells the delegate that the discovery ended due to an error.
func ssdpDiscovery(_ discovery: SSDPDiscovery, didFinishWithError error: Error)

/// Tells the delegate that the discovery has started.
func ssdpDiscoveryDidStart(_ discovery: SSDPDiscovery)

/// Tells the delegate that the discovery has finished.
func ssdpDiscoveryDidFinish(_ discovery: SSDPDiscovery)
}

// public extension SSDPDiscoveryDelegate {
// func ssdpDiscovery(_: SSDPDiscovery, didDiscoverService _: SSDPService) {}
//
// func ssdpDiscovery(_: SSDPDiscovery, didFinishWithError _: Error) {}
//
// func ssdpDiscoveryDidStart(_: SSDPDiscovery) {}
//
// func ssdpDiscoveryDidFinish(_: SSDPDiscovery) {}
// }

/// SSDP discovery for UPnP devices on the LAN
public class SSDPDiscovery {
/// The UDP socket
private var socket: Socket?

/// Delegate for service discovery
public var delegate: SSDPDiscoveryDelegate?

/// The client is discovering
public var isDiscovering: Bool {
return socket != nil
}

// MARK: Initialisation

public init() {}

deinit {
self.stop()
}

// MARK: Private functions

/// Read responses.
private func readResponses() {
do {
var data = Data()
let (bytesRead, address) = try socket!.readDatagram(into: &data)

if bytesRead > 0 {
let response = String(data: data, encoding: .utf8)
let (remoteHost, _) = Socket.hostnameAndPort(from: address!)!
delegate?.ssdpDiscovery(self, didDiscoverService: SSDPService(host: remoteHost, response: response!))
}

} catch {
forceStop()
delegate?.ssdpDiscovery(self, didFinishWithError: error)
}
}

/// Read responses with timeout.
private func readResponses(forDuration duration: TimeInterval) {
let queue = DispatchQueue.global()

queue.async {
while self.isDiscovering {
self.readResponses()
}
}

queue.asyncAfter(deadline: .now() + duration) { [unowned self] in
self.stop()
}
}

/// Force stop discovery closing the socket.
private func forceStop() {
if isDiscovering {
socket?.close()
}
socket = nil
}

// MARK: Public functions

/**
Discover SSDP services for a duration.
- Parameters:
- duration: The amount of time to wait.
- searchTarget: The type of the searched service.
*/
open func discoverService(forDuration duration: TimeInterval = 10, searchTarget: String = "ssdp:all") {
delegate?.ssdpDiscoveryDidStart(self)

let message = "M-SEARCH * HTTP/1.1\r\n" +
"MAN: \"ssdp:discover\"\r\n" +
"HOST: 239.255.255.250:1900\r\n" +
"ST: \(searchTarget)\r\n" + "ST: ssdp:all" + "\r\n" +
"MX: \(Int(duration))\r\n\r\n"

do {
socket = try Socket.create(type: .datagram, proto: .udp)
try socket?.listen(on: 0)

readResponses(forDuration: duration)
try socket?.write(from: message, to: Socket.createAddress(for: "239.255.255.250", on: 1900)!)

} catch {
forceStop()
delegate?.ssdpDiscovery(self, didFinishWithError: error)
}
}

/// Stop the discovery before the timeout.
open func stop() {
if socket != nil {
forceStop()
delegate?.ssdpDiscoveryDidFinish(self)
}
}
}
49 changes: 49 additions & 0 deletions EspressifProvision/EspressifProvision/SSDPClient/SSDPService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import Foundation

public class SSDPService {
/// The host of service
public internal(set) var host: String
/// The value of `LOCATION` header
public internal(set) var location: String?
/// The value of `SERVER` header
public internal(set) var server: String?
/// The value of `ST` header
public internal(set) var searchTarget: String?
/// The value of `USN` header
public internal(set) var uniqueServiceName: String?

// MARK: Initialisation

/**
Initialize the `SSDPService` with the discovery response.

- Parameters:
- host: The host of service
- response: The discovery response.
*/
init(host: String, response: String) {
self.host = host
location = parse(header: "LOCATION", in: response)
server = parse(header: "SERVER", in: response)
searchTarget = parse(header: "ST", in: response)
uniqueServiceName = parse(header: "USN", in: response)
}

// MARK: Private functions

/**
Parse the discovery response.

- Parameters:
- header: The header to parse.
- response: The discovery response.
*/
private func parse(header: String, in response: String) -> String? {
if let range = response.range(of: "\(header): .*", options: .regularExpression) {
var value = String(response[range])
value = value.replacingOccurrences(of: "\(header): ", with: "")
return value
}
return nil
}
}
28 changes: 28 additions & 0 deletions EspressifProvision/EspressifProvision/Utilities/Constants.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// Constants.swift
// EspressifProvision
//
// Created by Vikas Chandra on 28/05/19.
// Copyright © 2019 Espressif. All rights reserved.
//

import Foundation
import MBProgressHUD
import UIKit

struct Constants {
static let friendlynameKey = "friendlyname"
static let UUIDKey = "uuid"

// Reuse identifier
static let deviceListCellReuseIdentifier = "deviceListCell"
static let deviceDetailVCIndentifier = "deviceDetailVC"

static func showLoader(message: String, view: UIView) {
DispatchQueue.main.async {
let loader = MBProgressHUD.showAdded(to: view, animated: true)
loader.mode = MBProgressHUDMode.indeterminate
loader.label.text = message
}
}
}
51 changes: 51 additions & 0 deletions EspressifProvision/EspressifProvision/avs/ConfigureAVS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,44 @@ class ConfigureAVS {
}
}

public func isLoggedIn(completionHandler: @escaping (Bool) -> Void) {
var payload = Avs_AVSConfigPayload()
payload.msg = Avs_AVSConfigMsgType.typeCmdSignInStatus
payload.cmdSigninStatus = Avs_CmdSignInStatus()
do {
let encryptedData = try security.encrypt(data: payload.serializedData())
if let data = encryptedData {
transport.SendConfigData(path: ConfigureAVS.AVS_CONFIG_PATH, data: data) { response, error in
if response != nil, error == nil {
completionHandler(self.processAVSLoginStatus(response: response!))
}
}
}
} catch {
print(error)
completionHandler(false)
}
}

public func signOut(completionHandler: @escaping (Bool) -> Void) {
var payload = Avs_AVSConfigPayload()
payload.msg = Avs_AVSConfigMsgType.typeCmdSignOut
payload.cmdSigninStatus = Avs_CmdSignInStatus()
do {
let encryptedData = try security.encrypt(data: payload.serializedData())
if let data = encryptedData {
transport.SendConfigData(path: ConfigureAVS.AVS_CONFIG_PATH, data: data) { response, error in
if response != nil, error == nil {
completionHandler(self.processAVSLoginStatus(response: response!))
}
}
}
} catch {
print(error)
completionHandler(false)
}
}

private func createSetAVSConfigRequest(cliendId: String,
authCode: String,
redirectUri: String) throws -> Data? {
Expand Down Expand Up @@ -135,6 +173,19 @@ class ConfigureAVS {
return responseStatus
}

private func processAVSLoginStatus(response: Data) -> Bool {
do {
if let decryptedResponse = security.decrypt(data: response) {
let statusResponse = try Avs_AVSConfigPayload(serializedData: decryptedResponse)
return statusResponse.respSigninStatus.status == .signedIn
}
} catch {
print(error)
return false
}
return false
}

private static func generateCodeChallenge(codeVerifier: String) -> String {
let codeChallenge = Data(codeVerifier.bytes).sha256()
return base64ToBase64url(base64: codeChallenge.base64EncodedString())
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit 3c18983

Please sign in to comment.