1 /*****************************************************************************/
2 // Serial port transport add-on.
7 // This application and all source files used in its construction, except
8 // where noted, are licensed under the MIT License, and have been written
11 // Copyright (c) 2001-2003 OpenBeOS Project
13 // Permission is hereby granted, free of charge, to any person obtaining a
14 // copy of this software and associated documentation files (the "Software"),
15 // to deal in the Software without restriction, including without limitation
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 // and/or sell copies of the Software, and to permit persons to whom the
18 // Software is furnished to do so, subject to the following conditions:
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 // DEALINGS IN THE SOFTWARE.
30 /*****************************************************************************/
35 #include <StorageKit.h>
36 #include <SupportKit.h>
38 #include "PrintTransportAddOn.h"
40 class SerialTransport
: public BDataIO
{
42 SerialTransport(BDirectory
* printer
, BMessage
* msg
);
45 status_t
InitCheck() { return fFile
> -1 ? B_OK
: B_ERROR
; }
47 ssize_t
Read(void* buffer
, size_t size
);
48 ssize_t
Write(const void* buffer
, size_t size
);
54 // Impelmentation of SerialTransport
55 SerialTransport::SerialTransport(BDirectory
* printer
, BMessage
* msg
)
59 char device
[B_PATH_NAME_LENGTH
];
60 bool bidirectional
= true;
62 unsigned int size
= printer
->ReadAttr("transport_address", B_STRING_TYPE
, 0, address
, sizeof(address
));
63 if (size
<= 0 || size
>= sizeof(address
)) return;
64 address
[size
] = 0; // make sure string is 0-terminated
66 strcat(strcpy(device
, "/dev/ports/"), address
);
67 fFile
= open(device
, O_RDWR
| O_EXCL
, 0);
69 // Try unidirectional access mode
70 bidirectional
= false;
71 fFile
= open(device
, O_WRONLY
| O_EXCL
, 0);
78 // Caller don't care about transport init message output content...
82 msg
->AddBool("bidirectional", bidirectional
);
83 msg
->AddString("_serial/DeviceName", device
);
87 SerialTransport::~SerialTransport()
89 if (InitCheck() == B_OK
)
93 ssize_t
SerialTransport::Read(void* buffer
, size_t size
)
95 return read(fFile
, buffer
, size
);
98 ssize_t
SerialTransport::Write(const void* buffer
, size_t size
)
100 return write(fFile
, buffer
, size
);
103 BDataIO
* instantiate_transport(BDirectory
* printer
, BMessage
* msg
)
105 SerialTransport
* transport
= new SerialTransport(printer
, msg
);
106 if (transport
->InitCheck() == B_OK
)
113 status_t
list_transport_ports(BMessage
* msg
)
115 BDirectory
dir("/dev/ports");
118 if ((rc
=dir
.InitCheck()) != B_OK
)
121 if ((rc
=dir
.Rewind()) != B_OK
)
125 while(dir
.GetNextRef(&ref
) == B_OK
)
126 msg
->AddString("port_id", ref
.name
);