123
Dependencies: MQTTSN
Revision 15:8c473836feba, committed 2019-08-06
- Comitter:
- m_ahsan
- Date:
- Tue Aug 06 05:32:43 2019 +0000
- Parent:
- 14:e544557919d9
- Commit message:
- 123
Changed in this revision
diff -r e544557919d9 -r 8c473836feba Client-AE.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Client-AE.cpp Tue Aug 06 05:32:43 2019 +0000 @@ -0,0 +1,175 @@ +#define MQTTSNCLIENT_QOS2 1 + +#include "mbed.h" +#include "stdio.h" +#include "MQTT_server_setting.h" +#include "MQTTSNUDP.h" +#include "MQTTSNClient.h" +#include "sensor.h" +#include "Enumeration.h" +#include "easy-connect.h" +#include "MQTTSN_func.h" +#include "registration.h" +#include "container.h" +#include "contentInstance.h" +#include "Subscription.h" + +NetworkInterface* network = NULL; + +int main() { + + float version = 1.0; + MQTTSNUDP* ipstack = NULL; + MQTTSN::Client<MQTTSNUDP, Countdown> *client = NULL; + + ///////////////////////////////////////////////////////////////////////////////////////// + printf("Opening network interface...\r\n"); + { + network = easy_connect(true); // If true, prints out connection details. + if (!network) { + printf("Unable to open network interface.\r\n"); + return -1; + } + } + printf("Network interface opened successfully.\r\n"); + printf("\r\n"); + + const char* Local_IP = network->get_ip_address(); + printf("IP Address of Device is: %s\n", Local_IP); + + const char* Local_MAC = network->get_mac_address(); + printf("MAC Address of Device is: %s\n", Local_MAC); + ///////////////////////////////////////////////////////////////////////////////////////// + ipstack = new MQTTSNUDP(network); + client = new MQTTSN::Client<MQTTSNUDP, Countdown>(*ipstack); + + //////////////////////////////////UDP Connect/////////////////////////////////////////// + printf("Connecting to %s:%d\n", MQTT_SERVER_HOST_NAME, PORT); + rc = ipstack->connect(MQTT_SERVER_HOST_NAME, PORT); + if (rc != 0) + printf("rc from UDP connect is %d\n", rc); + else + printf("UDP connected\n"); + + //////////////////////////////////MQTT Connect/////////////////////////////////////////// + attemptConnect(client, ipstack); + + //////////////////////////////////MQTT Subscribe////////////////////////////////////////// + + //Set Topic to /oneM2M/reg_resp/Sensor01/CSE_01 + create_Topic("reg_resp", MQTT_CLIENT_ID, "CSE_01"); + + MQTTSN_topicid topicid; + if ((rc = subscribe(client, ipstack, topicid)) != 0) + printf("rc from MQTT subscribe is %d\n", rc); + else + printf("Subscribed to Topic %s\n", MQTT_TOPIC); + + //Set Topic to /oneM2M/reg_req/Sensor01/CSE_01 + create_Topic("reg_req", MQTT_CLIENT_ID, "CSE_01"); + + MQTTSN_topicid topicid2; + if ((rc = subscribe(client, ipstack, topicid2)) != 0) + printf("rc from MQTT subscribe is %d\n", rc); + else + printf("Subscribed to Topic %s\n", MQTT_TOPIC); + + //////////////////////////////////Registration/////////////////////////////////////////// + if(Registration(client, ipstack, topicid2) != 0 ) + printf("Registration failed with response: %s\n", response.c_str()); + else printf("Registration Done wth response: %s\n", response.c_str()); + + ////////////////////////////////Req-Resp Topics////////////////////////////////////////// + c_aei = new char [aei.length()+1];; + printf("\nAEI in main: %s\r\n\n\n", aei.c_str()); + strcpy(c_aei, aei.c_str()); + printf("C_AEI: %s\r\n", c_aei); + + //Set Topic to /oneM2M/resp/SAE01/CSE_01 + create_Topic("resp", c_aei, "CSE_01"); + + MQTTSN_topicid topicid3; + if ((rc = subscribe(client, ipstack, topicid3)) != 0) + printf("rc from MQTT subscribe is %d\n", rc); + else + printf("Subscribed to Topic %s\n", MQTT_TOPIC); + + //Set Topic to /oneM2M/req/SAE01/CSE_01 + create_Topic("req", c_aei, "CSE_01"); + + MQTTSN_topicid topicid4; + if ((rc = subscribe(client, ipstack, topicid4)) != 0) + printf("rc from MQTT subscribe is %d\n", rc); + else + printf("Subscribed to Topic %s\n", MQTT_TOPIC); + wait(3); + + ////////////////////////////////// Container //////////////////////////////////////////// + if(Container(client, ipstack, topicid4) != 0 ) + printf("Container Creation failed with response: %s\n", response.c_str()); + else printf("Container Created wth response: %s\n", response.c_str()); + wait(3); + ////////////////////////////////// Subscription //////////////////////////////////////////// + if(Subscription(client, ipstack, topicid4) != 0 ) + printf("Subscription Creation failed with response: %s\n", response.c_str()); + else printf("Subscription Created wth response: %s\n", response.c_str()); + wait(3); + ////////////////////////////////// ContentInstance ////////////////////////////////////// + if(contentInstance(client, ipstack, topicid4) != 0 ) + printf("ContentInstance Creation failed with response: %s\n", response.c_str()); + else printf("ContentInstance Created wth response: %s\n", response.c_str()); + + rc = delete_cnt(client, ipstack, topicid4); + rc = delete_sub(client, ipstack, topicid4); + + + while(1) + { + // Check connection // + if(!client->isConnected()){ + break; + } + // Received a control message. // + if(isMessageArrived) { + isMessageArrived = false; + // Just print it out here. + printf("\r\nMessage arrived:\r\n%s\r\n", messageBuffer); + + //generate notification response + if(op == 5) + { + Response Resp; + RES.resourceName = rn; + Resp.Request_Identifier = rqi; + Resp.To = From; + lcl = false; + Resp.From = CSE_ID; + buf = Notify_Resp(Resp); + if ((rc = publish(client, ipstack, topicid3)) != 0) + printf("rc from MQTT Publish is %d\n", rc); + else + printf("Published Buffer: %s to Topic %s\n",buf, MQTT_TOPIC); + } + + } + client->yield(4000); + //wait(4); + } + + //if ((rc = client->unsubscribe(topicid2)) != 0) + // printf("rc from unsubscribe was %d\n", rc); + + if ((rc = client->disconnect()) != 0) + printf("rc from disconnect was %d\n", rc); + + ipstack->disconnect(); + + delete ipstack; + delete client; + + printf("Version %.2f: finish %d msgs\n", version, arrivedcount); + printf("Finishing with %d messages received\n", arrivedcount); + + return 0; + +}
diff -r e544557919d9 -r 8c473836feba Client-main.cpp --- a/Client-main.cpp Mon Dec 10 08:06:33 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,190 +0,0 @@ -/* SpwfInterface NetworkSocketAPI Example Program - * Copyright (c) 2015 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "mbed.h" -#define MQTTSNCLIENT_QOS2 1 -//#include "TCPSocket.h" -//#include "UDPSocket.h" -#include "MQTTSNUDP.h" -#include "MQTTSNClient.h" - -// - -//------------------------------------ -// Hyperterminal configuration -// 9600 bauds, 8-bit data, no parity -//------------------------------------ - -Serial pc(USBTX, USBRX); -DigitalOut myled(LED1); - -int arrivedcount = 0; - -void messageArrived(MQTTSN::MessageData& md) -{ - MQTTSN::Message &message = md.message; - printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n, Number: %d", message.qos, message.retained, message.dup, message.id, arrivedcount); - printf("Payload %.*s\n", message.payloadlen, (char*)message.payload); - ++arrivedcount; - puts((char*)message.payload); -} - -int main() { - - float version = 0.47; - char* topic = "mbed-sample"; - - MQTTSNUDP ipstack = MQTTSNUDP(); - - char * ssid = "KICS-IOT-DHCP"; - char * seckey = "AAAFFFBBBC"; - - printf("\r\nX-NUCLEO-IDW01M1 mbed Application\r\n"); - printf("\r\nconnecting to AP\r\n"); - - if(spwf.connect(ssid, seckey, NSAPI_SECURITY_WPA2)) { - printf("\r\nnow connected\r\n"); - } else { - printf("\r\nerror connecting to AP.\r\n"); - return -1; - } - const char* Local_IP = spwf.get_ip_address(); - printf("IP Address of Device is: %s\n", Local_IP); - - const char* Local_MAC = spwf.get_mac_address(); - printf("MAC Address of Device is: %s\n", Local_MAC); - - MQTTSN::Client<MQTTSNUDP, Countdown> client = MQTTSN::Client<MQTTSNUDP, Countdown>(ipstack); - char* hostname = "192.168.0.114"; - int port = 10000; - printf("Connecting to %s:%d\n", hostname, port); - int rc = ipstack.connect(hostname, port); - if (rc != 0) - printf("rc from UDP connect is %d\n", rc); - else - printf("UDP connected\n"); - - MQTTSNPacket_connectData data = MQTTSNPacket_connectData_initializer; - data.clientID.cstring = "mbed-sample"; - data.duration = 60; - if ((rc = client.connect(data)) != 0) - printf("rc from MQTT connect is %d\n", rc); - else - printf("MQTT Connected\n"); - - MQTTSN_topicid topicid; - topicid.type = MQTTSN_TOPIC_TYPE_NORMAL; - topicid.data.long_.name = topic; - topicid.data.long_.len = strlen(topic); - MQTTSN::QoS grantedQoS; - if ((rc = client.subscribe(topicid, MQTTSN::QOS1, grantedQoS, messageArrived)) != 0) - printf("rc from MQTT subscribe is %d\n", rc); - else - printf("Subscribed to Topic %s\n", topic); - - MQTTSN::Message message; - - char buf[100]; - int i; - // QoS 0 - for(i = 0 ; i<= 10; i++){ - sprintf(buf, "Hello World! QoS 0 message from app version %f\n", version); - message.qos = MQTTSN::QOS0; - message.retained = false; - message.dup = false; - message.payload = (void*)buf; - message.payloadlen = strlen(buf)+1; - - if ((rc = client.publish(topicid, message)) != 0) - printf("rc from MQTT Publish is %d\n", rc); - else - printf("Published Buffer %s to Topic %s\n",buf, topic); - printf("arrivedcount %d, i = %d\n",arrivedcount, i); - //while (arrivedcount < i) - client.yield(100); - } - wait(1); - // QoS 1 - - sprintf(buf, "Hello World! QoS 1 message from app version %f\n", version); - message.qos = MQTTSN::QOS1; - message.payloadlen = strlen(buf)+1; - if ((rc = client.publish(topicid, message)) != 0) - printf("rc from MQTT Publish is %d\n", rc); - else - printf("Published Buffer %s to Topic %s\n",buf, topic); - i++; - //while (arrivedcount < i) - client.yield(100); - wait(1); - // QoS 1 - - sprintf(buf, "Hello World! QoS 2 message from app version %f\n", version); - message.qos = MQTTSN::QOS2; - message.payloadlen = strlen(buf)+1; - if ((rc = client.publish(topicid, message)) != 0) - printf("rc from MQTT Publish is %d\n", rc); - else - printf("Published Buffer %s to Topic %s\n",buf, topic); - i++; - //while (arrivedcount < i) - client.yield(100); - wait(1); - /* - // QoS 2 - sprintf(buf, "Hello World! QoS 2 message from app version %f\n", version); - message.qos = MQTTSN::QOS2; - message.payloadlen = strlen(buf)+1; - if ((rc = client.publish(topicid, message)) != 0) - printf("rc from MQTT Publish is %d\n", rc); - else - printf("Published Buffer %s to Topic %s\n",buf, topic); - i++; - printf("arrivedcount %d, i = %d\n",arrivedcount, i); - //while (arrivedcount < i) - client.yield(100); - wait(3); */ - /* - // n * QoS 2 - for (int a = 1; a <= 10; a++) - { - sprintf(buf, "Hello World! QoS 2 message number %d from app version %f\n", i, version); - message.qos = MQTTSN::QOS2; - message.payloadlen = strlen(buf)+1; - if ((rc = client.publish(topicid, message)) != 0) - printf("rc from MQTT Publish is %d\n", rc); - else - printf("Published Buffer %s to Topic %s\n",buf, topic); - i++; - printf("arrivedcount %d, i = %d\n",arrivedcount, i); - while (arrivedcount < i) - client.yield(100); - } - */ - if ((rc = client.unsubscribe(topicid)) != 0) - printf("rc from unsubscribe was %d\n", rc); - - if ((rc = client.disconnect()) != 0) - printf("rc from disconnect was %d\n", rc); - - ipstack.disconnect(); - - printf("Version %.2f: finish %d msgs\n", version, arrivedcount); - printf("Finishing with %d messages received\n", arrivedcount); - - return 0; - -}
diff -r e544557919d9 -r 8c473836feba Enumeration.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Enumeration.h Tue Aug 06 05:32:43 2019 +0000 @@ -0,0 +1,325 @@ +#include <string> + +int ty, op, cst, rsc; +std::string resourceType, cseType, operation, content, response; +bool RequestReachability; +bool rr, local; +std::string From, csi, api, poa,to, rqi, aei, rn; +char URI[40]; +char* c_aei; +std::string con, cnf; +std::string nu; +std::string sur; +int net, nct; + +extern struct Resource RES; +extern struct regularResource regRES; +extern struct announceableResource ancRES; +extern struct announcedResource ancdRES; +extern struct subordinateResource subRES; +extern struct announceableSubordinateResource ancsubRES; +extern struct announcedSubordinateResource ancdsubRES; + +extern struct CreateAE CAE; +extern struct CreateCIN CIN; +extern struct CreateSub CSub; +extern struct respAE RAE; +extern struct respCnt RCnt; +extern struct respCin RCin; +extern struct respSub RSub; + +char* CSE_ID = "cse01"; +char* AE_ID; +bool lcl; + +bool CreateAE; + +void resource_type(); +void Operation_Type(); +void CSE_Type(); + +void resource_type() +{ + switch (ty) + { + case 1: + resourceType = "accessControlPolicy"; + content = "m2m:acp"; + break; + case 2: + resourceType = "AE"; + content = "m2m:ae"; + break; + case 3: + resourceType = "container"; + content = "m2m:cnt"; + break; + case 4: + resourceType = "contentInstance"; + content = "m2m:cin"; + break; + case 5: + resourceType = "CSEBase"; + content = "m2m:cb"; + break; + case 6: + resourceType = "delivery"; + content = "m2m:dlv"; + break; + case 7: + resourceType = "eventConfig"; + content = "m2m:evcg"; + break; + case 8: + resourceType = "execInstance"; + content = "m2m:exin"; + break; + case 9: + resourceType = "group"; + content = "m2m:grp"; + break; + case 10: + resourceType = "locationPolicy"; + content = "m2m:lcp"; + break; + case 11: + resourceType = "m2mServiceSubscriptionProfile"; + content = "m2m:mssp"; + break; + case 12: + resourceType = "mgmtCmd"; + content = "m2m:mgc"; + break; + case 13: + resourceType = "mgmtObj"; + //content = "m2m:cnt"; + break; + case 14: + resourceType = "node"; + content = "m2m:nod"; + break; + case 15: + resourceType = "pollingChannel"; + content = "m2m:contenth"; + break; + case 16: + resourceType = "remoteCSE"; + content = "m2m:csr"; + break; + case 17: + resourceType = "request"; + content = "m2m:req"; + break; + case 18: + resourceType = "schedule"; + content = "m2m:sch"; + break; + case 19: + resourceType = "serviceSubscribedAppRule"; + content = "m2m:asar"; + break; + case 20: + resourceType = "serviceSubscribedNode"; + content = "m2m:svsn"; + break; + case 21: + resourceType = "statsCollect"; + content = "m2m:stcl"; + break; + case 22: + resourceType = "statsConfig"; + content = "m2m:stcg"; + break; + case 23: + resourceType = "subscription"; + content = "m2m:sub"; + break; + case 10001: + resourceType = "accessControlPolicyAnnc"; + content = "m2m:acpA"; + break; + case 10002: + resourceType = "AEAnnc"; + content = "m2m:aeA"; + break; + case 10003: + resourceType = "containerAnnc"; + content = "m2m:cntA"; + break; + case 10004: + resourceType = "contentInstanceAnnc"; + content = "m2m:cinA"; + break; + case 10009: + resourceType = "groupAnnc"; + content = "m2m:grpA"; + break; + case 10010: + resourceType = "locationPolicyAnnc"; + content = "m2m:lcpA"; + break; + case 10013: + resourceType = "mgmtObjAnnc"; + //content = "m2m:cnt"; + break; + case 10014: + resourceType = "nodeAnnc"; + content = "m2m:nodA"; + break; + case 10016: + resourceType = "remoteCSEAnnc"; + content = "m2m:csrA"; + break; + case 10018: + resourceType = "scheduleAnnc"; + content = "m2m:schA"; + break; + } +} +void CSE_Type() +{ + switch (cst) + { + case 1: + cseType = "IN_CSE"; + break; + case 2: + cseType = "MN_CSE"; + break; + case 3: + cseType = "ASN_CSE"; + break; + } +} +void Operation_Type() //CRUD+N +{ + switch (op) + { + case 1: + operation = "Create"; + break; + case 2: + operation = "Retrieve"; + break; + case 3: + operation = "Update"; + break; + case 4: + operation = "Delete"; + break; + case 5: + operation = "Notify"; + break; + } +} +void Response_Type() +{ + switch (rsc) + { + //Informational response class + case 1000: + response = "ACCEPTED"; + break; + case 1001: + response = "ACCEPTED for nonBlockingRequestSynch"; + break; + case 1002: + response = "ACCEPTED for nonBlockingRequestAsynch"; + break; + //specifies the RSCs for successful responses. + case 2000: + response = "OK"; + break; + case 2001: + response = "CREATED"; + break; + case 2002: + response = "DELETED"; + break; + case 2004: + response = "UPDATED"; + break; + //RSCs for Originator error response class + case 4000: + response = "BAD_REQUEST"; + break; + case 4001: + response = "RELEASE_VERSION_NOT_SUPPORTED"; + break; + case 4004: + response = "NOT_FOUND"; + break; + case 4005: + response = "OPERATION_NOT_ALLOWED"; + break; + case 4008: + response = "REQUEST_TIMEOUT"; + break; + case 4101: + response = "SUBSCRIPTION_CREATOR_HAS_NO_PRIVILEGE"; + break; + case 4102: + response = "CONTENTS_UNACCEPTABLE"; + break; + case 4103: + response = "ORIGINATOR_HAS_NO_PRIVILEGE"; + break; + case 4104: + response = "GROUP_REQUEST_IDENTIFIER_EXISTS"; + break; + case 4105: + response = "CONFLICT"; + break; + case 4106: + response = "ORIGINATOR_HAS_NOT_REGISTERED"; + break; + case 4107: + response = "SECURITY_ASSOCIATION_REQUIRED"; + break; + case 4108: + response = "INVALID_CHILD_RESOURCE_TYPE"; + break; + case 4109: + response = "NO_MEMBERS"; + break; + case 4110: + response = "GROUP_MEMBER_TYPE_INCONSISTENT"; + break; + case 4111: + response = "ESPRIM_UNSUPPORTED_OPTION"; + break; + case 4112: + response = "ESPRIM_UNKNOWN_KEY_ID"; + break; + case 4113: + response = "ESPRIM_UNKNOWN_ORIG_RAND_ID"; + break; + case 4114: + response = "ESPRIM_UNKNOWN_RECV_RAND_ID"; + break; + case 4115: + response = "ESPRIM_BAD_MAC"; + break; + case 4116: + response = "ESPRIM_IMPERSONATION_ERROR"; + break; + case 4117: + response = "ORIGINATOR_HAS_ALREADY_REGISTERED"; + break; + case 4118: + response = "ONTOLOGY_NOT_AVAILABLE"; + break; + case 4119: + response = "LINKED_SEMANTICS_NOT_AVAILABLE"; + break; + case 4120: + response = "INVALID_SEMANTICS"; + break; + case 4121: + response = "MASHUP_MEMBER_NOT_FOUND"; + break; + case 4122: + response = "INVALID_TRIGGER_PURPOSE"; + break; + } +}
diff -r e544557919d9 -r 8c473836feba MQTTSN.lib --- a/MQTTSN.lib Mon Dec 10 08:06:33 2018 +0000 +++ b/MQTTSN.lib Tue Aug 06 05:32:43 2019 +0000 @@ -1,1 +1,1 @@ -https://os.mbed.com/teams/OneM2M/code/MQTTSN/#489a49ad708a +https://os.mbed.com/teams/OneM2M/code/MQTTSN/#19ee54aaabee
diff -r e544557919d9 -r 8c473836feba MQTTSN_func.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MQTTSN_func.h Tue Aug 06 05:32:43 2019 +0000 @@ -0,0 +1,107 @@ +int connack_rc = 0; +int retryAttempt = 0; +int connectTimeout = 1000; +int rc = 0; + +enum connack_return_codes +{ + MQTTSN_CONNECTION_ACCEPTED = 0, + MQTTSN_REJECTED_CONGESTION = 1, + MQTTSN_REJECTED_INVALID_TOPIC_ID = 2, + MQTTSN_REJECTED_NOT_SUPPORTED= 3, +}; + +static volatile bool isMessageArrived = false; +int arrivedcount = 0; + +int connect(MQTTSN::Client<MQTTSNUDP, Countdown> *client, MQTTSNUDP* ipstack) +{ + int rc; + MQTTSNPacket_connectData data = MQTTSNPacket_connectData_initializer; + data.clientID.cstring = MQTT_CLIENT_ID; + data.duration = 600; + if ((rc = client->connect(data)) == 0) { + printf ("--->MQTT-SN Connected\n\r"); + } + else { + printf("MQTT-SN connect returned %d\n", rc); + } + if (rc >= 0) + connack_rc = rc; + return rc; +} + +int getConnTimeout(int attemptNumber) +{ // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute + // after 20 attempts, retry every 10 minutes + return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600; +} + +void attemptConnect(MQTTSN::Client<MQTTSNUDP, Countdown> *client, MQTTSNUDP* ipstack) +{ + while (connect(client, ipstack) != MQTTSN_CONNECTION_ACCEPTED) + { + int timeout = getConnTimeout(++retryAttempt); + printf("Retry attempt number %d waiting %d\n", retryAttempt, timeout); + + // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed + // or maybe just add the proper members to do this disconnect and call attemptConnect(...) + // this works - reset the system when the retry count gets to a threshold + if (retryAttempt == 5) + NVIC_SystemReset(); + else + wait(timeout); + } +} + +void messageArrived(MQTTSN::MessageData& md) +{ + MQTTSN::Message &message = md.message; + printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n, Number: %d", message.qos, message.retained, message.dup, message.id, arrivedcount); + memcpy(messageBuffer, (char*)message.payload, message.payloadlen); + messageBuffer[message.payloadlen] = '\0'; + printf("Payload %.*s\n", message.payloadlen, (char*)message.payload); + printf("Payload length %d\n",message.payloadlen); + ++arrivedcount; + process_msg(messageBuffer); + isMessageArrived = true; +} + +int subscribe(MQTTSN::Client<MQTTSNUDP, Countdown> *client, MQTTSNUDP* ipstack, MQTTSN_topicid& topicid) +{ + //MQTTSN_topicid topicid; + topicid.type = MQTTSN_TOPIC_TYPE_NORMAL; + topicid.data.long_.name = MQTT_TOPIC; + topicid.data.long_.len = strlen(MQTT_TOPIC); + MQTTSN::QoS grantedQoS; + return client->subscribe(topicid, MQTTSN::QOS1, grantedQoS, messageArrived); +} + +int publish(MQTTSN::Client<MQTTSNUDP, Countdown> *client, MQTTSNUDP* ipstack, MQTTSN_topicid& topicid) +{ + /*int rc; + if(PUB_REG == false) + { + if ((rc = subscribe(client, ipstack, topicid2)) != 0) + printf("rc from MQTT subscribe is %d\n", rc); + else{ + printf("Subscribed to Topic %s\n", MQTT_TOPIC); + PUB_REG = true; + } + }*/ + + MQTTSN::Message message; + message.qos = MQTTSN::QOS1; + message.retained = false; + message.dup = false; + message.payload = (void*)buf; + message.payloadlen = strlen(buf)+1; + return client->publish(topicid, message); +} + +void create_Topic(char* tpc_ty, char* orignator, char* receiver) +{ + sprintf(MQTT_TOPIC,"/oneM2M/%s/%s/%s",tpc_ty,orignator,receiver); + printf("Topic is %s", MQTT_TOPIC); + return; +}
diff -r e544557919d9 -r 8c473836feba MQTT_server_setting.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MQTT_server_setting.h Tue Aug 06 05:32:43 2019 +0000 @@ -0,0 +1,29 @@ +#ifndef __MQTT_SERVER_SETTING_H__ +#define __MQTT_SERVER_SETTING_H__ + +#define MQTT_MAX_PAYLOAD_SIZE 700 +#define MQTT_CLIENT_ID "Sensor011" + +//char *buf; +const char * ssid = "KICS-IOT-DHCP"; +const char * seckey = "AAAFFFBBBC"; +bool PUB_REG = false; +/* Buffer size for a receiving message. */ +const int MESSAGE_BUFFER_SIZE = 512; +/* Buffer for a receiving message. */ +char messageBuffer[MESSAGE_BUFFER_SIZE]; + +//const char *buffer = NULL; +const char* buf; +//char buf[MESSAGE_BUFFER_SIZE]; + +char *MQTT_SERVER_HOST_NAME = "192.168.0.114"; +short MQTT_Atmpt = 0; +int PORT = 10000; +//char *MQTT_CLIENT_ID = "Sensor02"; + +char MQTT_TOPIC[40]; +//const unsigned char* kpsa = "123"; +//const unsigned char* kpsaID = "bridge"; + +#endif /* __MQTT_SERVER_SETTING_H__ */
diff -r e544557919d9 -r 8c473836feba MbedJSONValue.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MbedJSONValue.lib Tue Aug 06 05:32:43 2019 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/teams/OneM2M/code/MbedJSONValue1232/#93f9fd1ed417
diff -r e544557919d9 -r 8c473836feba NetworkSocketAPI.lib --- a/NetworkSocketAPI.lib Mon Dec 10 08:06:33 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/teams/NetworkSocketAPI/code/NetworkSocketAPI/#ea3a618e0818
diff -r e544557919d9 -r 8c473836feba Readme.md --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Readme.md Tue Aug 06 05:32:43 2019 +0000 @@ -0,0 +1,3 @@ +The program is tested with Nucleo-L476RG with ESP-8266 used for network connectivity. +The mbed-os is not updated and should not be as the easy connect library is not compatible with the newer version of OS. +Notification is yet to be completed. \ No newline at end of file
diff -r e544557919d9 -r 8c473836feba Sensor/sensor.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Sensor/sensor.cpp Tue Aug 06 05:32:43 2019 +0000 @@ -0,0 +1,461 @@ +#include "sensor.h" +#include "MbedJSONValue.h" + +void process_rsc(MbedJSONValue &MSG); + +struct Resource RES; +struct regularResource regRES; +struct announceableResource ancRES; +struct announcedResource ancdRES; +struct subordinateResource subRES; +struct announceableSubordinateResource ancsubRES; +struct announcedSubordinateResource ancdsubRES; + +struct CreateAE CAE; +struct CreateCIN CIN; +struct CreateSub CSub; +struct respAE RAE; +struct respCnt RCnt; +struct respCin RCin; +struct respSub RSub; + +//extern const char* CSE_ID; +extern char* AE_ID; +extern bool lcl; + +////////////////////////////////////////////// +// Func: Create_Req // +// returns a json string // +// input parameters Class Request // +////////////////////////////////////////////// +const char* Create_Req(Request Req) +{ + MbedJSONValue demo, demo2, demo3, demo4; + + std::string s; + switch(Req.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["cnf"] = CIN.contentInfo; + demo3["con"] = CIN.content; + demo2["m2m:cin"] = demo3; + break; + case 23: + demo3["rn"] = CSub.resourceName; + demo4["net"] = CSub.notificationEventType; + demo3["enc"] = demo4; + demo3["nu"] = CSub.notificationURI; + demo3["nct"] = CSub.notificationContentType; + demo2["m2m:sub"] = demo3; + break; + } + demo["fr"] = Req.From; + demo["op"] = Req.Operation; + demo["pc"] = demo2; + demo["rqi"] = Req.Request_Identifier; + demo["to"] = Req.To; + demo["ty"] = Req.Resource_Type; + demo["lcl"] = lcl; + + //serialize it into a JSON string + s = demo.serialize(); + printf("\nMSG SIZE: %d\n", s.length()); + printf("Req JSON: %s\r\n", s.c_str()); + + return s.c_str(); +} +////////////////////////////////////////////// +// Func: Delete_Req // +// returns a json string // +// input parameters Class Request // +////////////////////////////////////////////// +const char* Delete_Req(Request Req) +{ + MbedJSONValue demo, demo2, demo3, demo4; + + std::string s; + + demo["fr"] = Req.From; + demo["op"] = Req.Operation; + demo["rqi"] = Req.Request_Identifier; + demo["to"] = Req.To; + demo["lcl"] = lcl; + + //serialize it into a JSON string + s = demo.serialize(); + printf("\nMSG SIZE: %d\n", s.length()); + printf("Req JSON: %s\r\n", s.c_str()); + + return s.c_str(); +} +////////////////////////////////////////////// +// Func: Create_Resp // +// returns a json string // +// input parameters Class Response // +////////////////////////////////////////////// +const char* Create_Resp(Response Resp) +{ + MbedJSONValue demo, demo2, demo3, demo4; + demo3["ty"] = RES.Resource_Type; + demo3["ri"] = RES.resourceID; + demo3["pi"] = RES.parentID; + demo3["ct"] = RES.creationTime; //ct 1 + demo3["lt"] = RES.lastModifiedTime; //lt 1 + demo3["rn"] = RES.resourceName; + std::string s; + printf("RES.Resource_Type = %d",RES.Resource_Type); + switch(RES.Resource_Type){ + case 0: + demo3["api"] = RAE.App_ID; + demo3["rr"] = RAE.requestReachability; + demo3["aei"] = RAE.AE_ID; + demo3["poa"] = RAE.pointOfAccess; + demo2["m2m:ae"] = demo3; + break; + case 1: + break; + case 2: + demo3["api"] = RAE.App_ID; + demo3["rr"] = RAE.requestReachability; + demo3["aei"] = RAE.AE_ID; + demo3["poa"] = RAE.pointOfAccess; + demo2["m2m:ae"] = demo3; + break; + case 3: + demo3["st"] = RCnt.stateTag; + demo3["cni"] = RCnt.CurrentNrOfInstances; + demo3["cbs"] = RCnt.CurrentByteSize; + demo2["m2m:cnt"] = demo3; + break; + case 4: + demo3["et"] = ancsubRES.expirationTime; + demo3["st"] = RCin.stateTag; + demo3["cnf"] = RCin.contentInfo; + demo3["cs"] = RCin.contentSize; + demo3["con"] = RCin.content; + demo2["m2m:cin"] = demo3; + break; + case 23: + demo3["et"] = ancsubRES.expirationTime; + demo4["net"] = RSub.notificationEventType; + demo3["enc"] = demo4; + demo3["nct"] = RSub.notificationContentType; + demo3["nu"] = RSub.notificationURI; + demo2["m2m:sub"] = demo3; + break; + + } + demo["rsc"] = Resp.responseStatusCode; + demo["rqi"] = Resp.Request_Identifier; + demo["pc"] = demo2; + demo["to"] = Resp.To; + demo["fr"] = Resp.From; + demo["lcl"] = lcl; + + //serialize it into a JSON string + s = demo.serialize(); + printf("\nMSG SIZE: %d\n", s.length()); + printf("RESP JSON: %s\r\n", s.c_str()); + return s.c_str(); +} + +const char* Notify_Resp(Response Resp) +{ + MbedJSONValue demo; + std::string s; + demo["rsc"] = Resp.responseStatusCode; + demo["rqi"] = Resp.Request_Identifier; + demo["fr"] = Resp.From; + demo["to"] = Resp.To; + demo["lcl"] = lcl; + //serialize it into a JSON string + s = demo.serialize(); + printf("\nMSG SIZE: %d\n", s.length()); + printf("RESP JSON: %s\r\n", s.c_str()); + return s.c_str(); +} + +void process_msg(const char* Buffer) +{ + MbedJSONValue MSG; + + parse(MSG, Buffer); + + ////////////////////////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; + } + + ////////////////////////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()); + /* Process Notification*/ + if(op == 5) + { + MbedJSONValue &MSG_1 = MSG["pc"]["m2m:sgn"]; + sur = MSG_1["sur"].get<std::string>(); + printf("Subscription Reference: %s\r\n", sur.c_str()); + MbedJSONValue &MSG_2 = MSG_1["m2m:sgn"]["nev"]; + net = MSG_2["net"].get<int>(); + printf("NotificationEventType: %d\r\n", net); + MbedJSONValue &MSG_3 = MSG_2["nev"]["rep"]["cin"]; + con = MSG_3["con"].get<std::string>(); + printf("Content: %s\r\n", con.c_str()); + cnf = MSG_3["cnf"].get<std::string>(); + printf("Content Info: %s\r\n", cnf.c_str()); + to = MSG["to"].get<std::string>(); + printf("To: %s\r\n", to.c_str()); + rqi = MSG["rqi"].get<std::string>(); + printf("Request Identifier: %s\r\n", rqi.c_str()); + MSG.~MbedJSONValue(); + MSG_1.~MbedJSONValue(); + MSG_2.~MbedJSONValue(); + MSG_3.~MbedJSONValue(); + return; + } + } + 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; } + + ////////////////////////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; } + + ////////////////////////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()); + } + /////////////////////// Resource specific attributes resourceName + if(MSG_1.hasMember("rn")) + { + rn = MSG_1["rn"].get<std::string>(); + printf("resourceName : %s\r\n", rn.c_str()); + } + /////////////////////// Resource specific attributes Content + if(MSG_1.hasMember("con")) + { + con = MSG_1["con"].get<std::string>(); + printf("Content: %s\r\n", con.c_str()); + } + /////////////////////// Resource specific attributes ContentInfo + if(MSG_1.hasMember("cnf")) + { + cnf = MSG_1["cnf"].get<std::string>(); + printf("Content Info: %s\r\n", cnf.c_str()); + } + /////////////////////// Resource specific attributes NotificationURI nu + if(MSG_1.hasMember("nu")) + { + nu = MSG_1["nu"].get<std::string>(); + printf("Notification URI: %s\r\n", nu.c_str()); + } + /////////////////////// Resource specific attributes NotificationContentType + if(MSG_1.hasMember("nct")) + { + nct = MSG_1["nct"].get<int>(); + printf("NotificationContentType: %d\r\n", nct); + MbedJSONValue &MSG_2 = MSG_1["m2m:sub"]["enc"]; + if(MSG_2.hasMember("net")) + { + net = MSG_2["net"].get<int>(); + printf("NotificationEventType: %d\r\n", net); + } + MSG_2.~MbedJSONValue(); + } + + MSG.~MbedJSONValue(); + MSG_1.~MbedJSONValue(); + return; +} + +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 in response: %d\r\n", ty); + //resource_type(); + //printf("ResourceType in response: %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()); + } + /////////////////////// Resource specific attributes Content + if(MSG_1.hasMember("con")) + { + con = MSG_1["con"].get<std::string>(); + printf("Content: %s\r\n", con.c_str()); + } + /////////////////////// Resource specific attributes ContentInfo + if(MSG_1.hasMember("cnf")) + { + cnf = MSG_1["cnf"].get<std::string>(); + printf("Content Info: %s\r\n", cnf.c_str()); + } + + return; +}
diff -r e544557919d9 -r 8c473836feba Sensor/sensor.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Sensor/sensor.h Tue Aug 06 05:32:43 2019 +0000 @@ -0,0 +1,160 @@ +#pragma once +#include <string> + + +using namespace std; + +extern int ty, op, cst, rsc; +extern std::string resourceType, cseType, operation, content, response; +extern bool RequestReachability; +extern bool rr, local; +extern std::string From, csi, api, poa, to, rqi, aei, rn; +extern char URI[40]; +extern char* c_aei; +extern std::string con, cnf; +extern std::string nu; +extern std::string sur; +extern int net, nct; + +extern void resource_type(); +extern void Operation_Type(); +extern void Response_Type(); +extern void CSE_Type(); + + +struct Resource +{ + string resourceName; //rn 1 + int Resource_Type; //ty 1 + string resourceID; //ri 1 + string parentID; //pi 1 + string creationTime; //ct 1 + string lastModifiedTime; //lt 1 + string labels; // 0-1 +}; + +struct regularResource +{ + //struct RES; + string accessControlPolicyIDs; // + string expirationTime; //et 1 + string dynamicAuthorizationConsultationIDs; // +}; + +struct announceableResource +{ + //struct regRES; + string announceTo; // + string announcedAttribute; // +}; + +struct announcedResource +{ + //struct RES; + string accessControlPolicyIDs; // 1 + string expirationTime; //et 1 + string link; // 1 + string dynamicAuthorizationConsultationIDs; // 0-1 +}; + +struct subordinateResource +{ + //struct RES; + string expirationTime; //et 1 +}; + +struct announceableSubordinateResource +{ + //struct RES; + string expirationTime; //et 1 + string announceTo; // 0-1 + string announcedAttribute; // 0-1 +}; + +struct announcedSubordinateResource +{ + //struct RES; + string expirationTime; //et 1 + string link; // 1 +}; + +class Response +{ +public: + int responseStatusCode; + string Request_Identifier; + string To; + string From; + int releaseVersion; +}; + +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; +}; + +struct CreateCIN +{ + string resourceName; //rn + string contentInfo; //cnf + string content; //con +}; + +struct CreateSub +{ + string resourceName; //rn + string notificationURI; //nu + int notificationContentType; //nct + int notificationEventType; //net +}; + +struct respAE +{ + string App_ID; + bool requestReachability; + string AE_ID; + string pointOfAccess; +}; + +struct respCnt +{ + int stateTag; + int CurrentNrOfInstances; + int CurrentByteSize; +}; +struct respCin +{ + string contentInfo; + int contentSize; + int stateTag; + string content; +}; + +struct respSub +{ + string notificationURI; + int notificationContentType; + int notificationEventType; +}; + +const char* Create_Req(Request Req); + +const char* Delete_Req(Request Req); + +const char* Create_Resp(Response Resp); + +const char* Notify_Resp(Response Resp); + +void process_msg(const char* Buffer); \ No newline at end of file
diff -r e544557919d9 -r 8c473836feba Subscription.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Subscription.h Tue Aug 06 05:32:43 2019 +0000 @@ -0,0 +1,78 @@ + +int Subscription(MQTTSN::Client<MQTTSNUDP, Countdown> *client, MQTTSNUDP* ipstack, MQTTSN_topicid& topicid) +{ + //Create Subscription + //Mendatory resources: Operation, To, From, Request Identifier ,Resource Type, Content + //Resource Specific Attributes [M]: + // [0]: + + //struct Mendatory m1; (to, fr, rqi, ty, op) + Request R1; + R1.To = CSE_ID; + R1.From = aei; + R1.Request_Identifier = "createSub01"; + R1.Resource_Type = 23; + R1.Operation = 1; + + //struct CreateSub CSub; + CSub.notificationEventType = 3; + CSub.resourceName = "sub_monitor01";; + CSub.notificationURI = aei; + CSub.notificationContentType = 1; + lcl = false; + //buffer = Create_Cnt(); + buf = Create_Req(R1); + //buf[0] = '\0'; + + //PUBLISH + if ((rc = publish(client, ipstack, topicid)) != 0) + printf("rc from MQTT Publish is %d\n", rc); + else + printf("Published Buffer: %s to Topic %s\n",buf, MQTT_TOPIC); + + while(1) + { + client->yield(4000); + if(rsc != 0){ + printf("rsc = %d\n",rsc); + break; + } + else rsc = 0; + } + if(rsc == 2001 ){ + rsc = 0; + return 0; + } + else if(rsc == 2002){ + rsc = 0; + return -1; + } + return 0; +} + +int delete_sub (MQTTSN::Client<MQTTSNUDP, Countdown> *client, MQTTSNUDP* ipstack, MQTTSN_topicid& topicid) +{ + Request R1; + //Create to parameter URI cse01/AE01/cont_monitor01 + sprintf(URI,"/%s/%s/sub_monitor01",CSE_ID, c_aei); + R1.To = URI; + R1.From = aei; + R1.Request_Identifier = "deleteSub01"; + R1.Operation = 4; + buf = Delete_Req(R1); + //PUBLISH + if ((rc = publish(client, ipstack, topicid)) != 0) + printf("rc from MQTT Publish is %d\n", rc); + else + printf("Published Buffer: %s to Topic %s\n",buf, MQTT_TOPIC); + while(1) + { + client->yield(4000); + if(rsc != 0){ + printf("rsc = %d\n",rsc); + break; + } + else rsc = 0; + } + return rsc; +} \ No newline at end of file
diff -r e544557919d9 -r 8c473836feba X_NUCLEO_IDW01M1v2.lib --- a/X_NUCLEO_IDW01M1v2.lib Mon Dec 10 08:06:33 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://developer.mbed.org/teams/ST/code/X_NUCLEO_IDW01M1v2/#c8697141ce44
diff -r e544557919d9 -r 8c473836feba container.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/container.h Tue Aug 06 05:32:43 2019 +0000 @@ -0,0 +1,79 @@ + +int Container(MQTTSN::Client<MQTTSNUDP, Countdown> *client, MQTTSNUDP* ipstack, MQTTSN_topicid& topicid) +{ + //Create Container + //Mendatory resources: Operation, To, From, Request Identifier ,Resource Type, Content + //Resource Specific Attributes [M]: App-ID, requestReachability, supportedReleaseVersions + // [0]: PointofAccess, ResourceName + + //struct Mendatory m1; (to, fr, rqi, ty, op) + Request R1; + //Create to parameter URI cse01/AE01 + sprintf(URI,"/%s/%s",CSE_ID, c_aei); + R1.To = URI; + R1.From = aei; + R1.Request_Identifier = "createCont01"; + R1.Resource_Type = 3; + R1.Operation = 1; + + //struct CreateAE CAE; + CAE.resourceName = "cont_monitor01"; + lcl = false; + //buffer = Create_Cnt(); + buf = Create_Req(R1); + //buf[0] = '\0'; + + //PUBLISH + if ((rc = publish(client, ipstack, topicid)) != 0) + printf("rc from MQTT Publish is %d\n", rc); + else + printf("Published Buffer: %s to Topic %s\n",buf, MQTT_TOPIC); + + while(1) + { + client->yield(4000); + + if(rsc != 0){ + //reg_resp = true; + printf("rsc = %d\n",rsc); + break; + } + else rsc = 0; + } + if(rsc == 2001 ){ + rsc = 0; + return 0; + } + else if(rsc == 2002){ + rsc = 0; + return -1; + } + return 0; +} + +int delete_cnt (MQTTSN::Client<MQTTSNUDP, Countdown> *client, MQTTSNUDP* ipstack, MQTTSN_topicid& topicid) +{ + Request R1; + //Create to parameter URI cse01/AE01/cont_monitor01 + sprintf(URI,"/%s/%s/cont_monitor01",CSE_ID, c_aei); + R1.To = URI; + R1.From = aei; + R1.Request_Identifier = "deleteCont01"; + R1.Operation = 4; + buf = Delete_Req(R1); + //PUBLISH + if ((rc = publish(client, ipstack, topicid)) != 0) + printf("rc from MQTT Publish is %d\n", rc); + else + printf("Published Buffer: %s to Topic %s\n",buf, MQTT_TOPIC); + while(1) + { + client->yield(4000); + if(rsc != 0){ + printf("rsc = %d\n",rsc); + break; + } + else rsc = 0; + } + return rsc; +} \ No newline at end of file
diff -r e544557919d9 -r 8c473836feba contentInstance.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/contentInstance.h Tue Aug 06 05:32:43 2019 +0000 @@ -0,0 +1,59 @@ + +int contentInstance(MQTTSN::Client<MQTTSNUDP, Countdown> *client, MQTTSNUDP* ipstack, MQTTSN_topicid& topicid) +{ + //Create Container + //Mendatory resources: Operation, To, From, Request Identifier ,Resource Type, Content + //Resource Specific Attributes [M]: App-ID, requestReachability, supportedReleaseVersions + // [0]: PointofAccess, ResourceName + + //struct Mendatory m1; (to, fr, rqi, ty, op) + Request R1; + R1.To = CSE_ID; + R1.From = aei; + R1.Request_Identifier = "createCin01"; + R1.Resource_Type = 4; + R1.Operation = 1; + + //struct CreateAE CAE; + CIN.resourceName = "cin_monitor01"; + CIN.contentInfo = "text/plains:0"; + CIN.content = "25"; + //buffer = Create_Cnt(); + lcl = false; + buf = Create_Req(R1); + //buf[0] = '\0'; + + //PUBLISH + if ((rc = publish(client, ipstack, topicid)) != 0) + printf("rc from MQTT Publish is %d\n", rc); + else + printf("Published Buffer: %s to Topic %s\n",buf, MQTT_TOPIC); + + while(1) + { + client->yield(4000); + /*if(isMessageArrived) { + isMessageArrived = false; + // Just print it out here. + //printf("\r\nMessage arrived:\r\n%s\r\n", messageBuffer); + //process_msg(); + //free(messageBuffer); + } */ + if(rsc != 0){ + //reg_resp = true; + printf("rsc = %d\n",rsc); + break; + } + else rsc = 0; + } + if(rsc == 2001 ){ + rsc = 0; + return 0; + } + else if(rsc == 2002){ + rsc = 0; + return -1; + } + //wait for response + return 0; +}
diff -r e544557919d9 -r 8c473836feba easy-connect.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/easy-connect.lib Tue Aug 06 05:32:43 2019 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/teams/OneM2M/code/easy-connect123/#08b6c2bb7a44
diff -r e544557919d9 -r 8c473836feba mbed-os.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Tue Aug 06 05:32:43 2019 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#2fd0c5cfbd83fce62da6308f9d64c0ab64e1f0d6
diff -r e544557919d9 -r 8c473836feba mbed.bld --- a/mbed.bld Mon Dec 10 08:06:33 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/2e9cc70d1897 \ No newline at end of file
diff -r e544557919d9 -r 8c473836feba mbed_app.json --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed_app.json Tue Aug 06 05:32:43 2019 +0000 @@ -0,0 +1,56 @@ +{ + "macros": [ + ], + "config": { + "main-stack-size": { + "value": 8192 + }, + "network-interface":{ + "help": "options are ETHERNET,WIFI_IDW0XX1, WIFI_ESP8266, WIFI_ODIN, WIFI_RTW, MESH_LOWPAN_ND, MESH_THREAD, CELLULAR_ONBOARD", + "value": "WIFI_ESP8266" + }, + "mesh_radio_type": { + "help": "options are ATMEL, MCR20", + "value": "ATMEL" + }, + "esp8266-tx": { + "help": "Pin used as TX (connects to ESP8266 RX)", + "value": "D8" + }, + "esp8266-rx": { + "help": "Pin used as RX (connects to ESP8266 TX)", + "value": "D2" + }, + "wifi-ssid": { + "value": "\"KICS-IOT-DHCP\"" + }, + "wifi-password": { + "value": "\"AAAFFFBBBC\"" + }, + "esp8266-debug": { + "value": false + } + }, + "target_overrides": { + "*": { + "target.features_add": ["NANOSTACK", "LOWPAN_ROUTER", "COMMON_PAL"], + "mbed-mesh-api.6lowpan-nd-channel-page": 0, + "mbed-mesh-api.6lowpan-nd-channel": 12, + "mbed-trace.enable": 0, + "platform.stdio-baud-rate": 9600, + "platform.stdio-convert-newlines": false + }, + "HEXIWEAR": { + "esp8266-tx": "PTD3", + "esp8266-rx": "PTD2" + }, + "NUCLEO_L476RG": { + "esp8266-tx": "D8", + "esp8266-rx": "D2" + }, + "NUCLEO_F411RE": { + "esp8266-tx": "D8", + "esp8266-rx": "D2" + } + } +}
diff -r e544557919d9 -r 8c473836feba registration.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/registration.h Tue Aug 06 05:32:43 2019 +0000 @@ -0,0 +1,64 @@ + +int Registration(MQTTSN::Client<MQTTSNUDP, Countdown> *client, MQTTSNUDP* ipstack, MQTTSN_topicid& topicid) +{ + //Create AE + //Mendatory resources: Operation, To, From, Request Identifier ,Resource Type, Content + //Resource Specific Attributes [M]: App-ID, requestReachability, supportedReleaseVersions + // [0]: PointofAccess, ResourceName + Request R1; + AE_ID = "S"; //Initial registration + //struct Mendatory m1; + R1.To = CSE_ID; + R1.From = AE_ID; + R1.Request_Identifier = "createAE11"; + R1.Resource_Type = 2; + R1.Operation = 1; + + //struct CreateAE CAE; + CAE.resourceName = MQTT_CLIENT_ID; + CAE.requestReachability = true; + CAE.App_ID = "A01.com.sensor011"; + //buffer = Create_AE(); + lcl = false; + buf = Create_Req(R1); + printf("string in buf is %s\n",buf); + //buf[0] = '\0'; + + + //PUBLISH + + if ((rc = publish(client, ipstack, topicid)) != 0) + printf("rc from MQTT Publish is %d\n", rc); + else + printf("Published Buffer: %s to Topic %s\n",buf, MQTT_TOPIC); + + while(1) + { + + client->yield(4000); + /*if(isMessageArrived) { + isMessageArrived = false; + // Just print it out here. + //printf("\r\nMessage arrived:\r\n%s\r\n", messageBuffer); + //process_msg(); + + //free(messageBuffer); + } */ + if(rsc != 0){ + //reg_resp = true; + printf("rsc = %d\n",rsc); + break; + } + else rsc = 0; + } + if(rsc == 2001 ){ + rsc = 0; + return 0; + } + else if(rsc == 2002){ + rsc = 0; + return -1; + } + //wait for response + return 0; +}
diff -r e544557919d9 -r 8c473836feba sensor.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sensor.txt Tue Aug 06 05:32:43 2019 +0000 @@ -0,0 +1,305 @@ +#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; +}