diff --git a/internal/wrphandlers/mocktr181/handler.go b/internal/wrphandlers/mocktr181/handler.go index 1ee5198..4bb08bb 100644 --- a/internal/wrphandlers/mocktr181/handler.go +++ b/internal/wrphandlers/mocktr181/handler.go @@ -205,32 +205,70 @@ func (h Handler) set(tr181 *Tr181Payload) (int64, []byte, error) { Names: tr181.Names, StatusCode: http.StatusAccepted, } + + var ( + writableParams []*MockParameter + failedParams []Parameter + ) + // Check for any parameters that are not writable. for _, parameter := range tr181.Parameters { + var found bool for i := range h.parameters { mockParameter := &h.parameters[i] if mockParameter.Name != parameter.Name { continue } - switch mockParameter.Access { - case "w", "wr", "rw": - mockParameter.Value = parameter.Value - mockParameter.DataType = parameter.DataType - mockParameter.Attributes = parameter.Attributes - result.Parameters = append(result.Parameters, Parameter{ - Name: mockParameter.Name, - Value: mockParameter.Value, - DataType: mockParameter.DataType, - Attributes: mockParameter.Attributes, - Message: "Success", - }) - default: - result.Parameters = append(result.Parameters, Parameter{ - Name: mockParameter.Name, - Message: "Parameter is not writable", - }) - result.StatusCode = 520 + // Check whether mockParameter is writable. + if strings.Contains(mockParameter.Access, "w") { + found = true + // Add mockParameter to the list of parameters to be updated. + writableParams = append(writableParams, mockParameter) + continue } + + // mockParameter is not writable. + failedParams = append(failedParams, Parameter{ + Name: mockParameter.Name, + Message: "Parameter is not writable", + }) + } + + if !found { + // Requested parameter was not found. + failedParams = append(failedParams, Parameter{ + Name: parameter.Name, + Message: "Invalid parameter name", + }) + } + } + + // Check if any parameters failed. + if len(failedParams) != 0 { + // If any parameter failed, then do not apply any changes to the parameters in writableParams. + writableParams = nil + result.Parameters = failedParams + result.StatusCode = 520 + } + + // If all the selected parameters are writable, then update the parameters. Otherwise, do nothing. + for _, parameter := range tr181.Parameters { + // writableParams will be nil if any parameters failed (i.e.: were not writable). + for _, mockParameter := range writableParams { + if mockParameter.Name != parameter.Name { + continue + } + + mockParameter.Value = parameter.Value + mockParameter.DataType = parameter.DataType + mockParameter.Attributes = parameter.Attributes + result.Parameters = append(result.Parameters, Parameter{ + Name: mockParameter.Name, + Value: mockParameter.Value, + DataType: mockParameter.DataType, + Attributes: mockParameter.Attributes, + Message: "Success", + }) } }