Works now with Windows Visual Studio C++ too
[owTools.git] / windows / owTools / tmexnet.cpp
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 int owInUse = 0;\r
40 \r
41 // external TMEX variables\r
42 extern long SessionHandle[MAX_PORTNUM];\r
43 extern "C" short far pascal TMSearch(long session_handle, void *start_buffer,\r
44                                  short ResetSearch,  short PerformReset, \r
45                                  short SrchCmd);\r
46 extern "C" short pascal TMFirst(long, void *);\r
47 extern "C" short pascal TMNext(long, void *);\r
48 extern "C" short pascal TMAccess(long, void *);\r
49 extern "C" short pascal TMStrongAccess(long, void *);\r
50 extern "C" short pascal TMStrongAlarmAccess(long, void *);\r
51 extern "C" short pascal TMOverAccess(long, void *);\r
52 extern "C" short pascal TMRom(long, void *, short *);\r
53 extern "C" short pascal TMFirstAlarm(long, void *);\r
54 extern "C" short pascal TMNextAlarm(long, void *);\r
55 extern "C" short pascal TMFamilySearchSetup(long, void *, short);\r
56 extern "C" short pascal TMSkipFamily(long, void *);\r
57 extern "C" short pascal TMAutoOverDrive(long, void *, short);\r
58 \r
59 \r
60 //--------------------------------------------------------------------------\r
61 // The 'owFirst' finds the first device on the 1-Wire Net  This function\r
62 // contains one parameter 'alarm_only'.  When\r
63 // 'alarm_only' is TRUE (1) the find alarm command 0xEC is\r
64 // sent instead of the normal search command 0xF0.\r
65 // Using the find alarm command 0xEC will limit the search to only\r
66 // 1-Wire devices that are in an 'alarm' state.\r
67 //\r
68 // 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to\r
69 //                indicate the symbolic port number.\r
70 // 'do_reset'   - TRUE (1) perform reset before search, FALSE (0) do not\r
71 //                perform reset before search.\r
72 // 'alarm_only' - TRUE (1) the find alarm command 0xEC is\r
73 //                sent instead of the normal search command 0xF0\r
74 //\r
75 // Returns:   TRUE (1) : when a 1-Wire device was found and it's\r
76 //                        Serial Number placed in the global SerialNum[portnum]\r
77 //            FALSE (0): There are no devices on the 1-Wire Net.\r
78 //\r
79 SMALLINT owFirst_(int portnum, SMALLINT do_reset, SMALLINT alarm_only)\r
80 {\r
81    return (TMSearch(SessionHandle[portnum], StateBuffer[portnum], 1, \r
82                     (short)do_reset, (short)((alarm_only) ? 0xEC : 0xF0)) == 1);\r
83 }\r
84 \r
85 //--------------------------------------------------------------------------\r
86 // The 'owNext' function does a general search.  This function\r
87 // continues from the previos search state. The search state\r
88 // can be reset by using the 'owFirst' function.\r
89 // This function contains one parameter 'alarm_only'.\r
90 // When 'alarm_only' is TRUE (1) the find alarm command\r
91 // 0xEC is sent instead of the normal search command 0xF0.\r
92 // Using the find alarm command 0xEC will limit the search to only\r
93 // 1-Wire devices that are in an 'alarm' state.\r
94 //\r
95 // 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to\r
96 //                indicate the symbolic port number.\r
97 // 'do_reset'   - TRUE (1) perform reset before search, FALSE (0) do not\r
98 //                perform reset before search.\r
99 // 'alarm_only' - TRUE (1) the find alarm command 0xEC is\r
100 //                sent instead of the normal search command 0xF0\r
101 //\r
102 // Returns:   TRUE (1) : when a 1-Wire device was found and it's\r
103 //                       Serial Number placed in the global SerialNum[portnum]\r
104 //            FALSE (0): when no new device was found.  Either the\r
105 //                       last search was the last device or there\r
106 //                       are no devices on the 1-Wire Net.\r
107 //\r
108 SMALLINT owNext_(int portnum, SMALLINT do_reset, SMALLINT alarm_only)\r
109 {\r
110    return (TMSearch(SessionHandle[portnum], StateBuffer[portnum], 0, \r
111                     (short)do_reset, (short)((alarm_only) ? 0xEC : 0xF0)) == 1);\r
112 }\r
113 \r
114 //--------------------------------------------------------------------------\r
115 // The 'owSerialNum' function either reads or sets the SerialNum buffer\r
116 // that is used in the search functions 'owFirst' and 'owNext'.\r
117 // This function contains two parameters, 'serialnum_buf' is a pointer\r
118 // to a buffer provided by the caller.  'serialnum_buf' should point to\r
119 // an array of 8 unsigned chars.  The second parameter is a flag called\r
120 // 'do_read' that is TRUE (1) if the operation is to read and FALSE\r
121 // (0) if the operation is to set the internal SerialNum buffer from\r
122 // the data in the provided buffer.\r
123 //\r
124 // 'portnum'       - number 0 to MAX_PORTNUM-1.  This number is provided to\r
125 //                   indicate the symbolic port number.\r
126 // 'serialnum_buf' - buffer to that contains the serial number to set\r
127 //                   when do_read = FALSE (0) and buffer to get the serial\r
128 //                   number when do_read = TRUE (1).\r
129 // 'do_read'       - flag to indicate reading (1) or setting (0) the current\r
130 //                   serial number.\r
131 //\r
132 void owSerialNum(int portnum, uchar *serialnum_buf, SMALLINT do_read)\r
133 {\r
134    short ROM[8],i;\r
135 \r
136    // check if reading or writing\r
137    if (do_read)\r
138    {\r
139       ROM[0] = 0;\r
140    }\r
141    else\r
142    {\r
143       for (i = 0; i < 8; i++)\r
144          ROM[i] = serialnum_buf[i];\r
145    }\r
146 \r
147    // call TMEX to read or set the current device\r
148    TMRom(SessionHandle[portnum], StateBuffer[portnum], ROM);\r
149 \r
150    // place in 'serialnum_buf'\r
151    if (do_read)\r
152    {\r
153       for (i = 0; i < 8; i++)\r
154          serialnum_buf[i] = (uchar)ROM[i];\r
155    }\r
156 }\r
157 \r
158 //--------------------------------------------------------------------------\r
159 // Setup the search algorithm to find a certain family of devices\r
160 // the next time a search function is called 'owNext'.\r
161 //\r
162 // 'portnum'       - number 0 to MAX_PORTNUM-1.  This number was provided to\r
163 //                   OpenCOM to indicate the port number.\r
164 // 'search_family' - family code type to set the search algorithm to find\r
165 //                   next.\r
166 //\r
167 void owFamilySearchSetup(int portnum, SMALLINT search_family)\r
168 {\r
169    TMFamilySearchSetup(SessionHandle[portnum], StateBuffer[portnum], \r
170                        (short)search_family);\r
171 }\r
172 \r
173 //--------------------------------------------------------------------------\r
174 // Set the current search state to skip the current family code.\r
175 //\r
176 // 'portnum'     - number 0 to MAX_PORTNUM-1.  This number is provided to\r
177 //                 indicate the symbolic port number.\r
178 //\r
179 void owSkipFamily(int portnum)\r
180 {\r
181    TMSkipFamily(SessionHandle[portnum], StateBuffer[portnum]);\r
182 }\r
183 \r
184 //--------------------------------------------------------------------------\r
185 // The 'owAccess' function resets the 1-Wire and sends a MATCH Serial\r
186 // Number command followed by the current SerialNum code. After this\r
187 // function is complete the 1-Wire device is ready to accept device-specific\r
188 // commands.\r
189 //\r
190 // 'portnum'     - number 0 to MAX_PORTNUM-1.  This number is provided to\r
191 //                 indicate the symbolic port number.\r
192 //\r
193 // Returns:   TRUE (1) : reset indicates present and device is ready\r
194 //                       for commands.\r
195 //            FALSE (0): reset does not indicate presence or echos 'writes'\r
196 //                       are not correct.\r
197 //\r
198 SMALLINT owAccess(int portnum)\r
199 {\r
200    return (TMAccess(SessionHandle[portnum], StateBuffer[portnum]) == 1);\r
201 }\r
202 \r
203 //----------------------------------------------------------------------\r
204 // The function 'owVerify' verifies that the current device\r
205 // is in contact with the 1-Wire Net.\r
206 // Using the find alarm command 0xEC will verify that the device\r
207 // is in contact with the 1-Wire Net and is in an 'alarm' state.\r
208 //\r
209 // 'portnum'     - number 0 to MAX_PORTNUM-1.  This number is provided to\r
210 //                 indicate the symbolic port number.\r
211 // 'alarm_only'  - TRUE (1) the find alarm command 0xEC\r
212 //                 is sent instead of the normal search\r
213 //                 command 0xF0.\r
214 //\r
215 // Returns:   TRUE (1) : when the 1-Wire device was verified\r
216 //                       to be on the 1-Wire Net\r
217 //                       with alarm_only == FALSE\r
218 //                       or verified to be on the 1-Wire Net\r
219 //                       AND in an alarm state when\r
220 //                       alarm_only == TRUE.\r
221 //            FALSE (0): the 1-Wire device was not on the\r
222 //                       1-Wire Net or if alarm_only\r
223 //                       == TRUE, the device may be on the\r
224 //                       1-Wire Net but in a non-alarm state.\r
225 //\r
226 SMALLINT owVerify(int portnum, SMALLINT alarm_only)\r
227 {\r
228    if (alarm_only)\r
229       return (TMStrongAlarmAccess(SessionHandle[portnum], StateBuffer[portnum]) == 1);\r
230    else\r
231       return (TMStrongAccess(SessionHandle[portnum], StateBuffer[portnum]) == 1);\r
232 }\r
233 \r
234 //----------------------------------------------------------------------\r
235 // Perform a overdrive MATCH command to select the 1-Wire device with\r
236 // the address in the ID data register.\r
237 //\r
238 // 'portnum'     - number 0 to MAX_PORTNUM-1.  This number is provided to\r
239 //                 indicate the symbolic port number.\r
240 //\r
241 // Returns:  TRUE: If the device is present on the 1-Wire Net and\r
242 //                 can do overdrive then the device is selected.\r
243 //           FALSE: Device is not present or not capable of overdrive.\r
244 //\r
245 //  *Note: This function could be converted to send DS2480\r
246 //         commands in one packet.\r
247 //\r
248 SMALLINT owOverdriveAccess(int portnum)\r
249 {\r
250    return (TMOverAccess(SessionHandle[portnum], StateBuffer[portnum]) == 1);\r
251 }\r