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.
Dependencies: FXAS21002 FXOS8700Q
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 <stdio.h> 00024 #include "mbed-trace/mbed_trace.h" 00025 #include "ftcd_comm_serial.h" 00026 #if defined(__MBED__) 00027 #include "mbed.h" 00028 #elif defined(__SXOS__) 00029 extern "C" int sxos_uart_write(const char *data, uint16_t len); 00030 #else 00031 #error OS not supported 00032 #endif 00033 00034 #define TRACE_GROUP "fcsr" 00035 00036 FtcdCommSerial::FtcdCommSerial(ftcd_comm_network_endianness_e network_endianness, const uint8_t *header_token, bool use_signature) 00037 : FtcdCommBase(network_endianness, header_token, use_signature) 00038 { 00039 } 00040 00041 FtcdCommSerial::~FtcdCommSerial() 00042 { 00043 } 00044 00045 size_t FtcdCommSerial::_serial_read(char *buffOut, size_t buffSize) 00046 { 00047 ssize_t count = 0; 00048 size_t left_to_read = buffSize; 00049 char* buffer = buffOut; 00050 while (left_to_read > 0) { 00051 #if defined(__MBED__) 00052 count = read(STDIN_FILENO, buffer, left_to_read); 00053 #elif defined(__SXOS__) 00054 *buffer = (char)getchar(); 00055 count = 1; 00056 #endif 00057 if (count < 0) { 00058 break; 00059 } 00060 buffer += count; 00061 left_to_read -= count; 00062 } 00063 00064 return buffSize - left_to_read; 00065 } 00066 00067 size_t FtcdCommSerial::_serial_write(const char *buff, size_t buffSize) 00068 { 00069 ssize_t count = 0; 00070 size_t left_to_write = buffSize; 00071 char* buffer = (char*)buff; 00072 while (left_to_write > 0) { 00073 #if defined(__MBED__) 00074 count = write(STDOUT_FILENO, buffer, left_to_write); 00075 #elif defined(__SXOS__) 00076 count = sxos_uart_write(buffer, left_to_write); 00077 #endif 00078 if (count < 0) { 00079 break; 00080 } 00081 buffer += count; 00082 left_to_write -= count; 00083 } 00084 00085 return buffSize - left_to_write; 00086 } 00087 00088 ftcd_comm_status_e FtcdCommSerial::is_token_detected() 00089 { 00090 char c; 00091 size_t idx = 0; 00092 00093 //read char by char to detect token 00094 while (idx < FTCD_MSG_HEADER_TOKEN_SIZE_BYTES) { 00095 _serial_read(&c, 1); 00096 if (c == _header_token[idx]) { 00097 idx++; 00098 } else { 00099 idx = 0; 00100 } 00101 } 00102 return FTCD_COMM_STATUS_SUCCESS; 00103 } 00104 00105 uint32_t FtcdCommSerial::read_message_size() 00106 { 00107 uint32_t message_size = 0; 00108 00109 size_t read_chars = _serial_read(reinterpret_cast<char*>(&message_size), sizeof(message_size)); 00110 if (read_chars != sizeof(message_size)) { 00111 mbed_tracef(TRACE_LEVEL_CMD, TRACE_GROUP, "Failed reading message size (read %d bytes out of %d)", read_chars, sizeof(message_size)); 00112 return 0; 00113 } 00114 00115 return message_size; 00116 } 00117 00118 bool FtcdCommSerial::read_message(uint8_t *message_out, size_t message_size) 00119 { 00120 if (message_out == NULL) { 00121 mbed_tracef(TRACE_LEVEL_CMD, TRACE_GROUP, "Invalid message buffer"); 00122 return false; 00123 } 00124 00125 // Read CBOR message bytes 00126 // We assume that LENGTH is NOT bigger than INT_MAX 00127 size_t read_chars = _serial_read(reinterpret_cast<char*>(message_out), message_size); 00128 if (read_chars != message_size) { 00129 mbed_tracef(TRACE_LEVEL_CMD, TRACE_GROUP, "Failed reading message bytes (read %d bytes out of %d)", read_chars, message_size); 00130 return false; 00131 } 00132 00133 return true; 00134 } 00135 00136 bool FtcdCommSerial::read_message_signature(uint8_t *sig, size_t sig_size) 00137 { 00138 if (sig == NULL) { 00139 mbed_tracef(TRACE_LEVEL_CMD, TRACE_GROUP, "Invalid sig buffer"); 00140 return false; 00141 } 00142 00143 // Read signature from medium 00144 size_t read_chars = _serial_read(reinterpret_cast<char*>(sig), sig_size); 00145 if (read_chars != sig_size) { 00146 mbed_tracef(TRACE_LEVEL_CMD, TRACE_GROUP, "Failed reading message signature bytes (read %d bytes out of %d)", read_chars, sig_size); 00147 return false; 00148 } 00149 00150 return true; 00151 } 00152 00153 bool FtcdCommSerial::send(const uint8_t *data, uint32_t data_size) 00154 { 00155 if (data == NULL) { 00156 mbed_tracef(TRACE_LEVEL_CMD, TRACE_GROUP, "Invalid response_message"); 00157 return false; 00158 } 00159 if (data_size == 0) { 00160 mbed_tracef(TRACE_LEVEL_CMD, TRACE_GROUP, "Got an empty message"); 00161 return false; 00162 } 00163 00164 // Send data on the serial medium 00165 size_t write_chars = _serial_write(reinterpret_cast<const char*>(data), data_size); 00166 if (write_chars != data_size) { 00167 mbed_tracef(TRACE_LEVEL_CMD, TRACE_GROUP, "Failed writing message bytes (wrote %" PRIu32 " bytes out of %" PRIu32 ")", (uint32_t)write_chars, data_size); 00168 return false; 00169 } 00170 00171 return true; 00172 }
Generated on Tue Jul 12 2022 20:20:59 by
