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