node-mavlink 2.1.0 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -4,4 +4,6 @@ export * from './lib/utils';
4
4
  export * from './lib/logger';
5
5
  export * from './lib/mavlink';
6
6
  export * from './lib/mavesp';
7
+ export * from './lib/mavtcp';
8
+ export * from './lib/debug';
7
9
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA;AAChC,cAAc,qBAAqB,CAAA;AACnC,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,eAAe,CAAA;AAC7B,cAAc,cAAc,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA;AAChC,cAAc,qBAAqB,CAAA;AACnC,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,eAAe,CAAA;AAC7B,cAAc,cAAc,CAAA;AAC5B,cAAc,cAAc,CAAA;AAC5B,cAAc,aAAa,CAAA"}
package/dist/index.js CHANGED
@@ -20,3 +20,5 @@ __exportStar(require("./lib/utils"), exports);
20
20
  __exportStar(require("./lib/logger"), exports);
21
21
  __exportStar(require("./lib/mavlink"), exports);
22
22
  __exportStar(require("./lib/mavesp"), exports);
23
+ __exportStar(require("./lib/mavtcp"), exports);
24
+ __exportStar(require("./lib/debug"), exports);
@@ -0,0 +1,14 @@
1
+ /// <reference types="node" />
2
+ import { MavLinkData } from 'mavlink-mappings';
3
+ import { MavLinkProtocolV2 } from './mavlink';
4
+ /**
5
+ * Serialize and deserialize a command into selected class
6
+ */
7
+ export declare function reserialize(command: MavLinkData): {
8
+ protocol: MavLinkProtocolV2;
9
+ buffer: Buffer;
10
+ header: import("./mavlink").MavLinkPacketHeader;
11
+ payload: Buffer;
12
+ data: MavLinkData;
13
+ };
14
+ //# sourceMappingURL=debug.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"debug.d.ts","sourceRoot":"","sources":["../../lib/debug.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAC9C,OAAO,EAAmB,iBAAiB,EAAE,MAAM,WAAW,CAAA;AAE9D;;GAEG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,WAAW;;;;;;EAc/C"}
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.reserialize = void 0;
4
+ const mavlink_1 = require("./mavlink");
5
+ /**
6
+ * Serialize and deserialize a command into selected class
7
+ */
8
+ function reserialize(command) {
9
+ const protocol = new mavlink_1.MavLinkProtocolV2(mavlink_1.MavLinkProtocol.SYS_ID, mavlink_1.MavLinkProtocol.COMP_ID);
10
+ const buffer = protocol.serialize(command, 1);
11
+ const header = protocol.header(buffer);
12
+ const payload = protocol.payload(buffer);
13
+ const data = protocol.data(payload, command.constructor);
14
+ return {
15
+ protocol,
16
+ buffer,
17
+ header,
18
+ payload,
19
+ data,
20
+ };
21
+ }
22
+ exports.reserialize = reserialize;
@@ -3,7 +3,7 @@
3
3
  import { EventEmitter } from 'events';
4
4
  import { MavLinkPacketSplitter, MavLinkPacketParser } from './mavlink';
5
5
  import { uint8_t, MavLinkData } from 'mavlink-mappings';
6
- export interface ConnectionInfo {
6
+ export interface EspConnectionInfo {
7
7
  ip: string;
8
8
  sendPort: number;
9
9
  receivePort: number;
@@ -32,7 +32,7 @@ export declare class MavEsp8266 extends EventEmitter {
32
32
  * @param sendPort port to send messages to (default: 14555)
33
33
  * @param ip IP address to send to in case there is no broadcast (default: empty string)
34
34
  */
35
- start(receivePort?: number, sendPort?: number, ip?: string): Promise<ConnectionInfo>;
35
+ start(receivePort?: number, sendPort?: number, ip?: string): Promise<EspConnectionInfo>;
36
36
  /**
37
37
  * Closes the client stopping any message handlers
38
38
  */
@@ -1 +1 @@
1
- {"version":3,"file":"mavesp.d.ts","sourceRoot":"","sources":["../../lib/mavesp.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAA;AAIrC,OAAO,EAAE,qBAAqB,EAAE,mBAAmB,EAA0B,MAAM,WAAW,CAAA;AAG9F,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAEvD,MAAM,WAAW,cAAc;IAC7B,EAAE,EAAE,MAAM,CAAA;IACV,QAAQ,EAAE,MAAM,CAAA;IAChB,WAAW,EAAE,MAAM,CAAA;CACpB;AAED;;GAEG;AACH,qBAAa,UAAW,SAAQ,YAAY;IAC1C,OAAO,CAAC,KAAK,CAAU;IACvB,OAAO,CAAC,MAAM,CAAC,CAAQ;IACvB,OAAO,CAAC,EAAE,CAAa;IACvB,OAAO,CAAC,QAAQ,CAAgB;IAChC,OAAO,CAAC,GAAG,CAAY;IAEvB;;;OAGG;gBACS,EACV,QAAsC,EACtC,MAAkC,GACnC;;;KAAK;IAiBN;;;;;;OAMG;IACG,KAAK,CAAC,WAAW,GAAE,MAAc,EAAE,QAAQ,GAAE,MAAc,EAAE,EAAE,GAAE,MAAW,GAAG,OAAO,CAAC,cAAc,CAAC;IAoB5G;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAY5B;;;;;;OAMG;IACG,IAAI,CAAC,GAAG,EAAE,WAAW,EAAE,KAAK,GAAE,OAAgC,EAAE,MAAM,GAAE,OAAiC,GAAG,OAAO,CAAC,MAAM,CAAC;IAOjI;;;;;;;OAOG;IACG,UAAU,CAAC,GAAG,EAAE,WAAW,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,GAAE,OAAW,EAAE,KAAK,GAAE,OAAgC,EAAE,MAAM,GAAE,OAAiC,GAAG,OAAO,CAAC,MAAM,CAA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AAK,GAAE,OAAgC,EAAE,MAAM,GAAE,OAAiC,GAAG,OAAO,CAAC,MAAM,CAAC;IAQzK;;;;OAIG;IACG,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IASjD,OAAO,CAAC,sBAAsB;IAO9B,OAAO,CAAC,qBAAqB;CAI9B"}
@@ -47,7 +47,7 @@ class MavEsp8266 extends events_1.EventEmitter {
47
47
  this.socket.on('message', this.processIncomingUDPData);
48
48
  // Start listening on the socket
49
49
  return new Promise((resolve, reject) => {
50
- this.socket?.bind(receivePort, () => {
50
+ this.socket?.bind(receivePort, async () => {
51
51
  // Wait for the first package to be returned to read the ip address
52
52
  // of the controller
53
53
  (0, utils_1.waitFor)(() => this.ip !== '')
@@ -0,0 +1,65 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import { EventEmitter } from 'events';
4
+ import { MavLinkPacketSplitter, MavLinkPacketParser } from './mavlink';
5
+ import { uint8_t, MavLinkData } from 'mavlink-mappings';
6
+ export interface TCPConnectionInfo {
7
+ ip: string;
8
+ port: number;
9
+ }
10
+ /**
11
+ * Encapsulation of communication over TCP
12
+ */
13
+ export declare class MavTCP extends EventEmitter {
14
+ private input;
15
+ private socket?;
16
+ private ip;
17
+ private port;
18
+ private seq;
19
+ /**
20
+ * @param splitter packet splitter instance
21
+ * @param parser packet parser instance
22
+ */
23
+ constructor({ splitter, parser, }?: {
24
+ splitter?: MavLinkPacketSplitter | undefined;
25
+ parser?: MavLinkPacketParser | undefined;
26
+ });
27
+ /**
28
+ * Start communication with the controller via MAVESP8266
29
+ *
30
+ * @param receivePort port to receive messages on (default: 14550)
31
+ * @param sendPort port to send messages to (default: 14555)
32
+ * @param ip IP address to send to in case there is no broadcast (default: empty string)
33
+ */
34
+ start(host?: string, port?: number): Promise<TCPConnectionInfo>;
35
+ /**
36
+ * Closes the client stopping any message handlers
37
+ */
38
+ close(): Promise<void>;
39
+ /**
40
+ * Send a packet
41
+ *
42
+ * @param msg message to send
43
+ * @param sysid system id
44
+ * @param compid component id
45
+ */
46
+ send(msg: MavLinkData, sysid?: uint8_t, compid?: uint8_t): Promise<number>;
47
+ /**
48
+ * Send a signed packet
49
+ *
50
+ * @param msg message to send
51
+ * @param sysid system id
52
+ * @param compid component id
53
+ * @param linkId link id for the signature
54
+ */
55
+ sendSigned(msg: MavLinkData, key: Buffer, linkId?: uint8_t, sysid?: uint8_t, compid?: uint8_t): Promise<number>;
56
+ /**
57
+ * Send raw data over the socket. Useful for custom implementation of data sending
58
+ *
59
+ * @param buffer buffer to send
60
+ */
61
+ sendBuffer(buffer: Buffer): Promise<number>;
62
+ private processIncomingTCPData;
63
+ private processIncomingPacket;
64
+ }
65
+ //# sourceMappingURL=mavtcp.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mavtcp.d.ts","sourceRoot":"","sources":["../../lib/mavtcp.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAA;AAIrC,OAAO,EAAE,qBAAqB,EAAE,mBAAmB,EAA0B,MAAM,WAAW,CAAA;AAE9F,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAEvD,MAAM,WAAW,iBAAiB;IAChC,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,EAAE,MAAM,CAAA;CACb;AAED;;GAEG;AACH,qBAAa,MAAO,SAAQ,YAAY;IACtC,OAAO,CAAC,KAAK,CAAU;IACvB,OAAO,CAAC,MAAM,CAAC,CAAQ;IACvB,OAAO,CAAC,EAAE,CAAsB;IAChC,OAAO,CAAC,IAAI,CAAe;IAC3B,OAAO,CAAC,GAAG,CAAY;IAEvB;;;OAGG;gBACS,EACV,QAAsC,EACtC,MAAkC,GACnC;;;KAAK;IAiBN;;;;;;OAMG;IACG,KAAK,CAAC,IAAI,GAAE,MAAoB,EAAE,IAAI,GAAE,MAAa,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAmBxF;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAa5B;;;;;;OAMG;IACG,IAAI,CAAC,GAAG,EAAE,WAAW,EAAE,KAAK,GAAE,OAAgC,EAAE,MAAM,GAAE,OAAiC,GAAG,OAAO,CAAC,MAAM,CAAC;IAOjI;;;;;;;OAOG;IACG,UAAU,CAAC,GAAG,EAAE,WAAW,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,GAAE,OAAW,EAAE,KAAK,GAAE,OAAgC,EAAE,MAAM,GAAE,OAAiC,GAAG,OAAO,CAAC,MAAM,CAAC;IAQzK;;;;OAIG;IACG,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IASjD,OAAO,CAAC,sBAAsB;IAK9B,OAAO,CAAC,qBAAqB;CAI9B"}
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MavTCP = void 0;
4
+ const events_1 = require("events");
5
+ const net_1 = require("net");
6
+ const stream_1 = require("stream");
7
+ const mavlink_1 = require("./mavlink");
8
+ const mavlink_2 = require("./mavlink");
9
+ /**
10
+ * Encapsulation of communication over TCP
11
+ */
12
+ class MavTCP extends events_1.EventEmitter {
13
+ input;
14
+ socket;
15
+ ip = '127.0.0.1';
16
+ port = 5760;
17
+ seq = 0;
18
+ /**
19
+ * @param splitter packet splitter instance
20
+ * @param parser packet parser instance
21
+ */
22
+ constructor({ splitter = new mavlink_1.MavLinkPacketSplitter(), parser = new mavlink_1.MavLinkPacketParser(), } = {}) {
23
+ super();
24
+ this.input = new stream_1.PassThrough();
25
+ this.processIncomingTCPData = this.processIncomingTCPData.bind(this);
26
+ this.processIncomingPacket = this.processIncomingPacket.bind(this);
27
+ // Create the reader as usual by piping the source stream through the splitter
28
+ // and packet parser
29
+ const reader = this.input
30
+ .pipe(splitter)
31
+ .pipe(parser);
32
+ reader.on('data', this.processIncomingPacket);
33
+ }
34
+ /**
35
+ * Start communication with the controller via MAVESP8266
36
+ *
37
+ * @param receivePort port to receive messages on (default: 14550)
38
+ * @param sendPort port to send messages to (default: 14555)
39
+ * @param ip IP address to send to in case there is no broadcast (default: empty string)
40
+ */
41
+ async start(host = '127.0.0.1', port = 5760) {
42
+ if (this.socket)
43
+ throw new Error('Already connected');
44
+ this.ip = host;
45
+ this.port = port;
46
+ // Create a TCP socket to connect to SITL
47
+ this.socket = new net_1.Socket();
48
+ this.socket.on('data', this.processIncomingTCPData);
49
+ this.socket.once('close', () => this.emit('close'));
50
+ // Start listening on the socket
51
+ return new Promise((resolve, reject) => {
52
+ this.socket?.connect(this.port, host, async () => {
53
+ resolve({ ip: this.ip, port: this.port });
54
+ });
55
+ });
56
+ }
57
+ /**
58
+ * Closes the client stopping any message handlers
59
+ */
60
+ async close() {
61
+ if (!this.socket)
62
+ throw new Error('Not connected');
63
+ // Unregister event handlers
64
+ this.socket.off('data', this.processIncomingTCPData);
65
+ // Close the socket
66
+ return new Promise(resolve => {
67
+ this.socket?.end(resolve);
68
+ this.socket = undefined;
69
+ });
70
+ }
71
+ /**
72
+ * Send a packet
73
+ *
74
+ * @param msg message to send
75
+ * @param sysid system id
76
+ * @param compid component id
77
+ */
78
+ async send(msg, sysid = mavlink_2.MavLinkProtocol.SYS_ID, compid = mavlink_2.MavLinkProtocol.COMP_ID) {
79
+ const protocol = new mavlink_2.MavLinkProtocolV2(sysid, compid);
80
+ const buffer = protocol.serialize(msg, this.seq++);
81
+ this.seq &= 255;
82
+ return this.sendBuffer(buffer);
83
+ }
84
+ /**
85
+ * Send a signed packet
86
+ *
87
+ * @param msg message to send
88
+ * @param sysid system id
89
+ * @param compid component id
90
+ * @param linkId link id for the signature
91
+ */
92
+ async sendSigned(msg, key, linkId = 1, sysid = mavlink_2.MavLinkProtocol.SYS_ID, compid = mavlink_2.MavLinkProtocol.COMP_ID) {
93
+ const protocol = new mavlink_2.MavLinkProtocolV2(sysid, compid, mavlink_2.MavLinkProtocolV2.IFLAG_SIGNED);
94
+ const b1 = protocol.serialize(msg, this.seq++);
95
+ this.seq &= 255;
96
+ const b2 = protocol.sign(b1, linkId, key);
97
+ return this.sendBuffer(b2);
98
+ }
99
+ /**
100
+ * Send raw data over the socket. Useful for custom implementation of data sending
101
+ *
102
+ * @param buffer buffer to send
103
+ */
104
+ async sendBuffer(buffer) {
105
+ return new Promise((resolve, reject) => {
106
+ this.socket?.write(buffer, (err) => {
107
+ if (err)
108
+ reject(err);
109
+ else
110
+ resolve(buffer.length);
111
+ });
112
+ });
113
+ }
114
+ processIncomingTCPData(buffer) {
115
+ // pass on the data to the input stream
116
+ this.input.write(buffer);
117
+ }
118
+ processIncomingPacket(packet) {
119
+ // let the user know we received the packet
120
+ this.emit('data', packet);
121
+ }
122
+ }
123
+ exports.MavTCP = MavTCP;