Changeset 201 for trunk

Show
Ignore:
Timestamp:
10/17/07 17:34:53 (3 years ago)
Author:
zany
Message:

at-command rewrite

Location:
trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/bfb/bfb_io.c

    r197 r201  
    109109        if(select(fd+1, &fdset, NULL, NULL, &time)) { 
    110110                actual = read(fd, buffer, length); 
    111                 if(actual < 0) 
     111                if (actual < 0) { 
    112112                        DEBUG(2, "%s() Read error: %d\n", __func__, actual); 
     113                } 
    113114                return actual; 
    114115        } 
     
    125126        int actual; 
    126127        int tries=3; 
    127         bfb_frame_t *frame; 
     128        bfb_frame_t *frame = NULL; 
    128129        uint8_t rspbuf[200]; 
     130        int rsplen; 
    129131 
    130132        uint8_t init_magic = BFB_CONNECT_HELLO; 
     
    141143#endif 
    142144 
    143         while (tries-- > 0) { 
     145        while (!frame && tries-- > 0) { 
    144146                actual = bfb_write_packets (fd, BFB_FRAME_CONNECT, &init_magic, sizeof(init_magic)); 
    145147                DEBUG(2, "%s() Wrote %d packets\n", __func__, actual); 
     
    150152                } 
    151153 
    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); 
    158170                } 
    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; 
    165171        } 
    166172                 
     
    184190} 
    185191 
    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 */ 
    188201int do_at_cmd(fd_t fd, const char *cmd, char *rspbuf, int rspbuflen) 
    189202{ 
     
    195208 
    196209        char *answer = NULL; 
    197         char *answer_end = NULL; 
    198210        int answer_size; 
    199211 
    200212        char tmpbuf[100] = {0,}; 
    201213        int total = 0; 
    202         int done = 0; 
    203214        int cmdlen; 
    204215 
     
    211222 
    212223        /* Write command */ 
    213         if(bfb_io_write(fd, cmd, cmdlen) < cmdlen) 
     224        if (bfb_io_write(fd, cmd, cmdlen) < cmdlen) 
    214225                return -1; 
    215226 
    216         while(!done)    { 
     227        actual = 1; 
     228        while (actual > 0)      { 
    217229                actual = bfb_io_read(fd, &tmpbuf[total], sizeof(tmpbuf) - total, 2); 
    218                 /* error checking */ 
    219                 if(actual < 0) 
     230                if (actual < 0) /* error checking */ 
    220231                        return actual; 
    221  
    222                 if(actual == 0) 
    223                         /* Anser didn't come in time. Cancel */ 
    224                         return -1; 
    225232                 
    226233                total += actual; 
     234                tmpbuf[total] = '\0'; /* terminate the string, always */ 
    227235 
    228236                DEBUG(3, "%s() tmpbuf=%d: %s\n", __func__, total, tmpbuf); 
    229237 
    230238                /* Answer not found within 100 bytes. Cancel */ 
    231                 if(total == sizeof(tmpbuf)) 
     239                if (total >= sizeof(tmpbuf)) 
    232240                        return -1; 
    233241 
    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')) 
    236245                                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 */ 
    241251                        } 
    242252                } 
    243253        } 
    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); 
    259271        if( (answer_size) >= rspbuflen ) 
    260272                return -1; 
     
    381393        //} 
    382394 
    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) { 
    384396                DEBUG(1, "Comm-error or already in BFB mode\n"); 
    385397#ifdef _WIN32 
     
    389401                (void) tcflush(ttyfd, TCIFLUSH); 
    390402                (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) { 
    392404                        DEBUG(1, "Comm-error or already in BFB mode\n"); 
    393405                        goto bfbmode; 
     
    400412        } 
    401413 
    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)    { 
    403415                DEBUG(1, "Comm-error\n"); 
    404416                goto err; 
     
    421433        } 
    422434 
    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)   { 
    424436                DEBUG(1, "Comm-error\n"); 
    425437                goto err; 
     
    433445 
    434446        /* 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) { 
    436448                DEBUG(1, "Comm-error\n"); 
    437449                goto err; 
     
    443455        DEBUG(1, "Old BFB Siemens protocol. (%s)\n", rspbuf); 
    444456         
    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) { 
    446458                DEBUG(1, "Comm-error\n"); 
    447459                goto err; 
     
    473485 
    474486 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) { 
    476488                DEBUG(1, "Comm-error\n"); 
    477489                goto err; 
     
    486498 
    487499 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) { 
    489501                DEBUG(1, "Comm-error\n"); 
    490502                goto err; 
     
    504516         * No need to implement BFC(1) mode if we only want to run OBEX. 
    505517         */  
    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) {  
    507519                DEBUG(1, "Comm-error\n");  
    508520                goto err;  
    509521        }  
    510522        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) {  
    512524                        DEBUG(1, "Comm-error\n");  
    513525                        goto err;  
     
    519531                sleep(1);  
    520532        }  
    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) { 
    522534                DEBUG(1, "Comm-error\n"); 
    523535                goto err; 
     
    533545 
    534546 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) { 
    536548                DEBUG(1, "Comm-error\n"); 
    537549                goto err; 
  • trunk/multicobex/multi_cobex.c

    r186 r201  
    7272                 * with the linux driver (linux-2.6.17.13)  
    7373                 */ 
    74                 bfb_write_at(c->fd, "at^sbfb=0\r"); 
     74                bfb_write_at(c->fd, "AT^SBFB=0\r"); 
    7575                sleep(1); 
    7676                bfb_io_write(c->fd, "+++", 3); 
    7777                sleep(1); 
    78                 bfb_io_write(c->fd,"\r",1); 
     78                bfb_io_write(c->fd,"\r", 1); 
    7979        } 
    8080        bfb_io_close(c->fd, force);