Program Crashing if Using PHP to Read FIFO - Raspberry Pi Forums
i somehow broke code tags in first post of thread. copied post reply , code tags worked no changes. please see original post copied response below.
first, need preface question stating on vacation , can access forum semi-reliably in morning, before wife wakes , tells me form of fun going beach... not "working" on pi. may able check on phone throughout day, responses method short , perhaps not full.
i'm having issue php script , reading fifos program i'm writing. here how create fifos: ssh terminal application running, can perform following , fifos appear work desired: start cat read output program echo command input fifo. program writes "1\n" indicate command successful , temperature of channels followed \n. here code generates output:
, here code handles fifos: 2 service functions run in own thread, execution should block when file fifo opened until php opens file, or @ least think blocks. testing seemed indicate it, why put them own threads. further, input service routine should block on fgetc call until character available.
of working when issuing commands @ ssh terminal , using cat print output. problem i'm experiencing if use php read/write fifos, c program stops @ point after php script reads response.
basically, index.html file has button click. when click button, php script executed parameter get_all_temps. php script appends \n , passes fifo. program receives data , returns "1\n" , temperature string followed "\n" in output fifo. php script reads 1 fifo reads temperature data. web page updated temperature data desired, program stops execution , returns console.
secondary problem php can't open files unless issue command "chmod 777 /tmp/*" @ terminal, despite application trying create them 777. i'm not sure issue though once i've completed program plan in having www-user (can't remember actual name of user) starts application. should result in correct user being owner of files.
i'm having issue php script , reading fifos program i'm writing. here how create fifos:
code: select all
#define smpi_inpfifo "/tmp/smpiinp" #define smpi_outfifo "/tmp/smpiout" #define smpi_errfifo "/tmp/smpierr" int file_fifo_init( void ) { int result = 0; // attempt remove files in case present remove(smpi_inpfifo); remove(smpi_outfifo); remove(smpi_errfifo); // create fifos if (mkfifo(smpi_inpfifo, 0777) || mkfifo(smpi_outfifo, 0777) || mkfifo(smpi_errfifo, 0777)) result = -1; else result = 1; return result; }
code: select all
pi@smokinpi ~ $ cat /tmp/smpiout & [1] 9034 pi@smokinpi ~ $ echo get_all_temps > /tmp/smpiinp pi@smokinpi ~ $ 1 34.24,34.24,34.24,34.24,34.24,34.24,34.24,34.24,34.24,34.24
code: select all
static void file_fifo_get_all_probe_temps( void ) { char buffer[150]; int channel; strcpy(buffer, "1\n"); pthread_mutex_lock(&mutex); for (channel = 0; channel < nbr_of_thermistors; channel++) sprintf(buffer, "%s%4.2f,", buffer, gp_shared_data->temp_deg_f[channel]); pthread_mutex_unlock(&mutex); // change ',' @ end of string '\n' buffer[strlen(buffer)-1] = '\n'; file_fifo_respond(buffer); }
code: select all
void file_fifo_service_input( void *shared_data_address ) { int rd_index; char ch; gp_shared_data = (shared_data_type*)shared_data_address; signal(sigint, file_fifo_signal_handler); while (1) { g_fifo_input = fopen(smpi_inpfifo, "r"); rd_index = 0; g_in_buffer[0] = 0; do { ch = fgetc(g_fifo_input); g_in_buffer[rd_index++] = ch; } while ((ch != '\n') && (rd_index < sizeof(g_in_buffer))); file_fifo_process_cmd( g_in_buffer ); fclose(g_fifo_input); } } void file_fifo_service_output( void *shared_data_address ) { int i; gp_shared_data = (shared_data_type*)shared_data_address; signal(sigint, file_fifo_signal_handler); while (1) { g_fifo_output = fopen(smpi_outfifo, "w"); while (g_fifo_output) { if (g_write_data_bytes > 0) { i = 0; while (i < g_write_data_bytes) fputc(g_out_buffer[i++], g_fifo_output); fflush(g_fifo_output); g_write_data_bytes = 0; } else usleep(1000); } } } void file_fifo_respond( char* presponse ) { strcpy(g_out_buffer, presponse); g_write_data_bytes = strlen(presponse); }
of working when issuing commands @ ssh terminal , using cat print output. problem i'm experiencing if use php read/write fifos, c program stops @ point after php script reads response.
basically, index.html file has button click. when click button, php script executed parameter get_all_temps. php script appends \n , passes fifo. program receives data , returns "1\n" , temperature string followed "\n" in output fifo. php script reads 1 fifo reads temperature data. web page updated temperature data desired, program stops execution , returns console.
code: select all
<!doctype html> <html> <head> <script> function query_device(str) { var xmlhttp; if (window.xmlhttprequest) { // code ie7+, firefox, chrome, opera, safari xmlhttp=new xmlhttprequest(); } else { // code ie6, ie5 xmlhttp=new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4 && xmlhttp.status==200) { document.getelementbyid("mydiv").innerhtml=xmlhttp.responsetext; } } xmlhttp.open("get","smpi_query.php?q="+str,true); xmlhttp.send(); } </script> </head> <body> <div id="mydiv"><h2>let ajax change text</h2></div> <button type="button" onclick="query_device('get_all_temps')">get temps</button> </body> </html>
code: select all
<?php $query = $_request["q"]; $query .= "\n"; $writefile = fopen("/tmp/smpiinp", "w") or die("unable open file writing"); $bytes_written = fwrite($writefile, $query); fclose($writefile); $readfile = fopen("/tmp/smpiout", "r") or die ("unable open file reading"); $response = fgets( $readfile ); if ($response > 0) $response = fgets( $readfile ); fclose($readfile); echo $response; ?>
secondary problem php can't open files unless issue command "chmod 777 /tmp/*" @ terminal, despite application trying create them 777. i'm not sure issue though once i've completed program plan in having www-user (can't remember actual name of user) starts application. should result in correct user being owner of files.
code: select all
pi@smokinpi ~/projects/smokinpi $ ls -l /tmp total 0 prwxr-xr-x 1 pi pi 0 jul 13 12:54 smpierr prwxr-xr-x 1 pi pi 0 jul 13 12:54 smpiinp prwxr-xr-x 1 pi pi 0 jul 13 12:54 smpiout pi@smokinpi ~/projects/smokinpi $ chmod 777 /tmp/* pi@smokinpi ~/projects/smokinpi $ ls -l /tmp total 0 prwxrwxrwx 1 pi pi 0 jul 13 12:54 smpierr prwxrwxrwx 1 pi pi 0 jul 13 12:54 smpiinp prwxrwxrwx 1 pi pi 0 jul 13 12:54 smpiout pi@smokinpi ~/projects/smokinpi $
raspberrypi
Comments
Post a Comment