summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/hmtrp/hmtrp.cxx
blob: ca71c3197191d5577651ee7d8ecd0249542b6e0d (plain)
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*
 * Author: Jon Trulson <jtrulson@ics.com>
 * Copyright (c) 2015 Intel Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <iostream>
#include <string>
#include <stdexcept>

#include "hmtrp.h"

using namespace upm;
using namespace std;

static const int defaultDelay = 100;     // max wait time for read

// protocol start code
const uint8_t HMTRP_START1 = 0xaa;
const uint8_t HMTRP_START2 = 0xfa;

HMTRP::HMTRP(int uart)
{
  m_ttyFd = -1;

  if ( !(m_uart = mraa_uart_init(uart)) )
    {
      throw std::invalid_argument(std::string(__FUNCTION__) +
                                  ": mraa_uart_init() failed");
      return;
    }

  // This requires a recent MRAA (1/2015)
  const char *devPath = mraa_uart_get_dev_path(m_uart);

  if (!devPath)
    {
      throw std::runtime_error(std::string(__FUNCTION__) +
                               ": mraa_uart_get_dev_path() failed");
      return;
    }

  // now open the tty
  if ( (m_ttyFd = open(devPath, O_RDWR)) == -1)
    {
      throw std::runtime_error(std::string(__FUNCTION__) +
                               ": open of " + 
                               string(devPath) + " failed: " +
                               string(strerror(errno)));
      return;
    }
}

HMTRP::~HMTRP()
{
  if (m_ttyFd != -1)
    close(m_ttyFd);
}

bool HMTRP::dataAvailable(unsigned int millis)
{
  if (m_ttyFd == -1)
    return false;

  struct timeval timeout;

  timeout.tv_sec = 0;
  timeout.tv_usec = millis * 1000;

  int nfds;  
  fd_set readfds;

  FD_ZERO(&readfds);

  FD_SET(m_ttyFd, &readfds);
  
  if (select(m_ttyFd + 1, &readfds, NULL, NULL, &timeout) > 0)
    return true;                // data is ready
  else
    return false;
}

int HMTRP::readData(char *buffer, int len, int millis)
{
  if (m_ttyFd == -1)
    return(-1);

  // if specified, wait to see if input shows up, otherwise block
  if (millis >= 0)
    {
      if (!dataAvailable(millis))
        return 0;               // timed out
    }

  int rv = read(m_ttyFd, buffer, len);

  if (rv < 0)
    {
      throw std::runtime_error(std::string(__FUNCTION__) +
                               ": read() failed: " +
                               string(strerror(errno)));
      return rv;
    }

  return rv;
}

int HMTRP::writeData(char *buffer, int len)
{
  if (m_ttyFd == -1)
    return(-1);

  int rv = write(m_ttyFd, buffer, len);

  if (rv < 0)
    {
      throw std::runtime_error(std::string(__FUNCTION__) +
                               ": write() failed: " +
                               string(strerror(errno)));
      return rv;
    }

  tcdrain(m_ttyFd);

  return rv;
}

bool HMTRP::setupTty(speed_t baud)
{
  if (m_ttyFd == -1)
    return(false);
  
  struct termios termio;

  // get current modes
  tcgetattr(m_ttyFd, &termio);

  // setup for a 'raw' mode.  81N, no echo or special character
  // handling, such as flow control.
  cfmakeraw(&termio);

  // set our baud rates
  cfsetispeed(&termio, baud);
  cfsetospeed(&termio, baud);

  // make it so
  if (tcsetattr(m_ttyFd, TCSAFLUSH, &termio) < 0)
    {
      throw std::runtime_error(std::string(__FUNCTION__) +
                               ": tcsetattr() failed: " +
                               string(strerror(errno)));
      return false;
    }

  return true;
}

bool HMTRP::checkOK()
{
  char buf[4];

  int rv = readData(buf, 4, defaultDelay);
  
  if (rv != 4)
    {
      cerr << __FUNCTION__ << ": failed to receive OK response, rv = " 
           << rv << ", expected 4" << endl;
      return false;
    }

  // looking for "OK\r\n"
  if (buf[0] == 'O' && buf[1] == 'K' &&
      buf[2] == '\r' && buf[3] == '\n')
    return true;
  else
    return false;
}

bool HMTRP::reset()
{
  char pkt[3];

  pkt[0] = HMTRP_START1;
  pkt[1] = HMTRP_START2;
  pkt[2] = RESET;

  writeData(pkt, 3);

  return checkOK();
}

bool HMTRP::getConfig(uint32_t *freq, uint32_t *dataRate, 
                      uint16_t *rxBandwidth, uint8_t *modulation, 
                      uint8_t *txPower, uint32_t *uartBaud)
{
  char pkt[3];
  pkt[0] = HMTRP_START1;
  pkt[1] = HMTRP_START2;
  pkt[2] = GET_CONFIG;

  writeData(pkt, 3);
  usleep(100000);

  // now read back a 16 byte response
  char buf[16];
  int rv = readData(buf, 16, defaultDelay);

  if (rv != 16)
    {
      cerr << __FUNCTION__ << ": failed to receive correct response: rv = " 
           << rv << ", expected 16" << endl;
      return false;
    }

  // now decode
  if (freq)
    {
      *freq = ( ((buf[0] & 0xff) << 24) |
                ((buf[1] & 0xff) << 16) |
                ((buf[2] & 0xff) << 8)  |
                 (buf[3] & 0xff) );
    }

  if (dataRate)
    {
      *dataRate = ( ((buf[4] & 0xff) << 24) |
                    ((buf[5] & 0xff) << 16) |
                    ((buf[6] & 0xff) << 8)  |
                     (buf[7] & 0xff) );
    }

  if (rxBandwidth)
    {
      *rxBandwidth = ( ((buf[8] & 0xff) << 8) |
                        (buf[9] & 0xff) );
    }

  if (modulation)
    {
      *modulation = buf[10] & 0xff;
    }

  if (txPower)
    {
      *txPower = buf[11] & 0xff;
    }

  if (uartBaud)
    {
      *uartBaud = ( ((buf[12] & 0xff) << 24) |
                    ((buf[13] & 0xff) << 16) |
                    ((buf[14] & 0xff) << 8)  |
                     (buf[15] & 0xff) );
    } 

  return true;
}

bool HMTRP::setFrequency(uint32_t freq)
{
  char pkt[7];

  pkt[0] = HMTRP_START1;
  pkt[1] = HMTRP_START2;
  pkt[2] = SET_FREQUENCY;

  pkt[3] = ( ((freq & 0xff000000) >> 24) & 0xff );
  pkt[4] = ( ((freq & 0x00ff0000) >> 16) & 0xff );
  pkt[5] = ( ((freq & 0x0000ff00) >> 8)  & 0xff );
  pkt[6] = ( (freq & 0x000000ff) & 0xff );

  writeData(pkt, 7);

  return checkOK();
}

bool HMTRP::setRFDataRate(uint32_t rate)
{
  //  Valid values are between 1200-115200

  if (rate < 1200 || rate > 115200)
    {
      throw std::out_of_range(std::string(__FUNCTION__) +
                              ": Valid rate values are between 1200-115200");
      return false;
    }

  char pkt[7];

  pkt[0] = HMTRP_START1;
  pkt[1] = HMTRP_START2;
  pkt[2] = SET_RF_DATARATE;

  pkt[3] = ( ((rate & 0xff000000) >> 24) & 0xff );
  pkt[4] = ( ((rate & 0x00ff0000) >> 16) & 0xff );
  pkt[5] = ( ((rate & 0x0000ff00) >> 8)  & 0xff );
  pkt[6] = ( (rate & 0x000000ff) & 0xff );

  writeData(pkt, 7);

  return checkOK();
}

bool HMTRP::setRXBandwidth(uint16_t rxBand)
{
  //  Valid values are between 30-620 (in Khz)

  if (rxBand < 30 || rxBand > 620)
    {
      throw std::out_of_range(std::string(__FUNCTION__) +
                              ": Valid rxBand values are between 30-620");
      return false;
    }
        
  char pkt[5];

  pkt[0] = HMTRP_START1;
  pkt[1] = HMTRP_START2;
  pkt[2] = SET_RX_BW;

  pkt[3] = ( ((rxBand & 0xff00) >> 8) & 0xff );
  pkt[4] = ( rxBand & 0xff );

  writeData(pkt, 5);

  return checkOK();
}

bool HMTRP::setFrequencyModulation(uint8_t modulation)
{
  //  Valid values are between 10-160 (in Khz)

  if (modulation < 10 || modulation > 160)
    {
      throw std::out_of_range(std::string(__FUNCTION__) +
                              ": Valid modulation values are between 10-160");
      return false;
    }
        
  char pkt[4];

  pkt[0] = HMTRP_START1;
  pkt[1] = HMTRP_START2;
  pkt[2] = SET_FREQ_MODULATION;

  pkt[3] = modulation;

  writeData(pkt, 4);

  return checkOK();
}

bool HMTRP::setTransmitPower(uint8_t power)
{
  //  Valid values are between 0-7

  if (power > 7)
    {
      throw std::out_of_range(std::string(__FUNCTION__) +
                              ": Valid power values are between 0-7");
      return false;
    }
        
  char pkt[4];

  pkt[0] = HMTRP_START1;
  pkt[1] = HMTRP_START2;
  pkt[2] = SET_TX_POWER;

  pkt[3] = power;

  writeData(pkt, 4);

  return checkOK();
}

bool HMTRP::setUARTSpeed(uint32_t speed)
{
  //  Valid values are between 1200-115200

  if (speed < 1200 || speed > 115200)
    {
      throw std::out_of_range(std::string(__FUNCTION__) +
                              ": Valid speed values are between 1200-115200");
      return false;
    }

  char pkt[7];

  pkt[0] = HMTRP_START1;
  pkt[1] = HMTRP_START2;
  pkt[2] = SET_UART_SPEED;

  pkt[3] = ( ((speed & 0xff000000) >> 24) & 0xff );
  pkt[4] = ( ((speed & 0x00ff0000) >> 16) & 0xff );
  pkt[5] = ( ((speed & 0x0000ff00) >> 8)  & 0xff );
  pkt[6] = ( (speed & 0x000000ff) & 0xff );

  writeData(pkt, 7);

  return checkOK();
}


bool HMTRP::getRFSignalStrength(uint8_t *strength)
{
  if (!strength)
    return false;

  *strength = 0;

  char pkt[3];
  pkt[0] = HMTRP_START1;
  pkt[1] = HMTRP_START2;
  pkt[2] = GET_RF_SIGNAL_STR;

  writeData(pkt, 3);
  usleep(100000);

  // now read back a 1 byte response
  char buf;
  int rv = readData(&buf, 1, defaultDelay);

  if (rv != 1)
    {
      cerr << __FUNCTION__ << ": failed to receive correct response: rv = " 
           << rv << ", expected 1" << endl;
      return false;
    }

  // now decode
  *strength = (uint8_t)buf;

  return true;
}

uint8_t HMTRP::getRFSignalStrength()
{
  uint8_t strength = 0;
  if (!getRFSignalStrength(&strength))
    throw std::runtime_error(std::string(__PRETTY_FUNCTION__) +
                                ": readData() failed");
  return strength;
}

bool HMTRP::getModSignalStrength(uint8_t *strength)
{
  if (!strength)
    return false;

  *strength = 0;

  char pkt[3];
  pkt[0] = HMTRP_START1;
  pkt[1] = HMTRP_START2;
  pkt[2] = GET_MOD_SIGNAL_STR;

  writeData(pkt, 3);
  usleep(100000);

  // now read back a 1 byte response
  char buf;
  int rv = readData(&buf, 1, defaultDelay);

  if (rv != 1)
    {
      cerr << __FUNCTION__ << ": failed to receive correct response: rv = " 
           << rv << ", expected 1" << endl;
      return false;
    }

  // now decode
  *strength = (uint8_t)buf;

  return true;
}

uint8_t HMTRP::getModSignalStrength()
{
  uint8_t strength = 0;
  if (!getModSignalStrength(&strength))
    throw std::runtime_error(std::string(__PRETTY_FUNCTION__) +
                                ": readData() failed");
  return strength;
}