How to capture keyboard in linux kernel module? - c

I am trying to write a Linux kernel module to work similar to a 'MouseKeys' application - to grab certain keys and then output mouse events. I'm currently struggling trying to grab input in such a way that I can discard or passthrough keys based on some internal state of my module - what I want is something like:
int keyboard_callback(event) {
if (active) {
processKey(event); // this would output mouse events
return BLOCK_KEY_EVENT; // input is not visible to any applications
}
// input would be processed downstream as normal
return PASSTHROUGH_KEY_EVENT;
}
....
register_input_handler(keyboard_callback);

Related

how to get bluetooth connected device data in read,write,notify methods using react-native-ble-plx or ble-manager

const monitorchars = DeviceManager.monitorCharacteristicForDevice(
device.id,
"00001800-0000-1000-8000-00805f9b34fb",
"00002a01-0000-1000-8000-00805f9b34fb",
function (err, result) {
if(err) {
console.log(err)
return;
}
console.log(result);
});
console.log(monitorchars)
I am using the react-native-ble-plx package.
I get read characteristics, read descriptors, and I am trying to write and notify props in react-native code. But, values are showing errors.
Can anyone can help to write and notify in ble-plx?
BleError: Characteristic X notify change failed for device Y and service Z
We are working on a device by using this we got services and their respective characteristics,among those characteristics we have one characteristic with notify property(isNotifiable:true,isNotifying:false) and a client characteristic configuration descriptor. now we want to make this isNotifying property value to true so that we can get some data stream from the device i refered this document ,in this there is a statement like "to set the notification value, we need to tell the sensor to enables us this notification mode. We will write to the characteristic’s descriptor to set the right value: Notify or Indicate." in ble-plx there is a method bleManager.writeDescriptorForDevice(), we tried using this but ended up with an error,
so,help me in getting the device data by making isNotifying property value true

Sending AT Commands via LabWindows code to SPBT2632C2A

I am trying to make a LabWindows/CVI program that talks with SPBT2632C2A Bluetooth chip. I am using a st eval spbt3atv3 dongle. I am trying to push a button and send a command to chip, but chip do not answer to me. I know my LabWindows program can receive messages from dongle, because every time I press reset button on dongle it shows me the boot up message. As I searched I need to add \n\r to end of my string, but it still didn't work. Chip does not send even error message.
CODE
int CVICALLBACK rasti (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
sprintf(discovery,"AT+AB discovery\n\r");
if(ComWrt (4,discovery, 18)!= 18){
SetCtrlVal (panelHandle, PANEL_TEXTBOX, "Nesekmingas duomenu siuntimas");
//Unsuccessful sending data
}
else {
SetCtrlVal (panelHandle, PANEL_TEXTBOX, discovery);
}
break;
}
return 0;
}
It's event called by button. It appends my discovery string to text box. So I think it's sending data correctly to serial port. But I think my string is wrong so I don't get the answer from chip.
Any ideas how to solve this to get responses from chip?
Make sure to call OpenComConfig with the correct parameters before invoking ComWrt. You can look for the correct port settings in the Hyperterminal (since you mentioned it communicates correctly with the device).
Refer to NI's documentation for more on this.
Also, trying different types of line termination characters might help (try using \r\n, \n or \r).

save display before calling a CDKpopup using C

Im trying to edit a program that capture user input. My intention is to add a function counting to a certain time. When it meet a timeout, a popup should be displayed. After that, the user should see back its previous shell/display. My problem is:
If the popup appear while the user are running vi, the display in that vi mode are hidden after the popup are destroyed.
Expectation: the user should have the same display as before the popup
Before the popup appear
After the popup destroyed
If the user are writing in a shell, it does not display the bash name (I do not know what its called. eg. fikrie#fikrie-VirtualBox:~/mysoftware/src$).
Expectation: the user should see the same display as before.
at the top line, the command (id) is displayed without bash name. I need to make the display same as the third line.
Im writing the code in C language and I'm using cdk library(ncurses) to do the popup. This is the function that create the popup.
int call_warning(struct tm *warning_display)
{
char *mesg[4];
WINDOW *cursesWin;
CDKSCREEN *cdkscreen;
char *buttons[1] = { "[ OK ]" };
char enddatetime[1024];
strftime(enddatetime, sizeof(enddatetime), "%d-%m-%Y %I:%M:%S%p", warning_display);
mesg[0] = "<C>----------------------";
mesg[1] = "<C>Your timeout is near";
mesg[2] = enddatetime;
mesg[3] = "<C>----------------------";
//I have tried using def_shell_mode() here
cursesWin = initscr();
cdkscreen = initCDKScreen(cursesWin);
initCDKColor();
while (1) {
popupDialogWithColor(cdkscreen, mesg, 4, buttons, 1, "</2>");
destroyCDKScreen(cdkscreen);
werase(cursesWin);
wrefresh(cursesWin);
endCDK();
endwin();
break;
}
//I tried using reset_shell_mode() here and removing system("clear");
system("clear"); //tried using refresh() as well. Didnt do the job
return 0;
}
From what i read in the manual guide, the def_shell_mode() will save the prior terminal settings so they can be restored during the call to endwin(). I tested using that but it doesnt solve my problem. I did google on how to clear screen using C. From the result, its either use curse or termios. I dont think termios is a good approach because I want to let the user continue its previous session and not making a new one. So my only option is curses. Is there any function in ncurses that can do this?
EDIT: After a few days trying, I found out CDK is the one causing problem. So I change my code to use curses function instead. The idea to do it is here.
How do you clear console screen in C?
How to clear screen from simple C program?

Dependency reset design pattern (embedded C)

Folks, I am working on an embedded system in C that uses an I2C module. Other modules that use the I2C have the I2C module as a dependency, or "child system". They pass it message context structs and when the I2C module is ready it asynchronously sends or reads messages as the context instructs, running a callback from the context to let the overlying module know the work has completed, successfully or with error.
I would like the I2C module to be able to recover itself from errors i.e. reset external connected hardware, itself and then reinitialise the external devices. the former two of these three actions can be handled internal to the I2C module with no problem, however the I2C module doesn't know what overlying modules may want to use it and need to send data to external devices to configure them again after their earlier hardware reset.
The message contexts could have callbacks to reinitialisation functions, but the I2C module only holds a reference to one of these at a time, the one it is currently working on how would it know to call the reinitialise handle for the next time a different module (with reset hardware) wants to talk to it's external hardware? the overlying module doesn't know the I2C has been reset, and the I2C module doesn't know if the overlying module has performed its reinitialisation sequence or not, or even if it needs to.
Is there a common design pattern to ameliorate such a problem?
To illustrate differently:
Normal initialisation of I2C and external hardware sequence:
initExtIOExpander() {
context_t initialisationMsg = {/*...*/};
i2cSend(&initialisationMsg )
}
initExtADC() {
context_t initialisationMsg = {/*...*/};
i2cSend(&initialisationMsg )
}
i2cSend() {
// Start sending data using passed context,
}
interrupt i2c_isr() {
//If the message completed run the callback handle in the current context
// else do the next part of the message, etc.
}
main () {
//...
initI2c();
initExtIOExpander();
initExtADC();
//...
// Use external devices here.
//
}
Reset and reinitialisation sequence:
If the normal sequence from above has already happened but now the I2C ISR is altered to detects errors:
interrupt i2c_isr () {
//If there was an error reset external H/W, and own H/W
// but how to know to call initExtIOExpander() and initExtADC()??
// this module might know about the ADC if it is processing and ADC
// message but it has "forgotten" about the IO expander or any other
// device
// else if the message completed run the callback handle in the current
// context
// else do the next part of the message, etc.
}
Thanks!
What about to put all your devies initialisation to 'aggregate' function and call it when need?
void init_all_i2c_devices(void) {
initExtIOExpander();
initExtADC();
...
}
interrupt i2c() {
....
init_all_i2c_devices();// or i2c_init_callback()
}
main() {
init_i2c();
init_all_i2c_devices();
// set_i2c_init_callback(init_all_i2c_devices) if need
}
For design patterns in the embedded context take a look here and here.
Especially the watchdog timer fits your case.
I think it is not the I2C module which should reset the HW.

WPF - How to model a loop operation in the UI

I am creating a quick'n'dirty utility that will enable editing of data read sequentially from a set of files. Here's a very simplified explanation of what will happen in a single iteration of a loop:
Read a line from the input stream
Parse it and use the parsed results to populate form controls
Allow user editing and await a confirmation button click
Retrieve the updated form control values and write to the output stream
What I can't figure out is how to integrate the processing loop with the event-driven UI. For example, how do I suspend operation of the loop while waiting for user input.
I understand that this is possible by launching the loop operation on its own thread and writing code to manage its interaction with the UI thread, but I am wondering if there is a simpler approach that works out of the box.
Thanks for any ideas you may be able to offer.
Tim
I can think of 2 approaches to do that:
The easiest is probably to use a modal dialog: when your worker thread needs input from the user, display a dialog, which is a blocking operation. Something like that:
// Worker thread loop
while(...)
{
...
// prompt user for data (invoke synchronously on UI thread)
UserData data = (UserData)window.Invoke(PromptUserData);
...
}
...
UserData PromptUserData()
{
UserInputDialog dlg = new UserInputDialog();
dlg.ShowDialog();
return dlg.UserData;
}
The other option, if you don't want to use a modal dialog, is to use a wait handle to synchronize the worker thread and the UI:
private readonly AutoResetWaitHandle _userInputWaitHandle = new AutoResetWaitHandle(false);
...
// Worker thread loop
while(...)
{
...
// Setup the UI to allow user input
window.Invoke(SetupUIForInput);
// Wait for the input to be validated
_userInputWaitHandle.WaitOne();
...
}
...
void SetupUIForInput()
{
// Enable the UI
inputForm.Enabled = true;
// Whatever else you need to do...
...
}
void buttonOK_Click(object sender, EventArgs e)
{
// Signal the worker thread to continue
_userInputWaitHandle.Set();
}
You wouldn't really implement a loop in the narrow sense.
You would do something like this (pseudo code):
OnStartOperationButtonClick()
{
if(!ReadLine())
return;
ParseLineAndPopulateControls();
ShowEditingControls();
}
OnConfirmationButtonClick()
{
GetControlValuesAndWriteToOutputStream();
if(!ReadLine())
{
HideEditingControls();
return;
}
ParseLineAndPopulateControls();
}
No need for a separate thread, given that reading and parsing the line doesn't take long.

Resources