|
1 |
| -<?php |
2 |
| - |
3 |
| -define("ERR_OK", 0); |
4 |
| -define("ERR_CMD_WAIT", 10); |
5 |
| -define("ERR_CMD_UND", 11); |
6 |
| -define("ERR_CMD_ARG", 12); |
7 |
| -define("ERR_CMD_DATA", 13); |
8 |
| -define("ERR_EPIPE", 14); |
9 |
| - |
10 |
| -define("TCP_ID_SSL", 0); |
11 |
| -define("TCP_ID_SSH", 1); |
12 |
| -define("TCP_ID_SMTP", 0); |
13 |
| -define("UDP_ID_DNS", 4); |
14 |
| - |
15 |
| -function err2str($err) |
16 |
| -{ |
17 |
| - switch($err) |
18 |
| - { |
19 |
| - case ERR_OK: |
20 |
| - return ""; |
21 |
| - case ERR_CMD_UND: |
22 |
| - return "unknown command"; |
23 |
| - case ERR_CMD_ARG: |
24 |
| - return "invalid argument"; |
25 |
| - case ERR_CMD_DATA: |
26 |
| - return "invalid command data"; |
27 |
| - case ERR_EPIPE: |
28 |
| - return "broken pipe"; |
29 |
| - default: |
30 |
| - return "error $err"; |
31 |
| - } |
32 |
| -} |
33 |
| - |
34 |
| -function slave_write($err, $data = "") |
35 |
| -{ |
36 |
| - global $spi_pid; |
37 |
| - |
38 |
| - if($err) |
39 |
| - { |
40 |
| - $head32 = 0xa0000000; // data & NAK bit set, csum bit clear |
41 |
| - |
42 |
| - if($err == ERR_CMD_WAIT) |
43 |
| - { |
44 |
| - $wait = (int)$data; |
45 |
| - if($wait < 1) |
46 |
| - $wait = 1; |
47 |
| - if($wait > 30) |
48 |
| - $wait = 30; |
49 |
| - |
50 |
| - $data = sprintf("W%03u", $wait); |
51 |
| - //echo "cmd_wait: $wait\r\n"; |
52 |
| - } |
53 |
| - else |
54 |
| - { |
55 |
| - $data = sprintf("E%03u%s", $err, err2str($err)); |
56 |
| - echo "cmd_error: ", err2str($err), "\r\n"; |
57 |
| - } |
58 |
| - } |
59 |
| - else |
60 |
| - $head32 = 0x80000000; // data bit set, csum bit clear |
61 |
| - |
62 |
| - $head32 |= (strlen($data) << 16); |
63 |
| - $wbuf = int2bin($head32, 4, true) . $data; |
64 |
| - |
65 |
| - return pid_write($spi_pid, $wbuf); |
66 |
| -} |
67 |
| - |
68 |
| -function slave_rxlen_wait($rxlen, $wait_ms) |
69 |
| -{ |
70 |
| - global $spi_pid; |
71 |
| - |
72 |
| - if($rxlen && $wait_ms) |
73 |
| - { |
74 |
| - while(pid_ioctl($spi_pid, "get rxlen") < $rxlen) |
75 |
| - { |
76 |
| - usleep(1000); |
77 |
| - $wait_ms--; |
78 |
| - |
79 |
| - if(!$wait_ms) |
80 |
| - { |
81 |
| - echo "slave_read: rxlen wait timeout\r\n"; |
82 |
| - return 0; |
83 |
| - } |
84 |
| - } |
85 |
| - } |
86 |
| - |
87 |
| - return pid_ioctl($spi_pid, "get rxlen"); |
88 |
| -} |
89 |
| - |
90 |
| -function slave_read(&$rbuf, $wait_ms, $is_cmd) |
91 |
| -{ |
92 |
| - global $spi_pid; |
93 |
| - |
94 |
| - if($wait_ms && !slave_rxlen_wait(4, $wait_ms)) |
95 |
| - return 0; |
96 |
| - |
97 |
| - if(pid_ioctl($spi_pid, "get rxlen") < 4) |
98 |
| - return 0; |
99 |
| - |
100 |
| - pid_peek($spi_pid, $rbuf, 4); |
101 |
| - |
102 |
| - $head32 = bin2int($rbuf, 0, 4, true); |
103 |
| - $len12 = ($head32 >> 16) & 0x0fff; |
104 |
| - |
105 |
| - if(!$is_cmd && !($head32 & 0x80000000)) |
106 |
| - { |
107 |
| - // skip reading data if command head is received when we read data. |
108 |
| - echo "slave_read: skip read data\r\n"; |
109 |
| - return 0; |
110 |
| - } |
111 |
| - |
112 |
| - pid_read($spi_pid, $rbuf, 4); // drop head |
113 |
| - |
114 |
| - if($len12) |
115 |
| - { |
116 |
| - if($wait_ms && !slave_rxlen_wait($len12, $wait_ms)) |
117 |
| - return 0; |
118 |
| - |
119 |
| - pid_read($spi_pid, $rbuf, $len12); |
120 |
| - |
121 |
| - if($is_cmd && ($head32 & 0x80000000)) |
122 |
| - { |
123 |
| - // drop data if data head is received when we read command. |
124 |
| - echo "slave_read: drop data\r\n"; |
125 |
| - $rbuf = ""; |
126 |
| - return 0; |
127 |
| - } |
128 |
| - else |
129 |
| - return $len12; |
130 |
| - } |
131 |
| - else |
132 |
| - { |
133 |
| - $rbuf = ""; |
134 |
| - return 0; |
135 |
| - } |
136 |
| -} |
137 |
| - |
138 |
| -function slave_read_command(&$rbuf, $wait_ms) |
139 |
| -{ |
140 |
| - return slave_read($rbuf, $wait_ms, true); |
141 |
| -} |
142 |
| - |
143 |
| -function slave_read_data(&$rbuf, $wait_ms) |
144 |
| -{ |
145 |
| - return slave_read($rbuf, $wait_ms, false); |
146 |
| -} |
147 |
| - |
148 |
| -?> |
| 1 | +<?php |
| 2 | + |
| 3 | +define("ERR_OK", 0); |
| 4 | +define("ERR_CMD_WAIT", 10); |
| 5 | +define("ERR_CMD_UND", 11); |
| 6 | +define("ERR_CMD_ARG", 12); |
| 7 | +define("ERR_CMD_DATA", 13); |
| 8 | +define("ERR_EPIPE", 14); |
| 9 | + |
| 10 | +define("TCP_ID_SSL", 0); |
| 11 | +define("TCP_ID_SSH", 1); |
| 12 | +define("TCP_ID_SMTP", 0); |
| 13 | +define("UDP_ID_DNS", 4); |
| 14 | + |
| 15 | +function err2str($err) |
| 16 | +{ |
| 17 | + switch($err) |
| 18 | + { |
| 19 | + case ERR_OK: |
| 20 | + return ""; |
| 21 | + case ERR_CMD_UND: |
| 22 | + return "unknown command"; |
| 23 | + case ERR_CMD_ARG: |
| 24 | + return "invalid argument"; |
| 25 | + case ERR_CMD_DATA: |
| 26 | + return "invalid command data"; |
| 27 | + case ERR_EPIPE: |
| 28 | + return "broken pipe"; |
| 29 | + default: |
| 30 | + return "error $err"; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +function slave_write($err, $data = "") |
| 35 | +{ |
| 36 | + global $spi_pid; |
| 37 | + |
| 38 | + if($err) |
| 39 | + { |
| 40 | + $head32 = 0xa0000000; // data & NAK bit set, csum bit clear |
| 41 | + |
| 42 | + if($err == ERR_CMD_WAIT) |
| 43 | + { |
| 44 | + $wait = (int)$data; |
| 45 | + if($wait < 1) |
| 46 | + $wait = 1; |
| 47 | + if($wait > 30) |
| 48 | + $wait = 30; |
| 49 | + |
| 50 | + $data = sprintf("W%03u", $wait); |
| 51 | + //echo "cmd_wait: $wait\r\n"; |
| 52 | + } |
| 53 | + else |
| 54 | + { |
| 55 | + $data = sprintf("E%03u%s", $err, err2str($err)); |
| 56 | + echo "cmd_error: ", err2str($err), "\r\n"; |
| 57 | + } |
| 58 | + } |
| 59 | + else |
| 60 | + $head32 = 0x80000000; // data bit set, csum bit clear |
| 61 | + |
| 62 | + $head32 |= (strlen($data) << 16); |
| 63 | + $wbuf = int2bin($head32, 4, true) . $data; |
| 64 | + |
| 65 | + return pid_write($spi_pid, $wbuf); |
| 66 | +} |
| 67 | + |
| 68 | +function slave_rxlen_wait($rxlen, $wait_ms) |
| 69 | +{ |
| 70 | + global $spi_pid; |
| 71 | + |
| 72 | + if($rxlen && $wait_ms) |
| 73 | + { |
| 74 | + while(pid_ioctl($spi_pid, "get rxlen") < $rxlen) |
| 75 | + { |
| 76 | + usleep(1000); |
| 77 | + $wait_ms--; |
| 78 | + |
| 79 | + if(!$wait_ms) |
| 80 | + { |
| 81 | + echo "slave_read: rxlen wait timeout\r\n"; |
| 82 | + return 0; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return pid_ioctl($spi_pid, "get rxlen"); |
| 88 | +} |
| 89 | + |
| 90 | +function slave_read(&$rbuf, $wait_ms, $is_cmd) |
| 91 | +{ |
| 92 | + global $spi_pid; |
| 93 | + |
| 94 | + if($wait_ms && !slave_rxlen_wait(4, $wait_ms)) |
| 95 | + return 0; |
| 96 | + |
| 97 | + if(pid_ioctl($spi_pid, "get rxlen") < 4) |
| 98 | + return 0; |
| 99 | + |
| 100 | + pid_peek($spi_pid, $rbuf, 4); |
| 101 | + |
| 102 | + $head32 = bin2int($rbuf, 0, 4, true); |
| 103 | + $len12 = ($head32 >> 16) & 0x0fff; |
| 104 | + |
| 105 | + if(!$is_cmd && !($head32 & 0x80000000)) |
| 106 | + { |
| 107 | + // skip reading data if command head is received when we read data. |
| 108 | + echo "slave_read: skip read data\r\n"; |
| 109 | + return 0; |
| 110 | + } |
| 111 | + |
| 112 | + pid_read($spi_pid, $rbuf, 4); // drop head |
| 113 | + |
| 114 | + if($len12) |
| 115 | + { |
| 116 | + if($wait_ms && !slave_rxlen_wait($len12, $wait_ms)) |
| 117 | + return 0; |
| 118 | + |
| 119 | + pid_read($spi_pid, $rbuf, $len12); |
| 120 | + |
| 121 | + if($is_cmd && ($head32 & 0x80000000)) |
| 122 | + { |
| 123 | + // drop data if data head is received when we read command. |
| 124 | + echo "slave_read: drop data\r\n"; |
| 125 | + $rbuf = ""; |
| 126 | + return 0; |
| 127 | + } |
| 128 | + else |
| 129 | + return $len12; |
| 130 | + } |
| 131 | + else |
| 132 | + { |
| 133 | + $rbuf = ""; |
| 134 | + return 0; |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +function slave_read_command(&$rbuf, $wait_ms) |
| 139 | +{ |
| 140 | + return slave_read($rbuf, $wait_ms, true); |
| 141 | +} |
| 142 | + |
| 143 | +function slave_read_data(&$rbuf, $wait_ms) |
| 144 | +{ |
| 145 | + return slave_read($rbuf, $wait_ms, false); |
| 146 | +} |
| 147 | + |
| 148 | +?> |
0 commit comments