1- #include < algorithm>
21#if defined(__unix__) || defined(__unix) || defined(__APPLE__)
32#include " serial.h"
43#include < fstream>
87#include < asm/termbits.h>
98#include < cerrno>
109#include < cstring>
11- // #include <filesystem>
12-
13-
14- // namespace fs = std::filesystem;
10+ #include < dirent.h>
1511
1612int hSerialPort;
1713termios2 tty;
18-
19- std::ofstream logging (" log.log" , std::ios::out);
14+ std::string data;
2015
2116void (*errorCallback)(int errorCode);
2217void (*readCallback)(int bytes);
@@ -155,7 +150,7 @@ auto serialRead(
155150
156151 tty.c_cc [VMIN] = bufferSize;
157152
158- if (ioctl (hSerialPort, TCSETS2, &tty) == -1 ){
153+ if (ioctl (hSerialPort, TCSETS2, &tty) == -1 ) {
159154 errorCallback (status (StatusCodes::SET_STATE_ERROR));
160155 return 0 ;
161156 }
@@ -177,7 +172,31 @@ auto serialReadUntil(
177172 const int multiplier,
178173 void * searchString
179174) -> int {
180- return 0 ;
175+
176+ data = " " ;
177+
178+ for (int i{0 }; i < bufferSize && data.find (std::string (static_cast <char *>(searchString))) == std::string::npos; i++) {
179+ char bufferChar[1 ];
180+
181+ // Error if read fails
182+ int bytesRead = read (hSerialPort, static_cast <char *>(bufferChar), 1 );
183+ if (bytesRead == -1 ) {
184+ errorCallback (status (StatusCodes::READ_ERROR));
185+ return 0 ;
186+ }
187+
188+ if (bytesRead == 0 ) {
189+ break ;
190+ }
191+
192+ data.append (std::string (bufferChar, bytesRead));
193+ }
194+
195+ memcpy (buffer, data.c_str (), data.length () + 1 );
196+
197+ readCallback (data.length ());
198+
199+ return data.length ();
181200}
182201
183202auto serialWrite (
@@ -191,12 +210,13 @@ auto serialWrite(
191210
192211 int bytesWritten = write (hSerialPort, tmp, bufferSize);
193212
194- if (bytesWritten >= 0 ){
195- return bytesWritten;
213+ if (bytesWritten == -1 ) {
214+ errorCallback (status (StatusCodes::WRITE_ERROR));
215+ return 0 ;
196216 }
197-
198- errorCallback ( status (StatusCodes::WRITE_ERROR) );
199- return 0 ;
217+
218+ writeCallback (bytesWritten );
219+ return bytesWritten ;
200220}
201221
202222auto serialOnError (void (*func)(int code)) -> void {
@@ -212,44 +232,45 @@ auto serialOnWrite(void (*func)(int bytes)) -> void {
212232};
213233
214234auto serialGetPortsInfo (void *buffer, const int bufferSize, void *separator) -> int {
215- return 0 ;
216- }
235+ std::string result = " " ;
217236
218- // auto getPortsInfoUnix(
219- // void* buffer,
220- // const int bufferSize,
221- // void* separator
222- // ) -> int {
223- // std::string result;
237+ int portsCounter = 0 ;
224238
225- // int portsCounter = 0;
239+ DIR* dir = opendir (" /dev/serial/by-id" );
240+ if (dir == nullptr ) {
241+ // Handle directory not found error
242+ return -1 ; // Return an appropriate error code or define your own
243+ }
226244
227- // fs::path p("/dev/serial/by-id");
228-
229- // try {
230- // if (!exists(p)) {
231- // returnStatus(StatusCodes::NOT_FOUND_ERROR);
232- // }
233-
234- // else {
235- // for (auto de : fs::directory_iterator(p)) {
236- // if (is_symlink(de.symlink_status())) {
237- // fs::path symlink_points_at = read_symlink(de);
238- // fs::path canonical_path = fs::canonical(p / symlink_points_at);
239- // result += canonical_path.generic_string().append(std::string(static_cast<char*>(separator)));
240- // portsCounter++;
241- // }
242- // }
243- // }
244- // } catch (const fs::filesystem_error &exeption) {
245- // }
246-
247- // if (result.length() + 1 <= bufferSize){
248- // memcpy(buffer, result.c_str(), result.length() + 1);
249- // } else {
250- // returnStatus(StatusCodes::BUFFER_ERROR);
251- // }
252- // return portsCounter;
253- // }
245+ struct dirent * entry;
246+ while ((entry = readdir (dir)) != nullptr ) {
247+ if (entry->d_type == DT_LNK) {
248+ std::string symlinkPath = " /dev/serial/by-id/" ;
249+ symlinkPath += entry->d_name ;
250+
251+ char canonicalPath[PATH_MAX];
252+ if (realpath (symlinkPath.c_str (), canonicalPath) != nullptr ) {
253+ result += std::string (canonicalPath) + std::string (static_cast <char *>(separator));
254+ portsCounter++;
255+ }
256+ }
257+ }
258+
259+ closedir (dir);
260+
261+ // Remove last trailing comma
262+ if (result.length () > 0 ) {
263+ result.erase (result.length () - 1 );
264+ }
265+
266+ if (result.length () + 1 <= bufferSize) {
267+ memcpy (buffer, result.c_str (), result.length () + 1 );
268+ } else {
269+ errorCallback (status (StatusCodes::BUFFER_ERROR));
270+ return 0 ;
271+ }
272+
273+ return portsCounter;
274+ }
254275
255276#endif
0 commit comments