iOS HMLinkDelegate

Link Delegate is used to dispatch HMLink's events. All invocations are executed on the main thread.

Methods

Link's state changed to a new one

Declaration

func link(_ link: HMLink, stateChanged newState: HMLinkState, previousState: HMLinkState)

Parameters

link

(HMLink) the link what's state changed

newState

(HMLinkState) new state of that Link

previousState

(HMLinkState) previous state of that Link

Read More

Example

import HMKit

extension SomeType: HMLinkDelegate {

    /*
    Other required methods implementation
    */

    func link(_ link: HMLink, stateChanged newState: HMLinkState, previousState: HMLinkState) {
        switch (newState, previousState) {
        case (.autheticated, .connected):
            print("Secure connection established.")

        case (.disconnected, _):
            print("Connection lost.")

        default:
            break
        }
    }
}

Link received a command

Declaration

func link(_ link: HMLink, commandReceived bytes: [UInt8], contentType: HMContainerContentType, requestID: [UInt8])

Parameters

link

(HMLink) the link that received a command

bytes

([UInt8]) representing the received command

contentType

(HMContainerContentType) type of data received as content

requestID

([UInt8]) ID of the sent command

Read More

Example

import HMKit

extension SomeType: HMLinkDelegate {

    /*
    Other required methods implementation
    */

    func link(_ link: HMLink, commandReceived bytes: [UInt8], contentType: HMContainerContentType, requestID: [UInt8]) {
        let commandHex = bytes.map { String(format: "%02X", $0) }.joined()

        print("Command received: \(commandHex)")

        if contentType == .autoAPI {
            /*
            It's recommended to parse the bytes with AutoAPI.parseBinary(_:)
            */
        }
    }
}

Link completed a revoke

Declaration

func link(_ link: HMLink, revokeCompleted bytes: [UInt8])

Parameters

link

(HMLink) the link that was revoked

bytes

([UInt8]) representing the data sent on completion

Discussion

Called after the link is sent a revoke command, it has revoked the certificates for this device and sent back a response.
The response is usually used to forward some data from the car to a server.

Read More

Example

import HMKit

extension SomeType: HMLinkDelegate {

    /*
    Other required methods implementation
    */

    func link(_ link: HMLink, revokeCompleted bytes: [UInt8]) {
        let dataHex = bytes.map { String(format: "%02X", $0) }.joined()

        print("Revoke data received: \(dataHex)")
    }
}

Link received an authorisation request for approval

Declaration

func link(_ link: HMLink, authorisationRequestedBy serialNumber: [UInt8], approve: @escaping Approve, timeout: TimeInterval)

Parameters

link

(HMLink) the link that sent the request

serialNumber

([UInt8]) serial number of the device requesting to authorise

approve

(Approve) block to call when the request is approved

timeout

(TimeInterval) amount of seconds it takes for the request to time out

Discussion

Warning: If the approve-block is not called before the timeout interval elapses (starting after this method is invoked), the authorisation fails.

Read More

Example

import HMKit

extension SomeType: HMLinkDelegate {

    /*
    Other required methods implementation
    */

    func link(_ link: HMLink, authorisationRequestedBy serialNumber: [UInt8], approve: @escaping Approve, timeout: TimeInterval) {
        // When not asking the User for approval, but automatically approving
        do {
            try approve()
        }
        catch {
            print("Failed to approve authorisation request, error: \(error)")
        }
    }
}

Link received an error

Declaration

func link(_ link: HMLink, receivedError error: HMProtocolError)

Parameters

link

(HMLink) the link that received an error

error

(HMProtocolError) the error received

Read More

Example

import HMKit

extension SomeType: HMLinkDelegate {

    /*
    Other required methods implementation
    */

    func link(_ link: HMLink, receivedError error: HMProtocolError) {
        // Handle the error
    }
}

Types

Authorisation request approve-block

Declaration

typealias Approve = () throws -> Void

Discussion

Throws HMLinkError.timeout when it's called after the timeout period.

Read More

Example

import HMKit

do {
    try approve()
}
catch {
    print("Failed to approve authorisation request, error: \(error)")
}