NerfUS / NerfUSXbee

Dependents:   NerfUS-Coord NerfUSTarget

Fork of APP3_xbee by Team APP

Committer:
dupm2216
Date:
Sun Feb 12 21:33:48 2017 +0000
Revision:
4:e97cfe6cc18c
Parent:
3:37ea92feece2
Child:
5:cd3c79853dc8
Handle received frames of any type

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dupm2216 0:8f5379c94a69 1 #include "xbee.h"
dupm2216 4:e97cfe6cc18c 2 #include <cassert>
dupm2216 0:8f5379c94a69 3
dupm2216 0:8f5379c94a69 4 DigitalOut led_1(LED1);
dupm2216 4:e97cfe6cc18c 5 Mail<vector<char>, 30> parsed_frames;
dupm2216 0:8f5379c94a69 6 RawSerial xbee(p13, p14);
dupm2216 0:8f5379c94a69 7 const int FRAME_SPECIFIC_DATA_BEGIN[14] = {0x10, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFE, 0x00, 0x00};
dupm2216 0:8f5379c94a69 8 const int RECEIVE_PACKET_MESSAGE_START_INDEX = 15;
dupm2216 0:8f5379c94a69 9
dupm2216 0:8f5379c94a69 10 const char START_DELIMITER = 0x7E;
dupm2216 0:8f5379c94a69 11
dupm2216 0:8f5379c94a69 12 void send_message_via_xbee(const char* message, const int length)
dupm2216 0:8f5379c94a69 13 {
dupm2216 0:8f5379c94a69 14 const vector<char> transmit_request = generate_transmit_request(message, length);
dupm2216 0:8f5379c94a69 15
dupm2216 0:8f5379c94a69 16 for(int i=0; i<transmit_request.size(); i++)
dupm2216 0:8f5379c94a69 17 {
dupm2216 0:8f5379c94a69 18 xbee.putc(transmit_request[i]);
dupm2216 0:8f5379c94a69 19 }
dupm2216 0:8f5379c94a69 20 }
dupm2216 0:8f5379c94a69 21
dupm2216 0:8f5379c94a69 22 vector<char> generate_transmit_request(const char* message, const int length)
dupm2216 0:8f5379c94a69 23 {
dupm2216 0:8f5379c94a69 24 vector<char> request;
dupm2216 0:8f5379c94a69 25
dupm2216 0:8f5379c94a69 26 unsigned char checksum = 0xFF;
dupm2216 0:8f5379c94a69 27
dupm2216 0:8f5379c94a69 28 request.push_back(START_DELIMITER);
dupm2216 0:8f5379c94a69 29
dupm2216 0:8f5379c94a69 30 const uint16_t frame_length = 0x0E + length;
dupm2216 0:8f5379c94a69 31 const uint8_t frame_length_msb = frame_length >> 8;
dupm2216 0:8f5379c94a69 32 const uint8_t frame_length_lsb = (frame_length << 8) >> 8;
dupm2216 0:8f5379c94a69 33 request.push_back(frame_length_msb);
dupm2216 0:8f5379c94a69 34 request.push_back(frame_length_lsb);
dupm2216 0:8f5379c94a69 35
dupm2216 0:8f5379c94a69 36 for(int i=0; i<14; i++)
dupm2216 0:8f5379c94a69 37 {
dupm2216 0:8f5379c94a69 38 request.push_back(FRAME_SPECIFIC_DATA_BEGIN[i]);
dupm2216 0:8f5379c94a69 39 checksum -= FRAME_SPECIFIC_DATA_BEGIN[i];
dupm2216 0:8f5379c94a69 40 }
dupm2216 0:8f5379c94a69 41
dupm2216 0:8f5379c94a69 42 for(int i=0; i<length; i++)
dupm2216 0:8f5379c94a69 43 {
dupm2216 0:8f5379c94a69 44 request.push_back(message[i]);
dupm2216 0:8f5379c94a69 45 checksum -= message[i];
dupm2216 0:8f5379c94a69 46 }
dupm2216 0:8f5379c94a69 47
dupm2216 0:8f5379c94a69 48 request.push_back(checksum);
dupm2216 0:8f5379c94a69 49
dupm2216 0:8f5379c94a69 50 return request;
dupm2216 0:8f5379c94a69 51 }
dupm2216 0:8f5379c94a69 52
dupm2216 0:8f5379c94a69 53 void read_frame()
dupm2216 0:8f5379c94a69 54 {
dupm2216 0:8f5379c94a69 55 while(true)
dupm2216 0:8f5379c94a69 56 {
dupm2216 0:8f5379c94a69 57 while(xbee.getc() != 0x7E);
dupm2216 0:8f5379c94a69 58
dupm2216 0:8f5379c94a69 59 vector<char> frame;
dupm2216 0:8f5379c94a69 60
dupm2216 0:8f5379c94a69 61 frame.push_back(0x7E);
dupm2216 0:8f5379c94a69 62
dupm2216 0:8f5379c94a69 63 const uint8_t frame_size_msb = xbee.getc();
dupm2216 0:8f5379c94a69 64 const uint8_t frame_size_lsb = xbee.getc();
dupm2216 0:8f5379c94a69 65 frame.push_back(frame_size_msb);
dupm2216 0:8f5379c94a69 66 frame.push_back(frame_size_lsb);
dupm2216 0:8f5379c94a69 67 const uint16_t frame_size = (frame_size_msb << 8) + frame_size_lsb;
dupm2216 0:8f5379c94a69 68
dupm2216 0:8f5379c94a69 69 for(int i=0; i<frame_size + 1; i++)
dupm2216 0:8f5379c94a69 70 {
dupm2216 0:8f5379c94a69 71 frame.push_back(xbee.getc());
dupm2216 0:8f5379c94a69 72 }
dupm2216 4:e97cfe6cc18c 73
dupm2216 4:e97cfe6cc18c 74 handle_frame(frame);
dupm2216 0:8f5379c94a69 75 }
dupm2216 0:8f5379c94a69 76 }
dupm2216 0:8f5379c94a69 77
dupm2216 3:37ea92feece2 78 vector<char> parse_receive_packet(const vector<char>& frame)
dupm2216 0:8f5379c94a69 79 {
dupm2216 3:37ea92feece2 80 vector<char>::const_iterator first = frame.begin() + RECEIVE_PACKET_MESSAGE_START_INDEX;
dupm2216 3:37ea92feece2 81 vector<char>::const_iterator last = frame.end() - 1;
dupm2216 0:8f5379c94a69 82 vector<char> message(first, last);
dupm2216 0:8f5379c94a69 83 return message;
dupm2216 0:8f5379c94a69 84 }
dupm2216 0:8f5379c94a69 85
dupm2216 3:37ea92feece2 86 vector<char> parse_transmit_status(const vector<char>& frame)
dupm2216 3:37ea92feece2 87 {
dupm2216 3:37ea92feece2 88 vector<char> relevant_content;
dupm2216 3:37ea92feece2 89
dupm2216 3:37ea92feece2 90 const char delivery_status = frame.at(8);
dupm2216 3:37ea92feece2 91 relevant_content.push_back(delivery_status);
dupm2216 3:37ea92feece2 92
dupm2216 3:37ea92feece2 93 return relevant_content;
dupm2216 3:37ea92feece2 94 }
dupm2216 3:37ea92feece2 95
dupm2216 3:37ea92feece2 96 vector<char> parse_at_command_response(const vector<char>& frame)
dupm2216 3:37ea92feece2 97 {
dupm2216 3:37ea92feece2 98 vector<char> relevant_content;
dupm2216 3:37ea92feece2 99
dupm2216 3:37ea92feece2 100 const char command_status = frame.at(7);
dupm2216 3:37ea92feece2 101 relevant_content.push_back(command_status);
dupm2216 3:37ea92feece2 102
dupm2216 3:37ea92feece2 103 return relevant_content;
dupm2216 3:37ea92feece2 104 }
dupm2216 3:37ea92feece2 105
dupm2216 4:e97cfe6cc18c 106 void handle_parsed_frames_from_mailbox()
dupm2216 0:8f5379c94a69 107 {
dupm2216 0:8f5379c94a69 108 while(true)
dupm2216 0:8f5379c94a69 109 {
dupm2216 4:e97cfe6cc18c 110 osEvent event = parsed_frames.get();
dupm2216 4:e97cfe6cc18c 111 assert(event.status == osEventMail);
dupm2216 4:e97cfe6cc18c 112
dupm2216 4:e97cfe6cc18c 113 vector<char> *message = (vector<char>*)event.value.p;
dupm2216 4:e97cfe6cc18c 114
dupm2216 4:e97cfe6cc18c 115 const char parsed_frame_type = message->at(0);
dupm2216 4:e97cfe6cc18c 116 printf("Handling parsed frame. Type: %d\r\n", parsed_frame_type);
dupm2216 4:e97cfe6cc18c 117
dupm2216 4:e97cfe6cc18c 118 printf("Relevant content: ");
dupm2216 4:e97cfe6cc18c 119 for(int i=1; i<message->size(); i++)
dupm2216 0:8f5379c94a69 120 {
dupm2216 4:e97cfe6cc18c 121 printf("%d\r\n", message->at(i));
dupm2216 0:8f5379c94a69 122 }
dupm2216 4:e97cfe6cc18c 123
dupm2216 4:e97cfe6cc18c 124 parsed_frames.free(message);
dupm2216 0:8f5379c94a69 125 }
dupm2216 3:37ea92feece2 126 }
dupm2216 3:37ea92feece2 127
dupm2216 4:e97cfe6cc18c 128 void handle_frame(const vector<char>& frame)
dupm2216 4:e97cfe6cc18c 129 {
dupm2216 4:e97cfe6cc18c 130 vector<char> *parsed_frame = parsed_frames.alloc();
dupm2216 4:e97cfe6cc18c 131 *parsed_frame = parse_frame(frame);
dupm2216 4:e97cfe6cc18c 132 parsed_frames.put(parsed_frame);
dupm2216 4:e97cfe6cc18c 133 }
dupm2216 4:e97cfe6cc18c 134
dupm2216 3:37ea92feece2 135 vector<char> parse_frame(const vector<char>& frame)
dupm2216 3:37ea92feece2 136 {
dupm2216 3:37ea92feece2 137 vector<char> parsed_frame;
dupm2216 3:37ea92feece2 138
dupm2216 3:37ea92feece2 139 const char frame_type = frame.at(3);
dupm2216 3:37ea92feece2 140 parsed_frame.push_back(frame_type);
dupm2216 3:37ea92feece2 141
dupm2216 3:37ea92feece2 142 vector<char> parsed_frame_relevant_content;
dupm2216 3:37ea92feece2 143
dupm2216 3:37ea92feece2 144 switch(frame_type)
dupm2216 3:37ea92feece2 145 {
dupm2216 3:37ea92feece2 146 case FRAME_TYPE_RECEIVE_PACKET:
dupm2216 3:37ea92feece2 147 {
dupm2216 3:37ea92feece2 148 parsed_frame_relevant_content = parse_receive_packet(frame);
dupm2216 3:37ea92feece2 149 break;
dupm2216 3:37ea92feece2 150 }
dupm2216 3:37ea92feece2 151 case FRAME_TYPE_TRANSMIT_STATUS:
dupm2216 3:37ea92feece2 152 {
dupm2216 3:37ea92feece2 153 parsed_frame_relevant_content = parse_transmit_status(frame);
dupm2216 3:37ea92feece2 154 break;
dupm2216 3:37ea92feece2 155 }
dupm2216 3:37ea92feece2 156 case FRAME_TYPE_AT_COMMAND_RESPONSE:
dupm2216 3:37ea92feece2 157 {
dupm2216 3:37ea92feece2 158 parsed_frame_relevant_content = parse_at_command_response(frame);
dupm2216 3:37ea92feece2 159 break;
dupm2216 3:37ea92feece2 160 }
dupm2216 3:37ea92feece2 161 default: assert(false && "Unsupported frame type");
dupm2216 3:37ea92feece2 162 }
dupm2216 3:37ea92feece2 163
dupm2216 3:37ea92feece2 164 for(vector<char>::iterator it = parsed_frame_relevant_content.begin(); it < parsed_frame_relevant_content.end(); it++)
dupm2216 3:37ea92feece2 165 {
dupm2216 3:37ea92feece2 166 parsed_frame.push_back(*it);
dupm2216 3:37ea92feece2 167 }
dupm2216 3:37ea92feece2 168
dupm2216 3:37ea92feece2 169 return parsed_frame;
dupm2216 3:37ea92feece2 170 }