Example of HTTPServer with additional features: * SNTPClient, DST rules * Link status indication * Local or SDCard-based WebServer * RPC-able class * Static and Dynamic HTML page
myrpc.cpp@2:360fda42fefd, 2010-01-12 (annotated)
- Committer:
- iva2k
- Date:
- Tue Jan 12 07:41:55 2010 +0000
- Revision:
- 2:360fda42fefd
- Parent:
- 0:886e4b3119ad
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
iva2k | 0:886e4b3119ad | 1 | // myrpc.cpp |
iva2k | 0:886e4b3119ad | 2 | // |
iva2k | 0:886e4b3119ad | 3 | // Based on: |
iva2k | 0:886e4b3119ad | 4 | // http://mbed.org/projects/cookbook/svn/Servo/trunk/Servo.h |
iva2k | 0:886e4b3119ad | 5 | // http://mbed.org/projects/cookbook/svn/Servo/trunk/Servo.cpp |
iva2k | 0:886e4b3119ad | 6 | // http://mbed.org/forum/topic/234/ |
iva2k | 0:886e4b3119ad | 7 | |
iva2k | 0:886e4b3119ad | 8 | #include "myrpc.h" |
iva2k | 0:886e4b3119ad | 9 | #ifdef MBED_RPC |
iva2k | 0:886e4b3119ad | 10 | #include "rpc.h" |
iva2k | 0:886e4b3119ad | 11 | #endif |
iva2k | 0:886e4b3119ad | 12 | |
iva2k | 0:886e4b3119ad | 13 | #include "SNTPClient.h" |
iva2k | 0:886e4b3119ad | 14 | |
iva2k | 0:886e4b3119ad | 15 | extern int gDebug; |
iva2k | 0:886e4b3119ad | 16 | |
iva2k | 0:886e4b3119ad | 17 | namespace mbed { |
iva2k | 0:886e4b3119ad | 18 | |
iva2k | 0:886e4b3119ad | 19 | myrpc::myrpc(PinName pin, const char *name) : Base(name), _pin(pin) {} |
iva2k | 0:886e4b3119ad | 20 | |
iva2k | 0:886e4b3119ad | 21 | void myrpc::debug(int val) { |
iva2k | 0:886e4b3119ad | 22 | gDebug=val; |
iva2k | 0:886e4b3119ad | 23 | printf("Debug is %s\r\n", gDebug ? "ON" : "OFF"); |
iva2k | 0:886e4b3119ad | 24 | } |
iva2k | 0:886e4b3119ad | 25 | |
iva2k | 0:886e4b3119ad | 26 | void myrpc::blink(int n) { |
iva2k | 0:886e4b3119ad | 27 | for (int i=0; i<n; i++) { |
iva2k | 0:886e4b3119ad | 28 | _pin = 1; |
iva2k | 0:886e4b3119ad | 29 | wait(0.2); |
iva2k | 0:886e4b3119ad | 30 | _pin = 0; |
iva2k | 0:886e4b3119ad | 31 | wait(0.2); |
iva2k | 0:886e4b3119ad | 32 | } |
iva2k | 0:886e4b3119ad | 33 | } |
iva2k | 0:886e4b3119ad | 34 | |
iva2k | 2:360fda42fefd | 35 | char *myrpc::echo(const char * in) { |
iva2k | 2:360fda42fefd | 36 | printf("DEBUG myrpc::echo() IN=%s\r\n",in); |
iva2k | 2:360fda42fefd | 37 | sprintf(_buffer, "%s", in); |
iva2k | 2:360fda42fefd | 38 | return _buffer; |
iva2k | 2:360fda42fefd | 39 | } |
iva2k | 2:360fda42fefd | 40 | |
iva2k | 0:886e4b3119ad | 41 | void myrpc::settime(const char *t) { |
iva2k | 0:886e4b3119ad | 42 | time_t seconds = time(NULL); |
iva2k | 0:886e4b3119ad | 43 | if (seconds == (unsigned)-1 || seconds == 0) { |
iva2k | 0:886e4b3119ad | 44 | seconds = 1256729737; // Set RTC time to Wed, 28 Oct 2009 11:35:37 |
iva2k | 0:886e4b3119ad | 45 | } |
iva2k | 0:886e4b3119ad | 46 | // set_time(seconds); |
iva2k | 0:886e4b3119ad | 47 | SNTPSetTime(seconds); |
iva2k | 0:886e4b3119ad | 48 | strftime(_buffer, sizeof(_buffer), "%A %m/%d/%Y %H:%M:%S", localtime(&seconds)); |
iva2k | 0:886e4b3119ad | 49 | printf("settime(%s) seconds=%d datetime=%s\r\n", t, seconds, _buffer); |
iva2k | 0:886e4b3119ad | 50 | } |
iva2k | 0:886e4b3119ad | 51 | char *myrpc::gettime(const char *fmt="%A %m/%d/%Y %H:%M:%S") { |
iva2k | 0:886e4b3119ad | 52 | // time_t seconds = time(NULL); |
iva2k | 0:886e4b3119ad | 53 | time_t seconds = SNTPTime(); |
iva2k | 0:886e4b3119ad | 54 | //printf("Time as seconds since January 1, 1970 = %d\r\n", seconds); |
iva2k | 0:886e4b3119ad | 55 | //printf("Time as a basic string = %s\r\n", ctime(&seconds)); |
iva2k | 0:886e4b3119ad | 56 | if (!fmt || !fmt[0]) |
iva2k | 0:886e4b3119ad | 57 | fmt = "%A %m/%d/%Y %H:%M:%S"; |
iva2k | 0:886e4b3119ad | 58 | strftime(_buffer, sizeof(_buffer), fmt, localtime(&seconds)); |
iva2k | 0:886e4b3119ad | 59 | return _buffer; |
iva2k | 0:886e4b3119ad | 60 | } |
iva2k | 0:886e4b3119ad | 61 | |
iva2k | 0:886e4b3119ad | 62 | #ifdef MBED_RPC |
iva2k | 0:886e4b3119ad | 63 | const rpc_method *myrpc::get_rpc_methods() { |
iva2k | 0:886e4b3119ad | 64 | static const rpc_method rpc_methods[] = { |
iva2k | 0:886e4b3119ad | 65 | { "debug", rpc_method_caller<myrpc, int, &myrpc::debug> }, |
iva2k | 0:886e4b3119ad | 66 | { "blink", rpc_method_caller<myrpc, int, &myrpc::blink> }, |
iva2k | 2:360fda42fefd | 67 | { "echo", rpc_method_caller<char *, myrpc, const char *, &myrpc::echo> }, |
iva2k | 0:886e4b3119ad | 68 | { "settime", rpc_method_caller<myrpc, const char *, &myrpc::settime> }, |
iva2k | 0:886e4b3119ad | 69 | { "gettime", rpc_method_caller<char *, myrpc, const char *, &myrpc::gettime> }, |
iva2k | 0:886e4b3119ad | 70 | RPC_METHOD_SUPER(Base) |
iva2k | 0:886e4b3119ad | 71 | }; |
iva2k | 0:886e4b3119ad | 72 | return rpc_methods; |
iva2k | 0:886e4b3119ad | 73 | } |
iva2k | 0:886e4b3119ad | 74 | rpc_class *myrpc::get_rpc_class() { |
iva2k | 0:886e4b3119ad | 75 | static const rpc_function funcs[] = { |
iva2k | 0:886e4b3119ad | 76 | "new", rpc_function_caller<const char*, PinName, const char*, &Base::construct<myrpc,PinName,const char*> >, |
iva2k | 0:886e4b3119ad | 77 | RPC_METHOD_END |
iva2k | 0:886e4b3119ad | 78 | }; |
iva2k | 0:886e4b3119ad | 79 | static rpc_class c = { "myrpc", funcs, NULL }; |
iva2k | 0:886e4b3119ad | 80 | return &c; |
iva2k | 0:886e4b3119ad | 81 | } |
iva2k | 0:886e4b3119ad | 82 | #endif |
iva2k | 0:886e4b3119ad | 83 | |
iva2k | 0:886e4b3119ad | 84 | } // namespace mbed |