v1

Dependencies:   MQTTSN mbed-http

sensor.h

Committer:
m_ahsan
Date:
2019-03-11
Revision:
15:557d0008dd2d

File content as of revision 15:557d0008dd2d:

#include <string>
void process_rsc(MbedJSONValue &MSG);

bool reg_resp = false;

char* CSE_ID = "cse1";
char* AE_ID;

struct Mendatory
{
    string To; 
    string From; 
    string Request_Identifier;
    int Operation; 
    int Resource_Type;
}m1;

struct CreateAE
{
    string resourceName; 
    string App_ID; 
    string pointOfAccess; 
    bool requestReachability;
}CAE;

const char* Create_AE()
{
    MbedJSONValue MSG, demo, demo2, demo3;
         
    std::string s;

   //fill the object

    demo3["api"] = CAE.App_ID;
    demo3["rn"] = CAE.resourceName;
    demo3["rr"] = CAE.requestReachability; 
    demo2["m2m:ae"] = demo3;
    demo["fr"] = m1.From;
    demo["op"] = m1.Operation;
    demo["pc"] = demo2;
    demo["rqi"] = m1.Request_Identifier;
    demo["to"] = m1.To;
    demo["ty"] = m1.Resource_Type;
    
    //serialize it into a JSON string
    s = demo.serialize();
    printf("\nMSG SIZE: %d\n", s.length());
    //printf("json: %s\r\n", s.c_str());
    return s.c_str();
}

void process_msg()
{
    MbedJSONValue MSG;
    //printf("Before Parsing\n");
    parse(MSG, messageBuffer); 
    //bool temp; //Check fot parameters presence
    
    ////////////////////////resource Type Parameter (Mendatory parameter)//////////////////////
    if(MSG.hasMember("rsc"))
    {
        rsc = MSG["rsc"].get<int>();
        printf("Response Status Code: %d\r\n", rsc);
        Response_Type();
        printf("Response Status: %s\r\n", response.c_str());
        process_rsc(MSG);
        return;
    }
    
    ////////////////////////resource Type Parameter (Mendatory parameter)//////////////////////
    if(MSG.hasMember("ty"))
    {
       ty = MSG["ty"].get<int>();
       printf("ResourceType: %d\r\n", ty);
       resource_type();
       printf("ResourceType: %s\r\n", resourceType.c_str());
    }
    else     {
    //add response code for no mendatory parameter
    return;   }
    
    ////////////////////////From parameter (Mendatory parameter[optional for Create AE])//////////
    if(MSG.hasMember("fr"))
    {
       From = MSG["fr"].get<std::string>();
       printf("From: %s\r\n", From.c_str());
    }
    else     {
    //add Response Status Code for no mendatory parameter BAD_REQUEST 
    return;  }
    
    ////////////////////////Operation Parameter (Mendatory parameter)//////////////////////
    if(MSG.hasMember("op"))
    {
       op = MSG["op"].get<int>();
       printf("Operation: %d\r\n", op);
       Operation_Type();
       printf("Operation: %s\r\n", operation.c_str());
    }
    else     {
    //add response code for no mendatory parameter
    return;  }
    
    ////////////////////////To Parameter (Mendatory parameter)//////////////////////
    if(MSG.hasMember("to"))
    {
       to = MSG["to"].get<std::string>();
       printf("To: %s\r\n", to.c_str());
    }
    else     {
    //add response code for no mendatory parameter
    return;  } 
         
    ////////////////////////Request Identifier (Mendatory parameter)//////////////////////
    if(MSG.hasMember("rqi"))
    {
       rqi = MSG["rqi"].get<std::string>();
       printf("Request Identifier: %s\r\n", rqi.c_str());
    }
    else     {
    //add response code for no mendatory parameter
    return;   }        
    
    ////////////////////////Response Status Code
    if(MSG.hasMember("rsc"))
    {
       rsc = MSG["rsc"].get<int>();
       printf("Response Status Code: %d\r\n", rsc);
       Response_Type();
       printf("Response Status: %s\r\n", response.c_str());
    }
    MbedJSONValue &MSG_1 = MSG["pc"][content]; //content value depends on the resource type e.g. m2m:ae for resource type 2
    
    //////////////////////// Resource specific attributes CSE-ID
    if(MSG_1.hasMember("csi"))
    {
       csi = MSG_1["csi"].get<std::string>();
       printf("CSI: %s\r\n", csi.c_str());
    }
       
    //////////////////////// Resource specific attributes Point of Access
    if(MSG_1.hasMember("poa"))
    {
       poa = MSG_1["poa"].get<std::string>();
       printf("POA: %s\r\n", poa.c_str());
    }  
     
    /////////////////////// Resource specific attributes CSE Type
    if(MSG_1.hasMember("cst"))
    {
       cst = MSG_1["cst"].get<int>();
       printf("CSE Type: %d\r\n", cst);
       CSE_Type();
       printf("CSE Type: %s\r\n", cseType.c_str());
    }
       
    /////////////////////// Resource specific attributes RequestReachability
    if(MSG_1.hasMember("rr"))
    {
       RequestReachability = MSG_1["rr"].get<bool>();
       printf("RR: %s\r\n", RequestReachability? "true" : "false");
    }
       
    /////////////////////// Resource specific attributes App-ID
    if(MSG_1.hasMember("api"))
    {
       api = MSG_1["api"].get<std::string>();
       printf("App-ID: %s\r\n", api.c_str());
    }   
    // msg[0] = '\0';
    //MSG.~MbedJSONValue();
    //MSG_1.~MbedJSONValue();*/
}

void process_rsc(MbedJSONValue &MSG)
{
    if(MSG.hasMember("rqi"))
    {
       rqi = MSG["rqi"].get<std::string>();
       printf("Request Identifier: %s\r\n", rqi.c_str());
    }
    else     {
    //add response code for no mendatory parameter
    return;   }  
    if(MSG.hasMember("to"))
    {
       to = MSG["to"].get<std::string>();
       printf("To: %s\r\n", to.c_str());
    }
    else     {
    //add response code for no mendatory parameter
    return;   } 
    if(MSG.hasMember("fr"))
    {
       From = MSG["fr"].get<std::string>();
       printf("From: %s\r\n", From.c_str());
    }
    else     {
    //add response code for no mendatory parameter
    return;   }
    
    MbedJSONValue &MSG_1 = MSG["pc"][content];
    
    if(MSG_1.hasMember("ty"))
    {
        ty = MSG_1["ty"].get<int>();
        printf("ResourceType: %d\r\n", ty);
        resource_type();
        printf("ResourceType: %s\r\n", resourceType.c_str());
    }
    else     {
    //add response code for no mendatory parameter
    return;   }
    
    if(MSG_1.hasMember("rr"))
    {
       RequestReachability = MSG_1["rr"].get<bool>();
       printf("RR: %s\r\n", RequestReachability? "true" : "false");
    } 
    if(MSG_1.hasMember("aei"))
    {
       aei = MSG_1["aei"].get<std::string>();
       printf("AE_ID: %s\r\n", aei.c_str());
    }         
    if(MSG_1.hasMember("poa"))
    {
       poa = MSG_1["poa"].get<std::string>();
       printf("POA: %s\r\n", poa.c_str());
    } 
    if(MSG_1.hasMember("rn"))
    {
       rn = MSG_1["rn"].get<std::string>();
       printf("Resource Name: %s\r\n", rn.c_str());
    }
    if(MSG_1.hasMember("api"))
    {
       api = MSG_1["api"].get<std::string>();
       printf("App-ID: %s\r\n", api.c_str());
    } 
    return;
}