-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsendKey.cpp
140 lines (120 loc) · 3.38 KB
/
sendKey.cpp
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
/*
* Copyright (c) 2020 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <string.h>
#include <iostream>
#include <cstdint>
#include "sendKey.h"
using namespace std;
static string helpMsg =
"Usage: \n./sendkey --vm <0, 1, 2….n> --volume <up/down>\n"\
"./sendkey --vm <0, 1, 2….n> --power <delay in seconds>\n"\
"Example:\n\t./sendkey --vm 0 --volume up\n"\
"\t./sendkey --vm 0 --volume down\n"\
"\t./sendkey --vm 0 --power 0";
static void usage()
{
cout << helpMsg << endl;
exit(0);
}
static int initMsgQ(const char file[])
{
key_t key = 0;
int mqId = 0;
if ((key = ftok(file, 99)) < 0) {
cout << "Failed to get msgq key" << endl;
return -1;
}
if ((mqId = msgget(key, 0666)) < 0) {
cout << "Failed to get msgq id" << endl;
return -1;
}
return mqId;
}
static void sendPowerEvent(int32_t ctrl)
{
struct mQData buf = {1, 0};
int mqId = 0;
if ((mqId = initMsgQ(POWER_BUTTON_FILE_PATH)) < 0)
exit(0);
buf.bCtrl = static_cast<uint32_t>(ctrl);
msgsnd(mqId, &buf, sizeof(buf) - sizeof(long), 0);
}
static void sendVolumeEvent(int32_t ctrl)
{
struct mQData buf = {1, 0};
int mqId = 0;
if ((mqId = initMsgQ(VOLUME_BUTTON_FILE_PATH)) < 0)
exit(0);
switch (ctrl) {
case UP:
buf.bCtrl = UP;
msgsnd(mqId, &buf, sizeof(buf) - sizeof(long), 0);
break;
case DOWN:
buf.bCtrl = DOWN;
msgsnd(mqId, &buf, sizeof(buf) - sizeof(long), 0);
break;
default:
cout << "Invalid Volume control option" << endl;
usage();
}
}
static void parseArgs(uint32_t *button, int32_t *bCtrl, char **argv)
{
if (!strncmp(reinterpret_cast<const char *>(argv[3]), "--volume", 9)) {
*button = VOLUME;
if (!strncmp(argv[4], "up", 3))
*bCtrl = UP;
else if (!strncmp(argv[4], "down", 5))
*bCtrl = DOWN;
else
usage();
} else if (!strncmp(reinterpret_cast<const char *>(argv[3]),
"--power", 8)) {
*button = POWER;
string intS(argv[4]);
*bCtrl = stoi(intS);
if (*bCtrl < 0 || *bCtrl > 10) {
cout << "Power button delay limit is 0 to 10 seconds" << endl;
usage();
}
} else {
usage();
}
}
int main(int nargs, char **argv)
{
uint32_t button = 0;
int32_t bCtrl = 0;
if ((nargs != 5) || strncmp(argv[1], "--vm", 5)) {
cout << "--vm not found" << endl;
usage();
return 0;
}
parseArgs(&button, &bCtrl, argv);
switch (button) {
case POWER:
cout << "sending power button" << endl;
sendPowerEvent(bCtrl);
break;
case VOLUME:
cout << "sending volume button" << endl;
sendVolumeEvent(bCtrl);
break;
}
return 0;
}