Skip to content

Commit

Permalink
Refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
IHateYourCode committed Sep 17, 2022
1 parent 1ecb550 commit 046b921
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 44 deletions.
28 changes: 13 additions & 15 deletions Source/Library/API/AvailablePorts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { allPorts } from '../Ports.js'
import * as Serial from '../Native.js'


const { NotFound } = Deno.errors;
const { log } = console;


Expand All @@ -26,30 +26,28 @@ export default async function listPorts (){

async function isAvailable ( port ){

const [ file ] = await Serial.openPort(port);

if(file < 1)
return false;

try {

const [ data , error ] = await Serial.portInfo(file);
const file = await Serial.openPort(port);

// log('Info',port,data,error,exception());
try {

if(data)
await Serial.portInfo(file);
return true;

throw `Unknown IOCTL Serial Reading Error : ${ error }`;
} catch (error) {

} catch (exception) {
error.stack = 'IOCTL Serial Reading Error\n' + error.stack;
throw error;

const [ result , error ] = await
} finally {
Serial.closeFile(file);
}
} catch (error) {

if(result !== 0)
throw `Wasn't able to close Serial Port file descriptor : ${ error }`;
if(error instanceof NotFound)
return false;

throw exception;
throw error;
}
}
7 changes: 4 additions & 3 deletions Source/Library/Errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ const {
AddrInUse , ConnectionAborted , AddrNotAvailable ,
ConnectionRefused , ConnectionReset , AlreadyExists ,
Interrupted , OutOfMemory , Unsupported , NotConnected ,
BrokenPipe , TimedOut
BrokenPipe , TimedOut , InvalidInput
} = Deno.errors;


export default Errors = {
export default {

1 : PermissionDenied ,
2 : NotFound ,
3 : NotFound ,
Expand All @@ -22,7 +23,7 @@ export default Errors = {
// 11 : TryAgain_OperationWouldBlock ,
12 : OutOfMemory ,
13 : PermissionDenied ,
14 : BadAddress ,
// 14 : BadAddress ,
// 15 : BlockDeviceRequired ,
// 16 : DeviceOrResourceBusy ,
17 : AlreadyExists ,
Expand Down
77 changes: 51 additions & 26 deletions Source/Library/Native.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@

import { Buffer , BufReader , readShort }
from 'https://deno.land/[email protected]/io/mod.ts'

import Definitions from './Definitions.js'
import Commands from './Commands.js'
import Errors from './Errors.js'
import * as Paths from './Paths.js'


const { dlopen , errors } = Deno;
const { Interrupted } = errors;
const { log } = console;

const { symbols : Native } =
dlopen(Paths.sharedLibrary,Definitions);
Expand All @@ -20,6 +19,28 @@ const cString = ( string ) =>
.encode(string);


function exception (){

const error = Native.error();

const exception = (error in Errors)
? new Errors[error]
: new Error(`System Error : ${ error }`) ;

exception.stack = exception.stack
.split('\n')
.filter((_,index) => index !== 1)
.join('\n');

return exception;

// if(error in Errors)
// return new Errors[error];
//
// return new Error(`System Error : ${ error }`);
}


/*
* Try to open a serial port.
*/
Expand All @@ -28,16 +49,28 @@ export async function openPort ( port ){

const bytes = cString(port);

return await tryUninterrupted(Native.openPort,bytes);
const [ file , error ] = await
retry(Native.openPort,bytes);

if(file < 0)
throw error;

return file;
}


/*
* Close a file.
*/

export async function closeFile ( file ){
return await wrapError(Native.closePort,file);
export function closeFile ( file ){

const success = Native.closePort(file) + 1;

if(success)
return;

throw exception();
}


Expand All @@ -49,14 +82,13 @@ export async function portInfo ( file ){

const data = new Uint8Array(72);

const success = await Native.deviceCall(file,Commands.QuerySerial,data) + 1;

const error = await Native.error();
const [ success , error ] = await
retry(deviceCall,file,Commands.QuerySerial,data);

if(success)
return [ data , error ];
if(success !== -1)
return data;

return [ -1 , error ];
throw error;
}


Expand All @@ -68,27 +100,20 @@ export async function deviceCall ( file , command , data ){
return Native.deviceCall(file,command,data);
}

async function wrapError ( func , ... parameters ){

let value = func(...parameters);

if(value instanceof Promise)
value = await value;

return [ value , await Native.error() ]
}

/*
* Continuosly try calling the
* function if it is interrupted.
*/

async function tryUninterrupted ( ... args ){
async function retry ( func , ... parameters ){

let value , error ;
let result , error ;

do { [ value , error ] = await wrapError(...args); }
while ( value === -1 && error === Interrupted );
do {
result = await func(...parameters);
error = exception();
} while ( result < 0 && error instanceof Interrupted );

return [ value , error ];
return [ result , error ];
}

0 comments on commit 046b921

Please sign in to comment.