AiCPlayer
Interface of aic vm - for rendering aspect, sensors, video records
mockVMSensors.c
Go to the documentation of this file.
1 
2 #include "mockVMSensors.h"
3 #include "logger.h"
4 #include "socket.h"
5 #include <pthread.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include <sys/socket.h>
10 #include <netinet/in.h>
11 
12 #define LOG_TAG "mockVMSensors"
13 
14 int acceptNewClient(int serverSock, int* clientsSocks)
15 {
16  int j = 0;
17  int clientSock = accept(serverSock, NULL, NULL);
18  if (clientSock == -1)
19  {
20  LOGI("Client connection refused ");
21  return -1;
22  }
23 
24  // Pick a slot in the client sockets list
25  for (j = 0; j < MAX_CLIENTS; j++)
26  {
27  if (clientsSocks[j] == 0)
28  {
29  clientsSocks[j] = clientSock;
30  LOGI("Client connection accepted (%d)", clientSock);
31  break;
32  }
33  }
34  // No space left in the client sockets table
35  if (j >= MAX_CLIENTS)
36  {
37  LOGI("Too many clients, connection refused");
38  return -1;
39  }
40  return 0;
41 }
42 
43 /* open listen() port on any interface */
44 int socket_inaddr_any_server(int port, int type)
45 {
46  struct sockaddr_in addr;
47  int s, n;
48 
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);
53 
54  s = socket(AF_INET, type, 0);
55  if (s < 0)
56  return -1;
57 
58  n = 1;
59  setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n));
60 
61  if (bind(s, (struct sockaddr*) &addr, sizeof(addr)) < 0)
62  {
63  close(s);
64  return -1;
65  }
66 
67  if (type == SOCK_STREAM)
68  {
69  int ret;
70 
71  ret = listen(s, 1);
72  LOGI("listen() returned %d", ret);
73 
74  if (ret < 0)
75  {
76  close(s);
77  return -1;
78  }
79  }
80 
81  return s;
82 }
83 
84 SensorsPacket__SensorAccelerometerPayload* readBody(int csock, uint32_t siz)
85 {
86  int bytecount;
87 
88  SensorsPacket__SensorAccelerometerPayload* payloadsens;
89  // protobuf_c_message_init(&sensors_packet__sensor_accelerometer_payload__descriptor,payloadsens);
90 
91  void* buffer = malloc(siz);
92  // Read the entire buffer including the hdr
93  if ((bytecount = recv(csock, buffer, siz, MSG_WAITALL)) == -1)
94  {
95  LOGI("Error receiving data %d", bytecount);
96  }
97 
98  LOGI(" readBody -- Second read byte count is %d", bytecount);
99 
100  payloadsens = (SensorsPacket__SensorAccelerometerPayload*) protobuf_c_message_unpack(
101  &sensors_packet__sensor_accelerometer_payload__descriptor, NULL, bytecount, buffer);
102 
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);
106 
107  LOGI(" readBody payloadsens->x %f", payloadsens->x);
108  LOGI(" readBody payloadsens->y %f", payloadsens->y);
109  LOGI(" readBody payloadsens->z %f", payloadsens->z);
110 
111  return payloadsens;
112 }
113 
114 void* mock_vm_recv_poll(void* args)
115 {
116  int ret;
117  int maxfd;
118  fd_set readfs;
119  struct timeval max_delay;
120 
121  max_delay.tv_sec = 0;
122  max_delay.tv_usec = 100000;
123 
124  sensor_params_acc* params = (sensor_params_acc*) args;
125 
126  int serverSock = socket_inaddr_any_server(PORT_SENSORS, SOCK_STREAM);
127  if (serverSock == SOCKET_ERROR || serverSock < 0 || serverSock > FD_SETSIZE)
128  {
129  LOGE("Unable to listen to %d", PORT_SENSORS);
130  return 0;
131  }
132  int clientsSocks[MAX_CLIENTS] = {0};
133 
134  while (1)
135  {
136  maxfd = serverSock;
137 
138  FD_ZERO(&readfs);
139 
140  FD_SET(serverSock, &readfs);
141 
142  // Add connected sockets to the list of sockets to watch
143  for (int i = 0; i < MAX_CLIENTS; i++)
144  {
145  if (clientsSocks[i] > 0)
146  {
147  FD_SET(clientsSocks[i], &readfs);
148  // make sure we stored the biggest fd
149  if (clientsSocks[i] > maxfd)
150  {
151  maxfd = clientsSocks[i];
152  }
153  }
154  }
155 
156  ret = select(maxfd + 1, &readfs, NULL, NULL, &max_delay);
157  // LOGI("select() returns %d", ret);
158 
159  if (ret < 0)
160  {
161  if (maxfd == serverSock)
162  {
163  close(serverSock);
164  LOGI("Server closed connection, exiting. ");
165  exit(EXIT_FAILURE);
166  }
167  else
168  {
169  // Something's wrong, disconnect every clients but keep server cnx
170  for (int c = 0; c < MAX_CLIENTS; c++)
171  {
172  if (clientsSocks[c] > 0)
173  {
174  close(clientsSocks[c]);
175  clientsSocks[c] = 0;
176  }
177  }
178  LOGI("Select fail, disconnect all clients ");
179  // continue;
180  }
181  }
182 
183  if (FD_ISSET(serverSock, &readfs))
184  {
185  acceptNewClient(serverSock, clientsSocks);
186  }
187 
188  params->nbevent = 0;
189  for (int c = 0; c < MAX_CLIENTS; c++)
190  {
191  if (FD_ISSET(clientsSocks[c], &readfs))
192  {
193  params->payload = readBody(clientsSocks[c], params->toread);
194  params->nbevent++;
195  // events_read += readData(&clientsSocks[c],
196  // &data[events_read],/* first empty slot */
197  // count - events_read /* space left */);
198  }
199  if (params->nbevent)
200  {
201  // Stop events read, we have read enough
202  LOGI("Stop events read, we have read enough %d %f %f %f", params->nbevent,
203  params->payload->x, params->payload->y, params->payload->z);
204  pthread_exit(NULL);
205  break;
206  }
207  }
208  // if no client write data, we should lastRead
209  }
210 
211  // return read events number
212 }
#define MAX_CLIENTS
Definition: mockVMNfcd.h:4
int socket_inaddr_any_server(int port, int type)
Definition: mockVMSensors.c:44
SensorsPacket__SensorAccelerometerPayload * payload
Definition: mockVMSensors.h:11
void * mock_vm_recv_poll(void *args)
#define LOGE(...)
Log at ERROR level (makes the application abort)
Definition: logger.h:31
int acceptNewClient(int serverSock, int *clientsSocks)
Definition: mockVMSensors.c:14
#define PORT_SENSORS
Port for the "sensors" command in the VM.
Definition: sensors.h:10
Logging macros.
SensorsPacket__SensorAccelerometerPayload * readBody(int csock, uint32_t siz)
Definition: mockVMSensors.c:84
#define SOCKET_ERROR
Alias for the recv() return value in case of error.
Definition: socket.h:10
Define socket utilities to simplify networking.
#define LOGI(...)
Log at INFO level.
Definition: logger.h:23