make: allows to build in source tree by filtering subdirs
[contrib/upm.git] / src / sm130 / sm130.h
1 /*
2  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  * 
5  * Based on SM130 library developed by Marc Boon <http://www.marcboon.com>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  */
26 #pragma once
27
28 #include <string>
29 #include <mraa/aio.h>
30 #include <mraa/gpio.h>
31 #include <mraa/i2c.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34
35 #define SIZE_PAYLOAD 18                 // maximum payload size of I2C packet
36 #define SIZE_PACKET (SIZE_PAYLOAD + 2)  // total I2C packet size, including length uint8_t and checksum
37
38 #define HIGH                  1
39 #define LOW                   0
40
41 namespace upm {
42
43 /**
44  * @brief C++ API for SM130 RFID reader module
45  *
46  * This file defines the SM130 C++ interface for libsm130
47  */
48 class SM130 {
49     
50     uint8_t m_Data[SIZE_PACKET]; //!< packet data
51         char    m_Version[8];      //!< version string
52         uint8_t m_TagNumber[7];    //!< tag number as uint8_t array
53         uint8_t m_TagLength;       //!< length of tag number in uint8_ts (4 or 7)
54         char    m_TagString[15];   //!< tag number as hex string
55         uint8_t m_TagType;         //!< type of tag
56         char    errorCode;         //!< error code from some commands
57         uint8_t antennaPower;      //!< antenna power level
58         uint8_t m_LastCMD;         //!< last sent command
59     
60     public:
61         static const uint8_t MIFARE_ULTRALIGHT = 1;
62         static const uint8_t MIFARE_1K         = 2;
63         static const uint8_t MIFARE_4K         = 3;
64
65         static const uint8_t CMD_RESET         = 0x80;
66         static const uint8_t CMD_VERSION       = 0x81;
67         static const uint8_t CMD_SEEK_TAG      = 0x82;
68         static const uint8_t CMD_SELECT_TAG    = 0x83;
69         static const uint8_t CMD_AUTHENTICATE  = 0x85;
70         static const uint8_t CMD_READ16        = 0x86;
71         static const uint8_t CMD_READ_VALUE    = 0x87;
72         static const uint8_t CMD_WRITE16       = 0x89;
73         static const uint8_t CMD_WRITE_VALUE   = 0x8a;
74         static const uint8_t CMD_WRITE4        = 0x8b;
75         static const uint8_t CMD_WRITE_KEY     = 0x8c;
76         static const uint8_t CMD_INC_VALUE     = 0x8d;
77         static const uint8_t CMD_DEC_VALUE     = 0x8e;
78         static const uint8_t CMD_ANTENNA_POWER = 0x90;
79         static const uint8_t CMD_READ_PORT     = 0x91;
80         static const uint8_t CMD_WRITE_PORT    = 0x92;
81         static const uint8_t CMD_HALT_TAG      = 0x93;
82         static const uint8_t CMD_SET_BAUD      = 0x94;
83         static const uint8_t CMD_SLEEP         = 0x96;
84     
85          /**
86          * Instanciates a SM130 object
87          *
88          * @param di data pin
89          * @param dcki clock pin
90          */
91         SM130 (int bus, int devAddr, int rst, int dready);
92
93         /**
94          * SM130 object destructor
95          */
96         ~SM130 ();
97         
98         /**
99          * Get the firmware version string.
100          */
101         const char* getFirmwareVersion ();
102         
103         /**
104          *      Checks for availability of a valid response packet.
105          *
106          *      This function should always be called and return true prior to using results
107          *      of a command.
108          *
109          *      @returns        true if a valid response packet is available
110          */
111         uint8_t available ();
112         
113         /**
114          * Returns the packet length, excluding checksum
115          */
116         uint8_t getPacketLength () { return this->m_Data[0]; };
117         
118         /**
119          * Returns the last executed command
120          */
121         uint8_t getCommand () { return this->m_Data[1]; };
122
123         /**
124          * Return name of the component
125          */
126         std::string name()
127         {
128             return m_name;
129         }
130     private:
131         std::string m_name;
132         mraa_gpio_context m_resetPinCtx;
133         mraa_gpio_context m_dataReadyPinCtx;
134         
135         int m_i2cAddr;
136         int m_bus;
137         mraa_i2c_context m_i2Ctx;
138         
139         void arrayToHex (char *s, uint8_t array[], uint8_t len);
140         char toHex (uint8_t b);
141         
142         uint16_t i2cRecievePacket (uint32_t len);
143         mraa_result_t i2cTransmitPacket (uint32_t len);
144         mraa_result_t sendCommand (uint8_t cmd);
145 };
146
147 }