How can we write our own custom AT commands? - unetstack

Is there a way that we can write a custom AT command for unetstack/subnero modems ? I refered Chapter 12 but could not find this information.

AT command shells (and Groovy shells) can be extended with shell extensions. Shell extensions implement a org.arl.fjage.shell.ShellExtension tag interface. Static methods (and attributes) of the shell extension class are made available in the shell as commands (and variables/constants). In the AT command shell, they are called using AT commands as briefly described in section 12.3 of the Unet handbook.
For example, the handbook shows an example of loading the PhysicalShellExt via:
AT~EXT=org.arl.unet.phy.PhysicalShellExt
On loading this, static methods of this class become accessible using AT commands. For example, methods:
static def plvl() {
// code to get power level here
}
static String plvl(float p) {
// code to set power level here
}
are accessed as:
AT~PLVL
AT~PLVL=-3
respectively.

Related

Doxygen: Outputting Version Numbers

I would like to have Doxygen display the source code version number as part of the main page or the title header.
Presently, our code has the version defined as a text literal:
/*!
* \brief Text literal containing the build number portion of the
* ESG Application Version.
*/
static const char build_version_text[] = "105";
I have searched the internet for a method to get the 105 from the above statement into the Doxygen main page (or header) with no luck.
Background
We have a build server that updates the text string as part of a nightly build operation. The file is updated, then checked into the Software Configuration Management system. The build server is also capable of generating the documentation. We would also like to have the developers be able to check out the code, the build the Doxygen documentation at their workstations.
We are using Doxygen version 1.8.11.
What you're looking for is to set the PROJECT_NUMBER config option based on the value in your source. I don't think this can be done, but the way I would go about achieving the same result is as follows.
Since the project version is updated when a build script runs, have the build script generate an extra file, for example Doxyversion. Have the content of the file be:
PROJECT_NUMBER = "<versiontext>"
Update your main Doxyfile and replace
PROJECT_NUMBER =
with
#INCLUDE = "<pathToDoxyversion>"
Edit:
A solution I can think of that does not require duplicating the version string requires parsing the version string out from the file into an environment variable. Then you can set PROJECT_NUMBER to
PROJECT_NUMBER=$(ENV_VAR)
Another option is you can call doxygen with
( cat Doxyfile ; echo "PROJECT_NUMBER=$ENV_VAR" ) | doxygen
Both solutions would require the developers to know to do this when generating the documentation, or wrapping the entire doxygen call in a script. Also potential portability issues.
Full solution below, from a real example.
Main page
In the documentation for the main page (or anywhere, really), use special markers for the text to substitute dynamically.
Main page source:
https://github.com/mysql/mysql-server/blob/8.0/sql/mysqld.cc#L22
See the special ${DOXYGEN_GENERATION_DATE} markers
Doxygen input filters
In the doxygen configuration file, define an input filter for the file containing the special markers. For example,
FILTER_PATTERNS = "*/sql/mysqld.cc=./doxygen-filter-mysqld"
Implement the doxygen-filter-mysqld script to:
Find the dynamic value to substitute (in your case, parse the value of build_version_text)
Replace (sed) the special marker with the value
Output the result to stdout
For example:
CMD1="s/\\\${DOXYGEN_GENERATION_DATE}/"`date -I`"/g"
...
sed -e ${CMD1} -e ${CMD2} -e ${CMD3} $1
Results
Result is at
http://devdocs.no.oracle.com/mysql-server/8.0.0/
See Also
All this is a work around for that I think should be a good Doxygen feature.
See bug#769679 (Feature Request: doxygen command to expand an environment variable) that was entered for this.
https://bugzilla.gnome.org/show_bug.cgi?id=769679

How do I create functions based on user input?

I am currently working on a combination software and Arduino project that has the following general structure:
User inputs a string of commands through a terminal program such as CoolTerm
Commands are sent to Arduino through USB Serial
First command is parsed, along with included arguments
Function associated with first command is executed
Second command is parsed, along with included arguments
Function associated with second command is executed
Etc. until all commands have been parsed and executed
So far, all of this works as I would expect it to. However, the project I am working on requires very precise timing, and having to parse each individual command creates a considerable amount of processing time (not sure if this is the right term) between each command execution.
For example, in a user input string that contains three commands, between the first command being parsed and the last command being executed, there is an additional 5.8 milliseconds of processing time from start to finish.
To be clear, all parts of my program are functional, including user input, string parsing, and function execution as described above. I need to improve on my existing code, not correct errors.
Ideally, I imagine that the program will parse each command, "set aside" the function that is associated with the command, and execute all commands sequentially once they have all been "set aside." This will shorten the processing time significantly by getting rid of the need to continue parsing commands between each function execution. I am not sure how to accomplish this, or if it is even possible.
To illustrate my ideas in very basic C++ pseudocode:
(assuming example user input is "A, B, C")
loop() {
// Example user input received: "A, B, C" corresponds to:
// functionA, functionB, functionC
String userInput = receiveInput();
// Parse user input
parse(userInput);
// Execute functions specified by user input
executeFunctions();
}
/*Parsing separates "A, B, C" to functionA, functionB, functionC
Functions are "set aside" to be executed sequentially,
the next beginning directly after the last ends*/
executeFunctions{
// Example functions to be executed
functionA();
functionB();
functionC();
}
Question:
I need a way to create a function based on user input, or based on another function. I have never heard of such a concept through the extensive research I have done, and I am not sure if it exists. If possible, this is the method I would like to use to proceed with my project, as I believe it will require the least amount of restructuring of my code.
Edit:
This project requires compatibility with Arduino hardware and the Arduino IDE. Standard C++ will not work.
You could use a Command Pattern.
Basically, make your parser to put a different command object for each user input into some sort of queue. You can use a basic function object for this:
struct Command {
virtual ~Command() {}
virtual void operator()(); // this will execute the command
};
class FirstCommand : public Command {
// some private data + constructor
public:
virtual void operator()() { /* do stuff for this user input */ }
};
class SecondCommand : public Command {
// some private data + constructor
public:
virtual void operator()() { /* do stuff for this user input */ }
};
A parser would create either FirstCommand or SecondCommand, and store them in the std::queue<Command*> or something more sophisticated. Your consumer code would then execute every command by doing something like:
while (!q.empty() {
Command* command = q.front();
(*command)();
q.pop();
}
With thread-safe queues, the consumer code can even be run in parallel to your parser.
You could use a queue of simple pointers to functions instead of command objects, but if you do, their signatures will have to be the same, while a constructor for a specific command can be arbitrary.
You could create a map of functions. Like this:
typedef void (*ftype)(void);
map<string, ftype> function_map;
Now you map all your functions to a command:
function_map["A"] = functionA;
function_map["B"] = functionB;
function_map["C"] = functionC;
Now after you take the user input and parse it, you can just do:
//foreach command
function_map[command]();
Here's a short code demo with primitive parsing
Could be an alternative, not exact answer. Write a computer program that parses input and sends the commands in binary through the serial. This way serial read (which is quite slow even at 115200) and parsing would be averted. As a bonus, your application has the possibility of being more user-friendly compared to a terminal.

C Loading Code dynamically in the same way as the Java Compiler Api 7

I have the following use case which I had previously solved in Java, but am now required to port the program to C.
I had a method A which called a method do_work() belonging to an abstract class Engine. Each concrete implementation of the class was constructed as follows:
users would submit the definition of the do_work() method . If this definition was correct, the programmer would construct a concrete implementation of the Engine class using the Java Compiler API. (code for this is included for reference below).
How can I do something similar in C:
I now have a structure Engine, with a function pointer to the do_work() method. I want users to be able to submit this method at run time (note: this only occurs once, on startup, once the Engine structure has been constructed, I do not want to change it) via command line.
How could I go about this? I've read around suggestions stating that I would have to use assembly to do this, others stating that this was not possible, but none of them giving a good explanation or references. Any help would be appreciated.
The solution doesn't need to be compatible with 32/64 bits machines, as the program this is written for is only for 64 bits machines.
For reference, the Java Code:
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager stdFileManager = compiler
.getStandardFileManager(null, Locale.getDefault(), null);
Iterable<? extends JavaFileObject> compilationUnits = null;
String[] compileOptions = new String[] { "-d", "bin" };
Iterable<String> compilationOptions = Arrays.asList(compileOptions);
SimpleJavaFileObject fileObject = new DynamicJavaSourceCodeObject(
"package.adress",getCode());
JavaFileObject javaFileObjects[] = new JavaFileObject[] { fileObject };
compilationUnits = Arrays.asList(javaFileObjects);
}
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
CompilationTask compilerTask = compiler.getTask(null, stdFileManager,
diagnostics, compilationOptions, null, compilationUnits);
boolean status = compilerTask.call();
if (!status) {// If compilation error occurs
/* Iterate through each compilation problem and print it */
String result = "";
for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
result = String.format("Error on line %d in %s",
diagnostic.getLineNumber(), diagnostic);
}
Exception e = new Exception(result);
throw e;
}
stdFileManager.close();// Close the file manager
/*
* Assuming that the Policy has been successfully compiled, create a new
* instance
*/
Class newEngine = Class
.forName("package.name");
Constructor[] constructor = newPolicy.getConstructors();
constructor[0].setAccessible(true);
etc.
}
In C all code must be compiled to native one before usage, so the only way for you is to use command line compiler to build code submitted by users. It may be GNU C++ compiler for example, or Visual C++ compiler (but for Visual C++ I don't know what about legal problems, is it permitted by license to do that).
So, first of all, select your compiler, probably GNU one.
Next, you can compile it as executable program or as DLL (assuming your software is for
Windows). If you decide to compile it to DLL, you have to use Win32 function LoadLibrary to load new built DLL into your process, and after that you can use GetProcAddress function to get method address and call it dynamically from C++ (you must implement a function wrapper and make it public in DLL).
If you decide to compile it as EXE file, you have to use CreateProcess function to run your code, send parameters via command line and receive data, may be, with pipe (see CreatePipe function), or may be with temporary file, or any other interprocess communication way available in Windows.
I think in your situation it is better to compile to EXE file, because in DLL if user code is buggy your main program may crash.

c console application auto-complete dynamic arguments

I am looking for a method of having console auto complete - such that given an application like:
int main (int argc, char ** argv)
{
if (argc == 1) return EXIT_FAILURE;
if (strcmp(argv[1], "good")==0) printf("good\n");
if (strcmp(argv[1], "bad")==0) printf("bad\n");
return EXIT_FAILURE;
}
When running it, I would like pressing [tab] after the command, such that it would give me one of the possible useful options.
Example:
./a.out g[tab]
would auto complete to
./a.out good
I don't want to edit /etc/bash-completion.d/, I was hoping for a much stronger auto-complete, something like a function in the executable itself that would be called - perhaps so it could query a database for the list of possible options. Or perhaps output a message letting you know what the options are.
If you think this is simply totally impossible, let me know!
Completions are a property of the shell you run the application from. You will have to provide completion functions for all the shells you want to support (bash, zsh, tcsh and fish have customizable completions). A completion function can call your application (e.g. run you_application --list-possible-arguments) or do whatever it chooses to generate the completions — it's already a “strong” completion in your terminology.
In bash, you declare completions with the complete built-in. Look in /etc/completion.d for examples (gpg is a fairly simple example; git is a rather involved one).
If you are using BASH then have a look at this similar post:
Auto-complete command line arguments
================================
If you want to provide your own command line then have a look at the Readline library:
The GNU Readline library provides a
set of functions for use by
applications that allow users to edit
command lines as they are typed in.
Both Emacs and vi editing modes are
available. The Readline library
includes additional functions to
maintain a list of previously-entered
command lines, to recall and perhaps
reedit those lines, and perform
csh-like history expansion on previous
commands.

Distributing loadable builtin bash modules

I've written a built-in for bash which modifies the 'cd' command, a requirement for my software. Is there a way to actually distribute a loadable independently of bash itself? I'd ideally like to distribute just a drop in "additional feature" because I know people can be put off by patching and compiling their shell from source code.
I want to time how long a user is in a directory so I can determine where they want to be. It's this functionality: http://github.com/joelthelion/autojump/tree/master rewritten as a bash builtin, for performance issues. This implementation uses $PROMPT_COMMAND to work but I wanted something integrated.
It is unclear what you have modified but in any case, bash (like at least ksh93 which IIRC introduced the concept and zsh) supports, using the enable -f file name syntax, loading built-in functions as external dynamically loaded modules.
These modules being plain files can certainly be distributed independently, as long as you make sure they are compatible with the target version/architecture. This was already true 5 years ago when you asked this question.
One issue in your case is there seems to be no documented way to overload a internal built-in like cd by a dynamically loaded one while keeping the ability to access the former.
A simple workaround would be to implement your customized cd with a different name, say mycd, like this:
int mycd_builtin(list)
WORD_LIST *list;
{
int rv;
rv=cd_builtin(list);
if(rv == EXECUTION_SUCCESS) {
char wd[PATH_MAX+1];
getcwd(wd,sizeof(wd));
// do your custom stuff knowing the new working directory
...
}
return (rv);
}
then to use an alias, or better, a shell function for your customized version to be used instead of the regular one:
cd() {
mycd "$#"
}
As long as your customization doesn't affect the behavior of the standard command and thus doesn't risk breaking scripts using it, there is nothing wrong in your approach.
Changing the built-in cd is a support nightmare for any admin and unwelcome to foreign users. What is wrong with naming it 'smart-cd' and letting the USER decide if they want the functionality by including it in their .bashrc or .profile? Then they can setup things however they want.
Also, using how long you've been in a directory is a pretty poor indication of preference. How would you distinguish between idling (a forgotten shell hanging in /tmp overnight), long-running scripts (nightly cron jobs), and actual activity.
There are a multitude of other methods for creating shortcuts to favorite directories: aliases, softlinks, $VARIABLES, scripts. It is arrogant of you to assume that your usage patterns will be welcomed by other users of your system.

Resources