- Timestamp:
- 10/17/07 17:34:53 (2 years ago)
- Location:
- trunk
- Files:
-
- 2 modified
-
bfb/bfb_io.c (modified) (18 diffs)
-
multicobex/multi_cobex.c (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/bfb/bfb_io.c
r197 r201 109 109 if(select(fd+1, &fdset, NULL, NULL, &time)) { 110 110 actual = read(fd, buffer, length); 111 if (actual < 0)111 if (actual < 0) { 112 112 DEBUG(2, "%s() Read error: %d\n", __func__, actual); 113 } 113 114 return actual; 114 115 } … … 125 126 int actual; 126 127 int tries=3; 127 bfb_frame_t *frame ;128 bfb_frame_t *frame = NULL; 128 129 uint8_t rspbuf[200]; 130 int rsplen; 129 131 130 132 uint8_t init_magic = BFB_CONNECT_HELLO; … … 141 143 #endif 142 144 143 while ( tries-- > 0) {145 while (!frame && tries-- > 0) { 144 146 actual = bfb_write_packets (fd, BFB_FRAME_CONNECT, &init_magic, sizeof(init_magic)); 145 147 DEBUG(2, "%s() Wrote %d packets\n", __func__, actual); … … 150 152 } 151 153 152 actual = bfb_io_read(fd, rspbuf, sizeof(rspbuf), 1); 153 DEBUG(2, "%s() Read %d bytes\n", __func__, actual); 154 155 if (actual < 1) { 156 DEBUG(1, "BFB read error\n"); 157 return FALSE; 154 rsplen = 0; 155 while (!frame && actual > 0) { 156 actual = bfb_io_read(fd, &rspbuf[rsplen], sizeof(rspbuf)-rsplen, 2); 157 DEBUG(2, "%s() Read %d bytes\n", __func__, actual); 158 159 if (actual < 0) { 160 DEBUG(1, "BFB read error\n"); 161 return FALSE; 162 } 163 if (actual == 0) { 164 DEBUG(1, "BFB read timeout\n"); 165 } 166 167 rsplen += actual; 168 frame = bfb_read_packets(rspbuf, &rsplen); 169 DEBUG(2, "%s() Unstuffed, %d bytes remaining\n", __func__, rsplen); 158 170 } 159 160 frame = bfb_read_packets(rspbuf, &actual);161 DEBUG(2, "%s() Unstuffed, %d bytes remaining\n", __func__, actual);162 163 if (frame != NULL)164 break;165 171 } 166 172 … … 184 190 } 185 191 186 /* Send an AT-command an expect 1 line back as answer */ 187 /* Ericsson may choose to answer one line, blank one line and then send OK */ 192 /** 193 Send an AT-command and expect an answer of one or more lines. 194 195 \note Start your command with "AT" and terminate it with "\r" (CR). 196 197 \note The expected lines are the the echo, 198 one optional information response and 199 a final result code of "OK" or "ERROR". 200 */ 188 201 int do_at_cmd(fd_t fd, const char *cmd, char *rspbuf, int rspbuflen) 189 202 { … … 195 208 196 209 char *answer = NULL; 197 char *answer_end = NULL;198 210 int answer_size; 199 211 200 212 char tmpbuf[100] = {0,}; 201 213 int total = 0; 202 int done = 0;203 214 int cmdlen; 204 215 … … 211 222 212 223 /* Write command */ 213 if (bfb_io_write(fd, cmd, cmdlen) < cmdlen)224 if (bfb_io_write(fd, cmd, cmdlen) < cmdlen) 214 225 return -1; 215 226 216 while(!done) { 227 actual = 1; 228 while (actual > 0) { 217 229 actual = bfb_io_read(fd, &tmpbuf[total], sizeof(tmpbuf) - total, 2); 218 /* error checking */ 219 if(actual < 0) 230 if (actual < 0) /* error checking */ 220 231 return actual; 221 222 if(actual == 0)223 /* Anser didn't come in time. Cancel */224 return -1;225 232 226 233 total += actual; 234 tmpbuf[total] = '\0'; /* terminate the string, always */ 227 235 228 236 DEBUG(3, "%s() tmpbuf=%d: %s\n", __func__, total, tmpbuf); 229 237 230 238 /* Answer not found within 100 bytes. Cancel */ 231 if (total == sizeof(tmpbuf))239 if (total >= sizeof(tmpbuf)) 232 240 return -1; 233 241 234 if( (answer = strchr(tmpbuf, '\n')) ) { 235 while((*answer == '\r') || (*answer == '\n')) 242 /* Remove first line (echo), then search final result code */ 243 for (answer = tmpbuf; answer && *answer ; ) { 244 while ((*answer != '\r') && (*answer != '\n') && (*answer != '\0')) 236 245 answer++; 237 /* Remove first line (echo) */ 238 if( (answer_end = strchr(answer+1, '\n')) ) { 239 /* Found end of answer */ 240 done = 1; 246 while ((*answer == '\r') || (*answer == '\n')) 247 answer++; 248 if (!strncmp(answer, "OK\r", 3) || !strncmp(answer, "ERROR\r", 6) || 249 !strncmp(answer, "OK\n", 3) || !strncmp(answer, "ERROR\n", 6)) { 250 actual = 0; /* we are done */ 241 251 } 242 252 } 243 253 } 244 /* try hard to discard remaing lines */ 245 actual = bfb_io_read(fd, &tmpbuf[total], sizeof(tmpbuf) - total, 2); 246 247 /* DEBUG(3, "%s() buf:%08lx answer:%08lx end:%08lx\n", __func__, tmpbuf, answer, answer_end); */ 248 249 DEBUG(3, "%s() Answer: %s\n", __func__, answer); 250 251 /* Remove heading and trailing \r */ 252 while((*answer_end == '\r') || (*answer_end == '\n')) 253 answer_end--; 254 DEBUG(3, "%s() Answer: %s\n", __func__, answer); 255 256 answer_size = (answer_end) - answer +1; 257 258 DEBUG(2, "%s() Answer size=%d\n", __func__, answer_size); 254 /* try hard to discard remaing CR/LF */ 255 if (total > 0 && tmpbuf[total-1] != '\n') 256 actual = bfb_io_read(fd, &tmpbuf[total], sizeof(tmpbuf) - total, 2); 257 258 /* Remove echo and trailing CRs */ 259 answer = strchr(tmpbuf, '\r'); 260 if (!answer) /* no echo found */ 261 return -1; 262 263 while (*answer == '\r' || *answer == '\n') 264 answer++; 265 answer_size = 0; 266 while (answer[answer_size] != '\0' && 267 answer[answer_size] != '\r' && answer[answer_size] != '\n') 268 answer_size++; 269 270 DEBUG(3, "%s() Answer (size=%d): %s\n", __func__, answer_size, answer); 259 271 if( (answer_size) >= rspbuflen ) 260 272 return -1; … … 381 393 //} 382 394 383 if(do_at_cmd(ttyfd, "ATZ\r \n", rspbuf, sizeof(rspbuf)) < 0) {395 if(do_at_cmd(ttyfd, "ATZ\r", rspbuf, sizeof(rspbuf)) < 0) { 384 396 DEBUG(1, "Comm-error or already in BFB mode\n"); 385 397 #ifdef _WIN32 … … 389 401 (void) tcflush(ttyfd, TCIFLUSH); 390 402 (void) tcsetattr(ttyfd, TCSANOW, &newtio); 391 if(do_at_cmd(ttyfd, "ATZ\r \n", rspbuf, sizeof(rspbuf)) < 0) {403 if(do_at_cmd(ttyfd, "ATZ\r", rspbuf, sizeof(rspbuf)) < 0) { 392 404 DEBUG(1, "Comm-error or already in BFB mode\n"); 393 405 goto bfbmode; … … 400 412 } 401 413 402 if(do_at_cmd(ttyfd, "AT+GMI\r \n", rspbuf, sizeof(rspbuf)) < 0) {414 if(do_at_cmd(ttyfd, "AT+GMI\r", rspbuf, sizeof(rspbuf)) < 0) { 403 415 DEBUG(1, "Comm-error\n"); 404 416 goto err; … … 421 433 } 422 434 423 if(do_at_cmd(ttyfd, "AT^SIFS\r \n", rspbuf, sizeof(rspbuf)) < 0) {435 if(do_at_cmd(ttyfd, "AT^SIFS\r", rspbuf, sizeof(rspbuf)) < 0) { 424 436 DEBUG(1, "Comm-error\n"); 425 437 goto err; … … 433 445 434 446 /* prefer connection without BFB */ 435 if(do_at_cmd(ttyfd, "AT^SBFB=?\r \n", rspbuf, sizeof(rspbuf)) < 0) {447 if(do_at_cmd(ttyfd, "AT^SBFB=?\r", rspbuf, sizeof(rspbuf)) < 0) { 436 448 DEBUG(1, "Comm-error\n"); 437 449 goto err; … … 443 455 DEBUG(1, "Old BFB Siemens protocol. (%s)\n", rspbuf); 444 456 445 if(do_at_cmd(ttyfd, "AT^SBFB=1\r \n", rspbuf, sizeof(rspbuf)) < 0) {457 if(do_at_cmd(ttyfd, "AT^SBFB=1\r", rspbuf, sizeof(rspbuf)) < 0) { 446 458 DEBUG(1, "Comm-error\n"); 447 459 goto err; … … 473 485 474 486 ericsson: 475 if(do_at_cmd(ttyfd, "AT*EOBEX\r \n", rspbuf, sizeof(rspbuf)) < 0) {487 if(do_at_cmd(ttyfd, "AT*EOBEX\r", rspbuf, sizeof(rspbuf)) < 0) { 476 488 DEBUG(1, "Comm-error\n"); 477 489 goto err; … … 486 498 487 499 motorola: 488 if(do_at_cmd(ttyfd, "AT+MODE=22\r \n", rspbuf, sizeof(rspbuf)) < 0) {500 if(do_at_cmd(ttyfd, "AT+MODE=22\r", rspbuf, sizeof(rspbuf)) < 0) { 489 501 DEBUG(1, "Comm-error\n"); 490 502 goto err; … … 504 516 * No need to implement BFC(1) mode if we only want to run OBEX. 505 517 */ 506 if(do_at_cmd(ttyfd, "AT^SQWE?\r \n", rspbuf, sizeof(rspbuf)) < 0) {518 if(do_at_cmd(ttyfd, "AT^SQWE?\r", rspbuf, sizeof(rspbuf)) < 0) { 507 519 DEBUG(1, "Comm-error\n"); 508 520 goto err; 509 521 } 510 522 if (strcasecmp("^SQWE:0",rspbuf) != 0) { 511 if(do_at_cmd(ttyfd, "AT^SQWE=0\r \n", rspbuf, sizeof(rspbuf)) < 0) {523 if(do_at_cmd(ttyfd, "AT^SQWE=0\r", rspbuf, sizeof(rspbuf)) < 0) { 512 524 DEBUG(1, "Comm-error\n"); 513 525 goto err; … … 519 531 sleep(1); 520 532 } 521 if(do_at_cmd(ttyfd, "AT^SQWE=3\r \n", rspbuf, sizeof(rspbuf)) < 0) {533 if(do_at_cmd(ttyfd, "AT^SQWE=3\r", rspbuf, sizeof(rspbuf)) < 0) { 522 534 DEBUG(1, "Comm-error\n"); 523 535 goto err; … … 533 545 534 546 generic: 535 if(do_at_cmd(ttyfd, "AT+CPROT=0\r \n", rspbuf, sizeof(rspbuf)) < 0) {547 if(do_at_cmd(ttyfd, "AT+CPROT=0\r", rspbuf, sizeof(rspbuf)) < 0) { 536 548 DEBUG(1, "Comm-error\n"); 537 549 goto err; -
trunk/multicobex/multi_cobex.c
r186 r201 72 72 * with the linux driver (linux-2.6.17.13) 73 73 */ 74 bfb_write_at(c->fd, " at^sbfb=0\r");74 bfb_write_at(c->fd, "AT^SBFB=0\r"); 75 75 sleep(1); 76 76 bfb_io_write(c->fd, "+++", 3); 77 77 sleep(1); 78 bfb_io_write(c->fd,"\r", 1);78 bfb_io_write(c->fd,"\r", 1); 79 79 } 80 80 bfb_io_close(c->fd, force);
