File: attr_source_impl.cc

package info (click to toggle)
gr-iio 0.3-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,368 kB
  • sloc: cpp: 3,155; xml: 1,468; lex: 107; yacc: 104; python: 60; makefile: 11; ansic: 9
file content (287 lines) | stat: -rw-r--r-- 8,769 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* -*- c++ -*- */
/*
 * Copyright 2018 Analog Devices Inc.
 * Author: Travis Collins <travis.collins@analog.com>
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this software; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gnuradio/io_signature.h>
#include "attr_source_impl.h"
#include <boost/lexical_cast.hpp>
#include <boost/chrono.hpp>
#include <boost/thread/thread.hpp>
#include <vector>


namespace gr {
  namespace iio {

    attr_source::sptr
    attr_source::make(const std::string &uri, const std::string &device, const std::string &channel,
      const std::string &attribute, int update_interval_ms, int samples_per_update, int data_type,
      int attr_type, bool output, uint32_t address, bool required_enable)
    {
      return gnuradio::get_initial_sptr
        (new attr_source_impl(uri, device, channel, attribute, update_interval_ms, samples_per_update,
          data_type, attr_type, output, address, required_enable));
    }

    size_t attr_source_impl::type_sizeof(int data_type, int attr_type)
    {
      size_t dsize = 0;
      if (attr_type == 3)
        dsize = sizeof(int);
      else {
        switch (data_type) {
          case 0:
            dsize = sizeof(double);
            break;
          case 1:
            dsize = sizeof(float);
            break;
          case 2:
            dsize = sizeof(long long);
            break;
          case 3:
            dsize = sizeof(int);
            break;
          case 4:
            dsize = sizeof(uint8_t);
            break;
        }
      }
      return dsize;
    }

    /*
     * The private constructor
     */
    attr_source_impl::attr_source_impl(const std::string &uri, const std::string &device, const std::string &channel,
      const std::string &attribute, int update_interval_ms, int samples_per_update, int data_type, int attr_type,
      bool output, uint32_t address, bool required_enable)
      : gr::sync_block("attr_source",
              gr::io_signature::make(0, 0, 0),
              gr::io_signature::make(1, -1, type_sizeof(data_type, attr_type) )),
      device(device),
      channel(channel),
      uri(uri),
      attribute(attribute),
      update_interval_ms(update_interval_ms),
      samples_per_update(samples_per_update),
      attr_type(attr_type),
      data_type(data_type),
      output(output),
      m_attr_type(attr_type),
      address(address),
      required_enable(required_enable)
    {
      ctx = device_source_impl::get_context(uri);
      if (!ctx)
        throw std::runtime_error("Unable to create context");

      dev = iio_context_find_device(ctx, device.c_str());
      if (!dev) {
		    iio_context_destroy(ctx);
		    throw std::runtime_error("Device not found");
	    }
      // Channel only needed for channel attributes
      if (attr_type==0)
      {
        chan = iio_device_find_channel(dev, channel.c_str(), output);
        if (!chan) {
          iio_context_destroy(ctx);
          throw std::runtime_error("Channel not found");
        }
      }

      // Required for MathWorks generated IP
      if ((attr_type==3) && required_enable)
      {
        // int ret = iio_device_debug_attr_write(dev, "direct_reg_access", "enabled");
        int ret = iio_device_attr_write(dev, "reg_access", "enabled");

        if (ret < 0) {
          char error[1024];
          sprintf(error, "Failed to enabled register for device: %s [%d]", device.c_str(), ret);
          throw std::runtime_error(error);
        }
      }

      set_output_multiple(samples_per_update);

    }

    /*
     * Our virtual destructor.
     */
    attr_source_impl::~attr_source_impl()
    {
    }

    void
    attr_source_impl::check(int ret)
    {
      if (ret < 0)
        std::cerr << "Reading parameter failed: "<< ret << std::endl;
    }

    void
    attr_source_impl::get_register_data(uint32_t address, int* value)
    {
      uint32_t u32value;
      ret = iio_device_reg_read(dev, address, &u32value);
      attr_source_impl::check(ret);
      *value = boost::lexical_cast<int>(u32value);
    }

    void
    attr_source_impl::get_attribute_data(const std::string& attribute, double* value)
    {
      switch (attr_type) {
        case 0:
          ret = iio_channel_attr_read_double(chan, attribute.c_str(), value);
          break;
        case 1:
          ret = iio_device_attr_read_double(dev, attribute.c_str(), value);
          break;
        default:
          ret = iio_device_debug_attr_read_double(dev, attribute.c_str(), value);
          break;
      }
      attr_source_impl::check(ret);
    }

    void
    attr_source_impl::get_attribute_data(const std::string& attribute, float* value)
    {
      double dvalue;
      switch (attr_type) {
        case 0:
          ret = iio_channel_attr_read_double(chan, attribute.c_str(), &dvalue);
          break;
        case 1:
          ret = iio_device_attr_read_double(dev, attribute.c_str(), &dvalue);
          break;
        default:
          ret = iio_device_debug_attr_read_double(dev, attribute.c_str(), &dvalue);
          break;
      }
      attr_source_impl::check(ret);
      *value = boost::lexical_cast<float>(dvalue);
    }

    void
    attr_source_impl::get_attribute_data(const std::string& attribute, long long* value)
    {
      switch (attr_type) {
        case 0:
          ret = iio_channel_attr_read_longlong(chan, attribute.c_str(), value);
          break;
        case 1:
          ret = iio_device_attr_read_longlong(dev, attribute.c_str(), value);
          break;
        default:
          ret = iio_device_debug_attr_read_longlong(dev, attribute.c_str(), value);
          break;
      }
      attr_source_impl::check(ret);
    }

    void
    attr_source_impl::get_attribute_data(const std::string& attribute, int* value)
    {
      long long llvalue;
      switch (attr_type) {
        case 0:
          ret = iio_channel_attr_read_longlong(chan, attribute.c_str(), &llvalue);
          break;
        case 1:
          ret = iio_device_attr_read_longlong(dev, attribute.c_str(), &llvalue);
          break;
        default:
          ret = iio_device_debug_attr_read_longlong(dev, attribute.c_str(), &llvalue);
          break;
      }
      attr_source_impl::check(ret);
      *value = boost::lexical_cast<int>(llvalue);
    }

    void
    attr_source_impl::get_attribute_data(const std::string& attribute, uint8_t* value)
    {
      bool bvalue;
      switch (attr_type) {
        case 0:
          ret = iio_channel_attr_read_bool(chan, attribute.c_str(), &bvalue);
          break;
        case 1:
          ret = iio_device_attr_read_bool(dev, attribute.c_str(), &bvalue);
          break;
        default:
          ret = iio_device_debug_attr_read_bool(dev, attribute.c_str(), &bvalue);
          break;
      }
      attr_source_impl::check(ret);
      *value = boost::lexical_cast<uint8_t>(bvalue);
    }

    int
    attr_source_impl::work(int noutput_items,
        gr_vector_const_void_star &input_items,
        gr_vector_void_star &output_items)
    {
      int sample;
      void *out;

      out = output_items[0];

      for (sample = 0; sample<samples_per_update; sample++)
      {
        boost::this_thread::sleep_for(boost::chrono::milliseconds(update_interval_ms));
        if (attr_type == 3)
          get_register_data(address, ( ((int*) out)+sample) );
        else {
          switch (data_type) {
            case 0:
              get_attribute_data(attribute, ((double *)out+sample) );
              break;
            case 1:
              get_attribute_data(attribute, ((float *)out+sample) );
              break;
            case 2:
              get_attribute_data(attribute, ((long long *)out+sample) );
              break;
            case 3:
              get_attribute_data(attribute, ((int *)out+sample) );
              break;
            case 4:
              get_attribute_data(attribute, ((uint8_t *)out+sample) );
              break;
          }
        }
      }

      // Tell runtime system how many output items we produced.
      return samples_per_update;
    }

  } /* namespace iio */
} /* namespace gr */