NIMIBASE = -I$(PKGDIRBASE)/nimble

# include RIOT glue code headers
INCLUDES += -I$(RIOTPKG)/nimble/contrib/include

# include generic nimble headers
INCLUDES += $(NIMIBASE)/nimble/include

# include the RIOT NPL headers
INCLUDES += $(NIMIBASE)/porting/npl/riot/include
INCLUDES += $(NIMIBASE)/porting/nimble/include

# include nimble controller headers
ifneq (,$(filter nimble_controller,$(USEMODULE)))
  INCLUDES += $(NIMIBASE)/nimble/controller/include
  # set environment
  CFLAGS += -DNIMBLE_CFG_CONTROLLER=1
  CFLAGS += -DMYNEWT_VAL_OS_CPUTIME_FREQ=32768

  ifneq (,$(filter nimble_drivers_nrf5x,$(USEMODULE)))
    INCLUDES += $(NIMIBASE)/nimble/drivers/$(CPU_FAM)/include
  endif
endif

# include nimble host headers
ifneq (,$(filter nimble_host,$(USEMODULE)))
  INCLUDES += $(NIMIBASE)/nimble/host/include
endif
ifneq (,$(filter nimble_host_store_ram,$(USEMODULE)))
  INCLUDES += $(NIMIBASE)/nimble/host/store/ram/include
endif
ifneq (,$(filter nimble_host_util,$(USEMODULE)))
  INCLUDES += $(NIMIBASE)/nimble/host/util/include
endif

# include tinycrypt headers
ifneq (,$(filter nimble_controller,$(USEMODULE)))
  INCLUDES += $(NIMIBASE)/ext/tinycrypt/include
endif

# include transport headers
ifneq (,$(filter nimble_transport_ram,$(USEMODULE)))
  INCLUDES += $(NIMIBASE)/nimble/transport/ram/include
endif

# include services
ifneq (,$(filter nimble_svc_ans,$(USEMODULE)))
  INCLUDES += $(NIMIBASE)/nimble/host/services/ans/include
endif
ifneq (,$(filter nimble_svc_bas,$(USEMODULE)))
  INCLUDES += $(NIMIBASE)/nimble/host/services/bas/include
endif
ifneq (,$(filter nimble_svc_bleuart,$(USEMODULE)))
  INCLUDES += $(NIMIBASE)/nimble/host/services/bleuart/include
endif
ifneq (,$(filter nimble_svc_gap,$(USEMODULE)))
  INCLUDES += $(NIMIBASE)/nimble/host/services/gap/include
endif
ifneq (,$(filter nimble_svc_gatt,$(USEMODULE)))
  INCLUDES += $(NIMIBASE)/nimble/host/services/gatt/include
endif
ifneq (,$(filter nimble_svc_ias,$(USEMODULE)))
  INCLUDES += $(NIMIBASE)/nimble/host/services/ias/include
endif
ifneq (,$(filter nimble_svc_ipss,$(USEMODULE)))
  INCLUDES += $(NIMIBASE)/nimble/host/services/ipss/include
endif
ifneq (,$(filter nimble_svc_lls,$(USEMODULE)))
  INCLUDES += $(NIMIBASE)/nimble/host/services/lls/include
endif
ifneq (,$(filter nimble_svc_tps,$(USEMODULE)))
  INCLUDES += $(NIMIBASE)/nimble/host/services/tps/include
endif

# include additional headers for RIOT specific NimBLE submodules
ifneq (,$(filter nimble_addr,$(USEMODULE)))
  INCLUDES += -I$(RIOTPKG)/nimble/addr/include
endif
ifneq (,$(filter nimble_autoadv,$(USEMODULE)))
  INCLUDES += -I$(RIOTPKG)/nimble/autoadv/include
endif
ifneq (,$(filter nimble_autoconn,$(USEMODULE)))
  INCLUDES += -I$(RIOTPKG)/nimble/autoconn/include
endif
ifneq (,$(filter nimble_netif,$(USEMODULE)))
  INCLUDES += -I$(RIOTPKG)/nimble/netif/include

  # when using IP, the host should have a higher prio than the netif thread, but
  # MUST always have a lower priority then the controller thread. Setting it to
  # controller prio plus will guarantee the latter while also matching the first
  # condition as long as the default netif prio is not changed.
  CFLAGS += -DNIMBLE_HOST_PRIO="(NIMBLE_CONTROLLER_PRIO + 1)"

  # configure NimBLE's internals
  NIMBLE_MAX_CONN ?= 3
  CFLAGS += -DMYNEWT_VAL_MSYS_1_BLOCK_SIZE=264
  CFLAGS += -DMYNEWT_VAL_BLE_L2CAP_COC_MAX_NUM=$(NIMBLE_MAX_CONN)
  CFLAGS += -DMYNEWT_VAL_BLE_MAX_CONNECTIONS=$(NIMBLE_MAX_CONN)
  # NimBLEs internal buffer need to hold one IPv6 MTU per connection
  # for the internal MTU of 256 byte, we need 10 mbufs per connection...
  CFLAGS += -DMYNEWT_VAL_MSYS_1_BLOCK_COUNT=35

  # optimize the NimBLE controller for IP traffic
  ifneq (,$(filter nimble_controller,$(USEMODULE)))
    CFLAGS += -DMYNEWT_VAL_BLE_LL_CFG_FEAT_DATA_LEN_EXT=1
  endif
endif
ifneq (,$(filter nimble_scanlist,$(USEMODULE)))
  INCLUDES += -I$(RIOTPKG)/nimble/scanlist/include
endif
ifneq (,$(filter nimble_scanner,$(USEMODULE)))
  INCLUDES += -I$(RIOTPKG)/nimble/scanner/include
endif
ifneq (,$(filter nimble_statconn,$(USEMODULE)))
  INCLUDES += -I$(RIOTPKG)/nimble/statconn/include
endif