50
Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

Embed Size (px)

Citation preview

Page 1: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

Loosely-coupled Component InfrastructureUsing LooCI Contiki / OSGi / Android

1

Page 2: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

Structure

ProgrammingLooCI OSGiLooCI Contiki

Setup of environmentAssignment

2

Page 3: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

To start

Copy the virtual machine from USB stick on your drive

Get, install & open virtual box or vm softwareDownload Ask for the USB stick containing executables

Import the loociVM into your virtual machine :file -> import appliance -> select appliance -> select the copy -> next -> next.Password of VM is ‘user’

3

Page 4: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

To Start

Make sure that the VM network is set to “bridged adaptor”Settings -> network Or ctrl+s while in VM -> network -> adaptor1Or devices -> network adaptors

To change keyboard if required:1) Click on the icon placed at the top right corner.2) Click on System Settings3) Click on Keyboard icon4) Click on Layout Setting label.5) Add your language in the layout tab and put it as the first one in the list.

4

Page 5: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

LooCI OSGi

Component = OSGi bundleloociOsgi workspace : home/user/looci/looci.osgiTo create a new component:

Create new plug-in projectAdd LooCI services as dependency & required bundle in metaInfActivator must extend the LoociComponent class

• At meta-inf set activator to your component class

To create jar: Copy a build.xml file from another component (several present in osgi)Adjust name of projectDo ant build in eclipsefile “projectName.jar” in felix/looci/comps -> local component repository

Deploy : in gui : “deploy projectName.jar nodeIP osgi”

5

Page 6: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

OSGi : Declaring codebase

public class LoociSensor extends LoociCodebase {public LoociSensor () {

super("Sensordisplay", // component namenew short[] {EventTypes.TEMP_READING}, //provided

tempnew short[] {}); //nothing

required}

@Overridepublic ILoociInstance createLoociComponent() {

return new LoociSensorInstance(); //abstract factory pattern

}}

6

Page 7: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

OSGi : Declaring component

public class LoociSensorComponent extends LoociComponentimplements ITimeListener {

private LoociTimer timer;public void componentStart() {

timer = new LoociTimer(this, 20, true);rand = new Random();timer.start();

}public void componentStop() {

timer.stop();}

7

Page 8: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

OSGi: Declaring component:Sending events

@Overridepublic void receive(short eventID, byte[] payload) {

// do nothing, this is event producer}

@Overridepublic void doOnTimeEvent(LoociTimer expiredTimer) {

//callback for timer interfacebyte val = (byte)rand.nextInt(); //random valuepublish(EventTypes.TEMP_READING, new byte[]{val});

}}

8

Page 9: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

OSGi: Declaring componentReceiving event & print it out

@Overridepublic void receive(short eventID, byte[] payload) {

if(eventID == EventTypes.TEMP_READING){System.out.println(“received temp: “+ payload[0]);

}}

9

Page 10: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

LooCI Contiki

Create folder in component directoryCopy / create makefileEasiest => copy from existing project and rename projectname in make file and c and header file (copy a-testcomponent folder)Make sure link to looci is correct, can change due to pathTo declare component: C MacrosTo create a component:

Go to the contiki component folder• /home/user/looci/looci.contiki/looci/applications/

Enter : ‘make copy’• Potentially requires root in some cases, pw = user• Automatically puts file in component repository (if makefile.component set)

To deploy: use gui and name the component file

10

Page 11: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

Contiki: Sending/ receiving#define INT_TEMP_EVENT 402#define PROPERTY_ID_INTERVAL 1

struct state{struct etimer et;uint8_t interval;};static struct state initVar PROGMEM = {.interval = 10};

#define LOOCI_COMPONENT_NAME temp_sample#define LOOCI_NR_PROPERTIES 1LOOCI_PROPERTIES({PROP_TIME_INTERVAL,DATATYPE_BYTE,offsetof(struct state,interval),1,name});COMPONENT_INTERFACES(INT_TEMP_EVENT );COMPONENT_NO_RECEPTACLES();LOOCI_COMPONENT_INIT("Temp Sampling",struct state,&initVar);

11

Page 12: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

Contiki: Sending/ receivingstatic uint8_t activate(struct state* compState, void* data){

ETIMER_SET(&compState->et, compState->interval * CLOCK_SECOND);return LC_SUCCESS;

}

static uint8_t time(struct state* compState, void* data){uint8_t temp = rng();

PUBLISH_EVENT(TEMP_EVENT, (uint8_t*)&temp, sizeof(temp)); ETIMER_RESET(&compState->et);

return LC_SUCCESS;}COMP_FUNCS_INIT //THIS LINE MUST BE PRESENTCOMP_FUNC_ACTIVATE(activate)COMP_FUNC_TIMER(time)COMP_FUNCS_END(NULL)//THIS LINE MUST BE PRESENT

12

Page 13: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

LooCI Gui overview

GuiSimple Gui to manage LooCI platformsEnter commands, receive feedback

List of commands : enter “help”More info on command enter “help commandName”For more information : slides in the back

No access control in these VM’s Please be careful not to mess up other people’s configurationsWhen working with android device, only do wires, or ask for us !!

13

Page 14: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

Exercises:

Exercise 1: receive temp, filter, forward, press button, do buzzDeploy the necessary components Instantiate & wire all componentsActivate components -> should produce beep

Exercise 2:Write a forward temperature on button press componentDeploy & wire instead of the previous componentsResult should be the same

Exercise 3:Write a button press component for OSGiOn button press on your local machine / android device -> avr buzzes

14

Page 15: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

Sensor node

NODE: nodeXY

Exercise 1: Setup to realize

15

Gateway

XY = pcNr01, 02, 03,10, 11, etc

Aaaa::1 Aaab::1

TempFilter

OSGi NODE: aaab::XY

TutorialBack-end

Buzz_evEv = 403

ButtonSensor

Btn_evEv = 401

Buzzer

Temp_evEv = 402

TempSensor

nodeBC

Temp_evEv = 402

Page 16: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

Overview of addresses and ID’s

Your backend IP: aaab::XY (e.g. aaab::2 / aaab::12)Your back-end component: codebase ID = 10, component ID = 10tutorialButtonComp: codebase ID = 11, component ID = 11

Your node IP: nodeXY (e.g. node2 / node12)Temp filter : codebase ID = 2, component ID = 10Button sensor: codebase ID = 3, component ID = 11Buzzer : codebase ID = 4, component ID = 12ButtonTempCounter: codebase ID = 5, component ID = 13

Temp producer : nodeBCTemp producer component: codebase ID = 2, component ID = 10

• Please do not touch this node

Please: only use your own nodes16

Page 17: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

Start environment

Import VM, set network settings to bridged adaptorStart VMEach of you must receive a unique numbered node: nr is XYInitiate your IP address with the correct number

Double click the setup iconThe user password is asked

• Enter ‘user’

Your number is asked• Enter your own number• (Will keep asking for number until valid number is entered)

17

Page 18: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

Start LooCI

Start LooCI guiDouble click on loociGui icon on desktop

Open eclipse environments (icon on desktop) : /home/user/looci/looci.contiki workspace should be selected

Alternatively you can work in command line and use text editor/home/user/looci/looci.contiki/looci/applications/tutorialComponents//home/user/looci/looci.osgi/LoociComponents/

You now have a working environment and should be able to ping the nodes:

In gui enter ‘ping nodeXY’ with XY your number» Enter ‘help’ for list of commands» Enter ‘ls’ to see list of available components

18

Page 19: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

At end

Please click the end icon when you finishThis sends the log file containing the trace of your gui

This allows us to view where common mistakes were madeNo personal info is in these files

We identify by node numberFeel free to check

19

Page 20: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

1a Deploy the back-end After starting LooCI OSGi, the LooCI gui should appearDeploy and activate your backend component

“deploy tutorialBackend.jar aaab::XY osgi”• Deploys your backend component• Should return codebaseID 10

“instantiate 10 aaab::XY”• Creates component instance of backend

– a window should appear on your screen• Should return componentID 10

“activate 10 aaab::XY”• Activates your backend

XY is the number of the node you are assigned

20

Page 21: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

1b Instantiate and wire

Now, instantiate all the components on your nodeWe already deployed the necessary components to your nodeAfterwards you will deploy your own component

‘instantiate 2 nodeXY’ -> returns componentID 10 This instantiates the temp filter

‘instantiate 3 nodeXY’ -> returns componentID 11This instantiates the button sensor

‘instantiate 4 nodeXY’ -> returns componentID 12This instantiates the buzzer component

21

Page 22: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

1b Perform wirings on node

‘wireFrom temp_ev 0 nodeBC 10 nodeXY’Send temp events from sensor node broadcaster to filter

‘wireTo temp_ev 10 nodeXY aaab::XY’Send temp events from filter to your backend

‘wireTo button_ev 11 nodeXY aaab::XY’Send button press events from your node to your backend

‘wireFrom buzz_ev 0 :: 12 nodeXY’Deliver buzz events from any address to component 12 on your node

22

Page 23: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

1b Perform wirings backend

‘wireFrom temp_ev 0 nodeXY 10 aaab::XY’Deliver temp events from your node to component 10

‘wireFrom button_ev 0 nodeXY 10 aaab::XY’Deliver button events from your node to component 10

‘wireTo buzz_ev 10 aaab::XY nodeXY’Send buzz events from your backend to your node

23

Page 24: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

1c Activate components

‘activate 10 nodeXY’Activates the temp filter

‘activate 11 nodeXY’Activates the button sensor

‘activate 12 nodeXY’Activates the buzzer

=> press button on avr, you should see on your OSGi interface that an event comes in, and that it sends out a new buzz event.If it does not work : check wires & state of all componentsDo ‘getState 10 nodeXY’ to see if component 10 is active (returns 1)To view the setup, enter ‘draw nodeXY’ in the gui

This shows the current wirings on your avr raven node» NOTE: visualizer is still in beta, so might not be perfect

24

Page 25: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

Sensor node

NODE: nodeXY

Exercise 1: Setup realized

25

Gateway

XY = pcNr01, 02, 03,10, 11, etc

Aaaa::1 Aaab::1

TempFilter

OSGi NODE: aaab::XY

TutorialBack-end

Buzz_evEv = 403

ButtonSensor

Btn_evEv = 401

Buzzer

Temp_evEv = 402

TempSensor

nodeBC

Temp_evEv = 402

Page 26: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

1d Play around

Change the wires and the properties a bit, see how it affects your composition.For example: set temp filter property

Get lower bound of temp filter• getProperty 1 10 node1 byte

– Should return 0

Set property to 100 => should not allow any more events• setProperty 1 10 node1 100 byte

Please be careful and do not interrupt the nodes of others

26

Page 27: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

1e Deactivate

Deactivate temp filter‘deactivate 10 nodeXY ‘

• deactivate temp filter

‘deactivate 12 nodeXY ‘• Deactivate buzzer

27

Page 28: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

Sensor node

NODE: nodeXY

Setup to realize exercise 2

28

Gateway

XY = pcNr01, 02, 03,10, 11, etc

Aaaa::1 Aaab::1

ButtonTempCounter

OSGi NODE: aaab::XY

TutorialBack-end

Buzz_evEv = 403

ButtonSensorBtn_evEv = 401

Buzzer

Temp_evEv = 402

TempSensor

nodeBC

Temp_evEv = 402

Page 29: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

Exercise 2 Create a buttonTempCounter

Create a component that allows a temperature event to pass for each time you press the buttonGo to your Contiki eclipse environment (or other editor)

Looci contiki path/workspace : /home/user/looci/looci.contiki/loociComponent subfolder : applications/tutorialComponents/buttonTempCounter

Go to: applications/tutorialComponents/buttonTempCounter/Open buttonTempCounter.cWrite the necessary code to do the following:

If the button is pressed, you increment the nrToSendIf the temp reading comes in, and nrToSend > 0, then publish the temp reading again, and decremetn nrReads

Note, the nrReads is already a property in the exampleYou can check the tempSample and buttonSensor projects for aid

29

Page 30: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

2b Build and copy

Once you made your component you can build itOpen a linux terminal and go to

/looci/looci.contiki/looci/applications/tutorialComponents/buttonTempCounter

Enter “sudo make copy” -> this creates the component file if all goes wellThe component is copied in the looci component repositoryDo “ls” in LooCI Gui

• This should show that you now have a new component there

Now you have put the component in the LooCI component repository, from which you can easily deploy it

30

Page 31: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

2c Deploy and wire

Go back to your gui, and deploy and wire the component‘ deploy buttonTempCounter.comp nodeXY raven ‘

• Returns codebaseID 5

‘instantiate 5 nodeXY ‘• Returns componentID 13

‘ wireFrom temp_ev 0 nodeBC 13 nodeXY ‘‘ wireLocal button_ev 11 13 nodeXY ‘‘ wireTo temp_ev 13 nodeXY aaab::XY ‘‘ activate 13 nodeXY ‘

Component is now activated and should be running

31

Page 32: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

2d Using properties

Press the button a few timesDo ‘getProperty 1 13 nodeXY byte’

• This returns the number of reads the component still has to do• Wait a bit, do it again, see it decrease

Do ‘setProperty 1 13 nodeXY 5 byte’• Now you tell the component to let 5 readings through

32

Page 33: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

Sensor node

NODE: nodeXY

Setup realized exercise 2

33

Gateway

XY = pcNr01, 02, 03,10, 11, etc

Aaaa::1 Aaab::1

ButtonTempCounter

OSGi NODE: aaab::XY

TutorialBack-end

Buzz_evEv = 403

ButtonSensorBtn_evEv = 401

Buzzer

Temp_evEv = 402

TempSensor

nodeBC

Temp_evEv = 402

Page 34: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

Exercise 2: Learned lessons

This exercise showed the followingThe deployment interface between osgi and raven is identicalChanging internal configuration on raven did not affect the backend

• Easy adaptation of internal configuration

Changing component state very straight forwardCreating and integrating new components in the event flowProperty and events work seamless

You can do ‘draw nodeXY’Shows you the new component composition

• Remember, visualizer is still in beta

34

Page 35: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

Exercise 3: Make buzzer go off

You will create an OSGi component, that sends out a button event. In the first instance, this button event will be wired to the back-end component, which will then send out a buzz eventSwitch eclipse to osgi workspace

Go to OSGi workspace: project comp_tutorialButtonSender.Open the buttonSenderComponent

File -> switch workspace -> /home/user/looci/looci.osgiOr location:

/home/user/looci/looci.osgi/LoociComponent/ButtonSender

35

Page 36: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

Sensor node

NODE: nodeXY

Setup to realize exercise 3

36

Gateway

Aaaa::1 Aaab::1

ButtonTempCounter

OSGi NODE: aaab::XY

TutorialBack-end

Buzz_evEv = 403

ButtonSensorBtn_evEv = 401

Buzzer

Temp_evEv = 402

TempSensor

nodeBC

Temp_evEv = 402

ButtonSendComponent

Btn_evEv = 401

Page 37: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

3b Implement in OSGi

What you must do is make it publish a buttonEventImplement the relevant method.In the folder is an ant build file. Execute the build file

Right click on build.xml, Or do ‘ant’ in the right folder in terminal

This automatically puts the component jar in the component folder.Deploy, and wire the button press component to your back-end serverThis is a wireLocal command !!

37

Page 38: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

3c Deploy and wire

Commands:‘deploy tutorialButtonSender.jar aaab::XY osgi’ -> codebaseId 11‘instantiate 11 aaab::XY’ -> componentID 11‘wireLocal button_ev 11 10 aaab::XY’‘activate 11 aaab::XY’

Activate the buzzer again to hear buzzes:‘activate 12 nodeXY’

Now if you press the button on your gui, the buzzer goesDeactivate buzzer again ‘deactivate 12 nodeXY’

38

Page 39: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

Exercise 4 : Do a temp reading on button press in gui

Concept, we only take a temp reading if we press a buttonSend the button press event from back-end to node

wireTo button_ev 11 aaab::XY nodeXYDeliver button press event from back_end to component on node

wireFrom button_ev 0 aaab::XY 13 nodeXY

Extra:Wire the tempFilter component on Contiki between the buttonTempSender and the buttonTempCounter component

39

Page 40: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

Sensor node

NODE: nodeXY

Setup to realize exercise 4

40

Gateway

Aaaa::1 Aaab::1

ButtonTempCounter

OSGi NODE: aaab::XY

TutorialBack-end

Buzz_evEv = 403

ButtonSensorBtn_evEv = 401

Buzzer

Temp_evEv = 402

TempSensor

nodeBC

Temp_evEv = 402

ButtonSendComponent

Btn_evEv = 401

Btn_evEv = 401

Page 41: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

Exercise 5: Write a temp filter

You have seen how to write new components in contiki and osgiNow write an osgi component almost from scratchIt must filter temp events before being shown on the screenIt receives temp events, checks whether they’re inside the boundaries and publishes them.To start: Open the Comp_TutorialFilter project

Declare provided and required interfaces in LoociFilter.javaCreate necessary propertiesWrite filter logic in the receive method, and publish the event if it matchesExecute the ant build in eclipse

41

Page 42: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

Sensor node

NODE: nodeXY

Setup to realize exercise 5

42

Gateway

Aaaa::1 Aaab::1

ButtonTempCounter

OSGi NODE: aaab::XY

TutorialBack-end

Buzz_evEv = 403

ButtonSensorBtn_evEv = 401

Buzzer

Temp_evEv = 402

TempSensor

nodeBC

Temp_evEv = 402

ButtonSendComponent

Btn_evEv = 401

Btn_evEv = 401

TempFilter

Page 43: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

5b Deploy and wire filter

Insert the tempfilter between the back-end component and the remote node

Remove existing wire from remote node to back-end component at back-end (unwireFrom)Wire your buttonTempCounter remote component to the filter (wireFrom)Wire the filter component to the back-end component (wireLocal)Don’t forget to activate the components

43

Page 44: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

Exercise 6 : Use adroid as button and to log

Concept: add a wireless android device to the setup!!Ask for the android IP address and component to use !!Connect your unique component on the android device to your node.If button is pressed, a temperature reading is taken

You can also send out the button to your back-end => buzzerWire the temperature also to the correct OSGi component on androidBe carefull, android is a shared resource

44

Page 45: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

Lessons Learned

Homogeneous interface for heterogeneous devicesSoftware development in two languages, yet they interact the sameDistributed event bus

Changes on one node do not necessarily influence changes on anotherClashes are possible though !!

Properties and events work seamless togetherLimited overhead to create new behaviour

45

Page 46: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

Questions?

For any other questions with regards to LooCI:

Architecture/collaborations : [email protected]/Osgi : [email protected] : [email protected] : [email protected]

46

Page 47: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

Relevant commands

47

Command Arguments: (also available in gui by entering ‘help’)deploy file - address - nodetyperemoveCodebase codebase id - addressInstantiate codebase id - addressdestroyComponent component id - addressActivate component id - addressdeactivate component id - addresswireLocal event id - src component id - dst component id - addressunwireLocal event id - src component id - dst component id - addresswireFrom event id - src comp id - src address - dst comp id - dst addrunwireFrom event id - src comp id - src addr - dst comp id - dst addrwireTo event id - src component id - dst address - src addressunwireTo event id - src component id - dst address - src addressresetWires component id - addresssetProperty property id - comp id - addr - prop value - prop type

Page 48: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

Relevant commands (2)getComponentsOfCodebase codebase id - addressgetCodebaseName codebase id - addressgetCodebaseIDOfComponent component id - addressgetCodebaseNameOfComponent component id - addressgetComponentIDs addressgetState component id - addressgetProvidedInterfaces component id - addressgetRequiredInterfaces component id - addressgetLocalWires event id - src component id - dst component id - addressgetWiresTo event id - src component id - dst address - addressgetWiresFrom event id - src comp id - src addr - dst comp id - dst addrgetProperties component id - addressgetProperty property id - component id - address - property typegetPropertyInfo property id - component id - addressgetPlatformType address

48

Page 49: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

Relevant node addressing

NodeBC -> broadcast nodeAaab::xy -> your own back-endnodeXY -> node as defined in hosts file

Please make sure you only user your own nodes

49

Page 50: Loosely-coupled Component Infrastructure Using LooCI Contiki / OSGi / Android 1

Relevant event types

button_ev 401temp_ev 402buzz_ev 403

Relevant properties:We only used propertyId 1 to make it easy for everyoneFor filtering we propose to use propertyId 1 for the lower boundary and 2 for the upper boundary

50