Update Iot.js
[platform/upstream/iotjs.git] / tools / src / js / uart.js
1 /* Copyright 2016-present Samsung Electronics Co., Ltd. and other contributors
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 var EventEmitter = require('events').EventEmitter;
17 var util = require('util');
18 var uart = process.binding(process.binding.uart);
19
20 // VALIDATION ARRAYS
21 var BAUDRATE = [0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400
22                 , 4800, 9600, 19200, 38400, 57600, 115200, 230400];
23 var DATABITS = [5, 6, 7, 8];
24
25 var defaultConfiguration = {
26   baudRate: 9600,
27   dataBits: 8
28 };
29
30
31 function Uart() {
32   if (!(this instanceof Uart)) {
33     return new Uart();
34   }
35 }
36
37 Uart.prototype.open = function(configuration, callback) {
38   return new UartPort(configuration, callback);
39 };
40
41
42 function UartPort(configuration, callback) { //constructor
43   var self = this;
44
45   if (util.isObject(configuration)) {
46     if (!util.isString(configuration.device)) {
47       throw new TypeError(
48         'Bad configuration - device is mandatory and should be String');
49     }
50   } else {
51     throw new TypeError('Bad arguments - configuration should be Object');
52   }
53
54   // validate baud rate
55   if (!util.isUndefined(configuration.baudRate)) {
56     if (BAUDRATE.indexOf(configuration.baudRate) === -1) {
57       throw new TypeError("Invalid 'baudRate': " + configuration.baudRate);
58     }
59   } else {
60     configuration.baudRate = defaultConfiguration.baudRate;
61   }
62
63   // validate data bits
64   if (!util.isUndefined(configuration.dataBits)) {
65     if (DATABITS.indexOf(configuration.dataBits) === -1) {
66       throw new TypeError("Invalid 'databits': " + configuration.dataBits);
67     }
68   } else {
69     configuration.dataBits = defaultConfiguration.dataBits;
70   }
71
72   EventEmitter.call(this);
73
74   this._binding = new uart(configuration, this, function(err) {
75     util.isFunction(callback) && callback.call(self, err);
76   });
77
78   process.on('exit', (function(self) {
79     return function() {
80       if (!util.isNull(self._binding)) {
81         self.closeSync();
82       }
83     };
84   })(this));
85 }
86
87 util.inherits(UartPort, EventEmitter);
88
89 UartPort.prototype.write = function(buffer, callback) {
90   var self = this;
91
92   if (util.isNull(this._binding)) {
93     throw new Error('UART port is not opened');
94   }
95
96   this._binding.write(buffer, function(err) {
97     util.isFunction(callback) && callback.call(self, err);
98   });
99 };
100
101 UartPort.prototype.writeSync = function(buffer) {
102   var self = this;
103
104   if (util.isNull(this._binding)) {
105     throw new Error('UART port is not opened');
106   }
107
108   this._binding.write(buffer);
109 };
110
111 UartPort.prototype.close = function(callback) {
112   var self = this;
113
114   if (util.isNull(this._binding)) {
115     throw new Error('UART port is not opened');
116   }
117
118   this._binding.close(function(err) {
119     util.isFunction(callback) && callback.call(self, err);
120   });
121   this._binding = null;
122 };
123
124 UartPort.prototype.closeSync = function() {
125   if (util.isNull(this._binding)) {
126     throw new Error('UART port is not opened');
127   }
128
129   this._binding.close();
130   this._binding = null;
131 };
132
133 module.exports = Uart;