Various bugfixes

This commit is contained in:
dcp1990 2005-06-28 00:52:17 +00:00
parent 0a845ce0b3
commit 9cb193a306
3 changed files with 30 additions and 13 deletions

View File

@ -67,8 +67,11 @@ int main(argc, argv)
cf.loglevels = LL_ALL; cf.loglevels = LL_ALL;
pthread_mutex_unlock(&cfmx); pthread_mutex_unlock(&cfmx);
initialize(); initialize();
lprintf(debug, "INIT IS 0x%x", PTHREAD_MUTEX_INITIALIZER);
pthread_create(&networkth, NULL, network, NULL); pthread_create(&networkth, NULL, network, NULL);
lprintf(debug, "Started network with 0x%x.", networkth);
pthread_create(&modemth, NULL, modem_io, NULL); pthread_create(&modemth, NULL, modem_io, NULL);
lprintf(debug, "Started modemth with 0x%x.", modemth);
pthread_create(&mainth, NULL, mainthf, NULL); pthread_create(&mainth, NULL, mainthf, NULL);
pthread_exit(NULL); pthread_exit(NULL);
return 0; return 0;

View File

@ -56,10 +56,15 @@ short doing_cid = 0;
extern struct conf cf; extern struct conf cf;
pthread_t modemth; pthread_t modemth;
pthread_mutex_t modemmx = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t modemmx = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t miomx = PTHREAD_MUTEX_INITIALIZER; /* is modem_io() running? */ /* pthread_mutex_t modemiomx = PTHREAD_MUTEX_INITIALIZER; *//* is modem_io() running? */
pthread_mutex_t mpipemx = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t mpipemx = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t buffermx = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t buffermx = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t mpcond = PTHREAD_COND_INITIALIZER; pthread_cond_t mpcond = PTHREAD_COND_INITIALIZER;
pthread_t chie;
short miorunning = 0;
extern pthread_t networkth, modemth;
#define mylock(s) lprintf(debug, "%s (0x%x) owns it", pthread_equal(chie, networkth) ? "network" : (pthread_equal(chie, modemth) ? "modem" : "other/main"), chie); \
pthread_mutex_lock(s); chie = pthread_self(); lprintf(debug, "mutex acq");
int modempipes[2]; int modempipes[2];
extern pthread_mutex_t cfmx; extern pthread_mutex_t cfmx;
modem_t* mo; modem_t* mo;
@ -67,7 +72,7 @@ extern modem_t rockwell;
void stmod(str) void stmod(str)
const char* str; const char* str;
{ {
if(pthread_mutex_trylock(&modemmx) != 0 && pthread_mutex_trylock(&miomx) != 0) { if(pthread_mutex_trylock(&modemmx) != 0 && miorunning) {
pthread_mutex_lock(&mpipemx); pthread_mutex_lock(&mpipemx);
write(modempipes[1], "G", 1); write(modempipes[1], "G", 1);
pthread_mutex_unlock(&mpipemx); pthread_mutex_unlock(&mpipemx);
@ -88,7 +93,7 @@ int dialogue_with_modem(cback, arg)
void *arg; void *arg;
{ {
int rc; int rc;
if(pthread_mutex_trylock(&modemmx) != 0 && pthread_mutex_trylock(&miomx) != 0) { if(pthread_mutex_trylock(&modemmx) != 0 && miorunning) {
pthread_mutex_lock(&mpipemx); pthread_mutex_lock(&mpipemx);
write(modempipes[1], "G", 1); write(modempipes[1], "G", 1);
pthread_mutex_unlock(&mpipemx); pthread_mutex_unlock(&mpipemx);
@ -109,7 +114,7 @@ char *sendwr(str, bufferback, howmuch)
size_t howmuch; size_t howmuch;
{ {
struct pollfd fds[1]; struct pollfd fds[1];
if(pthread_mutex_trylock(&modemmx) != 0 && pthread_mutex_trylock(&miomx) != 0) { if(pthread_mutex_trylock(&modemmx) != 0 && miorunning) {
pthread_mutex_lock(&mpipemx); pthread_mutex_lock(&mpipemx);
write(modempipes[1], "G", 1); write(modempipes[1], "G", 1);
pthread_mutex_unlock(&mpipemx); pthread_mutex_unlock(&mpipemx);
@ -118,6 +123,7 @@ char *sendwr(str, bufferback, howmuch)
write(modemfd, "\r\n", 3); write(modemfd, "\r\n", 3);
fds[0].fd = modemfd; fds[0].fd = modemfd;
fds[0].events = POLLRDNORM; fds[0].events = POLLRDNORM;
fcntl(modemfd, F_SETFL, O_NONBLOCK);
switch(poll(fds, 1, 3000)) { switch(poll(fds, 1, 3000)) {
case 0: case 0:
pthread_cond_signal(&mpcond); pthread_cond_signal(&mpcond);
@ -130,7 +136,8 @@ char *sendwr(str, bufferback, howmuch)
pthread_mutex_unlock(&modemmx); pthread_mutex_unlock(&modemmx);
return bufferback; return bufferback;
default: default:
fgets(bufferback, howmuch, modem); /* fgets(bufferback, howmuch, modem); */
read(modemfd, bufferback, howmuch);
break; break;
} }
pthread_cond_signal(&mpcond); pthread_cond_signal(&mpcond);
@ -146,7 +153,7 @@ char *sendwr(str, bufferback, howmuch)
} }
void modem_wake(void) void modem_wake(void)
{ {
if(pthread_mutex_trylock(&miomx) != 0) { if(miorunning) {
pthread_mutex_lock(&mpipemx); pthread_mutex_lock(&mpipemx);
write(modempipes[1], "D", 1); write(modempipes[1], "D", 1);
pthread_mutex_unlock(&mpipemx); pthread_mutex_unlock(&mpipemx);
@ -211,6 +218,7 @@ int init_modem(char* dev)
pthread_mutex_unlock(&modemmx); pthread_mutex_unlock(&modemmx);
return -3; return -3;
} }
fcntl(modemfd, F_SETFL, O_NONBLOCK);
mo = &rockwell; mo = &rockwell;
mo->init(); mo->init();
voice_init(); voice_init();
@ -267,17 +275,18 @@ void *modem_io(k)
*cbuf = '\0'; cbuf[1] = '\0'; *cbuf = '\0'; cbuf[1] = '\0';
fillset(); fillset();
pthread_mutex_lock(&modemmx); pthread_mutex_lock(&modemmx);
pthread_mutex_lock(&miomx); miorunning = 0x1;
for(;;) { for(;;) {
FD_ZERO(&fds); FD_ZERO(&fds);
FD_SET(modemfd, &fds); FD_SET(modemfd, &fds);
FD_SET(modempipes[0], &fds); FD_SET(modempipes[0], &fds);
/* tv.tv_sec = 2; tunable */ /* tv.tv_sec = 2; tunable */
/* tv.tv_usec = 0; */ /* tv.tv_usec = 0; */
switch(select(modempipes[0] + 1, &fds, NULL, NULL, /* dotm ? &tv :*/ NULL)) { switch(select((modempipes[0] > modemfd ? modempipes[0] : modemfd) + 1, &fds, NULL, NULL, /* dotm ? &tv :*/ NULL)) {
case -1: case -1:
lprintf(error, "select on modem: %s", strerror(errno)); lprintf(error, "select on modem: %s", strerror(errno));
pthread_mutex_unlock(&modemmx); pthread_mutex_unlock(&modemmx);
miorunning = 0;
pthread_exit(NULL); pthread_exit(NULL);
break; break;
case 0: case 0:
@ -296,8 +305,10 @@ void *modem_io(k)
} }
if(FD_ISSET(modempipes[0], &fds) != 0) { if(FD_ISSET(modempipes[0], &fds) != 0) {
read(modempipes[0], cbuf, 1); read(modempipes[0], cbuf, 1);
if(*cbuf == 'G') pthread_cond_wait(&mpcond, &modemmx); else { if(*cbuf == 'G') {
pthread_mutex_unlock(&miomx); pthread_cond_wait(&mpcond, &modemmx);
} else {
miorunning = 0;
pthread_mutex_unlock(&modemmx); pthread_mutex_unlock(&modemmx);
pthread_mutex_lock(&cfmx); pthread_mutex_lock(&cfmx);
close_modem(cf.modemdev); close_modem(cf.modemdev);
@ -311,7 +322,7 @@ void *modem_io(k)
if(*cbuf == 'D') break; if(*cbuf == 'D') break;
} }
/* NOTREACHED */ /* NOTREACHED */
pthread_mutex_unlock(&miomx); miorunning = 0;
pthread_mutex_unlock(&modemmx); pthread_mutex_unlock(&modemmx);
pthread_exit(NULL); pthread_exit(NULL);
return 0; return 0;

View File

@ -28,7 +28,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
*/ */
/* $Amigan: phoned/phoned/remote.c,v 1.16 2005/06/26 21:45:20 dcp1990 Exp $ */ /* $Amigan: phoned/phoned/remote.c,v 1.17 2005/06/28 00:52:17 dcp1990 Exp $ */
/* system includes */ /* system includes */
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
@ -221,6 +221,7 @@ int dialogue_cb(fd, sck)
int rc; int rc;
s = fileno(so); s = fileno(so);
memset(buffer, 0, sizeof(buffer)); memset(buffer, 0, sizeof(buffer));
lprintf(debug, "dialogue_cb: Started");
for(;;) { for(;;) {
FD_ZERO(&fds); FD_ZERO(&fds);
FD_ZERO(&ex); FD_ZERO(&ex);
@ -350,7 +351,7 @@ char *parse_command(cmd, cont, s)
RNF("500 OK: Handler tested.\n"); RNF("500 OK: Handler tested.\n");
} else if(CHK("mdlg")) { } else if(CHK("mdlg")) {
int rc; int rc;
write(s->fd, "500 OK: Entering dialogue...\n", sizeof("500 OK: Entering dialogue...\n")); write(s->fd, "500 OK: Entering dialogue...warning: this may crash!\n", sizeof("500 OK: Entering dialogue...warning: this may crash!\n"));
rc = dialogue_with_modem(&dialogue_cb, (void*)s->fpo); rc = dialogue_with_modem(&dialogue_cb, (void*)s->fpo);
if(!rc) { if(!rc) {
return NULL; return NULL;
@ -407,7 +408,9 @@ void begin_dialogue(fp, fd)
memset(&si, 0, sizeof(state_info_t)); memset(&si, 0, sizeof(state_info_t));
si.fpo = fp; /* this is JUST for data, not real commands */ si.fpo = fp; /* this is JUST for data, not real commands */
si.fd = fd; si.fd = fd;
memset(buffer, 0, sizeof(buffer));
while(keep_going == 0x1 && !feof(fp)) { while(keep_going == 0x1 && !feof(fp)) {
memset(buffer, 0, sizeof(buffer));
rc = recv(fd, buffer, MAXBUFSIZE - 1, 0x0); rc = recv(fd, buffer, MAXBUFSIZE - 1, 0x0);
if(rc == 0) { if(rc == 0) {
lprintf(debug, "Socket closed! Got zero!\n"); lprintf(debug, "Socket closed! Got zero!\n");