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 long far pascal TMExtendedStartSession(short, short, void far *);
\r
43 extern short far pascal TMEndSession(long);
\r
44 extern short far pascal TMClose(long);
\r
45 extern short far pascal TMSetup(long);
\r
46 extern short far pascal TMReadDefaultPort(short far *, short far *);
\r
48 short PortNum=1,PortType=2;
\r
49 long SessionHandle[MAX_PORTNUM];
\r
50 SMALLINT handle_init = FALSE;
\r
52 //---------------------------------------------------------------------------
\r
53 // Attempt to acquire a 1-Wire net using a com port and a DS2480 based
\r
56 // 'port_zstr' - zero terminated port name. For this platform
\r
57 // use format {port number, port type}.
\r
59 // Returns: port number and -1 if not successful in setting up the port.
\r
61 int owAcquireEx(char *port_zstr)
\r
64 int string_counter, counter, i, lenmax;
\r
65 char portnum_str[15];
\r
66 char porttype_str[15];
\r
67 void *tmex_options = NULL;
\r
71 for(i=0; i<MAX_PORTNUM; i++)
\r
72 SessionHandle[i] = 0;
\r
76 // check to find first available handle slot
\r
77 for(portnum = 0; portnum<MAX_PORTNUM; portnum++)
\r
79 if(!SessionHandle[portnum])
\r
82 OWASSERT( portnum<MAX_PORTNUM, OWERROR_PORTNUM_ERROR, -1 );
\r
84 // convert the string in port_zstr to be the port number and port type
\r
87 lenmax = strlen(port_zstr);
\r
94 portnum_str[counter] = port_zstr[string_counter];
\r
99 while((port_zstr[string_counter] != ',') && (string_counter <= lenmax));
\r
101 portnum_str[counter] = '\0';
\r
108 porttype_str[counter] = port_zstr[string_counter];
\r
113 while((port_zstr[string_counter] != '}') && (string_counter <= lenmax));
\r
115 porttype_str[counter] = '\0';
\r
117 PortNum = atoi(portnum_str);
\r
118 PortType = atoi(porttype_str);
\r
122 SessionHandle[portnum] = TMExtendedStartSession(PortNum,PortType,tmex_options);
\r
124 // check the session handle
\r
125 if (SessionHandle[portnum] <= 0)
\r
127 OWERROR(OWERROR_GET_SYSTEM_RESOURCE_FAILED);
\r
128 SessionHandle[portnum] = 0;
\r
133 if (TMSetup(SessionHandle[portnum]) != 1)
\r
135 TMClose(SessionHandle[portnum]);
\r
136 TMEndSession(SessionHandle[portnum]);
\r
137 OWERROR(OWERROR_SYSTEM_RESOURCE_INIT_FAILED);
\r
138 SessionHandle[portnum] = 0;
\r
145 //---------------------------------------------------------------------------
\r
146 // Attempt to acquire a 1-Wire net using a com port and a DS2480 based
\r
149 // 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to
\r
150 // indicate the symbolic port number.
\r
151 // 'port_zstr' - zero terminated port name. For this platform
\r
152 // use format COMX where X is the port number.
\r
154 // Returns: TRUE - success, COM port opened
\r
156 SMALLINT owAcquire(int portnum, char *port_zstr)
\r
162 for(i=0; i<MAX_PORTNUM; i++)
\r
163 SessionHandle[i] = 0;
\r
164 handle_init = TRUE;
\r
167 OWASSERT( portnum<MAX_PORTNUM && portnum>=0 && !SessionHandle[portnum],
\r
168 OWERROR_PORTNUM_ERROR, FALSE );
\r
170 // read the default PortNum and PortType
\r
171 TMReadDefaultPort(&PortNum,&PortType);
\r
173 // convert the string in port_zstr to be the port number
\r
174 PortNum = atoi(port_zstr);
\r
177 SessionHandle[portnum] = TMExtendedStartSession(PortNum,PortType,NULL);
\r
179 // check the session handle
\r
180 if (SessionHandle[portnum] <= 0)
\r
182 OWERROR(OWERROR_GET_SYSTEM_RESOURCE_FAILED);
\r
183 SessionHandle[portnum] = 0;
\r
188 if (TMSetup(SessionHandle[portnum]) != 1)
\r
190 TMClose(SessionHandle[portnum]);
\r
191 TMEndSession(SessionHandle[portnum]);
\r
192 OWERROR(OWERROR_SYSTEM_RESOURCE_INIT_FAILED);
\r
193 SessionHandle[portnum] = 0;
\r
200 //---------------------------------------------------------------------------
\r
201 // Release the previously acquired a 1-Wire net.
\r
203 // 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to
\r
204 // indicate the symbolic port number.
\r
206 void owRelease(int portnum)
\r
208 TMClose(SessionHandle[portnum]);
\r
209 TMEndSession(SessionHandle[portnum]);
\r
210 SessionHandle[portnum] = 0;
\r