Orefatoi / afLib_1_3
Committer:
Rhyme
Date:
Mon Apr 23 06:15:05 2018 +0000
Revision:
1:112741fe45d1
Parent:
0:6f371c791202
afLib1.3 first mbed version with working UART

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rhyme 0:6f371c791202 1 /**
Rhyme 0:6f371c791202 2 * Copyright 2015 Afero, Inc.
Rhyme 0:6f371c791202 3 *
Rhyme 0:6f371c791202 4 * Licensed under the Apache License, Version 2.0 (the "License");
Rhyme 0:6f371c791202 5 * you may not use this file except in compliance with the License.
Rhyme 0:6f371c791202 6 * You may obtain a copy of the License at
Rhyme 0:6f371c791202 7 *
Rhyme 0:6f371c791202 8 * http://www.apache.org/licenses/LICENSE-2.0
Rhyme 0:6f371c791202 9 *
Rhyme 0:6f371c791202 10 * Unless required by applicable law or agreed to in writing, software
Rhyme 0:6f371c791202 11 * distributed under the License is distributed on an "AS IS" BASIS,
Rhyme 0:6f371c791202 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Rhyme 0:6f371c791202 13 * See the License for the specific language governing permissions and
Rhyme 0:6f371c791202 14 * limitations under the License.
Rhyme 0:6f371c791202 15 */
Rhyme 0:6f371c791202 16
Rhyme 0:6f371c791202 17 #include "mbed.h"
Rhyme 0:6f371c791202 18 #include <stdio.h>
Rhyme 0:6f371c791202 19 #include "Command.h"
Rhyme 0:6f371c791202 20 #include "msg_types.h"
Rhyme 0:6f371c791202 21
Rhyme 0:6f371c791202 22 #define CMD_HDR_LEN 4 // 4 byte header on all commands
Rhyme 0:6f371c791202 23 #define CMD_VAL_LEN 2 // 2 byte value length for commands that have a value
Rhyme 0:6f371c791202 24
Rhyme 0:6f371c791202 25 const char *CMD_NAMES[] = {"SET ", "GET ", "UPDATE"};
Rhyme 0:6f371c791202 26
Rhyme 0:6f371c791202 27
Rhyme 0:6f371c791202 28 Command::Command(Stream *serial,uint16_t len, uint8_t *bytes) {
Rhyme 0:6f371c791202 29 _serial = serial;
Rhyme 0:6f371c791202 30 int index = 0;
Rhyme 0:6f371c791202 31
Rhyme 0:6f371c791202 32 _cmd = bytes[index++];
Rhyme 0:6f371c791202 33 _requestId = bytes[index++];
Rhyme 0:6f371c791202 34 _attrId = bytes[index + 0] | bytes[index + 1] << 8;
Rhyme 0:6f371c791202 35 index += 2;
Rhyme 0:6f371c791202 36
Rhyme 0:6f371c791202 37 if (_cmd == MSG_TYPE_GET) {
Rhyme 0:6f371c791202 38 return;
Rhyme 0:6f371c791202 39 }
Rhyme 0:6f371c791202 40 if (_cmd == MSG_TYPE_UPDATE) {
Rhyme 0:6f371c791202 41 _status = bytes[index++];
Rhyme 0:6f371c791202 42 _reason = bytes[index++];
Rhyme 0:6f371c791202 43 }
Rhyme 0:6f371c791202 44
Rhyme 0:6f371c791202 45 _valueLen = bytes[index + 0] | bytes[index + 1] << 8;
Rhyme 0:6f371c791202 46 index += 2;
Rhyme 0:6f371c791202 47 _value = new uint8_t[_valueLen];
Rhyme 0:6f371c791202 48 for (int i = 0; i < _valueLen; i++) {
Rhyme 0:6f371c791202 49 _value[i] = bytes[index + i];
Rhyme 0:6f371c791202 50 }
Rhyme 0:6f371c791202 51 }
Rhyme 0:6f371c791202 52
Rhyme 0:6f371c791202 53 #if 1
Rhyme 0:6f371c791202 54 char *strdup(const char *str)
Rhyme 0:6f371c791202 55 {
Rhyme 0:6f371c791202 56 int len ;
Rhyme 0:6f371c791202 57 char *ptr = 0 ;
Rhyme 0:6f371c791202 58 if (str) {
Rhyme 0:6f371c791202 59 len = strlen(str) ;
Rhyme 0:6f371c791202 60 ptr = (char*)malloc(len + 1) ;
Rhyme 0:6f371c791202 61 strcpy(ptr, str) ;
Rhyme 0:6f371c791202 62 }
Rhyme 0:6f371c791202 63 return(ptr) ;
Rhyme 0:6f371c791202 64 }
Rhyme 0:6f371c791202 65 #endif
Rhyme 0:6f371c791202 66
Rhyme 0:6f371c791202 67 Command::Command(Stream *serial,uint8_t requestId, const char *str) {
Rhyme 0:6f371c791202 68 _serial = serial;
Rhyme 0:6f371c791202 69 _requestId = requestId & 0xff;
Rhyme 0:6f371c791202 70
Rhyme 0:6f371c791202 71 char *cp = strdup(str);
Rhyme 0:6f371c791202 72 char *tok = strtok(cp, " ");
Rhyme 0:6f371c791202 73 _cmd = strToCmd(tok);
Rhyme 0:6f371c791202 74
Rhyme 0:6f371c791202 75 tok = strtok(NULL, " ");
Rhyme 0:6f371c791202 76 _attrId = strToAttrId(tok);
Rhyme 0:6f371c791202 77
Rhyme 0:6f371c791202 78 if (_cmd == MSG_TYPE_GET) {
Rhyme 0:6f371c791202 79 _valueLen = 0;
Rhyme 0:6f371c791202 80 _value = NULL;
Rhyme 0:6f371c791202 81 } else {
Rhyme 0:6f371c791202 82 tok = strtok(NULL, " ");
Rhyme 0:6f371c791202 83 _valueLen = strlen(tok) / 2;
Rhyme 0:6f371c791202 84 _value = new uint8_t[_valueLen];
Rhyme 0:6f371c791202 85 strToValue(tok, _value);
Rhyme 0:6f371c791202 86 }
Rhyme 0:6f371c791202 87
Rhyme 0:6f371c791202 88 free(cp);
Rhyme 0:6f371c791202 89 }
Rhyme 0:6f371c791202 90
Rhyme 0:6f371c791202 91 Command::Command(Stream *serial,uint8_t requestId, uint8_t cmd, uint16_t attrId) {
Rhyme 0:6f371c791202 92 _serial = serial;
Rhyme 0:6f371c791202 93 _requestId = requestId;
Rhyme 0:6f371c791202 94 _cmd = cmd;
Rhyme 0:6f371c791202 95 _attrId = attrId;
Rhyme 0:6f371c791202 96 _valueLen = 0;
Rhyme 0:6f371c791202 97 _value = NULL;
Rhyme 0:6f371c791202 98 }
Rhyme 0:6f371c791202 99
Rhyme 0:6f371c791202 100 Command::Command(Stream *serial,uint8_t requestId, uint8_t cmd, uint16_t attrId, uint16_t valueLen, uint8_t *value) {
Rhyme 0:6f371c791202 101 _serial = serial;
Rhyme 0:6f371c791202 102 _requestId = requestId;
Rhyme 0:6f371c791202 103 _cmd = cmd;
Rhyme 0:6f371c791202 104 _attrId = attrId;
Rhyme 0:6f371c791202 105 _valueLen = valueLen;
Rhyme 0:6f371c791202 106 _value = new uint8_t[_valueLen];
Rhyme 0:6f371c791202 107 memcpy(_value, value, valueLen);
Rhyme 0:6f371c791202 108 }
Rhyme 0:6f371c791202 109
Rhyme 0:6f371c791202 110 Command::Command(Stream *serial,uint8_t requestId, uint8_t cmd, uint16_t attrId, uint8_t status, uint8_t reason, uint16_t valueLen,
Rhyme 0:6f371c791202 111 uint8_t *value) {
Rhyme 0:6f371c791202 112 _serial = serial;
Rhyme 0:6f371c791202 113 _requestId = requestId;
Rhyme 0:6f371c791202 114 _cmd = cmd;
Rhyme 0:6f371c791202 115 _attrId = attrId;
Rhyme 0:6f371c791202 116 _status = status;
Rhyme 0:6f371c791202 117 _reason = reason;
Rhyme 0:6f371c791202 118 _valueLen = valueLen;
Rhyme 0:6f371c791202 119 _value = new uint8_t[_valueLen];
Rhyme 0:6f371c791202 120 memcpy(_value, value, valueLen);
Rhyme 0:6f371c791202 121 }
Rhyme 0:6f371c791202 122
Rhyme 0:6f371c791202 123 Command::Command(Stream *serial) {
Rhyme 0:6f371c791202 124 _serial = serial;
Rhyme 0:6f371c791202 125
Rhyme 0:6f371c791202 126 }
Rhyme 0:6f371c791202 127
Rhyme 0:6f371c791202 128 Command::~Command() {
Rhyme 0:6f371c791202 129 if (_value != NULL) {
Rhyme 0:6f371c791202 130 delete (_value);
Rhyme 0:6f371c791202 131 }
Rhyme 0:6f371c791202 132 }
Rhyme 0:6f371c791202 133
Rhyme 0:6f371c791202 134 int Command::strToValue(char *valueStr, uint8_t *value) {
Rhyme 0:6f371c791202 135 for (int i = 0; i < (int) (strlen(valueStr) / 2); i++) {
Rhyme 0:6f371c791202 136 int j = i * 2;
Rhyme 0:6f371c791202 137 value[i] = getVal(valueStr[j + 1]) + (getVal(valueStr[j]) << 4);
Rhyme 0:6f371c791202 138 }
Rhyme 0:6f371c791202 139
Rhyme 0:6f371c791202 140 return 0;
Rhyme 0:6f371c791202 141 }
Rhyme 0:6f371c791202 142
Rhyme 0:6f371c791202 143 uint16_t Command::strToAttrId(char *attrIdStr) {
Rhyme 0:6f371c791202 144 return atoi(attrIdStr);
Rhyme 0:6f371c791202 145 //return String(attrIdStr).toInt();
Rhyme 0:6f371c791202 146 }
Rhyme 0:6f371c791202 147
Rhyme 0:6f371c791202 148 uint8_t Command::strToCmd(char *cmdStr) {
Rhyme 0:6f371c791202 149 char c = cmdStr[0];
Rhyme 0:6f371c791202 150 if (c == 'g' || c == 'G') {
Rhyme 0:6f371c791202 151 return MSG_TYPE_GET;
Rhyme 0:6f371c791202 152 } else if (c == 's' || c == 'S') {
Rhyme 0:6f371c791202 153 return MSG_TYPE_SET;
Rhyme 0:6f371c791202 154 } else if (c == 'u' || c == 'U') {
Rhyme 0:6f371c791202 155 return MSG_TYPE_UPDATE;
Rhyme 0:6f371c791202 156 }
Rhyme 0:6f371c791202 157
Rhyme 0:6f371c791202 158 return -1;
Rhyme 0:6f371c791202 159 }
Rhyme 0:6f371c791202 160
Rhyme 0:6f371c791202 161 uint8_t Command::getCommand() {
Rhyme 0:6f371c791202 162 return _cmd;
Rhyme 0:6f371c791202 163 }
Rhyme 0:6f371c791202 164
Rhyme 0:6f371c791202 165 uint8_t Command::getReqId() {
Rhyme 0:6f371c791202 166 return _requestId;
Rhyme 0:6f371c791202 167 }
Rhyme 0:6f371c791202 168
Rhyme 0:6f371c791202 169 uint16_t Command::getAttrId() {
Rhyme 0:6f371c791202 170 return _attrId;
Rhyme 0:6f371c791202 171 }
Rhyme 0:6f371c791202 172
Rhyme 0:6f371c791202 173 uint16_t Command::getValueLen() {
Rhyme 0:6f371c791202 174 return _valueLen;
Rhyme 0:6f371c791202 175 }
Rhyme 0:6f371c791202 176
Rhyme 0:6f371c791202 177 void Command::getValue(uint8_t *value) {
Rhyme 0:6f371c791202 178 for (int i = 0; i < _valueLen; i++) {
Rhyme 0:6f371c791202 179 value[i] = _value[i];
Rhyme 0:6f371c791202 180 }
Rhyme 0:6f371c791202 181 }
Rhyme 0:6f371c791202 182
Rhyme 0:6f371c791202 183 uint8_t *Command::getValueP() {
Rhyme 0:6f371c791202 184 return _value;
Rhyme 0:6f371c791202 185 }
Rhyme 0:6f371c791202 186
Rhyme 0:6f371c791202 187 uint16_t Command::getSize() {
Rhyme 0:6f371c791202 188 uint16_t len = CMD_HDR_LEN;
Rhyme 0:6f371c791202 189
Rhyme 0:6f371c791202 190 if (_cmd != MSG_TYPE_GET) {
Rhyme 0:6f371c791202 191 len += CMD_VAL_LEN + _valueLen;
Rhyme 0:6f371c791202 192 }
Rhyme 0:6f371c791202 193
Rhyme 0:6f371c791202 194 if (_cmd == MSG_TYPE_UPDATE) {
Rhyme 0:6f371c791202 195 len += 2; // status byte + reason byte
Rhyme 0:6f371c791202 196 }
Rhyme 0:6f371c791202 197
Rhyme 0:6f371c791202 198 return len;
Rhyme 0:6f371c791202 199 }
Rhyme 0:6f371c791202 200
Rhyme 0:6f371c791202 201 uint16_t Command::getBytes(uint8_t *bytes) {
Rhyme 0:6f371c791202 202 uint16_t len = getSize();
Rhyme 0:6f371c791202 203
Rhyme 0:6f371c791202 204 int index = 0;
Rhyme 0:6f371c791202 205
Rhyme 0:6f371c791202 206 bytes[index++] = (_cmd);
Rhyme 0:6f371c791202 207
Rhyme 0:6f371c791202 208 bytes[index++] = (_requestId);
Rhyme 0:6f371c791202 209
Rhyme 0:6f371c791202 210 bytes[index++] = (_attrId & 0xff);
Rhyme 0:6f371c791202 211 bytes[index++] = ((_attrId >> 8) & 0xff);
Rhyme 0:6f371c791202 212
Rhyme 0:6f371c791202 213 if (_cmd == MSG_TYPE_GET) {
Rhyme 0:6f371c791202 214 return len;
Rhyme 0:6f371c791202 215 }
Rhyme 0:6f371c791202 216
Rhyme 0:6f371c791202 217 if (_cmd == MSG_TYPE_UPDATE) {
Rhyme 0:6f371c791202 218 bytes[index++] = (_status);
Rhyme 0:6f371c791202 219 bytes[index++] = (_reason);
Rhyme 0:6f371c791202 220 }
Rhyme 0:6f371c791202 221
Rhyme 0:6f371c791202 222 bytes[index++] = (_valueLen & 0xff);
Rhyme 0:6f371c791202 223 bytes[index++] = ((_valueLen >> 8) & 0xff);
Rhyme 0:6f371c791202 224
Rhyme 0:6f371c791202 225 for (int i = 0; i < _valueLen; i++) {
Rhyme 0:6f371c791202 226 bytes[index++] = (_value[i]);
Rhyme 0:6f371c791202 227 }
Rhyme 0:6f371c791202 228
Rhyme 0:6f371c791202 229 return len;
Rhyme 0:6f371c791202 230 }
Rhyme 0:6f371c791202 231
Rhyme 0:6f371c791202 232 uint8_t Command::getReason() {
Rhyme 0:6f371c791202 233 return _reason;
Rhyme 0:6f371c791202 234 }
Rhyme 0:6f371c791202 235
Rhyme 0:6f371c791202 236 bool Command::isValid() {
Rhyme 0:6f371c791202 237 return (_cmd == MSG_TYPE_SET) || (_cmd == MSG_TYPE_GET) || (_cmd == MSG_TYPE_UPDATE);
Rhyme 0:6f371c791202 238 }
Rhyme 0:6f371c791202 239
Rhyme 0:6f371c791202 240 void Command::dumpBytes() {
Rhyme 0:6f371c791202 241 uint16_t len = getSize();
Rhyme 0:6f371c791202 242 uint8_t bytes[len];
Rhyme 0:6f371c791202 243 getBytes(bytes);
Rhyme 0:6f371c791202 244
Rhyme 0:6f371c791202 245 _printBuf[0] = 0;
Rhyme 0:6f371c791202 246 sprintf(_printBuf, "len: %d value: ", len);
Rhyme 0:6f371c791202 247 for (int i = 0; i < len; i++) {
Rhyme 0:6f371c791202 248 int b = bytes[i] & 0xff;
Rhyme 0:6f371c791202 249 sprintf(&_printBuf[strlen(_printBuf)], "%02x", b);
Rhyme 0:6f371c791202 250 }
Rhyme 0:6f371c791202 251 _serial->printf(_printBuf);
Rhyme 0:6f371c791202 252 }
Rhyme 0:6f371c791202 253
Rhyme 0:6f371c791202 254 void Command::dump() {
Rhyme 0:6f371c791202 255 _printBuf[0] = 0;
Rhyme 0:6f371c791202 256 sprintf(_printBuf, "cmd: %s attr: %d value: ",
Rhyme 0:6f371c791202 257 CMD_NAMES[_cmd - MESSAGE_CHANNEL_BASE - 1],
Rhyme 0:6f371c791202 258 _attrId
Rhyme 0:6f371c791202 259 );
Rhyme 0:6f371c791202 260 if (_cmd != MSG_TYPE_GET) {
Rhyme 0:6f371c791202 261 for (int i = 0; i < _valueLen; i++) {
Rhyme 0:6f371c791202 262 int b = _value[i] & 0xff;
Rhyme 0:6f371c791202 263 sprintf(&_printBuf[strlen(_printBuf)], "%02x", b);
Rhyme 0:6f371c791202 264 }
Rhyme 0:6f371c791202 265 }
Rhyme 0:6f371c791202 266 _serial->printf(_printBuf);
Rhyme 0:6f371c791202 267 }
Rhyme 0:6f371c791202 268
Rhyme 0:6f371c791202 269 uint8_t Command::getVal(char c) {
Rhyme 0:6f371c791202 270 if (c >= '0' && c <= '9')
Rhyme 0:6f371c791202 271 return (uint8_t)(c - '0');
Rhyme 0:6f371c791202 272 else if (c >= 'A' && c <= 'F')
Rhyme 0:6f371c791202 273 return (uint8_t)(c - 'A' + 10);
Rhyme 0:6f371c791202 274 else if (c >= 'a' && c <= 'f')
Rhyme 0:6f371c791202 275 return (uint8_t)(c - 'a' + 10);
Rhyme 0:6f371c791202 276
Rhyme 0:6f371c791202 277 _serial->printf("bad hex char: 0x%02X\n", c);
Rhyme 0:6f371c791202 278
Rhyme 0:6f371c791202 279 return 0;
Rhyme 0:6f371c791202 280 }
Rhyme 0:6f371c791202 281