-
Notifications
You must be signed in to change notification settings - Fork 1
/
comm.cpp
66 lines (61 loc) · 1.56 KB
/
comm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "comm.hpp"
extern "C" {
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "serio.h"
#include "crc16.h"
#include "packet.h"
#include <string.h>
#include <arpa/inet.h>
pthread_t comm_th;
pthread_mutex_t lck;
int rv, comm_run;
uint8_t s_power,s_steer;
void packet_crc(packet_t *p){
p->cksum = htons(compute_crc((char *)p,sizeof(packet_t)-sizeof(uint16_t)));
}
void* comm_thread(void * serial_port){
char msg[RECVBUF];
connection_t c;
packet_t ctl;
serio_init(&c, (char *)serial_port);
while(comm_run){
if(serio_recv(&c, msg) < 0){
printf("Error reading data!\n");
return 0;
}
//update data here
pthread_mutex_lock( &lck );
ctl.power=s_power;
ctl.steer=s_steer;
pthread_mutex_unlock( &lck );
packet_crc(&ctl);
if(serio_send(&c, &ctl, sizeof(packet_t)) < 0){
printf("Unable to send data!\n");
}
printf("power: %i, steering: %i, CRC: %i, Resp: %s\n", ctl.power, ctl.steer, ctl.cksum, msg);
}
}
int comm_start(char * serial_port){
comm_run=1;
s_power=127;
s_steer=127;
lck = PTHREAD_MUTEX_INITIALIZER;
if((rv=pthread_create( &comm_th, NULL, comm_thread, (void *)serial_port)))
{
fprintf(stderr,"Error - pthread_create() return code: %d\n",rv);
exit(EXIT_FAILURE);
}
}
void comm_stop(){
comm_run=0;
pthread_join(comm_th, NULL);
}
void comm_check(){
if(!pthread_tryjoin_np(comm_th,NULL)){
printf("FATAL: Serial communication failed, exiting!\n");
exit(4);
}
}
}