Browse Source

refactor

master
Eray Arslan 1 year ago
parent
commit
7d632c6ad3
  1. 14
      backend/backend.go
  2. 502
      bladerf.go
  3. 253
      bladerf_test.go
  4. 3
      channel/channel.go
  5. 13
      channel_layout/channel_layout.go
  6. 12
      clock_select/clock_select.go
  7. 13
      correction/correction.go
  8. 12
      device_speed/device_speed.go
  9. 11
      direction/direction.go
  10. 126
      enums.go
  11. 4
      error/error.go
  12. 11
      format/format.go
  13. 14
      fpga_size/fpga_size.go
  14. 12
      fpga_source/fpga_source.go
  15. 14
      gain_mode/gain_mode.go
  16. 11
      io_module/io_module.go
  17. 19
      loopback/loopback.go
  18. 9
      macros.go
  19. 15
      pmic_register/pmic_register.go
  20. 12
      power_source/power_source.go
  21. 14
      rx_mux/rx_mux.go
  22. 55
      structs.go

14
backend/backend.go

@ -1,14 +0,0 @@ @@ -1,14 +0,0 @@
package backend
// #include <libbladeRF.h>
import "C"
type Backend int
const (
Any Backend = C.BLADERF_BACKEND_ANY
Linux Backend = C.BLADERF_BACKEND_LINUX
LibUSB Backend = C.BLADERF_BACKEND_LIBUSB
Cypress Backend = C.BLADERF_BACKEND_CYPRESS
Dummy Backend = C.BLADERF_BACKEND_DUMMY
)

502
bladerf.go

@ -11,17 +11,7 @@ import "C" @@ -11,17 +11,7 @@ import "C"
import "C"
import (
"fmt"
"github.com/erayarslan/go-bladerf/backend"
"github.com/erayarslan/go-bladerf/channel"
"github.com/erayarslan/go-bladerf/channel_layout"
"github.com/erayarslan/go-bladerf/device_speed"
"github.com/erayarslan/go-bladerf/direction"
exception "github.com/erayarslan/go-bladerf/error"
"github.com/erayarslan/go-bladerf/format"
"github.com/erayarslan/go-bladerf/fpga_size"
"github.com/erayarslan/go-bladerf/fpga_source"
"github.com/erayarslan/go-bladerf/gain_mode"
"github.com/erayarslan/go-bladerf/loopback"
"github.com/mattn/go-pointer"
"unsafe"
)
@ -65,135 +55,198 @@ func LoadFpga(bladeRF BladeRF, imagePath string) error { @@ -65,135 +55,198 @@ func LoadFpga(bladeRF BladeRF, imagePath string) error {
return GetError(C.bladerf_load_fpga(bladeRF.ref, path))
}
func GetFpgaSize(bladeRF *BladeRF) (fpga_size.FPGASize, error) {
func GetFpgaSize(bladeRF BladeRF) (FpgaSize, error) {
var size C.bladerf_fpga_size
err := GetError(C.bladerf_get_fpga_size((*bladeRF).ref, &size))
return fpga_size.FPGASize(int(size)), err
err := GetError(C.bladerf_get_fpga_size(bladeRF.ref, &size))
if err != nil {
return 0, err
}
return FpgaSize(size), nil
}
func GetFpgaSource(bladeRF *BladeRF) (fpga_source.FPGASource, error) {
func GetFpgaSource(bladeRF BladeRF) (FpgaSource, error) {
var source C.bladerf_fpga_source
err := GetError(C.bladerf_get_fpga_source((*bladeRF).ref, &source))
return fpga_source.FPGASource(int(source)), err
err := GetError(C.bladerf_get_fpga_source(bladeRF.ref, &source))
if err != nil {
return 0, err
}
return FpgaSource(source), nil
}
func GetFpgaBytes(bladeRF *BladeRF) (uint32, error) {
func GetFpgaBytes(bladeRF BladeRF) (uint32, error) {
var size C.size_t
err := GetError(C.bladerf_get_fpga_bytes((*bladeRF).ref, &size))
return uint32(size), err
err := GetError(C.bladerf_get_fpga_bytes(bladeRF.ref, &size))
if err != nil {
return 0, err
}
return uint32(size), nil
}
func GetFpgaFlashSize(bladeRF *BladeRF) (uint32, bool, error) {
func GetFpgaFlashSize(bladeRF BladeRF) (uint32, bool, error) {
var size C.uint32_t
var isGuess C.bool
err := GetError(C.bladerf_get_flash_size((*bladeRF).ref, &size, &isGuess))
return uint32(size), bool(isGuess), err
err := GetError(C.bladerf_get_flash_size(bladeRF.ref, &size, &isGuess))
if err != nil {
return 0, false, err
}
return uint32(size), bool(isGuess), nil
}
func GetFirmwareVersion(bladeRF *BladeRF) (Version, error) {
func GetFirmwareVersion(bladeRF BladeRF) (Version, error) {
var version C.struct_bladerf_version
err := GetError(C.bladerf_fw_version((*bladeRF).ref, &version))
return NewVersion(&version), err
err := GetError(C.bladerf_fw_version(bladeRF.ref, &version))
if err != nil {
return Version{}, err
}
return NewVersion(&version), nil
}
func IsFpgaConfigured(bladeRF *BladeRF) (bool, error) {
out := C.bladerf_is_fpga_configured((*bladeRF).ref)
if int(out) < 0 {
func IsFpgaConfigured(bladeRF BladeRF) (bool, error) {
out := C.bladerf_is_fpga_configured(bladeRF.ref)
if out < 0 {
return false, GetError(out)
}
return int(out) == 1, nil
return out == 1, nil
}
func GetDeviceSpeed(bladeRF *BladeRF) device_speed.DeviceSpeed {
return device_speed.DeviceSpeed(int(C.bladerf_device_speed((*bladeRF).ref)))
func GetDeviceSpeed(bladeRF BladeRF) DeviceSpeed {
return DeviceSpeed(int(C.bladerf_device_speed(bladeRF.ref)))
}
func GetFpgaVersion(bladeRF *BladeRF) (Version, error) {
func GetFpgaVersion(bladeRF BladeRF) (Version, error) {
var version C.struct_bladerf_version
err := GetError(C.bladerf_fpga_version((*bladeRF).ref, &version))
return NewVersion(&version), err
err := GetError(C.bladerf_fpga_version(bladeRF.ref, &version))
if err != nil {
return Version{}, err
}
return NewVersion(&version), nil
}
func FreeDeviceList(devInfo DevInfo) {
C.bladerf_free_device_list(devInfo.ref)
func FreeDeviceList(deviceInfo DeviceInfo) {
C.bladerf_free_device_list(deviceInfo.ref)
}
func GetDeviceList() []DevInfo {
var devInfo *C.struct_bladerf_devinfo
var devices []DevInfo
func GetDeviceList() ([]DeviceInfo, error) {
var deviceInfo *C.struct_bladerf_devinfo
var devices []DeviceInfo
count := int(C.bladerf_get_device_list(&devInfo))
codeOrCount := C.bladerf_get_device_list(&deviceInfo)
if codeOrCount < 0 {
return nil, GetError(codeOrCount)
}
count := int(codeOrCount)
if count == 0 {
return make([]DeviceInfo, 0), nil
}
if count > 0 {
size := unsafe.Sizeof(*devInfo)
size := unsafe.Sizeof(*deviceInfo)
for i := 0; i < count; i++ {
devices = append(devices, NewDevInfo(
(*C.struct_bladerf_devinfo)(unsafe.Pointer(uintptr(unsafe.Pointer(devInfo))+(uintptr(i)*size))),
devices = append(devices, NewDeviceInfo(
(*C.struct_bladerf_devinfo)(unsafe.Pointer(uintptr(unsafe.Pointer(deviceInfo))+(uintptr(i)*size))),
))
}
FreeDeviceList(devices[0])
}
return devices
return devices, nil
}
func GetBootloaderList() []DevInfo {
var devInfo *C.struct_bladerf_devinfo
var devices []DevInfo
func GetBootloaderList() ([]DeviceInfo, error) {
var deviceInfo *C.struct_bladerf_devinfo
var devices []DeviceInfo
count := int(C.bladerf_get_bootloader_list(&devInfo))
codeOrCount := C.bladerf_get_bootloader_list(&deviceInfo)
if codeOrCount < 0 {
return nil, GetError(codeOrCount)
}
count := int(codeOrCount)
if count == 0 {
return make([]DeviceInfo, 0), nil
}
if count > 0 {
size := unsafe.Sizeof(*devInfo)
size := unsafe.Sizeof(*deviceInfo)
for i := 0; i < count; i++ {
devices = append(devices, NewDevInfo(
(*C.struct_bladerf_devinfo)(unsafe.Pointer(uintptr(unsafe.Pointer(devInfo))+(uintptr(i)*size))),
devices = append(devices, NewDeviceInfo(
(*C.struct_bladerf_devinfo)(unsafe.Pointer(uintptr(unsafe.Pointer(deviceInfo))+(uintptr(i)*size))),
))
}
FreeDeviceList(devices[0])
}
return devices
return devices, nil
}
func InitDevInfo() DevInfo {
var devInfo C.struct_bladerf_devinfo
C.bladerf_init_devinfo(&devInfo)
return NewDevInfo(&devInfo)
func InitDeviceInfo() DeviceInfo {
var deviceInfo C.struct_bladerf_devinfo
C.bladerf_init_devinfo(&deviceInfo)
return NewDeviceInfo(&deviceInfo)
}
func GetDevInfo(bladeRF *BladeRF) DevInfo {
var devInfo C.struct_bladerf_devinfo
C.bladerf_get_devinfo((*bladeRF).ref, &devInfo)
return NewDevInfo(&devInfo)
func GetDeviceInfo(bladeRF BladeRF) (DeviceInfo, error) {
var deviceInfo C.struct_bladerf_devinfo
err := GetError(C.bladerf_get_devinfo(bladeRF.ref, &deviceInfo))
if err != nil {
return DeviceInfo{}, err
}
return NewDeviceInfo(&deviceInfo), nil
}
func DevInfoMatches(a DevInfo, b DevInfo) bool {
func DeviceInfoMatches(a DeviceInfo, b DeviceInfo) bool {
return bool(C.bladerf_devinfo_matches(a.ref, b.ref))
}
func DevStrMatches(devStr string, info DevInfo) bool {
val := C.CString(devStr)
func DeviceStringMatches(deviceString string, deviceInfo DeviceInfo) bool {
val := C.CString(deviceString)
defer C.free(unsafe.Pointer(val))
return bool(C.bladerf_devstr_matches(val, info.ref))
return bool(C.bladerf_devstr_matches(val, deviceInfo.ref))
}
func GetDevInfoFromStr(devStr string) DevInfo {
val := C.CString(devStr)
func GetDeviceInfoFromString(deviceString string) (DeviceInfo, error) {
val := C.CString(deviceString)
defer C.free(unsafe.Pointer(val))
var devInfo C.struct_bladerf_devinfo
C.bladerf_get_devinfo_from_str(val, &devInfo)
return NewDevInfo(&devInfo)
var deviceInfo C.struct_bladerf_devinfo
err := GetError(C.bladerf_get_devinfo_from_str(val, &deviceInfo))
if err != nil {
return DeviceInfo{}, err
}
return NewDeviceInfo(&deviceInfo), nil
}
func OpenWithDevInfo(devInfo DevInfo) (BladeRF, error) {
func OpenWithDeviceInfo(deviceInfo DeviceInfo) (BladeRF, error) {
var bladeRF *C.struct_bladerf
err := GetError(C.bladerf_open_with_devinfo(&bladeRF, devInfo.ref))
err := GetError(C.bladerf_open_with_devinfo(&bladeRF, deviceInfo.ref))
if err != nil {
return BladeRF{}, err
@ -202,46 +255,66 @@ func OpenWithDevInfo(devInfo DevInfo) (BladeRF, error) { @@ -202,46 +255,66 @@ func OpenWithDevInfo(devInfo DevInfo) (BladeRF, error) {
return BladeRF{ref: bladeRF}, nil
}
func OpenWithDeviceIdentifier(identify string) BladeRF {
func OpenWithDeviceIdentifier(identify string) (BladeRF, error) {
var bladeRF *C.struct_bladerf
C.bladerf_open(&bladeRF, C.CString(identify))
return BladeRF{ref: bladeRF}
err := GetError(C.bladerf_open(&bladeRF, C.CString(identify)))
if err != nil {
return BladeRF{}, err
}
return BladeRF{ref: bladeRF}, nil
}
func Open() BladeRF {
func Open() (BladeRF, error) {
var bladeRF *C.struct_bladerf
C.bladerf_open(&bladeRF, nil)
return BladeRF{ref: bladeRF}
err := GetError(C.bladerf_open(&bladeRF, nil))
if err != nil {
return BladeRF{}, err
}
return BladeRF{ref: bladeRF}, nil
}
func Close(bladeRF BladeRF) {
C.bladerf_close(bladeRF.ref)
}
func SetLoopback(bladeRF *BladeRF, loopback loopback.Loopback) error {
return GetError(C.bladerf_set_loopback((*bladeRF).ref, C.bladerf_loopback(loopback)))
func SetLoopback(bladeRF BladeRF, loopback Loopback) error {
return GetError(C.bladerf_set_loopback(bladeRF.ref, C.bladerf_loopback(loopback)))
}
func SetFrequency(bladeRF *BladeRF, channel channel.Channel, frequency int) error {
return GetError(C.bladerf_set_frequency((*bladeRF).ref, C.bladerf_channel(channel), C.bladerf_frequency(frequency)))
func SetFrequency(bladeRF BladeRF, channel Channel, frequency uint64) error {
return GetError(C.bladerf_set_frequency(bladeRF.ref, C.bladerf_channel(channel), C.bladerf_frequency(frequency)))
}
func SetSampleRate(bladeRF *BladeRF, channel channel.Channel, sampleRate int) error {
func SetSampleRate(bladeRF BladeRF, channel Channel, sampleRate uint) (uint, error) {
var actual C.uint
err := GetError(C.bladerf_set_sample_rate((*bladeRF).ref, C.bladerf_channel(channel), C.uint(sampleRate), &actual))
return err
err := GetError(C.bladerf_set_sample_rate(bladeRF.ref, C.bladerf_channel(channel), C.bladerf_sample_rate(sampleRate), &actual))
if err != nil {
return 0, err
}
return uint(actual), err
}
func GetRationalSampleRate(bladeRF *BladeRF, channel channel.Channel) (RationalRate, error) {
func GetRationalSampleRate(bladeRF BladeRF, channel Channel) (RationalRate, error) {
var rate C.struct_bladerf_rational_rate
err := GetError(C.bladerf_get_rational_sample_rate((*bladeRF).ref, C.bladerf_channel(channel), &rate))
return NewRationalRate(&rate), err
err := GetError(C.bladerf_get_rational_sample_rate(bladeRF.ref, C.bladerf_channel(channel), &rate))
if err != nil {
return RationalRate{}, err
}
return NewRationalRate(&rate), nil
}
func GetSampleRateRange(bladeRF *BladeRF, channel channel.Channel) (int, int, int, error) {
func GetSampleRateRange(bladeRF BladeRF, channel Channel) (int, int, int, error) {
var bfRange *C.struct_bladerf_range
err := GetError(C.bladerf_get_sample_rate_range((*bladeRF).ref, C.bladerf_channel(channel), &bfRange))
err := GetError(C.bladerf_get_sample_rate_range(bladeRF.ref, C.bladerf_channel(channel), &bfRange))
if err != nil {
return 0, 0, 0, err
@ -250,138 +323,173 @@ func GetSampleRateRange(bladeRF *BladeRF, channel channel.Channel) (int, int, in @@ -250,138 +323,173 @@ func GetSampleRateRange(bladeRF *BladeRF, channel channel.Channel) (int, int, in
return int(bfRange.min), int(bfRange.max), int(bfRange.step), nil
}
func SetBandwidth(bladeRF *BladeRF, channel channel.Channel, bandwidth int) (int, error) {
func SetBandwidth(bladeRF BladeRF, channel Channel, bandwidth uint) (uint, error) {
var actual C.bladerf_bandwidth
return int(actual), GetError(C.bladerf_set_bandwidth((*bladeRF).ref, C.bladerf_channel(channel), C.uint(bandwidth), &actual))
err := GetError(C.bladerf_set_bandwidth(bladeRF.ref, C.bladerf_channel(channel), C.bladerf_bandwidth(bandwidth), &actual))
if err != nil {
return 0, err
}
return uint(actual), nil
}
func SetGain(bladeRF *BladeRF, channel channel.Channel, gain int) error {
return GetError(C.bladerf_set_gain((*bladeRF).ref, C.bladerf_channel(channel), C.int(gain)))
func SetGain(bladeRF BladeRF, channel Channel, gain int) error {
return GetError(C.bladerf_set_gain(bladeRF.ref, C.bladerf_channel(channel), C.bladerf_gain(gain)))
}
func GetGain(bladeRF *BladeRF, channel channel.Channel) (int, error) {
func GetGain(bladeRF BladeRF, channel Channel) (int, error) {
var gain C.bladerf_gain
err := GetError(C.bladerf_get_gain((*bladeRF).ref, C.bladerf_channel(channel), &gain))
if err == nil {
return int(gain), nil
err := GetError(C.bladerf_get_gain(bladeRF.ref, C.bladerf_channel(channel), &gain))
if err != nil {
return 0, err
}
return int(gain), err
return int(gain), nil
}
func GetGainStage(bladeRF *BladeRF, channel channel.Channel, stage string) (int, error) {
func GetGainStage(bladeRF BladeRF, channel Channel, stage string) (int, error) {
val := C.CString(stage)
defer C.free(unsafe.Pointer(val))
var gain C.bladerf_gain
err := GetError(C.bladerf_get_gain_stage((*bladeRF).ref, C.bladerf_channel(channel), val, &gain))
if err == nil {
return int(gain), nil
err := GetError(C.bladerf_get_gain_stage(bladeRF.ref, C.bladerf_channel(channel), val, &gain))
if err != nil {
return 0, err
}
return int(gain), err
return int(gain), nil
}
func GetGainMode(bladeRF *BladeRF, channel channel.Channel) (gain_mode.GainMode, error) {
func GetGainMode(bladeRF BladeRF, channel Channel) (GainMode, error) {
var mode C.bladerf_gain_mode
err := GetError(C.bladerf_get_gain_mode((*bladeRF).ref, C.bladerf_channel(channel), &mode))
result := gain_mode.GainMode(int(mode))
if err == nil {
return result, nil
err := GetError(C.bladerf_get_gain_mode(bladeRF.ref, C.bladerf_channel(channel), &mode))
if err != nil {
return 0, err
}
return result, err
return GainMode(mode), nil
}
func SetGainStage(bladeRF *BladeRF, channel channel.Channel, stage string, gain int) error {
func SetGainStage(bladeRF BladeRF, channel Channel, stage string, gain int) error {
val := C.CString(stage)
defer C.free(unsafe.Pointer(val))
return GetError(C.bladerf_set_gain_stage((*bladeRF).ref, C.bladerf_channel(channel), val, C.int(gain)))
return GetError(C.bladerf_set_gain_stage(bladeRF.ref, C.bladerf_channel(channel), val, C.bladerf_gain(gain)))
}
func GetGainStageRange(bladeRF *BladeRF, channel channel.Channel, stage string) (Range, error) {
var bfRange *C.struct_bladerf_range
func GetGainStageRange(bladeRF BladeRF, channel Channel, stage string) (Range, error) {
val := C.CString(stage)
defer C.free(unsafe.Pointer(val))
err := GetError(C.bladerf_get_gain_stage_range((*bladeRF).ref, C.bladerf_channel(channel), val, &bfRange))
var bfRange *C.struct_bladerf_range
err := GetError(C.bladerf_get_gain_stage_range(bladeRF.ref, C.bladerf_channel(channel), val, &bfRange))
if err == nil {
return Range{
ref: bfRange,
min: int64(bfRange.min),
max: int64(bfRange.max),
step: int64(bfRange.step),
scale: float64(bfRange.scale),
}, nil
if err != nil {
return Range{}, err
}
return Range{}, err
return Range{
ref: bfRange,
min: int64(bfRange.min),
max: int64(bfRange.max),
step: int64(bfRange.step),
scale: float64(bfRange.scale),
}, nil
}
func GetGainRange(bladeRF *BladeRF, channel channel.Channel) (Range, error) {
func GetGainRange(bladeRF BladeRF, channel Channel) (Range, error) {
var bfRange *C.struct_bladerf_range
err := GetError(C.bladerf_get_gain_range(bladeRF.ref, C.bladerf_channel(channel), &bfRange))
err := GetError(C.bladerf_get_gain_range((*bladeRF).ref, C.bladerf_channel(channel), &bfRange))
if err == nil {
return Range{
ref: bfRange,
min: int64(bfRange.min),
max: int64(bfRange.max),
step: int64(bfRange.step),
scale: float64(bfRange.scale),
}, nil
if err != nil {
return Range{}, err
}
return Range{}, err
return Range{
ref: bfRange,
min: int64(bfRange.min),
max: int64(bfRange.max),
step: int64(bfRange.step),
scale: float64(bfRange.scale),
}, nil
}
func GetNumberOfGainStages(bladeRF *BladeRF, channel channel.Channel) int {
count := int(C.bladerf_get_gain_stages((*bladeRF).ref, C.bladerf_channel(channel), nil, 0))
func GetNumberOfGainStages(bladeRF BladeRF, channel Channel) (int, error) {
countOrCode := C.bladerf_get_gain_stages(bladeRF.ref, C.bladerf_channel(channel), nil, 0)
if count < 1 {
return 0
if countOrCode < 0 {
return 0, GetError(countOrCode)
}
return count
return int(countOrCode), nil
}
func BackendSTR(backend backend.Backend) string {
func BackendString(backend Backend) string {
return C.GoString(C.bladerf_backend_str(C.bladerf_backend(backend)))
}
func GetBoardName(bladeRF *BladeRF) string {
return C.GoString(C.bladerf_get_board_name((*bladeRF).ref))
func GetBoardName(bladeRF BladeRF) string {
return C.GoString(C.bladerf_get_board_name(bladeRF.ref))
}
func SetUSBResetOnOpen(enabled bool) {
C.bladerf_set_usb_reset_on_open(C.bool(enabled))
}
func GetSerial(bladeRF *BladeRF) (string, error) {
func GetSerial(bladeRF BladeRF) (string, error) {
var serial C.char
GetError(C.bladerf_get_serial((*bladeRF).ref, &serial))
err := GetError(C.bladerf_get_serial(bladeRF.ref, &serial))
if err != nil {
return "", err
}
return C.GoString(&serial), nil
}
func GetSerialStruct(bladeRF *BladeRF) (Serial, error) {
func GetSerialStruct(bladeRF BladeRF) (Serial, error) {
var serial C.struct_bladerf_serial
GetError(C.bladerf_get_serial_struct((*bladeRF).ref, &serial))
err := GetError(C.bladerf_get_serial_struct(bladeRF.ref, &serial))
if err != nil {
return Serial{}, err
}
return NewSerial(&serial), nil
}
func GetGainStages(bladeRF *BladeRF, channel channel.Channel) []string {
func GetGainStages(bladeRF BladeRF, channel Channel) ([]string, error) {
var stage *C.char
var stages []string
count := int(C.bladerf_get_gain_stages(
(*bladeRF).ref,
numberOfGainStages, err := GetNumberOfGainStages(bladeRF, channel)
if err != nil {
return nil, err
}
countOrCode := C.bladerf_get_gain_stages(
bladeRF.ref,
C.bladerf_channel(channel),
&stage,
C.ulong(GetNumberOfGainStages(bladeRF, channel))),
C.ulong(numberOfGainStages),
)
if countOrCode < 0 {
return nil, GetError(countOrCode)
}
if countOrCode == 0 {
return make([]string, 0), nil
}
count := int(countOrCode)
if count > 0 {
size := unsafe.Sizeof(*stage)
@ -392,14 +500,24 @@ func GetGainStages(bladeRF *BladeRF, channel channel.Channel) []string { @@ -392,14 +500,24 @@ func GetGainStages(bladeRF *BladeRF, channel channel.Channel) []string {
}
}
return stages
return stages, nil
}
func GetGainModes(bladeRF *BladeRF, channel channel.Channel) []GainModes {
func GetGainModes(bladeRF BladeRF, channel Channel) ([]GainModes, error) {
var gainMode *C.struct_bladerf_gain_modes
var gainModes []GainModes
count := int(C.bladerf_get_gain_modes((*bladeRF).ref, C.bladerf_channel(channel), &gainMode))
countOrCode := C.bladerf_get_gain_modes(bladeRF.ref, C.bladerf_channel(channel), &gainMode)
if countOrCode < 0 {
return nil, GetError(countOrCode)
}
if countOrCode == 0 {
return make([]GainModes, 0), nil
}
count := int(countOrCode)
if count > 0 {
size := unsafe.Sizeof(*gainMode)
@ -411,52 +529,51 @@ func GetGainModes(bladeRF *BladeRF, channel channel.Channel) []GainModes { @@ -411,52 +529,51 @@ func GetGainModes(bladeRF *BladeRF, channel channel.Channel) []GainModes {
}
}
return gainModes
return gainModes, nil
}
func SetGainMode(bladeRF *BladeRF, channel channel.Channel, mode gain_mode.GainMode) error {
return GetError(C.bladerf_set_gain_mode((*bladeRF).ref, C.bladerf_channel(channel), C.bladerf_gain_mode(mode)))
func SetGainMode(bladeRF BladeRF, channel Channel, mode GainMode) error {
return GetError(C.bladerf_set_gain_mode(bladeRF.ref, C.bladerf_channel(channel), C.bladerf_gain_mode(mode)))
}
func EnableModule(bladeRF *BladeRF, channel channel.Channel) error {
return GetError(C.bladerf_enable_module((*bladeRF).ref, C.bladerf_channel(channel), true))
func EnableModule(bladeRF BladeRF, channel Channel) error {
return GetError(C.bladerf_enable_module(bladeRF.ref, C.bladerf_channel(channel), true))
}
func DisableModule(bladeRF *BladeRF, channel channel.Channel) error {
return GetError(C.bladerf_enable_module((*bladeRF).ref, C.bladerf_channel(channel), false))
func DisableModule(bladeRF BladeRF, channel Channel) error {
return GetError(C.bladerf_enable_module(bladeRF.ref, C.bladerf_channel(channel), false))
}
func SyncRX(bladeRF *BladeRF, bufferSize uintptr) ([]int16, error) {
func SyncRX(bladeRF BladeRF, bufferSize uintptr) ([]int16, error) {
var metadata C.struct_bladerf_metadata
start := C.malloc(C.size_t(C.sizeof_int16_t * bufferSize * 2 * 1))
err := GetError(C.bladerf_sync_rx(bladeRF.ref, start, C.uint(bufferSize), &metadata, 32))
var err error
var results []int16
err = GetError(C.bladerf_sync_rx((*bladeRF).ref, start, C.uint(bufferSize), &metadata, 32))
if err != nil {
return nil, err
}
if err == nil {
for i := 0; i < (int(metadata.actual_count)); i++ {
n := (*C.int16_t)(unsafe.Pointer(uintptr(start) + (C.sizeof_int16_t * uintptr(i))))
results = append(results, int16(*n))
}
var results []int16
return results, nil
for i := 0; i < (int(metadata.actual_count)); i++ {
n := (*C.int16_t)(unsafe.Pointer(uintptr(start) + (C.sizeof_int16_t * uintptr(i))))
results = append(results, int16(*n))
}
return nil, err
return results, nil
}
func InitStream(bladeRF *BladeRF, format format.Format, numBuffers int, samplesPerBuffer int, numTransfers int, callback func(data []int16)) *Stream {
func InitStream(bladeRF BladeRF,
format Format,
numBuffers int, samplesPerBuffer int, numTransfers int, callback func(data []int16)) (Stream, error) {
var buffers *unsafe.Pointer
var rxStream *C.struct_bladerf_stream
stream := Stream{ref: rxStream}
C.bladerf_init_stream(
err := GetError(C.bladerf_init_stream(
&((stream).ref),
(*bladeRF).ref,
bladeRF.ref,
(*[0]byte)((C.StreamCallback)),
&buffers,
C.ulong(numBuffers),
@ -464,29 +581,38 @@ func InitStream(bladeRF *BladeRF, format format.Format, numBuffers int, samplesP @@ -464,29 +581,38 @@ func InitStream(bladeRF *BladeRF, format format.Format, numBuffers int, samplesP
C.ulong(samplesPerBuffer),
C.ulong(numTransfers),
pointer.Save(NewUserData(callback, samplesPerBuffer)),
)
))
return &stream
if err != nil {
return Stream{}, err
}
return stream, nil
}
func DeInitStream(stream *Stream) {
func DeInitStream(stream Stream) {
C.bladerf_deinit_stream(stream.ref)
}
func GetStreamTimeout(bladeRF *BladeRF, direction direction.Direction) (int, error) {
func GetStreamTimeout(bladeRF BladeRF, direction Direction) (uint, error) {
var timeout C.uint
err := GetError(C.bladerf_get_stream_timeout((*bladeRF).ref, C.bladerf_direction(direction), &timeout))
return int(timeout), err
err := GetError(C.bladerf_get_stream_timeout(bladeRF.ref, C.bladerf_direction(direction), &timeout))
if err != nil {
return 0, err
}
return uint(timeout), err
}
func SetStreamTimeout(bladeRF *BladeRF, direction direction.Direction, timeout int) error {
return GetError(C.bladerf_set_stream_timeout((*bladeRF).ref, C.bladerf_direction(direction), C.uint(timeout)))
func SetStreamTimeout(bladeRF BladeRF, direction Direction, timeout uint) error {
return GetError(C.bladerf_set_stream_timeout(bladeRF.ref, C.bladerf_direction(direction), C.uint(timeout)))
}
func SyncConfig(bladeRF *BladeRF, layout channel_layout.ChannelLayout, format format.Format, numBuffers int, bufferSize int, numTransfers int, timeout int) error {
return GetError(C.bladerf_sync_config((*bladeRF).ref, C.bladerf_channel_layout(layout), C.bladerf_format(format), C.uint(numBuffers), C.uint(bufferSize), C.uint(numTransfers), C.uint(timeout)))
func SyncConfig(bladeRF BladeRF, layout ChannelLayout, format Format, numBuffers uint, bufferSize uint, numTransfers uint, timeout uint) error {
return GetError(C.bladerf_sync_config(bladeRF.ref, C.bladerf_channel_layout(layout), C.bladerf_format(format), C.uint(numBuffers), C.uint(bufferSize), C.uint(numTransfers), C.uint(timeout)))
}
func StartStream(stream *Stream, layout channel_layout.ChannelLayout) error {
func StartStream(stream Stream, layout ChannelLayout) error {
return GetError(C.bladerf_stream(stream.ref, C.bladerf_channel_layout(layout)))
}

253
bladerf_test.go

@ -2,11 +2,6 @@ package bladerf @@ -2,11 +2,6 @@ package bladerf
import (
"fmt"
"github.com/erayarslan/go-bladerf/backend"
"github.com/erayarslan/go-bladerf/channel_layout"
"github.com/erayarslan/go-bladerf/direction"
"github.com/erayarslan/go-bladerf/format"
"github.com/erayarslan/go-bladerf/gain_mode"
"github.com/erayarslan/go-bladerf/log"
"github.com/gordonklaus/portaudio"
fifo "github.com/racerxdl/go.fifo"
@ -61,11 +56,11 @@ func GetFinalDataString(input []int16) string { @@ -61,11 +56,11 @@ func GetFinalDataString(input []int16) string {
}
func TestBackendSTR(t *testing.T) {
x := BackendSTR(backend.LibUSB)
y := BackendSTR(backend.Dummy)
z := BackendSTR(backend.Any)
a := BackendSTR(backend.Cypress)
b := BackendSTR(backend.Linux)
x := BackendString(BackendLibUSB)
y := BackendString(BackendDummy)
z := BackendString(BackendAny)
a := BackendString(BackendCypress)
b := BackendString(BackendLinux)
fmt.Println(x, y, z, a, b)
}
@ -77,205 +72,220 @@ func TestSetUSBResetOnOpen(t *testing.T) { @@ -77,205 +72,220 @@ func TestSetUSBResetOnOpen(t *testing.T) {
func TestGetSerial(t *testing.T) {
log.SetVerbosity(log.Debug)
devices := GetDeviceList()
devices, _ := GetDeviceList()
if len(devices) == 0 {
fmt.Println("NO DEVICE")
return
}
rf, _ := OpenWithDevInfo(devices[0])
rf, _ := OpenWithDeviceInfo(devices[0])
defer Close(rf)
serial, _ := GetSerial(&rf)
serial, _ := GetSerial(rf)
fmt.Println(serial)
}
func TestGetSerialStruct(t *testing.T) {
log.SetVerbosity(log.Debug)
devices := GetDeviceList()
devices, _ := GetDeviceList()
if len(devices) == 0 {
fmt.Println("NO DEVICE")
return
}
rf, _ := OpenWithDevInfo(devices[0])
rf, _ := OpenWithDeviceInfo(devices[0])
defer Close(rf)
serial, _ := GetSerialStruct(&rf)
serial, _ := GetSerialStruct(rf)
fmt.Print(serial)
}
func TestGetFpgaSize(t *testing.T) {
log.SetVerbosity(log.Debug)
devices := GetDeviceList()
devices, _ := GetDeviceList()
if len(devices) == 0 {
fmt.Println("NO DEVICE")
return
}
rf, _ := OpenWithDevInfo(devices[0])
rf, _ := OpenWithDeviceInfo(devices[0])
defer Close(rf)
size, _ := GetFpgaSize(&rf)
size, _ := GetFpgaSize(rf)
fmt.Print(size)
}
func TestGetFpgaSource(t *testing.T) {
log.SetVerbosity(log.Debug)
devices := GetDeviceList()
devices, _ := GetDeviceList()
if len(devices) == 0 {
fmt.Println("NO DEVICE")
return
}
rf, _ := OpenWithDevInfo(devices[0])
rf, _ := OpenWithDeviceInfo(devices[0])
defer Close(rf)
source, _ := GetFpgaSource(&rf)
source, _ := GetFpgaSource(rf)
fmt.Print(source)
}
func TestGetDeviceSpeed(t *testing.T) {
log.SetVerbosity(log.Debug)
devices := GetDeviceList()
devices, _ := GetDeviceList()
if len(devices) == 0 {
fmt.Println("NO DEVICE")
return
}
rf, _ := OpenWithDevInfo(devices[0])
rf, _ := OpenWithDeviceInfo(devices[0])
defer Close(rf)
speed := GetDeviceSpeed(&rf)
speed := GetDeviceSpeed(rf)
fmt.Print(speed)
}
func TestGetBoardName(t *testing.T) {
log.SetVerbosity(log.Debug)
devices := GetDeviceList()
devices, _ := GetDeviceList()
if len(devices) == 0 {
fmt.Println("NO DEVICE")
return
}
rf, _ := OpenWithDevInfo(devices[0])
rf, _ := OpenWithDeviceInfo(devices[0])
defer Close(rf)
name := GetBoardName(&rf)
name := GetBoardName(rf)
fmt.Print(name)
}
func TestGetRationalSampleRate(t *testing.T) {
log.SetVerbosity(log.Debug)
devices := GetDeviceList()
devices, _ := GetDeviceList()
if len(devices) == 0 {
fmt.Println("NO DEVICE")
return
}
rf, _ := OpenWithDevInfo(devices[0])
rf, _ := OpenWithDeviceInfo(devices[0])
defer Close(rf)
rate, _ := GetRationalSampleRate(&rf, CHANNEL_RX(1))
rate, _ := GetRationalSampleRate(rf, ChannelRx(1))
fmt.Print(rate)
}
func TestGetFpgaBytes(t *testing.T) {
log.SetVerbosity(log.Debug)
devices := GetDeviceList()
devices, _ := GetDeviceList()
if len(devices) == 0 {
fmt.Println("NO DEVICE")
return
}
rf, _ := OpenWithDevInfo(devices[0])
rf, _ := OpenWithDeviceInfo(devices[0])
defer Close(rf)
bytes, _ := GetFpgaBytes(&rf)
bytes, _ := GetFpgaBytes(rf)
fmt.Print(bytes)
}
func TestGetFpgaFlashSize(t *testing.T) {
log.SetVerbosity(log.Debug)
devices := GetDeviceList()
devices, _ := GetDeviceList()
if len(devices) == 0 {
fmt.Println("NO DEVICE")
return
}
rf, _ := OpenWithDevInfo(devices[0])
rf, _ := OpenWithDeviceInfo(devices[0])
defer Close(rf)
size, guess, _ := GetFpgaFlashSize(&rf)
fmt.Print(size)
fmt.Print(guess)
size, guess, err := GetFpgaFlashSize(rf)
if err != nil {
panic(err)
} else {
fmt.Print(size)
fmt.Print(guess)
}
}
func TestGetFirmwareVersion(t *testing.T) {
log.SetVerbosity(log.Debug)
devices := GetDeviceList()
devices, _ := GetDeviceList()
if len(devices) == 0 {
fmt.Println("NO DEVICE")
return
}
rf, _ := OpenWithDevInfo(devices[0])
rf, _ := OpenWithDeviceInfo(devices[0])
defer Close(rf)
version, _ := GetFirmwareVersion(&rf)
version, err := GetFirmwareVersion(rf)
if err != nil {
panic(err)
}
fmt.Print(version.describe)
}
func TestGetFpgaVersion(t *testing.T) {
log.SetVerbosity(log.Debug)
devices := GetDeviceList()
devices, _ := GetDeviceList()
if len(devices) == 0 {
fmt.Println("NO DEVICE")
return
}
rf, _ := OpenWithDevInfo(devices[0])
rf, _ := OpenWithDeviceInfo(devices[0])
defer Close(rf)
version, _ := GetFpgaVersion(&rf)
version, _ := GetFpgaVersion(rf)
fmt.Print(version.describe)
}
func TestIsFpgaConfigured(t *testing.T) {
log.SetVerbosity(log.Debug)
devices := GetDeviceList()
devices, _ := GetDeviceList()
if len(devices) == 0 {
fmt.Println("NO DEVICE")
return
}
rf, _ := OpenWithDevInfo(devices[0])
rf, _ := OpenWithDeviceInfo(devices[0])
defer Close(rf)
isConfigured, _ := IsFpgaConfigured(&rf)
isConfigured, err := IsFpgaConfigured(rf)
if err != nil {
panic(err)
}
fmt.Print(isConfigured)
}
@ -284,63 +294,69 @@ func TestBladeRF(t *testing.T) { @@ -284,63 +294,69 @@ func TestBladeRF(t *testing.T) {
PrintVersion(GetVersion())
bootloaders := GetBootloaderList()
bootloaders, _ := GetBootloaderList()
fmt.Printf("Bootloaders Len: %d\n", len(bootloaders))
devices := GetDeviceList()
devices, _ := GetDeviceList()
fmt.Printf("Devices Len: %d\n", len(devices))
rf, _ := OpenWithDevInfo(devices[0])
LoadFpga(rf, "/Users/erayarslan/Downloads/hostedxA4-latest.rbf")
rf, _ := OpenWithDeviceInfo(devices[0])
_ = LoadFpga(rf, "/Users/erayarslan/Downloads/hostedxA4-latest.rbf")
Close(rf)
rf = Open()
info := GetDevInfo(&rf)
rf, _ = Open()
info, _ := GetDeviceInfo(rf)
Close(rf)
out, _ := OpenWithDevInfo(GetDevInfo(&rf))
h, _ := GetDeviceInfo(rf)
out, _ := OpenWithDeviceInfo(h)
Close(out)
Close(Open())
Close(OpenWithDeviceIdentifier("*:serial=" + info.serial))
out2, _ := OpenWithDevInfo(GetDevInfoFromStr("*:serial=" + info.serial))
c1, _ := Open()
Close(c1)
c2, _ := OpenWithDeviceIdentifier("*:serial=" + info.serial)
Close(c2)
o1, _ := GetDeviceInfoFromString("*:serial=" + info.serial)
out2, _ := OpenWithDeviceInfo(o1)
Close(out2)
result := DevInfoMatches(GetDevInfo(&rf), GetDevInfo(&rf))
g, _ := GetDeviceInfo(rf)
result := DeviceInfoMatches(g, g)
fmt.Println("---------")
fmt.Println(result)
fmt.Println("---------")
result = DevStrMatches("*:serial="+info.serial, GetDevInfo(&rf))
g0, _ := GetDeviceInfo(rf)
result = DeviceStringMatches("*:serial="+info.serial, g0)
fmt.Println("---------")
fmt.Println(result)
fmt.Println("---------")
rf = Open()
rf, _ = Open()
err := EnableModule(&rf, CHANNEL_RX(1))
err := EnableModule(rf, ChannelRx(1))
if err != nil {
fmt.Println(err)
}
_ = InitStream(&rf, format.SC16_Q11, 16, audioBufferSize, 8, cb)
_, _ = InitStream(rf, FormatSc16Q11, 16, audioBufferSize, 8, cb)
// _ = StartStream(stream, RX_X1)
}
func TestSetGainStage(t *testing.T) {
log.SetVerbosity(log.Debug)
devices := GetDeviceList()
devices, _ := GetDeviceList()
if len(devices) == 0 {
fmt.Println("NO DEVICE")
return
}
rf, _ := OpenWithDevInfo(devices[0])
rf, _ := OpenWithDeviceInfo(devices[0])
defer Close(rf)
stages := GetGainStages(&rf, CHANNEL_RX(1))
stages, _ := GetGainStages(rf, ChannelRx(1))
fmt.Println(len(stages))
bfRange, _ := GetGainStageRange(&rf, CHANNEL_RX(1), stages[0])
_ = SetGainStage(&rf, CHANNEL_RX(1), stages[0], int(bfRange.max))
gain, _ := GetGainStage(&rf, CHANNEL_RX(1), stages[0])
bfRange, _ := GetGainStageRange(rf, ChannelRx(1), stages[0])
_ = SetGainStage(rf, ChannelRx(1), stages[0], int(bfRange.max))
gain, _ := GetGainStage(rf, ChannelRx(1), stages[0])
fmt.Println(gain)
}
@ -350,30 +366,30 @@ func TestStream(t *testing.T) { @@ -350,30 +366,30 @@ func TestStream(t *testing.T) {
log.SetVerbosity(log.Debug)
devices := GetDeviceList()
channel := CHANNEL_RX(1)
devices, _ := GetDeviceList()
channel := ChannelRx(1)
if len(devices) == 0 {
fmt.Println("NO DEVICE")
return
}
rf, _ := OpenWithDevInfo(devices[0])
rf, _ := OpenWithDeviceInfo(devices[0])
defer Close(rf)
_ = SetFrequency(&rf, channel, 96600000)
min, max, step, _ := GetSampleRateRange(&rf, channel)
_ = SetFrequency(rf, channel, 96600000)
min, max, step, _ := GetSampleRateRange(rf, channel)
fmt.Printf("Min: %d, Max: %d, Step: %d\n", min, max, step)
_ = SetSampleRate(&rf, channel, 4e6)
_ = SyncConfig(&rf, channel_layout.RX_X2, format.SC16_Q11, 16, audioBufferSize, 8, 32)
actual, _ := SetBandwidth(&rf, channel, 240000)
_, _ = SetSampleRate(rf, channel, 4e6)
_ = SyncConfig(rf, RxX2, FormatSc16Q11, 16, audioBufferSize, 8, 32)
actual, _ := SetBandwidth(rf, channel, 240000)
fmt.Println(actual)
_ = EnableModule(&rf, channel)
_ = SetGainMode(&rf, channel, gain_mode.Hybrid_AGC)
_ = EnableModule(rf, channel)
_ = SetGainMode(rf, channel, GainModeHybridAgc)
demodulator = demodcore.MakeWBFMDemodulator(uint32(2e6), 80e3, 48000)
portaudio.Initialize()
_ = portaudio.Initialize()
h, _ := portaudio.DefaultHostApi()
p := portaudio.LowLatencyParameters(nil, h.DefaultOutputDevice)
@ -387,7 +403,7 @@ func TestStream(t *testing.T) { @@ -387,7 +403,7 @@ func TestStream(t *testing.T) {
go func() {
for {
data, _ := SyncRX(&rf, audioBufferSize)
data, _ := SyncRX(rf, audioBufferSize)
out := demodulator.Work(GetFinalData(data))
if out != nil {
@ -423,59 +439,59 @@ func cb(data []int16) { @@ -423,59 +439,59 @@ func cb(data []int16) {
func TestGetGainModes(t *testing.T) {
log.SetVerbosity(log.Debug)
devices := GetDeviceList()
devices, _ := GetDeviceList()
if len(devices) == 0 {
fmt.Println("NO DEVICE")
return
}
rf, _ := OpenWithDevInfo(devices[0])
rf, _ := OpenWithDeviceInfo(devices[0])
defer Close(rf)
GetGainModes(&rf, CHANNEL_RX(1))
_, _ = GetGainModes(rf, ChannelRx(1))
}
func TestGetGainRange(t *testing.T) {
log.SetVerbosity(log.Debug)
devices := GetDeviceList()
devices, _ := GetDeviceList()
if len(devices) == 0 {
fmt.Println("NO DEVICE")
return
}
rf, _ := OpenWithDevInfo(devices[0])
rf, _ := OpenWithDeviceInfo(devices[0])
defer Close(rf)
bfRange, _ := GetGainRange(&rf, CHANNEL_RX(1))
bfRange, _ := GetGainRange(rf, ChannelRx(1))
fmt.Println(bfRange.max)
}
func TestGPSData(t *testing.T) {
log.SetVerbosity(log.Debug)
channel := CHANNEL_RX(1)
channel := ChannelRx(1)
devices := GetDeviceList()
devices, _ := GetDeviceList()
if len(devices) == 0 {
fmt.Println("NO DEVICE")
return
}
rf, _ := OpenWithDevInfo(devices[0])
rf, _ := OpenWithDeviceInfo(devices[0])
defer Close(rf)
_ = SetFrequency(&rf, channel, 1525420000)
_ = SyncConfig(&rf, channel_layout.RX_X2, format.SC16_Q11, 16, audioBufferSize, 8, 32)
_ = SetSampleRate(&rf, channel, 2600000)
_, _ = SetBandwidth(&rf, channel, 2500000)
//_ = SetGainMode(&rf, channel, Hybrid_AGC)
_ = EnableModule(&rf, channel)
_ = SetFrequency(rf, channel, 1525420000)
_ = SyncConfig(rf, RxX2, FormatSc16Q11, 16, audioBufferSize, 8, 32)
_, _ = SetSampleRate(rf, channel, 2600000)
_, _ = SetBandwidth(rf, channel, 2500000)
_ = SetGainMode(rf, channel, GainModeHybridAgc)
_ = EnableModule(rf, channel)
for {
data, _ := SyncRX(&rf, audioBufferSize)
data, _ := SyncRX(rf, audioBufferSize)
out := GetFinalDataString(data)
fmt.Println(out)
}
@ -487,34 +503,46 @@ func TestAsyncStream(t *testing.T) { @@ -487,34 +503,46 @@ func TestAsyncStream(t *testing.T) {
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
channel := CHANNEL_RX(0)
channel := ChannelRx(0)
devices, err := GetDeviceList()
devices := GetDeviceList()
if err != nil {
panic(err)
}
if len(devices) == 0 {
fmt.Println("NO DEVICE")
return
}
rf, _ := OpenWithDevInfo(devices[0])
rf, err := OpenWithDeviceInfo(devices[0])