Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
operation/PollThread.cpp@98:e369fc75c000, 2015-05-07 (annotated)
- Committer:
- xinlei
- Date:
- Thu May 07 09:57:55 2015 +0000
- Revision:
- 98:e369fc75c000
- Parent:
- 95:5dfdc8568e9f
- Child:
- 100:dbcd3bc51758
prepare for v2.1rc3.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xinlei | 95:5dfdc8568e9f | 1 | #include <stdio.h> |
xinlei | 95:5dfdc8568e9f | 2 | #include <ctype.h> |
xinlei | 95:5dfdc8568e9f | 3 | #include "PollThread.h" |
xinlei | 95:5dfdc8568e9f | 4 | #include "lex.h" |
xinlei | 95:5dfdc8568e9f | 5 | #include "logging.h" |
xinlei | 95:5dfdc8568e9f | 6 | |
xinlei | 95:5dfdc8568e9f | 7 | bool PollThread::handshake() |
xinlei | 95:5dfdc8568e9f | 8 | { |
xinlei | 95:5dfdc8568e9f | 9 | int l = snprintf(buf2, SMARRESTBODY_SIZE, "%s", "80\r\n"); |
xinlei | 98:e369fc75c000 | 10 | l = snprintf(buf, SMARTREST_SIZE, fmtSmartRest, uri, l, buf2); |
xinlei | 98:e369fc75c000 | 11 | sock.set_blocking(false); |
xinlei | 95:5dfdc8568e9f | 12 | l = sock.sendAndReceive(buf, l, SMARTREST_SIZE); |
xinlei | 95:5dfdc8568e9f | 13 | if (l < 0) |
xinlei | 95:5dfdc8568e9f | 14 | return false; |
xinlei | 95:5dfdc8568e9f | 15 | size_t i = 0; |
xinlei | 95:5dfdc8568e9f | 16 | for (const char* p = skipHTTPHeader(buf); isalnum(*p); ++p, ++i) { |
xinlei | 95:5dfdc8568e9f | 17 | bayeuxId[i] = *p; |
xinlei | 95:5dfdc8568e9f | 18 | } |
xinlei | 95:5dfdc8568e9f | 19 | bayeuxId[i] = '\0'; |
xinlei | 95:5dfdc8568e9f | 20 | return bayeuxId[0]; |
xinlei | 95:5dfdc8568e9f | 21 | } |
xinlei | 95:5dfdc8568e9f | 22 | |
xinlei | 95:5dfdc8568e9f | 23 | bool PollThread::subscribe() |
xinlei | 95:5dfdc8568e9f | 24 | { |
xinlei | 95:5dfdc8568e9f | 25 | int l = snprintf(buf2, SMARRESTBODY_SIZE, "81,%s,/%s\r\n", bayeuxId, chn); |
xinlei | 98:e369fc75c000 | 26 | l = snprintf(buf, SMARTREST_SIZE, fmtSmartRest, uri, l, buf2); |
xinlei | 98:e369fc75c000 | 27 | sock.set_blocking(false); |
xinlei | 98:e369fc75c000 | 28 | l = sock.sendOnly(buf, l, SMARTREST_SIZE); |
xinlei | 95:5dfdc8568e9f | 29 | return l>=0; |
xinlei | 95:5dfdc8568e9f | 30 | } |
xinlei | 95:5dfdc8568e9f | 31 | |
xinlei | 95:5dfdc8568e9f | 32 | bool PollThread::connect() |
xinlei | 95:5dfdc8568e9f | 33 | { |
xinlei | 95:5dfdc8568e9f | 34 | int l = snprintf(buf2, SMARRESTBODY_SIZE, "83,%s\r\n", bayeuxId); |
xinlei | 98:e369fc75c000 | 35 | l = snprintf(buf, SMARTREST_SIZE, fmtSmartRest, uri, l, buf2); |
xinlei | 98:e369fc75c000 | 36 | sock.set_blocking(true); |
xinlei | 95:5dfdc8568e9f | 37 | l = sock.sendAndReceive(buf, l, SMARTREST_SIZE); |
xinlei | 95:5dfdc8568e9f | 38 | return l>=0; |
xinlei | 95:5dfdc8568e9f | 39 | } |
xinlei | 95:5dfdc8568e9f | 40 | |
xinlei | 95:5dfdc8568e9f | 41 | void PollThread::threadFunc() |
xinlei | 95:5dfdc8568e9f | 42 | { |
xinlei | 98:e369fc75c000 | 43 | unsigned short state = 1; |
xinlei | 95:5dfdc8568e9f | 44 | aInfo("Poll thread: %p\n", Thread::gettid()); |
xinlei | 95:5dfdc8568e9f | 45 | while (true) { |
xinlei | 95:5dfdc8568e9f | 46 | switch (state) { |
xinlei | 95:5dfdc8568e9f | 47 | case 1: if (!handshake()) { |
xinlei | 95:5dfdc8568e9f | 48 | aCritical("Poll: handshake fail!\n"); |
xinlei | 95:5dfdc8568e9f | 49 | break; |
xinlei | 95:5dfdc8568e9f | 50 | } |
xinlei | 95:5dfdc8568e9f | 51 | case 2: if(!subscribe()) { |
xinlei | 95:5dfdc8568e9f | 52 | aCritical("Poll: subscribe fail!\n"); |
xinlei | 95:5dfdc8568e9f | 53 | state = 1; |
xinlei | 95:5dfdc8568e9f | 54 | break; |
xinlei | 95:5dfdc8568e9f | 55 | } |
xinlei | 95:5dfdc8568e9f | 56 | case 3: if(!connect()) { |
xinlei | 95:5dfdc8568e9f | 57 | aCritical("Poll: connect fail!\n"); |
xinlei | 95:5dfdc8568e9f | 58 | state = 1; |
xinlei | 95:5dfdc8568e9f | 59 | break; |
xinlei | 95:5dfdc8568e9f | 60 | } |
xinlei | 98:e369fc75c000 | 61 | default: parser.parse(buf); |
xinlei | 95:5dfdc8568e9f | 62 | if (parser.getBayeuxAdvice() == BA_HANDSHAKE) |
xinlei | 95:5dfdc8568e9f | 63 | state = 1; |
xinlei | 95:5dfdc8568e9f | 64 | else |
xinlei | 95:5dfdc8568e9f | 65 | state = 3; |
xinlei | 95:5dfdc8568e9f | 66 | } |
xinlei | 95:5dfdc8568e9f | 67 | } |
xinlei | 95:5dfdc8568e9f | 68 | } |