2011-08-04 20:32:01 +00:00
|
|
|
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
|
|
|
|
|
|
|
|
var assert = require('assert');
|
|
|
|
var util = require('util');
|
|
|
|
|
|
|
|
var asn1 = require('asn1');
|
|
|
|
|
2011-12-08 19:22:35 +00:00
|
|
|
var Protocol = require('../protocol');
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2011-12-08 22:54:40 +00:00
|
|
|
|
|
|
|
|
2011-08-04 20:32:01 +00:00
|
|
|
///--- Globals
|
2011-12-08 22:54:40 +00:00
|
|
|
|
2011-08-04 20:32:01 +00:00
|
|
|
var Ber = asn1.Ber;
|
|
|
|
|
2011-12-08 22:54:40 +00:00
|
|
|
|
|
|
|
|
2011-08-04 20:32:01 +00:00
|
|
|
///--- API
|
|
|
|
|
|
|
|
function Control(options) {
|
|
|
|
if (options) {
|
2012-02-18 08:15:52 +00:00
|
|
|
if (typeof (options) !== 'object')
|
2011-08-04 20:32:01 +00:00
|
|
|
throw new TypeError('options must be an object');
|
2012-02-18 08:15:52 +00:00
|
|
|
if (options.type && typeof (options.type) !== 'string')
|
2011-08-04 20:32:01 +00:00
|
|
|
throw new TypeError('options.type must be a string');
|
|
|
|
if (options.criticality !== undefined &&
|
2012-02-18 08:15:52 +00:00
|
|
|
typeof (options.criticality) !== 'boolean')
|
2011-08-04 20:32:01 +00:00
|
|
|
throw new TypeError('options.criticality must be a boolean');
|
2012-02-18 08:15:52 +00:00
|
|
|
if (options.value && typeof (options.value) !== 'string')
|
2011-08-04 20:32:01 +00:00
|
|
|
throw new TypeError('options.value must be a string');
|
|
|
|
} else {
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
this.type = options.type || '';
|
2011-12-08 22:54:40 +00:00
|
|
|
this.criticality = options.critical || options.criticality || false;
|
2012-01-06 16:51:54 +00:00
|
|
|
this.value = options.value || null;
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
var self = this;
|
2012-02-18 08:15:52 +00:00
|
|
|
this.__defineGetter__('json', function () {
|
2011-12-09 21:59:17 +00:00
|
|
|
var obj = {
|
2011-08-04 20:32:01 +00:00
|
|
|
controlType: self.type,
|
|
|
|
criticality: self.criticality,
|
|
|
|
controlValue: self.value
|
|
|
|
};
|
2012-02-18 08:15:52 +00:00
|
|
|
return (typeof (self._json) === 'function' ? self._json(obj) : obj);
|
2011-08-04 20:32:01 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
module.exports = Control;
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Control.prototype.toBer = function (ber) {
|
2011-09-16 16:06:07 +00:00
|
|
|
assert.ok(ber);
|
|
|
|
|
|
|
|
ber.startSequence();
|
|
|
|
ber.writeString(this.type || '');
|
|
|
|
ber.writeBoolean(this.criticality);
|
2012-02-18 08:15:52 +00:00
|
|
|
if (typeof (this._toBer) === 'function') {
|
2011-12-08 22:54:40 +00:00
|
|
|
this._toBer(ber);
|
|
|
|
} else {
|
|
|
|
if (this.value)
|
|
|
|
ber.writeString(this.value);
|
|
|
|
}
|
|
|
|
|
2011-09-16 16:06:07 +00:00
|
|
|
ber.endSequence();
|
2011-12-08 22:54:40 +00:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Control.prototype.toString = function () {
|
2011-12-08 22:54:40 +00:00
|
|
|
return this.json;
|
2011-09-16 16:06:07 +00:00
|
|
|
};
|