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