Skip to content

Commit 89cdccd

Browse files
authored
Update note-c subtree (#97)
* remove note-c before re-add * Squashed 'src/note-c/' content from commit 5c65c34 git-subtree-dir: src/note-c git-subtree-split: 5c65c345f6eb2e3663800337d1b5c6370736ee1a
1 parent b860326 commit 89cdccd

File tree

81 files changed

+4704
-234
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+4704
-234
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
2+
// https://github.com/microsoft/vscode-dev-containers/tree/v0.205.2/containers/docker-existing-dockerfile
3+
{
4+
"name": "Note-C Development Environment Dockerfile",
5+
6+
// Sets the run context to one level up instead of the .devcontainer folder.
7+
"context": "..",
8+
9+
// Update the 'dockerFile' property if you aren't using the standard 'Dockerfile' filename.
10+
//"dockerFile": "../.github/actions/compile-examples/Dockerfile",
11+
"dockerFile": "../.github/actions/run_unit_tests/Dockerfile",
12+
13+
// Set *default* container specific settings.json values on container create.
14+
"settings": {},
15+
16+
// Add the IDs of extensions you want installed when the container is created.
17+
"extensions": [
18+
"ms-vscode.cpptools",
19+
"shardulm94.trailing-spaces",
20+
"twxs.cmake"
21+
],
22+
23+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
24+
// "forwardPorts": [],
25+
26+
// Uncomment the next line to run commands after the container is created - for example installing curl.
27+
// "postCreateCommand": "apt-get update && apt-get install -y curl",
28+
29+
// Uncomment when using a ptrace-based debugger like C++, Go, and Rust
30+
// "runArgs": [
31+
// "--device=/dev/bus/usb/"
32+
// ],
33+
34+
// Uncomment to use the Docker CLI from inside the container. See https://aka.ms/vscode-remote/samples/docker-from-docker.
35+
// "mounts": [ "source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind" ],
36+
37+
// Uncomment to connect as a non-root user if you've added one. See https://aka.ms/vscode-remote/containers/non-root.
38+
"remoteUser": "blues"
39+
}

src/note-c/.github/actions/run_unit_tests/Dockerfile

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,53 @@
66
# Launch development environment (mount source root as /note-c/)
77
# $ docker run --rm --volume $(pwd)/../../../:/note-c/ --workdir /note-c/ note_c_run_unit_tests
88

9+
# Global Argument(s)
10+
ARG DEBIAN_FRONTEND="noninteractive"
11+
ARG UID=1000
12+
ARG USER="blues"
13+
914
# POSIX compatible (Linux/Unix) base image.
1015
FROM debian:stable-slim
1116

17+
# Import Global Argument(s)
18+
ARG DEBIAN_FRONTEND
19+
ARG UID
20+
ARG USER
21+
22+
# Local Argument(s)
23+
24+
# Create Non-Root User
25+
RUN ["dash", "-c", "\
26+
addgroup \
27+
--gid ${UID} \
28+
\"${USER}\" \
29+
&& adduser \
30+
--disabled-password \
31+
--gecos \"\" \
32+
--ingroup \"${USER}\" \
33+
--uid ${UID} \
34+
\"${USER}\" \
35+
&& usermod \
36+
--append \
37+
--groups \"dialout,plugdev\" \
38+
\"${USER}\" \
39+
"]
40+
1241
# Install whatever dependencies we can via apt-get.
1342
RUN ["dash", "-c", "\
1443
apt-get update --quiet \
1544
&& apt-get install --assume-yes --no-install-recommends --quiet \
16-
build-essential \
45+
astyle \
1746
ca-certificates \
1847
curl \
48+
g++ \
49+
gcc \
50+
gdb \
51+
git \
1952
lcov \
53+
make \
54+
nano \
55+
valgrind \
2056
&& apt-get clean \
2157
&& apt-get purge \
2258
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
@@ -40,4 +76,4 @@ RUN ["dash", "-c", "\
4076
&& rm -rf Catch2-3.2.1 v3.2.1.tar.gz \
4177
"]
4278

43-
ENTRYPOINT ["./scripts/run_unit_tests.sh", "--coverage"]
79+
ENTRYPOINT ["./scripts/run_unit_tests.sh", "--coverage", "--mem-check"]

src/note-c/.github/workflows/ci.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ jobs:
1414
uses: actions/checkout@v3
1515
- name: Run unit tests
1616
uses: ./.github/actions/run_unit_tests
17+
- name: Coveralls Action Bug Workaround
18+
id: coveralls_bug_workaround
19+
run: sudo sed -i 's/github\/workspace/home\/runner\/work\/note-c\/note-c/g' ./build/test/coverage/lcov.info
20+
- name: Publish Test Coverage
21+
id: publish_coverage
22+
uses: coverallsapp/github-action@master
23+
with:
24+
github-token: ${{ secrets.GITHUB_TOKEN }}
25+
path-to-lcov: ./build/test/coverage/lcov.info
1726
run_astyle:
1827
runs-on: ubuntu-latest
1928
steps:

src/note-c/.vscode/tasks.json

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "Note-C: Compile and Run ALL Tests",
6+
"type": "cppbuild",
7+
"command": "./scripts/run_unit_tests.sh",
8+
"args": [],
9+
"options": {
10+
"cwd": "${workspaceFolder}"
11+
},
12+
"problemMatcher": [
13+
"$gcc"
14+
],
15+
"group": {
16+
"kind": "build",
17+
"isDefault": true
18+
}
19+
},
20+
{
21+
"label": "Note-C: Compile and Run ALL Tests (with coverage and memory check)",
22+
"type": "cppbuild",
23+
"command": "./scripts/run_unit_tests.sh",
24+
"args": [
25+
"--coverage",
26+
"--mem-check"
27+
],
28+
"options": {
29+
"cwd": "${workspaceFolder}"
30+
},
31+
"problemMatcher": [
32+
"$gcc"
33+
],
34+
"group": {
35+
"kind": "build",
36+
"isDefault": false
37+
}
38+
},
39+
{
40+
"label": "Note-C: Run `astyle` Formatter",
41+
"type": "shell",
42+
"command": "./scripts/run_astyle.sh",
43+
"args": [],
44+
"options": {
45+
"cwd": "${workspaceFolder}",
46+
"env": {
47+
"LC_ALL":"C"
48+
}
49+
},
50+
"problemMatcher": [
51+
"$gcc"
52+
],
53+
"group": {
54+
"kind": "none"
55+
}
56+
}
57+
]
58+
}

src/note-c/CMakeLists.txt

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,10 @@ if(NOT EXISTS ${PROJECT_BINARY_DIR}/.gitignore)
1414
file(WRITE ${PROJECT_BINARY_DIR}/.gitignore "*")
1515
endif()
1616

17-
option(BUILD_TESTS "Build tests." ON)
18-
option(BUILD_SHARED_LIBS "Build note-c as a shared library." ON)
17+
option(BUILD_TESTS "Build tests." OFF)
18+
option(BUILD_SHARED_LIBS "Build note-c as a shared library." OFF)
1919
option(COVERAGE "Compile for test coverage reporting." OFF)
20-
21-
add_compile_options(
22-
-Wall
23-
-Wextra
24-
-Wpedantic
25-
-Werror
26-
)
20+
option(MEM_CHECK "Run tests with Valgrind." OFF)
2721

2822
set(NOTE_C_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR})
2923
add_library(
@@ -44,16 +38,37 @@ add_library(
4438
${NOTE_C_SRC_DIR}/n_request.c
4539
${NOTE_C_SRC_DIR}/n_str.c
4640
)
41+
target_compile_options(
42+
note_c
43+
PRIVATE
44+
-Wall
45+
-Wextra
46+
-Wpedantic
47+
-Werror
48+
)
49+
target_include_directories(
50+
note_c
51+
PUBLIC ${NOTE_C_SRC_DIR}
52+
)
4753

4854
if(BUILD_TESTS)
49-
add_compile_definitions(
50-
TEST
51-
)
52-
53-
# Including this here rather than in test/CMakeLists.txt allows us to run
55+
# Including CTest here rather than in test/CMakeLists.txt allows us to run
5456
# ctest from the root build directory (e.g. build/ instead of build/test/).
57+
# We also need to set MEMORYCHECK_COMMAND_OPTIONS before including this.
58+
# See: https://stackoverflow.com/a/60741757
59+
if(MEM_CHECK)
60+
# Go ahead and make sure we can find valgrind while we're here.
61+
find_program(VALGRIND valgrind REQUIRED)
62+
message(STATUS "Found valgrind: ${VALGRIND}")
63+
set(MEMORYCHECK_COMMAND_OPTIONS "--leak-check=full --error-exitcode=1")
64+
endif(MEM_CHECK)
5565
include(CTest)
5666

67+
target_compile_definitions(
68+
note_c
69+
PUBLIC TEST
70+
)
71+
5772
# If we don't weaken the functions we're mocking in the tests, the linker
5873
# will complain about multiple function definitions: the mocked one and the
5974
# "real" one from note-c. Weakening the real function causes the mock
@@ -82,7 +97,21 @@ if(BUILD_TESTS)
8297
NoteDebug;
8398
NotePrint;
8499
NoteNewCommand;
85-
NoteRequest"
100+
NoteRequest;
101+
JCreateObject;
102+
NoteTransactionStart;
103+
NoteUserAgent;
104+
serialNoteReset;
105+
serialNoteTransaction;
106+
i2cNoteReset;
107+
i2cNoteTransaction;
108+
JCreateStringValue;
109+
NoteSetEnvDefault;
110+
NoteSleep;
111+
NotePayloadRetrieveAfterSleep;
112+
NoteTimeValidST;
113+
NoteTimeST;
114+
NoteRegion"
86115
)
87116
foreach(MOCKED_FN ${MOCKED_FNS})
88117
string(APPEND OBJCOPY_WEAKEN "-W ${MOCKED_FN} ")

src/note-c/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[![Coverage Status](https://coveralls.io/repos/github/blues/note-c/badge.svg?branch=master)](https://coveralls.io/github/blues/note-c?branch=master)
2+
13
# note-c
24

35
The note-c C library for communicating with the

src/note-c/n_cjson_helpers.c

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ bool JGetBinaryFromObject(J *rsp, const char *fieldName, uint8_t **retBinaryData
386386
/*!
387387
@brief Get the object name.
388388
@param item The JSON object.
389-
@returns The number, or and empty string, if NULL.
389+
@returns The name, or the empty string, if NULL.
390390
*/
391391
/**************************************************************************/
392392
const char *JGetItemName(const J * item)
@@ -399,10 +399,10 @@ const char *JGetItemName(const J * item)
399399

400400
//**************************************************************************/
401401
/*!
402-
@brief Convert an integer to text
403-
@param an integer
404-
@returns The number converted to text, null-terminated.
405-
@note The buffer midt be large enough because no bounds checking is done.
402+
@brief Convert an integer to text.
403+
@param n The integer to convert.
404+
@param s The buffer to hold the text.
405+
@note The buffer must be large enough because no bounds checking is done.
406406
*/
407407
/**************************************************************************/
408408
void JItoA(long int n, char *s)
@@ -430,7 +430,7 @@ void JItoA(long int n, char *s)
430430
//**************************************************************************/
431431
/*!
432432
@brief Convert text to an integer
433-
@param a null-terminated text buffer
433+
@param string A null-terminated text buffer
434434
@returns An integer, or 0 if invalid
435435
*/
436436
/**************************************************************************/
@@ -467,15 +467,18 @@ long int JAtoI(const char *string)
467467
//**************************************************************************/
468468
/*!
469469
@brief Convert a buffer/len to a null-terminated c-string
470-
@param a buffer containing text with a counted length
471-
@returns A c-string (NULL if invalid) that must be freed with JFree()
470+
@param buffer A buffer containing the bytes to convert.
471+
@param len The length of buffer in bytes.
472+
@returns If buffer is NULL or length 0, the empty string. If allocation
473+
fails, NULL. On success, the converted c-string. The returned
474+
string must be freed with NoteFree.
472475
*/
473476
/**************************************************************************/
474477
char *JAllocString(uint8_t *buffer, uint32_t len)
475478
{
476479
char *buf = _Malloc(len+1);
477480
if (buf == NULL) {
478-
return false;
481+
return NULL;
479482
}
480483
if (len > 0) {
481484
memcpy(buf, buffer, len);
@@ -517,10 +520,11 @@ const char *JType(J *item)
517520

518521
//**************************************************************************/
519522
/*!
520-
@brief Return the type of an item, as an int usable in a switch statement
521-
@param json object
522-
@param field within the json object
523-
@returns The type
523+
@brief Get the type of a field, as an int usable in a switch statement.
524+
@param rsp The JSON object containing the field.
525+
@param field The field's name.
526+
@returns The type of the field on success. JTYPE_NOT_PRESENT on error or if
527+
the field doesn't exist.
524528
*/
525529
/**************************************************************************/
526530
int JGetType(J *rsp, const char *field)

0 commit comments

Comments
 (0)