14 static ResultCode results[] = {
15 { "0", "OK", Rok, 0, },
16 { "1", "CONNECT", Rconnect, 0, },
17 { "2", "RING", Rring, 0, },
18 { "3", "NO CARRIER", Rfailure, 0, },
19 { "4", "ERROR", Rrerror, 0, },
20 { "5", "CONNECT 1200", Rconnect, 0, },
21 { "6", "NO DIALTONE", Rfailure, 0, },
22 { "7", "BUSY", Rfailure, 0, },
23 { "8", "NO ANSWER", Rfailure, 0, },
24 { "9", "CONNECT 2400", Rconnect, 0, }, /* MT1432BA */
25 { "10", "CONNECT 2400", Rconnect, 0, }, /* Hayes */
26 { "11", "CONNECT 4800", Rconnect, 0, },
27 { "12", "CONNECT 9600", Rconnect, 0, },
28 { "13", "CONNECT 14400",Rconnect, 0, },
29 { "23", "CONNECT 1275", Rconnect, 0, }, /* MT1432BA */
31 { "-1", "+FCON", Rcontinue, fcon, },
32 { "-1", "+FTSI", Rcontinue, ftsi, },
33 { "-1", "+FDCS", Rcontinue, fdcs, },
34 { "-1", "+FCFR", Rcontinue, fcfr, },
35 { "-1", "+FPTS", Rcontinue, fpts, },
36 { "-1", "+FET", Rcontinue, fet, },
37 { "-1", "+FHNG", Rcontinue, fhng, },
43 initmodem(Modem *m, int fd, int cfd, char *type, char *id)
54 rawmchar(Modem *m, char *p)
70 if((d = dirfstat(m->fd)) == nil){
71 verbose("rawmchar: dirfstat: %r");
72 return seterror(m, Esys);
79 if(n > sizeof(m->ibuf)-1)
80 n = sizeof(m->ibuf)-1;
81 if((m->icount = read(m->fd, m->ibuf, n)) <= 0){
82 verbose("rawmchar: read: %r");
84 return seterror(m, Esys);
93 getmchar(Modem *m, char *buf, long timeout)
98 while((t = time(0)) <= timeout){
99 switch(r = rawmchar(m, buf)){
112 verbose("getmchar: time %ud, timeout %ud", t, timeout);
114 return seterror(m, Enoresponse);
118 putmchar(Modem *m, char *p)
120 if(write(m->fd, p, 1) < 0)
121 return seterror(m, Esys);
126 * lines terminate with cr-lf
129 getmline(Modem *m, char *buf, int len, long timeout)
136 while((t = time(0)) <= timeout){
137 switch(r = rawmchar(m, buf)){
140 /* ignore ^s ^q which are used for flow */
141 if(*buf == '\021' || *buf == '\023')
144 /* ignore nl if its not with a cr */
169 verbose("getmline: time %ud, timeout %ud", t, timeout);
171 return seterror(m, Enoresponse);
175 command(Modem *m, char *s)
177 verbose("m->: %s", s);
178 if(fprint(m->fd, "%s\r", s) < 0)
179 return seterror(m, Esys);
184 * Read till we see a message or we time out.
185 * BUG: line lengths not checked;
189 response(Modem *m, int timeout)
194 while(getmline(m, m->response, sizeof(m->response), timeout) == Eok){
195 if(m->response[0] == 0)
197 verbose("<-m: %s", m->response);
198 for(rp = results; rp->terse; rp++){
199 if(strncmp(rp->verbose, m->response, strlen(rp->verbose)))
202 if(rp->f && (r = (*rp->f)(m)) == Rcontinue)
213 xonoff(Modem *m, int i)
217 sprint(buf, "x%d", i);
219 write(m->cfd, buf, i);