Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ftcd_comm_serial.cpp
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 // Note: this macro is needed on armcc to get the the PRI*32 macros 00018 // from inttypes.h in a C++ code. 00019 #ifndef __STDC_FORMAT_MACROS 00020 #define __STDC_FORMAT_MACROS 00021 #endif 00022 00023 #include <stdlib.h> 00024 #include "pv_endian.h" 00025 #include "pal.h" 00026 #include "pv_log.h" 00027 #include "ftcd_comm_serial.h" 00028 00029 #define TRACE_GROUP "fcsr" 00030 00031 FtcdCommSerial::FtcdCommSerial(Serial *pc, ftcd_comm_network_endianness_e network_endianness, const uint8_t *header_token, bool use_signature) 00032 : FtcdCommBase(network_endianness, header_token, use_signature) 00033 { 00034 _pc = pc; 00035 } 00036 00037 FtcdCommSerial::~FtcdCommSerial() 00038 { 00039 } 00040 00041 size_t FtcdCommSerial::_serial_read(char *buffOut, size_t buffSize) 00042 { 00043 size_t count; 00044 //TODO: 00045 //getc is blocking. There is currently no way to check if there is anything left to read. seems readable() us not working 00046 //Once determined, the relevant check should be added to this code. 00047 for (count = 0; count < buffSize; count++) { 00048 buffOut[count] = _pc->getc(); 00049 } 00050 return count; 00051 } 00052 00053 size_t FtcdCommSerial::_serial_write(const char *buff, size_t buffSize) 00054 { 00055 00056 for (size_t i = 0; i < buffSize; i++) { 00057 _pc->putc(buff[i]); 00058 } 00059 00060 return buffSize; 00061 } 00062 00063 ftcd_comm_status_e FtcdCommSerial::is_token_detected() 00064 { 00065 char c; 00066 size_t idx = 0; 00067 00068 //read char by char to detect token 00069 while (idx < FTCD_MSG_HEADER_TOKEN_SIZE_BYTES) { 00070 _serial_read(&c, 1); 00071 if (c == _header_token[idx]) { 00072 idx++; 00073 } else { 00074 idx = 0; 00075 } 00076 } 00077 return FTCD_COMM_STATUS_SUCCESS; 00078 } 00079 00080 uint32_t FtcdCommSerial::read_message_size() 00081 { 00082 uint32_t message_size = 0; 00083 00084 size_t read_chars = _serial_read(reinterpret_cast<char*>(&message_size), sizeof(message_size)); 00085 if (read_chars != sizeof(message_size)) { 00086 mbed_tracef(TRACE_LEVEL_CMD, TRACE_GROUP, "Failed reading message size (read %d bytes out of %d)", read_chars, sizeof(message_size)); 00087 return 0; 00088 } 00089 00090 return message_size; 00091 } 00092 00093 bool FtcdCommSerial::read_message(uint8_t *message_out, size_t message_size) 00094 { 00095 if (message_out == NULL) { 00096 mbed_tracef(TRACE_LEVEL_CMD, TRACE_GROUP, "Invalid message buffer"); 00097 return false; 00098 } 00099 00100 // Read CBOR message bytes 00101 // We assume that LENGTH is NOT bigger than INT_MAX 00102 size_t read_chars = _serial_read(reinterpret_cast<char*>(message_out), message_size); 00103 if (read_chars != message_size) { 00104 mbed_tracef(TRACE_LEVEL_CMD, TRACE_GROUP, "Failed reading message bytes (read %d bytes out of %d)", read_chars, message_size); 00105 return false; 00106 } 00107 00108 return true; 00109 } 00110 00111 bool FtcdCommSerial::read_message_signature(uint8_t *sig, size_t sig_size) 00112 { 00113 if (sig == NULL) { 00114 mbed_tracef(TRACE_LEVEL_CMD, TRACE_GROUP, "Invalid sig buffer"); 00115 return false; 00116 } 00117 00118 // Read signature from medium 00119 size_t read_chars = _serial_read(reinterpret_cast<char*>(sig), sig_size); 00120 if (read_chars != sig_size) { 00121 mbed_tracef(TRACE_LEVEL_CMD, TRACE_GROUP, "Failed reading message signature bytes (read %d bytes out of %d)", read_chars, sig_size); 00122 return false; 00123 } 00124 00125 return true; 00126 } 00127 00128 bool FtcdCommSerial::send(const uint8_t *data, uint32_t data_size) 00129 { 00130 if (data == NULL) { 00131 mbed_tracef(TRACE_LEVEL_CMD, TRACE_GROUP, "Invalid response_message"); 00132 return false; 00133 } 00134 if (data_size == 0) { 00135 mbed_tracef(TRACE_LEVEL_CMD, TRACE_GROUP, "Got an empty message"); 00136 return false; 00137 } 00138 00139 // Send data on the serial medium 00140 size_t write_chars = _serial_write(reinterpret_cast<const char*>(data), data_size); 00141 if (write_chars != data_size) { 00142 mbed_tracef(TRACE_LEVEL_CMD, TRACE_GROUP, "Failed writing message bytes (wrote %" PRIu32 " bytes out of %" PRIu32 ")", (uint32_t)write_chars, data_size); 00143 return false; 00144 } 00145 00146 return true; 00147 }
Generated on Tue Jul 12 2022 19:01:34 by
1.7.2