Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Committer:
xinlei
Date:
Mon Jun 01 11:24:17 2015 +0000
Revision:
124:979ab0d075de
Parent:
123:311fa85af2b3
Child:
125:e1fcf720c63a
Poti: sample interval extended to 10 ms.; Polling: pointer null checking in handshake

Who changed what in which revision?

UserRevisionLine numberNew 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 124:979ab0d075de 12 if (l <= 0) return false;
xinlei 124:979ab0d075de 13 const char* p = skipHTTPHeader(buf);
xinlei 124:979ab0d075de 14 if (p == NULL) return false;
xinlei 95:5dfdc8568e9f 15 size_t i = 0;
xinlei 124:979ab0d075de 16 for (; isalnum(*p); ++p, ++i) {
xinlei 95:5dfdc8568e9f 17 bayeuxId[i] = *p;
xinlei 95:5dfdc8568e9f 18 }
xinlei 116:5de54f09f754 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 105:c61f0d62b625 25 int l = snprintf(buf2, sizeof(buf2), "81,%s,%s\r\n", bayeuxId, chn);
xinlei 105:c61f0d62b625 26 l = snprintf(buf, sizeof(buf), fmtSmartRest, uri, l, buf2);
xinlei 105:c61f0d62b625 27 sock.setBlocking(3000);
xinlei 122:3e4a1ed4bad5 28 l = sock.sendAndReceive(buf, l, sizeof(buf));
xinlei 122:3e4a1ed4bad5 29 if (l <= 0) return false;
xinlei 122:3e4a1ed4bad5 30 const char *p = skipHTTPHeader(buf);
xinlei 122:3e4a1ed4bad5 31 if (p == NULL) return false;
xinlei 122:3e4a1ed4bad5 32 for (; *p && !isgraph(*p); ++p);
xinlei 122:3e4a1ed4bad5 33 return *p == 0;
xinlei 95:5dfdc8568e9f 34 }
xinlei 95:5dfdc8568e9f 35
xinlei 95:5dfdc8568e9f 36 bool PollThread::connect()
xinlei 95:5dfdc8568e9f 37 {
xinlei 105:c61f0d62b625 38 int l = snprintf(buf2, sizeof(buf2), "83,%s\r\n", bayeuxId);
xinlei 105:c61f0d62b625 39 l = snprintf(buf, sizeof(buf), fmtSmartRest, uri, l, buf2);
xinlei 123:311fa85af2b3 40 sock.setBlocking(300000); // Timeout after 5 minutes
xinlei 105:c61f0d62b625 41 l = sock.sendAndReceive(buf, l, sizeof(buf));
xinlei 122:3e4a1ed4bad5 42 return l>0;
xinlei 95:5dfdc8568e9f 43 }
xinlei 95:5dfdc8568e9f 44
xinlei 95:5dfdc8568e9f 45 void PollThread::threadFunc()
xinlei 95:5dfdc8568e9f 46 {
xinlei 98:e369fc75c000 47 unsigned short state = 1;
xinlei 95:5dfdc8568e9f 48 while (true) {
xinlei 95:5dfdc8568e9f 49 switch (state) {
xinlei 95:5dfdc8568e9f 50 case 1: if (!handshake()) {
xinlei 95:5dfdc8568e9f 51 aCritical("Poll: handshake fail!\n");
xinlei 95:5dfdc8568e9f 52 break;
xinlei 95:5dfdc8568e9f 53 }
xinlei 95:5dfdc8568e9f 54 case 2: if(!subscribe()) {
xinlei 95:5dfdc8568e9f 55 aCritical("Poll: subscribe fail!\n");
xinlei 95:5dfdc8568e9f 56 state = 1;
xinlei 95:5dfdc8568e9f 57 break;
xinlei 95:5dfdc8568e9f 58 }
xinlei 95:5dfdc8568e9f 59 case 3: if(!connect()) {
xinlei 95:5dfdc8568e9f 60 aCritical("Poll: connect fail!\n");
xinlei 95:5dfdc8568e9f 61 state = 1;
xinlei 95:5dfdc8568e9f 62 break;
xinlei 95:5dfdc8568e9f 63 }
xinlei 98:e369fc75c000 64 default: parser.parse(buf);
xinlei 95:5dfdc8568e9f 65 if (parser.getBayeuxAdvice() == BA_HANDSHAKE)
xinlei 95:5dfdc8568e9f 66 state = 1;
xinlei 95:5dfdc8568e9f 67 else
xinlei 95:5dfdc8568e9f 68 state = 3;
xinlei 95:5dfdc8568e9f 69 }
xinlei 95:5dfdc8568e9f 70 }
xinlei 95:5dfdc8568e9f 71 }