Service declaration

## Contents

- Your rights

- Introduction

- Component model

- Component description file

  - Preprocessing

  - Elements of a description file

  - Data type and constant definitions

  - Module declaration

  - Component declaration

  - Interface declaration

  - IDS declaration

  - Port declaration

  - Task declaration

  - Service declaration

  - Codel declaration

  - Identifiers and reserved keywords

  - Pragmas

  - Grammar reference

- GenoM IDL mappings

- Running GenoM

- Tutorials

- Templates

## Service declaration

### Attribute declaration

Attributes are variables that control the behavior of the component. They are stored in the `ids` and they can be changed dynamically by the codels during execution. Genom provides a way to set and get attributes through the chosen middleware.

An "attribute" declaration creates a service that can either set or get one or several attibutes.

(26) `attribute ::=` | `"attribute"` `identifier` `"("` `attribute-parameters` `")"` `opt-properties` `";"`
(30) `attribute-parameters ::=` | `[` `{` `attribute-parameter` `","` `}` `attribute-parameter` `]`
(31) `attribute-parameter ::=` | `parameter-dir` `(` `parameter-variable` `opt-initializer` `|` `parameter-variable` `"::"` `identifier` `opt-initializer` `|` `"::"` `identifier` `opt-initializer` `)`
(125) `opt-properties ::=` | `[` `"{"` `properties` `"}"` `]`
(126) `properties ::=` | `{` `property` `}`

Attributes can have a number of properties:

(29) `service-property ::=` | `(` `"task"` `identifier` `|` `"interrupts"` `identifier-list` `|` `"before"` `identifier-list` `|` `"after"` `identifier-list` `|` `"validate"` `codel` `|` `"local"` `local-variables` `|` `"visibility"` `visibility-attribute` `)` `";"`

doc

  A string that describes the functionality of the service.

validate

  A "validate" codel can be declared to check the validity of input data and throw an exception in case of a problem. The `validate` codel is called before updating the value in the `ids`. The codel can throw an exception if the new value is not acceptable. In this case, the value of the corresponding `ids` member is not changed. If the codel returns no error, the value of the `ids` member is updated.

### Example

This would declare an attribute for changing the speed of a moving thing:

    component foo {
      ids { double velocity };

      exception invalid_speed;

      attribute SetSpeed(in velocity = 0.0: "Thing velocity")
      {
        doc "Change the velocity of a moving thing";
        validate check_velocity(local in velocity);
        throw invalid_speed;
      };
    };

In this example `SetSpeed` is a setter for the `velocity` variable (`in` keyword). It is possible to specify a default value ("= 0.0") and a small documentation string that is used by interactive applications to guide the user.

This example is of course not functional. The `velocity` value should be used by other codels and services as the reference value for actually controlling the moving thing.

### Function and activity declaration

(28) `service-kind ::=` | `"function"` `|` `"activity"`
(32) `service-parameters ::=` | `[` `{` `service-parameter` `","` `}` `service-parameter` `]`
(33) `service-parameter ::=` | `parameter-dir` `type-spec` `declarator` `opt-initializer`
(125) `opt-properties ::=` | `[` `"{"` `properties` `"}"` `]`
(126) `properties ::=` | `{` `property` `}`
(127) `property ::=` | `component-property` `|` `interface-property` `|` `task-property` `|` `service-property` `|` `codel-property` `|` `throw-property`
(29) `service-property ::=` | `(` `"task"` `identifier` `|` `"interrupts"` `identifier-list` `|` `"before"` `identifier-list` `|` `"after"` `identifier-list` `|` `"validate"` `codel` `|` `"local"` `local-variables` `|` `"visibility"` `visibility-attribute` `)` `";"`

### Function and activity parameters

(31) `attribute-parameter ::=` | `parameter-dir` `(` `parameter-variable` `opt-initializer` `|` `parameter-variable` `"::"` `identifier` `opt-initializer` `|` `"::"` `identifier` `opt-initializer` `)`
(33) `service-parameter ::=` | `parameter-dir` `type-spec` `declarator` `opt-initializer`
(44) `parameter-dir ::=` | `"in"` `|` `"out"` `|` `"inout"`
(45) `parameter-variable ::=` | `identifier` `|` `parameter-variable` `"."` `identifier` `|` `parameter-variable` `"["` `positive-int-const` `"]"`
(46) `opt-initializer ::=` | `[` `"="` `initializer` `]`
(47) `initializers ::=` | `[` `{` `initializer` `","` `}` `initializer` `]`
(48) `initializer ::=` | `initializer-value` `|` `":"` `string-literals` `|` `initializer-value` `":"` `string-literals`
(49) `initializer-value ::=` | `const-expr` `|` `"{"` `initializers` `"}"` `|` `"["` `positive-int-const` `"]"` `"="` `const-expr` `|` `"["` `positive-int-const` `"]"` `"="` `"{"` `initializers` `"}"` `|` `"["` `positive-int-const` `"]"` `"="` `|` `"."` `identifier` `"="` `const-expr` `|` `"."` `identifier` `"="` `"{"` `initializers` `"}"` `|` `"."` `identifier` `"="`