if(0 != read_json_str_value(item, "name", model_name, sizeof(model_name))){
break;
}
item_array = cJSON_GetObjectItem(item,"serialNeData");
if( item_array == NULL)
{
}
serial_len = cJSON_GetArraySize(item_array);
for(i = 0; i < serial_len; i++)
{
arr_item = cJSON_GetArrayItem(item_array,i);
if(arr_item == NULL)
{
}
snprintf( serial_data[i],sizeof(serial_data[i]),"%s",arr_item->valuestring );
}
item_array = cJSON_GetObjectItem(item,"sideNeData");
if( item_array == NULL)
{
}
side_len = cJSON_GetArraySize(item_array);
for(i = 0; i < side_len; i++)
{
arr_item = cJSON_GetArrayItem(item_array,i);
if(arr_item == NULL)
{
}
snprintf( side_data[i],sizeof(side_data[i]),"%s",arr_item->valuestring );
}
[root@localhost /]# cat myjson.c
#include
#include
#include
#include "v_json.h"
int main()
{
char *text = NULL;
char body[1024] = {0};
char body2[1024] = {0};
char cmd[1024] = {0};
cJSON * root = cJSON_CreateObject();
// cJSON * item = cJSON_CreateObject();
int i = 0;
// cJSON *array =cJSON_CreateArray();
// cJSON *array1 =cJSON_CreateArray();
// cJSON *obj = NULL;
int j = 0;
cJSON_AddItemToObject(root, "username", cJSON_CreateString("1111111"));
cJSON_AddItemToObject(root, "password", cJSON_CreateString("2222222"));
//cJSON_AddItemToObject(root, "data", item);
//cJSON_AddItemToObject(item, "name", cJSON_CreateString("hahahahahahhahah"));
//cJSON_AddItemToObject(item,"serialNeData",array);
//
//cJSON_AddItemToArray(array,cJSON_CreateString("FW"));
//cJSON_AddItemToArray(array,cJSON_CreateString("WAF"));
//cJSON_AddItemToArray(array1,cJSON_CreateString("IDS"));
// cJSON_AddItemToObject(root, "vmName", cJSON_CreateString("d"));
// cJSON_AddItemToObject(root, "passive", cJSON_CreateString("0"));
text = cJSON_Print(root);
strcpy(body, text);
if(root != NULL)
cJSON_Delete(root);
if(text != NULL)
free(text);
for(i=0; i
if(body[i] != '\"')
{
body2[j] = body[i];
}
else
{
strcat(body2,"\\");
strcat(body2,"\"");
j++;
}
j++;
}
memset(cmd,0,sizeof(cmd));
snprintf(cmd,sizeof(cmd),"/software/bin/vetrix_mqtt tcp://%s:1883 %s \"%s\"", "192.168.27.245", "1ac97d25-abbe-44fd-825b-2d9a60c04587", body2);
printf("%s\n",cmd);
system(cmd);
return 0;
}
[root@localhost /]#
[root@localhost /]# cat b.c
#include
#include
#include
#include "cJSON.h"
int main()
{
char *text = NULL;
char body[1024] = {0};
char cmd[1024] = {0};
cJSON * root = cJSON_CreateObject();
cJSON_AddItemToObject(root, "uuid", cJSON_CreateString("11111111111111111111111111111"));
cJSON_AddItemToObject(root, "username", cJSON_CreateString("2222222222222222222222222222"));
cJSON_AddItemToObject(root, "password", cJSON_CreateString("33333333333333333333333333333"));
cJSON_AddItemToObject(root, "type", cJSON_CreateString("market"));
cJSON_AddItemToObject(root, "action", cJSON_CreateString("update"));
text = cJSON_Print(root);
strcpy(body, text);
if(root != NULL)
cJSON_Delete(root);
if(text != NULL)
free(text);
memset(cmd,0,sizeof(cmd));
snprintf(cmd,sizeof(cmd),"/software/bin/vetrix_mqtt tcp://%s:1883 %s \"%s\"", "192.168.27.245", "06ab1d92-acc7-45d6-adfa-296ef39b0370", body);
system(cmd);
return 0;
}
[root@localhost /]# ls
1 cJSON.h docker-cli-dev.tar go1.10.8.linux-amd64.tar.gz home libzmq-master opt run %{_unitdir}
a.c config docker-cli-native.tar go1.11.5.linux-amd64.tar.gz igb.ko libzmq-master.zip paho.mqtt.c-master sbin usr
b.c cpu_used.sh docker-dev.tar go1.9.1.linux-amd64.tar.gz ipmitool-1.8.11 lost+found paho.mqtt.c-master.zip srv var
bin dev Eclipse-Paho-MQTT-C-1.3.1-Linux go1.9.2.linux-amd64.tar.gz ipmitool-1.8.11.tar.gz media proc sys vetrix_mqtt
boot disk Eclipse-Paho-MQTT-C-1.3.1-Linux.tar.gz gocache lib mnt rabbit time.c
cJSON.c docker etc haha lib64 mymqtt root tmp
[root@localhost /]# gcc b.c cJSON.c -o haha -lm
[root@localhost /]#
[root@localhost /]# cat a.c
#include
#include
#include
#include
int agent_vetrix_report_vcloud_mqtt(char *mqtt_ip, char *topic, char *message)
{
int ret = 0;
//声明一个MQTTClient
MQTTClient client;
//初始化MQTT Client选项
MQTTClient_connectOptions conn = MQTTClient_connectOptions_initializer;
MQTTClient_deliveryToken dt;
MQTTClient_message pub_msg = MQTTClient_message_initializer;
ret = MQTTClient_create(&client, mqtt_ip, "vetrix_pub", MQTTCLIENT_PERSISTENCE_NONE, NULL);
if(ret != 0)
{
return -3;
}
//connect config init
conn.keepAliveInterval = 20;
conn.cleansession = 1;
conn.username = "admin";
conn.password = "public";
ret = MQTTClient_connect(client, &conn);
if (ret != 0)
{
MQTTClient_destroy(&client);
return -2;
}
pub_msg.payload = message;
pub_msg.payloadlen = (int)strlen(message);
pub_msg.retained = 0;
pub_msg.qos = 0;
ret = MQTTClient_publish(client, topic, pub_msg.payloadlen, pub_msg.payload, pub_msg.qos, pub_msg.retained, NULL);
if(ret != 0)
{
return -5;
}
ret = MQTTClient_waitForCompletion(client, dt, 100);
if(ret != 0){
return -1;
}
MQTTClient_disconnect(client, 10000);
MQTTClient_destroy(&client);
return ret;
}
int main(int argc, char* argv[])
{
agent_vetrix_report_vcloud_mqtt(argv[1], argv[2], argv[3]);
return 0;
}
[root@localhost /]# gcc a.c -o hehe
/tmp/ccmul0t1.o:在函数‘agent_vetrix_report_vcloud_mqtt’中:
a.c:(.text+0x18a):对‘MQTTClient_create’未定义的引用
a.c:(.text+0x1d7):对‘MQTTClient_connect’未定义的引用
a.c:(.text+0x1ec):对‘MQTTClient_destroy’未定义的引用
a.c:(.text+0x266):对‘MQTTClient_publish’未定义的引用
a.c:(.text+0x290):对‘MQTTClient_waitForCompletion’未定义的引用
a.c:(.text+0x2b1):对‘MQTTClient_disconnect’未定义的引用
a.c:(.text+0x2bd):对‘MQTTClient_destroy’未定义的引用
collect2: 错误:ld 返回 1
[root@localhost /]# gcc a.c -o hehe -lpaho-mqtt3cs
[root@localhost /]#