2011-08-04 20:32:01 +00:00
|
|
|
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
|
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const assert = require('assert-plus')
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
// var asn1 = require('asn1')
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
// var Protocol = require('../protocol')
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
/// --- Globals
|
2011-12-08 22:54:40 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
// var Ber = asn1.Ber
|
2011-12-08 22:54:40 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
/// --- API
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function Control (options) {
|
|
|
|
assert.optionalObject(options)
|
|
|
|
options = options || {}
|
|
|
|
assert.optionalString(options.type)
|
|
|
|
assert.optionalBool(options.criticality)
|
2015-11-12 04:36:21 +00:00
|
|
|
if (options.value) {
|
2019-08-27 21:11:49 +00:00
|
|
|
assert.buffer(options.value)
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
this.type = options.type || ''
|
|
|
|
this.criticality = options.critical || options.criticality || false
|
|
|
|
this.value = options.value || null
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
2015-11-12 04:36:21 +00:00
|
|
|
Object.defineProperties(Control.prototype, {
|
|
|
|
json: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getJson () {
|
2020-10-31 21:07:32 +00:00
|
|
|
const obj = {
|
2015-11-12 04:36:21 +00:00
|
|
|
controlType: this.type,
|
|
|
|
criticality: this.criticality,
|
|
|
|
controlValue: this.value
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|
|
|
|
return (typeof (this._json) === 'function' ? this._json(obj) : obj)
|
2015-11-12 04:36:21 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
Control.prototype.toBer = function toBer (ber) {
|
|
|
|
assert.ok(ber)
|
2011-09-16 16:06:07 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
ber.startSequence()
|
|
|
|
ber.writeString(this.type || '')
|
|
|
|
ber.writeBoolean(this.criticality)
|
2012-02-18 08:15:52 +00:00
|
|
|
if (typeof (this._toBer) === 'function') {
|
2019-08-27 21:11:49 +00:00
|
|
|
this._toBer(ber)
|
2011-12-08 22:54:40 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
if (this.value) { ber.writeString(this.value) }
|
2011-12-08 22:54:40 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
ber.endSequence()
|
|
|
|
}
|
2015-11-12 04:36:21 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
Control.prototype.toString = function toString () {
|
|
|
|
return this.json
|
|
|
|
}
|
2015-11-12 04:36:21 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
/// --- Exports
|
|
|
|
module.exports = Control
|