|
| 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 | +} |
0 commit comments