jolly has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/osmo-remsim/+/43742?usp=email )


Change subject: Introduce osmo-remsim-client-socket
......................................................................

Introduce osmo-remsim-client-socket

Add new remsim client with socket interface. It is similar to the
osmo-rmsim-client-shell, but it uses a unix socket instead of stdin and
stdout to exchange APDUs. The coding is binary instead of hex string.

Related: SYS#8159
Change-Id: I9eddb60e4e7c3191ca2b340690416b53005f714f
---
M src/client/Makefile.am
M src/client/client.h
M src/client/remsim_client_main.c
A src/client/user_socket.c
4 files changed, 194 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-remsim refs/changes/42/43742/1

diff --git a/src/client/Makefile.am b/src/client/Makefile.am
index 6c7abc6..0609e32 100644
--- a/src/client/Makefile.am
+++ b/src/client/Makefile.am
@@ -21,6 +21,18 @@
                                 $(OSMOCORE_LIBS) \
                                 $(NULL)

+bin_PROGRAMS += osmo-remsim-client-socket
+
+osmo_remsim_client_socket_SOURCES = user_socket.c remsim_client_main.c \
+                                  remsim_client.c main_fsm.c 
../rspro_client_fsm.c ../debug.c
+osmo_remsim_client_socket_CPPFLAGS = -DSOCKET_SUPPORT
+osmo_remsim_client_socket_CFLAGS = $(AM_CFLAGS)
+osmo_remsim_client_socket_LDADD = $(top_builddir)/src/libosmo-rspro.la \
+                                 $(OSMONETIF_LIBS) \
+                                 $(OSMOGSM_LIBS) \
+                                 $(OSMOCORE_LIBS) \
+                                 $(NULL)
+
 if BUILD_CLIENT_IFDHANDLER
 EXTRA_DIST=PkgInfo osmo-remsim-client-reader_conf.in
 serialconf_DATA=osmo-remsim-client-reader_conf
diff --git a/src/client/client.h b/src/client/client.h
index 6ec94b7..1d9431e 100644
--- a/src/client/client.h
+++ b/src/client/client.h
@@ -92,6 +92,11 @@
                bool presence_valid;
                bool presence_pol;
        } simtrace;
+
+       /* only valid for socket client */
+       struct {
+               char *name;
+       } socket;
 };

 struct bankd_client {
diff --git a/src/client/remsim_client_main.c b/src/client/remsim_client_main.c
index c3f36d6..935e25e 100644
--- a/src/client/remsim_client_main.c
+++ b/src/client/remsim_client_main.c
@@ -39,6 +39,9 @@
 #ifdef SIMTRACE_SUPPORT
                "  -Z --set-sim-presence <0-1> Define the presence pin 
behaviour (only supported on some boards)\n"
 #endif
+#ifdef SOCKET_SUPPORT
+               "  -S --socket-name PATH      Unix socket name\n"
+#endif
 #ifdef USB_SUPPORT
                "  -V --usb-vendor VENDOR_ID\n"
                "  -P --usb-product PRODUCT_ID\n"
@@ -69,6 +72,9 @@
                        { "atr-ignore-rspro", 0, 0, 'r' },
                        { "event-script", 1, 0, 'e' },
                        {" disable-color", 0, 0, 'L' },
+#ifdef SOCKET_SUPPORT
+                       { "socket-name", 1, 0, 'S' },
+#endif
 #ifdef USB_SUPPORT
                        { "usb-vendor", 1, 0, 'V' },
                        { "usb-product", 1, 0, 'P' },
@@ -85,6 +91,9 @@
 #ifdef SIMTRACE_SUPPORT
                                                "Z:"
 #endif
+#ifdef SOCKET_SUPPORT
+                                               "S:"
+#endif
 #ifdef USB_SUPPORT
                                                "V:P:C:I:S:A:H:"
 #endif
@@ -140,6 +149,11 @@
                        cfg->simtrace.presence_pol = atoi(optarg);
                        break;
 #endif
+#ifdef SOCKET_SUPPORT
+               case 'S':
+                       osmo_talloc_replace_string(cfg, &cfg->socket.name, 
optarg);
+                       break;
+#endif
 #ifdef USB_SUPPORT
                case 'V':
                        cfg->usb.vendor_id = strtol(optarg, NULL, 16);
diff --git a/src/client/user_socket.c b/src/client/user_socket.c
new file mode 100644
index 0000000..af3e050
--- /dev/null
+++ b/src/client/user_socket.c
@@ -0,0 +1,163 @@
+#include <errno.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <osmocom/core/select.h>
+#include <osmocom/core/socket.h>
+
+#include "client.h"
+
+/* This is a remsim-client with a unix socket to exchange APDUs. */
+
+struct socket_state {
+       struct osmo_fd ofd;
+       struct sockaddr_un addr;
+        socklen_t addr_len;
+       struct bankd_client *bc;
+};
+
+/***********************************************************************
+ * stdin frontend code to remsim-client
+ ***********************************************************************/
+
+int frontend_request_card_insert(struct bankd_client *bc)
+{
+       LOGP(DMAIN, LOGL_INFO, "SIM inseterd\n");
+       return 0;
+}
+
+int frontend_request_card_remove(struct bankd_client *bc)
+{
+       LOGP(DMAIN, LOGL_INFO, "SIM removed\n");
+       return 0;
+}
+
+int frontend_request_sim_remote(struct bankd_client *bc)
+{
+       return 0;
+}
+
+int frontend_request_sim_local(struct bankd_client *bc)
+{
+       return 0;
+}
+
+int frontend_request_modem_reset(struct bankd_client *bc)
+{
+       return 0;
+}
+
+int frontend_handle_card2modem(struct bankd_client *bc, const uint8_t *data, 
size_t len)
+{
+       struct socket_state *ss = bc->data;
+       int rc;
+
+       OSMO_ASSERT(data);
+
+       LOGP(DMAIN, LOGL_INFO, "APDU response %s\n", osmo_hexdump(data, len));
+
+       rc = sendto(ss->ofd.fd, data, len, 0, (struct sockaddr *)&ss->addr, 
ss->addr_len);
+       if (rc < 0) {
+               LOGP(DMAIN, LOGL_ERROR, "Failed to write to socket (errno = 
%d).\n", errno);
+               return rc;
+       }
+
+       return 0;
+}
+
+int frontend_handle_set_atr(struct bankd_client *bc, const uint8_t *data, 
size_t len)
+{
+       struct socket_state *ss = bc->data;
+       int rc;
+
+       OSMO_ASSERT(data);
+
+       /* FIXME: Why do we get this "short" ATR before the actual one? */
+       if (len <= 2)
+               return -EINVAL;
+
+       LOGP(DMAIN, LOGL_INFO, "SET_ATR %s\n", osmo_hexdump(data, len));
+
+       rc = sendto(ss->ofd.fd, data, len, 0, (struct sockaddr *)&ss->addr, 
ss->addr_len);
+       if (rc < 0) {
+               LOGP(DMAIN, LOGL_ERROR, "Failed to write to socket (errno = 
%d).\n", errno);
+               return rc;
+       }
+       return 0;
+}
+
+int frontend_handle_slot_status(struct bankd_client *bc, const 
SlotPhysStatus_t *sts)
+{
+       return 0;
+}
+
+int frontend_append_script_env(struct bankd_client *bc, char **env, int idx, 
size_t max_env)
+{
+       return idx;
+}
+
+
+/***********************************************************************
+ * Incoming command from the user application (stdin shell in our case)
+ ***********************************************************************/
+
+/* call-back function for socket read. */
+static int socket_fd_cb(struct osmo_fd *ofd, unsigned int what)
+{
+       struct socket_state *ss = ofd->data;
+       struct frontend_tpdu ftpdu;
+       uint8_t buf[1024];
+       int rc;
+
+       OSMO_ASSERT(what & OSMO_FD_READ);
+
+        ss->addr_len = sizeof(ss->addr);
+       rc = recvfrom(ss->ofd.fd, buf, sizeof(buf), 0, (struct sockaddr 
*)&ss->addr, &ss->addr_len);
+       if (rc < 0) {
+               LOGP(DMAIN, LOGL_ERROR, "Failed to read from socket (errno = 
%d).\n", errno);
+               return rc;
+       }
+       if (rc == 0) {
+               /* Ignore empty frame. */
+               return 0;
+       }
+
+       LOGP(DMAIN, LOGL_DEBUG, "APDU request %s\n", osmo_hexdump(buf, rc));
+       ftpdu.buf = buf;
+       ftpdu.len = rc;
+       osmo_fsm_inst_dispatch(ss->bc->main_fi, MF_E_MDM_TPDU, &ftpdu);
+
+       return 0;
+}
+
+/* main function */
+int client_user_main(struct bankd_client *bc)
+{
+       struct client_config *cfg = bc->cfg;
+       struct socket_state ss;
+       int rc;
+
+       memset(&ss, 0, sizeof(ss));
+       ss.bc = bc;
+       bc->data = &ss;
+
+       if (!cfg->socket.name) {
+               LOGP(DMAIN, LOGL_ERROR, "No socket name specified.\n");
+               return -1;
+       }
+       rc = osmo_sock_unix_init(SOCK_DGRAM, 0, cfg->socket.name, 
OSMO_SOCK_F_BIND);
+       if (rc < 0) {
+               LOGP(DMAIN, LOGL_ERROR, "Failed to create socket with name '%s' 
(errno = %d).\n",
+                    cfg->socket.name, errno);
+               return -1;
+       }
+       osmo_fd_setup(&ss.ofd, rc, OSMO_FD_READ, &socket_fd_cb, &ss, 0);
+
+       rc = osmo_fd_register(&ss.ofd);
+       if (rc < 0)
+               return rc;
+
+       while (1) {
+               osmo_select_main(0);
+       }
+}

--
To view, visit https://gerrit.osmocom.org/c/osmo-remsim/+/43742?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings?usp=email

Gerrit-MessageType: newchange
Gerrit-Project: osmo-remsim
Gerrit-Branch: master
Gerrit-Change-Id: I9eddb60e4e7c3191ca2b340690416b53005f714f
Gerrit-Change-Number: 43742
Gerrit-PatchSet: 1
Gerrit-Owner: jolly <[email protected]>

Reply via email to