XBee API mode library

Committer:
yamaguch
Date:
Thu Mar 21 06:51:31 2013 +0000
Revision:
17:2f728fd13bc0
Parent:
16:cdfcb63b2c4b
rewrote to use rtos Semaphore only

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yamaguch 0:0232a97b3883 1 /*
yamaguch 5:b82970ef7fb0 2 Copyright (c) 2013, Senio Networks, Inc.
yamaguch 0:0232a97b3883 3
yamaguch 0:0232a97b3883 4 Permission is hereby granted, free of charge, to any person obtaining a copy
yamaguch 0:0232a97b3883 5 of this software and associated documentation files (the "Software"), to deal
yamaguch 0:0232a97b3883 6 in the Software without restriction, including without limitation the rights
yamaguch 0:0232a97b3883 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
yamaguch 0:0232a97b3883 8 copies of the Software, and to permit persons to whom the Software is
yamaguch 0:0232a97b3883 9 furnished to do so, subject to the following conditions:
yamaguch 0:0232a97b3883 10
yamaguch 0:0232a97b3883 11 The above copyright notice and this permission notice shall be included in
yamaguch 0:0232a97b3883 12 all copies or substantial portions of the Software.
yamaguch 0:0232a97b3883 13
yamaguch 0:0232a97b3883 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
yamaguch 0:0232a97b3883 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
yamaguch 0:0232a97b3883 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
yamaguch 0:0232a97b3883 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
yamaguch 0:0232a97b3883 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
yamaguch 0:0232a97b3883 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
yamaguch 0:0232a97b3883 20 THE SOFTWARE.
yamaguch 0:0232a97b3883 21 */
yamaguch 0:0232a97b3883 22
yamaguch 0:0232a97b3883 23 #include "XBee.h"
yamaguch 0:0232a97b3883 24
yamaguch 17:2f728fd13bc0 25 #define INDEX(n) ((n) % BUFSIZE)
yamaguch 17:2f728fd13bc0 26 #define SIZE(b, i) (b[i] << 8 | b[INDEX(i + 1)])
yamaguch 17:2f728fd13bc0 27 #define min(x, y) ((x) <= (y) ? (x) : (y))
yamaguch 17:2f728fd13bc0 28
yamaguch 17:2f728fd13bc0 29 bool XBee::scan(XBeeFrameID& id) {
yamaguch 17:2f728fd13bc0 30 return scan(XBee::FrameID, id.raw_address(), 1);
yamaguch 17:2f728fd13bc0 31 }
yamaguch 17:2f728fd13bc0 32
yamaguch 17:2f728fd13bc0 33 bool XBee::scan(XBeeRetryCount& count) {
yamaguch 17:2f728fd13bc0 34 return scan(XBee::RetryCount, count.raw_address(), 1);
yamaguch 17:2f728fd13bc0 35 }
yamaguch 17:2f728fd13bc0 36
yamaguch 17:2f728fd13bc0 37 bool XBee::scan(XBeeStatus& status) {
yamaguch 17:2f728fd13bc0 38 return scan(XBee::Status, status.raw_address(), 1);
yamaguch 17:2f728fd13bc0 39 }
yamaguch 17:2f728fd13bc0 40
yamaguch 17:2f728fd13bc0 41 bool XBee::scan(XBeeDeliveryStatus& status) {
yamaguch 17:2f728fd13bc0 42 return scan(XBee::DeliveryStatus, status.raw_address(), 1);
yamaguch 17:2f728fd13bc0 43 }
yamaguch 17:2f728fd13bc0 44
yamaguch 17:2f728fd13bc0 45 bool XBee::scan(XBeeDiscoveryStatus& status) {
yamaguch 17:2f728fd13bc0 46 return scan(XBee::DiscoveryStatus, status.raw_address(), 1);
yamaguch 17:2f728fd13bc0 47 }
yamaguch 17:2f728fd13bc0 48
yamaguch 17:2f728fd13bc0 49 bool XBee::scan(XBeeReceiveOptions& options) {
yamaguch 17:2f728fd13bc0 50 return scan(XBee::ReceiveOptions, options.raw_address(), 1);
yamaguch 17:2f728fd13bc0 51 }
yamaguch 17:2f728fd13bc0 52
yamaguch 17:2f728fd13bc0 53 bool XBee::scan(XBeeDeviceType& device) {
yamaguch 17:2f728fd13bc0 54 return scan(XBee::DeviceType, device.raw_address(), 1);
yamaguch 17:2f728fd13bc0 55 }
yamaguch 17:2f728fd13bc0 56
yamaguch 17:2f728fd13bc0 57 bool XBee::scan(XBeeSourceEvent& event) {
yamaguch 17:2f728fd13bc0 58 return scan(XBee::SourceEvent, event.raw_address(), 1);
yamaguch 17:2f728fd13bc0 59 }
yamaguch 17:2f728fd13bc0 60
yamaguch 17:2f728fd13bc0 61 bool XBee::scan(XBeeAddress64& address64) {
yamaguch 17:2f728fd13bc0 62 return scan(XBee::Address64, address64.raw_address(), 8);
yamaguch 17:2f728fd13bc0 63 }
yamaguch 17:2f728fd13bc0 64
yamaguch 17:2f728fd13bc0 65 bool XBee::scan(XBeeAddress16& address16) {
yamaguch 17:2f728fd13bc0 66 return scan(XBee::Address16, address16.raw_address(), 2);
yamaguch 17:2f728fd13bc0 67 }
yamaguch 17:2f728fd13bc0 68
yamaguch 17:2f728fd13bc0 69 bool XBee::scan(XBeeATCommand& command) {
yamaguch 17:2f728fd13bc0 70 return scan(XBee::ATCommand, command.raw_address(), 3);
yamaguch 17:2f728fd13bc0 71 }
yamaguch 17:2f728fd13bc0 72
yamaguch 17:2f728fd13bc0 73 bool XBee::scan(XBeeCommandData& data) {
yamaguch 17:2f728fd13bc0 74 return scan(XBee::CommandData, data.raw_address(), data.capacity, &data.size);
yamaguch 17:2f728fd13bc0 75 }
yamaguch 17:2f728fd13bc0 76
yamaguch 17:2f728fd13bc0 77 bool XBee::scan(XBeeReceivedData& data) {
yamaguch 17:2f728fd13bc0 78 return scan(XBee::ReceivedData, data.raw_address(), data.capacity, &data.size);
yamaguch 17:2f728fd13bc0 79 }
yamaguch 17:2f728fd13bc0 80
yamaguch 17:2f728fd13bc0 81 bool XBee::scan(XBeeNodeIdentifier& ni) {
yamaguch 17:2f728fd13bc0 82 return scan(XBee::NIString, ni.raw_address(), ni.capacity, &ni.size);
yamaguch 17:2f728fd13bc0 83 }
yamaguch 17:2f728fd13bc0 84
yamaguch 17:2f728fd13bc0 85 bool XBee::scan(XBeeRawData& data) {
yamaguch 17:2f728fd13bc0 86 return scan(XBee::RawData, data.raw_address(), data.capacity, &data.size);
yamaguch 17:2f728fd13bc0 87 }
yamaguch 17:2f728fd13bc0 88
yamaguch 0:0232a97b3883 89 bool XBee::scan(ValueType type, char *value, int maxlength, int *length) {
yamaguch 0:0232a97b3883 90 if (received != out || (in == out && free != 0))
yamaguch 0:0232a97b3883 91 return false;
yamaguch 0:0232a97b3883 92
yamaguch 0:0232a97b3883 93 return scan(out, type, value, maxlength, length);
yamaguch 0:0232a97b3883 94 }
yamaguch 0:0232a97b3883 95
yamaguch 0:0232a97b3883 96 bool XBee::scan(int index, ValueType type, char *value, int maxlength, int *length) {
yamaguch 0:0232a97b3883 97 int from = 0;
yamaguch 0:0232a97b3883 98 int len = 1;
yamaguch 0:0232a97b3883 99 FrameType frameType = getFrameType(buf[INDEX(index + 2)]);
yamaguch 0:0232a97b3883 100
yamaguch 0:0232a97b3883 101 if (frameType == (FrameType) None)
yamaguch 0:0232a97b3883 102 return false;
yamaguch 0:0232a97b3883 103
yamaguch 0:0232a97b3883 104 switch (type) {
yamaguch 0:0232a97b3883 105 case FrameID:
yamaguch 0:0232a97b3883 106 from = 1;
yamaguch 0:0232a97b3883 107 break;
yamaguch 0:0232a97b3883 108 case ATCommand:
yamaguch 0:0232a97b3883 109 switch (frameType) {
yamaguch 0:0232a97b3883 110 case ATCommandResponse:
yamaguch 0:0232a97b3883 111 from = 2;
yamaguch 0:0232a97b3883 112 break;
yamaguch 0:0232a97b3883 113 case RemoteCommandResponse:
yamaguch 0:0232a97b3883 114 from = 12;
yamaguch 0:0232a97b3883 115 break;
yamaguch 0:0232a97b3883 116 }
yamaguch 0:0232a97b3883 117 len = 2;
yamaguch 0:0232a97b3883 118 break;
yamaguch 0:0232a97b3883 119 case Status:
yamaguch 0:0232a97b3883 120 switch (frameType) {
yamaguch 0:0232a97b3883 121 case ATCommandResponse:
yamaguch 0:0232a97b3883 122 from = 4;
yamaguch 0:0232a97b3883 123 break;
yamaguch 0:0232a97b3883 124 case ModemStatus:
yamaguch 0:0232a97b3883 125 from = 1;
yamaguch 0:0232a97b3883 126 break;
yamaguch 0:0232a97b3883 127 case RemoteCommandResponse:
yamaguch 0:0232a97b3883 128 from = 14;
yamaguch 0:0232a97b3883 129 break;
yamaguch 0:0232a97b3883 130 }
yamaguch 0:0232a97b3883 131 break;
yamaguch 0:0232a97b3883 132 case CommandData:
yamaguch 0:0232a97b3883 133 switch (frameType) {
yamaguch 0:0232a97b3883 134 case ATCommandResponse:
yamaguch 0:0232a97b3883 135 from = 5;
yamaguch 0:0232a97b3883 136 len = SIZE(buf, index) - 5;
yamaguch 0:0232a97b3883 137 break;
yamaguch 0:0232a97b3883 138 case RemoteCommandResponse:
yamaguch 0:0232a97b3883 139 from = 15;
yamaguch 0:0232a97b3883 140 len = SIZE(buf, index) - 15;
yamaguch 0:0232a97b3883 141 break;
yamaguch 0:0232a97b3883 142 }
yamaguch 0:0232a97b3883 143 break;
yamaguch 0:0232a97b3883 144 case Address16:
yamaguch 0:0232a97b3883 145 switch (frameType) {
yamaguch 0:0232a97b3883 146 case ZigBeeTransmitStatus:
yamaguch 16:cdfcb63b2c4b 147 from = 2;
yamaguch 0:0232a97b3883 148 break;
yamaguch 0:0232a97b3883 149 case ZigBeeReceivePacket:
yamaguch 0:0232a97b3883 150 case ZigBeeIODataSampleRxIndicator:
yamaguch 5:b82970ef7fb0 151 case NodeIdentificationIndicator:
yamaguch 0:0232a97b3883 152 from = 9;
yamaguch 0:0232a97b3883 153 break;
yamaguch 0:0232a97b3883 154 case RemoteCommandResponse:
yamaguch 0:0232a97b3883 155 from = 10;
yamaguch 0:0232a97b3883 156 break;
yamaguch 0:0232a97b3883 157 }
yamaguch 0:0232a97b3883 158 len = 2;
yamaguch 0:0232a97b3883 159 break;
yamaguch 0:0232a97b3883 160 case Address64:
yamaguch 0:0232a97b3883 161 switch (frameType) {
yamaguch 0:0232a97b3883 162 case ZigBeeReceivePacket:
yamaguch 0:0232a97b3883 163 case ZigBeeIODataSampleRxIndicator:
yamaguch 5:b82970ef7fb0 164 case NodeIdentificationIndicator:
yamaguch 0:0232a97b3883 165 from = 1;
yamaguch 0:0232a97b3883 166 break;
yamaguch 0:0232a97b3883 167 case RemoteCommandResponse:
yamaguch 0:0232a97b3883 168 from = 2;
yamaguch 0:0232a97b3883 169 break;
yamaguch 0:0232a97b3883 170 }
yamaguch 0:0232a97b3883 171 len = 8;
yamaguch 0:0232a97b3883 172 break;
yamaguch 5:b82970ef7fb0 173 case RemoteAddress16:
yamaguch 5:b82970ef7fb0 174 from = 12;
yamaguch 5:b82970ef7fb0 175 len = 2;
yamaguch 5:b82970ef7fb0 176 break;
yamaguch 5:b82970ef7fb0 177 case RemoteAddress64:
yamaguch 5:b82970ef7fb0 178 from = 14;
yamaguch 5:b82970ef7fb0 179 len = 8;
yamaguch 5:b82970ef7fb0 180 break;
yamaguch 5:b82970ef7fb0 181 case ParentAddress16:
yamaguch 5:b82970ef7fb0 182 from = SIZE(buf, index) - 8;
yamaguch 5:b82970ef7fb0 183 len = 2;
yamaguch 5:b82970ef7fb0 184 break;
yamaguch 0:0232a97b3883 185 case RetryCount:
yamaguch 0:0232a97b3883 186 from = 4;
yamaguch 0:0232a97b3883 187 break;
yamaguch 0:0232a97b3883 188 case DeliveryStatus:
yamaguch 0:0232a97b3883 189 from = 5;
yamaguch 0:0232a97b3883 190 break;
yamaguch 0:0232a97b3883 191 case DiscoveryStatus:
yamaguch 0:0232a97b3883 192 from = 6;
yamaguch 0:0232a97b3883 193 break;
yamaguch 0:0232a97b3883 194 case ReceiveOptions:
yamaguch 0:0232a97b3883 195 from = 11;
yamaguch 0:0232a97b3883 196 break;
yamaguch 0:0232a97b3883 197 case ReceivedData:
yamaguch 0:0232a97b3883 198 from = 12;
yamaguch 0:0232a97b3883 199 len = SIZE(buf, index) - 12;
yamaguch 0:0232a97b3883 200 break;
yamaguch 5:b82970ef7fb0 201 case NIString:
yamaguch 5:b82970ef7fb0 202 from = 22;
yamaguch 5:b82970ef7fb0 203 len = SIZE(buf, index) - (22 + 8);
yamaguch 5:b82970ef7fb0 204 break;
yamaguch 5:b82970ef7fb0 205 case DeviceType:
yamaguch 5:b82970ef7fb0 206 from = SIZE(buf, index) - 6;
yamaguch 5:b82970ef7fb0 207 break;
yamaguch 5:b82970ef7fb0 208 case SourceEvent:
yamaguch 5:b82970ef7fb0 209 from = SIZE(buf, index) - 5;
yamaguch 5:b82970ef7fb0 210 break;
yamaguch 0:0232a97b3883 211 case RawData:
yamaguch 0:0232a97b3883 212 from = 1;
yamaguch 0:0232a97b3883 213 len = SIZE(buf, index) - 1;
yamaguch 0:0232a97b3883 214 break;
yamaguch 0:0232a97b3883 215 }
yamaguch 0:0232a97b3883 216
yamaguch 0:0232a97b3883 217 if (from > 0) {
yamaguch 0:0232a97b3883 218 copy(value, INDEX(index + 2 + from), min(len, maxlength));
yamaguch 0:0232a97b3883 219 if (length) *length = len;
yamaguch 0:0232a97b3883 220 return true;
yamaguch 0:0232a97b3883 221 } else
yamaguch 0:0232a97b3883 222 return false;
yamaguch 0:0232a97b3883 223 }