@@ -19,12 +19,12 @@ import (
19
19
"github.com/BitBoxSwiss/bitbox02-api-go/util/errp"
20
20
)
21
21
22
- // queryBluetooth is like query, but nested one level deeper for Bluetooth.
23
- func (device * Device ) queryBluetooth (request * messages.BluetoothRequest ) (* messages.BluetoothResponse , error ) {
22
+ // nonAtomicQueryBluetooth is like query, but nested one level deeper for Bluetooth.
23
+ func (device * Device ) nonAtomicQueryBluetooth (request * messages.BluetoothRequest ) (* messages.BluetoothResponse , error ) {
24
24
if ! device .SupportsBluetooth () {
25
25
return nil , errp .New ("this device does not support Bluetooth" )
26
26
}
27
- response , err := device .query (& messages.Request {
27
+ response , err := device .nonAtomicQuery (& messages.Request {
28
28
Request : & messages.Request_Bluetooth {
29
29
Bluetooth : request ,
30
30
},
@@ -41,44 +41,42 @@ func (device *Device) queryBluetooth(request *messages.BluetoothRequest) (*messa
41
41
42
42
// BluetoothUpgrade attempts an upgrade of the Bluetooth firmware.
43
43
func (device * Device ) BluetoothUpgrade (firmware []byte ) error {
44
- // Send initial upgrade request
45
- initReq := & messages.BluetoothUpgradeInitRequest {
46
- FirmwareLength : uint32 (len (firmware )),
47
- }
48
- req := & messages.BluetoothRequest {
49
- Request : & messages.BluetoothRequest_UpgradeInit {
50
- UpgradeInit : initReq ,
51
- },
52
- }
53
-
54
- currentResponse , err := device .queryBluetooth (req )
55
- if err != nil {
56
- return err
57
- }
44
+ return device .atomicQueries (func () error {
45
+ currentResponse , err := device .nonAtomicQueryBluetooth (& messages.BluetoothRequest {
46
+ Request : & messages.BluetoothRequest_UpgradeInit {
47
+ UpgradeInit : & messages.BluetoothUpgradeInitRequest {
48
+ FirmwareLength : uint32 (len (firmware )),
49
+ },
50
+ },
51
+ })
52
+ if err != nil {
53
+ return err
54
+ }
58
55
59
- for {
60
- switch resp := currentResponse .Response .(type ) {
61
- case * messages.BluetoothResponse_RequestChunk :
62
- chunkReq := resp .RequestChunk
63
- chunkData := firmware [chunkReq .Offset : chunkReq .Offset + chunkReq .Length ]
56
+ for {
57
+ switch resp := currentResponse .Response .(type ) {
58
+ case * messages.BluetoothResponse_RequestChunk :
59
+ chunkReq := resp .RequestChunk
60
+ chunkData := firmware [chunkReq .Offset : chunkReq .Offset + chunkReq .Length ]
64
61
65
- currentResponse , err = device .queryBluetooth (& messages.BluetoothRequest {
66
- Request : & messages.BluetoothRequest_Chunk {
67
- Chunk : & messages.BluetoothChunkRequest {
68
- Data : chunkData ,
62
+ currentResponse , err = device .nonAtomicQueryBluetooth (& messages.BluetoothRequest {
63
+ Request : & messages.BluetoothRequest_Chunk {
64
+ Chunk : & messages.BluetoothChunkRequest {
65
+ Data : chunkData ,
66
+ },
69
67
},
70
- },
71
- })
72
- if err != nil {
73
- return err
74
- }
68
+ })
69
+ if err != nil {
70
+ return err
71
+ }
75
72
76
- case * messages.BluetoothResponse_Success :
77
- // Upgrade complete
78
- return nil
73
+ case * messages.BluetoothResponse_Success :
74
+ // Upgrade complete
75
+ return nil
79
76
80
- default :
81
- return errp .New ("unexpected response type during bluetooth upgrade" )
77
+ default :
78
+ return errp .New ("unexpected response type during bluetooth upgrade" )
79
+ }
82
80
}
83
- }
81
+ })
84
82
}
0 commit comments