Skip to content

Commit

Permalink
Allow WriteByDid only for dids on white list.
Browse files Browse the repository at this point in the history
  • Loading branch information
MyHomeMyData committed Jan 6, 2024
1 parent d95542a commit 2e88ab1
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions lib/canUds.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ class uds {
// Stop worker:
this.callback = null;
if (opMode == 'normal') {
await ctx.unsubscribeStates(ctx.namespace+'.'+this.config.stateBase+'.*');
await ctx.log.info('UDS worker stopped on '+this.config.stateBase);
} else {
await ctx.log.silly('UDS worker stopped in mode '+opMode+' on '+this.config.stateBase);
Expand Down Expand Up @@ -309,12 +310,32 @@ class uds {

async onUdsStateChange(ctx, ctxWorker, id, state) {

// Change of UDS Writables
// =======================
if (id.includes(this.storage.storageDids.didsWritablesId)) {
// User requests change of UDS writables
await ctx.log.info('User requested change of UDS dids writable on '+this.config.stateBase);
await this.storage.storageDids.readKnownDids(ctx, await this.storage.getOpMode());
await ctx.setStateAsync(id, { val: state.val, ack: true }); // Acknowlegde user command
return;
}

// Change of UDS device specific datapoint definition
// ==================================================
if (id.includes(this.storage.storageDids.didsSpecId)) {
// User requests change of UDS device specific datapoint definition
await ctx.log.info('User requested change of UDS device specific datapoint definition on '+this.config.stateBase);
await this.storage.storageDids.readKnownDids(ctx, await this.storage.getOpMode());
await ctx.setStateAsync(id, { val: state.val, ack: true }); // Acknowlegde user command
return;
}

// User command ReadByDid
// ======================
if (id.includes(this.userReadByDidId)) {
// User requests ReadByDid
const dids = JSON.parse(state.val);
await ctx.log.debug('UDS user command ReadByDid on '+this.config.stateBase+'. Dids='+JSON.stringify(dids));
await ctx.log.debug('User command UDS ReadByDid on '+this.config.stateBase+'. Dids='+JSON.stringify(dids));
await this.pushCmnd(ctx, 'read', dids);
await ctx.setStateAsync(id, { val: JSON.stringify(dids), ack: true }); // Acknowlegde user command
return;
Expand All @@ -325,15 +346,15 @@ class uds {
const dcs = await ctx.idToDCS(id); // Get device, channel and state id
if (dcs.state.length < 6) {
// Implausible state id
ctx.log.warn('UDS user command WriteByDid on '+this.config.stateBase+': Could not evaluate state change on id '+id);
ctx.log.warn('User command UDS WriteByDid on '+this.config.stateBase+': Could not evaluate state change on id '+id);
return;
}
const did = Number(dcs.state.slice(0,4));
if (!(did in this.storage.storageDids.didsWritable)) {
ctx.log.error('UDS user command WriteByDid on '+this.config.stateBase+'.'+String(did)+': Writing not allowed on this did. Pls. refer to README for further informations.');
ctx.log.error('User command UDS WriteByDid on '+this.config.stateBase+'.'+String(did)+': Writing not allowed on this did. Pls. refer to README for further informations.');
return;
}
await ctx.log.debug('UDS user command WriteByDid on '+this.config.stateBase+'.'+String(did));
await ctx.log.debug('User command UDS WriteByDid on '+this.config.stateBase+'.'+String(did));
//await ctx.log.debug(JSON.stringify(dcs)+' did='+String(did)+' id='+id+' state='+JSON.stringify(state));
let byteArr=null; // Encoded data
let lenBaseId; // Index of start of did state id (did_name ...) in full state id (e3oncan ...)
Expand All @@ -345,7 +366,7 @@ class uds {
await this.pushCmnd(ctx, 'write', [[did,byteArr]]);
setTimeout(function(ctxWorker,did){ctxWorker.cmndsQueue.push({'mode':'read', 'did': did});},2500,this,did); // Read value after 2500 ms
} else {
ctx.log.error('UDS user command WriteByDid on '+this.config.stateBase+': Encoding of data failed.');
ctx.log.error('User command UDS WriteByDid on '+this.config.stateBase+': Encoding of data failed.');
}
break;
case 'raw':
Expand All @@ -355,7 +376,7 @@ class uds {
await this.pushCmnd(ctx, 'write', [[did,byteArr]]);
setTimeout(function(ctxWorker,did){ctxWorker.cmndsQueue.push({'mode':'read', 'did': did});},2500,this,did); // Read value after 2500 ms
} else {
ctx.log.error('UDS user command WriteByDid on '+this.config.stateBase+': Encoding of data failed.');
ctx.log.error('User command UDS WriteByDid on '+this.config.stateBase+': Encoding of data failed.');
}
break;
case 'tree':
Expand All @@ -369,7 +390,7 @@ class uds {
await this.pushCmnd(ctx, 'write', [[did,byteArr]]);
setTimeout(function(ctxWorker,did){ctxWorker.cmndsQueue.push({'mode':'read', 'did': did});},2500,this,did); // Read value after 2500 ms
} else {
ctx.log.error('UDS user command WriteByDid on '+this.config.stateBase+': Encoding of data failed.');
ctx.log.error('User command UDS WriteByDid on '+this.config.stateBase+': Encoding of data failed.');
}
break;
}
Expand Down Expand Up @@ -417,12 +438,12 @@ class uds {
await ctxWorker.pushCmnd(ctx, 'write', [[did,byteArr]]);
setTimeout(function(ctxWorker,did){ctxWorker.cmndsQueue.push({'mode':'read', 'did': did});},2500,ctxWorker,did); // Read value after 2500 ms
} else {
ctx.log.error('UDS user command WriteByDid on '+ctxWorker.config.stateBase+': Encoding of data failed.');
ctx.log.error('User command UDS WriteByDid on '+ctxWorker.config.stateBase+': Encoding of data failed.');
}
});
break;
default:
ctx.log.warn('UDS user command WriteByDid on '+this.config.stateBase+': Could not evaluate state change on id '+id);
ctx.log.warn('User command UDS WriteByDid on '+this.config.stateBase+': Could not evaluate state change on id '+id);
}
}

Expand Down

0 comments on commit 2e88ab1

Please sign in to comment.