No scan result using iwinfo library - c

I'm new to OpenWrt. I am using iwinfo lib to scan wifi. It was working fine until I scan multiple time in my code. At first I think it is because of I am doing wrong using this library, so I tried to modified the cli program from iwinfo library and make it scan twice. Here is the main function of the cli code, which will work only once. The print_scanlist function implementation is same it in the cli program.
int main() {
const struct iwinfo_ops *iw = iwinfo_backend("wlan0");
print_scanlist(iw, "wlan0");
print_scanlist(iw, "wlan0");
iwinfo_finish();
return 0;
}
At first I think it was because the hardware does not allowed to scan in short amount of time. But if I run the program second time it still work for the first function. Now I have no idea why is this happenging, does anyone know why?

I`m almost sure one of these options (maybe even both) will work:
int main() {
const struct iwinfo_ops *iw = iwinfo_backend("wlan0");
print_scanlist(iw, "wlan0");
iwinfo_finish();
print_scanlist(iw, "wlan0");
iwinfo_finish();
return 0;
}
int main() {
print_scanlist(iwinfo_backend("wlan0"), "wlan0");
iwinfo_finish();
print_scanlist(iwinfo_backend("wlan0"), "wlan0");
iwinfo_finish();
return 0;
}
It`s important to understand that iw is not a handle of some sort, it is just a pointer to a list of functions stored in the backend library. The list is hard-coded and it cannot be modified at runtime.

Since I am using nl80211 as backend, I dug into the source code of iwinfo. I found that it is using WPA supplicant. In the scan function, it called a function name nl80211_get_scanlist_wpactl and it's purpose is to connect to the WPA supplicant and ask it to scan and get its result.
The steps in iwinfo are
send(sock, "ATTACH", 6, 0);
send(sock, "SCAN", 4, 0);
send(sock, "SCAN_RESULTS", 12, 0);
The problem is iwinfo forgot to call a DETACH therefore the next time you wont able to do anything. So after I add a send(sock, "DETACH", 6, 0), I got it to work. Thanks everybody

Related

gtk filechooser with msys2 return not correct values

in my project i compiled a gtk-gui with msys2. I think everything works fine except for one thing.
If i start my application and try to choose a file with the filechooser i recognized that in the first attempt the return value of the
gtk_file_chooser_get_current_folder() is NULL. In the second try the return value seems to be correct but the last folder is missing.
Compiling and running it under linux leads to no problem. I use glade in combination with gtk.
I'm not sure what kind of code snipped i should post here, but if you want to see a specific thing i will upload it of course.
I would be very pleased about some suggestions.
At first i initialize the FileChooserWidget:
WCO_GUI_Ref()->MyFileChosserButton1 = GTK_WIDGET(gtk_builder_get_object(WCO_GUI_Ref()->MyBuilder, "MyFileChosserButton1"));
If you choose a folder with the FileChooser a function is called which stores the return value and print it out:
WCO_PDF_SetFoldername(WCO_ENTRY(WCO_GUI_Get(folder_name)));
The WCO_GUI_Get() function returns a void pointer which is casted by the WCO_ENTRY() macro. In this function the i call gtk_file_chooser_get_current_folder() like this:
void *WCO_GUI_Get(int id)
{
void *ret;
switch(id)
{
case 0: ret = gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(WCO_GUI_Ref()->MyFileChosserButton1)); break;
default: break;
}
return ret;
}
If i place this debug code:
printf("%s", WCO_ENTRY(WCO_GUI_Get(folder_name)) );
The Output of it in the first try is (null) and after the first one i got a output like this C:\usr\documents\ but the actual selected folder is C:\usr\documents\test\

Can't get HTML text using libtidy

everybody - long time listener, first time caller.
I've been playing around with libtidy in C on macOS 10.13. I started with the sample code here and modified it to read a local html file instead of using curl. Everything seems to work okay except for text. It will find and output every tag in my test file, but does not seem able to get the text at all, and it's driving me nuts.
The code in question occurs in the DumpNode tree-walking function. My hacked-up version:
#include <stdio.h>
#include <tidy.h>
#include <tidybuffio.h>
/* Wrapper functions for file i/o */
int w_getc(void* ptr)
{
return getc((FILE *)ptr);
}
void w_ungetc(void *ptr, unsigned char bv)
{
ungetc((int)bv, (FILE *)ptr);
}
Bool w_feof(void *ptr)
{
return (Bool)feof((FILE *)ptr);
}
/* Traverse the document tree */
void dumpNode(TidyDoc doc, TidyNode tnod, int indent)
{
TidyNode child;
for(child = tidyGetChild(tnod); child; child = tidyGetNext(child) ) {
ctmbstr name = tidyNodeGetName(child);
if (!name) {
/* if it doesn't have a name, then it's probably text, cdata, etc... */
TidyBuffer buf;
tidyBufInit(&buf);
if (tidyNodeHasText(doc, child) && tidyNodeGetText(doc, child, &buf)) {
printf("%u, %u, %u\n", buf.size, buf.allocated, buf.next);
printf("%*.*s\n", indent, indent, (buf.bp && buf.size > 0)?(char *)buf.bp:"");
}
tidyBufFree(&buf);
}
dumpNode(doc, child, indent + 4); /* recursive */
}
}
int main(int argc, char **argv)
{
if(argc == 2) {
TidyDoc tdoc;
int err;
FILE *fp;
TidyInputSource insrc;
tdoc = tidyCreate();
fp = fopen(argv[1], "r");
if (!fp) return -1;
if (tidyInitSource(&insrc, fp, &w_getc, &w_ungetc, &w_feof)) {
err = tidyParseSource(tdoc, &insrc); /* parse the input */
if(err >= 0) dumpNode(tdoc, tidyGetRoot(tdoc), 0); /* walk the tree */
}
/* clean-up */
fclose(fp);
tidyRelease(tdoc);
return err;
}
return 0;
}
And my compiler string: gcc -o TidyExample tidyexample.c -ltidy -DENABLE_DEBUG_LOG -DDEBUG_PPRINT -DDEBUG_INDENT
Here's what I've deduced so far:
When encountering a text node, tidyNodeHasText(doc, child); and tidyNodeGetText(doc, child, &buf); both return yes.
That tells me that tidyNodeGetText is calling the pretty print functions like it should. I've verified that neither TidyXmlOut or TidyXhtmlOut are set for doc, so TY_(PPrintTree) should fire.
Since I use -DENABLE_DEBUG_LOG and -DDEBUG_PPRINT, TY_(PPrintTree) should call dbg_show_node, but it doesn't appear to. This could be because the node == NULL condition prompted an immediate return, but I asserted the existence of the node, and tidyNodeGetText would return no if it didn't exist, so that can't be what's happening.
I also tried to set a progress callback to get an better view of what was going on in there, but weirdly, the linker didn't recognize the symbol _tidySetPrettyPrinterCallback.
ETA: I figured this out; there's a second linked library needed for this to work: -ltidys. I can now get pretty printer progress.
The only output the above snippet generates is 0, 0, 0\n from the first printf statement, and \n from the second.
Incidentally, that curl sample code I ripped off? It has the same problem. If you run it, you get a segfault as soon as it hits any text, because it doesn't check whether there's anything in the buffer before it calls printf.
I am out of ideas. Either I'm doing something majorly wrong (likely), or there's a good-sized bug in libtidy (less likely, but possible).
ETA: Here's a teeny-tiny HTML file, that when you invoke TidyExample minimal.html results in the usual empty buffers:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<p>This is text.</p>
</body>
</html>
Okay, I've found a "solution" of sorts. It gets the job done, but I have no idea why.
So, after discovering -ltidys, I was playing around with setting pretty print callbacks, and I discovered that if I set one, the output would be what I expected... even if I didn't actually set a callback!
Seriously, all I have to do is insert the line tidySetPrettyPrinterCallback(tdoc, NULL);, and the buffers fill up and print like they should. Comment it out, and it stops working.
I've investigated a few of the other functions that link with libtidys.a, and they seem to have the same effect. I haven't done any rigorous experimentation, though.
If anyone has any insight into what might be causing this, I'd be interested to know, for my own knowledge's sake. But I'm not going to probe any further. Since I've found a practical workaround for my problem, I'm going to let this be for now, and work on the actual project I was trying to use Tidy for.

Calling Chicken Scheme function from SDL2 audio callback function hangs

I am trying to embed Chicken Scheme into a C program, to generate sounds to be played with SDL2's audio system. I would have liked to use the sdl2 egg, but it does not seem to support Audio yet (despite the documentation mentioning the 'audio flag for the init! function).
At first, I was using SDL_QueueAudio from C, passing it a buffer that I had allocated in C and then filled in Scheme. This worked fine, passing a Sint16 * and size_t into Scheme, then using pointer-s16-set! from Scheme to fill it and returning a size_t to note how many cells were filled.
Then, when I realised that using the callback api for generating the audio was much better suited to this, I tried switching to it (having already used it before in C), only for the Scheme function to never be entered. Logging something in the callback function before the Scheme call worked, but logging directly within the Scheme function, or after the Scheme call, never happened.
I can only imagine that this is due to SDL2's audio callback running on a separate thread, and that messing with calling through to Scheme somehow. With this in mind, I tried calling CHICKEN_run(C_toplevel); from within the callback function, the first time that it was called, but that only resulted in a bus error.
So my question is: is there a way of calling embedded Chicken Scheme from SDL2's audio callback?
I am on macOs 10.13.6 High Sierra, with SDL2 and chicken both installed and up-to-date through Homebrew.
I compile with (as I said, this works fine when using the queue audio api):
csc code.c codescm.scm -embedded -o code -L -lSDL2
My simplified code is below:
#include <chicken.h>
#include "SDL2/SDL.h"
extern size_t fill_sound_buffer(Sint16 *buffer, size_t buffer_length);
void fill_sound_callback(void *user_data, Uint8 *stream, int stream_length)
{
// Logging here prints to the console
fill_sound_buffer((Sint16 *)stream, stream_length / 2);
// Logging here does not print to the console
}
void play(void)
{
SDL_AudioSpec audio_want;
SDL_zero(audio_want);
audio_want.freq = 44100;
audio_want.format = AUDIO_S16SYS;
audio_want.channels = 1;
audio_want.samples = 2048;
audio_want.callback = fill_sound_callback;
SDL_AudioSpec audio_have;
SDL_AudioDeviceID audio_device = SDL_OpenAudioDevice(NULL, 0, &audio_want, &audio_have, 0);
SDL_PauseAudioDevice(audio_device, 0);
SDL_Delay(5000);
// Logging here shows up after 5 seconds, but the program then continues to wait
SDL_CloseAudioDevice(audio_device);
}
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_AUDIO);
CHICKEN_run(C_toplevel);
play();
SDL_Quit();
return 0;
}
(import (chicken format)
(chicken foreign)
(chicken memory)
(chicken platform))
(define-external (fill_sound_buffer ((c-pointer short) buffer) (size_t buffer_length)) size_t
; This never prints when using the callback api
(printf "In Scheme~%")
; Removed the code that calculates a sine wave and fills the buffer with it, which works
0)
(return-to-host)

C - a certain function in my program wont work with either returning an integer nor using a pointer

This is my first time posting of this forum and I'm doing is just because of this problem. I've been working on a program for a while(just for fun) and to make things simple for my self I used loads of global variables, but now I've been trying to make the individual functions more independent and flexible. A certain function is giving a a lot of issues for some reason.
int which_move(int ac,int bc,int cc){
int illcheck;
int ill_done;
int ill_pos;
int true_move;
true_move=3;
ill_done=-1;
for(u=6;u>=0;u--){
ill_pos=ert-1;
illcheck=0;
for(y=0;y<ill_len[u];y++){
if(buff[ill_pos]==ill_move[u][y]){
++illcheck;
if(ill_pos==0)
ill_pos=100;
--ill_pos;
if(illcheck==ill_len[u]){
ill_done=u;
break;
}
}
else
break;
}
if(ill_done!=-1)
break;
}
if(ac==1||ill_done==1||ill_done==2||ill_done==6)
true_move=0;
if(bc>2)
true_move=1;
if(cc>2)
true_move=2;
if(ill_done==0||ill_done==3||ill_done==4)
true_move=4;
if(ill_done==5)
true_move=5;
return true_move;
}
and this is how i call the function:
int open_move;
open_move=which_move(acheck,bcheck,ccheck);
and open_move never match true_move.
I've tried to convert to something like this
int which_move(int *true_move,int ac,int bc,int cc)
and remove int true_move; and the return of return true_move; and implement the function like this:
int open_move;
which_move(open_move,acheck,bcheck,ccheck);
still i get it to work.
I've googled til chrome starts lagging because of too many tabs open and tried every trick I can find, but I'm not getting any wiser. Please help me with what I'm doing wrong.
Thanks from a hobbyist.

Clang sqlite3 float error

Any ideas on how to track down this error would be appreciated.
I have some c code that runs in two or more processes. The first process listens in on a message queue and saves the resulting struct to a database. The remaining processes query one or more serial devices and pass this information through the message queue to the first process to be stored in the database.
It all works great except the following. One of the structs I am using contains a float. This struct gets sent through the queue and decoded correctly however when binding the value using sqlite3_bind_double() the resulting value in the database is 0. Placing a printf() statement around the sqlite3_bind_double() statement causes the code to work and place the correct value in the database.
But even more interesting is if I remove the printf() statement and compile the program with gcc the code works.
Any help would be great. Thanks in advance.
Code:
int
add_inverter_stat(sqlite3 *db_conn, struct inverter_stat const *istat
,int *sqlite3_err)
{
sqlite3_stmt *stmt = NULL;
*sqlite3_err = sqlite3_prepare_v2(db_conn, SQL_INSERT_INVERTER_STAT, -1
,&stmt, NULL);
*sqlite3_err = sqlite3_bind_int(stmt, 1, istat->stat_id);
*sqlite3_err = sqlite3_bind_text(stmt, 2, istat->serial_no, -1, NULL);
*sqlite3_err = sqlite3_bind_int64(stmt, 3, istat->time_taken);
*sqlite3_err = sqlite3_bind_double(stmt, 4, (double)istat->value);
*sqlite3_err = sqlite3_step(stmt);
sqlite3_finalize(stmt);
return 1;
}
That's the sort of thing I would expect from something trying to read a four-byte float as an eight-byte double, with the other four bytes being ... well, whatever. This really shouldn't work, but if you put an explicit cast (double)flt_var as the third parameter, does that help?

Resources