1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2015 Intel Corporation.
4  *
5  * Thanks to Adafruit for supplying a google translated version of the
6  * Chinese datasheet and some clues in their code.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  */
27 #pragma once
28 
29 #include <string>
30 #include <iostream>
31 
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <fcntl.h>
37 #include <errno.h>
38 #include <termios.h>
39 #include <sys/time.h>
40 #include <sys/select.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 
44 #include <mraa/uart.h>
45 
46 #define HM11_DEFAULT_UART 0
47 
48 namespace upm {
49     /**
50      * @brief HM-11 Bluetooth 4.0 Low Energy Module library
51      * @defgroup hm11 libupm-hm11
52      * @ingroup seeed uart wifi
53      */
54 
55     /**
56      * @library hm11
57      * @sensor hm11
58      * @comname HM-11 Bluetooth Low Energy
59      * @altname Grove BLE
60      * @altid HM-10, HM-12
61      * @type wifi
62      * @man seeed
63      * @con uart
64      * @web http://www.seeedstudio.com/wiki/images/c/cd/Bluetooth4_en.pdf
65      *
66      * @brief API for the HM-11 4.0 Bluetooth Low Energy Module
67      *
68      * The driver was tested with the Grove BLE module.  It's an HM-11
69      * BLE 4.0 module based on a TI CC2541 chip.  It operates using a
70      * standard 'AT' command set.  See the datasheet for a full list
71      * of available commands and their possible responses:
72      *
73      * http://www.seeedstudio.com/wiki/images/c/cd/Bluetooth4_en.pdf
74      *
75      * It is connected via a UART at 9,600 baud.
76      *
77      * @image html hm11.jpg
78      * @snippet hm11.cxx Interesting
79      */
80 
81   class HM11 {
82   public:
83 
84     /**
85      * HM11 object constructor
86      *
87      * @param uart Default UART to use (0 or 1)
88      */
89     HM11(int uart);
90 
91     /**
92      * HM11 object destructor
93      */
94     ~HM11();
95 
96     /**
97      * Checks to see if there is data available for reading
98      *
99      * @param millis Number of milliseconds to wait; 0 means no waiting
100      * @return True if there is data available for reading
101      */
102     bool dataAvailable(unsigned int millis);
103 
104     /**
105      * Reads any available data into a user-supplied buffer. Note: the
106      * call blocks until data is available for reading. Use
107      * dataAvailable() to determine whether there is data available
108      * beforehand, to avoid blocking.
109      *
110      * @param buffer Buffer to hold the data read
111      * @param len Length of the buffer
112      * @return Number of bytes read
113      */
114     int readData(char *buffer, int len);
115 
116     /**
117      * Writes the data in the buffer to the device
118      *
119      * @param buffer Buffer to hold the data read
120      * @param len Length of the buffer
121      * @return Number of bytes written
122      */
123     int writeData(char *buffer, int len);
124 
125     /**
126      * Sets up proper tty I/O modes and the baud rate. For this device, the default
127      * baud rate is 9,600 (B9600).
128      *
129      * @param baud Desired baud rate.
130      * @return True if successful
131      */
132     bool setupTty(speed_t baud=B9600);
133 
134 
135   protected:
ttyFd()136     int ttyFd() { return m_ttyFd; };
137 
138   private:
139     mraa_uart_context m_uart;
140     int m_ttyFd;
141   };
142 }
143 
144 
145