Skip to content

Commit 842573d

Browse files
committed
firmware: add bluetooth upgrade
Protobuf msgs taken from: BitBoxSwiss/bitbox02-firmware#1405 We can add simulator tests later once a BB02+ simulator is ready.
1 parent 4f738f4 commit 842573d

File tree

7 files changed

+1086
-242
lines changed

7 files changed

+1086
-242
lines changed

api/firmware/bluetooth.go

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright 2025 Shift Crypto AG
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package firmware
16+
17+
import (
18+
"github.com/BitBoxSwiss/bitbox02-api-go/api/firmware/messages"
19+
"github.com/BitBoxSwiss/bitbox02-api-go/util/errp"
20+
)
21+
22+
// queryBluetooth is like query, but nested one level deeper for Bluetooth.
23+
func (device *Device) queryBluetooth(request *messages.BluetoothRequest) (*messages.BluetoothResponse, error) {
24+
if !device.SupportsBluetooth() {
25+
return nil, errp.New("this device does not support Bluetooth")
26+
}
27+
response, err := device.query(&messages.Request{
28+
Request: &messages.Request_Bluetooth{
29+
Bluetooth: request,
30+
},
31+
})
32+
if err != nil {
33+
return nil, err
34+
}
35+
bluetoothResponse, ok := response.Response.(*messages.Response_Bluetooth)
36+
if !ok {
37+
return nil, errp.New("unexpected reply")
38+
}
39+
return bluetoothResponse.Bluetooth, nil
40+
}
41+
42+
// BluetoothUpgrade attempts an upgrade of the Bluetooth firmware.
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+
}
58+
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]
64+
65+
currentResponse, err = device.queryBluetooth(&messages.BluetoothRequest{
66+
Request: &messages.BluetoothRequest_Chunk{
67+
Chunk: &messages.BluetoothChunkRequest{
68+
Data: chunkData,
69+
},
70+
},
71+
})
72+
if err != nil {
73+
return err
74+
}
75+
76+
case *messages.BluetoothResponse_Success:
77+
// Upgrade complete
78+
return nil
79+
80+
default:
81+
return errp.New("unexpected response type during bluetooth upgrade")
82+
}
83+
}
84+
}

api/firmware/device.go

+5
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,11 @@ func (device *Device) SupportsETH(chainID uint64) bool {
352352
return false
353353
}
354354

355+
// SupportsBluetooth returns true if this device supports Bluetooth.
356+
func (device *Device) SupportsBluetooth() bool {
357+
return *device.product == common.ProductBitBox02PlusMulti || *device.product == common.ProductBitBox02PlusBTCOnly
358+
}
359+
355360
// SupportsERC20 returns true if an ERC20 token is supported by the device api.
356361
//
357362
// For now, this list only contains tokens relevant to the BitBoxApp, otherwise the bitbox02-api-js

0 commit comments

Comments
 (0)