Skip to content

Commit

Permalink
feat(linter): Added 'cc' to lint the C++ code on 'npm lint' (#1501)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanSanchez authored and reconbot committed Feb 28, 2018
1 parent cf5b8fb commit 59960a3
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 64 deletions.
16 changes: 14 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
},
"devDependencies": {
"bluebird": "^3.5.0",
"cc": "^1.0.1",
"chai": "^4.0.2",
"chai-subset": "^1.5.0",
"conventional-changelog-cli": "^1.3.2",
Expand Down Expand Up @@ -97,7 +98,7 @@
"arduino-test": "TEST_PORT=$(./bin/find-arduino.js) npm test",
"changelog": "conventional-changelog -i CHANGELOG.md -s",
"docs": "jsdoc -c ./.jsdoc.json ",
"lint": "eslint lib test bin examples",
"lint": "eslint lib test bin examples && cc",
"rebuild-all": "npm rebuild && node-gyp rebuild",
"repl": "node bin/repl.js",
"terminal": "node bin/terminal.js",
Expand All @@ -111,5 +112,16 @@
"prebuild": "prebuild --all --strip --verbose",
"prebuild-upload": "prebuild --all --strip --verbose"
},
"gypfile": true
"gypfile": true,
"cc": {
"filter": [
"legal/copyright",
"build/include"
],
"files": [
"src/*.cpp",
"src/*.h"
],
"linelength": "120"
}
}
21 changes: 12 additions & 9 deletions src/darwin_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#include <IOKit/serial/ioss.h>
#endif

#include <string>
#include <list>

uv_mutex_t list_mutex;
Boolean lockInitialised = FALSE;

Expand All @@ -21,7 +24,7 @@ NAN_METHOD(List) {
}

ListBaton* baton = new ListBaton();
strcpy(baton->errorString, "");
snprintf(baton->errorString, sizeof(baton->errorString), "");
baton->callback.Reset(info[0].As<v8::Function>());

uv_work_t* req = new uv_work_t();
Expand Down Expand Up @@ -99,19 +102,19 @@ static void ExtractUsbInformation(stSerialDevice *serialDevice, IOUSBDeviceInter
UInt32 locationID;
kernResult = (*deviceInterface)->GetLocationID(deviceInterface, &locationID);
if (KERN_SUCCESS == kernResult) {
snprintf(serialDevice->locationId, 11, "%08x", locationID);
snprintf(serialDevice->locationId, sizeof(serialDevice->locationId), "%08x", locationID);
}

UInt16 vendorID;
kernResult = (*deviceInterface)->GetDeviceVendor(deviceInterface, &vendorID);
if (KERN_SUCCESS == kernResult) {
snprintf(serialDevice->vendorId, 7, "%04x", vendorID);
snprintf(serialDevice->vendorId, sizeof(serialDevice->vendorId), "%04x", vendorID);
}

UInt16 productID;
kernResult = (*deviceInterface)->GetDeviceProduct(deviceInterface, &productID);
if (KERN_SUCCESS == kernResult) {
snprintf(serialDevice->productId, 7, "%04x", productID);
snprintf(serialDevice->productId, sizeof(serialDevice->productId), "%04x", productID);
}
}

Expand Down Expand Up @@ -152,9 +155,9 @@ static stDeviceListItem* GetSerialDevices() {
CFRelease(bsdPathAsCFString);

if (result) {
stDeviceListItem *deviceListItem = (stDeviceListItem*) malloc(sizeof(stDeviceListItem));
stDeviceListItem *deviceListItem = reinterpret_cast<stDeviceListItem*>( malloc(sizeof(stDeviceListItem)));
stSerialDevice *serialDevice = &(deviceListItem->value);
strcpy(serialDevice->port, bsdPath);
snprintf(serialDevice->port, sizeof(serialDevice->port), bsdPath);
memset(serialDevice->locationId, 0, sizeof(serialDevice->locationId));
memset(serialDevice->vendorId, 0, sizeof(serialDevice->vendorId));
memset(serialDevice->productId, 0, sizeof(serialDevice->productId));
Expand Down Expand Up @@ -196,7 +199,7 @@ static stDeviceListItem* GetSerialDevices() {
kCFStringEncodingUTF8);

if (result) {
strcpy(serialDevice->manufacturer, manufacturer);
snprintf(serialDevice->manufacturer, sizeof(serialDevice->manufacturer), manufacturer);
}

CFRelease(manufacturerAsCFString);
Expand All @@ -219,7 +222,7 @@ static stDeviceListItem* GetSerialDevices() {
kCFStringEncodingUTF8);

if (result) {
strcpy(serialDevice->serialNumber, serialNumber);
snprintf(serialDevice->serialNumber, sizeof(serialDevice->serialNumber), serialNumber);
}

CFRelease(serialNumberAsCFString);
Expand All @@ -240,7 +243,7 @@ static stDeviceListItem* GetSerialDevices() {

// Use the plugin interface to retrieve the device interface.
res = (*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID),
(LPVOID*) &deviceInterface);
reinterpret_cast<LPVOID*> (&deviceInterface));

// Now done with the plugin interface.
(*plugInInterface)->Release(plugInInterface);
Expand Down
9 changes: 5 additions & 4 deletions src/darwin_list.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#ifndef SRC_SERIALPORT_DARWIN_LIST_H_
#define SRC_SERIALPORT_DARWIN_LIST_H_
#ifndef SRC_DARWIN_LIST_H_
#define SRC_DARWIN_LIST_H_
#include <sys/param.h> // For MAXPATHLEN
#include <nan.h>
#include <list>
#include <sys/param.h> // For MAXPATHLEN
#include <string>

#define ERROR_STRING_SIZE 1024

Expand Down Expand Up @@ -41,4 +42,4 @@ typedef struct DeviceListItem {
int* length;
} stDeviceListItem;

#endif // SRC_SERIALPORT_DARWIN_LIST_H_
#endif // SRC_DARWIN_LIST_H_
2 changes: 1 addition & 1 deletion src/poller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Poller::~Poller() {
// if we call uv_poll_stop after uv_poll_init failed we segfault
if (uv_poll_init_success) {
uv_poll_stop(poll_handle);
uv_close((uv_handle_t*) poll_handle, Poller::onClose);
uv_close(reinterpret_cast<uv_handle_t*> (poll_handle), Poller::onClose);
} else {
delete poll_handle;
}
Expand Down
2 changes: 1 addition & 1 deletion src/serialport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ NAN_METHOD(Open) {
}

OpenBaton* baton = new OpenBaton();
strcpy(baton->path, *path);
snprintf(baton->path, sizeof(baton->path), *path);
baton->baudRate = getIntFromObject(options, "baudRate");
baton->dataBits = getIntFromObject(options, "dataBits");
baton->parity = ToParityEnum(getStringFromObj(options, "parity"));
Expand Down
8 changes: 4 additions & 4 deletions src/serialport_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
int linuxSetCustomBaudRate(const int fd, const unsigned int baudrate) {
struct termios2 t;

if(ioctl(fd, TCGETS2, &t)) {
if (ioctl(fd, TCGETS2, &t)) {
return -1;
}

t.c_cflag &= ~CBAUD;
t.c_cflag |= BOTHER;
t.c_ospeed = t.c_ispeed = baudrate;

if(ioctl(fd, TCSETS2, &t)) {
if (ioctl(fd, TCSETS2, &t)) {
return -2;
}

Expand All @@ -27,11 +27,11 @@ int linuxSetCustomBaudRate(const int fd, const unsigned int baudrate) {
int linuxGetSystemBaudRate(const int fd, int* const outbaud) {
struct termios2 t;

if(ioctl(fd, TCGETS2, &t)) {
if (ioctl(fd, TCGETS2, &t)) {
return -1;
}

*outbaud = (int)t.c_ospeed;
*outbaud = static_cast<int>(t.c_ospeed);

return 0;
}
Expand Down
6 changes: 3 additions & 3 deletions src/serialport_linux.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef CUSTOM_BAUDRATE_H
#define CUSTOM_BAUDRATE_H
#ifndef SRC_SERIALPORT_LINUX_H_
#define SRC_SERIALPORT_LINUX_H_

int linuxSetCustomBaudRate(const int fd, const unsigned int baudrate);
int linuxGetSystemBaudRate(const int fd, int* const outbaud);

#endif
#endif // SRC_SERIALPORT_LINUX_H_

35 changes: 20 additions & 15 deletions src/serialport_unix.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "./serialport_unix.h"
#include "./serialport.h"
#include "serialport_unix.h"
#include "serialport.h"

#include <sys/file.h>
#include <unistd.h>
Expand All @@ -15,13 +15,11 @@
#if defined(MAC_OS_X_VERSION_10_4) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4)
#include <sys/ioctl.h>
#include <IOKit/serial/ioss.h>
#endif

#if defined(__OpenBSD__)
#elif defined(__OpenBSD__)
#include <sys/ioctl.h>
#endif

#if defined(__linux__)
#elif defined(__linux__)
#include <sys/ioctl.h>
#include <linux/serial.h>
#include "serialport_linux.h"
Expand Down Expand Up @@ -105,7 +103,8 @@ int setBaudRate(ConnectionOptionsBaton *data) {
// get port options
struct termios options;
if (-1 == tcgetattr(fd, &options)) {
snprintf(data->errorString, sizeof(data->errorString), "Error: %s setting custom baud rate of %d", strerror(errno), data->baudRate);
snprintf(data->errorString, sizeof(data->errorString),
"Error: %s setting custom baud rate of %d", strerror(errno), data->baudRate);
return -1;
}

Expand All @@ -115,10 +114,12 @@ int setBaudRate(ConnectionOptionsBaton *data) {
int err = linuxSetCustomBaudRate(fd, data->baudRate);

if (err == -1) {
snprintf(data->errorString, sizeof(data->errorString), "Error: %s || while retrieving termios2 info", strerror(errno));
snprintf(data->errorString, sizeof(data->errorString),
"Error: %s || while retrieving termios2 info", strerror(errno));
return -1;
} else if(err == -2) {
snprintf(data->errorString, sizeof(data->errorString), "Error: %s || while setting custom baud rate of %d", strerror(errno), data->baudRate);
} else if (err == -2) {
snprintf(data->errorString, sizeof(data->errorString),
"Error: %s || while setting custom baud rate of %d", strerror(errno), data->baudRate);
return -1;
}

Expand All @@ -131,7 +132,8 @@ int setBaudRate(ConnectionOptionsBaton *data) {
if (-1 == baudRate) {
speed_t speed = data->baudRate;
if (-1 == ioctl(fd, IOSSIOSPEED, &speed)) {
snprintf(data->errorString, sizeof(data->errorString), "Error: %s calling ioctl(.., IOSSIOSPEED, %ld )", strerror(errno), speed );
snprintf(data->errorString, sizeof(data->errorString),
"Error: %s calling ioctl(.., IOSSIOSPEED, %ld )", strerror(errno), speed);
return -1;
} else {
tcflush(fd, TCIOFLUSH);
Expand All @@ -141,7 +143,8 @@ int setBaudRate(ConnectionOptionsBaton *data) {
#endif

if (-1 == baudRate) {
snprintf(data->errorString, sizeof(data->errorString), "Error baud rate of %d is not supported on your platform", data->baudRate);
snprintf(data->errorString, sizeof(data->errorString),
"Error baud rate of %d is not supported on your platform", data->baudRate);
return -1;
}

Expand All @@ -163,7 +166,8 @@ void EIO_Update(uv_work_t* req) {
int setup(int fd, OpenBaton *data) {
int dataBits = ToDataBitsConstant(data->dataBits);
if (-1 == dataBits) {
snprintf(data->errorString, sizeof(data->errorString), "Invalid data bits setting %d", data->dataBits);
snprintf(data->errorString, sizeof(data->errorString),
"Invalid data bits setting %d", data->dataBits);
return -1;
}

Expand Down Expand Up @@ -299,7 +303,8 @@ void EIO_Close(uv_work_t* req) {
VoidBaton* data = static_cast<VoidBaton*>(req->data);

if (-1 == close(data->fd)) {
snprintf(data->errorString, sizeof(data->errorString), "Error: %s, unable to close fd %d", strerror(errno), data->fd);
snprintf(data->errorString, sizeof(data->errorString),
"Error: %s, unable to close fd %d", strerror(errno), data->fd);
}
}

Expand Down Expand Up @@ -370,7 +375,7 @@ void EIO_GetBaudRate(uv_work_t* req) {
}
#endif

// TODO implement on mac
// TODO(Fumon) implement on mac
#if defined(MAC_OS_X_VERSION_10_4) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4)
snprintf(data->errorString, sizeof(data->errorString), "Error: System baud rate check not implemented on darwin");
return;
Expand Down
Loading

0 comments on commit 59960a3

Please sign in to comment.