24 #define LOG_TAG "sensors"
30 static int write_protobuf_for_test(
socket_t sock, amqp_envelope_t* envelope)
32 return send(sock, envelope->message.body.bytes, envelope->message.body.len, 0);
36 static void* amqp_generic(
void* args)
38 amqp_envelope_t envelope;
39 amqp_connection_state_t conn;
40 int err_amqlisten = 0;
45 if (!strncmp(params->
sensor,
"sensors", 7))
61 LOGD(
"FREQ - %s sock=%d", params->
sensor, sock);
65 if (err_amqlisten == 0)
68 LOGM(
"Sending %d bytes to %s hardware device (:%d)", envelope.message.body.len + 4,
71 if (size != envelope.message.body.len + 4)
72 LOGW(
"Failed to send %d bytes to %s hardware device (:%d), error %d",
73 envelope.message.body.len + 4, params->
sensor, params->
port, size);
75 unsigned int size = write_protobuf_for_test(sock, &envelope);
76 LOGM(
"Sending %d bytes ", size);
82 LOGW(
"Unable to connect to hardware device %s (:%d)", params->
sensor, params->
port);
96 struct timespec duration = {0, params->
frequency * 1000};
97 nanosleep(&duration, NULL);
106 const char* amqp_host)
108 const int32_t str_length =
BUF_SIZE;
111 LOGE(
"ParamEventsWorker: out of memory");
114 paramListener->
gvmip = vmip;
116 g_strlcpy(paramListener->
sensor, sensor_name, str_length);
117 g_strlcpy(paramListener->
exchange, sensor_name, str_length);
118 snprintf(paramListener->
queue, str_length,
"android-events.%s.%s", vmid, sensor_name);
120 if (!strncmp(sensor_name,
"battery", 7))
125 else if (!strncmp(sensor_name,
"sensors", 7))
130 else if (!strncmp(sensor_name,
"gps", 3))
135 else if (!strncmp(sensor_name,
"gsm", 3))
140 else if (!strncmp(sensor_name,
"nfc", 3))
146 LOGE(
"Unkwnown sensor type: %s", sensor_name);
148 LOGD(
"ParamEventsWorker - %d ; %s ; %s ; %s ", paramListener->
port, paramListener->
gvmip,
151 return paramListener;
156 pthread_create(thread, 0, &amqp_generic, params);
162 char* amqp_host = NULL;
173 pthread_t threadsens;
177 signal(SIGPIPE, SIG_IGN);
178 LOGI(
"Starting sensor listening");
205 pthread_join(threadbat, NULL);
207 pthread_join(threadsens, NULL);
209 pthread_join(threadgps, NULL);
211 pthread_join(threadgsm, NULL);
215 #endif // UNIT_TESTING
Utilities to get config values from the environment.
#define LOGD(...)
Log at DEBUG level.
Utilities for consuming RabbitMQ messages.
#define PORT_GSM
Port for the GSM command in the VM.
char sensor[BUF_SIZE]
Sensor name.
Parameter for sensor threads.
int configvar_bool(char *varname)
Get the value of a boolean config variable from the env.
#define LOGE(...)
Log at ERROR level (makes the application abort)
#define BUF_SIZE
Small, fixed-size buffers.
sensor_params * ParamEventsWorker(const char *vmip, const char *vmid, const char *sensor_name, const char *amqp_host)
Creates the data structure for sensor threads.
#define PORT_GPS
Port for the GPS command in the VM.
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.
int socket_t
Alias to differenciate between regular ints and socket fds.
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.
socket_t open_socket(const char *ip, short port)
Connect to a host:port couple.
char queue[BUF_SIZE]
Queue name.
void * listen_NFC(void *args)
Listen to AMQP and send data to the NFC sensor in the VM.
Defines ports and structures for sensor threads.
char * configvar_string(char *varname)
Get the value of a config variable from the env.
char exchange[BUF_SIZE]
Exchange name.
#define PORT_SENSORS
Port for the "sensors" command in the VM.
#define PORT_NFC
Port for the NFC command in the VM.
int amqp_consume(amqp_connection_state_t *conn, amqp_envelope_t *envelope)
Consume one message from a connection object.
#define PORT_BAT
Port for the battery command in the VM.
void start_sensor(sensor_params *params, pthread_t *thread)
Start the sensor listener thread.
#define LOGM(...)
Log at MESSAGE level.
int32_t frequency
Sensor throttling.
#define LOGW(...)
Log at WARNING level.
socket_t open_socket_reuseaddr(const char *ip, short port)
Open a socket with SO_REUSEADDR.
#define SOCKET_ERROR
Alias for the recv() return value in case of error.
Define common buffer sizes.
Define socket utilities to simplify networking.
Utility to convert framing to varint32 for google’s parser.
#define LOGI(...)
Log at INFO level.
const char * amqp_host
AMQP host.