Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Device Status improvements #158

Merged
merged 3 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
774 changes: 573 additions & 201 deletions Example/Example/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions Example/Example/Util/BootloaderMode+String.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2018 Nordic Semiconductor ASA.
*
* SPDX-License-Identifier: Apache-2.0
*/


import Foundation
import iOSMcuManagerLibrary

extension BootloaderInfoResponse.Mode {

var text: String {
switch self {
case .SingleApplication: return "Single application"
case .SwapNoScratch: return "Swap without scratch"
case .SwapUsingScratch: return "Swap with scratch"
case .RAMLoader: return "RAM loader"
case .DirectXIPNoRevert: return "Direct-XIP without revert"
case .DirectXIPWithRevert: return "Direct-XIP with revert"
case .Overwrite: return "Overwrite"
default: return "Unknown"
}
}

}
23 changes: 23 additions & 0 deletions Example/Example/Util/PeripheralState+String.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2018 Nordic Semiconductor ASA.
*
* SPDX-License-Identifier: Apache-2.0
*/


import Foundation
import iOSMcuManagerLibrary

extension PeripheralState: CustomStringConvertible {

public var description: String {
switch self {
case .connecting: return "CONNECTING..."
case .initializing: return "INITIALIZING..."
case .connected: return "CONNECTED"
case .disconnecting: return "DISCONNECTING..."
case .disconnected: return "DISCONNECTED"
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,83 @@

import UIKit
import iOSMcuManagerLibrary
import CoreBluetooth

class BaseViewController: UITabBarController {
protocol DeviceStatusDelegate: AnyObject {
func connectionStateDidChange(_ state: PeripheralState)
func bootloaderNameReceived(_ name: String)
func bootloaderModeReceived(_ mode: BootloaderInfoResponse.Mode)
func appInfoReceived(_ output: String)
func mcuMgrParamsReceived(buffers: Int, size: Int)
}

class BaseViewController: UITabBarController {
weak var deviceStatusDelegate: DeviceStatusDelegate? {
didSet {
if let state {
deviceStatusDelegate?.connectionStateDidChange(state)
}
if let bootloaderName {
deviceStatusDelegate?.bootloaderNameReceived(bootloaderName)
}
if let bootloaderMode {
deviceStatusDelegate?.bootloaderModeReceived(bootloaderMode)
}
if let appInfoOutput {
deviceStatusDelegate?.appInfoReceived(appInfoOutput)
}
if let mcuMgrParams {
deviceStatusDelegate?.mcuMgrParamsReceived(buffers: mcuMgrParams.buffers, size: mcuMgrParams.size)
}
}
}

var transporter: McuMgrTransport!
var peripheral: DiscoveredPeripheral! {
didSet {
let bleTransporter = McuMgrBleTransport(peripheral.basePeripheral)
bleTransporter.logDelegate = UIApplication.shared.delegate as? McuMgrLogDelegate
bleTransporter.delegate = self
transporter = bleTransporter
}
}

private var state: PeripheralState? {
didSet {
if let state {
deviceStatusDelegate?.connectionStateDidChange(state)
}
}
}
private var bootloaderName: String? {
didSet {
if let bootloaderName {
deviceStatusDelegate?.bootloaderNameReceived(bootloaderName)
}
}
}
private var bootloaderMode: BootloaderInfoResponse.Mode? {
didSet {
if let bootloaderMode {
deviceStatusDelegate?.bootloaderModeReceived(bootloaderMode)
}
}
}
private var appInfoOutput: String? {
didSet {
if let appInfoOutput {
deviceStatusDelegate?.appInfoReceived(appInfoOutput)
}
}
}
private var mcuMgrParams: (buffers: Int, size: Int)? {
didSet {
if let mcuMgrParams {
deviceStatusDelegate?.mcuMgrParamsReceived(buffers: mcuMgrParams.buffers, size: mcuMgrParams.size)
}
}
}

override func viewDidLoad() {
title = peripheral.advertisedName
}
Expand All @@ -26,3 +91,35 @@ class BaseViewController: UITabBarController {
transporter?.close()
}
}

extension BaseViewController: PeripheralDelegate {

func peripheral(_ peripheral: CBPeripheral, didChangeStateTo state: PeripheralState) {
self.state = state

if state == .connected {
let defaultManager = DefaultManager(transporter: transporter)
defaultManager.logDelegate = UIApplication.shared.delegate as? McuMgrLogDelegate
defaultManager.params { [weak self] response, error in
if let count = response?.bufferCount,
let size = response?.bufferSize {
self?.mcuMgrParams = (Int(count), Int(size))
}
defaultManager.applicationInfo(format: [.KernelName, .KernelVersion]) { [weak self] response, error in
self?.appInfoOutput = response?.response

defaultManager.bootloaderInfo(query: .Name) { [weak self] response, error in
self?.bootloaderName = response?.bootloader

if response?.bootloader == "MCUboot" {
defaultManager.bootloaderInfo(query: .Mode) { [weak self] response, error in
self?.bootloaderMode = response?.mode
}
}
}
}
}
}
}

}
43 changes: 32 additions & 11 deletions Example/Example/View Controllers/Manager/DeviceController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ class DeviceController: UITableViewController, UITextFieldDelegate {

// MARK: IBOutlet(s)

@IBOutlet weak var connectionStatus: ConnectionStateLabel!
@IBOutlet weak var connectionStatus: UILabel!
@IBOutlet weak var mcuMgrParams: UILabel!
@IBOutlet weak var bootloaderName: UILabel!
@IBOutlet weak var bootloaderMode: UILabel!
@IBOutlet weak var kernel: UILabel!
@IBOutlet weak var actionSend: UIButton!
@IBOutlet weak var message: UITextField!
@IBOutlet weak var messageSent: UILabel!
Expand Down Expand Up @@ -56,15 +60,8 @@ class DeviceController: UITableViewController, UITextFieldDelegate {
}

override func viewDidAppear(_ animated: Bool) {
// Set the connection status label as transport delegate.
let bleTransporter = defaultManager.transporter as? McuMgrBleTransport
bleTransporter?.delegate = connectionStatus
}

override func viewWillDisappear(_ animated: Bool) {
// Close the connection to allow other UIViewController(s) to do
// their own thing.
defaultManager.transporter.close()
let baseController = parent as? BaseViewController
baseController?.deviceStatusDelegate = self
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
Expand Down Expand Up @@ -100,7 +97,7 @@ class DeviceController: UITableViewController, UITextFieldDelegate {
if let messageText = self?.messageSent.text {
self?.send(message: messageText)
}
} catch McuManagerError.mtuValueHasNotchanged {
} catch McuManagerError.mtuValueHasNotChanged {
// If MTU value did not change, try reassembly.
if let messageText = self?.messageSent.text,
let bleTransport = self?.defaultManager.transporter as? McuMgrBleTransport,
Expand Down Expand Up @@ -129,3 +126,27 @@ class DeviceController: UITableViewController, UITextFieldDelegate {
messageReceivedBackground.isHidden = false
}
}

extension DeviceController: DeviceStatusDelegate {

func connectionStateDidChange(_ state: PeripheralState) {
connectionStatus.text = state.description
}

func bootloaderNameReceived(_ name: String) {
bootloaderName.text = name
}

func bootloaderModeReceived(_ mode: BootloaderInfoResponse.Mode) {
bootloaderMode.text = mode.text
}

func appInfoReceived(_ output: String) {
kernel.text = output
}

func mcuMgrParamsReceived(buffers: Int, size: Int) {
mcuMgrParams.text = "\(buffers) x \(size) bytes"
}

}
38 changes: 32 additions & 6 deletions Example/Example/View Controllers/Manager/FilesController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ class FilesController: UITableViewController {
*/
static let defaultPartition = "lfs1"

@IBOutlet weak var connectionStatus: ConnectionStateLabel!
@IBOutlet weak var connectionStatus: UILabel!
@IBOutlet weak var mcuMgrParams: UILabel!
@IBOutlet weak var bootloaderName: UILabel!
@IBOutlet weak var bootloaderMode: UILabel!
@IBOutlet weak var kernel: UILabel!

var fileDownloadViewController: FileDownloadViewController!

override func viewDidAppear(_ animated: Bool) {
showPartitionControl()

// Set the connection status label as transport delegate.
let baseController = parent as! BaseViewController
let bleTransporter = baseController.transporter as? McuMgrBleTransport
bleTransporter?.delegate = connectionStatus
let baseController = parent as? BaseViewController
baseController?.deviceStatusDelegate = self
}

override func viewDidDisappear(_ animated: Bool) {
Expand Down Expand Up @@ -61,7 +63,7 @@ class FilesController: UITableViewController {

@objc func presentPartitionSettings() {
let alert = UIAlertController(title: "Settings",
message: "Specify the mount point,\ne.g. \"lfs\" or \"nffs\":",
message: "Specify the mount point,\ne.g. \"lfs1\" or \"nffs\":",
preferredStyle: .alert)
alert.addTextField { field in
field.placeholder = "Partition"
Expand Down Expand Up @@ -91,3 +93,27 @@ class FilesController: UITableViewController {
present(alert, animated: true)
}
}

extension FilesController: DeviceStatusDelegate {

func connectionStateDidChange(_ state: PeripheralState) {
connectionStatus.text = state.description
}

func bootloaderNameReceived(_ name: String) {
bootloaderName.text = name
}

func bootloaderModeReceived(_ mode: BootloaderInfoResponse.Mode) {
bootloaderMode.text = mode.text
}

func appInfoReceived(_ output: String) {
kernel.text = output
}

func mcuMgrParamsReceived(buffers: Int, size: Int) {
mcuMgrParams.text = "\(buffers) x \(size) bytes"
}

}
37 changes: 31 additions & 6 deletions Example/Example/View Controllers/Manager/ImageController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ import UIKit
import iOSMcuManagerLibrary

class ImageController: UITableViewController {
@IBOutlet weak var connectionStatus: ConnectionStateLabel!

@IBOutlet weak var connectionStatus: UILabel!
@IBOutlet weak var mcuMgrParams: UILabel!
@IBOutlet weak var bootloaderName: UILabel!
@IBOutlet weak var bootloaderMode: UILabel!
@IBOutlet weak var kernel: UILabel!
/// Instance if Images View Controller, required to get its
/// height when data are obtained and height changes.
private var imagesViewController: ImagesViewController!

override func viewDidAppear(_ animated: Bool) {
showModeSwitch()

// Set the connection status label as transport delegate.
let baseController = parent as! BaseViewController
let bleTransporter = baseController.transporter as? McuMgrBleTransport
bleTransporter?.delegate = connectionStatus
let baseController = parent as? BaseViewController
baseController?.deviceStatusDelegate = self
}

override func viewWillDisappear(_ animated: Bool) {
Expand Down Expand Up @@ -97,3 +98,27 @@ class ImageController: UITableViewController {
return super.tableView(tableView, titleForHeaderInSection: section)
}
}

extension ImageController: DeviceStatusDelegate {

func connectionStateDidChange(_ state: PeripheralState) {
connectionStatus.text = state.description
}

func bootloaderNameReceived(_ name: String) {
bootloaderName.text = name
}

func bootloaderModeReceived(_ mode: BootloaderInfoResponse.Mode) {
bootloaderMode.text = mode.text
}

func appInfoReceived(_ output: String) {
kernel.text = output
}

func mcuMgrParamsReceived(buffers: Int, size: Int) {
mcuMgrParams.text = "\(buffers) x \(size) bytes"
}

}
Loading