[69a13a4] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 Maurizio Lombardi
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 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.
|
---|
| 16 | *
|
---|
| 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.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup libdrv
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <async.h>
|
---|
| 36 | #include <errno.h>
|
---|
| 37 | #include <ops/battery_dev.h>
|
---|
[26c03dbd] | 38 | #include <battery_iface.h>
|
---|
[69a13a4] | 39 | #include <ddf/driver.h>
|
---|
[9be30cdf] | 40 | #include <macros.h>
|
---|
[69a13a4] | 41 |
|
---|
[26c03dbd] | 42 | /** Read the current battery status from the device
|
---|
| 43 | *
|
---|
| 44 | * @param sess Session of the device
|
---|
| 45 | * @param status Current status of the battery
|
---|
| 46 | *
|
---|
| 47 | * @return EOK on success or a negative error code
|
---|
| 48 | */
|
---|
| 49 | int
|
---|
| 50 | battery_status_get(async_sess_t *sess, battery_status_t *batt_status)
|
---|
| 51 | {
|
---|
| 52 | sysarg_t status;
|
---|
| 53 |
|
---|
| 54 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 55 |
|
---|
| 56 | int const rc = async_req_1_1(exch, DEV_IFACE_ID(BATTERY_DEV_IFACE),
|
---|
| 57 | BATTERY_STATUS_GET, &status);
|
---|
| 58 |
|
---|
| 59 | async_exchange_end(exch);
|
---|
| 60 |
|
---|
| 61 | if (rc == EOK)
|
---|
| 62 | *batt_status = (battery_status_t) status;
|
---|
| 63 |
|
---|
| 64 | return rc;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | /** Read the current battery charge level from the device
|
---|
| 68 | *
|
---|
| 69 | * @param sess Session of the device
|
---|
| 70 | * @param level Battery charge level (0 - 100)
|
---|
| 71 | *
|
---|
| 72 | * @return EOK on success or a negative error code
|
---|
| 73 | */
|
---|
| 74 | int
|
---|
| 75 | battery_charge_level_get(async_sess_t *sess, int *level)
|
---|
| 76 | {
|
---|
| 77 | sysarg_t charge_level;
|
---|
| 78 |
|
---|
| 79 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 80 |
|
---|
| 81 | int const rc = async_req_1_1(exch, DEV_IFACE_ID(BATTERY_DEV_IFACE),
|
---|
| 82 | BATTERY_CHARGE_LEVEL_GET, &charge_level);
|
---|
| 83 |
|
---|
| 84 | async_exchange_end(exch);
|
---|
| 85 |
|
---|
| 86 | if (rc == EOK)
|
---|
| 87 | *level = (int) charge_level;
|
---|
| 88 |
|
---|
| 89 | return rc;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[69a13a4] | 92 | static void remote_battery_status_get(ddf_fun_t *, void *, ipc_callid_t,
|
---|
| 93 | ipc_call_t *);
|
---|
| 94 | static void remote_battery_charge_level_get(ddf_fun_t *, void *, ipc_callid_t,
|
---|
| 95 | ipc_call_t *);
|
---|
| 96 |
|
---|
| 97 | /** Remote battery interface operations */
|
---|
[9be30cdf] | 98 | static const remote_iface_func_ptr_t remote_battery_dev_iface_ops[] = {
|
---|
[c1ee46f] | 99 | [BATTERY_STATUS_GET] = remote_battery_status_get,
|
---|
| 100 | [BATTERY_CHARGE_LEVEL_GET] = remote_battery_charge_level_get,
|
---|
[69a13a4] | 101 | };
|
---|
| 102 |
|
---|
| 103 | /** Remote battery interface structure
|
---|
| 104 | *
|
---|
| 105 | * Interface for processing request from remote clients
|
---|
| 106 | * addressed by the battery interface.
|
---|
| 107 | *
|
---|
| 108 | */
|
---|
[7f80313] | 109 | const remote_iface_t remote_battery_dev_iface = {
|
---|
[9be30cdf] | 110 | .method_count = ARRAY_SIZE(remote_battery_dev_iface_ops),
|
---|
[69a13a4] | 111 | .methods = remote_battery_dev_iface_ops,
|
---|
| 112 | };
|
---|
| 113 |
|
---|
| 114 | /** Process the status_get() request from the remote client
|
---|
| 115 | *
|
---|
| 116 | * @param fun The function from which the battery status is read
|
---|
| 117 | * @param ops The local ops structure
|
---|
| 118 | */
|
---|
| 119 | static void
|
---|
| 120 | remote_battery_status_get(ddf_fun_t *fun, void *ops, ipc_callid_t callid,
|
---|
| 121 | ipc_call_t *call)
|
---|
| 122 | {
|
---|
| 123 | const battery_dev_ops_t *bops = (battery_dev_ops_t *) ops;
|
---|
| 124 |
|
---|
| 125 | if (bops->battery_status_get == NULL) {
|
---|
| 126 | async_answer_0(callid, ENOTSUP);
|
---|
| 127 | return;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | battery_status_t batt_status;
|
---|
| 131 | const int rc = bops->battery_status_get(fun, &batt_status);
|
---|
| 132 |
|
---|
| 133 | if (rc != EOK)
|
---|
| 134 | async_answer_0(callid, rc);
|
---|
| 135 | else
|
---|
| 136 | async_answer_1(callid, rc, batt_status);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | /** Process the battery_charge_level_get() request from the remote client
|
---|
| 140 | *
|
---|
| 141 | * @param fun The function from which the battery charge level is read
|
---|
| 142 | * @param ops The local ops structure
|
---|
| 143 | *
|
---|
| 144 | */
|
---|
| 145 | static void
|
---|
| 146 | remote_battery_charge_level_get(ddf_fun_t *fun, void *ops, ipc_callid_t callid,
|
---|
| 147 | ipc_call_t *call)
|
---|
| 148 | {
|
---|
| 149 | const battery_dev_ops_t *bops = (battery_dev_ops_t *) ops;
|
---|
| 150 |
|
---|
| 151 | if (bops->battery_charge_level_get == NULL) {
|
---|
| 152 | async_answer_0(callid, ENOTSUP);
|
---|
| 153 | return;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | int battery_level;
|
---|
| 157 | const int rc = bops->battery_charge_level_get(fun, &battery_level);
|
---|
| 158 |
|
---|
| 159 | if (rc != EOK)
|
---|
| 160 | async_answer_0(callid, rc);
|
---|
| 161 | else
|
---|
| 162 | async_answer_1(callid, rc, battery_level);
|
---|
| 163 | }
|
---|
| 164 |
|
---|