From 0b5e5e425fba77db956932d8cd29c1e94324bf9b Mon Sep 17 00:00:00 2001 From: Jean-Michel Friedt Date: Tue, 24 Dec 2024 11:05:33 +0100 Subject: [PATCH] add message passing to kill flowgraph --- examples/m17_loopback.grc | 31 ++++++++++++++++++++----- examples/m17_rx.grc | 3 ++- examples/m17_tx.grc | 28 +++++++++++++++++++---- grc/m17_m17_coder.block.yml | 3 +++ lib/m17_coder_impl.cc | 45 ++++++++++++++++++++++++++----------- lib/m17_coder_impl.h | 2 ++ 6 files changed, 88 insertions(+), 24 deletions(-) diff --git a/examples/m17_loopback.grc b/examples/m17_loopback.grc index 92cff70..7a6b16f 100644 --- a/examples/m17_loopback.grc +++ b/examples/m17_loopback.grc @@ -136,7 +136,7 @@ blocks: bus_sink: false bus_source: false bus_structure: null - coordinate: [8, 412.0] + coordinate: [8, 452.0] rotation: 0 state: enabled - name: samp_rate @@ -235,7 +235,7 @@ blocks: bus_sink: false bus_source: false bus_structure: null - coordinate: [968, 184.0] + coordinate: [1056, 184.0] rotation: 0 state: enabled - name: blocks_null_sink_0 @@ -273,7 +273,7 @@ blocks: bus_sink: false bus_source: false bus_structure: null - coordinate: [568, 192.0] + coordinate: [656, 192.0] rotation: 0 state: true - name: blocks_vector_source_x_0_2 @@ -294,9 +294,27 @@ blocks: bus_sink: false bus_source: false bus_structure: null - coordinate: [16, 184.0] + coordinate: [8, 168.0] rotation: 0 state: true +- name: end_of_transmission + id: variable_qtgui_msg_push_button + parameters: + comment: '' + gui_hint: '' + label: End of Transmission + msgName: end_of_transmission + relBackgroundColor: default + relFontColor: default + type: string + value: end_of_transmission + states: + bus_sink: false + bus_source: false + bus_structure: null + coordinate: [8, 272.0] + rotation: 0 + state: enabled - name: m17_m17_coder_0 id: m17_m17_coder parameters: @@ -324,7 +342,7 @@ blocks: bus_sink: false bus_source: false bus_structure: null - coordinate: [320, 96.0] + coordinate: [408, 96.0] rotation: 0 state: true - name: m17_m17_decoder_0 @@ -347,7 +365,7 @@ blocks: bus_sink: false bus_source: false bus_structure: null - coordinate: [784, 140.0] + coordinate: [872, 140.0] rotation: 0 state: true - name: note_0_2 @@ -367,6 +385,7 @@ blocks: connections: - [blocks_throttle2_0, '0', m17_m17_decoder_0, '0'] - [blocks_vector_source_x_0_2, '0', m17_m17_coder_0, '0'] +- [end_of_transmission, pressed, m17_m17_coder_0, end_of_transmission] - [m17_m17_coder_0, '0', blocks_throttle2_0, '0'] - [m17_m17_decoder_0, '0', blocks_file_sink_0, '0'] - [m17_m17_decoder_0, '0', blocks_null_sink_0, '0'] diff --git a/examples/m17_rx.grc b/examples/m17_rx.grc index 0d1ec5f..dcefcf1 100644 --- a/examples/m17_rx.grc +++ b/examples/m17_rx.grc @@ -117,13 +117,14 @@ blocks: key: '' maxoutbuf: '0' minoutbuf: '0' + seed: '' signed_str: 'False' threshold: '2.0' states: bus_sink: false bus_source: false bus_structure: null - coordinate: [392, 36.0] + coordinate: [392, 28.0] rotation: 0 state: enabled diff --git a/examples/m17_tx.grc b/examples/m17_tx.grc index c8e35a6..49e9558 100644 --- a/examples/m17_tx.grc +++ b/examples/m17_tx.grc @@ -126,7 +126,7 @@ blocks: bus_sink: false bus_source: false bus_structure: null - coordinate: [776, 184.0] + coordinate: [880, 200.0] rotation: 0 state: enabled - name: blocks_throttle2_0 @@ -147,7 +147,7 @@ blocks: bus_sink: false bus_source: false bus_structure: null - coordinate: [560, 192.0] + coordinate: [664, 208.0] rotation: 0 state: true - name: blocks_vector_source_x_0_2 @@ -171,6 +171,24 @@ blocks: coordinate: [8, 184.0] rotation: 0 state: true +- name: end_of_transmission + id: variable_qtgui_msg_push_button + parameters: + comment: '' + gui_hint: '' + label: End of Transmission + msgName: end_of_transmission + relBackgroundColor: default + relFontColor: default + type: string + value: end_of_transmission + states: + bus_sink: false + bus_source: false + bus_structure: null + coordinate: [8, 288.0] + rotation: 0 + state: enabled - name: m17_m17_coder_0 id: m17_m17_coder parameters: @@ -182,12 +200,13 @@ blocks: dst_id: dst_str encr_subtype: '0' encr_type: '0' - key: '' + key: '''\x00\x00\x65\x41\xb0\x93\xff\x00''' maxoutbuf: '0' meta: meta_str minoutbuf: '0' mode: '1' priv_key: '' + seed: '' signed_str: 'False' src_id: src_str type: type_val @@ -195,7 +214,7 @@ blocks: bus_sink: false bus_source: false bus_structure: null - coordinate: [312, 108.0] + coordinate: [416, 112.0] rotation: 0 state: true - name: note_0_2 @@ -215,6 +234,7 @@ blocks: connections: - [blocks_throttle2_0, '0', blocks_file_sink_0, '0'] - [blocks_vector_source_x_0_2, '0', m17_m17_coder_0, '0'] +- [end_of_transmission, pressed, m17_m17_coder_0, end_of_transmission] - [m17_m17_coder_0, '0', blocks_throttle2_0, '0'] metadata: diff --git a/grc/m17_m17_coder.block.yml b/grc/m17_m17_coder.block.yml index f164506..59ec5d4 100644 --- a/grc/m17_m17_coder.block.yml +++ b/grc/m17_m17_coder.block.yml @@ -103,6 +103,9 @@ inputs: dtype: byte vlen: 1 optional: 0 +- label: end_of_transmission + domain: message + optional: 0 outputs: - label: out diff --git a/lib/m17_coder_impl.cc b/lib/m17_coder_impl.cc index 43785ed..a53e5de 100644 --- a/lib/m17_coder_impl.cc +++ b/lib/m17_coder_impl.cc @@ -102,6 +102,14 @@ namespace gr for (uint8_t i = 4; i < 4 + 10; i++) _iv[i] = 0; //10 random bytes TODO: replace with a rand() or pass through an additional arg } + message_port_register_in(pmt::mp("end_of_transmission")); + set_msg_handler(pmt::mp("end_of_transmission"), [this](const pmt::pmt_t& msg) { end_of_transmission(msg); }); + } + + void m17_coder_impl::end_of_transmission(const pmt::pmt_t& msg) + {_finished=true; + std::cout << "***** End of Transmission ********\n"; + pmt::print(msg); } void m17_coder_impl::set_encr_type (int encr_type) @@ -538,15 +546,8 @@ namespace gr uint8_t data[16], next_data[16]; //raw payload, packed bits - while (countout < (uint32_t) noutput_items) + while ((countout < (uint32_t) noutput_items) && (countin + 16 <= noutput_items)) { - if (countin + 16 <= noutput_items) - { - for (int i = 0; i < 16; i++) - { - data[i] = in[countin]; - countin++; - } if (!_got_lsf) //stream frames { //send LSF syncword @@ -614,8 +615,8 @@ namespace gr memset (next_data, 0, sizeof (next_data)); memcpy (data, next_data, sizeof (data)); - if (_fn == 60) - _finished = true; +// if (_fn == 60) +// _finished = true; //debug sig with random payloads (don't play the audio) for (uint8_t i = 0; i < 16; i++) @@ -631,6 +632,14 @@ namespace gr dummy=fread(&(lsf.meta), 14, 1, stdin); dummy=fread(data, 16, 1, stdin); */ + if (countin + 16 <= noutput_items) + { + for (int i = 0; i < 16; i++) + { + data[i] = in[countin]; + countin++; + } + } //AES encryption enabled - use 112 bits of IV if (_encr_type == ENCR_AES) @@ -645,7 +654,7 @@ namespace gr _lsf.crc[1] = ccrc & 0xFF; } - while (_finished == false) +// while (_finished == false) { if (!_got_lsf) { //debug @@ -734,6 +743,14 @@ namespace gr if(fread(&(next_lsf.meta), 14, 1, stdin)<1) finished=1; if(fread(next_data, 16, 1, stdin)<1) _finished=true; */ + if (countin + 16 <= noutput_items) + { + for (int i = 0; i < 16; i++) + { + next_data[i] = in[countin]; + countin++; + } + } } //AES @@ -793,7 +810,7 @@ namespace gr memcpy (data, next_data, 16); } else //send last frame(s) - { + { printf("Sending last frame\n"); send_syncword (out, &countout, SYNC_STR); extract_LICH (lich, _lich_cnt, &_lsf); encode_LICH (lich_encoded, lich); @@ -858,14 +875,16 @@ namespace gr //fprintf(stderr, "Stream has ended. Exiting.\n"); } // finished == true } // finished == false - } // in enough data left } // loop on input data // Tell runtime system how many input items we consumed on // each input stream. consume_each (countin); // printf(" noutput_items=%d countin=%d countout=%d\n",noutput_items,countin,countout); // Tell runtime system how many output items we produced. + if (_finished==false) return countout; + else {printf("Killing flowgraph\n"); return -1;} // https://lists.gnu.org/archive/html/discuss-gnuradio/2016-12/msg00206.html + // returning -1 (which is the magical value for "there's nothing coming anymore, you can shut down") would normally end a flow graph } } /* namespace m17 */ diff --git a/lib/m17_coder_impl.h b/lib/m17_coder_impl.h index 14b6d4a..f567f9a 100644 --- a/lib/m17_coder_impl.h +++ b/lib/m17_coder_impl.h @@ -91,6 +91,8 @@ namespace gr void set_can (int can); void set_debug (bool debug); void set_signed (bool signed_str); + void end_of_transmission(const pmt::pmt_t& msg); + m17_coder_impl (std::string src_id, std::string dst_id, int mode, int data, int encr_type, int encr_subtype, int can, std::string meta, std::string key, std::string priv_key,