9 #include <sys/socket.h>
10 #include <netinet/in.h>
12 #define LOG_TAG "mockVMSensors"
17 int clientSock = accept(serverSock, NULL, NULL);
20 LOGI(
"Client connection refused ");
27 if (clientsSocks[j] == 0)
29 clientsSocks[j] = clientSock;
30 LOGI(
"Client connection accepted (%d)", clientSock);
37 LOGI(
"Too many clients, connection refused");
46 struct sockaddr_in addr;
49 memset(&addr, 0,
sizeof(addr));
50 addr.sin_family = AF_INET;
51 addr.sin_port = htons(port);
52 addr.sin_addr.s_addr = htonl(INADDR_ANY);
54 s = socket(AF_INET, type, 0);
59 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &n,
sizeof(n));
61 if (bind(s, (
struct sockaddr*) &addr,
sizeof(addr)) < 0)
67 if (type == SOCK_STREAM)
72 LOGI(
"listen() returned %d", ret);
84 SensorsPacket__SensorAccelerometerPayload*
readBody(
int csock, uint32_t siz)
88 SensorsPacket__SensorAccelerometerPayload* payloadsens;
91 void* buffer = malloc(siz);
93 if ((bytecount = recv(csock, buffer, siz, MSG_WAITALL)) == -1)
95 LOGI(
"Error receiving data %d", bytecount);
98 LOGI(
" readBody -- Second read byte count is %d", bytecount);
100 payloadsens = (SensorsPacket__SensorAccelerometerPayload*) protobuf_c_message_unpack(
101 &sensors_packet__sensor_accelerometer_payload__descriptor, NULL, bytecount, buffer);
103 LOGI(
" readBody payloadsens->has_x %d", payloadsens->has_x);
104 LOGI(
" readBody payloadsens->has_y %d", payloadsens->has_y);
105 LOGI(
" readBody payloadsens->has_z %d", payloadsens->has_z);
107 LOGI(
" readBody payloadsens->x %f", payloadsens->x);
108 LOGI(
" readBody payloadsens->y %f", payloadsens->y);
109 LOGI(
" readBody payloadsens->z %f", payloadsens->z);
119 struct timeval max_delay;
121 max_delay.tv_sec = 0;
122 max_delay.tv_usec = 100000;
127 if (serverSock ==
SOCKET_ERROR || serverSock < 0 || serverSock > FD_SETSIZE)
140 FD_SET(serverSock, &readfs);
145 if (clientsSocks[i] > 0)
147 FD_SET(clientsSocks[i], &readfs);
149 if (clientsSocks[i] > maxfd)
151 maxfd = clientsSocks[i];
156 ret = select(maxfd + 1, &readfs, NULL, NULL, &max_delay);
161 if (maxfd == serverSock)
164 LOGI(
"Server closed connection, exiting. ");
172 if (clientsSocks[c] > 0)
174 close(clientsSocks[c]);
178 LOGI(
"Select fail, disconnect all clients ");
183 if (FD_ISSET(serverSock, &readfs))
191 if (FD_ISSET(clientsSocks[c], &readfs))
202 LOGI(
"Stop events read, we have read enough %d %f %f %f", params->
nbevent,
int socket_inaddr_any_server(int port, int type)
SensorsPacket__SensorAccelerometerPayload * payload
void * mock_vm_recv_poll(void *args)
#define LOGE(...)
Log at ERROR level (makes the application abort)
int acceptNewClient(int serverSock, int *clientsSocks)
#define PORT_SENSORS
Port for the "sensors" command in the VM.
SensorsPacket__SensorAccelerometerPayload * readBody(int csock, uint32_t siz)
#define SOCKET_ERROR
Alias for the recv() return value in case of error.
Define socket utilities to simplify networking.
#define LOGI(...)
Log at INFO level.