-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'features/amazon_sign_in' into versions/avs-ble
Contain changes of ssdp discovery branch also
- Loading branch information
Showing
21 changed files
with
1,004 additions
and
24 deletions.
There are no files selected for viewing
122 changes: 111 additions & 11 deletions
122
EspressifProvision/EspressifProvision.xcodeproj/project.pbxproj
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+12.4 KB
...Provision/Assets.xcassets/alexa_logo.imageset/vertical_RGB_color_darktext-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+12.4 KB
...Provision/Assets.xcassets/alexa_logo.imageset/vertical_RGB_color_darktext-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+12.4 KB
...ifProvision/Assets.xcassets/alexa_logo.imageset/vertical_RGB_color_darktext.png
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
312
EspressifProvision/EspressifProvision/Base.lproj/Main.storyboard
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
EspressifProvision/EspressifProvision/Model/AlexaDevice.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
132
EspressifProvision/EspressifProvision/SSDPClient/SSDPDiscovery.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
49
EspressifProvision/EspressifProvision/SSDPClient/SSDPService.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
EspressifProvision/EspressifProvision/Utilities/Constants.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Oops, something went wrong.