home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: OtherApp / OtherApp.zip / wincam.zip / winc_src.zip / protocol.c < prev    next >
C/C++ Source or Header  |  1997-02-28  |  5KB  |  232 lines

  1. /*
  2.  *
  3.  * handle the serial command structure for getting data to and from the camera.
  4.  *
  5.  * Copyright (C) 1996, Paul G. Fox
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify it
  8.  * under the terms of the GNU General Public License as published by the
  9.  * Free Software Foundation; either version 2 of the License, or (at your
  10.  * option) any later version.
  11.  * 
  12.  * This program is distributed in the hope that it will be useful, but
  13.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU General Public License along
  18.  * with this program; if not, write to the Free Software Foundation, Inc.,
  19.  * 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  *
  21.  * This software was created with the help of proprietary information
  22.  * belonging to StarDot Technologies.
  23.  *
  24.  * $Header: E:/winc/RCS/protocol.c 1.1 1997/03/01 03:44:14 Derek Exp Derek $
  25.  */
  26. #define NDEBUG 1
  27.  
  28. /* routines for shuffling bytes to and from the camera */
  29.  
  30. #include <stdlib.h>
  31. #include <unistd.h>
  32. #include <string.h>
  33. #include <errno.h>
  34. #include <stdarg.h>
  35. #include <assert.h>
  36. #include "private.h"
  37. #include "trace.h"
  38.  
  39. result
  40. winc_reset( cam_t cam )
  41. {
  42.     assert(cam);
  43.  
  44.     TRACE("p", __FILE__ ": resetting camera\n");
  45.  
  46.     if (comms_send_break(cam, 300) != OK)
  47.     return NotOK;
  48.     _sleep2(100);    /* wait 100 milliseconds */
  49.     return OK;
  50. }
  51.  
  52. result
  53. winc_flush(cam_t cam)
  54. {
  55.     byte buf[2048];
  56.     static byte nops[] = { Cmd_nop, Cmd_nop, Cmd_nop, Cmd_nop, Cmd_nop };
  57.  
  58.     assert(cam);
  59.  
  60.  
  61.     if (!cam->havelock)
  62.     return OK;
  63.  
  64.     TRACE("a", __FILE__ ": had lock, flushing commands, timeouts are ok\n");
  65.  
  66.     if (comms_write(cam, nops, 5) == -1) {
  67.     errormsg(__FILE__ ": couldn't send nops\n");
  68.     return NotOK;
  69.     }
  70.     (void)comms_timed_read(cam, buf, sizeof(buf), 1000);
  71.     return OK;
  72. }
  73.  
  74. result
  75. winc_send_cmd( cam_t cam, Cmd_byte_t cmd, ... )
  76. {
  77.  
  78.     byte b;
  79.     int i;
  80.     va_list ap;
  81.     int retry = 2;
  82.     result res = OK;
  83.  
  84.     assert(cam && cam->havelock);
  85.  
  86.     TRACE("p", __FILE__ ": sending camera command 0x%x\n", cmd);
  87.  
  88.  
  89.     while (retry--) {
  90.  
  91.         b = (byte)cmd;
  92.  
  93.     if (comms_write(cam, &b, 1) == -1) {
  94.         errormsg(__FILE__ ": couldn't send cmd byte: %s\n", strerror(errno));
  95.         return NotOK;
  96.     }
  97.  
  98.     va_start (ap, cmd);
  99.     while ((i = va_arg(ap, int)) != -1) {
  100.         b = (byte)i;
  101.         TRACE("p", __FILE__ ": sending 0x%x\n", b);
  102.         if (comms_write(cam, &b, 1) == -1) {
  103.         errormsg(__FILE__ ": couldn't send arg byte: %s\n", strerror(errno));
  104.         return NotOK;
  105.         }
  106.     }
  107.     va_end (ap);
  108.  
  109.     res = OK;
  110.  
  111.     /* almost all commands come back with an ACK */
  112.     if (cmd != Cmd_nop && cmd != Cmd_disable_all) {
  113.         i = comms_timed_read(cam, &b, 1, 4000);
  114.         if (i == -1)
  115.         return NotOK;
  116.         else if (i == -2)    /* timeout */
  117.         res = NotOK;
  118.         else if (b != Resp_Ack) {
  119.         TRACE("P", __FILE__ ": expected 0x%x, got 0x%x\n",
  120.                     Resp_Ack, i);
  121.         res = NotOK;
  122.         }
  123.     }
  124.     if (res == OK)
  125.         break;
  126.     else if (retry) {
  127.         winc_flush(cam);
  128.         errormsg(__FILE__ ": retrying command %x\n", cmd);
  129.     }
  130.     }
  131.  
  132.     return res;
  133. }
  134.  
  135. result
  136. winc_get_resp( cam_t cam,
  137.     int nbytes,
  138.     byte *bp,
  139.     unsigned long *sump)
  140. {
  141.     return winc_get_resp_wait( cam, nbytes, bp, 200, sump);
  142. }
  143.  
  144. result
  145. winc_get_resp_wait( cam_t cam,
  146.     int nbytes,
  147.     byte *buf,
  148.     int millisec,
  149.     unsigned long *sump)
  150. {
  151.     int i;
  152.     int tcnt = 16;
  153.     byte *bp;
  154.  
  155.     TRACE("p", __FILE__ ": getting camera response, want %d bytes\n", nbytes);
  156.  
  157.     TRACE("P", __FILE__ ": receiving:");
  158.     if (sump)
  159.     *sump = 0;
  160.     i = comms_timed_read(cam, buf, nbytes, millisec);
  161.     if (i == -1) {
  162.     errormsg(__FILE__ ": response failed\n");
  163.     return NotOK;
  164.     } else if (i == -2) {
  165.     errormsg(__FILE__ ": response timed out\n");
  166.     return NotOK;
  167.     }
  168.     if (TRACE_IS_ACTIVE()) {
  169.     bp = buf;
  170.     for (i = 0; i < nbytes; i++) {
  171.         if (tcnt == 16) {
  172.         TRACE("P", "\n    " __FILE__ ": got");
  173.         tcnt = 0;
  174.         }
  175.         if ((tcnt % 4) == 0)
  176.         TRACE("P", " ");
  177.         TRACE("P", " %02x", *bp++);
  178.         tcnt++;
  179.     }
  180.  
  181.     }
  182.  
  183.     if (sump) {
  184.     bp = buf;
  185.     for (i = 0; i < nbytes; i++)
  186.         *sump += *bp++;
  187.     }
  188.  
  189.     TRACE("P", "\ngot %d bytes\n", nbytes);
  190.  
  191.     return OK;
  192. }
  193.  
  194. result
  195. winc_check_for_modem( cam_t cam )
  196. {
  197.     char buf[10];
  198.     int i;
  199.  
  200.     assert(cam);
  201.  
  202.     TRACE("p", __FILE__ ": checking for modem\n");
  203.     if (comms_write(cam, "AT\r", 3) == -1) {
  204.     errormsg(__FILE__ ": couldn't send AT cmd: %s\n", strerror(errno));
  205.     return NotOK;
  206.     }
  207.  
  208.     i = comms_timed_read(cam, buf, 1, 4000);
  209.     if (i < 0)
  210.     return NotOK;
  211.  
  212.     if (buf[0] != 'A') /* then it's not a modem */
  213.         return NotOK;
  214.  
  215.     TRACE("P", __FILE__ ": looking for modem got A\n");
  216.  
  217.     /* we should get "OK" within 8 characters */
  218.     i = comms_timed_read(cam, buf, 8, 4000);
  219.     if (i < 0)
  220.     return NotOK;
  221.  
  222.     buf[7] = 0;
  223.     if (i > 1 && buf[0] == 'T' && strstr(buf, "OK")) {
  224.     TRACE("p", __FILE__ ": probably have modem\n");
  225.     return OK;
  226.     }
  227.     TRACE("p", __FILE__ ": not modem: '%s'\n", buf);
  228.  
  229.     return OK;
  230. }
  231.  
  232.