source: mainline/uspace/app/trace/proto.c@ 3fafe5e0

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3fafe5e0 was 1433ecda, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

  • Property mode set to 100644
File size: 4.9 KB
Line 
1/*
2 * Copyright (c) 2008 Jiri Svoboda
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 trace
30 * @{
31 */
32/** @file
33 */
34
35#include <stdio.h>
36#include <stdlib.h>
37#include <adt/hash_table.h>
38
39#include "trace.h"
40#include "proto.h"
41
42
43/* Maps service number to protocol */
44static hash_table_t srv_proto;
45
46typedef struct {
47 int srv;
48 proto_t *proto;
49 ht_link_t link;
50} srv_proto_t;
51
52typedef struct {
53 int method;
54 oper_t *oper;
55 ht_link_t link;
56} method_oper_t;
57
58/* Hash table operations. */
59
60static size_t srv_proto_key_hash(void *key)
61{
62 return *(int *)key;
63}
64
65static size_t srv_proto_hash(const ht_link_t *item)
66{
67 srv_proto_t *sp = hash_table_get_inst(item, srv_proto_t, link);
68 return sp->srv;
69}
70
71static bool srv_proto_key_equal(void *key, const ht_link_t *item)
72{
73 srv_proto_t *sp = hash_table_get_inst(item, srv_proto_t, link);
74 return sp->srv == *(int *)key;
75}
76
77static hash_table_ops_t srv_proto_ops = {
78 .hash = srv_proto_hash,
79 .key_hash = srv_proto_key_hash,
80 .key_equal = srv_proto_key_equal,
81 .equal = NULL,
82 .remove_callback = NULL
83};
84
85
86static size_t method_oper_key_hash(void *key)
87{
88 return *(int *)key;
89}
90
91static size_t method_oper_hash(const ht_link_t *item)
92{
93 method_oper_t *mo = hash_table_get_inst(item, method_oper_t, link);
94 return mo->method;
95}
96
97static bool method_oper_key_equal(void *key, const ht_link_t *item)
98{
99 method_oper_t *mo = hash_table_get_inst(item, method_oper_t, link);
100 return mo->method == *(int *)key;
101}
102
103static hash_table_ops_t method_oper_ops = {
104 .hash = method_oper_hash,
105 .key_hash = method_oper_key_hash,
106 .key_equal = method_oper_key_equal,
107 .equal = NULL,
108 .remove_callback = NULL
109};
110
111
112void proto_init(void)
113{
114 /* todo: check return value. */
115 bool ok = hash_table_create(&srv_proto, 0, 0, &srv_proto_ops);
116 assert(ok);
117}
118
119void proto_cleanup(void)
120{
121 hash_table_destroy(&srv_proto);
122}
123
124void proto_register(int srv, proto_t *proto)
125{
126 srv_proto_t *sp;
127
128 sp = malloc(sizeof(srv_proto_t));
129 sp->srv = srv;
130 sp->proto = proto;
131
132 hash_table_insert(&srv_proto, &sp->link);
133}
134
135proto_t *proto_get_by_srv(int srv)
136{
137 ht_link_t *item = hash_table_find(&srv_proto, &srv);
138 if (item == NULL)
139 return NULL;
140
141 srv_proto_t *sp = hash_table_get_inst(item, srv_proto_t, link);
142 return sp->proto;
143}
144
145static void proto_struct_init(proto_t *proto, const char *name)
146{
147 proto->name = name;
148 /* todo: check return value. */
149 bool ok = hash_table_create(&proto->method_oper, 0, 0, &method_oper_ops);
150 assert(ok);
151}
152
153proto_t *proto_new(const char *name)
154{
155 proto_t *p;
156
157 p = malloc(sizeof(proto_t));
158 proto_struct_init(p, name);
159
160 return p;
161}
162
163void proto_delete(proto_t *proto)
164{
165 free(proto);
166}
167
168void proto_add_oper(proto_t *proto, int method, oper_t *oper)
169{
170 method_oper_t *mo;
171
172 mo = malloc(sizeof(method_oper_t));
173 mo->method = method;
174 mo->oper = oper;
175
176 hash_table_insert(&proto->method_oper, &mo->link);
177}
178
179oper_t *proto_get_oper(proto_t *proto, int method)
180{
181 ht_link_t *item = hash_table_find(&proto->method_oper, &method);
182 if (item == NULL)
183 return NULL;
184
185 method_oper_t *mo = hash_table_get_inst(item, method_oper_t, link);
186 return mo->oper;
187}
188
189static void oper_struct_init(oper_t *oper, const char *name)
190{
191 oper->name = name;
192}
193
194oper_t *oper_new(const char *name, int argc, val_type_t *arg_types,
195 val_type_t rv_type, int respc, val_type_t *resp_types)
196{
197 oper_t *o;
198 int i;
199
200 o = malloc(sizeof(oper_t));
201 oper_struct_init(o, name);
202
203 o->argc = argc;
204 for (i = 0; i < argc; i++)
205 o->arg_type[i] = arg_types[i];
206
207 o->rv_type = rv_type;
208
209 o->respc = respc;
210 for (i = 0; i < respc; i++)
211 o->resp_type[i] = resp_types[i];
212
213 return o;
214}
215
216/** @}
217 */
Note: See TracBrowser for help on using the repository browser.