-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEPLCharacteristic.swift
More file actions
218 lines (189 loc) · 6.88 KB
/
EPLCharacteristic.swift
File metadata and controls
218 lines (189 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//
// EPLCharacteristic.swift
//
//
// Created by Ting-Chou Chien on 1/18/15.
//
//
import Foundation
import CoreBluetooth
import XCGLogger
import BrightFutures
import Result
// MARK: -
// MARK: EPLCharacteristicDelegate Protocol
@objc public protocol EPLCharacteristicDelegate {
optional func characteristic(characteristic: EPLCharacteristic!, parseData rawData: NSData) -> String
func characteristic(characteristic: EPLCharacteristic!, notifyData rawData: NSData)
func characteristic(characteristic: EPLCharacteristic!, updateData rawData: NSData)
}
// MARK: -
// MARK: EPLCharacteristicValueUpdate protocol
@objc public protocol EPLCharacteristicValueUpdate {
func valueUpdate(UUID: String, newValue: AnyObject)
optional func rawDataUpdate(UUID: String, rawData: NSData)
}
// MARK: -
// MARK: EPLCharacteristicDataSource Protocol
@objc public protocol EPLCharacteristicDataSource {
var UUID: String {get}
var name: String {get}
optional var bytecount: Int {get}
optional var valueUpdateReceivers: [EPLCharacteristicValueUpdate] {get set}
optional var rawDataUpdateReceivers: [EPLCharacteristicValueUpdate] {get set}
optional func serialize(data: [String : AnyObject]) -> NSData?
optional func addValueUpdateReceiver(receiver: EPLCharacteristicValueUpdate)
optional func addRawDataUpdateReceiver(receiver: EPLCharacteristicValueUpdate)
}
// MARK: -
// MARK: EPLCharacteristic Class
public class EPLCharacteristic: NSObject {
// MARK: -
// MARK: Private variables
private var log = XCGLogger.defaultInstance()
private var _readable:Bool = false
private var _writable:Bool = false
private var _notify:Bool = false
private var _indicate: Bool = false
private var _cbCharacteristic: CBCharacteristic!
// MARK: -
// MARK: Internal variables
internal var cbCharacteristic: CBCharacteristic! {
get {
return self._cbCharacteristic
}
set(newValue) {
self._cbCharacteristic = newValue
}
}
internal var property: CBCharacteristicProperties
internal var cbService: CBService! {
if let char = self.cbCharacteristic {
return char.service
} else {
return nil
}
}
internal var cbPeripheral: CBPeripheral! {
if let char = self.cbCharacteristic {
return char.service.peripheral
} else {
return nil
}
}
internal var characteristicReadPromise = Promise<EPLCharacteristic, NSError>()
internal var characteristicWritePromise = Promise<EPLCharacteristic, NSError>()
// MARK: -
// MARK: Public variables
public var delegate: EPLCharacteristicDelegate?
public var dataSource: EPLCharacteristicDataSource?
public var name: String {
if let dataSource = self.dataSource {
return dataSource.name
}
return ""
}
public var data: String?
public var rawData: NSData? {
return self.cbCharacteristic.value
}
public var readable: Bool {
return self._readable
}
public var writable: Bool {
return self._writable
}
public var notifiable: Bool {
return self._notify
}
public var indicatable: Bool {
return self._indicate
}
public var isNotifying: Bool {
get {
return self.cbCharacteristic.isNotifying
}
set(enabled) {
if enabled && !self.cbCharacteristic.isNotifying {
self.cbPeripheral.setNotifyValue(enabled , forCharacteristic: self.cbCharacteristic)
}
}
}
public var UUID: String {
return self.cbCharacteristic.UUID.UUIDString
}
public var value: String {
if let delegate = self.delegate {
return delegate.characteristic!(self, parseData: self.rawData!)
}
return ""
}
// MARK: -
// MARK: Private Interface
private func processProperties() {
if (self.property.rawValue & CBCharacteristicProperties.Read.rawValue) > 0 {
self._readable = true
}
if (self.property.rawValue & CBCharacteristicProperties.Write.rawValue) > 0 {
self._writable = true
}
if (self.property.rawValue & CBCharacteristicProperties.Notify.rawValue) > 0 {
self._notify = true
}
if (self.property.rawValue & CBCharacteristicProperties.Indicate.rawValue) > 0 {
self._indicate = true
}
}
// MARK: -
// MARK: Internal Interface
// MARK: -
// MARK: Public Interface
public init(cbCharacteristic: CBCharacteristic) {
property = cbCharacteristic.properties
super.init()
self.cbCharacteristic = cbCharacteristic
self.property = self.cbCharacteristic.properties
self.processProperties()
}
public func read() -> Future<EPLCharacteristic, NSError> {
self.log.debug("Read Characteristic(\(self.name)) content")
self.cbService.peripheral.readValueForCharacteristic(self.cbCharacteristic)
return self.characteristicReadPromise.future
}
public func write(data: NSData) -> Future<EPLCharacteristic, NSError> {
// length check
if let length = self.dataSource?.bytecount {
if data.length > length {
self.log.error("Require length \(data) but get \(data.length)")
}
}
var count = data.length / sizeof(UInt8)
self.log.debug {
var content = ""
if let delegate = self.delegate {
content = delegate.characteristic!(self, parseData: data)
} else {
content = "0x"
var count = data.length / sizeof(UInt8)
var array = [UInt8](count: count, repeatedValue: 0)
data.getBytes(&array, length: count)
for datum in array {
content += String(format: "%02X", datum)
}
}
return String(format: "Write Characteristic(%@) content with %@", self.name, content)
}
self.cbService.peripheral.writeValue(data, forCharacteristic: self.cbCharacteristic, type: CBCharacteristicWriteType.WithResponse)
return self.characteristicWritePromise.future
}
public func write(data: [String : AnyObject]) throws -> Future<EPLCharacteristic, NSError> {
if let dataSource = self.dataSource {
if let sentData = dataSource.serialize!(data) {
return self.write(sentData)
}
try self.characteristicWritePromise.failure(NSError(domain: "Data is not converted", code: 1, userInfo: nil))
}
try self.characteristicWritePromise.failure(NSError(domain: "DataSource does not exist", code: 2, userInfo: nil))
return self.characteristicReadPromise.future
}
}