]> oss.titaniummirror.com Git - tinyos-2.x.git/commitdiff
Initial commit of TinyLD (TOSThreads' dynamic loader)
authorliang_mike <liang_mike>
Wed, 4 Feb 2009 07:41:35 +0000 (07:41 +0000)
committerliang_mike <liang_mike>
Wed, 4 Feb 2009 07:41:35 +0000 (07:41 +0000)
37 files changed:
apps/tosthreads/tinyld/LoadFromRAM/LoadFromRAMAppC.nc [new file with mode: 0644]
apps/tosthreads/tinyld/LoadFromRAM/LoadFromRAMP.nc [new file with mode: 0644]
apps/tosthreads/tinyld/LoadFromRAM/Makefile [new file with mode: 0644]
apps/tosthreads/tinyld/LoadFromRAM/README [new file with mode: 0644]
apps/tosthreads/tinyld/LoadFromRAM/volumes-stm25p.xml [new file with mode: 0755]
apps/tosthreads/tinyld/SerialLoader/Blink.tos [new file with mode: 0644]
apps/tosthreads/tinyld/SerialLoader/Makefile [new file with mode: 0755]
apps/tosthreads/tinyld/SerialLoader/README [new file with mode: 0644]
apps/tosthreads/tinyld/SerialLoader/SerialLoader.h [new file with mode: 0755]
apps/tosthreads/tinyld/SerialLoader/SerialLoaderAppC.nc [new file with mode: 0755]
apps/tosthreads/tinyld/SerialLoader/SerialLoaderP.nc [new file with mode: 0755]
apps/tosthreads/tinyld/SerialLoader/serialloader.py [new file with mode: 0755]
apps/tosthreads/tinyld/SerialLoader/tinyos.py [new file with mode: 0755]
tools/tinyos/tosthreads/Makefile.am
tools/tinyos/tosthreads/tosthreads-gen-dynamic-app [new file with mode: 0755]
tools/tinyos/tosthreads/tosthreads-gen-dynamic-app.1 [new file with mode: 0755]
tos/lib/tosthreads/lib/tinyld/BigCrc.nc [new file with mode: 0644]
tos/lib/tosthreads/lib/tinyld/BigCrcC.nc [new file with mode: 0644]
tos/lib/tosthreads/lib/tinyld/BigCrcP.nc [new file with mode: 0644]
tos/lib/tosthreads/lib/tinyld/DynamicLoader.h [new file with mode: 0755]
tos/lib/tosthreads/lib/tinyld/DynamicLoader.nc [new file with mode: 0755]
tos/lib/tosthreads/lib/tinyld/DynamicLoaderC.nc [new file with mode: 0755]
tos/lib/tosthreads/lib/tinyld/DynamicLoaderP.nc [new file with mode: 0755]
tos/lib/tosthreads/lib/tinyld/LoadSourceMapC.nc [new file with mode: 0755]
tos/lib/tosthreads/lib/tinyld/LoadSourceMapP.nc [new file with mode: 0755]
tos/lib/tosthreads/lib/tinyld/MemoryStorageC.nc [new file with mode: 0755]
tos/lib/tosthreads/lib/tinyld/MemoryStorageP.nc [new file with mode: 0755]
tos/lib/tosthreads/lib/tinyld/NullVolumeMapC.nc [new file with mode: 0755]
tos/lib/tosthreads/lib/tinyld/PMManager.nc [new file with mode: 0644]
tos/lib/tosthreads/lib/tinyld/PMManagerC.nc [new file with mode: 0644]
tos/lib/tosthreads/lib/tinyld/PMManagerP.nc [new file with mode: 0644]
tos/lib/tosthreads/lib/tinyld/TosThreadApiC.nc [new file with mode: 0644]
tos/lib/tosthreads/lib/tinyld/UserButton.nc [new file with mode: 0755]
tos/lib/tosthreads/lib/tinyld/UserButtonC.nc [new file with mode: 0755]
tos/lib/tosthreads/lib/tinyld/UserButtonP.nc [new file with mode: 0755]
tos/lib/tosthreads/lib/tinyld/slcs_types.h [new file with mode: 0755]
tos/lib/tosthreads/lib/tinyld/tosthread_slcs_types.h [new file with mode: 0755]

diff --git a/apps/tosthreads/tinyld/LoadFromRAM/LoadFromRAMAppC.nc b/apps/tosthreads/tinyld/LoadFromRAM/LoadFromRAMAppC.nc
new file mode 100644 (file)
index 0000000..9664737
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2008 Johns Hopkins University.
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose, without fee, and without written
+ * agreement is hereby granted, provided that the above copyright
+ * notice, the (updated) modification history and the author appear in
+ * all copies of this source code.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, LOSS OF USE, DATA,
+ * OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+/**
+ * @author Chieh-Jan Mike Liang <cliang4@cs.jhu.edu>
+ */
+
+configuration LoadFromRAMAppC {}
+
+implementation {
+  components MainC,
+             LoadFromRAMP,
+             DynamicLoaderC,
+             LedsC;
+             
+  LoadFromRAMP.Boot -> MainC;
+  LoadFromRAMP.DynamicLoader -> DynamicLoaderC;
+  LoadFromRAMP.Leds -> LedsC;
+}
diff --git a/apps/tosthreads/tinyld/LoadFromRAM/LoadFromRAMP.nc b/apps/tosthreads/tinyld/LoadFromRAM/LoadFromRAMP.nc
new file mode 100644 (file)
index 0000000..3472345
--- /dev/null
@@ -0,0 +1,139 @@
+/*
+ * Copyright (c) 2008 Johns Hopkins University.
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose, without fee, and without written
+ * agreement is hereby granted, provided that the above copyright
+ * notice, the (updated) modification history and the author appear in
+ * all copies of this source code.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, LOSS OF USE, DATA,
+ * OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+/**
+ * @author Chieh-Jan Mike Liang <cliang4@cs.jhu.edu>
+ */
+
+module LoadFromRAMP 
+{
+  uses {
+    interface Boot;
+    interface DynamicLoader;
+    interface Leds;
+  }
+}
+
+implementation
+{
+  // The following blinks led 2
+  uint8_t code1[] = {
+    0x1c, 0x00, 0x01, 0x00, 0x03, 0x00, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00,
+    0x28, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x01, 0x00, 0x2c, 0x00, 0x08, 0x00, 0x44, 0x00, 0x09, 0x00,
+    0x56, 0x00, 0x0a, 0x00, 0x32, 0x00, 0x30, 0x00, 0x24, 0x00, 0x0f, 0x42, 0x12, 0xc3, 0x0f, 0x10,
+    0x0f, 0x11, 0x0f, 0x11, 0x5f, 0xf3, 0x32, 0xc2, 0x03, 0x43, 0x7f, 0xf3, 0x30, 0x41, 0x4f, 0x93,
+    0x01, 0x24, 0x32, 0xd2, 0x30, 0x41, 0x3c, 0x40, 0xf4, 0x01, 0x0d, 0x43, 0x3e, 0x40, 0x00, 0x00,
+    0x3f, 0x40, 0x00, 0x00, 0xb0, 0x12, 0x00, 0x00, 0x30, 0x41, 0xb0, 0x12, 0x00, 0x00, 0x3e, 0x40,
+    0xe8, 0x03, 0x0f, 0x43, 0xb0, 0x12, 0x4e, 0x00, 0xf8, 0x3f, 0x30, 0x41, 0xb0, 0x12, 0x00, 0x00,
+    0x3e, 0x40, 0xe8, 0x03, 0x0f, 0x43, 0xb0, 0x12, 0x60, 0x00, 0xf8, 0x3f, 0x30, 0x41, 0xb0, 0x12,
+    0x00, 0x00, 0x3e, 0x40, 0xe8, 0x03, 0x0f, 0x43, 0xb0, 0x12, 0x00, 0x00, 0xf8, 0x3f, 0x30, 0x41
+  };
+
+  // The following is BaseStationForC
+  uint8_t code2[] = {
+    0x1c, 0x00, 0x05, 0x00, 0x58, 0x01, 0x1e, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x26, 0x03, 0xac, 0x00,
+    0x26, 0x00, 0xae, 0x00, 0x88, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x01, 0x00, 0x52, 0x00, 0x02, 0x00,
+    0x64, 0x00, 0x01, 0x00, 0x44, 0x00, 0x08, 0x00, 0xe2, 0x00, 0x09, 0x00, 0x00, 0x02, 0x0a, 0x00,
+    0xdc, 0x00, 0x0b, 0x00, 0x32, 0x00, 0x0d, 0x00, 0x10, 0x03, 0x0e, 0x00, 0x98, 0x01, 0x11, 0x00,
+    0xd6, 0x02, 0x12, 0x00, 0xce, 0x02, 0x14, 0x00, 0x9c, 0x02, 0x16, 0x00, 0xc6, 0x02, 0x1a, 0x00,
+    0x94, 0x02, 0x1b, 0x00, 0xde, 0x02, 0x22, 0x00, 0x2e, 0x00, 0x24, 0x00, 0xb6, 0x00, 0x25, 0x00,
+    0xfa, 0x02, 0x28, 0x00, 0x84, 0x02, 0x29, 0x00, 0x7c, 0x02, 0x2b, 0x00, 0xee, 0x02, 0x2d, 0x00,
+    0x74, 0x02, 0x31, 0x00, 0xe6, 0x02, 0x32, 0x00, 0x8c, 0x02, 0x3f, 0x00, 0xa2, 0x00, 0x40, 0x00,
+    0x04, 0x01, 0x42, 0x00, 0x18, 0x01, 0x44, 0x00, 0x98, 0x00, 0x45, 0x00, 0xec, 0x00, 0x46, 0x00,
+    0x0e, 0x01, 0x51, 0x00, 0x8a, 0x01, 0x6d, 0x00, 0x6c, 0x01, 0x90, 0x00, 0x22, 0x00, 0xb0, 0x00,
+    0x3c, 0x00, 0x84, 0x01, 0x4e, 0x00, 0x92, 0x01, 0x60, 0x00, 0x0a, 0x03, 0x72, 0x00, 0x18, 0x03,
+    0x84, 0x00, 0xbe, 0x00, 0xba, 0x00, 0x28, 0x01, 0xf4, 0x00, 0x00, 0x00, 0x46, 0x01, 0xa0, 0x01,
+    0x9c, 0x01, 0x18, 0x02, 0xbc, 0x01, 0xb8, 0x02, 0xfc, 0x01, 0x66, 0x02, 0x08, 0x02, 0x0f, 0x42,
+    0x12, 0xc3, 0x0f, 0x10, 0x0f, 0x11, 0x0f, 0x11, 0x5f, 0xf3, 0x32, 0xc2, 0x03, 0x43, 0x7f, 0xf3,
+    0x30, 0x41, 0x4f, 0x93, 0x01, 0x24, 0x32, 0xd2, 0x30, 0x41, 0x3f, 0x40, 0x40, 0x00, 0xb0, 0x12,
+    0x2a, 0x00, 0x3f, 0x40, 0x76, 0x00, 0xb0, 0x12, 0x00, 0x00, 0xb0, 0x12, 0x00, 0x00, 0xb0, 0x12,
+    0x00, 0x00, 0x3c, 0x40, 0xc8, 0x00, 0x0d, 0x43, 0x3e, 0x40, 0x00, 0x00, 0x3f, 0x40, 0xb2, 0x00,
+    0xb0, 0x12, 0x56, 0x00, 0x3c, 0x40, 0xc8, 0x00, 0x0d, 0x43, 0x3e, 0x40, 0x00, 0x00, 0x3f, 0x40,
+    0x00, 0x00, 0xb0, 0x12, 0x68, 0x00, 0x3c, 0x40, 0xc8, 0x00, 0x0d, 0x43, 0x3e, 0x40, 0x00, 0x00,
+    0x3f, 0x40, 0x00, 0x00, 0xb0, 0x12, 0x7a, 0x00, 0x3c, 0x40, 0xc8, 0x00, 0x0d, 0x43, 0x3e, 0x40,
+    0x00, 0x00, 0x3f, 0x40, 0x0c, 0x03, 0xb0, 0x12, 0x8c, 0x00, 0x3c, 0x40, 0xc8, 0x00, 0x0d, 0x43,
+    0x3e, 0x40, 0x00, 0x00, 0x3f, 0x40, 0x00, 0x00, 0xb0, 0x12, 0x00, 0x00, 0x30, 0x41, 0x0b, 0x12,
+    0x0b, 0x4f, 0x2f, 0x52, 0xb0, 0x12, 0x00, 0x00, 0x0f, 0x4b, 0x3f, 0x50, 0x0a, 0x00, 0xb0, 0x12,
+    0x00, 0x00, 0xcb, 0x43, 0xaa, 0x00, 0xcb, 0x43, 0xab, 0x00, 0x3b, 0x41, 0x30, 0x41, 0x3e, 0x40,
+    0x86, 0x01, 0x3f, 0x40, 0x00, 0x00, 0xb0, 0x12, 0x8e, 0x01, 0x30, 0x41, 0x0b, 0x12, 0x0a, 0x12,
+    0x09, 0x12, 0x31, 0x80, 0x34, 0x00, 0x09, 0x4f, 0x0b, 0x4e, 0x7c, 0x43, 0x0d, 0x43, 0x0e, 0x43,
+    0x0f, 0x41, 0x89, 0x12, 0x4f, 0x93, 0x03, 0x24, 0xb0, 0x12, 0x00, 0x00, 0xf6, 0x3f, 0xb0, 0x12,
+    0x00, 0x00, 0x0a, 0x4b, 0x2a, 0x52, 0x0f, 0x4a, 0xb0, 0x12, 0xb6, 0x01, 0x0e, 0x41, 0x0f, 0x4b,
+    0xb0, 0x12, 0x00, 0x00, 0x5f, 0x93, 0x07, 0x20, 0x0e, 0x4a, 0x0f, 0x4b, 0x3f, 0x50, 0x0a, 0x00,
+    0xb0, 0x12, 0xcc, 0x01, 0xf3, 0x3f, 0x0f, 0x4b, 0x2f, 0x52, 0xb0, 0x12, 0xe6, 0x01, 0x0f, 0x4b,
+    0x3f, 0x50, 0x0a, 0x00, 0xb0, 0x12, 0xf0, 0x01, 0xd8, 0x3f, 0x31, 0x50, 0x34, 0x00, 0x39, 0x41,
+    0x3a, 0x41, 0x3b, 0x41, 0x30, 0x41, 0x0b, 0x12, 0x0a, 0x12, 0x09, 0x12, 0x09, 0x4f, 0x0d, 0x4e,
+    0xff, 0x90, 0x03, 0x00, 0xaa, 0x00, 0x20, 0x2c, 0x5f, 0x4f, 0xab, 0x00, 0x0a, 0x4f, 0x3c, 0x40,
+    0x34, 0x00, 0xb0, 0x12, 0x48, 0x02, 0x0e, 0x59, 0x3e, 0x50, 0x0e, 0x00, 0x0f, 0x4d, 0x3d, 0x40,
+    0x34, 0x00, 0xfe, 0x4f, 0x00, 0x00, 0x1e, 0x53, 0x1d, 0x83, 0xfb, 0x23, 0x5f, 0x49, 0xab, 0x00,
+    0x1f, 0x53, 0x0c, 0x4f, 0x3a, 0x40, 0x03, 0x00, 0xb0, 0x12, 0x3e, 0x02, 0xc9, 0x4e, 0xab, 0x00,
+    0xd9, 0x53, 0xaa, 0x00, 0x0f, 0x43, 0x01, 0x3c, 0x1f, 0x43, 0x39, 0x41, 0x3a, 0x41, 0x3b, 0x41,
+    0x30, 0x41, 0x3e, 0x40, 0x94, 0x01, 0x3f, 0x40, 0x00, 0x00, 0xb0, 0x12, 0x14, 0x03, 0x30, 0x41,
+    0x3e, 0x40, 0x00, 0x00, 0x3f, 0x40, 0xf4, 0x01, 0xb0, 0x12, 0x22, 0x03, 0x30, 0x41, 0x0b, 0x12,
+    0x0a, 0x12, 0x09, 0x12, 0x31, 0x80, 0x34, 0x00, 0x09, 0x4f, 0x0b, 0x4e, 0x0a, 0x4b, 0x2a, 0x52,
+    0x0f, 0x4a, 0xb0, 0x12, 0x00, 0x00, 0x0f, 0x4b, 0xb0, 0x12, 0x00, 0x00, 0x0f, 0x93, 0x07, 0x20,
+    0x0e, 0x4a, 0x0f, 0x4b, 0x3f, 0x50, 0x0a, 0x00, 0xb0, 0x12, 0x00, 0x00, 0xf4, 0x3f, 0x0d, 0x41,
+    0x3e, 0x40, 0x34, 0x00, 0xfd, 0x4f, 0x00, 0x00, 0x1d, 0x53, 0x1e, 0x83, 0xfb, 0x23, 0x0f, 0x4b,
+    0x2f, 0x52, 0xb0, 0x12, 0x00, 0x00, 0x0f, 0x4b, 0x3f, 0x50, 0x0a, 0x00, 0xb0, 0x12, 0x00, 0x00,
+    0x39, 0x90, 0xa8, 0x02, 0x06, 0x24, 0x0f, 0x41, 0xb0, 0x12, 0x00, 0x00, 0xb0, 0x12, 0x00, 0x00,
+    0xd5, 0x3f, 0x0f, 0x41, 0xb0, 0x12, 0x00, 0x00, 0xf9, 0x3f, 0x31, 0x50, 0x34, 0x00, 0x39, 0x41,
+    0x3a, 0x41, 0x3b, 0x41, 0x30, 0x41, 0x0b, 0x12, 0x0a, 0x12, 0x09, 0x12, 0x08, 0x12, 0x09, 0x4f,
+    0x58, 0x4f, 0xaa, 0x00, 0x48, 0x93, 0x18, 0x24, 0x5f, 0x4f, 0xab, 0x00, 0x4e, 0x48, 0x0f, 0x8e,
+    0x3f, 0x50, 0x03, 0x00, 0x0c, 0x4f, 0x3a, 0x40, 0x03, 0x00, 0xb0, 0x12, 0x00, 0x00, 0x0a, 0x4e,
+    0x3c, 0x40, 0x34, 0x00, 0xb0, 0x12, 0x00, 0x00, 0x0f, 0x4e, 0x0f, 0x59, 0x78, 0x53, 0xc9, 0x48,
+    0xaa, 0x00, 0x3f, 0x50, 0x0e, 0x00, 0x01, 0x3c, 0x0f, 0x43, 0x38, 0x41, 0x39, 0x41, 0x3a, 0x41,
+    0x3b, 0x41, 0x30, 0x41, 0x0b, 0x12, 0x0a, 0x12, 0x09, 0x12, 0x08, 0x12, 0x07, 0x12, 0x0b, 0x4f,
+    0xb0, 0x12, 0x00, 0x00, 0x47, 0x4f, 0x0f, 0x4b, 0xb0, 0x12, 0x00, 0x00, 0x09, 0x4f, 0x0f, 0x4b,
+    0xb0, 0x12, 0x00, 0x00, 0x08, 0x4f, 0x0f, 0x4b, 0xb0, 0x12, 0x00, 0x00, 0x4a, 0x4f, 0x0f, 0x4b,
+    0xb0, 0x12, 0x00, 0x00, 0x0e, 0x49, 0x0f, 0x4b, 0xb0, 0x12, 0x00, 0x00, 0x4c, 0x47, 0x4d, 0x4a,
+    0x0e, 0x4b, 0x0f, 0x48, 0xb0, 0x12, 0x00, 0x00, 0x7f, 0xf3, 0x37, 0x41, 0x38, 0x41, 0x39, 0x41,
+    0x3a, 0x41, 0x3b, 0x41, 0x30, 0x41, 0x0b, 0x12, 0x0a, 0x12, 0x09, 0x12, 0x08, 0x12, 0x07, 0x12,
+    0x0b, 0x4f, 0xb0, 0x12, 0x00, 0x00, 0x47, 0x4f, 0x0f, 0x4b, 0xb0, 0x12, 0x00, 0x00, 0x09, 0x4f,
+    0x0f, 0x4b, 0xb0, 0x12, 0x00, 0x00, 0x08, 0x4f, 0x0f, 0x4b, 0xb0, 0x12, 0x00, 0x00, 0x4a, 0x4f,
+    0x0f, 0x4b, 0xb0, 0x12, 0x00, 0x00, 0x0e, 0x49, 0x0f, 0x4b, 0xb0, 0x12, 0x00, 0x00, 0x4c, 0x47,
+    0x4d, 0x4a, 0x0e, 0x4b, 0x0f, 0x48, 0xb0, 0x12, 0x1e, 0x03, 0x7f, 0xf3, 0x37, 0x41, 0x38, 0x41,
+    0x39, 0x41, 0x3a, 0x41, 0x3b, 0x41, 0x30, 0x41, 0x3e, 0x40, 0x1a, 0x03, 0x3f, 0x40, 0x00, 0x00,
+    0xb0, 0x12, 0x00, 0x00, 0x30, 0x41, 0x3e, 0x40, 0x00, 0x00, 0x3f, 0x40, 0x00, 0x00, 0xb0, 0x12,
+    0x00, 0x00, 0x30, 0x41
+  };
+
+  task void loadCode1() {
+    call DynamicLoader.loadFromMemory(code1);
+  }
+
+  task void loadCode2() {
+    call DynamicLoader.loadFromMemory(code2);
+  }
+
+  event void DynamicLoader.loadFromMemoryDone(void *addr, tosthread_t id, error_t error)
+  {
+    if (addr == code1) {
+      post loadCode2();
+    }
+  }
+
+  event void Boot.booted() {
+    post loadCode1();
+  }
+  
+  event void DynamicLoader.loadFromFlashDone(uint8_t volumeId, tosthread_t id, error_t error) {}
+}
diff --git a/apps/tosthreads/tinyld/LoadFromRAM/Makefile b/apps/tosthreads/tinyld/LoadFromRAM/Makefile
new file mode 100644 (file)
index 0000000..97b77fa
--- /dev/null
@@ -0,0 +1,17 @@
+COMPONENT=LoadFromRAMAppC
+
+GOALS += threads
+
+THREADS_DIR ?= $(TOSDIR)/lib/tosthreads
+CFLAGS += -I$(THREADS_DIR)/lib/tinyld
+CFLAGS += -I$(THREADS_DIR)/csystem
+CFLAGS += -I$(THREADS_DIR)/sensorboards/tmote_onboard
+CFLAGS += -I$(THREADS_DIR)/sensorboards/universal
+CFLAGS += -I$(THREADS_DIR)/lib/net/ctp
+CFLAGS += -I$(THREADS_DIR)/lib/net
+CFLAGS += -I$(TOSDIR)/lib/net
+CFLAGS += -I$(TOSDIR)/lib/net/ctp
+CFLAGS += -I$(TOSDIR)/lib/net/4bitle
+CFLAGS += -I$(THREADS_DIR)/lib/printf
+
+include $(MAKERULES)
diff --git a/apps/tosthreads/tinyld/LoadFromRAM/README b/apps/tosthreads/tinyld/LoadFromRAM/README
new file mode 100644 (file)
index 0000000..cda23b7
--- /dev/null
@@ -0,0 +1,11 @@
+README for LoadFromRAM
+Author/Contact: Chieh-Jan Mike Liang <cliang4@cs.jhu.edu>
+
+Description:
+
+LoadFromRAM is a simple program that dynamically loads two loadable
+programs hardcoded in the byte arrays; One program is Blink, and the
+other one is Basestation.
+
+If you would like to generate your own loadable program:
+    tosthread-gen-dynamic-app ../../capps/Blink/Blink.c
diff --git a/apps/tosthreads/tinyld/LoadFromRAM/volumes-stm25p.xml b/apps/tosthreads/tinyld/LoadFromRAM/volumes-stm25p.xml
new file mode 100755 (executable)
index 0000000..dfc0bd4
--- /dev/null
@@ -0,0 +1,3 @@
+<volume_table>
+  <volume name="MICROEXEIMAGE" size="65536" type="block"/>
+</volume_table>
diff --git a/apps/tosthreads/tinyld/SerialLoader/Blink.tos b/apps/tosthreads/tinyld/SerialLoader/Blink.tos
new file mode 100644 (file)
index 0000000..bd897a2
Binary files /dev/null and b/apps/tosthreads/tinyld/SerialLoader/Blink.tos differ
diff --git a/apps/tosthreads/tinyld/SerialLoader/Makefile b/apps/tosthreads/tinyld/SerialLoader/Makefile
new file mode 100755 (executable)
index 0000000..92bfa55
--- /dev/null
@@ -0,0 +1,25 @@
+COMPONENT=SerialLoaderAppC
+
+GOALS += threads
+
+THREADS_DIR ?= $(TOSDIR)/lib/tosthreads
+CFLAGS += -I$(THREADS_DIR)/lib/tinyld
+CFLAGS += -I$(THREADS_DIR)/csystem
+CFLAGS += -I$(THREADS_DIR)/sensorboards/tmote_onboard
+CFLAGS += -I$(THREADS_DIR)/sensorboards/universal
+CFLAGS += -I$(THREADS_DIR)/lib/net/ctp
+CFLAGS += -I$(THREADS_DIR)/lib/net
+CFLAGS += -I$(TOSDIR)/lib/net
+CFLAGS += -I$(TOSDIR)/lib/net/ctp
+CFLAGS += -I$(TOSDIR)/lib/net/4bitle
+CFLAGS += -I$(THREADS_DIR)/lib/printf
+
+ifdef TENET
+    CFLAGS += -DTOSTHREAD_TENET=1
+endif
+CFLAGS += -DDISABLE_LOADER_FLASH=1
+CFLAGS += -DDISABLE_LOADER_USERBUTTON=1
+
+CLEAN_EXTRA += *.pyc
+
+include $(MAKERULES)
diff --git a/apps/tosthreads/tinyld/SerialLoader/README b/apps/tosthreads/tinyld/SerialLoader/README
new file mode 100644 (file)
index 0000000..ded9353
--- /dev/null
@@ -0,0 +1,24 @@
+README for SerialLoader
+Author/Contact: Chieh-Jan Mike Liang <cliang4@cs.jhu.edu>
+
+Description:
+
+SerialLoader receives loadable programs from the serial port and stores
+it in a byte array. Then, when it receives the command to load the code,
+it makes the call to the dynamic loader.
+
+Here are the steps:
+1.) Load SerialLoader:
+    make telosb install bsl,<device_port>
+    
+2.) Create the loadable code, Blink.tos:
+    tosthread-gen-dynamic-app ../../capps/Blink/Blink.c
+
+3.) Clear the byte array in the mote RAM buffer:
+    ./serialloader.py <device_port> 0
+
+4.) Upload the binary:
+    ./serialloader.py <device_port> 1 Blink.tos
+    
+5.) Run the binary:
+    ./serialloader.py <device_port> 7
diff --git a/apps/tosthreads/tinyld/SerialLoader/SerialLoader.h b/apps/tosthreads/tinyld/SerialLoader/SerialLoader.h
new file mode 100755 (executable)
index 0000000..d3ff65a
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2008 Johns Hopkins University.
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose, without fee, and without written
+ * agreement is hereby granted, provided that the above copyright
+ * notice, the (updated) modification history and the author appear in
+ * all copies of this source code.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, LOSS OF USE, DATA,
+ * OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+/**
+ * @author Chieh-Jan Mike Liang <cliang4@cs.jhu.edu>
+ */
+
+#ifndef SERIALLOADER_H
+#define SERIALLOADER_H
+
+#define MAX_BIN_SIZE 2000
+
+#define SERIALMSG_ERASE 0
+#define SERIALMSG_WRITE 1
+#define SERIALMSG_READ  2
+#define SERIALMSG_CRC   3
+#define SERIALMSG_LEDS  5
+#define SERIALMSG_RUN   7
+
+typedef nx_struct SerialReqPacket {
+  nx_uint8_t msg_type;
+  nx_uint8_t pad;
+  nx_uint16_t offset;
+  nx_uint16_t len;
+  nx_uint8_t data[0];
+} SerialReqPacket;
+
+#define SERIALMSG_SUCCESS 0
+#define SERIALMSG_FAIL    1
+
+typedef nx_struct SerialReplyPacket {
+  nx_uint8_t error;
+  nx_uint8_t pad;
+  nx_uint8_t data[0];
+} SerialReplyPacket;
+
+#endif
diff --git a/apps/tosthreads/tinyld/SerialLoader/SerialLoaderAppC.nc b/apps/tosthreads/tinyld/SerialLoader/SerialLoaderAppC.nc
new file mode 100755 (executable)
index 0000000..c2103e2
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2008 Johns Hopkins University.
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose, without fee, and without written
+ * agreement is hereby granted, provided that the above copyright
+ * notice, the (updated) modification history and the author appear in
+ * all copies of this source code.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, LOSS OF USE, DATA,
+ * OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+/**
+ * @author Chieh-Jan Mike Liang <cliang4@cs.jhu.edu>
+ * @author Jeongyeup Paek <jpaek@enl.usc.edu>
+ */
+
+#include "AM.h"
+#include "SerialLoader.h"
+
+configuration SerialLoaderAppC {}
+implementation
+{
+  components MainC,
+             SerialActiveMessageC,
+             new SerialAMSenderC(0xAB),
+             new SerialAMReceiverC(0xAB),
+             SerialLoaderP,
+             BigCrcC,
+             LedsC;
+  
+  SerialLoaderP.Boot -> MainC;
+  SerialLoaderP.SerialSplitControl -> SerialActiveMessageC;
+  SerialLoaderP.SerialAMSender -> SerialAMSenderC;
+  SerialLoaderP.SerialAMReceiver -> SerialAMReceiverC;
+  SerialLoaderP.Leds -> LedsC;
+  SerialLoaderP.BigCrc -> BigCrcC;
+
+  components DynamicLoaderC;
+  SerialLoaderP.DynamicLoader -> DynamicLoaderC;
+}
+
diff --git a/apps/tosthreads/tinyld/SerialLoader/SerialLoaderP.nc b/apps/tosthreads/tinyld/SerialLoader/SerialLoaderP.nc
new file mode 100755 (executable)
index 0000000..15b17c4
--- /dev/null
@@ -0,0 +1,157 @@
+/*
+ * Copyright (c) 2008 Johns Hopkins University.
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose, without fee, and without written
+ * agreement is hereby granted, provided that the above copyright
+ * notice, the (updated) modification history and the author appear in
+ * all copies of this source code.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, LOSS OF USE, DATA,
+ * OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+/**
+ * @author Chieh-Jan Mike Liang <cliang4@cs.jhu.edu>
+ * @author Jeongyeup Paek <jpaek@enl.usc.edu>
+ **/
+
+#include "SerialLoader.h"
+
+module SerialLoaderP
+{
+  uses {
+    interface Boot;
+    interface SplitControl as SerialSplitControl;
+    interface AMSend as SerialAMSender;
+    interface Receive as SerialAMReceiver;
+    interface Leds;
+    interface DynamicLoader;
+    interface BigCrc;
+  }
+}
+implementation
+{
+  message_t serialMsg;
+  uint32_t dumpAddr = 0;
+  
+  uint8_t image[MAX_BIN_SIZE];
+
+  event void Boot.booted()
+  {
+    call SerialSplitControl.start();
+  }
+  
+  event void SerialSplitControl.startDone(error_t error)
+  {
+    if (error != SUCCESS) {
+      call SerialSplitControl.start();
+    }
+  }
+  
+  event void SerialSplitControl.stopDone(error_t error) {}
+  
+  void sendReply(error_t error, uint8_t len)
+  {
+    SerialReplyPacket *srpkt = (SerialReplyPacket *)call SerialAMSender.getPayload(&serialMsg, sizeof(SerialReplyPacket));
+    if (error == SUCCESS) {
+      srpkt->error = SERIALMSG_SUCCESS;
+    } else {
+      srpkt->error = SERIALMSG_FAIL;
+    }
+    call SerialAMSender.send(AM_BROADCAST_ADDR, &serialMsg, len);
+  }
+  
+  
+  event void SerialAMSender.sendDone(message_t* msg, error_t error) {}
+  
+  error_t write_image(uint16_t offset, void *data, uint16_t len) {
+    if ((offset + len > MAX_BIN_SIZE) || (data == NULL))
+      return FAIL;
+    memcpy(&image[offset], data, len);
+    return SUCCESS;
+  }
+
+  error_t read_image(uint16_t offset, void *readbuf, uint16_t len) {
+    if ((offset + len > MAX_BIN_SIZE) || (readbuf == NULL))
+      return FAIL;
+    memcpy(readbuf, &image[offset], len);
+    return SUCCESS;
+  }
+
+  event void DynamicLoader.loadFromFlashDone(uint8_t volumeId, tosthread_t id, error_t error) {}
+  event void DynamicLoader.loadFromMemoryDone(void *addr, tosthread_t id, error_t error) {
+    sendReply(error, sizeof(SerialReplyPacket));
+  }
+
+  event void BigCrc.computeCrcDone(void* buf, uint16_t len, uint16_t crc, error_t error)
+  {
+    SerialReplyPacket *srpkt = (SerialReplyPacket *)call SerialAMSender.getPayload(&serialMsg, sizeof(SerialReplyPacket));
+        
+    srpkt->data[1] = crc & 0xFF;
+    srpkt->data[0] = (crc >> 8) & 0xFF;
+    sendReply(SUCCESS, 2 + sizeof(SerialReplyPacket));
+  }
+
+  void sendCrcReply(uint16_t offset, uint16_t len)
+  {
+    call BigCrc.computeCrc(&(image[offset]), len);
+  }
+
+
+  event message_t* SerialAMReceiver.receive(message_t* msg, void* payload, uint8_t len)
+  {
+    uint16_t i;
+    error_t error = FAIL;
+    SerialReqPacket *srpkt = (SerialReqPacket *)payload;
+    SerialReplyPacket *serialMsg_payload = (SerialReplyPacket *)call SerialAMSender.getPayload(&serialMsg, sizeof(SerialReplyPacket));
+    
+    switch (srpkt->msg_type) {
+      case SERIALMSG_ERASE :
+        for (i = 0; i < MAX_BIN_SIZE; i++) { image[i] = 0; }
+        call Leds.set(7);
+        for (i = 0; i < 2000; i++) {}
+        call Leds.set(0);
+        sendReply(SUCCESS, sizeof(SerialReplyPacket));
+        break;
+      case SERIALMSG_RUN :
+        error = call DynamicLoader.loadFromMemory(image);
+        if (error != SUCCESS)
+          sendReply(error, sizeof(SerialReplyPacket));
+        break;
+      case SERIALMSG_WRITE :
+        error = write_image(srpkt->offset, srpkt->data, srpkt->len);
+        if (error != SUCCESS)
+          call Leds.led0On();
+        sendReply(error, sizeof(SerialReplyPacket));
+        break;
+      case SERIALMSG_READ :
+        error = read_image(srpkt->offset, serialMsg_payload->data, srpkt->len);
+        if (error != SUCCESS)
+          sendReply(error, sizeof(SerialReplyPacket));
+        else
+          sendReply(error, len + sizeof(SerialReplyPacket));
+        break;
+      case SERIALMSG_LEDS:
+        call Leds.set(7);
+        for (i = 0; i < 2000; i++) {}
+        call Leds.set(0);
+        break;
+      case SERIALMSG_CRC :
+        sendCrcReply(srpkt->offset, srpkt->len);
+        break;
+    }
+    
+    return msg;
+  }
+}
+
diff --git a/apps/tosthreads/tinyld/SerialLoader/serialloader.py b/apps/tosthreads/tinyld/SerialLoader/serialloader.py
new file mode 100755 (executable)
index 0000000..f7964dd
--- /dev/null
@@ -0,0 +1,232 @@
+#!/usr/bin/env python
+
+# Copyright (c) 2008 Johns Hopkins University.
+# All rights reserved.
+#
+# Permission to use, copy, modify, and distribute this software and its
+# documentation for any purpose, without fee, and without written
+# agreement is hereby granted, provided that the above copyright
+# notice, the (updated) modification history and the author appear in
+# all copies of this source code.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
+# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, LOSS OF USE, DATA,
+# OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+# THE POSSIBILITY OF SUCH DAMAGE.
+
+# @author Chieh-Jan Mike Liang <cliang4@cs.jhu.edu>
+
+import sys, os, stat, struct
+import tinyos
+
+SERIAL_BAUDRATE = 57600
+SERIALMSG_AMGROUP = 0
+SERIALMSG_AMID    = 0xAB
+
+SERIALMSG_ERASE = 0
+SERIALMSG_WRITE = 1
+SERIALMSG_READ  = 2
+SERIALMSG_CRC   = 3
+SERIALMSG_LEDS  = 5
+SERIALMSG_RUN   = 7
+
+SERIALMSG_SUCCESS = 0
+SERIALMSG_FAIL    = 1
+
+SERIALMSG_DATA_PAYLOAD_SIZE = 20
+MAX_BIN_SIZE = 2000
+
+HEX_OUTPUT_LINE_SIZE = 16
+
+class SerialReqPacket(tinyos.GenericPacket):
+  def __init__(self, packet = None):
+      tinyos.GenericPacket.__init__(self,
+                             [('msg_type', 'int', 1),
+                              ('pad', 'int', 1),
+                              ('offset', 'int', 2),
+                              ('len', 'int', 2),
+                              ('data', 'blob', None)],
+                             packet)
+
+class SerialReplyPacket(tinyos.GenericPacket):
+  def __init__(self, packet = None):
+      tinyos.GenericPacket.__init__(self,
+                             [('error', 'int', 1),
+                              ('pad', 'int', 1),
+                             ('data', 'blob', None)],
+                             packet)
+
+# Display an integer representation of byte stream to hex representation
+def print_hex(start_addr, byte_stream):
+  byte_stream = ["%02x" % one_byte for one_byte in byte_stream]   # Converts to each byte to hex
+  
+  num_iterations = int( (len(byte_stream) - 1) / HEX_OUTPUT_LINE_SIZE )
+  num_iterations += 1
+  
+  for i in range(num_iterations):
+    line = "%07x" % start_addr + " "   # Prints memory address
+    for j in range(HEX_OUTPUT_LINE_SIZE):
+      if (i * HEX_OUTPUT_LINE_SIZE + j) < len(byte_stream):
+        line += byte_stream[i * HEX_OUTPUT_LINE_SIZE + j] + " "
+    print line
+    
+    start_addr += HEX_OUTPUT_LINE_SIZE
+
+def op_run(s, sreqpkt):
+  success = s.write_packet(SERIALMSG_AMGROUP, SERIALMSG_AMID, sreqpkt.payload())
+  if success == True:
+    packet = s.read_packet()
+    sreplypkt = SerialReplyPacket(packet[1])
+    return (sreplypkt.error == SERIALMSG_SUCCESS)
+
+def op_erase(s, sreqpkt):
+  success = s.write_packet(SERIALMSG_AMGROUP, SERIALMSG_AMID, sreqpkt.payload())
+  if success == True:
+    packet = s.read_packet()
+    sreplypkt = SerialReplyPacket(packet[1])
+    return (sreplypkt.error == SERIALMSG_SUCCESS)
+
+def op_print(s, sreqpkt, offset, length):
+  if (offset + length) <= DELUGE_VOLUME_SIZE:
+    while length > 0:
+      sreqpkt.offset = offset
+      # Calculates the payload size for the reply packet
+      if length >= HEX_OUTPUT_LINE_SIZE:
+        sreqpkt.len = HEX_OUTPUT_LINE_SIZE
+      else:
+        sreqpkt.len = length
+      
+      success = s.write_packet(SERIALMSG_AMGROUP, SERIALMSG_AMID, sreqpkt.payload())
+      if success == True:
+        packet = s.read_packet()
+        sreplypkt = SerialReplyPacket(packet[1])
+        if sreplypkt.error != SERIALMSG_SUCCESS:
+          return False
+  
+      print_hex(offset, sreplypkt.data)
+      length -= sreqpkt.len
+      offset += sreqpkt.len
+  else:
+    print "ERROR: Specified offset and length are too large for the flash volume"
+    return False
+  
+  return True
+
+def op_write(s, sreqpkt, input_file, length):
+  local_crc = 0
+  input_file_size = length
+  
+  sreqpkt.offset = 0
+  while length > 0:
+    # Calculates the payload size for the current packet
+    if length >= SERIALMSG_DATA_PAYLOAD_SIZE:
+      sreqpkt.len = SERIALMSG_DATA_PAYLOAD_SIZE
+    else:
+      sreqpkt.len = length
+    sreqpkt.data = []
+    
+    # Reads in the file we want to transmit
+    for i in range(sreqpkt.len):
+      sreqpkt.data.append(struct.unpack("B", input_file.read(1))[0])
+    
+    # Sends over serial to the mote
+    if s.write_packet(SERIALMSG_AMGROUP, SERIALMSG_AMID, sreqpkt.payload()) == True:
+      # Waiting for confirmation
+      packet = s.read_packet()
+      sreplypkt = SerialReplyPacket(packet[1])
+      if sreplypkt.error != SERIALMSG_SUCCESS:
+        print "ERROR: !SUCCESS"
+        return False
+      local_crc = s.crc16(local_crc, sreqpkt.data)   # Computes running CRC
+    else:
+      print "ERROR: Unable to write to flash"
+      return False
+    
+    length -= sreqpkt.len
+    sreqpkt.offset += sreqpkt.len
+  
+  # Check local and remote CRC
+  sreqpkt.msg_type = SERIALMSG_CRC
+  remote_crc = op_crc(s, sreqpkt, 0, input_file_size)
+  if remote_crc != None:
+    local_crc = [(local_crc >> 8) & 0xFF, local_crc & 0xFF]
+    print "Local CRC:  " + ("%02x" % local_crc[0]) + " " + ("%02x" % local_crc[1])
+    print "Remote CRC: " + ("%02x" % remote_crc[0]) + " " + ("%02x" % remote_crc[1])
+    if remote_crc != local_crc:
+      print "ERROR: Remote CRC doesn't match local CRC"
+      return False
+  else:
+    print "ERROR: Unable to verify CRC"
+    return False
+
+  return True
+
+def op_crc(s, sreqpkt, offset, length):
+  sreqpkt.offset = offset
+  sreqpkt.len = length
+  success = s.write_packet(SERIALMSG_AMGROUP, SERIALMSG_AMID, sreqpkt.payload())
+  if success == True:
+    packet = s.read_packet()
+    sreplypkt = SerialReplyPacket(packet[1])
+    if sreplypkt.error == SERIALMSG_SUCCESS:
+      return sreplypkt.data
+    else:
+      return None
+
+def op_leds(s, sreqpkt):
+  success = s.write_packet(SERIALMSG_AMGROUP, SERIALMSG_AMID, sreqpkt.payload())
+
+# ======== MAIN ======== #
+if len(sys.argv) >= 3:
+  sys.argv[2] = int(sys.argv[2])
+  
+  s = tinyos.Serial(sys.argv[1], SERIAL_BAUDRATE)
+  s.set_debug(False)   # Disables debug msg
+  sreqpkt = SerialReqPacket((sys.argv[2], 0, 0, 0, []))   # msg_type, pad, offset, length, data
+  
+  if sys.argv[2] == SERIALMSG_RUN:
+    if op_run(s, sreqpkt) == True:
+      print "Loaded image should be running now!"
+    else:
+      print "ERROR: Unable to run loaded image"
+    
+  elif sys.argv[2] == SERIALMSG_ERASE:
+    if op_erase(s, sreqpkt) == True:
+      print "Flash volume has been erased"
+    else:
+      print "ERROR: Unable to erase flash volume"
+    
+  elif sys.argv[2] == SERIALMSG_WRITE:
+    input_file = file(sys.argv[3], 'rb')
+    fileStats = os.stat(sys.argv[3])
+    
+    if fileStats[stat.ST_SIZE] <= MAX_BIN_SIZE:
+      #sreqpkt = SerialReqPacket((SERIALMSG_LEDS, 0, 0, 0, []))
+      #op_leds(s, sreqpkt)
+      sreqpkt = SerialReqPacket((sys.argv[2], 0, 0, 0, []))
+      if op_write(s, sreqpkt, input_file, fileStats[stat.ST_SIZE]) == True:
+        print "File has been successfully transmitted (" + str(fileStats[stat.ST_SIZE]) + " bytes)"
+      else:
+        print "ERROR: Unable to transmit file"
+      sreqpkt = SerialReqPacket((SERIALMSG_LEDS, 0, 0, 0, []))
+      op_leds(s, sreqpkt)
+    else:
+      print "ERROR: File is larger than max buffer size (" + str(MAX_BIN_SIZE) + ")"
+  
+  elif sys.argv[2] == SERIALMSG_READ:
+    data = op_print(s, sreqpkt, int(sys.argv[3]), int(sys.argv[4]))
+    if data != True:
+      print "ERROR: Unable to read the specified range"
+    
+  elif sys.argv[2] == SERIALMSG_CRC:
+    remote_crc = op_crc(s, sreqpkt, int(sys.argv[3]), int(sys.argv[4]))
+    if remote_crc != None:
+      print_hex(0, remote_crc)
+    else:
+      print "ERROR: Unable to compute remote CRC"
diff --git a/apps/tosthreads/tinyld/SerialLoader/tinyos.py b/apps/tosthreads/tinyld/SerialLoader/tinyos.py
new file mode 100755 (executable)
index 0000000..e922f58
--- /dev/null
@@ -0,0 +1,223 @@
+import struct, time, serial
+
+class Serial:
+  HDLC_FLAG_BYTE = 0x7e
+  HDLC_CTLESC_BYTE = 0x7d
+  
+  TOS_SERIAL_ACTIVE_MESSAGE_ID = 0
+  TOS_SERIAL_CC1000_ID = 1
+  TOS_SERIAL_802_15_4_ID = 2
+  TOS_SERIAL_UNKNOWN_ID = 255
+  
+  SERIAL_PROTO_ACK = 67
+  SERIAL_PROTO_PACKET_ACK = 68
+  SERIAL_PROTO_PACKET_NOACK = 69
+  SERIAL_PROTO_PACKET_UNKNOWN = 255
+  
+  __s = None;       # An instance of serial.Serial object
+  __debug = True   # Debug mode
+  
+  def __init__(self, port, baudrate):
+     self.__s = serial.Serial(port, baudrate, rtscts=0)
+  
+  def __format_packet(self, packet):
+      return " ".join(["%02x" % p for p in packet]) + " | " + \
+             " ".join(["%d" % p for p in packet])
+  
+  def crc16(self, base_crc, frame_data):
+      crc = base_crc
+      for b in frame_data:
+          crc = crc ^ (b << 8)
+          for i in range(0, 8):
+              if crc & 0x8000 == 0x8000:
+                  crc = (crc << 1) ^ 0x1021
+              else:
+                  crc = crc << 1
+              crc = crc & 0xffff
+      return crc
+  
+  def __encode(self, val, dim):
+      output = []
+      for i in range(dim):
+          output.append(val & 0xFF)
+          val = val >> 8
+      return output
+  
+  def __decode(self, v):
+      r = long(0)
+      for i in v[::-1]:
+          r = (r << 8) + i
+      return r
+  
+  def __get_byte(self):
+      r = struct.unpack("B", self.__s.read())[0]
+      return r
+  
+  def __put_bytes(self, data):
+      for b in data:
+          self.__s.write(struct.pack('B', b))
+  
+  def __unescape(self, packet):
+      r = []
+      esc = False
+      for b in packet:
+          if esc:
+              r.append(b ^ 0x20)
+              esc = False
+          elif b == self.HDLC_CTLESC_BYTE:
+              esc = True
+          else:
+              r.append(b)
+      return r
+  
+  def __escape(self, packet):
+      r = []
+      for b in packet:
+          if b == self.HDLC_FLAG_BYTE or b == self.HDLC_CTLESC_BYTE:
+              r.append(self.HDLC_CTLESC_BYTE)
+              r.append(b ^ 0x20)
+          else:
+              r.append(b)
+      return r
+  
+  def read_packet(self):
+      d = self.__get_byte()
+      ts = time.time()
+      while d != self.HDLC_FLAG_BYTE:
+          d = self.__get_byte()
+          ts = time.time()
+      packet = [d]
+      d = self.__get_byte()
+      if d == self.HDLC_FLAG_BYTE:
+          d = self.__get_byte()
+          ts = time.time()
+      else:
+          packet.append(d)
+      while d != self.HDLC_FLAG_BYTE:
+          d = self.__get_byte()
+          packet.append(d)
+      un_packet = self.__unescape(packet)
+      crc = self.crc16(0, un_packet[1:-3])
+      packet_crc = self.__decode(un_packet[-3:-1])
+      if crc != packet_crc:
+          print "Warning: wrong CRC!"
+      if self.__debug == True:
+          print "Recv:", self.__format_packet(un_packet)
+      return (ts, un_packet)
+      
+  def write_packet(self, am_group, am_id, data):
+      # The first byte after SERIAL_PROTO_PACKET_ACK is a sequence
+      # number that will be send back by the mote to ack the receive of
+      # the data.
+      packet = [self.SERIAL_PROTO_PACKET_ACK, 0, self.TOS_SERIAL_ACTIVE_MESSAGE_ID,
+                0xff, 0xff,
+                0, 0,
+                len(data), am_group, am_id] + data;
+      crc = self.crc16(0, packet)
+      packet.append(crc & 0xff)
+      packet.append((crc >> 8) & 0xff)
+      packet = [self.HDLC_FLAG_BYTE] + self.__escape(packet) + [self.HDLC_FLAG_BYTE]
+      if self.__debug == True:
+          print "Send:", self.__format_packet(packet)
+      self.__put_bytes(packet)
+      
+      # Waiting for ACK
+      packet = self.read_packet()
+      if len(packet) > 1 and len(packet[1]) > 1:
+        return ((packet[1])[1] == self.SERIAL_PROTO_ACK)
+      return False
+  
+  def set_debug(self, debug):
+      self.__debug = debug
+
+class GenericPacket:
+    """ GenericPacket """
+
+    def __decode(self, v):
+        r = long(0)
+        for i in v:
+            r = (r << 8) + i
+        return r
+    
+    def __encode(self, val, dim):
+        output = []
+        for i in range(dim):
+            output.append(int(val & 0xFF))
+            val = val >> 8
+        output.reverse()
+        return output
+
+    def __init__(self, desc, packet = None):
+        self.__dict__['_schema'] = [(t, s) for (n, t, s) in desc]
+        self.__dict__['_names'] = [n for (n, t, s) in desc]
+        self.__dict__['_values'] = []
+        offset = 10
+        if type(packet) == type([]):
+            for (t, s) in self._schema:
+                if t == 'int':
+                    self._values.append(self.__decode(packet[offset:offset + s]))
+                    offset += s
+                elif t == 'blob':
+                    if s:
+                        self._values.append(packet[offset:offset + s])
+                        offset += s
+                    else:
+                        self._values.append(packet[offset:-3])
+        elif type(packet) == type(()):
+            for i in packet:
+                self._values.append(i)
+        else:
+            for v in self._schema:
+                self._values.append(None)
+
+    def __repr__(self):
+        return self._values.__repr__()
+
+    def __str__(self):
+        return self._values.__str__()
+
+    # Implement the map behavior
+    def __getitem__(self, key):
+        return self.__getattr__(key)
+
+    def __setitem__(self, key, value):
+        self.__setattr__(key, value)
+
+    def __len__(self):
+        return len(self._values)
+
+    def keys(self):
+        return self._names
+
+    def values(self):
+        return self._names
+
+    # Implement the struct behavior
+    def __getattr__(self, name):
+        if type(name) == type(0):
+            return self._names[name]
+        else:
+            return self._values[self._names.index(name)]
+
+    def __setattr__(self, name, value):
+        if type(name) == type(0):
+            self._values[name] = value
+        else:
+            self._values[self._names.index(name)] = value
+
+    # Custom
+    def names(self):
+        return self._names
+
+    def sizes(self):
+        return self._schema
+
+    def payload(self):
+        r = []
+        for i in range(len(self._schema)):
+            (t, s) = self._schema[i]
+            if t == 'int':
+                r += self.__encode(self._values[i], s)
+            else:
+                r += self._values[i]
+        return r
index 389348bddd906a3869e32b3603f6db622950f02f..e6a3b21e1a6c6717f5cbe75de7e295b89c25d832 100644 (file)
@@ -4,7 +4,8 @@ dist_man_MANS = tosthreads-dynamic-app.1
 
 tosthreadslibdir=$(libdir)/tinyos/tosthreads
 
-bin_SCRIPTS = tosthreads-dynamic-app
+bin_SCRIPTS = tosthreads-dynamic-app \
+              tosthreads-gen-dynamic-app
 
 tosthreadslib_DATA = tosthreads_standard_api.py  \
                      tosthreads_tenet_api.py
diff --git a/tools/tinyos/tosthreads/tosthreads-gen-dynamic-app b/tools/tinyos/tosthreads/tosthreads-gen-dynamic-app
new file mode 100755 (executable)
index 0000000..5f6abc2
--- /dev/null
@@ -0,0 +1,63 @@
+#!/bin/bash
+
+if [ $# -ne 1 -a $# -ne 2 ]
+then
+  echo "Usage: `basename $0` path_to_cthread_app [options]"
+  echo ""
+  echo "-a --array"
+  echo "     Print out the loadable binary code as a byte array."
+  exit -1
+fi
+
+CFILE=$1
+FLAG=$2
+
+GCC="msp430-gcc"
+OBJCOPY="msp430-objcopy"
+NESCFLAGS="-target=telosb -x nesc -fnesc-target=msp430"
+CFLAGS="-c -gcc=$GCC -mmcu=msp430x1611 -Os -mdisable-hwmul -Wall -Wshadow"
+
+OBJFILE=`basename $CFILE .c`.o
+BINFILE=`basename $CFILE .c`.bin
+TOSFILE=`basename $CFILE .c`.tos
+
+TOS_THREADS_DIR=$TOSDIR/lib/tosthreads
+THREADS_CSYSTEM_DIR=$TOS_THREADS_DIR/csystem
+THREADS_SYSTEM_DIR=$TOS_THREADS_DIR/system
+THREADS_INTERFACES_DIR=$TOS_THREADS_DIR/interfaces
+THREADS_TYPES_DIR=$TOS_THREADS_DIR/types
+THREADS_MSP430_DIR=$TOS_THREADS_DIR/chips/msp430
+THREADS_TMOTE_SENSORS_DIR=$TOS_THREADS_DIR/tos/sensorboards/tmote_onboard
+THREADS_PRINTF_DIR=$TOS_THREADS_DIR/tos/lib/printf
+TOS_TELOSA_DIR=$TOSDIR/platforms/telosa
+TOS_TELOSB_DIR=$TOSDIR/platforms/telosb
+TOS_CC2420_DIR=$TOSDIR/chips/cc2420
+TOS_SERIAL_DIR=$TOSDIR/lib/serial
+TOS_SYSTEM_DIR=$TOSDIR/system
+TOS_TYPES_DIR=$TOSDIR/types
+
+#Set up includes 
+CFLAGS="$CFLAGS -I$THREADS_CSYSTEM_DIR -I$THREADS_SYSTEM_DIR -I$THREADS_INTERFACES_DIR -I$THREADS_TYPES_DIR -I$THREADS_MSP430_DIR"
+CFLAGS="$CFLAGS -I$THREADS_PRINTF_DIR"
+CFLAGS="$CFLAGS -I$THREADS_TMOTE_SENSORS_DIR"
+CFLAGS="$CFLAGS -DTOSTHREAD_EXTERNAL_BINARY"
+
+#Set up the proper scheduler
+NESCFLAGS="$NESCFLAGS -tosscheduler=TinyTaskSchedulerC,TinyTaskSchedulerC.TaskBasic,TaskBasic,TaskBasic,runTask,postTask"
+
+rm -rf $OBJFILE
+rm -rf $TOSFILE
+
+COMMAND="ncc $CFLAGS $NESCFLAGS $CFILE"
+echo $COMMAND
+command  $COMMAND
+
+COMMAND="$OBJCOPY --output-target=binary $OBJFILE $BINFILE"
+echo $COMMAND
+command  $COMMAND
+
+COMMAND="tosthreads-dynamic-app $FLAG $OBJFILE $BINFILE $TOSFILE"
+echo $COMMAND
+command $COMMAND
+rm $BINFILE
+rm $OBJFILE
diff --git a/tools/tinyos/tosthreads/tosthreads-gen-dynamic-app.1 b/tools/tinyos/tosthreads/tosthreads-gen-dynamic-app.1
new file mode 100755 (executable)
index 0000000..5f6abc2
--- /dev/null
@@ -0,0 +1,63 @@
+#!/bin/bash
+
+if [ $# -ne 1 -a $# -ne 2 ]
+then
+  echo "Usage: `basename $0` path_to_cthread_app [options]"
+  echo ""
+  echo "-a --array"
+  echo "     Print out the loadable binary code as a byte array."
+  exit -1
+fi
+
+CFILE=$1
+FLAG=$2
+
+GCC="msp430-gcc"
+OBJCOPY="msp430-objcopy"
+NESCFLAGS="-target=telosb -x nesc -fnesc-target=msp430"
+CFLAGS="-c -gcc=$GCC -mmcu=msp430x1611 -Os -mdisable-hwmul -Wall -Wshadow"
+
+OBJFILE=`basename $CFILE .c`.o
+BINFILE=`basename $CFILE .c`.bin
+TOSFILE=`basename $CFILE .c`.tos
+
+TOS_THREADS_DIR=$TOSDIR/lib/tosthreads
+THREADS_CSYSTEM_DIR=$TOS_THREADS_DIR/csystem
+THREADS_SYSTEM_DIR=$TOS_THREADS_DIR/system
+THREADS_INTERFACES_DIR=$TOS_THREADS_DIR/interfaces
+THREADS_TYPES_DIR=$TOS_THREADS_DIR/types
+THREADS_MSP430_DIR=$TOS_THREADS_DIR/chips/msp430
+THREADS_TMOTE_SENSORS_DIR=$TOS_THREADS_DIR/tos/sensorboards/tmote_onboard
+THREADS_PRINTF_DIR=$TOS_THREADS_DIR/tos/lib/printf
+TOS_TELOSA_DIR=$TOSDIR/platforms/telosa
+TOS_TELOSB_DIR=$TOSDIR/platforms/telosb
+TOS_CC2420_DIR=$TOSDIR/chips/cc2420
+TOS_SERIAL_DIR=$TOSDIR/lib/serial
+TOS_SYSTEM_DIR=$TOSDIR/system
+TOS_TYPES_DIR=$TOSDIR/types
+
+#Set up includes 
+CFLAGS="$CFLAGS -I$THREADS_CSYSTEM_DIR -I$THREADS_SYSTEM_DIR -I$THREADS_INTERFACES_DIR -I$THREADS_TYPES_DIR -I$THREADS_MSP430_DIR"
+CFLAGS="$CFLAGS -I$THREADS_PRINTF_DIR"
+CFLAGS="$CFLAGS -I$THREADS_TMOTE_SENSORS_DIR"
+CFLAGS="$CFLAGS -DTOSTHREAD_EXTERNAL_BINARY"
+
+#Set up the proper scheduler
+NESCFLAGS="$NESCFLAGS -tosscheduler=TinyTaskSchedulerC,TinyTaskSchedulerC.TaskBasic,TaskBasic,TaskBasic,runTask,postTask"
+
+rm -rf $OBJFILE
+rm -rf $TOSFILE
+
+COMMAND="ncc $CFLAGS $NESCFLAGS $CFILE"
+echo $COMMAND
+command  $COMMAND
+
+COMMAND="$OBJCOPY --output-target=binary $OBJFILE $BINFILE"
+echo $COMMAND
+command  $COMMAND
+
+COMMAND="tosthreads-dynamic-app $FLAG $OBJFILE $BINFILE $TOSFILE"
+echo $COMMAND
+command $COMMAND
+rm $BINFILE
+rm $OBJFILE
diff --git a/tos/lib/tosthreads/lib/tinyld/BigCrc.nc b/tos/lib/tosthreads/lib/tinyld/BigCrc.nc
new file mode 100644 (file)
index 0000000..248ef1a
--- /dev/null
@@ -0,0 +1,4 @@
+interface BigCrc {
+  command error_t computeCrc(void* buf, uint16_t len);
+  event void computeCrcDone(void* buf, uint16_t len, uint16_t crc, error_t error);
+}
diff --git a/tos/lib/tosthreads/lib/tinyld/BigCrcC.nc b/tos/lib/tosthreads/lib/tinyld/BigCrcC.nc
new file mode 100644 (file)
index 0000000..ed90bfa
--- /dev/null
@@ -0,0 +1,12 @@
+configuration BigCrcC {
+  provides interface BigCrc;
+}
+
+implementation {
+  components CrcC,
+             BigCrcP;
+  
+  BigCrc = BigCrcP;
+  
+  BigCrcP.Crc -> CrcC;
+}
diff --git a/tos/lib/tosthreads/lib/tinyld/BigCrcP.nc b/tos/lib/tosthreads/lib/tinyld/BigCrcP.nc
new file mode 100644 (file)
index 0000000..6565bd4
--- /dev/null
@@ -0,0 +1,44 @@
+module BigCrcP {
+  provides interface BigCrc;
+  uses interface Crc;
+}
+
+implementation {
+  bool isBusy = FALSE;
+  void* inbuf;
+  uint16_t inlen;
+  uint16_t pos;
+  uint16_t computedCrc;
+
+  task void computeCrc()
+  {
+    uint8_t len = 0xFF;
+    if (inlen < 0xFF) {
+      len = inlen;
+    }
+    computedCrc = call Crc.seededCrc16(computedCrc, inbuf + pos, len);
+    inlen -= len;
+    pos += len;
+    
+    if (inlen > 0) {
+      post computeCrc();
+    } else {
+      isBusy = FALSE;
+      signal BigCrc.computeCrcDone(inbuf, pos + 1, computedCrc, SUCCESS);
+    }
+  }
+
+  command error_t BigCrc.computeCrc(void* buf, uint16_t len)
+  {
+    if (isBusy == TRUE) {
+      return EBUSY;
+    }
+    
+    inbuf = buf;
+    inlen = len;
+    computedCrc = pos = 0;
+    post computeCrc();
+    
+    return SUCCESS;
+  }
+}
diff --git a/tos/lib/tosthreads/lib/tinyld/DynamicLoader.h b/tos/lib/tosthreads/lib/tinyld/DynamicLoader.h
new file mode 100755 (executable)
index 0000000..469a36d
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2008 Johns Hopkins University.
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose, without fee, and without written
+ * agreement is hereby granted, provided that the above copyright
+ * notice, the (updated) modification history and the author appear in
+ * all copies of this source code.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, LOSS OF USE, DATA,
+ * OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+/**
+ * @author Chieh-Jan Mike Liang <cliang4@cs.jhu.edu>
+ */
+
+#ifndef DYNAMICLOADER_H
+#define DYNAMICLOADER_H
+
+//#defines for setting up which set of apis the dynamic loader 
+//should recognize.  This should eventually move into a file that 
+//gets #included in here, but for now we set the defaults here.
+
+#ifdef TOSTHREAD_TENET
+#define ApiForDynamicLoaderC TosThreadTenetApiC
+#define SLCS_TYPES_FILE      "tosthread_tenet_slcs_types.h"
+#else
+#define ApiForDynamicLoaderC TosThreadApiC
+#define SLCS_TYPES_FILE      "tosthread_slcs_types.h"
+#endif
+
+#define TOSTHREAD_DYNAMIC_LOADER
+
+#ifndef DISABLE_LOADER_FLASH
+#include "StorageVolumes.h"
+#endif
+
+enum {
+  READSOURCE_MEMORY = 0xFF,
+};
+
+#endif
diff --git a/tos/lib/tosthreads/lib/tinyld/DynamicLoader.nc b/tos/lib/tosthreads/lib/tinyld/DynamicLoader.nc
new file mode 100755 (executable)
index 0000000..102b8c3
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2008 Johns Hopkins University.
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose, without fee, and without written
+ * agreement is hereby granted, provided that the above copyright
+ * notice, the (updated) modification history and the author appear in
+ * all copies of this source code.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, LOSS OF USE, DATA,
+ * OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+/**
+ * @author Chieh-Jan Mike Liang <cliang4@cs.jhu.edu>
+ */
+
+#include "thread.h"
+
+interface DynamicLoader
+{
+  command error_t loadFromFlash(uint8_t volumeId);
+  event void loadFromFlashDone(uint8_t volumeId, tosthread_t id, error_t error);
+  command error_t loadFromMemory(void *addr);
+  event void loadFromMemoryDone(void *addr, tosthread_t id, error_t error);
+}
diff --git a/tos/lib/tosthreads/lib/tinyld/DynamicLoaderC.nc b/tos/lib/tosthreads/lib/tinyld/DynamicLoaderC.nc
new file mode 100755 (executable)
index 0000000..9adcdc8
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2008 Johns Hopkins University.
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose, without fee, and without written
+ * agreement is hereby granted, provided that the above copyright
+ * notice, the (updated) modification history and the author appear in
+ * all copies of this source code.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, LOSS OF USE, DATA,
+ * OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+/**
+ * @author Chieh-Jan Mike Liang <cliang4@cs.jhu.edu>
+ * @author Razvan Musaloiu-E. <razvanm@cs.jhu.edu>
+ */
+#include "DynamicLoader.h"
+
+configuration DynamicLoaderC
+{
+  provides interface DynamicLoader;
+}
+
+implementation
+{
+  components MainC, 
+             DynamicLoaderP,
+             LedsC, NoLedsC,
+             LoadSourceMapC,
+             DynamicThreadC,
+             ReferenceCounterC,
+             TinyThreadSchedulerC;
+
+  DynamicLoader = DynamicLoaderP;
+
+  DynamicLoaderP <- MainC.SoftwareInit;
+  DynamicLoaderP.Leds -> LedsC;
+  DynamicLoaderP.ImageRead -> LoadSourceMapC;
+  DynamicLoaderP.DynamicThread -> DynamicThreadC;
+  DynamicLoaderP.ReferenceCounter -> ReferenceCounterC;
+  DynamicLoaderP.ThreadNotification -> DynamicThreadC;
+  DynamicLoaderP.ThreadScheduler -> TinyThreadSchedulerC;
+  
+  components ApiForDynamicLoaderC;
+  components PMManagerC;
+  DynamicLoaderP.PMManager -> PMManagerC;
+
+#ifndef DISABLE_LOADER_USERBUTTON
+  components UserButtonC;
+  DynamicLoaderP.UserButton -> UserButtonC;
+#endif
+}
diff --git a/tos/lib/tosthreads/lib/tinyld/DynamicLoaderP.nc b/tos/lib/tosthreads/lib/tinyld/DynamicLoaderP.nc
new file mode 100755 (executable)
index 0000000..37095b2
--- /dev/null
@@ -0,0 +1,471 @@
+/*
+ * Copyright (c) 2008 Johns Hopkins University.
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose, without fee, and without written
+ * agreement is hereby granted, provided that the above copyright
+ * notice, the (updated) modification history and the author appear in
+ * all copies of this source code.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, LOSS OF USE, DATA,
+ * OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+/**
+ * @author Chieh-Jan Mike Liang <cliang4@cs.jhu.edu>
+ * @author Razvan Musaloiu-E. <razvanm@cs.jhu.edu>
+ */
+#include SLCS_TYPES_FILE
+#include "thread.h"
+
+module DynamicLoaderP
+{
+  provides {
+    interface Init;
+    interface DynamicLoader;
+  }
+  
+  uses {
+    interface Leds;
+    interface BlockRead as ImageRead[uint8_t id];
+    interface DynamicThread;
+    interface ThreadNotification[uint8_t id];
+    interface ThreadScheduler;
+    interface ReferenceCounter;
+    interface PMManager;
+#ifndef DISABLE_LOADER_USERBUTTON
+    interface UserButton;
+#endif
+  }
+}
+
+implementation
+{ 
+  uint8_t *code;   // Points to first byte of the program code in the internal flash
+  uint8_t *tablesMemory;
+  void *gVarMemory;
+  struct prog_desc prog_desc;
+  init_block_t *proc;
+  
+  uint16_t codePtr;   // Records what code has been copied to the internal flash
+  uint16_t nextAddr;
+  uint8_t *nextTask_chain;   // Used to update the next patching address in a chain
+  
+  uint8_t readSource;   // Loads from flash or memory
+  uint16_t readSourceOffset;   // If loading from memory, then this is effectively the passed-in memory address  
+  error_t retError = FAIL;
+  tosthread_t handler;
+  uint16_t threadCodeSizes[TOSTHREAD_MAX_NUM_THREADS];
+  uint16_t codeFirstAddrs[TOSTHREAD_MAX_NUM_THREADS];
+  
+  async event void ThreadNotification.justCreated[uint8_t id]()
+  {
+    thread_t *t = call ThreadScheduler.threadInfo(id);
+    if(t->init_block != NULL) {
+      call ReferenceCounter.increment(&(t->init_block->thread_counter));
+    }
+  }
+  
+  async event void ThreadNotification.aboutToDestroy[uint8_t id]()
+  {
+    thread_t *t = call ThreadScheduler.threadInfo(id);
+    if(t->init_block != NULL) {
+      call ReferenceCounter.decrement(&(t->init_block->thread_counter));
+    }
+  }
+
+  task void loadDoneTask()
+  {
+    if (retError != SUCCESS)
+      handler = TOSTHREAD_INVALID_THREAD_ID;
+
+    if (readSource == READSOURCE_MEMORY) {
+      signal DynamicLoader.loadFromMemoryDone(((void *)readSourceOffset), handler, retError);
+    } else {
+      signal DynamicLoader.loadFromFlashDone(readSource, handler, retError);
+    }
+  }
+    
+  void initProgDesc()
+  {
+    prog_desc.main_addr = 0;
+    prog_desc.alloc_count = 0;
+    prog_desc.g_reloc_count = 0;
+    prog_desc.l_reloc_count = 0;
+    prog_desc.code_count = 0;
+    prog_desc.patch_table_count = 0;
+
+    prog_desc.loading_stage = 0;
+    codePtr = 0;
+  }
+
+  void errorHandler()
+  {
+    call Leds.set(7);
+    
+    if (tablesMemory != NULL) { free(tablesMemory); tablesMemory = NULL; }
+    if (gVarMemory != NULL) { free(gVarMemory); gVarMemory = NULL; }
+    if (proc != NULL) { free(proc); proc = NULL; }
+    initProgDesc();
+    
+    retError = FAIL;
+    post loadDoneTask();
+  }
+
+  void run_proc(void *arg)
+  {
+    init_block_t* curProc = arg;
+    thread_t* t = call ThreadScheduler.currentThreadInfo();
+    t->init_block = curProc;
+    
+    (*(curProc->init_ptr))(curProc->init_arg);
+    call ReferenceCounter.waitOnValue(&(curProc->thread_counter), 0);
+    
+    call PMManager.release(codeFirstAddrs[t->id], threadCodeSizes[t->id]);
+    codeFirstAddrs[t->id] = 0;
+    threadCodeSizes[t->id] = 0;
+    if (curProc->globals != NULL) {
+      free(curProc->globals);
+    }
+    free(curProc);
+  }
+
+  task void start_prog()
+  {
+    free(tablesMemory);
+    proc = malloc(sizeof(init_block_t));
+    proc->globals = gVarMemory;
+    proc->init_ptr = (void *)((uint16_t)code + prog_desc.main_addr);
+    proc->init_arg = NULL;
+    call ReferenceCounter.init( &(proc->thread_counter) );
+    
+    if (call DynamicThread.create(&handler, run_proc, proc, 200) == SUCCESS) {  
+      codeFirstAddrs[handler] = (uint16_t)code;
+      threadCodeSizes[handler] = prog_desc.code_count;
+      retError = SUCCESS;
+      post loadDoneTask();
+    } else {
+      retError = FAIL;
+      post loadDoneTask();
+      if (proc->globals != NULL) {
+        free(proc->globals);
+      }
+      free(proc);
+    }
+    
+    initProgDesc();
+  }
+
+  // Gets write access to the internal flash
+  void eeprom_w()
+  {
+    FCTL2 = FWKEY + FSSEL1 + FN2;   // selects SMCLK and divides it by 4
+    FCTL3 = FWKEY;   // enables the writing/erasing by clearing the LOCK bit
+    FCTL1 = FWKEY + WRT;   // enables the writing
+  }
+
+  // Gets read-only access to the internal flash
+  void eeprom_r()
+  {
+    FCTL1 = FWKEY;   // Clear WRT bit
+    FCTL3 = FWKEY + LOCK;   // disabling the writing/erasing
+  }
+
+  // Calculates where should the code be placed in the internal flash
+  /*
+  uint16_t eeprom_code_addr()
+  {
+    uint16_t addr;
+    
+    addr = (((prog_desc.code_count - 1) / 512) + 1) * 512;   // Spaces needed for the code
+    addr = 0xFFFF - 512 - addr + 1;   // The 1 is to align the code
+    
+    return addr; 
+  }
+  */
+
+  // Loads program image meta data
+  void loader_metaData()
+  {
+    prog_desc.patch_table_count = prog_desc.alloc_count +
+                                  prog_desc.g_reloc_count +
+                                  prog_desc.l_reloc_count;
+    prog_desc.code_offset = sizeof(prog_desc.main_addr) +
+                            sizeof(prog_desc.alloc_count) +
+                            sizeof(prog_desc.alloc_size) +
+                            sizeof(prog_desc.g_reloc_count) +
+                            sizeof(prog_desc.l_reloc_count) +
+                            sizeof(prog_desc.datasec_count) +
+                            sizeof(prog_desc.code_count) +
+                            (prog_desc.patch_table_count * 4) +
+                            (prog_desc.datasec_count * 6);
+    
+    if (prog_desc.patch_table_count > 0 || prog_desc.datasec_count > 0) {
+      if ((prog_desc.patch_table_count * 4) > (prog_desc.datasec_count * 6)) {
+        tablesMemory = malloc(prog_desc.patch_table_count * 4);
+      } else {
+        tablesMemory = malloc(prog_desc.datasec_count * 6);
+      }
+    } else {
+      tablesMemory = NULL;
+    }
+  }
+
+  // Prepares the patch table before patching addresses in the binary code
+  void loader_patch_table()
+  {
+    uint16_t i, tempUInt16 = 0;
+    
+    // Find out the total space global variables need, and malloc
+    /*
+    for (i = 0; i < (prog_desc.alloc_count * 4); i+=4) {
+      tempUInt16 += *((uint16_t *)&tablesMemory[i]);
+      *((uint16_t *)&tablesMemory[i]) = tempUInt16 - *((uint16_t *)&tablesMemory[i]);
+    }
+    */
+    if (prog_desc.alloc_size > 0) {
+      gVarMemory = malloc(prog_desc.alloc_size);
+      memset(gVarMemory, 0, prog_desc.alloc_size);
+    } else {
+      gVarMemory = NULL;
+    }
+    
+    // Some "real" addresses need offsets added. For example, local relocation table entries need
+    // the starting code address
+    for (i = 0; i < (prog_desc.patch_table_count * 4); i+=4) {
+      if (i < (prog_desc.alloc_count * 4)) {
+        tempUInt16 = (uint16_t)gVarMemory;   // Allocation table needs memory's offset
+      } else if (i < ((prog_desc.alloc_count + prog_desc.g_reloc_count) * 4)) {
+        tempUInt16 = 0;   // Global relocation table doesn't need anything
+      } else {
+        tempUInt16 = (uint16_t)code;   // Local relocation table needs code's offset
+      }
+      *((uint16_t *)&(tablesMemory[i])) = *((uint16_t *)&(tablesMemory[i])) + tempUInt16;   // Writes the real address
+    }
+    
+    // Converts function IDs in global relocation table to real addresses
+    for (i = (prog_desc.alloc_count * 4); i < ((prog_desc.alloc_count + prog_desc.g_reloc_count) * 4); i+=4) {
+      tempUInt16 = *((uint16_t *)&tablesMemory[i]);   // Gets function ID
+      tempUInt16 = (uint16_t)fun[tempUInt16].addr;   // Gets the real address of the function ID
+      *((uint16_t *)&tablesMemory[i]) = tempUInt16;   // Writes the real address
+    }
+  }
+  
+  void loader_addr_1()
+  {
+    uint16_t i, laddr = 0, raddr = 0;
+    
+    // Resets before start
+    nextTask_chain = 0x0;
+    nextAddr = 0;
+    
+    // Gets the next task by searching for the lowest next patching address
+    raddr = 0xFFFF;   // Temp variable to store the minimum patching address so far
+    laddr = 0;   // Temp variable to store the current patching address
+    for (i = 0; i < (prog_desc.patch_table_count * 4); i+=4) {
+      laddr = *((uint16_t *)&tablesMemory[i + 2]);
+      
+      if (((uint16_t)nextTask_chain == 0x0 && laddr != 0xFFFF) || 
+           raddr > laddr) {
+        nextTask_chain = &(tablesMemory[i]);
+        raddr = laddr;
+      }
+    }
+    
+    if (nextTask_chain != 0x0) {
+      // Gets the next patching address in the chain from the flash
+      raddr = *((uint16_t *)&nextTask_chain[2]);
+      call ImageRead.read[readSource](readSourceOffset + prog_desc.code_offset + raddr, 
+                                      &nextAddr, 
+                                      2);
+    } else {
+      // Copies the rest of the binary code
+      call ImageRead.read[readSource](readSourceOffset + prog_desc.code_offset + codePtr, 
+                                      &(code[codePtr]), 
+                                      prog_desc.code_count - codePtr);
+      prog_desc.loading_stage++;
+    }
+  }
+  
+  // Patches the part of binary code that needs "real" addresses
+  void loader_addr_2()
+  {
+    uint16_t laddr;
+    
+    laddr = *((uint16_t *)&nextTask_chain[2]);   // Gets the current patching address
+    
+    // Updates the chain with the next patching address
+    if (nextAddr == 0x0000) {
+      nextAddr = 0xFFFF;   // End of chain, marks it with a big number
+    }
+    *((uint16_t *)&nextTask_chain[2]) = nextAddr;
+    
+    // Patches address in the binary code
+    *((uint16_t *)&code[laddr]) = *((uint16_t *)&nextTask_chain[0]);
+    
+    // Copies the binary code between the last patching address and the current patching address
+    call ImageRead.read[readSource](readSourceOffset + prog_desc.code_offset + codePtr, 
+                                    &(code[codePtr]), 
+                                    laddr - codePtr);
+    codePtr = laddr + 2;   // Notes up to what location in the binary code we have copied
+  }
+  
+  void loader_datasec()
+  {
+    uint16_t i, j;
+    
+    for (i = 0; i < (prog_desc.datasec_count * 6); i+=6) {
+      uint16_t destAddr = *((uint16_t *)&(tablesMemory[i])) + (uint16_t)gVarMemory;   // Writes the real address
+      uint16_t srcAddr = *((uint16_t *)&(tablesMemory[i + 2])) + (uint16_t)code;   // Writes the real address
+      uint16_t size = *((uint16_t *)&(tablesMemory[i + 4]));
+      
+      for (j = 0; j < size; j++) {
+        ((uint8_t *)((void *)(destAddr + j)))[0] = ((uint8_t *)((void *)(srcAddr + j)))[0];
+        //*((uint8_t *)&code[destAddr + j]) = *((uint8_t *)&code[srcAddr + j]);
+      }
+    }
+  }
+  
+  void loadProgram()
+  {
+    error_t error = SUCCESS;
+    switch (prog_desc.loading_stage) {
+      case 0:
+        // Loads meta data to memory array
+        error = call ImageRead.read[readSource](readSourceOffset + 0, 
+                                                &prog_desc, 
+                                                7 * 2);
+        if (error == SUCCESS) {
+          prog_desc.loading_stage++;   // Moves to next loading phase
+        } else {
+          errorHandler();
+        }
+        break;
+      
+      case 1:
+        loader_metaData();   // Gets meta data
+        code = (void *) call PMManager.request(prog_desc.code_count);   // Gets the location of where the code will be copied to
+        
+        if ((uint16_t)code != 0xFFFF) {
+          // Loads patch table to memory array
+          error = call ImageRead.read[readSource](readSourceOffset + 7 * 2, 
+                                                  tablesMemory, 
+                                                  prog_desc.patch_table_count * 4);
+          if (error == SUCCESS) {
+            prog_desc.loading_stage++;   // Moves to next loading phase
+          } else {
+            errorHandler();
+          }
+        } else {
+          errorHandler();
+        }
+        
+        break;
+      
+      case 2:
+        loader_patch_table();
+        
+        eeprom_w();   // Gets write-access to internal flash
+        prog_desc.loading_stage++;   // Moves to next loading phase
+      
+      case 3:
+        loader_addr_1();
+        break;
+      
+      case 4:
+        eeprom_r();   // Locks the internal flash back
+        
+        error = call ImageRead.read[readSource](readSourceOffset + 7 * 2 + prog_desc.patch_table_count * 4, 
+                                                tablesMemory, 
+                                                prog_desc.datasec_count * 6);
+        if (error == SUCCESS) {
+          prog_desc.loading_stage++;   // Moves to next loading phase
+        } else {
+          errorHandler();
+        }
+        
+        break;
+      case 5:
+        loader_datasec();
+        prog_desc.loading_stage++;
+        
+      case 6:
+        post start_prog();
+        break;
+    }
+  }
+  
+  command error_t Init.init()
+  {
+    int i;
+    for (i = 0; i < TOSTHREAD_MAX_NUM_THREADS; i++) {
+      threadCodeSizes[i] = 0;
+      codeFirstAddrs[i] = 0;
+    }
+    initProgDesc();
+    return SUCCESS;
+  }
+  
+  task void taskLoadProgram()
+  {
+    loadProgram();
+  }
+  
+  error_t start_load(uint8_t in_readSource, uint16_t in_readSourceOffset)
+  {
+    if (prog_desc.loading_stage == 0) {
+      uint16_t i;
+      call Leds.set(7);
+      for (i = 0; i < 2000; i++) { }
+      call Leds.set(0);
+      
+      readSource = in_readSource;
+      readSourceOffset = in_readSourceOffset;
+      post taskLoadProgram();   // Start Loading
+      
+      return SUCCESS;
+    }
+    
+    return EBUSY;
+  }
+  
+  command error_t DynamicLoader.loadFromFlash(uint8_t volumeId) { return start_load(volumeId, 0); }
+  
+  command error_t DynamicLoader.loadFromMemory(void *addr) { return start_load(READSOURCE_MEMORY, (uint16_t)addr); }
+  
+  event void ImageRead.readDone[uint8_t id](storage_addr_t addr, void* buf, storage_len_t len, error_t error)
+  {
+    if (error == SUCCESS) {
+      if (buf == &nextAddr) {
+        loader_addr_2();
+      } else {
+        post taskLoadProgram();
+      }
+    } else {
+      errorHandler();
+    }
+  }
+
+#ifndef DISABLE_LOADER_USERBUTTON
+  event void UserButton.fired()
+  {
+     call DynamicLoader.loadFromFlash(VOLUME_MICROEXEIMAGE);
+  }
+#endif
+  
+  event void ImageRead.computeCrcDone[uint8_t id](storage_addr_t addr, storage_len_t len, uint16_t crc, error_t error) {}
+  
+  default command error_t ImageRead.read[uint8_t id](storage_addr_t addr, void *buf, storage_len_t len) { return FAIL; }
+  default event void DynamicLoader.loadFromFlashDone(uint8_t volumeId, tosthread_t id, error_t error) {}
+  default event void DynamicLoader.loadFromMemoryDone(void *addr, tosthread_t id, error_t error) {}
+}
diff --git a/tos/lib/tosthreads/lib/tinyld/LoadSourceMapC.nc b/tos/lib/tosthreads/lib/tinyld/LoadSourceMapC.nc
new file mode 100755 (executable)
index 0000000..8ccd4bb
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2008 Johns Hopkins University.
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose, without fee, and without written
+ * agreement is hereby granted, provided that the above copyright
+ * notice, the (updated) modification history and the author appear in
+ * all copies of this source code.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, LOSS OF USE, DATA,
+ * OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+/**
+ * @author Chieh-Jan Mike Liang <cliang4@cs.jhu.edu>
+ */
+
+configuration LoadSourceMapC
+{
+  provides interface BlockRead[uint8_t id];
+}
+
+implementation
+{
+  components LoadSourceMapP,
+        #ifdef DISABLE_LOADER_FLASH
+             NullVolumeMapC as VolumeMapC,
+        #else
+             VolumeMapC,
+        #endif
+             MemoryStorageC;
+  
+  BlockRead = LoadSourceMapP;
+  
+  LoadSourceMapP.SubBlockRead -> VolumeMapC;
+  LoadSourceMapP.SubMemoryRead -> MemoryStorageC;
+}
diff --git a/tos/lib/tosthreads/lib/tinyld/LoadSourceMapP.nc b/tos/lib/tosthreads/lib/tinyld/LoadSourceMapP.nc
new file mode 100755 (executable)
index 0000000..ca55766
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2008 Johns Hopkins University.
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose, without fee, and without written
+ * agreement is hereby granted, provided that the above copyright
+ * notice, the (updated) modification history and the author appear in
+ * all copies of this source code.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, LOSS OF USE, DATA,
+ * OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+/**
+ * @author Chieh-Jan Mike Liang <cliang4@cs.jhu.edu>
+ */
+
+#include "DynamicLoader.h"
+
+module LoadSourceMapP
+{
+  provides interface BlockRead[uint8_t id];
+  
+  uses {
+    interface BlockRead as SubBlockRead[uint8_t id];
+    interface BlockRead as SubMemoryRead;
+  }
+}
+
+implementation
+{
+  event void SubMemoryRead.readDone(storage_addr_t addr, void* buf, storage_len_t len, error_t error)
+  {
+    signal BlockRead.readDone[READSOURCE_MEMORY](addr, buf, len, error);
+  }
+  
+  event void SubBlockRead.readDone[uint8_t id](storage_addr_t addr, void* buf, storage_len_t len, error_t error)
+  {
+    signal BlockRead.readDone[id](addr, buf, len, error);
+  }
+                          
+  command error_t BlockRead.read[uint8_t id](storage_addr_t addr, void* buf, storage_len_t len)
+  {
+    error_t error = FAIL;
+    
+    if (id == READSOURCE_MEMORY) {
+      error = call SubMemoryRead.read(addr, buf, len);
+    } else {
+      error = call SubBlockRead.read[id](addr, buf, len);
+    }
+    
+    return error;
+  }
+
+  event void SubMemoryRead.computeCrcDone(storage_addr_t addr, storage_len_t len, uint16_t crc, error_t error)
+  {
+    signal BlockRead.computeCrcDone[READSOURCE_MEMORY](addr, len, crc, error);
+  }
+
+  event void SubBlockRead.computeCrcDone[uint8_t id](storage_addr_t addr, storage_len_t len, uint16_t crc, error_t error)
+  {
+    signal BlockRead.computeCrcDone[id](addr, len, crc, error);
+  }
+  
+  command error_t BlockRead.computeCrc[uint8_t id](storage_addr_t addr, storage_len_t len, uint16_t crc)
+  {
+    error_t error = FAIL;
+    
+    if (id == READSOURCE_MEMORY) {
+      error = call SubMemoryRead.computeCrc(addr, len, crc);
+    } else {
+      error = call SubBlockRead.computeCrc[id](addr, len, crc);
+    }
+    
+    return error;
+  }
+  
+  command storage_len_t BlockRead.getSize[uint8_t id]()
+  {
+    storage_len_t len;
+    
+    if (id == READSOURCE_MEMORY) {
+      len = call SubMemoryRead.getSize();
+    } else {
+      len = call SubBlockRead.getSize[id]();
+    }
+    
+    return len;
+  }
+  
+  default command error_t SubBlockRead.read[uint8_t id](storage_addr_t addr, void* buf, storage_len_t len) { return FAIL; }
+  default command error_t SubBlockRead.computeCrc[uint8_t id](storage_addr_t addr, storage_len_t len, uint16_t crc) { return FAIL; }
+  default command storage_len_t SubBlockRead.getSize[uint8_t id]() { return 0; }
+  default event void BlockRead.readDone[uint8_t id](storage_addr_t addr, void* buf, storage_len_t len, error_t error) {}
+  default event void BlockRead.computeCrcDone[uint8_t id](storage_addr_t addr, storage_len_t len, uint16_t crc, error_t error) {}
+}
diff --git a/tos/lib/tosthreads/lib/tinyld/MemoryStorageC.nc b/tos/lib/tosthreads/lib/tinyld/MemoryStorageC.nc
new file mode 100755 (executable)
index 0000000..8134e53
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2008 Johns Hopkins University.
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose, without fee, and without written
+ * agreement is hereby granted, provided that the above copyright
+ * notice, the (updated) modification history and the author appear in
+ * all copies of this source code.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, LOSS OF USE, DATA,
+ * OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+/**
+ * @author Chieh-Jan Mike Liang <cliang4@cs.jhu.edu>
+ */
+configuration MemoryStorageC
+{
+  provides interface BlockRead;
+}
+
+implementation
+{
+  components MemoryStorageP,
+             CrcC;
+  
+  BlockRead = MemoryStorageP;
+  
+  MemoryStorageP.Crc -> CrcC;
+}
diff --git a/tos/lib/tosthreads/lib/tinyld/MemoryStorageP.nc b/tos/lib/tosthreads/lib/tinyld/MemoryStorageP.nc
new file mode 100755 (executable)
index 0000000..2782cc7
--- /dev/null
@@ -0,0 +1,56 @@
+module MemoryStorageP
+{
+  provides interface BlockRead;
+  uses interface Crc;
+}
+
+implementation
+{
+  storage_addr_t retAddr;
+  void *retBuf;
+  storage_len_t retLen;
+  uint16_t retCrc;
+
+  task void taskReadDone()
+  {
+    signal BlockRead.readDone(retAddr, retBuf, retLen, SUCCESS);
+  }
+
+  command error_t BlockRead.read(storage_addr_t addr, void* buf, storage_len_t len)
+  {
+    storage_len_t i;
+    uint8_t *from = (uint8_t *)((void *)((uint16_t)addr));
+    
+    for (i = 0; i < len; i++) {
+      ((uint8_t *)buf)[i] = from[i];
+    }
+    
+    retAddr = addr;
+    retBuf = buf;
+    retLen = len;
+    post taskReadDone();
+    return SUCCESS;
+  }
+  
+  task void taskCrcDone()
+  {
+    signal BlockRead.computeCrcDone(retAddr, retLen, retCrc, SUCCESS);
+  }
+  
+  command error_t BlockRead.computeCrc(storage_addr_t addr, storage_len_t len, uint16_t crc)
+  {
+    retCrc = call Crc.seededCrc16(crc, (void *)addr, len);
+    retAddr = addr;
+    retLen = len;
+    post taskCrcDone();
+    return SUCCESS;
+  }
+  
+  command storage_len_t BlockRead.getSize()
+  {
+    return 0;   // Not sure what to do
+  }
+  
+  default event void BlockRead.readDone(storage_addr_t addr, void* buf, storage_len_t len, error_t error) {}
+  default event void BlockRead.computeCrcDone(storage_addr_t addr, storage_len_t len, uint16_t crc, error_t error) {}
+}
diff --git a/tos/lib/tosthreads/lib/tinyld/NullVolumeMapC.nc b/tos/lib/tosthreads/lib/tinyld/NullVolumeMapC.nc
new file mode 100755 (executable)
index 0000000..0c0084f
--- /dev/null
@@ -0,0 +1,18 @@
+
+/**
+ * @author Jeongyeup Paek <jpaek@enl.usc.edu>
+ */
+
+#include "DynamicLoader.h"
+
+module NullVolumeMapC
+{
+  provides interface BlockRead[uint8_t id];
+}
+
+implementation
+{
+  command error_t BlockRead.read[uint8_t id](storage_addr_t addr, void* buf, storage_len_t len) { return FAIL; }
+  command error_t BlockRead.computeCrc[uint8_t id](storage_addr_t addr, storage_len_t len, uint16_t crc) { return FAIL; }
+  command storage_len_t BlockRead.getSize[uint8_t id]() { return 0; }
+}
diff --git a/tos/lib/tosthreads/lib/tinyld/PMManager.nc b/tos/lib/tosthreads/lib/tinyld/PMManager.nc
new file mode 100644 (file)
index 0000000..01b44f1
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2008 Johns Hopkins University.
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose, without fee, and without written
+ * agreement is hereby granted, provided that the above copyright
+ * notice, the (updated) modification history and the author appear in
+ * all copies of this source code.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, LOSS OF USE, DATA,
+ * OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+/**
+ * @author Chieh-Jan Mike Liang <cliang4@cs.jhu.edu>
+ */
+
+interface PMManager
+{
+  command uint16_t request(uint16_t size);
+  command void release(uint16_t startingAddr, uint16_t size);
+}
diff --git a/tos/lib/tosthreads/lib/tinyld/PMManagerC.nc b/tos/lib/tosthreads/lib/tinyld/PMManagerC.nc
new file mode 100644 (file)
index 0000000..ab7988b
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2008 Johns Hopkins University.
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose, without fee, and without written
+ * agreement is hereby granted, provided that the above copyright
+ * notice, the (updated) modification history and the author appear in
+ * all copies of this source code.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, LOSS OF USE, DATA,
+ * OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+/**
+ * @author Chieh-Jan Mike Liang <cliang4@cs.jhu.edu>
+ */
+
+configuration PMManagerC
+{
+  provides interface PMManager;
+}
+
+implementation
+{
+  components MainC,
+             PMManagerP,
+             LedsC,
+             BitArrayUtilsC;
+  
+  PMManager = PMManagerP;
+  
+  MainC.SoftwareInit -> PMManagerP;
+  PMManagerP.BitArrayUtils -> BitArrayUtilsC;
+  PMManagerP.Leds -> LedsC;
+}
diff --git a/tos/lib/tosthreads/lib/tinyld/PMManagerP.nc b/tos/lib/tosthreads/lib/tinyld/PMManagerP.nc
new file mode 100644 (file)
index 0000000..35558e5
--- /dev/null
@@ -0,0 +1,139 @@
+/*
+ * Copyright (c) 2008 Johns Hopkins University.
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose, without fee, and without written
+ * agreement is hereby granted, provided that the above copyright
+ * notice, the (updated) modification history and the author appear in
+ * all copies of this source code.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, LOSS OF USE, DATA,
+ * OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+/**
+ * @author Chieh-Jan Mike Liang <cliang4@cs.jhu.edu>
+ */
+
+module PMManagerP {
+  provides {
+    interface Init;
+    interface PMManager;
+  }
+  
+  uses {
+    interface BitArrayUtils;
+    interface Leds;
+  }
+}
+
+implementation {
+  uint16_t HOST_ROM_SIZE = 0;   // Set by tos-set-symbol
+  uint16_t SEGMENT_SIZE = 512;   // For telosb, the internal flash segment size is 512 bytes
+  uint16_t FLASH_ROM_START_ADDR = 0x4000;   // For telosb, the program memory starts at 0x4000
+  uint16_t FLASH_ROM_END_ADDR = 0xFDFF;   // Last free byte (the last segment is used for interrupt vector)
+  
+  uint16_t numFreeSegments = 0;
+  uint8_t *segmentBitArray;
+  
+  command error_t Init.init()
+  {
+    uint16_t numBytes;
+    
+    // Adjust FLASH_ROM_START_ADDR to account for the code loaded with PMManager
+    if (HOST_ROM_SIZE == 0) {
+      // Should not be here at all
+      // return FAIL;
+    } else {
+      FLASH_ROM_START_ADDR += (((HOST_ROM_SIZE - 1) / SEGMENT_SIZE) + 1) * SEGMENT_SIZE;
+    }
+    
+    // Calculates the number of available segments
+    numFreeSegments = FLASH_ROM_END_ADDR - FLASH_ROM_START_ADDR + 1;
+    numFreeSegments = ((numFreeSegments - 1) / SEGMENT_SIZE) + 1;
+    
+    // Initializes an bit array to track the status of available segments
+    numBytes = ((numFreeSegments - 1) / 8) + 1;
+    segmentBitArray = malloc(numBytes);
+    call BitArrayUtils.clrArray(segmentBitArray, numBytes);
+    
+    return SUCCESS;
+  }
+  
+  uint16_t bitIndexToAddress(uint16_t bitIndex)
+  {
+    return FLASH_ROM_START_ADDR + (bitIndex * SEGMENT_SIZE);
+  }
+
+  void eraseSegment(void* addr)
+  {
+    FCTL2 = FWKEY + FSSEL1 + FN2;
+    FCTL3 = FWKEY;
+    FCTL1 = FWKEY + ERASE;
+    *((uint16_t *)addr) = 0;
+    FCTL1 = FWKEY;
+    FCTL3 = FWKEY + LOCK;
+  }
+
+  command uint16_t PMManager.request(uint16_t size)
+  {    
+    if (size > 0) {
+      uint8_t numSegments = ((size - 1) / SEGMENT_SIZE) + 1;   // Number of segments needed to cover size
+      int i;
+      
+      for (i = (numFreeSegments - 1); i >= 0; i--) {
+        if (call BitArrayUtils.getBit(segmentBitArray, i) == FALSE) {
+          int j, tempNumSegments = numSegments - 1;
+          
+          for (j = (i - 1); j >= 0 && tempNumSegments > 0; ) {
+            // Checks if there are enough consecutive free segments
+            if (call BitArrayUtils.getBit(segmentBitArray, j) == TRUE) {
+              break;
+            } else {
+              j--;
+              tempNumSegments--;
+            }
+          }
+          j++;
+          if ((i - j + 1) >= numSegments) {
+            // There are enough consecutive free segments (starting segment index (j + 1))
+            int k;
+            for (k = j; k <= i; k++) {
+              eraseSegment((void *)bitIndexToAddress(k));   // Erase segment content
+              call BitArrayUtils.setBit(segmentBitArray, k);   // Mark segment as occupied
+            }
+            
+            return bitIndexToAddress(j);
+          } else {
+            i = j;
+          }
+        }
+      }
+    }
+    
+    return 0xFFFF;
+  }
+  
+  command void PMManager.release(uint16_t startingAddr, uint16_t size)
+  {
+    if ((startingAddr >= FLASH_ROM_START_ADDR && startingAddr <= FLASH_ROM_END_ADDR) &&
+        size > 0) {
+      uint8_t numSegments = ((size - 1) / SEGMENT_SIZE) + 1;   // Number of segments needed to cover size
+      uint8_t startingSegment = (startingAddr - FLASH_ROM_START_ADDR) / SEGMENT_SIZE;
+      int i;
+      
+      for (i = 0; i < numSegments && (i + startingSegment) < numFreeSegments; i++) {
+        call BitArrayUtils.clrBit(segmentBitArray, i + startingSegment);   // Mark the segment as free
+      }
+    }
+  }
+}
diff --git a/tos/lib/tosthreads/lib/tinyld/TosThreadApiC.nc b/tos/lib/tosthreads/lib/tinyld/TosThreadApiC.nc
new file mode 100644 (file)
index 0000000..225de13
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * Copyright (c) 2008 Stanford University.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the
+ *   distribution.
+ * - Neither the name of the Stanford University nor the names of
+ *   its contributors may be used to endorse or promote products derived
+ *   from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL STANFORD
+ * UNIVERSITY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+/**
+ * @author Kevin Klues <klueska@cs.stanford.edu>
+ */
+#include "tosthread.h"
+
+configuration TosThreadApiC {
+}
+implementation {
+  //Here are all the components that implement the Tosthread API calls
+  components CThreadC;
+  #if defined(PRINTF_H) || defined(TOSTHREAD_DYNAMIC_LOADER)
+    components PrintfC;
+  #endif
+  #if defined(TOSTHREAD_QUEUE_H) || defined(TOSTHREAD_DYNAMIC_LOADER)
+    components CQueueC;
+  #endif
+  #if defined(TOSTHREAD_LINKED_LIST_H) || defined(TOSTHREAD_DYNAMIC_LOADER)
+    components CLinkedListC;
+  #endif
+  #if defined(TOSTHREAD_THREADSYNC_H) || defined(TOSTHREAD_DYNAMIC_LOADER)
+    components CThreadSynchronizationC;
+  #endif
+  #if defined(TOSTHREAD_LEDS_H) || defined(TOSTHREAD_DYNAMIC_LOADER)
+    components CLedsC;
+  #endif
+  #if defined(TOSTHREAD_AMRADIO_H) || defined(TOSTHREAD_DYNAMIC_LOADER)
+    components CAMRadioC;
+  #endif
+  #if defined(TOSTHREAD_AMSERIAL_H) || defined(TOSTHREAD_DYNAMIC_LOADER)
+    components CAMSerialC;
+  #endif
+  #if defined(TOSTHREAD_BLOCKSTORAGE_H) || defined(TOSTHREAD_DYNAMIC_LOADER)
+    components CBlockStorageC;
+  #endif 
+  #if defined(TOSTHREAD_LOGSTORAGE_H) || defined(TOSTHREAD_DYNAMIC_LOADER)
+    components CLogStorageC;
+  #endif 
+  #if defined(TOSTHREAD_COLLECTION_H) || defined(TOSTHREAD_DYNAMIC_LOADER)
+  //  components CCollectionC;
+  #endif 
+  
+  //Telosb sensorboard specific.
+  #if defined(TOSTHREAD_HAMAMATSUS1087_H) || defined(TOSTHREAD_DYNAMIC_LOADER)
+    components CHamamatsuS1087ParC;
+  #endif
+  #if defined(TOSTHREAD_HAMAMATSUS10871_H) || defined(TOSTHREAD_DYNAMIC_LOADER)
+    components CHamamatsuS10871TsrC;
+  #endif
+  #if defined(TOSTHREAD_SENSIRIONSHT11_H) || defined(TOSTHREAD_DYNAMIC_LOADER)
+    components CSensirionSht11C;
+  #endif
+  
+  //Universal sensorboard specific
+  #if defined(TOSTHREAD_SINESENSOR_H) || defined(TOSTHREAD_DYNAMIC_LOADER)
+  //  components CSineSensorC;
+  #endif
+  
+  //Basicsb sensorboard specific
+  #if defined(TOSTHREAD_PHOTO_H) || defined(TOSTHREAD_DYNAMIC_LOADER)
+  //  components CPhotoC;
+  #endif
+  #if defined(TOSTHREAD_TEMP_H) || defined(TOSTHREAD_DYNAMIC_LOADER)
+  //  components CTempC;
+  #endif
+}
diff --git a/tos/lib/tosthreads/lib/tinyld/UserButton.nc b/tos/lib/tosthreads/lib/tinyld/UserButton.nc
new file mode 100755 (executable)
index 0000000..8b10c14
--- /dev/null
@@ -0,0 +1,4 @@
+interface UserButton
+{
+  event void fired();
+}
diff --git a/tos/lib/tosthreads/lib/tinyld/UserButtonC.nc b/tos/lib/tosthreads/lib/tinyld/UserButtonC.nc
new file mode 100755 (executable)
index 0000000..29ac64e
--- /dev/null
@@ -0,0 +1,48 @@
+/* "Copyright (c) 2000-2003 The Regents of the University of California.  
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose, without fee, and without written agreement
+ * is hereby granted, provided that the above copyright notice, the following
+ * two paragraphs and the author appear in all copies of this software.
+ * 
+ * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
+ * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
+ * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY
+ * OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * 
+ * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
+ * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
+ * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
+ */
+
+/**
+ * Init/start/stop the user button with StdControl and get interrupt events
+ * when the button is released with MSP430Event.
+ *
+ * @author Cory Sharp <cssharp@eecs.berkeley.edu>
+ * @author Andrew Redfern <aredfern@kingkong.me.berkeley.edu>
+ */
+
+configuration UserButtonC
+{
+  provides interface UserButton;
+}
+
+implementation
+{
+  components MainC, new TimerMilliC(),
+    HplMsp430GeneralIOC, HplMsp430InterruptC,
+    UserButtonP;
+
+  UserButton = UserButtonP;
+
+  MainC.SoftwareInit -> UserButtonP;
+
+  UserButtonP -> HplMsp430GeneralIOC.Port27;
+  UserButtonP -> HplMsp430InterruptC.Port27;
+  UserButtonP.Timer -> TimerMilliC;
+}
+
diff --git a/tos/lib/tosthreads/lib/tinyld/UserButtonP.nc b/tos/lib/tosthreads/lib/tinyld/UserButtonP.nc
new file mode 100755 (executable)
index 0000000..7f91314
--- /dev/null
@@ -0,0 +1,77 @@
+/* "Copyright (c) 2000-2003 The Regents of the University of California.  
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose, without fee, and without written agreement
+ * is hereby granted, provided that the above copyright notice, the following
+ * two paragraphs and the author appear in all copies of this software.
+ * 
+ * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
+ * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
+ * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY
+ * OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * 
+ * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
+ * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
+ * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
+ */
+
+/**
+ * Andew's timer debouce logic used from the CountInput application.
+ *
+ * @author Cory Sharp <cssharp@eecs.berkeley.edu>
+ * @author Andrew Redfern <aredfern@kingkong.me.berkeley.edu>
+ */
+
+module UserButtonP
+{
+  provides {
+    interface Init;
+    interface UserButton;
+  }
+  uses {
+    interface HplMsp430GeneralIO;
+    interface HplMsp430Interrupt;
+    interface Timer<TMilli>;
+  }
+}
+implementation
+{
+  command error_t Init.init()
+  {
+    atomic {
+      call HplMsp430Interrupt.disable();
+      call HplMsp430GeneralIO.makeInput();
+      call HplMsp430GeneralIO.selectIOFunc();
+      call HplMsp430Interrupt.edge(TRUE);
+      call HplMsp430Interrupt.clear();
+      call HplMsp430Interrupt.enable();
+    }
+    return SUCCESS;
+  }
+
+  event void Timer.fired()
+  {
+    atomic {
+      call HplMsp430Interrupt.clear();
+      call HplMsp430Interrupt.enable();
+    }
+  }
+
+  task void debounce()
+  {
+    call Timer.startOneShot(100);
+    signal UserButton.fired();
+  }
+
+  async event void HplMsp430Interrupt.fired()
+  {
+    atomic {
+      call HplMsp430Interrupt.disable();
+      post debounce();
+    }
+  }
+}
+
diff --git a/tos/lib/tosthreads/lib/tinyld/slcs_types.h b/tos/lib/tosthreads/lib/tinyld/slcs_types.h
new file mode 100755 (executable)
index 0000000..319add1
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) 2008 Johns Hopkins University.
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose, without fee, and without written
+ * agreement is hereby granted, provided that the above copyright
+ * notice, the (updated) modification history and the author appear in
+ * all copies of this source code.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, LOSS OF USE, DATA,
+ * OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+/**
+ * @author Chieh-Jan Mike Liang <cliang4@cs.jhu.edu>
+ */
+
+#ifndef _SLCS_TYPES_H
+#define _SLCS_TYPES_H
+
+#include "tosthread.h"
+#include "tosthread_leds.h"
+#include "tosthread_amradio.h"
+#include "tosthread_blockstorage.h"
+#include "tosthread_threadsync.h"
+#include "tosthread_amserial.h"
+#include "tosthread_queue.h"
+#include "tosthread_sensirionSht11.h"
+#include "tosthread_hamamatsuS10871.h"
+#include "tosthread_hamamatsuS1087.h"
+
+struct value_addr_pair {
+  uint16_t value;
+  void *addr;
+};
+
+struct addr {
+  void *addr;
+};
+
+struct prog_desc {
+  uint16_t main_addr;   // Loadable program's main function (or tosthread_main() in our case)
+  uint16_t alloc_count;
+  uint16_t alloc_size;
+  uint16_t g_reloc_count;
+  uint16_t l_reloc_count;
+  uint16_t datasec_count;
+  uint16_t code_count;
+  
+  uint16_t patch_table_count;   // alloc_count + g_reloc_count + l_reloc_count;
+  uint16_t code_offset;   // sizeof(main_addr) +
+                          // sizeof(alloc_count) +
+                          // sizeof(alloc_size) +
+                          // sizeof(g_reloc_count) +
+                          // sizeof(l_reloc_count) +
+                          // sizeof(datasec_count) +
+                          // sizeof(code_count) +
+                          // (g_sym_count + patch_table_count) * 4
+  
+  uint16_t loading_stage;
+};
+
+#endif
diff --git a/tos/lib/tosthreads/lib/tinyld/tosthread_slcs_types.h b/tos/lib/tosthreads/lib/tinyld/tosthread_slcs_types.h
new file mode 100755 (executable)
index 0000000..3bf38b5
--- /dev/null
@@ -0,0 +1,112 @@
+/*
+ * Copyright (c) 2008 Johns Hopkins University.
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose, without fee, and without written
+ * agreement is hereby granted, provided that the above copyright
+ * notice, the (updated) modification history and the author appear in
+ * all copies of this source code.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, LOSS OF USE, DATA,
+ * OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+/**
+ * @author Chieh-Jan Mike Liang <cliang4@cs.jhu.edu>
+ */
+
+#ifndef _TOSTHREAD_SLCS_TYPES_H
+#define _TOSTHREAD_SLCS_TYPES_H
+
+#include "tosthread.h"
+#include "tosthread_leds.h"
+#include "tosthread_amradio.h"
+#include "tosthread_blockstorage.h"
+#include "tosthread_logstorage.h"
+#include "tosthread_threadsync.h"
+#include "tosthread_amserial.h"
+#include "tosthread_queue.h"
+#include "tosthread_sensirionSht11.h"
+#include "tosthread_hamamatsuS10871.h"
+#include "tosthread_hamamatsuS1087.h"
+
+
+/*******************************/
+#include "slcs_types.h"
+
+
+struct addr fun[] = {
+  
+  {tosthread_sleep}, {tosthread_create},
+    
+  {led0On}, {led1On}, {led2On},
+  {led0Off}, {led1Off}, {led2Off},
+  {led0Toggle}, {led1Toggle}, {led2Toggle},
+  
+  {amSerialStart}, {amSerialStop}, {amSerialReceive},
+  {amSerialSend}, {amSerialLocalAddress}, {amSerialGetLocalGroup},
+  {amSerialGetDestination}, {amSerialGetSource}, {amSerialSetDestination},
+  {amSerialSetSource}, {amSerialIsForMe}, {amSerialGetType},
+  {amSerialSetType}, {amSerialGetGroup}, {amSerialSetGroup},
+  {serialClear}, {serialGetPayloadLength}, {serialSetPayloadLength},
+  {serialMaxPayloadLength}, {serialGetPayload}, {serialRequestAck},
+  {serialNoAck}, {serialWasAcked},
+  
+  {amRadioStart}, {amRadioStop}, {amRadioReceive},
+  {amRadioSend}, {amRadioGetLocalAddress}, {amRadioGetLocalGroup},
+  {amRadioGetDestination}, {amRadioGetSource}, {amRadioSetDestination},
+  {amRadioSetSource}, {amRadioIsForMe}, {amRadioGetType},
+  {amRadioSetType}, {amRadioGetGroup}, {amRadioSetGroup},
+  {radioClear}, {radioGetPayloadLength}, {radioSetPayloadLength},
+  {radioMaxPayloadLength}, {radioGetPayload}, {radioRequestAck},
+  {radioNoAck}, {radioWasAcked},
+    
+  {semaphore_reset}, {semaphore_acquire}, {semaphore_release},
+
+  {barrier_reset}, {barrier_block}, {barrier_isBlocking},
+
+  {condvar_init}, {condvar_wait}, {condvar_signalNext},
+  {condvar_signalAll}, {condvar_isBlocking},
+
+  {mutex_init}, {mutex_lock}, {mutex_unlock},
+
+  {volumeBlockRead}, {volumeBlockWrite}, {volumeBlockCrc},
+  {volumeBlockErase}, {volumeBlockSync},
+
+  {refcounter_init}, {refcounter_increment}, {refcounter_decrement},
+  {refcounter_waitOnValue}, {refcounter_count},
+  
+  {amRadioSnoop},
+
+  {queue_init}, {queue_clear}, {queue_enqueue},
+  {queue_dequeue}, {queue_remove}, {queue_size},
+  {queue_is_empty},
+   
+  {sensirionSht11_humidity_read}, {sensirionSht11_humidity_getNumBits}, {sensirionSht11_temperature_read},
+  {sensirionSht11_temperature_getNumBits},
+  
+  {hamamatsuS10871_tsr_read}, {hamamatsuS10871_tsr_readStream}, {hamamatsuS10871_tsr_getNumBits},
+  
+  {hamamatsuS1087_par_read}, {hamamatsuS1087_par_readStream}, {hamamatsuS1087_par_getNumBits},
+  
+  {volumeLogRead}, {volumeLogCurrentReadOffset}, {volumeLogSeek},
+  {volumeLogGetSize},
+  
+  {volumeLogAppend}, {volumeLogCurrentWriteOffset}, {volumeLogErase},
+  {volumeLogSync},
+  
+  {getLeds}, {setLeds},
+
+  {div}
+};
+
+#endif