Add missing calls to refcount_init()
[helenos.git] / uspace / lib / drv / generic / remote_battery_dev.c
blobea73343d3f4ac4795931b42f1344459cef8fda4d
1 /*
2 * Copyright (c) 2012 Maurizio Lombardi
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /** @addtogroup libdrv
30 * @{
32 /** @file
35 #include <async.h>
36 #include <errno.h>
37 #include <ops/battery_dev.h>
38 #include <battery_iface.h>
39 #include <ddf/driver.h>
40 #include <macros.h>
42 /** Read the current battery status from the device
44 * @param sess Session of the device
45 * @param status Current status of the battery
47 * @return EOK on success or an error code
49 errno_t
50 battery_status_get(async_sess_t *sess, battery_status_t *batt_status)
52 sysarg_t status;
54 async_exch_t *exch = async_exchange_begin(sess);
56 errno_t const rc = async_req_1_1(exch, DEV_IFACE_ID(BATTERY_DEV_IFACE),
57 BATTERY_STATUS_GET, &status);
59 async_exchange_end(exch);
61 if (rc == EOK)
62 *batt_status = (battery_status_t) status;
64 return rc;
67 /** Read the current battery charge level from the device
69 * @param sess Session of the device
70 * @param level Battery charge level (0 - 100)
72 * @return EOK on success or an error code
74 errno_t
75 battery_charge_level_get(async_sess_t *sess, int *level)
77 sysarg_t charge_level;
79 async_exch_t *exch = async_exchange_begin(sess);
81 errno_t const rc = async_req_1_1(exch, DEV_IFACE_ID(BATTERY_DEV_IFACE),
82 BATTERY_CHARGE_LEVEL_GET, &charge_level);
84 async_exchange_end(exch);
86 if (rc == EOK)
87 *level = (int) charge_level;
89 return rc;
92 static void remote_battery_status_get(ddf_fun_t *, void *, ipc_call_t *);
93 static void remote_battery_charge_level_get(ddf_fun_t *, void *, ipc_call_t *);
95 /** Remote battery interface operations */
96 static const remote_iface_func_ptr_t remote_battery_dev_iface_ops[] = {
97 [BATTERY_STATUS_GET] = remote_battery_status_get,
98 [BATTERY_CHARGE_LEVEL_GET] = remote_battery_charge_level_get,
101 /** Remote battery interface structure
103 * Interface for processing request from remote clients
104 * addressed by the battery interface.
107 const remote_iface_t remote_battery_dev_iface = {
108 .method_count = ARRAY_SIZE(remote_battery_dev_iface_ops),
109 .methods = remote_battery_dev_iface_ops,
112 /** Process the status_get() request from the remote client
114 * @param fun The function from which the battery status is read
115 * @param ops The local ops structure
118 static void remote_battery_status_get(ddf_fun_t *fun, void *ops,
119 ipc_call_t *call)
121 const battery_dev_ops_t *bops = (battery_dev_ops_t *) ops;
123 if (bops->battery_status_get == NULL) {
124 async_answer_0(call, ENOTSUP);
125 return;
128 battery_status_t batt_status;
129 const errno_t rc = bops->battery_status_get(fun, &batt_status);
131 if (rc != EOK)
132 async_answer_0(call, rc);
133 else
134 async_answer_1(call, rc, batt_status);
137 /** Process the battery_charge_level_get() request from the remote client
139 * @param fun The function from which the battery charge level is read
140 * @param ops The local ops structure
143 static void remote_battery_charge_level_get(ddf_fun_t *fun, void *ops,
144 ipc_call_t *call)
146 const battery_dev_ops_t *bops = (battery_dev_ops_t *) ops;
148 if (bops->battery_charge_level_get == NULL) {
149 async_answer_0(call, ENOTSUP);
150 return;
153 int battery_level;
154 const errno_t rc = bops->battery_charge_level_get(fun, &battery_level);
156 if (rc != EOK)
157 async_answer_0(call, rc);
158 else
159 async_answer_1(call, rc, battery_level);