![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
operation/PollThread.cpp@138:f8ab852e83e7, 2016-08-08 (annotated)
- Committer:
- xinlei
- Date:
- Mon Aug 08 11:05:57 2016 +0000
- Revision:
- 138:f8ab852e83e7
- Parent:
- 134:c4009ecaf5c0
Etisalat and Teleena APN.
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 | 134:c4009ecaf5c0 | 6 | |
xinlei | 95:5dfdc8568e9f | 7 | bool PollThread::handshake() |
xinlei | 95:5dfdc8568e9f | 8 | { |
xinlei | 105:c61f0d62b625 | 9 | int l = snprintf(buf2, sizeof(buf2), "%s", "80\r\n"); |
xinlei | 105:c61f0d62b625 | 10 | l = snprintf(buf, sizeof(buf), fmtSmartRest, uri, l, buf2); |
xinlei | 105:c61f0d62b625 | 11 | sock.setBlocking(3000); |
xinlei | 105:c61f0d62b625 | 12 | l = sock.sendAndReceive(buf, l, sizeof(buf)); |
xinlei | 124:979ab0d075de | 13 | if (l <= 0) return false; |
xinlei | 124:979ab0d075de | 14 | const char* p = skipHTTPHeader(buf); |
xinlei | 124:979ab0d075de | 15 | if (p == NULL) return false; |
xinlei | 95:5dfdc8568e9f | 16 | size_t i = 0; |
xinlei | 124:979ab0d075de | 17 | for (; isalnum(*p); ++p, ++i) { |
xinlei | 95:5dfdc8568e9f | 18 | bayeuxId[i] = *p; |
xinlei | 95:5dfdc8568e9f | 19 | } |
xinlei | 116:5de54f09f754 | 20 | bayeuxId[i] = 0; |
xinlei | 95:5dfdc8568e9f | 21 | return bayeuxId[0]; |
xinlei | 95:5dfdc8568e9f | 22 | } |
xinlei | 95:5dfdc8568e9f | 23 | |
xinlei | 134:c4009ecaf5c0 | 24 | |
xinlei | 95:5dfdc8568e9f | 25 | bool PollThread::subscribe() |
xinlei | 95:5dfdc8568e9f | 26 | { |
xinlei | 105:c61f0d62b625 | 27 | int l = snprintf(buf2, sizeof(buf2), "81,%s,%s\r\n", bayeuxId, chn); |
xinlei | 105:c61f0d62b625 | 28 | l = snprintf(buf, sizeof(buf), fmtSmartRest, uri, l, buf2); |
xinlei | 105:c61f0d62b625 | 29 | sock.setBlocking(3000); |
xinlei | 122:3e4a1ed4bad5 | 30 | l = sock.sendAndReceive(buf, l, sizeof(buf)); |
xinlei | 122:3e4a1ed4bad5 | 31 | if (l <= 0) return false; |
xinlei | 122:3e4a1ed4bad5 | 32 | const char *p = skipHTTPHeader(buf); |
xinlei | 122:3e4a1ed4bad5 | 33 | if (p == NULL) return false; |
xinlei | 122:3e4a1ed4bad5 | 34 | for (; *p && !isgraph(*p); ++p); |
xinlei | 122:3e4a1ed4bad5 | 35 | return *p == 0; |
xinlei | 95:5dfdc8568e9f | 36 | } |
xinlei | 95:5dfdc8568e9f | 37 | |
xinlei | 134:c4009ecaf5c0 | 38 | |
xinlei | 95:5dfdc8568e9f | 39 | bool PollThread::connect() |
xinlei | 95:5dfdc8568e9f | 40 | { |
xinlei | 105:c61f0d62b625 | 41 | int l = snprintf(buf2, sizeof(buf2), "83,%s\r\n", bayeuxId); |
xinlei | 105:c61f0d62b625 | 42 | l = snprintf(buf, sizeof(buf), fmtSmartRest, uri, l, buf2); |
xinlei | 126:871e6eb0fa76 | 43 | sock.setBlocking(610000); // Timeout after 10m:10s |
xinlei | 129:dc9e37d4bc05 | 44 | // sock.setBlocking(-1); |
xinlei | 129:dc9e37d4bc05 | 45 | // l = sock.sendAndReceive(buf, l, sizeof(buf)); |
xinlei | 129:dc9e37d4bc05 | 46 | int c = sock.connect(); |
xinlei | 129:dc9e37d4bc05 | 47 | if (c < 0) { |
xinlei | 129:dc9e37d4bc05 | 48 | sock.close(); |
xinlei | 129:dc9e37d4bc05 | 49 | return false; |
xinlei | 129:dc9e37d4bc05 | 50 | } |
xinlei | 129:dc9e37d4bc05 | 51 | c = sock.send(buf, l); |
xinlei | 129:dc9e37d4bc05 | 52 | if (c < 0) { |
xinlei | 129:dc9e37d4bc05 | 53 | sock.close(); |
xinlei | 129:dc9e37d4bc05 | 54 | return false; |
xinlei | 129:dc9e37d4bc05 | 55 | } |
xinlei | 129:dc9e37d4bc05 | 56 | // Avoid heartbeat message overflows the buffer |
xinlei | 129:dc9e37d4bc05 | 57 | l = 0; |
xinlei | 134:c4009ecaf5c0 | 58 | for (unsigned short i = 0; i < 300; ++i) { |
xinlei | 129:dc9e37d4bc05 | 59 | int l2 = sock.receive(buf+l, sizeof(buf)-l); |
xinlei | 129:dc9e37d4bc05 | 60 | if (l2 > 0) { |
xinlei | 129:dc9e37d4bc05 | 61 | l += l2; |
xinlei | 129:dc9e37d4bc05 | 62 | } else { |
xinlei | 129:dc9e37d4bc05 | 63 | break; |
xinlei | 129:dc9e37d4bc05 | 64 | } |
xinlei | 129:dc9e37d4bc05 | 65 | } |
xinlei | 129:dc9e37d4bc05 | 66 | sock.close(); |
xinlei | 134:c4009ecaf5c0 | 67 | buf[l] = 0; |
xinlei | 122:3e4a1ed4bad5 | 68 | return l>0; |
xinlei | 95:5dfdc8568e9f | 69 | } |
xinlei | 95:5dfdc8568e9f | 70 | |
xinlei | 134:c4009ecaf5c0 | 71 | |
xinlei | 95:5dfdc8568e9f | 72 | void PollThread::threadFunc() |
xinlei | 95:5dfdc8568e9f | 73 | { |
xinlei | 98:e369fc75c000 | 74 | unsigned short state = 1; |
xinlei | 95:5dfdc8568e9f | 75 | while (true) { |
xinlei | 95:5dfdc8568e9f | 76 | switch (state) { |
xinlei | 95:5dfdc8568e9f | 77 | case 1: if (!handshake()) { |
xinlei | 95:5dfdc8568e9f | 78 | aCritical("Poll: handshake fail!\n"); |
xinlei | 95:5dfdc8568e9f | 79 | break; |
xinlei | 95:5dfdc8568e9f | 80 | } |
xinlei | 95:5dfdc8568e9f | 81 | case 2: if(!subscribe()) { |
xinlei | 95:5dfdc8568e9f | 82 | aCritical("Poll: subscribe fail!\n"); |
xinlei | 95:5dfdc8568e9f | 83 | state = 1; |
xinlei | 95:5dfdc8568e9f | 84 | break; |
xinlei | 95:5dfdc8568e9f | 85 | } |
xinlei | 95:5dfdc8568e9f | 86 | case 3: if(!connect()) { |
xinlei | 95:5dfdc8568e9f | 87 | aCritical("Poll: connect fail!\n"); |
xinlei | 95:5dfdc8568e9f | 88 | break; |
xinlei | 95:5dfdc8568e9f | 89 | } |
xinlei | 134:c4009ecaf5c0 | 90 | parser.parse(buf); |
xinlei | 95:5dfdc8568e9f | 91 | if (parser.getBayeuxAdvice() == BA_HANDSHAKE) |
xinlei | 95:5dfdc8568e9f | 92 | state = 1; |
xinlei | 95:5dfdc8568e9f | 93 | else |
xinlei | 95:5dfdc8568e9f | 94 | state = 3; |
xinlei | 95:5dfdc8568e9f | 95 | } |
xinlei | 95:5dfdc8568e9f | 96 | } |
xinlei | 95:5dfdc8568e9f | 97 | } |