From 9b4a17c7574798bc0f51e1ae31dd037318180804 Mon Sep 17 00:00:00 2001 From: Ryou Ezoe Date: Wed, 2 Mar 2016 17:30:06 +0900 Subject: [PATCH 1/4] C++11 port --- Makefile | 21 +++++++++++++++ README.md | 26 ++++++++++++++++--- main.c | 78 ------------------------------------------------------- main.cpp | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 116 insertions(+), 82 deletions(-) create mode 100644 Makefile delete mode 100644 main.c create mode 100644 main.cpp diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6f343cc --- /dev/null +++ b/Makefile @@ -0,0 +1,21 @@ +all : gmain cmain + +clean : + rm -f gmain + rm -f cmain + +grun : gmain + ./gmain + +crun : cmain + ./cmain + + +gmain : main.cpp + g++ -Wall -O2 -std=c++11 -pthread -lrt -o gmain main.cpp + +cmain : main.cpp + clang++ -Wall -O2 -std=c++11 -stdlib=libc++ -pthread -lrt -o cmain main.cpp + + +.PHONY : all clean run diff --git a/README.md b/README.md index 4f4d5b6..19c3605 100644 --- a/README.md +++ b/README.md @@ -10,13 +10,31 @@ Publicly available documents already discuss exfiltration from secured systems u How to Use It ------------------ -Compile the problem using: +You need a decent C++11 implementation(GCC or Clang) +Compile the program by make: - gcc main.c -Wall -O2 -o main +If you have GCC: -And run it on an Apple MacBook Air (13-inch, Early 2015): + make gmain + +If you have Clang: + + make cmain + +If you have both: + + make + +And run it on your computer: + +For GCC: + + make grun + +For Clang: + + make crun - ./main Then use a Sony STR-K670P radio receiver with the included antenna and tune it to 1580 kHz on AM. diff --git a/main.c b/main.c deleted file mode 100644 index 5218224..0000000 --- a/main.c +++ /dev/null @@ -1,78 +0,0 @@ -// SYSTEM BUS RADIO -// https://github.com/fulldecent/system-bus-radio -// Copyright 2016 William Entriken - -#include -#include -#include -#include -#include - -__m128i reg; -__m128i reg_zero; -__m128i reg_one; -mach_port_t clock_port; -mach_timespec_t remain; - -static inline void square_am_signal(float time, float frequency) { - printf("Playing / %0.3f seconds / %4.0f Hz\n", time, frequency); - uint64_t period = NSEC_PER_SEC / frequency; - - uint64_t start = mach_absolute_time(); - uint64_t end = start + time * NSEC_PER_SEC; - - while (mach_absolute_time() < end) { - uint64_t mid = start + period / 2; - uint64_t reset = start + period; - while (mach_absolute_time() < mid) { - _mm_stream_si128(®, reg_one); - _mm_stream_si128(®, reg_zero); - } - clock_sleep_trap(clock_port, TIME_ABSOLUTE, reset / NSEC_PER_SEC, reset % NSEC_PER_SEC, &remain); - start = reset; - } -} - -int main() -{ - mach_timebase_info_data_t theTimeBaseInfo; - mach_timebase_info(&theTimeBaseInfo); - puts("TESTING TIME BASE: the following should be 1 / 1"); - printf(" Mach base: %u / %u nanoseconds\n\n", theTimeBaseInfo.numer, theTimeBaseInfo.denom); - - uint64_t start = mach_absolute_time(); - uint64_t end = mach_absolute_time(); - printf("TESTING TIME TO EXECUTE mach_absolute_time()\n Result: %lld nanoseconds\n\n", end - start); - - reg_zero = _mm_set_epi32(0, 0, 0, 0); - reg_one = _mm_set_epi32(-1, -1, -1, -1); - - while (1) { - square_am_signal(0.400, 2673); - square_am_signal(0.400, 2349); - square_am_signal(0.400, 2093); - square_am_signal(0.400, 2349); - square_am_signal(0.400, 2673); - square_am_signal(0.400, 2673); - square_am_signal(0.790, 2673); - square_am_signal(0.400, 2349); - square_am_signal(0.400, 2349); - square_am_signal(0.790, 2349); - square_am_signal(0.400, 2673); - square_am_signal(0.400, 3136); - square_am_signal(0.790, 3136); - square_am_signal(0.400, 2673); - square_am_signal(0.400, 2349); - square_am_signal(0.400, 2093); - square_am_signal(0.400, 2349); - square_am_signal(0.400, 2673); - square_am_signal(0.400, 2673); - square_am_signal(0.400, 2673); - square_am_signal(0.400, 2673); - square_am_signal(0.400, 2349); - square_am_signal(0.400, 2349); - square_am_signal(0.400, 2673); - square_am_signal(0.400, 2349); - square_am_signal(0.790, 2093); - } -} diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..e3770a4 --- /dev/null +++ b/main.cpp @@ -0,0 +1,73 @@ +// SYSTEM BUS RADIO +// https://github.com/fulldecent/system-bus-radio +// Copyright 2016 William Entriken +// C++11 port by Ryou Ezoe + +#include +#include +#include +#include +#include + +void square_am_signal(float time, float frequency) +{ + using namespace std::chrono ; + + std::cout << "Playing / " << time << " seconds / " << frequency << " Hz\n" ; + + seconds const sec{1} ; + nanoseconds const nsec{ sec } ; + using rep = nanoseconds::rep ; + auto nsec_per_sec = nsec.count() ; + + nanoseconds const period( static_cast( nsec_per_sec / frequency) ) ; + + auto start = high_resolution_clock::now() ; + auto const end = start + nanoseconds( static_cast(time * nsec_per_sec) ) ; + + while (high_resolution_clock::now() < end) + { + auto mid = start + period / 2 ; + auto reset = start + period ; + while (high_resolution_clock::now() < mid) + { + std::atomic x{0} ; + ++x ; + } + std::this_thread::sleep_until( reset ) ; + start = reset; + } +} + +int main() +{ + while (1) + { + square_am_signal(0.400, 2673); + square_am_signal(0.400, 2349); + square_am_signal(0.400, 2093); + square_am_signal(0.400, 2349); + square_am_signal(0.400, 2673); + square_am_signal(0.400, 2673); + square_am_signal(0.790, 2673); + square_am_signal(0.400, 2349); + square_am_signal(0.400, 2349); + square_am_signal(0.790, 2349); + square_am_signal(0.400, 2673); + square_am_signal(0.400, 3136); + square_am_signal(0.790, 3136); + square_am_signal(0.400, 2673); + square_am_signal(0.400, 2349); + square_am_signal(0.400, 2093); + square_am_signal(0.400, 2349); + square_am_signal(0.400, 2673); + square_am_signal(0.400, 2673); + square_am_signal(0.400, 2673); + square_am_signal(0.400, 2673); + square_am_signal(0.400, 2349); + square_am_signal(0.400, 2349); + square_am_signal(0.400, 2673); + square_am_signal(0.400, 2349); + square_am_signal(0.790, 2093); + } +} From 722414ef44744ffc26a816e97a4241253ac91966 Mon Sep 17 00:00:00 2001 From: Ryou Ezoe Date: Wed, 2 Mar 2016 18:45:01 +0900 Subject: [PATCH 2/4] boost song --- main.cpp | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/main.cpp b/main.cpp index e3770a4..a9ee03e 100644 --- a/main.cpp +++ b/main.cpp @@ -8,6 +8,33 @@ #include #include #include +#include +#include + +std::mutex m ; +std::condition_variable cv ; +std::chrono::high_resolution_clock::time_point mid ; +std::chrono::high_resolution_clock::time_point reset ; + + +void boost_song() +{ + using namespace std::chrono ; + + while( true ) + { + std::unique_lock lk{m} ; + cv.wait( lk ) ; + + std::atomic x{0} ; + while( high_resolution_clock::now() < mid ) + { + ++x ; + } + std::this_thread::sleep_until( reset ) ; + + } +} void square_am_signal(float time, float frequency) { @@ -27,13 +54,11 @@ void square_am_signal(float time, float frequency) while (high_resolution_clock::now() < end) { - auto mid = start + period / 2 ; + mid = start + period / 2 ; auto reset = start + period ; - while (high_resolution_clock::now() < mid) - { - std::atomic x{0} ; - ++x ; - } + std::atomic x{0} ; + + cv.notify_all() ; std::this_thread::sleep_until( reset ) ; start = reset; } @@ -41,6 +66,13 @@ void square_am_signal(float time, float frequency) int main() { + + for ( unsigned i = 0 ; i < 8 ; ++i ) + { + std::thread t( boost_song ) ; + t.detach() ; + } + while (1) { square_am_signal(0.400, 2673); From 76b1fcd26d23a9bf5ef5f3967565d09cd3febd27 Mon Sep 17 00:00:00 2001 From: Ryou Ezoe Date: Wed, 2 Mar 2016 18:55:16 +0900 Subject: [PATCH 3/4] cleanup --- main.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/main.cpp b/main.cpp index a9ee03e..6ebda58 100644 --- a/main.cpp +++ b/main.cpp @@ -32,7 +32,6 @@ void boost_song() ++x ; } std::this_thread::sleep_until( reset ) ; - } } @@ -55,8 +54,7 @@ void square_am_signal(float time, float frequency) while (high_resolution_clock::now() < end) { mid = start + period / 2 ; - auto reset = start + period ; - std::atomic x{0} ; + reset = start + period ; cv.notify_all() ; std::this_thread::sleep_until( reset ) ; From b68776cc2acda4aa979b7381ddfffd246a00e483 Mon Sep 17 00:00:00 2001 From: Ryou Ezoe Date: Wed, 2 Mar 2016 19:02:02 +0900 Subject: [PATCH 4/4] aquire number of thread to create from hardware_concurrency() --- main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index 6ebda58..7e425f2 100644 --- a/main.cpp +++ b/main.cpp @@ -65,7 +65,7 @@ void square_am_signal(float time, float frequency) int main() { - for ( unsigned i = 0 ; i < 8 ; ++i ) + for ( unsigned i = 0 ; i < std::thread::hardware_concurrency() ; ++i ) { std::thread t( boost_song ) ; t.detach() ;