2Copyright 2018-2021 Intel Corporation
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
17var obj = { meshCoreInfo: 'TinyCore v1' };
20// Replace a string with a number if the string is an exact number
21function toNumberIfNumber(x) { if ((typeof x == 'string') && (+parseInt(x) === x)) { x = parseInt(x); } return x; }
23// Split a string taking into account the quoats. Used for command line parsing
24function splitArgs(str)
26 var myArray = [], myRegexp = /[^\s"]+|"([^"]*)"/gi;
27 do { var match = myRegexp.exec(str); if (match != null) { myArray.push(match[1] ? match[1] : match[0]); } } while (match != null);
30// Parse arguments string array into an object
31function parseArgs(argv)
33 var results = { '_': [] }, current = null;
34 for (var i = 1, len = argv.length; i < len; i++)
37 if (x.length > 2 && x[0] == '-' && x[1] == '-')
39 if (current != null) { results[current] = true; }
40 current = x.substring(2);
43 if (current != null) { results[current] = toNumberIfNumber(x); current = null; } else { results['_'].push(toNumberIfNumber(x)); }
46 if (current != null) { results[current] = true; }
49function sendConsoleText(msg, sessionid)
53 if (sessionid != null)
55 require('MeshAgent').SendCommand({ action: 'msg', type: 'console', value: msg, sessionid: sessionid });
59 require('MeshAgent').SendCommand({ action: 'msg', type: 'console', value: msg });
67function processConsoleCommand(cmd, args, rights, sessionid)
75 response = "Available commands are: eval, osinfo, setdebug, versions.";
78 response = JSON.stringify(process.versions, null, ' ');
82 if (args['_'].length < 1)
84 response = 'Proper usage: eval "JavaScript code"'; // Display correct command usage
87 response = JSON.stringify(require('MeshAgent').eval(args['_'][0])); // This can only be run by trusted administrator.
93 if (args['_'].length < 1) { response = 'Proper usage: setdebug (target), 0 = Disabled, 1 = StdOut, 2 = This Console, * = All Consoles, 4 = WebLog, 8 = Logfile'; } // Display usage
94 else { if (args['_'][0] == '*') { console.setDestination(2); } else { console.setDestination(parseInt(args['_'][0]), sessionid); } }
97 case 'osinfo': { // Return the operating system information
99 if (args['_'].length > 0) { i = parseInt(args['_'][0]); if (i > 8) { i = 8; } response = 'Calling ' + i + ' times.'; }
100 for (var j = 0; j < i; j++)
102 var pr = require('os').name();
103 pr.sessionid = sessionid;
106 sendConsoleText("OS: " + v, this.sessionid);
111 default: { // This is an unknown command, return an error message
112 response = 'Unknown command \"' + cmd + '\", type \"help\" for list of available commands.';
116 } catch (e) { response = "Command returned an exception error: " + e; console.log(e); }
117 if (response != null) { sendConsoleText(response, sessionid); }
121// Handle a mesh agent command
122function handleServerCommand(data)
124 if ((typeof data == 'object') && (data.action == 'msg') && (data.type == 'console') && data.value && data.sessionid)
126 if (data.value && data.sessionid)
130 var args = splitArgs(data.value);
131 processConsoleCommand(args[0].toLowerCase(), parseArgs(args), data.rights, data.sessionid);
141 console.log(JSON.stringify(data, null, 1));
145// Called when the server connection state changes
146function handleServerConnection(state)
148 if (state == 1) { mesh.SendCommand({ "action": "coreinfo", "value": obj.meshCoreInfo }); } // Server connected, send mesh core information
151obj.start = function ()
153 // Hook up mesh agent events
154 mesh.AddCommandHandler(handleServerCommand);
155 mesh.AddConnectHandler(handleServerConnection);
156 mesh.SendCommand({ action: 'coreinfo', value: "TinyCore", caps: 0 });
159obj.stop = function ()
161 mesh.AddCommandHandler(null);
162 mesh.AddConnectHandler(null);
167try { xexports = module.exports; } catch (e) { }
171 // If we are running within NodeJS, export the core
172 module.exports.createMeshCore = function (agent) { mesh = agent.getMeshApi(); return (obj); };
176 // If we are not running in NodeJS, launch the core
177 sendConsoleText('TinyCore Started...');
178 mesh = require('MeshAgent');