Browse Source

Improve and simplify command parsing.

Georgi Chorbadzhiyski 9 years ago
parent
commit
55959179ea
2 changed files with 11 additions and 18 deletions
  1. 9
    18
      cmd.c
  2. 2
    0
      cmd.h

+ 9
- 18
cmd.c View File

@@ -119,6 +119,8 @@ static struct videohub_commands {
119 119
 	{ CMD_VIDEO_OUTPUT_LOCKS,      "VIDEO OUTPUT LOCKS" },
120 120
 	{ CMD_VIDEO_OUTPUT_ROUTING,    "VIDEO OUTPUT ROUTING" },
121 121
 	{ CMD_PING,                    "PING" },
122
+	{ CMD_ACK,                     "ACK" },
123
+	{ CMD_NAK,                     "NAK" },
122 124
 };
123 125
 
124 126
 static const char *get_cmd_text(enum vcmd cmd) {
@@ -140,6 +142,7 @@ static char *parse_text(char *line, char *cmd) {
140 142
 
141 143
 bool parse_command(struct videohub_data *data, char *cmd) {
142 144
 	unsigned int i;
145
+	bool ret = true;
143 146
 	if (!strlen(cmd))
144 147
 		return false;
145 148
 	struct videohub_commands *v = NULL;
@@ -168,26 +171,10 @@ bool parse_command(struct videohub_data *data, char *cmd) {
168 171
 	if (debug > 1)
169 172
 		d("----\n%s\n----\n", cmd);
170 173
 
171
-	bool cmd_response = false;
172 174
 	char *p, *cmd_data = xstrdup( cmd + strlen(v->txt) + 2 ); // +2 to compensate for :\n at the end of the command
173 175
 	// Split line by line
174 176
 	char *line, *saveptr = NULL;
175 177
 	for(i = 0, line = strtok_r(cmd_data, "\n", &saveptr); line; line = strtok_r(NULL, "\n", &saveptr), i++) {
176
-		if (i == 0) { // Handle command response
177
-			if (streq(line, "NAK"))
178
-				return false;
179
-			if (streq(line, "ACK")) {
180
-				cmd_response = true;
181
-				continue;
182
-			}
183
-		}
184
-
185
-		if (i == 1 && cmd_response) { // The command must match
186
-			if (strstr(line, v->txt) != line)
187
-				return false;
188
-			continue;
189
-		}
190
-
191 178
 		// Parse command data response looking like that: "[slot_pos] [slot_data]"
192 179
 		bool valid_slot = false;
193 180
 		unsigned int slot_pos = 0;
@@ -280,12 +267,16 @@ bool parse_command(struct videohub_data *data, char *cmd) {
280 267
 			break;
281 268
 
282 269
 		case CMD_PING:
283
-			// Do nothing, we just get ACK without any data
270
+		case CMD_ACK:
271
+			// Do nothing
272
+			break;
273
+		case CMD_NAK:
274
+			ret = false;
284 275
 			break;
285 276
 		}
286 277
 	}
287 278
 	free(cmd_data);
288
-	return true;
279
+	return ret;
289 280
 }
290 281
 
291 282
 int parse_text_buffer(struct videohub_data *data, char *cmd_buffer) {

+ 2
- 0
cmd.h View File

@@ -23,6 +23,8 @@ enum vcmd {
23 23
 	CMD_VIDEO_OUTPUT_LOCKS,
24 24
 	CMD_VIDEO_OUTPUT_ROUTING,
25 25
 	CMD_PING,
26
+	CMD_ACK,
27
+	CMD_NAK,
26 28
 };
27 29
 
28 30
 bool parse_command(struct videohub_data *d, char *cmd);

Loading…
Cancel
Save