Ethernet test for tinydtls-0.5.0

Dependencies:   EthernetInterface mbed-rtos mbed tinydtls

Fork of tinydtls_test_ethernet by Ashley Mills

Committer:
ashleymills
Date:
Fri Oct 18 14:29:21 2013 +0000
Revision:
4:4d466a913c11
Parent:
1:391ec57807fa
Updated to tinydtls v0.5.0;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ashleymills 1:391ec57807fa 1 /* dbg.cpp */
ashleymills 1:391ec57807fa 2 /* Copyright (C) 2012 mbed.org, MIT License
ashleymills 1:391ec57807fa 3 *
ashleymills 1:391ec57807fa 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ashleymills 1:391ec57807fa 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
ashleymills 1:391ec57807fa 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
ashleymills 1:391ec57807fa 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
ashleymills 1:391ec57807fa 8 * furnished to do so, subject to the following conditions:
ashleymills 1:391ec57807fa 9 *
ashleymills 1:391ec57807fa 10 * The above copyright notice and this permission notice shall be included in all copies or
ashleymills 1:391ec57807fa 11 * substantial portions of the Software.
ashleymills 1:391ec57807fa 12 *
ashleymills 1:391ec57807fa 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ashleymills 1:391ec57807fa 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ashleymills 1:391ec57807fa 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ashleymills 1:391ec57807fa 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ashleymills 1:391ec57807fa 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ashleymills 1:391ec57807fa 18 */
ashleymills 1:391ec57807fa 19
ashleymills 1:391ec57807fa 20 #include "dbg.h"
ashleymills 1:391ec57807fa 21
ashleymills 1:391ec57807fa 22 #include "mbed.h"
ashleymills 1:391ec57807fa 23 #include "rtos.h"
ashleymills 1:391ec57807fa 24
ashleymills 1:391ec57807fa 25 #include <cstdio>
ashleymills 1:391ec57807fa 26 #include <cstdarg>
ashleymills 1:391ec57807fa 27
ashleymills 1:391ec57807fa 28 using namespace std;
ashleymills 1:391ec57807fa 29
ashleymills 1:391ec57807fa 30 static Serial debug_pc(USBTX, USBRX);
ashleymills 1:391ec57807fa 31
ashleymills 1:391ec57807fa 32 static char debug_newline[3];
ashleymills 1:391ec57807fa 33
ashleymills 1:391ec57807fa 34 static void debug_lock(bool set)
ashleymills 1:391ec57807fa 35 {
ashleymills 1:391ec57807fa 36 static Mutex* mtx = new Mutex(); //Singleton runtime initialisation to avoid static initialisation chaos problems
ashleymills 1:391ec57807fa 37 static bool init = false;
ashleymills 1:391ec57807fa 38 if(set)
ashleymills 1:391ec57807fa 39 {
ashleymills 1:391ec57807fa 40 mtx->lock();
ashleymills 1:391ec57807fa 41 if(!init)
ashleymills 1:391ec57807fa 42 {
ashleymills 1:391ec57807fa 43 strncpy( debug_newline, "\n", 2 );
ashleymills 1:391ec57807fa 44 printf("[START]\n");
ashleymills 1:391ec57807fa 45 fflush(stdout);
ashleymills 1:391ec57807fa 46 init = true;
ashleymills 1:391ec57807fa 47 }
ashleymills 1:391ec57807fa 48 }
ashleymills 1:391ec57807fa 49 else
ashleymills 1:391ec57807fa 50 {
ashleymills 1:391ec57807fa 51 mtx->unlock();
ashleymills 1:391ec57807fa 52 }
ashleymills 1:391ec57807fa 53 }
ashleymills 1:391ec57807fa 54
ashleymills 1:391ec57807fa 55 void debug_init()
ashleymills 1:391ec57807fa 56 {
ashleymills 1:391ec57807fa 57 debug_lock(true); //Force init
ashleymills 1:391ec57807fa 58 debug_lock(false);
ashleymills 1:391ec57807fa 59 }
ashleymills 1:391ec57807fa 60
ashleymills 1:391ec57807fa 61 void debug_set_newline(const char* newline)
ashleymills 1:391ec57807fa 62 {
ashleymills 1:391ec57807fa 63 debug_lock(true);
ashleymills 1:391ec57807fa 64 strncpy( debug_newline, newline, 2 );
ashleymills 1:391ec57807fa 65 debug_newline[2] = '\0';
ashleymills 1:391ec57807fa 66 debug_lock(false);
ashleymills 1:391ec57807fa 67 }
ashleymills 1:391ec57807fa 68
ashleymills 1:391ec57807fa 69 void debug_set_speed(int speed)
ashleymills 1:391ec57807fa 70 {
ashleymills 1:391ec57807fa 71 debug_pc.baud(speed);
ashleymills 1:391ec57807fa 72 }
ashleymills 1:391ec57807fa 73
ashleymills 1:391ec57807fa 74 void debug(int level, const char* module, int line, const char* fmt, ...)
ashleymills 1:391ec57807fa 75 {
ashleymills 1:391ec57807fa 76 debug_lock(true);
ashleymills 1:391ec57807fa 77 switch(level)
ashleymills 1:391ec57807fa 78 {
ashleymills 1:391ec57807fa 79 default:
ashleymills 1:391ec57807fa 80 case 1:
ashleymills 1:391ec57807fa 81 printf("E");
ashleymills 1:391ec57807fa 82 break;
ashleymills 1:391ec57807fa 83 case 2:
ashleymills 1:391ec57807fa 84 printf("W");
ashleymills 1:391ec57807fa 85 break;
ashleymills 1:391ec57807fa 86 case 3:
ashleymills 1:391ec57807fa 87 printf("I");
ashleymills 1:391ec57807fa 88 break;
ashleymills 1:391ec57807fa 89 case 4:
ashleymills 1:391ec57807fa 90 printf("D");
ashleymills 1:391ec57807fa 91 break;
ashleymills 1:391ec57807fa 92 }
ashleymills 1:391ec57807fa 93
ashleymills 1:391ec57807fa 94 printf(" %s:%d ", module, line);
ashleymills 1:391ec57807fa 95
ashleymills 1:391ec57807fa 96 va_list argp;
ashleymills 1:391ec57807fa 97
ashleymills 1:391ec57807fa 98 va_start(argp, fmt);
ashleymills 1:391ec57807fa 99 vprintf(fmt, argp);
ashleymills 1:391ec57807fa 100 va_end(argp);
ashleymills 1:391ec57807fa 101
ashleymills 1:391ec57807fa 102 printf(debug_newline);
ashleymills 1:391ec57807fa 103
ashleymills 1:391ec57807fa 104 fflush(stdout);
ashleymills 1:391ec57807fa 105
ashleymills 1:391ec57807fa 106 debug_lock(false);
ashleymills 1:391ec57807fa 107
ashleymills 1:391ec57807fa 108 }
ashleymills 1:391ec57807fa 109
ashleymills 1:391ec57807fa 110 void debug_error(const char* module, int line, int ret)
ashleymills 1:391ec57807fa 111 {
ashleymills 1:391ec57807fa 112 debug_lock(true);
ashleymills 1:391ec57807fa 113 printf("[RC] Module %s - Line %d : Error %d\n", module, line, ret);
ashleymills 1:391ec57807fa 114 fflush(stdout);
ashleymills 1:391ec57807fa 115 debug_lock(false);
ashleymills 1:391ec57807fa 116 }
ashleymills 1:391ec57807fa 117
ashleymills 1:391ec57807fa 118 void debug_exact(const char* fmt, ...)
ashleymills 1:391ec57807fa 119 {
ashleymills 1:391ec57807fa 120 debug_lock(true);
ashleymills 1:391ec57807fa 121 va_list argp;
ashleymills 1:391ec57807fa 122
ashleymills 1:391ec57807fa 123 va_start(argp, fmt);
ashleymills 1:391ec57807fa 124 vprintf(fmt, argp);
ashleymills 1:391ec57807fa 125 va_end(argp);
ashleymills 1:391ec57807fa 126 debug_lock(false);
ashleymills 1:391ec57807fa 127 }