-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmtc_module.c
147 lines (122 loc) · 3.1 KB
/
smtc_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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "smtc_module.h"
typedef struct str2enum_s {
const char *name;
int enum_value;
} str2enum_t;
static str2enum_t object[] = {
{"LIGHT", LIGHT},
{"WINDOW", WINDOW},
{"DOOR", DOOR},
{"RADIATOR", RADIATOR}
};
static str2enum_t location[] = {
{"LIVING-ROOM", LIVING_ROOM},
{"KITCHEN", KITCHEN},
{"BATHROOM", BATHROOM}
};
static str2enum_t action[] = {
{"OPEN", OPEN},
{"CLOSE", CLOSE},
{"TURN-ON", TURN_ON},
{"TURN-OFF", TURN_OFF},
{"TURN-UP", TURN_UP},
{"TURN-DOWN", TURN_DOWN},
{"SWITCH-ON", SWITCH_ON},
{"SWITCH-OFF", SWITCH_OFF},
{"LOCK", LOCK},
{"UNLOCK", UNLOCK}
};
static void init_result(result_t *result) {
result->det_action = ACTION_INV;
result->det_location = ANYWHERE;
result->det_object = OBJECT_INV;
}
static void detect_object(const char *command, result_t *result) {
int i;
for (i = 0; i < (sizeof(object)/sizeof(object[0])); i++) {
if (strstr(command, object[i].name) != NULL) {
result->det_object = object[i].enum_value;
LOG_YELLOW("===>object ");
LOG_BLUE("[");
LOG_BLUE(object[i].name);
LOG_BLUE("]");
LOG_YELLOW("detected!\n");
break;
}
}
return;
}
static void detect_location(const char *command, result_t *result) {
int i;
for (i = 0; i < (sizeof(location)/sizeof(location[0])); i++) {
if (strstr(command, location[i].name) != NULL) {
result->det_location = location[i].enum_value;
LOG_YELLOW("===>location ");
LOG_BLUE("[");
LOG_BLUE(location[i].name);
LOG_BLUE("]");
LOG_YELLOW("detected!\n");
break;
}
}
return;
}
static void detect_action(const char *command, result_t *result) {
int i;
for (i = 0; i < (sizeof(action)/sizeof(action[0])); i++) {
if (strstr(command, action[i].name) != NULL) {
result->det_action = action[i].enum_value;
LOG_YELLOW("===>action ");
LOG_BLUE("[");
LOG_BLUE(action[i].name);
LOG_BLUE("]");
LOG_YELLOW("detected!\n");
break;
}
}
return;
}
static int check_result(result_t *result) {
if (result->det_object == OBJECT_INV) {
LOG_RED("===>[error]UNKONW object!\n");
return -1;
}
if (result->det_action == ACTION_INV) {
LOG_RED("===>[error]UNKNOWN action!\n");
return -1;
}
return 0;
}
static int processing(int pipe_write, result_t *result) {
int ret = 0;
if (write(pipe_write, result, sizeof(result_t)) < 0) {
perror("send semantic result");
ret = -1;
}
return ret;
}
int command_proc(int pipe_write, const char *command){
result_t result;
LOG_YELLOW("*******PROCESSING MODULE*******\n");
LOG_YELLOW("===>receive command: ");
LOG_BLUE("[");
LOG_BLUE(command);
LOG_BLUE("]\n");
init_result(&result);
detect_object(command, &result);
detect_location(command, &result);
detect_action(command, &result);
if (check_result(&result) != 0) {
LOG_RED("[error]check result failed!\n");
return -1;
}
if (processing(pipe_write, &result) != 0) {
LOG_RED("[error]processing result failed!\n");
return -1;
}
return 0;
}