Permalink
Browse files

initial commit

  • Loading branch information...
lasselukkari committed May 17, 2014
0 parents commit e30b902f522902a0571ed317a84f17e3e889a768
Showing with 18,177 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +15 −0 PhoneGap-BT-Ajax/README.md
  3. +257 −0 PhoneGap-BT-Ajax/btajax.js
  4. +91 −0 README.md
  5. +1,474 −0 aWOT/CC3000/Adafruit_CC3000.cpp
  6. +175 −0 aWOT/CC3000/Adafruit_CC3000.h
  7. +211 −0 aWOT/CC3000/Adafruit_CC3000_Server.cpp
  8. +98 −0 aWOT/CC3000/Adafruit_CC3000_Server.h
  9. +21 −0 aWOT/CC3000/README.txt
  10. +747 −0 aWOT/CC3000/ccspi.cpp
  11. +90 −0 aWOT/CC3000/ccspi.h
  12. +20 −0 aWOT/CC3000/license.txt
  13. +195 −0 aWOT/CC3000/utility/cc3000_common.cpp
  14. +385 −0 aWOT/CC3000/utility/cc3000_common.h
  15. +124 −0 aWOT/CC3000/utility/debug.cpp
  16. +67 −0 aWOT/CC3000/utility/debug.h
  17. +871 −0 aWOT/CC3000/utility/evnt_handler.cpp
  18. +175 −0 aWOT/CC3000/utility/evnt_handler.h
  19. +239 −0 aWOT/CC3000/utility/hci.cpp
  20. +336 −0 aWOT/CC3000/utility/hci.h
  21. +55 −0 aWOT/CC3000/utility/host_driver_version.h
  22. +476 −0 aWOT/CC3000/utility/netapp.cpp
  23. +353 −0 aWOT/CC3000/utility/netapp.h
  24. +376 −0 aWOT/CC3000/utility/nvmem.cpp
  25. +256 −0 aWOT/CC3000/utility/nvmem.h
  26. +533 −0 aWOT/CC3000/utility/security.cpp
  27. +135 −0 aWOT/CC3000/utility/security.h
  28. +664 −0 aWOT/CC3000/utility/sntp.cpp
  29. +409 −0 aWOT/CC3000/utility/sntp.h
  30. +1,190 −0 aWOT/CC3000/utility/socket.cpp
  31. +691 −0 aWOT/CC3000/utility/socket.h
  32. +1,261 −0 aWOT/CC3000/utility/wlan.cpp
  33. +533 −0 aWOT/CC3000/utility/wlan.h
  34. +120 −0 aWOT/CC3000ClientAdapter.cpp
  35. +45 −0 aWOT/CC3000ClientAdapter.h
  36. +24 −0 aWOT/CC3000ServerAdapter.cpp
  37. +23 −0 aWOT/CC3000ServerAdapter.h
  38. +27 −0 aWOT/ClientInterface.h
  39. +79 −0 aWOT/EthernetClientAdapter.cpp
  40. +42 −0 aWOT/EthernetClientAdapter.h
  41. +23 −0 aWOT/EthernetServerAdapter.cpp
  42. +22 −0 aWOT/EthernetServerAdapter.h
  43. +880 −0 aWOT/HTTPServer.cpp
  44. +309 −0 aWOT/HTTPServer.h
  45. +479 −0 aWOT/HTTPStore.cpp
  46. +112 −0 aWOT/HTTPStore.h
  47. +124 −0 aWOT/MemoryStore.cpp
  48. +39 −0 aWOT/MemoryStore.h
  49. +348 −0 aWOT/SDStore.cpp
  50. +41 −0 aWOT/SDStore.h
  51. +103 −0 aWOT/SerialClientAdapter.cpp
  52. +35 −0 aWOT/SerialClientAdapter.h
  53. +28 −0 aWOT/SerialServerAdapter.cpp
  54. +34 −0 aWOT/SerialServerAdapter.h
  55. +11 −0 aWOT/ServerInterface.h
  56. +1,331 −0 aWOT/aJSON.cpp
  57. +255 −0 aWOT/aJSON.h
  58. +35 −0 aWOT/aJsonStore.h
  59. +318 −0 aWOT/examples/AppleTVRemote/AppleTVRemote.ino
  60. +134 −0 aWOT/examples/QUnitTests/QUnitTests.ino
  61. +143 −0 aWOT/examples/RCSwitch/RCSwitch.ino
  62. +28 −0 aWOT/examples/SensorNode/SensorNode.ino
  63. +48 −0 aWOT/examples/ServerNode/ServerNode.ino
  64. +132 −0 aWOT/examples/WeatherStation/WeatherStation.ino
  65. +66 −0 aWOT/examples/WoTBlink/WoTBlink.ino
  66. +35 −0 aWOT/examples/WoTBlinkRemote/WoTBlinkRemote.ino
  67. +125 −0 aWOT/utility/stringbuffer.c
  68. +55 −0 aWOT/utility/stringbuffer.h
@@ -0,0 +1 @@
+.DS_Store
@@ -0,0 +1,15 @@
+Proof of concept level prototype for using HTTP services over serial connections in PhoneCap applications.
+
+Redirects the HTTP requests to phones Bluetooth serial and parses the response.
+
+Transparent when works, often doesn't.
+
+##Usage
+
+- Import the BT library to your project. https://github.com/don/BluetoothSerial
+- Include jQuery
+- Include the btajax.js
+- $.ajax.useBluetooth = true;
+
+##Example
+https://www.cs.helsinki.fi/u/ljlukkar/wot/
@@ -0,0 +1,257 @@
+function ResponseParser() {
+
+ this.states = {
+ PARSING_HEADERS: 0,
+ PARSING_BODY: 1,
+ PARSING_RESPONSE_STATUS: 2
+ };
+
+ this.responseStatusExpression = /HTTP\/(\d)\.(\d) (\d{3}) (.*)/i;
+
+}
+
+ResponseParser.prototype.parse = function (data, callback) {
+
+ var res = {
+ body: "",
+ statusCode: 0,
+ statusText: "",
+ httpVersion: 0,
+ headers: []
+ };
+ var current_line = [];
+
+ var state = this.states.PARSING_RESPONSE_STATUS;
+
+ for (var i = 0; i < data.length; i++) {
+
+ var c = data.charAt(i);
+
+ if (state == this.states.PARSING_BODY) {
+ try {
+ res.body = JSON.parse(data.substring(i, data.length));
+ }
+ catch (e) {
+ res.body = data.substring(i, data.length);
+ }
+ break;
+ }
+ if (c == '\n') {
+ if (current_line.length == 0) {
+ state = this.states.PARSING_BODY;
+ } else {
+ var line = current_line.join("");
+ if (state == this.states.PARSING_RESPONSE_STATUS) {
+ var match = this.responseStatusExpression.exec(line);
+ if (match == null) {
+ return callback(new Error("Failed to parse response"), null);
+ }
+ res.statusCode = match[3];
+ }
+ else {
+ var lastIndex = line.indexOf(":");
+ res.headers.push(
+ {
+ name: line.substring(0, lastIndex),
+ value: line.substring(lastIndex + 1).trim()
+ }
+ );
+ }
+ state = this.states.PARSING_HEADERS;
+ current_line = [];
+ }
+ continue;
+ } else if (c == '\r') {
+ continue;
+ }
+
+ current_line.push(c);
+ }
+
+ return callback(null, res);
+};
+
+
+(function ($) {
+
+ var oldAjax = $.ajax;
+
+ $.ajax = function (options) {
+
+ var deferred = $.Deferred();
+
+ if ($.ajax.useBluetooth === false) {
+ return oldAjax.apply(this, arguments);
+ }
+
+ bluetoothSerial.isConnected(
+ function () {
+ makeRequest();
+ },
+ function () {
+ connectBT(function (err) {
+ if (!err) {
+
+ makeRequest();
+ }
+ else {
+ console.log(err);
+ }
+
+ });
+ }
+ );
+
+ function connectBT (cb) {
+
+ bluetoothSerial.list(
+ function (results) {
+
+ var deviceList = [];
+
+ for (var i = 0; i < results.length; i++) {
+ var deviceListItem = {
+ classNumber: results[i].class,
+ name: results[i].name,
+ address: results[i].address
+ };
+
+ deviceList.push(deviceListItem);
+ }
+
+ var connectPopupTemplate =
+ '<div id="bt-ajax-popup">' +
+ '<div class="container">' +
+ '<div class="form-group">' +
+ '<label for="select-bt-device"><h3>Connect to device</h3></label>' +
+ '<select id = "select-bt-device" class="form-control">' +
+ '<option selected disabled>Choose one...</option>' +
+ '</select>' +
+ '</div>' +
+ '<a class="btn btn-primary" id="close-bt-ajax-popup">Close<a>' +
+ '</div>' +
+ '</div>';
+
+
+ var connectPopup = $(connectPopupTemplate);
+
+ var options = connectPopup.find('#select-bt-device');
+
+ for (i = 0; i < deviceList.length; i++) {
+ var option = $('<option value="' + deviceList[i].address + '">' + deviceList[i].name + '</option>');
+ options.append(option);
+ }
+
+ options.on('change', function () {
+ bluetoothSerial.connect(this.value, function () {
+ $("#bt-ajax-popup").remove();
+ cb();
+
+ }, function () {
+ cb(new Error('Connection failed'));
+ });
+ });
+
+ connectPopup.find('#close-bt-ajax-popup').on('click', function () {
+ $("#bt-ajax-popup").remove();
+ cb(new Error('User cancelled'));
+ });
+
+ connectPopup.css("width", $(window).width());
+ connectPopup.css("height", $(window).height());
+ connectPopup.css("position", "absolute");
+ connectPopup.css("top", 0);
+ connectPopup.css("left", 0);
+ connectPopup.css("left", 0);
+ connectPopup.css("z-index", 10000000000);
+ connectPopup.css("background", "#fff");
+
+ $("body").append(connectPopup);
+
+
+ },
+ function (error) {
+ console.log(error);
+ }
+ );
+
+ }
+
+
+ function makeRequest() {
+ var request = "";
+
+ options.url = (options.url[0] == '/') ? options.url.substr(1) : options.url;
+
+
+ switch (options.type) {
+ case 'POST':
+ request += "POST /" + options.url + " HTTP 1.0\r\n";
+ request += "Content-Length: " + options.data.length + "\r\n\r\n";
+ request += options.data;
+ break;
+
+ case 'PUT':
+ request += "PUT /" + options.url + " HTTP 1.0\r\n";
+ request += "Content-Length: " + options.data.length + "\r\n\r\n";
+ request += options.data;
+ break;
+
+ case 'DELETE':
+ request += "DELETE /" + options.url + " HTTP 1.0 \r\n\r\n";
+ break;
+
+ case 'GET':
+ request += "GET /" + options.url + " HTTP 1.0 \r\n\r\n";
+ break;
+ }
+
+ console.log(request);
+
+ bluetoothSerial.clear(function () {
+ bluetoothSerial.write(request, function () {
+
+ checkIfMore(0);
+
+ function checkIfMore(last) {
+ setTimeout(function () {
+ bluetoothSerial.available(function (numBytes) {
+ if (last != 0 && last == numBytes) {
+ bluetoothSerial.read(function (data) {
+ console.log(data);
+ var parser = new ResponseParser();
+ parser.parse(data, function (err, res) {
+ if (!err) {
+ if (res.statusCode >= 200 && res.statusCode < 300) {
+ if (options.success) {
+ options.success(res.body);
+ }
+ if (options.complete) {
+ options.complete(res.body);
+ }
+ deferred.resolve(res.body);
+ }
+ else {
+ return deferred.reject(res.statusCode);
+ }
+ }
+ else {
+ return deferred.reject(err);
+ }
+ })
+
+ });
+ }
+ else {
+ checkIfMore(numBytes);
+ }
+ });
+ }, 100);
+ }
+ });
+ });
+ return deferred.promise();
+ }
+ };
+
+})(jQuery);
@@ -0,0 +1,91 @@
+aWOT
+==========
+
+Collection of tools to build RESTful stuff with Arduino compatible boards. Only some of the features work on Arduino Uno. There is just not enough ROM and RAM.
+
+It's a hack and under constant change. Prepare to get your hands dirty.
+
+## WoT?
+
+IoT, The Internet Of Things has been a buzzword for the past decade. While IoT focuses on getting devices connected to the global Internet and other existing networks with new novel communication medias and protocols Web Of Things, a sidekick of the IoT, is recycling the current web development patterns and tools to integrate everyday objects to the existing World Wide Web. In WoT the idea is to realize the IoT concept using the current web standards and practices. In order to do so WoT uses the REST architectural style implemented on top of the HTTP already in use all over the Internet.
+
+The WoT concept can be taken a step further to allow the communications over other protocol stacks than the traditional TCP. By implementing the HTTP client and server components so that they don’t directly operate the underlying transport protocol but instead use an abstraction layer we are able to switch the whole protocol stack without making any changes to the actual software logic that is built on top of the components. A device designed to be controlled over Ethernet can be for example be managed over any number of compliant media such as Bluetooth , Wi-Fi, and serial port communications.
+
+## Usage
+
+```cpp
+#include <SPI.h>
+#include <Ethernet.h>
+#include <HTTPServer.h>
+#include <ServerInterface.h>
+#include <EthernetServerAdapter.h>
+#include <SerialServerAdapter.h>
+#include <aJSON.h>
+#include <aJsonStore.h>
+#include <MemoryStore.h>
+
+byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
+EthernetServer server(80);
+
+// Server interface adapters
+EthernetServerAdapter ethernetServerAdapter(&server);
+SerialServerAdapter serialServerAdapter(&Serial);
+
+HTTPServer webServer("");
+MemoryStore memoryStore;
+
+// Define a handler function
+void getModel(HTTPServer &server, HTTPServer::MethodType type, char ** routeParams, char *, bool) {
+
+ char * collentionName = routeParams[0];
+ int modelId = atoi(routeParams[1]);
+
+ aJsonObject* model = memoryStore.findModelFromCollection(collentionName, modelId);
+
+ if(model){
+ server.httpSuccess("application/json");
+ aJsonStream serverStream(&server);
+ aJson.print(model,&serverStream);
+ }
+ else{
+ server.httpNotFound();
+ }
+
+}
+
+void setup() {
+
+ Ethernet.begin(mac);
+ Serial.begin(9600);
+
+ // Setup routing
+ webServer.onGET("examples/:modelId", &getModel);
+
+ // Populate dummy data
+ memoryStore.createCollection("examples");
+
+ for (int i = 0; i < 5; i++){
+ memoryStore.saveModelToCollection("examples", aJson.parse("{\"test\":\"test\"}"));
+ }
+
+}
+
+// Process incoming requests from serial and ethernet
+void loop() {
+ webServer.processConnection(&serialServerAdapter);
+ webServer.processConnection(&ethernetServerAdapter);
+}
+```
+## Performance
+
+With Wiznet W5100 and Arduino Mega CRUD operations with memory store take ~100 ms and with Teensy 3.1 and W5200 ~40ms.
+
+## Integration testing
+
+See QUnit example
+
+## Links
+
+https://www.cs.helsinki.fi/u/ljlukkar/wot/
+http://hackaday.com/2014/04/14/fixing-apple-tvs-terrible-ui/
+https://www.cs.helsinki.fi/u/ljlukkar/appletv/
Oops, something went wrong.

0 comments on commit e30b902

Please sign in to comment.