-
Notifications
You must be signed in to change notification settings - Fork 0
/
morse-module.c
178 lines (150 loc) · 3.83 KB
/
morse-module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/kmod.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
MODULE_AUTHOR("Ilya Kurin, 4uf04eg@gmail.com");
MODULE_DESCRIPTION("Beeps message using Morse code");
MODULE_LICENSE("GPL");
#define DOT dot();
#define DASH dash();
#define INTRA_CHARACTER_SPACE msleep(50);
#define INTER_CHARACTER_SPACE msleep(150);
#define WORD_SPACE msleep(350);
const char* MORSE_TABLE[36] = {
"01", //A
"1000", //B
"1010", //C
"100", //D
"0", //E
"0010", //F
"110", //G
"0000", //H
"00", //I
"0111", //J
"101", //K
"0100", //L
"11", //M
"10", //N
"111", //O
"0110", //P
"1101", //Q
"010", //R
"000", //S
"1", //T
"001", //U
"0001", //V
"011", //W
"1001", //X
"1011", //Y
"1100", //Z
"11111",//0
"01111",//1
"00111",//2
"00011",//3
"00001",//4
"00000",//5
"10000",//6
"11000",//7
"11100",//8
"11110",//9
};
static struct proc_dir_entry *entry;
static enum sound_type { INTERNAL, EXTERNAL } type;
static void dot(void) {
char * envp[] = { "HOME=/", NULL };
if (type == EXTERNAL) {
char * argv[] = {
"/usr/local/bin/beep-morse", "--dot", NULL
};
call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
}
else {
char * argv[] = {
"/usr/bin/env",
"-i", "/usr/bin/beep",
"-f","800","-l","50", NULL
};
call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
}
}
static void dash(void) {
char * envp[] = {"HOME=/", NULL };
if (type == EXTERNAL) {
char * argv[] = {
"/usr/local/bin/beep-morse", "--dash", NULL
};
call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
}
else {
char * argv[] = {
"/usr/bin/env",
"-i", "/usr/bin/beep",
"-f","800","-l","150", NULL
};
call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
}
}
static void make_sound(const char* morse_code) {
int i;
for (i = 0; i < strlen(morse_code); i++) {
if (morse_code[i] == '1') DASH;
if (morse_code[i] == '0') DOT;
INTRA_CHARACTER_SPACE;
}
}
static void letter_to_code(char letter) {
int lowerPos = letter - 'a';
int upperPos = letter - 'A';
int digitPos = 26 + letter - '0';
int size = sizeof(MORSE_TABLE) / sizeof(MORSE_TABLE[0]);
if (lowerPos >= 0 && lowerPos < size)
make_sound(MORSE_TABLE[lowerPos]);
else if (upperPos >= 0 && upperPos < size)
make_sound(MORSE_TABLE[upperPos]);
else if (digitPos >= 0 && digitPos < size)
make_sound(MORSE_TABLE[digitPos]);
INTER_CHARACTER_SPACE;
}
static void proceed_message(char* message, int start_point) {
int i = start_point;
for (; i < strlen(message); i++){
if (message[i] == ' ') {
WORD_SPACE;
continue;
}
letter_to_code(message[i]);
}
}
//Returns start of real message
static int proceed_params(char* message) {
if (strncmp(message, "--external ", 11) == 0) {
type = EXTERNAL;
return 11;
}
return 0;
}
static ssize_t handle_write(
struct file *file,
const char __user *ubuf,
size_t count,
loff_t *ppos) {
char procfs_buffer[1024];
if (raw_copy_from_user(procfs_buffer, ubuf, count))
return -EFAULT;
proceed_message(procfs_buffer, proceed_params(procfs_buffer));
return count;
}
static struct file_operations operations = {
.owner = THIS_MODULE,
.write = handle_write,
};
static int __init morse_init(void) {
entry = proc_create("morse", 0222, NULL, &operations);
return 0;
}
static void __exit morse_exit(void) {
proc_remove(entry);
}
module_init(morse_init);
module_exit(morse_exit);