mirror of
https://github.com/intrepidcs/libicsneo.git
synced 2026-11-04 12:59:07 +01:00
Compare commits
6
Commits
4db27aa187
..
1.5.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f4fe69f992 | ||
|
|
7ff14ae86d | ||
|
|
5c61a3b3c1 | ||
|
|
320fec74bd | ||
|
|
fd8f7b328f | ||
|
|
6ac041cadc |
+19
-27
@@ -47,7 +47,7 @@ unit_test windows/x64:
|
|||||||
script:
|
script:
|
||||||
- apt update -y
|
- apt update -y
|
||||||
- apt upgrade -y
|
- apt upgrade -y
|
||||||
- apt install -y g++ ninja-build cmake libpcap-dev git ca-certificates
|
- apt install -y g++ ninja-build cmake git ca-certificates
|
||||||
- echo "$ICS_IPA_CA_CRT" >/usr/local/share/ca-certificates/ica-ipa-ca.crt
|
- echo "$ICS_IPA_CA_CRT" >/usr/local/share/ca-certificates/ica-ipa-ca.crt
|
||||||
- update-ca-certificates --fresh
|
- update-ca-certificates --fresh
|
||||||
- sh ci/build-posix.sh
|
- sh ci/build-posix.sh
|
||||||
@@ -64,7 +64,6 @@ unit_test windows/x64:
|
|||||||
script:
|
script:
|
||||||
- apt update -y
|
- apt update -y
|
||||||
- apt upgrade -y
|
- apt upgrade -y
|
||||||
- apt install -y libpcap-dev
|
|
||||||
- build/libicsneo-unit-tests
|
- build/libicsneo-unit-tests
|
||||||
tags:
|
tags:
|
||||||
- linux-build
|
- linux-build
|
||||||
@@ -75,7 +74,7 @@ unit_test windows/x64:
|
|||||||
script:
|
script:
|
||||||
- apt update -y
|
- apt update -y
|
||||||
- apt upgrade -y
|
- apt upgrade -y
|
||||||
- apt install -y clang lld ninja-build cmake libpcap-dev git ca-certificates
|
- apt install -y clang lld ninja-build cmake git ca-certificates
|
||||||
- echo "$ICS_IPA_CA_CRT" >/usr/local/share/ca-certificates/ica-ipa-ca.crt
|
- echo "$ICS_IPA_CA_CRT" >/usr/local/share/ca-certificates/ica-ipa-ca.crt
|
||||||
- update-ca-certificates --fresh
|
- update-ca-certificates --fresh
|
||||||
- CC=clang CXX=clang++ LDFLAGS=-fuse-ld=lld sh ci/build-posix.sh
|
- CC=clang CXX=clang++ LDFLAGS=-fuse-ld=lld sh ci/build-posix.sh
|
||||||
@@ -92,7 +91,6 @@ unit_test windows/x64:
|
|||||||
script:
|
script:
|
||||||
- apt update -y
|
- apt update -y
|
||||||
- apt upgrade -y
|
- apt upgrade -y
|
||||||
- apt install -y libpcap-dev
|
|
||||||
- build/libicsneo-unit-tests
|
- build/libicsneo-unit-tests
|
||||||
tags:
|
tags:
|
||||||
- linux-build
|
- linux-build
|
||||||
@@ -159,7 +157,7 @@ unit_test linux/ubuntu/2604/amd64/clang:
|
|||||||
- echo max_parallel_downloads=10 >>/etc/dnf/dnf.conf
|
- echo max_parallel_downloads=10 >>/etc/dnf/dnf.conf
|
||||||
- echo fastestmirror=True >>/etc/dnf/dnf.conf
|
- echo fastestmirror=True >>/etc/dnf/dnf.conf
|
||||||
- dnf upgrade -y
|
- dnf upgrade -y
|
||||||
- dnf install -y g++ libpcap-devel cmake ninja-build git ca-certificates
|
- dnf install -y g++ cmake ninja-build git ca-certificates
|
||||||
- echo "$ICS_IPA_CA_CRT" >/etc/pki/ca-trust/source/anchors/ica-ipa-ca.crt
|
- echo "$ICS_IPA_CA_CRT" >/etc/pki/ca-trust/source/anchors/ica-ipa-ca.crt
|
||||||
- update-ca-trust
|
- update-ca-trust
|
||||||
- sh ci/build-posix.sh
|
- sh ci/build-posix.sh
|
||||||
@@ -180,7 +178,6 @@ unit_test linux/ubuntu/2604/amd64/clang:
|
|||||||
- echo max_parallel_downloads=10 >>/etc/dnf/dnf.conf
|
- echo max_parallel_downloads=10 >>/etc/dnf/dnf.conf
|
||||||
- echo fastestmirror=True >>/etc/dnf/dnf.conf
|
- echo fastestmirror=True >>/etc/dnf/dnf.conf
|
||||||
- dnf upgrade -y
|
- dnf upgrade -y
|
||||||
- dnf install -y libpcap-devel
|
|
||||||
- build/libicsneo-unit-tests
|
- build/libicsneo-unit-tests
|
||||||
tags:
|
tags:
|
||||||
- linux-build
|
- linux-build
|
||||||
@@ -195,7 +192,7 @@ unit_test linux/ubuntu/2604/amd64/clang:
|
|||||||
- echo max_parallel_downloads=10 >>/etc/dnf/dnf.conf
|
- echo max_parallel_downloads=10 >>/etc/dnf/dnf.conf
|
||||||
- echo fastestmirror=True >>/etc/dnf/dnf.conf
|
- echo fastestmirror=True >>/etc/dnf/dnf.conf
|
||||||
- dnf upgrade -y
|
- dnf upgrade -y
|
||||||
- dnf install -y clang lld libpcap-devel cmake ninja-build git ca-certificates
|
- dnf install -y clang lld cmake ninja-build git ca-certificates
|
||||||
- echo "$ICS_IPA_CA_CRT" >/etc/pki/ca-trust/source/anchors/ica-ipa-ca.crt
|
- echo "$ICS_IPA_CA_CRT" >/etc/pki/ca-trust/source/anchors/ica-ipa-ca.crt
|
||||||
- update-ca-trust
|
- update-ca-trust
|
||||||
- CC=clang CXX=clang++ LDFLAGS=-fuse-ld=lld sh ci/build-posix.sh
|
- CC=clang CXX=clang++ LDFLAGS=-fuse-ld=lld sh ci/build-posix.sh
|
||||||
@@ -216,7 +213,6 @@ unit_test linux/ubuntu/2604/amd64/clang:
|
|||||||
- echo max_parallel_downloads=10 >>/etc/dnf/dnf.conf
|
- echo max_parallel_downloads=10 >>/etc/dnf/dnf.conf
|
||||||
- echo fastestmirror=True >>/etc/dnf/dnf.conf
|
- echo fastestmirror=True >>/etc/dnf/dnf.conf
|
||||||
- dnf upgrade -y
|
- dnf upgrade -y
|
||||||
- dnf install -y libpcap-devel
|
|
||||||
- build/libicsneo-unit-tests
|
- build/libicsneo-unit-tests
|
||||||
tags:
|
tags:
|
||||||
- linux-build
|
- linux-build
|
||||||
@@ -279,13 +275,9 @@ build python/linux/amd64:
|
|||||||
tags:
|
tags:
|
||||||
- linux-native-amd64
|
- linux-native-amd64
|
||||||
variables:
|
variables:
|
||||||
CIBW_BEFORE_ALL: sh ci/bootstrap-cibuildwheel.sh && sh ci/bootstrap-libpcap.sh
|
|
||||||
CIBW_BUILD: "*manylinux*" # no musl
|
|
||||||
CIBW_ARCHS: x86_64
|
CIBW_ARCHS: x86_64
|
||||||
CIBW_ENVIRONMENT: PCAP_ROOT=/project/libpcap/install
|
|
||||||
SKBUILD_CMAKE_DEFINE: ICSPB_BOOTSTRAP_DIR=/project/icspb
|
|
||||||
script:
|
script:
|
||||||
- sh ci/build-wheel-posix.sh
|
- sh ci/build-wheel-linux.sh
|
||||||
artifacts:
|
artifacts:
|
||||||
paths:
|
paths:
|
||||||
- wheelhouse
|
- wheelhouse
|
||||||
@@ -295,13 +287,9 @@ build python/linux/arm64:
|
|||||||
tags:
|
tags:
|
||||||
- arm64-linux-build
|
- arm64-linux-build
|
||||||
variables:
|
variables:
|
||||||
CIBW_BEFORE_ALL: sh ci/bootstrap-cibuildwheel.sh && sh ci/bootstrap-libpcap.sh
|
|
||||||
CIBW_BUILD: "*manylinux*" # no musl
|
|
||||||
CIBW_ARCHS: aarch64
|
CIBW_ARCHS: aarch64
|
||||||
CIBW_ENVIRONMENT: PCAP_ROOT=/project/libpcap/install
|
|
||||||
SKBUILD_CMAKE_DEFINE: ICSPB_BOOTSTRAP_DIR=/project/icspb
|
|
||||||
script:
|
script:
|
||||||
- sh ci/build-wheel-posix.sh
|
- sh ci/build-wheel-linux.sh
|
||||||
artifacts:
|
artifacts:
|
||||||
paths:
|
paths:
|
||||||
- wheelhouse
|
- wheelhouse
|
||||||
@@ -310,14 +298,8 @@ build python/macos:
|
|||||||
stage: build
|
stage: build
|
||||||
tags:
|
tags:
|
||||||
- macos-arm64
|
- macos-arm64
|
||||||
variables:
|
|
||||||
CIBW_BEFORE_ALL: sh ci/bootstrap-libpcap.sh
|
|
||||||
CIBW_ARCHS: arm64
|
|
||||||
CIBW_ENVIRONMENT: PCAP_ROOT=$CI_PROJECT_DIR/libpcap/install
|
|
||||||
SKBUILD_CMAKE_DEFINE: ICSPB_BOOTSTRAP_DIR=$CI_PROJECT_DIR/icspb
|
|
||||||
MACOSX_DEPLOYMENT_TARGET: 10.14
|
|
||||||
script:
|
script:
|
||||||
- sh ci/build-wheel-posix.sh
|
- sh ci/build-wheel-macos.sh
|
||||||
artifacts:
|
artifacts:
|
||||||
paths:
|
paths:
|
||||||
- wheelhouse
|
- wheelhouse
|
||||||
@@ -347,9 +329,10 @@ deploy python/pypi:
|
|||||||
image: python:3.13
|
image: python:3.13
|
||||||
rules:
|
rules:
|
||||||
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
|
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
|
||||||
|
- if: $CI_COMMIT_TAG
|
||||||
script:
|
script:
|
||||||
- python3 -m pip install -U twine
|
- python3 -m pip install -U twine
|
||||||
- twine upload wheelhouse/*
|
- twine upload --verbose wheelhouse/*
|
||||||
dependencies:
|
dependencies:
|
||||||
- build python/linux/amd64
|
- build python/linux/amd64
|
||||||
- build python/linux/arm64
|
- build python/linux/arm64
|
||||||
@@ -367,14 +350,23 @@ push github:
|
|||||||
tags:
|
tags:
|
||||||
- linux-build
|
- linux-build
|
||||||
image: alpine:latest
|
image: alpine:latest
|
||||||
|
variables:
|
||||||
|
GIT_DEPTH: 0
|
||||||
rules:
|
rules:
|
||||||
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
|
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
|
||||||
when: delayed
|
when: delayed
|
||||||
start_in: 10 minutes
|
start_in: 10 minutes
|
||||||
|
variables:
|
||||||
|
REFSPEC: HEAD:master
|
||||||
|
- if: $CI_COMMIT_TAG
|
||||||
|
when: delayed
|
||||||
|
start_in: 10 minutes
|
||||||
|
variables:
|
||||||
|
REFSPEC: $CI_COMMIT_TAG
|
||||||
dependencies:
|
dependencies:
|
||||||
- deploy python/pypi
|
- deploy python/pypi
|
||||||
needs:
|
needs:
|
||||||
- deploy python/pypi
|
- deploy python/pypi
|
||||||
script:
|
script:
|
||||||
- apk add git
|
- apk add git
|
||||||
- git push https://$LIBICSNEO_GITHUB_USERNAME:$LIBICSNEO_GITHUB_TOKEN@github.com/intrepidcs/libicsneo.git HEAD:master
|
- git push https://$LIBICSNEO_GITHUB_USERNAME:$LIBICSNEO_GITHUB_TOKEN@github.com/intrepidcs/libicsneo.git $REFSPEC
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
# Intrepid Control System, Inc. neoVI Devices
|
|
||||||
SUBSYSTEM=="usb", ATTRS{idVendor}=="093c", GROUP="users", MODE="0666"
|
|
||||||
KERNEL=="ttyUSB?", ATTRS{idVendor}=="093c", GROUP="users", MODE="0666"
|
|
||||||
KERNEL=="ttyACM?", ATTRS{idVendor}=="093c", GROUP="users", MODE="0666"
|
|
||||||
|
|
||||||
ACTION=="add", SUBSYSTEMS=="usb", ATTRS{idVendor}=="093c", KERNEL=="ttyUSB*", \
|
|
||||||
RUN+="/bin/sh -c 'echo $id:1.0>/sys/bus/usb/drivers/ftdi_sio/unbind'"
|
|
||||||
|
|
||||||
ACTION=="add", SUBSYSTEMS=="usb", ATTRS{idVendor}=="093c", DRIVER=="usbhid", \
|
|
||||||
RUN+="/bin/sh -c 'echo $id:1.0>/sys/bus/usb/drivers/usbhid/unbind'"
|
|
||||||
+80
-145
@@ -53,22 +53,20 @@ option(LIBICSNEO_BUILD_ICSNEOC2 "Build dynamic C2 library" ON)
|
|||||||
option(LIBICSNEO_BUILD_ICSNEOC2_STATIC "Build static C2 library" ON)
|
option(LIBICSNEO_BUILD_ICSNEOC2_STATIC "Build static C2 library" ON)
|
||||||
option(LIBICSNEO_BUILD_ICSNEOLEGACY "Build icsnVC40 compatibility library" ON)
|
option(LIBICSNEO_BUILD_ICSNEOLEGACY "Build icsnVC40 compatibility library" ON)
|
||||||
option(LIBICSNEO_BUILD_ICSNEOLEGACY_STATIC "Build static icsnVC40 compatibility library" ON)
|
option(LIBICSNEO_BUILD_ICSNEOLEGACY_STATIC "Build static icsnVC40 compatibility library" ON)
|
||||||
set(LIBICSNEO_NPCAP_INCLUDE_DIR "" CACHE STRING "Npcap include directory; set to build with Npcap")
|
|
||||||
|
|
||||||
# Device Drivers
|
# Device Drivers
|
||||||
# You almost certainly don't want firmio for your build,
|
# You almost certainly don't want firmio for your build,
|
||||||
# it is only relevant for communication between Linux and
|
# it is only relevant for communication between Linux and
|
||||||
# CoreMini from the onboard processor of the device.
|
# CoreMini from the onboard processor of the device.
|
||||||
option(LIBICSNEO_ENABLE_FIRMIO "Enable communication between Linux and CoreMini within the same device" OFF)
|
option(LIBICSNEO_ENABLE_FIRMIO "Enable communication between Linux and CoreMini within the same device" OFF)
|
||||||
option(LIBICSNEO_ENABLE_RAW_ETHERNET "Enable devices which communicate over raw ethernet" ON)
|
|
||||||
option(LIBICSNEO_ENABLE_CDCACM "Enable devices which communicate over USB CDC ACM" ON)
|
|
||||||
option(LIBICSNEO_ENABLE_TCP "Enable devices which communicate over TCP" OFF)
|
|
||||||
option(LIBICSNEO_ENABLE_DXX "Enable devices which communicate over D2XX/D3XX via libredxx" ON)
|
|
||||||
|
|
||||||
option(LIBICSNEO_ENABLE_BINDINGS_PYTHON "Enable Python library" OFF)
|
option(LIBICSNEO_ENABLE_BINDINGS_PYTHON "Enable Python library" OFF)
|
||||||
|
|
||||||
|
option(LIBICSNEO_ENABLE_BOOTLOADER "Enable bootloader firmware update support" OFF)
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 17)
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded")
|
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded")
|
||||||
|
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
||||||
|
|
||||||
include(GNUInstallDirs)
|
include(GNUInstallDirs)
|
||||||
|
|
||||||
@@ -142,82 +140,10 @@ if(LIBICSNEO_BUILD_DOCS)
|
|||||||
endif()
|
endif()
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if(WIN32)
|
if(LIBICSNEO_ENABLE_FIRMIO AND NOT WIN32)
|
||||||
set(PLATFORM_SRC
|
set(PLATFORM_SRC platform/posix/firmio.cpp)
|
||||||
platform/windows/strings.cpp
|
|
||||||
platform/windows/registry.cpp
|
|
||||||
)
|
|
||||||
|
|
||||||
if(LIBICSNEO_ENABLE_RAW_ETHERNET)
|
|
||||||
list(APPEND PLATFORM_SRC
|
|
||||||
platform/windows/pcap.cpp
|
|
||||||
platform/windows/internal/pcapdll.cpp
|
|
||||||
)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
if(LIBICSNEO_ENABLE_CDCACM)
|
|
||||||
list(APPEND PLATFORM_SRC
|
|
||||||
platform/windows/cdcacm.cpp
|
|
||||||
)
|
|
||||||
endif()
|
|
||||||
else() # Darwin or Linux
|
|
||||||
set(PLATFORM_SRC)
|
|
||||||
|
|
||||||
if(LIBICSNEO_ENABLE_FIRMIO)
|
|
||||||
list(APPEND PLATFORM_SRC
|
|
||||||
platform/posix/firmio.cpp
|
|
||||||
)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
if(LIBICSNEO_ENABLE_RAW_ETHERNET)
|
|
||||||
list(APPEND PLATFORM_SRC
|
|
||||||
platform/posix/pcap.cpp
|
|
||||||
)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
if(LIBICSNEO_ENABLE_CDCACM)
|
|
||||||
list(APPEND PLATFORM_SRC
|
|
||||||
platform/posix/cdcacm.cpp
|
|
||||||
)
|
|
||||||
|
|
||||||
if(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
|
|
||||||
list(APPEND PLATFORM_SRC
|
|
||||||
platform/posix/darwin/cdcacmdarwin.cpp
|
|
||||||
)
|
|
||||||
else() # Linux or other
|
|
||||||
list(APPEND PLATFORM_SRC
|
|
||||||
platform/posix/linux/cdcacmlinux.cpp
|
|
||||||
)
|
|
||||||
if(NOT ${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
|
|
||||||
message(WARNING
|
|
||||||
"There is no CDCACM platform port defined for ${CMAKE_SYSTEM_NAME}!\n"
|
|
||||||
"The Linux platform code will be used, as it will generally allow building, but some devices may not enumerate properly."
|
|
||||||
)
|
|
||||||
endif()
|
|
||||||
endif()
|
|
||||||
endif()
|
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if(LIBICSNEO_ENABLE_DXX)
|
|
||||||
list(APPEND PLATFORM_SRC
|
|
||||||
platform/dxx.cpp
|
|
||||||
)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
if(LIBICSNEO_ENABLE_TCP)
|
|
||||||
list(APPEND PLATFORM_SRC
|
|
||||||
platform/tcp.cpp
|
|
||||||
)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
# Extensions
|
|
||||||
set(LIBICSNEO_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
|
|
||||||
foreach(EXT_PATH ${LIBICSNEO_EXTENSION_DIRS})
|
|
||||||
get_filename_component(EXT_DIR ${EXT_PATH} NAME)
|
|
||||||
message("Adding extension " ${EXT_DIR})
|
|
||||||
add_subdirectory(${EXT_PATH} ${CMAKE_CURRENT_BINARY_DIR}/${EXT_DIR})
|
|
||||||
endforeach()
|
|
||||||
|
|
||||||
set(SRC_FILES
|
set(SRC_FILES
|
||||||
communication/message/flexray/control/flexraycontrolmessage.cpp
|
communication/message/flexray/control/flexraycontrolmessage.cpp
|
||||||
communication/message/callback/streamoutput/a2bwavoutput.cpp
|
communication/message/callback/streamoutput/a2bwavoutput.cpp
|
||||||
@@ -306,6 +232,28 @@ set(SRC_FILES
|
|||||||
${PLATFORM_SRC}
|
${PLATFORM_SRC}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if(LIBICSNEO_ENABLE_BOOTLOADER)
|
||||||
|
list(APPEND SRC_FILES
|
||||||
|
device/extensions/bootloader/api.cpp
|
||||||
|
device/extensions/bootloader/version.cpp
|
||||||
|
device/extensions/bootloader/platform/logging.cpp
|
||||||
|
device/extensions/bootloader/parser/ief.cpp
|
||||||
|
device/extensions/bootloader/parser/fwzip.cpp
|
||||||
|
device/extensions/bootloader/parser/fwbin.cpp
|
||||||
|
device/extensions/bootloader/parser/fwbundle.cpp
|
||||||
|
device/extensions/bootloader/parser/fwloader.cpp
|
||||||
|
device/extensions/bootloader/communication/red.cpp
|
||||||
|
device/extensions/bootloader/communication/radmultichipcommunication.cpp
|
||||||
|
device/extensions/bootloader/communication/communication.cpp
|
||||||
|
device/extensions/bootloader/communication/radperiphcommunication.cpp
|
||||||
|
device/extensions/bootloader/communication/radcommunication.cpp
|
||||||
|
device/extensions/bootloader/communication/redpacketizer.cpp
|
||||||
|
device/extensions/bootloader/communication/redcore.cpp
|
||||||
|
device/extensions/bootloader/communication/application.cpp
|
||||||
|
device/extensions/bootloader/communication/redbase.cpp
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
|
||||||
# Generate build info header
|
# Generate build info header
|
||||||
execute_process(
|
execute_process(
|
||||||
COMMAND git rev-parse --abbrev-ref HEAD
|
COMMAND git rev-parse --abbrev-ref HEAD
|
||||||
@@ -335,17 +283,6 @@ configure_file(api/icsneocpp/buildinfo.h.template ${CMAKE_CURRENT_BINARY_DIR}/ge
|
|||||||
configure_file(api/icsneoc/version.rc.template ${CMAKE_CURRENT_BINARY_DIR}/generated/icsneoc/version.rc)
|
configure_file(api/icsneoc/version.rc.template ${CMAKE_CURRENT_BINARY_DIR}/generated/icsneoc/version.rc)
|
||||||
configure_file(api/icsneoc2/version.rc.template ${CMAKE_CURRENT_BINARY_DIR}/generated/icsneoc2/version.rc)
|
configure_file(api/icsneoc2/version.rc.template ${CMAKE_CURRENT_BINARY_DIR}/generated/icsneoc2/version.rc)
|
||||||
|
|
||||||
foreach(EXTINC ${LIBICSNEO_EXTENSION_INCLUDES})
|
|
||||||
message("Including " ${EXTINC})
|
|
||||||
list(APPEND LIBICSNEO_EXT_CODE_INCS_LIST "#include \"${EXTINC}\"")
|
|
||||||
endforeach()
|
|
||||||
list(JOIN LIBICSNEO_EXT_CODE_INCS_LIST "\n" LIBICSNEO_EXT_CODE_INCS)
|
|
||||||
foreach(EXTCLASS ${LIBICSNEO_EXTENSION_CLASSES})
|
|
||||||
list(APPEND LIBICSNEO_EXT_CODE_LIST "device->addExtension(std::make_shared<${EXTCLASS}>(*device))\;")
|
|
||||||
endforeach()
|
|
||||||
list(JOIN LIBICSNEO_EXT_CODE_LIST "\n\t" LIBICSNEO_EXT_CODE)
|
|
||||||
configure_file(include/icsneo/device/extensions/builtin.h.template ${CMAKE_CURRENT_BINARY_DIR}/generated/extensions/builtin.h)
|
|
||||||
|
|
||||||
include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR})
|
include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR})
|
||||||
|
|
||||||
add_library(icsneocpp
|
add_library(icsneocpp
|
||||||
@@ -356,78 +293,24 @@ add_library(icsneocpp
|
|||||||
api/icsneocpp/version.cpp
|
api/icsneocpp/version.cpp
|
||||||
${SRC_FILES}
|
${SRC_FILES}
|
||||||
)
|
)
|
||||||
message("Include paths " ${LIBICSNEO_EXTENSION_INCLUDE_PATHS})
|
|
||||||
target_include_directories(icsneocpp
|
target_include_directories(icsneocpp
|
||||||
PUBLIC
|
PUBLIC
|
||||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
||||||
$<INSTALL_INTERFACE:>
|
$<INSTALL_INTERFACE:>
|
||||||
PRIVATE
|
PRIVATE
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/include
|
${CMAKE_CURRENT_SOURCE_DIR}/include
|
||||||
${LIBICSNEO_EXTENSION_INCLUDE_PATHS}
|
|
||||||
)
|
)
|
||||||
target_link_libraries(icsneocpp PUBLIC Threads::Threads $<$<BOOL:${WIN32}>:ws2_32 iphlpapi>)
|
target_link_libraries(icsneocpp PUBLIC Threads::Threads $<$<BOOL:${WIN32}>:ws2_32 iphlpapi>)
|
||||||
set_property(TARGET icsneocpp PROPERTY POSITION_INDEPENDENT_CODE ON)
|
|
||||||
target_compile_features(icsneocpp PUBLIC cxx_auto_type cxx_constexpr cxx_lambdas cxx_nullptr cxx_range_for cxx_rvalue_references cxx_sizeof_member cxx_strong_enums)
|
target_compile_features(icsneocpp PUBLIC cxx_auto_type cxx_constexpr cxx_lambdas cxx_nullptr cxx_range_for cxx_rvalue_references cxx_sizeof_member cxx_strong_enums)
|
||||||
target_compile_options(icsneocpp PRIVATE ${LIBICSNEO_COMPILER_WARNINGS})
|
target_compile_options(icsneocpp PRIVATE ${LIBICSNEO_COMPILER_WARNINGS})
|
||||||
message("Loaded extensions: " ${LIBICSNEO_EXTENSION_TARGETS})
|
|
||||||
target_link_libraries(icsneocpp PUBLIC ${LIBICSNEO_EXTENSION_TARGETS})
|
|
||||||
if(LIBICSNEO_ENABLE_FIRMIO)
|
if(LIBICSNEO_ENABLE_FIRMIO)
|
||||||
target_compile_definitions(icsneocpp PRIVATE ICSNEO_ENABLE_FIRMIO)
|
target_compile_definitions(icsneocpp PRIVATE ICSNEO_ENABLE_FIRMIO)
|
||||||
endif()
|
endif()
|
||||||
if(LIBICSNEO_ENABLE_RAW_ETHERNET)
|
|
||||||
target_compile_definitions(icsneocpp PRIVATE ICSNEO_ENABLE_RAW_ETHERNET)
|
|
||||||
endif()
|
|
||||||
if(LIBICSNEO_ENABLE_CDCACM)
|
|
||||||
target_compile_definitions(icsneocpp PRIVATE ICSNEO_ENABLE_CDCACM)
|
|
||||||
if(APPLE)
|
|
||||||
find_library(CORE_FOUNDATION_FRAMEWORK CoreFoundation REQUIRED)
|
|
||||||
find_library(IO_KIT_FRAMEWORK IOKit REQUIRED)
|
|
||||||
target_link_libraries(icsneocpp PRIVATE ${CORE_FOUNDATION_FRAMEWORK} ${IO_KIT_FRAMEWORK})
|
|
||||||
endif()
|
|
||||||
endif()
|
|
||||||
if(LIBICSNEO_ENABLE_DXX)
|
|
||||||
target_compile_definitions(icsneocpp PRIVATE ICSNEO_ENABLE_DXX)
|
|
||||||
target_link_libraries(icsneocpp PRIVATE libredxx::libredxx)
|
|
||||||
endif()
|
|
||||||
if(LIBICSNEO_ENABLE_TCP)
|
|
||||||
target_compile_definitions(icsneocpp PRIVATE ICSNEO_ENABLE_TCP)
|
|
||||||
if(WIN32)
|
|
||||||
target_link_libraries(icsneocpp PRIVATE ws2_32 iphlpapi)
|
|
||||||
endif()
|
|
||||||
endif()
|
|
||||||
|
|
||||||
# fatfs
|
# fatfs
|
||||||
add_subdirectory(third-party/fatfs)
|
add_subdirectory(third-party/fatfs)
|
||||||
set_property(TARGET fatfs PROPERTY POSITION_INDEPENDENT_CODE ON)
|
|
||||||
target_link_libraries(icsneocpp PRIVATE fatfs)
|
target_link_libraries(icsneocpp PRIVATE fatfs)
|
||||||
|
|
||||||
# dxx
|
|
||||||
if(LIBICSNEO_ENABLE_DXX)
|
|
||||||
include(FetchContent)
|
|
||||||
FetchContent_Declare(libredxx
|
|
||||||
GIT_REPOSITORY https://github.com/Zeranoe/libredxx.git
|
|
||||||
GIT_TAG e823a96c39a64ab41b7d1632dbe8f86bb854df83
|
|
||||||
)
|
|
||||||
set(LIBREDXX_DISABLE_INSTALL ON)
|
|
||||||
FetchContent_MakeAvailable(libredxx)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
# pcap
|
|
||||||
if(LIBICSNEO_ENABLE_RAW_ETHERNET)
|
|
||||||
if(WIN32)
|
|
||||||
if(LIBICSNEO_NPCAP_INCLUDE_DIR STREQUAL "")
|
|
||||||
target_include_directories(icsneocpp PUBLIC AFTER third-party/winpcap/include)
|
|
||||||
add_definitions(-DWPCAP -DHAVE_REMOTE)
|
|
||||||
else()
|
|
||||||
target_include_directories(icsneocpp PUBLIC AFTER ${LIBICSNEO_NPCAP_INCLUDE_DIR})
|
|
||||||
endif()
|
|
||||||
else()
|
|
||||||
find_package(PCAP REQUIRED)
|
|
||||||
target_include_directories(icsneocpp PUBLIC ${PCAP_INCLUDE_DIR})
|
|
||||||
target_link_libraries(icsneocpp PUBLIC ${PCAP_LIBRARY})
|
|
||||||
endif(WIN32)
|
|
||||||
endif(LIBICSNEO_ENABLE_RAW_ETHERNET)
|
|
||||||
|
|
||||||
# protobuf
|
# protobuf
|
||||||
if(DEFINED ENV{LIBICSNEO_ICSPB_REPO})
|
if(DEFINED ENV{LIBICSNEO_ICSPB_REPO})
|
||||||
set(LIBICSNEO_ICSPB_REPO "$ENV{LIBICSNEO_ICSPB_REPO}")
|
set(LIBICSNEO_ICSPB_REPO "$ENV{LIBICSNEO_ICSPB_REPO}")
|
||||||
@@ -443,6 +326,59 @@ FetchContent_Declare(icspb
|
|||||||
FetchContent_MakeAvailable(icspb)
|
FetchContent_MakeAvailable(icspb)
|
||||||
target_link_libraries(icsneocpp PRIVATE icspb::icspb)
|
target_link_libraries(icsneocpp PRIVATE icspb::icspb)
|
||||||
|
|
||||||
|
# libicsbl
|
||||||
|
if(LIBICSNEO_ENABLE_BOOTLOADER)
|
||||||
|
if(NOT LIBICSNEO_LIBICSBL_ZIP_URL)
|
||||||
|
if(DEFINED ENV{LIBICSNEO_LIBICSBL_ZIP_URL})
|
||||||
|
set(LIBICSNEO_LIBICSBL_ZIP_URL "$ENV{LIBICSNEO_LIBICSBL_ZIP_URL}")
|
||||||
|
else()
|
||||||
|
if(NOT LIBICSNEO_LIBICSBL_ZIP_URL_PREFIX)
|
||||||
|
if(DEFINED ENV{LIBICSNEO_LIBICSBL_ZIP_URL_PREFIX})
|
||||||
|
set(LIBICSNEO_LIBICSBL_ZIP_URL_PREFIX "$ENV{LIBICSNEO_LIBICSBL_ZIP_URL_PREFIX}")
|
||||||
|
else()
|
||||||
|
set(LIBICSNEO_LIBICSBL_ZIP_URL_PREFIX "https://cdn.intrepidcs.net/libicsbl/5ce699ada0ccaadb92cabda6e3537c09b333020e")
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
if(NOT LIBICSNEO_LIBICSBL_VERSION)
|
||||||
|
if(DEFINED ENV{LIBICSNEO_LIBICSBL_VERSION})
|
||||||
|
set(LIBICSNEO_LIBICSBL_VERSION "$ENV{LIBICSNEO_LIBICSBL_VERSION}")
|
||||||
|
else()
|
||||||
|
set(LIBICSNEO_LIBICSBL_VERSION "26.4.0")
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
if(WIN32)
|
||||||
|
if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8 OR CMAKE_CXX_COMPILER_ARCHITECTURE_ID MATCHES "ARM")
|
||||||
|
message(FATAL_ERROR "Unsupported bootloader architecture: ${CMAKE_CXX_COMPILER_ARCHITECTURE_ID}")
|
||||||
|
endif()
|
||||||
|
set(LIBICSNEO_LIBICSBL_ZIP_URL "${LIBICSNEO_LIBICSBL_ZIP_URL_PREFIX}/libicsbl-${LIBICSNEO_LIBICSBL_VERSION}-windows-amd64.zip")
|
||||||
|
elseif(APPLE)
|
||||||
|
set(LIBICSNEO_LIBICSBL_ZIP_URL "${LIBICSNEO_LIBICSBL_ZIP_URL_PREFIX}/libicsbl-${LIBICSNEO_LIBICSBL_VERSION}-macos-universal.zip")
|
||||||
|
else()
|
||||||
|
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
|
||||||
|
set(LIBICSNEO_LIBICSBL_ZIP_URL "${LIBICSNEO_LIBICSBL_ZIP_URL_PREFIX}/libicsbl-${LIBICSNEO_LIBICSBL_VERSION}-linux-x86_64-glibc.zip")
|
||||||
|
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
|
||||||
|
set(LIBICSNEO_LIBICSBL_ZIP_URL "${LIBICSNEO_LIBICSBL_ZIP_URL_PREFIX}/libicsbl-${LIBICSNEO_LIBICSBL_VERSION}-linux-aarch64-glibc.zip")
|
||||||
|
else()
|
||||||
|
message(FATAL_ERROR "Unsupported CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
FetchContent_Declare(
|
||||||
|
libicsbl
|
||||||
|
URL "${LIBICSNEO_LIBICSBL_ZIP_URL}"
|
||||||
|
)
|
||||||
|
FetchContent_MakeAvailable(libicsbl)
|
||||||
|
set(libicsbl_ROOT "${libicsbl_SOURCE_DIR}")
|
||||||
|
find_package(libicsbl CONFIG REQUIRED)
|
||||||
|
target_link_libraries(icsneocpp PUBLIC libicsbl::icsbl)
|
||||||
|
|
||||||
|
# miniz
|
||||||
|
add_subdirectory(third-party/miniz)
|
||||||
|
target_link_libraries(icsneocpp PRIVATE miniz)
|
||||||
|
endif()
|
||||||
|
|
||||||
if(LIBICSNEO_BUILD_ICSNEOC)
|
if(LIBICSNEO_BUILD_ICSNEOC)
|
||||||
add_library(icsneoc SHARED api/icsneoc/icsneoc.cpp ${CMAKE_CURRENT_BINARY_DIR}/generated/icsneoc/version.rc)
|
add_library(icsneoc SHARED api/icsneoc/icsneoc.cpp ${CMAKE_CURRENT_BINARY_DIR}/generated/icsneoc/version.rc)
|
||||||
target_include_directories(icsneoc
|
target_include_directories(icsneoc
|
||||||
@@ -576,7 +512,6 @@ if(LIBICSNEO_BUILD_UNIT_TESTS)
|
|||||||
test/unit/icsneoc2.cpp
|
test/unit/icsneoc2.cpp
|
||||||
test/unit/ethphyregistertest.cpp
|
test/unit/ethphyregistertest.cpp
|
||||||
test/unit/tc10test.cpp
|
test/unit/tc10test.cpp
|
||||||
test/unit/windowsstrings.cpp
|
|
||||||
test/unit/periodictest.cpp
|
test/unit/periodictest.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -118,6 +118,8 @@ static constexpr const char* SETTING_NOT_AVAILABLE = "Requested a setting that i
|
|||||||
static constexpr const char* DISK_FORMAT_NOT_SUPPORTED = "Disk formatting is not supported on this device.";
|
static constexpr const char* DISK_FORMAT_NOT_SUPPORTED = "Disk formatting is not supported on this device.";
|
||||||
static constexpr const char* DISK_FORMAT_INVALID_COUNT = "Disk format config disk count is mismatched with device disk count.";
|
static constexpr const char* DISK_FORMAT_INVALID_COUNT = "Disk format config disk count is mismatched with device disk count.";
|
||||||
static constexpr const char* TC10_REQUEST_FAILED = "The device rejected the TC10 request.";
|
static constexpr const char* TC10_REQUEST_FAILED = "The device rejected the TC10 request.";
|
||||||
|
static constexpr const char* FIRMWARE_UPDATE_NOT_SUPPORTED = "Firmware updates are not supported for this device.";
|
||||||
|
static constexpr const char* FIRMWARE_UPDATE_FAILED = "The firmware update failed.";
|
||||||
|
|
||||||
// Transport Errors
|
// Transport Errors
|
||||||
static constexpr const char* FAILED_TO_READ = "A read operation failed.";
|
static constexpr const char* FAILED_TO_READ = "A read operation failed.";
|
||||||
@@ -169,6 +171,7 @@ static constexpr const char* SERVD_POLL_ERROR = "Error polling on Servd socket";
|
|||||||
static constexpr const char* SERVD_NODATA_ERROR = "No data received from Servd";
|
static constexpr const char* SERVD_NODATA_ERROR = "No data received from Servd";
|
||||||
static constexpr const char* SERVD_JOIN_MULTICAST_ERROR = "Error joining Servd multicast group";
|
static constexpr const char* SERVD_JOIN_MULTICAST_ERROR = "Error joining Servd multicast group";
|
||||||
static constexpr const char* SERVD_NOT_REACHABLE = "Could not reach Servd; ensure it is installed and running";
|
static constexpr const char* SERVD_NOT_REACHABLE = "Could not reach Servd; ensure it is installed and running";
|
||||||
|
static constexpr const char* SERVD_DISABLED = "Servd is disabled via LIBICSNEO_USE_SERVD; no devices will be found";
|
||||||
|
|
||||||
// DXX
|
// DXX
|
||||||
static constexpr const char* DXX_ERROR_SYS = "System error, check errno/GetLastError()";
|
static constexpr const char* DXX_ERROR_SYS = "System error, check errno/GetLastError()";
|
||||||
@@ -384,6 +387,10 @@ const char* APIEvent::DescriptionForType(Type type) {
|
|||||||
return DISK_FORMAT_INVALID_COUNT;
|
return DISK_FORMAT_INVALID_COUNT;
|
||||||
case Type::TC10RequestFailed:
|
case Type::TC10RequestFailed:
|
||||||
return TC10_REQUEST_FAILED;
|
return TC10_REQUEST_FAILED;
|
||||||
|
case Type::FirmwareUpdateNotSupported:
|
||||||
|
return FIRMWARE_UPDATE_NOT_SUPPORTED;
|
||||||
|
case Type::FirmwareUpdateFailed:
|
||||||
|
return FIRMWARE_UPDATE_FAILED;
|
||||||
|
|
||||||
// VSA
|
// VSA
|
||||||
case Type::VSABufferCorrupted:
|
case Type::VSABufferCorrupted:
|
||||||
@@ -445,6 +452,8 @@ const char* APIEvent::DescriptionForType(Type type) {
|
|||||||
return SERVD_JOIN_MULTICAST_ERROR;
|
return SERVD_JOIN_MULTICAST_ERROR;
|
||||||
case Type::ServdNotReachable:
|
case Type::ServdNotReachable:
|
||||||
return SERVD_NOT_REACHABLE;
|
return SERVD_NOT_REACHABLE;
|
||||||
|
case Type::ServdDisabled:
|
||||||
|
return SERVD_DISABLED;
|
||||||
|
|
||||||
// DXX
|
// DXX
|
||||||
case Type::DXXErrorSys:
|
case Type::DXXErrorSys:
|
||||||
|
|||||||
@@ -57,6 +57,18 @@ pybind11_add_module(icsneopy
|
|||||||
)
|
)
|
||||||
target_link_libraries(icsneopy PRIVATE icsneocpp)
|
target_link_libraries(icsneopy PRIVATE icsneocpp)
|
||||||
|
|
||||||
|
if(LIBICSNEO_ENABLE_BOOTLOADER)
|
||||||
|
target_sources(icsneopy PRIVATE icsneopy/bootloader/bootloader.cpp)
|
||||||
|
target_compile_definitions(icsneopy PRIVATE ICSNEO_ENABLE_BOOTLOADER)
|
||||||
|
if(WIN32)
|
||||||
|
# Windows has no build RPATH; the .pyd only finds DLLs in its own directory
|
||||||
|
add_custom_command(TARGET icsneopy POST_BUILD
|
||||||
|
COMMAND "${CMAKE_COMMAND}" -E copy_if_different "$<TARGET_FILE:libicsbl::icsbl>" "$<TARGET_FILE_DIR:icsneopy>"
|
||||||
|
VERBATIM
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
install(TARGETS icsneopy LIBRARY DESTINATION icsneopy)
|
install(TARGETS icsneopy LIBRARY DESTINATION icsneopy)
|
||||||
|
|
||||||
find_program(STUBGEN_EXE stubgen)
|
find_program(STUBGEN_EXE stubgen)
|
||||||
|
|||||||
@@ -93,6 +93,8 @@ void init_event(pybind11::module_& m) {
|
|||||||
.value("DiskFormatNotSupported", APIEvent::Type::DiskFormatNotSupported)
|
.value("DiskFormatNotSupported", APIEvent::Type::DiskFormatNotSupported)
|
||||||
.value("DiskFormatInvalidCount", APIEvent::Type::DiskFormatInvalidCount)
|
.value("DiskFormatInvalidCount", APIEvent::Type::DiskFormatInvalidCount)
|
||||||
.value("TC10RequestFailed", APIEvent::Type::TC10RequestFailed)
|
.value("TC10RequestFailed", APIEvent::Type::TC10RequestFailed)
|
||||||
|
.value("FirmwareUpdateNotSupported", APIEvent::Type::FirmwareUpdateNotSupported)
|
||||||
|
.value("FirmwareUpdateFailed", APIEvent::Type::FirmwareUpdateFailed)
|
||||||
.value("FailedToRead", APIEvent::Type::FailedToRead)
|
.value("FailedToRead", APIEvent::Type::FailedToRead)
|
||||||
.value("FailedToWrite", APIEvent::Type::FailedToWrite)
|
.value("FailedToWrite", APIEvent::Type::FailedToWrite)
|
||||||
.value("DriverFailedToOpen", APIEvent::Type::DriverFailedToOpen)
|
.value("DriverFailedToOpen", APIEvent::Type::DriverFailedToOpen)
|
||||||
@@ -128,6 +130,7 @@ void init_event(pybind11::module_& m) {
|
|||||||
.value("ServdNoDataError", APIEvent::Type::ServdNoDataError)
|
.value("ServdNoDataError", APIEvent::Type::ServdNoDataError)
|
||||||
.value("ServdJoinMulticastError", APIEvent::Type::ServdJoinMulticastError)
|
.value("ServdJoinMulticastError", APIEvent::Type::ServdJoinMulticastError)
|
||||||
.value("ServdNotReachable", APIEvent::Type::ServdNotReachable)
|
.value("ServdNotReachable", APIEvent::Type::ServdNotReachable)
|
||||||
|
.value("ServdDisabled", APIEvent::Type::ServdDisabled)
|
||||||
.value("DXXErrorSys", APIEvent::Type::DXXErrorSys)
|
.value("DXXErrorSys", APIEvent::Type::DXXErrorSys)
|
||||||
.value("DXXErrorInt", APIEvent::Type::DXXErrorInt)
|
.value("DXXErrorInt", APIEvent::Type::DXXErrorInt)
|
||||||
.value("DXXErrorOverflow", APIEvent::Type::DXXErrorOverflow)
|
.value("DXXErrorOverflow", APIEvent::Type::DXXErrorOverflow)
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
#include <pybind11/pybind11.h>
|
||||||
|
#include <pybind11/stl.h>
|
||||||
|
#include <pybind11/functional.h>
|
||||||
|
|
||||||
|
#include "icsneo/device/extensions/bootloader/api.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/parser/fwloader.h"
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
struct BootloaderNamespace {};
|
||||||
|
|
||||||
|
void init_bootloader(pybind11::module_& m) {
|
||||||
|
// Dummy class to act as Bootloader namespace
|
||||||
|
pybind11::classh<BootloaderNamespace> bootloader(m, "Bootloader");
|
||||||
|
|
||||||
|
// Opaque handle returned by FWLoader.load and consumed by update
|
||||||
|
pybind11::classh<Bootloader::FWRepo>(bootloader, "FWRepo");
|
||||||
|
|
||||||
|
pybind11::classh<Bootloader::FWLoader>(bootloader, "FWLoader")
|
||||||
|
.def_static("load", &Bootloader::FWLoader::Load, pybind11::arg("path"), pybind11::call_guard<pybind11::gil_scoped_release>());
|
||||||
|
|
||||||
|
bootloader.def_static("update",
|
||||||
|
[](Device& device, const Bootloader::FWRepo& fw, Bootloader::Logger logger, bool alreadyInBootloader, bool forceFlash) {
|
||||||
|
if(!logger) {
|
||||||
|
logger = [](Device::OpenStatusType, const std::string&, std::optional<double>) { return Device::OpenDirective::Continue; };
|
||||||
|
}
|
||||||
|
return Bootloader::update(device, fw, logger, alreadyInBootloader, forceFlash);
|
||||||
|
},
|
||||||
|
pybind11::arg("device"),
|
||||||
|
pybind11::arg("fw"),
|
||||||
|
pybind11::arg("logger") = pybind11::none(),
|
||||||
|
pybind11::arg("already_in_bootloader") = false,
|
||||||
|
pybind11::arg("force_flash") = false,
|
||||||
|
pybind11::call_guard<pybind11::gil_scoped_release>());
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
@@ -135,6 +135,7 @@ void init_chipid(pybind11::module_& m) {
|
|||||||
.value("RADGigastar2_ZYNQ", ChipID::RADGigastar2_ZYNQ)
|
.value("RADGigastar2_ZYNQ", ChipID::RADGigastar2_ZYNQ)
|
||||||
.value("SFPModule_ent11100_MCHIP", ChipID::SFPModule_ent11100_MCHIP)
|
.value("SFPModule_ent11100_MCHIP", ChipID::SFPModule_ent11100_MCHIP)
|
||||||
.value("RADArgos_MCHIP", ChipID::RADArgos_MCHIP)
|
.value("RADArgos_MCHIP", ChipID::RADArgos_MCHIP)
|
||||||
|
.value("SFPModule_ad1101_MCHIP", ChipID::SFPModule_ad1101_MCHIP)
|
||||||
.value("Invalid", ChipID::Invalid)
|
.value("Invalid", ChipID::Invalid)
|
||||||
.finalize();
|
.finalize();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#include <pybind11/stl.h>
|
#include <pybind11/stl.h>
|
||||||
#include <pybind11/functional.h>
|
#include <pybind11/functional.h>
|
||||||
#include <pybind11/chrono.h>
|
#include <pybind11/chrono.h>
|
||||||
|
#include <pybind11/native_enum.h>
|
||||||
|
|
||||||
#include "icsneo/device/device.h"
|
#include "icsneo/device/device.h"
|
||||||
#include "icsneo/device/extensions/deviceextension.h"
|
#include "icsneo/device/extensions/deviceextension.h"
|
||||||
@@ -12,7 +13,22 @@
|
|||||||
namespace icsneo {
|
namespace icsneo {
|
||||||
|
|
||||||
void init_device(pybind11::module_& m) {
|
void init_device(pybind11::module_& m) {
|
||||||
pybind11::classh<Device>(m, "Device")
|
pybind11::classh<Device> device(m, "Device");
|
||||||
|
|
||||||
|
pybind11::native_enum<Device::OpenDirective>(device, "OpenDirective", "enum.IntEnum")
|
||||||
|
.value("Continue", Device::OpenDirective::Continue)
|
||||||
|
.value("Cancel", Device::OpenDirective::Cancel)
|
||||||
|
.value("Skip", Device::OpenDirective::Skip)
|
||||||
|
.finalize();
|
||||||
|
|
||||||
|
pybind11::native_enum<Device::OpenStatusType>(device, "OpenStatusType", "enum.IntEnum")
|
||||||
|
.value("QuestionContinueSkipCancel", Device::OpenStatusType::QuestionContinueSkipCancel)
|
||||||
|
.value("QuestionContinueCancel", Device::OpenStatusType::QuestionContinueCancel)
|
||||||
|
.value("Progress", Device::OpenStatusType::Progress)
|
||||||
|
.value("Failed", Device::OpenStatusType::Failed)
|
||||||
|
.finalize();
|
||||||
|
|
||||||
|
device
|
||||||
.def("__repr__", &Device::describe)
|
.def("__repr__", &Device::describe)
|
||||||
.def("add_message_callback", &Device::addMessageCallback, pybind11::call_guard<pybind11::gil_scoped_release>())
|
.def("add_message_callback", &Device::addMessageCallback, pybind11::call_guard<pybind11::gil_scoped_release>())
|
||||||
.def("clear_script", &Device::clearScript, pybind11::call_guard<pybind11::gil_scoped_release>())
|
.def("clear_script", &Device::clearScript, pybind11::call_guard<pybind11::gil_scoped_release>())
|
||||||
|
|||||||
@@ -44,6 +44,9 @@ void init_idevicesettings(pybind11::module_&);
|
|||||||
void init_ethphymessage(pybind11::module_&);
|
void init_ethphymessage(pybind11::module_&);
|
||||||
void init_livedata(pybind11::module_&);
|
void init_livedata(pybind11::module_&);
|
||||||
void init_livedatamessage(pybind11::module_&);
|
void init_livedatamessage(pybind11::module_&);
|
||||||
|
#ifdef ICSNEO_ENABLE_BOOTLOADER
|
||||||
|
void init_bootloader(pybind11::module_&);
|
||||||
|
#endif
|
||||||
|
|
||||||
PYBIND11_MODULE(icsneopy, m) {
|
PYBIND11_MODULE(icsneopy, m) {
|
||||||
pybind11::options options;
|
pybind11::options options;
|
||||||
@@ -88,6 +91,9 @@ PYBIND11_MODULE(icsneopy, m) {
|
|||||||
init_device(m);
|
init_device(m);
|
||||||
init_deviceextension(m);
|
init_deviceextension(m);
|
||||||
init_idevicesettings(m);
|
init_idevicesettings(m);
|
||||||
|
#ifdef ICSNEO_ENABLE_BOOTLOADER
|
||||||
|
init_bootloader(m);
|
||||||
|
#endif
|
||||||
|
|
||||||
m.def("find_all_devices", &FindAllDevices);
|
m.def("find_all_devices", &FindAllDevices);
|
||||||
m.def("get_supported_devices", &GetSupportedDevices);
|
m.def("get_supported_devices", &GetSupportedDevices);
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
VERSION="1.10.5"
|
|
||||||
ROOT="$PWD/libpcap"
|
|
||||||
SOURCE="$ROOT/source"
|
|
||||||
BUILD="$ROOT/build"
|
|
||||||
INSTALL="$ROOT/install"
|
|
||||||
|
|
||||||
mkdir -p "$ROOT"
|
|
||||||
cd "$ROOT" || exit 1
|
|
||||||
|
|
||||||
curl -LO "https://www.tcpdump.org/release/libpcap-$VERSION.tar.xz" || exit 1
|
|
||||||
tar -xf "libpcap-$VERSION.tar.xz" || exit 1
|
|
||||||
mv "libpcap-$VERSION" "$SOURCE" || exit 1
|
|
||||||
|
|
||||||
cmake -D CMAKE_POSITION_INDEPENDENT_CODE=ON -D CMAKE_INSTALL_PREFIX="$INSTALL" -D BUILD_SHARED_LIBS=OFF -D BUILD_WITH_LIBNL=OFF -D DISABLE_DBUS=ON -D DISABLE_LINUX_USBMON=ON -D DISABLE_BLUETOOTH=ON -D DISABLE_NETMAP=ON -D DISABLE_DPDK=ON -D DISABLE_RDMA=ON -D DISABLE_DAG=ON -D DISABLE_SEPTEL=ON -D DISABLE_SNF=ON -D DISABLE_TC=ON -B "$BUILD" -S "$SOURCE" || exit 1
|
|
||||||
cmake --build "$BUILD" || exit 1
|
|
||||||
cmake --install "$BUILD" || exit 1
|
|
||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
cmake -GNinja -Bbuild -DCMAKE_BUILD_TYPE=Release -DLIBICSNEO_BUILD_EXAMPLES=ON -DLIBICSNEO_BUILD_UNIT_TESTS=ON -DLIBICSNEO_ENABLE_TCP=OFF || exit 1
|
cmake -GNinja -Bbuild -DCMAKE_BUILD_TYPE=Release -DLIBICSNEO_BUILD_EXAMPLES=ON -DLIBICSNEO_BUILD_UNIT_TESTS=ON -DLIBICSNEO_ENABLE_BOOTLOADER=ON || exit 1
|
||||||
|
|
||||||
cmake --build build || exit 1
|
cmake --build build || exit 1
|
||||||
|
|
||||||
|
|||||||
Executable
+8
@@ -0,0 +1,8 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
export CIBW_BEFORE_ALL="sh ci/bootstrap-cibuildwheel.sh"
|
||||||
|
export CIBW_BUILD="*manylinux*" # no musl
|
||||||
|
export CIBW_ENVIRONMENT="LD_LIBRARY_PATH=/project/fc/libicsbl-src/lib64"
|
||||||
|
export SKBUILD_CMAKE_DEFINE="LIBICSNEO_ENABLE_BOOTLOADER=ON;ICSPB_BOOTSTRAP_DIR=/project/icspb;FETCHCONTENT_BASE_DIR=/project/fc"
|
||||||
|
|
||||||
|
sh ci/build-wheel-posix.sh
|
||||||
Executable
+8
@@ -0,0 +1,8 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
export CIBW_ARCHS="arm64"
|
||||||
|
export SKBUILD_CMAKE_DEFINE="LIBICSNEO_ENABLE_BOOTLOADER=ON;ICSPB_BOOTSTRAP_DIR=$PWD/icspb;FETCHCONTENT_BASE_DIR=$PWD/fc"
|
||||||
|
export REPAIR_LIBRARY_PATH="$PWD/fc/libicsbl-src/lib"
|
||||||
|
export MACOSX_DEPLOYMENT_TARGET="10.14"
|
||||||
|
|
||||||
|
sh ci/build-wheel-posix.sh
|
||||||
@@ -3,6 +3,9 @@
|
|||||||
|
|
||||||
call "%VCVARS64_2022%"
|
call "%VCVARS64_2022%"
|
||||||
|
|
||||||
|
set "SKBUILD_CMAKE_DEFINE=LIBICSNEO_ENABLE_BOOTLOADER=ON;ICSPB_BOOTSTRAP_DIR=%CD%\icspb;FETCHCONTENT_BASE_DIR=%CD%\fc"
|
||||||
|
set "PATH=%CD%\fc\libicsbl-src\bin;%PATH%"
|
||||||
|
|
||||||
python.exe -m venv env || exit /b 1
|
python.exe -m venv env || exit /b 1
|
||||||
call env\Scripts\Activate.bat || exit /b 1
|
call env\Scripts\Activate.bat || exit /b 1
|
||||||
python.exe -m pip install cibuildwheel || exit /b 1
|
python.exe -m pip install cibuildwheel || exit /b 1
|
||||||
|
|||||||
@@ -3,6 +3,6 @@
|
|||||||
|
|
||||||
mkdir build >nul 2>&1
|
mkdir build >nul 2>&1
|
||||||
|
|
||||||
cmake -GNinja -Bbuild -DCMAKE_BUILD_TYPE=Release -DCMAKE_COMPILE_WARNING_AS_ERROR=ON -DLIBICSNEO_BUILD_UNIT_TESTS=ON-DLIBICSNEO_ENABLE_TCP=ON || exit /b 1
|
cmake -GNinja -Bbuild -DCMAKE_BUILD_TYPE=Release -DCMAKE_COMPILE_WARNING_AS_ERROR=ON -DLIBICSNEO_BUILD_UNIT_TESTS=ON -DLIBICSNEO_ENABLE_BOOTLOADER=ON || exit /b 1
|
||||||
|
|
||||||
cmake --build build || exit /b 1
|
cmake --build build || exit /b 1
|
||||||
|
|||||||
@@ -1,74 +0,0 @@
|
|||||||
# - Try to find libpcap include dirs and libraries
|
|
||||||
#
|
|
||||||
# Usage of this module as follows:
|
|
||||||
#
|
|
||||||
# find_package(PCAP)
|
|
||||||
#
|
|
||||||
# Variables used by this module, they can change the default behaviour and need
|
|
||||||
# to be set before calling find_package:
|
|
||||||
#
|
|
||||||
# PCAP_ROOT_DIR Set this variable to the root installation of
|
|
||||||
# libpcap if the module has problems finding the
|
|
||||||
# proper installation path.
|
|
||||||
#
|
|
||||||
# Variables defined by this module:
|
|
||||||
#
|
|
||||||
# PCAP_FOUND System has libpcap, include and library dirs found
|
|
||||||
# PCAP_INCLUDE_DIR The libpcap include directories.
|
|
||||||
# PCAP_LIBRARY The libpcap library (possibly includes a thread
|
|
||||||
# library e.g. required by pf_ring's libpcap)
|
|
||||||
# HAVE_PF_RING If a found version of libpcap supports PF_RING
|
|
||||||
|
|
||||||
find_path(PCAP_ROOT_DIR
|
|
||||||
NAMES include/pcap.h
|
|
||||||
)
|
|
||||||
|
|
||||||
find_path(PCAP_INCLUDE_DIR
|
|
||||||
NAMES pcap.h
|
|
||||||
HINTS ${PCAP_ROOT_DIR}/include
|
|
||||||
)
|
|
||||||
|
|
||||||
find_library(PCAP_LIBRARY
|
|
||||||
NAMES pcap
|
|
||||||
HINTS ${PCAP_ROOT_DIR}/lib
|
|
||||||
)
|
|
||||||
|
|
||||||
include(FindPackageHandleStandardArgs)
|
|
||||||
find_package_handle_standard_args(PCAP DEFAULT_MSG
|
|
||||||
PCAP_LIBRARY
|
|
||||||
PCAP_INCLUDE_DIR
|
|
||||||
)
|
|
||||||
|
|
||||||
include(CheckCSourceCompiles)
|
|
||||||
set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LIBRARY})
|
|
||||||
check_c_source_compiles("int main() { return 0; }" PCAP_LINKS_SOLO)
|
|
||||||
set(CMAKE_REQUIRED_LIBRARIES)
|
|
||||||
|
|
||||||
# check if linking against libpcap also needs to link against a thread library
|
|
||||||
if (NOT PCAP_LINKS_SOLO)
|
|
||||||
find_package(Threads)
|
|
||||||
if (THREADS_FOUND)
|
|
||||||
set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LIBRARY} ${CMAKE_THREAD_LIBS_INIT})
|
|
||||||
check_c_source_compiles("int main() { return 0; }" PCAP_NEEDS_THREADS)
|
|
||||||
set(CMAKE_REQUIRED_LIBRARIES)
|
|
||||||
endif ()
|
|
||||||
if (THREADS_FOUND AND PCAP_NEEDS_THREADS)
|
|
||||||
set(_tmp ${PCAP_LIBRARY} ${CMAKE_THREAD_LIBS_INIT})
|
|
||||||
list(REMOVE_DUPLICATES _tmp)
|
|
||||||
set(PCAP_LIBRARY ${_tmp}
|
|
||||||
CACHE STRING "Libraries needed to link against libpcap" FORCE)
|
|
||||||
else ()
|
|
||||||
message(FATAL_ERROR "Couldn't determine how to link against libpcap")
|
|
||||||
endif ()
|
|
||||||
endif ()
|
|
||||||
|
|
||||||
include(CheckFunctionExists)
|
|
||||||
set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LIBRARY})
|
|
||||||
check_function_exists(pcap_get_pfring_id HAVE_PF_RING)
|
|
||||||
set(CMAKE_REQUIRED_LIBRARIES)
|
|
||||||
|
|
||||||
mark_as_advanced(
|
|
||||||
PCAP_ROOT_DIR
|
|
||||||
PCAP_INCLUDE_DIR
|
|
||||||
PCAP_LIBRARY
|
|
||||||
)
|
|
||||||
@@ -152,6 +152,7 @@ static std::vector<uint8_t> EncodeFromMessageLIN(std::shared_ptr<Frame> frame, c
|
|||||||
linmsg->linMsgType == LINMessage::Type::LIN_UPDATE_RESPONDER) && dataLen > 0;
|
linmsg->linMsgType == LINMessage::Type::LIN_UPDATE_RESPONDER) && dataLen > 0;
|
||||||
|
|
||||||
if(hasData) {
|
if(hasData) {
|
||||||
|
LINMessage::calcChecksum(*linmsg);
|
||||||
// len includes data bytes + 1 checksum byte
|
// len includes data bytes + 1 checksum byte
|
||||||
linpacket->CoreMiniBitsLIN.len = static_cast<uint16_t>(dataLen + 1);
|
linpacket->CoreMiniBitsLIN.len = static_cast<uint16_t>(dataLen + 1);
|
||||||
std::copy(linmsg->data.begin(), linmsg->data.begin() + dataLen, linpacket->data);
|
std::copy(linmsg->data.begin(), linmsg->data.begin() + dataLen, linpacket->data);
|
||||||
|
|||||||
+2
-37
@@ -1,29 +1,12 @@
|
|||||||
#include "icsneo/device/devicefinder.h"
|
#include "icsneo/device/devicefinder.h"
|
||||||
#include "icsneo/platform/devices.h"
|
#include "icsneo/platform/devices.h"
|
||||||
#include "icsneo/device/founddevice.h"
|
#include "icsneo/device/founddevice.h"
|
||||||
#include "generated/extensions/builtin.h"
|
|
||||||
#include "icsneo/platform/servd.h"
|
#include "icsneo/platform/servd.h"
|
||||||
|
|
||||||
#ifdef ICSNEO_ENABLE_FIRMIO
|
#ifdef ICSNEO_ENABLE_FIRMIO
|
||||||
#include "icsneo/platform/firmio.h"
|
#include "icsneo/platform/firmio.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef ICSNEO_ENABLE_RAW_ETHERNET
|
|
||||||
#include "icsneo/platform/pcap.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef ICSNEO_ENABLE_CDCACM
|
|
||||||
#include "icsneo/platform/cdcacm.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef ICSNEO_ENABLE_DXX
|
|
||||||
#include "icsneo/platform/dxx.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef ICSNEO_ENABLE_TCP
|
|
||||||
#include "icsneo/platform/tcp.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
using namespace icsneo;
|
using namespace icsneo;
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -56,22 +39,8 @@ std::vector<std::shared_ptr<Device>> DeviceFinder::FindAll() {
|
|||||||
} else {
|
} else {
|
||||||
#ifdef ICSNEO_ENABLE_FIRMIO
|
#ifdef ICSNEO_ENABLE_FIRMIO
|
||||||
FirmIO::Find(newDriverFoundDevices);
|
FirmIO::Find(newDriverFoundDevices);
|
||||||
#endif
|
#else
|
||||||
|
EventManager::GetInstance().add(APIEvent::Type::ServdDisabled, APIEvent::Severity::EventWarning);
|
||||||
#ifdef ICSNEO_ENABLE_TCP
|
|
||||||
TCP::Find(newDriverFoundDevices);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef ICSNEO_ENABLE_RAW_ETHERNET
|
|
||||||
PCAP::Find(newDriverFoundDevices);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef ICSNEO_ENABLE_CDCACM
|
|
||||||
CDCACM::Find(newDriverFoundDevices);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef ICSNEO_ENABLE_DXX
|
|
||||||
DXX::Find(newDriverFoundDevices);
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -274,10 +243,6 @@ std::vector<std::shared_ptr<Device>> DeviceFinder::FindAll() {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
for(auto& device : newFoundDevices) {
|
|
||||||
AddBuiltInExtensionsTo(device);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Grab a weak pointer from the new devices
|
// Grab a weak pointer from the new devices
|
||||||
foundDevices.insert(foundDevices.end(), newFoundDevices.begin(), newFoundDevices.end());
|
foundDevices.insert(foundDevices.end(), newFoundDevices.begin(), newFoundDevices.end());
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,366 @@
|
|||||||
|
#include "icsneo/device/extensions/bootloader/api.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/communication/radcommunication.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/communication/radmultichipcommunication.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/communication/radperiphcommunication.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/communication/redcore.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/communication/red.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/communication/application.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/platform/logging.h"
|
||||||
|
#include "icsneo/communication/message/resetstatusmessage.h"
|
||||||
|
#include "icsneo/api/lifetime.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <chrono>
|
||||||
|
#include <set>
|
||||||
|
#include <thread>
|
||||||
|
#include <unordered_set>
|
||||||
|
|
||||||
|
using namespace icsneo;
|
||||||
|
using namespace icsneo::Bootloader;
|
||||||
|
|
||||||
|
static std::unique_ptr<Bootloader::Communication> getCommunication(
|
||||||
|
icsneo::Device& device, const FlashPhase& phase, const ChipInfo& chip
|
||||||
|
) {
|
||||||
|
switch(phase.comm) {
|
||||||
|
case BootloaderCommunication::Application:
|
||||||
|
return std::make_unique<Application>(device, chip, phase.authenticate, phase.encrypt);
|
||||||
|
case BootloaderCommunication::RED:
|
||||||
|
return std::make_unique<REDCommunication>(device, chip, phase.authenticate, phase.encrypt);
|
||||||
|
case BootloaderCommunication::REDCore:
|
||||||
|
return std::make_unique<REDCoreCommunication>(device, chip);
|
||||||
|
case BootloaderCommunication::RAD:
|
||||||
|
return std::make_unique<RADCommunication>(device, chip);
|
||||||
|
case BootloaderCommunication::RADMultiChip:
|
||||||
|
return std::make_unique<RADMultiChipCommunication>(device, chip, phase.authenticate, phase.slot);
|
||||||
|
case BootloaderCommunication::RADGalaxy2Peripheral:
|
||||||
|
return std::make_unique<RADPeriphCommunication>(device, chip);
|
||||||
|
case BootloaderCommunication::Invalid:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool findInfo(icsneo::Device& device, ChipID chip, ChipInfo& info) {
|
||||||
|
const auto& allInfo = device.getChipInfo();
|
||||||
|
auto iter = std::find_if(allInfo.begin(), allInfo.end(), [chip](const auto& candidate) { return candidate.id == chip; });
|
||||||
|
if(iter == allInfo.end()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
info = *iter;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const FlashPhase* findCorrespondingFlashPhase(const BootloaderPipeline& pipeline, ChipID chip) {
|
||||||
|
for(const auto& phase : pipeline.phases) {
|
||||||
|
if(phase->getType() == BootloaderPhase::Type::Flash) {
|
||||||
|
const auto* flashPhase = static_cast<const FlashPhase*>(phase.get());
|
||||||
|
if(flashPhase->chip == chip) {
|
||||||
|
return flashPhase;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string chipString(ChipID chip) {
|
||||||
|
return "chip id: " + std::to_string(static_cast<int>(chip));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Bootloader::requiresUpdate(icsneo::Device& device, const FWRepo& fw) {
|
||||||
|
const auto versions = device.getChipVersions();
|
||||||
|
for(const auto& info : device.getChipInfo()) {
|
||||||
|
if(!info.defaultEnabled || !info.iefName) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
auto iter = std::find_if(versions.begin(), versions.end(), [&info](const auto& ver) { return ver.id == info.id; });
|
||||||
|
if(iter == versions.end()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
auto ver = fw.getChipVersion(info.iefName);
|
||||||
|
if(ver && *ver != iter->makeAppVersion()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Open the communication, run the action, then close it
|
||||||
|
*/
|
||||||
|
template<typename Action>
|
||||||
|
static bool withCommunication(Bootloader::Communication* com, const ChipInfo& info, const char* phaseName, Action action) {
|
||||||
|
if(!com) {
|
||||||
|
Log::Error(phaseName, "no bootloader communication for " + chipString(info.id));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(!com->open()) {
|
||||||
|
Log::Error(phaseName, "failed to open communication for " + chipString(info.id));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(!action(*com)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(!com->close()) {
|
||||||
|
Log::Error(phaseName, "failed to close communication for " + chipString(info.id));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool runPipeline(
|
||||||
|
icsneo::Device& device,
|
||||||
|
const BootloaderPipeline& pipeline,
|
||||||
|
const FWRepo& fw,
|
||||||
|
const Logger& logger,
|
||||||
|
bool alreadyInBootloader,
|
||||||
|
bool forceFlash
|
||||||
|
) {
|
||||||
|
icsneo::Lifetime suppressHeartbeats = device.suppressDisconnects();
|
||||||
|
const auto versions = device.getChipVersions();
|
||||||
|
std::unordered_set<icsneo::ChipID> chipsFlashed;
|
||||||
|
|
||||||
|
bool updateAll = false;
|
||||||
|
if(auto iter = pipeline.settings.find(BootloaderSetting::UpdateAll); iter != pipeline.settings.end()) {
|
||||||
|
updateAll = std::get<bool>(iter->second);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(const auto& phase : pipeline.phases) {
|
||||||
|
switch(phase->getType()) {
|
||||||
|
case BootloaderPhase::Type::EnterBootloader: {
|
||||||
|
if(alreadyInBootloader) {
|
||||||
|
Log::Trace("EnterBootloaderPhase", "device already in bootloader, skipping");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Log::Info("EnterBootloaderPhase", "entering bootloader");
|
||||||
|
if(!enterBootloader(device)) {
|
||||||
|
Log::Error("EnterBootloaderPhase", "failed to enter bootloader");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case BootloaderPhase::Type::Flash: {
|
||||||
|
const auto& flashPhase = static_cast<const FlashPhase&>(*phase);
|
||||||
|
Log::Info("FlashPhase", "started for " + chipString(flashPhase.chip));
|
||||||
|
|
||||||
|
ChipInfo info;
|
||||||
|
if(!findInfo(device, flashPhase.chip, info)) {
|
||||||
|
Log::Error("FlashPhase", "failed to find chip info for " + chipString(flashPhase.chip));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto sourceVer = info.iefName ? fw.getChipVersion(info.iefName) : std::nullopt;
|
||||||
|
if(!sourceVer) {
|
||||||
|
Log::Trace("FlashPhase", "skipped, no source version for " + chipString(flashPhase.chip));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(!updateAll && !forceFlash) {
|
||||||
|
auto iter = std::find_if(versions.begin(), versions.end(), [&info](const auto& ver) { return ver.id == info.id; });
|
||||||
|
if(iter == versions.end()) {
|
||||||
|
Log::Trace("FlashPhase", "skipped, chip not reported by the device, " + chipString(flashPhase.chip));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(iter->makeAppVersion() == *sourceVer) {
|
||||||
|
Log::Trace("FlashPhase", "skipped, chip already at target version, " + chipString(flashPhase.chip));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
auto com = getCommunication(device, flashPhase, info);
|
||||||
|
if(!withCommunication(com.get(), info, "FlashPhase", [&](Bootloader::Communication& opened) {
|
||||||
|
if(!opened.flash(fw, logger)) {
|
||||||
|
Log::Error("FlashPhase", "failed to flash " + chipString(flashPhase.chip));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
})) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
chipsFlashed.insert(info.id);
|
||||||
|
Log::Info("FlashPhase", "completed successfully for " + chipString(flashPhase.chip));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case BootloaderPhase::Type::Finalize: {
|
||||||
|
const auto& finalizePhase = static_cast<const FinalizePhase&>(*phase);
|
||||||
|
if(!chipsFlashed.count(finalizePhase.chip)) {
|
||||||
|
Log::Trace("FinalizePhase", "skipped, chip not flashed, " + chipString(finalizePhase.chip));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Log::Info("FinalizePhase", "started for " + chipString(finalizePhase.chip));
|
||||||
|
const FlashPhase* flashPhase = findCorrespondingFlashPhase(pipeline, finalizePhase.chip);
|
||||||
|
ChipInfo info;
|
||||||
|
if(!flashPhase || !findInfo(device, finalizePhase.chip, info)) {
|
||||||
|
// Should never happen (developer error)
|
||||||
|
Log::Error("FinalizePhase", "no flash phase or chip info for " + chipString(finalizePhase.chip));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto com = getCommunication(device, *flashPhase, info);
|
||||||
|
if(com && !com->hasFinalizationStep()) {
|
||||||
|
Log::Trace("FinalizePhase", "skipped, no finalization step for " + chipString(finalizePhase.chip));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!withCommunication(com.get(), info, "FinalizePhase", [&](Bootloader::Communication& opened) {
|
||||||
|
if(!opened.finalize()) {
|
||||||
|
Log::Error("FinalizePhase", "failed to finalize " + chipString(finalizePhase.chip));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
})) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Log::Info("FinalizePhase", "completed successfully for " + chipString(finalizePhase.chip));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case BootloaderPhase::Type::Reconnect: {
|
||||||
|
Log::Info("ReconnectPhase", "reconnecting to device");
|
||||||
|
if(!device.reconnect(std::chrono::milliseconds(60000))) {
|
||||||
|
Log::Error("ReconnectPhase", "failed to reconnect to device");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case BootloaderPhase::Type::Wait: {
|
||||||
|
const auto& waitPhase = static_cast<const WaitPhase&>(*phase);
|
||||||
|
Log::Trace("WaitPhase", "waiting " + std::to_string(waitPhase.timeout.count()) + "ms");
|
||||||
|
std::this_thread::sleep_for(waitPhase.timeout);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case BootloaderPhase::Type::EnterApplication: {
|
||||||
|
const auto& enterAppPhase = static_cast<const EnterApplicationPhase&>(*phase);
|
||||||
|
if(!chipsFlashed.count(enterAppPhase.chip)) {
|
||||||
|
Log::Trace("EnterApplicationPhase", "skipped, chip not flashed, " + chipString(enterAppPhase.chip));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Log::Info("EnterApplicationPhase", "started for " + chipString(enterAppPhase.chip));
|
||||||
|
const FlashPhase* flashPhase = findCorrespondingFlashPhase(pipeline, enterAppPhase.chip);
|
||||||
|
ChipInfo info;
|
||||||
|
if(!flashPhase || !findInfo(device, enterAppPhase.chip, info)) {
|
||||||
|
// Should never happen (developer error)
|
||||||
|
Log::Error("EnterApplicationPhase", "no flash phase or chip info for " + chipString(enterAppPhase.chip));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto com = getCommunication(device, *flashPhase, info);
|
||||||
|
if(!withCommunication(com.get(), info, "EnterApplicationPhase", [&](Bootloader::Communication& opened) {
|
||||||
|
if(!opened.enterApplication()) {
|
||||||
|
Log::Error("EnterApplicationPhase", "failed to enter application for " + chipString(enterAppPhase.chip));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
})) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Log::Info("EnterApplicationPhase", "completed successfully for " + chipString(enterAppPhase.chip));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return exitBootloader(device, logger, !chipsFlashed.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Bootloader::update(
|
||||||
|
icsneo::Device& device,
|
||||||
|
const FWRepo& fw,
|
||||||
|
Logger logger,
|
||||||
|
bool alreadyInBootloader,
|
||||||
|
bool forceFlash
|
||||||
|
) {
|
||||||
|
const auto pipeline = device.getBootloader();
|
||||||
|
if(!pipeline) {
|
||||||
|
device.getEventHandler()(APIEvent::Type::FirmwareUpdateNotSupported, APIEvent::Severity::Error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!runPipeline(device, pipeline, fw, logger, alreadyInBootloader, forceFlash)) {
|
||||||
|
device.getEventHandler()(APIEvent::Type::FirmwareUpdateFailed, APIEvent::Severity::Error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Bootloader::enterBootloader(icsneo::Device& device) {
|
||||||
|
if(!device.com->sendCommand(icsneo::Command::EnterBootloader)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return device.reconnect(std::chrono::milliseconds(30000));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Bootloader::exitBootloader(icsneo::Device& device, Logger logger, bool updatedChips) {
|
||||||
|
if(!updatedChips) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
logger(icsneo::Device::OpenStatusType::Progress, "Connecting to the device...", std::nullopt);
|
||||||
|
return verifyConnection(device, logger);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Toggle network communication and wait up to 5 seconds for a reset status confirming the change.
|
||||||
|
* Like icsneo::Device::goOnline(), a timeout is not an error.
|
||||||
|
*/
|
||||||
|
static bool setCommunicationEnabled(icsneo::Device& device, const Logger& logger, bool enable, double progressStart) {
|
||||||
|
static constexpr std::chrono::seconds Timeout(5);
|
||||||
|
const auto filter = std::make_shared<MessageFilter>(Message::Type::ResetStatus);
|
||||||
|
|
||||||
|
if(!device.com->sendCommand(Command::EnableNetworkCommunication, enable)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
device.com->sendCommand(Command::UpdateLEDState, std::vector<uint8_t>({uint8_t(enable ? 0x10 : 0x04)}));
|
||||||
|
|
||||||
|
const auto startTime = std::chrono::steady_clock::now();
|
||||||
|
auto elapsedTime = std::chrono::duration<double>(0);
|
||||||
|
while(elapsedTime < Timeout) {
|
||||||
|
bool failOut = false;
|
||||||
|
auto msg = device.com->waitForMessageSync([&device, &failOut]() {
|
||||||
|
if(!device.com->sendCommand(Command::RequestStatusUpdate)) {
|
||||||
|
failOut = true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}, filter, std::chrono::milliseconds(100));
|
||||||
|
|
||||||
|
if(failOut) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(msg && std::static_pointer_cast<icsneo::ResetStatusMessage>(msg)->comEnabled == enable) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Each call covers half of the overall progress
|
||||||
|
logger(icsneo::Device::OpenStatusType::Progress, "Connecting to the device...", progressStart + (elapsedTime / Timeout) / 2.0);
|
||||||
|
elapsedTime = std::chrono::steady_clock::now() - startTime;
|
||||||
|
}
|
||||||
|
logger(icsneo::Device::OpenStatusType::Progress, "Connecting to the device...", progressStart + 0.5);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Bootloader::verifyConnection(icsneo::Device& device, Logger logger) {
|
||||||
|
// Firmware needs to confirm the update by verifying that it can enable/disable the connection on the device
|
||||||
|
return setCommunicationEnabled(device, logger, true, 0.0) && setCommunicationEnabled(device, logger, false, 0.5);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Bootloader::isSFPChip(const icsneo::Bootloader::Chip& chip) {
|
||||||
|
static const std::set<icsneo::ChipID> sfpChipIDs = {
|
||||||
|
icsneo::ChipID::SFPModule_88q2112_MCHIP,
|
||||||
|
icsneo::ChipID::SFPModule_88q2221_MCHIP,
|
||||||
|
icsneo::ChipID::SFPModule_88q3244_MCHIP,
|
||||||
|
icsneo::ChipID::SFPModule_ent11100_MCHIP,
|
||||||
|
icsneo::ChipID::SFPModule_lan8670_MCHIP,
|
||||||
|
icsneo::ChipID::SFPModule_ad1101_MCHIP,
|
||||||
|
};
|
||||||
|
return sfpChipIDs.find(chip.getID()) != sfpChipIDs.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Bootloader::isSFPIEF(const icsneo::Bootloader::IEF& ief) {
|
||||||
|
static const std::set<icsneo::Device::ProductID> sfpProductIDs = {
|
||||||
|
icsneo::Device::ProductID::SFPModule_88q2112,
|
||||||
|
icsneo::Device::ProductID::SFPModule_88q2221m,
|
||||||
|
icsneo::Device::ProductID::SFPModule_88q4364,
|
||||||
|
icsneo::Device::ProductID::SFPModule_ent11100,
|
||||||
|
icsneo::Device::ProductID::SFPModule_lan8670,
|
||||||
|
icsneo::Device::ProductID::SFPModule_ad1101,
|
||||||
|
};
|
||||||
|
return sfpProductIDs.find(static_cast<icsneo::Device::ProductID>(ief.getVersion().prodId)) != sfpProductIDs.end();
|
||||||
|
}
|
||||||
@@ -0,0 +1,407 @@
|
|||||||
|
#include "icsneo/device/extensions/bootloader/communication/application.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/parser/fwrepo.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/platform/logging.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/api.h"
|
||||||
|
#include "icsneo/communication/message/extendedresponsemessage.h"
|
||||||
|
#include "icsneo/communication/message/main51message.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <condition_variable>
|
||||||
|
#include <cstring>
|
||||||
|
#include <limits>
|
||||||
|
#include <mutex>
|
||||||
|
#include <optional>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
#include <icsbl/icsbl.h>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
bool Application::enterApplication() {
|
||||||
|
// Always reboot
|
||||||
|
Log::Info(__func__, "sending reboot command");
|
||||||
|
bool ok = device.com->sendCommand(icsneo::ExtendedCommand::Reboot);
|
||||||
|
if(!ok) {
|
||||||
|
Log::Error(__func__, "reboot command failed");
|
||||||
|
}
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Application::finalize() {
|
||||||
|
Log::Debug(__func__, "sending finalize command");
|
||||||
|
bool ok = sendCommand(Update::Finalize, [](double) {});
|
||||||
|
if(!ok) {
|
||||||
|
Log::Error(__func__, "finalize command failed");
|
||||||
|
}
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Application::flash(const FWRepo& fw, const icsneo::Device::OpenStatusHandler& handler) {
|
||||||
|
Log::Info(__func__, "starting flash for " + targetChip.getName());
|
||||||
|
const auto& srcVer = fw.getVersion();
|
||||||
|
|
||||||
|
std::vector<uint8_t> bin;
|
||||||
|
if(!fw.getFW(targetChip.getIEFName(), bin)) {
|
||||||
|
Log::Error(__func__, "failed to get firmware for " + targetChip.getIEFName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
IEF ief;
|
||||||
|
if(!ief.load(std::move(bin))) {
|
||||||
|
Log::Error(__func__, "failed to load IEF for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto currentString = "Preparing to update " + targetChip.getName() + "...";
|
||||||
|
handler(icsneo::Device::OpenStatusType::Progress, currentString, std::nullopt);
|
||||||
|
|
||||||
|
// The supplied IEF is not for this product
|
||||||
|
if(ief.getVersion().prodId != static_cast<uint8_t>(device.getProductID())) {
|
||||||
|
Log::Error(__func__, "IEF product ID mismatch for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The supplied IEF is for this product, but not for this chip
|
||||||
|
if(ief.getVersion().chipId != static_cast<uint8_t>(targetChip.getID())) {
|
||||||
|
Log::Error(__func__, "IEF chip ID mismatch for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(doAuthChecks) {
|
||||||
|
if(!authenticate()) {
|
||||||
|
Log::Error(__func__, "authentication failed for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tell the application software what version of Vehicle Spy 3 this firmware is from.
|
||||||
|
// We check to make sure it approves, but also, the device will not accept new firmware
|
||||||
|
// if we have not sent this.
|
||||||
|
// Sometimes new hardware revisions can not accept firmware which is too old.
|
||||||
|
if(!sendCommand(Update::CheckVersion, [](double) {}, 0, {srcVer.major, srcVer.minor, srcVer.release, srcVer.build})) {
|
||||||
|
Log::Error(__func__, "version check failed for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ok = sendSoftware(ief.getSectors(), ief.getBlockSize(), handler);
|
||||||
|
if(!ok) {
|
||||||
|
Log::Error(__func__, "sendSoftware failed for " + targetChip.getName());
|
||||||
|
}
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Application::authenticate() {
|
||||||
|
icsbl_seed_v1 seed = {};
|
||||||
|
if(!icsbl_generate_seed_v1(device.getSerialNumber(), &seed)) {
|
||||||
|
Log::Error(__func__, "libicsbl could not generate an authentication challenge");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send the challenge (seed.a), the device replies with seed.b and one trailing byte
|
||||||
|
std::vector<uint8_t> cmdArgs(sizeof(seed.a));
|
||||||
|
writeLE32(cmdArgs.data(), seed.a[0]);
|
||||||
|
writeLE32(cmdArgs.data() + 4, seed.a[1]);
|
||||||
|
|
||||||
|
auto filter = std::make_shared<icsneo::MessageFilter>(icsneo::Message::Type::Main51);
|
||||||
|
auto msg = device.com->waitForMessageSync([&] {
|
||||||
|
return device.com->sendCommand(icsneo::Command::ValidateDevice, cmdArgs);
|
||||||
|
}, filter, std::chrono::milliseconds(200));
|
||||||
|
|
||||||
|
if(!msg) {
|
||||||
|
Log::Error(__func__, "No response from the device");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto main51Msg = std::static_pointer_cast<icsneo::Main51Message>(msg);
|
||||||
|
|
||||||
|
if(main51Msg->data.size() != sizeof(seed.b) + 1) {
|
||||||
|
Log::Error(__func__, "Unexpected authentication reply size from the device");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const bool authenticated = readLE32(main51Msg->data.data()) == seed.b[0] && readLE32(main51Msg->data.data() + 4) == seed.b[1];
|
||||||
|
if(!authenticated) {
|
||||||
|
Log::Error(__func__, "The authentication reply does not match, the device did not authenticate");
|
||||||
|
}
|
||||||
|
return authenticated;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Application::validateFirmware(const DeviceAppVersion& chipSrcVer) {
|
||||||
|
return sendCommand(Update::CheckComponentVersion, [](double) {}, 0, {chipSrcVer.major, chipSrcVer.minor});
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Application::sendSoftware(const std::vector<uint8_t>& bin, const icsneo::Device::OpenStatusHandler& handler) {
|
||||||
|
Log::Debug(__func__, "starting for " + targetChip.getName() + " (" + std::to_string(bin.size()) + " bytes)");
|
||||||
|
auto currentString = "Preparing to update " + targetChip.getName() + "...";
|
||||||
|
handler(icsneo::Device::OpenStatusType::Progress, currentString, std::nullopt);
|
||||||
|
if(bin.empty() || bin.size() > std::numeric_limits<uint32_t>::max()) {
|
||||||
|
Log::Error(__func__, "invalid firmware size for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const uint32_t totalSize = static_cast<uint32_t>(bin.size());
|
||||||
|
|
||||||
|
Log::Debug(__func__, "erasing " + targetChip.getName());
|
||||||
|
if(!sendCommand(Update::Erase, 0, totalSize, [&](double progress) {
|
||||||
|
handler(icsneo::Device::OpenStatusType::Progress, currentString, progress);
|
||||||
|
}, std::chrono::milliseconds(45000))) {
|
||||||
|
Log::Error(__func__, "erase failed for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
currentString = "Updating " + targetChip.getName() + "...";
|
||||||
|
handler(icsneo::Device::OpenStatusType::Progress, currentString, std::nullopt);
|
||||||
|
|
||||||
|
const uint32_t stepSize = std::min<uint32_t>(totalSize, 2048);
|
||||||
|
uint32_t bytesTransferred = 0;
|
||||||
|
while(bytesTransferred < totalSize) {
|
||||||
|
const uint32_t bytesToSend = std::min(stepSize, totalSize - bytesTransferred);
|
||||||
|
if(!sendCommand(Update::Write, [](double) {}, bytesTransferred, { bin.begin() + bytesTransferred, bin.begin() + bytesTransferred + bytesToSend})) {
|
||||||
|
Log::Error(__func__, "write failed at offset " + std::to_string(bytesTransferred) + " for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bytesTransferred += bytesToSend;
|
||||||
|
handler(icsneo::Device::OpenStatusType::Progress, currentString, double(bytesTransferred) / totalSize);
|
||||||
|
}
|
||||||
|
Log::Debug(__func__, "write complete, validating " + targetChip.getName());
|
||||||
|
if(!validateComponent()) {
|
||||||
|
Log::Error(__func__, "validation failed for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Log::Debug(__func__, "completed successfully for " + targetChip.getName());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Application::sendSoftware(const std::vector<IEF::Sector>& sectorPairs, uint32_t blockSize, const icsneo::Device::OpenStatusHandler& handler) {
|
||||||
|
if(sectorPairs.empty()) {
|
||||||
|
Log::Error(__func__, "no sectors to flash for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Log::Debug(__func__, "starting for " + targetChip.getName() + " (" + std::to_string(sectorPairs.size()) + " sectors)");
|
||||||
|
auto currentString = "Preparing to update " + targetChip.getName() + "...";
|
||||||
|
handler(icsneo::Device::OpenStatusType::Progress, currentString, std::nullopt);
|
||||||
|
uint64_t totalSize = 0;
|
||||||
|
for(const auto& sector : sectorPairs) {
|
||||||
|
totalSize += sector.second.size();
|
||||||
|
}
|
||||||
|
if(totalSize > std::numeric_limits<uint32_t>::max()) {
|
||||||
|
Log::Error(__func__, "total size too large for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(totalSize == 0) {
|
||||||
|
Log::Error(__func__, "total size is 0 for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Log::Debug(__func__, "erasing " + targetChip.getName() + " (" + std::to_string(totalSize) + " bytes)");
|
||||||
|
if(!sendCommand(Update::Erase, 0, static_cast<uint32_t>(totalSize), [&](double progress) {
|
||||||
|
handler(icsneo::Device::OpenStatusType::Progress, currentString, progress);
|
||||||
|
}, std::chrono::milliseconds(45000))) {
|
||||||
|
Log::Error(__func__, "erase failed for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(isSFPChip(targetChip)) {
|
||||||
|
// SFPs are not ready to write until 4 seconds after finishing erasing (for some reason)
|
||||||
|
Log::Trace(__func__, "SFP chip detected, waiting 4000ms after erase");
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(4000));
|
||||||
|
}
|
||||||
|
|
||||||
|
currentString = "Updating " + targetChip.getName() + "...";
|
||||||
|
handler(icsneo::Device::OpenStatusType::Progress, currentString, std::nullopt);
|
||||||
|
|
||||||
|
uint32_t bytesTransferred = 0;
|
||||||
|
for(const auto& pair : sectorPairs) {
|
||||||
|
const auto& rawData = pair.second;
|
||||||
|
const uint32_t offset = pair.first * blockSize;
|
||||||
|
std::vector<uint8_t> processedData(rawData.begin(), rawData.end());
|
||||||
|
if(encrypted) {
|
||||||
|
if(rawData.size() % 8 != 0) {
|
||||||
|
Log::Error(__func__, "raw data size not aligned");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
icsbl_encrypt_v1(device.getSerialNumber(), rawData.data(), rawData.size(), processedData.data());
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!sendCommand(Update::Write, [](double) {}, offset, processedData)) {
|
||||||
|
Log::Error(__func__, "write failed at offset " + std::to_string(offset) + " for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bytesTransferred += static_cast<uint32_t>(rawData.size());
|
||||||
|
handler(icsneo::Device::OpenStatusType::Progress, currentString, double(bytesTransferred) / double(totalSize));
|
||||||
|
}
|
||||||
|
Log::Debug(__func__, "write complete, validating " + targetChip.getName());
|
||||||
|
bool ok = validateComponent();
|
||||||
|
if(!ok) {
|
||||||
|
Log::Error(__func__, "validation failed for " + targetChip.getName());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Log::Debug(__func__, "completed successfully for " + targetChip.getName());
|
||||||
|
}
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Application::validateComponent() {
|
||||||
|
Log::Debug(__func__, "validating " + targetChip.getName());
|
||||||
|
bool ok = sendCommand(Update::ValidateComponent, [](double) {}, 0, {}, std::chrono::milliseconds(45000));
|
||||||
|
if(!ok) {
|
||||||
|
Log::Error(__func__, "validation failed for " + targetChip.getName());
|
||||||
|
}
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send one SoftwareUpdate sub-command and wait for it to complete.
|
||||||
|
*
|
||||||
|
* The device answers the request with OK when it is already done, or OperationPending when it will
|
||||||
|
* finish later. For a pending command we poll Update::GetProgress (except for Write and
|
||||||
|
* ValidateComponent, which do not report progress) until the device sends a final OK or the timeout
|
||||||
|
* passes. Progress replies are OperationPending with a SoftwareUpdateCommandHeader whose
|
||||||
|
* commandSizeOrProgress holds the progress so far, or a GenericReturn with no progress.
|
||||||
|
*/
|
||||||
|
bool Application::sendCommand(Update command, uint32_t offset, uint32_t length, const StatusHandler& handler, std::chrono::milliseconds timeout, std::vector<uint8_t> args) {
|
||||||
|
bool finishedResponse = false;
|
||||||
|
std::condition_variable finishedResponseCV;
|
||||||
|
std::mutex finishedResponseMutex;
|
||||||
|
|
||||||
|
std::optional<uint32_t> progress;
|
||||||
|
std::condition_variable progressCV;
|
||||||
|
std::mutex progressMutex;
|
||||||
|
|
||||||
|
bool firstResponse = true;
|
||||||
|
// Register the callback before sending the command, so that if it goes from pending to finished quickly we will still catch it
|
||||||
|
const auto callback = std::make_shared<icsneo::MessageCallback>(std::make_shared<icsneo::MessageFilter>(icsneo::Message::Type::ExtendedResponse), [&](std::shared_ptr<icsneo::Message> msg) {
|
||||||
|
if(firstResponse) {
|
||||||
|
firstResponse = false;
|
||||||
|
return true; // let waitForMessageSync handle the first message
|
||||||
|
}
|
||||||
|
auto extMsg = std::static_pointer_cast<ExtendedResponseMessage>(msg);
|
||||||
|
if(extMsg->command != ExtendedCommand::SoftwareUpdate) {
|
||||||
|
Log::Error(__func__, "Got an extended response for a command other than SoftwareUpdate");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(extMsg->response == ExtendedResponse::OK) {
|
||||||
|
{
|
||||||
|
std::scoped_lock lk(finishedResponseMutex);
|
||||||
|
finishedResponse = true;
|
||||||
|
}
|
||||||
|
finishedResponseCV.notify_one();
|
||||||
|
} else if(extMsg->response == ExtendedResponse::OperationPending) {
|
||||||
|
if(extMsg->data.size() < sizeof(ExtendedResponseMessage::ResponseHeader)) {
|
||||||
|
Log::Error(__func__, "The pending response is too short to contain a header");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
ExtendedResponseMessage::ResponseHeader respHeader;
|
||||||
|
std::memcpy(&respHeader, extMsg->data.data(), sizeof(respHeader));
|
||||||
|
if(respHeader.command == ExtendedCommand::SoftwareUpdate) {
|
||||||
|
if(respHeader.length != sizeof(SoftwareUpdateCommandHeader) ||
|
||||||
|
extMsg->data.size() < sizeof(ExtendedResponseMessage::ResponseHeader) + sizeof(SoftwareUpdateCommandHeader)) {
|
||||||
|
Log::Error(__func__, "The progress response does not contain a SoftwareUpdate header");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
SoftwareUpdateCommandHeader resp;
|
||||||
|
std::memcpy(&resp, extMsg->data.data() + sizeof(ExtendedResponseMessage::ResponseHeader), sizeof(resp));
|
||||||
|
{
|
||||||
|
std::scoped_lock lk(progressMutex);
|
||||||
|
progress.emplace(resp.commandSizeOrProgress);
|
||||||
|
}
|
||||||
|
progressCV.notify_one();
|
||||||
|
} else if(respHeader.command == ExtendedCommand::GenericReturn) {
|
||||||
|
// No progress information, the final OK comes later
|
||||||
|
{
|
||||||
|
std::scoped_lock lk(progressMutex);
|
||||||
|
progress.emplace(0);
|
||||||
|
}
|
||||||
|
progressCV.notify_one();
|
||||||
|
} else {
|
||||||
|
Log::Error(__func__, "The pending response is for an unexpected command");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Log::Error(__func__, "The device reported an error for the SoftwareUpdate command");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
const auto finishedCallbackHandle = device.com->addMessageCallback(callback);
|
||||||
|
Lifetime statusCallbackCleanup([&] {
|
||||||
|
device.com->removeMessageCallback(finishedCallbackHandle);
|
||||||
|
});
|
||||||
|
|
||||||
|
SoftwareUpdateCommandHeader header = {};
|
||||||
|
header.componentIdentifier = static_cast<uint32_t>(targetChip.getID());
|
||||||
|
header.commandType = command;
|
||||||
|
header.offset = offset;
|
||||||
|
header.commandSizeOrProgress = length;
|
||||||
|
header.expansionSlot = targetChip.expansionSlot;
|
||||||
|
const auto headerBytes = reinterpret_cast<const uint8_t*>(&header);
|
||||||
|
args.insert(args.begin(), headerBytes, headerBytes + sizeof(header));
|
||||||
|
|
||||||
|
{
|
||||||
|
auto msg = device.com->waitForMessageSync([&] {
|
||||||
|
return device.com->sendCommand(ExtendedCommand::SoftwareUpdate, args);
|
||||||
|
}, std::make_shared<icsneo::MessageFilter>(icsneo::Message::Type::ExtendedResponse), timeout);
|
||||||
|
|
||||||
|
if(!msg) {
|
||||||
|
Log::Error(__func__, "No response to the SoftwareUpdate command");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto extMsg = std::static_pointer_cast<ExtendedResponseMessage>(msg);
|
||||||
|
if(extMsg->command != ExtendedCommand::SoftwareUpdate) {
|
||||||
|
Log::Error(__func__, "Got an extended response for a command other than SoftwareUpdate");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(extMsg->response == ExtendedResponse::OK) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if(extMsg->response != ExtendedResponse::OperationPending) {
|
||||||
|
Log::Error(__func__, "The device rejected the SoftwareUpdate command");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SoftwareUpdateCommandHeader progressHeader = {};
|
||||||
|
progressHeader.componentIdentifier = static_cast<uint32_t>(targetChip.getID());
|
||||||
|
progressHeader.commandType = Update::GetProgress;
|
||||||
|
progressHeader.expansionSlot = targetChip.expansionSlot;
|
||||||
|
const auto progressHeaderBytes = reinterpret_cast<const uint8_t*>(&progressHeader);
|
||||||
|
const std::vector<uint8_t> progressArgs(progressHeaderBytes, progressHeaderBytes + sizeof(progressHeader));
|
||||||
|
const auto deadline = std::chrono::steady_clock::now() + timeout;
|
||||||
|
// Write and ValidateComponent do not report progress, just wait for the final OK
|
||||||
|
if(command != Update::Write && command != Update::ValidateComponent) {
|
||||||
|
while(std::chrono::steady_clock::now() < deadline) {
|
||||||
|
{
|
||||||
|
std::unique_lock lk(progressMutex);
|
||||||
|
progress.reset();
|
||||||
|
if(!device.com->sendCommand(ExtendedCommand::SoftwareUpdate, progressArgs)) {
|
||||||
|
Log::Error(__func__, "Failed to send progress request to the device");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(!progressCV.wait_for(lk, std::chrono::milliseconds(5000), [&]{ return progress.has_value(); })) {
|
||||||
|
Log::Error(__func__, "No progress response from device");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
handler(*progress / double(length));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
std::scoped_lock lk(finishedResponseMutex);
|
||||||
|
if(finishedResponse) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_lock lk(finishedResponseMutex);
|
||||||
|
return finishedResponseCV.wait_until(lk, deadline, [&]{ return finishedResponse; });
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
#include "icsneo/device/extensions/bootloader/communication/communication.h"
|
||||||
|
|
||||||
|
using namespace icsneo::Bootloader;
|
||||||
|
|
||||||
|
uint32_t Communication::readLE32(const uint8_t* buf) {
|
||||||
|
return (uint32_t)buf[0] | ((uint32_t)buf[1] << 8) | ((uint32_t)buf[2] << 16) | ((uint32_t)buf[3] << 24);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Communication::writeLE32(uint8_t* buf, uint32_t num) {
|
||||||
|
buf[0] = num & 0xFFu;
|
||||||
|
buf[1] = (num >> 8) & 0xFFu;
|
||||||
|
buf[2] = (num >> 16) & 0xFFu;
|
||||||
|
buf[3] = (num >> 24) & 0xFFu;
|
||||||
|
}
|
||||||
@@ -0,0 +1,547 @@
|
|||||||
|
#include "icsneo/device/extensions/bootloader/communication/radcommunication.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/communication/bootloaderversion.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/parser/fwzip.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/parser/fwrepo.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/platform/logging.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cstring>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
#include <icsbl/icsbl.h>
|
||||||
|
|
||||||
|
using namespace icsneo::Bootloader;
|
||||||
|
|
||||||
|
RADCommunication::RADCommunication(icsneo::Device& dev, const ChipInfo& info)
|
||||||
|
: Communication(dev), targetChip(info) {}
|
||||||
|
|
||||||
|
bool RADCommunication::supportsFlashBuffer() const {
|
||||||
|
switch(targetChip.getID()) {
|
||||||
|
case icsneo::ChipID::RADGigastar_USBZ_ZYNQ:
|
||||||
|
case icsneo::ChipID::RADGigastar_USBZ_Z7007S_ZYNQ:
|
||||||
|
case icsneo::ChipID::RADGigastar_USBZ_Z7010_ZYNQ: {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
RADCommunication::Request RADCommunication::makeRequest(Request::Type type, size_t payloadSize, size_t expectedResponseLen) {
|
||||||
|
const size_t headerSize = supportsZynqBinaryFlasher ? ZynqFlasherHeaderSize : 0;
|
||||||
|
const size_t packetSize = headerSize + 1 + payloadSize; // Header + command + payload
|
||||||
|
|
||||||
|
// This shouldn't cause any re-allocations since we already reserved the MaxMessageLength in initialize()
|
||||||
|
txBuffer.resize(packetSize, 0);
|
||||||
|
|
||||||
|
if(supportsZynqBinaryFlasher) {
|
||||||
|
std::copy(std::begin(ZynqFlasherSync), std::end(ZynqFlasherSync), txBuffer.begin());
|
||||||
|
|
||||||
|
// The length covers the command byte and the payload
|
||||||
|
const uint16_t payloadPlusCmd = static_cast<uint16_t>(payloadSize) + 1;
|
||||||
|
txBuffer[4] = payloadPlusCmd & 0xFFu;
|
||||||
|
txBuffer[5] = (payloadPlusCmd >> 8) & 0xFFu;
|
||||||
|
}
|
||||||
|
|
||||||
|
txBuffer[headerSize] = static_cast<uint8_t>(type);
|
||||||
|
|
||||||
|
Request newRequest;
|
||||||
|
newRequest.type = type;
|
||||||
|
newRequest.expectedResponseLen = expectedResponseLen;
|
||||||
|
if(payloadSize > 0) {
|
||||||
|
newRequest.payload = &txBuffer[headerSize + 1]; // First byte right after the command
|
||||||
|
}
|
||||||
|
|
||||||
|
return newRequest;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADCommunication::sendRequestNoResponse() {
|
||||||
|
/**
|
||||||
|
* Send our current packet, the payload was written to it if the user
|
||||||
|
* of this function wrote to request.payload. The remaining details were
|
||||||
|
* written when makeRequest() was called
|
||||||
|
*/
|
||||||
|
return device.com->sendPacket(txBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
RADCommunication::Response& RADCommunication::sendRequest(const Request& request, std::chrono::milliseconds timeout) {
|
||||||
|
static constexpr size_t ResponseHeaderSize = 2;
|
||||||
|
const size_t rxSize = ResponseHeaderSize + request.expectedResponseLen;
|
||||||
|
/**
|
||||||
|
* Clear previous response
|
||||||
|
*/
|
||||||
|
lastResponse = Response();
|
||||||
|
|
||||||
|
if(!sendRequestNoResponse()) {
|
||||||
|
Log::Error(__func__, "Failed to send request " + std::to_string(static_cast<uint8_t>(request.type)));
|
||||||
|
return lastResponse;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto& rb = device.com->driver->getReadBuffer();
|
||||||
|
rb.clear();
|
||||||
|
auto result = device.com->driver->waitForRx(rxSize, timeout);
|
||||||
|
|
||||||
|
if(!result) {
|
||||||
|
Log::Error(__func__, "No response to request " + std::to_string(static_cast<uint8_t>(request.type)));
|
||||||
|
return lastResponse;
|
||||||
|
}
|
||||||
|
|
||||||
|
rb.read(rxBuffer.data(), 0, rxSize);
|
||||||
|
rb.clear();
|
||||||
|
|
||||||
|
lastResponse.done = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Second byte is success/failure byte
|
||||||
|
*/
|
||||||
|
lastResponse.type = static_cast<Response::Type>(rxBuffer[1]);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The rest is the payload, if we requested any
|
||||||
|
*/
|
||||||
|
if(request.expectedResponseLen > 0) {
|
||||||
|
lastResponse.payload = &rxBuffer[2];
|
||||||
|
lastResponse.size = request.expectedResponseLen;
|
||||||
|
}
|
||||||
|
|
||||||
|
return lastResponse;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADCommunication::enterApplication() {
|
||||||
|
Log::Info(__func__, "sending EnterApplication request");
|
||||||
|
makeRequest(Request::Type::EnterApplication);
|
||||||
|
bool ok = sendRequestNoResponse();
|
||||||
|
if(!ok) {
|
||||||
|
Log::Error(__func__, "EnterApplication request failed");
|
||||||
|
}
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADCommunication::close() {
|
||||||
|
device.com->resumeReads();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADCommunication::open() {
|
||||||
|
device.com->pauseReads();
|
||||||
|
|
||||||
|
if(!initialize()) {
|
||||||
|
close();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!sanityCheck()) {
|
||||||
|
// Note: RAD-Gigastar on low power USB flash fails here for some reason, try another sanity check
|
||||||
|
if(!sanityCheck()) {
|
||||||
|
close();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!getBootloaderVersion()) {
|
||||||
|
// Try again, just like the sanity checks
|
||||||
|
if(!getBootloaderVersion()) {
|
||||||
|
close();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!sanityCheck()) {
|
||||||
|
// Note: RAD-Gigastar on low power USB flash fails here for some reason, try another sanity check
|
||||||
|
if(!sanityCheck()) {
|
||||||
|
close();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!authenticate()) {
|
||||||
|
close();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADCommunication::initialize() {
|
||||||
|
auto& rb = device.com->driver->getReadBuffer();
|
||||||
|
|
||||||
|
rxBuffer = std::vector<uint8_t>(MaxMessageLength, 0);
|
||||||
|
txBuffer.reserve(MaxMessageLength);
|
||||||
|
|
||||||
|
// An EnterBootloader command on Main51, as a framed short packet (0xAA, then length << 4 | netid)
|
||||||
|
// with a trailing 0xAF, followed by the same command without the 0xAA framing
|
||||||
|
const uint8_t lengthAndNetID = (1 << 4) | static_cast<uint8_t>(icsneo::Network::NetID::Main51);
|
||||||
|
std::vector<uint8_t> pkt = {
|
||||||
|
0xAA,
|
||||||
|
lengthAndNetID,
|
||||||
|
static_cast<uint8_t>(icsneo::Command::EnterBootloader),
|
||||||
|
0xAF
|
||||||
|
};
|
||||||
|
|
||||||
|
rb.clear();
|
||||||
|
if(!device.com->sendPacket(pkt)) {
|
||||||
|
Log::Error(__func__, "Failed to send the framed enter bootloader command");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
pkt = {
|
||||||
|
lengthAndNetID,
|
||||||
|
static_cast<uint8_t>(icsneo::Command::EnterBootloader)
|
||||||
|
};
|
||||||
|
|
||||||
|
if(!device.com->sendPacket(pkt)) {
|
||||||
|
Log::Error(__func__, "Failed to send the unframed enter bootloader command");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The Zynq binary flasher answers with the command, Success, then ZynqFlasherSync
|
||||||
|
static constexpr size_t ZynqFlasherReplySize = 2 + sizeof(ZynqFlasherSync);
|
||||||
|
if(!device.com->driver->waitForRx(ZynqFlasherReplySize, std::chrono::milliseconds(1000))) {
|
||||||
|
Log::Error(__func__, "No response to the enter bootloader command");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const size_t rxLen = rb.size();
|
||||||
|
if(rxLen == ZynqFlasherReplySize) {
|
||||||
|
rb.read(rxBuffer.data(), 0, rxLen);
|
||||||
|
}
|
||||||
|
rb.clear();
|
||||||
|
supportsZynqBinaryFlasher =
|
||||||
|
rxLen == ZynqFlasherReplySize &&
|
||||||
|
rxBuffer[0] == static_cast<uint8_t>(icsneo::Command::EnterBootloader) &&
|
||||||
|
rxBuffer[1] == static_cast<uint8_t>(Response::Type::Success) &&
|
||||||
|
std::equal(std::begin(ZynqFlasherSync), std::end(ZynqFlasherSync), rxBuffer.begin() + 2);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADCommunication::finalize() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADCommunication::hasFinalizationStep() const {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADCommunication::sanityCheck() {
|
||||||
|
if(!supportsZynqBinaryFlasher) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Two numbered test patterns the bootloader must accept, the 8 byte replies are not checked
|
||||||
|
constexpr uint8_t PatternA[8] = {0xAA, 0x55, 0xAA, 0x55, 0xAA, 0xAA, 0x55, 0x55};
|
||||||
|
constexpr uint8_t PatternB[8] = {0x12, 0x21, 0x34, 0x43, 0x56, 0x65, 0x78, 0x87};
|
||||||
|
|
||||||
|
auto request = makeRequest(Request::Type::SanityCheck, 1 + sizeof(PatternA), sizeof(PatternA));
|
||||||
|
|
||||||
|
request.payload[0] = 0x01u;
|
||||||
|
std::copy(std::begin(PatternA), std::end(PatternA), request.payload + 1);
|
||||||
|
|
||||||
|
if(!sendRequest(request, std::chrono::milliseconds(1000))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
request.payload[0] = 0x02u;
|
||||||
|
std::copy(std::begin(PatternB), std::end(PatternB), request.payload + 1);
|
||||||
|
|
||||||
|
return sendRequest(request, std::chrono::milliseconds(1000));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADCommunication::getBootloaderVersion() {
|
||||||
|
auto request = makeRequest(Request::Type::BootloaderVersion, 0, sizeof(BootloaderVersionResponse));
|
||||||
|
auto response = sendRequest(request, std::chrono::milliseconds(1000));
|
||||||
|
|
||||||
|
if(!response) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(response.size < sizeof(BootloaderVersionResponse)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
BootloaderVersionResponse version;
|
||||||
|
std::memcpy(&version, response.payload, sizeof(version));
|
||||||
|
targetChip.info.id = static_cast<ChipID>(version.zynqId);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADCommunication::validateChipVersion(const icsneo::DeviceAppVersion& chipSrcVer) {
|
||||||
|
if(!supportsZynqBinaryFlasher) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto request = makeRequest(Request::Type::ValidateVersion, 2, 2);
|
||||||
|
|
||||||
|
request.payload[0] = chipSrcVer.major;
|
||||||
|
request.payload[1] = chipSrcVer.minor;
|
||||||
|
|
||||||
|
return sendRequest(request, std::chrono::milliseconds(1000));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADCommunication::authenticate() {
|
||||||
|
icsbl_seed_v1 seed = {};
|
||||||
|
if(!icsbl_generate_seed_v1(device.getSerialNumber(), &seed)) {
|
||||||
|
Log::Error(__func__, "libicsbl could not generate an authentication challenge");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto request = makeRequest(Request::Type::Authenticate, sizeof(seed.a), sizeof(seed.b));
|
||||||
|
|
||||||
|
writeLE32(&request.payload[0], seed.a[0]);
|
||||||
|
writeLE32(&request.payload[4], seed.a[1]);
|
||||||
|
|
||||||
|
auto response = sendRequest(request, std::chrono::milliseconds(2000));
|
||||||
|
|
||||||
|
if(!response) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return seed.b[0] == readLE32(&response.payload[0]) && seed.b[1] == readLE32(&response.payload[4]);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADCommunication::sendSoftware(const std::vector<uint8_t>& chipSoftware) {
|
||||||
|
Log::Debug(__func__, "starting for " + targetChip.getName() + " (" + std::to_string(chipSoftware.size()) + " bytes)");
|
||||||
|
static constexpr size_t MaxEthernetPayload = 1487;
|
||||||
|
|
||||||
|
if(supportsZynqBinaryFlasher && chipSoftware.size() < sizeof(uint32_t)) {
|
||||||
|
Log::Error(__func__, "firmware too short to contain a CRC for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Log::Trace(__func__, "sending BeginTransfer for " + targetChip.getName());
|
||||||
|
auto request = makeRequest(Request::Type::BeginTransfer, 0, 0);
|
||||||
|
auto response = sendRequest(request, std::chrono::milliseconds(10000));
|
||||||
|
|
||||||
|
if(!response) {
|
||||||
|
Log::Error(__func__, "BeginTransfer failed for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const size_t headerSize = 1 + (supportsZynqBinaryFlasher ? ZynqFlasherHeaderSize : 0); // Command byte plus the optional Zynq flasher header
|
||||||
|
const size_t maxPayloadSize = MaxEthernetPayload - headerSize;
|
||||||
|
|
||||||
|
size_t remainingBytes = chipSoftware.size();
|
||||||
|
size_t currentlyReading = 0;
|
||||||
|
|
||||||
|
logger(
|
||||||
|
icsneo::Device::OpenStatusType::Progress,
|
||||||
|
"Flashing " + targetChip.getName() + "...",
|
||||||
|
std::nullopt
|
||||||
|
);
|
||||||
|
|
||||||
|
while(remainingBytes > 0) {
|
||||||
|
const double progress = static_cast<double>(currentlyReading) / static_cast<double>(chipSoftware.size());
|
||||||
|
|
||||||
|
logger(
|
||||||
|
icsneo::Device::OpenStatusType::Progress,
|
||||||
|
"Flashing " + targetChip.getName() + "...",
|
||||||
|
progress
|
||||||
|
);
|
||||||
|
|
||||||
|
size_t numToSend = std::min<size_t>(maxPayloadSize, remainingBytes);
|
||||||
|
request = makeRequest(Request::Type::FWPayload, numToSend, supportsZynqBinaryFlasher ? 2 : 0);
|
||||||
|
|
||||||
|
std::copy(
|
||||||
|
chipSoftware.begin() + currentlyReading,
|
||||||
|
chipSoftware.begin() + currentlyReading + numToSend,
|
||||||
|
request.payload
|
||||||
|
);
|
||||||
|
|
||||||
|
if(supportsFlashBuffer()) {
|
||||||
|
if(!sendRequest(request, std::chrono::milliseconds(2000))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else if(!sendRequestNoResponse()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
currentlyReading += numToSend;
|
||||||
|
remainingBytes -= numToSend;
|
||||||
|
if(device.com->driver->isEthernet()) {
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Log::Trace(__func__, "sending EndTransfer for " + targetChip.getName());
|
||||||
|
request = makeRequest(Request::Type::EndTransfer, 0, 0);
|
||||||
|
response = sendRequest(request, std::chrono::milliseconds(10000));
|
||||||
|
|
||||||
|
if(!response) {
|
||||||
|
Log::Error(__func__, "EndTransfer failed for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
request = makeRequest(Request::Type::DownloadedBinInfo, 0, 4);
|
||||||
|
response = sendRequest(request, std::chrono::milliseconds(6000));
|
||||||
|
|
||||||
|
if(!response) {
|
||||||
|
Log::Error(__func__, "DownloadedBinInfo failed for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const uint32_t numDownloaded = readLE32(response.payload);
|
||||||
|
|
||||||
|
if(numDownloaded != static_cast<uint32_t>(chipSoftware.size())) {
|
||||||
|
Log::Error(__func__, "downloaded size mismatch for " + targetChip.getName() + ": expected " + std::to_string(chipSoftware.size()) + ", got " + std::to_string(numDownloaded));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(supportsZynqBinaryFlasher) {
|
||||||
|
request = makeRequest(Request::Type::VerifyCRC, sizeof(uint32_t), 2);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copy CRC from end of the binary
|
||||||
|
*/
|
||||||
|
std::copy(chipSoftware.end() - sizeof(uint32_t), chipSoftware.end(), request.payload);
|
||||||
|
|
||||||
|
response = sendRequest(request, std::chrono::milliseconds(2000));
|
||||||
|
|
||||||
|
if(!response) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The first response byte is 1 when the CRC matched
|
||||||
|
if(response.payload[0] != 0x01) {
|
||||||
|
Log::Error(__func__, "CRC mismatch for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logger(
|
||||||
|
icsneo::Device::OpenStatusType::Progress,
|
||||||
|
"Flashing " + targetChip.getName() + "...",
|
||||||
|
1.0
|
||||||
|
);
|
||||||
|
|
||||||
|
Log::Debug(__func__, "completed successfully for " + targetChip.getName());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADCommunication::getProgrammingStatus(size_t sizeToValidate) {
|
||||||
|
Log::Debug(__func__, "starting for " + targetChip.getName());
|
||||||
|
auto request = makeRequest(Request::Type::ProgramFlash, 0, supportsZynqBinaryFlasher ? 2 : 0);
|
||||||
|
|
||||||
|
if(!sendRequest(request, std::chrono::milliseconds(2000))) {
|
||||||
|
Log::Error(__func__, "ProgramFlash failed for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
request = makeRequest(Request::Type::ProgrammingStatus, 0, 4);
|
||||||
|
|
||||||
|
uint32_t status = 0;
|
||||||
|
const auto beginTime = std::chrono::steady_clock::now();
|
||||||
|
|
||||||
|
logger(
|
||||||
|
icsneo::Device::OpenStatusType::Progress,
|
||||||
|
"Programming " + targetChip.getName() + "...",
|
||||||
|
std::nullopt
|
||||||
|
);
|
||||||
|
|
||||||
|
while(status < static_cast<uint32_t>(sizeToValidate)) {
|
||||||
|
const double progress = static_cast<double>(status) / static_cast<double>(sizeToValidate);
|
||||||
|
|
||||||
|
logger(
|
||||||
|
icsneo::Device::OpenStatusType::Progress,
|
||||||
|
"Programming " + targetChip.getName() + "...",
|
||||||
|
progress
|
||||||
|
);
|
||||||
|
|
||||||
|
const auto& response = sendRequest(request, std::chrono::milliseconds(60000));
|
||||||
|
if(!response) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
status = readLE32(response.payload);
|
||||||
|
|
||||||
|
if(std::chrono::steady_clock::now() - beginTime >= std::chrono::milliseconds(180000)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logger(
|
||||||
|
icsneo::Device::OpenStatusType::Progress,
|
||||||
|
"Programming " + targetChip.getName() + "...",
|
||||||
|
1.0
|
||||||
|
);
|
||||||
|
|
||||||
|
Log::Debug(__func__, "completed successfully for " + targetChip.getName());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADCommunication::loadZips(const FWRepo& fw, std::vector<uint8_t>& chipSoftware) {
|
||||||
|
if(const auto& segmentNames = targetChip.getMultiIEFName(); !segmentNames.empty()) {
|
||||||
|
std::vector<std::unique_ptr<FWZip>> zips;
|
||||||
|
zips.reserve(segmentNames.size());
|
||||||
|
|
||||||
|
size_t totalSize = 0;
|
||||||
|
for(const auto& name : segmentNames) {
|
||||||
|
std::vector<uint8_t> segment;
|
||||||
|
if(!fw.getFW(name, segment)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
zips.push_back(std::make_unique<FWZip>(std::move(segment)));
|
||||||
|
auto& zip = zips.back();
|
||||||
|
if(!zip->size()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
totalSize += *zip->size();
|
||||||
|
}
|
||||||
|
chipSoftware.resize(totalSize, 0);
|
||||||
|
size_t offset = 0;
|
||||||
|
for(auto& zip : zips) {
|
||||||
|
if(!zip->readAll(chipSoftware.data() + offset)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
offset += *(zip->size());
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
} else if(!targetChip.getIEFName().empty()) {
|
||||||
|
std::vector<uint8_t> bin;
|
||||||
|
if(!fw.getFW(targetChip.getIEFName(), bin)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
FWZip fwZip(std::move(bin));
|
||||||
|
if(!fwZip.readAll(chipSoftware)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// Should never happen
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADCommunication::flash(const FWRepo& fw, const icsneo::Device::OpenStatusHandler& handler) {
|
||||||
|
Log::Info(__func__, "starting flash for " + targetChip.getName());
|
||||||
|
auto chipSrcVer = fw.getChipVersion(targetChip.getIEFName());
|
||||||
|
if(!chipSrcVer) {
|
||||||
|
Log::Error(__func__, "no chip version found for " + targetChip.getIEFName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<uint8_t> chipSoftware;
|
||||||
|
if(!loadZips(fw, chipSoftware)) {
|
||||||
|
Log::Error(__func__, "loadZips failed for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
logger = handler;
|
||||||
|
if(!validateChipVersion(*chipSrcVer)) {
|
||||||
|
Log::Error(__func__, "validateChipVersion failed for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!sendSoftware(chipSoftware)) {
|
||||||
|
Log::Error(__func__, "sendSoftware failed for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!supportsFlashBuffer()) {
|
||||||
|
if(!getProgrammingStatus(chipSoftware.size())) {
|
||||||
|
Log::Error(__func__, "getProgrammingStatus failed for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Log::Info(__func__, "completed successfully for " + targetChip.getName());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
@@ -0,0 +1,144 @@
|
|||||||
|
#include "icsneo/device/extensions/bootloader/communication/radmultichipcommunication.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/parser/fwrepo.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/parser/ief.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/api.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/platform/logging.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
using namespace icsneo::Bootloader;
|
||||||
|
|
||||||
|
RADMultiChipCommunication::RADMultiChipCommunication(icsneo::Device& dev, const ChipInfo& chipToFlash, bool auth, uint8_t slot)
|
||||||
|
: RADCommunication(dev, chipToFlash), doAuthChecks(auth), slot(slot) {}
|
||||||
|
|
||||||
|
bool RADMultiChipCommunication::authenticate() {
|
||||||
|
if(!doAuthChecks) {
|
||||||
|
Log::Trace(__func__, "auth checks disabled, skipping");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
Log::Debug(__func__, "starting authentication");
|
||||||
|
if(!standardComm->authenticate()) {
|
||||||
|
Log::Warning(__func__, "standardComm auth failed, retrying with sanity check");
|
||||||
|
device.com->pauseReads();
|
||||||
|
if(!sanityCheck()) {
|
||||||
|
if(!sanityCheck()) {
|
||||||
|
device.com->resumeReads();
|
||||||
|
Log::Error(__func__, "sanity check failed after retries");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!RADCommunication::authenticate()) {
|
||||||
|
device.com->resumeReads();
|
||||||
|
Log::Error(__func__, "RADCommunication auth failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
device.com->resumeReads();
|
||||||
|
}
|
||||||
|
Log::Debug(__func__, "authentication successful");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADMultiChipCommunication::flash(const FWRepo& fw, const icsneo::Device::OpenStatusHandler& handler) {
|
||||||
|
Log::Info(__func__, "starting flash for " + targetChip.getName());
|
||||||
|
auto chipSrcVer = fw.getChipVersion(targetChip.getIEFName());
|
||||||
|
if(!chipSrcVer) {
|
||||||
|
Log::Error(__func__, "no chip version found for " + targetChip.getIEFName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!authenticate()) {
|
||||||
|
Log::Error(__func__, "authentication failed for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!standardComm->validateFirmware(*chipSrcVer)) {
|
||||||
|
Log::Error(__func__, "firmware validation failed for " + targetChip.getName());
|
||||||
|
handler(icsneo::Device::OpenStatusType::Failed, "Failed to validate firmware", std::nullopt);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(targetChip.getFirmwareType() == FirmwareType::Zip) {
|
||||||
|
std::vector<uint8_t> chipSoftware;
|
||||||
|
if(!loadZips(fw, chipSoftware)) {
|
||||||
|
Log::Error(__func__, "loadZips failed for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!standardComm->sendSoftware(chipSoftware, handler)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
std::vector<uint8_t> bin;
|
||||||
|
if(!fw.getFW(targetChip.getIEFName(), bin)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
IEF ief;
|
||||||
|
if(!ief.load(std::move(bin))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The supplied IEF is not for this product
|
||||||
|
if(ief.getVersion().prodId != static_cast<uint8_t>(device.getProductID()) && !isSFPIEF(ief)) {
|
||||||
|
Log::Error(__func__, "IEF product ID mismatch for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The supplied IEF is for this product, but not for this chip
|
||||||
|
if(ief.getVersion().chipId != static_cast<uint8_t>(targetChip.getID())) {
|
||||||
|
Log::Error(__func__, "IEF chip ID mismatch for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!standardComm->sendSoftware(ief.getSectors(), ief.getBlockSize(), handler)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Log::Debug(__func__, "calling finalize");
|
||||||
|
if(!standardComm->finalize()) {
|
||||||
|
Log::Error(__func__, "finalize failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Log::Debug(__func__, "finalize successful, proceeding to reboot");
|
||||||
|
handler(icsneo::Device::OpenStatusType::Progress, "Rebooting device...", std::nullopt);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADMultiChipCommunication::enterApplication() {
|
||||||
|
Log::Info(__func__, "starting");
|
||||||
|
if(!standardComm->enterApplication()) {
|
||||||
|
Log::Error(__func__, "reboot failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Log::Debug(__func__, "reboot sent, waiting 5000ms");
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(5000));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADMultiChipCommunication::open() {
|
||||||
|
const auto& components = device.getComponentVersions();
|
||||||
|
auto component = std::find_if(components.begin(), components.end(), [this](const auto& candidate) {
|
||||||
|
if(slot != UINT8_MAX && slot != candidate.expansionSlot) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return targetChip.getID() == (ChipID)candidate.identifier;
|
||||||
|
});
|
||||||
|
if(component == components.end()) {
|
||||||
|
Log::Error(__func__, "no component found for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
standardComm = std::make_unique<Application>(device, targetChip.info);
|
||||||
|
standardComm->setExpansionSlot(component->expansionSlot);
|
||||||
|
// Pause the read thread so it doesn't consume any responses
|
||||||
|
device.com->pauseReads();
|
||||||
|
const bool res = initialize();
|
||||||
|
device.com->resumeReads();
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADMultiChipCommunication::close() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
@@ -0,0 +1,334 @@
|
|||||||
|
#include "icsneo/device/extensions/bootloader/communication/radperiphcommunication.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/parser/fwrepo.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/platform/logging.h"
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
#include <icsbl/icsbl.h>
|
||||||
|
|
||||||
|
using namespace icsneo::Bootloader;
|
||||||
|
|
||||||
|
RADPeriphCommunication::RADPeriphCommunication(icsneo::Device& dev, const ChipInfo& chipToFlash)
|
||||||
|
: Communication(dev), targetChip(chipToFlash) {}
|
||||||
|
|
||||||
|
RADPeriphCommunication::~RADPeriphCommunication() {
|
||||||
|
if(device.com->readsArePaused()) {
|
||||||
|
device.com->resumeReads();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADPeriphCommunication::sendRequestNoResponse(Request req, std::vector<uint8_t>&& args) {
|
||||||
|
return sendRequestNoResponse(req, targetChip.getID(), std::move(args));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADPeriphCommunication::sendRequestNoResponse(Request req, ChipID id, std::vector<uint8_t>&& args) {
|
||||||
|
args.insert(args.begin(), {
|
||||||
|
(uint8_t)id,
|
||||||
|
(uint8_t)req,
|
||||||
|
});
|
||||||
|
return device.com->sendCommand(icsneo::Command::PeripheralBootloader, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
RADPeriphCommunication::Response RADPeriphCommunication::sendRequest(Request req, std::vector<uint8_t>&& args, std::chrono::milliseconds timeout) {
|
||||||
|
return sendRequest(req, targetChip.getID(), std::move(args), timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
RADPeriphCommunication::Response RADPeriphCommunication::sendRequest(Request req, ChipID id, std::vector<uint8_t>&& args, std::chrono::milliseconds timeout) {
|
||||||
|
// Total reply sizes, including the header bytes
|
||||||
|
size_t expectedResponseLength = 0;
|
||||||
|
bool checkBlAccepted = false;
|
||||||
|
switch(req) {
|
||||||
|
case Request::Mode:
|
||||||
|
expectedResponseLength = 6;
|
||||||
|
break;
|
||||||
|
case Request::Version:
|
||||||
|
expectedResponseLength = 24;
|
||||||
|
checkBlAccepted = true;
|
||||||
|
break;
|
||||||
|
case Request::Authenticate:
|
||||||
|
expectedResponseLength = 13;
|
||||||
|
checkBlAccepted = true;
|
||||||
|
break;
|
||||||
|
case Request::Echo:
|
||||||
|
expectedResponseLength = 3;
|
||||||
|
break;
|
||||||
|
case Request::Erase:
|
||||||
|
case Request::FirmwareValidate:
|
||||||
|
case Request::FlashSector:
|
||||||
|
expectedResponseLength = 3;
|
||||||
|
checkBlAccepted = true;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
expectedResponseLength = 3;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto& rb = device.com->driver->getReadBuffer();
|
||||||
|
rb.clear();
|
||||||
|
if(!sendRequestNoResponse(req, id, std::move(args))) {
|
||||||
|
Log::Error(__func__, "Failed to send request " + std::to_string(static_cast<uint8_t>(req)));
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
auto result = device.com->driver->waitForRx(expectedResponseLength, timeout);
|
||||||
|
if(!result) {
|
||||||
|
Log::Error(__func__, "No response to request " + std::to_string(static_cast<uint8_t>(req)));
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
Response resp;
|
||||||
|
resp.data.resize(expectedResponseLength, 0);
|
||||||
|
rb.read(resp.data.data(), 0, expectedResponseLength);
|
||||||
|
resp.success = resp.data[ReplySuccessOffset] == ReplySuccess;
|
||||||
|
if(!resp.success) {
|
||||||
|
Log::Error(__func__, "The peripheral reported failure for request " + std::to_string(static_cast<uint8_t>(req)));
|
||||||
|
return resp;
|
||||||
|
}
|
||||||
|
if(checkBlAccepted && resp.data[ReplyAcceptedOffset] != ReplyAccepted) {
|
||||||
|
Log::Error(__func__, "The bootloader rejected request " + std::to_string(static_cast<uint8_t>(req)));
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp;
|
||||||
|
}
|
||||||
|
|
||||||
|
RADPeriphCommunication::Mode RADPeriphCommunication::getMode() {
|
||||||
|
auto resp = sendRequest(Request::Mode, {}, std::chrono::milliseconds(2000));
|
||||||
|
|
||||||
|
if(!resp) {
|
||||||
|
return Mode::NoAnswer;
|
||||||
|
}
|
||||||
|
static constexpr size_t ModeOffset = 4;
|
||||||
|
return (Mode)resp.data[ModeOffset];
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADPeriphCommunication::sendPeriphTest() {
|
||||||
|
// Echo is sent to chip id 0, the reply carries the bitwise inverse of the test byte
|
||||||
|
const uint8_t testByte = 0x00;
|
||||||
|
auto resp = sendRequest(Request::Echo, (ChipID)0x00, {testByte}, std::chrono::milliseconds(2000));
|
||||||
|
|
||||||
|
if(!resp) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(resp.data[2] != static_cast<uint8_t>(~testByte)) {
|
||||||
|
Log::Error(__func__, "The peripheral echo reply did not match");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADPeriphCommunication::readSerialNumber() {
|
||||||
|
auto resp = sendRequest(Request::Version, {0x00}, std::chrono::milliseconds(2000));
|
||||||
|
if(!resp) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The reply payload is the start of a BootloaderVersionResponse
|
||||||
|
static constexpr size_t SerialOffset = ReplyHeaderSize + offsetof(BootloaderVersionResponse, serialNum);
|
||||||
|
serialNumber = readLE32(resp.data.data() + SerialOffset);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADPeriphCommunication::authenticate() {
|
||||||
|
icsbl_seed_v1 seed = {};
|
||||||
|
if(!icsbl_generate_seed_v1(serialNumber, &seed)) {
|
||||||
|
Log::Error(__func__, "libicsbl could not generate an authentication challenge");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<uint8_t> args(BootloaderArgsOffset + sizeof(seed.a), 0);
|
||||||
|
writeLE32(args.data() + BootloaderArgsOffset, seed.a[0]);
|
||||||
|
writeLE32(args.data() + BootloaderArgsOffset + 4, seed.a[1]);
|
||||||
|
|
||||||
|
auto resp = sendRequest(Request::Authenticate, std::move(args), std::chrono::milliseconds(3000));
|
||||||
|
if(!resp) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// seed.b follows the header and one more byte
|
||||||
|
static constexpr size_t ReplyOffset = ReplyHeaderSize + 1;
|
||||||
|
return readLE32(resp.data.data() + ReplyOffset) == seed.b[0] &&
|
||||||
|
readLE32(resp.data.data() + ReplyOffset + 4) == seed.b[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADPeriphCommunication::validateFirmware() {
|
||||||
|
return sendRequest(Request::FirmwareValidate, {0x00u}, std::chrono::milliseconds(2000));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADPeriphCommunication::flashSector(const IEF::Sector& sector) {
|
||||||
|
if(sector.second.size() < FlashSectorFirstChunkSize) {
|
||||||
|
Log::Error(__func__, "Invalid sector length");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The first request is the 32-bit little-endian address followed by the first chunk, encrypted
|
||||||
|
std::vector<uint8_t> args(BootloaderArgsOffset + sizeof(uint32_t) + FlashSectorFirstChunkSize, 0);
|
||||||
|
writeLE32(args.data() + BootloaderArgsOffset, sector.first);
|
||||||
|
icsbl_encrypt_v1(serialNumber, sector.second.begin(), FlashSectorFirstChunkSize, args.data() + BootloaderArgsOffset + sizeof(uint32_t));
|
||||||
|
if(!sendRequestNoResponse(Request::FlashSector, std::move(args))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(400));
|
||||||
|
|
||||||
|
// The second request is the rest of the sector, encrypted
|
||||||
|
const size_t bytesLeft = sector.second.size() - FlashSectorFirstChunkSize;
|
||||||
|
args = std::vector<uint8_t>(bytesLeft, 0);
|
||||||
|
icsbl_encrypt_v1(serialNumber, sector.second.begin() + FlashSectorFirstChunkSize, bytesLeft, args.data());
|
||||||
|
return sendRequest(Request::FlashSector, std::move(args), std::chrono::milliseconds(3000));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADPeriphCommunication::flash(const FWRepo& fw, const icsneo::Device::OpenStatusHandler& handler) {
|
||||||
|
// Validate the firmware before touching the device so a bad file can not leave it erased
|
||||||
|
std::vector<uint8_t> bin;
|
||||||
|
if(!fw.getFW(targetChip.getIEFName(), bin)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
IEF ief;
|
||||||
|
if(!ief.load(std::move(bin))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The supplied IEF is not for this product
|
||||||
|
if(ief.getVersion().prodId != static_cast<uint8_t>(device.getProductID())) {
|
||||||
|
Log::Error(__func__, "IEF product ID mismatch for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The supplied IEF is for this product, but not for this chip
|
||||||
|
if(ief.getVersion().chipId != static_cast<uint8_t>(targetChip.getID())) {
|
||||||
|
Log::Error(__func__, "IEF chip ID mismatch for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto& sectors = ief.getSectors();
|
||||||
|
uint32_t totalSize = 0;
|
||||||
|
for(const auto& sector : sectors) {
|
||||||
|
if(sector.second.size() < FlashSectorFirstChunkSize) {
|
||||||
|
Log::Error(__func__, "Invalid sector length in IEF for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
totalSize += static_cast<uint32_t>(sector.second.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!readSerialNumber()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!authenticate()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!sendRequest(Request::Erase)) {
|
||||||
|
Log::Error(__func__, "Failed to erase firmware");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
handler(
|
||||||
|
icsneo::Device::OpenStatusType::Progress,
|
||||||
|
"Flashing " + targetChip.getName() + "...",
|
||||||
|
std::nullopt
|
||||||
|
);
|
||||||
|
|
||||||
|
uint32_t sizeFlashed = 0;
|
||||||
|
|
||||||
|
for(const auto& sector : sectors) {
|
||||||
|
if(!flashSector(sector)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
sizeFlashed += static_cast<uint32_t>(sector.second.size());
|
||||||
|
handler(
|
||||||
|
icsneo::Device::OpenStatusType::Progress,
|
||||||
|
"Flashing " + targetChip.getName() + "...",
|
||||||
|
(double)sizeFlashed / (double)totalSize
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return validateFirmware();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADPeriphCommunication::waitForConnect(std::chrono::milliseconds timeout) {
|
||||||
|
const auto deadline = std::chrono::steady_clock::now() + timeout;
|
||||||
|
while(std::chrono::steady_clock::now() < deadline) {
|
||||||
|
if(sendPeriphTest()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADPeriphCommunication::open() {
|
||||||
|
device.com->pauseReads();
|
||||||
|
|
||||||
|
int attempts = 3;
|
||||||
|
do {
|
||||||
|
mode = getMode();
|
||||||
|
if(mode == Mode::NoAnswer) {
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||||
|
mode = getMode();
|
||||||
|
if(mode == Mode::NoAnswer) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(mode == Mode::MainApplication) {
|
||||||
|
if(!sendRequestNoResponse(Request::SwitchToBootloader)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(!device.reconnect(std::chrono::seconds(30))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(!waitForConnect(std::chrono::seconds(30))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else if(mode == Mode::Bootloader) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} while(attempts--);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADPeriphCommunication::close() {
|
||||||
|
if(mode != Mode::Bootloader) {
|
||||||
|
device.com->resumeReads();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if(!sendRequestNoResponse(Request::SwitchToApp)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::this_thread::sleep_for(std::chrono::seconds(5)); // measured at 2s, 5s for buffer, yes this is needed
|
||||||
|
|
||||||
|
if(!device.reconnect(std::chrono::seconds(30))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!waitForConnect()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
mode = getMode();
|
||||||
|
if(mode == Mode::MainApplication) {
|
||||||
|
device.com->resumeReads();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADPeriphCommunication::enterApplication() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADPeriphCommunication::finalize() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RADPeriphCommunication::hasFinalizationStep() const {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
@@ -0,0 +1,244 @@
|
|||||||
|
#include "icsneo/device/extensions/bootloader/communication/red.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/parser/ief.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/parser/fwrepo.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/platform/logging.h"
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
|
#include <icsbl/icsbl.h>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
REDCommunication::REDCommunication(icsneo::Device& dev, const ChipInfo& chip, bool auth, bool encryptWithDeviceKey)
|
||||||
|
: REDCommunicationBase(dev, chip), doAuthChecks(auth), encryptFirmwareWithDeviceKey(encryptWithDeviceKey) {}
|
||||||
|
|
||||||
|
bool REDCommunication::open() {
|
||||||
|
device.com->pauseReads();
|
||||||
|
|
||||||
|
if(doAuthChecks) {
|
||||||
|
if(!authenticate()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return getVersion();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool REDCommunication::authenticate(bool firstTime) {
|
||||||
|
icsbl_seed_v1 seed = {};
|
||||||
|
if(!icsbl_generate_seed_v1(device.getSerialNumber(), &seed)) {
|
||||||
|
Log::Error(__func__, "libicsbl could not generate an authentication challenge");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The challenge is sent as: uint8_t 0x09, uint16_t 0 (a length on top of the protocol length, always zero here),
|
||||||
|
// one padding byte, then seed.a
|
||||||
|
std::vector<uint8_t> challenge(3 + sizeof(seed.a));
|
||||||
|
challenge[0] = 0x09;
|
||||||
|
std::memcpy(&challenge[3], &seed.a, sizeof(seed.a));
|
||||||
|
|
||||||
|
// Ask the bootloader to decode the seed
|
||||||
|
Log::Debug(__func__, "Authenticating");
|
||||||
|
if(!sendCommand(uint8_t(Command::Authenticate), challenge, std::chrono::seconds(1), false)) {
|
||||||
|
if(firstTime) {
|
||||||
|
// Attempt to flush out any buffers
|
||||||
|
Log::Debug(__func__, "Authentication failed, flushing buffers");
|
||||||
|
std::vector<uint8_t> flush(4096, PaddingByte);
|
||||||
|
device.com->sendPacket(flush);
|
||||||
|
|
||||||
|
// Allow any failure messages to come back
|
||||||
|
const bool any = waitForRx(std::chrono::milliseconds(100));
|
||||||
|
Log::Trace(__func__, any ? "A message came back" : "No communication came back");
|
||||||
|
|
||||||
|
lastData.clear();
|
||||||
|
|
||||||
|
// Now try again
|
||||||
|
return authenticate(false); // Either way, we don't need to continue authenticating
|
||||||
|
} else {
|
||||||
|
Log::Error(__func__, "The bootloader rejected the authentication challenge twice");
|
||||||
|
enterApplication();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The reply is one byte followed by seed.b
|
||||||
|
if(lastData.back().data.size() < 1 + sizeof(seed.b)) {
|
||||||
|
Log::Error(__func__, "The authentication reply is too short");
|
||||||
|
enterApplication();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(std::memcmp(&lastData.back().data[1], &seed.b, sizeof(seed.b)) != 0) {
|
||||||
|
Log::Error(__func__, "The authentication reply does not match, the device did not authenticate");
|
||||||
|
enterApplication();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
lastData.clear();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool REDCommunication::getVersion() {
|
||||||
|
if(!sendCommand(uint8_t(Command::GetVersion), std::chrono::seconds(1), false)) {
|
||||||
|
Log::Error(__func__, "The bootloader did not answer the version request");
|
||||||
|
enterApplication();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(lastData.front().data.size() < VersionResponse::EncodedSize) {
|
||||||
|
Log::Error(__func__, "The bootloader version reply is too short");
|
||||||
|
enterApplication();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
currentVersion = VersionResponse(lastData.front());
|
||||||
|
lastData.clear();
|
||||||
|
|
||||||
|
if(doAuthChecks) {
|
||||||
|
// Check that the serial number the bootloader reports is correct
|
||||||
|
uint8_t correctSerial[sizeof(VersionResponse::serial_num)] = {};
|
||||||
|
uint32_t devSerial = device.getSerialNumber();
|
||||||
|
std::memcpy(correctSerial, &devSerial, sizeof(devSerial));
|
||||||
|
if(std::memcmp(correctSerial, currentVersion.serial_num, sizeof(correctSerial))) {
|
||||||
|
Log::Error(__func__, "The serial number reported by the bootloader does not match the device");
|
||||||
|
enterApplication();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
blockSize = currentVersion.block_size;
|
||||||
|
device.setBootloaderVersion({currentVersion.bl_major, currentVersion.bl_minor});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool REDCommunication::flash(const FWRepo& fw, const icsneo::Device::OpenStatusHandler& handler) {
|
||||||
|
const auto& srcVer = fw.getVersion();
|
||||||
|
|
||||||
|
std::vector<uint8_t> bin;
|
||||||
|
if(!fw.getFW(targetChip.getIEFName(), bin)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
IEF ief;
|
||||||
|
if(!ief.load(std::move(bin))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The supplied IEF is not for this product
|
||||||
|
if(ief.getVersion().prodId != static_cast<uint8_t>(device.getProductID())) {
|
||||||
|
Log::Error(__func__, "IEF product ID mismatch for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The supplied IEF is for this product, but not for this chip
|
||||||
|
if(ief.getVersion().chipId != static_cast<uint8_t>(targetChip.getID())) {
|
||||||
|
Log::Error(__func__, "IEF chip ID mismatch for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Log::Debug(__func__, "IEF version " + std::to_string(ief.getVersion().appMajor) + "." + std::to_string(ief.getVersion().appMinor)
|
||||||
|
+ ", " + std::to_string(ief.getSectors().size()) + " sectors");
|
||||||
|
|
||||||
|
if(doAuthChecks && device.supportsSwVersionValidate()) {
|
||||||
|
// Tell the bootloader what version of Vehicle Spy 3 this firmware is from.
|
||||||
|
// We check to make sure it approves, but also, some devices will not accept
|
||||||
|
// new firmware if we have not sent this.
|
||||||
|
// Sometimes new hardware revisions can not accept firmware which is too old.
|
||||||
|
if(!sendCommand(uint8_t(Command::ValidateSWVersion), {srcVer.major, srcVer.minor, srcVer.release, srcVer.build})) {
|
||||||
|
Log::Error(__func__, "SW version validation failed for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
handler(icsneo::Device::OpenStatusType::Progress,
|
||||||
|
"Preparing to update " + targetChip.getName() + "...", std::nullopt);
|
||||||
|
Log::Debug(__func__, "Erasing " + targetChip.getName());
|
||||||
|
if(!sendCommand(uint8_t(Command::Erase), std::chrono::seconds(45))) {
|
||||||
|
Log::Error(__func__, "Erase failed for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto& sectorPairs = ief.getSectors();
|
||||||
|
size_t sectorsFlashed = 0;
|
||||||
|
for(const auto& [id, sector] : sectorPairs) {
|
||||||
|
Log::Trace(__func__, "Flashing sector " + std::to_string(id));
|
||||||
|
handler(icsneo::Device::OpenStatusType::Progress,
|
||||||
|
"Updating " + targetChip.getName() + "...",
|
||||||
|
double(sectorsFlashed++) / sectorPairs.size());
|
||||||
|
|
||||||
|
if(sector.size() > blockSize) {
|
||||||
|
Log::Error(__func__, "Sector " + std::to_string(id) + " too large");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sectors smaller than the block size are padded out with zeros
|
||||||
|
std::vector<uint8_t> padded(sector.begin(), sector.end());
|
||||||
|
padded.resize(blockSize, 0);
|
||||||
|
|
||||||
|
std::vector<uint8_t> encrypted(padded.size());
|
||||||
|
if(encryptFirmwareWithDeviceKey)
|
||||||
|
icsbl_encrypt_v1(device.getSerialNumber(), padded.data(), padded.size(), encrypted.data());
|
||||||
|
else
|
||||||
|
encrypted = std::move(padded);
|
||||||
|
|
||||||
|
encrypted.insert(encrypted.begin(), {
|
||||||
|
0x00, 0x00, 0x00, // Padding bytes
|
||||||
|
// Address
|
||||||
|
uint8_t((id >> 0x00) & 0xFF),
|
||||||
|
uint8_t((id >> 0x08) & 0xFF),
|
||||||
|
uint8_t((id >> 0x10) & 0xFF),
|
||||||
|
uint8_t((id >> 0x18) & 0xFF),
|
||||||
|
});
|
||||||
|
|
||||||
|
// Unencrypted firmware is used for FIRE2 Zynq (and VNET) which also uses the top bit of the ID
|
||||||
|
// to denote whether the sector is flashed to chip A or chip B. That is indicated
|
||||||
|
// with a 1 (for chip B) or a 0 as the first byte of the flash packet payload.
|
||||||
|
if(!encryptFirmwareWithDeviceKey)
|
||||||
|
encrypted.insert(encrypted.begin(), uint8_t(id & (1u << 31) ? 1 : 0));
|
||||||
|
|
||||||
|
if(!sendCommand(uint8_t(Command::Flash), encrypted)) {
|
||||||
|
Log::Error(__func__, "Flash failed at sector " + std::to_string(id) + " for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tell the application that we flashed 100%
|
||||||
|
handler(icsneo::Device::OpenStatusType::Progress,
|
||||||
|
"Updating " + targetChip.getName() + "...",
|
||||||
|
1.0);
|
||||||
|
|
||||||
|
handler(icsneo::Device::OpenStatusType::Progress, "Finalizing update...", std::nullopt);
|
||||||
|
if(!sendCommand(uint8_t(Command::ValidateFirmware))) {
|
||||||
|
Log::Error(__func__, "Firmware validation failed for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
REDCommunication::VersionResponse::VersionResponse(const REDPacketizer::RxFrame& r) {
|
||||||
|
auto i = r.data.begin();
|
||||||
|
bl_major = *i++;
|
||||||
|
bl_minor = *i++;
|
||||||
|
app_major = *i++;
|
||||||
|
app_minor = *i++;
|
||||||
|
hardware_rev_major = *i++;
|
||||||
|
hardware_rev_minor = *i++;
|
||||||
|
flags = *i++;
|
||||||
|
block_size = *i++;
|
||||||
|
block_size |= uint16_t(*i++) << 8;
|
||||||
|
man_day = *i++;
|
||||||
|
man_month = *i++;
|
||||||
|
man_year = *i++;
|
||||||
|
man_year |= uint16_t(*i++) << 8;
|
||||||
|
for(size_t x = 0; x != sizeof(serial_num); ++x)
|
||||||
|
serial_num[x] = *i++;
|
||||||
|
prod_id = *i++;
|
||||||
|
last_error_code = *i++;
|
||||||
|
pad_byte = *i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
#include "icsneo/device/extensions/bootloader/communication/redbase.h"
|
||||||
|
|
||||||
|
#include <limits>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
REDCommunicationBase::REDCommunicationBase(icsneo::Device& dev, const ChipInfo& chip)
|
||||||
|
: Communication(dev), targetChip(chip) {}
|
||||||
|
|
||||||
|
bool REDCommunicationBase::open() {
|
||||||
|
device.com->pauseReads();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool REDCommunicationBase::close() {
|
||||||
|
device.com->resumeReads();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool REDCommunicationBase::handleData() {
|
||||||
|
auto& rb = device.com->driver->getReadBuffer();
|
||||||
|
|
||||||
|
if(rp.input(rb)) {
|
||||||
|
lastData = rp.output();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool REDCommunicationBase::waitForRx(std::chrono::milliseconds timeout) {
|
||||||
|
return device.com->driver->waitForRx([this]() { return handleData(); }, timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool REDCommunicationBase::sendCommand(uint8_t cmd, std::vector<uint8_t> args, std::chrono::seconds timeout, bool clearLastResponse) {
|
||||||
|
lastData.clear();
|
||||||
|
device.com->driver->getReadBuffer().clear();
|
||||||
|
|
||||||
|
if(!sendCommandRaw(cmd, std::move(args)))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if(!waitForRx(std::chrono::duration_cast<std::chrono::milliseconds>(timeout)))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
const bool ret = lastData.front().resp == REDPacketizer::Response::Accepted;
|
||||||
|
|
||||||
|
if(clearLastResponse) {
|
||||||
|
lastData.clear();
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool REDCommunicationBase::sendCommandRaw(uint8_t cmd, std::vector<uint8_t> args) {
|
||||||
|
// For some reason, some devices need it padded to even total bytes, and some to odd total bytes
|
||||||
|
const bool padEven = device.getType() == DeviceType::FIRE2;
|
||||||
|
|
||||||
|
args.insert(args.begin(), uint8_t(cmd)); // Prepend the command byte to the arguments
|
||||||
|
if(args.size() > std::numeric_limits<uint16_t>::max())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
uint16_t size = uint16_t(args.size());
|
||||||
|
// The header counts, and the header is 5 bytes, so we're checking for even inner size here
|
||||||
|
const bool padding = size % 2 != (padEven ? 0 : 1);
|
||||||
|
if(padding) // We will add padding, so make space for it
|
||||||
|
size++;
|
||||||
|
|
||||||
|
// Prepend the magic, 16-bit little-endian chip ID, and 16-bit little-endian size
|
||||||
|
const uint16_t chipID = uint16_t(targetChip.getID());
|
||||||
|
args.insert(args.begin(), {
|
||||||
|
REDPacketizer::Magic,
|
||||||
|
uint8_t(chipID & 0xFF), uint8_t(chipID >> 8),
|
||||||
|
uint8_t(size & 0xFF), uint8_t(size >> 8)
|
||||||
|
});
|
||||||
|
|
||||||
|
if(padding)
|
||||||
|
args.push_back(PaddingByte); // Ignored by the firmware
|
||||||
|
|
||||||
|
// Write it to the device
|
||||||
|
return device.com->sendPacket(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
@@ -0,0 +1,111 @@
|
|||||||
|
#include "icsneo/device/extensions/bootloader/communication/redcore.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/parser/fwrepo.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/platform/logging.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <limits>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
bool REDCoreCommunication::flash(const FWRepo& fw, const icsneo::Device::OpenStatusHandler& handler) {
|
||||||
|
constexpr const size_t MaxSectorSize = 0x600;
|
||||||
|
|
||||||
|
std::vector<uint8_t> bin;
|
||||||
|
if(!fw.getFW(targetChip.getIEFName(), bin)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(bin.size() > std::numeric_limits<uint32_t>::max()) {
|
||||||
|
Log::Error(__func__, "Firmware too large for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string message = "Updating " + targetChip.getName() + "...";
|
||||||
|
|
||||||
|
// First send the firmware
|
||||||
|
const uint32_t fileSize = uint32_t(bin.size());
|
||||||
|
size_t sectorsFlashed = 0;
|
||||||
|
const size_t sectorCount = (fileSize / MaxSectorSize) + (fileSize % MaxSectorSize ? 1 : 0);
|
||||||
|
std::vector<uint8_t> sector;
|
||||||
|
for(size_t i = 0; i < fileSize; i += MaxSectorSize) {
|
||||||
|
Log::Trace(__func__, "Sending sector " + std::to_string(sectorsFlashed));
|
||||||
|
handler(icsneo::Device::OpenStatusType::Progress, message, double(sectorsFlashed++) / sectorCount);
|
||||||
|
|
||||||
|
sector.assign(bin.begin() + i, bin.begin() + i + std::min<size_t>(MaxSectorSize, fileSize - i));
|
||||||
|
if(!sendCommand(uint8_t(Command::SendFirmware), sector)) {
|
||||||
|
Log::Error(__func__, "Failed to send sector " + std::to_string(sectorsFlashed - 1) + " for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then flash it
|
||||||
|
Log::Debug(__func__, "Starting core flash for " + targetChip.getName());
|
||||||
|
handler(icsneo::Device::OpenStatusType::Progress, message, std::nullopt);
|
||||||
|
|
||||||
|
// There is a long timeout on the StartFirmwareFlash because the bootloader may block here,
|
||||||
|
// potentially for the entire duration of the flash
|
||||||
|
if(!sendCommand(uint8_t(Command::StartFirmwareFlash), {
|
||||||
|
uint8_t(fileSize & 0xFF), uint8_t((fileSize >> 8) & 0xFF),
|
||||||
|
uint8_t((fileSize >> 16) & 0xFF), uint8_t((fileSize >> 24) & 0xFF),
|
||||||
|
}, std::chrono::seconds(60))) {
|
||||||
|
Log::Error(__func__, "Failed to start core flash for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Watch the responses to know when it's done
|
||||||
|
const auto endTime = std::chrono::steady_clock::now() + std::chrono::seconds(60);
|
||||||
|
uint32_t flashed = 0;
|
||||||
|
while(flashed < fileSize && std::chrono::steady_clock::now() < endTime) {
|
||||||
|
if(!sendCommandRaw(uint8_t(Command::GetFlashProgress))) {
|
||||||
|
Log::Error(__func__, "Failed to request flash progress for " + targetChip.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!waitForRx(std::chrono::milliseconds(3000))) {
|
||||||
|
Log::Trace(__func__, "No flash progress packet within 3 seconds");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const uint32_t oldFlashed = flashed;
|
||||||
|
for(const auto& packet : lastData) {
|
||||||
|
if(packet.resp != REDPacketizer::Response::Accepted)
|
||||||
|
return false;
|
||||||
|
if(packet.data.size() != 4) {
|
||||||
|
Log::Trace(__func__, "Got a flash progress packet of length " + std::to_string(packet.data.size()));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const uint32_t newFlashed = readLE32(packet.data.data());
|
||||||
|
if(newFlashed >= flashed)
|
||||||
|
flashed = newFlashed;
|
||||||
|
else
|
||||||
|
Log::Trace(__func__, "Got a flash progress packet which went backwards " + std::to_string(newFlashed) + " " + std::to_string(flashed));
|
||||||
|
|
||||||
|
// After sending us the packet that confirms the flash, the flash position is cleared and will send us zeros.
|
||||||
|
// We don't need any of these other packets, in that case.
|
||||||
|
if(flashed >= fileSize)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
lastData.clear();
|
||||||
|
|
||||||
|
Log::Trace(__func__, "Flashed " + std::to_string(flashed) + " of " + std::to_string(fileSize));
|
||||||
|
|
||||||
|
if(flashed != oldFlashed)
|
||||||
|
handler(icsneo::Device::OpenStatusType::Progress, message, std::min(double(flashed) / fileSize, 1.0));
|
||||||
|
}
|
||||||
|
|
||||||
|
return flashed == fileSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool REDCoreCommunication::sendCommandRaw(uint8_t cmd, std::vector<uint8_t> args) {
|
||||||
|
if(args.size() > std::numeric_limits<uint16_t>::max())
|
||||||
|
return false;
|
||||||
|
const uint16_t size = static_cast<uint16_t>(args.size());
|
||||||
|
args.insert(args.begin(), { CoreFlasherMagic, uint8_t(size & 0xFF), uint8_t((size >> 8) & 0xFF), cmd });
|
||||||
|
return REDCommunicationBase::sendCommandRaw(CoreFlasherCommand, std::move(args));
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
#include "icsneo/device/extensions/bootloader/communication/redpacketizer.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/platform/logging.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
bool REDPacketizer::input(icsneo::RingBuffer& bytes) {
|
||||||
|
while(bytes.size() > 0) {
|
||||||
|
switch(state) {
|
||||||
|
case HEADER:
|
||||||
|
if(bytes[0] == Magic) {
|
||||||
|
working.emplace_back();
|
||||||
|
state = CMD;
|
||||||
|
} else {
|
||||||
|
Log::Trace(__func__, "Got bootloader unknown byte: " + std::to_string(bytes[0]));
|
||||||
|
}
|
||||||
|
bytes.pop_front();
|
||||||
|
break;
|
||||||
|
case CMD:
|
||||||
|
working.back().resp = Response(bytes[0]);
|
||||||
|
if(working.back().resp != Response::Accepted && working.back().resp != Response::Rejected) {
|
||||||
|
Log::Trace(__func__, "Got bootloader unknown response: " + std::to_string(bytes[0]));
|
||||||
|
working.pop_back();
|
||||||
|
state = HEADER;
|
||||||
|
} else {
|
||||||
|
state = LENGTH_LOWER;
|
||||||
|
}
|
||||||
|
bytes.pop_front();
|
||||||
|
break;
|
||||||
|
case LENGTH_LOWER:
|
||||||
|
working.back().len = bytes[0];
|
||||||
|
state = LENGTH_UPPER;
|
||||||
|
bytes.pop_front();
|
||||||
|
break;
|
||||||
|
case LENGTH_UPPER:
|
||||||
|
working.back().len |= uint16_t(bytes[0]) << 8;
|
||||||
|
if(working.back().len == 0) {
|
||||||
|
working.back().done = true;
|
||||||
|
state = HEADER;
|
||||||
|
} else {
|
||||||
|
state = DATA;
|
||||||
|
}
|
||||||
|
bytes.pop_front();
|
||||||
|
break;
|
||||||
|
case DATA: {
|
||||||
|
// The RingBuffer is not contiguous, so copy out with read() rather than taking a pointer
|
||||||
|
auto& frame = working.back();
|
||||||
|
const size_t dataToGo = frame.len - frame.data.size();
|
||||||
|
const size_t toCopy = std::min(bytes.size(), dataToGo);
|
||||||
|
const size_t oldSize = frame.data.size();
|
||||||
|
frame.data.resize(oldSize + toCopy);
|
||||||
|
bytes.read(frame.data.data() + oldSize, 0, toCopy);
|
||||||
|
bytes.pop(toCopy);
|
||||||
|
if(toCopy == dataToGo) {
|
||||||
|
frame.done = true;
|
||||||
|
state = HEADER;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::any_of(working.begin(), working.end(), [](const RxFrame& rx) { return rx.done; });
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<REDPacketizer::RxFrame> REDPacketizer::output() {
|
||||||
|
// Hand back the completed frames, anything still in progress stays in working
|
||||||
|
std::vector<RxFrame> done;
|
||||||
|
std::vector<RxFrame> pending;
|
||||||
|
for(auto& rx : working) {
|
||||||
|
if(rx.done)
|
||||||
|
done.push_back(std::move(rx));
|
||||||
|
else
|
||||||
|
pending.push_back(std::move(rx));
|
||||||
|
}
|
||||||
|
working = std::move(pending);
|
||||||
|
return done;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
@@ -0,0 +1,249 @@
|
|||||||
|
#include "icsneo/device/extensions/bootloader/parser/fwbin.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/platform/logging.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/parser/ief.h"
|
||||||
|
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* fw.bin file format (all integers little-endian):
|
||||||
|
*
|
||||||
|
* uint8_t versionLength
|
||||||
|
* char version[versionLength] Vehicle Spy 3 version, "major.minor.release.build"
|
||||||
|
* uint8_t hashLength
|
||||||
|
* char hash[hashLength] Build hash, unused
|
||||||
|
* Then tags until the end of the file:
|
||||||
|
* uint32_t nameHash HashDjb2() of the tag name, e.g. "fire2_core"
|
||||||
|
* uint8_t type TagData::Type
|
||||||
|
* uint32_t length
|
||||||
|
* uint8_t data[length]
|
||||||
|
*
|
||||||
|
* Exactly one Signature tag is required, it uses the reserved name hash 0.
|
||||||
|
*/
|
||||||
|
|
||||||
|
uint32_t FWBin::HashDjb2(const std::string& tag) {
|
||||||
|
uint32_t retv = 5381;
|
||||||
|
for(char c : tag)
|
||||||
|
retv = ((retv << 5) + retv) + c;
|
||||||
|
return retv;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FWBin::load(const std::string& path) {
|
||||||
|
file.open(path, std::ios::binary);
|
||||||
|
if(!file.is_open() || file.fail() || file.eof()) {
|
||||||
|
Log::Error(__func__, "Could not open file at " + path);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// Read out the version string, it is a single byte length followed by a string
|
||||||
|
uint8_t size;
|
||||||
|
file.read((char*)&size, 1);
|
||||||
|
if(file.fail() || file.eof()) {
|
||||||
|
Log::Error(__func__, "The fw.bin is truncated, could not read the version length");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
std::string unparsedVersion;
|
||||||
|
unparsedVersion.resize(size);
|
||||||
|
file.read(unparsedVersion.data(), size);
|
||||||
|
if(file.fail() || file.eof()) {
|
||||||
|
Log::Error(__func__, "The fw.bin is truncated, could not read the version string");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!version.load(unparsedVersion)) {
|
||||||
|
Log::Error(__func__, "The fw.bin version string is invalid: " + unparsedVersion);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read out the hash string, it is a single byte length followed by a string
|
||||||
|
file.read((char*)&size, 1);
|
||||||
|
if(file.fail() || file.eof()) {
|
||||||
|
Log::Error(__func__, "The fw.bin is truncated, could not read the hash length");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The hash string is not used, skip over it
|
||||||
|
file.seekg(size, file.cur);
|
||||||
|
if(file.fail() || file.eof()) {
|
||||||
|
Log::Error(__func__, "The fw.bin is truncated, could not read the hash string");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
while(true) {
|
||||||
|
uint32_t tagHash;
|
||||||
|
file.read((char*)&tagHash, sizeof(uint32_t));
|
||||||
|
if(file.eof()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(file.fail()) {
|
||||||
|
Log::Error(__func__, "The fw.bin is truncated, could not read a tag name hash");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(tags.count(tagHash)) {
|
||||||
|
// Duplicate tag
|
||||||
|
Log::Error(__func__, "The fw.bin contains a duplicate tag");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
TagData& current = tags[tagHash];
|
||||||
|
|
||||||
|
// Read the type field
|
||||||
|
file.read((char*)¤t.type, sizeof(TagData::Type));
|
||||||
|
if(file.fail() || file.eof()) {
|
||||||
|
Log::Error(__func__, "The fw.bin is truncated, could not read a tag type");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch(current.type) {
|
||||||
|
case TagData::Type::DataBytes:
|
||||||
|
case TagData::Type::DataLength:
|
||||||
|
case TagData::Type::IEFVersion:
|
||||||
|
if(tagHash == SignatureHash) {
|
||||||
|
Log::Error(__func__, "The fw.bin contains a data tag using the reserved signature name hash");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case TagData::Type::Signature:
|
||||||
|
if(tagHash != SignatureHash) {
|
||||||
|
Log::Error(__func__, "The fw.bin contains a signature tag with a non-signature name hash");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default: // Unknown type
|
||||||
|
Log::Error(__func__, "The fw.bin contains a tag of unknown type " + std::to_string(static_cast<int>(current.type)));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read the length field
|
||||||
|
file.read((char*)¤t.arrayLength, sizeof(uint32_t));
|
||||||
|
if(file.fail() || file.eof()) {
|
||||||
|
Log::Error(__func__, "The fw.bin is truncated, could not read a tag length");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Note the current position so we can jump back to it and read it later
|
||||||
|
current.arrayPosition = file.tellg();
|
||||||
|
file.seekg(current.arrayLength, file.cur);
|
||||||
|
if(file.fail()) {
|
||||||
|
Log::Error(__func__, "The fw.bin is truncated, a tag extends past the end of the file");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
file.clear();
|
||||||
|
|
||||||
|
// The signature tag is required to be present, but it is not verified
|
||||||
|
if(tags.count(SignatureHash) == 0) {
|
||||||
|
Log::Error(__func__, "The fw.bin is missing its signature tag");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FWBin::readTag(const std::string& tag, std::vector<uint8_t>& out) const {
|
||||||
|
const auto hash = HashDjb2(tag);
|
||||||
|
if(hash == SignatureHash) {
|
||||||
|
Log::Error(__func__, "The signature tag can not be read as data");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const auto it = tags.find(hash);
|
||||||
|
if(it == tags.end()) {
|
||||||
|
Log::Error(__func__, "The fw.bin does not contain the tag " + tag);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return it->second.read(file, out);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FWBin::TagData::read(std::ifstream& stream, std::vector<uint8_t>& out) const {
|
||||||
|
stream.clear();
|
||||||
|
stream.seekg(arrayPosition, stream.beg);
|
||||||
|
out.resize(arrayLength);
|
||||||
|
stream.read((char*)out.data(), arrayLength);
|
||||||
|
if(stream.fail()) {
|
||||||
|
Log::Error(__func__, "The fw.bin is truncated, could not read tag data");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FWBin::getFW(const std::string& iefName, std::vector<uint8_t>& fw) const {
|
||||||
|
return readTag(iefName, fw);
|
||||||
|
}
|
||||||
|
|
||||||
|
FWBin::VersionTags FWBin::GetVersionTags(const std::string& iefName) {
|
||||||
|
static const std::unordered_map<std::string, VersionTags> VersionTable = {
|
||||||
|
{"fire2_core", {"fire2_core_version_major", "fire2_core_version_minor"}},
|
||||||
|
{"fire2_core_sg4", {"fire2_core_sg4_version_major", "fire2_core_sg4_version_minor"}},
|
||||||
|
{"RADA2B_SW_bin", {"RADA2B_SW_Version_Major", "RADA2B_SW_Version_Minor"}},
|
||||||
|
{"RADA2B_REVB_SW_bin", {"RADA2B_SW_Version_Major", "RADA2B_SW_Version_Minor"}},
|
||||||
|
{"RADComet_SW_bin", {"RADComet_SW_Version_Major", "RADComet_SW_Version_Minor"}},
|
||||||
|
{"RADComet3_SW_bin", {"RADComet3_SW_Version_Major", "RADComet3_SW_Version_Minor"}},
|
||||||
|
{"RADGalaxy_SG2_SW_bin", {"RADGalaxy_SG2_SW_Version_Major", "RADGalaxy_SG2_SW_Version_Minor"}},
|
||||||
|
{"RADGalaxy_SG2_FFG_SW_bin", {"RADGalaxy_SG2_FFG_SW_Version_Major", "RADGalaxy_SG2_SW_Version_Minor"}},
|
||||||
|
{"RADGalaxy2_SW_bin_p1", {"RADGalaxy2_SW_Version_Major", "RADGalaxy2_SW_Version_Minor"}},
|
||||||
|
{"RADGalaxy2_SW_bin_p2", {"RADGalaxy2_SW_Version_Major", "RADGalaxy2_SW_Version_Minor"}},
|
||||||
|
{"RADGigastar_SW_bin", {"RADGigastar_SW_Version_Major", "RADGigastar_SW_Version_Minor"}},
|
||||||
|
{"RADGigastar_FFG_SW_bin", {"RADGigastar_SW_Version_Major", "RADGigastar_SW_Version_Minor"}},
|
||||||
|
{"RADGigastar_USBz_SW_bin", {"RADGigastar_USBz_SW_Version_Major", "RADGigastar_USBz_SW_Version_Minor"}},
|
||||||
|
{"RADGigastar_USBz_Z7010_SW_bin", {"RADGigastar_USBz_SW_Version_Major", "RADGigastar_USBz_SW_Version_Minor"}},
|
||||||
|
{"RADGigastar_USBz_Z7007s_SW_bin", {"RADGigastar_USBz_SW_Version_Major", "RADGigastar_USBz_SW_Version_Minor"}},
|
||||||
|
{"RADGigastar2_T1S_LIN_SW_bin", {"RADGigastar2_T1S_LIN_SW_Version_Major", "RADGigastar2_T1S_LIN_SW_Version_Minor"}},
|
||||||
|
{"RADGigastar2_T1S_CAN_SW_bin", {"RADGigastar2_T1S_CAN_SW_Version_Major", "RADGigastar2_T1S_CAN_SW_Version_Minor"}},
|
||||||
|
{"RADMoon2_SW_bin", {"RADMoon2_SW_Version_Major", "RADMoon2_SW_Version_Minor"}},
|
||||||
|
{"RADMoon2_Z7010_SW_bin", {"RADMoon2_SW_Version_Major", "RADMoon2_SW_Version_Minor"}},
|
||||||
|
{"RADMoonT1S_SW_bin", {"RADMoonT1S_SW_Version_Major", "RADMoonT1S_SW_Version_Minor"}},
|
||||||
|
{"vcan44_core_ief", {"vcan44_core_version_major", "vcan44_core_version_minor"}}
|
||||||
|
};
|
||||||
|
auto iter = VersionTable.find(iefName);
|
||||||
|
return iter != VersionTable.end() ? iter->second : VersionTags();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<icsneo::DeviceAppVersion> FWBin::getChipVersion(const std::string& iefName) const {
|
||||||
|
auto versionTags = GetVersionTags(iefName);
|
||||||
|
|
||||||
|
if(versionTags) {
|
||||||
|
std::vector<uint8_t> majData;
|
||||||
|
std::vector<uint8_t> minData;
|
||||||
|
|
||||||
|
if(!readTag(versionTags.majorTag, majData)) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!readTag(versionTags.minorTag, minData)) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(majData.size() != 1)
|
||||||
|
return std::nullopt;
|
||||||
|
|
||||||
|
if(minData.size() != 1)
|
||||||
|
return std::nullopt;
|
||||||
|
|
||||||
|
icsneo::DeviceAppVersion chipVersion;
|
||||||
|
chipVersion.major = majData[0];
|
||||||
|
chipVersion.minor = minData[0];
|
||||||
|
return chipVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<uint8_t> buf;
|
||||||
|
|
||||||
|
if(!readTag(iefName, buf)) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
IEF ief;
|
||||||
|
|
||||||
|
if(!ief.load(std::move(buf))) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
icsneo::DeviceAppVersion chipVersion;
|
||||||
|
chipVersion.major = ief.getVersion().appMajor;
|
||||||
|
chipVersion.minor = ief.getVersion().appMinor;
|
||||||
|
return chipVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
@@ -0,0 +1,142 @@
|
|||||||
|
#include "icsneo/device/extensions/bootloader/parser/fwbundle.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/parser/fwbin.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/platform/logging.h"
|
||||||
|
#include "icsneo/core/crc32.h"
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstring>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iterator>
|
||||||
|
#include <limits>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Firmware bundle file format (all integers little-endian):
|
||||||
|
*
|
||||||
|
* FWBundleHeader
|
||||||
|
* Then chips until the end of the file:
|
||||||
|
* FWBundleChip
|
||||||
|
* uint8_t data[FWBundleChip::length]
|
||||||
|
*
|
||||||
|
* The CRC32 covers the whole file with the crc field set to zero.
|
||||||
|
*/
|
||||||
|
#pragma pack(push, 1)
|
||||||
|
struct FWBundleHeader {
|
||||||
|
char magic[2]; // "FW"
|
||||||
|
uint16_t fmtVersion;
|
||||||
|
uint32_t crc;
|
||||||
|
uint8_t build;
|
||||||
|
uint8_t maintenance;
|
||||||
|
uint8_t minor;
|
||||||
|
uint8_t major;
|
||||||
|
uint8_t type; // Build type, unused
|
||||||
|
};
|
||||||
|
|
||||||
|
struct FWBundleChip {
|
||||||
|
uint32_t hash; // FWBin::HashDjb2() of the chip's IEF name
|
||||||
|
uint8_t build;
|
||||||
|
uint8_t maintenance;
|
||||||
|
uint8_t minor;
|
||||||
|
uint8_t major;
|
||||||
|
uint32_t length;
|
||||||
|
};
|
||||||
|
#pragma pack(pop)
|
||||||
|
|
||||||
|
bool FWBundle::IsFWBundle(const std::string& path) {
|
||||||
|
std::ifstream file(path, std::ios::binary);
|
||||||
|
if(!file.is_open())
|
||||||
|
return false;
|
||||||
|
// Only check the magic, so a truncated bundle is still reported as a bundle error by load()
|
||||||
|
char magic[sizeof(FWBundleHeader::magic)];
|
||||||
|
file.read(magic, sizeof(magic));
|
||||||
|
if(file.fail())
|
||||||
|
return false;
|
||||||
|
return magic[0] == 'F' && magic[1] == 'W';
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FWBundle::load(const std::string& path) {
|
||||||
|
std::ifstream file(path, std::ios::binary);
|
||||||
|
if(!file.is_open()) {
|
||||||
|
Log::Error(__func__, "Could not open file at " + path);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
std::vector<uint8_t> bytes((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
||||||
|
if(file.bad()) {
|
||||||
|
Log::Error(__func__, "Could not read file at " + path);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(bytes.size() < sizeof(FWBundleHeader) || bytes.size() > std::numeric_limits<uint32_t>::max()) {
|
||||||
|
Log::Error(__func__, "The firmware bundle is smaller than its header or larger than 4 GiB");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
FWBundleHeader header;
|
||||||
|
std::memcpy(&header, bytes.data(), sizeof(header));
|
||||||
|
if(header.magic[0] != 'F' || header.magic[1] != 'W') {
|
||||||
|
Log::Error(__func__, "The file is not a firmware bundle, bad magic");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The CRC is computed with the CRC field zeroed
|
||||||
|
std::memset(bytes.data() + offsetof(FWBundleHeader, crc), 0, sizeof(header.crc));
|
||||||
|
if(icsneo::crc32(0, bytes.data(), static_cast<uint32_t>(bytes.size())) != header.crc) {
|
||||||
|
Log::Error(__func__, "The firmware bundle failed CRC validation");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unordered_map<uint32_t, ChipBinary> parsed;
|
||||||
|
size_t offset = sizeof(FWBundleHeader);
|
||||||
|
while(offset < bytes.size()) {
|
||||||
|
if(bytes.size() - offset < sizeof(FWBundleChip)) {
|
||||||
|
Log::Error(__func__, "The firmware bundle is truncated, could not read a chip header");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
FWBundleChip chip;
|
||||||
|
std::memcpy(&chip, bytes.data() + offset, sizeof(chip));
|
||||||
|
offset += sizeof(FWBundleChip);
|
||||||
|
|
||||||
|
if(bytes.size() - offset < chip.length) {
|
||||||
|
Log::Error(__func__, "The firmware bundle is truncated, a chip binary extends past the end of the file");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto& bin = parsed[chip.hash];
|
||||||
|
bin.data.assign(bytes.begin() + offset, bytes.begin() + offset + chip.length);
|
||||||
|
bin.version.major = chip.major;
|
||||||
|
bin.version.minor = chip.minor;
|
||||||
|
bin.version.release = chip.maintenance;
|
||||||
|
bin.version.build = chip.build;
|
||||||
|
offset += chip.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
version.major = header.major;
|
||||||
|
version.minor = header.minor;
|
||||||
|
version.release = header.maintenance;
|
||||||
|
version.build = header.build;
|
||||||
|
binaries = std::move(parsed);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FWBundle::getFW(const std::string& iefName, std::vector<uint8_t>& fw) const {
|
||||||
|
const auto it = binaries.find(FWBin::HashDjb2(iefName));
|
||||||
|
if(it == binaries.end()) {
|
||||||
|
Log::Error(__func__, "The chip binary could not be found");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
fw = it->second.data;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<icsneo::DeviceAppVersion> FWBundle::getChipVersion(const std::string& iefName) const {
|
||||||
|
const auto it = binaries.find(FWBin::HashDjb2(iefName));
|
||||||
|
if(it == binaries.end())
|
||||||
|
return std::nullopt;
|
||||||
|
return icsneo::DeviceAppVersion(it->second.version.major, it->second.version.minor);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
#include "icsneo/device/extensions/bootloader/parser/fwloader.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/parser/fwbin.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/parser/fwbundle.h"
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
std::unique_ptr<FWRepo> FWLoader::Load(const std::string& path) {
|
||||||
|
if(FWBundle::IsFWBundle(path)) {
|
||||||
|
auto bundle = std::make_unique<FWBundle>();
|
||||||
|
if(!bundle->load(path))
|
||||||
|
return nullptr;
|
||||||
|
return bundle;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto bin = std::make_unique<FWBin>();
|
||||||
|
if(!bin->load(path))
|
||||||
|
return nullptr;
|
||||||
|
return bin;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
#include "icsneo/device/extensions/bootloader/parser/fwzip.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/platform/logging.h"
|
||||||
|
|
||||||
|
#include <miniz.h>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
class ZipHandle {
|
||||||
|
public:
|
||||||
|
ZipHandle() {
|
||||||
|
mz_zip_zero_struct(&handle);
|
||||||
|
}
|
||||||
|
~ZipHandle() {
|
||||||
|
mz_zip_reader_end(&handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
operator mz_zip_archive*() {
|
||||||
|
return &handle;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
mz_zip_archive handle;
|
||||||
|
};
|
||||||
|
|
||||||
|
// A zip firmware image holds a single entry, the raw binary to send to the device
|
||||||
|
FWZip::FWZip(std::vector<uint8_t>&& tagData)
|
||||||
|
: handle(std::make_unique<ZipHandle>()), zipData(std::move(tagData)) {
|
||||||
|
|
||||||
|
if(!mz_zip_reader_init_mem(*handle, zipData.data(), zipData.size(), 0)) {
|
||||||
|
Log::Error(__func__, "The firmware zip could not be opened");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
mz_zip_archive_file_stat file;
|
||||||
|
if(!mz_zip_reader_file_stat(*handle, 0, &file)) {
|
||||||
|
Log::Error(__func__, "The firmware zip is empty or its first entry could not be read");
|
||||||
|
} else {
|
||||||
|
uncompressedSize = static_cast<size_t>(file.m_uncomp_size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
FWZip::~FWZip() = default;
|
||||||
|
|
||||||
|
bool FWZip::readAll(uint8_t* out) {
|
||||||
|
if(!uncompressedSize) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!out) {
|
||||||
|
Log::Error(__func__, "No output buffer for the firmware zip");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!mz_zip_reader_extract_to_mem(*handle, 0, out, *uncompressedSize, 0)) {
|
||||||
|
Log::Error(__func__, "The firmware zip could not be decompressed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FWZip::readAll(std::vector<uint8_t>& out) {
|
||||||
|
if(!uncompressedSize) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
out = std::vector<uint8_t>(*uncompressedSize, 0);
|
||||||
|
return readAll(out.data());
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
#include "icsneo/device/extensions/bootloader/parser/ief.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/platform/logging.h"
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* IEF file format (all integers little-endian):
|
||||||
|
*
|
||||||
|
* uint8_t magic 0xAA
|
||||||
|
* Version version
|
||||||
|
* uint8_t configLength
|
||||||
|
* uint8_t config[configLength] Unused
|
||||||
|
* Then sectors until the end of the file:
|
||||||
|
* uint8_t magic 0xAA
|
||||||
|
* uint32_t index Sector index, see the communication for how this maps to an address
|
||||||
|
* uint32_t length
|
||||||
|
* uint8_t data[length]
|
||||||
|
*/
|
||||||
|
bool IEF::load(std::vector<uint8_t>&& bytes) {
|
||||||
|
static constexpr uint8_t Magic = 0xAA;
|
||||||
|
// Magic, version, config byte count
|
||||||
|
static constexpr size_t FixedHeaderSize = 1 + sizeof(Version) + 1;
|
||||||
|
// Magic, index, size
|
||||||
|
static constexpr size_t SectorHeaderSize = 1 + sizeof(uint32_t) + sizeof(uint32_t);
|
||||||
|
|
||||||
|
data = std::move(bytes);
|
||||||
|
sectors.clear();
|
||||||
|
if(data.size() < FixedHeaderSize) {
|
||||||
|
Log::Error(__func__, "IEF too short");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(data.front() != Magic) {
|
||||||
|
Log::Error(__func__, "Invalid IEF magic");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::memcpy(&version, data.data() + 1, sizeof(Version));
|
||||||
|
|
||||||
|
const size_t headerSize = FixedHeaderSize + data[FixedHeaderSize - 1];
|
||||||
|
if(data.size() <= headerSize) {
|
||||||
|
Log::Error(__func__, "Invalid IEF config bytes");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t offset = headerSize;
|
||||||
|
while(data.size() - offset >= SectorHeaderSize) {
|
||||||
|
if(data[offset] != Magic) {
|
||||||
|
Log::Error(__func__, "Invalid IEF sector magic");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
uint32_t index;
|
||||||
|
uint32_t size;
|
||||||
|
std::memcpy(&index, data.data() + offset + 1, sizeof(index));
|
||||||
|
std::memcpy(&size, data.data() + offset + 1 + sizeof(index), sizeof(size));
|
||||||
|
offset += SectorHeaderSize;
|
||||||
|
if(data.size() - offset < size) {
|
||||||
|
Log::Error(__func__, "Invalid IEF sector");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
sectors.emplace_back(index, BytesView(data.data() + offset, size));
|
||||||
|
offset += size;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
#include "icsneo/device/extensions/bootloader/platform/logging.h"
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
namespace Log {
|
||||||
|
|
||||||
|
// Set ICS_BOOTLOADER_LOG_LEVEL to 1 (error) through 5 (trace) to enable logging to stderr
|
||||||
|
static int GetLogLevel() {
|
||||||
|
static const int level = [] {
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#pragma warning(push)
|
||||||
|
#pragma warning(disable: 4996) // getenv
|
||||||
|
#endif
|
||||||
|
const char* env = std::getenv("ICS_BOOTLOADER_LOG_LEVEL");
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#pragma warning(pop)
|
||||||
|
#endif
|
||||||
|
return env ? std::atoi(env) : 0;
|
||||||
|
}();
|
||||||
|
return level;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void Write(int level, const char* prefix, const std::string& subject, const std::string& msg) {
|
||||||
|
if(GetLogLevel() >= level)
|
||||||
|
std::cerr << prefix << subject << " - " << msg << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Trace(const std::string& subject, const std::string& msg) {
|
||||||
|
Write(5, "[TRACE] ", subject, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Debug(const std::string& subject, const std::string& msg) {
|
||||||
|
Write(4, "[DEBUG] ", subject, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Info(const std::string& subject, const std::string& msg) {
|
||||||
|
Write(3, "[INFO] ", subject, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Warning(const std::string& subject, const std::string& msg) {
|
||||||
|
Write(2, "[WARN] ", subject, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Error(const std::string& subject, const std::string& msg) {
|
||||||
|
Write(1, "[ERROR] ", subject, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Log
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
#include "icsneo/device/extensions/bootloader/version.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/platform/logging.h"
|
||||||
|
|
||||||
|
#include <limits>
|
||||||
|
#include <optional>
|
||||||
|
#include <regex>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
bool FirmwareSourceVersion::load(const std::string& toParse) {
|
||||||
|
static const std::regex VersionRegex{"(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)"};
|
||||||
|
std::smatch match;
|
||||||
|
if(!std::regex_match(toParse, match, VersionRegex)) {
|
||||||
|
Log::Error(__func__, "Failed to parse version");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto parse = [](const std::string& input) -> std::optional<ValueType> {
|
||||||
|
// The regex guarantees only digits, limit the length so stoul can not overflow
|
||||||
|
if(input.size() > 3) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
const auto value = std::stoul(input);
|
||||||
|
if(value > std::numeric_limits<ValueType>::max()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
return static_cast<ValueType>(value);
|
||||||
|
};
|
||||||
|
|
||||||
|
const auto parsedMajor = parse(match[1]);
|
||||||
|
const auto parsedMinor = parse(match[2]);
|
||||||
|
const auto parsedRelease = parse(match[3]);
|
||||||
|
const auto parsedBuild = parse(match[4]);
|
||||||
|
if(!parsedMajor || !parsedMinor || !parsedRelease || !parsedBuild) {
|
||||||
|
Log::Error(__func__, "Invalid sized version number");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
major = *parsedMajor;
|
||||||
|
minor = *parsedMinor;
|
||||||
|
release = *parsedRelease;
|
||||||
|
build = *parsedBuild;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
@@ -8,7 +8,6 @@ Dependencies
|
|||||||
The minimum requirements to build libicsneo are:
|
The minimum requirements to build libicsneo are:
|
||||||
- CMake version 3.12 or newer
|
- CMake version 3.12 or newer
|
||||||
- A C++17 compiler
|
- A C++17 compiler
|
||||||
- libpcap on Linux and macOS
|
|
||||||
|
|
||||||
|
|
||||||
Building library & examples
|
Building library & examples
|
||||||
@@ -30,10 +29,3 @@ libicsneo supports being added as a sub-projects with CMake's
|
|||||||
#. Clone libicsneo into the desired location within the project
|
#. Clone libicsneo into the desired location within the project
|
||||||
#. Add ``add_subdirectory(path/to/libicsneo)`` to ``CMakeLists.txt``
|
#. Add ``add_subdirectory(path/to/libicsneo)`` to ``CMakeLists.txt``
|
||||||
#. Link the project to libicsneo with ``target_link_libraries(app icsneocpp)``
|
#. Link the project to libicsneo with ``target_link_libraries(app icsneocpp)``
|
||||||
|
|
||||||
Linux udev Rules
|
|
||||||
================
|
|
||||||
|
|
||||||
Linux users may want to install the included udev rules to run libicsneo based
|
|
||||||
applications without root, this can be done with:
|
|
||||||
``cp 99-intrepidcs.rules /etc/udev/rules.d/``
|
|
||||||
|
|||||||
@@ -118,3 +118,14 @@ firmware single frame, ``--both`` sends a segmented payload with firmware flow c
|
|||||||
|
|
||||||
.. literalinclude:: ../../examples/python/iso15765/iso15765_loopback.py
|
.. literalinclude:: ../../examples/python/iso15765/iso15765_loopback.py
|
||||||
:language: python
|
:language: python
|
||||||
|
|
||||||
|
Firmware Update
|
||||||
|
===============
|
||||||
|
|
||||||
|
Updates the first device found from a ``fw.bin`` or firmware bundle, see
|
||||||
|
:doc:`firmware_update`.
|
||||||
|
|
||||||
|
:download:`Download example <../../examples/python/bootloader/update.py>`
|
||||||
|
|
||||||
|
.. literalinclude:: ../../examples/python/bootloader/update.py
|
||||||
|
:language: python
|
||||||
|
|||||||
@@ -25,14 +25,3 @@ To upgrade an existing installation:
|
|||||||
.. code-block:: bash
|
.. code-block:: bash
|
||||||
|
|
||||||
pip install --pre --upgrade icsneopy
|
pip install --pre --upgrade icsneopy
|
||||||
|
|
||||||
Linux udev Rules
|
|
||||||
================
|
|
||||||
|
|
||||||
Linux users may want to install the included udev rules to run icsneopy-based
|
|
||||||
applications without root. The rules file can be found in the libicsneo source
|
|
||||||
repository at https://github.com/intrepidcs/libicsneo/.
|
|
||||||
|
|
||||||
.. code-block:: bash
|
|
||||||
|
|
||||||
sudo cp 99-intrepidcs.rules /etc/udev/rules.d/
|
|
||||||
|
|||||||
@@ -15,17 +15,3 @@ communication library. The source code for libicsneo can be found on GitHub:
|
|||||||
icsneoc/index
|
icsneoc/index
|
||||||
icsneoc2/index
|
icsneoc2/index
|
||||||
|
|
||||||
Linux Installation
|
|
||||||
==================
|
|
||||||
|
|
||||||
Applications that use raw Ethernet device discovery, such as PCAP-backed
|
|
||||||
discovery, need permission to open raw network sockets. Instead of running your
|
|
||||||
application with ``sudo``, grant the installed executable the required Linux
|
|
||||||
capabilities:
|
|
||||||
|
|
||||||
.. code-block:: bash
|
|
||||||
|
|
||||||
sudo setcap cap_net_raw,cap_net_admin=eip /usr/bin/your-app
|
|
||||||
|
|
||||||
Replace ``/usr/bin/your-app`` with the full path to the application executable.
|
|
||||||
|
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ Although the example program will build without successfully completing the step
|
|||||||
|
|
||||||
First, we are going to build the icsneoc library into a .so file that we can later use in order to access the library functions.
|
First, we are going to build the icsneoc library into a .so file that we can later use in order to access the library functions.
|
||||||
|
|
||||||
1. Install dependencies with `sudo apt update` then `sudo apt install build-essential cmake libpcap0.8-dev`
|
1. Install dependencies with `sudo apt update` then `sudo apt install build-essential cmake`
|
||||||
2. Change directories to `libicsneo-examples/third-party/libicsneo` and create a build directory by running `mkdir -p build`
|
2. Change directories to `libicsneo-examples/third-party/libicsneo` and create a build directory by running `mkdir -p build`
|
||||||
3. Enter the build directory with `cd build`
|
3. Enter the build directory with `cd build`
|
||||||
4. Run `cmake ..` to generate your Makefile.
|
4. Run `cmake ..` to generate your Makefile.
|
||||||
@@ -72,8 +72,7 @@ Although the example program will build without successfully completing the step
|
|||||||
* Hint! Running `cmake -DCMAKE_BUILD_TYPE=Debug ..` will generate the proper scripts to build debug, and `cmake -DCMAKE_BUILD_TYPE=Release ..` will generate the proper scripts to build with all optimizations on.
|
* Hint! Running `cmake -DCMAKE_BUILD_TYPE=Debug ..` will generate the proper scripts to build debug, and `cmake -DCMAKE_BUILD_TYPE=Release ..` will generate the proper scripts to build with all optimizations on.
|
||||||
5. Run `make` to build the library.
|
5. Run `make` to build the library.
|
||||||
* Hint! Speed up your build by using multiple processors! Use `make -j#` where `#` is the number of cores/threads your system has plus one. For instance, on a standard 8 thread Intel i7, you might use `-j9` for an ~8x speedup.
|
* Hint! Speed up your build by using multiple processors! Use `make -j#` where `#` is the number of cores/threads your system has plus one. For instance, on a standard 8 thread Intel i7, you might use `-j9` for an ~8x speedup.
|
||||||
6. Run `sudo ./libicsneoc-example` to run the example.
|
6. Run `./libicsneoc-example` to run the example.
|
||||||
* Hint! In order to run without sudo, you will need to set up the udev rules. Copy `libicsneo-examples/third-party/libicsneo/99-intrepidcs.rules` to `/etc/udev/rules.d`, then run `udevadm control --reload-rules && udevadm trigger` afterwards. While the program will still run without setting up these rules, it will fail to open any devices.
|
|
||||||
|
|
||||||
## macOS
|
## macOS
|
||||||
|
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ Although the example program will build without successfully completing the step
|
|||||||
|
|
||||||
First, we are going to build the icsneoc library into a .so file that we can later use in order to access the library functions.
|
First, we are going to build the icsneoc library into a .so file that we can later use in order to access the library functions.
|
||||||
|
|
||||||
1. Install dependencies with `sudo apt update` then `sudo apt install build-essential cmake libpcap0.8-dev`
|
1. Install dependencies with `sudo apt update` then `sudo apt install build-essential cmake`
|
||||||
2. Change directories to `libicsneo-examples/third-party/libicsneo` and create a build directory by running `mkdir -p build`
|
2. Change directories to `libicsneo-examples/third-party/libicsneo` and create a build directory by running `mkdir -p build`
|
||||||
3. Enter the build directory with `cd build`
|
3. Enter the build directory with `cd build`
|
||||||
4. Run `cmake ..` to generate your Makefile.
|
4. Run `cmake ..` to generate your Makefile.
|
||||||
@@ -72,8 +72,7 @@ Although the example program will build without successfully completing the step
|
|||||||
* Hint! Running `cmake -DCMAKE_BUILD_TYPE=Debug ..` will generate the proper scripts to build debug, and `cmake -DCMAKE_BUILD_TYPE=Release ..` will generate the proper scripts to build with all optimizations on.
|
* Hint! Running `cmake -DCMAKE_BUILD_TYPE=Debug ..` will generate the proper scripts to build debug, and `cmake -DCMAKE_BUILD_TYPE=Release ..` will generate the proper scripts to build with all optimizations on.
|
||||||
5. Run `make` to build the library.
|
5. Run `make` to build the library.
|
||||||
* Hint! Speed up your build by using multiple processors! Use `make -j#` where `#` is the number of cores/threads your system has plus one. For instance, on a standard 8 thread Intel i7, you might use `-j9` for an ~8x speedup.
|
* Hint! Speed up your build by using multiple processors! Use `make -j#` where `#` is the number of cores/threads your system has plus one. For instance, on a standard 8 thread Intel i7, you might use `-j9` for an ~8x speedup.
|
||||||
6. Run `sudo ./libicsneoc-example` to run the example.
|
6. Run `./libicsneoc-example` to run the example.
|
||||||
* Hint! In order to run without sudo, you will need to set up the udev rules. Copy `libicsneo-examples/third-party/libicsneo/99-intrepidcs.rules` to `/etc/udev/rules.d`, then run `udevadm control --reload-rules && udevadm trigger` afterwards. While the program will still run without setting up these rules, it will fail to open any devices.
|
|
||||||
|
|
||||||
## macOS
|
## macOS
|
||||||
|
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ Although the example program will build without successfully completing the step
|
|||||||
|
|
||||||
First, we are going to build the icsneoc library into a .so file that we can later use in order to access the library functions.
|
First, we are going to build the icsneoc library into a .so file that we can later use in order to access the library functions.
|
||||||
|
|
||||||
1. Install dependencies with `sudo apt update` then `sudo apt install build-essential cmake libpcap0.8-dev`
|
1. Install dependencies with `sudo apt update` then `sudo apt install build-essential cmake`
|
||||||
2. Change directories to `libicsneo-examples/third-party/libicsneo` and create a build directory by running `mkdir -p build`
|
2. Change directories to `libicsneo-examples/third-party/libicsneo` and create a build directory by running `mkdir -p build`
|
||||||
3. Enter the build directory with `cd build`
|
3. Enter the build directory with `cd build`
|
||||||
4. Run `cmake ..` to generate your Makefile.
|
4. Run `cmake ..` to generate your Makefile.
|
||||||
@@ -72,8 +72,7 @@ Although the example program will build without successfully completing the step
|
|||||||
* Hint! Running `cmake -DCMAKE_BUILD_TYPE=Debug ..` will generate the proper scripts to build debug, and `cmake -DCMAKE_BUILD_TYPE=Release ..` will generate the proper scripts to build with all optimizations on.
|
* Hint! Running `cmake -DCMAKE_BUILD_TYPE=Debug ..` will generate the proper scripts to build debug, and `cmake -DCMAKE_BUILD_TYPE=Release ..` will generate the proper scripts to build with all optimizations on.
|
||||||
5. Run `make` to build the library.
|
5. Run `make` to build the library.
|
||||||
* Hint! Speed up your build by using multiple processors! Use `make -j#` where `#` is the number of cores/threads your system has plus one. For instance, on a standard 8 thread Intel i7, you might use `-j9` for an ~8x speedup.
|
* Hint! Speed up your build by using multiple processors! Use `make -j#` where `#` is the number of cores/threads your system has plus one. For instance, on a standard 8 thread Intel i7, you might use `-j9` for an ~8x speedup.
|
||||||
6. Run `sudo ./libicsneoc-example` to run the example.
|
6. Run `./libicsneoc-example` to run the example.
|
||||||
* Hint! In order to run without sudo, you will need to set up the udev rules. Copy `libicsneo-examples/third-party/libicsneo/99-intrepidcs.rules` to `/etc/udev/rules.d`, then run `udevadm control --reload-rules && udevadm trigger` afterwards. While the program will still run without setting up these rules, it will fail to open any devices.
|
|
||||||
|
|
||||||
## macOS
|
## macOS
|
||||||
|
|
||||||
|
|||||||
@@ -32,15 +32,14 @@ If you haven't done this, `third-party/libicsneo` will be empty and you won't be
|
|||||||
|
|
||||||
### Ubuntu 18.04 LTS
|
### Ubuntu 18.04 LTS
|
||||||
|
|
||||||
1. Install dependencies with `sudo apt update` then `sudo apt install build-essential cmake libpcap0.8-dev`
|
1. Install dependencies with `sudo apt update` then `sudo apt install build-essential cmake`
|
||||||
2. Change directories to your `libicsneo-examples/libicsneocpp-example` folder and create a build directory by running `mkdir -p build`
|
2. Change directories to your `libicsneo-examples/libicsneocpp-example` folder and create a build directory by running `mkdir -p build`
|
||||||
3. Enter the build directory with `cd build`
|
3. Enter the build directory with `cd build`
|
||||||
4. Run `cmake ..` to generate your Makefile.
|
4. Run `cmake ..` to generate your Makefile.
|
||||||
* Hint! Running `cmake -DCMAKE_BUILD_TYPE=Debug ..` will generate the proper scripts to build debug, and `cmake -DCMAKE_BUILD_TYPE=Release ..` will generate the proper scripts to build with all optimizations on.
|
* Hint! Running `cmake -DCMAKE_BUILD_TYPE=Debug ..` will generate the proper scripts to build debug, and `cmake -DCMAKE_BUILD_TYPE=Release ..` will generate the proper scripts to build with all optimizations on.
|
||||||
5. Run `make libicsneocpp-interactive-example` to build.
|
5. Run `make libicsneocpp-interactive-example` to build.
|
||||||
* Hint! Speed up your build by using multiple processors! Use `make libicsneocpp-interactive-example -j#` where `#` is the number of cores/threads your system has plus one. For instance, on a standard 8 thread Intel i7, you might use `-j9` for an ~8x speedup.
|
* Hint! Speed up your build by using multiple processors! Use `make libicsneocpp-interactive-example -j#` where `#` is the number of cores/threads your system has plus one. For instance, on a standard 8 thread Intel i7, you might use `-j9` for an ~8x speedup.
|
||||||
6. Now run `sudo ./libicsneocpp-interactive-example` to run the example.
|
6. Now run `./libicsneocpp-interactive-example` to run the example.
|
||||||
* Hint! In order to run without sudo, you will need to set up the udev rules. Copy `libicsneo-examples/third-party/libicsneo/99-intrepidcs.rules` to `/etc/udev/rules.d`, then run `udevadm control --reload-rules && udevadm trigger` afterwards. While the program will still run without setting up these rules, it will fail to open any devices.
|
|
||||||
7. If you wish to run the simple example instead, replace any instances of "interactive" with "simple" in steps 5 and 6.
|
7. If you wish to run the simple example instead, replace any instances of "interactive" with "simple" in steps 5 and 6.
|
||||||
|
|
||||||
### macOS
|
### macOS
|
||||||
|
|||||||
@@ -32,15 +32,14 @@ If you haven't done this, `third-party/libicsneo` will be empty and you won't be
|
|||||||
|
|
||||||
### Ubuntu 18.04 LTS
|
### Ubuntu 18.04 LTS
|
||||||
|
|
||||||
1. Install dependencies with `sudo apt update` then `sudo apt install build-essential cmake libpcap0.8-dev`
|
1. Install dependencies with `sudo apt update` then `sudo apt install build-essential cmake`
|
||||||
2. Change directories to your `libicsneo-examples/libicsneocpp-example` folder and create a build directory by running `mkdir -p build`
|
2. Change directories to your `libicsneo-examples/libicsneocpp-example` folder and create a build directory by running `mkdir -p build`
|
||||||
3. Enter the build directory with `cd build`
|
3. Enter the build directory with `cd build`
|
||||||
4. Run `cmake ..` to generate your Makefile.
|
4. Run `cmake ..` to generate your Makefile.
|
||||||
* Hint! Running `cmake -DCMAKE_BUILD_TYPE=Debug ..` will generate the proper scripts to build debug, and `cmake -DCMAKE_BUILD_TYPE=Release ..` will generate the proper scripts to build with all optimizations on.
|
* Hint! Running `cmake -DCMAKE_BUILD_TYPE=Debug ..` will generate the proper scripts to build debug, and `cmake -DCMAKE_BUILD_TYPE=Release ..` will generate the proper scripts to build with all optimizations on.
|
||||||
5. Run `make libicsneocpp-interactive-example` to build.
|
5. Run `make libicsneocpp-interactive-example` to build.
|
||||||
* Hint! Speed up your build by using multiple processors! Use `make libicsneocpp-interactive-example -j#` where `#` is the number of cores/threads your system has plus one. For instance, on a standard 8 thread Intel i7, you might use `-j9` for an ~8x speedup.
|
* Hint! Speed up your build by using multiple processors! Use `make libicsneocpp-interactive-example -j#` where `#` is the number of cores/threads your system has plus one. For instance, on a standard 8 thread Intel i7, you might use `-j9` for an ~8x speedup.
|
||||||
6. Now run `sudo ./libicsneocpp-interactive-example` to run the example.
|
6. Now run `./libicsneocpp-interactive-example` to run the example.
|
||||||
* Hint! In order to run without sudo, you will need to set up the udev rules. Copy `libicsneo-examples/third-party/libicsneo/99-intrepidcs.rules` to `/etc/udev/rules.d`, then run `udevadm control --reload-rules && udevadm trigger` afterwards. While the program will still run without setting up these rules, it will fail to open any devices.
|
|
||||||
7. If you wish to run the simple example instead, replace any instances of "interactive" with "simple" in steps 5 and 6.
|
7. If you wish to run the simple example instead, replace any instances of "interactive" with "simple" in steps 5 and 6.
|
||||||
|
|
||||||
### macOS
|
### macOS
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
import sys
|
||||||
|
|
||||||
|
import icsneopy
|
||||||
|
|
||||||
|
def update(firmware_path):
|
||||||
|
devices = icsneopy.find_all_devices()
|
||||||
|
|
||||||
|
if len(devices) == 0:
|
||||||
|
print("no devices found")
|
||||||
|
return False
|
||||||
|
|
||||||
|
device = devices[0]
|
||||||
|
print(f"selected {device}")
|
||||||
|
|
||||||
|
if not device.open():
|
||||||
|
print("unable to open device")
|
||||||
|
return False
|
||||||
|
|
||||||
|
fw = icsneopy.Bootloader.FWLoader.load(firmware_path)
|
||||||
|
if fw is None:
|
||||||
|
print(f"unable to load firmware from {firmware_path}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
last_status = None
|
||||||
|
|
||||||
|
# repeated status messages overwrite the current line so progress updates in place
|
||||||
|
def logger(status_type, status, progress):
|
||||||
|
nonlocal last_status
|
||||||
|
if last_status not in (None, status):
|
||||||
|
print()
|
||||||
|
last_status = status
|
||||||
|
print(f"\r{status}" + (f" ({progress * 100:.1f}%)" if progress is not None else ""), end="", flush=True)
|
||||||
|
return icsneopy.Device.OpenDirective.Continue
|
||||||
|
|
||||||
|
# force_flash updates every chip, even those already at the version in the firmware file
|
||||||
|
ok = icsneopy.Bootloader.update(device, fw, logger, force_flash=True)
|
||||||
|
print()
|
||||||
|
if not ok:
|
||||||
|
print(f"update failed: {icsneopy.get_last_error()}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
print("update complete")
|
||||||
|
return True
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
if len(sys.argv) != 2:
|
||||||
|
print(f"usage: {sys.argv[0]} <fw.bin|bundle>")
|
||||||
|
sys.exit(1)
|
||||||
|
sys.exit(0 if update(sys.argv[1]) else 1)
|
||||||
@@ -127,6 +127,8 @@ public:
|
|||||||
DiskFormatNotSupported = 0x2058,
|
DiskFormatNotSupported = 0x2058,
|
||||||
DiskFormatInvalidCount = 0x2059,
|
DiskFormatInvalidCount = 0x2059,
|
||||||
TC10RequestFailed = 0x205A,
|
TC10RequestFailed = 0x205A,
|
||||||
|
FirmwareUpdateNotSupported = 0x205B,
|
||||||
|
FirmwareUpdateFailed = 0x205C,
|
||||||
|
|
||||||
// Transport Events
|
// Transport Events
|
||||||
FailedToRead = 0x3000,
|
FailedToRead = 0x3000,
|
||||||
@@ -168,6 +170,7 @@ public:
|
|||||||
ServdNoDataError = ServdBindError + 9,
|
ServdNoDataError = ServdBindError + 9,
|
||||||
ServdJoinMulticastError = ServdBindError + 10,
|
ServdJoinMulticastError = ServdBindError + 10,
|
||||||
ServdNotReachable = ServdBindError + 11,
|
ServdNotReachable = ServdBindError + 11,
|
||||||
|
ServdDisabled = ServdBindError + 12,
|
||||||
|
|
||||||
// DXX
|
// DXX
|
||||||
DXXErrorSys = 0x6100,
|
DXXErrorSys = 0x6100,
|
||||||
|
|||||||
@@ -31,7 +31,10 @@ enum class Command : uint8_t {
|
|||||||
LoadCoreMini = 0x43,
|
LoadCoreMini = 0x43,
|
||||||
GetRTC = 0x49,
|
GetRTC = 0x49,
|
||||||
SetRTC = 0x50,
|
SetRTC = 0x50,
|
||||||
|
PeripheralBootloader = 0x83,
|
||||||
|
EnterBootloader = 0xA0,
|
||||||
RequestSerialNumber = 0xA1,
|
RequestSerialNumber = 0xA1,
|
||||||
|
ValidateDevice = 0xA2,
|
||||||
GetMainVersion = 0xA3, // Previously known as RED_CMD_APP_VERSION_REQ
|
GetMainVersion = 0xA3, // Previously known as RED_CMD_APP_VERSION_REQ
|
||||||
SetSettings = 0xA4, // Previously known as RED_CMD_SET_BAUD_REQ, follow up with SaveSettings to write to EEPROM
|
SetSettings = 0xA4, // Previously known as RED_CMD_SET_BAUD_REQ, follow up with SaveSettings to write to EEPROM
|
||||||
//GetSettings = 0xA5, // Previously known as RED_CMD_READ_BAUD_REQ, now unused
|
//GetSettings = 0xA5, // Previously known as RED_CMD_READ_BAUD_REQ, now unused
|
||||||
|
|||||||
@@ -139,6 +139,7 @@ enum class ChipID : icsneoc2_chip_id_t {
|
|||||||
RADGigastar2_ZYNQ = icsneoc2_chip_id_radgigastar2_zynq,
|
RADGigastar2_ZYNQ = icsneoc2_chip_id_radgigastar2_zynq,
|
||||||
SFPModule_ent11100_MCHIP = icsneoc2_chip_id_sfpmodule_ent11100_mchip,
|
SFPModule_ent11100_MCHIP = icsneoc2_chip_id_sfpmodule_ent11100_mchip,
|
||||||
RADArgos_MCHIP = icsneoc2_chip_id_radargos_mchip,
|
RADArgos_MCHIP = icsneoc2_chip_id_radargos_mchip,
|
||||||
|
SFPModule_ad1101_MCHIP = icsneoc2_chip_id_sfpmodule_ad1101_mchip,
|
||||||
Invalid = icsneoc2_chip_id_invalid
|
Invalid = icsneoc2_chip_id_invalid
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -152,6 +152,7 @@ public:
|
|||||||
RADGigastar2 = 57,
|
RADGigastar2 = 57,
|
||||||
SFPModule_ent11100 = 58,
|
SFPModule_ent11100 = 58,
|
||||||
RADArgos = 60,
|
RADArgos = 60,
|
||||||
|
SFPModule_ad1101 = 61,
|
||||||
};
|
};
|
||||||
|
|
||||||
virtual ProductID getProductID() const = 0;
|
virtual ProductID getProductID() const = 0;
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
#ifndef __BOOTLOADER_API_H_
|
||||||
|
#define __BOOTLOADER_API_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
|
||||||
|
#include "icsneo/device/extensions/bootloader/chip.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/parser/fwrepo.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/parser/ief.h"
|
||||||
|
#include "icsneo/device/device.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
using Logger = icsneo::Device::OpenStatusHandler;
|
||||||
|
|
||||||
|
bool requiresUpdate(icsneo::Device& device, const FWRepo& fw);
|
||||||
|
bool update(
|
||||||
|
icsneo::Device& device,
|
||||||
|
const FWRepo& fw,
|
||||||
|
Logger logger = [](icsneo::Device::OpenStatusType, const std::string&, std::optional<double>) { return icsneo::Device::OpenDirective::Continue; },
|
||||||
|
bool alreadyInBootloader = false,
|
||||||
|
bool forceFlash = false
|
||||||
|
);
|
||||||
|
|
||||||
|
bool enterBootloader(icsneo::Device& device);
|
||||||
|
bool exitBootloader(icsneo::Device& device, Logger logger, bool updatedChips);
|
||||||
|
bool verifyConnection(icsneo::Device& device, Logger logger);
|
||||||
|
|
||||||
|
bool isSFPChip(const icsneo::Bootloader::Chip& chip);
|
||||||
|
bool isSFPIEF(const icsneo::Bootloader::IEF& ief);
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
|
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#endif // __BOOTLOADER_API_H_
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
#ifndef __BOOTLOADER_BYTESVIEW_H_
|
||||||
|
#define __BOOTLOADER_BYTESVIEW_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
class BytesView {
|
||||||
|
private:
|
||||||
|
const uint8_t* ptr = nullptr;
|
||||||
|
size_t length = 0;
|
||||||
|
public:
|
||||||
|
BytesView() = default;
|
||||||
|
BytesView(const uint8_t* buf, size_t size) : ptr(buf), length(size) {}
|
||||||
|
|
||||||
|
const uint8_t* begin() const {
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
const uint8_t* end() const {
|
||||||
|
return ptr + length;
|
||||||
|
}
|
||||||
|
|
||||||
|
const uint8_t* data() const {
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t size() const {
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
const uint8_t& operator[](size_t index) const {
|
||||||
|
return ptr[index];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
|
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#endif // __BOOTLOADER_BYTESVIEW_H_
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
#ifndef __BOOTLOADER_CHIP_H_
|
||||||
|
#define __BOOTLOADER_CHIP_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
|
||||||
|
#include "icsneo/device/chipinfo.h"
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
struct Chip {
|
||||||
|
uint8_t expansionSlot = 0;
|
||||||
|
ChipInfo info;
|
||||||
|
|
||||||
|
Chip(const ChipInfo& info) : info(info) {}
|
||||||
|
|
||||||
|
ChipID getID() const { return info.id; }
|
||||||
|
std::string getName() const { return info.name ? info.name : ""; }
|
||||||
|
std::string getIEFName() const { return info.iefName ? info.iefName : ""; }
|
||||||
|
const std::vector<const char*>& getMultiIEFName() const { return info.iefSegments; }
|
||||||
|
FirmwareType getFirmwareType() const { return info.fwType; }
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
|
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#endif // __BOOTLOADER_CHIP_H_
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
#ifndef __BOOTLOADER_APPLICATION_H_
|
||||||
|
#define __BOOTLOADER_APPLICATION_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
|
||||||
|
#include "icsneo/device/extensions/bootloader/communication/communication.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/parser/ief.h"
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <functional>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The software update protocol used with the application
|
||||||
|
* on RED devices, does not require entering the bootloader.
|
||||||
|
*/
|
||||||
|
class Application : public Communication {
|
||||||
|
public:
|
||||||
|
Application(icsneo::Device& dev, const ChipInfo& chip, bool doAuthChecks = false, bool encrypt = false)
|
||||||
|
: Communication(dev), targetChip(chip), doAuthChecks(doAuthChecks), encrypted(encrypt) {}
|
||||||
|
|
||||||
|
void setExpansionSlot(uint8_t slot) { targetChip.expansionSlot = slot; }
|
||||||
|
|
||||||
|
bool enterApplication() override;
|
||||||
|
bool hasFinalizationStep() const override {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool finalize() override;
|
||||||
|
bool flash(const FWRepo& repo, const icsneo::Device::OpenStatusHandler& handler) override;
|
||||||
|
|
||||||
|
bool open() override {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool close() override {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool authenticate();
|
||||||
|
bool sendSoftware(const std::vector<uint8_t>& bin, const icsneo::Device::OpenStatusHandler& handler);
|
||||||
|
bool sendSoftware(const std::vector<IEF::Sector>& sectors, uint32_t blockSize, const icsneo::Device::OpenStatusHandler& handler);
|
||||||
|
bool validateFirmware(const DeviceAppVersion& chipSrcVer);
|
||||||
|
bool validateComponent();
|
||||||
|
|
||||||
|
private:
|
||||||
|
// Sub-commands of ExtendedCommand::SoftwareUpdate
|
||||||
|
enum class Update : uint8_t {
|
||||||
|
Write = 0,
|
||||||
|
Erase = 1,
|
||||||
|
GetProgress = 2,
|
||||||
|
CheckVersion = 5,
|
||||||
|
ValidateComponent = 6,
|
||||||
|
Finalize = 7,
|
||||||
|
CheckComponentVersion = 9
|
||||||
|
};
|
||||||
|
|
||||||
|
// Prefixes the arguments of every ExtendedCommand::SoftwareUpdate request, and is echoed back with progress
|
||||||
|
#pragma pack(push, 2)
|
||||||
|
struct SoftwareUpdateCommandHeader {
|
||||||
|
uint32_t componentIdentifier; // unique id for the software component - analogous to what used to be "chip id"
|
||||||
|
Update commandType;
|
||||||
|
uint8_t expansionSlot;
|
||||||
|
uint32_t offset;
|
||||||
|
uint32_t commandSizeOrProgress; // size
|
||||||
|
};
|
||||||
|
#pragma pack(pop)
|
||||||
|
|
||||||
|
Chip targetChip;
|
||||||
|
const bool doAuthChecks;
|
||||||
|
const bool encrypted;
|
||||||
|
|
||||||
|
using StatusHandler = std::function<void(double progress)>;
|
||||||
|
bool sendCommand(Update command, const StatusHandler& handler, uint32_t offset = 0,
|
||||||
|
const std::vector<uint8_t>& args = {}, std::chrono::milliseconds timeout = std::chrono::milliseconds(3000)) {
|
||||||
|
return sendCommand(command, offset, static_cast<uint32_t>(args.size()), handler, timeout, args);
|
||||||
|
}
|
||||||
|
bool sendCommand(Update command, uint32_t offset, uint32_t length, const StatusHandler& handler,
|
||||||
|
std::chrono::milliseconds timeout = std::chrono::milliseconds(3000), std::vector<uint8_t> args = {});
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
|
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#endif // __BOOTLOADER_APPLICATION_H_
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
#ifndef __BOOTLOADER_BOOTLOADERVERSION_H_
|
||||||
|
#define __BOOTLOADER_BOOTLOADERVERSION_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
#pragma pack(push, 1)
|
||||||
|
struct BootloaderVersionResponse {
|
||||||
|
uint8_t blMajor;
|
||||||
|
uint8_t blMinor;
|
||||||
|
uint16_t reserved1;
|
||||||
|
uint8_t hwMajor;
|
||||||
|
uint8_t hwMinor;
|
||||||
|
uint8_t hwPlatform;
|
||||||
|
uint16_t reserved2;
|
||||||
|
uint8_t manDay;
|
||||||
|
uint8_t manMonth;
|
||||||
|
uint16_t manYear;
|
||||||
|
uint8_t serialNum[8];
|
||||||
|
uint8_t hwId;
|
||||||
|
uint8_t reserved3;
|
||||||
|
uint8_t zynqId;
|
||||||
|
};
|
||||||
|
#pragma pack(pop)
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
|
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#endif // __BOOTLOADER_BOOTLOADERVERSION_H_
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
#ifndef __BOOTLOADER_COMMUNICATION_H_
|
||||||
|
#define __BOOTLOADER_COMMUNICATION_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
|
||||||
|
#include "icsneo/device/extensions/bootloader/chip.h"
|
||||||
|
#include "icsneo/device/device.h"
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
class FWRepo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A bootloader protocol for one chip.
|
||||||
|
*
|
||||||
|
* Authentication is a challenge/response handled by libicsbl: icsbl_generate_seed_v1() derives
|
||||||
|
* a challenge (seed.a) and the reply the device must send back (seed.b) from the serial number.
|
||||||
|
* Firmware is encrypted for a specific device with icsbl_encrypt_v1(), also keyed by the serial number.
|
||||||
|
*/
|
||||||
|
class Communication {
|
||||||
|
public:
|
||||||
|
Communication(icsneo::Device& dev) : device(dev) {}
|
||||||
|
virtual ~Communication() = default;
|
||||||
|
|
||||||
|
virtual bool enterApplication() = 0;
|
||||||
|
virtual bool hasFinalizationStep() const = 0;
|
||||||
|
virtual bool finalize() = 0;
|
||||||
|
virtual bool flash(const FWRepo& repo, const icsneo::Device::OpenStatusHandler& handler) = 0;
|
||||||
|
|
||||||
|
virtual bool open() = 0;
|
||||||
|
virtual bool close() = 0;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
static uint32_t readLE32(const uint8_t* data);
|
||||||
|
static void writeLE32(uint8_t* data, uint32_t val);
|
||||||
|
icsneo::Device& device;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
|
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#endif // __BOOTLOADER_COMMUNICATION_H_
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
#ifndef __BOOTLOADER_RADCOMMUNICATION_H_
|
||||||
|
#define __BOOTLOADER_RADCOMMUNICATION_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
|
||||||
|
#include "icsneo/device/extensions/bootloader/communication/communication.h"
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The bootloader protocol used by RAD devices.
|
||||||
|
*
|
||||||
|
* Requests are a command byte followed by the payload, responses are the command byte,
|
||||||
|
* a Response::Type byte and then the payload. Newer RAD bootloaders (the "Zynq binary
|
||||||
|
* flasher") also prefix each request with ZynqFlasherSync and a 16-bit little-endian
|
||||||
|
* length of the command byte plus payload.
|
||||||
|
*/
|
||||||
|
class RADCommunication : public Communication {
|
||||||
|
protected:
|
||||||
|
static constexpr size_t MaxMessageLength = 1518;
|
||||||
|
static constexpr uint8_t ZynqFlasherSync[4] = {0xAA, 0xAA, 0x55, 0x55};
|
||||||
|
static constexpr size_t ZynqFlasherHeaderSize = sizeof(ZynqFlasherSync) + sizeof(uint16_t);
|
||||||
|
|
||||||
|
struct Request {
|
||||||
|
enum class Type : uint8_t {
|
||||||
|
BootloaderVersion = 0x01,
|
||||||
|
EnterApplication = 0x02,
|
||||||
|
BeginTransfer = 0x03,
|
||||||
|
FWPayload = 0x04,
|
||||||
|
EndTransfer = 0x05,
|
||||||
|
ProgramFlash = 0x06,
|
||||||
|
DownloadedBinInfo = 0x07,
|
||||||
|
ProgrammingStatus = 0x08,
|
||||||
|
Authenticate = 0x09,
|
||||||
|
SanityCheck = 0x0A,
|
||||||
|
VerifyCRC = 0x0B,
|
||||||
|
ValidateVersion = 0x0F,
|
||||||
|
Invalid = 0xFF
|
||||||
|
} type = Type::Invalid;
|
||||||
|
|
||||||
|
uint8_t* payload = nullptr;
|
||||||
|
size_t expectedResponseLen = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
Request makeRequest(Request::Type type, size_t payloadSize = 0, size_t expectedResponseLen = 0);
|
||||||
|
|
||||||
|
struct Response {
|
||||||
|
enum class Type {
|
||||||
|
Failure = 0x00,
|
||||||
|
Success = 0x01
|
||||||
|
} type = Type::Failure;
|
||||||
|
|
||||||
|
uint8_t* payload = nullptr;
|
||||||
|
size_t size = 0;
|
||||||
|
|
||||||
|
bool done = false;
|
||||||
|
|
||||||
|
operator bool() const {
|
||||||
|
return done && type == Type::Success;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Response& sendRequest(const Request& request, std::chrono::milliseconds timeout = std::chrono::milliseconds(2000));
|
||||||
|
bool sendRequestNoResponse();
|
||||||
|
|
||||||
|
icsneo::Device::OpenStatusHandler logger; // Progress updates, set in flash()
|
||||||
|
Response lastResponse; // Last response received from sendRequest
|
||||||
|
bool supportsZynqBinaryFlasher = false; // Dictates how messages are formatted, this gets set in initialize()
|
||||||
|
|
||||||
|
std::vector<uint8_t> rxBuffer;
|
||||||
|
std::vector<uint8_t> txBuffer;
|
||||||
|
Chip targetChip; // Chip to flash
|
||||||
|
|
||||||
|
bool loadZips(const FWRepo& fw, std::vector<uint8_t>& chipSoftware);
|
||||||
|
|
||||||
|
bool initialize();
|
||||||
|
bool sanityCheck();
|
||||||
|
bool getBootloaderVersion();
|
||||||
|
bool validateChipVersion(const icsneo::DeviceAppVersion& srcVer);
|
||||||
|
virtual bool authenticate();
|
||||||
|
bool sendSoftware(const std::vector<uint8_t>& chipSoftware);
|
||||||
|
bool getProgrammingStatus(size_t sizeToValidate);
|
||||||
|
bool supportsFlashBuffer() const;
|
||||||
|
public:
|
||||||
|
RADCommunication(icsneo::Device& dev, const ChipInfo& chipToFlash);
|
||||||
|
|
||||||
|
bool enterApplication() override;
|
||||||
|
bool finalize() override;
|
||||||
|
bool flash(const FWRepo& fw, const icsneo::Device::OpenStatusHandler& handler) override;
|
||||||
|
|
||||||
|
bool open() override;
|
||||||
|
bool close() override;
|
||||||
|
bool hasFinalizationStep() const override;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
|
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#endif // __BOOTLOADER_RADCOMMUNICATION_H_
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
#ifndef __BOOTLOADER_RADMULTICHIPCOMMUNICATION_H_
|
||||||
|
#define __BOOTLOADER_RADMULTICHIPCOMMUNICATION_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
|
||||||
|
#include "icsneo/device/extensions/bootloader/communication/radcommunication.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/communication/application.h"
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
class RADMultiChipCommunication : public RADCommunication {
|
||||||
|
private:
|
||||||
|
std::unique_ptr<Application> standardComm;
|
||||||
|
const bool doAuthChecks;
|
||||||
|
const uint8_t slot;
|
||||||
|
bool authenticate() override;
|
||||||
|
public:
|
||||||
|
RADMultiChipCommunication(icsneo::Device& dev, const ChipInfo& chipToFlash, bool auth, uint8_t slot = UINT8_MAX);
|
||||||
|
|
||||||
|
bool flash(const FWRepo& fw, const icsneo::Device::OpenStatusHandler& handler) override;
|
||||||
|
bool open() override;
|
||||||
|
bool close() override;
|
||||||
|
bool enterApplication() override;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
|
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#endif // __BOOTLOADER_RADMULTICHIPCOMMUNICATION_H_
|
||||||
@@ -0,0 +1,96 @@
|
|||||||
|
#ifndef __BOOTLOADER_RADPERIPHCOMMUNICATION_H_
|
||||||
|
#define __BOOTLOADER_RADPERIPHCOMMUNICATION_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
|
||||||
|
#include "icsneo/device/extensions/bootloader/communication/communication.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/communication/bootloaderversion.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/parser/ief.h"
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The bootloader protocol used by peripheral chips on the RAD-Galaxy 2, tunneled through
|
||||||
|
* Command::PeripheralBootloader. Requests are the chip id, the Request and the arguments.
|
||||||
|
* Replies are a byte, ReplySuccess, then for bootloader requests ReplyAccepted, then the payload.
|
||||||
|
*/
|
||||||
|
class RADPeriphCommunication : public Communication {
|
||||||
|
protected:
|
||||||
|
static constexpr size_t ReplySuccessOffset = 1;
|
||||||
|
static constexpr size_t ReplyAcceptedOffset = 2;
|
||||||
|
static constexpr uint8_t ReplySuccess = 0x01;
|
||||||
|
static constexpr uint8_t ReplyAccepted = 0x77;
|
||||||
|
static constexpr size_t ReplyHeaderSize = 3;
|
||||||
|
// Requests that write to the bootloader have three leading zero bytes before their arguments
|
||||||
|
static constexpr size_t BootloaderArgsOffset = 3;
|
||||||
|
// Flash sectors are sent as two requests, the first with this many bytes and the second with the rest
|
||||||
|
static constexpr size_t FlashSectorFirstChunkSize = 1024;
|
||||||
|
|
||||||
|
enum class Mode : uint8_t {
|
||||||
|
NoAnswer = 0x00u,
|
||||||
|
MainApplication = 0xAFu,
|
||||||
|
Bootloader = 0xFEu
|
||||||
|
} mode = Mode::NoAnswer;
|
||||||
|
|
||||||
|
Chip targetChip;
|
||||||
|
uint32_t serialNumber = 0;
|
||||||
|
public:
|
||||||
|
enum class Request : uint8_t {
|
||||||
|
SwitchToApp = 0x01u,
|
||||||
|
SwitchToBootloader = 0x02u,
|
||||||
|
Version = 0x12u,
|
||||||
|
Authenticate = 0x13u,
|
||||||
|
FlashSector = 0x14u,
|
||||||
|
FirmwareValidate = 0x16u,
|
||||||
|
Erase = 0x20u,
|
||||||
|
Echo = 0xAFu,
|
||||||
|
Mode = 0xADu,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Response {
|
||||||
|
bool success = false;
|
||||||
|
std::vector<uint8_t> data;
|
||||||
|
|
||||||
|
operator bool() const {
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
bool waitForConnect(std::chrono::milliseconds timeout = std::chrono::milliseconds(30000));
|
||||||
|
|
||||||
|
bool sendRequestNoResponse(Request req, std::vector<uint8_t>&& args = {});
|
||||||
|
bool sendRequestNoResponse(Request req, ChipID id, std::vector<uint8_t>&& args = {});
|
||||||
|
Response sendRequest(Request req, std::vector<uint8_t>&& args = {}, std::chrono::milliseconds timeout = std::chrono::milliseconds(2000));
|
||||||
|
Response sendRequest(Request req, ChipID id, std::vector<uint8_t>&& args = {}, std::chrono::milliseconds timeout = std::chrono::milliseconds(2000));
|
||||||
|
|
||||||
|
Mode getMode();
|
||||||
|
bool sendPeriphTest();
|
||||||
|
bool readSerialNumber();
|
||||||
|
bool authenticate();
|
||||||
|
bool validateFirmware();
|
||||||
|
bool flashSector(const IEF::Sector& sector);
|
||||||
|
|
||||||
|
RADPeriphCommunication(icsneo::Device& dev, const ChipInfo& chipToFlash);
|
||||||
|
~RADPeriphCommunication();
|
||||||
|
|
||||||
|
bool flash(const FWRepo& fw, const icsneo::Device::OpenStatusHandler& handler) override;
|
||||||
|
bool open() override;
|
||||||
|
bool close() override;
|
||||||
|
bool enterApplication() override;
|
||||||
|
bool finalize() override;
|
||||||
|
bool hasFinalizationStep() const override;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
|
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#endif // __BOOTLOADER_RADPERIPHCOMMUNICATION_H_
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
#ifndef __BOOTLOADER_RED_H_
|
||||||
|
#define __BOOTLOADER_RED_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
|
||||||
|
#include "icsneo/device/extensions/bootloader/communication/redbase.h"
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The bootloader communication protocol used for non-FPGA
|
||||||
|
* chips on neoVI and ValueCAN devices.
|
||||||
|
*
|
||||||
|
* Zynq doesn't count as an FPGA chip for the sake of
|
||||||
|
* this, so it's handled here.
|
||||||
|
*/
|
||||||
|
class REDCommunication : public REDCommunicationBase {
|
||||||
|
public:
|
||||||
|
REDCommunication(icsneo::Device& dev, const ChipInfo& chip, bool auth, bool encryptWithDeviceKey = true);
|
||||||
|
bool enterApplication() override { return sendCommand(uint8_t(Command::EnterApplication)); }
|
||||||
|
bool flash(const FWRepo& fw, const icsneo::Device::OpenStatusHandler& handler) override;
|
||||||
|
bool open() override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
const bool doAuthChecks;
|
||||||
|
const bool encryptFirmwareWithDeviceKey;
|
||||||
|
size_t blockSize = 2048;
|
||||||
|
|
||||||
|
enum class Command : uint8_t {
|
||||||
|
GetVersion = 0x12,
|
||||||
|
Authenticate = 0x13,
|
||||||
|
Flash = 0x14,
|
||||||
|
ValidateFirmware = 0x16,
|
||||||
|
Erase = 0x20,
|
||||||
|
EnterApplication = 0x21,
|
||||||
|
ValidateSWVersion = 0x27,
|
||||||
|
};
|
||||||
|
|
||||||
|
// The reply to Command::GetVersion
|
||||||
|
struct VersionResponse {
|
||||||
|
uint8_t bl_major, bl_minor, app_major, app_minor, flags, prod_id;
|
||||||
|
uint8_t hardware_rev_major, hardware_rev_minor;
|
||||||
|
uint8_t man_day, man_month;
|
||||||
|
uint16_t man_year;
|
||||||
|
uint8_t serial_num[8];
|
||||||
|
uint16_t block_size;
|
||||||
|
uint8_t last_error_code;
|
||||||
|
uint8_t pad_byte;
|
||||||
|
|
||||||
|
static constexpr size_t EncodedSize = 24;
|
||||||
|
|
||||||
|
VersionResponse() = default;
|
||||||
|
VersionResponse(const REDPacketizer::RxFrame& r);
|
||||||
|
} currentVersion;
|
||||||
|
|
||||||
|
// Called from open()
|
||||||
|
bool authenticate(bool firstTime = true);
|
||||||
|
bool getVersion();
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
|
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#endif // __BOOTLOADER_RED_H_
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
#ifndef __BOOTLOADER_REDBASE_H_
|
||||||
|
#define __BOOTLOADER_REDBASE_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
|
||||||
|
#include "icsneo/device/extensions/bootloader/communication/communication.h"
|
||||||
|
#include "icsneo/device/extensions/bootloader/communication/redpacketizer.h"
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implementation shared by REDCommunication and REDCoreCommunication
|
||||||
|
*
|
||||||
|
* Requests are framed as:
|
||||||
|
* uint8_t REDPacketizer::Magic
|
||||||
|
* uint16_t chip id (little-endian)
|
||||||
|
* uint16_t length of the command byte, arguments and padding (little-endian)
|
||||||
|
* uint8_t command
|
||||||
|
* uint8_t arguments[]
|
||||||
|
* uint8_t optional PaddingByte, see sendCommandRaw()
|
||||||
|
*/
|
||||||
|
class REDCommunicationBase : public Communication {
|
||||||
|
public:
|
||||||
|
REDCommunicationBase(icsneo::Device& dev, const ChipInfo& chip);
|
||||||
|
~REDCommunicationBase() {
|
||||||
|
// This must happen before the Packetizer is destroyed
|
||||||
|
device.com->resumeReads();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool open() override;
|
||||||
|
bool close() override;
|
||||||
|
bool hasFinalizationStep() const override {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
bool finalize() override {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
static constexpr uint8_t PaddingByte = 0x55;
|
||||||
|
|
||||||
|
Chip targetChip;
|
||||||
|
|
||||||
|
std::vector<REDPacketizer::RxFrame> lastData;
|
||||||
|
REDPacketizer rp;
|
||||||
|
|
||||||
|
bool handleData();
|
||||||
|
bool sendCommand(uint8_t cmd, std::chrono::seconds timeout, bool clearLastResponse = true) { return sendCommand(cmd, {}, timeout, clearLastResponse); }
|
||||||
|
bool sendCommand(uint8_t cmd, std::vector<uint8_t> args = {}, std::chrono::seconds timeout = std::chrono::seconds(1), bool clearLastResponse = true);
|
||||||
|
virtual bool sendCommandRaw(uint8_t cmd, std::vector<uint8_t> args = {});
|
||||||
|
bool waitForRx(std::chrono::milliseconds timeout);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
|
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#endif // __BOOTLOADER_REDBASE_H_
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
#ifndef __BOOTLOADER_REDCORE_H_
|
||||||
|
#define __BOOTLOADER_REDCORE_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
|
||||||
|
#include "icsneo/device/extensions/bootloader/communication/redbase.h"
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The bootloader communication protocol used for FPGA
|
||||||
|
* chips on neoVI and ValueCAN devices.
|
||||||
|
*
|
||||||
|
* Zynq doesn't count as an FPGA chip for the sake of
|
||||||
|
* this, it's just targetting the non-hybrid FPGAs.
|
||||||
|
*
|
||||||
|
* This was previously called FIRE 2 Core Flasher, but
|
||||||
|
* it does more than just FIRE 2 nowadays.
|
||||||
|
*/
|
||||||
|
class REDCoreCommunication : public REDCommunicationBase {
|
||||||
|
public:
|
||||||
|
using REDCommunicationBase::REDCommunicationBase;
|
||||||
|
|
||||||
|
bool enterApplication() override { return false; }
|
||||||
|
bool flash(const FWRepo& fw, const icsneo::Device::OpenStatusHandler& handler) override;
|
||||||
|
private:
|
||||||
|
/**
|
||||||
|
* Core flasher commands are tunneled through a RED request using CoreFlasherCommand,
|
||||||
|
* the arguments are CoreFlasherMagic, a 16-bit little-endian length, then the core command and its arguments
|
||||||
|
*/
|
||||||
|
static constexpr uint8_t CoreFlasherCommand = 0xCD;
|
||||||
|
static constexpr uint8_t CoreFlasherMagic = 0xEF;
|
||||||
|
|
||||||
|
enum class Command : uint8_t {
|
||||||
|
SendFirmware = 0,
|
||||||
|
StartFirmwareFlash = 1,
|
||||||
|
GetFlashProgress = 2,
|
||||||
|
};
|
||||||
|
|
||||||
|
bool sendCommandRaw(uint8_t cmd, std::vector<uint8_t> args = {}) override;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
|
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#endif // __BOOTLOADER_REDCORE_H_
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
#ifndef __BOOTLOADER_REDPACKETIZER_H_
|
||||||
|
#define __BOOTLOADER_REDPACKETIZER_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
|
||||||
|
#include "icsneo/core/ringbuffer.h"
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Splits bytes from a RED bootloader into frames:
|
||||||
|
* uint8_t Magic
|
||||||
|
* uint8_t Response
|
||||||
|
* uint16_t length (little-endian)
|
||||||
|
* uint8_t data[length]
|
||||||
|
*/
|
||||||
|
class REDPacketizer {
|
||||||
|
public:
|
||||||
|
static constexpr uint8_t Magic = 0xAA;
|
||||||
|
|
||||||
|
enum class Response : uint8_t {
|
||||||
|
Accepted = 0x77,
|
||||||
|
Rejected = 0x7F,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct RxFrame {
|
||||||
|
uint16_t len = 0;
|
||||||
|
Response resp = Response::Rejected;
|
||||||
|
std::vector<uint8_t> data;
|
||||||
|
bool done = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Call with bytes received from the device.
|
||||||
|
* The returned boolean indicates if there is output waiting in output()
|
||||||
|
*/
|
||||||
|
bool input(icsneo::RingBuffer& bytes);
|
||||||
|
std::vector<RxFrame> output();
|
||||||
|
private:
|
||||||
|
enum ReadState { HEADER, CMD, LENGTH_LOWER, LENGTH_UPPER, DATA };
|
||||||
|
ReadState state = HEADER;
|
||||||
|
std::vector<RxFrame> working;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
|
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#endif // __BOOTLOADER_REDPACKETIZER_H_
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
#ifndef __BOOTLOADER_FWBIN_H_
|
||||||
|
#define __BOOTLOADER_FWBIN_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
|
||||||
|
#include "icsneo/device/extensions/bootloader/parser/fwrepo.h"
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <fstream>
|
||||||
|
#include <map>
|
||||||
|
#include <optional>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
class FWBin : public FWRepo {
|
||||||
|
public:
|
||||||
|
FWBin() = default;
|
||||||
|
|
||||||
|
bool load(const std::string& path);
|
||||||
|
|
||||||
|
const FirmwareSourceVersion& getVersion() const override { return version; }
|
||||||
|
bool getFW(const std::string& iefName, std::vector<uint8_t>& fw) const override;
|
||||||
|
std::optional<icsneo::DeviceAppVersion> getChipVersion(const std::string& iefName) const override;
|
||||||
|
|
||||||
|
static uint32_t HashDjb2(const std::string& str);
|
||||||
|
|
||||||
|
private:
|
||||||
|
static constexpr const uint32_t SignatureHash = 0;
|
||||||
|
|
||||||
|
struct VersionTags {
|
||||||
|
const char* majorTag = nullptr;
|
||||||
|
const char* minorTag = nullptr;
|
||||||
|
|
||||||
|
operator bool() const {
|
||||||
|
return majorTag && minorTag;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static VersionTags GetVersionTags(const std::string& iefName);
|
||||||
|
|
||||||
|
struct TagData {
|
||||||
|
enum class Type : uint8_t {
|
||||||
|
DataBytes = 0,
|
||||||
|
DataLength = 1,
|
||||||
|
IEFVersion = 2,
|
||||||
|
Signature = 3,
|
||||||
|
Unknown = 0xFF
|
||||||
|
};
|
||||||
|
|
||||||
|
Type type = Type::Unknown;
|
||||||
|
std::streampos arrayPosition = 0;
|
||||||
|
uint32_t arrayLength = 0;
|
||||||
|
|
||||||
|
bool read(std::ifstream& stream, std::vector<uint8_t>& out) const;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool readTag(const std::string& tag, std::vector<uint8_t>& out) const;
|
||||||
|
|
||||||
|
mutable std::ifstream file;
|
||||||
|
FirmwareSourceVersion version;
|
||||||
|
std::map<uint32_t, TagData> tags;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
|
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#endif // __BOOTLOADER_FWBIN_H_
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
#ifndef __BOOTLOADER_FWBUNDLE_H_
|
||||||
|
#define __BOOTLOADER_FWBUNDLE_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
|
||||||
|
#include "icsneo/device/extensions/bootloader/parser/fwrepo.h"
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <optional>
|
||||||
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
class FWBundle : public FWRepo {
|
||||||
|
public:
|
||||||
|
FWBundle() = default;
|
||||||
|
|
||||||
|
bool load(const std::string& path);
|
||||||
|
static bool IsFWBundle(const std::string& path);
|
||||||
|
|
||||||
|
const FirmwareSourceVersion& getVersion() const override { return version; }
|
||||||
|
bool getFW(const std::string& iefName, std::vector<uint8_t>& fw) const override;
|
||||||
|
std::optional<icsneo::DeviceAppVersion> getChipVersion(const std::string& iefName) const override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
struct ChipBinary {
|
||||||
|
std::vector<uint8_t> data;
|
||||||
|
FirmwareSourceVersion version;
|
||||||
|
};
|
||||||
|
|
||||||
|
FirmwareSourceVersion version;
|
||||||
|
std::unordered_map<uint32_t, ChipBinary> binaries;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
|
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#endif // __BOOTLOADER_FWBUNDLE_H_
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
#ifndef __BOOTLOADER_FWLOADER_H_
|
||||||
|
#define __BOOTLOADER_FWLOADER_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
|
||||||
|
#include "icsneo/device/extensions/bootloader/parser/fwrepo.h"
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
class FWLoader {
|
||||||
|
public:
|
||||||
|
// Detects whether the file is a firmware bundle or a fw.bin and loads it, returns nullptr on failure
|
||||||
|
static std::unique_ptr<FWRepo> Load(const std::string& path);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
|
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#endif // __BOOTLOADER_FWLOADER_H_
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
#ifndef __BOOTLOADER_FWREPO_H_
|
||||||
|
#define __BOOTLOADER_FWREPO_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
|
||||||
|
#include "icsneo/device/extensions/bootloader/version.h"
|
||||||
|
#include "icsneo/device/deviceversion.h"
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <optional>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
class FWRepo {
|
||||||
|
public:
|
||||||
|
FWRepo() = default;
|
||||||
|
virtual ~FWRepo() = default;
|
||||||
|
|
||||||
|
virtual bool getFW(const std::string& iefName, std::vector<uint8_t>& fw) const = 0;
|
||||||
|
virtual std::optional<icsneo::DeviceAppVersion> getChipVersion(const std::string& iefName) const = 0;
|
||||||
|
virtual const FirmwareSourceVersion& getVersion() const = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
|
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#endif // __BOOTLOADER_FWREPO_H_
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
#ifndef __BOOTLOADER_FWZIP_H_
|
||||||
|
#define __BOOTLOADER_FWZIP_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <memory>
|
||||||
|
#include <optional>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
class ZipHandle;
|
||||||
|
|
||||||
|
class FWZip {
|
||||||
|
private:
|
||||||
|
std::unique_ptr<ZipHandle> handle;
|
||||||
|
std::vector<uint8_t> zipData;
|
||||||
|
std::optional<size_t> uncompressedSize = std::nullopt;
|
||||||
|
public:
|
||||||
|
FWZip(std::vector<uint8_t>&& tagData);
|
||||||
|
~FWZip();
|
||||||
|
|
||||||
|
std::optional<size_t> size() const { return uncompressedSize; }
|
||||||
|
bool readAll(uint8_t* out);
|
||||||
|
bool readAll(std::vector<uint8_t>& out);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
|
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#endif // __BOOTLOADER_FWZIP_H_
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
#ifndef __BOOTLOADER_IEF_H_
|
||||||
|
#define __BOOTLOADER_IEF_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
|
||||||
|
#include "icsneo/device/extensions/bootloader/bytesview.h"
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <utility>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
class IEF {
|
||||||
|
public:
|
||||||
|
struct Version {
|
||||||
|
uint8_t appMajor;
|
||||||
|
uint8_t appMinor;
|
||||||
|
uint8_t prodId;
|
||||||
|
uint8_t chipId;
|
||||||
|
};
|
||||||
|
using Sector = std::pair<uint32_t, BytesView>;
|
||||||
|
|
||||||
|
IEF() = default;
|
||||||
|
|
||||||
|
bool load(std::vector<uint8_t>&& bytes);
|
||||||
|
|
||||||
|
const Version& getVersion() const { return version; }
|
||||||
|
const std::vector<Sector>& getSectors() const { return sectors; }
|
||||||
|
// IEFs are comprised of equally sized blocks of data - take the size of the first
|
||||||
|
uint32_t getBlockSize() const { return sectors.empty() ? 0 : static_cast<uint32_t>(sectors.front().second.size()); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<uint8_t> data;
|
||||||
|
Version version = {};
|
||||||
|
std::vector<Sector> sectors;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
|
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#endif // __BOOTLOADER_IEF_H_
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
#ifndef __BOOTLOADER_LOGGING_H_
|
||||||
|
#define __BOOTLOADER_LOGGING_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
namespace Log {
|
||||||
|
|
||||||
|
void Trace(const std::string& subject, const std::string& msg);
|
||||||
|
void Debug(const std::string& subject, const std::string& msg);
|
||||||
|
void Info(const std::string& subject, const std::string& msg);
|
||||||
|
void Warning(const std::string& subject, const std::string& msg);
|
||||||
|
void Error(const std::string& subject, const std::string& msg);
|
||||||
|
|
||||||
|
} // namespace Log
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
|
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#endif // __BOOTLOADER_LOGGING_H_
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
#ifndef __BOOTLOADER_VERSION_H_
|
||||||
|
#define __BOOTLOADER_VERSION_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace icsneo {
|
||||||
|
|
||||||
|
namespace Bootloader {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tracks the Vehicle Spy 3 version that a firmware
|
||||||
|
* package is intended for.
|
||||||
|
*/
|
||||||
|
struct FirmwareSourceVersion {
|
||||||
|
using ValueType = uint8_t;
|
||||||
|
ValueType major = 0;
|
||||||
|
ValueType minor = 0;
|
||||||
|
ValueType release = 0;
|
||||||
|
ValueType build = 0;
|
||||||
|
bool load(const std::string& toParse);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Bootloader
|
||||||
|
|
||||||
|
} // namespace icsneo
|
||||||
|
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#endif // __BOOTLOADER_VERSION_H_
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
#ifndef __ICSNEO_DEVICE_EXTENSIONS_BUILTIN_H_
|
|
||||||
#define __ICSNEO_DEVICE_EXTENSIONS_BUILTIN_H_
|
|
||||||
|
|
||||||
@LIBICSNEO_EXT_CODE_INCS@
|
|
||||||
#include "icsneo/device/device.h"
|
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
namespace icsneo {
|
|
||||||
|
|
||||||
static void AddBuiltInExtensionsTo(const std::shared_ptr<icsneo::Device>& device) {
|
|
||||||
(void)device; // [[maybe_unused]]
|
|
||||||
@LIBICSNEO_EXT_CODE@
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -41,7 +41,8 @@ public:
|
|||||||
{ChipID::SFPModule_88q2221_MCHIP, true, "SFP-MV2221M", "sfp_mv2221m_b2_mchip_ief", 3, FirmwareType::IEF},
|
{ChipID::SFPModule_88q2221_MCHIP, true, "SFP-MV2221M", "sfp_mv2221m_b2_mchip_ief", 3, FirmwareType::IEF},
|
||||||
{ChipID::SFPModule_lan8670_MCHIP, true, "SFP-MC8670", "sfp_module_lan8670_ief", 3, FirmwareType::IEF},
|
{ChipID::SFPModule_lan8670_MCHIP, true, "SFP-MC8670", "sfp_module_lan8670_ief", 3, FirmwareType::IEF},
|
||||||
{ChipID::SFPModule_88q3244_MCHIP, true, "SFP-MV3244", "sfp_module_88q3244_ief", 3, FirmwareType::IEF},
|
{ChipID::SFPModule_88q3244_MCHIP, true, "SFP-MV3244", "sfp_module_88q3244_ief", 3, FirmwareType::IEF},
|
||||||
{ChipID::SFPModule_ent11100_MCHIP, true, "SFP-EN11100", "sfp_module_ent11100_ief", 3, FirmwareType::IEF}
|
{ChipID::SFPModule_ent11100_MCHIP, true, "SFP-EN11100", "sfp_module_ent11100_ief", 3, FirmwareType::IEF},
|
||||||
|
{ChipID::SFPModule_ad1101_MCHIP, true, "SFP-AD1101", "sfp_module_adi1101_ief", 3, FirmwareType::IEF},
|
||||||
};
|
};
|
||||||
return chips;
|
return chips;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -135,7 +135,8 @@ public:
|
|||||||
{ChipID::SFPModule_88q2221_MCHIP, true, "SFP-MV2221M", "sfp_mv2221m_b2_mchip_ief", 3, FirmwareType::IEF},
|
{ChipID::SFPModule_88q2221_MCHIP, true, "SFP-MV2221M", "sfp_mv2221m_b2_mchip_ief", 3, FirmwareType::IEF},
|
||||||
{ChipID::SFPModule_lan8670_MCHIP, true, "SFP-MC8670", "sfp_module_lan8670_ief", 3, FirmwareType::IEF},
|
{ChipID::SFPModule_lan8670_MCHIP, true, "SFP-MC8670", "sfp_module_lan8670_ief", 3, FirmwareType::IEF},
|
||||||
{ChipID::SFPModule_88q3244_MCHIP, true, "SFP-MV3244", "sfp_module_88q3244_ief", 3, FirmwareType::IEF},
|
{ChipID::SFPModule_88q3244_MCHIP, true, "SFP-MV3244", "sfp_module_88q3244_ief", 3, FirmwareType::IEF},
|
||||||
{ChipID::SFPModule_ent11100_MCHIP, true, "SFP-EN11100", "sfp_module_ent11100_ief", 3, FirmwareType::IEF}
|
{ChipID::SFPModule_ent11100_MCHIP, true, "SFP-EN11100", "sfp_module_ent11100_ief", 3, FirmwareType::IEF},
|
||||||
|
{ChipID::SFPModule_ad1101_MCHIP, true, "SFP-AD1101", "sfp_module_adi1101_ief", 3, FirmwareType::IEF},
|
||||||
};
|
};
|
||||||
|
|
||||||
static std::vector<ChipInfo> t1sChips = {
|
static std::vector<ChipInfo> t1sChips = {
|
||||||
@@ -147,7 +148,8 @@ public:
|
|||||||
{ChipID::SFPModule_88q2221_MCHIP, true, "SFP-MV2221M", "sfp_mv2221m_b2_mchip_ief", 3, FirmwareType::IEF},
|
{ChipID::SFPModule_88q2221_MCHIP, true, "SFP-MV2221M", "sfp_mv2221m_b2_mchip_ief", 3, FirmwareType::IEF},
|
||||||
{ChipID::SFPModule_lan8670_MCHIP, true, "SFP-MC8670", "sfp_module_lan8670_ief", 3, FirmwareType::IEF},
|
{ChipID::SFPModule_lan8670_MCHIP, true, "SFP-MC8670", "sfp_module_lan8670_ief", 3, FirmwareType::IEF},
|
||||||
{ChipID::SFPModule_88q3244_MCHIP, true, "SFP-MV3244", "sfp_module_88q3244_ief", 3, FirmwareType::IEF},
|
{ChipID::SFPModule_88q3244_MCHIP, true, "SFP-MV3244", "sfp_module_88q3244_ief", 3, FirmwareType::IEF},
|
||||||
{ChipID::SFPModule_ent11100_MCHIP, true, "SFP-EN11100", "sfp_module_ent11100_ief", 3, FirmwareType::IEF}
|
{ChipID::SFPModule_ent11100_MCHIP, true, "SFP-EN11100", "sfp_module_ent11100_ief", 3, FirmwareType::IEF},
|
||||||
|
{ChipID::SFPModule_ad1101_MCHIP, true, "SFP-AD1101", "sfp_module_adi1101_ief", 3, FirmwareType::IEF},
|
||||||
};
|
};
|
||||||
|
|
||||||
if(variantToFlash == FirmwareVariant::T1Sx8_CANx4_LINx6) {
|
if(variantToFlash == FirmwareVariant::T1Sx8_CANx4_LINx6) {
|
||||||
|
|||||||
@@ -5,7 +5,6 @@
|
|||||||
|
|
||||||
#include "icsneo/device/device.h"
|
#include "icsneo/device/device.h"
|
||||||
#include "icsneo/device/devicetype.h"
|
#include "icsneo/device/devicetype.h"
|
||||||
#include "icsneo/platform/cdcacm.h"
|
|
||||||
#include "icsneo/device/tree/vividcan/vividcansettings.h"
|
#include "icsneo/device/tree/vividcan/vividcansettings.h"
|
||||||
|
|
||||||
namespace icsneo {
|
namespace icsneo {
|
||||||
|
|||||||
@@ -679,6 +679,7 @@ typedef enum _icsneoc2_chip_id_t {
|
|||||||
icsneoc2_chip_id_radgigastar2_zynq = 131,
|
icsneoc2_chip_id_radgigastar2_zynq = 131,
|
||||||
icsneoc2_chip_id_sfpmodule_ent11100_mchip = 132,
|
icsneoc2_chip_id_sfpmodule_ent11100_mchip = 132,
|
||||||
icsneoc2_chip_id_radargos_mchip = 135,
|
icsneoc2_chip_id_radargos_mchip = 135,
|
||||||
|
icsneoc2_chip_id_sfpmodule_ad1101_mchip = 136,
|
||||||
icsneoc2_chip_id_maxsize, // Must be last entry, Don't use as a chip ID.
|
icsneoc2_chip_id_maxsize, // Must be last entry, Don't use as a chip ID.
|
||||||
|
|
||||||
// Used for unknown or invalid chip ID
|
// Used for unknown or invalid chip ID
|
||||||
|
|||||||
@@ -1,14 +0,0 @@
|
|||||||
#ifndef __CDCACM_H_
|
|
||||||
#define __CDCACM_H_
|
|
||||||
|
|
||||||
#define INTREPID_USB_VENDOR_ID (0x093c)
|
|
||||||
|
|
||||||
#if defined _WIN32
|
|
||||||
#include "icsneo/platform/windows/cdcacm.h"
|
|
||||||
#elif defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
|
|
||||||
#include "icsneo/platform/posix/cdcacm.h"
|
|
||||||
#else
|
|
||||||
#warning "This platform is not supported by the CDC ACM driver"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
#ifndef __DXX_H_
|
|
||||||
#define __DXX_H_
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
|
|
||||||
#include "icsneo/communication/driver.h"
|
|
||||||
#include "icsneo/device/founddevice.h"
|
|
||||||
|
|
||||||
#include "libredxx/libredxx.h"
|
|
||||||
|
|
||||||
namespace icsneo {
|
|
||||||
|
|
||||||
class DXX : public Driver {
|
|
||||||
public:
|
|
||||||
static void Find(std::vector<FoundDevice>& found);
|
|
||||||
|
|
||||||
DXX(const device_eventhandler_t& err, neodevice_t& forDevice, uint16_t pid, libredxx_device_type type);
|
|
||||||
|
|
||||||
bool open() override;
|
|
||||||
|
|
||||||
bool isOpen() override;
|
|
||||||
|
|
||||||
bool close() override;
|
|
||||||
driver_finder_t getFinder() override { return DXX::Find; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
void read();
|
|
||||||
void write();
|
|
||||||
neodevice_t neodevice;
|
|
||||||
uint16_t pid;
|
|
||||||
libredxx_device_type type;
|
|
||||||
libredxx_opened_device* device = nullptr;
|
|
||||||
std::thread readThread;
|
|
||||||
std::thread writeThread;
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // __cplusplus
|
|
||||||
#endif // __DXX_H_
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
#ifndef __PCAP_H_
|
|
||||||
#define __PCAP_H_
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
#include "icsneo/platform/windows/pcap.h"
|
|
||||||
#elif defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
|
|
||||||
#include "icsneo/platform/posix/pcap.h"
|
|
||||||
#else
|
|
||||||
#warning "This platform is not supported by the PCAP driver"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,62 +0,0 @@
|
|||||||
#ifndef __CDCACM_POSIX_H_
|
|
||||||
#define __CDCACM_POSIX_H_
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
|
|
||||||
#include "icsneo/communication/driver.h"
|
|
||||||
#include "icsneo/device/neodevice.h"
|
|
||||||
#include "icsneo/api/eventmanager.h"
|
|
||||||
#include <optional>
|
|
||||||
#include <chrono>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
namespace icsneo {
|
|
||||||
|
|
||||||
class CDCACM : public Driver {
|
|
||||||
public:
|
|
||||||
/**
|
|
||||||
* Note: This is a driver for all devices which use CDC_ACM
|
|
||||||
* Once we find the TTY we want it's a pretty generic POSIX TTY driver, but
|
|
||||||
* the method for finding the TTY we want varies by OS.
|
|
||||||
* On Linux, we read sysfs to find users of the CDC_ACM driver
|
|
||||||
* On macOS, we use IOKit to find the USB device we're looking for
|
|
||||||
* As such platform specific FindByProduct & HandleToTTY code can be found
|
|
||||||
* in cdcacmlinux.cpp and cdcacmdarwin.cpp respectively
|
|
||||||
* Other POSIX systems (BSDs, QNX, etc) will need bespoke code written in the future
|
|
||||||
*/
|
|
||||||
CDCACM(const device_eventhandler_t& err, neodevice_t& forDevice) : Driver(err), device(forDevice) {}
|
|
||||||
~CDCACM();
|
|
||||||
static void Find(std::vector<FoundDevice>& found);
|
|
||||||
|
|
||||||
bool open() override;
|
|
||||||
bool isOpen() override;
|
|
||||||
bool close() override;
|
|
||||||
driver_finder_t getFinder() override { return CDCACM::Find; }
|
|
||||||
|
|
||||||
void modeChangeIncoming() override;
|
|
||||||
void awaitModeChangeComplete() override;
|
|
||||||
|
|
||||||
private:
|
|
||||||
neodevice_t& device;
|
|
||||||
int fd = -1;
|
|
||||||
std::optional<ino_t> disallowedInode;
|
|
||||||
std::atomic<bool> modeChanging{false};
|
|
||||||
std::thread modeChangeThread;
|
|
||||||
std::mutex modeChangeMutex;
|
|
||||||
std::condition_variable modeChangeCV;
|
|
||||||
|
|
||||||
static std::string HandleToTTY(neodevice_handle_t handle);
|
|
||||||
|
|
||||||
std::thread readThread, writeThread;
|
|
||||||
void readTask();
|
|
||||||
void writeTask();
|
|
||||||
|
|
||||||
bool fdIsValid();
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // __cplusplus
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
#ifndef __PCAP_POSIX_H_
|
|
||||||
#define __PCAP_POSIX_H_
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
|
|
||||||
#include "icsneo/device/neodevice.h"
|
|
||||||
#include "icsneo/device/founddevice.h"
|
|
||||||
#include "icsneo/communication/driver.h"
|
|
||||||
#include "icsneo/communication/ethernetpacketizer.h"
|
|
||||||
#include "icsneo/api/eventmanager.h"
|
|
||||||
#include <string>
|
|
||||||
#include <pcap.h>
|
|
||||||
|
|
||||||
namespace icsneo {
|
|
||||||
|
|
||||||
class PCAP : public Driver {
|
|
||||||
public:
|
|
||||||
static void Find(std::vector<FoundDevice>& foundDevices);
|
|
||||||
static std::string GetEthDevSerialFromMacAddress(uint8_t product, uint16_t macSerial);
|
|
||||||
static bool IsHandleValid(neodevice_handle_t handle);
|
|
||||||
|
|
||||||
PCAP(device_eventhandler_t err, neodevice_t& forDevice);
|
|
||||||
bool open() override;
|
|
||||||
bool isOpen() override;
|
|
||||||
bool close() override;
|
|
||||||
bool isEthernet() const override { return true; }
|
|
||||||
driver_finder_t getFinder() override { return PCAP::Find; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
char errbuf[PCAP_ERRBUF_SIZE] = { 0 };
|
|
||||||
neodevice_t& device;
|
|
||||||
uint8_t deviceMAC[6];
|
|
||||||
bool openable = true;
|
|
||||||
EthernetPacketizer ethPacketizer;
|
|
||||||
|
|
||||||
std::thread readThread, writeThread;
|
|
||||||
void readTask();
|
|
||||||
void writeTask();
|
|
||||||
|
|
||||||
class NetworkInterface {
|
|
||||||
public:
|
|
||||||
uint8_t uuid;
|
|
||||||
uint8_t macAddress[6];
|
|
||||||
std::string nameFromPCAP;
|
|
||||||
std::string descriptionFromPCAP;
|
|
||||||
std::string fullName;
|
|
||||||
pcap_t* fp = nullptr;
|
|
||||||
pcap_stat stats;
|
|
||||||
};
|
|
||||||
static std::vector<NetworkInterface> knownInterfaces;
|
|
||||||
NetworkInterface iface;
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // __cplusplus
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
#ifndef __REGISTRY_H_
|
|
||||||
#define __REGISTRY_H_
|
|
||||||
|
|
||||||
#if defined _WIN32
|
|
||||||
#include "icsneo/platform/windows/registry.h"
|
|
||||||
#else
|
|
||||||
#warning "This platform is not supported by the registry driver"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,61 +0,0 @@
|
|||||||
#ifndef __TCP_H_
|
|
||||||
#define __TCP_H_
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
#include "icsneo/communication/driver.h"
|
|
||||||
|
|
||||||
namespace icsneo {
|
|
||||||
|
|
||||||
class TCP : public Driver {
|
|
||||||
public:
|
|
||||||
static void Find(std::vector<FoundDevice>& foundDevices);
|
|
||||||
|
|
||||||
struct NetworkInterface {
|
|
||||||
const std::string name;
|
|
||||||
const uint32_t ip;
|
|
||||||
};
|
|
||||||
|
|
||||||
TCP(const device_eventhandler_t& err, NetworkInterface on, uint32_t dstIP, uint16_t dstPort);
|
|
||||||
~TCP() override { if(isOpen()) close(); }
|
|
||||||
bool open() override;
|
|
||||||
bool isOpen() override;
|
|
||||||
bool close() override;
|
|
||||||
bool isEthernet() const override { return true; }
|
|
||||||
driver_finder_t getFinder() override { return TCP::Find; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
#ifdef _WIN32
|
|
||||||
typedef size_t SocketFileDescriptor;
|
|
||||||
#else
|
|
||||||
typedef int SocketFileDescriptor;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
class Socket {
|
|
||||||
public:
|
|
||||||
Socket(int domain, int type, int protocol, bool nonblocking = true);
|
|
||||||
~Socket();
|
|
||||||
explicit operator bool() const { return fd != -1; }
|
|
||||||
operator SocketFileDescriptor() const { return fd; }
|
|
||||||
void poll(uint16_t event, uint32_t msTimeout);
|
|
||||||
private:
|
|
||||||
SocketFileDescriptor fd;
|
|
||||||
};
|
|
||||||
|
|
||||||
NetworkInterface interfaceDescription;
|
|
||||||
uint32_t dstIP;
|
|
||||||
uint16_t dstPort;
|
|
||||||
std::unique_ptr<Socket> socket;
|
|
||||||
|
|
||||||
std::thread readThread, writeThread;
|
|
||||||
void readTask();
|
|
||||||
void writeTask();
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // __cplusplus
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
#ifndef __CDCACM_WINDOWS_H_
|
|
||||||
#define __CDCACM_WINDOWS_H_
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
|
|
||||||
#include "icsneo/communication/driver.h"
|
|
||||||
#include "icsneo/device/founddevice.h"
|
|
||||||
|
|
||||||
#define WIN32_LEAN_AND_MEAN
|
|
||||||
#define NOMINMAX
|
|
||||||
#include <windows.h>
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
namespace icsneo {
|
|
||||||
|
|
||||||
class CDCACM : public Driver {
|
|
||||||
public:
|
|
||||||
CDCACM(const device_eventhandler_t& err, const std::wstring& path);
|
|
||||||
static void Find(std::vector<FoundDevice>& found);
|
|
||||||
bool open() override;
|
|
||||||
bool isOpen() override;
|
|
||||||
bool close() override;
|
|
||||||
driver_finder_t getFinder() override { return CDCACM::Find; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
void read();
|
|
||||||
void write();
|
|
||||||
std::wstring path;
|
|
||||||
HANDLE handle = INVALID_HANDLE_VALUE;
|
|
||||||
std::thread readThread;
|
|
||||||
std::thread writeThread;
|
|
||||||
OVERLAPPED readOverlapped = {};
|
|
||||||
OVERLAPPED writeOverlapped = {};
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // __cplusplus
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,62 +0,0 @@
|
|||||||
#ifndef __PCAPDLL_WINDOWS_H_
|
|
||||||
#define __PCAPDLL_WINDOWS_H_
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
|
|
||||||
#define WIN32_LEAN_AND_MEAN
|
|
||||||
#define NOMINMAX
|
|
||||||
#include <windows.h>
|
|
||||||
#include <pcap.h>
|
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
namespace icsneo {
|
|
||||||
|
|
||||||
// Helper loader for the PCAP DLL
|
|
||||||
class PCAPDLL {
|
|
||||||
public:
|
|
||||||
// The first time we use the DLL we keep it in here and it won't get freed until the user unloads us (for speed reasons)
|
|
||||||
static const PCAPDLL& getInstance();
|
|
||||||
|
|
||||||
// Functions
|
|
||||||
typedef int(__cdecl* PCAPFINDDEVICE)(char* source, struct pcap_rmtauth* auth, pcap_if_t** alldevs, char* errbuf);
|
|
||||||
typedef pcap_t*(__cdecl* PCAPOPEN)(const char* source, int snaplen, int flags, int read_timeout, struct pcap_rmtauth* auth, char* errbuf);
|
|
||||||
typedef void(__cdecl* PCAPFREEDEVS)(pcap_if_t* alldevsp);
|
|
||||||
typedef void(__cdecl* PCAPCLOSE)(pcap_t* p);
|
|
||||||
typedef int(__cdecl* PCAPSTATS)(pcap_t* p, struct pcap_stat* ps);
|
|
||||||
typedef int(__cdecl* PCAPNEXTEX)(pcap_t* p, struct pcap_pkthdr** pkt_header, const unsigned char** pkt_data);
|
|
||||||
typedef int(__cdecl* PCAPSENDPACKET)(pcap_t* p, const unsigned char* buf, int size);
|
|
||||||
typedef pcap_send_queue*(__cdecl* PCAPSENDQUEUEALLOC)(unsigned int memsize);
|
|
||||||
typedef int(__cdecl* PCAPSENDQUEUEQUEUE)(pcap_send_queue* queue, const struct pcap_pkthdr* pkt_header, const unsigned char* pkt_data);
|
|
||||||
typedef void(__cdecl* PCAPSENDQUEUEDESTROY)(pcap_send_queue* queue);
|
|
||||||
typedef unsigned int(__cdecl* PCAPSENDQUEUETRANSMIT)(pcap_t* p, pcap_send_queue* queue, int sync);
|
|
||||||
typedef int(__cdecl* PCAPDATALINK)(pcap_t* p);
|
|
||||||
typedef int(__cdecl* PCAPCREATESRCSTR)(char* source, int type, const char* host, const char* port, const char* name, char* errbuf);
|
|
||||||
typedef int(__cdecl* PCAPSETBUFF)(pcap_t* p, int dim);
|
|
||||||
PCAPFINDDEVICE findalldevs_ex;
|
|
||||||
PCAPOPEN open;
|
|
||||||
PCAPFREEDEVS freealldevs;
|
|
||||||
PCAPCLOSE close;
|
|
||||||
PCAPSTATS stats;
|
|
||||||
PCAPNEXTEX next_ex;
|
|
||||||
PCAPSENDPACKET sendpacket;
|
|
||||||
PCAPSENDQUEUEALLOC sendqueue_alloc;
|
|
||||||
PCAPSENDQUEUEQUEUE sendqueue_queue;
|
|
||||||
PCAPSENDQUEUEDESTROY sendqueue_destroy;
|
|
||||||
PCAPSENDQUEUETRANSMIT sendqueue_transmit;
|
|
||||||
PCAPDATALINK datalink;
|
|
||||||
PCAPCREATESRCSTR createsrcstr;
|
|
||||||
PCAPSETBUFF setbuff;
|
|
||||||
|
|
||||||
|
|
||||||
~PCAPDLL();
|
|
||||||
bool ok() const;
|
|
||||||
private:
|
|
||||||
PCAPDLL();
|
|
||||||
HINSTANCE dll = nullptr;
|
|
||||||
void closeDLL();
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // __cplusplus
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,68 +0,0 @@
|
|||||||
#ifndef __PCAP_WINDOWS_H_
|
|
||||||
#define __PCAP_WINDOWS_H_
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
|
|
||||||
#include "icsneo/platform/windows/internal/pcapdll.h"
|
|
||||||
#include "icsneo/device/neodevice.h"
|
|
||||||
#include "icsneo/communication/driver.h"
|
|
||||||
#include "icsneo/device/founddevice.h"
|
|
||||||
#include "icsneo/api/eventmanager.h"
|
|
||||||
#include "icsneo/communication/ethernetpacketizer.h"
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
namespace icsneo {
|
|
||||||
|
|
||||||
class PCAP : public Driver {
|
|
||||||
public:
|
|
||||||
static void Find(std::vector<FoundDevice>& foundDevices);
|
|
||||||
static std::string GetEthDevSerialFromMacAddress(uint8_t product, uint16_t macSerial);
|
|
||||||
static bool IsHandleValid(neodevice_handle_t handle);
|
|
||||||
|
|
||||||
PCAP(const device_eventhandler_t& err, neodevice_t& forDevice);
|
|
||||||
bool open() override;
|
|
||||||
bool isOpen() override;
|
|
||||||
bool close() override;
|
|
||||||
bool isEthernet() const override { return true; }
|
|
||||||
driver_finder_t getFinder() override { return PCAP::Find; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
const PCAPDLL& pcap;
|
|
||||||
char errbuf[PCAP_ERRBUF_SIZE] = { 0 };
|
|
||||||
neodevice_t& device;
|
|
||||||
uint8_t deviceMAC[6];
|
|
||||||
bool openable = true;
|
|
||||||
EthernetPacketizer ethPacketizer;
|
|
||||||
|
|
||||||
std::thread readThread, writeThread;
|
|
||||||
std::thread transmitThread;
|
|
||||||
pcap_send_queue* transmitQueue = nullptr;
|
|
||||||
std::condition_variable transmitQueueCV;
|
|
||||||
std::mutex transmitQueueMutex;
|
|
||||||
|
|
||||||
void readTask();
|
|
||||||
void writeTask();
|
|
||||||
void transmitTask();
|
|
||||||
|
|
||||||
class NetworkInterface {
|
|
||||||
public:
|
|
||||||
uint8_t uuid;
|
|
||||||
uint8_t macAddress[6];
|
|
||||||
std::string nameFromWinPCAP;
|
|
||||||
std::string nameFromWin32API;
|
|
||||||
std::string descriptionFromWinPCAP;
|
|
||||||
std::string descriptionFromWin32API;
|
|
||||||
std::string friendlyNameFromWin32API;
|
|
||||||
std::string fullName;
|
|
||||||
pcap_t* fp = nullptr;
|
|
||||||
pcap_stat stats;
|
|
||||||
};
|
|
||||||
static std::vector<NetworkInterface> knownInterfaces;
|
|
||||||
NetworkInterface iface;
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // __cplusplus
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
#ifndef __REGISTRY_WINDOWS_H_
|
|
||||||
#define __REGISTRY_WINDOWS_H_
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
namespace icsneo {
|
|
||||||
|
|
||||||
class Registry {
|
|
||||||
public:
|
|
||||||
static bool EnumerateSubkeys(std::wstring path, std::vector<std::wstring>& subkeys);
|
|
||||||
|
|
||||||
// Get string value
|
|
||||||
static bool Get(std::wstring path, std::wstring key, std::wstring& value);
|
|
||||||
static bool Get(std::string path, std::string key, std::string& value);
|
|
||||||
|
|
||||||
// Get DWORD value
|
|
||||||
static bool Get(std::wstring path, std::wstring key, uint32_t& value);
|
|
||||||
static bool Get(std::string path, std::string key, uint32_t& value);
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // __cplusplus
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
#ifndef __WINDOWS_STRINGS_H__
|
|
||||||
#define __WINDOWS_STRINGS_H__
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
namespace icsneo {
|
|
||||||
|
|
||||||
// Helper function to convert UTF-16 to UTF-8 strings (wide to standard)
|
|
||||||
std::string convertWideString(const std::wstring& value);
|
|
||||||
|
|
||||||
// Helper function to convert UTF-8 to UTF-16 strings (standard to wide)
|
|
||||||
std::wstring convertStringToWide(const std::string& value);
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
#endif // __cplusplus
|
|
||||||
|
|
||||||
#endif // __WINDOWS_STRINGS_H__
|
|
||||||
@@ -1,201 +0,0 @@
|
|||||||
#include "icsneo/platform/dxx.h"
|
|
||||||
|
|
||||||
#define ICS_USB_VID 0x093C
|
|
||||||
|
|
||||||
using namespace icsneo;
|
|
||||||
|
|
||||||
static APIEvent::Type eventError(libredxx_status status) {
|
|
||||||
switch (status) {
|
|
||||||
case LIBREDXX_STATUS_ERROR_SYS: return APIEvent::Type::DXXErrorSys;
|
|
||||||
case LIBREDXX_STATUS_ERROR_INTERRUPTED: return APIEvent::Type::DXXErrorSys;
|
|
||||||
case LIBREDXX_STATUS_ERROR_OVERFLOW: return APIEvent::Type::DXXErrorSys;
|
|
||||||
case LIBREDXX_STATUS_ERROR_IO: return APIEvent::Type::DXXErrorSys;
|
|
||||||
case LIBREDXX_STATUS_ERROR_INVALID_ARGUMENT: return APIEvent::Type::DXXErrorSys;
|
|
||||||
default: return APIEvent::Type::Unknown;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void DXX::Find(std::vector<FoundDevice>& found) {
|
|
||||||
libredxx_status status;
|
|
||||||
static libredxx_find_filter filters[] = {
|
|
||||||
{ LIBREDXX_DEVICE_TYPE_D2XX, { ICS_USB_VID, 0x0005 } }, // RAD-Star 2
|
|
||||||
{ LIBREDXX_DEVICE_TYPE_D2XX, { ICS_USB_VID, 0x0006 } }, // RAD-A2B Rev A
|
|
||||||
{ LIBREDXX_DEVICE_TYPE_D2XX, { ICS_USB_VID, 0x0601 } }, // ValueCAN 3
|
|
||||||
{ LIBREDXX_DEVICE_TYPE_D2XX, { ICS_USB_VID, 0x0701 } }, // FIRE
|
|
||||||
{ LIBREDXX_DEVICE_TYPE_D2XX, { ICS_USB_VID, 0x0801 } }, // neoVI PLASMA
|
|
||||||
{ LIBREDXX_DEVICE_TYPE_D2XX, { ICS_USB_VID, 0x0901 } }, // neoVI ION
|
|
||||||
{ LIBREDXX_DEVICE_TYPE_D2XX, { ICS_USB_VID, 0x1000 } }, // neoVI FIRE2
|
|
||||||
{ LIBREDXX_DEVICE_TYPE_D3XX, { ICS_USB_VID, 0x1201 } }, // RAD-SuperMoon
|
|
||||||
{ LIBREDXX_DEVICE_TYPE_D3XX, { ICS_USB_VID, 0x1202 } }, // RAD-Moon2
|
|
||||||
{ LIBREDXX_DEVICE_TYPE_D3XX, { ICS_USB_VID, 0x1205 } }, // RAD-Moon2
|
|
||||||
{ LIBREDXX_DEVICE_TYPE_D3XX, { ICS_USB_VID, 0x1203 } }, // RAD-Gigalog
|
|
||||||
{ LIBREDXX_DEVICE_TYPE_D3XX, { ICS_USB_VID, 0x1204 } }, // RAD-Gigastar
|
|
||||||
{ LIBREDXX_DEVICE_TYPE_D3XX, { ICS_USB_VID, 0x1206 } }, // RAD-A2B Rev B
|
|
||||||
{ LIBREDXX_DEVICE_TYPE_D3XX, { ICS_USB_VID, 0x1207 } }, // RAD-Comet2
|
|
||||||
{ LIBREDXX_DEVICE_TYPE_D3XX, { ICS_USB_VID, 0x1208 } }, // RAD-Comet3
|
|
||||||
{ LIBREDXX_DEVICE_TYPE_D3XX, { ICS_USB_VID, 0x1209 } }, // RAD-MoonT1S
|
|
||||||
{ LIBREDXX_DEVICE_TYPE_D3XX, { ICS_USB_VID, 0x1210 } }, // RAD-Gigastar 2
|
|
||||||
};
|
|
||||||
static size_t filterCount = sizeof(filters) / sizeof(filters[0]);
|
|
||||||
|
|
||||||
libredxx_found_device** foundDevices = nullptr;
|
|
||||||
size_t foundDevicesCount;
|
|
||||||
status = libredxx_find_devices(filters, filterCount, &foundDevices, &foundDevicesCount);
|
|
||||||
if(status != LIBREDXX_STATUS_SUCCESS) {
|
|
||||||
EventManager::GetInstance().add(eventError(status), APIEvent::Severity::Error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if(foundDevicesCount == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for(size_t i = 0; i < foundDevicesCount; ++i) {
|
|
||||||
libredxx_found_device* foundDevice = foundDevices[i];
|
|
||||||
libredxx_serial serial = {};
|
|
||||||
status = libredxx_get_serial(foundDevice, &serial);
|
|
||||||
if(status != LIBREDXX_STATUS_SUCCESS) {
|
|
||||||
EventManager::GetInstance().add(eventError(status), APIEvent::Severity::Error);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
libredxx_device_id id;
|
|
||||||
status = libredxx_get_device_id(foundDevice, &id);
|
|
||||||
if(status != LIBREDXX_STATUS_SUCCESS) {
|
|
||||||
EventManager::GetInstance().add(eventError(status), APIEvent::Severity::Error);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
libredxx_device_type type;
|
|
||||||
status = libredxx_get_device_type(foundDevice, &type);
|
|
||||||
if(status != LIBREDXX_STATUS_SUCCESS) {
|
|
||||||
EventManager::GetInstance().add(eventError(status), APIEvent::Severity::Error);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto& device = found.emplace_back();
|
|
||||||
// -1 to make sure serial has a trailing '\0'
|
|
||||||
std::copy(serial.serial, serial.serial + (sizeof(device.serial) - 1), device.serial);
|
|
||||||
device.makeDriver = [id, type](device_eventhandler_t err, neodevice_t& forDevice) {
|
|
||||||
return std::make_unique<DXX>(err, forDevice, id.pid, type);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
libredxx_free_found(foundDevices);
|
|
||||||
}
|
|
||||||
|
|
||||||
DXX::DXX(const device_eventhandler_t& err, neodevice_t& forDevice, uint16_t pid, libredxx_device_type type) :
|
|
||||||
Driver(err), neodevice(forDevice), pid(pid), type(type) {
|
|
||||||
}
|
|
||||||
|
|
||||||
bool DXX::open() {
|
|
||||||
libredxx_status status;
|
|
||||||
libredxx_find_filter filters[] = {
|
|
||||||
{ (libredxx_device_type)type, { ICS_USB_VID, pid } }
|
|
||||||
};
|
|
||||||
libredxx_found_device** foundDevices = nullptr;
|
|
||||||
size_t foundDevicesCount;
|
|
||||||
status = libredxx_find_devices(filters, 1, &foundDevices, &foundDevicesCount);
|
|
||||||
if(status != LIBREDXX_STATUS_SUCCESS) {
|
|
||||||
EventManager::GetInstance().add(eventError(status), APIEvent::Severity::Error);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if(foundDevicesCount == 0) {
|
|
||||||
EventManager::GetInstance().add(APIEvent::Type::DeviceDisconnected, APIEvent::Severity::Error);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
libredxx_found_device* foundDevice = nullptr;
|
|
||||||
for(size_t i = 0; i < foundDevicesCount; ++i) {
|
|
||||||
libredxx_serial serial = {};
|
|
||||||
status = libredxx_get_serial(foundDevices[i], &serial);
|
|
||||||
if(status != LIBREDXX_STATUS_SUCCESS) {
|
|
||||||
EventManager::GetInstance().add(eventError(status), APIEvent::Severity::EventWarning);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(strncmp(serial.serial, neodevice.serial, sizeof(neodevice.serial) - 1) == 0) {
|
|
||||||
foundDevice = foundDevices[i];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(foundDevice == nullptr) {
|
|
||||||
EventManager::GetInstance().add(APIEvent::Type::DeviceDisconnected, APIEvent::Severity::Error);
|
|
||||||
libredxx_free_found(foundDevices);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
status = libredxx_open_device(foundDevice, &device);
|
|
||||||
if(status != LIBREDXX_STATUS_SUCCESS) {
|
|
||||||
EventManager::GetInstance().add(eventError(status), APIEvent::Severity::Error);
|
|
||||||
libredxx_free_found(foundDevices);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
libredxx_free_found(foundDevices);
|
|
||||||
setIsDisconnected(false);
|
|
||||||
readThread = std::thread(&DXX::read, this);
|
|
||||||
writeThread = std::thread(&DXX::write, this);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool DXX::isOpen() {
|
|
||||||
return device != nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool DXX::close() {
|
|
||||||
setIsClosing(true);
|
|
||||||
libredxx_close_device(device); // unblock read thread & close
|
|
||||||
writeQueue.enqueue(WriteOperation{}); // unblock write thread
|
|
||||||
readThread.join();
|
|
||||||
writeThread.join();
|
|
||||||
device = nullptr;
|
|
||||||
setIsClosing(false);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void DXX::read() {
|
|
||||||
EventManager::GetInstance().downgradeErrorsOnCurrentThread();
|
|
||||||
|
|
||||||
std::vector<uint8_t> buffer(ICSNEO_DRIVER_RINGBUFFER_SIZE);
|
|
||||||
|
|
||||||
while(!isDisconnected() && !isClosing()) {
|
|
||||||
size_t received = buffer.size();
|
|
||||||
const auto status = libredxx_read(device, buffer.data(), &received, LIBREDXX_ENDPOINT_A);
|
|
||||||
if(isDisconnected() || isClosing()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if(status != LIBREDXX_STATUS_SUCCESS) {
|
|
||||||
EventManager::GetInstance().add(eventError(status), APIEvent::Severity::Error);
|
|
||||||
setIsDisconnected(true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
while(!isDisconnected() && !isClosing()) {
|
|
||||||
if(pushRx(buffer.data(), received))
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void DXX::write() {
|
|
||||||
EventManager::GetInstance().downgradeErrorsOnCurrentThread();
|
|
||||||
|
|
||||||
WriteOperation writeOp;
|
|
||||||
|
|
||||||
while(!isDisconnected() && !isClosing()) {
|
|
||||||
writeQueue.wait_dequeue(writeOp);
|
|
||||||
|
|
||||||
if(isDisconnected() || isClosing()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for(size_t totalWritten = 0; totalWritten < writeOp.bytes.size();) {
|
|
||||||
size_t size = writeOp.bytes.size() - totalWritten;
|
|
||||||
const auto status = libredxx_write(device, &writeOp.bytes[totalWritten], &size, LIBREDXX_ENDPOINT_A);
|
|
||||||
if(isDisconnected() || isClosing()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if(status != LIBREDXX_STATUS_SUCCESS) {
|
|
||||||
EventManager::GetInstance().add(eventError(status), APIEvent::Severity::Error);
|
|
||||||
setIsDisconnected(true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
totalWritten += size;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,262 +0,0 @@
|
|||||||
#include "icsneo/platform/cdcacm.h"
|
|
||||||
#include <dirent.h>
|
|
||||||
#include <cstring>
|
|
||||||
#include <iostream>
|
|
||||||
#include <sstream>
|
|
||||||
#include <fstream>
|
|
||||||
#include <map>
|
|
||||||
#include <algorithm>
|
|
||||||
#include <termios.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <sys/select.h>
|
|
||||||
|
|
||||||
using namespace icsneo;
|
|
||||||
|
|
||||||
CDCACM::~CDCACM() {
|
|
||||||
awaitModeChangeComplete();
|
|
||||||
if(isOpen())
|
|
||||||
close();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CDCACM::open() {
|
|
||||||
if(isOpen()) {
|
|
||||||
report(APIEvent::Type::DeviceCurrentlyOpen, APIEvent::Severity::Error);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::string& ttyPath = HandleToTTY(device.handle);
|
|
||||||
if(ttyPath.empty()) {
|
|
||||||
report(APIEvent::Type::DriverFailedToOpen, APIEvent::Severity::Error);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Some devices can take a while to boot
|
|
||||||
for(int i = 0; i != 50; ++i) {
|
|
||||||
fd = ::open(ttyPath.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
|
|
||||||
|
|
||||||
if(fd != -1) {
|
|
||||||
// Opened successfully, check if it's the same inode as the disallowed one
|
|
||||||
// The inode is disallowed if we're expecting the device to re-enumerate (modeChanging)
|
|
||||||
if(disallowedInode.has_value()) {
|
|
||||||
struct stat fileStat = {};
|
|
||||||
if(fstat(fd, &fileStat) >= 0) {
|
|
||||||
if(fileStat.st_ino != *disallowedInode)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// This is still the old device, wait and try again
|
|
||||||
::close(fd);
|
|
||||||
fd = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
||||||
}
|
|
||||||
disallowedInode.reset();
|
|
||||||
if(!isOpen()) {
|
|
||||||
//std::cout << "Open of " << ttyPath.c_str() << " failed with " << strerror(errno) << ' ';
|
|
||||||
report(APIEvent::Type::DriverFailedToOpen, APIEvent::Severity::Error);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct termios tty = {};
|
|
||||||
struct termios compare = {};
|
|
||||||
|
|
||||||
if(tcgetattr(fd, &tty) != 0) {
|
|
||||||
close();
|
|
||||||
report(APIEvent::Type::DriverFailedToOpen, APIEvent::Severity::Error);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
tty.c_cflag |= (CLOCAL | CREAD); // Ignore modem controls
|
|
||||||
tty.c_cflag &= ~CSIZE;
|
|
||||||
tty.c_cflag |= CS8; // 8-bit characters
|
|
||||||
tty.c_cflag &= ~PARENB; // No parity bit
|
|
||||||
tty.c_cflag &= ~CSTOPB; // One stop bit
|
|
||||||
tty.c_cflag &= ~CRTSCTS; // No hardware flow control
|
|
||||||
|
|
||||||
// Non-canonical mode
|
|
||||||
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
|
|
||||||
tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
|
|
||||||
tty.c_oflag &= ~OPOST;
|
|
||||||
|
|
||||||
// Fetch bytes as they become available
|
|
||||||
// See http://man7.org/linux/man-pages/man3/termios.3.html
|
|
||||||
tty.c_cc[VMIN] = 0;
|
|
||||||
tty.c_cc[VTIME] = 1; // 100ms timeout (1 decisecond, what?)
|
|
||||||
|
|
||||||
if(tcsetattr(fd, TCSAFLUSH, &tty) != 0) { // Flushes input and output buffers as well as setting settings
|
|
||||||
close();
|
|
||||||
report(APIEvent::Type::DriverFailedToOpen, APIEvent::Severity::Error);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(tcgetattr(fd, &compare) != 0 || memcmp(&tty, &compare, sizeof(struct termios)) != 0) {
|
|
||||||
close();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create threads
|
|
||||||
readThread = std::thread(&CDCACM::readTask, this);
|
|
||||||
writeThread = std::thread(&CDCACM::writeTask, this);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CDCACM::isOpen() {
|
|
||||||
return fd >= 0; // Negative fd indicates error or not opened yet
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CDCACM::close() {
|
|
||||||
if(!isOpen() && !isDisconnected()) {
|
|
||||||
report(APIEvent::Type::DeviceCurrentlyClosed, APIEvent::Severity::Error);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
setIsClosing(true);
|
|
||||||
|
|
||||||
if(readThread.joinable())
|
|
||||||
readThread.join();
|
|
||||||
|
|
||||||
if(writeThread.joinable())
|
|
||||||
writeThread.join();
|
|
||||||
|
|
||||||
setIsClosing(false);
|
|
||||||
setIsDisconnected(false);
|
|
||||||
|
|
||||||
if(modeChanging) {
|
|
||||||
// We're expecting this inode to go away after we close the device
|
|
||||||
// In order to block waiting for this to happen, we first need to
|
|
||||||
// get the inode.
|
|
||||||
struct stat fileStat = {};
|
|
||||||
if(fstat(fd, &fileStat) >= 0)
|
|
||||||
disallowedInode = fileStat.st_ino;
|
|
||||||
}
|
|
||||||
|
|
||||||
int ret = ::close(fd);
|
|
||||||
fd = -1;
|
|
||||||
|
|
||||||
clearBuffers();
|
|
||||||
|
|
||||||
if(modeChanging) {
|
|
||||||
modeChanging = false;
|
|
||||||
return open(); // Reopen the reenumerated device
|
|
||||||
}
|
|
||||||
|
|
||||||
if(ret == 0) {
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
report(APIEvent::Type::DriverFailedToClose, APIEvent::Severity::Error);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void CDCACM::modeChangeIncoming() {
|
|
||||||
modeChanging = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void CDCACM::awaitModeChangeComplete() {
|
|
||||||
std::unique_lock<std::mutex> lk(modeChangeMutex);
|
|
||||||
if(modeChanging && !modeChangeThread.joinable()) // Waiting for the thread to start
|
|
||||||
modeChangeCV.wait_for(lk, std::chrono::seconds(1), [this] { return modeChangeThread.joinable(); });
|
|
||||||
if(modeChangeThread.joinable())
|
|
||||||
modeChangeThread.join();
|
|
||||||
}
|
|
||||||
|
|
||||||
void CDCACM::readTask() {
|
|
||||||
constexpr size_t READ_BUFFER_SIZE = 2048;
|
|
||||||
uint8_t readbuf[READ_BUFFER_SIZE];
|
|
||||||
EventManager::GetInstance().downgradeErrorsOnCurrentThread();
|
|
||||||
while(!isClosing() && !isDisconnected()) {
|
|
||||||
fd_set rfds = {0};
|
|
||||||
struct timeval tv = {};
|
|
||||||
FD_SET(fd, &rfds);
|
|
||||||
tv.tv_usec = 50000; // 50ms
|
|
||||||
::select(fd + 1, &rfds, NULL, NULL, &tv);
|
|
||||||
auto bytesRead = ::read(fd, readbuf, READ_BUFFER_SIZE);
|
|
||||||
if(bytesRead > 0) {
|
|
||||||
#if 0 // Perhaps helpful for debugging :)
|
|
||||||
std::cout << "Read data: (" << bytesRead << ')' << std::hex << std::endl;
|
|
||||||
for(int i = 0; i < bytesRead; i += 16) {
|
|
||||||
for(int j = 0; j < std::min<int>(bytesRead - i, 16); j++)
|
|
||||||
std::cout << std::setw(2) << std::setfill('0') << uint32_t(readbuf[i+j]) << ' ';
|
|
||||||
std::cout << std::endl;
|
|
||||||
}
|
|
||||||
std::cout << std::dec << std::endl;
|
|
||||||
#endif
|
|
||||||
pushRx(readbuf, bytesRead);
|
|
||||||
} else {
|
|
||||||
if(modeChanging) {
|
|
||||||
// We were expecting a disconnect for reenumeration
|
|
||||||
modeChangeThread = std::thread([this] {
|
|
||||||
modeChangeCV.notify_all();
|
|
||||||
// Requesting thread is responsible for calling close. This allows for more flexibility
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
} else if(!isClosing() && !fdIsValid() && !isDisconnected()) {
|
|
||||||
setIsDisconnected(true);
|
|
||||||
report(APIEvent::Type::DeviceDisconnected, APIEvent::Severity::Error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void CDCACM::writeTask() {
|
|
||||||
WriteOperation writeOp;
|
|
||||||
EventManager::GetInstance().downgradeErrorsOnCurrentThread();
|
|
||||||
while(!isClosing() && !isDisconnected()) {
|
|
||||||
if(!writeQueue.wait_dequeue_timed(writeOp, std::chrono::milliseconds(100)))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
const ssize_t totalWriteSize = (ssize_t)writeOp.bytes.size();
|
|
||||||
ssize_t totalWritten = 0;
|
|
||||||
while(totalWritten < totalWriteSize) {
|
|
||||||
const ssize_t writeSize = totalWriteSize - totalWritten;
|
|
||||||
ssize_t actualWritten = ::write(fd, writeOp.bytes.data() + totalWritten, writeSize);
|
|
||||||
if(actualWritten != writeSize) {
|
|
||||||
// If we partially wrote, it's probably EAGAIN but it won't have been set
|
|
||||||
// so don't signal an error unless it's < 0, we'll come back around and
|
|
||||||
// get a -1 to see the real error.
|
|
||||||
if(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
|
|
||||||
// We filled the TX FIFO, use select to wait for it to become available again
|
|
||||||
fd_set wfds = {0};
|
|
||||||
struct timeval tv = {};
|
|
||||||
FD_SET(fd, &wfds);
|
|
||||||
tv.tv_usec = 50000; // 50ms
|
|
||||||
::select(fd + 1, nullptr, &wfds, nullptr, &tv);
|
|
||||||
} else if (actualWritten < 0) {
|
|
||||||
if(!fdIsValid()) {
|
|
||||||
if(!isDisconnected()) {
|
|
||||||
setIsDisconnected(true);
|
|
||||||
report(APIEvent::Type::DeviceDisconnected, APIEvent::Severity::Error);
|
|
||||||
}
|
|
||||||
} else
|
|
||||||
report(APIEvent::Type::FailedToWrite, APIEvent::Severity::Error);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(actualWritten > 0) {
|
|
||||||
#if 0 // Perhaps helpful for debugging :)
|
|
||||||
std::cout << "Wrote data: (" << actualWritten << ')' << std::hex << std::endl;
|
|
||||||
for(int i = 0; i < actualWritten; i += 16) {
|
|
||||||
for(int j = 0; j < std::min<int>(actualWritten - i, 16); j++)
|
|
||||||
std::cout << std::setw(2) << std::setfill('0') << uint32_t(writeOp.bytes[totalWritten+i+j]) << ' ';
|
|
||||||
std::cout << std::endl;
|
|
||||||
}
|
|
||||||
std::cout << std::dec << std::endl;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
totalWritten += actualWritten;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CDCACM::fdIsValid() {
|
|
||||||
struct termios tty = {};
|
|
||||||
return tcgetattr(fd, &tty) == 0 ? true : false;
|
|
||||||
}
|
|
||||||
@@ -1,192 +0,0 @@
|
|||||||
#include "icsneo/platform/cdcacm.h"
|
|
||||||
#include "icsneo/device/founddevice.h"
|
|
||||||
#include <mutex>
|
|
||||||
#include <vector>
|
|
||||||
#include <cctype>
|
|
||||||
#include <CoreFoundation/CoreFoundation.h>
|
|
||||||
#include <IOKit/IOKitLib.h>
|
|
||||||
#include <IOKit/usb/IOUSBLib.h>
|
|
||||||
#include <IOKit/serial/IOSerialKeys.h>
|
|
||||||
|
|
||||||
using namespace icsneo;
|
|
||||||
|
|
||||||
/* The index into the TTY table starts at zero, but we want to keep zero
|
|
||||||
* for an undefined handle, so add a constant.
|
|
||||||
*/
|
|
||||||
static constexpr const neodevice_handle_t HANDLE_OFFSET = 10;
|
|
||||||
|
|
||||||
static std::mutex ttyTableMutex;
|
|
||||||
static std::vector<std::string> ttyTable;
|
|
||||||
|
|
||||||
static neodevice_handle_t TTYToHandle(const std::string& tty) {
|
|
||||||
std::lock_guard<std::mutex> lk(ttyTableMutex);
|
|
||||||
for(size_t i = 0; i < ttyTable.size(); i++) {
|
|
||||||
if(ttyTable[i] == tty)
|
|
||||||
return neodevice_handle_t(i + HANDLE_OFFSET);
|
|
||||||
}
|
|
||||||
ttyTable.push_back(tty);
|
|
||||||
return neodevice_handle_t(ttyTable.size() - 1 + HANDLE_OFFSET);
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::string CFStringToString(CFStringRef cfString) {
|
|
||||||
if(cfString == nullptr)
|
|
||||||
return std::string();
|
|
||||||
// As an optimization, we can try to directly read the CFString's CStringPtr
|
|
||||||
// CoreFoundation will seemingly not lift a finger if the pointer is not readily available
|
|
||||||
// so it will often return nullptr and we'll have to get the string "the hard way"
|
|
||||||
const char* cstr = CFStringGetCStringPtr(cfString, kCFStringEncodingUTF8);
|
|
||||||
if(cstr != nullptr)
|
|
||||||
return std::string(cstr);
|
|
||||||
// "The hard way"
|
|
||||||
// CFStringGetLength returns the length in UTF-16 Code Points
|
|
||||||
// CFStringGetCString will convert to UTF-8 for us so long as we give it enough space
|
|
||||||
const int len = CFStringGetLength(cfString);
|
|
||||||
if(len <= 0)
|
|
||||||
return std::string();
|
|
||||||
std::vector<char> utf8data;
|
|
||||||
utf8data.resize(len * 4 + 1); // UTF-16 => UTF-8, 1 code point can become up to 4 bytes, plus NUL
|
|
||||||
if(!CFStringGetCString(cfString, utf8data.data(), utf8data.size(), kCFStringEncodingUTF8))
|
|
||||||
return std::string();
|
|
||||||
return std::string(utf8data.data());
|
|
||||||
}
|
|
||||||
|
|
||||||
class CFReleaser {
|
|
||||||
public:
|
|
||||||
CFReleaser(CFTypeRef obj) : toRelease(obj) {}
|
|
||||||
~CFReleaser() {
|
|
||||||
if(toRelease != nullptr)
|
|
||||||
CFRelease(toRelease);
|
|
||||||
}
|
|
||||||
CFReleaser(CFReleaser const&) = delete;
|
|
||||||
CFReleaser& operator=(CFReleaser const&) = delete;
|
|
||||||
|
|
||||||
private:
|
|
||||||
CFTypeRef toRelease;
|
|
||||||
};
|
|
||||||
|
|
||||||
class IOReleaser {
|
|
||||||
public:
|
|
||||||
IOReleaser(io_object_t obj) : toRelease(obj) {}
|
|
||||||
~IOReleaser() {
|
|
||||||
if(toRelease != 0)
|
|
||||||
IOObjectRelease(toRelease);
|
|
||||||
}
|
|
||||||
IOReleaser(IOReleaser&& moved) : toRelease(moved.toRelease) {
|
|
||||||
moved.toRelease = 0;
|
|
||||||
}
|
|
||||||
IOReleaser(IOReleaser const&) = delete;
|
|
||||||
IOReleaser& operator=(IOReleaser const&) = delete;
|
|
||||||
|
|
||||||
private:
|
|
||||||
io_object_t toRelease;
|
|
||||||
};
|
|
||||||
|
|
||||||
void CDCACM::Find(std::vector<FoundDevice>& found) {
|
|
||||||
CFMutableDictionaryRef ref = IOServiceMatching(kIOSerialBSDServiceValue);
|
|
||||||
if(ref == nullptr)
|
|
||||||
return;
|
|
||||||
io_iterator_t matchingServices = 0;
|
|
||||||
kern_return_t kernResult = IOServiceGetMatchingServices(kIOMainPortDefault, ref, &matchingServices);
|
|
||||||
if(KERN_SUCCESS != kernResult || matchingServices == 0)
|
|
||||||
return;
|
|
||||||
IOReleaser matchingServicesReleaser(matchingServices);
|
|
||||||
|
|
||||||
io_object_t serialPort;
|
|
||||||
while((serialPort = IOIteratorNext(matchingServices))) {
|
|
||||||
IOReleaser serialPortReleaser(serialPort);
|
|
||||||
|
|
||||||
// First get the parent device
|
|
||||||
// We want to check that it has the right VID/PID
|
|
||||||
|
|
||||||
// Find the parent structure that describes the USB device providing this port
|
|
||||||
io_object_t parent = 0;
|
|
||||||
io_object_t current = serialPort;
|
|
||||||
io_object_t usb = 0;
|
|
||||||
// Need to release every parent we find in the chain, but we should defer that until later
|
|
||||||
std::vector<IOReleaser> releasers;
|
|
||||||
while(IORegistryEntryGetParentEntry(current, kIOServicePlane, &parent) == KERN_SUCCESS) {
|
|
||||||
releasers.emplace_back(parent);
|
|
||||||
current = parent;
|
|
||||||
// On old macOSes, IOUSBDevice is the type of the class we want
|
|
||||||
// On newer macOSes, IOUSBDevice may further be subclassed as IOUSBHostDevice
|
|
||||||
if(IOObjectConformsTo(parent, kIOUSBDeviceClassName)) {
|
|
||||||
usb = parent;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(!usb) // Did not find a USB parent (maybe this is a Bluetooth modem or something)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// Get the VID
|
|
||||||
CFTypeRef vendorProp = IORegistryEntryCreateCFProperty(usb, CFSTR("idVendor"), kCFAllocatorDefault, 0);
|
|
||||||
if(vendorProp == nullptr)
|
|
||||||
continue;
|
|
||||||
CFReleaser vendorPropReleaser(vendorProp);
|
|
||||||
if(CFGetTypeID(vendorProp) != CFNumberGetTypeID())
|
|
||||||
continue;
|
|
||||||
uint16_t vid = 0;
|
|
||||||
if(!CFNumberGetValue(static_cast<CFNumberRef>(vendorProp), kCFNumberSInt16Type, &vid))
|
|
||||||
continue;
|
|
||||||
if(vid != INTREPID_USB_VENDOR_ID)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// Get the PID
|
|
||||||
CFTypeRef productProp = IORegistryEntryCreateCFProperty(usb, CFSTR("idProduct"), kCFAllocatorDefault, 0);
|
|
||||||
if(productProp == nullptr)
|
|
||||||
continue;
|
|
||||||
CFReleaser productPropReleaser(productProp);
|
|
||||||
if(CFGetTypeID(productProp) != CFNumberGetTypeID())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// Read the PID directly into the FoundDevice structure
|
|
||||||
FoundDevice device;
|
|
||||||
if(!CFNumberGetValue(static_cast<CFNumberRef>(productProp), kCFNumberSInt16Type, &device.productId))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// Now, let's get the "call-out" device (/dev/cu.*)
|
|
||||||
// We get the /dev/cu.* variant instead of the /dev/tty.* variant because the device will not assert DCD
|
|
||||||
// Therefore, the open call on /dev/tty.* will hang, whereas /dev/cu.* will not
|
|
||||||
// This `propertyValue` will need to be freed, that will happen in CFStringToString
|
|
||||||
CFTypeRef calloutProp = IORegistryEntryCreateCFProperty(serialPort, CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0);
|
|
||||||
if(calloutProp == nullptr)
|
|
||||||
continue;
|
|
||||||
CFReleaser calloutPropReleaser(calloutProp);
|
|
||||||
if(CFGetTypeID(calloutProp) != CFStringGetTypeID())
|
|
||||||
continue;
|
|
||||||
// We can static cast here because we have verified the type to be a CFString
|
|
||||||
const std::string tty = CFStringToString(static_cast<CFStringRef>(calloutProp));
|
|
||||||
if(tty.empty())
|
|
||||||
continue;
|
|
||||||
device.handle = TTYToHandle(tty);
|
|
||||||
|
|
||||||
// Last but not least, get the serial number
|
|
||||||
CFTypeRef serialProp = IORegistryEntryCreateCFProperty(usb, CFSTR("kUSBSerialNumberString"), kCFAllocatorDefault, 0);
|
|
||||||
if(serialProp == nullptr)
|
|
||||||
continue;
|
|
||||||
CFReleaser serialPropReleaser(serialProp);
|
|
||||||
if(CFGetTypeID(serialProp) != CFStringGetTypeID())
|
|
||||||
continue;
|
|
||||||
// We can static cast here because we have verified the type to be a CFString
|
|
||||||
std::string serial = CFStringToString(static_cast<CFStringRef>(serialProp));
|
|
||||||
if(serial.empty())
|
|
||||||
continue;
|
|
||||||
for(char& c : serial)
|
|
||||||
c = static_cast<char>(toupper(c));
|
|
||||||
device.serial[serial.copy(device.serial, sizeof(device.serial)-1)] = '\0';
|
|
||||||
|
|
||||||
// Add a factory to make the driver
|
|
||||||
device.makeDriver = [](const device_eventhandler_t& report, neodevice_t& device) {
|
|
||||||
return std::unique_ptr<Driver>(new CDCACM(report, device));
|
|
||||||
};
|
|
||||||
|
|
||||||
found.push_back(device);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string CDCACM::HandleToTTY(neodevice_handle_t handle) {
|
|
||||||
std::lock_guard<std::mutex> lk(ttyTableMutex);
|
|
||||||
const size_t index = size_t(handle - HANDLE_OFFSET);
|
|
||||||
if(index >= ttyTable.size())
|
|
||||||
return ""; // Not found, generic driver (cdcacm.cpp) will throw an error
|
|
||||||
return ttyTable[index];
|
|
||||||
}
|
|
||||||
@@ -1,212 +0,0 @@
|
|||||||
#include "icsneo/platform/cdcacm.h"
|
|
||||||
#include "icsneo/device/founddevice.h"
|
|
||||||
#include <dirent.h>
|
|
||||||
#include <cstring>
|
|
||||||
#include <iostream>
|
|
||||||
#include <sstream>
|
|
||||||
#include <fstream>
|
|
||||||
#include <map>
|
|
||||||
#include <algorithm>
|
|
||||||
#include <cctype>
|
|
||||||
#include <termios.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <errno.h>
|
|
||||||
|
|
||||||
using namespace icsneo;
|
|
||||||
|
|
||||||
/* The TTY numbering starts at zero, but we want to keep zero for an undefined
|
|
||||||
* handle, so add a constant.
|
|
||||||
*/
|
|
||||||
static constexpr const neodevice_handle_t HANDLE_OFFSET = 10;
|
|
||||||
|
|
||||||
class Directory {
|
|
||||||
public:
|
|
||||||
class Listing {
|
|
||||||
public:
|
|
||||||
Listing(std::string newName, uint8_t newType) : name(newName), type(newType) {}
|
|
||||||
const std::string& getName() const { return name; }
|
|
||||||
uint8_t getType() const { return type; }
|
|
||||||
private:
|
|
||||||
std::string name;
|
|
||||||
uint8_t type;
|
|
||||||
};
|
|
||||||
Directory(std::string directory) {
|
|
||||||
dir = opendir(directory.c_str());
|
|
||||||
}
|
|
||||||
~Directory() {
|
|
||||||
if(openedSuccessfully())
|
|
||||||
closedir(dir);
|
|
||||||
dir = nullptr;
|
|
||||||
}
|
|
||||||
bool openedSuccessfully() { return dir != nullptr; }
|
|
||||||
std::vector<Listing> ls() {
|
|
||||||
std::vector<Listing> results;
|
|
||||||
struct dirent* entry;
|
|
||||||
while((entry = readdir(dir)) != nullptr) {
|
|
||||||
std::string name = entry->d_name;
|
|
||||||
if(name != "." && name != "..") // Ignore parent and self
|
|
||||||
results.emplace_back(name, entry->d_type);
|
|
||||||
}
|
|
||||||
return results;
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
DIR* dir;
|
|
||||||
};
|
|
||||||
|
|
||||||
class USBSerialGetter {
|
|
||||||
public:
|
|
||||||
USBSerialGetter(std::string usbid) {
|
|
||||||
std::stringstream ss;
|
|
||||||
auto colonpos = usbid.find(":");
|
|
||||||
if(colonpos == std::string::npos) {
|
|
||||||
succeeded = false;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ss << "/sys/bus/usb/devices/" << usbid.substr(0, colonpos) << "/serial";
|
|
||||||
try {
|
|
||||||
std::ifstream reader(ss.str());
|
|
||||||
std::getline(reader, serial);
|
|
||||||
for (auto& c : serial)
|
|
||||||
c = toupper(c);
|
|
||||||
} catch(...) {
|
|
||||||
succeeded = false;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
succeeded = true;
|
|
||||||
}
|
|
||||||
bool success() const { return succeeded; }
|
|
||||||
const std::string& getSerial() const { return serial; }
|
|
||||||
private:
|
|
||||||
bool succeeded;
|
|
||||||
std::string serial;
|
|
||||||
};
|
|
||||||
|
|
||||||
void CDCACM::Find(std::vector<FoundDevice>& found) {
|
|
||||||
Directory directory("/sys/bus/usb/drivers/cdc_acm"); // Query the CDCACM driver
|
|
||||||
if(!directory.openedSuccessfully())
|
|
||||||
return;
|
|
||||||
|
|
||||||
std::vector<std::string> foundusbs;
|
|
||||||
for(auto& entry : directory.ls()) {
|
|
||||||
/* This directory will have directories (links) for all devices using the cdc_acm driver (as CDCACM devices do)
|
|
||||||
* There will also be other files and directories providing information about the driver in here. We want to ignore them.
|
|
||||||
* Devices will be named like "7-2:1.0" where 7 is the enumeration for the USB controller, 2 is the device enumeration on
|
|
||||||
* that specific controller (will change if the device is unplugged and replugged), 1 is the device itself and 0 is
|
|
||||||
* enumeration for different services provided by the device. We're looking for the service that provides TTY.
|
|
||||||
* For now we find the directories with a digit for the first character, these are likely to be our USB devices.
|
|
||||||
*/
|
|
||||||
if(isdigit(entry.getName()[0]) && entry.getType() == DT_LNK)
|
|
||||||
foundusbs.emplace_back(entry.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Map the USB directory to the TTY and PID if found
|
|
||||||
// The PID will be filled later
|
|
||||||
std::map< std::string, std::pair<std::string, uint16_t> > foundttys;
|
|
||||||
for(auto& usb : foundusbs) {
|
|
||||||
std::stringstream ss;
|
|
||||||
ss << "/sys/bus/usb/drivers/cdc_acm/" << usb << "/tty";
|
|
||||||
Directory devicedir(ss.str());
|
|
||||||
if(!devicedir.openedSuccessfully()) // The tty directory doesn't exist, because this is not the tty service we want
|
|
||||||
continue;
|
|
||||||
|
|
||||||
auto listing = devicedir.ls();
|
|
||||||
if(listing.size() != 1) // We either got no serial ports or multiple, either way no good
|
|
||||||
continue;
|
|
||||||
|
|
||||||
foundttys.insert(std::make_pair(usb, std::make_pair(listing[0].getName(), 0)));
|
|
||||||
}
|
|
||||||
|
|
||||||
// We're going to remove from the map if this is not the product we're looking for
|
|
||||||
for(auto iter = foundttys.begin(); iter != foundttys.end(); ) {
|
|
||||||
auto& [_, pair] = *iter;
|
|
||||||
auto& [tty, ttyPid] = pair;
|
|
||||||
const std::string matchString = "PRODUCT=";
|
|
||||||
std::stringstream ss;
|
|
||||||
ss << "/sys/class/tty/" << tty << "/device/uevent"; // Read the uevent file, which contains should have a line like "PRODUCT=93c/1101/100"
|
|
||||||
std::ifstream fs(ss.str());
|
|
||||||
std::string productLine;
|
|
||||||
size_t pos = std::string::npos;
|
|
||||||
do {
|
|
||||||
std::getline(fs, productLine, '\n');
|
|
||||||
} while(((pos = productLine.find(matchString)) == std::string::npos) && !fs.eof());
|
|
||||||
|
|
||||||
if(pos != 0) { // We did not find a product line... weird
|
|
||||||
iter = foundttys.erase(iter); // Remove the element, this also moves iter forward for us
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t firstSlashPos = productLine.find('/', matchString.length());
|
|
||||||
if(firstSlashPos == std::string::npos) {
|
|
||||||
iter = foundttys.erase(iter);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
size_t pidpos = firstSlashPos + 1;
|
|
||||||
|
|
||||||
std::string vidstr = productLine.substr(matchString.length(), firstSlashPos - matchString.length());
|
|
||||||
std::string pidstr = productLine.substr(pidpos, productLine.find('/', pidpos) - pidpos); // In hex like "1101" or "93c"
|
|
||||||
|
|
||||||
uint16_t vid, pid;
|
|
||||||
try {
|
|
||||||
vid = (uint16_t)std::stoul(vidstr, nullptr, 16);
|
|
||||||
pid = (uint16_t)std::stoul(pidstr, nullptr, 16);
|
|
||||||
} catch(...) {
|
|
||||||
iter = foundttys.erase(iter); // We could not parse the numbers
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(vid != INTREPID_USB_VENDOR_ID) {
|
|
||||||
iter = foundttys.erase(iter); // Not the right VID, remove
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
ttyPid = pid; // Set the PID for this TTY
|
|
||||||
iter++; // If the loop ends without erasing the iter from the map, the item is good
|
|
||||||
}
|
|
||||||
|
|
||||||
// At this point, foundttys contains the the devices we want
|
|
||||||
|
|
||||||
// Get the serial number, create the neodevice_t
|
|
||||||
for(auto& [usb, pair] : foundttys) {
|
|
||||||
auto& [tty, ttyPid] = pair;
|
|
||||||
FoundDevice device;
|
|
||||||
|
|
||||||
USBSerialGetter getter(usb);
|
|
||||||
if(!getter.success())
|
|
||||||
continue; // Failure, could not get serial number
|
|
||||||
|
|
||||||
// In ttyACM0, we want the i to be the first character of the number
|
|
||||||
size_t i;
|
|
||||||
for(i = 0; i < tty.length(); i++) {
|
|
||||||
if(isdigit(tty[i]))
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
// Now we try to parse the number so we have a handle for later
|
|
||||||
try {
|
|
||||||
device.handle = (neodevice_handle_t)std::stoul(tty.substr(i));
|
|
||||||
/* The TTY numbering starts at zero, but we want to keep zero for an undefined
|
|
||||||
* handle, so add a constant, and we'll subtract that constant in the open function.
|
|
||||||
*/
|
|
||||||
device.handle += HANDLE_OFFSET;
|
|
||||||
} catch(...) {
|
|
||||||
continue; // Somehow this failed, have to toss the device
|
|
||||||
}
|
|
||||||
|
|
||||||
device.productId = ttyPid;
|
|
||||||
device.serial[getter.getSerial().copy(device.serial, sizeof(device.serial)-1)] = '\0';
|
|
||||||
|
|
||||||
// Add a factory to make the driver
|
|
||||||
device.makeDriver = [](const device_eventhandler_t& report, neodevice_t& device) {
|
|
||||||
return std::unique_ptr<Driver>(new CDCACM(report, device));
|
|
||||||
};
|
|
||||||
|
|
||||||
found.push_back(device); // Finally, add device to search results
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string CDCACM::HandleToTTY(neodevice_handle_t handle) {
|
|
||||||
std::stringstream ss;
|
|
||||||
ss << "/dev/ttyACM" << (int)(handle - HANDLE_OFFSET);
|
|
||||||
return ss.str();
|
|
||||||
}
|
|
||||||
@@ -1,314 +0,0 @@
|
|||||||
#include "icsneo/platform/posix/pcap.h"
|
|
||||||
#include "icsneo/communication/network.h"
|
|
||||||
#include "icsneo/communication/communication.h"
|
|
||||||
#include "icsneo/communication/ethernetpacketizer.h"
|
|
||||||
#include "icsneo/communication/packetizer.h"
|
|
||||||
#include "icsneo/communication/decoder.h"
|
|
||||||
#include <codecvt>
|
|
||||||
#include <chrono>
|
|
||||||
#include <cstring>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#ifdef __linux__
|
|
||||||
#include <netpacket/packet.h>
|
|
||||||
#else
|
|
||||||
#include <net/if_dl.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
using namespace icsneo;
|
|
||||||
|
|
||||||
std::vector<PCAP::NetworkInterface> PCAP::knownInterfaces;
|
|
||||||
|
|
||||||
void PCAP::Find(std::vector<FoundDevice>& found) {
|
|
||||||
static bool warned = false; // Only warn once for failure to open devices
|
|
||||||
|
|
||||||
// First we ask PCAP to give us all of the devices
|
|
||||||
pcap_if_t* alldevs;
|
|
||||||
char errbuf[PCAP_ERRBUF_SIZE] = { 0 };
|
|
||||||
bool success = false;
|
|
||||||
// Calling pcap_findalldevs too quickly can cause various errors. Retry a few times in this case.
|
|
||||||
for(auto retry = 0; retry < 10; retry++) {
|
|
||||||
auto ret = pcap_findalldevs(&alldevs, errbuf);
|
|
||||||
if(ret == 0) {
|
|
||||||
success = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!success) {
|
|
||||||
EventManager::GetInstance().add(APIEvent::Type::PCAPCouldNotFindDevices, APIEvent::Severity::Error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<NetworkInterface> interfaces;
|
|
||||||
for(pcap_if_t* dev = alldevs; dev != nullptr; dev = dev->next) {
|
|
||||||
if(dev->name == nullptr)
|
|
||||||
continue;
|
|
||||||
if(dev->addresses == nullptr) {
|
|
||||||
//std::cout << dev->name << " has no addresses" << std::endl;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
NetworkInterface netif;
|
|
||||||
netif.nameFromPCAP = dev->name;
|
|
||||||
if(dev->description)
|
|
||||||
netif.descriptionFromPCAP = dev->description;
|
|
||||||
pcap_addr* currentAddress = dev->addresses;
|
|
||||||
bool hasAddress = false;
|
|
||||||
while(!hasAddress && currentAddress != nullptr) {
|
|
||||||
#ifdef __linux__
|
|
||||||
if(currentAddress->addr && currentAddress->addr->sa_family == AF_PACKET) {
|
|
||||||
struct sockaddr_ll* s = (struct sockaddr_ll*)currentAddress->addr;
|
|
||||||
memcpy(netif.macAddress, s->sll_addr, sizeof(netif.macAddress));
|
|
||||||
hasAddress = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
#else // macOS and likely other BSDs
|
|
||||||
if(currentAddress->addr && currentAddress->addr->sa_family == AF_LINK) {
|
|
||||||
struct sockaddr_dl* s = (struct sockaddr_dl*)currentAddress->addr;
|
|
||||||
if(s->sdl_alen == 6 && s->sdl_alen + s->sdl_nlen < sizeof(s->sdl_data)) {
|
|
||||||
const uint8_t* mac = (uint8_t*)(s->sdl_data) + s->sdl_nlen;
|
|
||||||
memcpy(netif.macAddress, mac, sizeof(netif.macAddress));
|
|
||||||
hasAddress = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
currentAddress = currentAddress->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!hasAddress)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
interfaces.push_back(netif);
|
|
||||||
}
|
|
||||||
|
|
||||||
pcap_freealldevs(alldevs);
|
|
||||||
|
|
||||||
for(auto& iface : interfaces) {
|
|
||||||
bool exists = false;
|
|
||||||
for(auto& known : knownInterfaces)
|
|
||||||
if(memcmp(iface.macAddress, known.macAddress, sizeof(iface.macAddress)) == 0)
|
|
||||||
exists = true;
|
|
||||||
if(!exists)
|
|
||||||
knownInterfaces.emplace_back(iface);
|
|
||||||
}
|
|
||||||
|
|
||||||
for(size_t i = 0; i < knownInterfaces.size(); i++) {
|
|
||||||
auto& iface = knownInterfaces[i];
|
|
||||||
// if(iface.fullName.length() == 0)
|
|
||||||
// continue; // Win32 did not find this interface in the previous step
|
|
||||||
|
|
||||||
errbuf[0] = '\0';
|
|
||||||
iface.fp = pcap_open_live(iface.nameFromPCAP.c_str(), 65536, 1,
|
|
||||||
#ifdef __linux__ // -1 is required for instant reporting of new packets
|
|
||||||
-1, // to_ms
|
|
||||||
#else // macOS gives BIOCSRTIMEOUT for -1 and no packets for 0
|
|
||||||
1,
|
|
||||||
#endif
|
|
||||||
errbuf);
|
|
||||||
// TODO Handle warnings
|
|
||||||
// if(strlen(errbuf) != 0) { // This means a warning
|
|
||||||
// std::cout << "Warning for " << iface.nameFromPCAP << " " << errbuf << std::endl;
|
|
||||||
// }
|
|
||||||
|
|
||||||
if(iface.fp == nullptr) {
|
|
||||||
if (!warned) {
|
|
||||||
warned = true;
|
|
||||||
EventManager::GetInstance().add(APIEvent::Type::PCAPCouldNotFindDevices, APIEvent::Severity::EventWarning);
|
|
||||||
// std::cout << "pcap_open_live failed for " << iface.nameFromPCAP << " with " << errbuf << std::endl;
|
|
||||||
}
|
|
||||||
continue; // Could not open the interface
|
|
||||||
}
|
|
||||||
|
|
||||||
pcap_setnonblock(iface.fp, 1, errbuf);
|
|
||||||
|
|
||||||
EthernetPacketizer::EthernetPacket requestPacket;
|
|
||||||
memcpy(requestPacket.srcMAC, iface.macAddress, sizeof(requestPacket.srcMAC));
|
|
||||||
requestPacket.payload.reserve(4);
|
|
||||||
requestPacket.payload = {
|
|
||||||
((1 << 4) | (uint8_t)Network::NetID::Main51), // Packet size of 1 on NETID_MAIN51
|
|
||||||
(uint8_t)Command::RequestSerialNumber
|
|
||||||
};
|
|
||||||
requestPacket.payload.push_back(Packetizer::ICSChecksum(requestPacket.payload));
|
|
||||||
requestPacket.payload.insert(requestPacket.payload.begin(), 0xAA);
|
|
||||||
|
|
||||||
auto bs = requestPacket.getBytestream();
|
|
||||||
pcap_sendpacket(iface.fp, bs.data(), (int)bs.size());
|
|
||||||
|
|
||||||
auto timeout = std::chrono::high_resolution_clock::now() + std::chrono::milliseconds(50);
|
|
||||||
constexpr const size_t TempBufferSize = 4096;
|
|
||||||
RingBuffer tempBuffer(TempBufferSize);
|
|
||||||
while(std::chrono::high_resolution_clock::now() <= timeout) { // Wait up to 50ms for the response
|
|
||||||
struct pcap_pkthdr* header;
|
|
||||||
const uint8_t* data;
|
|
||||||
auto res = pcap_next_ex(iface.fp, &header, &data);
|
|
||||||
if(res == 0)
|
|
||||||
continue;
|
|
||||||
if(res < 0 || !header || !data) {
|
|
||||||
if (!warned) {
|
|
||||||
warned = true;
|
|
||||||
EventManager::GetInstance().add(APIEvent::Type::PCAPCouldNotFindDevices, APIEvent::Severity::EventWarning);
|
|
||||||
// std::cout << "pcapnextex failed with " << res << std::endl;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
EthernetPacketizer ethPacketizer([](APIEvent::Type, APIEvent::Severity) {});
|
|
||||||
memcpy(ethPacketizer.hostMAC, iface.macAddress, sizeof(ethPacketizer.hostMAC));
|
|
||||||
ethPacketizer.allowInPacketsFromAnyMAC = true;
|
|
||||||
if(!ethPacketizer.inputUp({ data, data + header->caplen }))
|
|
||||||
continue; // This packet is not for us
|
|
||||||
|
|
||||||
Packetizer packetizer([](APIEvent::Type, APIEvent::Severity) {});
|
|
||||||
tempBuffer.write(ethPacketizer.outputUp());
|
|
||||||
if(!packetizer.input(tempBuffer))
|
|
||||||
continue; // This packet was not well formed
|
|
||||||
|
|
||||||
EthernetPacketizer::EthernetPacket decoded(data, header->caplen);
|
|
||||||
Decoder decoder([](APIEvent::Type, APIEvent::Severity) {});
|
|
||||||
for(const auto& packet : packetizer.output()) {
|
|
||||||
std::shared_ptr<Message> message;
|
|
||||||
if(!decoder.decode(message, packet))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
const auto serial = std::dynamic_pointer_cast<SerialNumberMessage>(message);
|
|
||||||
if(!serial || serial->deviceSerial.size() != 6)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
FoundDevice foundDevice;
|
|
||||||
foundDevice.handle = (neodevice_handle_t)((i << 24) | (decoded.srcMAC[3] << 16) | (decoded.srcMAC[4] << 8) | (decoded.srcMAC[5]));
|
|
||||||
foundDevice.productId = decoded.srcMAC[2];
|
|
||||||
memcpy(foundDevice.serial, serial->deviceSerial.c_str(), sizeof(foundDevice.serial) - 1);
|
|
||||||
foundDevice.serial[sizeof(foundDevice.serial) - 1] = '\0';
|
|
||||||
|
|
||||||
if(std::any_of(found.begin(), found.end(), [&](const auto& found) { return ::strncmp(foundDevice.serial, found.serial, sizeof(foundDevice.serial)) == 0; }))
|
|
||||||
continue; // We already have this device on this interface
|
|
||||||
|
|
||||||
foundDevice.makeDriver = [](const device_eventhandler_t& report, neodevice_t& device) {
|
|
||||||
return std::unique_ptr<Driver>(new PCAP(report, device));
|
|
||||||
};
|
|
||||||
|
|
||||||
found.push_back(foundDevice);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pcap_close(iface.fp);
|
|
||||||
iface.fp = nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool PCAP::IsHandleValid(neodevice_handle_t handle) {
|
|
||||||
uint8_t netifIndex = (uint8_t)(handle >> 24);
|
|
||||||
return (netifIndex < knownInterfaces.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
PCAP::PCAP(device_eventhandler_t err, neodevice_t& forDevice) : Driver(err), device(forDevice), ethPacketizer(err) {
|
|
||||||
if(IsHandleValid(device.handle)) {
|
|
||||||
iface = knownInterfaces[(device.handle >> 24) & 0xFF];
|
|
||||||
iface.fp = nullptr; // We're going to open our own connection to the interface. This should already be nullptr but just in case.
|
|
||||||
|
|
||||||
deviceMAC[0] = 0x00;
|
|
||||||
deviceMAC[1] = 0xFC;
|
|
||||||
deviceMAC[2] = 0x70;
|
|
||||||
deviceMAC[3] = (device.handle >> 16) & 0xFF;
|
|
||||||
deviceMAC[4] = (device.handle >> 8) & 0xFF;
|
|
||||||
deviceMAC[5] = device.handle & 0xFF;
|
|
||||||
memcpy(ethPacketizer.deviceMAC, deviceMAC, 6);
|
|
||||||
memcpy(ethPacketizer.hostMAC, iface.macAddress, 6);
|
|
||||||
} else {
|
|
||||||
openable = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool PCAP::open() {
|
|
||||||
if(!openable)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if(isOpen())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// Open the interface
|
|
||||||
iface.fp = pcap_open_live(iface.nameFromPCAP.c_str(), 65536, 1,
|
|
||||||
#ifdef __linux__ // -1 is required for instant reporting of new packets
|
|
||||||
-1, // to_ms
|
|
||||||
#else // macOS gives BIOCSRTIMEOUT for -1 and no packets for 0
|
|
||||||
1,
|
|
||||||
#endif
|
|
||||||
errbuf);
|
|
||||||
if(iface.fp == nullptr) {
|
|
||||||
report(APIEvent::Type::DriverFailedToOpen, APIEvent::Severity::Error);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
pcap_setnonblock(iface.fp, 0, errbuf);
|
|
||||||
pcap_set_immediate_mode(iface.fp, 1);
|
|
||||||
|
|
||||||
// Create threads
|
|
||||||
readThread = std::thread(&PCAP::readTask, this);
|
|
||||||
writeThread = std::thread(&PCAP::writeTask, this);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool PCAP::isOpen() {
|
|
||||||
return iface.fp != nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool PCAP::close() {
|
|
||||||
if(!isOpen())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
setIsClosing(true); // Signal the threads that we are closing
|
|
||||||
pcap_breakloop(iface.fp);
|
|
||||||
#ifndef __linux__
|
|
||||||
pthread_cancel(readThread.native_handle());
|
|
||||||
#endif
|
|
||||||
readThread.join();
|
|
||||||
writeThread.join();
|
|
||||||
setIsClosing(false);
|
|
||||||
|
|
||||||
pcap_close(iface.fp);
|
|
||||||
iface.fp = nullptr;
|
|
||||||
|
|
||||||
clearBuffers();
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void PCAP::readTask() {
|
|
||||||
EventManager::GetInstance().downgradeErrorsOnCurrentThread();
|
|
||||||
while (!isClosing()) {
|
|
||||||
pcap_dispatch(iface.fp, -1, [](uint8_t* obj, const struct pcap_pkthdr* header, const uint8_t* data) {
|
|
||||||
PCAP* driver = reinterpret_cast<PCAP*>(obj);
|
|
||||||
if(driver->ethPacketizer.inputUp({data, data + header->caplen})) {
|
|
||||||
const auto bytes = driver->ethPacketizer.outputUp();
|
|
||||||
driver->pushRx(bytes.data(), bytes.size());
|
|
||||||
}
|
|
||||||
}, (uint8_t*)this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void PCAP::writeTask() {
|
|
||||||
WriteOperation writeOp;
|
|
||||||
EventManager::GetInstance().downgradeErrorsOnCurrentThread();
|
|
||||||
|
|
||||||
while(!isClosing()) {
|
|
||||||
if(!writeQueue.wait_dequeue_timed(writeOp, std::chrono::milliseconds(100)))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// If we have a bunch of small packets to send, try to pack them into a packet
|
|
||||||
// We use the average packet size to determine if we're likely to have enough room
|
|
||||||
size_t bytesPushed = 0;
|
|
||||||
size_t packetsPushed = 0;
|
|
||||||
do {
|
|
||||||
packetsPushed++;
|
|
||||||
bytesPushed += writeOp.bytes.size();
|
|
||||||
ethPacketizer.inputDown(std::move(writeOp.bytes));
|
|
||||||
} while(bytesPushed < (EthernetPacketizer::MaxPacketLength - (bytesPushed / packetsPushed * 2)) && writeQueue.try_dequeue(writeOp));
|
|
||||||
|
|
||||||
for(const auto& packet : ethPacketizer.outputDown()) {
|
|
||||||
pcap_sendpacket(iface.fp, packet.data(), (int)packet.size());
|
|
||||||
}
|
|
||||||
// TODO Handle packet send errors
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,573 +0,0 @@
|
|||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
#include <winsock2.h>
|
|
||||||
#include <ws2tcpip.h>
|
|
||||||
#include <iphlpapi.h>
|
|
||||||
#else
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <poll.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <ifaddrs.h>
|
|
||||||
#include <net/if.h>
|
|
||||||
#include <vector>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <cstring>
|
|
||||||
#include <netdb.h>
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <optional>
|
|
||||||
#include "icsneo/platform/tcp.h"
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
#define WIN_INT(a) static_cast<int>(a)
|
|
||||||
#else
|
|
||||||
#define WIN_INT(a) a
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef __APPLE__
|
|
||||||
#define APPLE_SIN_LEN(a) a.sin_len = sizeof(struct sockaddr_in);
|
|
||||||
#else
|
|
||||||
#define APPLE_SIN_LEN(a)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
using namespace icsneo;
|
|
||||||
|
|
||||||
TCP::Socket::Socket(int domain, int type, int protocol, bool nonblocking) {
|
|
||||||
#ifdef _WIN32
|
|
||||||
class WSAState {
|
|
||||||
public:
|
|
||||||
WSAState() {
|
|
||||||
WSADATA wsaData = {};
|
|
||||||
valid = ::WSAStartup(MAKEWORD(2, 2), &wsaData) == 0;
|
|
||||||
}
|
|
||||||
~WSAState() {
|
|
||||||
::WSACleanup();
|
|
||||||
}
|
|
||||||
explicit operator bool() const { return valid; }
|
|
||||||
private:
|
|
||||||
bool valid = false;
|
|
||||||
};
|
|
||||||
static const WSAState WSA_STATE;
|
|
||||||
if(!WSA_STATE)
|
|
||||||
return;
|
|
||||||
#endif
|
|
||||||
fd = ::socket(domain, type, protocol);
|
|
||||||
if(nonblocking) {
|
|
||||||
#ifdef _WIN32
|
|
||||||
unsigned long param = 1;
|
|
||||||
::ioctlsocket(fd, FIONBIO, ¶m);
|
|
||||||
#else
|
|
||||||
const int flags = fcntl(fd, F_GETFL, 0);
|
|
||||||
::fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TCP::Socket::~Socket() {
|
|
||||||
#ifdef _WIN32
|
|
||||||
::closesocket(fd);
|
|
||||||
#else
|
|
||||||
::close(fd);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void TCP::Socket::poll(uint16_t event, uint32_t msTimeout) {
|
|
||||||
#ifdef _WIN32
|
|
||||||
WSAPOLLFD pfd;
|
|
||||||
pfd.fd = fd;
|
|
||||||
pfd.events = event;
|
|
||||||
::WSAPoll(&pfd, 1, msTimeout);
|
|
||||||
#else
|
|
||||||
struct pollfd pfd;
|
|
||||||
pfd.fd = fd;
|
|
||||||
pfd.events = event;
|
|
||||||
::poll(&pfd, 1, msTimeout);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void TCP::Find(std::vector<FoundDevice>& found) {
|
|
||||||
static const auto tcpDisabled = []() -> bool {
|
|
||||||
#ifdef _MSC_VER
|
|
||||||
#pragma warning(push)
|
|
||||||
#pragma warning(disable : 4996)
|
|
||||||
#endif
|
|
||||||
const auto disabled = std::getenv("LIBICSNEO_DISABLE_TCP");
|
|
||||||
return disabled ? std::stoi(disabled) : false;
|
|
||||||
#ifdef _MSC_VER
|
|
||||||
#pragma warning(pop)
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
if(tcpDisabled()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const auto MDNS_PORT = htons((unsigned short)5353);
|
|
||||||
static const auto MDNS_IP = htonl((((uint32_t)224U) << 24U) | ((uint32_t)251U));
|
|
||||||
|
|
||||||
class IFAddresses {
|
|
||||||
public:
|
|
||||||
#ifdef _WIN32
|
|
||||||
typedef IP_ADAPTER_ADDRESSES* InterfaceHandle;
|
|
||||||
#else
|
|
||||||
typedef ifaddrs* InterfaceHandle;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
class Interface {
|
|
||||||
public:
|
|
||||||
Interface(InterfaceHandle handle) : handle(handle) {}
|
|
||||||
Interface next() {
|
|
||||||
#ifdef _WIN32
|
|
||||||
return Interface(handle->Next);
|
|
||||||
#else
|
|
||||||
return Interface(handle->ifa_next);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
unsigned flags() {
|
|
||||||
#ifdef _WIN32
|
|
||||||
return handle->Flags;
|
|
||||||
#else
|
|
||||||
return handle->ifa_flags;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
explicit operator bool() {
|
|
||||||
return handle;
|
|
||||||
}
|
|
||||||
bool validType() {
|
|
||||||
#ifdef _WIN32
|
|
||||||
return
|
|
||||||
handle &&
|
|
||||||
(handle->TunnelType != TUNNEL_TYPE_TEREDO) &&
|
|
||||||
(handle->OperStatus == IfOperStatusUp) &&
|
|
||||||
(address()->sa_family == AF_INET);
|
|
||||||
#else
|
|
||||||
return
|
|
||||||
handle &&
|
|
||||||
handle->ifa_addr &&
|
|
||||||
(flags() & IFF_UP) &&
|
|
||||||
(flags() & IFF_MULTICAST) &&
|
|
||||||
!(flags() & IFF_LOOPBACK) &&
|
|
||||||
!(flags() & IFF_POINTOPOINT) &&
|
|
||||||
(handle->ifa_addr->sa_family == AF_INET) &&
|
|
||||||
(((sockaddr_in*)address())->sin_addr.s_addr != htonl(INADDR_LOOPBACK));
|
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
|
||||||
InterfaceHandle operator->() {
|
|
||||||
return handle;
|
|
||||||
}
|
|
||||||
sockaddr* address() const {
|
|
||||||
#ifdef _WIN32
|
|
||||||
return handle->FirstUnicastAddress->Address.lpSockaddr;
|
|
||||||
#else
|
|
||||||
return handle->ifa_addr;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
std::string_view name() const {
|
|
||||||
#ifdef _WIN32
|
|
||||||
return handle->AdapterName;
|
|
||||||
#else
|
|
||||||
return handle->ifa_name;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
InterfaceHandle handle;
|
|
||||||
};
|
|
||||||
|
|
||||||
IFAddresses() {
|
|
||||||
#ifdef _WIN32
|
|
||||||
unsigned long ret;
|
|
||||||
unsigned long size = 15'000;
|
|
||||||
do {
|
|
||||||
storage.resize(size);
|
|
||||||
ret = ::GetAdaptersAddresses(AF_INET, 0, NULL, (InterfaceHandle)storage.data(), &size);
|
|
||||||
} while (ret == ERROR_BUFFER_OVERFLOW);
|
|
||||||
front = (InterfaceHandle)storage.data();
|
|
||||||
#else
|
|
||||||
::getifaddrs(&front);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
~IFAddresses() {
|
|
||||||
#ifdef _WIN32
|
|
||||||
#else
|
|
||||||
::freeifaddrs(front);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
explicit operator bool() {
|
|
||||||
return front;
|
|
||||||
}
|
|
||||||
Interface begin() const {
|
|
||||||
return Interface(front);
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
#ifdef _WIN32
|
|
||||||
std::vector<uint8_t> storage;
|
|
||||||
#endif
|
|
||||||
InterfaceHandle front;
|
|
||||||
};
|
|
||||||
|
|
||||||
IFAddresses interfaces;
|
|
||||||
if(!interfaces) {
|
|
||||||
EventManager::GetInstance().add(APIEvent::Type::GetIfAddrsError, APIEvent::Severity::EventWarning);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for(auto intf = interfaces.begin(); intf; intf = intf.next()) {
|
|
||||||
if(!intf.validType())
|
|
||||||
continue;
|
|
||||||
Socket socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
||||||
if(!socket) {
|
|
||||||
EventManager::GetInstance().add(APIEvent::Type::SocketFailedToOpen, APIEvent::Severity::EventWarning);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
unsigned int reuse = 1;
|
|
||||||
if(::setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) < 0) {
|
|
||||||
EventManager::GetInstance().add(APIEvent::Type::ErrorSettingSocketOption, APIEvent::Severity::EventWarning);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
#ifndef _WIN32
|
|
||||||
if(::setsockopt(socket, SOL_SOCKET, SO_REUSEPORT, (const char*)&reuse, sizeof(reuse)) < 0) {
|
|
||||||
EventManager::GetInstance().add(APIEvent::Type::ErrorSettingSocketOption, APIEvent::Severity::EventWarning);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
#ifndef __APPLE__
|
|
||||||
if(::setsockopt(socket, SOL_SOCKET, SO_BINDTODEVICE, intf.name().data(), intf.name().size()) < 0) {
|
|
||||||
EventManager::GetInstance().add(APIEvent::Type::ErrorSettingSocketOption, APIEvent::Severity::EventWarning);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
auto ifAddrIn = (sockaddr_in*)intf.address();
|
|
||||||
ifAddrIn->sin_port = MDNS_PORT;
|
|
||||||
|
|
||||||
{
|
|
||||||
sockaddr_in addr = *ifAddrIn;
|
|
||||||
APPLE_SIN_LEN(addr);
|
|
||||||
::setsockopt(socket, IPPROTO_IP, IP_MULTICAST_IF, (const char*)&addr.sin_addr, sizeof(addr.sin_addr));
|
|
||||||
#ifndef _WIN32
|
|
||||||
addr.sin_addr.s_addr = INADDR_ANY;
|
|
||||||
#endif
|
|
||||||
if(::bind(socket, (sockaddr*)&addr, sizeof(addr)) == -1) {
|
|
||||||
EventManager::GetInstance().add(APIEvent::Type::FailedToBind, APIEvent::Severity::EventWarning);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
ip_mreq req = {};
|
|
||||||
req.imr_multiaddr.s_addr = htonl((((uint32_t)224U) << 24U) | ((uint32_t)251U));
|
|
||||||
req.imr_interface = ifAddrIn->sin_addr;
|
|
||||||
::setsockopt(socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&req, sizeof(req));
|
|
||||||
}
|
|
||||||
|
|
||||||
std::array<uint8_t, 35> query = {
|
|
||||||
0x00, 0x00, /* id */
|
|
||||||
0x00, 0x00, /* flags */
|
|
||||||
0x00, 0x01, /* query count */
|
|
||||||
0x00, 0x00, /* answer count */
|
|
||||||
0x00, 0x00, /* auth count */
|
|
||||||
0x00, 0x00, /* additional count */
|
|
||||||
0x06, '_', 'n', 'e', 'o', 'v', 'i', 0x04, '_', 't', 'c', 'p', 0x05, 'l', 'o', 'c', 'a', 'l', 0x00,
|
|
||||||
0x00, 0x0c, /* type*/
|
|
||||||
0x00, 0x01 /* class */
|
|
||||||
};
|
|
||||||
sockaddr_in addr = {};
|
|
||||||
addr.sin_family = AF_INET;
|
|
||||||
addr.sin_addr.s_addr = MDNS_IP;
|
|
||||||
addr.sin_port = MDNS_PORT;
|
|
||||||
APPLE_SIN_LEN(addr);
|
|
||||||
if(::sendto(socket, (char*)query.data(), WIN_INT(query.size()), 0, (sockaddr*)&addr, sizeof(addr)) < 0) {
|
|
||||||
EventManager::GetInstance().add(APIEvent::Type::SendToError, APIEvent::Severity::EventWarning);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const auto rxTill = std::chrono::steady_clock::now() + std::chrono::milliseconds(100);
|
|
||||||
while(std::chrono::steady_clock::now() < rxTill) {
|
|
||||||
static constexpr size_t bufferLen = 2048;
|
|
||||||
uint8_t buffer[bufferLen];
|
|
||||||
// keep trying till the timeout
|
|
||||||
const auto msWait = std::chrono::duration_cast<std::chrono::milliseconds>(rxTill - std::chrono::steady_clock::now()).count();
|
|
||||||
if(msWait < 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
socket.poll(POLLIN, static_cast<uint32_t>(msWait));
|
|
||||||
const auto recvRet = ::recv(socket, (char*)buffer, bufferLen, 0);
|
|
||||||
static constexpr auto headerLength = 12;
|
|
||||||
if(recvRet < headerLength) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
uint8_t* bufferEnd = buffer + recvRet;
|
|
||||||
const auto flags = ntohs(*(uint16_t*)(buffer + 2));
|
|
||||||
const bool isResponse = flags >> 15;
|
|
||||||
if(!isResponse)
|
|
||||||
continue;
|
|
||||||
const auto answerCount = ntohs(*(uint16_t*)(buffer + 6));
|
|
||||||
const auto additionalCount = ntohs(*(uint16_t*)(buffer + 10));
|
|
||||||
if(answerCount < 1 || additionalCount < 1) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
std::function<std::optional<size_t>(uint8_t*,std::vector<std::string_view>&)> parseStrings = [&](uint8_t* start, std::vector<std::string_view>& strings) -> std::optional<size_t> {
|
|
||||||
uint8_t* origStart = start;
|
|
||||||
for(size_t i = 0; i < 10 /* infinite loop prevention */; ++i) {
|
|
||||||
if(start >= bufferEnd)
|
|
||||||
return std::nullopt;
|
|
||||||
static constexpr uint8_t isCompressed = 0xC0;
|
|
||||||
if(*start & isCompressed) {
|
|
||||||
if(start + 2 > bufferEnd)
|
|
||||||
return std::nullopt;
|
|
||||||
uint16_t offset = ntohs(*(uint16_t*)start) & 0x3FFF;
|
|
||||||
if(buffer + offset > bufferEnd)
|
|
||||||
return std::nullopt;
|
|
||||||
parseStrings(buffer + offset, strings);
|
|
||||||
start += 2;
|
|
||||||
return start - origStart;
|
|
||||||
} else if(*start == 0) {
|
|
||||||
return start + 1 - origStart;
|
|
||||||
} else {
|
|
||||||
if(start + 1 /* skip the length */ + *start > bufferEnd - 1 /* every string ends with '\0' */)
|
|
||||||
return std::nullopt;
|
|
||||||
strings.emplace_back((char*)(start + 1), *start);
|
|
||||||
start += 1 + *start;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return std::nullopt;
|
|
||||||
};
|
|
||||||
struct Record {
|
|
||||||
enum class Type {
|
|
||||||
PTR = 0x000C,
|
|
||||||
SRV = 0x0021,
|
|
||||||
A = 0x0001,
|
|
||||||
};
|
|
||||||
std::vector<std::string_view> name;
|
|
||||||
Type type;
|
|
||||||
uint8_t* data;
|
|
||||||
uint16_t dataLength;
|
|
||||||
};
|
|
||||||
const auto parseRecord = [&](uint8_t* start, Record& parsedRecord) -> std::optional<size_t> {
|
|
||||||
uint8_t* origStart = start;
|
|
||||||
const auto nameLength = parseStrings(start, parsedRecord.name);
|
|
||||||
if(!nameLength)
|
|
||||||
return std::nullopt;
|
|
||||||
if(start + *nameLength + 10 /* type + flags + TTL + data length */ > bufferEnd)
|
|
||||||
return std::nullopt;
|
|
||||||
start += *nameLength;
|
|
||||||
parsedRecord.type = Record::Type(ntohs(*(uint16_t*)start));
|
|
||||||
start += 8; /* type + flags + TTL */
|
|
||||||
parsedRecord.dataLength = ntohs(*(uint16_t*)start);
|
|
||||||
start += 2;
|
|
||||||
parsedRecord.data = start;
|
|
||||||
start += parsedRecord.dataLength;
|
|
||||||
return start - origStart;
|
|
||||||
};
|
|
||||||
|
|
||||||
const auto parseRecords = [&](uint8_t* start, std::vector<Record>& parsed) -> std::optional<size_t> {
|
|
||||||
uint8_t* origStart = start;
|
|
||||||
for(auto& record : parsed) {
|
|
||||||
const auto recordLength = parseRecord(start, record);
|
|
||||||
if(!recordLength || start + *recordLength > bufferEnd)
|
|
||||||
return std::nullopt;
|
|
||||||
start += *recordLength;
|
|
||||||
}
|
|
||||||
return start - origStart;
|
|
||||||
};
|
|
||||||
std::vector<Record> answers(answerCount);
|
|
||||||
const auto answersLength = parseRecords(buffer + headerLength, answers);
|
|
||||||
if(!answersLength)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
std::vector<Record> additional(additionalCount);
|
|
||||||
const auto additionalLength = parseRecords(buffer + headerLength + *answersLength, additional);
|
|
||||||
if(!additionalLength)
|
|
||||||
continue;
|
|
||||||
FoundDevice foundDevice;
|
|
||||||
|
|
||||||
const auto fillSerial = [&]() -> bool {
|
|
||||||
for(const auto& record : answers) {
|
|
||||||
if(record.name.size() == 3 && record.name[0] == "_neovi" && record.name[1] == "_tcp" && record.name[2] == "local") {
|
|
||||||
constexpr size_t deviceSerialBufferLength = sizeof(foundDevice.serial);
|
|
||||||
std::copy(record.data + 1 /* length field */, record.data + deviceSerialBufferLength, foundDevice.serial);
|
|
||||||
foundDevice.serial[deviceSerialBufferLength - 1] = '\0';
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
|
|
||||||
if(!fillSerial())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
std::optional<uint32_t> devIP;
|
|
||||||
std::optional<uint16_t> devPort;
|
|
||||||
const auto fillDevEndpoint = [&]() -> bool {
|
|
||||||
for(const auto& record : additional) {
|
|
||||||
if(record.name.size() == 4 && record.name[1] == "_neovi" && record.name[2] == "_tcp" && record.name[3] == "local") {
|
|
||||||
if(record.type == Record::Type::A) {
|
|
||||||
if(record.dataLength != 4)
|
|
||||||
return false;
|
|
||||||
devIP.emplace(ntohl(*(uint32_t*)record.data));
|
|
||||||
} else if(record.type == Record::Type::SRV) {
|
|
||||||
if(record.dataLength != 8 /* priority + weight + port */)
|
|
||||||
return false;
|
|
||||||
devPort.emplace(ntohs(*(uint16_t*)(record.data + 4)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return devIP && devPort;
|
|
||||||
};
|
|
||||||
|
|
||||||
if(!fillDevEndpoint())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
NetworkInterface on = {
|
|
||||||
std::string(intf.name()),
|
|
||||||
ntohl(ifAddrIn->sin_addr.s_addr)
|
|
||||||
};
|
|
||||||
|
|
||||||
foundDevice.makeDriver = [=](const device_eventhandler_t& eh, neodevice_t&) {
|
|
||||||
return std::unique_ptr<Driver>(new TCP(eh, on, *devIP, *devPort));
|
|
||||||
};
|
|
||||||
found.push_back(std::move(foundDevice));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TCP::TCP(const device_eventhandler_t& err, NetworkInterface on, uint32_t dstIP, uint16_t dstPort) :
|
|
||||||
Driver(err), interfaceDescription(on), dstIP(dstIP), dstPort(dstPort) {
|
|
||||||
}
|
|
||||||
|
|
||||||
bool TCP::open() {
|
|
||||||
if(socket) {
|
|
||||||
report(APIEvent::Type::DeviceCurrentlyOpen, APIEvent::Severity::Error);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto partiallyOpenSocket = std::make_unique<Socket>(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
|
||||||
|
|
||||||
#if !defined(_WIN32) && !defined(__APPLE__)
|
|
||||||
if(::setsockopt(*partiallyOpenSocket, SOL_SOCKET, SO_BINDTODEVICE, interfaceDescription.name.c_str(), interfaceDescription.name.size()) < 0) {
|
|
||||||
report(APIEvent::Type::ErrorSettingSocketOption, APIEvent::Severity::Error);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
{
|
|
||||||
sockaddr_in addr = {};
|
|
||||||
addr.sin_family = AF_INET;
|
|
||||||
addr.sin_addr.s_addr = htonl(interfaceDescription.ip);
|
|
||||||
APPLE_SIN_LEN(addr);
|
|
||||||
if(::bind(*partiallyOpenSocket, (sockaddr*)&addr, sizeof(addr)) < 0) {
|
|
||||||
report(APIEvent::Type::FailedToBind, APIEvent::Severity::Error);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
sockaddr_in addr = {};
|
|
||||||
addr.sin_family = AF_INET;
|
|
||||||
addr.sin_addr.s_addr = htonl(dstIP);
|
|
||||||
addr.sin_port = htons(dstPort);
|
|
||||||
APPLE_SIN_LEN(addr);
|
|
||||||
|
|
||||||
// the socket is non-blocking so it's expected that the first connect will fail
|
|
||||||
if(::connect(*partiallyOpenSocket, (sockaddr*)&addr, sizeof(addr)) == 0) {
|
|
||||||
report(APIEvent::Type::DriverFailedToOpen, APIEvent::Severity::Error);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
#ifdef _WIN32
|
|
||||||
if(::WSAGetLastError() != WSAEWOULDBLOCK) {
|
|
||||||
report(APIEvent::Type::DriverFailedToOpen, APIEvent::Severity::Error);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
if(errno != EINPROGRESS) {
|
|
||||||
report(APIEvent::Type::DriverFailedToOpen, APIEvent::Severity::Error);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
partiallyOpenSocket->poll(POLLOUT, 1000);
|
|
||||||
|
|
||||||
if(::connect(*partiallyOpenSocket, (sockaddr*)&addr, sizeof(addr)) < 0) {
|
|
||||||
#ifdef _WIN32
|
|
||||||
if(::WSAGetLastError() != WSAEISCONN) {
|
|
||||||
report(APIEvent::Type::DriverFailedToOpen, APIEvent::Severity::Error);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
#elif defined(__APPLE__)
|
|
||||||
if(errno != EISCONN) {
|
|
||||||
report(APIEvent::Type::DriverFailedToOpen, APIEvent::Severity::Error);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
report(APIEvent::Type::DriverFailedToOpen, APIEvent::Severity::Error);
|
|
||||||
return false;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
socket = std::move(partiallyOpenSocket);
|
|
||||||
readThread = std::thread(&TCP::readTask, this);
|
|
||||||
writeThread = std::thread(&TCP::writeTask, this);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool TCP::isOpen() {
|
|
||||||
return socket ? true : false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool TCP::close() {
|
|
||||||
if(!isOpen() && !isDisconnected()) {
|
|
||||||
report(APIEvent::Type::DeviceCurrentlyClosed, APIEvent::Severity::Error);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
setIsClosing(true);
|
|
||||||
setIsDisconnected(false);
|
|
||||||
|
|
||||||
if(readThread.joinable())
|
|
||||||
readThread.join();
|
|
||||||
if(writeThread.joinable())
|
|
||||||
writeThread.join();
|
|
||||||
|
|
||||||
clearBuffers();
|
|
||||||
|
|
||||||
socket.reset();
|
|
||||||
setIsClosing(false);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void TCP::readTask() {
|
|
||||||
EventManager::GetInstance().downgradeErrorsOnCurrentThread();
|
|
||||||
|
|
||||||
constexpr size_t READ_BUFFER_SIZE = 2048;
|
|
||||||
uint8_t readbuf[READ_BUFFER_SIZE];
|
|
||||||
while(!isClosing()) {
|
|
||||||
if(const auto received = ::recv(*socket, (char*)readbuf, READ_BUFFER_SIZE, 0); received > 0) {
|
|
||||||
pushRx(readbuf, received);
|
|
||||||
} else {
|
|
||||||
socket->poll(POLLIN, 100);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void TCP::writeTask() {
|
|
||||||
EventManager::GetInstance().downgradeErrorsOnCurrentThread();
|
|
||||||
|
|
||||||
WriteOperation writeOp;
|
|
||||||
while(!isClosing()) {
|
|
||||||
if(!writeQueue.wait_dequeue_timed(writeOp, std::chrono::milliseconds(100)))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
while(!isClosing()) {
|
|
||||||
if(::send(*socket, (char*)writeOp.bytes.data(), WIN_INT(writeOp.bytes.size()), 0) > 0)
|
|
||||||
break;
|
|
||||||
socket->poll(POLLOUT, 100);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,244 +0,0 @@
|
|||||||
#include "icsneo/platform/windows/cdcacm.h"
|
|
||||||
|
|
||||||
#include <setupapi.h>
|
|
||||||
#include <initguid.h>
|
|
||||||
#include <usbiodef.h>
|
|
||||||
#include <devpkey.h>
|
|
||||||
|
|
||||||
using namespace icsneo;
|
|
||||||
|
|
||||||
#define SERIAL_SIZE 6
|
|
||||||
|
|
||||||
CDCACM::CDCACM(const device_eventhandler_t& err, const std::wstring& path) : Driver(err), path(path) {
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CDCACM::open() {
|
|
||||||
handle = CreateFileW(path.c_str(), GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, nullptr);
|
|
||||||
if(handle == INVALID_HANDLE_VALUE) {
|
|
||||||
EventManager::GetInstance().add(APIEvent::Type::SyscallError, APIEvent::Severity::Error);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
COMMTIMEOUTS timeouts;
|
|
||||||
timeouts.ReadIntervalTimeout = MAXDWORD;
|
|
||||||
timeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
|
|
||||||
timeouts.ReadTotalTimeoutConstant = MAXDWORD - 1;
|
|
||||||
timeouts.WriteTotalTimeoutMultiplier = 0;
|
|
||||||
timeouts.WriteTotalTimeoutConstant = 0;
|
|
||||||
|
|
||||||
if(!SetCommTimeouts(handle, &timeouts)) {
|
|
||||||
EventManager::GetInstance().add(APIEvent::Type::SyscallError, APIEvent::Severity::Error);
|
|
||||||
CloseHandle(handle);
|
|
||||||
handle = INVALID_HANDLE_VALUE;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
DCB comstate;
|
|
||||||
if(!GetCommState(handle, &comstate)) {
|
|
||||||
EventManager::GetInstance().add(APIEvent::Type::SyscallError, APIEvent::Severity::Error);
|
|
||||||
CloseHandle(handle);
|
|
||||||
handle = INVALID_HANDLE_VALUE;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
comstate.BaudRate = 115200;
|
|
||||||
comstate.ByteSize = 8;
|
|
||||||
comstate.fRtsControl = RTS_CONTROL_DISABLE;
|
|
||||||
if(!SetCommState(handle, &comstate)) {
|
|
||||||
EventManager::GetInstance().add(APIEvent::Type::SyscallError, APIEvent::Severity::Error);
|
|
||||||
CloseHandle(handle);
|
|
||||||
handle = INVALID_HANDLE_VALUE;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
PurgeComm(handle, PURGE_RXCLEAR);
|
|
||||||
|
|
||||||
readOverlapped.hEvent = CreateEventA(nullptr, false, false, nullptr);
|
|
||||||
writeOverlapped.hEvent = CreateEventA(nullptr, false, false, nullptr);
|
|
||||||
|
|
||||||
setIsDisconnected(false);
|
|
||||||
readThread = std::thread(&CDCACM::read, this);
|
|
||||||
writeThread = std::thread(&CDCACM::write, this);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CDCACM::isOpen() {
|
|
||||||
return handle != INVALID_HANDLE_VALUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CDCACM::close() {
|
|
||||||
setIsClosing(true);
|
|
||||||
SetEvent(readOverlapped.hEvent); // unblock read thread
|
|
||||||
SetEvent(writeOverlapped.hEvent); // unblock write thread if waiting on COM write
|
|
||||||
writeQueue.enqueue(WriteOperation{}); // unblock write thread if waiting on write queue pop
|
|
||||||
readThread.join();
|
|
||||||
writeThread.join();
|
|
||||||
CloseHandle(readOverlapped.hEvent);
|
|
||||||
CloseHandle(writeOverlapped.hEvent);
|
|
||||||
CloseHandle(handle);
|
|
||||||
handle = INVALID_HANDLE_VALUE;
|
|
||||||
setIsClosing(false);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void CDCACM::read() {
|
|
||||||
EventManager::GetInstance().downgradeErrorsOnCurrentThread();
|
|
||||||
|
|
||||||
std::vector<uint8_t> buffer(ICSNEO_DRIVER_RINGBUFFER_SIZE);
|
|
||||||
|
|
||||||
while(!isDisconnected() && !isClosing()) {
|
|
||||||
if(!ReadFile(handle, buffer.data(), (DWORD)buffer.size(), nullptr, &readOverlapped)) {
|
|
||||||
if(GetLastError() != ERROR_IO_PENDING) {
|
|
||||||
EventManager::GetInstance().add(APIEvent::Type::SyscallError, APIEvent::Severity::Error);
|
|
||||||
setIsDisconnected(true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
DWORD read = 0;
|
|
||||||
if(!GetOverlappedResult(handle, &readOverlapped, &read, true)) {
|
|
||||||
EventManager::GetInstance().add(APIEvent::Type::SyscallError, APIEvent::Severity::Error);
|
|
||||||
setIsDisconnected(true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if(read == 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
while(!isDisconnected() && !isClosing()) {
|
|
||||||
if(pushRx(buffer.data(), read))
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void CDCACM::write() {
|
|
||||||
EventManager::GetInstance().downgradeErrorsOnCurrentThread();
|
|
||||||
|
|
||||||
WriteOperation writeOp;
|
|
||||||
|
|
||||||
while(!isDisconnected() && !isClosing()) {
|
|
||||||
writeQueue.wait_dequeue(writeOp);
|
|
||||||
|
|
||||||
if(isDisconnected() || isClosing()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!WriteFile(handle, writeOp.bytes.data(), (DWORD)writeOp.bytes.size(), nullptr, &writeOverlapped)) {
|
|
||||||
if(GetLastError() != ERROR_IO_PENDING) {
|
|
||||||
EventManager::GetInstance().add(APIEvent::Type::SyscallError, APIEvent::Severity::Error);
|
|
||||||
setIsDisconnected(true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
DWORD written;
|
|
||||||
if(!GetOverlappedResult(handle, &writeOverlapped, &written, true)) {
|
|
||||||
EventManager::GetInstance().add(APIEvent::Type::SyscallError, APIEvent::Severity::Error);
|
|
||||||
setIsDisconnected(true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(written != writeOp.bytes.size()) {
|
|
||||||
EventManager::GetInstance().add(APIEvent::Type::FailedToWrite, APIEvent::Severity::Error);
|
|
||||||
setIsDisconnected(true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class DeviceInfo {
|
|
||||||
public:
|
|
||||||
DeviceInfo() {
|
|
||||||
mDeviceInfo = SetupDiGetClassDevsW(&GUID_DEVINTERFACE_USB_DEVICE, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
|
|
||||||
}
|
|
||||||
~DeviceInfo() {
|
|
||||||
SetupDiDestroyDeviceInfoList(mDeviceInfo);
|
|
||||||
}
|
|
||||||
operator HDEVINFO() const {
|
|
||||||
return mDeviceInfo;
|
|
||||||
}
|
|
||||||
operator bool() const {
|
|
||||||
return mDeviceInfo != INVALID_HANDLE_VALUE;
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
HDEVINFO mDeviceInfo;
|
|
||||||
};
|
|
||||||
|
|
||||||
class DeviceInfoData {
|
|
||||||
public:
|
|
||||||
DeviceInfoData() {
|
|
||||||
mDeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
|
|
||||||
}
|
|
||||||
operator SP_DEVINFO_DATA*() {
|
|
||||||
return &mDeviceInfoData;
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
SP_DEVINFO_DATA mDeviceInfoData;
|
|
||||||
};
|
|
||||||
|
|
||||||
static constexpr size_t WSTRING_ELEMENT_SIZE = sizeof(std::wstring::value_type);
|
|
||||||
|
|
||||||
void CDCACM::Find(std::vector<FoundDevice>& found) {
|
|
||||||
DeviceInfoData deviceInfoData;
|
|
||||||
const std::wstring intrepidUSB(L"USB\\VID_093C");
|
|
||||||
DeviceInfo deviceInfoSet;
|
|
||||||
if(!deviceInfoSet) {
|
|
||||||
EventManager::GetInstance().add(APIEvent::Type::SyscallError, APIEvent::Severity::Error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for(DWORD i = 0; SetupDiEnumDeviceInfo(deviceInfoSet, i, deviceInfoData); ++i) {
|
|
||||||
DWORD DataT;
|
|
||||||
DWORD buffersize = 0;
|
|
||||||
|
|
||||||
std::wstring wclass;
|
|
||||||
while(!SetupDiGetDevicePropertyW(deviceInfoSet, deviceInfoData, &DEVPKEY_Device_Class, &DataT, reinterpret_cast<PBYTE>(wclass.data()), static_cast<DWORD>((wclass.size() + 1) * WSTRING_ELEMENT_SIZE), &buffersize, 0)) {
|
|
||||||
wclass.resize((buffersize - 1) / WSTRING_ELEMENT_SIZE);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(wclass != L"Ports") {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: is this a bug in Windows? why is this returned size different/wrong? It's like it's not a wstring at all
|
|
||||||
std::wstring deviceInstanceId;
|
|
||||||
while(!SetupDiGetDeviceInstanceIdW(deviceInfoSet, deviceInfoData, deviceInstanceId.data(), static_cast<DWORD>(deviceInstanceId.size() + 1), &buffersize)) {
|
|
||||||
deviceInstanceId.resize(buffersize - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(deviceInstanceId.find(intrepidUSB) != 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto serialOffset = deviceInstanceId.find_last_of('\\');
|
|
||||||
|
|
||||||
if(serialOffset == std::string::npos) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
++serialOffset; // move past '\'
|
|
||||||
|
|
||||||
if(deviceInstanceId.size() < serialOffset + SERIAL_SIZE) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::wstring wserial(deviceInstanceId.c_str() + serialOffset, SERIAL_SIZE);
|
|
||||||
|
|
||||||
FoundDevice device = {0};
|
|
||||||
|
|
||||||
if(WideCharToMultiByte(CP_ACP, 0, wserial.c_str(), (int)wserial.size(), device.serial, SERIAL_SIZE, NULL, NULL) == 0) {
|
|
||||||
EventManager::GetInstance().add(APIEvent::Type::SyscallError, APIEvent::Severity::Error);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::wstring wport;
|
|
||||||
while(!SetupDiGetCustomDevicePropertyW(deviceInfoSet, deviceInfoData, L"PortName", 0, &DataT, reinterpret_cast<PBYTE>(wport.data()), static_cast<DWORD>((wport.size() + 1) * WSTRING_ELEMENT_SIZE), &buffersize)) {
|
|
||||||
wport.resize((buffersize - 1) / WSTRING_ELEMENT_SIZE);
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::wstring path(L"\\\\.\\" + wport);
|
|
||||||
|
|
||||||
device.makeDriver = [path](device_eventhandler_t err, neodevice_t&) {
|
|
||||||
return std::make_unique<CDCACM>(err, path);
|
|
||||||
};
|
|
||||||
|
|
||||||
found.emplace_back(std::move(device));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,70 +0,0 @@
|
|||||||
#include "icsneo/platform/windows/internal/pcapdll.h"
|
|
||||||
#include <tchar.h>
|
|
||||||
|
|
||||||
using namespace icsneo;
|
|
||||||
|
|
||||||
const PCAPDLL& PCAPDLL::getInstance()
|
|
||||||
{
|
|
||||||
static PCAPDLL instance;
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
PCAPDLL::~PCAPDLL()
|
|
||||||
{
|
|
||||||
closeDLL();
|
|
||||||
}
|
|
||||||
|
|
||||||
void PCAPDLL::closeDLL()
|
|
||||||
{
|
|
||||||
if (dll)
|
|
||||||
FreeLibrary(dll);
|
|
||||||
dll = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool PCAPDLL::ok() const
|
|
||||||
{
|
|
||||||
return dll != nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
PCAPDLL::PCAPDLL()
|
|
||||||
{
|
|
||||||
TCHAR dir[512] = {0};
|
|
||||||
if(GetSystemDirectory(dir, 480)) {
|
|
||||||
_tcscat_s(dir, 512, TEXT("\\Npcap"));
|
|
||||||
if(SetDllDirectory(dir)) {
|
|
||||||
// will search for Npcap first, then fall back to WinPcap
|
|
||||||
dll = LoadLibrary(TEXT("wpcap.dll"));
|
|
||||||
SetDllDirectory(nullptr); // reset
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(dll == NULL) {
|
|
||||||
closeDLL();
|
|
||||||
} else {
|
|
||||||
findalldevs_ex = (PCAPFINDDEVICE)GetProcAddress(dll, "pcap_findalldevs_ex");
|
|
||||||
open = (PCAPOPEN)GetProcAddress(dll, "pcap_open");
|
|
||||||
freealldevs = (PCAPFREEDEVS)GetProcAddress(dll, "pcap_freealldevs");
|
|
||||||
close = (PCAPCLOSE)GetProcAddress(dll, "pcap_close");
|
|
||||||
stats = (PCAPSTATS)GetProcAddress(dll, "pcap_stats");
|
|
||||||
next_ex = (PCAPNEXTEX)GetProcAddress(dll, "pcap_next_ex");
|
|
||||||
sendpacket = (PCAPSENDPACKET)GetProcAddress(dll, "pcap_sendpacket");
|
|
||||||
sendqueue_alloc = (PCAPSENDQUEUEALLOC)GetProcAddress(dll, "pcap_sendqueue_alloc");
|
|
||||||
sendqueue_queue = (PCAPSENDQUEUEQUEUE)GetProcAddress(dll, "pcap_sendqueue_queue");
|
|
||||||
sendqueue_destroy = (PCAPSENDQUEUEDESTROY)GetProcAddress(dll, "pcap_sendqueue_destroy");
|
|
||||||
sendqueue_transmit = (PCAPSENDQUEUETRANSMIT)GetProcAddress(dll, "pcap_sendqueue_transmit");
|
|
||||||
datalink = (PCAPDATALINK)GetProcAddress(dll, "pcap_datalink");
|
|
||||||
createsrcstr = (PCAPCREATESRCSTR)GetProcAddress(dll, "pcap_createsrcstr");
|
|
||||||
setbuff = (PCAPSETBUFF)GetProcAddress(dll, "pcap_setbuff");
|
|
||||||
if(findalldevs_ex == NULL || open == NULL ||
|
|
||||||
freealldevs == NULL || close == NULL ||
|
|
||||||
stats == NULL || next_ex == NULL ||
|
|
||||||
sendpacket == NULL ||
|
|
||||||
sendqueue_alloc == NULL ||
|
|
||||||
sendqueue_queue == NULL || sendqueue_destroy == NULL ||
|
|
||||||
sendqueue_transmit == NULL ||
|
|
||||||
datalink == NULL ||
|
|
||||||
createsrcstr == NULL || setbuff == NULL) {
|
|
||||||
closeDLL();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,358 +0,0 @@
|
|||||||
#define WIN32_LEAN_AND_MEAN
|
|
||||||
#define NOMINMAX
|
|
||||||
#include <windows.h>
|
|
||||||
#include <winsock2.h>
|
|
||||||
|
|
||||||
#include "icsneo/platform/windows/pcap.h"
|
|
||||||
#include "icsneo/platform/windows/strings.h"
|
|
||||||
#include "icsneo/communication/network.h"
|
|
||||||
#include "icsneo/communication/communication.h"
|
|
||||||
#include "icsneo/communication/ethernetpacketizer.h"
|
|
||||||
#include "icsneo/communication/packetizer.h"
|
|
||||||
#include "icsneo/communication/decoder.h"
|
|
||||||
#include "icsneo/core/ringbuffer.h"
|
|
||||||
#include <pcap.h>
|
|
||||||
#include <iphlpapi.h>
|
|
||||||
#pragma comment(lib, "IPHLPAPI.lib")
|
|
||||||
#include <chrono>
|
|
||||||
#include <iostream>
|
|
||||||
#include <locale>
|
|
||||||
|
|
||||||
using namespace icsneo;
|
|
||||||
|
|
||||||
|
|
||||||
std::vector<PCAP::NetworkInterface> PCAP::knownInterfaces;
|
|
||||||
|
|
||||||
void PCAP::Find(std::vector<FoundDevice>& found) {
|
|
||||||
const PCAPDLL& pcap = PCAPDLL::getInstance();
|
|
||||||
if(!pcap.ok()) {
|
|
||||||
EventManager::GetInstance().add(APIEvent::Type::PCAPCouldNotStart, APIEvent::Severity::Error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// First we ask WinPCAP to give us all of the devices
|
|
||||||
pcap_if_t* alldevs;
|
|
||||||
char errbuf[PCAP_ERRBUF_SIZE] = { 0 };
|
|
||||||
bool success = false;
|
|
||||||
// Calling pcap.findalldevs_ex too quickly can cause various errors. Retry a few times in this case.
|
|
||||||
for(auto retry = 0; retry < 10; retry++) {
|
|
||||||
auto ret = pcap.findalldevs_ex((char*)PCAP_SRC_IF_STRING, nullptr, &alldevs, errbuf);
|
|
||||||
if(ret == 0) {
|
|
||||||
success = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!success) {
|
|
||||||
EventManager::GetInstance().add(APIEvent::Type::PCAPCouldNotFindDevices, APIEvent::Severity::Error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<NetworkInterface> interfaces;
|
|
||||||
for(pcap_if_t* dev = alldevs; dev != nullptr; dev = dev->next) {
|
|
||||||
NetworkInterface netif;
|
|
||||||
netif.nameFromWinPCAP = dev->name;
|
|
||||||
netif.descriptionFromWinPCAP = dev->description;
|
|
||||||
interfaces.push_back(netif);
|
|
||||||
}
|
|
||||||
|
|
||||||
pcap.freealldevs(alldevs);
|
|
||||||
|
|
||||||
// Now we're going to ask Win32 for the information as well
|
|
||||||
ULONG size = 0;
|
|
||||||
if(GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, nullptr, nullptr, &size) != ERROR_BUFFER_OVERFLOW) {
|
|
||||||
EventManager::GetInstance().add(APIEvent::Type::PCAPCouldNotFindDevices, APIEvent::Severity::Error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
std::vector<uint8_t> adapterAddressBuffer;
|
|
||||||
adapterAddressBuffer.resize(size);
|
|
||||||
if(GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, nullptr, (IP_ADAPTER_ADDRESSES*)adapterAddressBuffer.data(), &size) != ERROR_SUCCESS) {
|
|
||||||
EventManager::GetInstance().add(APIEvent::Type::PCAPCouldNotFindDevices, APIEvent::Severity::Error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// aa->AdapterName constains a unique name of the interface like "{3B1D2791-435A-456F-8A7B-9CB0EEE5DAB3}"
|
|
||||||
// iface.nameFromWinPCAP has "rpcap://\Device\NPF_{3B1D2791-435A-456F-8A7B-9CB0EEE5DAB3}"
|
|
||||||
// We're comparing strings to match the Win32 info with the WinPCAP info
|
|
||||||
for(IP_ADAPTER_ADDRESSES* aa = (IP_ADAPTER_ADDRESSES*)adapterAddressBuffer.data(); aa != nullptr; aa = aa->Next) {
|
|
||||||
for(auto& iface : interfaces) {
|
|
||||||
if(iface.nameFromWinPCAP.find(aa->AdapterName) == std::string::npos)
|
|
||||||
continue; // This is not the interface that corresponds
|
|
||||||
|
|
||||||
memcpy(iface.macAddress, aa->PhysicalAddress, sizeof(iface.macAddress));
|
|
||||||
iface.nameFromWin32API = aa->AdapterName;
|
|
||||||
iface.descriptionFromWin32API = convertWideString(aa->Description);
|
|
||||||
iface.friendlyNameFromWin32API = convertWideString(aa->FriendlyName);
|
|
||||||
if(iface.descriptionFromWin32API.find("LAN9512/LAN9514") != std::string::npos) {
|
|
||||||
// This is an Ethernet EVB device
|
|
||||||
iface.fullName = "Intrepid Ethernet EVB ( " + iface.friendlyNameFromWin32API + " : " + iface.descriptionFromWin32API + " )";
|
|
||||||
} else {
|
|
||||||
iface.fullName = iface.friendlyNameFromWin32API + " : " + iface.descriptionFromWin32API;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for(auto& iface : interfaces) {
|
|
||||||
bool exists = false;
|
|
||||||
for(auto& known : knownInterfaces)
|
|
||||||
if(memcmp(iface.macAddress, known.macAddress, sizeof(iface.macAddress)) == 0)
|
|
||||||
exists = true;
|
|
||||||
if(!exists)
|
|
||||||
knownInterfaces.emplace_back(iface);
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr auto openflags = (PCAP_OPENFLAG_MAX_RESPONSIVENESS | PCAP_OPENFLAG_NOCAPTURE_LOCAL);
|
|
||||||
for(size_t i = 0; i < knownInterfaces.size(); i++) {
|
|
||||||
auto& iface = knownInterfaces[i];
|
|
||||||
if(iface.fullName.length() == 0)
|
|
||||||
continue; // Win32 did not find this interface in the previous step
|
|
||||||
|
|
||||||
iface.fp = pcap.open(iface.nameFromWinPCAP.c_str(), 1518, openflags, 1, nullptr, errbuf);
|
|
||||||
|
|
||||||
if(iface.fp == nullptr)
|
|
||||||
continue; // Could not open the interface
|
|
||||||
|
|
||||||
EthernetPacketizer::EthernetPacket requestPacket;
|
|
||||||
memcpy(requestPacket.srcMAC, iface.macAddress, sizeof(requestPacket.srcMAC));
|
|
||||||
requestPacket.payload.reserve(4);
|
|
||||||
requestPacket.payload = {
|
|
||||||
((1 << 4) | (uint8_t)Network::NetID::Main51), // Packet size of 1 on NETID_MAIN51
|
|
||||||
(uint8_t)Command::RequestSerialNumber
|
|
||||||
};
|
|
||||||
requestPacket.payload.push_back(Packetizer::ICSChecksum(requestPacket.payload));
|
|
||||||
requestPacket.payload.insert(requestPacket.payload.begin(), 0xAA);
|
|
||||||
|
|
||||||
auto bs = requestPacket.getBytestream();
|
|
||||||
pcap.sendpacket(iface.fp, bs.data(), (int)bs.size());
|
|
||||||
|
|
||||||
auto timeout = std::chrono::high_resolution_clock::now() + std::chrono::milliseconds(250);
|
|
||||||
constexpr const size_t TempBufferSize = 4096;
|
|
||||||
RingBuffer tempBuffer(TempBufferSize);
|
|
||||||
while(std::chrono::high_resolution_clock::now() <= timeout) { // Wait up to 5ms for the response
|
|
||||||
struct pcap_pkthdr* header;
|
|
||||||
const uint8_t* data;
|
|
||||||
auto res = pcap.next_ex(iface.fp, &header, &data);
|
|
||||||
if(res < 0) {
|
|
||||||
//std::cout << "pcapnextex failed with " << res << std::endl;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if(res == 0)
|
|
||||||
continue; // Keep waiting for that packet
|
|
||||||
|
|
||||||
EthernetPacketizer ethPacketizer([](APIEvent::Type, APIEvent::Severity) {});
|
|
||||||
memcpy(ethPacketizer.hostMAC, iface.macAddress, sizeof(ethPacketizer.hostMAC));
|
|
||||||
ethPacketizer.allowInPacketsFromAnyMAC = true;
|
|
||||||
if(!ethPacketizer.inputUp({ data, data + header->caplen }))
|
|
||||||
continue; // This packet is not for us
|
|
||||||
|
|
||||||
Packetizer packetizer([](APIEvent::Type, APIEvent::Severity) {});
|
|
||||||
tempBuffer.write(ethPacketizer.outputUp());
|
|
||||||
if(!packetizer.input(tempBuffer))
|
|
||||||
continue; // This packet was not well formed
|
|
||||||
|
|
||||||
EthernetPacketizer::EthernetPacket decoded(data, header->caplen);
|
|
||||||
for(const auto& packet : packetizer.output()) {
|
|
||||||
Decoder decoder([](APIEvent::Type, APIEvent::Severity) {});
|
|
||||||
std::shared_ptr<Message> message;
|
|
||||||
if(!decoder.decode(message, packet))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
const auto serial = std::dynamic_pointer_cast<SerialNumberMessage>(message);
|
|
||||||
if(!serial || serial->deviceSerial.size() != 6)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
FoundDevice foundDevice;
|
|
||||||
foundDevice.handle = (neodevice_handle_t)((i << 24) | (decoded.srcMAC[3] << 16) | (decoded.srcMAC[4] << 8) | (decoded.srcMAC[5]));
|
|
||||||
foundDevice.productId = decoded.srcMAC[2];
|
|
||||||
memcpy(foundDevice.serial, serial->deviceSerial.c_str(), sizeof(foundDevice.serial) - 1);
|
|
||||||
foundDevice.serial[sizeof(foundDevice.serial) - 1] = '\0';
|
|
||||||
|
|
||||||
if(std::any_of(found.begin(), found.end(), [&](const auto& found) { return ::strncmp(foundDevice.serial, found.serial, sizeof(foundDevice.serial)) == 0; }))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
foundDevice.makeDriver = [](const device_eventhandler_t& reportFn, neodevice_t& device) {
|
|
||||||
return std::unique_ptr<Driver>(new PCAP(reportFn, device));
|
|
||||||
};
|
|
||||||
|
|
||||||
found.push_back(foundDevice);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pcap.close(iface.fp);
|
|
||||||
iface.fp = nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool PCAP::IsHandleValid(neodevice_handle_t handle) {
|
|
||||||
uint8_t netifIndex = (uint8_t)(handle >> 24);
|
|
||||||
return (netifIndex < knownInterfaces.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
PCAP::PCAP(const device_eventhandler_t& eventHandler, neodevice_t& forDevice) : Driver(eventHandler), pcap(PCAPDLL::getInstance()), device(forDevice), ethPacketizer(eventHandler) {
|
|
||||||
if(IsHandleValid(device.handle)) {
|
|
||||||
iface = knownInterfaces[(device.handle >> 24) & 0xFF];
|
|
||||||
iface.fp = nullptr; // We're going to open our own connection to the interface. This should already be nullptr but just in case.
|
|
||||||
|
|
||||||
deviceMAC[0] = 0x00;
|
|
||||||
deviceMAC[1] = 0xFC;
|
|
||||||
deviceMAC[2] = 0x70;
|
|
||||||
deviceMAC[3] = (device.handle >> 16) & 0xFF;
|
|
||||||
deviceMAC[4] = (device.handle >> 8) & 0xFF;
|
|
||||||
deviceMAC[5] = device.handle & 0xFF;
|
|
||||||
memcpy(ethPacketizer.deviceMAC, deviceMAC, 6);
|
|
||||||
memcpy(ethPacketizer.hostMAC, iface.macAddress, 6);
|
|
||||||
} else {
|
|
||||||
openable = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool PCAP::open() {
|
|
||||||
if(!openable) {
|
|
||||||
report(APIEvent::Type::InvalidNeoDevice, APIEvent::Severity::Error);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!pcap.ok()) {
|
|
||||||
report(APIEvent::Type::DriverFailedToOpen, APIEvent::Severity::Error);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(isOpen()) {
|
|
||||||
report(APIEvent::Type::DeviceCurrentlyOpen, APIEvent::Severity::Error);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Open the interface
|
|
||||||
iface.fp = pcap.open(iface.nameFromWinPCAP.c_str(), 65536, PCAP_OPENFLAG_MAX_RESPONSIVENESS | PCAP_OPENFLAG_NOCAPTURE_LOCAL, 50, nullptr, errbuf);
|
|
||||||
if(iface.fp == nullptr) {
|
|
||||||
report(APIEvent::Type::DriverFailedToOpen, APIEvent::Severity::Error);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create threads
|
|
||||||
readThread = std::thread(&PCAP::readTask, this);
|
|
||||||
writeThread = std::thread(&PCAP::writeTask, this);
|
|
||||||
transmitThread = std::thread(&PCAP::transmitTask, this);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool PCAP::isOpen() {
|
|
||||||
return iface.fp != nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool PCAP::close() {
|
|
||||||
if(!isOpen()) {
|
|
||||||
report(APIEvent::Type::DeviceCurrentlyClosed, APIEvent::Severity::Error);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
setIsClosing(true); // Signal the threads that we are closing
|
|
||||||
readThread.join();
|
|
||||||
writeThread.join();
|
|
||||||
transmitThread.join();
|
|
||||||
setIsClosing(false);
|
|
||||||
|
|
||||||
pcap.close(iface.fp);
|
|
||||||
iface.fp = nullptr;
|
|
||||||
|
|
||||||
clearBuffers();
|
|
||||||
|
|
||||||
transmitQueue = nullptr;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void PCAP::readTask() {
|
|
||||||
struct pcap_pkthdr* header;
|
|
||||||
const uint8_t* data;
|
|
||||||
EventManager::GetInstance().downgradeErrorsOnCurrentThread();
|
|
||||||
while(!isClosing()) {
|
|
||||||
auto readBytes = pcap.next_ex(iface.fp, &header, &data);
|
|
||||||
if(readBytes < 0) {
|
|
||||||
report(APIEvent::Type::FailedToRead, APIEvent::Severity::Error);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if(readBytes == 0)
|
|
||||||
continue; // Keep waiting for that packet
|
|
||||||
|
|
||||||
if(ethPacketizer.inputUp({data, data + header->caplen})) {
|
|
||||||
const auto bytes = ethPacketizer.outputUp();
|
|
||||||
pushRx(bytes.data(), bytes.size());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void PCAP::writeTask() {
|
|
||||||
WriteOperation writeOp;
|
|
||||||
EventManager::GetInstance().downgradeErrorsOnCurrentThread();
|
|
||||||
|
|
||||||
pcap_send_queue* queue1 = pcap.sendqueue_alloc(128000);
|
|
||||||
pcap_send_queue* queue2 = pcap.sendqueue_alloc(128000);
|
|
||||||
pcap_send_queue* queue = queue1;
|
|
||||||
|
|
||||||
while(!isClosing()) {
|
|
||||||
// Potentially, we added frames to a second queue faster than the other thread was able to hand the first
|
|
||||||
// off to the kernel. In that case, wait for a minimal amount of time before checking whether we can
|
|
||||||
// transmit it again.
|
|
||||||
if(writeQueue.wait_dequeue_timed(writeOp, std::chrono::milliseconds(queue->len ? 1 : 100))) {
|
|
||||||
unsigned int i = 0;
|
|
||||||
do {
|
|
||||||
ethPacketizer.inputDown(std::move(writeOp.bytes));
|
|
||||||
if(i++ >= (queue->maxlen - queue->len) / 1518 / 3)
|
|
||||||
break; // Not safe to try to fit any more packets in this queue, let it transmit and come around again
|
|
||||||
} while(writeQueue.try_dequeue(writeOp));
|
|
||||||
|
|
||||||
for(const auto& data : ethPacketizer.outputDown()) {
|
|
||||||
pcap_pkthdr header = {};
|
|
||||||
header.caplen = header.len = bpf_u_int32(data.size());
|
|
||||||
if(pcap.sendqueue_queue(queue, &header, data.data()) != 0)
|
|
||||||
report(APIEvent::Type::FailedToWrite, APIEvent::Severity::EventWarning);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::unique_lock<std::mutex> lk(transmitQueueMutex);
|
|
||||||
// Check if we want to transmit our current queue
|
|
||||||
// If we're not currently transmitting a queue, let this one transmit immediately for good latency
|
|
||||||
// If our queue is full and we're transmitting the other, we can't accept any more packets out of the writeQueue
|
|
||||||
// In that case we're putting as many packets into the driver as possible, so wait for it to be free
|
|
||||||
// This puts the backpressure on the writeQueue
|
|
||||||
if(queue->len && (!transmitQueue || queue->len + (1518*2) >= queue->maxlen)) {
|
|
||||||
if(transmitQueue) // Need to wait for the queue to become available
|
|
||||||
transmitQueueCV.wait(lk, [this] { return !transmitQueue; });
|
|
||||||
|
|
||||||
// Time to swap
|
|
||||||
transmitQueue = queue;
|
|
||||||
lk.unlock();
|
|
||||||
transmitQueueCV.notify_one();
|
|
||||||
|
|
||||||
// Set up our next queue
|
|
||||||
if(queue == queue1) {
|
|
||||||
pcap.sendqueue_destroy(queue2);
|
|
||||||
queue = queue2 = pcap.sendqueue_alloc(128000);
|
|
||||||
} else {
|
|
||||||
pcap.sendqueue_destroy(queue1);
|
|
||||||
queue = queue1 = pcap.sendqueue_alloc(128000);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pcap.sendqueue_destroy(queue1);
|
|
||||||
pcap.sendqueue_destroy(queue2);
|
|
||||||
}
|
|
||||||
|
|
||||||
void PCAP::transmitTask() {
|
|
||||||
while(!isClosing()) {
|
|
||||||
std::unique_lock<std::mutex> lk(transmitQueueMutex);
|
|
||||||
if(transmitQueueCV.wait_for(lk, std::chrono::milliseconds(100), [this] { return !!transmitQueue; }) && !isClosing() && transmitQueue) {
|
|
||||||
pcap_send_queue* current = transmitQueue;
|
|
||||||
lk.unlock();
|
|
||||||
pcap.sendqueue_transmit(iface.fp, current, 0);
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lk2(transmitQueueMutex);
|
|
||||||
transmitQueue = nullptr;
|
|
||||||
}
|
|
||||||
transmitQueueCV.notify_one();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,122 +0,0 @@
|
|||||||
#include "icsneo/platform/windows/registry.h"
|
|
||||||
#include "icsneo/platform/windows/strings.h"
|
|
||||||
|
|
||||||
#define WIN32_LEAN_AND_MEAN
|
|
||||||
#define NOMINMAX
|
|
||||||
#include <windows.h>
|
|
||||||
#include <codecvt>
|
|
||||||
#include <vector>
|
|
||||||
#include <locale>
|
|
||||||
|
|
||||||
using namespace icsneo;
|
|
||||||
|
|
||||||
class Key {
|
|
||||||
public:
|
|
||||||
Key(std::wstring path, bool readwrite = false);
|
|
||||||
~Key();
|
|
||||||
HKEY GetKey() { return key; }
|
|
||||||
bool IsOpen() { return key != nullptr; }
|
|
||||||
private:
|
|
||||||
HKEY key;
|
|
||||||
};
|
|
||||||
|
|
||||||
Key::Key(std::wstring path, bool readwrite) {
|
|
||||||
DWORD dwDisposition;
|
|
||||||
if(readwrite)
|
|
||||||
RegCreateKeyExW(HKEY_LOCAL_MACHINE, path.c_str(), 0, nullptr, 0, KEY_QUERY_VALUE | KEY_WRITE, nullptr, &key, &dwDisposition);
|
|
||||||
else
|
|
||||||
RegOpenKeyExW(HKEY_LOCAL_MACHINE, path.c_str(), 0, KEY_READ, &key);
|
|
||||||
}
|
|
||||||
|
|
||||||
Key::~Key() {
|
|
||||||
if(IsOpen())
|
|
||||||
RegCloseKey(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Registry::EnumerateSubkeys(std::wstring path, std::vector<std::wstring>& subkeys) {
|
|
||||||
Key regKey(path);
|
|
||||||
if(!regKey.IsOpen())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
wchar_t className[MAX_PATH];
|
|
||||||
memset(className, 0, sizeof(className));
|
|
||||||
DWORD classNameLen = MAX_PATH;
|
|
||||||
DWORD subKeyCount = 0;
|
|
||||||
DWORD maxSubKeyLen, maxClassStringLen, valueCount, maxValueNameLen, maxValueDataLen, securityDescriptorLen;
|
|
||||||
FILETIME lastWriteTime;
|
|
||||||
auto ret = RegQueryInfoKeyW(
|
|
||||||
regKey.GetKey(),
|
|
||||||
className,
|
|
||||||
&classNameLen,
|
|
||||||
nullptr,
|
|
||||||
&subKeyCount,
|
|
||||||
&maxSubKeyLen,
|
|
||||||
&maxClassStringLen,
|
|
||||||
&valueCount,
|
|
||||||
&maxValueNameLen,
|
|
||||||
&maxValueDataLen,
|
|
||||||
&securityDescriptorLen,
|
|
||||||
&lastWriteTime);
|
|
||||||
|
|
||||||
if(ret != ERROR_SUCCESS)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
subkeys.clear();
|
|
||||||
for(DWORD i = 0; i < subKeyCount; i++) {
|
|
||||||
DWORD nameLen = MAX_PATH;
|
|
||||||
wchar_t name[MAX_PATH];
|
|
||||||
memset(name, 0, sizeof(name));
|
|
||||||
ret = RegEnumKeyExW(regKey.GetKey(), i, name, &nameLen, nullptr, nullptr, nullptr, &lastWriteTime);
|
|
||||||
if(ret == ERROR_SUCCESS)
|
|
||||||
subkeys.push_back(name);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Registry::Get(std::wstring path, std::wstring key, std::wstring& value) {
|
|
||||||
Key regKey(path);
|
|
||||||
if(!regKey.IsOpen())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// Query for the type and size of the data
|
|
||||||
DWORD type, size;
|
|
||||||
auto ret = RegQueryValueExW(regKey.GetKey(), key.c_str(), nullptr, &type, (LPBYTE)nullptr, &size);
|
|
||||||
if(ret != ERROR_SUCCESS)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// Query for the data itself
|
|
||||||
std::vector<wchar_t> data(size / 2 + 1);
|
|
||||||
DWORD bytesRead = size; // We want to read up to the size we got earlier
|
|
||||||
ret = RegQueryValueExW(regKey.GetKey(), key.c_str(), nullptr, &type, (LPBYTE)data.data(), &bytesRead);
|
|
||||||
if(ret != ERROR_SUCCESS)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
value = data.data();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Registry::Get(std::string path, std::string key, std::string& value) {
|
|
||||||
std::wstring wvalue;
|
|
||||||
bool ret = Get(convertStringToWide(path), convertStringToWide(key), wvalue);
|
|
||||||
value = convertWideString(wvalue);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Registry::Get(std::wstring path, std::wstring key, uint32_t& value) {
|
|
||||||
Key regKey(path);
|
|
||||||
if(!regKey.IsOpen())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// Query for the data
|
|
||||||
DWORD type, size = sizeof(DWORD), kvalue;
|
|
||||||
auto ret = RegQueryValueExW(regKey.GetKey(), key.c_str(), nullptr, &type, (LPBYTE)&kvalue, &size);
|
|
||||||
if(ret != ERROR_SUCCESS || type != REG_DWORD)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
value = kvalue;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Registry::Get(std::string path, std::string key, uint32_t& value) {
|
|
||||||
return Get(convertStringToWide(path), convertStringToWide(key), value);
|
|
||||||
}
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
#include <string>
|
|
||||||
|
|
||||||
#define WIN32_LEAN_AND_MEAN
|
|
||||||
#define NOMINMAX
|
|
||||||
#include <Windows.h>
|
|
||||||
|
|
||||||
#include <icsneo/platform/windows/strings.h>
|
|
||||||
|
|
||||||
// Helper function to convert UTF-16 to UTF-8 strings (wide to standard)
|
|
||||||
std::string icsneo::convertWideString(const std::wstring& value) {
|
|
||||||
// Get the width of the string (character count)
|
|
||||||
int width = WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)value.c_str(), -1, 0, 0, NULL, NULL);
|
|
||||||
// Create the new string
|
|
||||||
std::string new_string(width+1, '\0');
|
|
||||||
// fill the new string with the converted characters
|
|
||||||
WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)value.c_str(), -1, new_string.data(), width, NULL, NULL);
|
|
||||||
return new_string;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Helper function to convert UTF-8 to UTF-16 strings (standard to wide)
|
|
||||||
std::wstring icsneo::convertStringToWide(const std::string& value) {
|
|
||||||
// Get the width of the string (character count)
|
|
||||||
int width = MultiByteToWideChar(CP_UTF8, 0, value.c_str(), -1, NULL, 0);
|
|
||||||
// Create the new string
|
|
||||||
std::wstring new_string(width+1, '\0');
|
|
||||||
// fill the new string with the converted characters
|
|
||||||
MultiByteToWideChar(CP_UTF8, 0, value.c_str(), -1, (LPWSTR)new_string.data(), width);
|
|
||||||
return new_string;
|
|
||||||
}
|
|
||||||
+4
-1
@@ -51,4 +51,7 @@ CMAKE_MSVC_RUNTIME_LIBRARY = "MultiThreaded"
|
|||||||
|
|
||||||
[tool.cibuildwheel]
|
[tool.cibuildwheel]
|
||||||
skip = "pp*"
|
skip = "pp*"
|
||||||
environment-pass = ["ICS_IPA_CA_CRT", "LIBICSNEO_ICSPB_REPO", "SKBUILD_CMAKE_DEFINE"]
|
environment-pass = ["ICS_IPA_CA_CRT", "LIBICSNEO_ICSPB_REPO", "SKBUILD_CMAKE_DEFINE", "LIBICSNEO_LIBICSBL_ZIP_URL", "LIBICSNEO_LIBICSBL_ZIP_URL_PREFIX", "LIBICSNEO_LIBICSBL_VERSION"]
|
||||||
|
|
||||||
|
[tool.cibuildwheel.macos]
|
||||||
|
repair-wheel-command = "DYLD_LIBRARY_PATH=$REPAIR_LIBRARY_PATH delocate-wheel --require-archs {delocate_archs} -w {dest_dir} -v {wheel}"
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user