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>
18 #define LOG_TAG "mockVMNfcd"
22 #include <sys/types.h>
24 #define NFC_UPDATE_PERIOD 1
26 #define SIM_NFC_PORT 22800
28 static int start_server(uint16_t port)
31 struct sockaddr_in srv_addr;
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);
38 if ((server = socket(AF_INET, SOCK_STREAM, 0)) < 0)
40 LOGI(
" NFC Unable to create socket\n");
45 setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(
int));
47 if (bind(server, (
struct sockaddr*) &srv_addr,
sizeof(srv_addr)) < 0)
49 LOGI(
" NFC Unable to bind socket, errno=%d\n", errno);
56 static int wait_for_client(
int server)
60 if (listen(server, 1) < 0)
62 LOGI(
"Unable to listen to socket, errno=%d\n", errno);
66 client = accept(server, NULL, 0);
70 LOGI(
"Unable to accept socket for main conection, errno=%d\n", errno);
81 NfcPayload* payloadnfc;
84 void* buffer = malloc(siz);
86 LOGI(
" readBody -- inSize %d", siz);
89 if ((bytecount = recv(csock, buffer, siz, MSG_WAITALL)) == -1)
91 LOGI(
"Error receiving data %d", bytecount);
94 LOGI(
" readBody -- Second read byte count is %d", bytecount);
96 payloadnfc = nfc_payload__unpack(NULL, bytecount, buffer);
98 LOGI(
" readBody payloadnfc->has_type %d", payloadnfc->has_type);
99 LOGI(
" readBody payloadnfc->has_lang %d", payloadnfc->has_lang);
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);
124 if ((sim_server = start_server(22800)) == -1)
126 LOGI(
" NFC Unable to create socket\n");
131 while ((sim_client = wait_for_client(sim_server)) != -1)
136 memset(inbuf,
'\0', 256);
140 recv(sim_client, inbuf, 3, MSG_PEEK);
142 LOGI(
"NFC - local nfc server recv inbuf %x%x%x", inbuf[0], inbuf[1], inbuf[2]);
143 int tt = strcmp(inbuf,
"303");
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);
150 LOGI(
"NFC - local nfc server recv inbuf tt=%d - zSiz=%d", tt, zSiz);
152 LOGI(
"NFC:: reveivinf data");
156 NfcPayload* nfcData = params->
payload;
158 msg_len =
codeNFC(nfcData, msg);
162 for (
int ii = 0; ii < msg_len; ii++)
163 printf(
"0x%0x-", msg2[ii]);
167 memcpy(params->
msg, msg2, 1024);
168 params->
len = msg_len;
170 LOGI(
"Stop events read, we have read enough %d %d %s ", params->
payload->lang,
174 LOGI(
"NFC:: First read byte count is %d", bytecount);
void vshort_sendata(uint8_t *strIN, int sizLen, uint8_t *strOUT)
int codeNFC(NfcPayload *nfcData, uint8_t *msg)
#define NFC_UPDATE_PERIOD
void * mock_vmnfc_recv_poll(void *args)
NfcPayload * readNfcBody(int csock, uint32_t siz)
#define LOGI(...)
Log at INFO level.