diff options
author | Alon Levy <alevy@redhat.com> | 2011-02-02 15:19:56 +0200 |
---|---|---|
committer | Alon Levy <alevy@redhat.com> | 2011-02-03 16:54:27 +0200 |
commit | 0c3e8378a77cd2c1ee63251da561ec8d642755a4 (patch) | |
tree | 71b72ca14f3de66c39a35b721c22fc73c8e05f5f | |
parent | e60724af583c17e4cc1c978f3438334c2a3b7adc (diff) |
vscclient: send init on connect, only start vevent thread on response
-rw-r--r-- | vscclient.c | 73 |
1 files changed, 55 insertions, 18 deletions
diff --git a/vscclient.c b/vscclient.c index 253d0a6..8716d72 100644 --- a/vscclient.c +++ b/vscclient.c @@ -427,6 +427,48 @@ connect_to_qemu ( return sock; } +static int on_host_init(VSCMsgHeader *mhHeader, VSCMsgInit *incoming) +{ + uint32_t *capabilities = (incoming->capabilities); + int num_capabilities = 1 + ((mhHeader->length - sizeof(VSCMsgInit)) / sizeof(uint32_t)); + int i; + int rv; + pthread_t thread_id; + + incoming->version = ntohl(incoming->version); + if (incoming->version != VSCARD_VERSION) { + if (verbose > 0) { + printf("warning: host has version %d, we have %d\n", + verbose, VSCARD_VERSION); + } + } + if (incoming->magic != VSCARD_MAGIC) { + printf("unexpected magic: got %d, expected %d\n", + incoming->magic, VSCARD_MAGIC); + return -1; + } + for (i = 0 ; i < num_capabilities; ++i) { + capabilities[i] = ntohl(capabilities[i]); + } + /* Future: check capabilities */ + /* remove whatever reader might be left in qemu, + * in case of an unclean previous exit. */ + send_msg( + VSC_ReaderRemove, + VSCARD_MINIMAL_READER_ID, + NULL, + 0 + ); + /* launch the event_thread. This will trigger reader adds for all the + * existing readers */ + rv = pthread_create(&thread_id, NULL, event_thread, NULL); + if (rv < 0) { + perror("pthread_create"); + return rv; + } + return 0; +} + int main ( int argc, @@ -445,7 +487,6 @@ main ( VReaderStatus reader_status; VReader *reader = NULL; VCardEmulOptions *command_line_options = NULL; - pthread_t thread_id; int passthru = 0; char* cert_names[MAX_CERTS]; @@ -522,15 +563,6 @@ main ( qemu_port = strdup(argv[argc -1]); sock = connect_to_qemu(qemu_host, qemu_port); - /* remove whatever reader might be left in qemu, - * in case of a unclean previous exit. */ - send_msg( - VSC_ReaderRemove, - VSCARD_MINIMAL_READER_ID, - NULL, - 0 - ); - MUTEX_INIT(write_lock); MUTEX_INIT(pending_reader_lock); CONDITION_INIT(pending_reader_condition); @@ -542,17 +574,17 @@ main ( #endif vcard_emul_init(command_line_options); - /* launch the event_thread. This will trigger reader adds for all the - * existing readers */ - rv = pthread_create(&thread_id, NULL, event_thread, reader); - if (rv < 0) { - perror("pthread_create"); - exit (1); - } - printf("> "); fflush(stdout); + /* Send init message, Host responds (and then we send reader attachments) */ + VSCMsgInit init = { + .version=htonl(VSCARD_VERSION), + .magic=VSCARD_MAGIC, + .capabilities={0} + }; + send_msg(VSC_Init, mhHeader.reader_id, &init, sizeof(init)); + do { fd_set fds; @@ -684,6 +716,11 @@ main ( MUTEX_UNLOCK(pending_reader_lock); } break; + case VSC_Init: + if (on_host_init(&mhHeader, (VSCMsgInit*)pbSendBuffer) < 0) { + return -1; + } + break; default: printf ("Default\n"); return 0; |