1 //---------------------------------------------------------------------------
\r
2 // Copyright (C) 2001 Dallas Semiconductor Corporation, All Rights Reserved.
\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
11 // The above copyright notice and this permission notice shall be included
\r
12 // in all copies or substantial portions of the Software.
\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
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
25 //---------------------------------------------------------------------------
\r
27 // owsestmx.c - Acquire and release a Session on the 1-Wire Net using TMEX.
\r
28 // (Requires TMEX 3.11 or newer)
\r
32 // History: 1.03 -> 2.00 Changed 'MLan' to 'ow'. Added support for
\r
34 // 2.00 -> 2.01 Added support for owError library.
\r
38 #include <windows.h>
\r
41 // external function prototypes
\r
42 extern "C" long far pascal TMExtendedStartSession(short, short, void far *);
\r
43 extern "C" short far pascal TMEndSession(long);
\r
44 extern "C" short far pascal TMClose(long);
\r
45 extern "C" short far pascal TMSetup(long);
\r
46 extern "C" short far pascal TMReadDefaultPort(short far *, short far *);
\r
47 extern "C" short far pascal TMOneWireCom(
\r
48 long session_handle, // session handle for the desired 1-Wire network
\r
49 short Operation,// Read (1) or Set (0) the communication rate
\r
50 short TimeMode // communication rate number
\r
52 short PortNum=1,PortType=2;
\r
53 long SessionHandle[MAX_PORTNUM];
\r
54 SMALLINT handle_init = FALSE;
\r
56 //---------------------------------------------------------------------------
\r
57 // Attempt to acquire a 1-Wire net using a com port and a DS2480 based
\r
60 // 'port_zstr' - zero terminated port name. For this platform
\r
61 // use format {port number, port type}.
\r
63 // Returns: port number and -1 if not successful in setting up the port.
\r
65 int owAcquireEx(char *port_zstr)
\r
68 int string_counter, counter, i, lenmax;
\r
69 char portnum_str[15];
\r
70 char porttype_str[15];
\r
71 void *tmex_options = NULL;
\r
75 for(i=0; i<MAX_PORTNUM; i++)
\r
76 SessionHandle[i] = 0;
\r
80 // check to find first available handle slot
\r
81 for(portnum = 0; portnum<MAX_PORTNUM; portnum++)
\r
83 if(!SessionHandle[portnum])
\r
86 OWASSERT( portnum<MAX_PORTNUM, OWERROR_PORTNUM_ERROR, -1 );
\r
88 // convert the string in port_zstr to be the port number and port type
\r
91 lenmax = strlen(port_zstr);
\r
98 portnum_str[counter] = port_zstr[string_counter];
\r
103 while((port_zstr[string_counter] != ',') && (string_counter <= lenmax));
\r
105 portnum_str[counter] = '\0';
\r
112 porttype_str[counter] = port_zstr[string_counter];
\r
117 while((port_zstr[string_counter] != '}') && (string_counter <= lenmax));
\r
119 porttype_str[counter] = '\0';
\r
121 PortNum = atoi(portnum_str);
\r
122 PortType = atoi(porttype_str);
\r
126 SessionHandle[portnum] = TMExtendedStartSession(PortNum,PortType,tmex_options);
\r
128 // check the session handle
\r
129 if (SessionHandle[portnum] <= 0)
\r
131 OWERROR(OWERROR_GET_SYSTEM_RESOURCE_FAILED);
\r
132 SessionHandle[portnum] = 0;
\r
137 if (TMSetup(SessionHandle[portnum]) != 1)
\r
139 TMClose(SessionHandle[portnum]);
\r
140 TMEndSession(SessionHandle[portnum]);
\r
141 OWERROR(OWERROR_SYSTEM_RESOURCE_INIT_FAILED);
\r
142 SessionHandle[portnum] = 0;
\r
149 //---------------------------------------------------------------------------
\r
150 // Attempt to acquire a 1-Wire net using a com port and a DS2480 based
\r
153 // 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to
\r
154 // indicate the symbolic port number.
\r
155 // 'port_zstr' - zero terminated port name. For this platform
\r
156 // use format COMX where X is the port number.
\r
158 // Returns: TRUE - success, COM port opened
\r
160 SMALLINT owAcquire(int portnum, char *port_zstr)
\r
166 for(i=0; i<MAX_PORTNUM; i++)
\r
167 SessionHandle[i] = 0;
\r
168 handle_init = TRUE;
\r
171 OWASSERT( portnum<MAX_PORTNUM && portnum>=0 && !SessionHandle[portnum],
\r
172 OWERROR_PORTNUM_ERROR, FALSE );
\r
174 // read the default PortNum and PortType
\r
175 TMReadDefaultPort(&PortNum,&PortType);
\r
177 // convert the string in port_zstr to be the port number
\r
178 PortNum = atoi(port_zstr);
\r
181 SessionHandle[portnum] = TMExtendedStartSession(PortNum,PortType,NULL);
\r
183 // check the session handle
\r
184 if (SessionHandle[portnum] <= 0)
\r
186 OWERROR(OWERROR_GET_SYSTEM_RESOURCE_FAILED);
\r
187 SessionHandle[portnum] = 0;
\r
192 if (TMSetup(SessionHandle[portnum]) != 1)
\r
194 TMClose(SessionHandle[portnum]);
\r
195 TMEndSession(SessionHandle[portnum]);
\r
196 OWERROR(OWERROR_SYSTEM_RESOURCE_INIT_FAILED);
\r
197 SessionHandle[portnum] = 0;
\r
204 //---------------------------------------------------------------------------
\r
205 // Release the previously acquired a 1-Wire net.
\r
207 // 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to
\r
208 // indicate the symbolic port number.
\r
210 void owRelease(int portnum)
\r
212 TMClose(SessionHandle[portnum]);
\r
213 TMEndSession(SessionHandle[portnum]);
\r
214 SessionHandle[portnum] = 0;
\r