项目作者: ssoudan

项目描述 :
A Service Discovery Protocol for Arduino-Xbee based sensor networks.
高级语言: C++
项目地址: git://github.com/ssoudan/SDP.git
创建时间: 2013-08-06T18:49:09Z
项目社区:https://github.com/ssoudan/SDP

开源协议:Apache License 2.0

下载


  1. _____ ____ ____
  2. / ___// __ \/ __ \
  3. \__ \/ / / / /_/ /
  4. ___/ / /_/ / ____/
  5. /____/_____/_/ ...
  6. Service Discovery Protocol for Embedded devices.

Sebastien Soudan sebastien.soudan@gmail.com 2013

GPLv3

A Service Discovery Protocol for Arduino-Xbee based sensor networks.

===============

Principle

  • One board register an action it can offer on a service
  • Another board want this action to be executed but have
    no clue on who is offering this service
  • This second guy, send a broadcast message to find the
    service: (FS message)
  • The first guy, or maybe another one, or both respond
    with a ‘find service’ response message (FSR) giving
    their addresses.
  • Next time first want to execute the action, he can
    just send directly a ‘do action’ message (DA) in
    unicast with the parameter payload and with get a
    response (or not) from the guy targeted by the message.
    Upon the reception of this ‘do action’ response message
    (DAR), the callback he has provided with the doAction,
    will be executed on the result of the execution of the
    remote method that has been packed in the DAR message
    together with the status in case this one is DONE.

Note that there are some ‘corner’ cases that are not yet supported by this lib:

  • the address of the first guy that replied to the FS will not be overriden by the
    one of the second guy (provider as well) which will get a seat in the
    Remote Service Directory but never be called because the search method sucks.
  • In case the first guy disappear, there is no timeout mechanism to remove the entry.
    Though in case of an error during the execution of an action (NOT_DONE status),
    the entry is removed.

Note that you can also use it with local serial port of the machine. This works great
to query the service from a Raspberry Pi using Sparkfun XBee explorer for example as
servers/collector.cpp does for example.

===============

To Do:

  • refactor the *Service into proper OOP code
  • extract XBee from the stuff
  • add timeout mechanism
  • refine service/action model (single callback for an entire service?)
  • [X] add location concept to the service
  • improve the way the xmit status is checked
  • [X] use it

===============

Getting started

My tree looks like something like:

  1. .../Arduino-Makefile/...
  2. .../libraries/AdafruitLCD
  3. .../libraries/AdafruitMPL115A2
  4. .../libraries/AdafruitRGBLCDShieldLibraryMaster
  5. .../libraries/RTClib
  6. .../libraries/SDP <- the lib we are talking about
  7. .../libraries/SDP/examples/Zero_v0/ <- the example thereafter
  8. .../libraries/SDP/examples/Zero_v0/Makefile <- [ the makefile next
  9. .../libraries/TFTLCD [ bullet point is about
  10. .../libraries/TKLCD
  11. .../libraries/TinkerKit
  12. .../libraries/USB_Host_Shield_20
  13. .../libraries/XBee <- one the lib dependencies
  14. .../Uno_v0/Uno_v0.ino
  • Create your own project Makefile similar to this one:
  1. #
  2. # Service Discovery Protocol - Makefile
  3. # @author: Sebastien Soudan <sebastien.soudan@gmail.com>
  4. #
  5. ARDMK_DIR=../../../../Arduino-Makefile/
  6. ARDUINO_DIR=/Applications/Arduino-1.0.5.app/Contents/Resources/Java
  7. # if you want some debug info (beware of the total size in RAM):
  8. #CPPFLAGS=-DDEBUG
  9. BOARD_TAG = uno
  10. MONITOR_PORT = /dev/cu.usb*
  11. ARDUINO_LIBS = AdafruitRGBLCDShieldLibraryMaster \
  12. AdafruitMPL115A2 \
  13. RTClib \
  14. SDP \
  15. XBee \
  16. Wire \
  17. Wire/utility
  18. include $(ARDMK_DIR)/arduino-mk/Arduino.mk
  • Create your own code but here is an example:

    1. #include <SDP.h> // Service Discovery lib
    2. // Xbee
    3. XBee xbee = XBee();
    4. // SDP
    5. SDP sdp = SDP(&xbee);
    6. // Remember to put the address of your XBee board here
    7. XBeeAddress64 local64 = XBeeAddress64(0xbadc0de, 0xdeadbeef);
    8. /**
    9. Service function to return epoch date on Jan, 1st 1970
    10. */
    11. size_t getDate(uint8_t *in, size_t inLength, uint8_t *out, size_t outLimit) {
    12. Serial.println("getDate");
    13. if (outLimit >= 4) {
    14. uint32_t epoc = 0;
    15. convertUint32toArray(epoc, out);
    16. return 4;
    17. }
    18. return 0;
    19. }
    20. void setup() {
    21. // Debugging output
    22. Serial.begin(9600);
    23. Serial.println("setup!");
    24. // Init xbee
    25. xbee.begin(Serial);
    26. sdp.setLocal64(local64);
    27. Serial.println("SDP init");
    28. // Service Discovery registration
    29. RegistrationStatus res = sdp.registerService(ServiceDiscovery::SDP_RTC,
    30. ServiceDiscovery::GET_VALUE,
    31. &getDate);
    32. if (!res) { /* Registration has failed */ }
    33. }
    34. /**
    35. Get packet read and processed.
    36. readPackets() returns a state giving some info about
    37. what has been done. But I removed it to save some place.
    38. */
    39. void processMessage() {
    40. /* SDPState state = */ sdp.readPackets();
    41. }
    42. /**
    43. Action callback.
    44. Executed when a reponse to a DO ACTION message has been
    45. sent, and the action has been successfully executed.
    46. */
    47. void actionCallback(uint8_t *data, size_t size) {
    48. // do something with the data
    49. }
    50. void loop() {
    51. processMessage();
    52. ActionStatus as = sdp.doAction(ServiceDiscovery::TEMPERATURE,
    53. ServiceDiscovery::GET_VALUE,
    54. 0,
    55. NULL,
    56. &actionCallback);
    57. // flash some led according to the status in 'as'?
    58. // do a lot of other stuff here.
    59. delay(300);
    60. }