Skip to content
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
6 changes: 5 additions & 1 deletion src/infuse_iot/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def DESCRIPTION(self) -> str:


class InfuseRpcCommand:
RPC_DATA = False
RPC_DATA_SEND = False
RPC_DATA_RECEIVE = False

@classmethod
def add_parser(cls, parser: argparse.ArgumentParser):
Expand All @@ -65,6 +66,9 @@ def data_payload(self) -> bytes:
"""Payload to send with RPC_DATA"""
raise NotImplementedError

def data_recv_cb(self, offset: int, data: bytes) -> None:
"""Data received callback"""

def data_progress_cb(self, offset: int) -> None:
"""Progress callback"""

Expand Down
3 changes: 3 additions & 0 deletions src/infuse_iot/generated/rpc_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,13 +437,16 @@ class request(ctypes.LittleEndianStructure):

class response(ctypes.LittleEndianStructure):
_fields_ = [
("bytes_logged", ctypes.c_uint64),
("logical_blocks", ctypes.c_uint32),
("physical_blocks", ctypes.c_uint32),
("boot_block", ctypes.c_uint32),
("current_block", ctypes.c_uint32),
("earliest_block", ctypes.c_uint32),
("block_size", ctypes.c_uint16),
("block_overhead", ctypes.c_uint16),
("erase_unit", ctypes.c_uint16),
("uptime", ctypes.c_uint32),
]
_pack_ = 1

Expand Down
8 changes: 3 additions & 5 deletions src/infuse_iot/generated/tdf_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def val_fmt(self) -> str:
class TdfStructBase(ctypes.LittleEndianStructure):
def iter_fields(
self,
field,
field: str,
) -> Generator[TdfField, None, None]:
for subfield in self._fields_:
sf_name = _public_name(subfield)
Expand All @@ -49,13 +49,11 @@ def iter_fields(


class TdfReadingBase(ctypes.LittleEndianStructure):
def iter_fields(
self,
) -> Generator[TdfField, None, None]:
def iter_fields(self, nested_iter: bool = True) -> Generator[TdfField, None, None]:
for field in self._fields_:
f_name = _public_name(field)
val = getattr(self, f_name)
if isinstance(val, ctypes.LittleEndianStructure):
if nested_iter and isinstance(val, ctypes.LittleEndianStructure):
yield from val.iter_fields(f_name)
else:
if isinstance(val, ctypes.Array):
Expand Down
86 changes: 82 additions & 4 deletions src/infuse_iot/generated/tdf_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,27 @@ class tdf_struct_lte_cell_id_global(TdfStructBase):
"tac": "{}",
}

class tdf_struct_bt_addr_le(TdfStructBase):
"""Bluetooth address type (bt_addr_le_t)"""

_fields_ = [
("type", ctypes.c_uint8),
("_val", 6 * ctypes.c_uint8),
]
_pack_ = 1
_postfix_ = {
"type": "",
"val": "",
}
_display_fmt_ = {
"type": "{}",
"val": "0x{:012x}",
}

@property
def val(self):
return int.from_bytes(self._val, byteorder='little')


class readings:
class announce(TdfReadingBase):
Expand Down Expand Up @@ -533,12 +554,12 @@ class ubx_nav_pvt(TdfReadingBase):
"hour": "{}",
"min": "{}",
"sec": "{}",
"valid": "{}",
"valid": "0x{:02x}",
"t_acc": "{}",
"nano": "{}",
"fix_type": "{}",
"flags": "{}",
"flags2": "{}",
"flags": "0x{:02x}",
"flags2": "0x{:02x}",
"num_sv": "{}",
"lon": "{}",
"lat": "{}",
Expand All @@ -554,7 +575,7 @@ class ubx_nav_pvt(TdfReadingBase):
"s_acc": "{}",
"head_acc": "{}",
"p_dop": "{}",
"flags3": "{}",
"flags3": "0x{:04x}",
"reserved0": "{}",
"head_veh": "{}",
"mag_dec": "{}",
Expand Down Expand Up @@ -786,6 +807,60 @@ class gnss_fix_info(TdfReadingBase):
"num_sv": "{}",
}

class bluetooth_connection(TdfReadingBase):
"""Bluetooth connection state change"""

name = "BLUETOOTH_CONNECTION"
_fields_ = [
("address", structs.tdf_struct_bt_addr_le),
("connected", ctypes.c_uint8),
]
_pack_ = 1
_postfix_ = {
"address": "",
"connected": "",
}
_display_fmt_ = {
"address": "{}",
"connected": "{}",
}

class bluetooth_rssi(TdfReadingBase):
"""Received signal strength of Bluetooth device"""

name = "BLUETOOTH_RSSI"
_fields_ = [
("address", structs.tdf_struct_bt_addr_le),
("rssi", ctypes.c_int8),
]
_pack_ = 1
_postfix_ = {
"address": "",
"rssi": "dBm",
}
_display_fmt_ = {
"address": "{}",
"rssi": "{}",
}

class bluetooth_data_throughput(TdfReadingBase):
"""Data throughput of Bluetooth link"""

name = "BLUETOOTH_DATA_THROUGHPUT"
_fields_ = [
("address", structs.tdf_struct_bt_addr_le),
("throughput", ctypes.c_int32),
]
_pack_ = 1
_postfix_ = {
"address": "",
"throughput": "B/sec",
}
_display_fmt_ = {
"address": "{}",
"throughput": "{}",
}

class array_type(TdfReadingBase):
"""Example array type"""

Expand Down Expand Up @@ -828,5 +903,8 @@ class array_type(TdfReadingBase):
26: readings.runtime_error,
27: readings.charger_en_control,
28: readings.gnss_fix_info,
29: readings.bluetooth_connection,
30: readings.bluetooth_rssi,
31: readings.bluetooth_data_throughput,
100: readings.array_type,
}
4 changes: 3 additions & 1 deletion src/infuse_iot/rpc_wrappers/application_info.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env python3

import os

import infuse_iot.generated.rpc_definitions as defs
from infuse_iot.commands import InfuseRpcCommand

Expand All @@ -17,7 +19,7 @@ def request_struct(self):

def handle_response(self, return_code, response):
if return_code != 0:
print(f"Failed to query current time ({return_code})")
print(f"Failed to query current time ({os.strerror(-return_code)})")
return

r = response
Expand Down
4 changes: 3 additions & 1 deletion src/infuse_iot/rpc_wrappers/bt_connect_infuse.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env python3

import os

import infuse_iot.generated.rpc_definitions as defs
from infuse_iot.commands import InfuseRpcCommand
from infuse_iot.generated.rpc_definitions import (
Expand Down Expand Up @@ -57,7 +59,7 @@ def request_struct(self) -> defs.bt_connect_infuse.request:

def handle_response(self, return_code, response):
if return_code < 0:
print(f"Failed to connect ({return_code})")
print(f"Failed to connect ({os.strerror(-return_code)})")
return

if return_code == 1:
Expand Down
3 changes: 2 additions & 1 deletion src/infuse_iot/rpc_wrappers/bt_disconnect.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3

import os

import infuse_iot.generated.rpc_definitions as defs
from infuse_iot.commands import InfuseRpcCommand
Expand Down Expand Up @@ -39,7 +40,7 @@ def request_struct(self):

def handle_response(self, return_code, response):
if return_code != 0:
print(f"Failed to disconnect ({return_code})")
print(f"Failed to disconnect ({os.strerror(-return_code)})")
return
else:
print("Disconnected")
3 changes: 2 additions & 1 deletion src/infuse_iot/rpc_wrappers/coap_download.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

import ctypes
import os

import infuse_iot.generated.rpc_definitions as defs
from infuse_iot.commands import InfuseRpcCommand
Expand Down Expand Up @@ -85,7 +86,7 @@ class request(ctypes.LittleEndianStructure):

def handle_response(self, return_code, response):
if return_code != 0:
print(f"Failed to download file ({return_code})")
print(f"Failed to download file ({os.strerror(-return_code)})")
return
else:
print("File downloaded")
Expand Down
49 changes: 49 additions & 0 deletions src/infuse_iot/rpc_wrappers/data_logger_state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env python3

import os

import infuse_iot.generated.rpc_definitions as defs
from infuse_iot.commands import InfuseRpcCommand


class data_logger_state(InfuseRpcCommand, defs.data_logger_state):
@classmethod
def add_parser(cls, parser):
logger = parser.add_mutually_exclusive_group(required=True)
logger.add_argument("--onboard", action="store_true", help="Onboard flash logger")
logger.add_argument("--removable", action="store_true", help="Removable flash logger (SD)")

def __init__(self, args):
if args.onboard:
self.logger = defs.rpc_enum_data_logger.FLASH_ONBOARD
elif args.removable:
self.logger = defs.rpc_enum_data_logger.FLASH_REMOVABLE
else:
raise NotImplementedError

def request_struct(self):
return self.request(self.logger)

def handle_response(self, return_code, response):
if return_code != 0:
print(f"Failed to query current time ({os.strerror(-return_code)})")
return

def sizeof_fmt(num, suffix="B"):
for unit in ("", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"):
if abs(num) < 1024.0:
return f"{num:3.2f} {unit}{suffix}"
num /= 1024.0
return f"{num:.2f} Yi{suffix}"

r = response
total_logged = r.current_block * r.block_size
percent = 100 * r.current_block / r.logical_blocks
block_rate = (r.current_block - r.boot_block) / r.uptime
byte_rate = r.bytes_logged / r.uptime

print(f"{self.logger.name}")
print(f"\t Logged: {sizeof_fmt(total_logged)}")
print(f"\t Blocks: {r.current_block}/{r.logical_blocks} ({percent:.0f}%)")
print(f"\t Block Rate: {block_rate:.2f} blocks/sec")
print(f"\t Byte Rate: {sizeof_fmt(byte_rate)}/sec")
4 changes: 3 additions & 1 deletion src/infuse_iot/rpc_wrappers/fault.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env python3

import os

import infuse_iot.generated.rpc_definitions as defs
from infuse_iot.commands import InfuseRpcCommand

Expand Down Expand Up @@ -65,4 +67,4 @@ def request_struct(self):
return self.request(self._fault_type, 0)

def handle_response(self, return_code, _):
print(f"Failed to trigger exception ({return_code})")
print(f"Failed to trigger exception ({os.strerror(-return_code)})")
5 changes: 3 additions & 2 deletions src/infuse_iot/rpc_wrappers/file_write_basic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

import binascii
import os

from rich.progress import (
DownloadColumn,
Expand All @@ -14,7 +15,7 @@


class file_write_basic(InfuseRpcCommand, defs.file_write_basic):
RPC_DATA = True
RPC_DATA_SEND = True

@classmethod
def add_parser(cls, parser):
Expand Down Expand Up @@ -95,7 +96,7 @@ def handle_response(self, return_code, response):
self.progress.stop()

if return_code != 0:
print(f"Failed to write file ({return_code})")
print(f"Failed to write file ({os.strerror(-return_code)})")
return
print("File written")
print(f"\tLength: {response.recv_len}")
Expand Down
Loading
Loading