upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
sm130.hpp
1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2015 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #pragma once
25 
26 #include <string>
27 #include <iostream>
28 
29 #include <stdlib.h>
30 #include <stdint.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <sys/time.h>
34 #include <sys/select.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 
38 #include <mraa/common.hpp>
39 #include <mraa/uart.hpp>
40 #include <mraa/gpio.hpp>
41 
42 #define SM130_DEFAULT_UART 0
43 #define SM130_DEFAULT_RESET_PIN 13
44 
45 namespace upm {
46 
79  class SM130 {
80 
81  public:
82 
83  // Valid commands
84  typedef enum {
85  CMD_RESET = 0x80,
86  CMD_VERSION = 0x81,
87  CMD_SEEK_TAG = 0x82,
88  CMD_SELECT_TAG = 0x83,
89  CMD_AUTHENTICATE = 0x85,
90  CMD_READ16 = 0x86,
91  CMD_READ_VALUE = 0x87,
92  CMD_WRITE16 = 0x89,
93  CMD_WRITE_VALUE = 0x8a,
94  CMD_WRITE4 = 0x8b,
95  CMD_WRITE_KEY = 0x8c,
96  CMD_INC_VALUE = 0x8d,
97  CMD_DEC_VALUE = 0x8e,
98  CMD_ANTENNA_POWER = 0x90,
99  CMD_READ_PORT = 0x91,
100  CMD_WRITE_PORT = 0x92,
101  CMD_HALT_TAG = 0x93,
102  CMD_SET_BAUD = 0x94,
103  CMD_SLEEP = 0x96
104  } CMD_T;
105 
106  // valid tag types.
107  typedef enum {
108  TAG_NONE = 0x00, // error/invalid
109 
110  TAG_MIFARE_ULTRALIGHT = 0x01,
111  TAG_MIFARE_1K = 0x02,
112  TAG_MIFARE_4K = 0x03,
113  TAG_UNKNOWN = 0xff
114  } TAG_TYPE_T;
115 
116  // Valid authentication keys
117  typedef enum {
118  KEY_TYPE_EEPROM_A0 = 0x10,
119  KEY_TYPE_EEPROM_A1 = 0x11,
120  KEY_TYPE_EEPROM_A2 = 0x12,
121  KEY_TYPE_EEPROM_A3 = 0x13,
122  KEY_TYPE_EEPROM_A4 = 0x14,
123  KEY_TYPE_EEPROM_A5 = 0x15,
124  KEY_TYPE_EEPROM_A6 = 0x16,
125  KEY_TYPE_EEPROM_A7 = 0x17,
126  KEY_TYPE_EEPROM_A8 = 0x18,
127  KEY_TYPE_EEPROM_A9 = 0x19,
128  KEY_TYPE_EEPROM_A10 = 0x1a,
129  KEY_TYPE_EEPROM_A11 = 0x1b,
130  KEY_TYPE_EEPROM_A12 = 0x1c,
131  KEY_TYPE_EEPROM_A13 = 0x1d,
132  KEY_TYPE_EEPROM_A14 = 0x1e,
133  KEY_TYPE_EEPROM_A15 = 0x1f,
134 
135  KEY_TYPE_EEPROM_B0 = 0x20,
136  KEY_TYPE_EEPROM_B1 = 0x21,
137  KEY_TYPE_EEPROM_B2 = 0x22,
138  KEY_TYPE_EEPROM_B3 = 0x23,
139  KEY_TYPE_EEPROM_B4 = 0x24,
140  KEY_TYPE_EEPROM_B5 = 0x25,
141  KEY_TYPE_EEPROM_B6 = 0x26,
142  KEY_TYPE_EEPROM_B7 = 0x27,
143  KEY_TYPE_EEPROM_B8 = 0x28,
144  KEY_TYPE_EEPROM_B9 = 0x29,
145  KEY_TYPE_EEPROM_B10 = 0x2a,
146  KEY_TYPE_EEPROM_B11 = 0x2b,
147  KEY_TYPE_EEPROM_B12 = 0x2c,
148  KEY_TYPE_EEPROM_B13 = 0x2d,
149  KEY_TYPE_EEPROM_B14 = 0x2e,
150  KEY_TYPE_EEPROM_B15 = 0x2f,
151 
152  KEY_TYPE_A = 0xaa,
153  KEY_TYPE_B = 0xbb,
154 
155  KEY_TYPE_A_AND_TRANSPORT_F = 0xff
156  } KEY_TYPES_T;
157 
164  SM130 (int uart=SM130_DEFAULT_UART, int reset=SM130_DEFAULT_RESET_PIN);
165 
169  ~SM130 ();
170 
177  mraa::Result setBaudRate(int baud=19200);
178 
184  std::string getFirmwareVersion();
185 
191  bool reset();
192 
198  void hardwareReset();
199 
207  bool select();
208 
218  bool waitForTag(uint32_t timeout);
219 
230  bool authenticate(uint8_t block, KEY_TYPES_T keyType, std::string key="");
231 
239  std::string readBlock16(uint8_t block);
240 
248  int32_t readValueBlock(uint8_t block);
249 
258  bool writeBlock16(uint8_t block, std::string contents);
259 
269  bool writeValueBlock(uint8_t block, int32_t value);
270 
280  bool writeBlock4(uint8_t block, std::string contents);
281 
296  bool writeKey(uint8_t eepromSector, KEY_TYPES_T keyType, std::string key);
297 
307  int32_t adjustValueBlock(uint8_t block, int32_t value, bool incr);
308 
317  bool setAntennaPower(bool on);
318 
325  uint8_t readPorts();
326 
334  bool writePorts(uint8_t val);
335 
342  bool haltTag();
343 
365  bool setSM130BaudRate(int baud);
366 
374  bool sleep();
375 
383  char getLastErrorCode() { return m_lastErrorCode; };
384 
393  std::string getLastErrorString() { return m_lastErrorString; };
394 
401  int getUIDLen() { return m_uidLen; };
402 
409  std::string getUID() { return m_uid; };
410 
417  TAG_TYPE_T getTagType() { return m_tagType; };
418 
425  std::string tag2String(TAG_TYPE_T tag);
426 
437  std::string string2HexString(std::string input);
438 
439  protected:
440  mraa::Uart m_uart;
441  mraa::Gpio m_gpioReset;
442 
443  std::string sendCommand(CMD_T cmd, std::string data);
444  void initClock();
445  uint32_t getMillis();
446 
447  private:
448  int m_uidLen;
449  std::string m_uid;
450 
451  char m_lastErrorCode;
452  std::string m_lastErrorString;
453 
454  TAG_TYPE_T m_tagType;
455 
456  int m_baud;
457 
458  struct timeval m_startTime;
459 
460  void clearError()
461  {
462  m_lastErrorCode = 0;
463  m_lastErrorString.clear();
464  }
465  };
466 
467 }
bool reset()
Definition: sm130.cxx:181
bool writeValueBlock(uint8_t block, int32_t value)
Definition: sm130.cxx:445
bool writeKey(uint8_t eepromSector, KEY_TYPES_T keyType, std::string key)
Definition: sm130.cxx:538
int getUIDLen()
Definition: sm130.hpp:401
std::string readBlock16(uint8_t block)
Definition: sm130.cxx:309
int32_t readValueBlock(uint8_t block)
Definition: sm130.cxx:349
~SM130()
Definition: sm130.cxx:52
bool authenticate(uint8_t block, KEY_TYPES_T keyType, std::string key="")
Definition: sm130.cxx:250
bool select()
Definition: sm130.cxx:202
bool setAntennaPower(bool on)
Definition: sm130.cxx:644
C++ API wrapper for the bh1749 driver.
Definition: a110x.hpp:29
TAG_TYPE_T getTagType()
Definition: sm130.hpp:417
bool writePorts(uint8_t val)
Definition: sm130.cxx:675
void hardwareReset()
Definition: sm130.cxx:195
std::string getLastErrorString()
Definition: sm130.hpp:393
bool haltTag()
Definition: sm130.cxx:696
char getLastErrorCode()
Definition: sm130.hpp:383
uint8_t readPorts()
Definition: sm130.cxx:659
mraa::Result setBaudRate(int baud=19200)
Definition: sm130.cxx:56
std::string tag2String(TAG_TYPE_T tag)
Definition: sm130.cxx:865
bool writeBlock4(uint8_t block, std::string contents)
Definition: sm130.cxx:488
SM130(int uart=SM130_DEFAULT_UART, int reset=SM130_DEFAULT_RESET_PIN)
Definition: sm130.cxx:39
std::string getUID()
Definition: sm130.hpp:409
bool setSM130BaudRate(int baud)
Definition: sm130.cxx:728
std::string string2HexString(std::string input)
Definition: sm130.cxx:788
std::string getFirmwareVersion()
Definition: sm130.cxx:164
API for the SM130 RFID Reader Module.
Definition: sm130.hpp:79
bool sleep()
Definition: sm130.cxx:773
bool writeBlock16(uint8_t block, std::string contents)
Definition: sm130.cxx:395
bool waitForTag(uint32_t timeout)
Definition: sm130.cxx:839
int32_t adjustValueBlock(uint8_t block, int32_t value, bool incr)
Definition: sm130.cxx:594