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