-
-
Save Pwera/7f3bde1064811e698609772be41b3f9b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CoapClient { | |
struct mg_mgr mgr; | |
struct mg_connection *nc; | |
std::string address{"udp://127.0.0.1:5683"}; | |
public: | |
static int s_time_to_exit; | |
CoapClient() { | |
std::cout << "[Client] Using %s as CoAP server" << address << std::endl; | |
mg_mgr_init(&mgr, 0); | |
nc = mg_connect(&mgr, address.c_str(), coap_handler); | |
} | |
static void coap_handler(struct mg_connection *nc, int ev, void *p) { | |
switch (ev) { | |
case MG_EV_CONNECT: { | |
struct mg_coap_message cm; | |
uint32_t res; | |
memset(&cm, 0, sizeof(cm)); | |
cm.msg_id = 1; | |
cm.msg_type = MG_COAP_MSG_CON; | |
std::cout << "[Client] Sending CON..." << std::endl; | |
res = mg_coap_send_message(nc, &cm); | |
if (res == 0) { | |
std::cout << "[Client] Sent CON with msg_id = " << cm.msg_id << std::endl; | |
} else { | |
std::cout << "[Client] Error: " << res << std::endl; | |
s_time_to_exit = 1; | |
} | |
break; | |
} | |
case MG_EV_COAP_ACK: | |
case MG_EV_COAP_RST: { | |
struct mg_coap_message *cm = (struct mg_coap_message *) p; | |
std::cout << "[Client] ACK/RST for message with msg_id =" << cm->msg_id << " received"<<std::endl; | |
s_time_to_exit = 1; | |
break; | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment