Skip to content

Commit

Permalink
Change approve API
Browse files Browse the repository at this point in the history
With the changed API it is now possible to provide a specific error number if approval is denied
  • Loading branch information
DerAndereAndi committed May 16, 2024
1 parent b3c1a8a commit df8b598
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
4 changes: 2 additions & 2 deletions api/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ type FeatureLocalInterface interface {
// SetWritePermissionCheckCallback being invoked by the stack returning wether
// the remote requested write command shall be allowed or not
//
// reason contains the error string reported back when write is not allwed
ApproveOrDenyWrite(msg *Message, allow bool, reason string)
// ErrorType.ErrorNumber should be 0 if write is allowed
ApproveOrDenyWrite(msg *Message, err model.ErrorType)
// Overwrite the default 1 minute timeout for write approvals
SetWriteApprovalTimeout(duration time.Duration)

Expand Down
7 changes: 3 additions & 4 deletions spine/feature_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (r *FeatureLocal) addPendingApproval(msg *api.Message) {
r.muxResponseCB.Unlock()
}

func (r *FeatureLocal) ApproveOrDenyWrite(msg *api.Message, allow bool, reason string) {
func (r *FeatureLocal) ApproveOrDenyWrite(msg *api.Message, err model.ErrorType) {
if r.Role() != model.RoleTypeServer {
return
}
Expand All @@ -198,13 +198,12 @@ func (r *FeatureLocal) ApproveOrDenyWrite(msg *api.Message, allow bool, reason s

timer.Stop()

if allow {
if err.ErrorNumber == 0 {
r.processWrite(msg)
return
}

err := model.NewErrorTypeFromString(reason)
_ = msg.FeatureRemote.Device().Sender().ResultError(msg.RequestHeader, r.Address(), err)
_ = msg.FeatureRemote.Device().Sender().ResultError(msg.RequestHeader, r.Address(), &err)
}

func (r *FeatureLocal) SetWriteApprovalTimeout(duration time.Duration) {
Expand Down
21 changes: 17 additions & 4 deletions spine/feature_local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,10 @@ func (suite *DeviceClassificationTestSuite) Test_SetWriteApprovalCallback_Invali
cb := func(msg *api.Message) {}
err := suite.localFeature.AddWriteApprovalCallback(cb)
assert.NotNil(suite.T(), err)
suite.localFeature.ApproveOrDenyWrite(&api.Message{}, true, "")
result := model.ErrorType{
ErrorNumber: 0,
}
suite.localFeature.ApproveOrDenyWrite(&api.Message{}, result)
}

func (suite *DeviceClassificationTestSuite) Test_AddPendingApproval_Invalid() {
Expand Down Expand Up @@ -412,7 +415,10 @@ func (suite *DeviceClassificationTestSuite) Test_Write_Callback() {
}

cb := func(msg *api.Message) {
suite.localServerFeatureWrite.ApproveOrDenyWrite(msg, true, "")
result := model.ErrorType{
ErrorNumber: 0,
}
suite.localServerFeatureWrite.ApproveOrDenyWrite(msg, result)
}

suite.localServerFeatureWrite.AddWriteApprovalCallback(cb)
Expand All @@ -422,7 +428,11 @@ func (suite *DeviceClassificationTestSuite) Test_Write_Callback() {
suite.senderMock.EXPECT().ResultError(mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()

cb = func(msg *api.Message) {
suite.localServerFeatureWrite.ApproveOrDenyWrite(msg, false, "not allowed by application")
result := model.ErrorType{
ErrorNumber: 7,
Description: util.Ptr(model.DescriptionType("not allowed by application")),
}
suite.localServerFeatureWrite.ApproveOrDenyWrite(msg, result)
}

suite.localServerFeatureWrite.AddWriteApprovalCallback(cb)
Expand Down Expand Up @@ -451,7 +461,10 @@ func (suite *DeviceClassificationTestSuite) Test_Write_Callback_Timeout() {

cb := func(msg *api.Message) {
time.Sleep(time.Second * 1)
suite.localServerFeatureWrite.ApproveOrDenyWrite(msg, true, "")
result := model.ErrorType{
ErrorNumber: 0,
}
suite.localServerFeatureWrite.ApproveOrDenyWrite(msg, result)
}

suite.localServerFeatureWrite.AddWriteApprovalCallback(cb)
Expand Down

0 comments on commit df8b598

Please sign in to comment.