Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
OperationSupport.cpp
00001 #include <string.h> 00002 #include "OperationSupport.h" 00003 //#include "ComposedRecord.h" 00004 //#include "ParsedRecord.h" 00005 //#include "IntegerValue.h" 00006 #include "SmartRestSocket.h" 00007 #include "ControlParser.h" 00008 #include "logging.h" 00009 00010 bool OperationSupport::init() 00011 { 00012 if (_init) 00013 return false; 00014 00015 // Get pending operations 00016 // USAGE: 110,<DEVICE/ID> 00017 if (!_tpl.add("10,110,GET,/devicecontrol/operations?status=PENDING&deviceId=%%&pageSize=100,,application/vnd.com.nsn.cumulocity.operationCollection+json,%%,UNSIGNED,\r\n")) 00018 return false; 00019 00020 // Set operation state 00021 // USAGE: 111,<OPERATION/ID>,<STATE> 00022 if (!_tpl.add("10,111,PUT,/devicecontrol/operations/%%,application/vnd.com.nsn.cumulocity.operation+json,application/vnd.com.nsn.cumulocity.operation+json,%%,UNSIGNED STRING,\"{\"\"status\"\":\"\"%%\"\"}\"\r\n")) 00023 return false; 00024 00025 // Get operations 00026 // Response: 210,<OPERATION/ID>,<STATUS> 00027 if (!_tpl.add("11,210,\"$.operations\",,\"$.id\",\"$.status\"\r\n")) 00028 return false; 00029 00030 // Get operation 00031 // Response: 211,<OPERATION/ID>,<STATUS> 00032 if (!_tpl.add("11,211,,\"$.deviceId\",\"$.id\",\"$.status\"\r\n")) 00033 return false; 00034 00035 // Get operation by id 00036 // USAGE: 112,<OPERATION/ID> 00037 if (!_tpl.add("10,112,GET,/devicecontrol/operations/%%,,application/vnd.com.nsn.cumulocity.operation+json,%%,UNSIGNED,\r\n")) 00038 return false; 00039 00040 // Relay operation response 00041 // Response: 220,<OPERATION/ID>,<STATUS> 00042 if (!_tpl.add("11,220,,\"$.c8y_Relay\",\"$.id\",\"$.c8y_Relay.relayState\"\r\n")) 00043 return false; 00044 00045 // Message operation response 00046 // Response: 221,<OPERATION/ID>,<MESSAGE> 00047 if (!_tpl.add("11,221,,\"$.c8y_Message\",\"$.id\",\"$.c8y_Message.text\"\r\n")) 00048 return false; 00049 00050 // Configuration operation response 00051 // Response: 222,<OPERATION/ID>,<CONFIGURATION/STRING> 00052 if (!_tpl.add("11,222,,\"$.c8y_Configuration\",\"$.id\",\"$.c8y_Configuration.config\"\r\n")) 00053 return false; 00054 00055 _init = true; 00056 return true; 00057 } 00058 00059 bool OperationSupport::executePendingOperations() 00060 { 00061 char buf[SMARTREST_SIZE]; 00062 char buf2[SMARTREST_BODY_SIZE]; 00063 SmartRestSocket sock; 00064 int l = snprintf(buf2, sizeof(buf2), "110,%ld\r\n", deviceID); 00065 l = snprintf(buf, sizeof(buf), fmtSmartRest, "/s", l, buf2); 00066 l = sock.sendAndReceive(buf, l, sizeof(buf)); 00067 if (l < 0) { 00068 aError("Retrieve pending operations\n"); 00069 return false; 00070 } 00071 const size_t N = 10; 00072 long opl[N]; 00073 size_t i = 0; 00074 for (const char* p = skipHTTPHeader(buf); i<N && *p;) { 00075 long id = 0; 00076 int c = 0; 00077 int n = sscanf(p, "210,1,%ld,PENDING\r\n%n", &id, &c); 00078 if (n == 1) { 00079 opl[i++] = id; 00080 p += c; 00081 } else 00082 break; 00083 } 00084 if (i == 0) 00085 return true; 00086 else if (i >= N) 00087 aWarning("Over %u pending operations.\n", N); 00088 ControlParser cp(opool); 00089 bool flag = true; 00090 l = 0; 00091 for (size_t j = 0; j < i; ++j) { 00092 l += snprintf(buf2+l, sizeof(buf2)-l, "112,%ld\r\n", opl[j]); 00093 } 00094 l = snprintf(buf, sizeof(buf), fmtSmartRest, "/s", l, buf2); 00095 l = sock.sendAndReceive(buf, l, sizeof(buf)); 00096 if (l >= 0) 00097 cp.parse(buf); 00098 else 00099 flag = false; 00100 return flag; 00101 }
Generated on Sat Jul 16 2022 07:10:59 by
1.7.2
