* Removed svn:executable property from non-executable files
[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
68    // detect DS2480
69    if (DS2480Detect(portnum))
70       cnt += sprintf(&return_msg[cnt],"DS2480-based adapter detected\n");
71    else
72    {
73       cnt += sprintf(&return_msg[cnt],"DS2480-based adapter not detected, aborting program\n");
74       cnt += sprintf(&return_msg[cnt],"Closing port %s.\n",port_zstr);
75       CloseCOM(portnum);
76       return FALSE;
77    }
78
79    // success
80    sprintf(portname[portnum],"%s",port_zstr);
81    return TRUE;
82 }
83
84 //---------------------------------------------------------------------------
85 // Release the previously acquired a 1-Wire net.
86 //
87 // 'portnum'    - number 0 to MAX_PORTNUM-1.  This number was provided to
88 //                OpenCOM to indicate the port number.
89 // 'return_msg' - zero terminated return message.
90 //
91 void owRelease(int portnum, char *return_msg)
92 {
93    // close the communications port
94    sprintf(return_msg,"Closing port %s.\n",portname[portnum]);
95    CloseCOM(portnum);
96 }