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