f688eba8770488e4ebf4fa147e5f6b643be10634
[deliverable/linux.git] / Documentation / connector / cn_test.c
1 /*
2 * cn_test.c
3 *
4 * 2004-2005 Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru>
5 * All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/skbuff.h>
26 #include <linux/timer.h>
27
28 #include <linux/connector.h>
29
30 static struct cb_id cn_test_id = { 0x123, 0x456 };
31 static char cn_test_name[] = "cn_test";
32 static struct sock *nls;
33 static struct timer_list cn_test_timer;
34
35 void cn_test_callback(void *data)
36 {
37 struct cn_msg *msg = (struct cn_msg *)data;
38
39 printk("%s: %lu: idx=%x, val=%x, seq=%u, ack=%u, len=%d: %s.\n",
40 __func__, jiffies, msg->id.idx, msg->id.val,
41 msg->seq, msg->ack, msg->len, (char *)msg->data);
42 }
43
44 /*
45 * Do not remove this function even if no one is using it as
46 * this is an example of how to get notifications about new
47 * connector user registration
48 */
49 #if 0
50 static int cn_test_want_notify(void)
51 {
52 struct cn_ctl_msg *ctl;
53 struct cn_notify_req *req;
54 struct cn_msg *msg = NULL;
55 int size, size0;
56 struct sk_buff *skb;
57 struct nlmsghdr *nlh;
58 u32 group = 1;
59
60 size0 = sizeof(*msg) + sizeof(*ctl) + 3 * sizeof(*req);
61
62 size = NLMSG_SPACE(size0);
63
64 skb = alloc_skb(size, GFP_ATOMIC);
65 if (!skb) {
66 printk(KERN_ERR "Failed to allocate new skb with size=%u.\n",
67 size);
68
69 return -ENOMEM;
70 }
71
72 nlh = NLMSG_PUT(skb, 0, 0x123, NLMSG_DONE, size - sizeof(*nlh));
73
74 msg = (struct cn_msg *)NLMSG_DATA(nlh);
75
76 memset(msg, 0, size0);
77
78 msg->id.idx = -1;
79 msg->id.val = -1;
80 msg->seq = 0x123;
81 msg->ack = 0x345;
82 msg->len = size0 - sizeof(*msg);
83
84 ctl = (struct cn_ctl_msg *)(msg + 1);
85
86 ctl->idx_notify_num = 1;
87 ctl->val_notify_num = 2;
88 ctl->group = group;
89 ctl->len = msg->len - sizeof(*ctl);
90
91 req = (struct cn_notify_req *)(ctl + 1);
92
93 /*
94 * Idx.
95 */
96 req->first = cn_test_id.idx;
97 req->range = 10;
98
99 /*
100 * Val 0.
101 */
102 req++;
103 req->first = cn_test_id.val;
104 req->range = 10;
105
106 /*
107 * Val 1.
108 */
109 req++;
110 req->first = cn_test_id.val + 20;
111 req->range = 10;
112
113 NETLINK_CB(skb).dst_group = ctl->group;
114 //netlink_broadcast(nls, skb, 0, ctl->group, GFP_ATOMIC);
115 netlink_unicast(nls, skb, 0, 0);
116
117 printk(KERN_INFO "Request was sent. Group=0x%x.\n", ctl->group);
118
119 return 0;
120
121 nlmsg_failure:
122 printk(KERN_ERR "Failed to send %u.%u\n", msg->seq, msg->ack);
123 kfree_skb(skb);
124 return -EINVAL;
125 }
126 #endif
127
128 static u32 cn_test_timer_counter;
129 static void cn_test_timer_func(unsigned long __data)
130 {
131 struct cn_msg *m;
132 char data[32];
133
134 m = kzalloc(sizeof(*m) + sizeof(data), GFP_ATOMIC);
135 if (m) {
136
137 memcpy(&m->id, &cn_test_id, sizeof(m->id));
138 m->seq = cn_test_timer_counter;
139 m->len = sizeof(data);
140
141 m->len =
142 scnprintf(data, sizeof(data), "counter = %u",
143 cn_test_timer_counter) + 1;
144
145 memcpy(m + 1, data, m->len);
146
147 cn_netlink_send(m, 0, GFP_ATOMIC);
148 kfree(m);
149 }
150
151 cn_test_timer_counter++;
152
153 mod_timer(&cn_test_timer, jiffies + HZ);
154 }
155
156 static int cn_test_init(void)
157 {
158 int err;
159
160 err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
161 if (err)
162 goto err_out;
163 cn_test_id.val++;
164 err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
165 if (err) {
166 cn_del_callback(&cn_test_id);
167 goto err_out;
168 }
169
170 setup_timer(&cn_test_timer, cn_test_timer_func, 0);
171 cn_test_timer.expires = jiffies + HZ;
172 add_timer(&cn_test_timer);
173
174 return 0;
175
176 err_out:
177 if (nls && nls->sk_socket)
178 sock_release(nls->sk_socket);
179
180 return err;
181 }
182
183 static void cn_test_fini(void)
184 {
185 del_timer_sync(&cn_test_timer);
186 cn_del_callback(&cn_test_id);
187 cn_test_id.val--;
188 cn_del_callback(&cn_test_id);
189 if (nls && nls->sk_socket)
190 sock_release(nls->sk_socket);
191 }
192
193 module_init(cn_test_init);
194 module_exit(cn_test_fini);
195
196 MODULE_LICENSE("GPL");
197 MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
198 MODULE_DESCRIPTION("Connector's test module");
This page took 0.033776 seconds and 4 git commands to generate.