yes, let's find the beast
[fw/sdcc] / device / examples / ds390 / ow390 / owsesu.c
1 //---------------------------------------------------------------------------
2 // Copyright (C) 2000 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 //  owSesU.C - Acquire and release a Session on the 1-Wire Net.
28 //
29 //  Version: 2.00
30 //
31 //  History: 1.03 -> 2.00  Changed 'MLan' to 'ow'. Added support for 
32 //                         multiple ports.  
33
34 #include <stdio.h>
35 #include "ownet.h"
36 #include "ds2480.h"
37
38 // keep port name for later message when closing
39 static char portname[MAX_PORTNUM][128];
40
41 //---------------------------------------------------------------------------
42 // Attempt to acquire a 1-Wire net using a com port and a DS2480 based
43 // adapter.  
44 //
45 // 'portnum'    - number 0 to MAX_PORTNUM-1.  This number was provided to
46 //                OpenCOM to indicate the port number.
47 // 'port_zstr'  - zero terminated port name.  For this platform
48 //                use format COMX where X is the port number.
49 // 'return_msg' - zero terminated return message. 
50 //
51 // Returns: TRUE - success, COM port opened
52 //
53 int owAcquire(int portnum, char *port_zstr, char *return_msg)
54 {
55    int cnt=0;
56    portname[portnum][0] = 0;
57
58    // attempt to open the communications port
59    if (OpenCOM(portnum,port_zstr))
60       cnt += sprintf(&return_msg[cnt],"%s opened\n",port_zstr);
61    else
62    {
63       cnt += sprintf(&return_msg[cnt],"Could not open port %s,"
64               " aborting.\nClosing port %s.\n",port_zstr,port_zstr);
65       return FALSE;
66    }
67    //printf (return_msg); // TODO: tini bug
68
69    // detect DS2480
70    if (DS2480Detect(portnum))
71       cnt += sprintf(&return_msg[cnt],"DS2480-based adapter detected\n");
72    else
73    {
74       cnt += sprintf(&return_msg[cnt],"DS2480-based adapter not detected, aborting program\n");
75       cnt += sprintf(&return_msg[cnt],"Closing port %s.\n",port_zstr);
76       CloseCOM(portnum);
77       return FALSE;
78    }      
79
80    // success
81    sprintf(portname[portnum],"%s",port_zstr);
82    return TRUE;
83 }
84
85 //---------------------------------------------------------------------------
86 // Release the previously acquired a 1-Wire net.
87 //
88 // 'portnum'    - number 0 to MAX_PORTNUM-1.  This number was provided to
89 //                OpenCOM to indicate the port number.
90 // 'return_msg' - zero terminated return message. 
91 //
92 void owRelease(int portnum, char *return_msg)
93 {
94    // close the communications port
95    sprintf(return_msg,"Closing port %s.\n",portname[portnum]);
96    CloseCOM(portnum);
97 }