AiCPlayer
Interface of aic vm - for rendering aspect, sensors, video records
mockVMNfcd.c
Go to the documentation of this file.
1 #include <netinet/tcp.h>
2 #include <sys/socket.h>
3 #include <netinet/in.h>
4 #include <sys/select.h>
5 #include <sys/socket.h>
6 #include <sys/errno.h>
7 #include <arpa/inet.h>
8 
9 #include <stdio.h>
10 #include <unistd.h>
11 #include <stdlib.h>
12 #include <pthread.h>
13 
14 #include "mockVMNfcd.h"
15 #include "logger.h"
16 #include <pthread.h>
17 
18 #define LOG_TAG "mockVMNfcd"
19 
20 #include "mockVMLibNci.h"
21 
22 #include <sys/types.h>
23 
24 #define NFC_UPDATE_PERIOD 1 /* period in sec between 2 nfc fix emission */
25 
26 #define SIM_NFC_PORT 22800
27 
28 static int start_server(uint16_t port)
29 {
30  int server = -1;
31  struct sockaddr_in srv_addr;
32 
33  bzero(&srv_addr, sizeof(srv_addr));
34  srv_addr.sin_family = AF_INET;
35  srv_addr.sin_addr.s_addr = INADDR_ANY;
36  srv_addr.sin_port = htons(port);
37 
38  if ((server = socket(AF_INET, SOCK_STREAM, 0)) < 0)
39  {
40  LOGI(" NFC Unable to create socket\n");
41  return -1;
42  }
43 
44  int yes = 1;
45  setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
46 
47  if (bind(server, (struct sockaddr*) &srv_addr, sizeof(srv_addr)) < 0)
48  {
49  LOGI(" NFC Unable to bind socket, errno=%d\n", errno);
50  return -1;
51  }
52 
53  return server;
54 }
55 
56 static int wait_for_client(int server)
57 {
58  int client = -1;
59 
60  if (listen(server, 1) < 0)
61  {
62  LOGI("Unable to listen to socket, errno=%d\n", errno);
63  return -1;
64  }
65 
66  client = accept(server, NULL, 0);
67 
68  if (client < 0)
69  {
70  LOGI("Unable to accept socket for main conection, errno=%d\n", errno);
71  return -1;
72  }
73 
74  return client;
75 }
76 
77 NfcPayload* readNfcBody(int csock, uint32_t siz)
78 {
79  int bytecount = 0;
80 
81  NfcPayload* payloadnfc;
82  // nfc_payload__init(payloadnfc);
83 
84  void* buffer = malloc(siz);
85 
86  LOGI(" readBody -- inSize %d", siz);
87 
88  // Read the entire buffer including the hdr
89  if ((bytecount = recv(csock, buffer, siz, MSG_WAITALL)) == -1)
90  {
91  LOGI("Error receiving data %d", bytecount);
92  }
93 
94  LOGI(" readBody -- Second read byte count is %d", bytecount);
95 
96  payloadnfc = nfc_payload__unpack(NULL, bytecount, buffer);
97 
98  LOGI(" readBody payloadnfc->has_type %d", payloadnfc->has_type);
99  LOGI(" readBody payloadnfc->has_lang %d", payloadnfc->has_lang);
100 
101  LOGI(" readBody payloadnfc->type %d", payloadnfc->type);
102  LOGI(" readBody payloadnfc->lang %d", payloadnfc->lang);
103  LOGI(" readBody payloadnfc->text %s", payloadnfc->text);
104  LOGI(" readBody payloadnfc->tittle %s", payloadnfc->tittle);
105 
106  return payloadnfc;
107 }
108 
109 void* mock_vmnfc_recv_poll(void* args)
110 {
111  int sim_server = -1;
112  int sim_client = -1;
113 
114  char inbuf[256];
115  int bytecount = 0;
116 
117  int zSiz = 0;
118  uint8_t msg[1024];
119  uint8_t msg2[1024];
120  int msg_len = 0;
121 
122  nfc_params* params = (nfc_params*) args;
123 
124  if ((sim_server = start_server(22800)) == -1)
125  {
126  LOGI(" NFC Unable to create socket\n");
127  return NULL;
128  }
129 
130  // Listen for main connection
131  while ((sim_client = wait_for_client(sim_server)) != -1)
132  {
133  // Update NFC info every NFC_UPDATE_PERIOD seconds
134  sleep(NFC_UPDATE_PERIOD);
135 
136  memset(inbuf, '\0', 256);
137 
138  // Peek into the socket and get the packet size
139  // bytecount = send (sim_client,cmdbuf, 3, 0 ) ;
140  recv(sim_client, inbuf, 3, MSG_PEEK);
141 
142  LOGI("NFC - local nfc server recv inbuf %x%x%x", inbuf[0], inbuf[1], inbuf[2]);
143  int tt = strcmp(inbuf, "303");
144  if (tt == 0)
145  {
146  bytecount = recv(sim_client, inbuf, 5, MSG_WAITALL);
147  LOGI("NFC - local nfc server recv inbuf - bytecount %d", bytecount);
148  zSiz = (int) strtol(inbuf + 3, NULL, 10);
149  }
150  LOGI("NFC - local nfc server recv inbuf tt=%d - zSiz=%d", tt, zSiz);
151 
152  LOGI("NFC:: reveivinf data");
153  params->payload = readNfcBody(sim_client, params->toread);
154  params->nbevent++;
155 
156  NfcPayload* nfcData = params->payload;
157 
158  msg_len = codeNFC(nfcData, msg);
159 
160  vshort_sendata(msg, msg_len, msg2);
161 
162  for (int ii = 0; ii < msg_len; ii++)
163  printf("0x%0x-", msg2[ii]);
164 
165  // size = tcp_write_buff( sock, msg2, len2send );
166 
167  memcpy(params->msg, msg2, 1024);
168  params->len = msg_len;
169 
170  LOGI("Stop events read, we have read enough %d %d %s ", params->payload->lang,
171  params->payload->type, params->payload->text);
172  pthread_exit(NULL);
173 
174  LOGI("NFC:: First read byte count is %d", bytecount);
175  }
176  return NULL;
177 }
unsigned char * msg
Definition: mockVMNfcd.h:16
uint32_t nbevent
Definition: mockVMNfcd.h:15
void vshort_sendata(uint8_t *strIN, int sizLen, uint8_t *strOUT)
unsigned int len
Definition: mockVMNfcd.h:17
NfcPayload * payload
Definition: mockVMNfcd.h:13
int codeNFC(NfcPayload *nfcData, uint8_t *msg)
Definition: mockVMLibNci.c:145
#define NFC_UPDATE_PERIOD
Definition: mockVMNfcd.c:24
void * mock_vmnfc_recv_poll(void *args)
Definition: mockVMNfcd.c:109
Logging macros.
uint32_t toread
Definition: mockVMNfcd.h:14
NfcPayload * readNfcBody(int csock, uint32_t siz)
Definition: mockVMNfcd.c:77
#define LOGI(...)
Log at INFO level.
Definition: logger.h:23