From: Matthew B. <mm...@gm...> - 2024-06-25 14:18:42
|
Hello! I am writing a Tcl extension in C to wrap a driver for a USB LED status light. The extension runs fine from tclsh but there are issues running it from a Tcl page under NaviServer. The extension implements one command via Tcl_CreateObjCommand. The command proc implements a number of sub-commands broken into two categories: device-independent functionality, e.g. count the number of status lights that are plugged in, and device-dependent functionality, e.g. attach to a status light, display a particular color, etc The device-independent sub-commands work when run in a Tcl page. For example, this page runs fine when served from NaviServer: package require blink set n [blink enumerate] ns_return 200 text/html " There are <strong>$n</strong> devices plugged in. " In order to have a device display a color, etc., you first have to run the "open" command. And that's not working. So here's an example Tcl page: package require blink set d [blink open 0] set q [ns_conn query] set s [ns_parsequery $q] set red [ns_set get $s red 255] set green [ns_set get $s green 0] set blue [ns_set get $s blue 0] blink set $d $red $green $blue ns_return 200 text/html " <p>Query: [ns_conn query]</p> <ul> <li>Red: $red</li> <li>Green: $green</li> <li>Blue: $blue</li> </ul> " When this page is served, I get an error in the line: set d [blink open 0]. There is a C struct, Blinker, that contains data about the attached device. My C code first allocates memory for the struct blinkPtr = (Blinker *)Tcl_Alloc(sizeof(Blinker)); and this seems to work -- blinkPtr is non-NULL after the assignment. Then there is a function from the driver library that actually attaches to the device: blinkPtr->device = blink1_openById(devid); It should return the memory address of an allocated structure that's part of the USB library. But the function returns NULL. (Just to reiterate, this works fine when run from tclsh and returns a non-NULL value.) I'm assuming there is some sort of memory protection or similar that I need to take into account. Any suggestions on what to look at would be greatly appreciated. Thanks, Matt P.S. This is on macOS 14.3. I'm compiling the extension as follows: clang -dynamiclib -DUSE_TCL_STUBS -I/usr/local/ns/include -L/usr/local/ns/lib -ltclstub8.6 -o blink.dylib blink.c -L/usr/local/lib -lBlink1 |