source: mainline/uspace/lib/device/src/hr.c@ 94d84a0

Last change on this file since 94d84a0 was 94d84a0, checked in by Miroslav Cimerman <mc@…>, 11 months ago

hr: initial trivial mirroring implementation

  • Property mode set to 100644
File size: 3.0 KB
Line 
1/*
2 * Copyright (c) 2024 Miroslav Cimerman
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 libdevice
30 * @{
31 */
32/**
33 * @file
34 */
35
36#include <abi/ipc/interfaces.h>
37#include <async.h>
38#include <hr.h>
39#include <ipc/hr.h>
40#include <ipc/services.h>
41#include <loc.h>
42#include <stdlib.h>
43#include <stdio.h>
44#include <str.h>
45
46errno_t hr_sess_init(hr_t **rhr)
47{
48 errno_t rc;
49
50 hr_t *hr = calloc(1, sizeof(hr_t));
51 if (hr == NULL) {
52 rc = ENOMEM;
53 goto error;
54 }
55
56 service_id_t hr_svcid;
57
58 rc = loc_service_get_id(SERVICE_NAME_HR, &hr_svcid, IPC_FLAG_BLOCKING);
59 if (rc != EOK) {
60 rc = EIO;
61 goto error;
62 }
63
64 hr->sess = loc_service_connect(hr_svcid, INTERFACE_HR,
65 IPC_FLAG_BLOCKING);
66 if (hr->sess == NULL) {
67 rc = EIO;
68 goto error;
69 }
70
71 *rhr = hr;
72 return EOK;
73error:
74 if (hr != NULL)
75 free(hr);
76 *rhr = NULL;
77 return rc;
78}
79
80void hr_sess_destroy(hr_t *hr)
81{
82 if (hr == NULL)
83 return;
84
85 async_hangup(hr->sess);
86 free(hr);
87}
88
89errno_t hr_create(hr_t *hr, hr_config_t *hr_config)
90{
91 errno_t rc, retval;
92 async_exch_t *exch;
93 aid_t req;
94
95 exch = async_exchange_begin(hr->sess);
96 if (exch == NULL) {
97 return EINVAL;
98 }
99
100 req = async_send_1(exch, HR_CREATE, hr_config->level, NULL);
101
102 rc = async_data_write_start(exch, hr_config->name,
103 str_size(hr_config->name));
104 if (rc != EOK) {
105 async_exchange_end(exch);
106 async_forget(req);
107 return rc;
108 }
109
110 rc = async_data_write_start(exch, hr_config->devs,
111 sizeof(service_id_t) * hr_config->dev_no);
112 if (rc != EOK) {
113 async_exchange_end(exch);
114 async_forget(req);
115 return rc;
116 }
117
118 async_exchange_end(exch);
119 async_wait_for(req, &retval);
120 if (retval != EOK)
121 return retval;
122
123 return EOK;
124}
125
126/** @}
127 */
Note: See TracBrowser for help on using the repository browser.