Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Committer:
xinlei
Date:
Wed May 13 13:03:20 2015 +0000
Revision:
107:f1ee3e1eb126
Parent:
106:fc5f25f0e0d5
Child:
116:5de54f09f754
ConfigSync is now singleton

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 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 95:5dfdc8568e9f 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 }