VOC Optimation
[owSlave2.git] / tools_cmd / rwOW / tmexnet.c
1 //---------------------------------------------------------------------------\r
2 // Copyright (C) 2001 Dallas Semiconductor Corporation, All Rights Reserved.\r
3 //\r
4 // Permission is hereby granted, free of charge, to any person obtaining a\r
5 // copy of this software and associated documentation files (the "Software"),\r
6 // to deal in the Software without restriction, including without limitation\r
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,\r
8 // and/or sell copies of the Software, and to permit persons to whom the\r
9 // Software is furnished to do so, subject to the following conditions:\r
10 //\r
11 // The above copyright notice and this permission notice shall be included\r
12 // in all copies or substantial portions of the Software.\r
13 //\r
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS\r
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
16 // MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\r
17 // IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES\r
18 // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,\r
19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR\r
20 // OTHER DEALINGS IN THE SOFTWARE.\r
21 //\r
22 // Except as contained in this notice, the name of Dallas Semiconductor\r
23 // shall not be used except as stated in the Dallas Semiconductor\r
24 // Branding Policy.\r
25 //---------------------------------------------------------------------------\r
26 //\r
27 //  tmexnet.C - Wrapper class to hook 1-Wire Public Domain API to TMEX API\r
28 //              for network functions.\r
29 //\r
30 //  Version: 3.00\r
31 //\r
32 //\r
33 \r
34 #include "ownet.h"\r
35 #include <windows.h>\r
36 \r
37 uchar StateBuffer[MAX_PORTNUM][5120];\r
38 \r
39 // external TMEX variables\r
40 extern long SessionHandle[MAX_PORTNUM];\r
41 extern short far pascal TMSearch(long session_handle, void *start_buffer, \r
42                                  short ResetSearch,  short PerformReset, \r
43                                  short SrchCmd);\r
44 extern short pascal TMFirst(long, void *);\r
45 extern short pascal TMNext(long, void *);\r
46 extern short pascal TMAccess(long, void *);\r
47 extern short pascal TMStrongAccess(long, void *);\r
48 extern short pascal TMStrongAlarmAccess(long, void *);\r
49 extern short pascal TMOverAccess(long, void *);\r
50 extern short pascal TMRom(long, void *, short *);\r
51 extern short pascal TMFirstAlarm(long, void *);\r
52 extern short pascal TMNextAlarm(long, void *);\r
53 extern short pascal TMFamilySearchSetup(long, void *, short);\r
54 extern short pascal TMSkipFamily(long, void *);\r
55 extern short pascal TMAutoOverDrive(long, void *, short);\r
56 \r
57 \r
58 //--------------------------------------------------------------------------\r
59 // The 'owFirst' finds the first device on the 1-Wire Net  This function\r
60 // contains one parameter 'alarm_only'.  When\r
61 // 'alarm_only' is TRUE (1) the find alarm command 0xEC is\r
62 // sent instead of the normal search command 0xF0.\r
63 // Using the find alarm command 0xEC will limit the search to only\r
64 // 1-Wire devices that are in an 'alarm' state.\r
65 //\r
66 // 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to\r
67 //                indicate the symbolic port number.\r
68 // 'do_reset'   - TRUE (1) perform reset before search, FALSE (0) do not\r
69 //                perform reset before search.\r
70 // 'alarm_only' - TRUE (1) the find alarm command 0xEC is\r
71 //                sent instead of the normal search command 0xF0\r
72 //\r
73 // Returns:   TRUE (1) : when a 1-Wire device was found and it's\r
74 //                        Serial Number placed in the global SerialNum[portnum]\r
75 //            FALSE (0): There are no devices on the 1-Wire Net.\r
76 //\r
77 SMALLINT owFirst(int portnum, SMALLINT do_reset, SMALLINT alarm_only)\r
78 {\r
79    return (TMSearch(SessionHandle[portnum], StateBuffer[portnum], 1, \r
80                     (short)do_reset, (short)((alarm_only) ? 0xEC : 0xF0)) == 1);\r
81 }\r
82 \r
83 //--------------------------------------------------------------------------\r
84 // The 'owNext' function does a general search.  This function\r
85 // continues from the previos search state. The search state\r
86 // can be reset by using the 'owFirst' function.\r
87 // This function contains one parameter 'alarm_only'.\r
88 // When 'alarm_only' is TRUE (1) the find alarm command\r
89 // 0xEC is sent instead of the normal search command 0xF0.\r
90 // Using the find alarm command 0xEC will limit the search to only\r
91 // 1-Wire devices that are in an 'alarm' state.\r
92 //\r
93 // 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to\r
94 //                indicate the symbolic port number.\r
95 // 'do_reset'   - TRUE (1) perform reset before search, FALSE (0) do not\r
96 //                perform reset before search.\r
97 // 'alarm_only' - TRUE (1) the find alarm command 0xEC is\r
98 //                sent instead of the normal search command 0xF0\r
99 //\r
100 // Returns:   TRUE (1) : when a 1-Wire device was found and it's\r
101 //                       Serial Number placed in the global SerialNum[portnum]\r
102 //            FALSE (0): when no new device was found.  Either the\r
103 //                       last search was the last device or there\r
104 //                       are no devices on the 1-Wire Net.\r
105 //\r
106 SMALLINT owNext(int portnum, SMALLINT do_reset, SMALLINT alarm_only)\r
107 {\r
108    return (TMSearch(SessionHandle[portnum], StateBuffer[portnum], 0, \r
109                     (short)do_reset, (short)((alarm_only) ? 0xEC : 0xF0)) == 1);\r
110 }\r
111 \r
112 //--------------------------------------------------------------------------\r
113 // The 'owSerialNum' function either reads or sets the SerialNum buffer\r
114 // that is used in the search functions 'owFirst' and 'owNext'.\r
115 // This function contains two parameters, 'serialnum_buf' is a pointer\r
116 // to a buffer provided by the caller.  'serialnum_buf' should point to\r
117 // an array of 8 unsigned chars.  The second parameter is a flag called\r
118 // 'do_read' that is TRUE (1) if the operation is to read and FALSE\r
119 // (0) if the operation is to set the internal SerialNum buffer from\r
120 // the data in the provided buffer.\r
121 //\r
122 // 'portnum'       - number 0 to MAX_PORTNUM-1.  This number is provided to\r
123 //                   indicate the symbolic port number.\r
124 // 'serialnum_buf' - buffer to that contains the serial number to set\r
125 //                   when do_read = FALSE (0) and buffer to get the serial\r
126 //                   number when do_read = TRUE (1).\r
127 // 'do_read'       - flag to indicate reading (1) or setting (0) the current\r
128 //                   serial number.\r
129 //\r
130 void owSerialNum(int portnum, uchar *serialnum_buf, SMALLINT do_read)\r
131 {\r
132    short ROM[8],i;\r
133 \r
134    // check if reading or writing\r
135    if (do_read)\r
136    {\r
137       ROM[0] = 0;\r
138    }\r
139    else\r
140    {\r
141       for (i = 0; i < 8; i++)\r
142          ROM[i] = serialnum_buf[i];\r
143    }\r
144 \r
145    // call TMEX to read or set the current device\r
146    TMRom(SessionHandle[portnum], StateBuffer[portnum], ROM);\r
147 \r
148    // place in 'serialnum_buf'\r
149    if (do_read)\r
150    {\r
151       for (i = 0; i < 8; i++)\r
152          serialnum_buf[i] = (uchar)ROM[i];\r
153    }\r
154 }\r
155 \r
156 //--------------------------------------------------------------------------\r
157 // Setup the search algorithm to find a certain family of devices\r
158 // the next time a search function is called 'owNext'.\r
159 //\r
160 // 'portnum'       - number 0 to MAX_PORTNUM-1.  This number was provided to\r
161 //                   OpenCOM to indicate the port number.\r
162 // 'search_family' - family code type to set the search algorithm to find\r
163 //                   next.\r
164 //\r
165 void owFamilySearchSetup(int portnum, SMALLINT search_family)\r
166 {\r
167    TMFamilySearchSetup(SessionHandle[portnum], StateBuffer[portnum], \r
168                        (short)search_family);\r
169 }\r
170 \r
171 //--------------------------------------------------------------------------\r
172 // Set the current search state to skip the current family code.\r
173 //\r
174 // 'portnum'     - number 0 to MAX_PORTNUM-1.  This number is provided to\r
175 //                 indicate the symbolic port number.\r
176 //\r
177 void owSkipFamily(int portnum)\r
178 {\r
179    TMSkipFamily(SessionHandle[portnum], StateBuffer[portnum]);\r
180 }\r
181 \r
182 //--------------------------------------------------------------------------\r
183 // The 'owAccess' function resets the 1-Wire and sends a MATCH Serial\r
184 // Number command followed by the current SerialNum code. After this\r
185 // function is complete the 1-Wire device is ready to accept device-specific\r
186 // commands.\r
187 //\r
188 // 'portnum'     - number 0 to MAX_PORTNUM-1.  This number is provided to\r
189 //                 indicate the symbolic port number.\r
190 //\r
191 // Returns:   TRUE (1) : reset indicates present and device is ready\r
192 //                       for commands.\r
193 //            FALSE (0): reset does not indicate presence or echos 'writes'\r
194 //                       are not correct.\r
195 //\r
196 SMALLINT owAccess(int portnum)\r
197 {\r
198    return (TMAccess(SessionHandle[portnum], StateBuffer[portnum]) == 1);\r
199 }\r
200 \r
201 //----------------------------------------------------------------------\r
202 // The function 'owVerify' verifies that the current device\r
203 // is in contact with the 1-Wire Net.\r
204 // Using the find alarm command 0xEC will verify that the device\r
205 // is in contact with the 1-Wire Net and is in an 'alarm' state.\r
206 //\r
207 // 'portnum'     - number 0 to MAX_PORTNUM-1.  This number is provided to\r
208 //                 indicate the symbolic port number.\r
209 // 'alarm_only'  - TRUE (1) the find alarm command 0xEC\r
210 //                 is sent instead of the normal search\r
211 //                 command 0xF0.\r
212 //\r
213 // Returns:   TRUE (1) : when the 1-Wire device was verified\r
214 //                       to be on the 1-Wire Net\r
215 //                       with alarm_only == FALSE\r
216 //                       or verified to be on the 1-Wire Net\r
217 //                       AND in an alarm state when\r
218 //                       alarm_only == TRUE.\r
219 //            FALSE (0): the 1-Wire device was not on the\r
220 //                       1-Wire Net or if alarm_only\r
221 //                       == TRUE, the device may be on the\r
222 //                       1-Wire Net but in a non-alarm state.\r
223 //\r
224 SMALLINT owVerify(int portnum, SMALLINT alarm_only)\r
225 {\r
226    if (alarm_only)\r
227       return (TMStrongAlarmAccess(SessionHandle[portnum], StateBuffer[portnum]) == 1);\r
228    else\r
229       return (TMStrongAccess(SessionHandle[portnum], StateBuffer[portnum]) == 1);\r
230 }\r
231 \r
232 //----------------------------------------------------------------------\r
233 // Perform a overdrive MATCH command to select the 1-Wire device with\r
234 // the address in the ID data register.\r
235 //\r
236 // 'portnum'     - number 0 to MAX_PORTNUM-1.  This number is provided to\r
237 //                 indicate the symbolic port number.\r
238 //\r
239 // Returns:  TRUE: If the device is present on the 1-Wire Net and\r
240 //                 can do overdrive then the device is selected.\r
241 //           FALSE: Device is not present or not capable of overdrive.\r
242 //\r
243 //  *Note: This function could be converted to send DS2480\r
244 //         commands in one packet.\r
245 //\r
246 SMALLINT owOverdriveAccess(int portnum)\r
247 {\r
248    return (TMOverAccess(SessionHandle[portnum], StateBuffer[portnum]) == 1);\r
249 }\r