Skip to content

Commit

Permalink
log ready
Browse files Browse the repository at this point in the history
  • Loading branch information
huxinbang committed Jan 23, 2018
1 parent 6ff5ae9 commit f7e3f95
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 38 deletions.
2 changes: 1 addition & 1 deletion include/common/tc_iot_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "tc_iot_inc.h"


int tc_iot_mqtt_client_construct(tc_iot_mqtt_client* p_mqtt_client,
tc_iot_mqtt_client_config* p_client_config);
void tc_iot_mqtt_client_destroy(tc_iot_mqtt_client* p_mqtt_client);
Expand All @@ -17,5 +18,4 @@ int tc_iot_mqtt_client_subscribe(tc_iot_mqtt_client* p_mqtt_client,
int tc_iot_mqtt_client_unsubscribe(tc_iot_mqtt_client* p_mqtt_client,
const char* topic_filter);
int tc_iot_mqtt_client_disconnect(tc_iot_mqtt_client* p_mqtt_client);

#endif /* end of include guard */
19 changes: 19 additions & 0 deletions include/tc_iot_export.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@

#include "tc_iot_inc.h"

void tc_iot_set_log_level(int log_level);
int tc_iot_get_log_level();
bool tc_iot_log_level_enabled(int log_level);

int tc_iot_mqtt_client_construct(tc_iot_mqtt_client* p_mqtt_client,
tc_iot_mqtt_client_config* p_client_config);
void tc_iot_mqtt_client_destroy(tc_iot_mqtt_client* p_mqtt_client);
char tc_iot_mqtt_client_is_connected(tc_iot_mqtt_client* p_mqtt_client);
int tc_iot_mqtt_client_yield(tc_iot_mqtt_client* p_mqtt_client, int timeout_ms);
int tc_iot_mqtt_client_publish(tc_iot_mqtt_client* p_mqtt_client,
const char* topic, tc_iot_mqtt_message* msg);
int tc_iot_mqtt_client_subscribe(tc_iot_mqtt_client* p_mqtt_client,
const char* topic_filter,
tc_iot_mqtt_qos_e qos,
message_handler msg_handler);
int tc_iot_mqtt_client_unsubscribe(tc_iot_mqtt_client* p_mqtt_client,
const char* topic_filter);
int tc_iot_mqtt_client_disconnect(tc_iot_mqtt_client* p_mqtt_client);

int tc_iot_shadow_construct(tc_iot_shadow_client *,
tc_iot_shadow_config *p_config);
void tc_iot_shadow_destroy(tc_iot_shadow_client *p_shadow_client);
Expand Down
13 changes: 3 additions & 10 deletions samples/linux/mqtt/demo_auth_mqtt_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,15 @@ tc_iot_mqtt_client_config g_client_config = {

int main(int argc, char** argv) {
int ret = 0;
tc_iot_set_log_level(TC_IOT_LOG_DEBUG);
LOG_DEBUG("debug logging");
LOG_INFO("info logging");
LOG_WARN("warn logging");
LOG_ERROR("error logging");
LOG_CRIT("crit logging");

printf("requesting username and password for mqtt.\n");
tc_iot_hal_printf("requesting username and password for mqtt.\n");
ret = http_refresh_auth_token(
TC_IOT_CONFIG_AUTH_API_URL, NULL,
&g_client_config.device_info);
if (ret != TC_IOT_SUCCESS) {
printf("refresh token failed, visit: https://github.com/tencentyun/tencent-cloud-iotsuite-embedded-c/wiki/trouble_shooting#%d\n.", ret);
tc_iot_hal_printf("refresh token failed, visit: https://github.com/tencentyun/tencent-cloud-iotsuite-embedded-c/wiki/trouble_shooting#%d\n.", ret);
return 0;
}
printf("request username and password for mqtt success.\n");
tc_iot_hal_printf("request username and password for mqtt success.\n");

// 连接mqtt服务器
run_simple_mqtt_client(&g_client_config);
Expand Down
14 changes: 7 additions & 7 deletions samples/linux/mqtt/simple_mqtt_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@

void _on_message_received(tc_iot_message_data* md) {
tc_iot_mqtt_message* message = md->message;
printf("[s->c] %.*s\n", (int)message->payloadlen, (char*)message->payload);
tc_iot_hal_printf("[s->c] %.*s\n", (int)message->payloadlen, (char*)message->payload);
}

static volatile int stop = 0;

void sig_handler(int sig) {
if (sig == SIGINT) {
printf("SIGINT received, going down.\n");
tc_iot_hal_printf("SIGINT received, going down.\n");
stop = 1;
} else if (sig == SIGTERM) {
printf("SIGTERM received, going down.\n");
tc_iot_hal_printf("SIGTERM received, going down.\n");
stop = 1;
} else {
printf("signal received:%d\n", sig);
tc_iot_hal_printf("signal received:%d\n", sig);
}
}

Expand Down Expand Up @@ -46,14 +46,14 @@ int run_simple_mqtt_client(tc_iot_mqtt_client_config* p_client_config) {
pubmsg.qos = QOS1;
pubmsg.retained = 0;
pubmsg.dup = 0;
printf("[c->s] shadow_get\n");
tc_iot_hal_printf("[c->s] shadow_get\n");
ret = tc_iot_mqtt_client_publish(p_client, pub_topic, &pubmsg);
if (TC_IOT_SUCCESS != ret) {
if (ret != TC_IOT_MQTT_RECONNECT_IN_PROGRESS) {
printf("publish failed: %d, not reconnect, exiting now\n", ret);
tc_iot_hal_printf("publish failed: %d, not reconnect, exiting now\n", ret);
break;
} else {
printf("publish failed client trying to reconnect.\n");
tc_iot_hal_printf("publish failed client trying to reconnect.\n");
}
}

Expand Down
38 changes: 18 additions & 20 deletions samples/linux/shadow/demo_shadow.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ static volatile int stop = 0;

void sig_handler(int sig) {
if (sig == SIGINT) {
printf("SIGINT received, going down.\n");
tc_iot_hal_printf("SIGINT received, going down.\n");
stop = 1;
} else if (sig == SIGTERM) {
printf("SIGTERM received, going down.\n");
tc_iot_hal_printf("SIGTERM received, going down.\n");
stop = 1;
} else {
printf("signal received:%d\n", sig);
tc_iot_hal_printf("signal received:%d\n", sig);
}
}

void _on_message_received(tc_iot_message_data* md) {
tc_iot_mqtt_message* message = md->message;
/* printf("->%.*s\t", md->topicName->lenstring.len, */
/* tc_iot_hal_printf("->%.*s\t", md->topicName->lenstring.len, */
/* md->topicName->lenstring.data); */
printf("[s->c] %.*s\n", (int)message->payloadlen, (char*)message->payload);
tc_iot_hal_printf("[s->c] %.*s\n", (int)message->payloadlen, (char*)message->payload);
}

tc_iot_shadow_config g_client_config = {
Expand Down Expand Up @@ -53,51 +53,49 @@ int main(int argc, char** argv) {
tc_iot_shadow_client* p_shadow_client = &client;
int ret = 0;

tc_iot_set_log_level(TC_IOT_LOG_DEBUG);

printf("requesting username and password for mqtt.\n");
tc_iot_hal_printf("requesting username and password for mqtt.\n");
ret = http_refresh_auth_token(
TC_IOT_CONFIG_AUTH_API_URL, NULL,
&g_client_config.mqtt_client_config.device_info);
if (ret != TC_IOT_SUCCESS) {
printf("refresh token failed, visit: https://github.com/tencentyun/tencent-cloud-iotsuite-embedded-c/wiki/trouble_shooting#%d\n.", ret);
tc_iot_hal_printf("refresh token failed, visit: https://github.com/tencentyun/tencent-cloud-iotsuite-embedded-c/wiki/trouble_shooting#%d\n.", ret);
return 0;
}
printf("request username and password for mqtt success.\n");
tc_iot_hal_printf("request username and password for mqtt success.\n");

printf("constructing mqtt shadow client.\n");
tc_iot_hal_printf("constructing mqtt shadow client.\n");
ret = tc_iot_shadow_construct(p_shadow_client, &g_client_config);
if (ret != TC_IOT_SUCCESS) {
printf("construct shadow failed, visit: https://github.com/tencentyun/tencent-cloud-iotsuite-embedded-c/wiki/trouble_shooting#%d\n.", ret);
tc_iot_hal_printf("construct shadow failed, visit: https://github.com/tencentyun/tencent-cloud-iotsuite-embedded-c/wiki/trouble_shooting#%d\n.", ret);
return 0;
}

printf("construct mqtt shadow client success.\n");
tc_iot_hal_printf("construct mqtt shadow client success.\n");
int timeout = TC_IOT_CONFIG_COMMAND_TIMEOUT_MS;
printf("yield waiting for server push.\n");
tc_iot_hal_printf("yield waiting for server push.\n");
tc_iot_shadow_yield(p_shadow_client, timeout);
printf("yield waiting for server finished.\n");
tc_iot_hal_printf("yield waiting for server finished.\n");

printf("[c->s] shadow_get\n");
tc_iot_hal_printf("[c->s] shadow_get\n");
tc_iot_shadow_get(p_shadow_client);
tc_iot_shadow_yield(p_shadow_client, timeout);

char* action_update =
"{\"method\":\"update\",\"state\":{\"reported\":{\"temperature\":1023,"
"\"switch\":1023},\"desired\":{\"temperature\":1024,\"switch\":1024}}}";
printf("[c->s] shadow_update\n");
tc_iot_hal_printf("[c->s] shadow_update\n");
tc_iot_shadow_update(p_shadow_client, action_update);
tc_iot_shadow_yield(p_shadow_client, timeout);

char* action_delete =
"{\"method\":\"delete\",\"state\":{\"reported\":{\"temperature\":null},"
"\"desired\":null}}";
printf("[c->s] shadow_delete\n");
tc_iot_hal_printf("[c->s] shadow_delete\n");
tc_iot_shadow_delete(p_shadow_client, action_delete);
tc_iot_shadow_yield(p_shadow_client, timeout);

printf("Stopping\n");
tc_iot_hal_printf("Stopping\n");
tc_iot_shadow_destroy(p_shadow_client);
printf("Exit success.\n");
tc_iot_hal_printf("Exit success.\n");
return 0;
}

0 comments on commit f7e3f95

Please sign in to comment.