IO
This Module provides I/O functions : file, HTTP, FTP and GPIO if available.
IO.ls
Returns the list of files as a JSON object.
IO.ls(string path, [ string regexp ])
The returned object has the following properties:
- "type" : "FOLDER" or "FILE"
If "type" == "FILE":
- "file" : the file name
- "subtype" : type, can be "FILE" for regular file, "LINK" for a link to another file, "FIFO" for a system fifo
- "size" : file size, in bytes.
If "type" == "FOLDER":
* "folder" : the folder name
* "files" : array of files
Example :
var r = IO.ls('/opt/vmbase/conf');
print( JSON.stringify( r )) ;
Will print:
{
"folder":"/opt/vmbase/conf",
"files":[
{"file":"rotctld.conf","type":"FILE","size":75},
{"file":"sdrvm.conf","type":"FILE","size":65},
{"file":"vmservice.conf","type":"FILE","size":172}
]
}
Using the regexp filter
For a detailed review of the accepted syntax, see ECMAScript regular expression grammar
- Regex example, find *.cs16 files in current directory :
var r = IO.ls('.','^.*\.(cs16)$');
print( JSON.stringify( r )) ;
for (var a=0; a< r.files.length; a++) {
print(JSON.stringify(r.files[a].file));
}
print('Found ',r.files.length.toFixed(0), ' files');
- To list all *.CS16 files excluding '_' prefix filename, use following regex string :
var r = IO.ls('.','^(?!_).*\.(cs16)$');
IO.fwrite
Writes a buffer to a file. The file is overwritten if already exists.
IO.fwrite(filename, buffer)
IO.fwritestr
Write a string to file. The file is overwritten if already exists.
IO.fwritestr(filename, string)
IO.fappend
Appends buffer content to file
IO.fappend(filename, buffer)
- Example : download and save a TLE file
var myTLE = IO.HTTPGet("http://www.celestrak.com/NORAD/elements/weather.txt",true);
IO.fappend('/tmp/weather.txt',myTLE);
IO.fappendstr
Appends a string to an existing file
IO.fappendstr(filename, string)
IO.fread
Loads (read) a file.
IO.fread(filename)
IO.frename
Rename file
IO.frename(old_name,new_name)
- Example :
var rename = IO.frename(old_name,new_name);
print(rename); // returns 1 if OK
IO.fdelete
Delete file
IO.fdelete(filename)
IO.getfsize
Get file size
IO.getfsize(filename)
- Example : display size of a local file
var size = IO.getfsize('/tmp/weather.txt');
print('Size : ',size.toFixed(0), ' bytes');
IO.getVolumeUse
Display occupied disk space (percent).
Note : 'filename' parameter is an existing file located on the volume.
IO.getVolumeUse(filename)
- Example :
var x = IO.getVolumeUse('/etc/passwd');
print('Disk use : ' + x + ' %' );
IO.FTPSend
Send a file to a remote host using FTP protocol
IO.FTPSend(params, filename)
- Set '' params'' as follows :
var params = {
host: '192.168.1.10',
user: 'myusername',
password: 'mypassword',
passive: true,
destination_folder: '/usr/remote/'
};
- Example:
var rc = IO.ftpSend( params, 'localfile.txt');
IO.HTTPGet
Download a remote file using HTTP protocol (similar to WGET/CURL).
Note : Follows Location: header that the server sends as part of an HTTP header in a 3xx response.
IO.HTTPGet(URL, [true])
True --> binary file
- Example : download and save as /tmp/weather.txt :
var myTLE = IO.HTTPGet("http://www.celestrak.com/NORAD/elements/weather.txt",true);
IO.fappend('/tmp/weather.txt',myTLE);
IO.HTTPPost
Send message using HTTP POST type request.
Note : Follows Location: header that the server sends as part of an HTTP header in a 3xx response.
IO.HTTPPost(URL, message)
var params='spectrum,name=MySDR freq=2400005001,value=-88.52'
IO.HTTPPost('http://10.2.0.1:8086/write?db=levels', params);
IO.HTTPGetExt
Download a remote file using HTTP protocol (similar to WGET/CURL) with extended parameters and output.
Note : Follows Location: header that the server sends as part of an HTTP header in a 3xx response.
var object = IO.HTTPGet(URL, [timeout in seconds, default is 2])
A JSON Object is returned :
{
http_url: 'the_url_you_called',
http_method: 'GET',
http_error_code: number,
http_error: 'a_written_message_explaining the error',
http_response: 'what_has_been_downloaded_if_ok'
}
For a detailed list of possible values of http_error_code, look at Curl Doc
- Example : calling a non-existing server :
var res = IO.HTTPGetExt("http://localhost/", 5);
print( JSON.stringify( res ));
{
http_url: 'http://localhost/',
http_method: 'GET',
http_error_code: 7,
http_error: 'Couldn't connect to server',
http_response: ''
}
IO.HTTPPostExt
Send message using HTTP POST type request, with extended parameters and output.
Note : Follows Location: header that the server sends as part of an HTTP header in a 3xx response.
IO.HTTPPost(URL, message, [timeout in seconds, default is 2])
A JSON Object is returned :
{
http_url: 'the_url_you_called',
http_method: 'GET',
http_error_code: number,
http_error: 'a_written_message_explaining the error',
http_response: 'what_has_been_downloaded_if_ok'
}
For a detailed list of possible values of http_error_code, look at Curl Doc
IO.SFTPSend
-
Send file using SFTP/SSH protocol
Initial exchange of SSH ID-keys between local host and remote server is mandatory (perform a manual SSH connection first).
IO.SFTPSend({host: 'remote_ip', tcp_port: 22, destination_folder: '/home/sdrvm/pub/', user: 'my_name', password: 'my_password'}, filename)
- Example: send file via SCP
// Send the last plot file 'plot.png' using SSH
var lastplot = IO.SFTPSend({host: 'remote_ip', tcp_port: 22, destination_folder: '/home/sdrvm/pub/', user: 'my_name', password: 'my_password'}, 'plot.png');
sleep(1000);
IO.hasGPIO
This function returns true if the VM target (running platform) supports GPIO. Typically on a standard x86 PC this function returns false.
GPIO
The following section is NVIDIA Jetson specific. It is not yet available on other platforms. The GPIO API exposes the pins numbered as on the NVIDIA Evaluation kit numbering.
IO.gpioOpen
- Claims exclusive access to one GPIO pin. The GPIO is no longer available for the system and cannot be claimed by another task.
- Pins configured as outputs will be configured at low level.
bool ok = IO.gpioOpen( gpio , direction )
- gpio pin number: check your board documentation,
- direction : 0 or negative number for input, positive and greater than 1 for output
Check the returned value, it is true if and only if the SDRVM has access to the pin.
IO.gpioClose
- Release GPIO exclusive access.
bool ok = IO.gpioClose( gpio )
IO.gpioSetValue
- Changes the output for one GPIO. The GPIO must be configured before.
bool ok = IO.gpioSetValue( gpio, value )
Parameters :
- value : negative or 0 for low level, >= 1 for high level
Returns true if the value has been sent to the GPIO hardware.
IO.gpioGetValue
- Reads the current value from a GPIO previously configured as input.
bool value = IO.gpioGetValue( gpio )
The returned boolean is true when the input is high.
Last update: October 17, 2023