AiCPlayer
Interface of aic vm - for rendering aspect, sensors, video records
testSensors.c
Go to the documentation of this file.
1 
2 #define PORT_SENSORS 22471
3 #define PORT_BAT 22473
4 #define PORT_GPS 22475
5 #define PORT_GSM 6704
6 #define PORT_NFC 22800
7 
8 #include <stdlib.h>
9 #include <stdarg.h>
10 #include <stddef.h>
11 #include <setjmp.h>
12 #include <google/cmockery.h>
13 #include "sensors_packet.pb-c.h"
14 #include "nfc.pb-c.h"
15 
16 #include "player_nfc.h"
17 #include "mockVMSensors.h"
18 #include "mockVMNfcd.h"
19 #include "mockVMLibNci.h"
20 
21 #include <stdio.h>
22 #include <signal.h>
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include "sensors.h"
26 #include "buffer_sizes.h"
27 #include "socket.h"
28 #include "stdint.h"
29 
30 #include "amqp_listen.h"
31 #include "amqp_send.h"
32 #include "config_env.h"
33 
34 #include "logger.h"
35 #include <pthread.h>
36 
37 #define LOG_TAG "testSensors"
38 
39 char* g_amqp_host = NULL;
40 char* g_vmid = NULL;
41 char* g_vmip = NULL;
42 
43 void test_sensors_acc(void** state)
44 {
45  (void) state;
46  char* g_amqp_host = NULL;
47  char* g_vmid = NULL;
48  char* g_vmip = NULL;
49  pthread_t sensor_thread;
50 
51  g_amqp_host = configvar_string("AIC_PLAYER_AMQP_HOST");
52  g_vmid = configvar_string("AIC_PLAYER_VM_ID");
53  g_vmip = configvar_string("AIC_PLAYER_VM_HOST");
54 
55  sensor_params* slp = ParamEventsWorker(g_vmip, g_vmid, "sensors", g_amqp_host);
56 
57  SensorsPacket__SensorAccelerometerPayload payload =
58  SENSORS_PACKET__SENSOR_ACCELEROMETER_PAYLOAD__INIT;
59 
60  payload.has_x = 1;
61  payload.has_y = 1;
62  payload.has_z = 1;
63  payload.x = 1;
64  payload.y = 2;
65  payload.z = 3;
66 
67  int msglen = protobuf_c_message_get_packed_size((const struct ProtobufCMessage*) &payload);
68  void* messagebody = malloc(msglen);
69 
70  int len = protobuf_c_message_pack((const struct ProtobufCMessage*) &payload, messagebody);
71 
72  amqp_send(g_amqp_host, 5672, slp->exchange, slp->queue, len, messagebody);
73  sleep(3);
74 
75  // MOCK senza filling the sensors amqp queue
76  free(messagebody);
77 
78  // Testing player_sensors
79  start_sensor(slp, &sensor_thread);
80 
81  // MOCK aicVM receving the sensors value from player_sensors
82  pthread_t* thread = malloc(sizeof(*thread));
83  sensor_params_acc* params = (sensor_params_acc*) malloc(sizeof(sensor_params_acc));
84  params->nbevent = 0;
85  params->toread = len;
86 
87  pthread_create(thread, 0, &mock_vm_recv_poll, params);
88  pthread_join(*thread, NULL);
89 
90  assert_int_equal(1, params->nbevent);
91  assert_int_equal(payload.x, params->payload->x);
92  assert_int_equal(payload.y, params->payload->y);
93  assert_int_equal(payload.z, params->payload->z);
94 
95  free(params);
96 }
97 
98 void test_sensors_nfc(void** state)
99 {
100  (void) state;
102 
103  NfcPayload payload = NFC_PAYLOAD__INIT;
104 
105  payload.has_lang = 1;
106  payload.has_type = 1;
107  payload.lang = 1;
108  payload.type = 1;
109  payload.text = "google.com";
110 
111  int msglen = nfc_payload__get_packed_size(&payload);
112  void* messagebody = malloc(msglen);
113 
114  int len = nfc_payload__pack(&payload, messagebody);
115 
116  amqp_send(g_amqp_host, 5672, slp->exchange, slp->queue, len, messagebody);
117  sleep(3);
118 
119  // MOCK senza filling the sensors amqp queue
120  free(messagebody);
121 
122  // Testing player_sensors
123  // listen_NFC( slp ) ;
124  pthread_t* thread0 = malloc(sizeof(*thread0));
125  void* retval;
126  pthread_create(thread0, 0, &listen_NFC, slp);
127  pthread_join(*thread0, &retval);
128 
129  // sleep(10);
130 
131  // //MOCK aicVM receving the sensors value from player_sensors
132  // pthread_t *thread = malloc(sizeof(*thread));
133  // nfc_params * params = (nfc_params *)malloc(sizeof(nfc_params ));
134  // params->nbevent=0;
135  // params->toread = len ;
136  // params->msg = (unsigned char* )malloc(sizeof(unsigned char)*128);
137  // pthread_create(thread, 0, &mock_vmnfc_recv_poll, params);
138  // pthread_join(*thread, NULL);
139  //
140  // assert_int_equal( 1 , params->nbevent );
141  // assert_int_equal( payload.lang , params->payload->lang);
142  // assert_int_equal( payload.type , params->payload->type);
143  // assert_string_equal( payload.text , params->payload->text);
144  //
145  // uint8_t *NDEF_RecGetType (uint8_t *p_rec, uint8_t *p_tnf, uint8_t *p_type_len);
146  // uint8_t *NDEF_RecGetId (uint8_t *p_rec, uint8_t *p_id_len);
147  // uint8_t *NDEF_RecGetPayload (uint8_t *p_rec, UINT32 *p_payload_len);
148  // tNDEF_STATUS _bValid = NDEF_MsgValidate (params->msg, params->len, 0);
149  //
150  assert_true(1);
151 
152  // pthread_cancel(*thread0);
153  // pthread_cancel(*thread);
154  //
155  // free(params);
156 }
157 
158 int main(int argc, char* argv[])
159 {
160  (void) argc;
161  (void) argv;
162  init_logger();
163  LOGI("Starting sensor listening");
164  g_amqp_host = configvar_string("AIC_PLAYER_AMQP_HOST");
165  g_vmid = configvar_string("AIC_PLAYER_VM_ID");
166  g_vmip = configvar_string("AIC_PLAYER_VM_HOST");
167 
168  UnitTest tests[] = {
169  unit_test(test_sensors_acc)
170  // unit_test(test_sensors_nfc)
171  };
172 
173  return run_tests(tests);
174 }
Utilities to get config values from the environment.
Utilities for consuming RabbitMQ messages.
Send AMQP messages.
SensorsPacket__SensorAccelerometerPayload * payload
Definition: mockVMSensors.h:11
sensor_params * ParamEventsWorker(const char *vmip, const char *vmid, const char *sensor_name, const char *amqp_host)
Creates the data structure for sensor threads.
Definition: sensors.c:105
char * g_vmip
Definition: testSensors.c:41
void * mock_vm_recv_poll(void *args)
Parameter for sensor threads.
Definition: sensors.h:31
int main(int argc, char *argv[])
Definition: testSensors.c:158
void test_sensors_acc(void **state)
Definition: testSensors.c:43
void init_logger()
Definition: logger.c:76
Nfc player.
char * g_amqp_host
Definition: testSensors.c:39
char * g_vmid
Definition: testSensors.c:40
char queue[BUF_SIZE]
Queue name.
Definition: sensors.h:40
void * listen_NFC(void *args)
Listen to AMQP and send data to the NFC sensor in the VM.
Definition: player_nfc.c:16
Defines ports and structures for sensor threads.
char * configvar_string(char *varname)
Get the value of a config variable from the env.
Definition: config_env.c:29
char exchange[BUF_SIZE]
Exchange name.
Definition: sensors.h:38
void start_sensor(sensor_params *params, pthread_t *thread)
Start the sensor listener thread.
Definition: sensors.c:154
Logging macros.
void test_sensors_nfc(void **state)
Definition: testSensors.c:98
Define common buffer sizes.
Define socket utilities to simplify networking.
#define LOGI(...)
Log at INFO level.
Definition: logger.h:23
int amqp_send(char const *hostname, int port, char const *exchange, char const *bindingkey, int size, unsigned char *messagebody)
Send a payload to an AMQP exchange.
Definition: amqp_send.c:71