Skip to content

Commit 34eb70a

Browse files
authored
drivers: fix nil connector check (#1178)
1 parent b98111d commit 34eb70a

File tree

8 files changed

+103
-30
lines changed

8 files changed

+103
-30
lines changed

drivers/aio/aio_driver.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,13 @@ func (d *driver) SetName(name string) {
8484
WithName(name).apply(d.driverCfg)
8585
}
8686

87-
// Connection returns the connection of the driver.
87+
// Connection returns the gobot connection of the driver.
8888
func (d *driver) Connection() gobot.Connection {
89+
if d.connection == nil {
90+
log.Printf("%s has no connection\n", d.driverCfg.name)
91+
return nil
92+
}
93+
8994
if conn, ok := d.connection.(gobot.Connection); ok {
9095
return conn
9196
}

drivers/ble/ble_driver.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,13 @@ func (d *Driver) SetName(name string) {
7777
WithName(name).apply(d.driverCfg)
7878
}
7979

80-
// Connection returns the connection of the driver.
80+
// Connection returns the gobot connection of the driver.
8181
func (d *Driver) Connection() gobot.Connection {
82+
if d.connection == nil {
83+
log.Printf("%s has no connection\n", d.driverCfg.name)
84+
return nil
85+
}
86+
8287
if conn, ok := d.connection.(gobot.Connection); ok {
8388
return conn
8489
}
@@ -109,6 +114,11 @@ func (d *Driver) Halt() error {
109114

110115
// Adaptor returns the BLE adaptor
111116
func (d *Driver) Adaptor() gobot.BLEConnector {
117+
if d.connection == nil {
118+
log.Printf("%s has no connection\n", d.driverCfg.name)
119+
return nil
120+
}
121+
112122
if a, ok := d.connection.(gobot.BLEConnector); ok {
113123
return a
114124
}

drivers/gpio/gpio_driver.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,13 @@ func (d *driver) Pin() string {
140140
return d.driverCfg.pin
141141
}
142142

143-
// Connection returns the connection of the gpio device.
143+
// Connection returns the gobot connection of the gpio device.
144144
func (d *driver) Connection() gobot.Connection {
145+
if d.connection == nil {
146+
log.Printf("%s has no connection\n", d.driverCfg.name)
147+
return nil
148+
}
149+
145150
if conn, ok := d.connection.(gobot.Connection); ok {
146151
return conn
147152
}
@@ -172,6 +177,10 @@ func (d *driver) Halt() error {
172177

173178
// digitalRead is a helper function with check that the connection implements DigitalReader
174179
func (d *driver) digitalRead(pin string) (int, error) {
180+
if d.connection == nil {
181+
return 0, fmt.Errorf("%s has no connection", d.driverCfg.name)
182+
}
183+
175184
if reader, ok := d.connection.(DigitalReader); ok {
176185
return reader.DigitalRead(pin)
177186
}
@@ -181,6 +190,10 @@ func (d *driver) digitalRead(pin string) (int, error) {
181190

182191
// digitalWrite is a helper function with check that the connection implements DigitalWriter
183192
func (d *driver) digitalWrite(pin string, val byte) error {
193+
if d.connection == nil {
194+
return fmt.Errorf("%s has no connection", d.driverCfg.name)
195+
}
196+
184197
if writer, ok := d.connection.(DigitalWriter); ok {
185198
return writer.DigitalWrite(pin, val)
186199
}
@@ -190,6 +203,10 @@ func (d *driver) digitalWrite(pin string, val byte) error {
190203

191204
// pwmWrite is a helper function with check that the connection implements PwmWriter
192205
func (d *driver) pwmWrite(pin string, level byte) error {
206+
if d.connection == nil {
207+
return fmt.Errorf("%s has no connection", d.driverCfg.name)
208+
}
209+
193210
if writer, ok := d.connection.(PwmWriter); ok {
194211
return writer.PwmWrite(pin, level)
195212
}
@@ -199,6 +216,10 @@ func (d *driver) pwmWrite(pin string, level byte) error {
199216

200217
// servoWrite is a helper function with check that the connection implements ServoWriter
201218
func (d *driver) servoWrite(pin string, level byte) error {
219+
if d.connection == nil {
220+
return fmt.Errorf("%s has no connection", d.driverCfg.name)
221+
}
222+
202223
if writer, ok := d.connection.(ServoWriter); ok {
203224
return writer.ServoWrite(pin, level)
204225
}

drivers/i2c/i2c_driver.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ func (d *Driver) SetName(name string) {
8383

8484
// Connection returns the gobot connection of the i2c device.
8585
func (d *Driver) Connection() gobot.Connection {
86+
if d.connector == nil {
87+
log.Printf("%s has no connector\n", d.name)
88+
return nil
89+
}
90+
8691
if conn, ok := d.connector.(gobot.Connection); ok {
8792
return conn
8893
}
@@ -96,6 +101,10 @@ func (d *Driver) Start() error {
96101
d.mutex.Lock()
97102
defer d.mutex.Unlock()
98103

104+
if d.connector == nil {
105+
return fmt.Errorf("%s has no connector", d.name)
106+
}
107+
99108
var err error
100109
bus := d.GetBusOrDefault(d.connector.DefaultI2cBus())
101110
address := d.GetAddressOrDefault(d.defaultAddress)
@@ -127,7 +136,7 @@ func (d *Driver) Write(pin string, val int) error {
127136
defer d.mutex.Unlock()
128137

129138
if d.connection == nil {
130-
return fmt.Errorf("i2c driver not started")
139+
return fmt.Errorf("i2c driver not started for '%s'", d.name)
131140
}
132141

133142
register, err := driverParseRegister(pin)
@@ -152,7 +161,7 @@ func (d *Driver) Read(pin string) (int, error) {
152161
defer d.mutex.Unlock()
153162

154163
if d.connection == nil {
155-
return 0, fmt.Errorf("i2c driver not started")
164+
return 0, fmt.Errorf("i2c driver not started for '%s'", d.name)
156165
}
157166

158167
register, err := driverParseRegister(pin)
@@ -170,79 +179,79 @@ func (d *Driver) Read(pin string) (int, error) {
170179

171180
func (d *Driver) write(data []byte) (int, error) {
172181
if d.connection == nil {
173-
return 0, fmt.Errorf("i2c driver not started")
182+
return 0, fmt.Errorf("i2c driver not started for '%s'", d.name)
174183
}
175184

176185
return d.connection.Write(data)
177186
}
178187

179188
func (d *Driver) writeByte(val byte) error {
180189
if d.connection == nil {
181-
return fmt.Errorf("i2c driver not started")
190+
return fmt.Errorf("i2c driver not started for '%s'", d.name)
182191
}
183192

184193
return d.connection.WriteByte(val)
185194
}
186195

187196
func (d *Driver) writeByteData(reg uint8, val byte) error {
188197
if d.connection == nil {
189-
return fmt.Errorf("i2c driver not started")
198+
return fmt.Errorf("i2c driver not started for '%s'", d.name)
190199
}
191200

192201
return d.connection.WriteByteData(reg, val)
193202
}
194203

195204
func (d *Driver) writeWordData(reg uint8, val uint16) error {
196205
if d.connection == nil {
197-
return fmt.Errorf("i2c driver not started")
206+
return fmt.Errorf("i2c driver not started for '%s'", d.name)
198207
}
199208

200209
return d.connection.WriteWordData(reg, val)
201210
}
202211

203212
func (d *Driver) writeBlockData(reg uint8, data []byte) error {
204213
if d.connection == nil {
205-
return fmt.Errorf("i2c driver not started")
214+
return fmt.Errorf("i2c driver not started for '%s'", d.name)
206215
}
207216

208217
return d.connection.WriteBlockData(reg, data)
209218
}
210219

211220
func (d *Driver) read(data []byte) (int, error) {
212221
if d.connection == nil {
213-
return 0, fmt.Errorf("i2c driver not started")
222+
return 0, fmt.Errorf("i2c driver not started for '%s'", d.name)
214223
}
215224

216225
return d.connection.Read(data)
217226
}
218227

219228
func (d *Driver) readByte() (byte, error) {
220229
if d.connection == nil {
221-
return 0, fmt.Errorf("i2c driver not started")
230+
return 0, fmt.Errorf("i2c driver not started for '%s'", d.name)
222231
}
223232

224233
return d.connection.ReadByte()
225234
}
226235

227236
func (d *Driver) readByteData(reg uint8) (byte, error) {
228237
if d.connection == nil {
229-
return 0, fmt.Errorf("i2c driver not started")
238+
return 0, fmt.Errorf("i2c driver not started for '%s'", d.name)
230239
}
231240

232241
return d.connection.ReadByteData(reg)
233242
}
234243

235244
func (d *Driver) readWordData(reg uint8) (uint16, error) {
236245
if d.connection == nil {
237-
return 0, fmt.Errorf("i2c driver not started")
246+
return 0, fmt.Errorf("i2c driver not started for '%s'", d.name)
238247
}
239248

240249
return d.connection.ReadWordData(reg)
241250
}
242251

243252
func (d *Driver) readBlockData(reg uint8, data []byte) error {
244253
if d.connection == nil {
245-
return fmt.Errorf("i2c driver not started")
254+
return fmt.Errorf("i2c driver not started for '%s'", d.name)
246255
}
247256

248257
return d.connection.ReadBlockData(reg, data)

drivers/i2c/jhd1313m1_driver.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,13 @@ func (d *JHD1313M1Driver) Name() string { return d.name }
147147
// SetName sets the name for the JHD1313M1 Driver.
148148
func (d *JHD1313M1Driver) SetName(n string) { d.name = n }
149149

150-
// Connection returns the driver connection to the device.
150+
// Connection returns the gobot connection to the device.
151151
func (d *JHD1313M1Driver) Connection() gobot.Connection {
152+
if d.connector == nil {
153+
log.Printf("%s has no connector\n", d.name)
154+
return nil
155+
}
156+
152157
if conn, ok := d.connector.(gobot.Connection); ok {
153158
return conn
154159
}
@@ -159,6 +164,10 @@ func (d *JHD1313M1Driver) Connection() gobot.Connection {
159164

160165
// Start starts the backlit and the screen and initializes the states.
161166
func (d *JHD1313M1Driver) Start() error {
167+
if d.connector == nil {
168+
return fmt.Errorf("%s has no connector", d.name)
169+
}
170+
162171
bus := d.GetBusOrDefault(d.connector.DefaultI2cBus())
163172

164173
var err error

drivers/onewire/onewire_driver.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ func (d *driver) SetName(name string) {
9090
// Connection returns the gobot connection of the device.
9191
func (d *driver) Connection() gobot.Connection {
9292
if d.connection == nil {
93-
log.Printf("1-wire driver not started for %s\n", d.driverCfg.name)
93+
log.Printf("1-wire driver not started for '%s'\n", d.driverCfg.name)
94+
return nil
9495
}
9596

9697
if conn, ok := d.connection.(gobot.Connection); ok {
@@ -106,6 +107,10 @@ func (d *driver) Start() error {
106107
d.mutex.Lock()
107108
defer d.mutex.Unlock()
108109

110+
if d.connector == nil {
111+
return fmt.Errorf("%s has no connector", d.driverCfg.name)
112+
}
113+
109114
var err error
110115
d.connection, err = d.connector.GetOneWireConnection(d.driverCfg.familyCode, d.driverCfg.serialNumber)
111116
if err != nil {
@@ -132,7 +137,7 @@ func (d *driver) Halt() error {
132137

133138
func (d *driver) id() (string, error) {
134139
if d.connection == nil {
135-
return "", fmt.Errorf("1-wire driver not started for %s", d.driverCfg.name)
140+
return "", fmt.Errorf("1-wire driver not started for '%s'", d.driverCfg.name)
136141
}
137142

138143
return d.connection.ID(), nil
@@ -141,29 +146,29 @@ func (d *driver) id() (string, error) {
141146
//nolint:unused // ok for now
142147
func (d *driver) readData(command string, data []byte) error {
143148
if d.connection == nil {
144-
return fmt.Errorf("1-wire driver not started for %s", d.driverCfg.name)
149+
return fmt.Errorf("1-wire driver not started for '%s'", d.driverCfg.name)
145150
}
146151
return d.connection.ReadData(command, data)
147152
}
148153

149154
//nolint:unused // ok for now
150155
func (d *driver) writeData(command string, data []byte) error {
151156
if d.connection == nil {
152-
return fmt.Errorf("1-wire driver not started for %s", d.driverCfg.name)
157+
return fmt.Errorf("1-wire driver not started for '%s'", d.driverCfg.name)
153158
}
154159
return d.connection.WriteData(command, data)
155160
}
156161

157162
func (d *driver) readInteger(command string) (int, error) {
158163
if d.connection == nil {
159-
return 0, fmt.Errorf("1-wire driver not started for %s", d.driverCfg.name)
164+
return 0, fmt.Errorf("1-wire driver not started for '%s'", d.driverCfg.name)
160165
}
161166
return d.connection.ReadInteger(command)
162167
}
163168

164169
func (d *driver) writeInteger(command string, val int) error {
165170
if d.connection == nil {
166-
return fmt.Errorf("1-wire driver not started for %s", d.driverCfg.name)
171+
return fmt.Errorf("1-wire driver not started for '%s'", d.driverCfg.name)
167172
}
168173
return d.connection.WriteInteger(command, val)
169174
}

drivers/serial/serial_driver.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,13 @@ func (d *Driver) SetName(name string) {
8383
WithName(name).apply(d.driverCfg)
8484
}
8585

86-
// Connection returns the connection of the driver.
86+
// Connection returns the gobot connection of the driver.
8787
func (d *Driver) Connection() gobot.Connection {
88+
if d.connection == nil {
89+
log.Printf("%s has no connection\n", d.driverCfg.name)
90+
return nil
91+
}
92+
8893
if conn, ok := d.connection.(gobot.Connection); ok {
8994
return conn
9095
}

0 commit comments

Comments
 (0)