@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.
- package/LICENSE +21 -0
- package/README.md +95 -0
- package/binding.gyp +80 -0
- package/package.json +113 -0
- package/src/darwin_list.cpp +317 -0
- package/src/darwin_list.h +68 -0
- package/src/poller.cpp +166 -0
- package/src/poller.h +35 -0
- package/src/serialport.cpp +342 -0
- package/src/serialport.h +202 -0
- package/src/serialport_linux.cpp +76 -0
- package/src/serialport_linux.h +10 -0
- package/src/serialport_unix.cpp +482 -0
- package/src/serialport_unix.h +7 -0
- package/src/serialport_win.cpp +989 -0
- package/src/serialport_win.h +93 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright 2010 Christopher Williams. All rights reserved.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to
|
|
7
|
+
deal in the Software without restriction, including without limitation the
|
|
8
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
9
|
+
sell copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
20
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
21
|
+
IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# @serialport/bindings-cpp
|
|
2
|
+
|
|
3
|
+
[](#backers)
|
|
4
|
+
[](#sponsors)
|
|
5
|
+
[](https://codecov.io/gh/serialport/bindings-cpp)
|
|
6
|
+
[](https://github.com/serialport/bindings-cpp/actions/workflows/test.yml)
|
|
7
|
+
|
|
8
|
+
Access serial ports with JavaScript. Linux, OSX and Windows. Welcome your robotic JavaScript overlords. Better yet, program them!
|
|
9
|
+
|
|
10
|
+
> Go to https://serialport.io/ to learn more, find guides and api documentation.
|
|
11
|
+
|
|
12
|
+
## Quick Links
|
|
13
|
+
|
|
14
|
+
- 📚 [**Guides**](https://serialport.io/docs/)
|
|
15
|
+
- [**API Docs**](https://serialport.io/docs/api-serialport)
|
|
16
|
+
- [`@serialport/bindings-cpp`](https://www.npmjs.com/package/@serialport/bindings-cpp)
|
|
17
|
+
- 🐛 [Help and Bugs](https://github.com/serialport/node-serialport/issues/new/choose) All serialport issues are pointed to the main serialport repo.
|
|
18
|
+
|
|
19
|
+
### Bindings
|
|
20
|
+
|
|
21
|
+
The Bindings provide a low level interface to work with your serialport. It is possible to use them alone but it's usually easier to use them with an interface.
|
|
22
|
+
|
|
23
|
+
- [`@serialport/bindings-cpp`](https://serialport.io/docs/api-bindings-cpp) bindings for Linux, Mac and Windows
|
|
24
|
+
- [`@serialport/binding-interface`](https://serialport.io/docs/api-bindings-interface) as an interface to use if you're making your own bindings
|
|
25
|
+
- [`@serialport/binding-mock`](https://serialport.io/docs/api-binding-mock) for a mock binding package for testing
|
|
26
|
+
|
|
27
|
+
## Developing
|
|
28
|
+
|
|
29
|
+
### Developing node serialport projects
|
|
30
|
+
|
|
31
|
+
1. Clone this repo `git clone git@github.com:serialport/bindings-cpp.git`
|
|
32
|
+
1. Run `npm install` to setup local package dependencies (run this any time you depend on a package local to this repo)
|
|
33
|
+
1. Run `npm test` to ensure everything is working properly
|
|
34
|
+
1. If you have a serial loopback device (TX to RX) you can run run `TEST_PORT=/path/to/port npm test` for a more comprehensive test suite. (Defaults to 115200 baud customize with the TEST_BAUD env.) You can use an arduino with the `test/arduino-echo` sketch.
|
|
35
|
+
|
|
36
|
+
### Developing Docs
|
|
37
|
+
|
|
38
|
+
See https://github.com/serialport/website
|
|
39
|
+
|
|
40
|
+
## License
|
|
41
|
+
|
|
42
|
+
SerialPort packages are all [MIT licensed](LICENSE) and all it's dependencies are MIT licensed.
|
|
43
|
+
|
|
44
|
+
## Code of Conduct
|
|
45
|
+
|
|
46
|
+
SerialPort follows the [Nodebots Code of Conduct](http://nodebots.io/conduct.html). While the code is MIT licensed participation in the community has some rules to make this a good place to work and learn.
|
|
47
|
+
|
|
48
|
+
### TLDR
|
|
49
|
+
|
|
50
|
+
- Be respectful.
|
|
51
|
+
- Abusive behavior is never tolerated.
|
|
52
|
+
- Data published to NodeBots is hosted at the discretion of the service administrators, and may be removed.
|
|
53
|
+
- Don't build evil robots.
|
|
54
|
+
- Violations of this code may result in swift and permanent expulsion from the NodeBots community.
|
|
55
|
+
|
|
56
|
+
## Governance and Community
|
|
57
|
+
|
|
58
|
+
SerialPort is currently employees a [governance](https://medium.com/the-node-js-collection/healthy-open-source-967fa8be7951) with a group of maintainers, committers and contributors, all fixing bugs and adding features and improving documentation. You need not apply to work on SerialPort, all are welcome to join, build, and maintain this project.
|
|
59
|
+
|
|
60
|
+
- A Contributor is any individual creating or commenting on an issue or pull request. By participating, this is you.
|
|
61
|
+
- Committers are contributors who have been given write access to the repository. They can review and merge pull requests.
|
|
62
|
+
- Maintainers are committers representing the required technical expertise to resolve rare disputes.
|
|
63
|
+
|
|
64
|
+
If you have a PR that improves the project people in any or all of the above people will help you land it.
|
|
65
|
+
|
|
66
|
+
**Maintainers**
|
|
67
|
+
|
|
68
|
+
- [Francis Gulotta](https://twitter.com/reconbot) | [reconbot](https://github.com/reconbot)
|
|
69
|
+
- [Nick Hehr](https://twitter.com/hipsterbrown) | [hipsterbrown](https://github.com/hipsterbrown)
|
|
70
|
+
|
|
71
|
+
### Contributors
|
|
72
|
+
|
|
73
|
+
This project exists thanks to all the people who contribute. [[Contribute](CONTRIBUTING.md)].
|
|
74
|
+
<a href="https://github.com/serialport/node-serialport/graphs/contributors"><img src="https://opencollective.com/serialport/contributors.svg?width=890&button=false" /></a>
|
|
75
|
+
|
|
76
|
+
### Backers
|
|
77
|
+
|
|
78
|
+
Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/serialport#backer)]
|
|
79
|
+
|
|
80
|
+
<a href="https://opencollective.com/serialport#backers" target="_blank"><img src="https://opencollective.com/serialport/backers.svg?width=890"></a>
|
|
81
|
+
|
|
82
|
+
### Sponsors
|
|
83
|
+
|
|
84
|
+
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/serialport#sponsor)]
|
|
85
|
+
|
|
86
|
+
<!-- <a href="https://opencollective.com/serialport/sponsor/0/website" target="_blank"><img src="https://opencollective.com/serialport/sponsor/0/avatar.svg"></a>
|
|
87
|
+
<a href="https://opencollective.com/serialport/sponsor/1/website" target="_blank"><img src="https://opencollective.com/serialport/sponsor/1/avatar.svg"></a>
|
|
88
|
+
<a href="https://opencollective.com/serialport/sponsor/2/website" target="_blank"><img src="https://opencollective.com/serialport/sponsor/2/avatar.svg"></a>
|
|
89
|
+
<a href="https://opencollective.com/serialport/sponsor/3/website" target="_blank"><img src="https://opencollective.com/serialport/sponsor/3/avatar.svg"></a>
|
|
90
|
+
<a href="https://opencollective.com/serialport/sponsor/4/website" target="_blank"><img src="https://opencollective.com/serialport/sponsor/4/avatar.svg"></a>
|
|
91
|
+
<a href="https://opencollective.com/serialport/sponsor/5/website" target="_blank"><img src="https://opencollective.com/serialport/sponsor/5/avatar.svg"></a>
|
|
92
|
+
<a href="https://opencollective.com/serialport/sponsor/6/website" target="_blank"><img src="https://opencollective.com/serialport/sponsor/6/avatar.svg"></a>
|
|
93
|
+
<a href="https://opencollective.com/serialport/sponsor/7/website" target="_blank"><img src="https://opencollective.com/serialport/sponsor/7/avatar.svg"></a>
|
|
94
|
+
<a href="https://opencollective.com/serialport/sponsor/8/website" target="_blank"><img src="https://opencollective.com/serialport/sponsor/8/avatar.svg"></a>
|
|
95
|
+
<a href="https://opencollective.com/serialport/sponsor/9/website" target="_blank"><img src="https://opencollective.com/serialport/sponsor/9/avatar.svg"></a> -->
|
package/binding.gyp
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
{
|
|
2
|
+
'variables': {
|
|
3
|
+
'openssl_fips': ''
|
|
4
|
+
},
|
|
5
|
+
'targets': [{
|
|
6
|
+
'target_name': 'bindings',
|
|
7
|
+
'sources': [
|
|
8
|
+
'src/serialport.cpp'
|
|
9
|
+
],
|
|
10
|
+
'include_dirs': ["<!(node -p \"require('node-addon-api').include_dir\")"],
|
|
11
|
+
'cflags!': [ '-fno-exceptions' ],
|
|
12
|
+
'cflags_cc!': [ '-fno-exceptions' ],
|
|
13
|
+
"defines": ["NAPI_CPP_EXCEPTIONS"],
|
|
14
|
+
'conditions': [
|
|
15
|
+
['OS=="win"',
|
|
16
|
+
{
|
|
17
|
+
'defines': ['CHECK_NODE_MODULE_VERSION'],
|
|
18
|
+
'sources': [
|
|
19
|
+
'src/serialport_win.cpp'
|
|
20
|
+
],
|
|
21
|
+
'msvs_settings': {
|
|
22
|
+
'VCCLCompilerTool': {
|
|
23
|
+
'ExceptionHandling': '1',
|
|
24
|
+
'DisableSpecificWarnings': [ '4530', '4506' ],
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
],
|
|
29
|
+
['OS=="mac"',
|
|
30
|
+
{
|
|
31
|
+
'sources': [
|
|
32
|
+
'src/serialport_unix.cpp',
|
|
33
|
+
'src/poller.cpp',
|
|
34
|
+
'src/darwin_list.cpp'
|
|
35
|
+
],
|
|
36
|
+
'xcode_settings': {
|
|
37
|
+
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
|
|
38
|
+
'MACOSX_DEPLOYMENT_TARGET': '10.9',
|
|
39
|
+
'OTHER_CFLAGS': [
|
|
40
|
+
'-arch x86_64',
|
|
41
|
+
'-arch arm64'
|
|
42
|
+
],
|
|
43
|
+
'OTHER_LDFLAGS': [
|
|
44
|
+
'-framework CoreFoundation',
|
|
45
|
+
'-framework IOKit',
|
|
46
|
+
'-arch x86_64',
|
|
47
|
+
'-arch arm64'
|
|
48
|
+
]
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
],
|
|
52
|
+
['OS=="linux"',
|
|
53
|
+
{
|
|
54
|
+
'sources': [
|
|
55
|
+
'src/serialport_unix.cpp',
|
|
56
|
+
'src/poller.cpp',
|
|
57
|
+
'src/serialport_linux.cpp'
|
|
58
|
+
]
|
|
59
|
+
}
|
|
60
|
+
],
|
|
61
|
+
['OS=="android"',
|
|
62
|
+
{
|
|
63
|
+
'sources': [
|
|
64
|
+
'src/serialport_unix.cpp',
|
|
65
|
+
'src/poller.cpp',
|
|
66
|
+
'src/serialport_linux.cpp'
|
|
67
|
+
]
|
|
68
|
+
}
|
|
69
|
+
],
|
|
70
|
+
['OS!="win"',
|
|
71
|
+
{
|
|
72
|
+
'sources': [
|
|
73
|
+
'src/serialport_unix.cpp',
|
|
74
|
+
'src/poller.cpp'
|
|
75
|
+
]
|
|
76
|
+
}
|
|
77
|
+
]
|
|
78
|
+
]
|
|
79
|
+
}],
|
|
80
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jason2866/serialport-bindings-cpp",
|
|
3
|
+
"description": "SerialPort Hardware bindings for node serialport written in c++",
|
|
4
|
+
"version": "0.0.0-development",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"serialport-binding",
|
|
9
|
+
"COM",
|
|
10
|
+
"com port",
|
|
11
|
+
"hardware",
|
|
12
|
+
"iot",
|
|
13
|
+
"modem",
|
|
14
|
+
"serial port",
|
|
15
|
+
"serial",
|
|
16
|
+
"serialport",
|
|
17
|
+
"tty",
|
|
18
|
+
"UART"
|
|
19
|
+
],
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@serialport/bindings-interface": "1.2.2",
|
|
22
|
+
"@serialport/parser-readline": "13.0.0",
|
|
23
|
+
"debug": "4.4.0",
|
|
24
|
+
"node-addon-api": "8.3.0",
|
|
25
|
+
"node-gyp-build": "4.8.4"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@semantic-release/exec": "6.0.3",
|
|
29
|
+
"@serialport/binding-mock": "10.2.2",
|
|
30
|
+
"@types/chai": "5.0.1",
|
|
31
|
+
"@types/chai-subset": "1.3.6",
|
|
32
|
+
"@types/debug": "4.1.12",
|
|
33
|
+
"@types/mocha": "10.0.10",
|
|
34
|
+
"@types/node": "22.19.1",
|
|
35
|
+
"@typescript-eslint/eslint-plugin": "6.21.0",
|
|
36
|
+
"@typescript-eslint/parser": "6.21.0",
|
|
37
|
+
"cc": "3.0.1",
|
|
38
|
+
"chai": "6.2.1",
|
|
39
|
+
"chai-subset": "1.6.0",
|
|
40
|
+
"esbuild": "0.27.1",
|
|
41
|
+
"esbuild-register": "3.6.0",
|
|
42
|
+
"eslint": "8.57.1",
|
|
43
|
+
"mocha": "11.7.5",
|
|
44
|
+
"node-abi": "3.71.0",
|
|
45
|
+
"node-gyp": "11.0.0",
|
|
46
|
+
"nyc": "17.1.0",
|
|
47
|
+
"prebuildify": "6.0.1",
|
|
48
|
+
"prebuildify-cross": "5.1.1",
|
|
49
|
+
"semantic-release": "24.2.0",
|
|
50
|
+
"shx": "0.3.4",
|
|
51
|
+
"sinon": "21.0.0",
|
|
52
|
+
"typescript": "5.7.2"
|
|
53
|
+
},
|
|
54
|
+
"engines": {
|
|
55
|
+
"node": ">=18.0.0"
|
|
56
|
+
},
|
|
57
|
+
"scripts": {
|
|
58
|
+
"build": "rm -rf dist && tsc -p tsconfig-build.json",
|
|
59
|
+
"install": "node-gyp-build",
|
|
60
|
+
"prebuildify": "prebuildify --napi --target 20.0.0 --force --strip --verbose",
|
|
61
|
+
"prebuildify-cross": "prebuildify-cross --napi --target 20.0.0 --force --strip --verbose",
|
|
62
|
+
"rebuild": "node-gyp rebuild",
|
|
63
|
+
"format": "eslint lib test bin --fix",
|
|
64
|
+
"lint": "eslint lib test bin && cc --verbose",
|
|
65
|
+
"test": "nyc --reporter lcov --reporter text mocha",
|
|
66
|
+
"test:arduino": "TEST_PORT=$(./bin/find-arduino.ts) npm test",
|
|
67
|
+
"test:watch": "mocha -w",
|
|
68
|
+
"semantic-release": "semantic-release",
|
|
69
|
+
"typecheck": "tsc"
|
|
70
|
+
},
|
|
71
|
+
"publishConfig": {
|
|
72
|
+
"access": "public"
|
|
73
|
+
},
|
|
74
|
+
"license": "MIT",
|
|
75
|
+
"gypfile": true,
|
|
76
|
+
"cc": {
|
|
77
|
+
"filter": [
|
|
78
|
+
"legal/copyright",
|
|
79
|
+
"build/include"
|
|
80
|
+
],
|
|
81
|
+
"files": [
|
|
82
|
+
"src/*.cpp",
|
|
83
|
+
"src/*.h"
|
|
84
|
+
],
|
|
85
|
+
"linelength": "120"
|
|
86
|
+
},
|
|
87
|
+
"binary": {
|
|
88
|
+
"napi_versions": [
|
|
89
|
+
8
|
|
90
|
+
]
|
|
91
|
+
},
|
|
92
|
+
"repository": {
|
|
93
|
+
"type": "git",
|
|
94
|
+
"url": "git+https://github.com/serialport/bindings-cpp.git"
|
|
95
|
+
},
|
|
96
|
+
"funding": "https://opencollective.com/serialport/donate",
|
|
97
|
+
"changelog": {
|
|
98
|
+
"labels": {
|
|
99
|
+
"breaking": ":boom: BREAKING CHANGES :boom:",
|
|
100
|
+
"feature-request": "Features",
|
|
101
|
+
"bug": "Bug Fixes",
|
|
102
|
+
"docs": "Documentation",
|
|
103
|
+
"internal": "Chores"
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
"mocha": {
|
|
107
|
+
"bail": true,
|
|
108
|
+
"require": [
|
|
109
|
+
"esbuild-register"
|
|
110
|
+
],
|
|
111
|
+
"spec": "lib/**/*.test.*"
|
|
112
|
+
}
|
|
113
|
+
}
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
#include "./darwin_list.h"
|
|
2
|
+
|
|
3
|
+
#include <IOKit/IOKitLib.h>
|
|
4
|
+
#include <IOKit/IOCFPlugIn.h>
|
|
5
|
+
#include <IOKit/usb/IOUSBLib.h>
|
|
6
|
+
#include <IOKit/serial/IOSerialKeys.h>
|
|
7
|
+
|
|
8
|
+
#if defined(MAC_OS_X_VERSION_10_4) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4)
|
|
9
|
+
#include <sys/ioctl.h>
|
|
10
|
+
#include <IOKit/serial/ioss.h>
|
|
11
|
+
#endif
|
|
12
|
+
|
|
13
|
+
#include <string>
|
|
14
|
+
#include <list>
|
|
15
|
+
|
|
16
|
+
uv_mutex_t list_mutex;
|
|
17
|
+
Boolean lockInitialised = FALSE;
|
|
18
|
+
|
|
19
|
+
Napi::Value List(const Napi::CallbackInfo& info) {
|
|
20
|
+
Napi::Env env = info.Env();
|
|
21
|
+
// callback
|
|
22
|
+
if (!info[0].IsFunction()) {
|
|
23
|
+
Napi::TypeError::New(env, "First argument must be a function").ThrowAsJavaScriptException();
|
|
24
|
+
return env.Null();
|
|
25
|
+
}
|
|
26
|
+
Napi::Function callback = info[0].As<Napi::Function>();
|
|
27
|
+
ListBaton* baton = new ListBaton(callback);
|
|
28
|
+
snprintf(baton->errorString, sizeof(baton->errorString), "");
|
|
29
|
+
|
|
30
|
+
baton->Queue();
|
|
31
|
+
return env.Undefined();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
void setIfNotEmpty(Napi::Object item, std::string key, const char *value) {
|
|
35
|
+
Napi::Env env = item.Env();
|
|
36
|
+
Napi::String v8key = Napi::String::New(env, key);
|
|
37
|
+
if (strlen(value) > 0) {
|
|
38
|
+
(item).Set(v8key, Napi::String::New(env, value));
|
|
39
|
+
} else {
|
|
40
|
+
(item).Set(v8key, env.Undefined());
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
// Function prototypes
|
|
46
|
+
static kern_return_t FindModems(io_iterator_t *matchingServices);
|
|
47
|
+
static io_service_t GetUsbDevice(io_service_t service);
|
|
48
|
+
static stDeviceListItem* GetSerialDevices();
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
static kern_return_t FindModems(io_iterator_t *matchingServices) {
|
|
52
|
+
kern_return_t kernResult;
|
|
53
|
+
CFMutableDictionaryRef classesToMatch;
|
|
54
|
+
classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue);
|
|
55
|
+
if (classesToMatch != NULL) {
|
|
56
|
+
CFDictionarySetValue(classesToMatch,
|
|
57
|
+
CFSTR(kIOSerialBSDTypeKey),
|
|
58
|
+
CFSTR(kIOSerialBSDAllTypes));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
kernResult = IOServiceGetMatchingServices(kIOMasterPortDefault, classesToMatch, matchingServices);
|
|
62
|
+
|
|
63
|
+
return kernResult;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
static io_service_t GetUsbDevice(io_service_t service) {
|
|
67
|
+
IOReturn status;
|
|
68
|
+
io_iterator_t iterator = 0;
|
|
69
|
+
io_service_t device = 0;
|
|
70
|
+
|
|
71
|
+
if (!service) {
|
|
72
|
+
return device;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
status = IORegistryEntryCreateIterator(service,
|
|
76
|
+
kIOServicePlane,
|
|
77
|
+
(kIORegistryIterateParents | kIORegistryIterateRecursively),
|
|
78
|
+
&iterator);
|
|
79
|
+
|
|
80
|
+
if (status == kIOReturnSuccess) {
|
|
81
|
+
io_service_t currentService;
|
|
82
|
+
while ((currentService = IOIteratorNext(iterator)) && device == 0) {
|
|
83
|
+
io_name_t serviceName;
|
|
84
|
+
status = IORegistryEntryGetNameInPlane(currentService, kIOServicePlane, serviceName);
|
|
85
|
+
if (status == kIOReturnSuccess && IOObjectConformsTo(currentService, kIOUSBDeviceClassName)) {
|
|
86
|
+
device = currentService;
|
|
87
|
+
} else {
|
|
88
|
+
// Release the service object which is no longer needed
|
|
89
|
+
(void) IOObjectRelease(currentService);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Release the iterator
|
|
94
|
+
(void) IOObjectRelease(iterator);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return device;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
static void ExtractUsbInformation(stSerialDevice *serialDevice, IOUSBDeviceInterface **deviceInterface) {
|
|
101
|
+
kern_return_t kernResult;
|
|
102
|
+
UInt32 locationID;
|
|
103
|
+
kernResult = (*deviceInterface)->GetLocationID(deviceInterface, &locationID);
|
|
104
|
+
if (KERN_SUCCESS == kernResult) {
|
|
105
|
+
snprintf(serialDevice->locationId, sizeof(serialDevice->locationId), "%08x", locationID);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
UInt16 vendorID;
|
|
109
|
+
kernResult = (*deviceInterface)->GetDeviceVendor(deviceInterface, &vendorID);
|
|
110
|
+
if (KERN_SUCCESS == kernResult) {
|
|
111
|
+
snprintf(serialDevice->vendorId, sizeof(serialDevice->vendorId), "%04x", vendorID);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
UInt16 productID;
|
|
115
|
+
kernResult = (*deviceInterface)->GetDeviceProduct(deviceInterface, &productID);
|
|
116
|
+
if (KERN_SUCCESS == kernResult) {
|
|
117
|
+
snprintf(serialDevice->productId, sizeof(serialDevice->productId), "%04x", productID);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
static stDeviceListItem* GetSerialDevices() {
|
|
122
|
+
char bsdPath[MAXPATHLEN];
|
|
123
|
+
|
|
124
|
+
io_iterator_t serialPortIterator;
|
|
125
|
+
FindModems(&serialPortIterator);
|
|
126
|
+
|
|
127
|
+
kern_return_t kernResult = KERN_FAILURE;
|
|
128
|
+
Boolean modemFound = false;
|
|
129
|
+
|
|
130
|
+
// Initialize the returned path
|
|
131
|
+
*bsdPath = '\0';
|
|
132
|
+
|
|
133
|
+
stDeviceListItem* devices = NULL;
|
|
134
|
+
stDeviceListItem* lastDevice = NULL;
|
|
135
|
+
int length = 0;
|
|
136
|
+
|
|
137
|
+
io_service_t modemService;
|
|
138
|
+
while ((modemService = IOIteratorNext(serialPortIterator))) {
|
|
139
|
+
CFTypeRef bsdPathAsCFString;
|
|
140
|
+
bsdPathAsCFString = IORegistryEntrySearchCFProperty(
|
|
141
|
+
modemService,
|
|
142
|
+
kIOServicePlane,
|
|
143
|
+
CFSTR(kIODialinDeviceKey),
|
|
144
|
+
kCFAllocatorDefault,
|
|
145
|
+
kIORegistryIterateRecursively);
|
|
146
|
+
|
|
147
|
+
if (bsdPathAsCFString) {
|
|
148
|
+
Boolean result;
|
|
149
|
+
|
|
150
|
+
// Convert the path from a CFString to a C (NUL-terminated)
|
|
151
|
+
result = CFStringGetCString((CFStringRef) bsdPathAsCFString,
|
|
152
|
+
bsdPath,
|
|
153
|
+
sizeof(bsdPath),
|
|
154
|
+
kCFStringEncodingUTF8);
|
|
155
|
+
CFRelease(bsdPathAsCFString);
|
|
156
|
+
|
|
157
|
+
if (result) {
|
|
158
|
+
stDeviceListItem *deviceListItem = reinterpret_cast<stDeviceListItem*>( malloc(sizeof(stDeviceListItem)));
|
|
159
|
+
stSerialDevice *serialDevice = &(deviceListItem->value);
|
|
160
|
+
snprintf(serialDevice->port, sizeof(serialDevice->port), "%s", bsdPath);
|
|
161
|
+
memset(serialDevice->locationId, 0, sizeof(serialDevice->locationId));
|
|
162
|
+
memset(serialDevice->vendorId, 0, sizeof(serialDevice->vendorId));
|
|
163
|
+
memset(serialDevice->productId, 0, sizeof(serialDevice->productId));
|
|
164
|
+
serialDevice->manufacturer[0] = '\0';
|
|
165
|
+
serialDevice->serialNumber[0] = '\0';
|
|
166
|
+
deviceListItem->next = NULL;
|
|
167
|
+
deviceListItem->length = &length;
|
|
168
|
+
|
|
169
|
+
if (devices == NULL) {
|
|
170
|
+
devices = deviceListItem;
|
|
171
|
+
} else {
|
|
172
|
+
lastDevice->next = deviceListItem;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
lastDevice = deviceListItem;
|
|
176
|
+
length++;
|
|
177
|
+
|
|
178
|
+
modemFound = true;
|
|
179
|
+
kernResult = KERN_SUCCESS;
|
|
180
|
+
|
|
181
|
+
uv_mutex_lock(&list_mutex);
|
|
182
|
+
|
|
183
|
+
io_service_t device = GetUsbDevice(modemService);
|
|
184
|
+
|
|
185
|
+
if (device) {
|
|
186
|
+
CFStringRef manufacturerAsCFString = (CFStringRef) IORegistryEntryCreateCFProperty(device,
|
|
187
|
+
CFSTR(kUSBVendorString),
|
|
188
|
+
kCFAllocatorDefault,
|
|
189
|
+
0);
|
|
190
|
+
|
|
191
|
+
if (manufacturerAsCFString) {
|
|
192
|
+
Boolean result;
|
|
193
|
+
char manufacturer[MAXPATHLEN];
|
|
194
|
+
|
|
195
|
+
// Convert from a CFString to a C (NUL-terminated)
|
|
196
|
+
result = CFStringGetCString(manufacturerAsCFString,
|
|
197
|
+
manufacturer,
|
|
198
|
+
sizeof(manufacturer),
|
|
199
|
+
kCFStringEncodingUTF8);
|
|
200
|
+
|
|
201
|
+
if (result) {
|
|
202
|
+
snprintf(serialDevice->manufacturer, sizeof(serialDevice->manufacturer), "%s", manufacturer);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
CFRelease(manufacturerAsCFString);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
CFStringRef serialNumberAsCFString = (CFStringRef) IORegistryEntrySearchCFProperty(device,
|
|
209
|
+
kIOServicePlane,
|
|
210
|
+
CFSTR(kUSBSerialNumberString),
|
|
211
|
+
kCFAllocatorDefault,
|
|
212
|
+
kIORegistryIterateRecursively);
|
|
213
|
+
|
|
214
|
+
if (serialNumberAsCFString) {
|
|
215
|
+
Boolean result;
|
|
216
|
+
char serialNumber[MAXPATHLEN];
|
|
217
|
+
|
|
218
|
+
// Convert from a CFString to a C (NUL-terminated)
|
|
219
|
+
result = CFStringGetCString(serialNumberAsCFString,
|
|
220
|
+
serialNumber,
|
|
221
|
+
sizeof(serialNumber),
|
|
222
|
+
kCFStringEncodingUTF8);
|
|
223
|
+
|
|
224
|
+
if (result) {
|
|
225
|
+
snprintf(serialDevice->serialNumber, sizeof(serialDevice->serialNumber), "%s", serialNumber);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
CFRelease(serialNumberAsCFString);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
IOCFPlugInInterface **plugInInterface = NULL;
|
|
232
|
+
SInt32 score;
|
|
233
|
+
HRESULT res;
|
|
234
|
+
|
|
235
|
+
IOUSBDeviceInterface **deviceInterface = NULL;
|
|
236
|
+
|
|
237
|
+
kernResult = IOCreatePlugInInterfaceForService(device, kIOUSBDeviceUserClientTypeID, kIOCFPlugInInterfaceID,
|
|
238
|
+
&plugInInterface, &score);
|
|
239
|
+
|
|
240
|
+
if ((kIOReturnSuccess == kernResult) && plugInInterface) {
|
|
241
|
+
// Use the plugin interface to retrieve the device interface.
|
|
242
|
+
res = (*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID),
|
|
243
|
+
reinterpret_cast<LPVOID*> (&deviceInterface));
|
|
244
|
+
|
|
245
|
+
// Now done with the plugin interface.
|
|
246
|
+
(*plugInInterface)->Release(plugInInterface);
|
|
247
|
+
|
|
248
|
+
if (!res && deviceInterface != NULL) {
|
|
249
|
+
// Extract the desired Information
|
|
250
|
+
ExtractUsbInformation(serialDevice, deviceInterface);
|
|
251
|
+
|
|
252
|
+
// Release the Interface
|
|
253
|
+
(*deviceInterface)->Release(deviceInterface);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// Release the device
|
|
258
|
+
(void) IOObjectRelease(device);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
uv_mutex_unlock(&list_mutex);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// Release the io_service_t now that we are done with it.
|
|
266
|
+
(void) IOObjectRelease(modemService);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
IOObjectRelease(serialPortIterator); // Release the iterator.
|
|
270
|
+
|
|
271
|
+
return devices;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
void ListBaton::Execute() {
|
|
275
|
+
|
|
276
|
+
if (!lockInitialised) {
|
|
277
|
+
uv_mutex_init(&list_mutex);
|
|
278
|
+
lockInitialised = TRUE;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
stDeviceListItem* devices = GetSerialDevices();
|
|
282
|
+
if (devices != NULL && *(devices->length) > 0) {
|
|
283
|
+
stDeviceListItem* next = devices;
|
|
284
|
+
|
|
285
|
+
for (int i = 0, len = *(devices->length); i < len; i++) {
|
|
286
|
+
stSerialDevice device = (* next).value;
|
|
287
|
+
|
|
288
|
+
ListResultItem* resultItem = new ListResultItem();
|
|
289
|
+
resultItem->path = device.port;
|
|
290
|
+
|
|
291
|
+
if (*device.locationId) {
|
|
292
|
+
resultItem->locationId = device.locationId;
|
|
293
|
+
}
|
|
294
|
+
if (*device.vendorId) {
|
|
295
|
+
resultItem->vendorId = device.vendorId;
|
|
296
|
+
}
|
|
297
|
+
if (*device.productId) {
|
|
298
|
+
resultItem->productId = device.productId;
|
|
299
|
+
}
|
|
300
|
+
if (*device.manufacturer) {
|
|
301
|
+
resultItem->manufacturer = device.manufacturer;
|
|
302
|
+
}
|
|
303
|
+
if (*device.serialNumber) {
|
|
304
|
+
resultItem->serialNumber = device.serialNumber;
|
|
305
|
+
}
|
|
306
|
+
results.push_back(resultItem);
|
|
307
|
+
|
|
308
|
+
stDeviceListItem* current = next;
|
|
309
|
+
|
|
310
|
+
if (next->next != NULL) {
|
|
311
|
+
next = next->next;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
free(current);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|