File: __recvResponse__.m

package info (click to toggle)
octave-arduino 0.12.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,616 kB
  • sloc: cpp: 3,221; python: 438; makefile: 152; xml: 22; sh: 1
file content (95 lines) | stat: -rw-r--r-- 3,003 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
## Copyright (C) 2021-2022 John Donoghue <john.donoghue@ieee.org>
## 
## This program is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
## 
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details. see
## <https://www.gnu.org/licenses/>.

## -*- texinfo -*- 
## @deftypefn {} {@var{retval} =} __recvResponse__ (@var{obj}, @var{lib}, @var{cmd}, @var{timeout})
## Private function
## @end deftypefn

function [dataOut, errcode] = __recvResponse__ (dev, libid, cmd, timeout, debug)
   
   dataOut = [];
   errcode = 0;
   
   set(dev, "timeout", timeout);
   
# TODO: current serial doesnt have a way to know if any data is awaiting
# so try read what we need first without waiting ?

   # read in initial part
   [tmpdataOut, tmpdataSize] = fread (dev, 4);
   if (debug)
     printf("<< "); printf("%d ", tmpdataOut); printf("\n");
   endif
   if tmpdataSize < 4
     errcode = 1;
     dataOut = "Undersized packet header";
   elseif tmpdataOut(1) != hex2dec("A5") || tmpdataOut(2) != libid || (tmpdataOut(3) != cmd && tmpdataOut(3) < 253)
     errcode = 2;
     dataOut = "Malformed packet header";
   elseif (tmpdataOut(3) == 254)
     # got a wait for response value - length is expected to be 0
     if (debug)
       printf("* wait for response\n");
     endif
 
     set(dev, "timeout", -1);

     [tmpdataOut, tmpdataSize] = fread (dev, 4);
     if (debug)
       printf("<< "); printf("%d ", tmpdataOut); printf("\n");
     endif
     if tmpdataSize < 4
       errcode = 1;
       dataOut = "Undersized packet header";
     elseif tmpdataOut(1) != hex2dec("A5") || tmpdataOut(2) != libid || (tmpdataOut(3) != cmd && tmpdataOut(3) != 255)
       errcode = 2;
       dataOut = "Malformed packet header";
     endif
   endif

   if(errcode == 0)
     expectlen =  tmpdataOut(4);
     if expectlen > 0
       [dataOut, tmpdataSize] = fread (dev, expectlen);
       if (debug)
         printf("<< "); printf("%d ", dataOut); printf("\n");
       endif
     else
	 tmpdataSize = 0;
     endif
     if tmpdataSize != expectlen
         errcode = 3;
         dataOut = "Malformed packet body";
     elseif tmpdataOut(3) == 255
	 # valid packet, but was coz we got an error
         errcode = 10;
	 if expectlen == 0
           dataOut = "Recieved error status";;
	 else
           dataOut = char(dataOut);
	 endif
     elseif tmpdataOut(3) == 253
         # valid but was a debug message
	 if debug
           s = char(dataOut);
           printf("DEBUG: %s\n", s);
	 endif
        
	[dataOut, errcode] = __recvResponse__ (dev, libid, cmd, timeout, debug); 
     else
	 errcode = 0;
	 # all is good
     endif
   endif
endfunction