Hiroshi Yamaguchi / XBee 1.0
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Scan.cpp Source File

Scan.cpp

00001 /*
00002 Copyright (c) 2011, Senio Networks, Inc.
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a copy
00005 of this software and associated documentation files (the "Software"), to deal
00006 in the Software without restriction, including without limitation the rights
00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008 copies of the Software, and to permit persons to whom the Software is
00009 furnished to do so, subject to the following conditions:
00010 
00011 The above copyright notice and this permission notice shall be included in
00012 all copies or substantial portions of the Software.
00013 
00014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020 THE SOFTWARE.
00021 */
00022 
00023 #include "XBee.h"
00024 
00025 bool XBee::scan(ValueType type, char *value, int maxlength, int *length) {
00026     if (received != out || (in == out && free != 0))
00027         return false;
00028 
00029     return scan(out, type, value, maxlength, length);
00030 }
00031 
00032 bool XBee::scan(int index, ValueType type, char *value, int maxlength, int *length) {
00033     int from = 0;
00034     int len = 1;
00035     FrameType frameType = getFrameType(buf[INDEX(index + 2)]);
00036 
00037     if (frameType == (FrameType) None)
00038         return false;
00039 
00040     switch (type) {
00041         case FrameID:
00042             from = 1;
00043             break;
00044         case ATCommand:
00045             switch (frameType) {
00046                 case ATCommandResponse:
00047                     from = 2;
00048                     break;
00049                 case RemoteCommandResponse:
00050                     from = 12;
00051                     break;
00052             }
00053             len = 2;
00054             break;
00055         case Status:
00056             switch (frameType) {
00057                 case ATCommandResponse:
00058                     from = 4;
00059                     break;
00060                 case ModemStatus:
00061                     from = 1;
00062                     break;
00063                 case RemoteCommandResponse:
00064                     from = 14;
00065                     break;
00066             }
00067             break;
00068         case CommandData:
00069             switch (frameType) {
00070                 case ATCommandResponse:
00071                     from = 5;
00072                     len = SIZE(buf, index) - 5;
00073                     break;
00074                 case RemoteCommandResponse:
00075                     from = 15;
00076                     len = SIZE(buf, index) - 15;
00077                     break;
00078             }
00079             break;
00080         case Address16:
00081             switch (frameType) {
00082                 case ZigBeeTransmitStatus:
00083                     from = 4;
00084                     break;
00085                 case ZigBeeReceivePacket:
00086                 case ZigBeeIODataSampleRxIndicator:
00087                     from = 9;
00088                     break;
00089                 case RemoteCommandResponse:
00090                     from = 10;
00091                     break;
00092             }
00093             len = 2;
00094             break;
00095         case Address64:
00096             switch (frameType) {
00097                 case ZigBeeReceivePacket:
00098                 case ZigBeeIODataSampleRxIndicator:
00099                     from = 1;
00100                     break;
00101                 case RemoteCommandResponse:
00102                     from = 2;
00103                     break;
00104             }
00105             len = 8;
00106             break;
00107         case RetryCount:
00108             from = 4;
00109             break;
00110         case DeliveryStatus:
00111             from = 5;
00112             break;
00113         case DiscoveryStatus:
00114             from = 6;
00115             break;
00116         case ReceiveOptions:
00117             from = 11;
00118             break;
00119         case ReceivedData:
00120             from = 12;
00121             len = SIZE(buf, index) - 12;
00122             break;
00123         case RawData:
00124             from = 1;
00125             len = SIZE(buf, index) - 1;
00126             break;
00127     }
00128 
00129     if (from > 0) {
00130         copy(value, INDEX(index + 2 + from), min(len, maxlength));
00131         if (length) *length = len;
00132         return true;
00133     } else
00134         return false;
00135 }