Skip to content

Commit

Permalink
release 2.5: update pkg/datalayer and pkg/fbs
Browse files Browse the repository at this point in the history
  • Loading branch information
guidfort committed Dec 3, 2024
1 parent e1c0ece commit 6104b05
Show file tree
Hide file tree
Showing 89 changed files with 8,534 additions and 25 deletions.
10 changes: 10 additions & 0 deletions pkg/datalayer/nodeHandler.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,13 @@ void nodeCallbackOnMetadata(void *userdata, char *address, TYPE_CB cb, TYPE_CBDA
{
nodeCallbackGo(userdata, address, NULL, cb, (uint64_t)cbdata, NODE_ACTION_ON_METADATA);
}

void nodeCallbackSubscription(void *userdata, DLR_SUBSCRIPTION subscription, char *address)
{
nodeCallbackSubGo(userdata, subscription, address, NODE_ACTION_ON_SUBSCRIPTION);
}

void nodeCallbackUnsubscription(void *userdata, DLR_SUBSCRIPTION subscription, char *address)
{
nodeCallbackSubGo(userdata, subscription, address, NODE_ACTION_ON_UNSUBSCRIPTION);
}
54 changes: 46 additions & 8 deletions pkg/datalayer/nodeHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ typedef enum NODE_ACTION {
NODE_ACTION_ON_READ,
NODE_ACTION_ON_WRITE,
NODE_ACTION_ON_METADATA,
NODE_ACTION_ON_SUBSCRIPTION,
NODE_ACTION_ON_UNSUBSCRIPTION
}NODE_ACTION;
extern void callCallbackC(TYPE_CB cb, uint64_t cbdata, DLR_RESULT result, DLR_VARIANT data);
Expand All @@ -48,7 +50,11 @@ extern void nodeCallbackOnRemove(void* userdata, char* address, TYPE_CB cb, TYPE
extern void nodeCallbackOnBrowse(void* userdata, char* address, TYPE_CB cb, TYPE_CBDATA cbdata);
extern void nodeCallbackOnRead(void* userdata, char* address, DLR_VARIANT data, TYPE_CB cb, TYPE_CBDATA cbdata);
extern void nodeCallbackOnWrite(void* userdata, char* address, DLR_VARIANT data, TYPE_CB cb, TYPE_CBDATA cbdata);
extern void nodeCallbackOnMetadata(void* userdata, char* address, TYPE_CB cb, TYPE_CBDATA cbdata);*/
extern void nodeCallbackOnMetadata(void* userdata, char* address, TYPE_CB cb, TYPE_CBDATA cbdata);
extern void nodeCallbackSubscription(void *userdata, DLR_SUBSCRIPTION subscription, char *address);
extern void nodeCallbackUnsubscription(void *userdata, DLR_SUBSCRIPTION subscription, char *address);
*/
import "C"
import (
"sync"
Expand All @@ -60,12 +66,14 @@ type nodeAction C.enum_NODE_ACTION

// nodeAction enum definition
const (
nodeActionOnCreate nodeAction = C.NODE_ACTION_ON_CREATE
nodeActionOnRemove nodeAction = C.NODE_ACTION_ON_REMOVE
nodeActionOnBrowse nodeAction = C.NODE_ACTION_ON_BROWSE
nodeActionOnRead nodeAction = C.NODE_ACTION_ON_READ
nodeActionOnWrite nodeAction = C.NODE_ACTION_ON_WRITE
nodeActionOnMetadata nodeAction = C.NODE_ACTION_ON_METADATA
nodeActionOnCreate nodeAction = C.NODE_ACTION_ON_CREATE
nodeActionOnRemove nodeAction = C.NODE_ACTION_ON_REMOVE
nodeActionOnBrowse nodeAction = C.NODE_ACTION_ON_BROWSE
nodeActionOnRead nodeAction = C.NODE_ACTION_ON_READ
nodeActionOnWrite nodeAction = C.NODE_ACTION_ON_WRITE
nodeActionOnMetadata nodeAction = C.NODE_ACTION_ON_METADATA
nodeActionOnSubsciption nodeAction = C.NODE_ACTION_ON_SUBSCRIPTION
nodeActionOnUnsubsciption nodeAction = C.NODE_ACTION_ON_UNSUBSCRIPTION
)

// nodeCallbackData type
Expand Down Expand Up @@ -96,7 +104,22 @@ func nodeCallbackGo(cuserdata unsafe.Pointer, caddress *C.char, cdata C.DLR_VARI
}
}

// getNodeCallbacksC get struct with all C callbacks
//export nodeCallbackSubGo
func nodeCallbackSubGo(cuserdata unsafe.Pointer, sub C.DLR_SUBSCRIPTION, caddress *C.char, action C.int) {
var i int = *(*int)(cuserdata)
var userdata *nodeUserData = nodeLookup(i)
address := C.GoString(caddress)
switch nodeAction(action) {
case nodeActionOnSubsciption:
userdata.channels.OnSubscribe <- ProviderNodeSubscription{Address: address, Subsciption: &ProviderSubscription{this: sub}}
case nodeActionOnUnsubsciption:
userdata.channels.OnUnsubscribe <- ProviderNodeSubscription{Address: address, Subsciption: &ProviderSubscription{this: sub}}
default:
panic("Unknown action type in callback")
}
}

// getNodeCallbacksC get struct with subset of the C callbacks
func getNodeCallbacksC(userdata unsafe.Pointer) C.DLR_PROVIDER_NODE_CALLBACKS {
return C.DLR_PROVIDER_NODE_CALLBACKS{
userData: userdata,
Expand All @@ -109,6 +132,21 @@ func getNodeCallbacksC(userdata unsafe.Pointer) C.DLR_PROVIDER_NODE_CALLBACKS {
}
}

// getNodeSubCallbacksC get struct with all C callbacks
func getNodeSubCallbacksC(userdata unsafe.Pointer) C.DLR_PROVIDER_NODE_CALLBACKS {
return C.DLR_PROVIDER_NODE_CALLBACKS{
userData: userdata,
onCreate: (*[0]byte)(C.nodeCallbackOnCreate),
onRemove: (*[0]byte)(C.nodeCallbackOnRemove),
onBrowse: (*[0]byte)(C.nodeCallbackOnBrowse),
onRead: (*[0]byte)(C.nodeCallbackOnRead),
onWrite: (*[0]byte)(C.nodeCallbackOnWrite),
onMetadata: (*[0]byte)(C.nodeCallbackOnMetadata),
onSubscribe: (*[0]byte)(C.nodeCallbackSubscription),
onUnsubscribe: (*[0]byte)(C.nodeCallbackUnsubscription),
}
}

// getNodeUserdata store Go channels in userdata of C callback
func getNodeUserdata(channels ProviderNodeChannels) unsafe.Pointer {
userdata := &nodeUserData{channels: channels}
Expand Down
113 changes: 113 additions & 0 deletions pkg/datalayer/notifyitempublish.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// MIT License
//
// Copyright (c) 2021-2024 Bosch Rexroth AG
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package datalayer

import (
"time"

fbs "github.com/boschrexroth/ctrlx-datalayer-golang/v2/pkg/fbs/comm/datalayer"
flatbuffers "github.com/google/flatbuffers/go"
)

// NotifyItemPublish interface for publish subscription items.
type NotifyItemPublish struct {
data *Variant
info *Variant
notifyInfo *fbs.NotifyInfoT
}

// NewNotifyItemPublish creates a NotifyItemPublish
// Parameter address is the address of the provider node.
func NewNotifyItemPublish(address string) *NotifyItemPublish {
i := &NotifyItemPublish{
data: NewVariant(),
info: NewVariant(),
notifyInfo: &fbs.NotifyInfoT{
Node: address,
Timestamp: toFiletime(time.Now()),
NotifyType: fbs.NotifyTypeData,
SequenceNumber: 0,
},
}
return i
}

// deleteNotifyItemPublish removes a NotifyItemPublish.
// Parameter n is an instance of the NotifyItemPublish.
func deleteNotifyItemPublish(n *NotifyItemPublish) {
if n == nil {
return
}
DeleteVariant(n.data)
DeleteVariant(n.info)
}

// DeleteNotifyItemsPublish removes a lit of NotifyItemPublish.
// Parameter s is list of the NotifyItemPublish.
func DeleteNotifyItemsPublish(l []*NotifyItemPublish) {
for _, n := range l {
deleteNotifyItemPublish(n)
}
}

// GetData gets the data of the notify item.
// It returns the variant.
func (i *NotifyItemPublish) GetData() *Variant {
return i.data
}

// GetInfo gets the info of the notify item.
// containing notify_info.fbs (address, timestamp, type, ...).
// It returns the variant
func (i *NotifyItemPublish) GetInfo() *Variant {
builder := flatbuffers.NewBuilder(4096)
niPack := i.notifyInfo.Pack(builder)
builder.Finish(niPack)
i.info.SetFlatbuffers(builder.FinishedBytes())
return i.info
}

// SetNotifyType sets the NotifyType of the notify_info.fbs.
func (i *NotifyItemPublish) SetNotifyType(n fbs.NotifyType) {
i.notifyInfo.NotifyType = n
}

// SetTimestamp sets a time.Time value since January 1, 1970 UTC of the notify_info.fbs.
func (i *NotifyItemPublish) SetTimestamp(t time.Time) {
i.notifyInfo.Timestamp = toFiletime(t)
}

// SetEventType sets eventtype of the notify_info.fbs.
func (i *NotifyItemPublish) SetEventType(e string) {
i.notifyInfo.EventType = e
}

// SetSequenceNumber sets sequence number of the notify_info.fbs.
func (i *NotifyItemPublish) SetSequenceNumber(n uint64) {
i.notifyInfo.SequenceNumber = n
}

// SetSequenceNumber sets source name of the notify_info.fbs.
func (i *NotifyItemPublish) SetSourceName(s string) {
i.notifyInfo.SourceName = s
}
44 changes: 44 additions & 0 deletions pkg/datalayer/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,47 @@ func (p *Provider) GetToken() *Variant {
ptr := C.DLR_providerGetToken(p.this)
return &Variant{this: ptr}
}

// GetRegisteredType get the variant of a registered type
// Parameter address is address of the type to get type (no wildcards allowed).
// It returns the status of function call and variant to stored type (Result, Variant).
func (p *Provider) GetRegisteredType(address string) (Result, *Variant) {
caddress := C.CString(address)
defer C.free(unsafe.Pointer(caddress))
data := NewVariant()
r := Result(C.DLR_providerGetRegisteredType(p.this, caddress, data.this))
return r, data
}

// GetRegisteredNodePaths return the current registered node paths.
// It returns the status of function call and list of registered node paths.
func (p *Provider) GetRegisteredNodePaths() (Result, []string) {
data := NewVariant()
defer DeleteVariant(data)
r := Result(C.DLR_providerGetRegisteredNodePaths(p.this, data.this))
if r != ResultOk {
return r, []string{}
}
return r, data.GetArrayString()
}

// GetRejectedNodePaths return the current rejected node paths.
// It returns the status of function call and list of rejected node paths.
func (p *Provider) GetRejectedNodePaths() (Result, []string) {
data := NewVariant()
defer DeleteVariant(data)
r := Result(C.DLR_providerGetRejectedNodePaths(p.this, data.this))
if r != ResultOk {
return r, []string{}
}
return r, data.GetArrayString()
}

// PublishEvent publishes an event
// Parameter data is the payload data of the event. Has to match the type, that is given in the notifyInfo.
// Parameter eventtype contains additional info about the event with type event_info.fbs.
// It returns the status of function call.
func (p *Provider) PublishEvent(data, eventtype *Variant) Result {
r := Result(C.DLR_providerPublishEvent(p.this, data.this, eventtype.this))
return r
}
66 changes: 52 additions & 14 deletions pkg/datalayer/providerNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,23 @@ type ProviderNodeEventData struct {
Callback ProviderNodeCallback
}

// ProviderNodeSubscription event
type ProviderNodeSubscription struct {
Address string
Subsciption *ProviderSubscription
}

// ProviderNodeChannels sets the struct.
type ProviderNodeChannels struct {
OnCreate chan ProviderNodeEventData
OnRemove chan ProviderNodeEvent
OnBrowse chan ProviderNodeEvent
OnRead chan ProviderNodeEventData
OnWrite chan ProviderNodeEventData
OnMetadata chan ProviderNodeEvent
Done chan bool
OnCreate chan ProviderNodeEventData
OnRemove chan ProviderNodeEvent
OnBrowse chan ProviderNodeEvent
OnRead chan ProviderNodeEventData
OnWrite chan ProviderNodeEventData
OnMetadata chan ProviderNodeEvent
OnSubscribe chan ProviderNodeSubscription
OnUnsubscribe chan ProviderNodeSubscription
Done chan bool
}

// ProviderNode interface for providing data to the system.
Expand All @@ -75,13 +83,15 @@ func (n *ProviderNode) Channels() *ProviderNodeChannels {
// NewProviderNode initializes the provider node.
func NewProviderNode() *ProviderNode {
channels := ProviderNodeChannels{
OnCreate: make(chan ProviderNodeEventData, providerNodeChannelSize),
OnRemove: make(chan ProviderNodeEvent, providerNodeChannelSize),
OnBrowse: make(chan ProviderNodeEvent, providerNodeChannelSize),
OnRead: make(chan ProviderNodeEventData, providerNodeChannelSize),
OnWrite: make(chan ProviderNodeEventData, providerNodeChannelSize),
OnMetadata: make(chan ProviderNodeEvent, providerNodeChannelSize),
Done: make(chan bool),
OnCreate: make(chan ProviderNodeEventData, providerNodeChannelSize),
OnRemove: make(chan ProviderNodeEvent, providerNodeChannelSize),
OnBrowse: make(chan ProviderNodeEvent, providerNodeChannelSize),
OnRead: make(chan ProviderNodeEventData, providerNodeChannelSize),
OnWrite: make(chan ProviderNodeEventData, providerNodeChannelSize),
OnMetadata: make(chan ProviderNodeEvent, providerNodeChannelSize),
OnSubscribe: nil,
OnUnsubscribe: nil,
Done: make(chan bool),
}
userdata := getNodeUserdata(channels)
ptr := C.DLR_providerNodeCreate(getNodeCallbacksC(userdata))
Expand All @@ -92,6 +102,28 @@ func NewProviderNode() *ProviderNode {
}
}

// NewProviderNodeSub initializes the provider node and support the OnSubscribe and OnUnsubscribe interface.
func NewProviderNodeSub() *ProviderNode {
channels := ProviderNodeChannels{
OnCreate: make(chan ProviderNodeEventData, providerNodeChannelSize),
OnRemove: make(chan ProviderNodeEvent, providerNodeChannelSize),
OnBrowse: make(chan ProviderNodeEvent, providerNodeChannelSize),
OnRead: make(chan ProviderNodeEventData, providerNodeChannelSize),
OnWrite: make(chan ProviderNodeEventData, providerNodeChannelSize),
OnMetadata: make(chan ProviderNodeEvent, providerNodeChannelSize),
OnSubscribe: make(chan ProviderNodeSubscription, providerNodeChannelSize),
OnUnsubscribe: make(chan ProviderNodeSubscription, providerNodeChannelSize),
Done: make(chan bool),
}
userdata := getNodeUserdata(channels)
ptr := C.DLR_providerNodeCreate(getNodeSubCallbacksC(userdata))
return &ProviderNode{
this: ptr,
userdata: userdata,
channels: channels,
}
}

// DeleteProviderNode destructs the provider node.
func DeleteProviderNode(n *ProviderNode) {
if n == nil {
Expand All @@ -109,5 +141,11 @@ func DeleteProviderNode(n *ProviderNode) {
close(n.channels.OnBrowse)
close(n.channels.OnRead)
close(n.channels.OnMetadata)
if n.channels.OnSubscribe != nil {
close(n.channels.OnSubscribe)
}
if n.channels.OnUnsubscribe != nil {
close(n.channels.OnUnsubscribe)
}
n.this = nil
}
Loading

0 comments on commit 6104b05

Please sign in to comment.