VOC Optimation
[owSlave2.git] / tools_cmd / rwOW / tmexses.c
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 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
47 \r
48 short PortNum=1,PortType=2;\r
49 long  SessionHandle[MAX_PORTNUM];\r
50 SMALLINT handle_init = FALSE;\r
51 \r
52 //---------------------------------------------------------------------------\r
53 // Attempt to acquire a 1-Wire net using a com port and a DS2480 based\r
54 // adapter.\r
55 //\r
56 // 'port_zstr'     - zero terminated port name.  For this platform\r
57 //                   use format {port number, port type}.\r
58 //\r
59 // Returns: port number and -1 if not successful in setting up the port.\r
60 //\r
61 int owAcquireEx(char *port_zstr)\r
62 {\r
63    int portnum;\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
68 \r
69    if(!handle_init)\r
70    {\r
71       for(i=0; i<MAX_PORTNUM; i++)\r
72          SessionHandle[i] = 0;\r
73       handle_init = TRUE;\r
74    }\r
75 \r
76    // check to find first available handle slot\r
77    for(portnum = 0; portnum<MAX_PORTNUM; portnum++)\r
78    {\r
79       if(!SessionHandle[portnum])\r
80          break;\r
81    }\r
82    OWASSERT( portnum<MAX_PORTNUM, OWERROR_PORTNUM_ERROR, -1 );\r
83 \r
84    // convert the string in port_zstr to be the port number and port type\r
85    if(port_zstr)\r
86    {\r
87       lenmax = strlen(port_zstr);\r
88       if (lenmax > 12)\r
89          lenmax = 12;\r
90       string_counter = 1;\r
91       counter = 0;\r
92       do\r
93       {\r
94          portnum_str[counter] = port_zstr[string_counter];\r
95 \r
96          counter++;\r
97          string_counter++;\r
98       }\r
99       while((port_zstr[string_counter] != ',') && (string_counter <= lenmax));\r
100 \r
101       portnum_str[counter] = '\0';\r
102 \r
103       string_counter++;\r
104       counter = 0;\r
105 \r
106       do\r
107       {\r
108          porttype_str[counter] = port_zstr[string_counter];\r
109 \r
110          counter++;\r
111          string_counter++;\r
112       }\r
113       while((port_zstr[string_counter] != '}') && (string_counter <= lenmax));\r
114 \r
115       porttype_str[counter] = '\0';\r
116 \r
117       PortNum = atoi(portnum_str);\r
118       PortType = atoi(porttype_str);\r
119    }\r
120 \r
121    // open a session\r
122    SessionHandle[portnum] = TMExtendedStartSession(PortNum,PortType,tmex_options);\r
123 \r
124    // check the session handle\r
125    if (SessionHandle[portnum] <= 0)\r
126    {\r
127       OWERROR(OWERROR_GET_SYSTEM_RESOURCE_FAILED);\r
128       SessionHandle[portnum] = 0;\r
129       return -1;\r
130    }\r
131 \r
132    // setup the port\r
133    if (TMSetup(SessionHandle[portnum]) != 1)\r
134    {\r
135       TMClose(SessionHandle[portnum]);\r
136       TMEndSession(SessionHandle[portnum]);\r
137       OWERROR(OWERROR_SYSTEM_RESOURCE_INIT_FAILED);\r
138       SessionHandle[portnum] = 0;\r
139       return -1;\r
140    }\r
141 \r
142    return portnum;\r
143 }\r
144 \r
145 //---------------------------------------------------------------------------\r
146 // Attempt to acquire a 1-Wire net using a com port and a DS2480 based\r
147 // adapter.\r
148 //\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
153 //\r
154 // Returns: TRUE - success, COM port opened\r
155 //\r
156 SMALLINT owAcquire(int portnum, char *port_zstr)\r
157 {\r
158    int i;\r
159 \r
160    if(!handle_init)\r
161    {\r
162       for(i=0; i<MAX_PORTNUM; i++)\r
163          SessionHandle[i] = 0;\r
164       handle_init = TRUE;\r
165    }\r
166 \r
167    OWASSERT( portnum<MAX_PORTNUM && portnum>=0 && !SessionHandle[portnum],\r
168              OWERROR_PORTNUM_ERROR, FALSE );\r
169 \r
170    // read the default PortNum and PortType\r
171    TMReadDefaultPort(&PortNum,&PortType);\r
172 \r
173    // convert the string in port_zstr to be the port number\r
174    PortNum = atoi(port_zstr);\r
175 \r
176    // open a session\r
177    SessionHandle[portnum] = TMExtendedStartSession(PortNum,PortType,NULL);\r
178 \r
179    // check the session handle\r
180    if (SessionHandle[portnum] <= 0)\r
181    {\r
182       OWERROR(OWERROR_GET_SYSTEM_RESOURCE_FAILED);\r
183       SessionHandle[portnum] = 0;\r
184       return FALSE;\r
185    }\r
186 \r
187    // setup the port\r
188    if (TMSetup(SessionHandle[portnum]) != 1)\r
189    {\r
190       TMClose(SessionHandle[portnum]);\r
191       TMEndSession(SessionHandle[portnum]);\r
192       OWERROR(OWERROR_SYSTEM_RESOURCE_INIT_FAILED);\r
193       SessionHandle[portnum] = 0;\r
194       return FALSE;\r
195    }\r
196 \r
197    return TRUE;\r
198 }\r
199 \r
200 //---------------------------------------------------------------------------\r
201 // Release the previously acquired a 1-Wire net.\r
202 //\r
203 // 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to\r
204 //                indicate the symbolic port number.\r
205 //\r
206 void owRelease(int portnum)\r
207 {\r
208    TMClose(SessionHandle[portnum]);\r
209    TMEndSession(SessionHandle[portnum]);\r
210    SessionHandle[portnum] = 0;\r
211 }\r
212 \r