1 |
|
2 |
|
3 | var assert = require('assert-plus');
|
4 | var util = require('util');
|
5 |
|
6 | var asn1 = require('asn1');
|
7 |
|
8 | var Protocol = require('../protocol');
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | var Ber = asn1.Ber;
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | function Control(options) {
|
19 | assert.optionalObject(options);
|
20 | options = options || {};
|
21 | assert.optionalString(options.type);
|
22 | assert.optionalBool(options.criticality);
|
23 | if (options.value) {
|
24 | assert.buffer(options.value);
|
25 | }
|
26 |
|
27 | this.type = options.type || '';
|
28 | this.criticality = options.critical || options.criticality || false;
|
29 | this.value = options.value || null;
|
30 | }
|
31 | Object.defineProperties(Control.prototype, {
|
32 | json: {
|
33 | get: function getJson() {
|
34 | var obj = {
|
35 | controlType: this.type,
|
36 | criticality: this.criticality,
|
37 | controlValue: this.value
|
38 | };
|
39 | return (typeof (this._json) === 'function' ? this._json(obj) : obj);
|
40 | }
|
41 | }
|
42 | });
|
43 |
|
44 | Control.prototype.toBer = function toBer(ber) {
|
45 | assert.ok(ber);
|
46 |
|
47 | ber.startSequence();
|
48 | ber.writeString(this.type || '');
|
49 | ber.writeBoolean(this.criticality);
|
50 | if (typeof (this._toBer) === 'function') {
|
51 | this._toBer(ber);
|
52 | } else {
|
53 | if (this.value)
|
54 | ber.writeString(this.value);
|
55 | }
|
56 |
|
57 | ber.endSequence();
|
58 | return;
|
59 | };
|
60 |
|
61 | Control.prototype.toString = function toString() {
|
62 | return this.json;
|
63 | };
|
64 |
|
65 |
|
66 |
|
67 | module.exports = Control;
|