793d6503be033a1868b6d4452528926624177dd1
[owSlave2.git] / tools_cmd / rwOW / tmexses.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 //  owsestmx.c - Acquire and release a Session on the 1-Wire Net using TMEX.
28 //               (Requires TMEX 3.11 or newer)
29 //
30 //  Version: 2.01
31 //
32 //  History: 1.03 -> 2.00  Changed 'MLan' to 'ow'. Added support for
33 //                         multiple ports.
34 //           2.00 -> 2.01  Added support for owError library.
35 //
36
37 #include <stdio.h>
38 #include <windows.h>
39 #include "ownet.h"
40
41 // external function prototypes
42 extern long  far pascal TMExtendedStartSession(short, short, void far *);
43 extern short far pascal TMEndSession(long);
44 extern short far pascal TMClose(long);
45 extern short far pascal TMSetup(long);
46 extern short far pascal TMReadDefaultPort(short far *, short far *);
47
48 short PortNum=1,PortType=2;
49 long  SessionHandle[MAX_PORTNUM];
50 SMALLINT handle_init = FALSE;
51
52 //---------------------------------------------------------------------------
53 // Attempt to acquire a 1-Wire net using a com port and a DS2480 based
54 // adapter.
55 //
56 // 'port_zstr'     - zero terminated port name.  For this platform
57 //                   use format {port number, port type}.
58 //
59 // Returns: port number and -1 if not successful in setting up the port.
60 //
61 int owAcquireEx(char *port_zstr)
62 {
63    int portnum;
64    int string_counter, counter, i, lenmax;
65    char portnum_str[15];
66    char porttype_str[15];
67    void *tmex_options = NULL;
68
69    if(!handle_init)
70    {
71       for(i=0; i<MAX_PORTNUM; i++)
72          SessionHandle[i] = 0;
73       handle_init = TRUE;
74    }
75
76    // check to find first available handle slot
77    for(portnum = 0; portnum<MAX_PORTNUM; portnum++)
78    {
79       if(!SessionHandle[portnum])
80          break;
81    }
82    OWASSERT( portnum<MAX_PORTNUM, OWERROR_PORTNUM_ERROR, -1 );
83
84    // convert the string in port_zstr to be the port number and port type
85    if(port_zstr)
86    {
87       lenmax = strlen(port_zstr);
88       if (lenmax > 12)
89          lenmax = 12;
90       string_counter = 1;
91       counter = 0;
92       do
93       {
94          portnum_str[counter] = port_zstr[string_counter];
95
96          counter++;
97          string_counter++;
98       }
99       while((port_zstr[string_counter] != ',') && (string_counter <= lenmax));
100
101       portnum_str[counter] = '\0';
102
103       string_counter++;
104       counter = 0;
105
106       do
107       {
108          porttype_str[counter] = port_zstr[string_counter];
109
110          counter++;
111          string_counter++;
112       }
113       while((port_zstr[string_counter] != '}') && (string_counter <= lenmax));
114
115       porttype_str[counter] = '\0';
116
117       PortNum = atoi(portnum_str);
118       PortType = atoi(porttype_str);
119    }
120
121    // open a session
122    SessionHandle[portnum] = TMExtendedStartSession(PortNum,PortType,tmex_options);
123
124    // check the session handle
125    if (SessionHandle[portnum] <= 0)
126    {
127       OWERROR(OWERROR_GET_SYSTEM_RESOURCE_FAILED);
128       SessionHandle[portnum] = 0;
129       return -1;
130    }
131
132    // setup the port
133    if (TMSetup(SessionHandle[portnum]) != 1)
134    {
135       TMClose(SessionHandle[portnum]);
136       TMEndSession(SessionHandle[portnum]);
137       OWERROR(OWERROR_SYSTEM_RESOURCE_INIT_FAILED);
138       SessionHandle[portnum] = 0;
139       return -1;
140    }
141
142    return portnum;
143 }
144
145 //---------------------------------------------------------------------------
146 // Attempt to acquire a 1-Wire net using a com port and a DS2480 based
147 // adapter.
148 //
149 // 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
150 //                indicate the symbolic port number.
151 // 'port_zstr'  - zero terminated port name.  For this platform
152 //                use format COMX where X is the port number.
153 //
154 // Returns: TRUE - success, COM port opened
155 //
156 SMALLINT owAcquire(int portnum, char *port_zstr)
157 {
158    int i;
159
160    if(!handle_init)
161    {
162       for(i=0; i<MAX_PORTNUM; i++)
163          SessionHandle[i] = 0;
164       handle_init = TRUE;
165    }
166
167    OWASSERT( portnum<MAX_PORTNUM && portnum>=0 && !SessionHandle[portnum],
168              OWERROR_PORTNUM_ERROR, FALSE );
169
170    // read the default PortNum and PortType
171    TMReadDefaultPort(&PortNum,&PortType);
172
173    // convert the string in port_zstr to be the port number
174    PortNum = atoi(port_zstr);
175
176    // open a session
177    SessionHandle[portnum] = TMExtendedStartSession(PortNum,PortType,NULL);
178
179    // check the session handle
180    if (SessionHandle[portnum] <= 0)
181    {
182       OWERROR(OWERROR_GET_SYSTEM_RESOURCE_FAILED);
183       SessionHandle[portnum] = 0;
184       return FALSE;
185    }
186
187    // setup the port
188    if (TMSetup(SessionHandle[portnum]) != 1)
189    {
190       TMClose(SessionHandle[portnum]);
191       TMEndSession(SessionHandle[portnum]);
192       OWERROR(OWERROR_SYSTEM_RESOURCE_INIT_FAILED);
193       SessionHandle[portnum] = 0;
194       return FALSE;
195    }
196
197    return TRUE;
198 }
199
200 //---------------------------------------------------------------------------
201 // Release the previously acquired a 1-Wire net.
202 //
203 // 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
204 //                indicate the symbolic port number.
205 //
206 void owRelease(int portnum)
207 {
208    TMClose(SessionHandle[portnum]);
209    TMEndSession(SessionHandle[portnum]);
210    SessionHandle[portnum] = 0;
211 }
212