Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
operation/PollThread.cpp@116:5de54f09f754, 2015-05-20 (annotated)
- Committer:
- xinlei
- Date:
- Wed May 20 09:55:49 2015 +0000
- Revision:
- 116:5de54f09f754
- Parent:
- 107:f1ee3e1eb126
- Child:
- 122:3e4a1ed4bad5
device bootstrap revamp
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 "logging.h" |
xinlei | 95:5dfdc8568e9f | 5 | |
xinlei | 95:5dfdc8568e9f | 6 | bool PollThread::handshake() |
xinlei | 95:5dfdc8568e9f | 7 | { |
xinlei | 105:c61f0d62b625 | 8 | int l = snprintf(buf2, sizeof(buf2), "%s", "80\r\n"); |
xinlei | 105:c61f0d62b625 | 9 | l = snprintf(buf, sizeof(buf), fmtSmartRest, uri, l, buf2); |
xinlei | 105:c61f0d62b625 | 10 | sock.setBlocking(3000); |
xinlei | 105:c61f0d62b625 | 11 | l = sock.sendAndReceive(buf, l, sizeof(buf)); |
xinlei | 95:5dfdc8568e9f | 12 | if (l < 0) |
xinlei | 95:5dfdc8568e9f | 13 | return false; |
xinlei | 95:5dfdc8568e9f | 14 | size_t i = 0; |
xinlei | 95:5dfdc8568e9f | 15 | for (const char* p = skipHTTPHeader(buf); isalnum(*p); ++p, ++i) { |
xinlei | 95:5dfdc8568e9f | 16 | bayeuxId[i] = *p; |
xinlei | 95:5dfdc8568e9f | 17 | } |
xinlei | 116:5de54f09f754 | 18 | bayeuxId[i] = 0; |
xinlei | 95:5dfdc8568e9f | 19 | return bayeuxId[0]; |
xinlei | 95:5dfdc8568e9f | 20 | } |
xinlei | 95:5dfdc8568e9f | 21 | |
xinlei | 95:5dfdc8568e9f | 22 | bool PollThread::subscribe() |
xinlei | 95:5dfdc8568e9f | 23 | { |
xinlei | 105:c61f0d62b625 | 24 | int l = snprintf(buf2, sizeof(buf2), "81,%s,%s\r\n", bayeuxId, chn); |
xinlei | 105:c61f0d62b625 | 25 | l = snprintf(buf, sizeof(buf), fmtSmartRest, uri, l, buf2); |
xinlei | 105:c61f0d62b625 | 26 | sock.setBlocking(3000); |
xinlei | 100:dbcd3bc51758 | 27 | l = sock.sendOnly(buf, l); |
xinlei | 95:5dfdc8568e9f | 28 | return l>=0; |
xinlei | 95:5dfdc8568e9f | 29 | } |
xinlei | 95:5dfdc8568e9f | 30 | |
xinlei | 95:5dfdc8568e9f | 31 | bool PollThread::connect() |
xinlei | 95:5dfdc8568e9f | 32 | { |
xinlei | 105:c61f0d62b625 | 33 | int l = snprintf(buf2, sizeof(buf2), "83,%s\r\n", bayeuxId); |
xinlei | 105:c61f0d62b625 | 34 | l = snprintf(buf, sizeof(buf), fmtSmartRest, uri, l, buf2); |
xinlei | 105:c61f0d62b625 | 35 | sock.setBlocking(-1); |
xinlei | 105:c61f0d62b625 | 36 | l = sock.sendAndReceive(buf, l, sizeof(buf)); |
xinlei | 95:5dfdc8568e9f | 37 | return l>=0; |
xinlei | 95:5dfdc8568e9f | 38 | } |
xinlei | 95:5dfdc8568e9f | 39 | |
xinlei | 95:5dfdc8568e9f | 40 | void PollThread::threadFunc() |
xinlei | 95:5dfdc8568e9f | 41 | { |
xinlei | 98:e369fc75c000 | 42 | unsigned short state = 1; |
xinlei | 95:5dfdc8568e9f | 43 | while (true) { |
xinlei | 95:5dfdc8568e9f | 44 | switch (state) { |
xinlei | 95:5dfdc8568e9f | 45 | case 1: if (!handshake()) { |
xinlei | 95:5dfdc8568e9f | 46 | aCritical("Poll: handshake fail!\n"); |
xinlei | 95:5dfdc8568e9f | 47 | break; |
xinlei | 95:5dfdc8568e9f | 48 | } |
xinlei | 95:5dfdc8568e9f | 49 | case 2: if(!subscribe()) { |
xinlei | 95:5dfdc8568e9f | 50 | aCritical("Poll: subscribe fail!\n"); |
xinlei | 95:5dfdc8568e9f | 51 | state = 1; |
xinlei | 95:5dfdc8568e9f | 52 | break; |
xinlei | 95:5dfdc8568e9f | 53 | } |
xinlei | 95:5dfdc8568e9f | 54 | case 3: if(!connect()) { |
xinlei | 95:5dfdc8568e9f | 55 | aCritical("Poll: connect fail!\n"); |
xinlei | 95:5dfdc8568e9f | 56 | state = 1; |
xinlei | 95:5dfdc8568e9f | 57 | break; |
xinlei | 95:5dfdc8568e9f | 58 | } |
xinlei | 98:e369fc75c000 | 59 | default: parser.parse(buf); |
xinlei | 95:5dfdc8568e9f | 60 | if (parser.getBayeuxAdvice() == BA_HANDSHAKE) |
xinlei | 95:5dfdc8568e9f | 61 | state = 1; |
xinlei | 95:5dfdc8568e9f | 62 | else |
xinlei | 95:5dfdc8568e9f | 63 | state = 3; |
xinlei | 95:5dfdc8568e9f | 64 | } |
xinlei | 95:5dfdc8568e9f | 65 | } |
xinlei | 95:5dfdc8568e9f | 66 | } |