void testPayload() { // test for both buffer and dynamic CoapPDU *pdu = NULL; uint8_t *buffer[32]; for(int i=0; i<4; i++) { switch(i) { case 0: pdu = new CoapPDU((uint8_t*)buffer,32,0); break; case 1: pdu->reset(); break; case 2: pdu = new CoapPDU(); break; case 3: pdu->reset(); break; } pdu->setType(CoapPDU::COAP_CONFIRMABLE); pdu->printBin(); pdu->setCode(CoapPDU::COAP_GET); pdu->setVersion(1); pdu->printBin(); pdu->setMessageID(0x1234); pdu->setURI((char*)"test",4); pdu->printBin(); CU_ASSERT_FATAL(pdu->setPayload(NULL,4)==1); CU_ASSERT_FATAL(pdu->setPayload((uint8_t*)"test",0)==1); pdu->setPayload((uint8_t*)"\1\2\3",3); pdu->printBin(); CU_ASSERT_EQUAL_FATAL(pdu->getPayloadLength(),3); CU_ASSERT_NSTRING_EQUAL_FATAL(pdu->getPDUPointer(),payloadTestPDUA,pdu->getPDULength()); CU_ASSERT_NSTRING_EQUAL_FATAL(pdu->getPayloadPointer(),"\1\2\3",pdu->getPayloadLength()); DBG("Trying to increase payload size"); pdu->setPayload((uint8_t*)"\4\3\2\1",4); pdu->printBin(); CU_ASSERT_EQUAL_FATAL(pdu->getPayloadLength(),4); CU_ASSERT_NSTRING_EQUAL_FATAL(pdu->getPDUPointer(),payloadTestPDUB,pdu->getPDULength()); CU_ASSERT_NSTRING_EQUAL_FATAL(pdu->getPayloadPointer(),"\4\3\2\1",pdu->getPayloadLength()); DBG("Trying to reduce payload size"); pdu->setPayload((uint8_t*)"\1\2",2); pdu->printBin(); CU_ASSERT_EQUAL_FATAL(pdu->getPayloadLength(),2); CU_ASSERT_NSTRING_EQUAL_FATAL(pdu->getPDUPointer(),payloadTestPDUC,pdu->getPDULength()); CU_ASSERT_NSTRING_EQUAL_FATAL(pdu->getPayloadPointer(),"\1\2",pdu->getPayloadLength()); if(i%2) { DBG("%d DELETE",i); delete pdu; } } }
void testOptionInsertion(void) { CoapPDU *pdu = NULL; uint8_t *buffer[64]; for(int constructorType=0; constructorType<4; constructorType++) { DBG("New iteration: constructorType: %d",constructorType); switch(constructorType) { case 0: pdu = new CoapPDU((uint8_t*)buffer,64,0); break; case 1: pdu->reset(); break; case 2: pdu = new CoapPDU(); break; case 3: pdu->reset(); break; } pdu->setVersion(1); pdu->setType(CoapPDU::COAP_CONFIRMABLE); pdu->setCode(CoapPDU::COAP_CHANGED); CU_ASSERT_NSTRING_EQUAL_FATAL(optionInsertionTestA,pdu->getPDUPointer(),pdu->getPDULength()); pdu->addOption(11,3,(uint8_t*)"\x55\x55\x55"); CU_ASSERT_NSTRING_EQUAL_FATAL(optionInsertionTestB,pdu->getPDUPointer(),pdu->getPDULength()); pdu->addOption(11,3,(uint8_t*)"\xff\xff\xff"); CU_ASSERT_NSTRING_EQUAL_FATAL(optionInsertionTestC,pdu->getPDUPointer(),pdu->getPDULength()); pdu->addOption(7,3,(uint8_t*)"\xf7\xf7\xf7"); CU_ASSERT_NSTRING_EQUAL_FATAL(optionInsertionTestD,pdu->getPDUPointer(),pdu->getPDULength()); pdu->addOption(200,3,(uint8_t*)"\x01\x02\x03"); CU_ASSERT_NSTRING_EQUAL_FATAL(optionInsertionTestE,pdu->getPDUPointer(),pdu->getPDULength()); pdu->addOption(190,3,(uint8_t*)"\x03\x02\x01"); CU_ASSERT_NSTRING_EQUAL_FATAL(optionInsertionTestF,pdu->getPDUPointer(),pdu->getPDULength()); pdu->addOption(300,3,(uint8_t*)"\x01\x02\x03"); CU_ASSERT_NSTRING_EQUAL_FATAL(optionInsertionTestG,pdu->getPDUPointer(),pdu->getPDULength()); pdu->addOption(195,3,(uint8_t*)"\x03\x02\x01"); CU_ASSERT_NSTRING_EQUAL_FATAL(optionInsertionTestH,pdu->getPDUPointer(),pdu->getPDULength()); pdu->addOption(1950,3,(uint8_t*)"\x03\x02\x01"); CU_ASSERT_NSTRING_EQUAL_FATAL(optionInsertionTestI,pdu->getPDUPointer(),pdu->getPDULength()); if(constructorType%2) { DBG("%d DELETE",constructorType); delete pdu; } } }
void testTokenInsertion(void) { CoapPDU *pdu = NULL; uint8_t *buffer[64]; for(int constructorType=0; constructorType<4; constructorType++) { DBG("New iteration: constructorType: %d",constructorType); switch(constructorType) { case 0: pdu = new CoapPDU((uint8_t*)buffer,64,0); break; case 1: pdu->reset(); break; case 2: pdu = new CoapPDU(); break; case 3: pdu->reset(); break; } pdu->setType(CoapPDU::COAP_CONFIRMABLE); pdu->setCode(CoapPDU::COAP_CHANGED); pdu->setVersion(2); pdu->setToken((uint8_t*)"\3\2\1\0",4); CU_ASSERT_NSTRING_EQUAL_FATAL(tokenInsertionA,pdu->getPDUPointer(),pdu->getPDULength()); pdu->setToken((uint8_t*)"\4\3\2\1\0",5); CU_ASSERT_NSTRING_EQUAL_FATAL(tokenInsertionB,pdu->getPDUPointer(),pdu->getPDULength()); pdu->setToken((uint8_t*)"\7\6\5\4\3\2\1",8); CU_ASSERT_NSTRING_EQUAL_FATAL(tokenInsertionC,pdu->getPDUPointer(),pdu->getPDULength()); pdu->setToken((uint8_t*)"\4\3\2\1\0",5); CU_ASSERT_NSTRING_EQUAL_FATAL(tokenInsertionB,pdu->getPDUPointer(),pdu->getPDULength()); pdu->setToken((uint8_t*)"\3\2\1\0",4); CU_ASSERT_NSTRING_EQUAL_FATAL(tokenInsertionA,pdu->getPDUPointer(),pdu->getPDULength()); CU_ASSERT_FATAL(pdu->setToken(NULL,4)==1); CU_ASSERT_FATAL(pdu->setToken((uint8_t*)"a",0)==1); if(constructorType%2) { DBG("%d DELETE",constructorType); delete pdu; } } }
int main() { DBG_INIT(); DBG_SET_SPEED(115200); DBG_SET_NEWLINE("\r\n"); // structure for getting address of incoming packets sockaddr_in fromAddr; socklen_t fromAddrLen = sizeof(struct sockaddr_in); memset(&fromAddr,0x00,fromAddrLen); // connect to cellular network HuaweiUSBCDMAModem modem(NC, true); modem.connect(APN,APN_USERNAME,APN_PASSWORD); // setup socket to remote server int sockfd = NULL; // if(!connectToSocketUDP("109.74.199.96", 5683, &sockfd)) { if(!connectToSocketUDP("123.57.235.186", 5683, &sockfd)) { DBG("Error connecting to socket"); fail(1); } DBG("\"Connected\" to UDP socket"); // construct CoAP packet CoapPDU *pdu = new CoapPDU(); pdu->setVersion(1); pdu->setType(CoapPDU::COAP_CONFIRMABLE); pdu->setCode(CoapPDU::COAP_POST); pdu->setToken((uint8_t*)"\3\2\1\1",4); pdu->setURI((char*)"register",8); #define BUFLEN 128 short messageID = 0; char buffer[BUFLEN]; MbedJSONValue json; json["devType"] = "T1"; json["firmwareNo"] = "F1"; json["softVerNo"] = "S4"; json["userDevType"] = "UT1"; json["userSoftVerNo"] = "US2"; json["mobile"] = "15811112222"; json["userHardVerNo"] = "UH1"; json["hardVerNo"] = "H1"; json["devUniqId"] = "q1w2e3r4t5"; string jsonStr = json.serialize(); printf("serialized content = %s\r\n" , jsonStr.c_str()); char *payload = (char *)jsonStr.c_str(); pdu->setPayload((uint8_t*)payload,strlen(payload)); int num = 3; // send packets to server while(num--) { // set PDU parameters pdu->setMessageID(messageID++); // send packet int ret = send(sockfd,pdu->getPDUPointer(),pdu->getPDULength(),0); if(ret!=pdu->getPDULength()) { INFO("Error sending packet."); perror(NULL); fail(2); } INFO("Packet sent"); // receive response ret = recvfrom(sockfd,&buffer,BUFLEN,0,(struct sockaddr *)&fromAddr,&fromAddrLen); if(ret==-1) { INFO("Error receiving data"); fail(3); } INFO("Received %d bytes from %s:%d",ret,inet_ntoa(fromAddr.sin_addr),ntohs(fromAddr.sin_port)); if(ret>BUFLEN) { INFO("PDU too large to fit in pre-allocated buffer"); continue; } // reuse the below coap object CoapPDU *recvPDU = new CoapPDU((uint8_t*)buffer,BUFLEN,BUFLEN); // validate packet recvPDU->setPDULength(ret); if(recvPDU->validate()!=1) { INFO("Malformed CoAP packet"); fail(4); } INFO("Valid CoAP PDU received"); recvPDU->printHuman(); Thread::wait(5000); } modem.disconnect(); return true; }