| 1 | /*
|
|---|
| 2 | * SPDX-FileCopyrightText: 2012 Maurizio Lombardi
|
|---|
| 3 | *
|
|---|
| 4 | * SPDX-License-Identifier: BSD-3-Clause
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | /** @addtogroup libdrv
|
|---|
| 8 | * @{
|
|---|
| 9 | */
|
|---|
| 10 | /** @file
|
|---|
| 11 | */
|
|---|
| 12 |
|
|---|
| 13 | #include <async.h>
|
|---|
| 14 | #include <errno.h>
|
|---|
| 15 | #include <ops/battery_dev.h>
|
|---|
| 16 | #include <battery_iface.h>
|
|---|
| 17 | #include <ddf/driver.h>
|
|---|
| 18 | #include <macros.h>
|
|---|
| 19 |
|
|---|
| 20 | /** Read the current battery status from the device
|
|---|
| 21 | *
|
|---|
| 22 | * @param sess Session of the device
|
|---|
| 23 | * @param status Current status of the battery
|
|---|
| 24 | *
|
|---|
| 25 | * @return EOK on success or an error code
|
|---|
| 26 | */
|
|---|
| 27 | errno_t
|
|---|
| 28 | battery_status_get(async_sess_t *sess, battery_status_t *batt_status)
|
|---|
| 29 | {
|
|---|
| 30 | sysarg_t status;
|
|---|
| 31 |
|
|---|
| 32 | async_exch_t *exch = async_exchange_begin(sess);
|
|---|
| 33 |
|
|---|
| 34 | errno_t const rc = async_req_1_1(exch, DEV_IFACE_ID(BATTERY_DEV_IFACE),
|
|---|
| 35 | BATTERY_STATUS_GET, &status);
|
|---|
| 36 |
|
|---|
| 37 | async_exchange_end(exch);
|
|---|
| 38 |
|
|---|
| 39 | if (rc == EOK)
|
|---|
| 40 | *batt_status = (battery_status_t) status;
|
|---|
| 41 |
|
|---|
| 42 | return rc;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | /** Read the current battery charge level from the device
|
|---|
| 46 | *
|
|---|
| 47 | * @param sess Session of the device
|
|---|
| 48 | * @param level Battery charge level (0 - 100)
|
|---|
| 49 | *
|
|---|
| 50 | * @return EOK on success or an error code
|
|---|
| 51 | */
|
|---|
| 52 | errno_t
|
|---|
| 53 | battery_charge_level_get(async_sess_t *sess, int *level)
|
|---|
| 54 | {
|
|---|
| 55 | sysarg_t charge_level;
|
|---|
| 56 |
|
|---|
| 57 | async_exch_t *exch = async_exchange_begin(sess);
|
|---|
| 58 |
|
|---|
| 59 | errno_t const rc = async_req_1_1(exch, DEV_IFACE_ID(BATTERY_DEV_IFACE),
|
|---|
| 60 | BATTERY_CHARGE_LEVEL_GET, &charge_level);
|
|---|
| 61 |
|
|---|
| 62 | async_exchange_end(exch);
|
|---|
| 63 |
|
|---|
| 64 | if (rc == EOK)
|
|---|
| 65 | *level = (int) charge_level;
|
|---|
| 66 |
|
|---|
| 67 | return rc;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | static void remote_battery_status_get(ddf_fun_t *, void *, ipc_call_t *);
|
|---|
| 71 | static void remote_battery_charge_level_get(ddf_fun_t *, void *, ipc_call_t *);
|
|---|
| 72 |
|
|---|
| 73 | /** Remote battery interface operations */
|
|---|
| 74 | static const remote_iface_func_ptr_t remote_battery_dev_iface_ops[] = {
|
|---|
| 75 | [BATTERY_STATUS_GET] = remote_battery_status_get,
|
|---|
| 76 | [BATTERY_CHARGE_LEVEL_GET] = remote_battery_charge_level_get,
|
|---|
| 77 | };
|
|---|
| 78 |
|
|---|
| 79 | /** Remote battery interface structure
|
|---|
| 80 | *
|
|---|
| 81 | * Interface for processing request from remote clients
|
|---|
| 82 | * addressed by the battery interface.
|
|---|
| 83 | *
|
|---|
| 84 | */
|
|---|
| 85 | const remote_iface_t remote_battery_dev_iface = {
|
|---|
| 86 | .method_count = ARRAY_SIZE(remote_battery_dev_iface_ops),
|
|---|
| 87 | .methods = remote_battery_dev_iface_ops,
|
|---|
| 88 | };
|
|---|
| 89 |
|
|---|
| 90 | /** Process the status_get() request from the remote client
|
|---|
| 91 | *
|
|---|
| 92 | * @param fun The function from which the battery status is read
|
|---|
| 93 | * @param ops The local ops structure
|
|---|
| 94 | *
|
|---|
| 95 | */
|
|---|
| 96 | static void remote_battery_status_get(ddf_fun_t *fun, void *ops,
|
|---|
| 97 | ipc_call_t *call)
|
|---|
| 98 | {
|
|---|
| 99 | const battery_dev_ops_t *bops = (battery_dev_ops_t *) ops;
|
|---|
| 100 |
|
|---|
| 101 | if (bops->battery_status_get == NULL) {
|
|---|
| 102 | async_answer_0(call, ENOTSUP);
|
|---|
| 103 | return;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | battery_status_t batt_status;
|
|---|
| 107 | const errno_t rc = bops->battery_status_get(fun, &batt_status);
|
|---|
| 108 |
|
|---|
| 109 | if (rc != EOK)
|
|---|
| 110 | async_answer_0(call, rc);
|
|---|
| 111 | else
|
|---|
| 112 | async_answer_1(call, rc, batt_status);
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | /** Process the battery_charge_level_get() request from the remote client
|
|---|
| 116 | *
|
|---|
| 117 | * @param fun The function from which the battery charge level is read
|
|---|
| 118 | * @param ops The local ops structure
|
|---|
| 119 | *
|
|---|
| 120 | */
|
|---|
| 121 | static void remote_battery_charge_level_get(ddf_fun_t *fun, void *ops,
|
|---|
| 122 | ipc_call_t *call)
|
|---|
| 123 | {
|
|---|
| 124 | const battery_dev_ops_t *bops = (battery_dev_ops_t *) ops;
|
|---|
| 125 |
|
|---|
| 126 | if (bops->battery_charge_level_get == NULL) {
|
|---|
| 127 | async_answer_0(call, ENOTSUP);
|
|---|
| 128 | return;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | int battery_level;
|
|---|
| 132 | const errno_t rc = bops->battery_charge_level_get(fun, &battery_level);
|
|---|
| 133 |
|
|---|
| 134 | if (rc != EOK)
|
|---|
| 135 | async_answer_0(call, rc);
|
|---|
| 136 | else
|
|---|
| 137 | async_answer_1(call, rc, battery_level);
|
|---|
| 138 | }
|
|---|