blob: f416818f0a98570797b1b4a84b34c430b6b8f3b4 [file] [log] [blame]
Brian Silvermanf7f267a2017-02-04 16:16:08 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2016-2017. All Rights Reserved. */
3/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
6/*----------------------------------------------------------------------------*/
7
8#pragma once
9
10#include <stdint.h>
11
12#include <string>
13#include <vector>
14
15#include "HAL/SerialPort.h"
16#include "HAL/cpp/priority_mutex.h"
17#include "llvm/SmallString.h"
18#include "llvm/SmallVector.h"
19
20namespace hal {
21class SerialHelper {
22 public:
23 SerialHelper();
24
25 std::string GetVISASerialPortName(HAL_SerialPort port, int32_t* status);
26 std::string GetOSSerialPortName(HAL_SerialPort port, int32_t* status);
27
28 std::vector<std::string> GetVISASerialPortList(int32_t* status);
29 std::vector<std::string> GetOSSerialPortList(int32_t* status);
30
31 private:
32 void SortHubPathVector();
33 void CoiteratedSort(llvm::SmallVectorImpl<llvm::SmallString<16>>& vec);
34 void QueryHubPaths(int32_t* status);
35
36 int32_t GetIndexForPort(HAL_SerialPort port, int32_t* status);
37
38 // Vectors to hold data before sorting.
39 // Note we will most likely have at max 2 instances, and the longest string
40 // is around 12, so these should never touch the heap;
41 llvm::SmallVector<llvm::SmallString<16>, 4> m_visaResource;
42 llvm::SmallVector<llvm::SmallString<16>, 4> m_osResource;
43 llvm::SmallVector<llvm::SmallString<16>, 4> m_unsortedHubPath;
44 llvm::SmallVector<llvm::SmallString<16>, 4> m_sortedHubPath;
45
46 int32_t m_resourceHandle;
47
48 static priority_mutex m_nameMutex;
49 static std::string m_usbNames[2];
50};
51} // namespace hal