Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature multiplatform #24

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
4edff5d
Automatically detect Microsoft Visual Studio NMAKE vs. GNU make
jy-wirepas Jan 10, 2020
2907f6d
Add support for Darwin / macOS
jy-wirepas Jan 12, 2020
b08d5f8
Harmonize path separator usage in makefile variables
jy-wirepas Jan 13, 2020
65ad696
Add optimization flags
jy-wirepas Jan 13, 2020
39e2844
Add missing "void" in function prototypes and definitions
jy-wirepas Jan 13, 2020
9791a35
Add support for packed structs on Microsoft Visual Studio C compiler
jy-wirepas Jan 13, 2020
f443589
SLIP module clean-up
jy-wirepas Jan 13, 2020
724da7c
Turn internal multi-line macros to inline functions for MSVC compatib…
jy-wirepas Jan 13, 2020
b9a6c11
Fix miscellaneous compiler warnings from Microsoft Visual Studio C co…
jy-wirepas Jan 13, 2020
fd67bd6
Fix a bug in setting TX packet callback parameters
jy-wirepas Jan 13, 2020
4c306ff
Add support for building with Microsoft Visual Studio NMAKE
jy-wirepas Jan 13, 2020
c58cf7e
Fix problems found by Codacy/PR Quality Review
jy-wirepas Jan 13, 2020
0fef4c3
Fix const pointer parameters in public and internal APIs
jy-wirepas Jan 14, 2020
01c15eb
Add more missing "void" in function prototypes and definitions
jy-wirepas Jan 14, 2020
290df87
Improve inconsistent log messages
jy-wirepas Jan 14, 2020
7348233
Add more error checks to POSIX serial port routines
jy-wirepas Jan 14, 2020
4239cd5
Unconditionally compile Wirepas C Mesh API library with examples
jy-wirepas Jan 14, 2020
b119b1f
Add support for Win32
jy-wirepas Jan 15, 2020
cc723ce
Add Win32 support to test applications
jy-wirepas Jan 15, 2020
5d94175
Change Win32 build flags to be compatible with official Python release
jy-wirepas Jan 17, 2020
38a5e89
Makefile clean up
jy-wirepas Jan 17, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions example/gnu_make.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Makefile for Wirepas C Mesh API example, GNU make version

# Variables

# This example needs the C Mesh API library
MESH_LIB_FOLDER := ../lib
MESH_LIB := $(MESH_LIB_FOLDER)/build/mesh_api_lib.a

# Detect platform and set toolchain variables
include $(MESH_LIB_FOLDER)/tools.mk

# Path of source files and build outputs
SOURCEPREFIX := .
BUILDPREFIX := build

# Targets definition
MAIN_APP := meshAPIExample

TARGET_APP := $(BUILDPREFIX)/$(MAIN_APP)

# Add API header
CFLAGS += -I$(MESH_LIB_FOLDER)/api

# Source files
SOURCES:= $(SOURCEPREFIX)/main.c

# Object files
OBJECTS := $(patsubst $(SOURCEPREFIX)/%, \
$(BUILDPREFIX)/%, \
$(SOURCES:.c=.o))


# Functions

# Also create the target directory if it does not exist
define COMPILE
echo " CC $(2)"
mkdir -p $(dir $(1))
$(CC) $(CFLAGS) -c -o $(1) $(2)
endef

define LINK
echo " LD $(1)"
$(CC) $(LDFLAGS) -o $(1) $(2) $(MESH_LIB)
endef

define CLEAN
echo " CLEAN"
$(RM) -r $(BUILDPREFIX)
endef


# Target rules

ifeq ($(V),1)
# "V=1" on command line, print commands
else
.SILENT:
# Default, do not print commands
endif

.PHONY: all
all: app

app: lib $(TARGET_APP)

.PHONY: clean
clean:
$(call CLEAN)

lib:
make -C $(MESH_LIB_FOLDER)

$(BUILDPREFIX)/%.o: $(SOURCEPREFIX)/%.c
$(call COMPILE,$@,$<)

$(BUILDPREFIX)/$(MAIN_APP): $(OBJECTS) $(MESH_LIB)
$(call LINK,$@,$^)
58 changes: 52 additions & 6 deletions example/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,31 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#ifdef PLATFORM_IS_WIN32
# include <windows.h> // For Sleep()
# define sleep(sec) Sleep(sec * 1000)
# define pause() Sleep(500)
#else
# include <unistd.h> // For sleep(), pause()
#endif

#include "wpc.h"

#define LOG_MODULE_NAME "Main"
#define LOG_MODULE_NAME "example_main"
#define MAX_LOG_LEVEL INFO_LOG_LEVEL
#include "logger.h"

// Default serial port
char * port_name = "/dev/ttyACM0";
#ifdef PLATFORM_IS_WIN32
static char * port_name = "COM1";
#elif PLATFORM_IS_LINUX
static char * port_name = "/dev/ttyACM0";
#elif PLATFORM_IS_DARWIN
static char * port_name = "/dev/cu.usbmodem0001234567890";
#else
static char * port_name = "/dev/ttyS0";
#endif

// Node configuration
#define NODE_ADDRESS 0x1
Expand All @@ -35,13 +50,26 @@ static bool onDataReceived(const uint8_t * bytes,
uint8_t hop_count,
unsigned long long timestamp_ms_epoch)
{
// Suppress warnings for unused parameters
(void) bytes;
(void) num_bytes;
(void) src_addr;
(void) dst_addr;
(void) qos;
(void) src_ep;
(void) dst_ep;
(void) travel_time;
(void) hop_count;
(void) timestamp_ms_epoch;

// Handle Data received here
LOGI("Data received on EP %d of len %d with id %d from 0x%x to 0x%x\n",
LOGI("Data received on EP %u of len %u with id %u from %lu to %lu\n",
dst_ep,
num_bytes,
bytes[0],
src_addr,
dst_addr);
(unsigned long) src_addr,
(unsigned long) dst_addr);

return true;
}

Expand All @@ -56,7 +84,25 @@ static bool onDiagReceived(const uint8_t * bytes,
uint8_t hop_count,
unsigned long long timestamp_ms_epoch)
{
// Suppress warnings for unused parameters
(void) bytes;
(void) num_bytes;
(void) src_addr;
(void) dst_addr;
(void) qos;
(void) src_ep;
(void) dst_ep;
(void) travel_time;
(void) hop_count;
(void) timestamp_ms_epoch;

// Handle diag packet here
// Handle Data received here
LOGI("Diag received from EP %u of len %u from %lu to %lu\n",
src_ep,
num_bytes,
(unsigned long) src_addr,
(unsigned long) dst_addr);

return true;
}
Expand Down
130 changes: 43 additions & 87 deletions example/makefile
Original file line number Diff line number Diff line change
@@ -1,87 +1,43 @@
# Makefile for Wirepas C Mesh API example

# Toolchain
CC := gcc
AS := as
LD := ld
AR := ar

# Paths, including trailing path separator
SOURCEPREFIX := ./
BUILDPREFIX := build/

# This example needs the mesh lib
MESH_LIB_FOLDER := ../lib/
MESH_LIB := $(MESH_LIB_FOLDER)build/mesh_api_lib.a

# General compiler flags
CFLAGS := -std=gnu99 -Wall -Werror

# Targets definition
MAIN_APP := meshAPIExample

TARGET_APP := $(BUILDPREFIX)$(MAIN_APP)

# Add Api header
CFLAGS += -I$(MESH_LIB_FOLDER)api
# Add pthtread lib as needed by Mesh Lib
LDFLAGS += -pthread
# Add Reentrant flag as using pthread
CFLAGS += -D_REENTRANT

# Specific sources for main
SOURCES:= $(SOURCEPREFIX)main.c

OBJECTS := $(patsubst $(SOURCEPREFIX)%, \
$(BUILDPREFIX)%, \
$(SOURCES:.c=.o))

# Functions

# Also create the target directory if it does not exist
define COMPILE
echo " CC $(2)"
mkdir -p $(dir $(1))
$(CC) $(CFLAGS) -c -o $(1) $(2)
endef

define LINK
echo " Linking $(1)"
$(CC) $(CFLAGS) $(LDFLAGS) -o $(1) $(2) $(MESH_LIB)
endef

define CLEAN
echo " Cleaning up"
$(RM) -r $(BUILDPREFIX)
endef

# Target rules

# Use dependency files automatically generated by GCC.
# First collect all C source files
AUTODEPS := $(patsubst $(SOURCEPREFIX)%.c, $(BUILDPREFIX)%.d, $(SOURCES))

ifeq ($(V),1)
# "V=1" on command line, print commands.
else
# Default, do not print commands.
.SILENT:
endif

.PHONY: all
all: app

app: $(TARGET_APP)

.PHONY: clean
clean:
$(call CLEAN)

$(MESH_LIB):
make -C $(MESH_LIB_FOLDER)

$(BUILDPREFIX)%.o: $(SOURCEPREFIX)%.c
$(call COMPILE,$@,$<)

$(BUILDPREFIX)$(MAIN_APP): $(OBJECTS) $(MESH_LIB)
$(call LINK,$@,$^)
# This file selects the correct makefile for Microsoft NMAKE and GNU make

# Theory of operation:
#
# Microsoft NMAKE considers the caret (^) as an escape character. It is
# possible to specify a literal newline character in a macro by using a
# caret at the end of a line. The next line will then be consumed as part
# of the macro definition. GNU make does not consider a caret as anything
# special, so macro definitions that end in a caret will consume just a
# single line.
#
# This distinction can be used to hide single lines from NMAKE. Putting
# GNU make-specific directives on those lines can then be used to hide
# NMAKE-specific portions from GNU make.

GNU_MAKE_IF=^
ifeq (0, 1) # This line only interpreted by GNU make

# Include NMAKE makefile # #
!INCLUDE nmake.mk # #
# # < This portion only seen by NMAKE #
# Begin skip everything else for NMAKE # #
!IF 0 # #

GNU_MAKE_ELSE=^
else # This line only interpreted by GNU make

# Include GNU make makefile # #
include gnu_make.mk # < This portion only seen by GNU make #
# # #

GNU_MAKE_ENDIF=^
endif # This line only interpreted by GNU make

GNU_MAKE_IF_2=^
ifeq (0, 1) # This line only interpreted by GNU make

# End skip everything else for NMAKE # #
!ENDIF # < This portion only seen by NMAKE #
# # #

GNU_MAKE_ENDIF_2=^
endif # This line only interpreted by GNU make
60 changes: 60 additions & 0 deletions example/nmake.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Makefile for Wirepas C Mesh API example, Microsoft NMAKE version

# Variables

# This example needs the C Mesh API library
MESH_LIB_FOLDER = ..\lib
MESH_LIB = $(MESH_LIB_FOLDER)\build\mesh_api_lib.lib

# Detect platform and set toolchain variables
!INCLUDE $(MESH_LIB_FOLDER)\tools_msvc.mk

# Path of source files and build outputs
SOURCEPREFIX = .
BUILDPREFIX = build

# Targets definition
MAIN_APP = meshAPIExample
TARGET_APP = $(BUILDPREFIX)\$(MAIN_APP).exe

# Add API header
CFLAGS = $(CFLAGS) /I$(MESH_LIB_FOLDER)\api

# Source files
SOURCES = $(SOURCEPREFIX)\main.c

# Object files, one for each C source file
OBJECTS = $(SOURCES:.c=.obj)


# Target rules

!IF "$(V)"=="1"
# "V=1" on command line, print commands
!MESSAGE SOURCES: $(SOURCES)
!MESSAGE OBJECTS: $(OBJECTS)
!ELSE
.SILENT:# Default, do not print commands
!ENDIF

all: app

app: lib $(TARGET_APP)

clean:
echo CLEAN
-del /F $(OBJECTS) $(TARGET_APP) >NUL 2>NUL
-rmdir /S /Q $(BUILDPREFIX) >NUL 2>NUL

lib:
cd $(MESH_LIB_FOLDER) && $(MAKE) /nologo /f nmake.mk

.c.obj:
echo CC $*.c
-mkdir $(@D) >NUL 2>NUL
$(CC) $(CFLAGS) /c /Fo$@ $*.c

$(TARGET_APP): $(OBJECTS) $(MESH_LIB)
echo LINK $@
-mkdir $(@D) >NUL 2>NUL
$(CC) $(LDFLAGS) /Fe$@ $**
Loading