123

Dependencies:   MQTTSN

Committer:
m_ahsan
Date:
Tue Aug 06 05:32:43 2019 +0000
Revision:
15:8c473836feba
123

Who changed what in which revision?

UserRevisionLine numberNew contents of line
m_ahsan 15:8c473836feba 1 #include <string>
m_ahsan 15:8c473836feba 2 void process_rsc(MbedJSONValue &MSG);
m_ahsan 15:8c473836feba 3
m_ahsan 15:8c473836feba 4 bool reg_resp = false;
m_ahsan 15:8c473836feba 5
m_ahsan 15:8c473836feba 6 const char* CSE_ID = "cse01";
m_ahsan 15:8c473836feba 7 char* AE_ID;
m_ahsan 15:8c473836feba 8 bool lcl;
m_ahsan 15:8c473836feba 9 ///////////////////////////////////////
m_ahsan 15:8c473836feba 10 class Request{
m_ahsan 15:8c473836feba 11 public:
m_ahsan 15:8c473836feba 12 int Operation;
m_ahsan 15:8c473836feba 13 string To;
m_ahsan 15:8c473836feba 14 string From;
m_ahsan 15:8c473836feba 15 string Request_Identifier;
m_ahsan 15:8c473836feba 16 int Resource_Type;
m_ahsan 15:8c473836feba 17 };
m_ahsan 15:8c473836feba 18
m_ahsan 15:8c473836feba 19 ///////////////////////////////////////
m_ahsan 15:8c473836feba 20
m_ahsan 15:8c473836feba 21 struct CreateAE
m_ahsan 15:8c473836feba 22 {
m_ahsan 15:8c473836feba 23 string resourceName;
m_ahsan 15:8c473836feba 24 string App_ID;
m_ahsan 15:8c473836feba 25 string pointOfAccess;
m_ahsan 15:8c473836feba 26 bool requestReachability;
m_ahsan 15:8c473836feba 27 }CAE;
m_ahsan 15:8c473836feba 28
m_ahsan 15:8c473836feba 29 struct CreateCIN
m_ahsan 15:8c473836feba 30 {
m_ahsan 15:8c473836feba 31 string resourceName; //rn
m_ahsan 15:8c473836feba 32 string contentInfo; //cnf
m_ahsan 15:8c473836feba 33 string content; //con
m_ahsan 15:8c473836feba 34 }CIN;
m_ahsan 15:8c473836feba 35
m_ahsan 15:8c473836feba 36 struct CreateSub
m_ahsan 15:8c473836feba 37 {
m_ahsan 15:8c473836feba 38 string resourceName; //rn
m_ahsan 15:8c473836feba 39 string notificationURI; //nu
m_ahsan 15:8c473836feba 40 int notificationContentType; //nct
m_ahsan 15:8c473836feba 41 int notificationEventType; //net
m_ahsan 15:8c473836feba 42 }CSub;
m_ahsan 15:8c473836feba 43 /////
m_ahsan 15:8c473836feba 44 const char* Create_Req(Request R1)
m_ahsan 15:8c473836feba 45 {
m_ahsan 15:8c473836feba 46 MbedJSONValue demo, demo2, demo3, demo4;
m_ahsan 15:8c473836feba 47
m_ahsan 15:8c473836feba 48 std::string s;
m_ahsan 15:8c473836feba 49 switch(R1.Resource_Type){
m_ahsan 15:8c473836feba 50 case 0:
m_ahsan 15:8c473836feba 51 break;
m_ahsan 15:8c473836feba 52 case 1:
m_ahsan 15:8c473836feba 53 break;
m_ahsan 15:8c473836feba 54 case 2:
m_ahsan 15:8c473836feba 55 demo3["api"] = CAE.App_ID;
m_ahsan 15:8c473836feba 56 demo3["rn"] = CAE.resourceName;
m_ahsan 15:8c473836feba 57 demo3["rr"] = CAE.requestReachability;
m_ahsan 15:8c473836feba 58 demo2["m2m:ae"] = demo3;
m_ahsan 15:8c473836feba 59 break;
m_ahsan 15:8c473836feba 60 case 3:
m_ahsan 15:8c473836feba 61 demo3["rn"] = CAE.resourceName;
m_ahsan 15:8c473836feba 62 demo2["m2m:cnt"] = demo3;
m_ahsan 15:8c473836feba 63 break;
m_ahsan 15:8c473836feba 64 case 4:
m_ahsan 15:8c473836feba 65 demo3["rn"] = CIN.resourceName;
m_ahsan 15:8c473836feba 66 demo3["cnf"] = CIN.contentInfo;
m_ahsan 15:8c473836feba 67 demo3["con"] = CIN.content;
m_ahsan 15:8c473836feba 68 demo2["m2m:cin"] = demo3;
m_ahsan 15:8c473836feba 69 break;
m_ahsan 15:8c473836feba 70 case 23:
m_ahsan 15:8c473836feba 71 demo4["net"] = CSub.notificationEventType;
m_ahsan 15:8c473836feba 72 demo3["enc"] = demo4; //enc = eventNotificationCriteria
m_ahsan 15:8c473836feba 73 demo3["rn"] = CSub.resourceName;
m_ahsan 15:8c473836feba 74 demo3["nu"] = CSub.notificationURI;
m_ahsan 15:8c473836feba 75 demo3["nct"] = CSub.notificationContentType;
m_ahsan 15:8c473836feba 76 demo2["m2m:sub"] = demo3;
m_ahsan 15:8c473836feba 77 break;
m_ahsan 15:8c473836feba 78 }
m_ahsan 15:8c473836feba 79 demo["fr"] = R1.From;
m_ahsan 15:8c473836feba 80 demo["op"] = R1.Operation;
m_ahsan 15:8c473836feba 81 demo["pc"] = demo2;
m_ahsan 15:8c473836feba 82 demo["rqi"] = R1.Request_Identifier;
m_ahsan 15:8c473836feba 83 demo["to"] = R1.To;
m_ahsan 15:8c473836feba 84 demo["ty"] = R1.Resource_Type;
m_ahsan 15:8c473836feba 85 demo["lcl"] = lcl;
m_ahsan 15:8c473836feba 86
m_ahsan 15:8c473836feba 87 //serialize it into a JSON string
m_ahsan 15:8c473836feba 88 s = demo.serialize();
m_ahsan 15:8c473836feba 89 printf("\nMSG SIZE: %d\n", s.length());
m_ahsan 15:8c473836feba 90 //printf("json: %s\r\n", s.c_str());
m_ahsan 15:8c473836feba 91 /*const char* temp = s.c_str();
m_ahsan 15:8c473836feba 92 char* temp1;
m_ahsan 15:8c473836feba 93 strncpy(temp1, temp, strlen(temp));
m_ahsan 15:8c473836feba 94 printf("string in temp1 is %s\n",temp1);
m_ahsan 15:8c473836feba 95 return temp1;
m_ahsan 15:8c473836feba 96 */
m_ahsan 15:8c473836feba 97 return s.c_str();
m_ahsan 15:8c473836feba 98 }
m_ahsan 15:8c473836feba 99 /////
m_ahsan 15:8c473836feba 100
m_ahsan 15:8c473836feba 101 void process_msg()
m_ahsan 15:8c473836feba 102 {
m_ahsan 15:8c473836feba 103 MbedJSONValue MSG;
m_ahsan 15:8c473836feba 104 //printf("Before Parsing\n");
m_ahsan 15:8c473836feba 105 //printf("messageBuffer %s \n", messageBuffer);
m_ahsan 15:8c473836feba 106 //MbedJSONValue demo;
m_ahsan 15:8c473836feba 107 //const char * json = "{\"my_array\": [\"demo_string\", 10], \"my_boolean\": true}";
m_ahsan 15:8c473836feba 108
m_ahsan 15:8c473836feba 109 //parse the previous string and fill the object demo
m_ahsan 15:8c473836feba 110 //parse(demo, json);
m_ahsan 15:8c473836feba 111 //printf("After Parsing json\n");
m_ahsan 15:8c473836feba 112 parse(MSG, messageBuffer);
m_ahsan 15:8c473836feba 113 //bool temp; //Check fot parameters presence
m_ahsan 15:8c473836feba 114 //printf("After Parsing\n");
m_ahsan 15:8c473836feba 115 ////////////////////////Local Parameter (Additional parameter)//////////////////////
m_ahsan 15:8c473836feba 116 if(MSG.hasMember("lcl"))
m_ahsan 15:8c473836feba 117 {
m_ahsan 15:8c473836feba 118 local = MSG["lcl"].get<bool>();
m_ahsan 15:8c473836feba 119 printf("Local: %s\r\n", local? "true" : "false");
m_ahsan 15:8c473836feba 120 }
m_ahsan 15:8c473836feba 121
m_ahsan 15:8c473836feba 122 ////////////////////////resource Type Parameter (Mendatory parameter)//////////////////////
m_ahsan 15:8c473836feba 123 if(MSG.hasMember("rsc"))
m_ahsan 15:8c473836feba 124 {
m_ahsan 15:8c473836feba 125 rsc = MSG["rsc"].get<int>();
m_ahsan 15:8c473836feba 126 printf("Response Status Code: %d\r\n", rsc);
m_ahsan 15:8c473836feba 127 Response_Type();
m_ahsan 15:8c473836feba 128 printf("Response Status: %s\r\n", response.c_str());
m_ahsan 15:8c473836feba 129 process_rsc(MSG);
m_ahsan 15:8c473836feba 130 return;
m_ahsan 15:8c473836feba 131 }
m_ahsan 15:8c473836feba 132
m_ahsan 15:8c473836feba 133 ////////////////////////resource Type Parameter (Mendatory parameter)//////////////////////
m_ahsan 15:8c473836feba 134 if(MSG.hasMember("ty"))
m_ahsan 15:8c473836feba 135 {
m_ahsan 15:8c473836feba 136 ty = MSG["ty"].get<int>();
m_ahsan 15:8c473836feba 137 printf("ResourceType: %d\r\n", ty);
m_ahsan 15:8c473836feba 138 resource_type();
m_ahsan 15:8c473836feba 139 printf("ResourceType: %s\r\n", resourceType.c_str());
m_ahsan 15:8c473836feba 140 }
m_ahsan 15:8c473836feba 141 else {
m_ahsan 15:8c473836feba 142 //add response code for no mendatory parameter
m_ahsan 15:8c473836feba 143 return; }
m_ahsan 15:8c473836feba 144
m_ahsan 15:8c473836feba 145 ////////////////////////From parameter (Mendatory parameter[optional for Create AE])//////////
m_ahsan 15:8c473836feba 146 if(MSG.hasMember("fr"))
m_ahsan 15:8c473836feba 147 {
m_ahsan 15:8c473836feba 148 From = MSG["fr"].get<std::string>();
m_ahsan 15:8c473836feba 149 printf("From: %s\r\n", From.c_str());
m_ahsan 15:8c473836feba 150 }
m_ahsan 15:8c473836feba 151 else {
m_ahsan 15:8c473836feba 152 //add Response Status Code for no mendatory parameter BAD_REQUEST
m_ahsan 15:8c473836feba 153 return; }
m_ahsan 15:8c473836feba 154
m_ahsan 15:8c473836feba 155 ////////////////////////Operation Parameter (Mendatory parameter)//////////////////////
m_ahsan 15:8c473836feba 156 if(MSG.hasMember("op"))
m_ahsan 15:8c473836feba 157 {
m_ahsan 15:8c473836feba 158 op = MSG["op"].get<int>();
m_ahsan 15:8c473836feba 159 printf("Operation: %d\r\n", op);
m_ahsan 15:8c473836feba 160 Operation_Type();
m_ahsan 15:8c473836feba 161 printf("Operation: %s\r\n", operation.c_str());
m_ahsan 15:8c473836feba 162 }
m_ahsan 15:8c473836feba 163 else {
m_ahsan 15:8c473836feba 164 //add response code for no mendatory parameter
m_ahsan 15:8c473836feba 165 return; }
m_ahsan 15:8c473836feba 166
m_ahsan 15:8c473836feba 167 ////////////////////////To Parameter (Mendatory parameter)//////////////////////
m_ahsan 15:8c473836feba 168 if(MSG.hasMember("to"))
m_ahsan 15:8c473836feba 169 {
m_ahsan 15:8c473836feba 170 to = MSG["to"].get<std::string>();
m_ahsan 15:8c473836feba 171 printf("To: %s\r\n", to.c_str());
m_ahsan 15:8c473836feba 172 }
m_ahsan 15:8c473836feba 173 else {
m_ahsan 15:8c473836feba 174 //add response code for no mendatory parameter
m_ahsan 15:8c473836feba 175 return; }
m_ahsan 15:8c473836feba 176
m_ahsan 15:8c473836feba 177 ////////////////////////Request Identifier (Mendatory parameter)//////////////////////
m_ahsan 15:8c473836feba 178 if(MSG.hasMember("rqi"))
m_ahsan 15:8c473836feba 179 {
m_ahsan 15:8c473836feba 180 rqi = MSG["rqi"].get<std::string>();
m_ahsan 15:8c473836feba 181 printf("Request Identifier: %s\r\n", rqi.c_str());
m_ahsan 15:8c473836feba 182 }
m_ahsan 15:8c473836feba 183 else {
m_ahsan 15:8c473836feba 184 //add response code for no mendatory parameter
m_ahsan 15:8c473836feba 185 return; }
m_ahsan 15:8c473836feba 186
m_ahsan 15:8c473836feba 187 ////////////////////////Response Status Code
m_ahsan 15:8c473836feba 188 if(MSG.hasMember("rsc"))
m_ahsan 15:8c473836feba 189 {
m_ahsan 15:8c473836feba 190 rsc = MSG["rsc"].get<int>();
m_ahsan 15:8c473836feba 191 printf("Response Status Code: %d\r\n", rsc);
m_ahsan 15:8c473836feba 192 Response_Type();
m_ahsan 15:8c473836feba 193 printf("Response Status: %s\r\n", response.c_str());
m_ahsan 15:8c473836feba 194 }
m_ahsan 15:8c473836feba 195 MbedJSONValue &MSG_1 = MSG["pc"][content]; //content value depends on the resource type e.g. m2m:ae for resource type 2
m_ahsan 15:8c473836feba 196
m_ahsan 15:8c473836feba 197 //////////////////////// Resource specific attributes CSE-ID
m_ahsan 15:8c473836feba 198 if(MSG_1.hasMember("csi"))
m_ahsan 15:8c473836feba 199 {
m_ahsan 15:8c473836feba 200 csi = MSG_1["csi"].get<std::string>();
m_ahsan 15:8c473836feba 201 printf("CSI: %s\r\n", csi.c_str());
m_ahsan 15:8c473836feba 202 }
m_ahsan 15:8c473836feba 203
m_ahsan 15:8c473836feba 204 //////////////////////// Resource specific attributes Point of Access
m_ahsan 15:8c473836feba 205 if(MSG_1.hasMember("poa"))
m_ahsan 15:8c473836feba 206 {
m_ahsan 15:8c473836feba 207 poa = MSG_1["poa"].get<std::string>();
m_ahsan 15:8c473836feba 208 printf("POA: %s\r\n", poa.c_str());
m_ahsan 15:8c473836feba 209 }
m_ahsan 15:8c473836feba 210
m_ahsan 15:8c473836feba 211 /////////////////////// Resource specific attributes CSE Type
m_ahsan 15:8c473836feba 212 if(MSG_1.hasMember("cst"))
m_ahsan 15:8c473836feba 213 {
m_ahsan 15:8c473836feba 214 cst = MSG_1["cst"].get<int>();
m_ahsan 15:8c473836feba 215 printf("CSE Type: %d\r\n", cst);
m_ahsan 15:8c473836feba 216 CSE_Type();
m_ahsan 15:8c473836feba 217 printf("CSE Type: %s\r\n", cseType.c_str());
m_ahsan 15:8c473836feba 218 }
m_ahsan 15:8c473836feba 219
m_ahsan 15:8c473836feba 220 /////////////////////// Resource specific attributes RequestReachability
m_ahsan 15:8c473836feba 221 if(MSG_1.hasMember("rr"))
m_ahsan 15:8c473836feba 222 {
m_ahsan 15:8c473836feba 223 RequestReachability = MSG_1["rr"].get<bool>();
m_ahsan 15:8c473836feba 224 printf("RR: %s\r\n", RequestReachability? "true" : "false");
m_ahsan 15:8c473836feba 225 }
m_ahsan 15:8c473836feba 226
m_ahsan 15:8c473836feba 227 /////////////////////// Resource specific attributes App-ID
m_ahsan 15:8c473836feba 228 if(MSG_1.hasMember("api"))
m_ahsan 15:8c473836feba 229 {
m_ahsan 15:8c473836feba 230 api = MSG_1["api"].get<std::string>();
m_ahsan 15:8c473836feba 231 printf("App-ID: %s\r\n", api.c_str());
m_ahsan 15:8c473836feba 232 }
m_ahsan 15:8c473836feba 233 messageBuffer[0] = '\0';
m_ahsan 15:8c473836feba 234 // msg[0] = '\0';
m_ahsan 15:8c473836feba 235 //MSG.~MbedJSONValue();
m_ahsan 15:8c473836feba 236 //MSG_1.~MbedJSONValue();*/
m_ahsan 15:8c473836feba 237 }
m_ahsan 15:8c473836feba 238
m_ahsan 15:8c473836feba 239 void process_rsc(MbedJSONValue &MSG)
m_ahsan 15:8c473836feba 240 {
m_ahsan 15:8c473836feba 241 if(MSG.hasMember("rqi"))
m_ahsan 15:8c473836feba 242 {
m_ahsan 15:8c473836feba 243 rqi = MSG["rqi"].get<std::string>();
m_ahsan 15:8c473836feba 244 printf("Request Identifier: %s\r\n", rqi.c_str());
m_ahsan 15:8c473836feba 245 }
m_ahsan 15:8c473836feba 246 else {
m_ahsan 15:8c473836feba 247 //add response code for no mendatory parameter
m_ahsan 15:8c473836feba 248 return; }
m_ahsan 15:8c473836feba 249 if(MSG.hasMember("to"))
m_ahsan 15:8c473836feba 250 {
m_ahsan 15:8c473836feba 251 to = MSG["to"].get<std::string>();
m_ahsan 15:8c473836feba 252 printf("To: %s\r\n", to.c_str());
m_ahsan 15:8c473836feba 253 }
m_ahsan 15:8c473836feba 254 else {
m_ahsan 15:8c473836feba 255 //add response code for no mendatory parameter
m_ahsan 15:8c473836feba 256 return; }
m_ahsan 15:8c473836feba 257 if(MSG.hasMember("fr"))
m_ahsan 15:8c473836feba 258 {
m_ahsan 15:8c473836feba 259 From = MSG["fr"].get<std::string>();
m_ahsan 15:8c473836feba 260 printf("From: %s\r\n", From.c_str());
m_ahsan 15:8c473836feba 261 }
m_ahsan 15:8c473836feba 262 else {
m_ahsan 15:8c473836feba 263 //add response code for no mendatory parameter
m_ahsan 15:8c473836feba 264 return; }
m_ahsan 15:8c473836feba 265
m_ahsan 15:8c473836feba 266 MbedJSONValue &MSG_1 = MSG["pc"][content];
m_ahsan 15:8c473836feba 267
m_ahsan 15:8c473836feba 268 if(MSG_1.hasMember("ty"))
m_ahsan 15:8c473836feba 269 {
m_ahsan 15:8c473836feba 270 ty = MSG_1["ty"].get<int>();
m_ahsan 15:8c473836feba 271 printf("ResourceType: %d\r\n", ty);
m_ahsan 15:8c473836feba 272 resource_type();
m_ahsan 15:8c473836feba 273 printf("ResourceType: %s\r\n", resourceType.c_str());
m_ahsan 15:8c473836feba 274 }
m_ahsan 15:8c473836feba 275 else {
m_ahsan 15:8c473836feba 276 //add response code for no mendatory parameter
m_ahsan 15:8c473836feba 277 return; }
m_ahsan 15:8c473836feba 278
m_ahsan 15:8c473836feba 279 if(MSG_1.hasMember("rr"))
m_ahsan 15:8c473836feba 280 {
m_ahsan 15:8c473836feba 281 RequestReachability = MSG_1["rr"].get<bool>();
m_ahsan 15:8c473836feba 282 printf("RR: %s\r\n", RequestReachability? "true" : "false");
m_ahsan 15:8c473836feba 283 }
m_ahsan 15:8c473836feba 284 if(MSG_1.hasMember("aei"))
m_ahsan 15:8c473836feba 285 {
m_ahsan 15:8c473836feba 286 aei = MSG_1["aei"].get<std::string>();
m_ahsan 15:8c473836feba 287 printf("AE_ID: %s\r\n", aei.c_str());
m_ahsan 15:8c473836feba 288 }
m_ahsan 15:8c473836feba 289 if(MSG_1.hasMember("poa"))
m_ahsan 15:8c473836feba 290 {
m_ahsan 15:8c473836feba 291 poa = MSG_1["poa"].get<std::string>();
m_ahsan 15:8c473836feba 292 printf("POA: %s\r\n", poa.c_str());
m_ahsan 15:8c473836feba 293 }
m_ahsan 15:8c473836feba 294 if(MSG_1.hasMember("rn"))
m_ahsan 15:8c473836feba 295 {
m_ahsan 15:8c473836feba 296 rn = MSG_1["rn"].get<std::string>();
m_ahsan 15:8c473836feba 297 printf("Resource Name: %s\r\n", rn.c_str());
m_ahsan 15:8c473836feba 298 }
m_ahsan 15:8c473836feba 299 if(MSG_1.hasMember("api"))
m_ahsan 15:8c473836feba 300 {
m_ahsan 15:8c473836feba 301 api = MSG_1["api"].get<std::string>();
m_ahsan 15:8c473836feba 302 printf("App-ID: %s\r\n", api.c_str());
m_ahsan 15:8c473836feba 303 }
m_ahsan 15:8c473836feba 304 return;
m_ahsan 15:8c473836feba 305 }