Orefatoi / Mbed 2 deprecated afero_poc15_171201

Dependencies:   mbed vt100

Committer:
Rhyme
Date:
Fri Dec 01 06:16:31 2017 +0000
Revision:
0:f0de320e23ac
OLED display for GAS pressure started working

Who changed what in which revision?

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