Skip to content

Commit 4197c34

Browse files
committed
Add support for the ubuntu platform.
1 parent 1412e1d commit 4197c34

File tree

3 files changed

+228
-0
lines changed

3 files changed

+228
-0
lines changed

plugin.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,10 @@
2424
<source-file src="src/android/fr/drangies/cordova/serial/UsbBroadcastReceiver.java" target-dir="src/fr/drangies/cordova/serial" />
2525
<source-file src="lib/usbseriallibrary.jar" target-dir="libs" />
2626
</platform>
27+
28+
<!-- ubuntu -->
29+
<platform name="ubuntu">
30+
<header-file src="src/ubuntu/serial.h" />
31+
<source-file src="src/ubuntu/serial.cpp" />
32+
</platform>
2733
</plugin>

src/ubuntu/serial.cpp

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
* Copyright 2015 Loci Controls Inc.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
* THE SOFTWARE.
21+
*
22+
* Author: Ian Martin <ian@locicontrols.com>
23+
*/
24+
25+
#include <cstdio>
26+
#include <QtCore>
27+
28+
#include "serial.h"
29+
30+
#define error_desc(desc) "'" desc "'"
31+
32+
static QString byteArrayToJavaScript(QByteArray data) {
33+
int n;
34+
char buf[5];
35+
QString qs = "\"";
36+
37+
for (n = 0; n < data.size(); n++) {
38+
sprintf(buf, "\\x%02x", data[n] & 0xff);
39+
qs += buf;
40+
}
41+
42+
return qs + "\"";
43+
}
44+
45+
void Serial::onDataAvailable(void) {
46+
if (port == NULL) return;
47+
QByteArray data = port->readAll();
48+
if (_scId != 0) callbackWithoutRemove(_scId, byteArrayToJavaScript(data));
49+
}
50+
51+
Serial::Serial(Cordova *cordova) : CPlugin(cordova) {
52+
_scId = 0;
53+
_ecId = 0;
54+
}
55+
56+
void Serial::requestPermission(int scId, int ecId, const QVariantMap& arg) {
57+
callback(scId, "{}");
58+
}
59+
60+
void Serial::openSerial(int scId, int ecId, const QVariantMap& arg) {
61+
bool result;
62+
63+
QVariantMap opts = arg["opts"].toMap();
64+
65+
port = new QSerialPort(opts.value("device", "/dev/ttyUSB0").toString());
66+
if (port == NULL) {
67+
callback(ecId, error_desc("QSerialPort constructor failed"));
68+
return;
69+
}
70+
71+
port->setBaudRate((QSerialPort::BaudRate)opts.value("baudRate", 9600).toInt());
72+
port->setParity(opts.contains("parity")? (QSerialPort::Parity)opts["parity"].toInt() : QSerialPort::NoParity);
73+
port->setFlowControl(QSerialPort::NoFlowControl);
74+
port->setDataBits((QSerialPort::DataBits)opts.value("dataBits", 8).toInt());
75+
port->setStopBits(opts.contains("stopBits")? (QSerialPort::StopBits)opts["stopBits"].toInt() : QSerialPort::OneStop);
76+
77+
readWaitMillis = opts.value("readWaitMillis", 200).toInt();
78+
79+
if (!connect(port, SIGNAL(readyRead()), this, SLOT(onDataAvailable()))) {
80+
callback(ecId, error_desc("connect() failed"));
81+
return;
82+
}
83+
84+
if (!port->open(QIODevice::ReadWrite)) {
85+
callback(ecId, error_desc("QSerialPort::open() failed"));
86+
return;
87+
}
88+
89+
callback(scId, "{}");
90+
}
91+
92+
void Serial::writeSerial(int scId, int ecId, const QVariantMap& arg) {
93+
if (port->isOpen()) {
94+
QByteArray data = arg["data"].toString().toLatin1();
95+
96+
qint64 written = 0;
97+
while (written < data.size()) {
98+
qint64 result = port->write(data.mid(written));
99+
if (result < 0) {
100+
callback(ecId, error_desc("Error writing data"));
101+
return;
102+
} else {
103+
written += result;
104+
}
105+
}
106+
107+
callback(scId, "{}");
108+
} else {
109+
callback(ecId, error_desc("Port not open"));
110+
}
111+
}
112+
113+
static QByteArray hex2bin(QString hexString) {
114+
QByteArray result = "";
115+
116+
while(hexString.size() >= 2) {
117+
result.append(QChar(hexString.left(2).toInt(NULL, 16)));
118+
hexString.remove(0, 2);
119+
}
120+
121+
return result;
122+
}
123+
124+
void Serial::writeSerialHex(int scId, int ecId, const QVariantMap& arg) {
125+
QVariantMap obj = arg;
126+
obj["data"] = hex2bin(arg["data"].toString());
127+
writeSerial(scId, ecId, obj);
128+
}
129+
130+
void Serial::readSerial(int scId, int ecId) {
131+
if (port->isOpen()) {
132+
if ( (port->bytesAvailable() > 0) || port->waitForReadyRead(readWaitMillis) ) {
133+
callback(scId, byteArrayToJavaScript(port->readAll()));
134+
} else {
135+
callback(ecId, error_desc("No data available"));
136+
}
137+
} else {
138+
callback(ecId, error_desc("Port not open"));
139+
}
140+
}
141+
142+
void Serial::closeSerial(int scId, int ecId) {
143+
if (port->isOpen()) {
144+
callback(scId, "{}");
145+
} else {
146+
callback(ecId, error_desc("Port not open"));
147+
}
148+
}
149+
150+
void Serial::registerReadCallback(int scId, int ecId) {
151+
_scId = scId;
152+
_ecId = ecId;
153+
}

src/ubuntu/serial.h

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2015 Loci Controls Inc.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
* THE SOFTWARE.
21+
*
22+
* Author: Ian Martin <ian@locicontrols.com>
23+
*/
24+
25+
#ifndef SERIAL_H
26+
#define SERIAL_H
27+
28+
#include <QSerialPort>
29+
#include <cplugin.h>
30+
31+
class Serial: public CPlugin {
32+
Q_OBJECT
33+
public:
34+
explicit Serial(Cordova *cordova);
35+
36+
virtual const QString fullName() override {
37+
return Serial::fullID();
38+
}
39+
40+
virtual const QString shortName() override {
41+
return "Serial";
42+
}
43+
44+
static const QString fullID() {
45+
return "fr.drangies.cordova.serial";
46+
}
47+
48+
public slots:
49+
void requestPermission(int scId, int ecId, const QVariantMap& arg);
50+
void openSerial(int scId, int ecId, const QVariantMap& arg);
51+
void writeSerial(int scId, int ecId, const QVariantMap& arg);
52+
void writeSerialHex(int scId, int ecId, const QVariantMap& arg);
53+
void readSerial(int scId, int ecId);
54+
void closeSerial(int scId, int ecId);
55+
void registerReadCallback(int scId, int ecId);
56+
57+
private slots:
58+
void onDataAvailable(void);
59+
60+
private:
61+
QSerialPort* port;
62+
63+
int readWaitMillis;
64+
65+
int _scId;
66+
int _ecId;
67+
};
68+
69+
#endif

0 commit comments

Comments
 (0)