# Copyright (C) 2007  University of Rostock
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA.

################################################################################
# Common for all ws4d-gsoap projects, don't change this !
################################################################################

# directory to put all generated files in
SET(gen_DIR ${CMAKE_CURRENT_BINARY_DIR}/gen)

# If the directory for the generated sources does not
# already exists, create it.
IF(NOT EXISTS ${gen_DIR})
	FILE(MAKE_DIRECTORY ${gen_DIR})
ENDIF(NOT EXISTS ${gen_DIR})


IF("${ALT_IO_MODE}" STREQUAL "none")
  INCLUDE_DIRECTORIES(AFTER ${GSOAP_INCLUDE_DIR} ${DPWS_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR} ${gen_DIR})
ELSE("${ALT_IO_MODE}" STREQUAL "none")
  ADD_DEFINITIONS ( "-DWITH_NOIO -DWITH_SOAPDEFS_H" )
  INCLUDE_DIRECTORIES(AFTER ${DPWS_ALT_IO_INCLUDES} ${GSOAP_INCLUDE_DIR} ${DPWS_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR} ${gen_DIR})
ENDIF("${ALT_IO_MODE}" STREQUAL "none")


# set options for gSOAP runtime
GSOAP_SET_RUNTIME_FLAGS("-DWITH_NONAMESPACES -DWITH_UDP")

###############################################################################
###############################################################################
##                                                                           ##
## Simple airconditioner device                                              ##
##                                                                           ##
###############################################################################
###############################################################################

###############################################################################
# Code generation - for custom projects this must be modified
###############################################################################

# generate code and C data binding operations of service
GSOAP_GENERATE(acs.gsoap acs1 ${gen_DIR})   # normal service

# generate code and C data binding for events of service
GSOAP_GENERATE(acs_inv.gsoap acs_inv1 ${gen_DIR}) # event service

# generate device description and setup code
DPWS_METADATA_GENERATE(metadata.xml acs ${gen_DIR})

# embed wsdl files into c code
SET(AIRCONDITIONER_WSDL ${CMAKE_CURRENT_SOURCE_DIR}/AirConditioner.wsdl)
DPWS_EMBEDD_WSDL(${gen_DIR} acs ${AIRCONDITIONER_WSDL})

################################################################################
# create config.h
################################################################################

CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/config.h.in
               ${gen_DIR}/config.h
               @ONLY)

################################################################################
# build simple air conditioner - for custom projects this must be modified
################################################################################

# create airconditioner executable und define dependencies
# the device includes the simple_airconditioner.c and event.c, for
# the NetBurner entry point it includes nbmain.cpp which setups the NetBurner
# stack on the board and is responsible for the initialization, the
# gsoap runtime (${GSOAP_STDSOAP2_SOURCE}), the server lib by gsoap
# (${gen_DIR}/acs1ServerLib.c), the client lib of the inverted service
# by gsoap for eventing (${gen_DIR}/acs_inv1ClientLib.c), the device
# description and setup code (${gen_DIR}/acs_metadata.c) and the embedded
# wsdl files (${gen_DIR}/acs_wsdl.c)

SET(simple_arcondtitioner_SRCS
	simple_airconditioner.c
	event.c
	pnp.cpp
	${GSOAP_STDSOAP2_SOURCE}
	${gen_DIR}/acs1ServerLib.c
	${gen_DIR}/acs_inv1ClientLib.c
	${gen_DIR}/acs_metadata.c
	${gen_DIR}/acs_wsdl.c)

# compile all files in simple_arcondtitioner_SRCS and link them into
# the acs_simple_device executable
ADD_EXECUTABLE(acs_simple_device ${simple_arcondtitioner_SRCS})

EXTEND_TGT_COMPILE_FLAGS(acs_simple_device FLAGS "-DDPWS_DEVICE")

# link the device specific dpws libraries into the airconditioner executable
TARGET_LINK_LIBRARIES(acs_simple_device ${DPWS_LIBRARIES} ${DPWS-D_LIBRARIES} ${ADDITIONAL_IO_LIBS})


# set target name since it is used multiple times in post build commands
SET(TARGET_NAME acs_simple_device)

# create a link map file 
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Map=${EXECUTABLE_OUTPUT_PATH}/${TARGET_NAME}.map")

ADD_CUSTOM_COMMAND(TARGET ${TARGET_NAME} POST_BUILD
  COMMAND ${CMAKE_OBJCOPY} --strip-all --output-target=srec ${TARGET_NAME}.elf ${TARGET_NAME}.s19
  WORKING_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
  COMMENT "Converting executable to S record format"
)

ADD_CUSTOM_COMMAND(TARGET ${TARGET_NAME} POST_BUILD
  COMMAND ${COMP_CODE_EXECUTABLE} ${TARGET_NAME}.s19 ${TARGET_NAME}_APP.s19 -R 0xffc08000 0xFFC80000 -P${DEFPLATFORM}
  WORKING_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
  COMMENT "Compressing S record file and prepend description"
)

