Tizen 2.1 base
[platform/upstream/hplip.git] / scan / sane / io.c
1 /************************************************************************************\
2
3   io.c - HP SANE backend for multi-function peripherals (libsane-hpaio)
4
5   (c) 2001-2008 Copyright Hewlett-Packard Development Company, LP
6
7   Permission is hereby granted, free of charge, to any person obtaining a copy 
8   of this software and associated documentation files (the "Software"), to deal 
9   in the Software without restriction, including without limitation the rights 
10   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
11   of the Software, and to permit persons to whom the Software is furnished to do 
12   so, subject to the following conditions:
13
14   The above copyright notice and this permission notice shall be included in all
15   copies or substantial portions of the Software.
16
17   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
18   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 
19   FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 
20   COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
21   IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
22   WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24   Contributing Authors: Don Welch, David Suffield 
25
26 \************************************************************************************/
27
28 #include <stdio.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31 #include <pwd.h>
32 #include "hpmud.h"
33 #include "common.h"
34 #include "pml.h"
35 #include "io.h"
36 #ifdef HAVE_DBUS
37 #include <dbus/dbus.h>
38 #endif
39 #define DEBUG_DECLARE_ONLY
40 #include "sanei_debug.h"
41
42 #ifdef HAVE_DBUS
43 DBusError dbus_err;
44 DBusConnection * dbus_conn;
45
46 int __attribute__ ((visibility ("hidden"))) InitDbus(void)
47 {
48    dbus_error_init(&dbus_err);
49    dbus_conn = dbus_bus_get(DBUS_BUS_SYSTEM, &dbus_err);
50     
51    if (dbus_error_is_set(&dbus_err))
52    { 
53       BUG("dBus Connection Error (%s)!\n", dbus_err.message); 
54       dbus_error_free(&dbus_err); 
55    }
56
57    if (NULL == dbus_conn) 
58    { 
59       return 0; 
60    }
61
62    return 1;
63 }
64
65 int __attribute__ ((visibility ("hidden"))) SendScanEvent(char *device_uri, int event)
66 {
67     DBusMessage * msg = dbus_message_new_signal(DBUS_PATH, DBUS_INTERFACE, "Event");
68     char * printer = "";
69     char * title = "";
70     int jobid = 0; 
71     char * username = "";
72
73     uid_t uid = getuid();
74     struct passwd *p = getpwuid (uid);
75     username = p->pw_name;
76
77     if (NULL == username)
78         username = "";
79
80     if (NULL == msg)
81     {
82         BUG("dbus message is NULL!\n");
83         return 0;
84     }
85
86     dbus_message_append_args(msg, 
87         DBUS_TYPE_STRING, &device_uri,
88         DBUS_TYPE_STRING, &printer,
89         DBUS_TYPE_UINT32, &event, 
90         DBUS_TYPE_STRING, &username, 
91         DBUS_TYPE_UINT32, &jobid,
92         DBUS_TYPE_STRING, &title, 
93         DBUS_TYPE_INVALID);
94
95     if (!dbus_connection_send(dbus_conn, msg, NULL))
96     {
97         BUG("dbus message send failed!\n");
98         return 0;
99     }
100
101     dbus_connection_flush(dbus_conn);
102     dbus_message_unref(msg);
103
104     return 1;
105 }
106 #else
107 int __attribute__ ((visibility ("hidden"))) InitDbus(void)
108 {
109    return 1;
110 }
111 int __attribute__ ((visibility ("hidden"))) SendScanEvent(char *device_uri, int event)
112 {
113     return 1;
114 }
115 #endif  /* HAVE_DBUS */
116  
117 /* Read full requested data length in BUFFER_SIZE chunks. Return number of bytes read. */
118 int __attribute__ ((visibility ("hidden"))) ReadChannelEx(int deviceid, int channelid, unsigned char * buffer, int length, int timeout)
119 {
120    int n, len, size, total=0;
121    enum HPMUD_RESULT stat;
122
123    size = length;
124
125    while(size > 0)
126    {
127       len = size > HPMUD_BUFFER_SIZE ? HPMUD_BUFFER_SIZE : size;
128         
129       stat = hpmud_read_channel(deviceid, channelid, buffer+total, len, timeout, &n);
130       if (n <= 0)
131       {
132          break;    /* error or timeout */
133       }
134       size-=n;
135       total+=n;
136    }
137         
138    return total;
139 }
140