an old afLib which supports both SPI and UART

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers afLib.h Source File

afLib.h

00001 /**
00002  * Copyright 2015 Afero, Inc.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef AFLIB_H__
00018 #define AFLIB_H__
00019 
00020 #include "iafLib.h"
00021 #include "SPI.h"
00022 #include "Command.h"
00023 #include "StatusCommand.h"
00024 #include "afSPI.h"
00025 #define Stream Serial
00026 
00027 #define STATE_IDLE                          0
00028 #define STATE_STATUS_SYNC                   1
00029 #define STATE_STATUS_ACK                    3
00030 #define STATE_SEND_BYTES                    4
00031 #define STATE_RECV_BYTES                    5
00032 #define STATE_CMD_COMPLETE                  6
00033 
00034 #define REQUEST_QUEUE_SIZE                  10
00035 
00036 typedef struct {
00037     uint8_t     messageType;
00038     uint16_t    attrId;
00039     uint8_t     requestId;
00040     uint16_t    valueLen;
00041     uint8_t     *p_value;
00042     uint8_t     status;
00043     uint8_t     reason;
00044 } request_t;
00045 
00046 class afLib : public iafLib {
00047 public:
00048     afLib(AttrSetHandler attrSet, AttrNotifyHandler attrNotify, Stream *serial, afTransport *theTransport);
00049     afLib(PinName mcuInterrupt, isr isrWrapper, AttrSetHandler attrSet, AttrNotifyHandler attrNotify, Stream *serial, afTransport *theTransport);
00050 
00051     virtual int getRequestId(void) ; /* 20-Mar-2018 by Motoo Tanaka */
00052     
00053     virtual void loop(void);
00054 
00055     virtual int getAttribute(const uint16_t attrId);
00056 
00057     virtual int setAttributeBool(const uint16_t attrId, const bool value);
00058 
00059     virtual int setAttribute8(const uint16_t attrId, const int8_t value);
00060 
00061     virtual int setAttribute16(const uint16_t attrId, const int16_t value);
00062 
00063     virtual int setAttribute32(const uint16_t attrId, const int32_t value);
00064 
00065     virtual int setAttribute64(const uint16_t attrId, const int64_t value);
00066 
00067     virtual int setAttributeStr(const uint16_t attrId, const char *value);
00068 
00069     virtual int setAttributeCStr(const uint16_t attrId, const uint16_t valueLen, const char *value);
00070 
00071     virtual int setAttributeBytes(const uint16_t attrId, const uint16_t valueLen, const uint8_t *value);
00072 
00073     virtual bool isIdle();
00074 
00075     virtual void mcuISR();
00076 
00077 private:
00078     Stream *_theLog;
00079     afTransport *_theTransport;
00080     InterruptIn *fco ; /* 20-Mar-2018 Motoo Tanaka */
00081 
00082     //SPISettings _spiSettings;
00083     volatile int _interrupts_pending;
00084     int _state;
00085     uint16_t _bytesToSend;
00086     uint16_t _bytesToRecv;
00087     uint8_t _requestId;
00088     uint16_t _outstandingSetGetAttrId;
00089 
00090     // Application Callbacks.
00091     AttrSetHandler _attrSetHandler;
00092     AttrNotifyHandler _attrNotifyHandler;
00093 
00094     Command *_writeCmd;
00095     uint16_t _writeBufferLen;
00096     uint8_t *_writeBuffer;
00097 
00098     Command *_readCmd;
00099     uint16_t _readBufferLen;
00100     uint8_t *_readBuffer;
00101 
00102     uint16_t _writeCmdOffset;
00103     uint16_t _readCmdOffset;
00104 
00105     StatusCommand *_txStatus;
00106     StatusCommand *_rxStatus;
00107 
00108     request_t _request;
00109 
00110 #ifdef ATTRIBUTE_CLI
00111     int parseCommand(const char *cmd);
00112 #endif
00113 
00114     void sendCommand(void);
00115 
00116     void runStateMachine(void);
00117 
00118     void printState(int state);
00119 
00120     bool inSync(StatusCommand *tx, StatusCommand *rx);
00121 
00122     void sendBytesSPI(char *bytes, int len);
00123     void sendBytesUART(char *bytes, int len);
00124     void sendBytes();
00125 
00126     void recvBytesSPI(char *bytes, int len);
00127     void recvBytesUART(char *bytes, int len);
00128     void recvBytes();
00129 
00130     void dumpBytes(char *label, int len, uint8_t *bytes);
00131 
00132     void updateIntsPending(int amount);
00133 
00134     void queueInit(void);
00135 
00136     int queuePut(uint8_t messageType, uint8_t requestId, const uint16_t attributeId, uint16_t valueLen, const uint8_t *value, uint8_t status, uint8_t reason);
00137 
00138     int queueGet(uint8_t *messageType, uint8_t *requestId, uint16_t *attributeId, uint16_t *valueLen, uint8_t **value, uint8_t *status, uint8_t *reason);
00139 
00140     int doGetAttribute(uint8_t requestId, uint16_t attrId);
00141 
00142     int doSetAttribute(uint8_t requestId, uint16_t attrId, uint16_t valueLen, uint8_t *value);
00143 
00144     int doUpdateAttribute(uint8_t requestId, uint16_t attrId, uint16_t valueLen, uint8_t *value, uint8_t status, uint8_t reason);
00145 
00146     int setAttributeComplete(uint8_t requestId, const uint16_t attrId, const uint16_t valueLen, const uint8_t *value, uint8_t status, uint8_t reason);
00147 
00148     void onStateIdle(void);
00149     void onStateSync(void);
00150     void onStateAck(void);
00151     void onStateSendBytes(void);
00152     void onStateRecvBytes(void);
00153     void onStateCmdComplete(void);
00154 };
00155 
00156 #endif // AFLIB_H__