Simulated product dispenser
Fork of mbed-cloud-workshop-connect-HTS221 by
ftcd_comm_base.h
00001 // ---------------------------------------------------------------------------- 00002 // Copyright 2016-2017 ARM Ltd. 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 __FTCD_COMM_BASE_H__ 00018 #define __FTCD_COMM_BASE_H__ 00019 00020 #include <stdint.h> 00021 00022 /** 00023 * @file ftcd_comm_base.h 00024 * 00025 * Token [64bit] : The message identifier 00026 * Status [32 bit] : Status of message parameters (exists in response messages only) 00027 * Length [32bit] : The message length in bytes 00028 * Message [Length] : A message to be processed 00029 * Signature [32B] : The hash (SHA256) value of the message 00030 */ 00031 00032 00033 /** Unique message identifiers 00034 */ 00035 #define FTCD_MSG_HEADER_TOKEN_FCC { 0x6d, 0x62, 0x65, 0x64, 0x70, 0x72, 0x6f, 0x76 } 00036 #define FTCD_MSG_HEADER_TOKEN_SDA { 0x6d, 0x62, 0x65, 0x64, 0x64, 0x62, 0x61, 0x70 } 00037 #define FTCD_MSG_HEADER_TOKEN_SIZE_BYTES 8 00038 00039 00040 typedef enum { 00041 FTCD_COMM_STATUS_SUCCESS, 00042 FTCD_COMM_STATUS_ERROR, //generic error 00043 FTCD_COMM_INVALID_PARAMETER, 00044 FTCD_COMM_MEMORY_OUT, 00045 FTCD_COMM_FAILED_TO_READ_MESSAGE_SIZE, 00046 FTCD_COMM_FAILED_TO_READ_MESSAGE_BYTES, 00047 FTCD_COMM_FAILED_TO_READ_MESSAGE_SIGNATURE, 00048 FTCD_COMM_FAILED_TO_CALCULATE_MESSAGE_SIGNATURE, 00049 FTCD_COMM_INCONSISTENT_MESSAGE_SIGNATURE, 00050 FTCD_COMM_FAILED_TO_PROCESS_DATA, 00051 FTCD_COMM_FAILED_TO_PROCESS_MESSAGE, 00052 FTCD_COMM_FAILED_TO_SEND_VALID_RESPONSE, 00053 00054 FTCD_COMM_NETWORK_TIMEOUT, //socket timeout error 00055 FTCD_COMM_NETWORK_CONNECTION_ERROR, //socket error 00056 FTCD_COMM_NETWORK_CONNECTION_CLOSED, // Socket connection was closed by the client 00057 FTCD_COMM_INTERNAL_ERROR, 00058 00059 FTCD_COMM_STATUS_MAX_ERROR = 0xFFFFFFFF 00060 } ftcd_comm_status_e; 00061 00062 typedef enum { 00063 FTCD_COMM_NET_ENDIANNESS_LITTLE, 00064 FTCD_COMM_NET_ENDIANNESS_BIG, 00065 } ftcd_comm_network_endianness_e; 00066 00067 /** 00068 * \brief ::FtcdCommBase implements the logic of processing incoming requests from the remote Factory Tool Demo. 00069 */ 00070 class FtcdCommBase { 00071 00072 public: 00073 00074 FtcdCommBase(ftcd_comm_network_endianness_e network_endianness,const uint8_t *header_token = NULL, bool use_signature = false); 00075 00076 /** Not certain that we need to do anything here, but just in case we need 00077 * to do some clean-up at some point. 00078 */ 00079 virtual ~FtcdCommBase() = 0; 00080 00081 /** 00082 * Initializes Network interface and opens socket 00083 * Prints IP address 00084 */ 00085 virtual bool init(void); 00086 00087 /** 00088 * Closes the opened socket 00089 */ 00090 virtual void finish(void); 00091 00092 /** Wait and read complete message from the communication line. 00093 * The method waits in blocking mode for new message, 00094 * allocate and read the message, 00095 * and sets message_out and message_size_out 00096 * 00097 * @param message_out The message allocated and read from the communication line 00098 * @param message_size_out The message size in bytes 00099 * 00100 * @returns 00101 * FTCD_COMM_STATUS_SUCCESS on success, otherwise appropriate error from ftcd_comm_status_e 00102 */ 00103 virtual ftcd_comm_status_e wait_for_message(uint8_t **message_out, uint32_t *message_size_out); 00104 00105 /** Writes a response message to the communication line. 00106 * The method build response message with header and signature (if requested) 00107 * and writes it to the line 00108 * 00109 * @param response_message The message to send through the communication line medium 00110 * @param response_message_size The message size in bytes 00111 * 00112 * @returns 00113 * FTCD_COMM_STATUS_SUCCESS on success, otherwise appropriate error from ftcd_comm_status_e 00114 */ 00115 ftcd_comm_status_e send_response(const uint8_t *response_message, uint32_t response_message_size); 00116 00117 /** Writes a response message with status to the communication line. 00118 * The method build response message with status, header and signature (if requested) 00119 * and writes it to the line 00120 * 00121 * @param response_message The message to send through the communication line medium 00122 * @param response_message_size The message size in bytes 00123 * 00124 * @returns 00125 * FTCD_COMM_STATUS_SUCCESS on success, otherwise appropriate error from ftcd_comm_status_e 00126 */ 00127 ftcd_comm_status_e send_response(const uint8_t *response_message, uint32_t response_message_size, ftcd_comm_status_e status_code); 00128 00129 /** Writes an allocated response message to the communication line medium. 00130 * 00131 * @param response_message The message to send through the communication line medium 00132 * @param response_message_size The message size in bytes 00133 * 00134 * @returns 00135 * true upon success, false otherwise 00136 */ 00137 virtual bool send(const uint8_t *response_message, uint32_t response_message_size) = 0; 00138 00139 /** Detects the message token from the communication line medium. 00140 * 00141 * @returns 00142 * zero, if token detected and different value otherwise 00143 */ 00144 virtual ftcd_comm_status_e is_token_detected(void) = 0; 00145 00146 /** Reads the message size in bytes from the communication line medium. 00147 * This is the amount of bytes needed to allocate for the upcoming message bytes. 00148 * 00149 * @returns 00150 * The message size in bytes in case of success, zero bytes otherwise. 00151 */ 00152 virtual uint32_t read_message_size(void) = 0; 00153 00154 /** Reads the message size in bytes from the communication line medium. 00155 * This is the amount of bytes needed to allocate for the upcoming message bytes. 00156 * 00157 * @param message_out The buffer to read into and return to the caller. 00158 * @param message_size The message size in bytes. 00159 * 00160 * @returns 00161 * true upon success, false otherwise 00162 */ 00163 virtual bool read_message(uint8_t *message_out, size_t message_size) = 0; 00164 00165 /** Reads the message size in bytes from the communication line medium. 00166 * This is the amount of bytes needed to allocate for the upcoming message bytes. 00167 * 00168 * @param sig The buffer to read into and return to the caller. 00169 * @param sig_size The sig buffer size in bytes. 00170 * 00171 * @returns 00172 * The message size in bytes in case of success, zero bytes otherwise. 00173 */ 00174 virtual bool read_message_signature(uint8_t *sig, size_t sig_size) = 0; 00175 00176 protected: 00177 00178 /* Member point to the token array */ 00179 uint8_t *_header_token; 00180 00181 private: 00182 00183 /* Internal method that build response message with status, header and signature (if requested) 00184 * and writes it to the line */ 00185 ftcd_comm_status_e _send_response(const uint8_t *response_message, uint32_t response_message_size, bool send_status_code, ftcd_comm_status_e status_code); 00186 00187 /** Holds the requested network bytes order */ 00188 ftcd_comm_network_endianness_e _network_endianness; 00189 00190 /** Holds the requested message format (with token or without) */ 00191 bool _use_token; 00192 00193 /** Holds the requested message format (with signature or without) */ 00194 bool _use_signature; 00195 }; 00196 00197 #endif // __FTCD_COMM_BASE_H__
Generated on Tue Jul 12 2022 19:12:12 by 1.7.2