1/*jslint node:true, vars:true, bitwise:true, unparam:true */
2/*jshint unused:true */
3
4/*
5 * Author: Jon Trulson <jtrulson@ics.com>
6 * Copyright (c) 2015 Intel Corporation.
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
28
29var sensorObj = require('jsupm_grovegprs');
30
31
32/************** Functions **************/
33function printUsage(progname)
34{
35    var outputStr = "Usage: " + progname + " [AT command]\n\n" +
36	"If an argument is supplied on the command line, that argument is\n" +
37	"sent to the module and the response is printed out.\n\n" +
38	"If no argument is used, then the manufacturer and the current\n" +
39	"saved profiles are queried and the results printed out.\n\n"
40    console.log(outputStr);
41}
42
43// simple helper function to send a command and wait for a response
44function sendCommand(sensor, cmd, callback)
45{
46    // commands need to be terminated with a carriage return
47    cmd += "\r";
48    sensor.writeDataStr(cmd);
49
50    // wait up to 1 second
51    if (sensor.dataAvailable(1000))
52    {
53	console.log("Returned: " + sensor.readDataStr(1024));
54    }
55    else
56	console.log("Timed out waiting for response");
57
58    if (callback)
59	callback();
60}
61
62/************** Main code **************/
63// Instantiate a GROVEGPRS Module on UART 0
64var sensor = new sensorObj.GroveGPRS(0);
65
66// Set the baud rate, 19200 baud is the default.
67if (sensor.setBaudRate(19200))
68{
69    console.log("Failed to set baud rate");
70    process.exit(0);
71}
72
73printUsage(process.argv[1]);
74
75// Note: in nodeJS, command-line argument 0 is "node".
76// Command-line argument 1 is "grovegprs.js"
77// If you have a third argument, then it's a command
78if (process.argv.length > 2)
79{
80    console.log("Sending command line argument (" + process.argv[2] + ")...");
81    sendCommand(sensor, process.argv[2]);
82}
83else
84{
85    // sending this command as a synchronous callback ensures better timing
86    var callbackFunc = function()
87    {
88	setTimeout(function()
89        {
90            // query the saved profiles
91            console.log("Querying the saved profiles (AT&V)...");
92            sendCommand(sensor, "AT&V");
93
94            // A comprehensive list is available from the
95            // datasheet at:
96            // http://www.seeedstudio.com/wiki/images/7/72/AT_Commands_v1.11.pdf
97	}, 1000);
98    };
99
100    // query the module manufacturer
101    console.log("Querying module manufacturer (AT+CGMI)...");
102    sendCommand(sensor, "AT+CGMI", callbackFunc);
103}
104
105
106/************** Exit code **************/
107process.on('SIGINT', function()
108{
109    sensor = null;
110    sensorObj.cleanUp();
111    sensorObj = null;
112    console.log("Exiting...");
113    process.exit(0);
114});
115