changeset 196:47d56330609d

fc-cmu200d: skeleton complete, ready to start adding meat
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 24 Apr 2017 02:17:04 +0000
parents db9ee7745cdd
children dbb54db721d1
files rfcal/cmu200/Makefile rfcal/cmu200/dispatch.c rfcal/cmu200/session.c rfcal/cmu200/socket.c
diffstat 4 files changed, 84 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/rfcal/cmu200/Makefile	Mon Apr 24 01:43:02 2017 +0000
+++ b/rfcal/cmu200/Makefile	Mon Apr 24 02:17:04 2017 +0000
@@ -3,7 +3,7 @@
 PROGS=	fc-cmu200d fc-serscpi
 INSTBIN=/opt/freecalypso/bin
 
-CMU200D_OBJS=	init.o main.o openport.o sercmd.o session.o socket.o
+CMU200D_OBJS=	dispatch.o init.o main.o openport.o sercmd.o session.o socket.o
 SERSCPI_OBJS=	openport.o sertool.o
 
 all:	${PROGS}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rfcal/cmu200/dispatch.c	Mon Apr 24 02:17:04 2017 +0000
@@ -0,0 +1,38 @@
+/*
+ * This module contains the code that dispatches client commands.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+
+extern char *client_cmd_fields[];
+extern int client_cmd_nfields;
+
+cmd_ping()
+{
+	send_socket_response("+Pong\n");
+	return(0);
+}
+
+static struct cmdtab {
+	char	*cmd_kw;
+	int	(*handler)();
+} cmdtab[] = {
+	{"ping", cmd_ping},
+	{0, 0}
+};
+
+dispatch_client_command()
+{
+	struct cmdtab *tp;
+
+	for (tp = cmdtab; tp->cmd_kw; tp++)
+		if (!strcmp(client_cmd_fields[0], tp->cmd_kw))
+			break;
+	if (tp->handler)
+		return tp->handler();
+	send_socket_response("-Unknown or unimplemented command\n");
+	return(0);
+}
--- a/rfcal/cmu200/session.c	Mon Apr 24 01:43:02 2017 +0000
+++ b/rfcal/cmu200/session.c	Mon Apr 24 02:17:04 2017 +0000
@@ -11,10 +11,39 @@
 
 extern int activesock;
 
+#define	MAX_FIELDS	10
+
+char client_cmd[256], *client_cmd_fields[MAX_FIELDS+1];
+int client_cmd_nfields;
+
+parse_cmd_into_fields()
+{
+	char *cp;
+
+	client_cmd_nfields = 0;
+	for (cp = client_cmd; ; ) {
+		while (isspace(*cp))
+			cp++;
+		if (*cp == '\0')
+			break;
+		if (client_cmd_nfields >= MAX_FIELDS) {
+			send_socket_response("-Command has too many fields\n");
+			return(1);
+		}
+		client_cmd_fields[client_cmd_nfields++] = cp;
+		while (*cp && !isspace(*cp))
+			cp++;
+		if (*cp)
+			*cp++ = '\0';
+	}
+	client_cmd_fields[client_cmd_nfields] = 0;
+	return(0);
+}
+
 handle_command()
 {
-	char readbuf[256], linebuf[256];
-	int cc, pos;
+	char readbuf[256];
+	int cc, pos, rc;
 
 	for (pos = 0; ; ) {
 		cc = read(activesock, readbuf, sizeof readbuf);
@@ -22,19 +51,25 @@
 			printf("Client program closed connection\n");
 			return(1);
 		}
-		if (pos + cc > sizeof linebuf) {
+		if (pos + cc > sizeof client_cmd) {
 			send_socket_response("-Command too long\n");
 			return(1);
 		}
-		bcopy(readbuf, linebuf + pos, cc);
+		bcopy(readbuf, client_cmd + pos, cc);
 		pos += cc;
-		if (linebuf[pos-1] == '\n')
+		if (client_cmd[pos-1] == '\n')
 			break;
 	}
-	linebuf[pos-1] = '\0';
-	printf("Client command: %s\n", linebuf);
-	/* actual command handling will go here */
-	return(0);
+	client_cmd[pos-1] = '\0';
+	printf("Client command: %s\n", client_cmd);
+	rc = parse_cmd_into_fields();
+	if (rc)
+		return(0);
+	if (!client_cmd_nfields) {
+		send_socket_response("+Empty command OK\n");
+		return(0);
+	}
+	return dispatch_client_command();
 }
 
 handle_session()
--- a/rfcal/cmu200/socket.c	Mon Apr 24 01:43:02 2017 +0000
+++ b/rfcal/cmu200/socket.c	Mon Apr 24 02:17:04 2017 +0000
@@ -76,5 +76,6 @@
 send_socket_response(str)
 	char *str;
 {
+	printf("Msg to client: %s", str);
 	write(activesock, str, strlen(str));
 }