@jason2866/serialport-bindings-cpp 0.0.0-development

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,93 @@
1
+ #ifndef PACKAGES_SERIALPORT_SRC_SERIALPORT_WIN_H_
2
+ #define PACKAGES_SERIALPORT_SRC_SERIALPORT_WIN_H_
3
+ #include <napi.h>
4
+ #include <uv.h>
5
+ #include <node_buffer.h>
6
+ #include <list>
7
+ #include <string>
8
+
9
+ #define ERROR_STRING_SIZE 1088
10
+
11
+ static inline HANDLE int2handle(int ptr) {
12
+ return reinterpret_cast<HANDLE>(static_cast<uintptr_t>(ptr));
13
+ }
14
+
15
+ struct WriteBaton {
16
+ WriteBaton() : bufferData(), errorString() {}
17
+ int fd = 0;
18
+ char* bufferData = nullptr;
19
+ size_t bufferLength = 0;
20
+ size_t offset = 0;
21
+ size_t bytesWritten = 0;
22
+ void* hThread = nullptr;
23
+ bool complete = false;
24
+ Napi::ObjectReference buffer;
25
+ Napi::FunctionReference callback;
26
+ int result = 0;
27
+ char errorString[ERROR_STRING_SIZE];
28
+ };
29
+
30
+ Napi::Value Write(const Napi::CallbackInfo& info);
31
+
32
+ struct ReadBaton {
33
+ ReadBaton() : errorString() {}
34
+ int fd = 0;
35
+ char* bufferData = nullptr;
36
+ size_t bufferLength = 0;
37
+ size_t bytesRead = 0;
38
+ size_t bytesToRead = 0;
39
+ size_t offset = 0;
40
+ void* hThread = nullptr;
41
+ Napi::FunctionReference callback;
42
+ bool complete = false;
43
+ char errorString[ERROR_STRING_SIZE];
44
+ };
45
+
46
+ Napi::Value Read(const Napi::CallbackInfo& info);
47
+
48
+ Napi::Value List(const Napi::CallbackInfo& info);
49
+ void setIfNotEmpty(Napi::Object item, std::string key, const char *value);
50
+ void setIfNotEmpty(Napi::Object item, std::string key, const wchar_t *value);
51
+
52
+ struct ListResultItem {
53
+ std::wstring path;
54
+ std::wstring manufacturer;
55
+ std::wstring serialNumber;
56
+ std::wstring pnpId;
57
+ std::wstring locationId;
58
+ std::wstring friendlyName;
59
+ std::wstring vendorId;
60
+ std::wstring productId;
61
+ };
62
+
63
+ struct ListBaton : public Napi::AsyncWorker {
64
+ ListBaton(Napi::Function& callback) : Napi::AsyncWorker(callback, "node-serialport:ListBaton"),
65
+ errorString() {}
66
+ std::list<ListResultItem*> results;
67
+ wchar_t errorString[ERROR_STRING_SIZE];
68
+ void Execute() override;
69
+
70
+ void OnOK() override {
71
+ Napi::Env env = Env();
72
+ Napi::HandleScope scope(env);
73
+ Napi::Array result = Napi::Array::New(env);
74
+ int i = 0;
75
+ for (std::list<ListResultItem*>::iterator it = results.begin(); it != results.end(); ++it, i++) {
76
+ Napi::Object item = Napi::Object::New(env);
77
+
78
+ setIfNotEmpty(item, "path", (*it)->path.c_str());
79
+ setIfNotEmpty(item, "manufacturer", (*it)->manufacturer.c_str());
80
+ setIfNotEmpty(item, "serialNumber", (*it)->serialNumber.c_str());
81
+ setIfNotEmpty(item, "pnpId", (*it)->pnpId.c_str());
82
+ setIfNotEmpty(item, "locationId", (*it)->locationId.c_str());
83
+ setIfNotEmpty(item, "friendlyName", (*it)->friendlyName.c_str());
84
+ setIfNotEmpty(item, "vendorId", (*it)->vendorId.c_str());
85
+ setIfNotEmpty(item, "productId", (*it)->productId.c_str());
86
+
87
+ (result).Set(i, item);
88
+ }
89
+ Callback().Call({env.Null(), result});
90
+ }
91
+ };
92
+
93
+ #endif // PACKAGES_SERIALPORT_SRC_SERIALPORT_WIN_H_