Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
123 lines (107 sloc) 3.58 KB
package openzwave
//
// Provides a facade for the C++ API that exposes just enough of the underlying C++
// API to be useful to implementing the Ninja Zwave driver
//
// The functions in this module are responsible for marshalling to and from the C functions
// declared in api.hpp and api.cpp.
//
//
// The following #cgo directives assumed that 'go' is a symbolic link that references the gopath that contains the current directory, e.g. ../../../..
//
// All 'go' packages that have this package as a dependency should include such a go link and will then inherit the library built in this package.
//
// #cgo LDFLAGS: -lopenzwave -Lgo/src/github.com/ninjasphere/go-openzwave/openzwave
// #cgo CPPFLAGS: -Iopenzwave/cpp/src/platform -Iopenzwave/cpp/src -Iopenzwave/cpp/src/value_classes
// #include "api.h"
import "C"
// A value-less type that is used to represent signals generated by the API, particularly quit signals used to
// ask an EventLoop to quit.
type api struct {
shareable
loop EventLoop
callback NotificationCallback
eventCallback EventCallback
deviceFactory DeviceFactory
device string
quitEventLoop chan int
shutdownDriver chan int
logger Logger
networks map[uint32]*network
quitDeviceMonitor chan int
}
//
// The API interface is available to implementors of the EventLoop type when the
// Configurator.Run() method is called.
//
type API interface {
// The EventLoop should return from the function when a signal is received on this channel
QuitSignal() chan int
// the API logger
Logger() Logger
// Get a network by HomeID
// Returns network, or nil if no network exists matching the specified HomeID
GetNetwork(homeId uint32) Network
// Shutdown the event loop
Shutdown(exit int)
}
//
// Begin the construction of the API by returning a Configurator
//
// configPath is the name of the directory containing openzwave configuration files;
// userPath is the name of the directory containing user specific openzwave configuration files;
// overrides are command line options (uptto --) that can overide the configuration provided by the previous two options.
//
// For more information about these parameters, refer to the documentation for the C++ OpenZWave::Options class.
//
func BuildAPI(configPath string, userPath string, overrides string) Configurator {
var (
cConfigPath *C.char = C.CString(configPath)
cUserPath *C.char = C.CString(userPath)
cOverrides *C.char = C.CString(overrides)
)
//defer C.free(unsafe.Pointer(cConfigPath))
//defer C.free(unsafe.Pointer(cUserPath))
//defer C.free(unsafe.Pointer(cOverrides)
C.startOptions(cConfigPath, cUserPath, cOverrides)
var r *api
r = &api{
loop: defaultEventLoop,
callback: nil,
eventCallback: defaultEventCallback,
deviceFactory: defaultDeviceFactory,
device: defaultDriverName,
quitEventLoop: make(chan int, 0),
shutdownDriver: make(chan int, 2),
logger: &defaultLogger{},
networks: make(map[uint32]*network),
quitDeviceMonitor: make(chan int, 2)}
r.shareable.init(r)
return r
}
func (a *api) QuitSignal() chan int {
return a.quitEventLoop
}
func (a *api) Logger() Logger {
return a.logger
}
func (a *api) GetNetwork(homeId uint32) Network {
net, ok := a.networks[homeId]
if !ok {
return nil
}
return net
}
func (a *api) getNetwork(homeId uint32) *network {
net, ok := a.networks[homeId]
if !ok {
net = newNetwork(homeId)
a.networks[homeId] = net
}
return net
}
func (a *api) notifyEvent(event Event) {
if a.eventCallback != nil {
a.eventCallback(a, event)
}
}