123

Dependencies:   MQTTSN

sensor.txt

Committer:
m_ahsan
Date:
2019-08-06
Revision:
15:8c473836feba

File content as of revision 15:8c473836feba:

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

bool reg_resp = false;

const char* CSE_ID = "cse01";
char* AE_ID;
bool lcl;
///////////////////////////////////////
class Request{
public:
    int Operation;  
    string To;
    string From;
    string Request_Identifier;
    int Resource_Type;
};

///////////////////////////////////////

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

struct CreateCIN
{
    string resourceName;                //rn
    string contentInfo;                 //cnf 
    string content;                     //con
}CIN;

struct CreateSub
{
    string resourceName;                //rn
    string notificationURI;             //nu
    int notificationContentType;        //nct
    int notificationEventType;          //net
}CSub;
/////
const char* Create_Req(Request R1)
{
    MbedJSONValue demo, demo2, demo3, demo4;
         
    std::string s;
    switch(R1.Resource_Type){
        case 0:
            break;
        case 1:
            break;
        case 2:
            demo3["api"] = CAE.App_ID;
            demo3["rn"] = CAE.resourceName;
            demo3["rr"] = CAE.requestReachability; 
            demo2["m2m:ae"] = demo3;
        break;
        case 3:
            demo3["rn"] = CAE.resourceName;
            demo2["m2m:cnt"] = demo3;
        break;
        case 4:
            demo3["rn"] = CIN.resourceName;
            demo3["cnf"] = CIN.contentInfo; 
            demo3["con"] = CIN.content;
            demo2["m2m:cin"] = demo3;
        break;
        case 23:
            demo4["net"] = CSub.notificationEventType;
            demo3["enc"] = demo4;                       //enc = eventNotificationCriteria
            demo3["rn"] = CSub.resourceName;
            demo3["nu"] = CSub.notificationURI; 
            demo3["nct"] = CSub.notificationContentType;
            demo2["m2m:sub"] = demo3;
        break;
    }
    demo["fr"] = R1.From;
    demo["op"] = R1.Operation;
    demo["pc"] = demo2;
    demo["rqi"] = R1.Request_Identifier;
    demo["to"] = R1.To;
    demo["ty"] = R1.Resource_Type;
    demo["lcl"] = lcl;
    
    //serialize it into a JSON string
    s = demo.serialize();
    printf("\nMSG SIZE: %d\n", s.length());
    //printf("json: %s\r\n", s.c_str());
    /*const char* temp = s.c_str();
    char* temp1;
    strncpy(temp1, temp, strlen(temp));
    printf("string in temp1 is %s\n",temp1);
    return temp1;
    */
    return s.c_str();
}
/////

void process_msg()
{
    MbedJSONValue MSG;
    //printf("Before Parsing\n");
    //printf("messageBuffer %s \n", messageBuffer);
    //MbedJSONValue demo;
    //const  char * json = "{\"my_array\": [\"demo_string\", 10], \"my_boolean\": true}";

    //parse the previous string and fill the object demo
    //parse(demo, json);
    //printf("After Parsing json\n");
    parse(MSG, messageBuffer); 
    //bool temp; //Check fot parameters presence
    //printf("After Parsing\n");
    ////////////////////////Local Parameter (Additional parameter)//////////////////////
    if(MSG.hasMember("lcl"))
    {
        local = MSG["lcl"].get<bool>();
        printf("Local: %s\r\n", local? "true" : "false");
    }
     
    ////////////////////////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());
    } 
    messageBuffer[0] = '\0';
    // 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;
}