Skip to content

firmware: add bluetooth upgrade #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions api/firmware/bluetooth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2025 Shift Crypto AG
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package firmware

import (
"github.com/BitBoxSwiss/bitbox02-api-go/api/firmware/messages"
"github.com/BitBoxSwiss/bitbox02-api-go/util/errp"
)

// queryBluetooth is like query, but nested one level deeper for Bluetooth.
func (device *Device) queryBluetooth(request *messages.BluetoothRequest) (*messages.BluetoothResponse, error) {
if !device.SupportsBluetooth() {
return nil, errp.New("this device does not support Bluetooth")
}
response, err := device.query(&messages.Request{
Request: &messages.Request_Bluetooth{
Bluetooth: request,
},
})
if err != nil {
return nil, err
}
bluetoothResponse, ok := response.Response.(*messages.Response_Bluetooth)
if !ok {
return nil, errp.New("unexpected reply")
}
return bluetoothResponse.Bluetooth, nil
}

// BluetoothUpgrade attempts an upgrade of the Bluetooth firmware.
func (device *Device) BluetoothUpgrade(firmware []byte) error {
// Send initial upgrade request
initReq := &messages.BluetoothUpgradeInitRequest{
FirmwareLength: uint32(len(firmware)),
}
req := &messages.BluetoothRequest{
Request: &messages.BluetoothRequest_UpgradeInit{
UpgradeInit: initReq,
},
}

currentResponse, err := device.queryBluetooth(req)
if err != nil {
return err
}

for {
switch resp := currentResponse.Response.(type) {
case *messages.BluetoothResponse_RequestChunk:
chunkReq := resp.RequestChunk
chunkData := firmware[chunkReq.Offset : chunkReq.Offset+chunkReq.Length]

currentResponse, err = device.queryBluetooth(&messages.BluetoothRequest{
Request: &messages.BluetoothRequest_Chunk{
Chunk: &messages.BluetoothChunkRequest{
Data: chunkData,
},
},
})
if err != nil {
return err
}

case *messages.BluetoothResponse_Success:
// Upgrade complete
return nil

default:
return errp.New("unexpected response type during bluetooth upgrade")
}
}
}
5 changes: 5 additions & 0 deletions api/firmware/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,11 @@ func (device *Device) SupportsETH(chainID uint64) bool {
return false
}

// SupportsBluetooth returns true if this device supports Bluetooth.
func (device *Device) SupportsBluetooth() bool {
return *device.product == common.ProductBitBox02PlusMulti || *device.product == common.ProductBitBox02PlusBTCOnly
}

// SupportsERC20 returns true if an ERC20 token is supported by the device api.
//
// For now, this list only contains tokens relevant to the BitBoxApp, otherwise the bitbox02-api-js
Expand Down
Loading
Loading