Skip to content

Commit

Permalink
Renamed GATTUUIDList
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Nov 7, 2024
1 parent 02d1517 commit ac3640a
Showing 1 changed file with 20 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@
// Copyright © 2018 PureSwift. All rights reserved.
//

#if canImport(Foundation)
import Foundation
#endif
import Bluetooth

/// GAP UUID List
internal struct GAPUUIDList <Element: GAPUUIDElement> {
internal struct GATTUUIDList <Element: GAPUUIDElement> {

internal var uuids: [Element]
var uuids: [Element]

internal init(uuids: [Element]) {
init(uuids: [Element]) {
self.uuids = uuids
}
}

extension GATTUUIDList: DataConvertible {

internal init?(data: Data) {
init?<Data: DataContainer>(data: Data) {

let elementSize = MemoryLayout<Element>.size

Expand All @@ -31,7 +36,7 @@ internal struct GAPUUIDList <Element: GAPUUIDElement> {
guard index + elementSize <= data.count
else { return nil }

let valueData = data.subdataNoCopy(in: data.startIndex + index ..< data.startIndex + index + elementSize)
let valueData = data.subdata(in: data.startIndex + index ..< data.startIndex + index + elementSize)

let value = Element(littleEndian: Element(data: valueData)!)

Expand All @@ -42,34 +47,29 @@ internal struct GAPUUIDList <Element: GAPUUIDElement> {

self.uuids = uuids
}
}

extension GAPUUIDList: DataConvertible {

static func += <T: DataContainer> (data: inout T, value: GAPUUIDList) {

value.forEach { data += $0.littleEndian }
func append<Data: DataContainer>(to data: inout Data) {
forEach { data += $0.littleEndian }
}

var dataLength: Int {

return MemoryLayout<Element>.size * uuids.count
}
}

// MARK: - Sequence

extension GAPUUIDList: Sequence {
extension GATTUUIDList: Sequence {

public func makeIterator() -> IndexingIterator<GAPUUIDList<Element>> {
public func makeIterator() -> IndexingIterator<GATTUUIDList<Element>> {

return IndexingIterator(_elements: self)
}
}

// MARK: - Collection

extension GAPUUIDList: Collection {
extension GATTUUIDList: Collection {

subscript(index: Int) -> Element {

Expand All @@ -94,41 +94,13 @@ extension GAPUUIDList: Collection {

// MARK: - Supporting Types

internal protocol GAPUUIDElement: UnsafeDataConvertible {

init?<Data: DataContainer>(data: Data)

internal protocol GAPUUIDElement: DataConvertible {

init(littleEndian: Self)

var littleEndian: Self { get }

static func += (data: inout LowEnergyAdvertisingData, value: Self)
}

extension UInt16: GAPUUIDElement {

init?<Data: DataContainer>(data: Data) {

guard data.count == MemoryLayout<UInt16>.size
else { return nil }

self.init(bytes: (data[0],
data[1]))
}
}

extension UInt32: GAPUUIDElement {

init?<Data: DataContainer>(data: Data) {

guard data.count == MemoryLayout<UInt32>.size
else { return nil }

self.init(bytes: (data[0],
data[1],
data[2],
data[3]))
}
}

extension Bluetooth.UInt128: GAPUUIDElement { }
extension UInt16: GAPUUIDElement { }
extension UInt32: GAPUUIDElement { }
extension UInt128: GAPUUIDElement { }

0 comments on commit ac3640a

Please sign in to comment.