Skip to content

Add GATT Peripheral example for Pico W #74

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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
55 changes: 55 additions & 0 deletions pico-w-ble-peripheral-sdk/BTStack/Error.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors.
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

/// BTstack Error Code
public struct BTStackError: Error, RawRepresentable, Equatable, Hashable, Sendable {

public let rawValue: Int32

public init(rawValue: Int32) {
self.rawValue = rawValue
}
}

public extension BTStackError {

init(_ hci: HCIError) {
self.init(rawValue: Int32(hci.rawValue))
}
}

public extension HCIError {

init?(_ error: BTStackError) {
guard error.rawValue <= UInt8.max else {
return nil
}
self.init(rawValue: UInt8(error.rawValue))
}
}

internal extension CInt {

func throwsError() throws(BTStackError) {
guard self == 0 else {
throw BTStackError(rawValue: self)
}
}
}

internal extension UInt8 {

func throwsError() throws(BTStackError) {
guard self == 0 else {
throw BTStackError(rawValue: numericCast(self))
}
}
}
55 changes: 55 additions & 0 deletions pico-w-ble-peripheral-sdk/BTStack/GAP.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors.
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

public extension HostController {

func setAdvertisementParameters(
advIntMin: UInt16 = 0x0030,
advIntMax: UInt16 = 0x0030,
advType: UInt8 = 0,
directAddressType: UInt8 = 0,
directAddress: BluetoothAddress = .zero,
channelMap: UInt8 = 0x07,
filterPolicy: UInt8 = 0x00
) {
var directAddress = directAddress
withUnsafeMutablePointer(to: &directAddress.bytes) {
gap_advertisements_set_params(advIntMin, advIntMax, advType, directAddressType, $0, channelMap, filterPolicy)
}
}

var address: BluetoothAddress {
var address: BluetoothAddress = .zero
gap_local_bd_addr(&address.bytes)
return address.bigEndian
}
}

internal extension HostController {

func setAdvertisementData() {
let length = advertisement.length
advertisementBuffer = [UInt8](advertisement)
// data is not copied, pointer has to stay valid
gap_advertisements_set_data(length, &advertisementBuffer)
}

func setScanResponse() {
let length = scanResponse.length
scanResponseBuffer = [UInt8](scanResponse)
// data is not copied, pointer has to stay valid
gap_scan_response_set_data(length, &scanResponseBuffer)
}

func setAdvertisementState() {
gap_advertisements_enable(isAdvertising ? 1 : 0)
}
}
Loading