161 lines
4.1 KiB
C
161 lines
4.1 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdint.h>
|
|
#include <stdarg.h>
|
|
#include <string.h>
|
|
#include <termios.h>
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
#include <sys/ioctl.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <pthread.h>
|
|
#include <signal.h>
|
|
#include <math.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
#include <curl/curl.h>
|
|
#include "serialhandler.h"
|
|
|
|
#define ADDRESS "http://127.0.0.1:3000"
|
|
#define packet_size 28
|
|
|
|
char *jsonString =
|
|
"{\n\
|
|
\t\"sensorid\": %d\n\
|
|
}";
|
|
|
|
void sigintHandler(int sig_num)
|
|
{
|
|
signal(SIGINT, sigintHandler);
|
|
curl_global_cleanup();
|
|
exit(1);
|
|
}
|
|
|
|
int send_curl_data(char *message)
|
|
{
|
|
|
|
CURL *curl = curl_easy_init();
|
|
if(curl)
|
|
{
|
|
CURLcode res;
|
|
|
|
struct curl_slist *headers = NULL;
|
|
curl_slist_append(headers, "Accept: application/json");
|
|
curl_slist_append(headers, "Content-Type: application/json");
|
|
curl_slist_append(headers, "charset: utf-8");
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, ADDRESS);
|
|
//curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
|
|
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
|
curl_easy_setopt(curl, CURLOPT_POST, 1L);
|
|
curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, message);
|
|
curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcrp/0.1");
|
|
/* Set the expected POST size. */
|
|
//curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(jsonObj));
|
|
|
|
res = curl_easy_perform(curl);
|
|
printf("Curl returned with code %d\n", res);
|
|
if(res != CURLE_OK)
|
|
fprintf(stderr, "curl_easy_perform() failed: %s\n",
|
|
curl_easy_strerror(res));
|
|
|
|
/* always cleanup */
|
|
curl_easy_cleanup(curl);
|
|
}
|
|
}
|
|
|
|
int read_packet(uint16_t size, uint8_t buff[], const int fd)
|
|
{
|
|
uint16_t i;
|
|
|
|
//find start
|
|
do{ //olvasás amíg nem a start header(0xFB55) első bájtja
|
|
//(0x55 a litle endian miatt(kisebb hejiértékü bájt érkezik hamarabb))
|
|
|
|
while(! (serialDataAvail(fd) > 0)) //vár amíg nincs adat
|
|
{
|
|
usleep(200);
|
|
}
|
|
|
|
buff[0] = serialGetchar (fd);
|
|
}while(buff[0] != 0x55); //ha start header eleje akkro tovább
|
|
|
|
|
|
while(! (serialDataAvail(fd) > 0)) //vár amíg nincs adat
|
|
{
|
|
//thread sleep? kelhet
|
|
}
|
|
buff[1] = serialGetchar (fd);
|
|
if(buff[1] != 0xFB) //nem start header return error
|
|
return -1;
|
|
|
|
|
|
//ha igen akkor beolvassa a maradék 22 bájtot
|
|
while(! (serialDataAvail(fd) >= (size-2))) //vár amíg nincs adat
|
|
{
|
|
usleep(1);
|
|
}
|
|
|
|
if(serialGetarray(fd, &buff[2], (size-2)) == -1) //beolvas 22 bájtott a bufferbe a header utánra
|
|
{
|
|
return -1; //uart hiba
|
|
}
|
|
|
|
if(buff[size-2] != 0xAA | buff[size-1] != 0xFE) //ha az end header nem passzol error
|
|
return -1;
|
|
|
|
//todo crc check
|
|
|
|
return 1; //adat beolvasva
|
|
}
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
signal(SIGINT, sigintHandler);
|
|
uint8_t raw_buff[packet_size + 10]; //packet_size + egy kis tartalék, ha nincs hiba sehol akkor nem kell
|
|
curl_global_init(CURL_GLOBAL_ALL);
|
|
char *device = "/dev/ttyACM0";
|
|
int c, index;
|
|
while((c = getopt(argc, argv, "d:")) != -1)
|
|
{
|
|
switch (c)
|
|
{
|
|
case 'd':
|
|
device = (char*)malloc(sizeof(optarg));
|
|
strcpy(device, optarg);
|
|
break;
|
|
default:
|
|
abort();
|
|
}
|
|
}
|
|
for (index = optind; index < argc; index++) printf ("Non-option argument %s\n", argv[index]);
|
|
|
|
int fd = 0;
|
|
int connect_retry_count = 0;
|
|
|
|
do
|
|
{
|
|
break;
|
|
if (connect_retry_count == 0) //if not the first time
|
|
{
|
|
printf("Cannot open %s. Trying again... (%d)\n", device, connect_retry_count);
|
|
sleep(1000);
|
|
}
|
|
fd=serialOpen(device, 115200);
|
|
connect_retry_count++;
|
|
} while (fd < 0);
|
|
int stringSize = strlen(jsonString) + 200;
|
|
char* jsonObj = (char*)malloc(stringSize); //"{ \"name\" : \"Pedro\" , \"age\" : \"22\" }";
|
|
snprintf(jsonObj, stringSize, jsonString, 15);
|
|
|
|
while (1)
|
|
{
|
|
if (1)//if(read_packet(packet_size, &raw_buff[0], fd) != -1)
|
|
{
|
|
printf("Sending: %s\n", jsonObj);
|
|
send_curl_data(jsonObj);
|
|
sleep(3);
|
|
}
|
|
}
|
|
} |