XBee API mode library

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

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yamaguch 0:0232a97b3883 1 /*
yamaguch 8:776b8dc51932 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 #include <ctype.h>
yamaguch 0:0232a97b3883 25
yamaguch 17:2f728fd13bc0 26 #define INDEX(n) ((n) % BUFSIZE)
yamaguch 17:2f728fd13bc0 27 #define SIZE(b, i) (b[i] << 8 | b[INDEX(i + 1)])
yamaguch 17:2f728fd13bc0 28 #define min(x, y) ((x) <= (y) ? (x) : (y))
yamaguch 17:2f728fd13bc0 29
yamaguch 0:0232a97b3883 30 void XBee::dump() {
yamaguch 0:0232a97b3883 31 if (received != out || (in == out && free != 0))
yamaguch 0:0232a97b3883 32 return;
yamaguch 0:0232a97b3883 33
yamaguch 0:0232a97b3883 34 switch (getFrameType(buf[INDEX(out + 2)])) {
yamaguch 0:0232a97b3883 35 case ATCommandResponse: {
yamaguch 0:0232a97b3883 36 char id, name[2], status, data[16];
yamaguch 0:0232a97b3883 37 int length = 0;
yamaguch 0:0232a97b3883 38 scan(FrameID, &id);
yamaguch 0:0232a97b3883 39 scan(ATCommand, name, sizeof(name));
yamaguch 0:0232a97b3883 40 scan(Status, &status);
yamaguch 0:0232a97b3883 41 scan(CommandData, data, sizeof(data), &length);
yamaguch 0:0232a97b3883 42 mon.printf("ATCommandResponse, Id=%d, Nm=%c%c, St=%02X, ", id & 255, name[0], name[1], status);
yamaguch 0:0232a97b3883 43 if (name[0] == 'I' && name[1] == 'S' && status == 0)
yamaguch 0:0232a97b3883 44 dumpIOSample(data, length);
yamaguch 0:0232a97b3883 45 else
yamaguch 0:0232a97b3883 46 dump(data, length);
yamaguch 0:0232a97b3883 47 }
yamaguch 0:0232a97b3883 48 break;
yamaguch 0:0232a97b3883 49
yamaguch 0:0232a97b3883 50 case ModemStatus: {
yamaguch 0:0232a97b3883 51 char status;
yamaguch 0:0232a97b3883 52 scan(Status, &status);
yamaguch 0:0232a97b3883 53 mon.printf("ModemStatus, St=%02X\n", status);
yamaguch 0:0232a97b3883 54 }
yamaguch 0:0232a97b3883 55 break;
yamaguch 0:0232a97b3883 56
yamaguch 0:0232a97b3883 57 case ZigBeeTransmitStatus: {
yamaguch 0:0232a97b3883 58 char id, address16[2], retry, delivery, discovery;
yamaguch 0:0232a97b3883 59 scan(FrameID, &id);
yamaguch 0:0232a97b3883 60 scan(Address16, address16, sizeof(address16));
yamaguch 0:0232a97b3883 61 scan(RetryCount, &retry);
yamaguch 0:0232a97b3883 62 scan(DeliveryStatus, &delivery);
yamaguch 0:0232a97b3883 63 scan(DiscoveryStatus, &discovery);
yamaguch 0:0232a97b3883 64 mon.printf("ZigBeeTransmitStatus, Id=%d, ad=%02X%02X, Rty=%d, Dlv=%02X, Dsc=%02X\n",
yamaguch 0:0232a97b3883 65 id & 255, address16[0], address16[1], retry & 255, delivery, discovery);
yamaguch 0:0232a97b3883 66 }
yamaguch 0:0232a97b3883 67 break;
yamaguch 0:0232a97b3883 68
yamaguch 0:0232a97b3883 69 case ZigBeeReceivePacket: {
yamaguch 0:0232a97b3883 70 char address64[8], address16[2], options, data[32];
yamaguch 0:0232a97b3883 71 int length = 0;
yamaguch 0:0232a97b3883 72 scan(Address64, address64, sizeof(address64));
yamaguch 0:0232a97b3883 73 scan(Address16, address16, sizeof(address16));
yamaguch 0:0232a97b3883 74 scan(ReceiveOptions, &options);
yamaguch 0:0232a97b3883 75 scan(ReceivedData, data, sizeof(data), &length);
yamaguch 0:0232a97b3883 76 mon.printf("ZigBeeReceivePacket, AD=%02X%02X%02X%02X %02X%02X%02X%02X, ad=%02X%02X, Op=%02X, Length=%d, ",
yamaguch 0:0232a97b3883 77 address64[0], address64[1], address64[2], address64[3], address64[4], address64[5],
yamaguch 0:0232a97b3883 78 address64[6], address64[7], address16[0], address16[1], options, length);
yamaguch 0:0232a97b3883 79 dump(data, min(sizeof(data), length));
yamaguch 0:0232a97b3883 80 }
yamaguch 0:0232a97b3883 81 break;
yamaguch 0:0232a97b3883 82
yamaguch 0:0232a97b3883 83 case ZigBeeIODataSampleRxIndicator: {
yamaguch 0:0232a97b3883 84 char address64[8], address16[2], options, data[16];
yamaguch 0:0232a97b3883 85 int length = 0;
yamaguch 0:0232a97b3883 86 scan(Address64, address64, sizeof(address64));
yamaguch 0:0232a97b3883 87 scan(Address16, address16, sizeof(address16));
yamaguch 0:0232a97b3883 88 scan(ReceiveOptions, &options);
yamaguch 0:0232a97b3883 89 scan(ReceivedData, data, sizeof(data), &length);
yamaguch 0:0232a97b3883 90 mon.printf("ZigBeeIODataSampleRxIndicator, AD=%02X%02X%02X%02X %02X%02X%02X%02X, ad=%02X%02X, Op=%02X, ",
yamaguch 0:0232a97b3883 91 address64[0], address64[1], address64[2], address64[3], address64[4], address64[5],
yamaguch 0:0232a97b3883 92 address64[6], address64[7], address16[0], address16[1], options);
yamaguch 0:0232a97b3883 93 dumpIOSample(data, length);
yamaguch 0:0232a97b3883 94 }
yamaguch 0:0232a97b3883 95 break;
yamaguch 0:0232a97b3883 96
yamaguch 5:b82970ef7fb0 97 case NodeIdentificationIndicator: {
yamaguch 5:b82970ef7fb0 98 char address64[8], address16[2], options, remote64[8], remote16[2], parent16[2], ni[20], device, event;
yamaguch 5:b82970ef7fb0 99 scan(Address64, address64, sizeof(address64));
yamaguch 5:b82970ef7fb0 100 scan(Address16, address16, sizeof(address16));
yamaguch 5:b82970ef7fb0 101 scan(ReceiveOptions, &options);
yamaguch 5:b82970ef7fb0 102 scan(RemoteAddress64, remote64, sizeof(remote64));
yamaguch 5:b82970ef7fb0 103 scan(RemoteAddress16, remote16, sizeof(remote16));
yamaguch 5:b82970ef7fb0 104 scan(ParentAddress16, parent16, sizeof(parent16));
yamaguch 5:b82970ef7fb0 105 scan(NIString, ni, sizeof(ni));
yamaguch 5:b82970ef7fb0 106 scan(DeviceType, &device);
yamaguch 5:b82970ef7fb0 107 scan(SourceEvent, &event);
yamaguch 5:b82970ef7fb0 108 mon.printf("NodeIdentificationIndicator, AD=%02X%02X%02X%02X %02X%02X%02X%02X, ad=%02X%02X, Op=%02X,\n"
yamaguch 7:19f03567ec4e 109 " RM=%02X%02X%02X%02X %02X%02X%02X%02X, rm=%02X%02X, Pa=%02X%02X, Dev=%02X, Ev=%02X, NI=%s\n",
yamaguch 5:b82970ef7fb0 110 address64[0], address64[1], address64[2], address64[3], address64[4], address64[5],
yamaguch 5:b82970ef7fb0 111 address64[6], address64[7], address16[0], address16[1], options,
yamaguch 5:b82970ef7fb0 112 remote64[0], remote64[1], remote64[2], remote64[3], remote64[4], remote64[5],
yamaguch 5:b82970ef7fb0 113 remote64[6], remote64[7], remote16[0], remote16[1], parent16[0], parent16[1], device, event, ni);
yamaguch 5:b82970ef7fb0 114 }
yamaguch 5:b82970ef7fb0 115 break;
yamaguch 5:b82970ef7fb0 116
yamaguch 0:0232a97b3883 117 case RemoteCommandResponse: {
yamaguch 0:0232a97b3883 118 char address64[8], address16[2], id, name[2], status, data[16];
yamaguch 0:0232a97b3883 119 int length = 0;
yamaguch 0:0232a97b3883 120 scan(Address64, address64, sizeof(address64));
yamaguch 0:0232a97b3883 121 scan(Address16, address16, sizeof(address16));
yamaguch 0:0232a97b3883 122 scan(FrameID, &id);
yamaguch 0:0232a97b3883 123 scan(ATCommand, name, sizeof(name));
yamaguch 0:0232a97b3883 124 scan(Status, &status);
yamaguch 0:0232a97b3883 125 scan(CommandData, data, sizeof(data), &length);
yamaguch 0:0232a97b3883 126 mon.printf("RemoteCommandResponse, AD=%02X%02X%02X%02X %02X%02X%02X%02X, ad=%02X%02X, Id=%d, Nm=%c%c, St=%02X, ",
yamaguch 0:0232a97b3883 127 address64[0], address64[1], address64[2], address64[3], address64[4], address64[5],
yamaguch 0:0232a97b3883 128 address64[6], address64[7], address16[0], address16[1], id & 255, name[0], name[1], status);
yamaguch 0:0232a97b3883 129 if (name[0] == 'I' && name[1] == 'S' && status == 0)
yamaguch 0:0232a97b3883 130 dumpIOSample(data, length);
yamaguch 0:0232a97b3883 131 else
yamaguch 0:0232a97b3883 132 dump(data, length);
yamaguch 0:0232a97b3883 133 }
yamaguch 0:0232a97b3883 134 break;
yamaguch 0:0232a97b3883 135
yamaguch 0:0232a97b3883 136 default: {
yamaguch 0:0232a97b3883 137 char data[32];
yamaguch 0:0232a97b3883 138 int length = 0;
yamaguch 0:0232a97b3883 139 scan(RawData, data, sizeof(data), &length);
yamaguch 0:0232a97b3883 140 mon.printf("Frametype:%02X, Length=%d, Data=", buf[INDEX(out + 2)] & 255, length);
yamaguch 0:0232a97b3883 141 dump(data, min(sizeof(data), length));
yamaguch 0:0232a97b3883 142 }
yamaguch 0:0232a97b3883 143 }
yamaguch 0:0232a97b3883 144 }
yamaguch 0:0232a97b3883 145
yamaguch 0:0232a97b3883 146 void XBee::dumpAll() {
yamaguch 0:0232a97b3883 147 mon.printf("cur = %04X\n", cur);
yamaguch 0:0232a97b3883 148 mon.printf("in = %04X\n", in);
yamaguch 0:0232a97b3883 149 mon.printf("out = %04X\n", out);
yamaguch 0:0232a97b3883 150 mon.printf("received = %04X\n", received);
yamaguch 0:0232a97b3883 151 mon.printf("free = %d\n", free);
yamaguch 0:0232a97b3883 152 for (int i = 0; i < BUFSIZE; i += 16) {
yamaguch 0:0232a97b3883 153 mon.printf("%04X: ", i);
yamaguch 0:0232a97b3883 154 dump(buf + i, 16);
yamaguch 0:0232a97b3883 155 }
yamaguch 0:0232a97b3883 156 }
yamaguch 0:0232a97b3883 157
yamaguch 0:0232a97b3883 158 void XBee::dump(const char* data, int length) {
yamaguch 0:0232a97b3883 159 for (int i = 0; i < length; i++) mon.printf("%02X", data[i]);
yamaguch 0:0232a97b3883 160 mon.printf(" | ");
yamaguch 0:0232a97b3883 161 for (int i = 0; i < length; i++) mon.printf("%c", isprint(data[i]) ? data[i] : '.');
yamaguch 0:0232a97b3883 162 mon.printf("\n");
yamaguch 0:0232a97b3883 163 }
yamaguch 0:0232a97b3883 164
yamaguch 0:0232a97b3883 165 void XBee::dumpIOSample(const char* data, int length) {
yamaguch 0:0232a97b3883 166 if (length > 0) {
yamaguch 0:0232a97b3883 167 IOSample sample(data);
yamaguch 0:0232a97b3883 168 mon.printf(sample);
yamaguch 1:482216f3f6ee 169 mon.printf("\n");
yamaguch 0:0232a97b3883 170 }
yamaguch 0:0232a97b3883 171 }