root / branches / slam / code / projects / colonet / DataRequests / server / data_requests.c @ 201
History | View | Annotate | Download (2.56 KB)
1 |
#include "data_requests.h" |
---|---|
2 |
#include <stdlib.h> |
3 |
#include <stdio.h> |
4 |
#include <pthread.h> |
5 |
#include "../../../libwireless/lib/wireless.h" |
6 |
|
7 |
PacketGroupHandler pgh; |
8 |
pthread_t* dr_thread; |
9 |
|
10 |
void receive_handle(char type, int source, unsigned char* packet, int length); |
11 |
|
12 |
void (*bom_handle)(BomNode* head);
|
13 |
void (*IR_handle)(unsigned char* data); |
14 |
void (*encoder_handle)(unsigned char* data); |
15 |
|
16 |
void data_requests_init(void(*bom_data_handler)(BomNode* head), |
17 |
void(*IR_data_handler)(unsigned char* data), |
18 |
void(*encoder_data_handler)(unsigned char* data)){ |
19 |
bom_handle = bom_data_handler; |
20 |
IR_handle = IR_data_handler; |
21 |
encoder_handle = encoder_data_handler; |
22 |
|
23 |
pgh.groupCode = DATA_REQUEST_GROUP; |
24 |
pgh.timeout_handler = NULL;
|
25 |
pgh.handle_response = NULL;
|
26 |
pgh.handle_receive = receive_handle; |
27 |
pgh.unregister = NULL;
|
28 |
|
29 |
wl_register_packet_group(&pgh); |
30 |
} |
31 |
|
32 |
|
33 |
void request_bom_data(int robot_id){ |
34 |
wl_send_robot_to_robot_global_packet(DATA_REQUEST_GROUP,BOM_TYPE,NULL,0,robot_id,0); |
35 |
} |
36 |
|
37 |
void request_IR_data(int robot_id){ |
38 |
wl_send_robot_to_robot_global_packet(DATA_REQUEST_GROUP,IR_TYPE,NULL,0,robot_id,0); |
39 |
} |
40 |
|
41 |
void request_encoder_data(int robot_id){ |
42 |
wl_send_robot_to_robot_global_packet(DATA_REQUEST_GROUP,ENCODER_TYPE,NULL,0,robot_id,0); |
43 |
} |
44 |
|
45 |
void* pthread_handle(void* arg){ |
46 |
|
47 |
RawData* data = (RawData*)arg; |
48 |
unsigned char* packet = data->packet; |
49 |
int length = data->length;
|
50 |
char type = data->type;
|
51 |
|
52 |
int i=0; |
53 |
BomNode* head; |
54 |
switch(type){
|
55 |
|
56 |
case BOM_TYPE:
|
57 |
//BomNode* head;
|
58 |
head = malloc(sizeof(BomNode));
|
59 |
|
60 |
BomNode* iter = head; |
61 |
|
62 |
while(i<length){
|
63 |
iter->robot_id = ((short)packet[i]<<8) + ((short)packet[i+1]); |
64 |
i+=2;
|
65 |
iter->value = packet[i++]; |
66 |
if(i<length){
|
67 |
iter-> next = malloc(sizeof(BomNode));
|
68 |
iter = iter->next; |
69 |
} |
70 |
} |
71 |
bom_handle(head); |
72 |
break;
|
73 |
|
74 |
case IR_TYPE:
|
75 |
/*An IR packet consists of just the 5 bytes representing the IR
|
76 |
values, in order.*/
|
77 |
if(length != 4){ |
78 |
//The packet is incomplete... bail.
|
79 |
break;
|
80 |
} |
81 |
else{
|
82 |
unsigned char data[4]; |
83 |
for(i=0;i<4;i++){ |
84 |
data[i] = packet[i]; |
85 |
} |
86 |
IR_handle(data); |
87 |
} |
88 |
break;
|
89 |
|
90 |
case ENCODER_TYPE:
|
91 |
/*I have no idea, unsupported.*/
|
92 |
break;
|
93 |
} |
94 |
} |
95 |
void receive_handle(char type, int source, unsigned char* packet, int length){ |
96 |
RawData data; |
97 |
data.packet=packet; |
98 |
data.source=source; |
99 |
data.length=length; |
100 |
data.type=type; |
101 |
pthread_create(dr_thread,NULL,pthread_handle,(void*)&data); |
102 |
} |