Skip to content

Commit 83a0db0

Browse files
committed
Merge branch 'pure_c' and set version to v1.2
2 parents 01be2a1 + eb619d9 commit 83a0db0

File tree

9 files changed

+800
-418
lines changed

9 files changed

+800
-418
lines changed

.github/workflows/BuildTest.yml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ on:
1515
- 'src/**'
1616
- 'libs/**'
1717
- '.github/workflows/*.yml'
18-
- 'CMakeList.txt'
18+
- 'CMakeLists.txt'
1919

2020
pull_request:
2121
paths:
@@ -55,19 +55,19 @@ jobs:
5555
# The matrix will produce one job for each configuration:
5656
matrix:
5757
config:
58-
- name: 'Linux'
58+
- name: 'Linux x86_64'
5959
os: 'ubuntu-latest'
6060
bindir: 'echo "BIN_DIR=$BUILD_DIR" >> $GITHUB_ENV'
6161
static: '*.a'
6262
dynamic: '*.so'
6363

64-
- name: 'MacOS'
64+
- name: 'MacOS x86_64'
6565
os: 'macos-latest'
6666
bindir: 'echo "BIN_DIR=$BUILD_DIR" >> $GITHUB_ENV'
6767
static: '*.a'
6868
dynamic: '*.dylib'
6969

70-
- name: 'Windows'
70+
- name: 'Windows x86_64'
7171
os: 'windows-latest'
7272
bindir: 'echo "BIN_DIR=$env:BUILD_DIR/$Env:BUILD_TYPE" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf-8 -Append'
7373
static: '*.lib'
@@ -90,29 +90,38 @@ jobs:
9090
working-directory: ${{ env.BUILD_DIR }}
9191
run: cmake .. -DCMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }}
9292

93-
- name: Build Library
93+
- name: Build PiCode Library
9494
working-directory: ${{ env.BUILD_DIR }}
9595
run: cmake --build . --config ${{ env.BUILD_TYPE }}
9696

97-
- name: Build Test ${{ env.TARGET_TEST }}
97+
- name: Build C++ test ${{ env.TARGET_TEST }}
9898
working-directory: ${{ env.BUILD_DIR }}
9999
# Execute the build. You can specify a specific target with "--target <NAME>"
100100
run: cmake --build . --config ${{ env.BUILD_TYPE }} --target ${{ env.TARGET_TEST }}
101101

102+
- name: Build C test c${{ env.TARGET_TEST }}
103+
working-directory: ${{ env.BUILD_DIR }}
104+
# Execute the build. You can specify a specific target with "--target <NAME>"
105+
run: cmake --build . --config ${{ env.BUILD_TYPE }} --target c${{ env.TARGET_TEST }}
106+
102107
# Set BIN_DIR from matrix bindir command
103108
- name: Setting the binaries directory
104109
run: ${{ matrix.config.bindir }}
105110

106111
# Execute test
107-
- name: Execution Test ${{env.TARGET_TEST}}
112+
- name: Execution C++ test ${{env.TARGET_TEST}}
108113
run: ${{ env.BIN_DIR }}/${{ env.TARGET_TEST }}
109114

115+
- name: Execution C test c${{env.TARGET_TEST}}
116+
run: ${{ env.BIN_DIR }}/c${{ env.TARGET_TEST }}
117+
110118
# Upload binary files to artifacts
111119
- name: Upload compiled binaries to Artifacts
112120
uses: actions/upload-artifact@v2
113121
with:
114122
name: "Binaries for ${{ matrix.config.name }}"
115123
path: |
116124
${{ env.BIN_DIR }}/${{ env.TARGET_TEST }}*
125+
${{ env.BIN_DIR }}/c${{ env.TARGET_TEST }}*
117126
${{ env.BIN_DIR }}/${{ matrix.config.static }}
118127
${{ env.BIN_DIR }}/${{ matrix.config.dynamic }}

CMakeLists.txt

Lines changed: 126 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ cmake_minimum_required(VERSION 3.1)
88
project(picode)
99

1010
# Set version number for shared libraries and executables
11-
set(CU_VERSION 1.1) # current version
11+
set(CU_VERSION 1.2) # current version
1212
set(SO_VERSION 1.1) # compatibility version
1313

1414
# Set C/C++ Standard
@@ -94,29 +94,62 @@ elseif(MSVC)
9494
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -W4 ${MSVC_DISABLED_WARNINGS_STR}")
9595
endif()
9696

97-
# Add source directory for sources
98-
AUX_SOURCE_DIRECTORY( src/ ${PROJECT_NAME}_SRC )
97+
# Add sources for C++ library
98+
FILE (GLOB ${PROJECT_NAME}_SRC_CPP src/*.cpp )
9999

100-
# Add library aditional sources
100+
# Pure C PiCode Library sources
101+
FILE (GLOB ${PROJECT_NAME}_SRC_C src/*.c )
102+
103+
# Add library common sources
101104
AUX_SOURCE_DIRECTORY( libs/pilight/libs/pilight/core/ CORE )
102105
AUX_SOURCE_DIRECTORY( libs/pilight/libs/pilight/protocols/ PROTOCOL )
103106
AUX_SOURCE_DIRECTORY( libs/pilight/libs/pilight/protocols/433.92/ PROTOCOLS )
104107

105-
# Compile as object library
108+
# Compile common library objects
106109
add_library(
107-
${PROJECT_NAME}-obj OBJECT
108-
${${PROJECT_NAME}_SRC}
110+
${PROJECT_NAME}-common OBJECT
109111
${CORE}
110112
${PROTOCOL}
111113
${PROTOCOLS}
112114
)
113115

116+
# Compile C++ library as object
117+
add_library(
118+
${PROJECT_NAME}-obj OBJECT
119+
${${PROJECT_NAME}_SRC_CPP}
120+
)
121+
122+
# Pure C PiCode Library compile as object
123+
add_library(
124+
c${PROJECT_NAME}-obj OBJECT
125+
${${PROJECT_NAME}_SRC_C}
126+
)
127+
# If git info available adds to pure C PiCode Library
128+
if(DEFINED BUILD_VERSION)
129+
target_compile_definitions( c${PROJECT_NAME}-obj PRIVATE BUILD_VERSION=${BUILD_VERSION} )
130+
endif()
131+
132+
# If version number for shared libraries and executables available adds to pure C PiCode Library
133+
if(DEFINED CU_VERSION)
134+
target_compile_definitions( c${PROJECT_NAME}-obj PRIVATE CU_VERSION=${CU_VERSION} )
135+
endif()
136+
114137
# Shared libraries need flag -fPIC
115-
set_property(TARGET ${PROJECT_NAME}-obj PROPERTY POSITION_INDEPENDENT_CODE 1)
138+
set_property(TARGET ${PROJECT_NAME}-common PROPERTY POSITION_INDEPENDENT_CODE 1)
139+
set_property(TARGET ${PROJECT_NAME}-obj PROPERTY POSITION_INDEPENDENT_CODE 1)
140+
# Pure C PiCode Library
141+
set_property(TARGET c${PROJECT_NAME}-obj PROPERTY POSITION_INDEPENDENT_CODE 1)
116142

117143
# Shared library built from the same object files
144+
add_library(
145+
${PROJECT_NAME}-dynamic SHARED
146+
$<TARGET_OBJECTS:${PROJECT_NAME}-obj>
147+
$<TARGET_OBJECTS:${PROJECT_NAME}-common>
148+
)
149+
# C++ PiCode Library v1.2 require the pure C library (dynamic or static) as "c${PROJECT_NAME}-dynamic"
150+
target_link_libraries(${PROJECT_NAME}-dynamic PUBLIC c${PROJECT_NAME})
151+
118152
# File extension OS depends, like: libpicode.so or libpicode.dylib or libpicode.dll
119-
add_library( ${PROJECT_NAME}-dynamic SHARED $<TARGET_OBJECTS:${PROJECT_NAME}-obj> )
120153
set_target_properties( ${PROJECT_NAME}-dynamic PROPERTIES OUTPUT_NAME ${PROJECT_NAME} )
121154

122155
# Set version numbers for the versioned shared libraries target.
@@ -134,13 +167,59 @@ set_target_properties( ${PROJECT_NAME}-dynamic PROPERTIES
134167
VERSION ${CU_VERSION}
135168
)
136169

137-
# Add static library libpicode.a
138-
add_library( ${PROJECT_NAME} STATIC $<TARGET_OBJECTS:${PROJECT_NAME}-obj> )
170+
# Add static library
171+
# File extension OS depends, like: libpicode.a or libpicode.lib
172+
add_library(
173+
${PROJECT_NAME} STATIC
174+
$<TARGET_OBJECTS:${PROJECT_NAME}-obj>
175+
$<TARGET_OBJECTS:${PROJECT_NAME}-common>
176+
)
177+
# C++ PiCode Library v1.2 require the pure C library static as "c${PROJECT_NAME}"
178+
target_link_libraries(${PROJECT_NAME} PUBLIC c${PROJECT_NAME})
179+
180+
# Pure C PiCode Library dynamic
181+
# ---------------------------------------------------------------------------------
182+
# Shared library built from the same object files
183+
add_library(
184+
c${PROJECT_NAME}-dynamic SHARED
185+
$<TARGET_OBJECTS:c${PROJECT_NAME}-obj>
186+
$<TARGET_OBJECTS:${PROJECT_NAME}-common>
187+
)
188+
# File extension OS depends, like: libcpicode.so or libcpicode.dylib or libcpicode.dll
189+
set_target_properties( c${PROJECT_NAME}-dynamic PROPERTIES OUTPUT_NAME c${PROJECT_NAME} )
190+
191+
# Set version numbers for the versioned shared libraries target.
192+
# For shared libraries and executables on Windows and Mach-O systems
193+
# the SOVERSION property corresponds to the compatibility version
194+
# and VERSION corresponds to the current version
195+
#
196+
# Note that SOVERSION will still be used to form the install_name and
197+
# both SOVERSION and VERSION may also affect the file and symlink names.
198+
# Use the NAMELINK_SKIP option of the install command to prevent the
199+
# generation of the versionless library name symbolic link to the
200+
# versioned library file.
201+
set_target_properties( c${PROJECT_NAME}-dynamic PROPERTIES
202+
SOVERSION ${SO_VERSION}
203+
VERSION ${CU_VERSION}
204+
)
205+
206+
# Pure C PiCode Library static
207+
# File extension OS depends, like: libcpicode.a or libcpicode.lib
208+
add_library(
209+
c${PROJECT_NAME} STATIC
210+
$<TARGET_OBJECTS:c${PROJECT_NAME}-obj>
211+
$<TARGET_OBJECTS:${PROJECT_NAME}-common>
212+
)
139213

140214
# Add install targets
141215
install(TARGETS ${PROJECT_NAME} DESTINATION lib)
142216
install(TARGETS ${PROJECT_NAME}-dynamic DESTINATION lib)
143217

218+
# Pure C PiCode Library install targets
219+
# ---------------------------------------------------------------------------------
220+
install(TARGETS c${PROJECT_NAME} DESTINATION lib)
221+
install(TARGETS c${PROJECT_NAME}-dynamic DESTINATION lib)
222+
144223
# If no has parent directory, add uninstall targets
145224
if(NOT hasParent)
146225
MESSAGE(STATUS "Install prefix: ${CMAKE_INSTALL_PREFIX}")
@@ -149,15 +228,50 @@ if(NOT hasParent)
149228
)
150229
endif()
151230

231+
# Examples
232+
# ---------------------------------------------------------------------------------
233+
# Checking for math library 'libm' used when including <math.h> in pilight sources
234+
find_library(MATH_LIBRARY NAMES m)
235+
if(MATH_LIBRARY)
236+
MESSAGE( STATUS "Math library: " ${MATH_LIBRARY} )
237+
else()
238+
if(NOT MSVC)
239+
message(FATAL_ERROR "Cannot find math library 'libm' ")
240+
else()
241+
set(MATH_LIBRARY "")
242+
endif()
243+
endif()
244+
152245
# Add picode_example source file, link static, no build as default
153246
add_executable( picode_example picode_example.cpp )
154-
target_link_libraries( picode_example PRIVATE ${PROJECT_NAME} )
247+
target_link_libraries( picode_example PRIVATE ${PROJECT_NAME} ${MATH_LIBRARY} )
155248
set_target_properties( picode_example PROPERTIES EXCLUDE_FROM_ALL TRUE )
156249

250+
# Add executable export symbols for loadable modules to prevent policy CMP0065 warning
251+
set_property(TARGET picode_example PROPERTY ENABLE_EXPORTS 1)
252+
157253
# If git info available adds to picode_example executable as environment var
158254
if(DEFINED BUILD_VERSION)
159255
target_compile_definitions( picode_example PRIVATE BUILD_VERSION=${BUILD_VERSION} )
160256
endif(DEFINED BUILD_VERSION)
161257

162258
# Add complier identification to picode_example executable as environment var
163259
target_compile_definitions( picode_example PRIVATE BUILD_COMPILER=${BUILD_COMPILER} )
260+
261+
# Pure C PiCode Library example
262+
# ---------------------------------------------------------------------------------
263+
# Add cpicode_example source file, link static, no build as default
264+
add_executable( cpicode_example cpicode_example.c )
265+
target_link_libraries( cpicode_example PRIVATE c${PROJECT_NAME} ${MATH_LIBRARY} )
266+
set_target_properties( cpicode_example PROPERTIES EXCLUDE_FROM_ALL TRUE )
267+
268+
# Add executable export symbols for loadable modules to prevent policy CMP0065 warning
269+
set_property(TARGET cpicode_example PROPERTY ENABLE_EXPORTS 1)
270+
271+
# If git info available adds to cpicode_example executable as environment var
272+
if(DEFINED BUILD_VERSION)
273+
target_compile_definitions( cpicode_example PRIVATE BUILD_VERSION=${BUILD_VERSION} )
274+
endif(DEFINED BUILD_VERSION)
275+
276+
# Add complier identification to cpicode_example executable as environment var
277+
target_compile_definitions( cpicode_example PRIVATE BUILD_COMPILER=${BUILD_COMPILER} )

cpicode_example.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
Example of using the pure C PiCode Library
3+
4+
https://github.com/latchdevel/PiCode
5+
6+
Copyright (c) 2021 Jorge Rivera. All right reserved.
7+
License GNU Lesser General Public License v3.0.
8+
9+
*/
10+
11+
#include <stdio.h> /* printf() */
12+
#include <stdlib.h> /* free(), uint8_t */
13+
14+
#include "src/cPiCode.h" /* Pure C PiCode library */
15+
16+
int main(){
17+
18+
int result = 0;
19+
20+
printf("cpicode_example (%s)\n", STRINGIFY(BUILD_VERSION));
21+
printf("Compiled at " __DATE__ " " __TIME__ " %s (%s)\n",STRINGIFY(BUILD_COMPILER), BUILD_TYPE );
22+
23+
/* Get PiCode library version */
24+
25+
char* library_version = getPiCodeVersion();
26+
27+
if (library_version){
28+
printf("PiCode library version: %s\n", library_version);
29+
free(library_version);
30+
}else{
31+
printf("ERROR: Unable to get PiCode library version.\n");
32+
result--;
33+
}
34+
35+
printf("\n");
36+
37+
/* Decode from pilight string */
38+
39+
char* pilight_string = (char*) "c:011010100101011010100110101001100110010101100110101010101010101012;p:1400,600,6800@";
40+
41+
char* decoded_string = decodeString(pilight_string);
42+
43+
printf("String to decode: \"%s\"\n",pilight_string);
44+
45+
if (decoded_string){
46+
47+
printf("Decode string successful:\n");
48+
printf("%s\n",decoded_string);
49+
50+
free(decoded_string);
51+
52+
}else{
53+
printf("ERROR: Unable to decode string.\n");
54+
result--;
55+
}
56+
57+
/* Encode to pilight string from json */
58+
59+
char* json = (char*) "{ 'arctech_switch' : { 'id': 92, 'unit': 0, 'on': 1 }}";
60+
uint8_t repeats = 5;
61+
62+
char* encoded_json_string = encodeJson(json,repeats);
63+
64+
printf("\nJSON to encode: \"%s\"\n",json);
65+
66+
if (encoded_json_string){
67+
68+
printf("Encode successful:\n");
69+
printf("%s\n",encoded_json_string);
70+
71+
free(encoded_json_string);
72+
73+
}else{
74+
printf("ERROR: Unable to encode JSON.\n");
75+
result--;
76+
}
77+
printf("\n");
78+
return result;
79+
}

libs/pilight/libs/pilight/protocols/433.92/x10.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ static void parseCode(void) {
7575
}
7676
}
7777

78-
char id[4]= {'\0'};
78+
char id[6]= {'\0'};
7979
int l = letters[binToDecRev(binary, 0, 3)];
8080
int s = binary[18];
8181
int i = 1;
@@ -89,7 +89,7 @@ static void parseCode(void) {
8989
}
9090
i += binToDec(binary, 19, 20);
9191
if(c1 == 255 && c2 == 255) {
92-
snprintf(id, sizeof(id), "%c%d", l, i);
92+
snprintf(id, sizeof(id)-1, "%c%d", (char)l, (unsigned char)i);
9393
createMessage(id, s);
9494
}
9595
}
@@ -175,15 +175,15 @@ static void createFooter(void) {
175175
}
176176

177177
static int createCode(struct JsonNode *code) {
178-
char id[4] = {'\0'};
178+
char id[6] = {'\0'};
179179
int state = -1;
180180
double itmp = -1;
181181
char *stmp = NULL;
182182

183183
strcpy(id, "-1");
184184

185185
if(json_find_string(code, "id", &stmp) == 0)
186-
strncpy(id, stmp, sizeof(id));
186+
strncpy(id, stmp, sizeof(id)-1);
187187
if(json_find_number(code, "off", &itmp) == 0)
188188
state=1;
189189
else if(json_find_number(code, "on", &itmp) == 0)

0 commit comments

Comments
 (0)