Skip to content

Commit

Permalink
✨ Basic API of the server ready
Browse files Browse the repository at this point in the history
  • Loading branch information
ronaldokun committed Oct 9, 2022
1 parent 3e122c3 commit aa8d04c
Show file tree
Hide file tree
Showing 17 changed files with 752 additions and 2 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: Deploy to GitHub Pages
on:
push:
branches: [ "main", "master" ]
workflow_dispatch:
jobs:
deploy:
runs-on: ubuntu-latest
steps: [uses: fastai/workflows/quarto-ghp@master]
7 changes: 7 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: CI
on: [workflow_dispatch, pull_request, push]

jobs:
test:
runs-on: ubuntu-latest
steps: [uses: fastai/workflows/nbdev-ci@master]
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,7 @@ dmypy.json

# Pyre type checker
.pyre/

# Quarto Files
_proc
_docs
227 changes: 227 additions & 0 deletions 00_core.ipynb

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include settings.ini
include LICENSE
include CONTRIBUTING.md
include README.md
recursive-exclude * __pycache__
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,23 @@
# hackrf2tcp
This repository holds python wrappers of hackrf_sweep cli
hackrf2tcp
================

<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

This file will become your README and also the index of your
documentation.

## Install

``` sh
pip install hackrf2tcp
```

## How to use

Fill me in please! Don’t forget code examples:

``` python
1+1
```

2
20 changes: 20 additions & 0 deletions _quarto.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
project:
type: website

format:
html:
theme: cosmo
css: styles.css
toc: true

website:
twitter-card: true
open-graph: true
repo-actions: [issue]
navbar:
background: primary
search: true
sidebar:
style: floating

metadata-files: [nbdev.yml, sidebar.yml]
93 changes: 93 additions & 0 deletions client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import socket
import json
from typing import Optional

HEADER_SIZE = 16
SERVER = socket.gethostbyname(socket.gethostname())
PORT = 4242
FORMAT = 'utf-8'
DISCONNECT_MSG = 'DISCONNECT'
# Create a socket
# socket.AF_INET - address family, IPv4, some otehr possible are AF_INET6, AF_BLUETOOTH, AF_UNIX
# socket.SOCK_STREAM - TCP, conection-based, socket.SOCK_DGRAM - UDP, connectionless, datagrams, socket.SOCK_RAW - raw IP packets
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect to a given ip and port
client_socket.connect((SERVER, PORT))

# Set connection to non-blocking state, so .recv() call won;t block, just return some exception we'll handle
client_socket.setblocking(False)

def encode_msg(msg):
message = msg.encode(FORMAT)
return bytes(f"{len(message):<{HEADER_SIZE}}", 'utf-8') + message


def send(msg):
client_socket.send(encode_msg(msg))
while msg_length := client_socket.recv(HEADER_SIZE).decode(FORMAT):
msg_length = int(msg_length)
if not msg_length: break
msg = client_socket.recv(msg_length).decode(FORMAT)
print(msg)
if msg == 'DISCONNECT': break

def fix_limits(value, min_val, max_val):
if value < min_val:
return min_val
return max_val if value > max_val else value

def sweep(serial_number: Optional[str] = None, # -d: Serial number of desired HackRF
amp_enable: bool = False, # -a: RX RF amplifier True=Enable, False=Disable
freq_min: int = 1, # -f: Minimum frequency in MHz
freq_max: 6000, # -f: Maximum frequency in MHz
antenna_enable: bool = False, # -p: Antenna port power, True=Enable, False=Disable
lna_gain: int = 0, # -l: RX LNA (IF) gain, 0-40dB, 8dB steps
vga_gain: int = 0, # -g: RX VGA (baseband) gain, 0-62dB, 2dB steps
num_samples: int = 8192, # -n: Number of samples per frequency, 8192-4294967296
bin_width: int 1000000, # -w: FFT bin width (frequency resolution) in Hz
one_shot: Optional[bool] = None, # -1: One shot mode
num_sweeps: Optional[int] = None, # -N: Number of sweeps to perform. If not set it defaults to continous sweeps if not one_shot mode
bin_out: Optional[bool] = None, # -B: binary output. Normalize only desirable if saving the result
bin_invfft_out: Optional[bool] = None, # -I: binary inverse FFT output. Normalize only desirable if saving the result
filename: Optional[str] = None # output file
):
amp_enable = 1 if amp_enable else 0

freq_min = fix_limits(freq_min, 0, 6000)
freq_max = fix_limits(freq_max, 0, 6000)
antenna_enable = 1 if antenna_enable else 0
lna_gain = fix_limits(lna_gain, 0, 40)
vga_gain = fix_limits(vga_gain, 0, 62)
num_samples = fix_limits(num_samples,8192, 4294967296)


parameters_dict = {'-d': serial_number,
'-a': 1 if amp_enable else 0,
'-f': f'{freq_min}:{freq_max}',
'-p': antenna_enable,
'-l': lna_gain,
'-g': vga_gain,
'-n': num_samples,
'-w': bin_width,
'-1': one_shot,
'-N': num_sweeps,
'-B': bin_out,
'-I': bin_invfft_out,
'-r': filename,
}

parameters_dict = {k:v for k,v in parameters_dict.items() if v is not None}


# send('-h')

send('-1')

send('-f=88:108 -w=10000 -N=10')

send(DISCONNECT_MSG)

# while True:
# msg = client_socket.recv(4080)
# print(msg)
Empty file added hackrf2tcp/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions hackrf2tcp/_modidx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Autogenerated by nbdev

d = { 'settings': { 'branch': 'master',
'doc_baseurl': '/hackrf2tcp',
'doc_host': 'https://ronaldokun.github.io',
'git_url': 'https://github.com/ronaldokun/hackrf2tcp',
'lib_path': 'hackrf2tcp'},
'syms': {'hackrf2tcp.core': {'hackrf2tcp.core.foo': ('core.html#foo', 'hackrf2tcp/core.py')}}}
7 changes: 7 additions & 0 deletions hackrf2tcp/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../00_core.ipynb.

# %% auto 0
__all__ = ['foo']

# %% ../00_core.ipynb 3
def foo(): pass
96 changes: 96 additions & 0 deletions index.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#| hide\n",
"from hackrf2tcp.core import *"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# hackrf2tcp\n",
"\n",
"> This repository holds python wrappers of hackrf_sweep cli"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This file will become your README and also the index of your documentation."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Install"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```sh\n",
"pip install hackrf2tcp\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## How to use"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Fill me in please! Don't forget code examples:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1+1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
9 changes: 9 additions & 0 deletions nbdev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
project:
output-dir: _docs

website:
title: "hackrf2tcp"
site-url: "https://ronaldokun.github.io/hackrf2tcp"
description: "This repository holds python wrappers of hackrf_sweep cli"
repo-branch: master
repo-url: "https://github.com/ronaldokun/hackrf2tcp"
Loading

0 comments on commit aa8d04c

Please sign in to comment.