Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_simple_framer.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <gr_simple_framer.h>
28 #include <gr_simple_framer_sync.h>
29 #include <gr_io_signature.h>
30 #include <assert.h>
31 #include <stdexcept>
32 #include <string.h>
33
34
35 gr_simple_framer_sptr
36 gr_make_simple_framer (int payload_bytesize)
37 {
38   return gr_simple_framer_sptr (new gr_simple_framer (payload_bytesize));
39 }
40
41 gr_simple_framer::gr_simple_framer (int payload_bytesize)
42   : gr_block ("simple_framer",
43               gr_make_io_signature (1, 1, sizeof (unsigned char)),
44               gr_make_io_signature (1, 1, sizeof (unsigned char))),
45     d_seqno (0), d_payload_bytesize (payload_bytesize),
46     d_input_block_size (payload_bytesize),
47     d_output_block_size (payload_bytesize + GRSF_OVERHEAD)
48 {
49   set_output_multiple (d_output_block_size);
50 }
51
52 void
53 gr_simple_framer::forecast (int noutput_items, gr_vector_int &ninput_items_required)
54 {
55   assert (noutput_items % d_output_block_size == 0);
56
57   int   nblocks = noutput_items / d_output_block_size;
58   int   input_required = nblocks * d_input_block_size;
59
60   unsigned ninputs = ninput_items_required.size();
61   for (unsigned int i = 0; i < ninputs; i++)
62     ninput_items_required[i] = input_required;
63 }
64
65 int
66 gr_simple_framer::general_work (int noutput_items,
67                                 gr_vector_int &ninput_items,
68                                 gr_vector_const_void_star &input_items,
69                                 gr_vector_void_star &output_items)
70 {
71   const unsigned char *in = (const unsigned char *) input_items[0];
72   unsigned char *out = (unsigned char *) output_items[0];
73
74   int n = 0;
75   int nblocks = 0;
76
77   memset (out, 0x55, noutput_items);
78
79   while (n < noutput_items){
80     out[0] = (GRSF_SYNC >> 56) & 0xff;
81     out[1] = (GRSF_SYNC >> 48) & 0xff;
82     out[2] = (GRSF_SYNC >> 40) & 0xff;
83     out[3] = (GRSF_SYNC >> 32) & 0xff;
84     out[4] = (GRSF_SYNC >> 24) & 0xff;
85     out[5] = (GRSF_SYNC >> 16) & 0xff;
86     out[6] = (GRSF_SYNC >>  8) & 0xff;
87     out[7] = (GRSF_SYNC >>  0) & 0xff;
88     out[8] = d_seqno++;
89
90     memcpy (&out[9], in, d_input_block_size);
91     in += d_input_block_size;
92     out += d_output_block_size;
93     n += d_output_block_size;
94     nblocks++;
95   }
96
97   assert (n == noutput_items);
98
99   consume_each (nblocks * d_input_block_size);
100   return n;
101 }