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 <stdlib.h>
33 #include <unistd.h>
34 #include <string.h>
35 
36 #include <mraa/common.hpp>
37 #include <mraa/uart.hpp>
38 
39 #define GROVEGPRS_DEFAULT_UART 0
40 
41 namespace upm {
42     /**
43      * @brief Grove GPRS Module library
44      * @defgroup grovegprs libupm-grovegprs
45      * @ingroup seeed uart wifi
46      */
47 
48     /**
49      * @library grovegprs
50      * @sensor grovegprs
51      * @comname Grove GPRS Module
52      * @type wifi
53      * @man seeed
54      * @con uart
55      * @web http://www.seeedstudio.com/wiki/GPRS_Shield_V2.0
56      *
57      * @brief API for the Grove GPRS Module
58      *
59      * The driver was tested with the Grove GPRS Module, V2.  It's a
60      * GSM GPRS module based on the SIM900.  This module uses a
61      * standard 'AT' command set.  See the datasheet for a full list
62      * of available commands and their possible responses:
63      *
64      * http://www.seeedstudio.com/wiki/images/7/72/AT_Commands_v1.11.pdf
65      *
66      * It is connected via a UART at 19200 baud.
67      *
68      * @image html grovegprs.jpg
69      * @snippet grovegprs.cxx Interesting
70      */
71 
72   class GroveGPRS {
73   public:
74 
75     /**
76      * GroveGPRS object constructor
77      *
78      * @param uart Default UART to use (0 or 1). Default is 0.
79      */
80     GroveGPRS(int uart=GROVEGPRS_DEFAULT_UART);
81 
82     /**
83      * GroveGPRS object destructor
84      */
85     ~GroveGPRS();
86 
87     /**
88      * Checks to see if there is data available for reading
89      *
90      * @param millis Number of milliseconds to wait; 0 means no waiting
91      * @return true if there is data available for reading
92      */
93     bool dataAvailable(unsigned int millis);
94 
95     /**
96      * Reads any available data into a user-supplied buffer. Note: the
97      * call blocks until data is available for reading. Use
98      * dataAvailable() to determine whether there is data available
99      * beforehand, to avoid blocking.
100      *
101      * @param buffer Buffer to hold the data read
102      * @param len Length of the buffer
103      * @return Number of bytes read
104      */
105     int readData(char *buffer, unsigned int len);
106 
107     /**
108      * Reads any available data and returns it in a std::string. Note:
109      * the call blocks until data is available for reading. Use
110      * dataAvailable() to determine whether there is data available
111      * beforehand, to avoid blocking.
112      *
113      * @param len Maximum length of the data to be returned
114      * @return Number of bytes read
115      */
116     std::string readDataStr(int len);
117 
118     /**
119      * Writes the data in the buffer to the device.  If you are
120      * writing a command, be sure to terminate it with a carriage
121      * return (\r)
122      *
123      * @param buffer Buffer to hold the data to write
124      * @param len Length of the buffer
125      * @return Number of bytes written
126      */
127     int writeData(char *buffer, unsigned len);
128 
129     /**
130      * Writes the std:string data to the device.  If you are writing a
131      * command, be sure to terminate it with a carriage return (\r)
132      *
133      * @param data Buffer to write to the device
134      * @return Number of bytes written
135      */
136     int writeDataStr(std::string data);
137 
138     /**
139      * Sets the baud rate for the device.  The default is 19200.
140      *
141      * @param baud Desired baud rate.
142      * @return true if successful
143      */
144     mraa::Result setBaudRate(int baud=19200);
145 
146 
147   protected:
148     mraa::Uart m_uart;
149 
150   private:
151   };
152 }
153 
154 
155