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
 	{ CMD_VIDEO_OUTPUT_LOCKS,      "VIDEO OUTPUT LOCKS" },
119
 	{ CMD_VIDEO_OUTPUT_LOCKS,      "VIDEO OUTPUT LOCKS" },
120
 	{ CMD_VIDEO_OUTPUT_ROUTING,    "VIDEO OUTPUT ROUTING" },
120
 	{ CMD_VIDEO_OUTPUT_ROUTING,    "VIDEO OUTPUT ROUTING" },
121
 	{ CMD_PING,                    "PING" },
121
 	{ CMD_PING,                    "PING" },
122
+	{ CMD_ACK,                     "ACK" },
123
+	{ CMD_NAK,                     "NAK" },
122
 };
124
 };
123
 
125
 
124
 static const char *get_cmd_text(enum vcmd cmd) {
126
 static const char *get_cmd_text(enum vcmd cmd) {
140
 
142
 
141
 bool parse_command(struct videohub_data *data, char *cmd) {
143
 bool parse_command(struct videohub_data *data, char *cmd) {
142
 	unsigned int i;
144
 	unsigned int i;
145
+	bool ret = true;
143
 	if (!strlen(cmd))
146
 	if (!strlen(cmd))
144
 		return false;
147
 		return false;
145
 	struct videohub_commands *v = NULL;
148
 	struct videohub_commands *v = NULL;
168
 	if (debug > 1)
171
 	if (debug > 1)
169
 		d("----\n%s\n----\n", cmd);
172
 		d("----\n%s\n----\n", cmd);
170
 
173
 
171
-	bool cmd_response = false;
172
 	char *p, *cmd_data = xstrdup( cmd + strlen(v->txt) + 2 ); // +2 to compensate for :\n at the end of the command
174
 	char *p, *cmd_data = xstrdup( cmd + strlen(v->txt) + 2 ); // +2 to compensate for :\n at the end of the command
173
 	// Split line by line
175
 	// Split line by line
174
 	char *line, *saveptr = NULL;
176
 	char *line, *saveptr = NULL;
175
 	for(i = 0, line = strtok_r(cmd_data, "\n", &saveptr); line; line = strtok_r(NULL, "\n", &saveptr), i++) {
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
 		// Parse command data response looking like that: "[slot_pos] [slot_data]"
178
 		// Parse command data response looking like that: "[slot_pos] [slot_data]"
192
 		bool valid_slot = false;
179
 		bool valid_slot = false;
193
 		unsigned int slot_pos = 0;
180
 		unsigned int slot_pos = 0;
280
 			break;
267
 			break;
281
 
268
 
282
 		case CMD_PING:
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
 			break;
275
 			break;
285
 		}
276
 		}
286
 	}
277
 	}
287
 	free(cmd_data);
278
 	free(cmd_data);
288
-	return true;
279
+	return ret;
289
 }
280
 }
290
 
281
 
291
 int parse_text_buffer(struct videohub_data *data, char *cmd_buffer) {
282
 int parse_text_buffer(struct videohub_data *data, char *cmd_buffer) {

+ 2
- 0
cmd.h View File

23
 	CMD_VIDEO_OUTPUT_LOCKS,
23
 	CMD_VIDEO_OUTPUT_LOCKS,
24
 	CMD_VIDEO_OUTPUT_ROUTING,
24
 	CMD_VIDEO_OUTPUT_ROUTING,
25
 	CMD_PING,
25
 	CMD_PING,
26
+	CMD_ACK,
27
+	CMD_NAK,
26
 };
28
 };
27
 
29
 
28
 bool parse_command(struct videohub_data *d, char *cmd);
30
 bool parse_command(struct videohub_data *d, char *cmd);

Loading…
Cancel
Save