AiCPlayer
Interface of aic vm - for rendering aspect, sensors, video records
player_nfc.c
Go to the documentation of this file.
1 #include <pthread.h>
2 #include <time.h>
3 #include <unistd.h>
4 
5 #include "sensors.h"
6 #include "nfc.pb-c.h"
7 #include "buffer_sizes.h"
8 #include "socket.h"
9 #include "amqp_listen.h"
10 #include "protobuf_framing.h"
11 #include "logger.h"
12 #include "player_nfc.h"
13 
14 #define LOG_TAG "player_nfc"
15 
16 void* listen_NFC(void* args)
17 {
18  sensor_params* params = (sensor_params*) args;
19 
20  int err_amqlisten = 0;
21 
22  amqp_envelope_t envelope;
23  amqp_connection_state_t conn;
24  LOGM("listen_NFC - %s %s %s %s", params->exchange, params->queue, params->sensor,
25  params->queue);
26 
27  amqp_listen_retry(params->amqp_host, 5672, params->queue, &conn, 5);
28  socket_t sock = open_socket_reuseaddr(params->gvmip, params->port);
29 
30  int size = 0;
31  while (1)
32  {
33  LOGM("FREQ - %s sock=%d", params->sensor, sock);
34  if (sock != SOCKET_ERROR)
35  {
36  LOGM(" NFC ready waiting data from amqp");
37  err_amqlisten = amqp_consume(&conn, &envelope);
38  if (err_amqlisten == 0)
39  {
40  size = write_protobuf(sock, &envelope);
41  LOGM("NFC send to nfcd size=%d, on socket=%d", size, sock);
42 
43  close(sock);
44 #ifdef WITH_TESTING
45  pthread_exit(0);
46 #endif
47  }
48  }
49  else
50  {
51  LOGW("Unable to connect to hardware device %s (:%d)", params->sensor, params->port);
52  close(sock);
53  sleep(3);
54  sock = open_socket_reuseaddr(params->gvmip, params->port);
55  }
56  struct timespec duration = {0, params->frequency * 1000};
57  nanosleep(&duration, NULL);
58  }
59  return NULL;
60 }
Utilities for consuming RabbitMQ messages.
char sensor[BUF_SIZE]
Sensor name.
Definition: sensors.h:36
Parameter for sensor threads.
Definition: sensors.h:31
void * listen_NFC(void *args)
Listen to AMQP and send data to the NFC sensor in the VM.
Definition: player_nfc.c:16
const char * gvmip
VM IP.
Definition: sensors.h:42
int amqp_listen_retry(const char *hostname, int port, const char *bindingkey, amqp_connection_state_t *conn, const unsigned int tries)
Setup a consumer for a specific queue.
Definition: amqp_listen.c:17
int socket_t
Alias to differenciate between regular ints and socket fds.
Definition: socket.h:13
int write_protobuf(socket_t sock, amqp_envelope_t *envelope)
Write a protobuf, with a varint32 framing, and padding at the end from an envelope.
Nfc player.
char queue[BUF_SIZE]
Queue name.
Definition: sensors.h:40
Defines ports and structures for sensor threads.
char exchange[BUF_SIZE]
Exchange name.
Definition: sensors.h:38
int amqp_consume(amqp_connection_state_t *conn, amqp_envelope_t *envelope)
Consume one message from a connection object.
Definition: amqp_listen.c:90
Logging macros.
#define LOGM(...)
Log at MESSAGE level.
Definition: logger.h:25
int32_t frequency
Sensor throttling.
Definition: sensors.h:46
#define LOGW(...)
Log at WARNING level.
Definition: logger.h:27
socket_t open_socket_reuseaddr(const char *ip, short port)
Open a socket with SO_REUSEADDR.
Definition: socket.c:39
#define SOCKET_ERROR
Alias for the recv() return value in case of error.
Definition: socket.h:10
Define common buffer sizes.
Define socket utilities to simplify networking.
Utility to convert framing to varint32 for google’s parser.
const char * amqp_host
AMQP host.
Definition: sensors.h:44
int32_t port
Remote port.
Definition: sensors.h:34